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

akonadi

  • akonadi
  • kmime
standardmailactionmanager.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 - 2010 Tobias Koenig <tokoe@kde.org>
5  Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
6  Copyright (c) 2010 Andras Mantia <andras@kdab.com>
7 
8  This library is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Library General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or (at your
11  option) any later version.
12 
13  This library is distributed in the hope that it will be useful, but WITHOUT
14  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
16  License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to the
20  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 */
23 
24 #include "standardmailactionmanager.h"
25 
26 #include "emptytrashcommand_p.h"
27 #include "markascommand_p.h"
28 #include "movetotrashcommand_p.h"
29 #include "specialmailcollections.h"
30 #include "removeduplicatesjob.h"
31 
32 #include "akonadi/agentfilterproxymodel.h"
33 #include "akonadi/agentinstance.h"
34 #include "akonadi/agentinstancecreatejob.h"
35 #include "akonadi/agentmanager.h"
36 #include "akonadi/agenttypedialog.h"
37 #include "akonadi/collectionstatistics.h"
38 #include "akonadi/entitytreemodel.h"
39 #include "akonadi/kmime/messagestatus.h"
40 #include "util_p.h"
41 #include "akonadi/mimetypechecker.h"
42 #include "akonadi/subscriptiondialog_p.h"
43 
44 #include <kaction.h>
45 #include <kactioncollection.h>
46 #include <klocale.h>
47 #include <kmessagebox.h>
48 #include <kmime/kmime_message.h>
49 
50 #include <QtCore/QPointer>
51 #include <QItemSelectionModel>
52 
53 using namespace Akonadi;
54 
55 class StandardMailActionManager::Private
56 {
57  public:
58  Private( KActionCollection *actionCollection, QWidget *parentWidget, StandardMailActionManager *parent )
59  : mActionCollection( actionCollection ),
60  mParentWidget( parentWidget ),
61  mCollectionSelectionModel( 0 ),
62  mItemSelectionModel( 0 ),
63  mParent( parent )
64  {
65  mGenericManager = new StandardActionManager( actionCollection, parentWidget );
66 
67  mParent->connect( mGenericManager, SIGNAL(actionStateUpdated()),
68  mParent, SIGNAL(actionStateUpdated()) );
69 
70 
71  mGenericManager->setMimeTypeFilter( QStringList() << KMime::Message::mimeType() );
72  mGenericManager->setCapabilityFilter( QStringList() << QLatin1String( "Resource" ) );
73  }
74 
75  ~Private()
76  {
77  delete mGenericManager;
78  }
79 
80  void updateGenericAllActions()
81  {
82  updateGenericAction(StandardActionManager::CreateCollection);
83  updateGenericAction(StandardActionManager::CopyCollections);
84  updateGenericAction(StandardActionManager::DeleteCollections);
85  updateGenericAction(StandardActionManager::SynchronizeCollections);
86  updateGenericAction(StandardActionManager::CollectionProperties);
87  updateGenericAction(StandardActionManager::CopyItems);
88  updateGenericAction(StandardActionManager::Paste);
89  updateGenericAction(StandardActionManager::DeleteItems);
90  updateGenericAction(StandardActionManager::ManageLocalSubscriptions);
91  updateGenericAction(StandardActionManager::AddToFavoriteCollections);
92  updateGenericAction(StandardActionManager::RemoveFromFavoriteCollections);
93  updateGenericAction(StandardActionManager::RenameFavoriteCollection);
94  updateGenericAction(StandardActionManager::CopyCollectionToMenu);
95  updateGenericAction(StandardActionManager::CopyItemToMenu);
96  updateGenericAction(StandardActionManager::MoveItemToMenu);
97  updateGenericAction(StandardActionManager::MoveCollectionToMenu);
98  updateGenericAction(StandardActionManager::CutItems);
99  updateGenericAction(StandardActionManager::CutCollections);
100  updateGenericAction(StandardActionManager::CreateResource);
101  updateGenericAction(StandardActionManager::DeleteResources);
102  updateGenericAction(StandardActionManager::ResourceProperties);
103  updateGenericAction(StandardActionManager::SynchronizeResources);
104  updateGenericAction(StandardActionManager::ToggleWorkOffline);
105  updateGenericAction(StandardActionManager::CopyCollectionToDialog);
106  updateGenericAction(StandardActionManager::MoveCollectionToDialog);
107  updateGenericAction(StandardActionManager::CopyItemToDialog);
108  updateGenericAction(StandardActionManager::MoveItemToDialog);
109  updateGenericAction(StandardActionManager::SynchronizeCollectionsRecursive);
110  updateGenericAction(StandardActionManager::MoveCollectionsToTrash);
111  updateGenericAction(StandardActionManager::MoveItemsToTrash);
112  updateGenericAction(StandardActionManager::RestoreCollectionsFromTrash);
113  updateGenericAction(StandardActionManager::RestoreItemsFromTrash);
114  updateGenericAction(StandardActionManager::MoveToTrashRestoreCollection);
115  updateGenericAction(StandardActionManager::MoveToTrashRestoreCollectionAlternative);
116  updateGenericAction(StandardActionManager::MoveToTrashRestoreItem);
117  updateGenericAction(StandardActionManager::MoveToTrashRestoreItemAlternative);
118  updateGenericAction(StandardActionManager::SynchronizeFavoriteCollections);
119  }
120  void updateGenericAction(StandardActionManager::Type type)
121  {
122  switch(type) {
123  case Akonadi::StandardActionManager::CreateCollection:
124  mGenericManager->action( Akonadi::StandardActionManager::CreateCollection )->setText(
125  i18n( "Add Folder..." ) );
126  mGenericManager->action( Akonadi::StandardActionManager::CreateCollection )->setWhatsThis(
127  i18n( "Add a new folder to the currently selected account." ) );
128  mGenericManager->setContextText(
129  StandardActionManager::CreateCollection, StandardActionManager::DialogTitle,
130  i18nc( "@title:window", "New Folder" ) );
131 
132  mGenericManager->setContextText(
133  StandardActionManager::CreateCollection, StandardActionManager::ErrorMessageText,
134  ki18n( "Could not create folder: %1" ) );
135 
136  mGenericManager->setContextText(
137  StandardActionManager::CreateCollection, StandardActionManager::ErrorMessageTitle,
138  i18n( "Folder creation failed" ) );
139 
140  break;
141  case Akonadi::StandardActionManager::CopyCollections:
142  mGenericManager->setActionText( Akonadi::StandardActionManager::CopyCollections,
143  ki18np( "Copy Folder", "Copy %1 Folders" ) );
144  mGenericManager->action( Akonadi::StandardActionManager::CopyCollections )->setWhatsThis(
145  i18n( "Copy the selected folders to the clipboard." ) );
146  mGenericManager->setContextText(
147  StandardActionManager::CollectionProperties, StandardActionManager::DialogTitle,
148  ki18nc( "@title:window", "Properties of Folder %1" ) );
149 
150  break;
151  case Akonadi::StandardActionManager::DeleteCollections:
152  mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteCollections,
153  ki18np( "Delete Folder", "Delete %1 Folders" ) );
154  mGenericManager->action( Akonadi::StandardActionManager::DeleteCollections )->setWhatsThis(
155  i18n( "Delete the selected folders from the account." ) );
156  mGenericManager->setContextText(
157  StandardActionManager::DeleteCollections, StandardActionManager::MessageBoxText,
158  ki18np( "Do you really want to delete this folder and all its sub-folders?",
159  "Do you really want to delete %1 folders and all their sub-folders?" ) );
160 
161  mGenericManager->setContextText(
162  StandardActionManager::DeleteCollections, StandardActionManager::MessageBoxTitle,
163  ki18ncp( "@title:window", "Delete folder?", "Delete folders?" ) );
164 
165  mGenericManager->setContextText(
166  StandardActionManager::DeleteCollections, StandardActionManager::ErrorMessageText,
167  ki18n( "Could not delete folder: %1" ) );
168 
169  mGenericManager->setContextText(
170  StandardActionManager::DeleteCollections, StandardActionManager::ErrorMessageTitle,
171  i18n( "Folder deletion failed" ) );
172 
173  break;
174  case Akonadi::StandardActionManager::SynchronizeCollections:
175  mGenericManager->setActionText( Akonadi::StandardActionManager::SynchronizeCollections,
176  ki18np( "Update Folder", "Update Folders" ) );
177 
178  mGenericManager->action( Akonadi::StandardActionManager::SynchronizeCollections )->setWhatsThis(
179  i18n( "Update the content of the selected folders." ) );
180  break;
181  case Akonadi::StandardActionManager::CutCollections:
182  mGenericManager->setActionText( Akonadi::StandardActionManager::CutCollections,
183  ki18np( "Cut Folder", "Cut %1 Folders" ) );
184  mGenericManager->action( Akonadi::StandardActionManager::CutCollections )->setWhatsThis(
185  i18n( "Cut the selected folders from the account." ) );
186  break;
187  case Akonadi::StandardActionManager::CollectionProperties:
188  mGenericManager->action( Akonadi::StandardActionManager::CollectionProperties )->setText(
189  i18n( "Folder Properties..." ) );
190  mGenericManager->action( Akonadi::StandardActionManager::CollectionProperties)->setWhatsThis(
191  i18n( "Open a dialog to edit the properties of the selected folder." ) );
192  break;
193  case Akonadi::StandardActionManager::CopyItems:
194  mGenericManager->setActionText( Akonadi::StandardActionManager::CopyItems,
195  ki18np( "Copy Email", "Copy %1 Emails" ) );
196  mGenericManager->action( Akonadi::StandardActionManager::CopyItems )->setWhatsThis(
197  i18n( "Copy the selected emails to the clipboard." ) );
198  break;
199  case Akonadi::StandardActionManager::DeleteItems:
200  mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteItems,
201  ki18np( "Delete Email", "Delete %1 Emails" ) );
202  mGenericManager->action( Akonadi::StandardActionManager::DeleteItems )->setWhatsThis(
203  i18n( "Delete the selected emails from the folder." ) );
204  mGenericManager->setContextText(
205  StandardActionManager::DeleteItems, StandardActionManager::MessageBoxText,
206  ki18np( "Do you really want to delete the selected email?",
207  "Do you really want to delete %1 emails?" ) );
208 
209  mGenericManager->setContextText(
210  StandardActionManager::DeleteItems, StandardActionManager::MessageBoxTitle,
211  ki18ncp( "@title:window", "Delete Email?", "Delete Emails?" ) );
212 
213  mGenericManager->setContextText(
214  StandardActionManager::DeleteItems, StandardActionManager::ErrorMessageText,
215  ki18n( "Could not delete email: %1" ) );
216 
217  mGenericManager->setContextText(
218  StandardActionManager::DeleteItems, StandardActionManager::ErrorMessageTitle,
219  i18n( "Email deletion failed" ) );
220  break;
221  case Akonadi::StandardActionManager::CutItems:
222  mGenericManager->setActionText( Akonadi::StandardActionManager::CutItems,
223  ki18np( "Cut Email", "Cut %1 Emails" ) );
224  mGenericManager->action( Akonadi::StandardActionManager::CutItems )->setWhatsThis(
225  i18n( "Cut the selected emails from the folder." ) );
226  break;
227  case Akonadi::StandardActionManager::CreateResource:
228  mGenericManager->action( Akonadi::StandardActionManager::CreateResource )->setText(
229  i18n( "Add &Account..." ) );
230  mGenericManager->action( Akonadi::StandardActionManager::CreateResource )->setWhatsThis(
231  i18n( "Add a new account<p>"
232  "You will be presented with a dialog where you can select "
233  "the type of the account that shall be added.</p>" ) );
234  mGenericManager->setContextText(
235  StandardActionManager::CreateResource, StandardActionManager::DialogTitle,
236  i18nc( "@title:window", "Add Account" ) );
237 
238  mGenericManager->setContextText(
239  StandardActionManager::CreateResource, StandardActionManager::ErrorMessageText,
240  ki18n( "Could not create account: %1" ) );
241 
242  mGenericManager->setContextText(
243  StandardActionManager::CreateResource, StandardActionManager::ErrorMessageTitle,
244  i18n( "Account creation failed" ) );
245  break;
246  case Akonadi::StandardActionManager::DeleteResources:
247  mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteResources,
248  ki18np( "&Delete Account", "&Delete %1 Accounts" ) );
249  mGenericManager->action( Akonadi::StandardActionManager::DeleteResources )->setWhatsThis(
250  i18n( "Delete the selected accounts<p>"
251  "The currently selected accounts will be deleted, "
252  "along with all the emails they contain.</p>" ) );
253  mGenericManager->setContextText(
254  StandardActionManager::DeleteResources, StandardActionManager::MessageBoxText,
255  ki18np( "Do you really want to delete this account?",
256  "Do you really want to delete %1 accounts?" ) );
257 
258  mGenericManager->setContextText(
259  StandardActionManager::DeleteResources, StandardActionManager::MessageBoxTitle,
260  ki18ncp( "@title:window", "Delete Account?", "Delete Accounts?" ) );
261  break;
262  case Akonadi::StandardActionManager::ResourceProperties:
263  mGenericManager->action( Akonadi::StandardActionManager::ResourceProperties )->setText(
264  i18n( "Account Properties..." ) );
265  mGenericManager->action( Akonadi::StandardActionManager::ResourceProperties )->setWhatsThis(
266  i18n( "Open a dialog to edit properties of the selected account." ) );
267  break;
268  case Akonadi::StandardActionManager::SynchronizeResources:
269  mGenericManager->setActionText( Akonadi::StandardActionManager::SynchronizeResources,
270  ki18np( "Update Account", "Update %1 Accounts" ) );
271  mGenericManager->action( Akonadi::StandardActionManager::SynchronizeResources )->setWhatsThis(
272  i18n( "Updates the content of all folders of the selected accounts." ) );
273 
274  break;
275  case Akonadi::StandardActionManager::SynchronizeCollectionsRecursive:
276 
277  mGenericManager->setActionText( Akonadi::StandardActionManager::SynchronizeCollectionsRecursive,
278  ki18np( "Update folder and its subfolders", "Update folders and their subfolders" ) );
279 
280  mGenericManager->action( Akonadi::StandardActionManager::SynchronizeCollectionsRecursive )->setWhatsThis(
281  i18n( "Update the content of the selected folders and their subfolders." ) );
282  break;
283  case Akonadi::StandardActionManager::Paste:
284  mGenericManager->setContextText(
285  StandardActionManager::Paste, StandardActionManager::ErrorMessageText,
286  ki18n( "Could not paste email: %1" ) );
287 
288  mGenericManager->setContextText(
289  StandardActionManager::Paste, StandardActionManager::ErrorMessageTitle,
290  i18n( "Paste failed" ) );
291  break;
292  default:
293  break;
294  }
295 
296  }
297 
298  void updateActions()
299  {
300  const Akonadi::Item::List selectedItems = mGenericManager->selectedItems();
301  const Akonadi::Collection::List selectedCollections = mGenericManager->selectedCollections();
302 
303  bool itemIsSelected = !selectedItems.isEmpty();
304  bool collectionIsSelected = !selectedCollections.isEmpty();
305 
306  if ( itemIsSelected ) {
307  bool allMarkedAsImportant = true;
308  bool allMarkedAsRead = true;
309  bool allMarkedAsUnread = true;
310  bool allMarkedAsActionItem = true;
311 
312  foreach ( const Akonadi::Item &item, selectedItems ) {
313  Akonadi::MessageStatus status;
314  status.setStatusFromFlags( item.flags() );
315  if ( !status.isImportant() ) {
316  allMarkedAsImportant = false;
317  }
318  if ( !status.isRead() ) {
319  allMarkedAsRead= false;
320  } else {
321  allMarkedAsUnread = false;
322  }
323  if ( !status.isToAct() ) {
324  allMarkedAsActionItem = false;
325  }
326  }
327 
328  QAction *action = mActions.value( Akonadi::StandardMailActionManager::MarkMailAsRead );
329  if ( action ) {
330  updateMarkAction( action, allMarkedAsRead );
331  if ( allMarkedAsRead ) {
332  action->setEnabled( false );
333  } else {
334  action->setEnabled( true );
335  }
336  }
337 
338  action = mActions.value( Akonadi::StandardMailActionManager::MarkMailAsUnread );
339  if ( action ) {
340  updateMarkAction( action, allMarkedAsUnread );
341  if ( allMarkedAsUnread ) {
342  action->setEnabled( false );
343  } else {
344  action->setEnabled( true );
345  }
346  }
347 
348  action = mActions.value( Akonadi::StandardMailActionManager::MarkMailAsImportant );
349  if ( action ) {
350  updateMarkAction( action, allMarkedAsImportant );
351  if ( allMarkedAsImportant ) {
352  action->setText( i18n( "Remove Important Mark" ) );
353  } else {
354  action->setText( i18n( "&Mark Mail as Important" ) );
355  }
356  action->setEnabled( true );
357  }
358 
359  action = mActions.value( Akonadi::StandardMailActionManager::MarkMailAsActionItem );
360  if ( action ) {
361  updateMarkAction( action, allMarkedAsActionItem );
362  if ( allMarkedAsActionItem ) {
363  action->setText( i18n( "Remove Action Item Mark" ) );
364  } else {
365  action->setText( i18n( "&Mark Mail as Action Item" ) );
366  }
367  action->setEnabled( true );
368  }
369  } else {
370  QAction *action = mActions.value( Akonadi::StandardMailActionManager::MarkMailAsRead );
371  if ( action ) {
372  action->setEnabled( false );
373  }
374 
375  action = mActions.value( Akonadi::StandardMailActionManager::MarkMailAsImportant );
376  if ( action ) {
377  action->setEnabled( false );
378  }
379 
380  action = mActions.value( Akonadi::StandardMailActionManager::MarkMailAsActionItem );
381  if ( action ) {
382  action->setEnabled( false );
383  }
384  }
385 
386  bool enableMarkAllAsRead = false;
387  bool enableMarkAllAsUnread = false;
388  bool canDeleteItem = true;
389  bool isSystemFolder = false;
390  if ( collectionIsSelected ) {
391  foreach ( const Collection &collection, selectedCollections ) {
392  if ( collection.isValid() ) {
393  const Akonadi::CollectionStatistics stats = collection.statistics();
394  if ( !enableMarkAllAsRead ) {
395  enableMarkAllAsRead = ( stats.unreadCount() > 0 );
396  }
397  if ( !enableMarkAllAsUnread ) {
398  enableMarkAllAsUnread = ( stats.count() != stats.unreadCount() );
399  }
400  if ( canDeleteItem ) {
401  canDeleteItem = collection.rights() & Akonadi::Collection::CanDeleteItem;
402  }
403  if ( !isSystemFolder ) {
404  isSystemFolder = ( collection == SpecialMailCollections::self()->defaultCollection( SpecialMailCollections::Inbox ) ||
405  collection == SpecialMailCollections::self()->defaultCollection( SpecialMailCollections::Outbox ) ||
406  collection == SpecialMailCollections::self()->defaultCollection( SpecialMailCollections::SentMail ) ||
407  collection == SpecialMailCollections::self()->defaultCollection( SpecialMailCollections::Trash ) ||
408  collection == SpecialMailCollections::self()->defaultCollection( SpecialMailCollections::Drafts ) ||
409  collection == SpecialMailCollections::self()->defaultCollection( SpecialMailCollections::Templates ) );
410  }
411  //We will not change after that.
412  if ( enableMarkAllAsRead && enableMarkAllAsUnread && !canDeleteItem && isSystemFolder ) {
413  break;
414  }
415  }
416  }
417  }
418  if ( isSystemFolder ) {
419  if ( mGenericManager->action( StandardActionManager::DeleteCollections ) ) {
420  mGenericManager->action( StandardActionManager::DeleteCollections )->setEnabled( false );
421  }
422  }
423 
424  if ( mActions.contains( Akonadi::StandardMailActionManager::MoveToTrash ) ) {
425  mActions.value( Akonadi::StandardMailActionManager::MoveToTrash )->setEnabled( itemIsSelected && canDeleteItem );
426  }
427  if ( mActions.contains( Akonadi::StandardMailActionManager::RemoveDuplicates ) ) {
428  mActions.value( Akonadi::StandardMailActionManager::RemoveDuplicates )->setEnabled( canDeleteItem );
429  }
430 
431  QAction *action = mActions.value( Akonadi::StandardMailActionManager::MarkAllMailAsRead );
432  if ( action ) {
433  action->setEnabled( enableMarkAllAsRead );
434  }
435 
436  action = mActions.value( Akonadi::StandardMailActionManager::MarkAllMailAsUnread );
437  if ( action ) {
438  action->setEnabled( enableMarkAllAsUnread );
439  }
440 
441  emit mParent->actionStateUpdated();
442  }
443 
444  void updateMarkAction( QAction* action, bool allMarked )
445  {
446  QByteArray data = action->data().toByteArray();
447  if ( allMarked ) {
448  if ( !data.startsWith( '!' ) ) {
449  data.prepend( '!' );
450  }
451  } else {
452  if ( data.startsWith( '!' ) ) {
453  data = data.mid( 1 );
454  }
455  }
456  action->setData( data );
457  }
458 
459  void slotMarkAs()
460  {
461  const QAction *action = qobject_cast<QAction*>( mParent->sender() );
462  Q_ASSERT( action );
463 
464  const Akonadi::Item::List items = mGenericManager->selectedItems();
465  if ( items.isEmpty() ) {
466  return;
467  }
468 
469  QByteArray typeStr = action->data().toByteArray();
470  kDebug() << "Mark mail as: " << typeStr;
471 
472  bool invert = false;
473  if ( typeStr.startsWith( '!' ) ) {
474  invert = true;
475  typeStr = typeStr.mid( 1 );
476  }
477 
478  Akonadi::MessageStatus targetStatus;
479  targetStatus.setStatusFromStr( QLatin1String( typeStr ) );
480 
481  StandardMailActionManager::Type type = MarkMailAsRead;
482  if ( typeStr == "U" ) {
483  type = MarkMailAsUnread;
484  targetStatus.setRead( true );
485  invert = true;
486  } else if ( typeStr == "K" ) {
487  type = MarkMailAsActionItem;
488  } else if ( typeStr == "G" ) {
489  type = MarkMailAsImportant;
490  }
491 
492  if ( mInterceptedActions.contains( type ) ) {
493  return;
494  }
495 
496  MarkAsCommand *command = new MarkAsCommand( targetStatus, items, invert, mParent );
497  command->execute();
498  }
499 
500  void slotMarkAllAs()
501  {
502  const QAction *action = qobject_cast<QAction*>( mParent->sender() );
503  Q_ASSERT( action );
504 
505  QByteArray typeStr = action->data().toByteArray();
506  kDebug() << "Mark all as: " << typeStr;
507 
508  const Akonadi::Collection::List collections = mGenericManager->selectedCollections();
509  if ( collections.isEmpty() ) {
510  return;
511  }
512 
513  Akonadi::MessageStatus targetStatus;
514  targetStatus.setStatusFromStr( QLatin1String( typeStr ) );
515 
516  bool invert = false;
517  if ( typeStr.startsWith( '!' ) ) {
518  invert = true;
519  typeStr = typeStr.mid( 1 );
520  }
521 
522  StandardMailActionManager::Type type = MarkAllMailAsRead;
523  if ( typeStr == "U" ) {
524  type = MarkAllMailAsUnread;
525  targetStatus.setRead( true );
526  invert = true;
527  } else if ( typeStr == "K" ) {
528  type = MarkAllMailAsActionItem;
529  } else if ( typeStr == "G" ) {
530  type = MarkAllMailAsImportant;
531  }
532 
533  if ( mInterceptedActions.contains( type ) ) {
534  return;
535  }
536 
537  MarkAsCommand *command = new MarkAsCommand( targetStatus, collections, invert, mParent );
538  command->execute();
539  }
540 
541  void slotMoveToTrash()
542  {
543  if ( mInterceptedActions.contains( StandardMailActionManager::MoveToTrash ) ) {
544  return;
545  }
546 
547  if ( mCollectionSelectionModel->selection().indexes().isEmpty() ) {
548  return;
549  }
550 
551  const Item::List items = mGenericManager->selectedItems();
552  if ( items.isEmpty() ) {
553  return;
554  }
555 
556  MoveToTrashCommand *command = new MoveToTrashCommand( mCollectionSelectionModel->model(), items, mParent );
557  command->execute();
558  }
559 
560  void slotMoveAllToTrash()
561  {
562  if ( mInterceptedActions.contains( StandardMailActionManager::MoveAllToTrash ) ) {
563  return;
564  }
565 
566  if ( mCollectionSelectionModel->selection().indexes().isEmpty() ) {
567  return;
568  }
569 
570  const Collection::List collections = mGenericManager->selectedCollections();
571  if ( collections.isEmpty() ) {
572  return;
573  }
574 
575  MoveToTrashCommand *command = new MoveToTrashCommand( mCollectionSelectionModel->model(), collections, mParent );
576  command->execute();
577  }
578 
579  void slotRemoveDuplicates()
580  {
581  if ( mInterceptedActions.contains( StandardMailActionManager::RemoveDuplicates ) ) {
582  return;
583  }
584 
585  const Collection::List collections = mGenericManager->selectedCollections();
586  if ( collections.isEmpty() ) {
587  return;
588  }
589 
590  RemoveDuplicatesJob *job = new RemoveDuplicatesJob( collections, mParent );
591  connect( job, SIGNAL(finished(KJob*)), mParent, SLOT(slotJobFinished(KJob*)) );
592  }
593 
594  void slotJobFinished( KJob *job )
595  {
596  if ( job->error() ) {
597  Util::showJobError( job );
598  }
599  }
600 
601  void slotEmptyAllTrash()
602  {
603  if ( mInterceptedActions.contains( StandardMailActionManager::EmptyAllTrash ) ) {
604  return;
605  }
606 
607  EmptyTrashCommand *command = new EmptyTrashCommand( const_cast<QAbstractItemModel*>( mCollectionSelectionModel->model() ), mParent );
608  command->execute();
609  }
610 
611  void slotEmptyTrash()
612  {
613  if ( mInterceptedActions.contains( StandardMailActionManager::EmptyTrash ) ) {
614  return;
615  }
616 
617  if ( mCollectionSelectionModel->selection().indexes().isEmpty() ) {
618  return;
619  }
620 
621  const Collection::List collections = mGenericManager->selectedCollections();
622  if ( collections.count() != 1 ) {
623  return;
624  }
625 
626  EmptyTrashCommand *command = new EmptyTrashCommand( collections.first(), mParent );
627  command->execute();
628  }
629 
630  KActionCollection *mActionCollection;
631  QWidget *mParentWidget;
632  StandardActionManager *mGenericManager;
633  QItemSelectionModel *mCollectionSelectionModel;
634  QItemSelectionModel *mItemSelectionModel;
635  QHash<StandardMailActionManager::Type, KAction*> mActions;
636  QSet<StandardMailActionManager::Type> mInterceptedActions;
637  StandardMailActionManager *mParent;
638 };
639 
640 
641 StandardMailActionManager::StandardMailActionManager( KActionCollection *actionCollection, QWidget *parent )
642  : QObject( parent ), d( new Private( actionCollection, parent, this ) )
643 {
644 }
645 
646 StandardMailActionManager::~StandardMailActionManager()
647 {
648  delete d;
649 }
650 
651 void StandardMailActionManager::setCollectionSelectionModel( QItemSelectionModel *selectionModel )
652 {
653  d->mCollectionSelectionModel = selectionModel;
654  d->mGenericManager->setCollectionSelectionModel( selectionModel );
655 
656  connect( selectionModel->model(), SIGNAL(rowsInserted(QModelIndex,int,int)),
657  SLOT(updateActions()) );
658  connect( selectionModel->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
659  SLOT(updateActions()) );
660  connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
661  SLOT(updateActions()) );
662 
663  d->updateActions();
664 }
665 
666 void StandardMailActionManager::setItemSelectionModel( QItemSelectionModel* selectionModel )
667 {
668  d->mItemSelectionModel = selectionModel;
669  d->mGenericManager->setItemSelectionModel( selectionModel );
670 
671  connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
672  SLOT(updateActions()) );
673 
674  //to catch item modifications, listen to the model's dataChanged signal as well
675  connect( selectionModel->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
676  SLOT(updateActions()) );
677 
678  d->updateActions();
679 }
680 
681 KAction* StandardMailActionManager::createAction( Type type )
682 {
683  if ( d->mActions.contains( type ) ) {
684  return d->mActions.value( type );
685  }
686 
687  KAction *action = 0;
688 
689  switch ( type ) {
690  case MarkMailAsRead:
691  action = new KAction( d->mParentWidget );
692  action->setIcon( KIcon( QLatin1String( "mail-mark-read" ) ) );
693  action->setText( i18n( "&Mark Mail as Read" ) );
694  action->setIconText( i18n( "Mark as Read" ) );
695  action->setWhatsThis( i18n( "Mark selected messages as read" ) );
696  d->mActions.insert( MarkMailAsRead, action );
697  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_mark_as_read" ), action );
698  action->setData( QByteArray( "R" ) );
699  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMarkAs()) );
700  break;
701  case MarkMailAsUnread:
702  action = new KAction( d->mParentWidget );
703  action->setText( i18n( "&Mark Mail as Unread" ) );
704  action->setIconText( i18n( "Mark as Unread" ) );
705  action->setIcon( KIcon( QLatin1String( "mail-mark-unread" ) ) );
706  d->mActions.insert( MarkMailAsUnread, action );
707  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_mark_as_unread" ), action );
708  action->setShortcut( Qt::CTRL+Qt::Key_U );
709  action->setData( QByteArray( "U" ) );
710  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMarkAs()) );
711  break;
712  case MarkMailAsImportant:
713  action = new KAction( d->mParentWidget );
714  action->setIcon( KIcon( QLatin1String( "mail-mark-important" ) ) );
715  action->setText( i18n( "&Mark Mail as Important" ) );
716  action->setIconText( i18n( "Mark as Important" ) );
717  d->mActions.insert( MarkMailAsImportant, action );
718  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_mark_as_important" ), action );
719  action->setData( QByteArray( "G" ) );
720  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMarkAs()) );
721  break;
722  case MarkMailAsActionItem:
723  action = new KAction( d->mParentWidget );
724  action->setText( i18n( "&Mark Mail as Action Item" ) );
725  action->setIconText( i18n( "Mark as Action Item" ) );
726  action->setIcon( KIcon( QLatin1String( "mail-mark-task" ) ) );
727  d->mActions.insert( MarkMailAsActionItem, action );
728  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_mark_as_action_item" ), action );
729  action->setData( QByteArray( "K" ) );
730  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMarkAs()) );
731  break;
732  case MarkAllMailAsRead:
733  action = new KAction( d->mParentWidget );
734  action->setIcon( KIcon( QLatin1String( "mail-mark-read" ) ) );
735  action->setText( i18n( "Mark &All Mails as Read" ) );
736  action->setIconText( i18n( "Mark All as Read" ) );
737  d->mActions.insert( MarkAllMailAsRead, action );
738  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_mark_all_as_read" ), action );
739  action->setData( QByteArray( "R" ) );
740  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMarkAllAs()) );
741  break;
742  case MarkAllMailAsUnread:
743  action = new KAction( d->mParentWidget );
744  action->setText( i18n( "Mark &All Mails as Unread" ) );
745  action->setIconText( i18n( "Mark All as Unread" ) );
746  action->setIcon( KIcon( QLatin1String( "mail-mark-unread" ) ) );
747  d->mActions.insert( MarkAllMailAsUnread, action );
748  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_mark_all_as_unread" ), action );
749  action->setData( QByteArray( "U" ) );
750  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMarkAllAs()) );
751  break;
752  case MarkAllMailAsImportant:
753  action = new KAction( d->mParentWidget );
754  action->setText( i18n( "Mark &All Mails as Important" ) );
755  action->setIconText( i18n( "Mark All as Important" ) );
756  action->setIcon( KIcon( QLatin1String( "mail-mark-important" ) ) );
757  d->mActions.insert( MarkAllMailAsImportant, action );
758  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_mark_all_as_important" ), action );
759  action->setData( QByteArray( "G" ) );
760  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMarkAllAs()) );
761  break;
762  case MarkAllMailAsActionItem:
763  action = new KAction( d->mParentWidget );
764  action->setText( i18n( "Mark &All Mails as Action Item" ) );
765  action->setIconText( i18n( "Mark All as Action Item" ) );
766  action->setIcon( KIcon( QLatin1String( "mail-mark-task" ) ) );
767  d->mActions.insert( MarkAllMailAsActionItem, action );
768  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_mark_all_as_action_item" ), action );
769  action->setData( QByteArray( "K" ) );
770  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMarkAllAs()) );
771  break;
772  case MoveToTrash:
773  action = new KAction( d->mParentWidget );
774  action->setIcon( KIcon( QLatin1String( "user-trash" ) ) );
775  action->setText( i18n( "Move to &Trash" ) );
776  action->setShortcut( QKeySequence( Qt::Key_Delete ) );
777  action->setWhatsThis( i18n( "Move message to trashcan" ) );
778  d->mActions.insert( MoveToTrash, action );
779  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_move_to_trash" ), action );
780  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMoveToTrash()) );
781  break;
782  case MoveAllToTrash:
783  action = new KAction( d->mParentWidget );
784  action->setIcon( KIcon( QLatin1String( "user-trash" ) ) );
785  action->setText( i18n( "Move All to &Trash" ) );
786  d->mActions.insert( MoveAllToTrash, action );
787  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_move_all_to_trash" ), action );
788  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotMoveAllToTrash()) );
789  break;
790  case RemoveDuplicates:
791  action = new KAction( d->mParentWidget );
792  action->setText( i18n( "Remove &Duplicate Mails" ) );
793  action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_Asterisk ) );
794  d->mActions.insert( RemoveDuplicates, action );
795  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_remove_duplicates" ), action );
796  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotRemoveDuplicates()) );
797  break;
798  case EmptyAllTrash:
799  action = new KAction( d->mParentWidget );
800  action->setText( i18n( "Empty All &Trash Folders" ) );
801  d->mActions.insert( EmptyAllTrash, action );
802  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_empty_all_trash" ), action );
803  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotEmptyAllTrash()) );
804  break;
805  case EmptyTrash:
806  action = new KAction( d->mParentWidget );
807  action->setText( i18n( "E&mpty Trash" ) );
808  d->mActions.insert( EmptyTrash, action );
809  d->mActionCollection->addAction( QString::fromLatin1( "akonadi_empty_trash" ), action );
810  connect( action, SIGNAL(triggered(bool)), this, SLOT(slotEmptyTrash()) );
811  break;
812  default:
813  Q_ASSERT( false ); // should never happen
814  break;
815  }
816 
817  return action;
818 }
819 
820 KAction* StandardMailActionManager::createAction( StandardActionManager::Type type )
821 {
822  KAction *act = d->mGenericManager->action(type);
823  if(!act )
824  act = d->mGenericManager->createAction( type );
825  d->updateGenericAction(type);
826  return act;
827 }
828 
829 void StandardMailActionManager::createAllActions()
830 {
831  createAction( MarkMailAsRead );
832  createAction( MarkMailAsUnread );
833  createAction( MarkMailAsImportant );
834  createAction( MarkMailAsActionItem );
835  createAction( MarkAllMailAsRead );
836  createAction( MarkAllMailAsUnread );
837  createAction( MarkAllMailAsImportant );
838  createAction( MarkAllMailAsActionItem );
839  createAction( MoveToTrash );
840  createAction( MoveAllToTrash );
841  createAction( RemoveDuplicates );
842  createAction( EmptyAllTrash );
843  createAction( EmptyTrash );
844 
845  d->mGenericManager->createAllActions();
846  d->updateGenericAllActions();
847 
848  d->updateActions();
849 }
850 
851 KAction* StandardMailActionManager::action( Type type ) const
852 {
853  if ( d->mActions.contains( type ) ) {
854  return d->mActions.value( type );
855  }
856 
857  return 0;
858 }
859 
860 KAction* StandardMailActionManager::action( StandardActionManager::Type type ) const
861 {
862  return d->mGenericManager->action( type );
863 }
864 
865 void StandardMailActionManager::setActionText( StandardActionManager::Type type, const KLocalizedString &text )
866 {
867  d->mGenericManager->setActionText( type, text );
868 }
869 
870 void StandardMailActionManager::interceptAction( Type type, bool intercept )
871 {
872  if ( intercept ) {
873  d->mInterceptedActions.insert( type );
874  } else {
875  d->mInterceptedActions.remove( type );
876  }
877 }
878 
879 void StandardMailActionManager::interceptAction( StandardActionManager::Type type, bool intercept )
880 {
881  d->mGenericManager->interceptAction( type, intercept );
882 }
883 
884 Akonadi::Collection::List StandardMailActionManager::selectedCollections() const
885 {
886  return d->mGenericManager->selectedCollections();
887 }
888 
889 Akonadi::Item::List StandardMailActionManager::selectedItems() const
890 {
891  return d->mGenericManager->selectedItems();
892 }
893 
894 void StandardMailActionManager::setFavoriteCollectionsModel( FavoriteCollectionsModel *favoritesModel )
895 {
896  d->mGenericManager->setFavoriteCollectionsModel( favoritesModel );
897 }
898 
899 void StandardMailActionManager::setFavoriteSelectionModel( QItemSelectionModel *selectionModel )
900 {
901  d->mGenericManager->setFavoriteSelectionModel( selectionModel );
902 }
903 
904 void StandardMailActionManager::setCollectionPropertiesPageNames( const QStringList &names )
905 {
906  d->mGenericManager->setCollectionPropertiesPageNames( names );
907 }
908 
909 Akonadi::StandardActionManager* StandardMailActionManager::standardActionManager() const
910 {
911  return d->mGenericManager;
912 }
913 
914 #include "moc_standardmailactionmanager.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Wed Apr 3 2013 13:31:46 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.2 API Reference

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