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

akonadi/kmime

movetotrashcommand.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 "movetotrashcommand_p.h"
00022 #include "util_p.h"
00023 #include "movecommand_p.h"
00024 #include "imapsettings.h"
00025 
00026 #include <akonadi/itemfetchjob.h>
00027 #include <akonadi/itemfetchscope.h>
00028 #include <akonadi/kmime/specialmailcollections.h>
00029 #include <akonadi/entitytreemodel.h>
00030 
00031 MoveToTrashCommand::MoveToTrashCommand(const QAbstractItemModel* model, const Akonadi::Collection::List& folders, QObject* parent): CommandBase( parent )
00032 {
00033   the_trashCollectionFolder = -1;
00034   mFolders = folders;
00035   mModel = model;
00036   mFolderListJobCount = mFolders.size();
00037 }
00038 
00039 MoveToTrashCommand::MoveToTrashCommand(const QAbstractItemModel* model, const QList< Akonadi::Item >& msgList, QObject* parent): CommandBase( parent )
00040 {
00041   the_trashCollectionFolder = -1;
00042   mMessages = msgList;
00043   mModel = model;
00044   mFolderListJobCount = 0;
00045 }
00046 
00047 
00048 void MoveToTrashCommand::slotFetchDone(KJob* job)
00049 {
00050   mFolderListJobCount--;
00051 
00052   if ( job->error() ) {
00053     // handle errors
00054     Util::showJobError(job);
00055     emitResult( Failed );
00056     return;
00057   }
00058 
00059   Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
00060   Q_ASSERT( fjob );
00061 
00062   mMessages =  fjob->items();
00063   moveMessages();
00064 
00065   if ( mFolderListJobCount > 0 ) {
00066     Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
00067     job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
00068     connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
00069   }
00070 }
00071 
00072 
00073 void MoveToTrashCommand::execute()
00074 {
00075   if ( !mFolders.isEmpty() ) {
00076     Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(  mFolders[mFolderListJobCount - 1], parent() );
00077     job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
00078     connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
00079   } else if ( !mMessages.isEmpty() ) {
00080     mFolders << mMessages.first().parentCollection();
00081     moveMessages();
00082   } else {
00083     emitResult( OK );
00084   }
00085 }
00086 
00087 void MoveToTrashCommand::moveMessages()
00088 {
00089   Akonadi::Collection folder = mFolders[mFolderListJobCount];
00090   if ( folder.isValid() ) {
00091     MoveCommand *moveCommand = new MoveCommand( findTrashFolder( folder ), mMessages, this );
00092     connect( moveCommand, SIGNAL(result(Result)), this, SLOT(slotMoveDone(Result)) );
00093     moveCommand->execute();
00094   } else {
00095     emitResult( Failed );
00096   }
00097 }
00098 
00099 void MoveToTrashCommand::slotMoveDone( const Result& result )
00100 {
00101   if (result == Failed )
00102     emitResult( Failed );
00103   if ( mFolderListJobCount == 0 && result == OK) {
00104     emitResult( OK );
00105   }
00106 }
00107 
00108 Akonadi::Collection MoveToTrashCommand::collectionFromId(const Akonadi::Collection::Id& id) const
00109 {
00110   const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
00111     mModel, Akonadi::Collection(id)
00112   );
00113   return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
00114 }
00115 
00116 Akonadi::Collection MoveToTrashCommand::trashCollectionFromResource( const Akonadi::Collection & col )
00117 {
00118   //NOTE(Andras): from kmail/kmkernel.cpp
00119   Akonadi::Collection trashCol;
00120   if ( col.isValid() ) {
00121     if ( col.resource().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
00122       //TODO: we really need some standard interface to query for special collections,
00123       //instead of relying on a resource's settings interface
00124       OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( col.resource() );
00125       if ( iface->isValid() ) {
00126 
00127         trashCol =  Akonadi::Collection( iface->trashCollection() );
00128         delete iface;
00129         return trashCol;
00130       }
00131       delete iface;
00132     }
00133   }
00134   return trashCol;
00135 }
00136 
00137 Akonadi::Collection MoveToTrashCommand::trashCollectionFolder()
00138 {
00139   if ( the_trashCollectionFolder < 0 )
00140     the_trashCollectionFolder = Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ).id();
00141   return collectionFromId( the_trashCollectionFolder );
00142 }
00143 
00144 
00145 Akonadi::Collection MoveToTrashCommand::findTrashFolder( const Akonadi::Collection& folder )
00146 {
00147   Akonadi::Collection col = trashCollectionFromResource( folder );
00148   if ( !col.isValid() ) {
00149     col = trashCollectionFolder();
00150   }
00151   if ( folder != col )
00152     return col;
00153   return Akonadi::Collection();
00154 }
00155 
00156 
00157 
00158 #include "movetotrashcommand_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:59 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/kmime

Skip menu "akonadi/kmime"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.8.5 API Reference

Skip menu "kdepimlibs-4.8.5 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