21 #include "movetotrashcommand_p.h"
23 #include "movecommand_p.h"
24 #include "imapsettings.h"
26 #include <akonadi/itemfetchjob.h>
27 #include <akonadi/itemfetchscope.h>
28 #include <akonadi/kmime/specialmailcollections.h>
29 #include <akonadi/entitytreemodel.h>
31 MoveToTrashCommand::MoveToTrashCommand(
const QAbstractItemModel* model,
const Akonadi::Collection::List& folders, QObject* parent): CommandBase( parent )
33 the_trashCollectionFolder = -1;
36 mFolderListJobCount = mFolders.size();
39 MoveToTrashCommand::MoveToTrashCommand(
const QAbstractItemModel* model,
const QList< Akonadi::Item >& msgList, QObject* parent): CommandBase( parent )
41 the_trashCollectionFolder = -1;
44 mFolderListJobCount = 0;
48 void MoveToTrashCommand::slotFetchDone(KJob* job)
50 mFolderListJobCount--;
54 Util::showJobError(job);
59 Akonadi::ItemFetchJob *fjob =
static_cast<Akonadi::ItemFetchJob*
>( job );
61 mMessages = fjob->items();
64 if ( mFolderListJobCount > 0 ) {
65 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
66 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
67 connect( job, SIGNAL(result(KJob*)),
this, SLOT(slotFetchDone(KJob*)) );
72 void MoveToTrashCommand::execute()
74 if ( !mFolders.isEmpty() ) {
75 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
76 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
77 connect( job, SIGNAL(result(KJob*)),
this, SLOT(slotFetchDone(KJob*)) );
78 }
else if ( !mMessages.isEmpty() ) {
79 mFolders << mMessages.first().parentCollection();
86 void MoveToTrashCommand::moveMessages()
88 Akonadi::Collection folder = mFolders[mFolderListJobCount];
89 if ( folder.isValid() ) {
90 MoveCommand *moveCommand =
new MoveCommand( findTrashFolder( folder ), mMessages,
this );
91 connect( moveCommand, SIGNAL(result(Result)),
this, SLOT(slotMoveDone(Result)) );
92 moveCommand->execute();
98 void MoveToTrashCommand::slotMoveDone(
const Result& result )
100 if (result == Failed )
101 emitResult( Failed );
102 if ( mFolderListJobCount == 0 && result == OK) {
107 Akonadi::Collection MoveToTrashCommand::collectionFromId(
const Akonadi::Collection::Id&
id)
const
109 const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
110 mModel, Akonadi::Collection(
id)
112 return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
115 Akonadi::Collection MoveToTrashCommand::trashCollectionFromResource(
const Akonadi::Collection & col )
118 Akonadi::Collection trashCol;
119 if ( col.isValid() ) {
120 if ( col.resource().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
123 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( col.resource() );
124 if ( iface->isValid() ) {
126 trashCol = Akonadi::Collection( iface->trashCollection() );
136 Akonadi::Collection MoveToTrashCommand::trashCollectionFolder()
138 if ( the_trashCollectionFolder < 0 )
140 return collectionFromId( the_trashCollectionFolder );
144 Akonadi::Collection MoveToTrashCommand::findTrashFolder(
const Akonadi::Collection& folder )
146 Akonadi::Collection col = trashCollectionFromResource( folder );
147 if ( !col.isValid() ) {
148 col = trashCollectionFolder();
152 return Akonadi::Collection();
157 #include "moc_movetotrashcommand_p.cpp"