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 <QItemSelectionModel>
39 using namespace Akonadi;
41 class StandardContactActionManager::Private
45 : mActionCollection( actionCollection ), mParentWidget( parentWidget ),
46 mCollectionSelectionModel( 0 ), mItemSelectionModel( 0 ), mParent( parent )
52 mGenericManager->setMimeTypeFilter(
53 QStringList() << KABC::Addressee::mimeType() << KABC::ContactGroup::mimeType() );
55 mGenericManager->setCapabilityFilter( QStringList() << QLatin1String(
"Resource" ) );
60 delete mGenericManager;
63 void updateGenericAllActions()
110 i18n(
"Add Address Book Folder..." ) );
113 i18n(
"Add a new address book folder to the currently selected address book folder." ) );
114 mGenericManager->setContextText(
116 i18nc(
"@title:window",
"New Address Book Folder" ) );
118 mGenericManager->setContextText(
120 ki18n(
"Could not create address book folder: %1" ) );
122 mGenericManager->setContextText(
124 i18n(
"Address book folder creation failed" ) );
129 ki18np(
"Copy Address Book Folder",
130 "Copy %1 Address Book Folders" ) );
132 i18n(
"Copy the selected address book folders to the clipboard." ) );
136 ki18np(
"Delete Address Book Folder",
137 "Delete %1 Address Book Folders" ) );
139 i18n(
"Delete the selected address book folders from the address book." ) );
141 mGenericManager->setContextText(
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(
147 ki18ncp(
"@title:window",
"Delete address book folder?",
"Delete address book folders?" ) );
149 mGenericManager->setContextText(
151 ki18n(
"Could not delete address book folder: %1" ) );
154 mGenericManager->setContextText(
156 i18n(
"Address book folder deletion failed" ) );
160 ki18np(
"Update Address Book Folder",
161 "Update %1 Address Book Folders" ) );
163 i18n(
"Update the content of the selected address book folders." ) );
167 ki18np(
"Cut Address Book Folder",
168 "Cut %1 Address Book Folders" ) );
170 i18n(
"Cut the selected address book folders from the address book." ) );
174 i18n(
"Folder Properties..." ) );
176 i18n(
"Open a dialog to edit the properties of the selected address book folder." ) );
177 mGenericManager->setContextText(
179 ki18nc(
"@title:window",
"Properties of Address Book Folder %1" ) );
183 ki18np(
"Copy Contact",
"Copy %1 Contacts" ) );
185 i18n(
"Copy the selected contacts to the clipboard." ) );
189 ki18np(
"Delete Contact",
"Delete %1 Contacts" ) );
191 i18n(
"Delete the selected contacts from the address book." ) );
192 mGenericManager->setContextText(
194 ki18np(
"Do you really want to delete the selected contact?",
195 "Do you really want to delete %1 contacts?" ) );
197 mGenericManager->setContextText(
199 ki18ncp(
"@title:window",
"Delete Contact?",
"Delete Contacts?" ) );
201 mGenericManager->setContextText(
203 ki18n(
"Could not delete contact: %1" ) );
205 mGenericManager->setContextText(
207 i18n(
"Contact deletion failed" ) );
211 ki18np(
"Cut Contact",
"Cut %1 Contacts" ) );
213 i18n(
"Cut the selected contacts from the address book." ) );
217 i18n(
"Add &Address Book..." ) );
219 i18n(
"Add a new address book<p>"
220 "You will be presented with a dialog where you can select "
221 "the type of the address book that shall be added.</p>" ) );
222 mGenericManager->setContextText(
224 i18nc(
"@title:window",
"Add Address Book" ) );
226 mGenericManager->setContextText(
228 ki18n(
"Could not create address book: %1" ) );
230 mGenericManager->setContextText(
232 i18n(
"Address book creation failed" ) );
237 ki18np(
"&Delete Address Book",
238 "&Delete %1 Address Books" ) );
240 i18n(
"Delete the selected address books<p>"
241 "The currently selected address books will be deleted, "
242 "along with all the contacts and contact groups they contain.</p>" ) );
243 mGenericManager->setContextText(
245 ki18np(
"Do you really want to delete this address book?",
246 "Do you really want to delete %1 address books?" ) );
248 mGenericManager->setContextText(
250 ki18ncp(
"@title:window",
"Delete Address Book?",
"Delete Address Books?" ) );
256 i18n(
"Address Book Properties..." ) );
258 i18n(
"Open a dialog to edit properties of the selected address book." ) );
262 ki18np(
"Update Address Book",
263 "Update %1 Address Books" ) );
266 ( i18n(
"Updates the content of all folders of the selected address books." ) );
270 mGenericManager->setContextText(
272 ki18n(
"Could not paste contact: %1" ) );
274 mGenericManager->setContextText(
276 i18n(
"Paste failed" ) );
284 static bool hasWritableCollection(
const QModelIndex &index,
const QString &mimeType )
295 const QAbstractItemModel *model = index.model();
300 for (
int row = 0; row < model->rowCount( index ); ++row ) {
301 if ( hasWritableCollection( model->index( row, 0, index ), mimeType ) ) {
309 bool hasWritableCollection(
const QString &mimeType )
const
311 if ( !mCollectionSelectionModel ) {
315 const QAbstractItemModel *collectionModel = mCollectionSelectionModel->model();
316 for (
int row = 0; row < collectionModel->rowCount(); ++row ) {
317 if ( hasWritableCollection( collectionModel->index( row, 0, QModelIndex() ), mimeType ) ) {
328 if ( mItemSelectionModel ) {
329 itemCount = mItemSelectionModel->selectedRows().count();
330 if ( itemCount == 1 ) {
331 const QModelIndex index = mItemSelectionModel->selectedRows().first();
332 if ( index.isValid() ) {
334 if ( mimeType == KABC::Addressee::mimeType() ) {
337 ki18np(
"Copy Contact",
"Copy %1 Contacts" ) );
345 ki18np(
"Delete Contact",
"Delete %1 Contacts" ) );
349 ki18np(
"Cut Contact",
"Cut %1 Contacts" ) );
357 }
else if ( mimeType == KABC::ContactGroup::mimeType() ) {
360 ki18np(
"Copy Group",
"Copy %1 Groups" ) );
370 ki18np(
"Delete Group",
"Delete %1 Groups" ) );
374 ki18np(
"Cut Group",
"Cut %1 Groups" ) );
398 bool canEditItem =
true;
401 canEditItem = canEditItem && ( itemCount == 1 );
404 const QModelIndexList rows = mItemSelectionModel->selectedRows();
405 if ( rows.count() == 1 ) {
406 const QModelIndex index = rows.first();
408 if ( parentCollection.
isValid() ) {
416 emit mParent->actionStateUpdated();
421 if ( !mCollectionSelectionModel ) {
425 if ( mCollectionSelectionModel->selectedIndexes().isEmpty() ) {
429 const QModelIndex index = mCollectionSelectionModel->selectedIndexes().first();
430 if ( !index.isValid() ) {
437 void slotCreateContact()
444 dlg.setDefaultAddressBook( selectedCollection() );
449 void slotCreateContactGroup()
456 dlg.setDefaultAddressBook( selectedCollection() );
467 if ( !mItemSelectionModel ) {
471 if ( mItemSelectionModel->selectedIndexes().isEmpty() ) {
475 const QModelIndex index = mItemSelectionModel->selectedIndexes().first();
476 if ( !index.isValid() ) {
487 dlg.setContact( item );
491 dlg.setContactGroup( item );
496 KActionCollection *mActionCollection;
497 QWidget *mParentWidget;
499 QItemSelectionModel *mCollectionSelectionModel;
500 QItemSelectionModel *mItemSelectionModel;
501 QHash<StandardContactActionManager::Type, KAction*> mActions;
502 QSet<StandardContactActionManager::Type> mInterceptedActions;
507 : QObject( parent ), d( new Private( actionCollection, parent, this ) )
518 d->mCollectionSelectionModel = selectionModel;
519 d->mGenericManager->setCollectionSelectionModel( selectionModel );
521 connect( selectionModel->model(), SIGNAL(rowsInserted(QModelIndex,
int,
int)),
522 SLOT(updateActions()) );
523 connect( selectionModel->model(), SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
524 SLOT(updateActions()) );
525 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
526 SLOT(updateActions()) );
533 d->mItemSelectionModel = selectionModel;
534 d->mGenericManager->setItemSelectionModel( selectionModel );
536 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
537 SLOT(updateActions()) );
544 if ( d->mActions.contains( type ) ) {
545 return d->mActions.value( type );
552 action =
new KAction( d->mParentWidget );
553 action->setIcon( KIcon( QLatin1String(
"contact-new" ) ) );
554 action->setText( i18n(
"New &Contact..." ) );
555 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_N ) );
556 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>" ) );
558 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_create" ), action );
559 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContact()) );
562 action =
new KAction( d->mParentWidget );
563 action->setIcon( KIcon( QLatin1String(
"user-group-new" ) ) );
564 action->setText( i18n(
"New &Group..." ) );
565 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_G ) );
566 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>" ) );
568 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_group_create" ), action );
569 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContactGroup()) );
572 action =
new KAction( d->mParentWidget );
573 action->setIcon( KIcon( QLatin1String(
"document-edit" ) ) );
574 action->setText( i18n(
"Edit Contact..." ) );
575 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>" ) );
576 action->setEnabled(
false );
577 d->mActions.insert(
EditItem, action );
578 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_item_edit" ), action );
579 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotEditItem()) );
591 KAction *act = d->mGenericManager->action(type);
593 act = d->mGenericManager->createAction( type );
594 d->updateGenericAction(type);
604 d->mGenericManager->createAllActions();
605 d->updateGenericAllActions();
612 if ( d->mActions.contains( type ) ) {
613 return d->mActions.value( type );
621 return d->mGenericManager->action( type );
626 d->mGenericManager->setActionText( type, text );
632 d->mInterceptedActions.insert( type );
634 d->mInterceptedActions.remove( type );
640 d->mGenericManager->interceptAction( type, intercept );
645 return d->mGenericManager->selectedCollections();
650 return d->mGenericManager->selectedItems();
656 d->mGenericManager->setCollectionPropertiesPageNames( names );
658 #include "moc_standardcontactactionmanager.cpp"