Akonadi Mime

emptytrashcommand.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>
3 SPDX-FileCopyrightText: 2010 Andras Mantia <andras@kdab.com>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "emptytrashcommand.h"
9#include "imapsettings.h"
10#include "specialmailcollections.h"
11#include "util_p.h"
12
13#include "akonadi_mime_debug.h"
14#include <KLocalizedString>
15#include <KMessageBox>
16
17#include <Akonadi/AgentManager>
18#include <Akonadi/EntityTreeModel>
19#include <Akonadi/ItemDeleteJob>
20#include <Akonadi/ItemFetchJob>
21#include <KMime/KMimeMessage>
22using namespace Akonadi;
23EmptyTrashCommand::EmptyTrashCommand(const QAbstractItemModel *model, QObject *parent)
24 : CommandBase(parent)
25 , mModel(model)
26 , the_trashCollectionFolder(-1)
27{
28}
29
30EmptyTrashCommand::EmptyTrashCommand(const Akonadi::Collection &folder, QObject *parent)
31 : CommandBase(parent)
32 , mModel(nullptr)
33 , the_trashCollectionFolder(-1)
34 , mFolder(folder)
35{
36}
37
38void EmptyTrashCommand::execute()
39{
40 if (!mFolder.isValid() && !mModel) {
41 emitResult(Failed);
42 return;
43 }
44
45 if (!mFolder.isValid()) { // expunge all
46 const QString title = i18n("Empty Trash");
47 const QString text = i18n("Are you sure you want to empty the trash folders of all accounts?");
49 text,
50 title,
53 QStringLiteral("confirm_empty_trash"))
55 emitResult(OK);
56 return;
57 }
58 Akonadi::Collection trash = trashCollectionFolder();
60 trashFolder << trash;
61
62 const Akonadi::AgentInstance::List lst = agentInstances();
63 for (const Akonadi::AgentInstance &type : lst) {
64 if (type.identifier().contains(IMAP_RESOURCE_IDENTIFIER)) {
65 if (type.status() == Akonadi::AgentInstance::Broken) {
66 continue;
67 }
68 QScopedPointer<OrgKdeAkonadiImapSettingsInterface> iface{Util::createImapSettingsInterface(type.identifier())};
69 if (iface->isValid()) {
70 const int trashImap = iface->trashCollection();
71 if (trashImap != trash.id()) {
72 trashFolder << Akonadi::Collection(trashImap);
73 }
74 }
75 }
76 }
77 mNumberOfTrashToEmpty = trashFolder.count();
78 for (int i = 0; i < mNumberOfTrashToEmpty; ++i) {
79 expunge(trashFolder.at(i));
80 }
81 } else {
82 if (folderIsTrash(mFolder)) {
83 mNumberOfTrashToEmpty++;
84 expunge(mFolder);
85 } else {
86 emitResult(OK);
87 }
88 }
89}
90
91void EmptyTrashCommand::expunge(const Akonadi::Collection &col)
92{
93 if (col.isValid()) {
94 auto jobDelete = new Akonadi::ItemDeleteJob(col, this);
95 connect(jobDelete, &Akonadi::ItemDeleteJob::result, this, [this, jobDelete]() {
96 if (jobDelete->error()) {
97 Util::showJobError(jobDelete);
98 emitResult(Failed);
99 }
100 emitResult(OK);
101 });
102 } else {
103 qCWarning(AKONADIMIME_LOG) << " Try to expunge an invalid collection :" << col;
104 emitResult(Failed);
105 }
106}
107
108Akonadi::AgentInstance::List EmptyTrashCommand::agentInstances()
109{
110 Akonadi::AgentInstance::List relevantInstances;
111 const auto instances = Akonadi::AgentManager::self()->instances();
112 for (const Akonadi::AgentInstance &instance : instances) {
113 if (instance.type().mimeTypes().contains(KMime::Message::mimeType()) && instance.type().capabilities().contains(QLatin1StringView("Resource"))
114 && !instance.type().capabilities().contains(QLatin1StringView("Virtual"))) {
115 relevantInstances << instance;
116 }
117 }
118 return relevantInstances;
119}
120
121Akonadi::Collection EmptyTrashCommand::collectionFromId(Collection::Id id) const
122{
125}
126
127Akonadi::Collection EmptyTrashCommand::trashCollectionFolder()
128{
129 if (the_trashCollectionFolder < 0) {
131 }
132 return collectionFromId(the_trashCollectionFolder);
133}
134
135bool EmptyTrashCommand::folderIsTrash(const Akonadi::Collection &col)
136{
138 return true;
139 }
140 const Akonadi::AgentInstance::List lst = agentInstances();
141 for (const Akonadi::AgentInstance &type : lst) {
142 if (type.status() == Akonadi::AgentInstance::Broken) {
143 continue;
144 }
145 if (type.identifier().contains(IMAP_RESOURCE_IDENTIFIER)) {
146 QScopedPointer<OrgKdeAkonadiImapSettingsInterface> iface{Util::createImapSettingsInterface(type.identifier())};
147 if (iface->isValid()) {
148 if (iface->trashCollection() == col.id()) {
149 return true;
150 }
151 }
152 }
153 }
154 return false;
155}
156
157void EmptyTrashCommand::emitResult(Result value)
158{
159 Q_EMIT result(value);
160 mNumberOfTrashToEmpty--;
161 if (mNumberOfTrashToEmpty <= 0) {
162 deleteLater();
163 }
164}
165
166#include "moc_emptytrashcommand.cpp"
static AgentManager * self()
AgentInstance::List instances() const
bool isValid() const
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
static SpecialMailCollections * self()
Returns the global SpecialMailCollections instance.
Akonadi::Collection defaultCollection(Type type) const
Returns the special mail collection of given type in the default resource, or an invalid collection i...
void result(KJob *job)
static QString mimeType()
QString i18n(const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
KIOCORE_EXPORT CopyJob * trash(const QList< QUrl > &src, JobFlags flags=DefaultFlags)
ButtonCode warningContinueCancel(QWidget *parent, const QString &text, const QString &title=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
KGuiItem cont()
KGuiItem cancel()
const_reference at(qsizetype i) const const
qsizetype count() const const
QVariant data(int role) const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:21:09 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.