akonadi/kmime
emptytrashcommand.cpp
00001 /* 00002 Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com 00003 Copyright (c) 2010 Andras Mantia <andras@kdab.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 00021 #include "emptytrashcommand_p.h" 00022 #include "util_p.h" 00023 #include "imapsettings.h" 00024 00025 #include <KDebug> 00026 #include <KLocale> 00027 #include <KMessageBox> 00028 00029 #include "akonadi/entitytreemodel.h" 00030 #include "akonadi/kmime/specialmailcollections.h" 00031 #include "akonadi/itemfetchjob.h" 00032 #include "akonadi/itemdeletejob.h" 00033 #include "akonadi/agentmanager.h" 00034 #include "kmime/kmime_message.h" 00035 00036 EmptyTrashCommand::EmptyTrashCommand(const QAbstractItemModel* model, QObject* parent) : CommandBase( parent ) 00037 { 00038 the_trashCollectionFolder = -1; 00039 mModel = model; 00040 } 00041 00042 EmptyTrashCommand::EmptyTrashCommand(Akonadi::Collection folder, QObject* parent) : CommandBase( parent ) 00043 { 00044 mFolder = folder; 00045 mModel = 0; 00046 } 00047 00048 00049 void EmptyTrashCommand::execute() 00050 { 00051 if ( !mFolder.isValid() && !mModel ) { 00052 emitResult( Failed ); 00053 return; 00054 } 00055 00056 if ( !mFolder.isValid() ) { //expunge all 00057 00058 QString title = i18n("Empty Trash"); 00059 QString text = i18n("Are you sure you want to empty the trash folders of all accounts?"); 00060 if (KMessageBox::warningContinueCancel(0, text, title, 00061 KStandardGuiItem::cont(), KStandardGuiItem::cancel(), 00062 QLatin1String( "confirm_empty_trash" ) ) 00063 != KMessageBox::Continue) 00064 { 00065 return; 00066 } 00067 Akonadi::Collection trash = trashCollectionFolder(); 00068 expunge( trash ); 00069 00070 const Akonadi::AgentInstance::List lst = agentInstances(); 00071 foreach ( const Akonadi::AgentInstance& type, lst ) { 00072 if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) { 00073 if ( type.status() == Akonadi::AgentInstance::Broken ) 00074 continue; 00075 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() ); 00076 if ( iface->isValid() ) { 00077 int trashImap = iface->trashCollection(); 00078 if ( trashImap != trash.id() ) { 00079 expunge( Akonadi::Collection( trashImap ) ); 00080 } 00081 } 00082 delete iface; 00083 } 00084 } 00085 } else { 00086 if ( folderIsTrash( mFolder ) ) { 00087 expunge( mFolder ); 00088 } else { 00089 emitResult( OK ); 00090 } 00091 00092 } 00093 } 00094 00095 void EmptyTrashCommand::expunge( const Akonadi::Collection & col ) 00096 { 00097 if ( col.isValid() ) { 00098 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( col,this ); 00099 connect( job, SIGNAL( result( KJob* ) ), this, SLOT( slotExpungeJob( KJob* ) ) ); 00100 } else { 00101 kDebug()<<" Try to expunge an invalid collection :"<<col; 00102 } 00103 } 00104 00105 void EmptyTrashCommand::slotExpungeJob( KJob *job ) 00106 { 00107 if ( job->error() ) { 00108 Util::showJobError( job ); 00109 emitResult( Failed ); 00110 return; 00111 } 00112 Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job ); 00113 if ( !fjob ) { 00114 emitResult( Failed ); 00115 return; 00116 } 00117 const Akonadi::Item::List lstItem = fjob->items(); 00118 if ( lstItem.isEmpty() ) { 00119 emitResult( OK ); 00120 return; 00121 } 00122 Akonadi::ItemDeleteJob *jobDelete = new Akonadi::ItemDeleteJob( lstItem, this ); 00123 connect( jobDelete, SIGNAL( result( KJob* ) ), this, SLOT( slotDeleteJob( KJob* ) ) ); 00124 00125 } 00126 00127 void EmptyTrashCommand::slotDeleteJob( KJob *job ) 00128 { 00129 if ( job->error() ) { 00130 Util::showJobError( job ); 00131 emitResult( Failed ); 00132 } 00133 emitResult( OK ); 00134 } 00135 00136 Akonadi::AgentInstance::List EmptyTrashCommand::agentInstances() 00137 { 00138 Akonadi::AgentInstance::List relevantInstances; 00139 foreach ( const Akonadi::AgentInstance &instance, Akonadi::AgentManager::self()->instances() ) { 00140 if ( instance.type().mimeTypes().contains( KMime::Message::mimeType() ) && 00141 instance.type().capabilities().contains( QLatin1String( "Resource" ) ) && 00142 !instance.type().capabilities().contains( QLatin1String( "Virtual" ) ) ) { 00143 relevantInstances << instance; 00144 } 00145 } 00146 return relevantInstances; 00147 } 00148 00149 Akonadi::Collection EmptyTrashCommand::collectionFromId(const Akonadi::Collection::Id& id) const 00150 { 00151 const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection( 00152 mModel, Akonadi::Collection(id) 00153 ); 00154 return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>(); 00155 } 00156 00157 Akonadi::Collection EmptyTrashCommand::trashCollectionFolder() 00158 { 00159 if ( the_trashCollectionFolder < 0 ) 00160 the_trashCollectionFolder = Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ).id(); 00161 return collectionFromId( the_trashCollectionFolder ); 00162 } 00163 00164 bool EmptyTrashCommand::folderIsTrash( const Akonadi::Collection & col ) 00165 { 00166 if ( col == Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ) ) 00167 return true; 00168 const Akonadi::AgentInstance::List lst = agentInstances(); 00169 foreach ( const Akonadi::AgentInstance& type, lst ) { 00170 if ( type.status() == Akonadi::AgentInstance::Broken ) 00171 continue; 00172 if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) { 00173 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() ); 00174 if ( iface->isValid() ) { 00175 if ( iface->trashCollection() == col.id() ) { 00176 delete iface; 00177 return true; 00178 } 00179 } 00180 delete iface; 00181 } 00182 } 00183 return false; 00184 }