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

akonadi

  • akonadi
  • kmime
movetotrashcommand.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 "movetotrashcommand_p.h"
21#include "util_p.h"
22#include "movecommand_p.h"
23#include "imapsettings.h"
24
25#include <akonadi/itemfetchjob.h>
26#include <akonadi/itemfetchscope.h>
27#include <akonadi/kmime/specialmailcollections.h>
28#include <akonadi/entitytreemodel.h>
29
30MoveToTrashCommand::MoveToTrashCommand(const QAbstractItemModel *model, const Akonadi::Collection::List &folders, QObject *parent)
31 : CommandBase(parent)
32{
33 the_trashCollectionFolder = -1;
34 mFolders = folders;
35 mModel = model;
36 mFolderListJobCount = mFolders.size();
37}
38
39MoveToTrashCommand::MoveToTrashCommand(const QAbstractItemModel *model, const QList< Akonadi::Item > &msgList, QObject *parent)
40 : CommandBase(parent)
41{
42 the_trashCollectionFolder = -1;
43 mMessages = msgList;
44 mModel = model;
45 mFolderListJobCount = 0;
46}
47
48void MoveToTrashCommand::slotFetchDone(KJob *job)
49{
50 mFolderListJobCount--;
51
52 if (job->error()) {
53 // handle errors
54 Util::showJobError(job);
55 emitResult(Failed);
56 return;
57 }
58
59 Akonadi::ItemFetchJob *fjob = static_cast<Akonadi::ItemFetchJob *>(job);
60
61 mMessages = fjob->items();
62 moveMessages();
63
64 if (mFolderListJobCount > 0) {
65 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(mFolders[mFolderListJobCount - 1], parent());
66 job->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
67 connect(job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)));
68 }
69}
70
71void MoveToTrashCommand::execute()
72{
73 if (!mFolders.isEmpty()) {
74 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(mFolders[mFolderListJobCount - 1], parent());
75 job->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
76 connect(job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)));
77 } else if (!mMessages.isEmpty()) {
78 mFolders << mMessages.first().parentCollection();
79 moveMessages();
80 } else {
81 emitResult(OK);
82 }
83}
84
85void MoveToTrashCommand::moveMessages()
86{
87 Akonadi::Collection folder = mFolders[mFolderListJobCount];
88 if (folder.isValid()) {
89 MoveCommand *moveCommand = new MoveCommand(findTrashFolder(folder), mMessages, this);
90 connect(moveCommand, SIGNAL(result(Result)), this, SLOT(slotMoveDone(Result)));
91 moveCommand->execute();
92 } else {
93 emitResult(Failed);
94 }
95}
96
97void MoveToTrashCommand::slotMoveDone(const Result &result)
98{
99 if (result == Failed) {
100 emitResult(Failed);
101 }
102 if (mFolderListJobCount == 0 && result == OK) {
103 emitResult(OK);
104 }
105}
106
107Akonadi::Collection MoveToTrashCommand::collectionFromId(const Akonadi::Collection::Id &id) const
108{
109 const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
110 mModel, Akonadi::Collection(id)
111 );
112 return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
113}
114
115Akonadi::Collection MoveToTrashCommand::trashCollectionFromResource(const Akonadi::Collection &col)
116{
117 //NOTE(Andras): from kmail/kmkernel.cpp
118 Akonadi::Collection trashCol;
119 if (col.isValid()) {
120 if (col.resource().contains(IMAP_RESOURCE_IDENTIFIER)) {
121 //TODO: we really need some standard interface to query for special collections,
122 //instead of relying on a resource's settings interface
123 OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface(col.resource());
124 if (iface->isValid()) {
125
126 trashCol = Akonadi::Collection(iface->trashCollection());
127 delete iface;
128 return trashCol;
129 }
130 delete iface;
131 }
132 }
133 return trashCol;
134}
135
136Akonadi::Collection MoveToTrashCommand::trashCollectionFolder()
137{
138 if (the_trashCollectionFolder < 0) {
139 the_trashCollectionFolder = Akonadi::SpecialMailCollections::self()->defaultCollection(Akonadi::SpecialMailCollections::Trash).id();
140 }
141 return collectionFromId(the_trashCollectionFolder);
142}
143
144Akonadi::Collection MoveToTrashCommand::findTrashFolder(const Akonadi::Collection &folder)
145{
146 Akonadi::Collection col = trashCollectionFromResource(folder);
147 if (!col.isValid()) {
148 col = trashCollectionFolder();
149 }
150 if (folder != col) {
151 return col;
152 }
153 return Akonadi::Collection();
154}
155
156#include "moc_movetotrashcommand_p.cpp"
Akonadi::Collection
Represents a collection of PIM items.
Definition collection.h:76
Akonadi::Collection::resource
QString resource() const
Returns the identifier of the resource owning the collection.
Definition collection.cpp:207
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition collection.h:81
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::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition itemfetchjob.h:83
Akonadi::ItemFetchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition itemfetchjob.cpp:261
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched items.
Definition itemfetchjob.cpp:233
Akonadi::ItemFetchScope::setAncestorRetrieval
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval.
Definition itemfetchscope.cpp:132
Akonadi::ItemFetchScope::Parent
@ Parent
Only retrieve the immediate parent collection.
Definition itemfetchscope.h:78
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