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

akonadi/kmime

  • 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 = dynamic_cast<Akonadi::ItemFetchJob*>( job );
55  Q_ASSERT( fjob );
56  mMessages.clear();
57  foreach( const Akonadi::Item &item, fjob->items() ) {
58  Akonadi::MessageStatus status;
59  status.setStatusFromFlags( item.flags() );
60  if ( mInvertMark ) {
61  if ( status & mTargetStatus ) {
62  mMessages.append( item );
63  }
64  } else
65  if ( !( status & mTargetStatus ) ) {
66  mMessages.append( item );
67  }
68  }
69  if ( mMessages.empty() ) {
70  if( mFolderListJobCount == 0 ) {
71  emitResult( OK );
72  return;
73  }
74  } else {
75  markMessages();
76  }
77  if ( mFolderListJobCount > 0 ) {
78  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
79  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
80  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
81  }
82 }
83 
84 
85 void MarkAsCommand::execute()
86 {
87  if ( !mFolders.isEmpty() ) {
88  //yes, we go backwards, shouldn't matter
89  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
90  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
91  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
92  } else if ( !mMessages.isEmpty() ) {
93  mFolders << mMessages.first().parentCollection();
94  markMessages();
95  } else {
96  emitResult( OK );
97  }
98 }
99 
100 void MarkAsCommand::markMessages()
101 {
102  mMarkJobCount = 0;
103 
104  QSet<QByteArray> flags = mTargetStatus.statusFlags();
105  Q_ASSERT( flags.size() == 1 );
106  Akonadi::Item::Flag flag;
107  if ( !flags.isEmpty() )
108  flag = *( flags.begin() );
109 
110  Akonadi::Item::List itemsToModify;
111  foreach( const Akonadi::Item &it, mMessages ) {
112  Akonadi::Item item( it );
113 
114  // be careful to only change the flags we want to change, not to overwrite them
115  // otherwise ItemModifyJob will not do what we expect
116  if ( mInvertMark ) {
117  if ( item.hasFlag( flag ) ) {
118  item.clearFlag( flag );
119  itemsToModify.push_back( item );
120  }
121  } else {
122  if ( !item.hasFlag( flag ) ) {
123  item.setFlag( flag );
124  itemsToModify.push_back( item );
125  }
126  }
127  }
128 
129  mMarkJobCount++;
130  if ( itemsToModify.isEmpty() ) {
131  slotModifyItemDone( 0 ); // pretend we did something
132  } else {
133  Akonadi::ItemModifyJob *modifyJob = new Akonadi::ItemModifyJob( itemsToModify, this );
134  modifyJob->setIgnorePayload( true );
135  modifyJob->disableRevisionCheck();
136  connect( modifyJob, SIGNAL(result(KJob*)), this, SLOT(slotModifyItemDone(KJob*)) );
137  }
138 }
139 
140 void MarkAsCommand::slotModifyItemDone( KJob * job )
141 {
142  mMarkJobCount--;
143  //NOTE(Andras): from kmail/kmmcommands, KMSetStatusCommand
144  if ( job && job->error() ) {
145  kDebug()<<" Error trying to set item status:" << job->errorText();
146  emitResult( Failed );
147  }
148  if ( mMarkJobCount == 0 && mFolderListJobCount == 0 ) {
149  emitResult( OK );
150  }
151 }
152 
153 
154 #include "moc_markascommand_p.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:28:59 by doxygen 1.8.3.1 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.10.5 API Reference

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