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

akonadi

  • akonadi
  • kmime
emptytrashcommand.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 "emptytrashcommand_p.h"
22 #include "util_p.h"
23 #include "imapsettings.h"
24 
25 #include <KDebug>
26 #include <KLocalizedString>
27 #include <KMessageBox>
28 
29 #include "akonadi/entitytreemodel.h"
30 #include "akonadi/kmime/specialmailcollections.h"
31 #include "akonadi/itemfetchjob.h"
32 #include "akonadi/itemdeletejob.h"
33 #include "akonadi/agentmanager.h"
34 #include "kmime/kmime_message.h"
35 
36 EmptyTrashCommand::EmptyTrashCommand(const QAbstractItemModel* model, QObject* parent)
37  : CommandBase( parent ),
38  mModel( model ),
39  the_trashCollectionFolder( -1 ),
40  mNumberOfTrashToEmpty( 0 )
41 {
42 }
43 
44 EmptyTrashCommand::EmptyTrashCommand(const Akonadi::Collection& folder, QObject* parent)
45  : CommandBase( parent ),
46  mModel( 0 ),
47  the_trashCollectionFolder( -1 ),
48  mFolder( folder ),
49  mNumberOfTrashToEmpty( 0 )
50 {
51 }
52 
53 
54 void EmptyTrashCommand::execute()
55 {
56  if ( !mFolder.isValid() && !mModel ) {
57  emitResult( Failed );
58  return;
59  }
60 
61  if ( !mFolder.isValid() ) { //expunge all
62  const QString title = i18n("Empty Trash");
63  const QString text = i18n("Are you sure you want to empty the trash folders of all accounts?");
64  if (KMessageBox::warningContinueCancel(0, text, title,
65  KStandardGuiItem::cont(), KStandardGuiItem::cancel(),
66  QLatin1String( "confirm_empty_trash" ) )
67  != KMessageBox::Continue)
68  {
69  emitResult( OK );
70  return;
71  }
72  Akonadi::Collection trash = trashCollectionFolder();
73  QList<Akonadi::Collection> trashFolder;
74  trashFolder<<trash;
75 
76  const Akonadi::AgentInstance::List lst = agentInstances();
77  foreach ( const Akonadi::AgentInstance& type, lst ) {
78  if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
79  if ( type.status() == Akonadi::AgentInstance::Broken )
80  continue;
81  OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() );
82  if ( iface->isValid() ) {
83  const int trashImap = iface->trashCollection();
84  if ( trashImap != trash.id() ) {
85  trashFolder<<Akonadi::Collection( trashImap );
86  }
87  }
88  delete iface;
89  }
90  }
91  mNumberOfTrashToEmpty = trashFolder.count();
92  for (int i = 0; i < mNumberOfTrashToEmpty; ++i) {
93  expunge( trashFolder.at( i ) );
94  }
95  } else {
96  if ( folderIsTrash( mFolder ) ) {
97  mNumberOfTrashToEmpty++;
98  expunge( mFolder );
99  } else {
100  emitResult( OK );
101  }
102 
103  }
104 }
105 
106 void EmptyTrashCommand::expunge( const Akonadi::Collection & col )
107 {
108  if ( col.isValid() ) {
109  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( col,this );
110  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotExpungeJob(KJob*)) );
111  } else {
112  kDebug()<<" Try to expunge an invalid collection :"<<col;
113  emitResult( Failed );
114  }
115 }
116 
117 void EmptyTrashCommand::slotExpungeJob( KJob *job )
118 {
119  if ( job->error() ) {
120  Util::showJobError( job );
121  emitResult( Failed );
122  return;
123  }
124  Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
125  if ( !fjob ) {
126  emitResult( Failed );
127  return;
128  }
129  const Akonadi::Item::List lstItem = fjob->items();
130  if ( lstItem.isEmpty() ) {
131  emitResult( OK );
132  return;
133  }
134  Akonadi::ItemDeleteJob *jobDelete = new Akonadi::ItemDeleteJob( lstItem, this );
135  connect( jobDelete, SIGNAL(result(KJob*)), this, SLOT(slotDeleteJob(KJob*)) );
136 
137 }
138 
139 void EmptyTrashCommand::slotDeleteJob( KJob *job )
140 {
141  if ( job->error() ) {
142  Util::showJobError( job );
143  emitResult( Failed );
144  }
145  emitResult( OK );
146 }
147 
148 Akonadi::AgentInstance::List EmptyTrashCommand::agentInstances()
149 {
150  Akonadi::AgentInstance::List relevantInstances;
151  foreach ( const Akonadi::AgentInstance &instance, Akonadi::AgentManager::self()->instances() ) {
152  if ( instance.type().mimeTypes().contains( KMime::Message::mimeType() ) &&
153  instance.type().capabilities().contains( QLatin1String( "Resource" ) ) &&
154  !instance.type().capabilities().contains( QLatin1String( "Virtual" ) ) ) {
155  relevantInstances << instance;
156  }
157  }
158  return relevantInstances;
159 }
160 
161 Akonadi::Collection EmptyTrashCommand::collectionFromId(const Akonadi::Collection::Id& id) const
162 {
163  const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(
164  mModel, Akonadi::Collection(id)
165  );
166  return idx.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
167 }
168 
169 Akonadi::Collection EmptyTrashCommand::trashCollectionFolder()
170 {
171  if ( the_trashCollectionFolder < 0 )
172  the_trashCollectionFolder = Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ).id();
173  return collectionFromId( the_trashCollectionFolder );
174 }
175 
176 bool EmptyTrashCommand::folderIsTrash( const Akonadi::Collection & col )
177 {
178  if ( col == Akonadi::SpecialMailCollections::self()->defaultCollection( Akonadi::SpecialMailCollections::Trash ) )
179  return true;
180  const Akonadi::AgentInstance::List lst = agentInstances();
181  foreach ( const Akonadi::AgentInstance& type, lst ) {
182  if ( type.status() == Akonadi::AgentInstance::Broken )
183  continue;
184  if ( type.identifier().contains( IMAP_RESOURCE_IDENTIFIER ) ) {
185  OrgKdeAkonadiImapSettingsInterface *iface = Util::createImapSettingsInterface( type.identifier() );
186  if ( iface->isValid() ) {
187  if ( iface->trashCollection() == col.id() ) {
188  delete iface;
189  return true;
190  }
191  }
192  delete iface;
193  }
194  }
195  return false;
196 }
197 
198 void EmptyTrashCommand::emitResult( Result value )
199 {
200  emit result( value );
201  mNumberOfTrashToEmpty--;
202  if ( mNumberOfTrashToEmpty <= 0 ) {
203  deleteLater();
204  }
205 }
Akonadi::AgentInstance::List
QList< AgentInstance > List
Describes a list of agent instances.
Definition: agentinstance.h:71
Akonadi::SpecialMailCollections::Trash
The trash collection.
Definition: specialmailcollections.h:85
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::AgentInstance::type
AgentType type() const
Returns the agent type of this instance.
Definition: agentinstance.cpp:51
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:64
Akonadi::AgentInstance::identifier
QString identifier() const
Returns the unique identifier of the agent instance.
Definition: agentinstance.cpp:56
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched item.
Definition: itemfetchjob.cpp:227
Akonadi::ItemDeleteJob
Job that deletes items from the Akonadi storage.
Definition: itemdeletejob.h:62
Akonadi::AgentType::mimeTypes
QStringList mimeTypes() const
Returns the list of supported mime types of the agent type.
Definition: agenttype.cpp:71
Akonadi::AgentType::capabilities
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
Definition: agenttype.cpp:76
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:1191
Akonadi::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:82
Akonadi::AgentManager::self
static AgentManager * self()
Returns the global instance of the agent manager.
Definition: agentmanager.cpp:379
Akonadi::AgentInstance::status
Status status() const
Returns the status of the agent instance.
Definition: agentinstance.cpp:71
Akonadi::AgentInstance
A representation of an agent instance.
Definition: agentinstance.h:62
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
Akonadi::Entity::isValid
bool isValid() const
Returns whether the entity is valid.
Definition: entity.cpp:97
Akonadi::AgentInstance::Broken
The agent instance encountered an error state.
Definition: agentinstance.h:79
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:17 by doxygen 1.8.5 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.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