• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 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#include "markascommand_p.h"
21#include "util_p.h"
22#include <akonadi/itemfetchjob.h>
23#include <akonadi/itemfetchscope.h>
24#include <akonadi/itemmodifyjob.h>
25
26MarkAsCommand::MarkAsCommand(const Akonadi::MessageStatus &targetStatus, const Akonadi::Item::List &msgList, bool invert, QObject *parent)
27 : CommandBase(parent)
28{
29 mInvertMark = invert;
30 mMessages = msgList;
31 mTargetStatus = targetStatus;
32 mFolderListJobCount = 0;
33 mMarkJobCount = 0;
34}
35
36MarkAsCommand::MarkAsCommand(const Akonadi::MessageStatus &targetStatus, const Akonadi::Collection::List &folders, bool invert, QObject *parent)
37 : CommandBase(parent)
38{
39 mInvertMark = invert;
40 mFolders = folders;
41 mTargetStatus = targetStatus;
42 mFolderListJobCount = mFolders.size();
43 mMarkJobCount = 0;
44}
45
46void MarkAsCommand::slotFetchDone(KJob *job)
47{
48 mFolderListJobCount--;
49
50 if (job->error()) {
51 // handle errors
52 Util::showJobError(job);
53 emitResult(Failed);
54 return;
55 }
56
57 Akonadi::ItemFetchJob *fjob = static_cast<Akonadi::ItemFetchJob *>(job);
58 mMessages.clear();
59 foreach (const Akonadi::Item &item, fjob->items()) {
60 Akonadi::MessageStatus status;
61 status.setStatusFromFlags(item.flags());
62 if (mInvertMark) {
63 if (status & mTargetStatus) {
64 mMessages.append(item);
65 }
66 } else if (!(status & mTargetStatus)) {
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
85void 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
100void 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
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
141void 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#include "moc_markascommand_p.cpp"
Akonadi::MessageStatus
Akonadi KMime Message Status.
Definition messagestatus.h:52
Akonadi::MessageStatus::setStatusFromFlags
void setStatusFromFlags(const QSet< QByteArray > &flags)
Set the status as a whole e.g.
Definition messagestatus.cpp:638
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Wed Jan 24 2024 00:00:00 by doxygen 1.10.0 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.14.10 API Reference

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