21 #include "emptytrashcommand_p.h"
23 #include "imapsettings.h"
27 #include <KMessageBox>
29 #include "akonadi/entitytreemodel.h"
30 #include "akonadi/kmime/specialmailcollections.h"
31 #include "akonadi/itemfetchjob.h"
32 #include "akonadi/itemdeletejob.h"
33 #include "akonadi/agentmanager.h"
34 #include "kmime/kmime_message.h"
36 EmptyTrashCommand::EmptyTrashCommand(
const QAbstractItemModel* model, QObject* parent)
37 : CommandBase( parent ),
39 the_trashCollectionFolder( -1 ),
40 mNumberOfTrashToEmpty( 0 )
44 EmptyTrashCommand::EmptyTrashCommand(
const Akonadi::Collection& folder, QObject* parent)
45 : CommandBase( parent ),
47 the_trashCollectionFolder( -1 ),
49 mNumberOfTrashToEmpty( 0 )
54 void EmptyTrashCommand::execute()
56 if ( !mFolder.isValid() && !mModel ) {
61 if ( !mFolder.isValid() ) {
62 const QString title = i18n(
"Empty Trash");
63 const QString text = i18n(
"Are you sure you want to empty the trash folders of all accounts?");
64 if (KMessageBox::warningContinueCancel(0, text, title,
65 KStandardGuiItem::cont(), KStandardGuiItem::cancel(),
66 QLatin1String(
"confirm_empty_trash" ) )
67 != KMessageBox::Continue)
72 Akonadi::Collection trash = trashCollectionFolder();
73 QList<Akonadi::Collection> trashFolder;
76 const Akonadi::AgentInstance::List lst = agentInstances();
77 foreach (
const Akonadi::AgentInstance& type, lst ) {
78 if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
79 if ( type.status() == Akonadi::AgentInstance::Broken )
81 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() );
82 if ( iface->isValid() ) {
83 const int trashImap = iface->trashCollection();
84 if ( trashImap != trash.id() ) {
85 trashFolder<<Akonadi::Collection( trashImap );
91 mNumberOfTrashToEmpty = trashFolder.count();
92 for (
int i = 0; i < mNumberOfTrashToEmpty; ++i) {
93 expunge( trashFolder.at( i ) );
96 if ( folderIsTrash( mFolder ) ) {
97 mNumberOfTrashToEmpty++;
106 void EmptyTrashCommand::expunge(
const Akonadi::Collection & col )
108 if ( col.isValid() ) {
109 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( col,
this );
110 connect( job, SIGNAL(result(KJob*)),
this, SLOT(slotExpungeJob(KJob*)) );
112 kDebug()<<
" Try to expunge an invalid collection :"<<col;
113 emitResult( Failed );
117 void EmptyTrashCommand::slotExpungeJob( KJob *job )
119 if ( job->error() ) {
120 Util::showJobError( job );
121 emitResult( Failed );
124 Akonadi::ItemFetchJob *fjob =
dynamic_cast<Akonadi::ItemFetchJob*
>( job );
126 emitResult( Failed );
129 const Akonadi::Item::List lstItem = fjob->items();
130 if ( lstItem.isEmpty() ) {
134 Akonadi::ItemDeleteJob *jobDelete =
new Akonadi::ItemDeleteJob( lstItem,
this );
135 connect( jobDelete, SIGNAL(result(KJob*)),
this, SLOT(slotDeleteJob(KJob*)) );
139 void EmptyTrashCommand::slotDeleteJob( KJob *job )
141 if ( job->error() ) {
142 Util::showJobError( job );
143 emitResult( Failed );
148 Akonadi::AgentInstance::List EmptyTrashCommand::agentInstances()
150 Akonadi::AgentInstance::List relevantInstances;
151 foreach (
const Akonadi::AgentInstance &instance, Akonadi::AgentManager::self()->instances() ) {
152 if ( instance.type().mimeTypes().contains( KMime::Message::mimeType() ) &&
153 instance.type().capabilities().contains( QLatin1String(
"Resource" ) ) &&
154 !instance.type().capabilities().contains( QLatin1String(
"Virtual" ) ) ) {
155 relevantInstances << instance;
158 return relevantInstances;
161 Akonadi::Collection EmptyTrashCommand::collectionFromId(
const Akonadi::Collection::Id&
id)
const
163 const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
164 mModel, Akonadi::Collection(
id)
166 return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
169 Akonadi::Collection EmptyTrashCommand::trashCollectionFolder()
171 if ( the_trashCollectionFolder < 0 )
173 return collectionFromId( the_trashCollectionFolder );
176 bool EmptyTrashCommand::folderIsTrash(
const Akonadi::Collection & col )
180 const Akonadi::AgentInstance::List lst = agentInstances();
181 foreach (
const Akonadi::AgentInstance& type, lst ) {
182 if ( type.status() == Akonadi::AgentInstance::Broken )
184 if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
185 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() );
186 if ( iface->isValid() ) {
187 if ( iface->trashCollection() == col.id() ) {
198 void EmptyTrashCommand::emitResult( Result value )
200 emit result( value );
201 mNumberOfTrashToEmpty--;
202 if ( mNumberOfTrashToEmpty <= 0 ) {