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