22 #include "standardcontactactionmanager.h"
24 #include "contacteditordialog.h"
25 #include "contactgroupeditordialog.h"
27 #include <akonadi/entitytreemodel.h>
28 #include <akonadi/mimetypechecker.h>
29 #include <kabc/addressee.h>
30 #include <kabc/contactgroup.h>
33 #include <kactioncollection.h>
34 #include <klocalizedstring.h>
35 #include <kmessagebox.h>
37 #include <QtCore/QPointer>
38 #include <QItemSelectionModel>
40 using namespace Akonadi;
42 class StandardContactActionManager::Private
46 : mActionCollection( actionCollection ), mParentWidget( parentWidget ),
47 mCollectionSelectionModel( 0 ), mItemSelectionModel( 0 ), mParent( parent )
49 mGenericManager =
new StandardActionManager( actionCollection, parentWidget );
53 mGenericManager->setMimeTypeFilter(
54 QStringList() << KABC::Addressee::mimeType() << KABC::ContactGroup::mimeType() );
61 delete mGenericManager;
64 void updateGenericAllActions()
66 updateGenericAction(StandardActionManager::CreateCollection);
67 updateGenericAction(StandardActionManager::CopyCollections);
68 updateGenericAction(StandardActionManager::DeleteCollections);
69 updateGenericAction(StandardActionManager::SynchronizeCollections);
70 updateGenericAction(StandardActionManager::CollectionProperties);
71 updateGenericAction(StandardActionManager::CopyItems);
72 updateGenericAction(StandardActionManager::Paste);
73 updateGenericAction(StandardActionManager::DeleteItems);
74 updateGenericAction(StandardActionManager::ManageLocalSubscriptions);
75 updateGenericAction(StandardActionManager::AddToFavoriteCollections);
76 updateGenericAction(StandardActionManager::RemoveFromFavoriteCollections);
77 updateGenericAction(StandardActionManager::RenameFavoriteCollection);
78 updateGenericAction(StandardActionManager::CopyCollectionToMenu);
79 updateGenericAction(StandardActionManager::CopyItemToMenu);
80 updateGenericAction(StandardActionManager::MoveItemToMenu);
81 updateGenericAction(StandardActionManager::MoveCollectionToMenu);
82 updateGenericAction(StandardActionManager::CutItems);
83 updateGenericAction(StandardActionManager::CutCollections);
84 updateGenericAction(StandardActionManager::CreateResource);
85 updateGenericAction(StandardActionManager::DeleteResources);
86 updateGenericAction(StandardActionManager::ResourceProperties);
87 updateGenericAction(StandardActionManager::SynchronizeResources);
88 updateGenericAction(StandardActionManager::ToggleWorkOffline);
89 updateGenericAction(StandardActionManager::CopyCollectionToDialog);
90 updateGenericAction(StandardActionManager::MoveCollectionToDialog);
91 updateGenericAction(StandardActionManager::CopyItemToDialog);
92 updateGenericAction(StandardActionManager::MoveItemToDialog);
93 updateGenericAction(StandardActionManager::SynchronizeCollectionsRecursive);
94 updateGenericAction(StandardActionManager::MoveCollectionsToTrash);
95 updateGenericAction(StandardActionManager::MoveItemsToTrash);
96 updateGenericAction(StandardActionManager::RestoreCollectionsFromTrash);
97 updateGenericAction(StandardActionManager::RestoreItemsFromTrash);
98 updateGenericAction(StandardActionManager::MoveToTrashRestoreCollection);
99 updateGenericAction(StandardActionManager::MoveToTrashRestoreCollectionAlternative);
100 updateGenericAction(StandardActionManager::MoveToTrashRestoreItem);
101 updateGenericAction(StandardActionManager::MoveToTrashRestoreItemAlternative);
102 updateGenericAction(StandardActionManager::SynchronizeFavoriteCollections);
106 void updateGenericAction(StandardActionManager::Type type)
109 case Akonadi::StandardActionManager::CreateCollection:
110 mGenericManager->action( Akonadi::StandardActionManager::CreateCollection )->setText(
111 i18n(
"Add Address Book Folder..." ) );
113 mGenericManager->action( Akonadi::StandardActionManager::CreateCollection )->setWhatsThis(
114 i18n(
"Add a new address book folder to the currently selected address book folder." ) );
115 mGenericManager->setContextText(
116 StandardActionManager::CreateCollection, StandardActionManager::DialogTitle,
117 i18nc(
"@title:window",
"New Address Book Folder" ) );
119 mGenericManager->setContextText(
120 StandardActionManager::CreateCollection, StandardActionManager::ErrorMessageText,
121 ki18n(
"Could not create address book folder: %1" ) );
123 mGenericManager->setContextText(
124 StandardActionManager::CreateCollection, StandardActionManager::ErrorMessageTitle,
125 i18n(
"Address book folder creation failed" ) );
126 mGenericManager->action(Akonadi::StandardActionManager::CreateCollection)->setProperty(
"ContentMimeTypes",
QStringList() <<
133 case Akonadi::StandardActionManager::CopyCollections:
134 mGenericManager->setActionText( Akonadi::StandardActionManager::CopyCollections,
135 ki18np(
"Copy Address Book Folder",
136 "Copy %1 Address Book Folders" ) );
137 mGenericManager->action( Akonadi::StandardActionManager::CopyCollections )->setWhatsThis(
138 i18n(
"Copy the selected address book folders to the clipboard." ) );
140 case Akonadi::StandardActionManager::DeleteCollections:
141 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteCollections,
142 ki18np(
"Delete Address Book Folder",
143 "Delete %1 Address Book Folders" ) );
144 mGenericManager->action( Akonadi::StandardActionManager::DeleteCollections )->setWhatsThis(
145 i18n(
"Delete the selected address book folders from the address book." ) );
147 mGenericManager->setContextText(
148 StandardActionManager::DeleteCollections, StandardActionManager::MessageBoxText,
149 ki18np(
"Do you really want to delete this address book folder and all its sub-folders?",
150 "Do you really want to delete %1 address book folders and all their sub-folders?" ) );
151 mGenericManager->setContextText(
152 StandardActionManager::DeleteCollections, StandardActionManager::MessageBoxTitle,
153 ki18ncp(
"@title:window",
"Delete address book folder?",
"Delete address book folders?" ) );
155 mGenericManager->setContextText(
156 StandardActionManager::DeleteCollections, StandardActionManager::ErrorMessageText,
157 ki18n(
"Could not delete address book folder: %1" ) );
159 mGenericManager->setContextText(
160 StandardActionManager::DeleteCollections, StandardActionManager::ErrorMessageTitle,
161 i18n(
"Address book folder deletion failed" ) );
163 case Akonadi::StandardActionManager::SynchronizeCollections:
164 mGenericManager->setActionText( Akonadi::StandardActionManager::SynchronizeCollections,
165 ki18np(
"Update Address Book Folder",
166 "Update %1 Address Book Folders" ) );
167 mGenericManager->action( Akonadi::StandardActionManager::SynchronizeCollections )->setWhatsThis(
168 i18n(
"Update the content of the selected address book folders." ) );
170 case Akonadi::StandardActionManager::CutCollections:
171 mGenericManager->setActionText( Akonadi::StandardActionManager::CutCollections,
172 ki18np(
"Cut Address Book Folder",
173 "Cut %1 Address Book Folders" ) );
174 mGenericManager->action( Akonadi::StandardActionManager::CutCollections )->setWhatsThis(
175 i18n(
"Cut the selected address book folders from the address book." ) );
177 case Akonadi::StandardActionManager::CollectionProperties:
178 mGenericManager->action( Akonadi::StandardActionManager::CollectionProperties )->setText(
179 i18n(
"Folder Properties..." ) );
180 mGenericManager->action( Akonadi::StandardActionManager::CollectionProperties )->setWhatsThis(
181 i18n(
"Open a dialog to edit the properties of the selected address book folder." ) );
182 mGenericManager->setContextText(
183 StandardActionManager::CollectionProperties, StandardActionManager::DialogTitle,
184 ki18nc(
"@title:window",
"Properties of Address Book Folder %1" ) );
186 case Akonadi::StandardActionManager::CopyItems:
187 mGenericManager->setActionText( Akonadi::StandardActionManager::CopyItems,
188 ki18np(
"Copy Contact",
"Copy %1 Contacts" ) );
189 mGenericManager->action( Akonadi::StandardActionManager::CopyItems )->setWhatsThis(
190 i18n(
"Copy the selected contacts to the clipboard." ) );
192 case Akonadi::StandardActionManager::DeleteItems:
193 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteItems,
194 ki18np(
"Delete Contact",
"Delete %1 Contacts" ) );
195 mGenericManager->action( Akonadi::StandardActionManager::DeleteItems )->setWhatsThis(
196 i18n(
"Delete the selected contacts from the address book." ) );
197 mGenericManager->setContextText(
198 StandardActionManager::DeleteItems, StandardActionManager::MessageBoxText,
199 ki18np(
"Do you really want to delete the selected contact?",
200 "Do you really want to delete %1 contacts?" ) );
202 mGenericManager->setContextText(
203 StandardActionManager::DeleteItems, StandardActionManager::MessageBoxTitle,
204 ki18ncp(
"@title:window",
"Delete Contact?",
"Delete Contacts?" ) );
206 mGenericManager->setContextText(
207 StandardActionManager::DeleteItems, StandardActionManager::ErrorMessageText,
208 ki18n(
"Could not delete contact: %1" ) );
210 mGenericManager->setContextText(
211 StandardActionManager::DeleteItems, StandardActionManager::ErrorMessageTitle,
212 i18n(
"Contact deletion failed" ) );
214 case Akonadi::StandardActionManager::CutItems:
215 mGenericManager->setActionText( Akonadi::StandardActionManager::CutItems,
216 ki18np(
"Cut Contact",
"Cut %1 Contacts" ) );
217 mGenericManager->action( Akonadi::StandardActionManager::CutItems )->setWhatsThis(
218 i18n(
"Cut the selected contacts from the address book." ) );
220 case Akonadi::StandardActionManager::CreateResource:
221 mGenericManager->action( Akonadi::StandardActionManager::CreateResource )->setText(
222 i18n(
"Add &Address Book..." ) );
223 mGenericManager->action( Akonadi::StandardActionManager::CreateResource )->setWhatsThis(
224 i18n(
"Add a new address book<p>"
225 "You will be presented with a dialog where you can select "
226 "the type of the address book that shall be added.</p>" ) );
227 mGenericManager->setContextText(
228 StandardActionManager::CreateResource, StandardActionManager::DialogTitle,
229 i18nc(
"@title:window",
"Add Address Book" ) );
231 mGenericManager->setContextText(
232 StandardActionManager::CreateResource, StandardActionManager::ErrorMessageText,
233 ki18n(
"Could not create address book: %1" ) );
235 mGenericManager->setContextText(
236 StandardActionManager::CreateResource, StandardActionManager::ErrorMessageTitle,
237 i18n(
"Address book creation failed" ) );
239 case Akonadi::StandardActionManager::DeleteResources:
241 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteResources,
242 ki18np(
"&Delete Address Book",
243 "&Delete %1 Address Books" ) );
244 mGenericManager->action( Akonadi::StandardActionManager::DeleteResources )->setWhatsThis(
245 i18n(
"Delete the selected address books<p>"
246 "The currently selected address books will be deleted, "
247 "along with all the contacts and contact groups they contain.</p>" ) );
248 mGenericManager->setContextText(
249 StandardActionManager::DeleteResources, StandardActionManager::MessageBoxText,
250 ki18np(
"Do you really want to delete this address book?",
251 "Do you really want to delete %1 address books?" ) );
253 mGenericManager->setContextText(
254 StandardActionManager::DeleteResources, StandardActionManager::MessageBoxTitle,
255 ki18ncp(
"@title:window",
"Delete Address Book?",
"Delete Address Books?" ) );
258 case Akonadi::StandardActionManager::ResourceProperties:
260 mGenericManager->action( Akonadi::StandardActionManager::ResourceProperties )->setText(
261 i18n(
"Address Book Properties..." ) );
262 mGenericManager->action( Akonadi::StandardActionManager::ResourceProperties )->setWhatsThis(
263 i18n(
"Open a dialog to edit properties of the selected address book." ) );
265 case Akonadi::StandardActionManager::SynchronizeResources:
266 mGenericManager->setActionText( Akonadi::StandardActionManager::SynchronizeResources,
267 ki18np(
"Update Address Book",
268 "Update %1 Address Books" ) );
270 mGenericManager->action( Akonadi::StandardActionManager::SynchronizeResources )->setWhatsThis
271 ( i18n(
"Updates the content of all folders of the selected address books." ) );
274 case StandardActionManager::Paste:
275 mGenericManager->setContextText(
276 StandardActionManager::Paste, StandardActionManager::ErrorMessageText,
277 ki18n(
"Could not paste contact: %1" ) );
279 mGenericManager->setContextText(
280 StandardActionManager::Paste, StandardActionManager::ErrorMessageTitle,
281 i18n(
"Paste failed" ) );
289 static bool hasWritableCollection(
const QModelIndex &index,
const QString &mimeType )
291 const Akonadi::Collection collection =
292 index.
data( Akonadi::EntityTreeModel::CollectionRole ).
value<Akonadi::Collection>();
293 if ( collection.isValid() ) {
294 if ( collection.contentMimeTypes().contains( mimeType ) &&
295 ( collection.rights() & Akonadi::Collection::CanCreateItem ) ) {
305 for (
int row = 0; row < model->
rowCount( index ); ++row ) {
306 if ( hasWritableCollection( model->
index( row, 0, index ), mimeType ) ) {
314 bool hasWritableCollection(
const QString &mimeType )
const
316 if ( !mCollectionSelectionModel ) {
321 for (
int row = 0; row < collectionModel->
rowCount(); ++row ) {
322 if ( hasWritableCollection( collectionModel->
index( row, 0,
QModelIndex() ), mimeType ) ) {
333 if ( mItemSelectionModel ) {
334 itemCount = mItemSelectionModel->selectedRows().count();
335 if ( itemCount == 1 ) {
336 const QModelIndex index = mItemSelectionModel->selectedRows().first();
339 if ( mimeType == KABC::Addressee::mimeType() ) {
340 if (mGenericManager->action( Akonadi::StandardActionManager::CopyItems )) {
341 mGenericManager->setActionText( Akonadi::StandardActionManager::CopyItems,
342 ki18np(
"Copy Contact",
"Copy %1 Contacts" ) );
344 if (mGenericManager->action( Akonadi::StandardActionManager::CopyItemToMenu )) {
345 mGenericManager->action( Akonadi::StandardActionManager::CopyItemToMenu )->setText( i18n(
"Copy Contact To" ) );
347 if (mGenericManager->action( Akonadi::StandardActionManager::CopyItemToDialog)) {
348 mGenericManager->action( Akonadi::StandardActionManager::CopyItemToDialog )->setText( i18n(
"Copy Contact To" ) );
350 if (mGenericManager->action( Akonadi::StandardActionManager::DeleteItems)) {
351 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteItems,
352 ki18np(
"Delete Contact",
"Delete %1 Contacts" ) );
354 if (mGenericManager->action( Akonadi::StandardActionManager::CutItems)) {
355 mGenericManager->setActionText( Akonadi::StandardActionManager::CutItems,
356 ki18np(
"Cut Contact",
"Cut %1 Contacts" ) );
358 if (mGenericManager->action( Akonadi::StandardActionManager::MoveItemToMenu)) {
359 mGenericManager->action( Akonadi::StandardActionManager::MoveItemToMenu )->setText( i18n(
"Move Contact To" ) );
361 if (mGenericManager->action( Akonadi::StandardActionManager::MoveItemToDialog )) {
362 mGenericManager->action( Akonadi::StandardActionManager::MoveItemToDialog )->setText( i18n(
"Move Contact To" ) );
367 }
else if ( mimeType == KABC::ContactGroup::mimeType() ) {
368 if (mGenericManager->action( Akonadi::StandardActionManager::CopyItems )) {
369 mGenericManager->setActionText( Akonadi::StandardActionManager::CopyItems,
370 ki18np(
"Copy Group",
"Copy %1 Groups" ) );
372 if (mGenericManager->action( Akonadi::StandardActionManager::CopyItemToMenu)) {
373 mGenericManager->action( Akonadi::StandardActionManager::CopyItemToMenu )->setText( i18n(
"Copy Group To" ) );
375 if (mGenericManager->action( Akonadi::StandardActionManager::CopyItemToDialog )) {
376 mGenericManager->action( Akonadi::StandardActionManager::CopyItemToDialog )->setText( i18n(
"Copy Group To" ) );
378 if (mGenericManager->action( Akonadi::StandardActionManager::DeleteItems)) {
379 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteItems,
380 ki18np(
"Delete Group",
"Delete %1 Groups" ) );
382 if (mGenericManager->action( Akonadi::StandardActionManager::CutItems)) {
383 mGenericManager->setActionText( Akonadi::StandardActionManager::CutItems,
384 ki18np(
"Cut Group",
"Cut %1 Groups" ) );
386 if (mGenericManager->action( Akonadi::StandardActionManager::MoveItemToMenu )) {
387 mGenericManager->action( Akonadi::StandardActionManager::MoveItemToMenu )->setText( i18n(
"Move Group To" ) );
389 if (mGenericManager->action( Akonadi::StandardActionManager::MoveItemToDialog )) {
390 mGenericManager->action( Akonadi::StandardActionManager::MoveItemToDialog )->setText( i18n(
"Move Group To" ) );
408 bool canEditItem =
true;
411 canEditItem = canEditItem && ( itemCount == 1 );
414 const QModelIndexList rows = mItemSelectionModel->selectedRows();
415 if ( rows.count() == 1 ) {
417 const Collection parentCollection = index.
data( EntityTreeModel::ParentCollectionRole ).
value<Collection>();
418 if ( parentCollection.isValid() ) {
419 canEditItem = canEditItem && ( parentCollection.rights() & Collection::CanChangeItem );
426 emit mParent->actionStateUpdated();
429 Collection selectedCollection()
const
431 if ( !mCollectionSelectionModel ) {
435 if ( mCollectionSelectionModel->selectedIndexes().isEmpty() ) {
439 const QModelIndex index = mCollectionSelectionModel->selectedIndexes().first();
444 return index.
data( EntityTreeModel::CollectionRole ).
value<Collection>();
447 void slotCreateContact()
456 dlg->setDefaultAddressBook( selectedCollection() );
461 void slotCreateContactGroup()
470 dlg->setDefaultAddressBook( selectedCollection() );
481 if ( !mItemSelectionModel ) {
485 if ( mItemSelectionModel->selectedIndexes().isEmpty() ) {
489 const QModelIndex index = mItemSelectionModel->selectedIndexes().first();
494 const Item item = index.
data( EntityTreeModel::ItemRole ).
value<Item>();
495 if ( !item.isValid() ) {
499 if ( Akonadi::MimeTypeChecker::isWantedItem( item, KABC::Addressee::mimeType() ) ) {
504 mParent, SLOT(slotContactEditorError(
QString)) );
505 dlg->setContact( item );
508 }
else if ( Akonadi::MimeTypeChecker::isWantedItem( item, KABC::ContactGroup::mimeType() ) ) {
512 dlg->setContactGroup( item );
518 void slotContactEditorError(
const QString& error)
520 KMessageBox::error(mParentWidget, i18n(
"Contact cannot be stored: %1", error), i18n(
"Failed to store contact"));
523 KActionCollection *mActionCollection;
525 StandardActionManager *mGenericManager;
534 :
QObject( parent ), d( new Private( actionCollection, parent, this ) )
545 d->mCollectionSelectionModel = selectionModel;
546 d->mGenericManager->setCollectionSelectionModel( selectionModel );
549 SLOT(updateActions()) );
551 SLOT(updateActions()) );
553 SLOT(updateActions()) );
560 d->mItemSelectionModel = selectionModel;
561 d->mGenericManager->setItemSelectionModel( selectionModel );
564 SLOT(updateActions()) );
571 if ( d->mActions.contains( type ) ) {
572 return d->mActions.value( type );
579 action =
new KAction( d->mParentWidget );
581 action->setText( i18n(
"New &Contact..." ) );
582 action->setShortcut(
QKeySequence( Qt::CTRL + Qt::Key_N ) );
583 action->setWhatsThis( i18n(
"Create a new contact<p>You will be presented with a dialog where you can add data about a person, including addresses and phone numbers.</p>" ) );
585 d->mActionCollection->addAction(
QString::fromLatin1(
"akonadi_contact_create" ), action );
586 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContact()) );
589 action =
new KAction( d->mParentWidget );
590 action->setIcon( KIcon(
QLatin1String(
"user-group-new" ) ) );
591 action->setText( i18n(
"New &Group..." ) );
592 action->setShortcut(
QKeySequence( Qt::CTRL + Qt::Key_G ) );
593 action->setWhatsThis( i18n(
"Create a new group<p>You will be presented with a dialog where you can add a new group of contacts.</p>" ) );
595 d->mActionCollection->addAction(
QString::fromLatin1(
"akonadi_contact_group_create" ), action );
596 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContactGroup()) );
599 action =
new KAction( d->mParentWidget );
600 action->setIcon( KIcon(
QLatin1String(
"document-edit" ) ) );
601 action->setText( i18n(
"Edit Contact..." ) );
602 action->setWhatsThis( i18n(
"Edit the selected contact<p>You will be presented with a dialog where you can edit the data stored about a person, including addresses and phone numbers.</p>" ) );
603 action->setEnabled(
false );
604 d->mActions.insert(
EditItem, action );
605 d->mActionCollection->addAction(
QString::fromLatin1(
"akonadi_contact_item_edit" ), action );
606 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotEditItem()) );
618 KAction *act = d->mGenericManager->action(type);
620 act = d->mGenericManager->createAction( type );
621 d->updateGenericAction(type);
631 d->mGenericManager->createAllActions();
632 d->updateGenericAllActions();
639 if ( d->mActions.contains( type ) ) {
640 return d->mActions.value( type );
648 return d->mGenericManager->action( type );
653 d->mGenericManager->setActionText( type, text );
659 d->mInterceptedActions.insert( type );
661 d->mInterceptedActions.remove( type );
667 d->mGenericManager->interceptAction( type, intercept );
672 return d->mGenericManager->selectedCollections();
677 return d->mGenericManager->selectedItems();
682 d->mGenericManager->setCollectionPropertiesPageNames( names );
684 #include "moc_standardcontactactionmanager.cpp"
virtual int rowCount(const QModelIndex &parent) const =0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
const QAbstractItemModel * model() const
QVariant data(int role) const
QString fromLatin1(const char *str, int size)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
const QAbstractItemModel * model() const