akonadi/kmime
markascommand.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 "markascommand_p.h" 00022 #include "util_p.h" 00023 #include <akonadi/itemfetchjob.h> 00024 #include <akonadi/itemfetchscope.h> 00025 #include <akonadi/itemmodifyjob.h> 00026 00027 MarkAsCommand::MarkAsCommand( const Akonadi::MessageStatus& targetStatus, const Akonadi::Item::List& msgList, bool invert, QObject* parent): CommandBase( parent ) 00028 { 00029 mInvertMark = invert; 00030 mMessages = msgList; 00031 mTargetStatus = targetStatus; 00032 mFolderListJobCount = 0; 00033 } 00034 00035 MarkAsCommand::MarkAsCommand(const Akonadi::MessageStatus &targetStatus, const Akonadi::Collection::List& folders, bool invert, QObject* parent): CommandBase( parent ) 00036 { 00037 mInvertMark = invert; 00038 mFolders = folders; 00039 mTargetStatus = targetStatus; 00040 mFolderListJobCount = mFolders.size(); 00041 } 00042 00043 void MarkAsCommand::slotFetchDone(KJob* job) 00044 { 00045 mFolderListJobCount--; 00046 00047 if ( job->error() ) { 00048 // handle errors 00049 Util::showJobError(job); 00050 emitResult( Failed ); 00051 return; 00052 } 00053 00054 Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job ); 00055 Q_ASSERT( fjob ); 00056 mMessages.clear(); 00057 foreach( const Akonadi::Item &item, fjob->items() ) { 00058 Akonadi::MessageStatus status; 00059 status.setStatusFromFlags( item.flags() ); 00060 if ( mInvertMark ) { 00061 if ( status & mTargetStatus ) { 00062 mMessages.append( item ); 00063 } 00064 } else 00065 if (! (status & mTargetStatus) ) 00066 { 00067 mMessages.append( item ); 00068 } 00069 } 00070 if ( mMessages.empty() ) { 00071 if( mFolderListJobCount == 0 ) { 00072 emitResult( OK ); 00073 return; 00074 } 00075 } else { 00076 markMessages(); 00077 } 00078 if ( mFolderListJobCount > 0 ) { 00079 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() ); 00080 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent ); 00081 connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) ); 00082 } 00083 } 00084 00085 00086 void MarkAsCommand::execute() 00087 { 00088 if ( !mFolders.isEmpty() ) { 00089 //yes, we go backwards, shouldn't matter 00090 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() ); 00091 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent ); 00092 connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) ); 00093 } else if ( !mMessages.isEmpty() ) { 00094 mFolders << mMessages.first().parentCollection(); 00095 markMessages(); 00096 } else { 00097 emitResult( OK ); 00098 } 00099 } 00100 00101 void MarkAsCommand::markMessages() 00102 { 00103 mMarkJobCount = 0; 00104 00105 QSet<QByteArray> flags = mTargetStatus.statusFlags(); 00106 Q_ASSERT( flags.size() == 1 ); 00107 Akonadi::Item::Flag flag; 00108 if(!flags.isEmpty()) 00109 flag = *(flags.begin()); 00110 00111 Akonadi::Item::List itemsToModify; 00112 foreach( const Akonadi::Item &it, mMessages ) { 00113 Akonadi::Item item( it ); 00114 00115 // be careful to only change the flags we want to change, not to overwrite them 00116 // otherwise ItemModifyJob will not do what we expect 00117 if ( mInvertMark ) { 00118 if ( item.hasFlag( flag ) ) { 00119 item.clearFlag( flag ); 00120 itemsToModify.push_back( item ); 00121 } 00122 } else { 00123 if ( !item.hasFlag( flag ) ) { 00124 item.setFlag( flag ); 00125 itemsToModify.push_back( item ); 00126 } 00127 } 00128 } 00129 00130 mMarkJobCount++; 00131 if ( itemsToModify.isEmpty() ) { 00132 slotModifyItemDone( 0 ); // pretend we did something 00133 } else { 00134 Akonadi::ItemModifyJob *modifyJob = new Akonadi::ItemModifyJob( itemsToModify, this ); 00135 modifyJob->setIgnorePayload( true ); 00136 modifyJob->disableRevisionCheck(); 00137 connect( modifyJob, SIGNAL(result(KJob*)), this, SLOT(slotModifyItemDone(KJob*)) ); 00138 } 00139 } 00140 00141 void MarkAsCommand::slotModifyItemDone( KJob * job ) 00142 { 00143 mMarkJobCount--; 00144 //NOTE(Andras): from kmail/kmmcommands, KMSetStatusCommand 00145 if ( job && job->error() ) { 00146 kDebug()<<" Error trying to set item status:" << job->errorText(); 00147 emitResult( Failed ); 00148 } 00149 if ( mMarkJobCount == 0 && mFolderListJobCount == 0 ) { 00150 emitResult( OK ); 00151 } 00152 } 00153 00154 00155 #include "markascommand_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:49 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:49 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.