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

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