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 =
dynamic_cast<Akonadi::ItemFetchJob*
>( job );
62 mMessages = fjob->items();
65 if ( mFolderListJobCount > 0 ) {
66 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
67 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
68 connect( job, SIGNAL(result(KJob*)),
this, SLOT(slotFetchDone(KJob*)) );
73 void MoveToTrashCommand::execute()
75 if ( !mFolders.isEmpty() ) {
76 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
77 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
78 connect( job, SIGNAL(result(KJob*)),
this, SLOT(slotFetchDone(KJob*)) );
79 }
else if ( !mMessages.isEmpty() ) {
80 mFolders << mMessages.first().parentCollection();
87 void MoveToTrashCommand::moveMessages()
89 Akonadi::Collection folder = mFolders[mFolderListJobCount];
90 if ( folder.isValid() ) {
91 MoveCommand *moveCommand =
new MoveCommand( findTrashFolder( folder ), mMessages,
this );
92 connect( moveCommand, SIGNAL(result(Result)),
this, SLOT(slotMoveDone(Result)) );
93 moveCommand->execute();
99 void MoveToTrashCommand::slotMoveDone(
const Result& result )
101 if (result == Failed )
102 emitResult( Failed );
103 if ( mFolderListJobCount == 0 && result == OK) {
108 Akonadi::Collection MoveToTrashCommand::collectionFromId(
const Akonadi::Collection::Id&
id)
const
110 const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
111 mModel, Akonadi::Collection(
id)
113 return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
116 Akonadi::Collection MoveToTrashCommand::trashCollectionFromResource(
const Akonadi::Collection & col )
119 Akonadi::Collection trashCol;
120 if ( col.isValid() ) {
121 if ( col.resource().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
124 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( col.resource() );
125 if ( iface->isValid() ) {
127 trashCol = Akonadi::Collection( iface->trashCollection() );
137 Akonadi::Collection MoveToTrashCommand::trashCollectionFolder()
139 if ( the_trashCollectionFolder < 0 )
141 return collectionFromId( the_trashCollectionFolder );
145 Akonadi::Collection MoveToTrashCommand::findTrashFolder(
const Akonadi::Collection& folder )
147 Akonadi::Collection col = trashCollectionFromResource( folder );
148 if ( !col.isValid() ) {
149 col = trashCollectionFolder();
153 return Akonadi::Collection();
158 #include "moc_movetotrashcommand_p.cpp"