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>
32 #include <kactioncollection.h>
34 #include <kmessagebox.h>
36 #include <QtCore/QPointer>
37 #include <QtGui/QItemSelectionModel>
39 using namespace Akonadi;
41 class StandardContactActionManager::Private
45 : mActionCollection( actionCollection ), mParentWidget( parentWidget ),
46 mCollectionSelectionModel( 0 ), mItemSelectionModel( 0 ), mParent( parent )
48 mGenericManager =
new StandardActionManager( actionCollection, parentWidget );
51 mGenericManager->createAllActions();
53 mGenericManager->action( Akonadi::StandardActionManager::CreateCollection )->setText(
54 i18n(
"Add Address Book Folder..." ) );
55 mGenericManager->action( Akonadi::StandardActionManager::CreateCollection )->setWhatsThis(
56 i18n(
"Add a new address book folder to the currently selected address book folder." ) );
58 mGenericManager->setActionText( Akonadi::StandardActionManager::CopyCollections,
59 ki18np(
"Copy Address Book Folder",
60 "Copy %1 Address Book Folders" ) );
61 mGenericManager->action( Akonadi::StandardActionManager::CopyCollections )->setWhatsThis(
62 i18n(
"Copy the selected address book folders to the clipboard." ) );
64 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteCollections,
65 ki18np(
"Delete Address Book Folder",
66 "Delete %1 Address Book Folders" ) );
67 mGenericManager->action( Akonadi::StandardActionManager::DeleteCollections )->setWhatsThis(
68 i18n(
"Delete the selected address book folders from the address book." ) );
70 mGenericManager->setActionText( Akonadi::StandardActionManager::SynchronizeCollections,
71 ki18np(
"Update Address Book Folder",
72 "Update %1 Address Book Folders" ) );
73 mGenericManager->action( Akonadi::StandardActionManager::SynchronizeCollections )->setWhatsThis(
74 i18n(
"Update the content of the selected address book folders." ) );
76 mGenericManager->setActionText( Akonadi::StandardActionManager::CutCollections,
77 ki18np(
"Cut Address Book Folder",
78 "Cut %1 Address Book Folders" ) );
79 mGenericManager->action( Akonadi::StandardActionManager::CutCollections )->setWhatsThis(
80 i18n(
"Cut the selected address book folders from the address book." ) );
82 mGenericManager->action( Akonadi::StandardActionManager::CollectionProperties )->setText(
83 i18n(
"Folder Properties..." ) );
84 mGenericManager->action( Akonadi::StandardActionManager::CollectionProperties)->setWhatsThis(
85 i18n(
"Open a dialog to edit the properties of the selected address book folder." ) );
87 mGenericManager->setActionText( Akonadi::StandardActionManager::CopyItems,
88 ki18np(
"Copy Contact",
"Copy %1 Contacts" ) );
89 mGenericManager->action( Akonadi::StandardActionManager::CopyItems )->setWhatsThis(
90 i18n(
"Copy the selected contacts to the clipboard." ) );
92 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteItems,
93 ki18np(
"Delete Contact",
"Delete %1 Contacts" ) );
94 mGenericManager->action( Akonadi::StandardActionManager::DeleteItems )->setWhatsThis(
95 i18n(
"Delete the selected contacts from the address book." ) );
97 mGenericManager->setActionText( Akonadi::StandardActionManager::CutItems,
98 ki18np(
"Cut Contact",
"Cut %1 Contacts" ) );
99 mGenericManager->action( Akonadi::StandardActionManager::CutItems )->setWhatsThis(
100 i18n(
"Cut the selected contacts from the address book." ) );
102 mGenericManager->action( Akonadi::StandardActionManager::CreateResource )->setText(
103 i18n(
"Add &Address Book..." ) );
104 mGenericManager->action( Akonadi::StandardActionManager::CreateResource )->setWhatsThis(
105 i18n(
"Add a new address book<p>"
106 "You will be presented with a dialog where you can select "
107 "the type of the address book that shall be added.</p>" ) );
109 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteResources,
110 ki18np(
"&Delete Address Book",
111 "&Delete %1 Address Books" ) );
112 mGenericManager->action( Akonadi::StandardActionManager::DeleteResources )->setWhatsThis(
113 i18n(
"Delete the selected address books<p>"
114 "The currently selected address books will be deleted, "
115 "along with all the contacts and contact groups they contain.</p>" ) );
117 mGenericManager->action( Akonadi::StandardActionManager::ResourceProperties )->setText(
118 i18n(
"Address Book Properties..." ) );
119 mGenericManager->action( Akonadi::StandardActionManager::ResourceProperties )->setWhatsThis(
120 i18n(
"Open a dialog to edit properties of the selected address book." ) );
122 mGenericManager->setActionText( Akonadi::StandardActionManager::SynchronizeResources,
123 ki18np(
"Update Address Book",
124 "Update %1 Address Books" ) );
126 mGenericManager->action( Akonadi::StandardActionManager::SynchronizeResources )->setWhatsThis
127 ( i18n(
"Updates the content of all folders of the selected address books." ) );
129 mGenericManager->setContextText(
130 StandardActionManager::CreateCollection, StandardActionManager::DialogTitle,
131 i18nc(
"@title:window",
"New Address Book Folder" ) );
133 mGenericManager->setContextText(
134 StandardActionManager::CreateCollection, StandardActionManager::ErrorMessageText,
135 ki18n(
"Could not create address book folder: %1" ) );
137 mGenericManager->setContextText(
138 StandardActionManager::CreateCollection, StandardActionManager::ErrorMessageTitle,
139 i18n(
"Address book folder creation failed" ) );
141 mGenericManager->setContextText(
142 StandardActionManager::DeleteCollections, StandardActionManager::MessageBoxText,
143 ki18np(
"Do you really want to delete this address book folder and all its sub-folders?",
144 "Do you really want to delete %1 address book folders and all their sub-folders?" ) );
145 mGenericManager->setContextText(
146 StandardActionManager::DeleteCollections, StandardActionManager::MessageBoxTitle,
147 ki18ncp(
"@title:window",
"Delete address book folder?",
"Delete address book folders?" ) );
149 mGenericManager->setContextText(
150 StandardActionManager::DeleteCollections, StandardActionManager::ErrorMessageText,
151 ki18n(
"Could not delete address book folder: %1" ) );
153 mGenericManager->setContextText(
154 StandardActionManager::DeleteCollections, StandardActionManager::ErrorMessageTitle,
155 i18n(
"Address book folder deletion failed" ) );
157 mGenericManager->setContextText(
158 StandardActionManager::CollectionProperties, StandardActionManager::DialogTitle,
159 ki18nc(
"@title:window",
"Properties of Address Book Folder %1" ) );
161 mGenericManager->setContextText(
162 StandardActionManager::DeleteItems, StandardActionManager::MessageBoxText,
163 ki18np(
"Do you really want to delete the selected contact?",
164 "Do you really want to delete %1 contacts?" ) );
166 mGenericManager->setContextText(
167 StandardActionManager::DeleteItems, StandardActionManager::MessageBoxTitle,
168 ki18ncp(
"@title:window",
"Delete Contact?",
"Delete Contacts?" ) );
170 mGenericManager->setContextText(
171 StandardActionManager::DeleteItems, StandardActionManager::ErrorMessageText,
172 ki18n(
"Could not delete contact: %1" ) );
174 mGenericManager->setContextText(
175 StandardActionManager::DeleteItems, StandardActionManager::ErrorMessageTitle,
176 i18n(
"Contact deletion failed" ) );
178 mGenericManager->setContextText(
179 StandardActionManager::CreateResource, StandardActionManager::DialogTitle,
180 i18nc(
"@title:window",
"Add Address Book" ) );
182 mGenericManager->setContextText(
183 StandardActionManager::CreateResource, StandardActionManager::ErrorMessageText,
184 ki18n(
"Could not create address book: %1" ) );
186 mGenericManager->setContextText(
187 StandardActionManager::CreateResource, StandardActionManager::ErrorMessageTitle,
188 i18n(
"Address book creation failed" ) );
190 mGenericManager->setContextText(
191 StandardActionManager::DeleteResources, StandardActionManager::MessageBoxText,
192 ki18np(
"Do you really want to delete this address book?",
193 "Do you really want to delete %1 address books?" ) );
195 mGenericManager->setContextText(
196 StandardActionManager::DeleteResources, StandardActionManager::MessageBoxTitle,
197 ki18ncp(
"@title:window",
"Delete Address Book?",
"Delete Address Books?" ) );
199 mGenericManager->setContextText(
200 StandardActionManager::Paste, StandardActionManager::ErrorMessageText,
201 ki18n(
"Could not paste contact: %1" ) );
203 mGenericManager->setContextText(
204 StandardActionManager::Paste, StandardActionManager::ErrorMessageTitle,
205 i18n(
"Paste failed" ) );
207 mGenericManager->setMimeTypeFilter(
208 QStringList() << KABC::Addressee::mimeType() << KABC::ContactGroup::mimeType() );
210 mGenericManager->setCapabilityFilter( QStringList() << QLatin1String(
"Resource" ) );
215 delete mGenericManager;
218 static bool hasWritableCollection(
const QModelIndex &index,
const QString &mimeType )
220 const Akonadi::Collection collection =
221 index.data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
222 if ( collection.isValid() ) {
223 if ( collection.contentMimeTypes().contains( mimeType ) &&
224 ( collection.rights() & Akonadi::Collection::CanCreateItem ) ) {
229 const QAbstractItemModel *model = index.model();
233 for (
int row = 0; row < model->rowCount( index ); ++row ) {
234 if ( hasWritableCollection( model->index( row, 0, index ), mimeType ) )
241 bool hasWritableCollection(
const QString &mimeType )
const
243 if ( !mCollectionSelectionModel )
246 const QAbstractItemModel *collectionModel = mCollectionSelectionModel->model();
247 for (
int row = 0; row < collectionModel->rowCount(); ++row ) {
248 if ( hasWritableCollection( collectionModel->index( row, 0, QModelIndex() ), mimeType ) )
258 if ( mItemSelectionModel ) {
259 itemCount = mItemSelectionModel->selectedRows().count();
260 if ( itemCount == 1 ) {
261 const QModelIndex index = mItemSelectionModel->selectedRows().first();
262 if ( index.isValid() ) {
263 const QString mimeType = index.data( EntityTreeModel::MimeTypeRole ).toString();
264 if ( mimeType == KABC::Addressee::mimeType() ) {
265 mGenericManager->setActionText( Akonadi::StandardActionManager::CopyItems,
266 ki18np(
"Copy Contact",
"Copy %1 Contacts" ) );
267 mGenericManager->action( Akonadi::StandardActionManager::CopyItemToMenu )->setText( i18n(
"Copy Contact To" ) );
268 mGenericManager->action( Akonadi::StandardActionManager::CopyItemToDialog )->setText( i18n(
"Copy Contact To" ) );
269 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteItems,
270 ki18np(
"Delete Contact",
"Delete %1 Contacts" ) );
271 mGenericManager->setActionText( Akonadi::StandardActionManager::CutItems,
272 ki18np(
"Cut Contact",
"Cut %1 Contacts" ) );
273 mGenericManager->action( Akonadi::StandardActionManager::MoveItemToMenu )->setText( i18n(
"Move Contact To" ) );
274 mGenericManager->action( Akonadi::StandardActionManager::MoveItemToDialog )->setText( i18n(
"Move Contact To" ) );
277 }
else if ( mimeType == KABC::ContactGroup::mimeType() ) {
278 mGenericManager->setActionText( Akonadi::StandardActionManager::CopyItems,
279 ki18np(
"Copy Group",
"Copy %1 Groups" ) );
280 mGenericManager->action( Akonadi::StandardActionManager::CopyItemToMenu )->setText( i18n(
"Copy Group To" ) );
281 mGenericManager->action( Akonadi::StandardActionManager::CopyItemToDialog )->setText( i18n(
"Copy Group To" ) );
282 mGenericManager->setActionText( Akonadi::StandardActionManager::DeleteItems,
283 ki18np(
"Delete Group",
"Delete %1 Groups" ) );
284 mGenericManager->setActionText( Akonadi::StandardActionManager::CutItems,
285 ki18np(
"Cut Group",
"Cut %1 Groups" ) );
286 mGenericManager->action( Akonadi::StandardActionManager::MoveItemToMenu )->setText( i18n(
"Move Group To" ) );
287 mGenericManager->action( Akonadi::StandardActionManager::MoveItemToDialog )->setText( i18n(
"Move Group To" ) );
301 bool canEditItem =
true;
304 canEditItem = canEditItem && (itemCount == 1);
307 const QModelIndexList rows = mItemSelectionModel->selectedRows();
308 if ( rows.count() == 1 ) {
309 const QModelIndex index = rows.first();
310 const Collection parentCollection = index.data( EntityTreeModel::ParentCollectionRole ).value<Collection>();
311 if ( parentCollection.isValid() )
312 canEditItem = canEditItem && (parentCollection.rights() & Collection::CanChangeItem);
318 emit mParent->actionStateUpdated();
321 Collection selectedCollection()
const
323 if ( !mCollectionSelectionModel )
326 if ( mCollectionSelectionModel->selectedIndexes().isEmpty() )
329 const QModelIndex index = mCollectionSelectionModel->selectedIndexes().first();
330 if ( !index.isValid() )
333 return index.data( EntityTreeModel::CollectionRole).value<Collection>();
336 void slotCreateContact()
342 dlg.setDefaultAddressBook( selectedCollection() );
347 void slotCreateContactGroup()
353 dlg.setDefaultAddressBook( selectedCollection() );
363 if ( !mItemSelectionModel )
366 if ( mItemSelectionModel->selectedIndexes().isEmpty() )
369 const QModelIndex index = mItemSelectionModel->selectedIndexes().first();
370 if ( !index.isValid() )
373 const Item item = index.data( EntityTreeModel::ItemRole ).value<Item>();
374 if ( !item.isValid() )
377 if ( Akonadi::MimeTypeChecker::isWantedItem( item, KABC::Addressee::mimeType() ) ) {
379 dlg.setContact( item );
382 else if ( Akonadi::MimeTypeChecker::isWantedItem( item, KABC::ContactGroup::mimeType() ) ) {
384 dlg.setContactGroup( item );
389 KActionCollection *mActionCollection;
390 QWidget *mParentWidget;
391 StandardActionManager *mGenericManager;
392 QItemSelectionModel *mCollectionSelectionModel;
393 QItemSelectionModel *mItemSelectionModel;
394 QHash<StandardContactActionManager::Type, KAction*> mActions;
395 QSet<StandardContactActionManager::Type> mInterceptedActions;
400 : QObject( parent ), d( new Private( actionCollection, parent, this ) )
411 d->mCollectionSelectionModel = selectionModel;
412 d->mGenericManager->setCollectionSelectionModel( selectionModel );
414 connect( selectionModel->model(), SIGNAL(rowsInserted(QModelIndex,
int,
int)),
415 SLOT(updateActions()) );
416 connect( selectionModel->model(), SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
417 SLOT(updateActions()) );
418 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
419 SLOT(updateActions()) );
426 d->mItemSelectionModel = selectionModel;
427 d->mGenericManager->setItemSelectionModel( selectionModel );
429 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
430 SLOT(updateActions()) );
437 if ( d->mActions.contains( type ) )
438 return d->mActions.value( type );
444 action =
new KAction( d->mParentWidget );
445 action->setIcon( KIcon( QLatin1String(
"contact-new" ) ) );
446 action->setText( i18n(
"New &Contact..." ) );
447 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_N ) );
448 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>" ) );
450 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_create" ), action );
451 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContact()) );
454 action =
new KAction( d->mParentWidget );
455 action->setIcon( KIcon( QLatin1String(
"user-group-new" ) ) );
456 action->setText( i18n(
"New &Group..." ) );
457 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_G ) );
458 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>" ) );
460 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_group_create" ), action );
461 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContactGroup()) );
464 action =
new KAction( d->mParentWidget );
465 action->setIcon( KIcon( QLatin1String(
"document-edit" ) ) );
466 action->setText( i18n(
"Edit Contact..." ) );
467 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>" ) );
468 action->setEnabled(
false );
469 d->mActions.insert(
EditItem, action );
470 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_item_edit" ), action );
471 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotEditItem()) );
483 return d->mGenericManager->createAction( type );
492 d->mGenericManager->createAllActions();
499 if ( d->mActions.contains( type ) )
500 return d->mActions.value( type );
507 return d->mGenericManager->action( type );
512 d->mGenericManager->setActionText( type, text );
518 d->mInterceptedActions.insert( type );
520 d->mInterceptedActions.remove( type );
525 d->mGenericManager->interceptAction( type, intercept );
530 return d->mGenericManager->selectedCollections();
535 return d->mGenericManager->selectedItems();
541 d->mGenericManager->setCollectionPropertiesPageNames( names );
543 #include "standardcontactactionmanager.moc"