00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "movetotrashcommand_p.h"
00022 #include "util_p.h"
00023 #include "movecommand_p.h"
00024 #include "imapsettings.h"
00025
00026 #include <akonadi/itemfetchjob.h>
00027 #include <akonadi/itemfetchscope.h>
00028 #include <akonadi/kmime/specialmailcollections.h>
00029 #include <akonadi/entitytreemodel.h>
00030
00031 MoveToTrashCommand::MoveToTrashCommand(const QAbstractItemModel* model, const Akonadi::Collection::List& folders, QObject* parent): CommandBase( parent )
00032 {
00033 the_trashCollectionFolder = -1;
00034 mFolders = folders;
00035 mModel = model;
00036 mFolderListJobCount = mFolders.size();
00037 }
00038
00039 MoveToTrashCommand::MoveToTrashCommand(const QAbstractItemModel* model, const QList< Akonadi::Item >& msgList, QObject* parent): CommandBase( parent )
00040 {
00041 the_trashCollectionFolder = -1;
00042 mMessages = msgList;
00043 mModel = model;
00044 mFolderListJobCount = 0;
00045 }
00046
00047
00048 void MoveToTrashCommand::slotFetchDone(KJob* job)
00049 {
00050 mFolderListJobCount--;
00051
00052 if ( job->error() ) {
00053
00054 Util::showJobError(job);
00055 emitResult( Failed );
00056 return;
00057 }
00058
00059 Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
00060 Q_ASSERT( fjob );
00061
00062 mMessages = fjob->items();
00063 moveMessages();
00064
00065 if ( mFolderListJobCount > 0 ) {
00066 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
00067 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
00068 connect( job, SIGNAL( result( KJob* ) ), this, SLOT( slotFetchDone( KJob* ) ) );
00069 }
00070 }
00071
00072
00073 void MoveToTrashCommand::execute()
00074 {
00075 if ( !mFolders.isEmpty() ) {
00076 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
00077 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
00078 connect( job, SIGNAL( result( KJob* ) ), this, SLOT( slotFetchDone( KJob* ) ) );
00079 } else if ( !mMessages.isEmpty() ) {
00080 mFolders << mMessages.first().parentCollection();
00081 moveMessages();
00082 } else {
00083 emitResult( OK );
00084 }
00085 }
00086
00087 void MoveToTrashCommand::moveMessages()
00088 {
00089 Akonadi::Collection folder = mFolders[mFolderListJobCount];
00090 if ( folder.isValid() ) {
00091 MoveCommand *moveCommand = new MoveCommand( findTrashFolder( folder ), mMessages, this );
00092 connect( moveCommand, SIGNAL( result( Result ) ), this, SLOT( slotMoveDone( Result ) ) );
00093 moveCommand->execute();
00094 } else {
00095 emitResult( Failed );
00096 }
00097 }
00098
00099 void MoveToTrashCommand::slotMoveDone( const Result& result )
00100 {
00101 if (result == Failed )
00102 emitResult( Failed );
00103 if ( mFolderListJobCount == 0 && result == OK) {
00104 emitResult( OK );
00105 }
00106 }
00107
00108 Akonadi::Collection MoveToTrashCommand::collectionFromId(const Akonadi::Collection::Id& id) const
00109 {
00110 const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
00111 mModel, Akonadi::Collection(id)
00112 );
00113 return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
00114 }
00115
00116 Akonadi::Collection MoveToTrashCommand::trashCollectionFromResource( const Akonadi::Collection & col )
00117 {
00118
00119 Akonadi::Collection trashCol;
00120 if ( col.isValid() ) {
00121 if ( col.resource().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
00122
00123
00124 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( col.resource() );
00125 if ( iface->isValid() ) {
00126
00127 trashCol = Akonadi::Collection( iface->trashCollection() );
00128 delete iface;
00129 return trashCol;
00130 }
00131 delete iface;
00132 }
00133 }
00134 return trashCol;
00135 }
00136
00137 Akonadi::Collection MoveToTrashCommand::trashCollectionFolder()
00138 {
00139 if ( the_trashCollectionFolder < 0 )
00140 the_trashCollectionFolder = Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ).id();
00141 return collectionFromId( the_trashCollectionFolder );
00142 }
00143
00144
00145 Akonadi::Collection MoveToTrashCommand::findTrashFolder( const Akonadi::Collection& folder )
00146 {
00147 Akonadi::Collection col = trashCollectionFromResource( folder );
00148 if ( !col.isValid() ) {
00149 col = trashCollectionFolder();
00150 }
00151 if ( folder != col )
00152 return col;
00153 return Akonadi::Collection();
00154 }
00155
00156
00157
00158 #include "movetotrashcommand_p.moc"