Bug 35836

Summary: Try getting MIME type from filename for better attachment display
Product: [Applications] kmail Reporter: Jonathan Marten <jjm>
Component: generalAssignee: kdepim bugs <kdepim-bugs>
Status: RESOLVED FIXED    
Severity: wishlist CC: kollix
Priority: NOR    
Version: 1.3   
Target Milestone: ---   
Platform: unspecified   
OS: Other   
Latest Commit: Version Fixed In:
Attachments: Patch to try harder for attachment type
Email message demonstrating problems

Description Jonathan Marten 2001-12-06 10:08:14 UTC
(*** This bug was imported into bugs.kde.org ***)

Package:kmail
Version:1.3 (KDE 2.2)
Severity:wishlist

If a mail message contains attachments whose MIME type (taken from the
message part's Content-Type) is not known the attachment is displayed
with the "unknown" type icon.  In this case KMail could look at the
attachment filename and try to get a known MIME type from that then
display the appropriate icon.

This change is not just cosmetic but useful too - showing the correct
icon will indicate to the user the application that will (normally) be
used to open the attachment without having to click on the attachment
to find out.  I don't believe there is any security risk in doing
this: looking up the MIME type from the file name involves only
parsing not reading any local files.

Patch for KDE 2.2 (also applies to KDE 2.2.2):

-------------------------------------------------------------------
--- kdenetwork-2.2/kmail/kmmsgpart.cpp.orig     Wed Dec  5 15:34:29 2001
+++ kdenetwork-2.2/kmail/kmmsgpart.cpp  Thu Dec  6 09:31:27 2001
@@ -2184 +21813 @@
   QString fileName = KMimeType::mimeType(mimeType.isEmpty() ?
     (mType + "/" + mSubtype).lower() : mimeType.lower())->icon(QString()FALSE);
+
+  if (!fileName)
+  {
+    kdDebug(5006) << "KMMessagePart::iconName: unknown mimetype " <<
+           (mType + "/" + mSubtype) << " using filename " << 
+           this->fileName() << endl;
+    fileName = KMimeType::iconForURL("file:/tmp/"+this->fileName()0);
+  }
+
   fileName = KGlobal::instance()->iconLoader()->iconPath( fileName
     KIcon::Desktop );
-------------------------------------------------------------------

-- 
Jonathan Marten SCM Team Engineer          VSP Bracknell UK
jonathan.marten@uk.sun.com                  Sun Microsystems
Comment 1 Jonathan Marten 2001-12-12 15:12:21 UTC
Just noticed that the original patch doesn't display the correct icon
for attachments whose MIME type is unknown and where using the
attachment name doesn't yield a known MIME type either - it shows as a
folder.  This new patch only uses the file name to generate an icon if
it actually corresponds to a known type.

The additional modification to kmreaderwin.cpp makes another cosmetic
change to suppress the attachment 'comment' if it is the same as the
'label' (often happens with attachments from CDE's dtmail).  Otherwise
the same text is pointlessly shown twice (one clickable the other
not).

----------------------------------------------------------------------
--- kdenetwork-2.2/kmail/kmmsgpart.cpp.origWed Dec  5 15:34:29 2001
+++ kdenetwork-2.2/kmail/kmmsgpart.cppWed Dec 12 13:55:49 2001
@@ -2184 +21822 @@
   QString fileName = KMimeType::mimeType(mimeType.isEmpty() ?
     (mType + "/" + mSubtype).lower() : mimeType.lower())->icon(QString()FALSE);
+
+  if (fileName.isEmpty())
+  {
+    fileName = this->fileName();
+    if (fileName.isEmpty()) fileName = this->name();
+    if (!fileName.isEmpty())
+    {
+      kdDebug(5006) << "KMMessagePart::iconName: unknown mimetype " <<
+      (mType + "/" + mSubtype) << " using filename " << fileName << endl;
+      fileName = KMimeType::iconForURL("file:/tmp/"+fileName0);
+    }
+    else
+    {
+      kdDebug(5006) << "KMMessagePart::iconName: unknown mimetype " <<
+      (mType + "/" + mSubtype) << endl;
+    }
+  }
+
   fileName = KGlobal::instance()->iconLoader()->iconPath( fileName
     KIcon::Desktop );
--- kdenetwork-2.2/kmail/kmreaderwin.cpp.origTue Nov  6 14:15:47 2001
+++ kdenetwork-2.2/kmail/kmreaderwin.cppWed Dec 12 14:01:43 2001
@@ -10614 +10616 @@
   if (fileName.isEmpty()) fileName = aMsgPart->name();
   label = fileName;
+  if (label==comment) comment = "";
+
       /* HTMLize label */
   label.replace(QRegExp("\"") "&quot;");
@@ -11114 +11135 @@
   else
     iconName = aMsgPart->iconName();
+
   if (iconName.right(14)=="mime_empty.png")
   {
@@ -11164 +11195 @@
     iconName = aMsgPart->iconName();
   }
+
   mViewer->write("<table><tr><td><a href=\"" + href + "\"><img src=\"" +
  iconName + "\" border=\"0\">" + label +
----------------------------------------------------------------------

-- 
Jonathan Marten SCM Team Engineer          VSP Bracknell UK
jonathan.marten@uk.sun.com                  Sun Microsystems
Comment 2 Jonathan Marten 2005-04-26 18:10:30 UTC
Closed as fixed, but the problem is still present in KDE 3.4 (KMail 1.8).
Updated patch against KDE 3.4 sources attached, along with a test case message that demonstrates both problems.
Comment 3 Jonathan Marten 2005-04-26 18:11:20 UTC
Created attachment 10799 [details]
Patch to try harder for attachment type
Comment 4 Jonathan Marten 2005-04-26 18:12:27 UTC
Created attachment 10800 [details]
Email message demonstrating problems
Comment 5 Martin Koller 2006-10-28 19:58:54 UTC
Can confirm with KDE-3.5.5; it's also duplicate with    77898
Comment 6 Martin Koller 2006-10-28 19:59:36 UTC
*** Bug 77898 has been marked as a duplicate of this bug. ***
Comment 7 Jonathan Marten 2006-11-08 11:03:25 UTC
SVN commit 603223 by marten:

Get attachment icon from file name if not available from Content-Type

BUG: 35836


 M  +9 -0      branches/work/kdepim-3.5.5+/kmail/kmmsgpart.cpp  
 M  +1 -0      branches/work/kdepim-3.5.5+/kmail/objecttreeparser.cpp  


--- branches/work/kdepim-3.5.5+/kmail/kmmsgpart.cpp #603222:603223
@@ -386,8 +386,17 @@
 {
   QCString mimeType( mType + "/" + mSubtype );
   KPIM::kAsciiToLower( mimeType.data() );
+
   QString fileName =
     KMimeType::mimeType( mimeType )->icon( QString::null, false );
+  if ( fileName.isEmpty() )
+  { 
+    fileName = this->fileName(); 
+    if ( fileName.isEmpty() ) fileName = this->name(); 
+    if ( !fileName.isEmpty() ) 
+      fileName = KMimeType::findByURL( "file:/tmp/"+fileName, 0, true, true )->icon( QString::null, true );
+  } 
+
   fileName =
     KGlobal::instance()->iconLoader()->iconPath( fileName, KIcon::Desktop );
   return fileName;
--- branches/work/kdepim-3.5.5+/kmail/objecttreeparser.cpp #603222:603223
@@ -1734,6 +1734,7 @@
 
     QString comment = msgPart->contentDescription();
     comment = KMMessage::quoteHtmlChars( comment, true );
+    if ( label == comment ) comment = QString::null;
 
     QString fileName = mReader->writeMessagePartToTempFile( msgPart, partNum );
 
Comment 8 Jonathan Marten 2006-11-08 13:02:51 UTC
SVN commit 603260 by marten:

Correct encoding of file name before lookup, using a KURL
(Correction from Martin Koller)

CCBUG: 35836


 M  +6 -2      branches/work/kdepim-3.5.5+/kmail/kmmsgpart.cpp  


--- branches/work/kdepim-3.5.5+/kmail/kmmsgpart.cpp #603259:603260
@@ -393,8 +393,12 @@
   { 
     fileName = this->fileName(); 
     if ( fileName.isEmpty() ) fileName = this->name(); 
-    if ( !fileName.isEmpty() ) 
-      fileName = KMimeType::findByURL( "file:/tmp/"+fileName, 0, true, true )->icon( QString::null, true );
+    if ( !fileName.isEmpty() )
+    {
+      KURL u( "file:///tmp/" );
+      u.addPath( fileName );
+      fileName = KMimeType::findByURL( u, 0, true, true )->icon( QString::null, true );
+    }
   } 
 
   fileName =
Comment 9 Jonathan Marten 2006-11-26 20:42:51 UTC
SVN commit 608103 by marten:

Merge commits 603223+603260 from branches/work/kdepim-3.5.5+: 
 
Get attachment icon from file name if not available from Content-Type
(Using KMimeType::findByPath as suggested by David Faure)
   
CCBUG: 35836 


 M  +11 -0     kmmsgpart.cpp  
 M  +1 -0      objecttreeparser.cpp  


--- branches/KDE/3.5/kdepim/kmail/kmmsgpart.cpp #608102:608103
@@ -386,8 +386,19 @@
 {
   QCString mimeType( mType + "/" + mSubtype );
   KPIM::kAsciiToLower( mimeType.data() );
+
   QString fileName =
     KMimeType::mimeType( mimeType )->icon( QString::null, false );
+  if ( fileName.isEmpty() )
+  { 
+    fileName = this->fileName(); 
+    if ( fileName.isEmpty() ) fileName = this->name(); 
+    if ( !fileName.isEmpty() )
+    {
+      fileName = KMimeType::findByPath( "/tmp/"+fileName, 0, true )->icon( QString::null, true );
+    }
+  } 
+
   fileName =
     KGlobal::instance()->iconLoader()->iconPath( fileName, KIcon::Desktop );
   return fileName;
--- branches/KDE/3.5/kdepim/kmail/objecttreeparser.cpp #608102:608103
@@ -1734,6 +1734,7 @@
 
     QString comment = msgPart->contentDescription();
     comment = KMMessage::quoteHtmlChars( comment, true );
+    if ( label == comment ) comment = QString::null;
 
     QString fileName = mReader->writeMessagePartToTempFile( msgPart, partNum );
 
Comment 10 Jonathan Marten 2006-11-28 15:15:05 UTC
SVN commit 608814 by marten:

Forward port 603223+603260 from branches/work/kdepim-3.5.5+: 

Get attachment icon from file name if not available from Content-Type 

CCBUG: 35836 


 M  +12 -1     kmmsgpart.cpp  
 M  +1 -0      objecttreeparser.cpp  


--- trunk/KDE/kdepim/kmail/kmmsgpart.cpp #608813:608814
@@ -388,7 +388,18 @@
   Q3CString mimeType( mType + '/' + mSubtype );
   kAsciiToLower( mimeType.data() );
   QString fileName =
-    KMimeType::mimeType( mimeType )->iconName( QString() );
+    KMimeType::mimeType( mimeType )->iconName();
+
+  if ( fileName.isEmpty() ) 
+  { 
+    fileName = this->fileName(); 
+    if ( fileName.isEmpty() ) fileName = this->name(); 
+    if ( !fileName.isEmpty() ) 
+    { 
+      fileName = KMimeType::findByPath( "/tmp/"+fileName, 0, true )->iconName(); 
+    } 
+  } 
+ 
   fileName =
     KGlobal::instance()->iconLoader()->iconPath( fileName, K3Icon::Desktop );
   return fileName;
--- trunk/KDE/kdepim/kmail/objecttreeparser.cpp #608813:608814
@@ -1744,6 +1744,7 @@
 
     QString comment = msgPart->contentDescription();
     comment = KMMessage::quoteHtmlChars( comment, true );
+    if ( label == comment ) comment = QString::null; 
 
     QString fileName = mReader->writeMessagePartToTempFile( msgPart, partNum );