From 3a2d4a6310e814d555962380844995b073bd8e0d Mon Sep 17 00:00:00 2001 From: Volker Krause Date: Thu, 17 May 2007 13:13:50 +0000 Subject: [PATCH] Allow to delete attachments from a mail. svn path=/branches/kdepim/enterprise/kdepim/; revision=665628 --- kmcommands.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ kmcommands.h | 19 +++++++++++ kmmessage.cpp | 5 +++ kmmessage.h | 3 ++ kmreaderwin.cpp | 25 ++++++++++---- 5 files changed, 137 insertions(+), 6 deletions(-) diff --git a/kmcommands.cpp b/kmcommands.cpp index 2f4d3fc32..8dc46e3b9 100644 --- a/kmcommands.cpp +++ b/kmcommands.cpp @@ -3250,4 +3250,95 @@ void KMHandleAttachmentCommand::slotAtmDecryptWithChiasmusUploadResult( KIO::Job d.setResult( OK ); } + +KMDeleteAttachmentCommand::KMDeleteAttachmentCommand(partNode * node, KMMessage * msg, QWidget * parent) : + KMCommand( parent, msg ), + mPartIndex( node->nodeId() ), + mSernum( 0 ) +{ + kdDebug() << k_funcinfo << endl; +} + +KMDeleteAttachmentCommand::~KMDeleteAttachmentCommand() +{ + kdDebug() << k_funcinfo << endl; +} + +KMCommand::Result KMDeleteAttachmentCommand::execute() +{ + kdDebug() << k_funcinfo << endl; + KMMessage *msg = retrievedMessage(); + if ( !msg ) + return Failed; + mSernum = msg->getMsgSerNum(); + KMMessagePart part; + // -2 because partNode counts root and body of the message as well + DwBodyPart *dwpart = msg->dwBodyPart( mPartIndex - 2 ); + if ( !dwpart ) + return Failed; + KMMessage::bodyPart( dwpart, &part, true ); + if ( !part.isComplete() ) + return Failed; + msg->removeBodyPart( dwpart ); + + // add dummy part to show that a attachment has been deleted + KMMessagePart dummyPart; + dummyPart.duplicate( part ); + QString content = i18n("This attachment has been deleted."); + if ( !part.fileName().isEmpty() ) + content = i18n( "The attachment '%1' has been deleted." ).arg( part.fileName() ); + dummyPart.setBodyFromUnicode( content ); + dummyPart.setTypeStr( "text" ); + dummyPart.setSubtypeStr( "plain" ); + QCString cd = dummyPart.contentDisposition(); + if ( cd.find( "attachment", 0, false ) == 0 ) { + cd.replace( 0, 10, "inline" ); + dummyPart.setContentDisposition( cd ); + } else if ( cd.isEmpty() ) { + dummyPart.setContentDisposition( "inline" ); + } + msg->addBodyPart( &dummyPart ); + + KMFolder *folder = msg->parent(); + if ( !folder ) + return Failed; + FolderStorage *storage = folder->storage(); + if ( !storage ) + return Failed; + + KMMessage *newMsg = new KMMessage(); + newMsg->fromDwString( msg->asDwString() ); + newMsg->setStatus( msg->status() ); + + FolderJob *job = storage->createJob( newMsg, FolderJob::tPutMessage, folder ); + connect( job, SIGNAL(result(KMail::FolderJob*)), SLOT(messageStoreResult(KMail::FolderJob*)) ); + job->start(); + + setEmitsCompletedItself( true ); + setDeletesItself( true ); + return OK; +} + +void KMDeleteAttachmentCommand::messageStoreResult(KMail::FolderJob * job) +{ + kdDebug() << k_funcinfo << job << job->error() << endl; + if ( !job->error() && mSernum ) { + KMCommand *delCmd = new KMDeleteMsgCommand( mSernum ); + connect( delCmd, SIGNAL(completed(KMCommand*)), SLOT(messageDeleteResult(KMCommand*)) ); + delCmd->start(); + return; + } + setResult( Failed ); + emit completed( this ); + deleteLater(); +} + +void KMDeleteAttachmentCommand::messageDeleteResult(KMCommand * cmd) +{ + kdDebug() << k_funcinfo << cmd << cmd->result() << endl; + setResult( cmd->result() ); + emit completed( this ); + deleteLater(); +} + #include "kmcommands.moc" diff --git a/kmcommands.h b/kmcommands.h index 34c21bcce..fd1090fcf 100644 --- a/kmcommands.h +++ b/kmcommands.h @@ -1018,4 +1018,23 @@ private: }; + +class KDE_EXPORT KMDeleteAttachmentCommand : public KMCommand +{ + Q_OBJECT + public: + KMDeleteAttachmentCommand( partNode *node, KMMessage *msg, QWidget *parent ); + ~KMDeleteAttachmentCommand(); + + private slots: + void messageStoreResult( KMail::FolderJob *job ); + void messageDeleteResult( KMCommand *cmd ); + + private: + Result execute(); + + int mPartIndex; + Q_UINT32 mSernum; +}; + #endif /*KMCommands_h*/ diff --git a/kmmessage.cpp b/kmmessage.cpp index d5f42224c..7203f3ae2 100644 --- a/kmmessage.cpp +++ b/kmmessage.cpp @@ -3091,6 +3091,11 @@ void KMMessage::deleteBodyParts() mMsg->Body().DeleteBodyParts(); } +void KMMessage::removeBodyPart(DwBodyPart * dwPart) +{ + mMsg->Body().RemoveBodyPart( dwPart ); + mNeedsAssembly = true; +} //----------------------------------------------------------------------------- DwBodyPart* KMMessage::createDWBodyPart(const KMMessagePart* aPart) diff --git a/kmmessage.h b/kmmessage.h index e2c833f7f..b3702a90f 100644 --- a/kmmessage.h +++ b/kmmessage.h @@ -621,6 +621,9 @@ public: /** Delete all body parts. */ void deleteBodyParts(); + /** Removes the given body part. */ + void removeBodyPart( DwBodyPart * dwPart ); + /** Set "Status" and "X-Status" fields of the message from the * internal message status. */ void setStatusFields(); diff --git a/kmreaderwin.cpp b/kmreaderwin.cpp index 6f34ddc9c..4efa3ef76 100644 --- a/kmreaderwin.cpp +++ b/kmreaderwin.cpp @@ -1903,6 +1903,7 @@ void KMReaderWin::showAttachmentPopup( int id, const QString & name, const QPoin menu->insertItem(i18n("Open With..."), 2); menu->insertItem(i18n("to view something", "View"), 3); menu->insertItem(SmallIcon("filesaveas"),i18n("Save As..."), 4); + menu->insertItem(SmallIcon("editdelete"), i18n("Delete Attachment"), 7 ); if ( name.endsWith( ".xia", false ) && Kleo::CryptoBackendFactory::instance()->protocol( "Chiasmus" ) ) menu->insertItem( i18n( "Decrypt With Chiasmus..." ), 6 ); @@ -1941,12 +1942,24 @@ void KMReaderWin::slotHandleAttachment( int choice ) { mAtmUpdate = true; partNode* node = mRootNode ? mRootNode->findId( mAtmCurrent ) : 0; - KMHandleAttachmentCommand* command = new KMHandleAttachmentCommand( - node, message(), mAtmCurrent, mAtmCurrentName, - KMHandleAttachmentCommand::AttachmentAction( choice ), 0, this ); - connect( command, SIGNAL( showAttachment( int, const QString& ) ), - this, SLOT( slotAtmView( int, const QString& ) ) ); - command->start(); + if ( choice != 7 ) { + KMHandleAttachmentCommand* command = new KMHandleAttachmentCommand( + node, message(), mAtmCurrent, mAtmCurrentName, + KMHandleAttachmentCommand::AttachmentAction( choice ), 0, this ); + connect( command, SIGNAL( showAttachment( int, const QString& ) ), + this, SLOT( slotAtmView( int, const QString& ) ) ); + command->start(); + } else { + if ( KMessageBox::warningContinueCancel( this, + i18n("Deleting an attachment might invalidate any digital signature on this message."), + i18n("Delete Attachment"), KStdGuiItem::del(), "DeleteAttachmentSignatureWarning" ) + != KMessageBox::Continue ) { + return; + } + kdDebug() << k_funcinfo << mAtmCurrent << mAtmCurrentName << endl; + KMDeleteAttachmentCommand* command = new KMDeleteAttachmentCommand( node, message(), this ); + command->start(); + } } //-----------------------------------------------------------------------------