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 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 emitResult( OK ); 00072 return; 00073 } 00074 00075 markMessages(); 00076 00077 if ( mFolderListJobCount > 0 ) { 00078 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() ); 00079 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent ); 00080 connect( job, SIGNAL( result( KJob* ) ), this, SLOT( slotFetchDone( KJob* ) ) ); 00081 } 00082 } 00083 00084 00085 void MarkAsCommand::execute() 00086 { 00087 if ( !mFolders.isEmpty() ) { 00088 //yes, we go backwards, shouldn't matter 00089 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() ); 00090 job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent ); 00091 connect( job, SIGNAL( result( KJob* ) ), this, SLOT( slotFetchDone( KJob* ) ) ); 00092 } else if ( !mMessages.isEmpty() ) { 00093 mFolders << mMessages.first().parentCollection(); 00094 markMessages(); 00095 } else { 00096 emitResult( OK ); 00097 } 00098 } 00099 00100 void MarkAsCommand::markMessages() 00101 { 00102 mMarkJobCount = 0; 00103 00104 Q_ASSERT( mTargetStatus.statusFlags().size() == 1 ); 00105 const Akonadi::Item::Flag flag = *(mTargetStatus.statusFlags().begin()); 00106 00107 Akonadi::Item::List itemsToModify; 00108 foreach( const Akonadi::Item &it, mMessages ) { 00109 Akonadi::Item item( it ); 00110 00111 // be careful to only change the flags we want to change, not to overwrite them 00112 // otherwise ItemModifyJob will not do what we expect 00113 if ( mInvertMark ) { 00114 if ( item.hasFlag( flag ) ) { 00115 item.clearFlag( flag ); 00116 itemsToModify.push_back( item ); 00117 } 00118 } else { 00119 if ( !item.hasFlag( flag ) ) { 00120 item.setFlag( flag ); 00121 itemsToModify.push_back( item ); 00122 } 00123 } 00124 } 00125 00126 mMarkJobCount++; 00127 if ( itemsToModify.isEmpty() ) { 00128 slotModifyItemDone( 0 ); // pretend we did something 00129 } else { 00130 Akonadi::ItemModifyJob *modifyJob = new Akonadi::ItemModifyJob( itemsToModify, this ); 00131 modifyJob->setIgnorePayload( true ); 00132 modifyJob->disableRevisionCheck(); 00133 connect( modifyJob, SIGNAL( result( KJob* ) ), this, SLOT( slotModifyItemDone( KJob* ) ) ); 00134 } 00135 } 00136 00137 void MarkAsCommand::slotModifyItemDone( KJob * job ) 00138 { 00139 mMarkJobCount--; 00140 //NOTE(Andras): from kmail/kmmcommands, KMSetStatusCommand 00141 if ( job && job->error() ) { 00142 kDebug()<<" Error trying to set item status:" << job->errorText(); 00143 emitResult( Failed ); 00144 } 00145 if ( mMarkJobCount == 0 && mFolderListJobCount == 0 ) { 00146 emitResult( OK ); 00147 } 00148 } 00149 00150 00151 #include "markascommand_p.moc"