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

akonadi/kmime

  • 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 
21 #include "movetotrashcommand_p.h"
22 #include "util_p.h"
23 #include "movecommand_p.h"
24 #include "imapsettings.h"
25 
26 #include <akonadi/itemfetchjob.h>
27 #include <akonadi/itemfetchscope.h>
28 #include <akonadi/kmime/specialmailcollections.h>
29 #include <akonadi/entitytreemodel.h>
30 
31 MoveToTrashCommand::MoveToTrashCommand(const QAbstractItemModel* model, const Akonadi::Collection::List& folders, QObject* parent): 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): CommandBase( parent )
40 {
41  the_trashCollectionFolder = -1;
42  mMessages = msgList;
43  mModel = model;
44  mFolderListJobCount = 0;
45 }
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 
72 void MoveToTrashCommand::execute()
73 {
74  if ( !mFolders.isEmpty() ) {
75  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
76  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
77  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
78  } else if ( !mMessages.isEmpty() ) {
79  mFolders << mMessages.first().parentCollection();
80  moveMessages();
81  } else {
82  emitResult( OK );
83  }
84 }
85 
86 void MoveToTrashCommand::moveMessages()
87 {
88  Akonadi::Collection folder = mFolders[mFolderListJobCount];
89  if ( folder.isValid() ) {
90  MoveCommand *moveCommand = new MoveCommand( findTrashFolder( folder ), mMessages, this );
91  connect( moveCommand, SIGNAL(result(Result)), this, SLOT(slotMoveDone(Result)) );
92  moveCommand->execute();
93  } else {
94  emitResult( Failed );
95  }
96 }
97 
98 void MoveToTrashCommand::slotMoveDone( const Result& result )
99 {
100  if (result == Failed )
101  emitResult( Failed );
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  return collectionFromId( the_trashCollectionFolder );
141 }
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  return Akonadi::Collection();
153 }
154 
155 
156 
157 #include "moc_movetotrashcommand_p.cpp"
Akonadi::SpecialMailCollections::Trash
The trash collection.
Definition: specialmailcollections.h:85
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:122
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:54 by doxygen 1.8.5 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 Members
  • File List
  • Related Pages

kdepimlibs-4.11.3 API Reference

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