akonadi
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 const Akonadi::Item::Flag flag = *(flags.begin()); 00108 Akonadi::Item::List itemsToModify; 00109 foreach( const Akonadi::Item &it, mMessages ) { 00110 Akonadi::Item item( it ); 00111 00112 // be careful to only change the flags we want to change, not to overwrite them 00113 // otherwise ItemModifyJob will not do what we expect 00114 if ( mInvertMark ) { 00115 if ( item.hasFlag( flag ) ) { 00116 item.clearFlag( flag ); 00117 itemsToModify.push_back( item ); 00118 } 00119 } else { 00120 if ( !item.hasFlag( flag ) ) { 00121 item.setFlag( flag ); 00122 itemsToModify.push_back( item ); 00123 } 00124 } 00125 } 00126 00127 mMarkJobCount++; 00128 if ( itemsToModify.isEmpty() ) { 00129 slotModifyItemDone( 0 ); // pretend we did something 00130 } else { 00131 Akonadi::ItemModifyJob *modifyJob = new Akonadi::ItemModifyJob( itemsToModify, this ); 00132 modifyJob->setIgnorePayload( true ); 00133 modifyJob->disableRevisionCheck(); 00134 connect( modifyJob, SIGNAL(result(KJob*)), this, SLOT(slotModifyItemDone(KJob*)) ); 00135 } 00136 } 00137 00138 void MarkAsCommand::slotModifyItemDone( KJob * job ) 00139 { 00140 mMarkJobCount--; 00141 //NOTE(Andras): from kmail/kmmcommands, KMSetStatusCommand 00142 if ( job && job->error() ) { 00143 kDebug()<<" Error trying to set item status:" << job->errorText(); 00144 emitResult( Failed ); 00145 } 00146 if ( mMarkJobCount == 0 && mFolderListJobCount == 0 ) { 00147 emitResult( OK ); 00148 } 00149 } 00150 00151 00152 #include "markascommand_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.