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

akonadi

  • akonadi
  • kmime
emptytrashcommand.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 "emptytrashcommand_p.h"
21#include "util_p.h"
22#include "imapsettings.h"
23
24#include <KDebug>
25#include <KLocalizedString>
26#include <KMessageBox>
27
28#include "akonadi/entitytreemodel.h"
29#include "akonadi/kmime/specialmailcollections.h"
30#include "akonadi/itemfetchjob.h"
31#include "akonadi/itemdeletejob.h"
32#include "akonadi/agentmanager.h"
33#include "kmime/kmime_message.h"
34
35EmptyTrashCommand::EmptyTrashCommand(const QAbstractItemModel *model, QObject *parent)
36 : CommandBase(parent)
37 , mModel(model)
38 , the_trashCollectionFolder(-1)
39 , mNumberOfTrashToEmpty(0)
40{
41}
42
43EmptyTrashCommand::EmptyTrashCommand(const Akonadi::Collection &folder, QObject *parent)
44 : CommandBase(parent)
45 , mModel(0)
46 , the_trashCollectionFolder(-1)
47 , mFolder(folder)
48 , mNumberOfTrashToEmpty(0)
49{
50}
51
52void EmptyTrashCommand::execute()
53{
54 if (!mFolder.isValid() && !mModel) {
55 emitResult(Failed);
56 return;
57 }
58
59 if (!mFolder.isValid()) { //expunge all
60 const QString title = i18n("Empty Trash");
61 const QString text = i18n("Are you sure you want to empty the trash folders of all accounts?");
62 if (KMessageBox::warningContinueCancel(0, text, title,
63 KStandardGuiItem::cont(), KStandardGuiItem::cancel(),
64 QLatin1String("confirm_empty_trash"))
65 != KMessageBox::Continue) {
66 emitResult(OK);
67 return;
68 }
69 Akonadi::Collection trash = trashCollectionFolder();
70 QList<Akonadi::Collection> trashFolder;
71 trashFolder << trash;
72
73 const Akonadi::AgentInstance::List lst = agentInstances();
74 foreach (const Akonadi::AgentInstance &type, lst) {
75 if (type.identifier().contains(IMAP_RESOURCE_IDENTIFIER)) {
76 if (type.status() == Akonadi::AgentInstance::Broken) {
77 continue;
78 }
79 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface(type.identifier());
80 if (iface->isValid()) {
81 const int trashImap = iface->trashCollection();
82 if (trashImap != trash.id()) {
83 trashFolder << Akonadi::Collection(trashImap);
84 }
85 }
86 delete iface;
87 }
88 }
89 mNumberOfTrashToEmpty = trashFolder.count();
90 for (int i = 0; i < mNumberOfTrashToEmpty; ++i) {
91 expunge(trashFolder.at(i));
92 }
93 } else {
94 if (folderIsTrash(mFolder)) {
95 mNumberOfTrashToEmpty++;
96 expunge(mFolder);
97 } else {
98 emitResult(OK);
99 }
100
101 }
102}
103
104void EmptyTrashCommand::expunge(const Akonadi::Collection &col)
105{
106 if (col.isValid()) {
107 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(col, this);
108 connect(job, SIGNAL(result(KJob*)), this, SLOT(slotExpungeJob(KJob*)));
109 } else {
110 kDebug() << " Try to expunge an invalid collection :" << col;
111 emitResult(Failed);
112 }
113}
114
115void EmptyTrashCommand::slotExpungeJob(KJob *job)
116{
117 if (job->error()) {
118 Util::showJobError(job);
119 emitResult(Failed);
120 return;
121 }
122 Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob *>(job);
123 if (!fjob) {
124 emitResult(Failed);
125 return;
126 }
127 const Akonadi::Item::List lstItem = fjob->items();
128 if (lstItem.isEmpty()) {
129 emitResult(OK);
130 return;
131 }
132 Akonadi::ItemDeleteJob *jobDelete = new Akonadi::ItemDeleteJob(lstItem, this);
133 connect(jobDelete, SIGNAL(result(KJob*)), this, SLOT(slotDeleteJob(KJob*)));
134
135}
136
137void EmptyTrashCommand::slotDeleteJob(KJob *job)
138{
139 if (job->error()) {
140 Util::showJobError(job);
141 emitResult(Failed);
142 }
143 emitResult(OK);
144}
145
146Akonadi::AgentInstance::List EmptyTrashCommand::agentInstances()
147{
148 Akonadi::AgentInstance::List relevantInstances;
149 foreach (const Akonadi::AgentInstance &instance, Akonadi::AgentManager::self()->instances()) {
150 if (instance.type().mimeTypes().contains(KMime::Message::mimeType()) &&
151 instance.type().capabilities().contains(QLatin1String("Resource")) &&
152 !instance.type().capabilities().contains(QLatin1String("Virtual"))) {
153 relevantInstances << instance;
154 }
155 }
156 return relevantInstances;
157}
158
159Akonadi::Collection EmptyTrashCommand::collectionFromId(const Akonadi::Collection::Id &id) const
160{
161 const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
162 mModel, Akonadi::Collection(id)
163 );
164 return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
165}
166
167Akonadi::Collection EmptyTrashCommand::trashCollectionFolder()
168{
169 if (the_trashCollectionFolder < 0) {
170 the_trashCollectionFolder = Akonadi::SpecialMailCollections::self()->defaultCollection(Akonadi::SpecialMailCollections::Trash).id();
171 }
172 return collectionFromId(the_trashCollectionFolder);
173}
174
175bool EmptyTrashCommand::folderIsTrash(const Akonadi::Collection &col)
176{
177 if (col == Akonadi::SpecialMailCollections::self()->defaultCollection(Akonadi::SpecialMailCollections::Trash)) {
178 return true;
179 }
180 const Akonadi::AgentInstance::List lst = agentInstances();
181 foreach (const Akonadi::AgentInstance &type, lst) {
182 if (type.status() == Akonadi::AgentInstance::Broken) {
183 continue;
184 }
185 if (type.identifier().contains(IMAP_RESOURCE_IDENTIFIER)) {
186 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface(type.identifier());
187 if (iface->isValid()) {
188 if (iface->trashCollection() == col.id()) {
189 delete iface;
190 return true;
191 }
192 }
193 delete iface;
194 }
195 }
196 return false;
197}
198
199void EmptyTrashCommand::emitResult(Result value)
200{
201 emit result(value);
202 mNumberOfTrashToEmpty--;
203 if (mNumberOfTrashToEmpty <= 0) {
204 deleteLater();
205 }
206}
Akonadi::AgentInstance
A representation of an agent instance.
Definition agentinstance.h:63
Akonadi::AgentInstance::type
AgentType type() const
Returns the agent type of this instance.
Definition agentinstance.cpp:50
Akonadi::AgentInstance::List
QList< AgentInstance > List
Describes a list of agent instances.
Definition agentinstance.h:71
Akonadi::AgentInstance::Broken
@ Broken
The agent instance encountered an error state.
Definition agentinstance.h:79
Akonadi::AgentManager::self
static AgentManager * self()
Returns the global instance of the agent manager.
Definition agentmanager.cpp:377
Akonadi::AgentType::mimeTypes
QStringList mimeTypes() const
Returns the list of supported mime types of the agent type.
Definition agenttype.cpp:71
Akonadi::AgentType::capabilities
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
Definition agenttype.cpp:76
Akonadi::Collection
Represents a collection of PIM items.
Definition collection.h:76
Akonadi::EntityTreeModel::modelIndexForCollection
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
Definition entitytreemodel.cpp:1238
Akonadi::EntityTreeModel::CollectionRole
@ CollectionRole
The collection.
Definition entitytreemodel.h:336
Akonadi::Entity::isValid
bool isValid() const
Returns whether the entity is valid.
Definition entity.cpp:97
Akonadi::Entity::id
Id id() const
Returns the unique identifier of the entity.
Definition entity.cpp:72
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition entity.h:65
Akonadi::ItemDeleteJob
Job that deletes items from the Akonadi storage.
Definition itemdeletejob.h:63
Akonadi::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition itemfetchjob.h:83
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched items.
Definition itemfetchjob.cpp:233
Akonadi::SpecialMailCollections::Trash
@ Trash
The trash collection.
Definition specialmailcollections.h:84
Akonadi::SpecialMailCollections::self
static SpecialMailCollections * self()
Returns the global SpecialMailCollections instance.
Definition specialmailcollections.cpp:97
Akonadi::SpecialMailCollections::defaultCollection
Akonadi::Collection defaultCollection(Type type) const
Returns the special mail collection of given type in the default resource, or an invalid collection i...
Definition specialmailcollections.cpp:131
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

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • 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