Akonadi Mime

movetotrashcommand.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 "movetotrashcommand.h"
9#include "imapsettings.h"
10#include "movecommand.h"
11#include "specialmailcollections.h"
12#include "util_p.h"
13
14#include <Akonadi/EntityTreeModel>
15#include <Akonadi/ItemFetchJob>
16#include <Akonadi/ItemFetchScope>
17using namespace Akonadi;
18MoveToTrashCommand::MoveToTrashCommand(const QAbstractItemModel *model, const Akonadi::Collection::List &folders, QObject *parent)
19 : CommandBase(parent)
20 , mFolders(folders)
21 , the_trashCollectionFolder(-1)
22 , mModel(model)
23 , mFolderListJobCount(mFolders.size())
24{
25}
26
27MoveToTrashCommand::MoveToTrashCommand(const QAbstractItemModel *model, const Akonadi::Item::List &msgList, QObject *parent)
28 : CommandBase(parent)
29 , mMessages(msgList)
30 , the_trashCollectionFolder(-1)
31 , mModel(model)
32 , mFolderListJobCount(0)
33{
34}
35
36void MoveToTrashCommand::slotFetchDone(KJob *job)
37{
38 mFolderListJobCount--;
39
40 if (job->error()) {
41 // handle errors
42 Util::showJobError(job);
43 emitResult(Failed);
44 return;
45 }
46
47 auto fjob = static_cast<Akonadi::ItemFetchJob *>(job);
48
49 mMessages = fjob->items();
50 moveMessages();
51
52 if (mFolderListJobCount > 0) {
53 auto fetchJob = new Akonadi::ItemFetchJob(mFolders[mFolderListJobCount - 1], parent());
54 fetchJob->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
55 connect(fetchJob, &Akonadi::ItemFetchJob::result, this, &MoveToTrashCommand::slotFetchDone);
56 }
57}
58
59void MoveToTrashCommand::execute()
60{
61 if (!mFolders.isEmpty()) {
62 auto job = new Akonadi::ItemFetchJob(mFolders[mFolderListJobCount - 1], parent());
63 job->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
64 connect(job, &Akonadi::ItemFetchJob::result, this, &MoveToTrashCommand::slotFetchDone);
65 } else if (!mMessages.isEmpty()) {
66 mFolders << mMessages.first().parentCollection();
67 moveMessages();
68 } else {
69 emitResult(OK);
70 }
71}
72
73void MoveToTrashCommand::moveMessages()
74{
75 const Akonadi::Collection folder = mFolders.at(mFolderListJobCount);
76 if (folder.isValid()) {
77 auto moveCommand = new MoveCommand(findTrashFolder(folder), mMessages, this);
78 connect(moveCommand, &MoveCommand::result, this, &MoveToTrashCommand::slotMoveDone);
79 moveCommand->execute();
80 } else {
81 emitResult(Failed);
82 }
83}
84
85void MoveToTrashCommand::slotMoveDone(Result result)
86{
87 if (result == Failed) {
88 emitResult(Failed);
89 }
90 if (mFolderListJobCount == 0 && result == OK) {
91 emitResult(OK);
92 }
93}
94
95Akonadi::Collection MoveToTrashCommand::collectionFromId(Akonadi::Collection::Id id) const
96{
99}
100
101Akonadi::Collection MoveToTrashCommand::trashCollectionFromResource(const Akonadi::Collection &col)
102{
103 // NOTE(Andras): from kmail/kmkernel.cpp
104 Akonadi::Collection trashCol;
105 if (col.isValid()) {
106 if (col.resource().contains(IMAP_RESOURCE_IDENTIFIER)) {
107 // TODO: we really need some standard interface to query for special collections,
108 // instead of relying on a resource's settings interface
109 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface(col.resource());
110 if (iface->isValid()) {
111 trashCol = Akonadi::Collection(iface->trashCollection());
112 delete iface;
113 return trashCol;
114 }
115 delete iface;
116 }
117 }
118 return trashCol;
119}
120
121Akonadi::Collection MoveToTrashCommand::trashCollectionFolder()
122{
123 if (the_trashCollectionFolder < 0) {
125 }
126 return collectionFromId(the_trashCollectionFolder);
127}
128
129Akonadi::Collection MoveToTrashCommand::findTrashFolder(const Akonadi::Collection &folder)
130{
131 Akonadi::Collection col = trashCollectionFromResource(folder);
132 if (!col.isValid()) {
133 col = trashCollectionFolder();
134 }
135 if (folder != col) {
136 return col;
137 }
138 return {};
139}
140
141#include "moc_movetotrashcommand.cpp"
QString resource() 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...
int error() const
void result(KJob *job)
const_reference at(qsizetype i) const const
T & first()
bool isEmpty() const const
QVariant data(int role) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:31 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.