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

akonadi

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

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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