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

akonadi/kmime

  • sources
  • kde-4.14
  • kdepimlibs
  • 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 
30 MoveToTrashCommand::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 
39 MoveToTrashCommand::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 
48 void 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 
71 void 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 
85 void 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 
97 void 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 
107 Akonadi::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 
115 Akonadi::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 
136 Akonadi::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 
144 Akonadi::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"
QModelIndex
Akonadi::SpecialMailCollections::Trash
The trash collection.
Definition: specialmailcollections.h:84
QVariant::value
T value() const
QObject
QList< Akonadi::Item >
Akonadi::SpecialMailCollections::self
static SpecialMailCollections * self()
Returns the global SpecialMailCollections instance.
Definition: specialmailcollections.cpp:97
QModelIndex::data
QVariant data(int role) const
QAbstractItemModel
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-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:24 by doxygen 1.8.7 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 Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

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

Search



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