Bug 232782

Summary: Encapsulated message viewing broken
Product: [Applications] kdepim Reporter: Thomas McGuire <mcguire>
Component: messageviewerAssignee: Till Adam <adam>
Status: RESOLVED UNMAINTAINED    
Severity: normal CC: adam, amantia, gmetal31, montel
Priority: NOR Keywords: akonadi-ports-regression
Version: 4.5   
Target Milestone: ---   
Platform: Unlisted Binaries   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Bug Depends on:    
Bug Blocks: 223438    

Description Thomas McGuire 2010-03-31 01:16:52 UTC
KMail 1 had the ability to view encapsulated messages, i.e. messages that are added as an attachment to other messages.

Viewing them could be started by two ways:
Clicking the "Encapsulated Message" link in the reader
Clicking on the message in the message structure viewer

I think for the first one, a separate reader window was launched.

This all currently does not work.

Some other implementation details:
- When hitting reply or forward while viewing the encapsulated message, the 
  reply/forward would be based on the encapsulated message, not on the original 
  message.
- Deleting or editing attachments of the encapsulated message should save the 
  changes back to the original message. The window that displays the  
  encapsulated message and the widget that displayed the original message both 
  need to be updated.

This probably requires some work to clean up the places dealing with KMime::Message::Ptr and the places dealing with Akonadi::Item. The API documentation of ViewerPrivate should be extended to clearly explain this matter.

Also make sure the following cases work:
https://issues.kolab.org/issue3740

This was hacked to work in the KMail 1 version with r1010835, r1010840 and r1071368. Here, it should be done in a cleaner way.
Comment 1 Till Adam 2010-05-10 09:54:05 UTC
"Clicking the "Encapsulated Message" link in the reader" works now

"Clicking on the message in the message structure viewer" works now

There's a crash when forwarding.
Comment 2 Till Adam 2010-05-10 14:36:18 UTC
Crash fixed with r1124977.

Still oddness when doubleclicking an embedded message, it seems to be wrapped in an empty outer shell message.
Comment 3 Till Adam 2010-05-10 18:06:08 UTC
Filename for nested messages in the attachment model of the composer fixed with r1125071.
Comment 4 Till Adam 2010-05-11 11:46:39 UTC
Remaining problem is opening attached messages (forwards) from the composer's attachment model.

kioexec(25106) KIOExec::slotRunApp: EXEC  "/Applications/KDE4/kmail.app/Contents/MacOS/kmail --view /private/var/folders/ur/urD8KXFVEwi0L+MgtbH5uU+++TI/-Tmp-/kde-tilloNaZav/kmailK24768.tmp"
QMetaObject::invokeMethod: No such method KUniqueApplication::loadCommandLineOptionsForNewInstance()

David is looking into it, looks like a kdelibs problem.
Comment 5 George Metaxas 2010-12-27 21:12:06 UTC
I cannot reproduce any of the behaviour discussed here. I can even open an attached message from the message composer. The only "annoyance" that I noticed is that the first time you attempt to open up an attached message, there is an "Unable to edit attachment" message box, where if you click OK, it also closes the attached message window. If you tick "Do not show this again" and click OK it also closes the window, but the next time you open up the attached message the window remains open without the message box.
Comment 6 George Metaxas 2010-12-27 21:16:18 UTC
Correction to my comment above. The attached message window is not closed. It is sent to back. It seems as if the message box is launched asynchronously, and is not modal.
Comment 7 Thomas McGuire 2011-01-01 21:47:47 UTC
There are still some issues left:

> - When hitting reply or forward while viewing the encapsulated message, the 
>  reply/forward would be based on the encapsulated message, not on the original 
>  message.

For me the reply action is disabled, forwarding seems to work fine.

> - Deleting or editing attachments of the encapsulated message should save the 
>  changes back to the original message. The window that displays the  
>  encapsulated message and the widget that displayed the original message both 
>  need to be updated.

This does not work, the delete attachment action is disabled.
Comment 8 Thomas McGuire 2011-01-01 21:48:50 UTC
> I can even open an attached message from the message composer. The only 
> "annoyance" that I noticed
> is that the first time you attempt to open up an attached message, there is an
> "Unable to edit attachment" message box, where if you click OK,

Indeed, that is a bug in the composer, do you want to fix that?
Comment 9 George Metaxas 2011-01-04 20:14:39 UTC
commit 0c8ece2bd5781f9f941a34e92e811b43ff897da5
branch master
Author: George Metaxas <gmetal31@gmail.com>
Date:   Tue Jan 4 21:07:25 2011 +0200

    Prevent the editing of an encapsulated message from the message composer.
    Ths involves disabling the Edit and Edit With popup menu entries and viewing
    rather than editing upon double clicking the attachment.
    
    CCBUG: 232782

diff --git a/kmail/attachmentcontroller.cpp b/kmail/attachmentcontroller.cpp
index 11157d0..b6224db 100644
--- a/kmail/attachmentcontroller.cpp
+++ b/kmail/attachmentcontroller.cpp
@@ -43,6 +43,7 @@
 #include <messagecore/attachmentfrommimecontentjob.h>
 #include <messagecore/attachmentfromurljob.h>
 #include <messagecore/attachmentpropertiesdialog.h>
+#include <messagecore/attachmentpart.h>
 #include <messageviewer/editorwatcher.h>
 
 using namespace KMail;
@@ -62,8 +63,8 @@ AttachmentController::AttachmentController( Message::AttachmentModel *model, Att
   connect( view, SIGNAL(contextMenuRequested()), this, SLOT(showContextMenu()) );
   connect( view->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
       this, SLOT(selectionChanged()) );
-  connect( view, SIGNAL(doubleClicked(QModelIndex)),
-      this, SLOT(editSelectedAttachment()) );
+  connect( view, SIGNAL( doubleClicked( const QModelIndex & ) ),
+      this, SLOT( doubleClicked( const QModelIndex &) ) );
 
   connect( this, SIGNAL(refreshSelection()), SLOT(selectionChanged()));
 
@@ -169,4 +170,26 @@ void AttachmentController::onShowAttachment( KMime::Content *content, const QByt
   win->show();
 }
 
+void AttachmentController::doubleClicked( const QModelIndex &itemClicked )
+{
+  if ( !itemClicked.isValid() ) {
+    kDebug() << "Received an invalid item clicked index";
+    return;
+  }
+  // The itemClicked index will contain the column information. But we want to retrieve
+  // the AttachmentPart, so we must recreate the QModelIndex without the column information
+  const QModelIndex &properItemClickedIndex = mView->model()->index( itemClicked.row(), 0 );
+  AttachmentPart::Ptr part = mView->model()->data(
+          properItemClickedIndex,
+          Message::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
+
+  // We can't edit encapsulated messages, but we can view them.
+  if ( part->isMessageOrMessageCollection() ) {
+    viewAttachment( part );
+  }
+  else {
+    editAttachment( part );
+  }
+}
+
 #include "attachmentcontroller.moc"
diff --git a/kmail/attachmentcontroller.h b/kmail/attachmentcontroller.h
index a634ce9..0a0d1a5 100644
--- a/kmail/attachmentcontroller.h
+++ b/kmail/attachmentcontroller.h
@@ -56,6 +56,7 @@ class AttachmentController : public Message::AttachmentControllerBase
     void slotFetchJob( KJob * job );
     void selectionChanged();
     void onShowAttachment( KMime::Content *content, const QByteArray &charset );
+    void doubleClicked( const QModelIndex &itemClicked );
 
   private:
     KMComposeWin *mComposer;
diff --git a/messagecomposer/attachmentcontrollerbase.cpp b/messagecomposer/attachmentcontrollerbase.cpp
index 1361426..052d86e 100644
--- a/messagecomposer/attachmentcontrollerbase.cpp
+++ b/messagecomposer/attachmentcontrollerbase.cpp
@@ -148,11 +148,13 @@ void AttachmentControllerBase::setSelectedParts( const AttachmentPart::List &sel
 {
   d->selectedParts = selectedParts;
   const int selectedCount = selectedParts.count();
+  const bool enableEditAction = (selectedCount == 1) &&
+                                ( !selectedParts.first()->isMessageOrMessageCollection() );
 
   d->openContextAction->setEnabled( selectedCount > 0 );
   d->viewContextAction->setEnabled( selectedCount > 0 );
-  d->editContextAction->setEnabled( selectedCount == 1 );
-  d->editWithContextAction->setEnabled( selectedCount == 1 );
+  d->editContextAction->setEnabled( enableEditAction );
+  d->editWithContextAction->setEnabled( enableEditAction );
   d->removeAction->setEnabled( selectedCount > 0 );
   d->removeContextAction->setEnabled( selectedCount > 0 );
   d->saveAsAction->setEnabled( selectedCount == 1 );
diff --git a/messagecore/attachmentpart.cpp b/messagecore/attachmentpart.cpp
index fa006d6..8e1bee2 100644
--- a/messagecore/attachmentpart.cpp
+++ b/messagecore/attachmentpart.cpp
@@ -223,3 +223,8 @@ qint64 AttachmentPart::size() const
 {
   return d->mSize;
 }
+
+bool AttachmentPart::isMessageOrMessageCollection() const
+{
+  return ( mimeType() == "message/rfc822" ) || ( mimeType() == "multipart/digest" );
+}
diff --git a/messagecore/attachmentpart.h b/messagecore/attachmentpart.h
index dd8915a..dd85ecb 100644
--- a/messagecore/attachmentpart.h
+++ b/messagecore/attachmentpart.h
@@ -192,6 +192,11 @@ class MESSAGECORE_EXPORT AttachmentPart
      */
     qint64 size() const;
 
+    /**
+     * Returns whether the specified attachment part is an encapsulated message
+     * (message/rfc822) or a collection of encapsulated messages (multipart/digest)
+     */
+    bool isMessageOrMessageCollection() const;
   private:
     //@cond PRIVATE
     class Private;
Comment 10 Laurent Montel 2011-11-20 12:21:05 UTC
Forward/reply encapsuled message works fine now.
Delete doesn't work for the moment. Will look at it.
Comment 11 Denis Kurz 2016-09-24 20:59:53 UTC
This bug has only been reported for versions before 4.14, which have been unsupported for at least two years now. Can anyone tell if this bug still present?

If noone confirms this bug for a Framework-based version of kdepim (version 5.0 or later, as part of KDE Applications 15.08 or later), it gets closed in about three months.
Comment 12 Denis Kurz 2017-01-07 22:38:17 UTC
Just as announced in my last comment, I close this bug. If you encounter it again in a recent version (at least 5.0 aka 15.08), please open a new one unless it already exists. Thank you for all your input.