20 #include "emptytrashcommand_p.h"
22 #include "imapsettings.h"
25 #include <KLocalizedString>
26 #include <KMessageBox>
28 #include "akonadi/entitytreemodel.h"
29 #include "akonadi/kmime/specialmailcollections.h"
30 #include "akonadi/itemfetchjob.h"
31 #include "akonadi/itemdeletejob.h"
32 #include "akonadi/agentmanager.h"
33 #include "kmime/kmime_message.h"
35 EmptyTrashCommand::EmptyTrashCommand(
const QAbstractItemModel* model, QObject* parent)
36 : CommandBase( parent ),
38 the_trashCollectionFolder( -1 ),
39 mNumberOfTrashToEmpty( 0 )
43 EmptyTrashCommand::EmptyTrashCommand(
const Akonadi::Collection& folder, QObject* parent)
44 : CommandBase( parent ),
46 the_trashCollectionFolder( -1 ),
48 mNumberOfTrashToEmpty( 0 )
52 void EmptyTrashCommand::execute()
54 if ( !mFolder.isValid() && !mModel ) {
59 if ( !mFolder.isValid() ) {
60 const QString title = i18n(
"Empty Trash");
61 const QString text = i18n(
"Are you sure you want to empty the trash folders of all accounts?");
62 if (KMessageBox::warningContinueCancel(0, text, title,
63 KStandardGuiItem::cont(), KStandardGuiItem::cancel(),
64 QLatin1String(
"confirm_empty_trash" ) )
65 != KMessageBox::Continue)
70 Akonadi::Collection trash = trashCollectionFolder();
71 QList<Akonadi::Collection> trashFolder;
74 const Akonadi::AgentInstance::List lst = agentInstances();
75 foreach (
const Akonadi::AgentInstance& type, lst ) {
76 if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
77 if ( type.status() == Akonadi::AgentInstance::Broken )
79 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() );
80 if ( iface->isValid() ) {
81 const int trashImap = iface->trashCollection();
82 if ( trashImap != trash.id() ) {
83 trashFolder<<Akonadi::Collection( trashImap );
89 mNumberOfTrashToEmpty = trashFolder.count();
90 for (
int i = 0; i < mNumberOfTrashToEmpty; ++i) {
91 expunge( trashFolder.at( i ) );
94 if ( folderIsTrash( mFolder ) ) {
95 mNumberOfTrashToEmpty++;
104 void EmptyTrashCommand::expunge(
const Akonadi::Collection & col )
106 if ( col.isValid() ) {
107 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob( col,
this );
108 connect( job, SIGNAL(result(KJob*)),
this, SLOT(slotExpungeJob(KJob*)) );
110 kDebug()<<
" Try to expunge an invalid collection :"<<col;
111 emitResult( Failed );
115 void EmptyTrashCommand::slotExpungeJob( KJob *job )
117 if ( job->error() ) {
118 Util::showJobError( job );
119 emitResult( Failed );
122 Akonadi::ItemFetchJob *fjob =
dynamic_cast<Akonadi::ItemFetchJob*
>( job );
124 emitResult( Failed );
127 const Akonadi::Item::List lstItem = fjob->items();
128 if ( lstItem.isEmpty() ) {
132 Akonadi::ItemDeleteJob *jobDelete =
new Akonadi::ItemDeleteJob( lstItem,
this );
133 connect( jobDelete, SIGNAL(result(KJob*)),
this, SLOT(slotDeleteJob(KJob*)) );
137 void EmptyTrashCommand::slotDeleteJob( KJob *job )
139 if ( job->error() ) {
140 Util::showJobError( job );
141 emitResult( Failed );
146 Akonadi::AgentInstance::List EmptyTrashCommand::agentInstances()
148 Akonadi::AgentInstance::List relevantInstances;
149 foreach (
const Akonadi::AgentInstance &instance, Akonadi::AgentManager::self()->instances() ) {
150 if ( instance.type().mimeTypes().contains( KMime::Message::mimeType() ) &&
151 instance.type().capabilities().contains( QLatin1String(
"Resource" ) ) &&
152 !instance.type().capabilities().contains( QLatin1String(
"Virtual" ) ) ) {
153 relevantInstances << instance;
156 return relevantInstances;
159 Akonadi::Collection EmptyTrashCommand::collectionFromId(
const Akonadi::Collection::Id&
id)
const
161 const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
162 mModel, Akonadi::Collection(
id)
164 return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
167 Akonadi::Collection EmptyTrashCommand::trashCollectionFolder()
169 if ( the_trashCollectionFolder < 0 )
171 return collectionFromId( the_trashCollectionFolder );
174 bool EmptyTrashCommand::folderIsTrash(
const Akonadi::Collection & col )
178 const Akonadi::AgentInstance::List lst = agentInstances();
179 foreach (
const Akonadi::AgentInstance& type, lst ) {
180 if ( type.status() == Akonadi::AgentInstance::Broken )
182 if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
183 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() );
184 if ( iface->isValid() ) {
185 if ( iface->trashCollection() == col.id() ) {
196 void EmptyTrashCommand::emitResult( Result value )
198 emit result( value );
199 mNumberOfTrashToEmpty--;
200 if ( mNumberOfTrashToEmpty <= 0 ) {
static SpecialMailCollections * self()
Returns the global SpecialMailCollections instance.
Akonadi::Collection defaultCollection(Type type) const
Returns the special mail collection of given type in the default resource, or an invalid collection i...