From 4666572dbced950faa8d16316f593dee686c495c Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Mon, 26 Oct 2009 09:38:52 +0000 Subject: [PATCH] Try to implement copy items svn path=/branches/work/akonadi-ports/kdepim/; revision=1040379 --- kmcommands.cpp | 160 +++---------------------------------------------- kmcommands.h | 6 +- 2 files changed, 9 insertions(+), 157 deletions(-) diff --git a/kmcommands.cpp b/kmcommands.cpp index 5054752f8..b02b7c658 100644 --- a/kmcommands.cpp +++ b/kmcommands.cpp @@ -123,6 +123,7 @@ using KMail::TemplateParser; #include "interfaces/htmlwriter.h" #include +#include #include "progressmanager.h" using KPIM::ProgressManager; @@ -2019,7 +2020,7 @@ KMCommand::Result KMMailingListFilterCommand::execute() KMCopyCommand::KMCopyCommand( const Akonadi::Collection& destFolder, const QList &msgList ) -:mDestFolder( destFolder ), mMsgList( msgList ) + :mDestFolder( destFolder ), mMsgList( msgList ) { setDeletesItself( true ); } @@ -2033,167 +2034,22 @@ KMCopyCommand::KMCopyCommand( const Akonadi::Collection& destFolder, const Akona KMCommand::Result KMCopyCommand::execute() { -#if 0 //TODO port to akonadi - KMime::Message *msgBase; - KMime::Message *msg, *newMsg; - int idx = -1; - bool isMessage; - QList list; - QList localList; - - if ( mDestFolder && mDestFolder->open( "kmcommand" ) != 0 ) { - deleteLater(); - return Failed; - } + Akonadi::ItemCopyJob *job = new Akonadi::ItemCopyJob( mMsgList, mDestFolder,this ); + connect( job, SIGNAL(result(KJob*)), this, SLOT(slotCopyResult(KJob*)) ); - setEmitsCompletedItself( true ); - KCursorSaver busy(KBusyPtr::busy()); - - QList::const_iterator it; - for ( it = mMsgList.constBegin(); it != mMsgList.constEnd(); ++it ) { - msgBase = (*it); - KMFolder *srcFolder = msgBase->parent(); - isMessage = msgBase->isMessage(); - if ( isMessage ) { - msg = static_cast( msgBase ); - } else { - idx = srcFolder->find( msgBase ); - assert( idx != -1 ); - msg = srcFolder->getMsg( idx ); - // corrupt IMAP cache, see FolderStorage::getMsg() - if ( msg == 0 ) { - KMessageBox::error( parentWidget(), - i18n( "Corrupt IMAP cache detected in folder %1. " - "Copying of messages aborted.", - srcFolder->prettyUrl() ) ); - deleteLater(); - return Failed; - } - } - - if (srcFolder && mDestFolder && - (srcFolder->folderType()== KMFolderTypeImap) && - (mDestFolder->folderType() == KMFolderTypeImap) && - (static_cast(srcFolder->storage())->account() == - static_cast(mDestFolder->storage())->account())) - { - // imap => imap with same account - list.append(msg); - } else { - newMsg = new KMime::Message( new DwMessage( *msg->asDwMessage() ) ); - newMsg->setComplete(msg->isComplete()); - // make sure the attachment state is only calculated when it's complete - if (!newMsg->isComplete()) - newMsg->setReadyToShow(false); - newMsg->setStatus( msg->messageStatus() ); - - if (srcFolder && !newMsg->isComplete()) - { - // imap => others - newMsg->setParent(msg->parent()); - FolderJob *job = srcFolder->createJob(newMsg); - job->setCancellable( false ); - mPendingJobs << job; - connect(job, SIGNAL(messageRetrieved(KMime::Message*)), - mDestFolder, SLOT(reallyAddCopyOfMsg(KMime::Message*))); - connect( job, SIGNAL(result(KMail::FolderJob*)), - this, SLOT(slotJobFinished(KMail::FolderJob*)) ); - job->start(); - } else { - // local => others - localList.append(newMsg); - } - } - - if (srcFolder && !isMessage && list.isEmpty()) - { - assert(idx != -1); - srcFolder->unGetMsg( idx ); - } - - } // end for - - bool deleteNow = false; - if ( !localList.isEmpty() && mDestFolder ) - { - QList index; - mDestFolder->addMessages( localList, index ); - for ( QList::Iterator it = index.begin(); it != index.end(); ++it ) { - mDestFolder->unGetMsg( *it ); - } - if ( mDestFolder->folderType() == KMFolderTypeImap ) { - if ( mPendingJobs.isEmpty() ) { - // wait for the end of the copy before closing the folder - KMFolderImap *imapDestFolder = static_cast(mDestFolder->storage()); - connect( imapDestFolder, SIGNAL( folderComplete( KMFolderImap*, bool ) ), - this, SLOT( slotFolderComplete( KMFolderImap*, bool ) ) ); - } - } else { - deleteNow = list.isEmpty() && mPendingJobs.isEmpty(); // we're done if there are no other mails we need to fetch - } - } - -//TODO: Get rid of the other cases just use this one for all types of folder -//TODO: requires adding copyMsg and getFolder methods to KMFolder.h - if ( !list.isEmpty() && mDestFolder ) - { - // copy the message(s); note: the list is empty afterwards! - KMFolderImap *imapDestFolder = static_cast(mDestFolder->storage()); - connect( imapDestFolder, SIGNAL( folderComplete( KMFolderImap*, bool ) ), - this, SLOT( slotFolderComplete( KMFolderImap*, bool ) ) ); - imapDestFolder->copyMsg(list); - imapDestFolder->getFolder(); - } - - // only close the folder and delete the job if we're done - // otherwise this is done in slotMsgAdded or slotFolderComplete - if ( deleteNow ) { - if ( mDestFolder ) - mDestFolder->close( "kmcommand" ); - setResult( OK ); - emit completed( this ); - deleteLater(); - } -#else - kDebug() << "AKONADI PORT: Disabled code in " << Q_FUNC_INFO; -#endif return OK; } -void KMCopyCommand::slotJobFinished(KMail::FolderJob * job) +void KMCopyCommand::slotCopyResult( KJob * job ) { - mPendingJobs.removeAll( job ); if ( job->error() ) { - kDebug() << "folder job failed:" << job->error(); - // kill all pending jobs - for ( QList::Iterator it = mPendingJobs.begin(); it != mPendingJobs.end(); ++it ) { - disconnect( (*it), SIGNAL(result(KMail::FolderJob*)), - this, SLOT(slotJobFinished(KMail::FolderJob*)) ); - (*it)->kill(); - } - mPendingJobs.clear(); + // handle errors + static_cast(job)->ui()->showErrorMessage(); setResult( Failed ); } - - if ( mPendingJobs.isEmpty() ) - { - //mDestFolder->close( "kmcommand" ); - emit completed( this ); - deleteLater(); - } -} - -void KMCopyCommand::slotFolderComplete( KMFolderImap*, bool success ) -{ - kDebug() << success; - if ( !success ) - setResult( Failed ); - //mDestFolder->close( "kmcommand" ); - emit completed( this ); deleteLater(); } - KMMoveCommand::KMMoveCommand( const Akonadi::Collection& destFolder, const QList &msgList) : mDestFolder( destFolder ), mProgressItem( 0 ) @@ -2226,7 +2082,7 @@ void KMMoveCommand::slotMoveResult( KJob * job ) KMCommand::Result KMMoveCommand::execute() { - Akonadi::ItemMoveJob *job = new Akonadi::ItemMoveJob( mItem, mDestFolder ); + Akonadi::ItemMoveJob *job = new Akonadi::ItemMoveJob( mItem, mDestFolder,this ); connect( job, SIGNAL(result(KJob*)), this, SLOT(slotMoveResult(KJob*)) ); #if 0 //TODO port to akonadi setEmitsCompletedItself( true ); diff --git a/kmcommands.h b/kmcommands.h index 95acfe17d..418623bb5 100644 --- a/kmcommands.h +++ b/kmcommands.h @@ -714,16 +714,12 @@ public: KMCopyCommand( const Akonadi::Collection& destFolder, const Akonadi::Item &msg ); protected slots: - void slotJobFinished( KMail::FolderJob *job ); - - void slotFolderComplete( KMFolderImap*, bool success ); - + void slotCopyResult( KJob * job ); private: virtual Result execute(); Akonadi::Collection mDestFolder; QList mMsgList; - QList mPendingJobs; }; namespace KPIM {