• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi

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)
00037   : CommandBase( parent ),
00038     mModel( model ), 
00039     the_trashCollectionFolder( -1 ),
00040     mNumberOfTrashToEmpty( 0 )
00041 {
00042 }
00043 
00044 EmptyTrashCommand::EmptyTrashCommand(const Akonadi::Collection& folder, QObject* parent)
00045   : CommandBase( parent ),
00046     mModel( 0 ), 
00047     the_trashCollectionFolder( -1 ),
00048     mFolder( folder ),
00049     mNumberOfTrashToEmpty( 0 )
00050 {
00051 }
00052 
00053 
00054 void EmptyTrashCommand::execute()
00055 {
00056   if ( !mFolder.isValid() && !mModel ) {
00057     emitResult( Failed );
00058     return;
00059   }
00060 
00061   if ( !mFolder.isValid() ) { //expunge all
00062     const QString title = i18n("Empty Trash");
00063     const QString text = i18n("Are you sure you want to empty the trash folders of all accounts?");
00064     if (KMessageBox::warningContinueCancel(0, text, title,
00065                                           KStandardGuiItem::cont(), KStandardGuiItem::cancel(),
00066                                           QLatin1String( "confirm_empty_trash" ) )
00067         != KMessageBox::Continue)
00068     {
00069       emitResult( OK );
00070       return;
00071     }
00072     Akonadi::Collection trash = trashCollectionFolder();
00073     QList<Akonadi::Collection> trashFolder;
00074     trashFolder<<trash;
00075 
00076     const Akonadi::AgentInstance::List lst = agentInstances();
00077     foreach ( const Akonadi::AgentInstance& type, lst ) {
00078       if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
00079         if ( type.status() == Akonadi::AgentInstance::Broken )
00080           continue;
00081         OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() );
00082         if ( iface->isValid() ) {
00083           const int trashImap = iface->trashCollection();
00084           if ( trashImap != trash.id() ) {
00085             trashFolder<<Akonadi::Collection( trashImap );
00086           }
00087         }
00088         delete iface;
00089       }
00090     }
00091     mNumberOfTrashToEmpty = trashFolder.count();
00092     for (int i = 0; i < mNumberOfTrashToEmpty; ++i) {
00093       expunge( trashFolder.at( i ) );
00094     }
00095   } else {
00096     if ( folderIsTrash( mFolder ) ) {
00097       mNumberOfTrashToEmpty++;
00098       expunge( mFolder );
00099     } else {
00100       emitResult( OK ); 
00101     }
00102     
00103   }
00104 }
00105 
00106 void EmptyTrashCommand::expunge( const Akonadi::Collection & col )
00107 {
00108   if ( col.isValid() ) {
00109     Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( col,this );
00110     connect( job, SIGNAL(result(KJob*)), this, SLOT(slotExpungeJob(KJob*)) );
00111   } else {
00112     kDebug()<<" Try to expunge an invalid collection :"<<col;
00113     emitResult( Failed ); 
00114   }
00115 }
00116 
00117 void EmptyTrashCommand::slotExpungeJob( KJob *job )
00118 {
00119   if ( job->error() ) {
00120     Util::showJobError( job );
00121     emitResult( Failed );
00122     return;
00123   }
00124   Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
00125   if ( !fjob ) {
00126     emitResult( Failed );
00127     return;
00128   }
00129   const Akonadi::Item::List lstItem = fjob->items();
00130   if ( lstItem.isEmpty() ) {
00131     emitResult( OK );
00132     return;
00133   }
00134   Akonadi::ItemDeleteJob *jobDelete = new Akonadi::ItemDeleteJob( lstItem, this );
00135   connect( jobDelete, SIGNAL(result(KJob*)), this, SLOT(slotDeleteJob(KJob*)) );
00136 
00137 }
00138 
00139 void EmptyTrashCommand::slotDeleteJob( KJob *job )
00140 {
00141   if ( job->error() ) {
00142     Util::showJobError( job );
00143     emitResult( Failed );
00144   }
00145   emitResult( OK );
00146 }
00147 
00148 Akonadi::AgentInstance::List EmptyTrashCommand::agentInstances()
00149 {
00150   Akonadi::AgentInstance::List relevantInstances;
00151   foreach ( const Akonadi::AgentInstance &instance, Akonadi::AgentManager::self()->instances() ) {
00152     if ( instance.type().mimeTypes().contains( KMime::Message::mimeType() ) &&
00153          instance.type().capabilities().contains( QLatin1String( "Resource" ) ) &&
00154          !instance.type().capabilities().contains( QLatin1String( "Virtual" ) ) ) {
00155       relevantInstances << instance;
00156     }
00157   }
00158   return relevantInstances;
00159 }
00160 
00161 Akonadi::Collection EmptyTrashCommand::collectionFromId(const Akonadi::Collection::Id& id) const
00162 {
00163   const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
00164     mModel, Akonadi::Collection(id)
00165   );
00166   return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
00167 }
00168 
00169 Akonadi::Collection EmptyTrashCommand::trashCollectionFolder()
00170 {
00171   if ( the_trashCollectionFolder < 0 )
00172     the_trashCollectionFolder = Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ).id();
00173   return collectionFromId( the_trashCollectionFolder );
00174 }
00175 
00176 bool EmptyTrashCommand::folderIsTrash( const Akonadi::Collection & col )
00177 {
00178   if ( col == Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ) )
00179     return true;
00180   const Akonadi::AgentInstance::List lst = agentInstances();
00181   foreach ( const Akonadi::AgentInstance& type, lst ) {
00182     if ( type.status() == Akonadi::AgentInstance::Broken )
00183       continue;
00184     if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
00185       OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() );
00186       if ( iface->isValid() ) {
00187         if ( iface->trashCollection() == col.id() ) {
00188           delete iface;
00189           return true;
00190         }
00191       }
00192       delete iface;
00193     }
00194   }
00195   return false;
00196 }
00197 
00198 void EmptyTrashCommand::emitResult( Result value )
00199 {
00200   emit result( value );
00201   mNumberOfTrashToEmpty--;
00202   if ( mNumberOfTrashToEmpty <= 0 ) {
00203     deleteLater();
00204   }
00205 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:32 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal