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

akonadi

  • akonadi
  • kmime
markascommand.cpp
1 /*
2  Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
3  Copyright (c) 2010 Andras Mantia <andras@kdab.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 
21 #include "markascommand_p.h"
22 #include "util_p.h"
23 #include <akonadi/itemfetchjob.h>
24 #include <akonadi/itemfetchscope.h>
25 #include <akonadi/itemmodifyjob.h>
26 
27 MarkAsCommand::MarkAsCommand( const Akonadi::MessageStatus& targetStatus, const Akonadi::Item::List& msgList, bool invert, QObject* parent): CommandBase( parent )
28 {
29  mInvertMark = invert;
30  mMessages = msgList;
31  mTargetStatus = targetStatus;
32  mFolderListJobCount = 0;
33 }
34 
35 MarkAsCommand::MarkAsCommand(const Akonadi::MessageStatus &targetStatus, const Akonadi::Collection::List& folders, bool invert, QObject* parent): CommandBase( parent )
36 {
37  mInvertMark = invert;
38  mFolders = folders;
39  mTargetStatus = targetStatus;
40  mFolderListJobCount = mFolders.size();
41 }
42 
43 void MarkAsCommand::slotFetchDone(KJob* job)
44 {
45  mFolderListJobCount--;
46 
47  if ( job->error() ) {
48  // handle errors
49  Util::showJobError(job);
50  emitResult( Failed );
51  return;
52  }
53 
54  Akonadi::ItemFetchJob *fjob = static_cast<Akonadi::ItemFetchJob*>( job );
55  mMessages.clear();
56  foreach( const Akonadi::Item &item, fjob->items() ) {
57  Akonadi::MessageStatus status;
58  status.setStatusFromFlags( item.flags() );
59  if ( mInvertMark ) {
60  if ( status & mTargetStatus ) {
61  mMessages.append( item );
62  }
63  } else
64  if ( !( status & mTargetStatus ) ) {
65  mMessages.append( item );
66  }
67  }
68  if ( mMessages.empty() ) {
69  if( mFolderListJobCount == 0 ) {
70  emitResult( OK );
71  return;
72  }
73  } else {
74  markMessages();
75  }
76  if ( mFolderListJobCount > 0 ) {
77  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
78  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
79  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
80  }
81 }
82 
83 
84 void MarkAsCommand::execute()
85 {
86  if ( !mFolders.isEmpty() ) {
87  //yes, we go backwards, shouldn't matter
88  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
89  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
90  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
91  } else if ( !mMessages.isEmpty() ) {
92  mFolders << mMessages.first().parentCollection();
93  markMessages();
94  } else {
95  emitResult( OK );
96  }
97 }
98 
99 void MarkAsCommand::markMessages()
100 {
101  mMarkJobCount = 0;
102 
103  QSet<QByteArray> flags = mTargetStatus.statusFlags();
104  Q_ASSERT( flags.size() == 1 );
105  Akonadi::Item::Flag flag;
106  if ( !flags.isEmpty() )
107  flag = *( flags.begin() );
108 
109  Akonadi::Item::List itemsToModify;
110  foreach( const Akonadi::Item &it, mMessages ) {
111  Akonadi::Item item( it );
112 
113  // be careful to only change the flags we want to change, not to overwrite them
114  // otherwise ItemModifyJob will not do what we expect
115  if ( mInvertMark ) {
116  if ( item.hasFlag( flag ) ) {
117  item.clearFlag( flag );
118  itemsToModify.push_back( item );
119  }
120  } else {
121  if ( !item.hasFlag( flag ) ) {
122  item.setFlag( flag );
123  itemsToModify.push_back( item );
124  }
125  }
126  }
127 
128  mMarkJobCount++;
129  if ( itemsToModify.isEmpty() ) {
130  slotModifyItemDone( 0 ); // pretend we did something
131  } else {
132  Akonadi::ItemModifyJob *modifyJob = new Akonadi::ItemModifyJob( itemsToModify, this );
133  modifyJob->setIgnorePayload( true );
134  modifyJob->disableRevisionCheck();
135  connect( modifyJob, SIGNAL(result(KJob*)), this, SLOT(slotModifyItemDone(KJob*)) );
136  }
137 }
138 
139 void MarkAsCommand::slotModifyItemDone( KJob * job )
140 {
141  mMarkJobCount--;
142  //NOTE(Andras): from kmail/kmmcommands, KMSetStatusCommand
143  if ( job && job->error() ) {
144  kDebug()<<" Error trying to set item status:" << job->errorText();
145  emitResult( Failed );
146  }
147  if ( mMarkJobCount == 0 && mFolderListJobCount == 0 ) {
148  emitResult( OK );
149  }
150 }
151 
152 
153 #include "moc_markascommand_p.cpp"
Akonadi::ItemModifyJob::disableRevisionCheck
void disableRevisionCheck()
Disables the check of the revision number.
Definition: itemmodifyjob.cpp:352
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched item.
Definition: itemfetchjob.cpp:227
Akonadi::ItemFetchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition: itemfetchjob.cpp:248
Akonadi::MessageStatus::setStatusFromFlags
void setStatusFromFlags(const QSet< QByteArray > &flags)
Set the status as a whole e.g.
Definition: messagestatus.cpp:621
Akonadi::ItemFetchScope::Parent
Only retrieve the immediate parent collection.
Definition: itemfetchscope.h:77
Akonadi::ItemFetchScope::setAncestorRetrieval
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval. ...
Definition: itemfetchscope.cpp:129
Akonadi::ItemModifyJob::setIgnorePayload
void setIgnorePayload(bool ignore)
Sets whether the payload of the modified item shall be omitted from transmission to the Akonadi stora...
Definition: itemmodifyjob.cpp:329
Akonadi::ItemModifyJob
Job that modifies an existing item in the Akonadi storage.
Definition: itemmodifyjob.h:97
Akonadi::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:82
Akonadi::MessageStatus
Akonadi KMime Message Status.
Definition: messagestatus.h:51
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition: collection.h:81
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:18 by doxygen 1.8.5 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.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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