• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 
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 = dynamic_cast<Akonadi::ItemFetchJob*>( job );
60  Q_ASSERT( fjob );
61 
62  mMessages = fjob->items();
63  moveMessages();
64 
65  if ( mFolderListJobCount > 0 ) {
66  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
67  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
68  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
69  }
70 }
71 
72 
73 void MoveToTrashCommand::execute()
74 {
75  if ( !mFolders.isEmpty() ) {
76  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolders[mFolderListJobCount - 1], parent() );
77  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
78  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
79  } else if ( !mMessages.isEmpty() ) {
80  mFolders << mMessages.first().parentCollection();
81  moveMessages();
82  } else {
83  emitResult( OK );
84  }
85 }
86 
87 void MoveToTrashCommand::moveMessages()
88 {
89  Akonadi::Collection folder = mFolders[mFolderListJobCount];
90  if ( folder.isValid() ) {
91  MoveCommand *moveCommand = new MoveCommand( findTrashFolder( folder ), mMessages, this );
92  connect( moveCommand, SIGNAL(result(Result)), this, SLOT(slotMoveDone(Result)) );
93  moveCommand->execute();
94  } else {
95  emitResult( Failed );
96  }
97 }
98 
99 void MoveToTrashCommand::slotMoveDone( const Result& result )
100 {
101  if (result == Failed )
102  emitResult( Failed );
103  if ( mFolderListJobCount == 0 && result == OK) {
104  emitResult( OK );
105  }
106 }
107 
108 Akonadi::Collection MoveToTrashCommand::collectionFromId(const Akonadi::Collection::Id& id) const
109 {
110  const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
111  mModel, Akonadi::Collection(id)
112  );
113  return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
114 }
115 
116 Akonadi::Collection MoveToTrashCommand::trashCollectionFromResource( const Akonadi::Collection & col )
117 {
118  //NOTE(Andras): from kmail/kmkernel.cpp
119  Akonadi::Collection trashCol;
120  if ( col.isValid() ) {
121  if ( col.resource().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
122  //TODO: we really need some standard interface to query for special collections,
123  //instead of relying on a resource's settings interface
124  OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( col.resource() );
125  if ( iface->isValid() ) {
126 
127  trashCol = Akonadi::Collection( iface->trashCollection() );
128  delete iface;
129  return trashCol;
130  }
131  delete iface;
132  }
133  }
134  return trashCol;
135 }
136 
137 Akonadi::Collection MoveToTrashCommand::trashCollectionFolder()
138 {
139  if ( the_trashCollectionFolder < 0 )
140  the_trashCollectionFolder = Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ).id();
141  return collectionFromId( the_trashCollectionFolder );
142 }
143 
144 
145 Akonadi::Collection MoveToTrashCommand::findTrashFolder( const Akonadi::Collection& folder )
146 {
147  Akonadi::Collection col = trashCollectionFromResource( folder );
148  if ( !col.isValid() ) {
149  col = trashCollectionFolder();
150  }
151  if ( folder != col )
152  return col;
153  return Akonadi::Collection();
154 }
155 
156 
157 
158 #include "moc_movetotrashcommand_p.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:39 by doxygen 1.8.3.1 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-4.10.5 API Reference

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