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 )
53 mGenericManager->setMimeTypeFilter(
54 QStringList() << KABC::Addressee::mimeType() << KABC::ContactGroup::mimeType() );
56 mGenericManager->setCapabilityFilter( QStringList() << QLatin1String(
"Resource" ) );
61 delete mGenericManager;
64 void updateGenericAllActions()
111 i18n(
"Add Address Book Folder..." ) );
114 i18n(
"Add a new address book folder to the currently selected address book folder." ) );
115 mGenericManager->setContextText(
117 i18nc(
"@title:window",
"New Address Book Folder" ) );
119 mGenericManager->setContextText(
121 ki18n(
"Could not create address book folder: %1" ) );
123 mGenericManager->setContextText(
125 i18n(
"Address book folder creation failed" ) );
130 ki18np(
"Copy Address Book Folder",
131 "Copy %1 Address Book Folders" ) );
133 i18n(
"Copy the selected address book folders to the clipboard." ) );
137 ki18np(
"Delete Address Book Folder",
138 "Delete %1 Address Book Folders" ) );
140 i18n(
"Delete the selected address book folders from the address book." ) );
142 mGenericManager->setContextText(
144 ki18np(
"Do you really want to delete this address book folder and all its sub-folders?",
145 "Do you really want to delete %1 address book folders and all their sub-folders?" ) );
146 mGenericManager->setContextText(
148 ki18ncp(
"@title:window",
"Delete address book folder?",
"Delete address book folders?" ) );
150 mGenericManager->setContextText(
152 ki18n(
"Could not delete address book folder: %1" ) );
155 mGenericManager->setContextText(
157 i18n(
"Address book folder deletion failed" ) );
161 ki18np(
"Update Address Book Folder",
162 "Update %1 Address Book Folders" ) );
164 i18n(
"Update the content of the selected address book folders." ) );
168 ki18np(
"Cut Address Book Folder",
169 "Cut %1 Address Book Folders" ) );
171 i18n(
"Cut the selected address book folders from the address book." ) );
175 i18n(
"Folder Properties..." ) );
177 i18n(
"Open a dialog to edit the properties of the selected address book folder." ) );
178 mGenericManager->setContextText(
180 ki18nc(
"@title:window",
"Properties of Address Book Folder %1" ) );
184 ki18np(
"Copy Contact",
"Copy %1 Contacts" ) );
186 i18n(
"Copy the selected contacts to the clipboard." ) );
190 ki18np(
"Delete Contact",
"Delete %1 Contacts" ) );
192 i18n(
"Delete the selected contacts from the address book." ) );
193 mGenericManager->setContextText(
195 ki18np(
"Do you really want to delete the selected contact?",
196 "Do you really want to delete %1 contacts?" ) );
198 mGenericManager->setContextText(
200 ki18ncp(
"@title:window",
"Delete Contact?",
"Delete Contacts?" ) );
202 mGenericManager->setContextText(
204 ki18n(
"Could not delete contact: %1" ) );
206 mGenericManager->setContextText(
208 i18n(
"Contact deletion failed" ) );
212 ki18np(
"Cut Contact",
"Cut %1 Contacts" ) );
214 i18n(
"Cut the selected contacts from the address book." ) );
218 i18n(
"Add &Address Book..." ) );
220 i18n(
"Add a new address book<p>"
221 "You will be presented with a dialog where you can select "
222 "the type of the address book that shall be added.</p>" ) );
223 mGenericManager->setContextText(
225 i18nc(
"@title:window",
"Add Address Book" ) );
227 mGenericManager->setContextText(
229 ki18n(
"Could not create address book: %1" ) );
231 mGenericManager->setContextText(
233 i18n(
"Address book creation failed" ) );
238 ki18np(
"&Delete Address Book",
239 "&Delete %1 Address Books" ) );
241 i18n(
"Delete the selected address books<p>"
242 "The currently selected address books will be deleted, "
243 "along with all the contacts and contact groups they contain.</p>" ) );
244 mGenericManager->setContextText(
246 ki18np(
"Do you really want to delete this address book?",
247 "Do you really want to delete %1 address books?" ) );
249 mGenericManager->setContextText(
251 ki18ncp(
"@title:window",
"Delete Address Book?",
"Delete Address Books?" ) );
257 i18n(
"Address Book Properties..." ) );
259 i18n(
"Open a dialog to edit properties of the selected address book." ) );
263 ki18np(
"Update Address Book",
264 "Update %1 Address Books" ) );
267 ( i18n(
"Updates the content of all folders of the selected address books." ) );
271 mGenericManager->setContextText(
273 ki18n(
"Could not paste contact: %1" ) );
275 mGenericManager->setContextText(
277 i18n(
"Paste failed" ) );
285 static bool hasWritableCollection(
const QModelIndex &index,
const QString &mimeType )
296 const QAbstractItemModel *model = index.model();
301 for (
int row = 0; row < model->rowCount( index ); ++row ) {
302 if ( hasWritableCollection( model->index( row, 0, index ), mimeType ) ) {
310 bool hasWritableCollection(
const QString &mimeType )
const
312 if ( !mCollectionSelectionModel ) {
316 const QAbstractItemModel *collectionModel = mCollectionSelectionModel->model();
317 for (
int row = 0; row < collectionModel->rowCount(); ++row ) {
318 if ( hasWritableCollection( collectionModel->index( row, 0, QModelIndex() ), mimeType ) ) {
329 if ( mItemSelectionModel ) {
330 itemCount = mItemSelectionModel->selectedRows().count();
331 if ( itemCount == 1 ) {
332 const QModelIndex index = mItemSelectionModel->selectedRows().first();
333 if ( index.isValid() ) {
335 if ( mimeType == KABC::Addressee::mimeType() ) {
338 ki18np(
"Copy Contact",
"Copy %1 Contacts" ) );
346 ki18np(
"Delete Contact",
"Delete %1 Contacts" ) );
350 ki18np(
"Cut Contact",
"Cut %1 Contacts" ) );
358 }
else if ( mimeType == KABC::ContactGroup::mimeType() ) {
361 ki18np(
"Copy Group",
"Copy %1 Groups" ) );
371 ki18np(
"Delete Group",
"Delete %1 Groups" ) );
375 ki18np(
"Cut Group",
"Cut %1 Groups" ) );
399 bool canEditItem =
true;
402 canEditItem = canEditItem && ( itemCount == 1 );
405 const QModelIndexList rows = mItemSelectionModel->selectedRows();
406 if ( rows.count() == 1 ) {
407 const QModelIndex index = rows.first();
409 if ( parentCollection.
isValid() ) {
417 emit mParent->actionStateUpdated();
422 if ( !mCollectionSelectionModel ) {
426 if ( mCollectionSelectionModel->selectedIndexes().isEmpty() ) {
430 const QModelIndex index = mCollectionSelectionModel->selectedIndexes().first();
431 if ( !index.isValid() ) {
438 void slotCreateContact()
444 QPointer<Akonadi::ContactEditorDialog> dlg =
447 dlg->setDefaultAddressBook( selectedCollection() );
452 void slotCreateContactGroup()
458 QPointer<Akonadi::ContactGroupEditorDialog> dlg =
461 dlg->setDefaultAddressBook( selectedCollection() );
472 if ( !mItemSelectionModel ) {
476 if ( mItemSelectionModel->selectedIndexes().isEmpty() ) {
480 const QModelIndex index = mItemSelectionModel->selectedIndexes().first();
481 if ( !index.isValid() ) {
491 QPointer<Akonadi::ContactEditorDialog> dlg =
494 connect( dlg, SIGNAL(error(QString)),
495 mParent, SLOT(slotContactEditorError(QString)) );
496 dlg->setContact( item );
500 QPointer<Akonadi::ContactGroupEditorDialog> dlg =
503 dlg->setContactGroup( item );
509 void slotContactEditorError(
const QString& error)
511 KMessageBox::error(mParentWidget, i18n(
"Contact cannot be stored: %1", error), i18n(
"Failed to store contact"));
514 KActionCollection *mActionCollection;
515 QWidget *mParentWidget;
517 QItemSelectionModel *mCollectionSelectionModel;
518 QItemSelectionModel *mItemSelectionModel;
519 QHash<StandardContactActionManager::Type, KAction*> mActions;
520 QSet<StandardContactActionManager::Type> mInterceptedActions;
525 : QObject( parent ), d( new Private( actionCollection, parent, this ) )
536 d->mCollectionSelectionModel = selectionModel;
537 d->mGenericManager->setCollectionSelectionModel( selectionModel );
539 connect( selectionModel->model(), SIGNAL(rowsInserted(QModelIndex,
int,
int)),
540 SLOT(updateActions()) );
541 connect( selectionModel->model(), SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
542 SLOT(updateActions()) );
543 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
544 SLOT(updateActions()) );
551 d->mItemSelectionModel = selectionModel;
552 d->mGenericManager->setItemSelectionModel( selectionModel );
554 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
555 SLOT(updateActions()) );
562 if ( d->mActions.contains( type ) ) {
563 return d->mActions.value( type );
570 action =
new KAction( d->mParentWidget );
571 action->setIcon( KIcon( QLatin1String(
"contact-new" ) ) );
572 action->setText( i18n(
"New &Contact..." ) );
573 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_N ) );
574 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>" ) );
576 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_create" ), action );
577 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContact()) );
580 action =
new KAction( d->mParentWidget );
581 action->setIcon( KIcon( QLatin1String(
"user-group-new" ) ) );
582 action->setText( i18n(
"New &Group..." ) );
583 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_G ) );
584 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>" ) );
586 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_group_create" ), action );
587 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContactGroup()) );
590 action =
new KAction( d->mParentWidget );
591 action->setIcon( KIcon( QLatin1String(
"document-edit" ) ) );
592 action->setText( i18n(
"Edit Contact..." ) );
593 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>" ) );
594 action->setEnabled(
false );
595 d->mActions.insert(
EditItem, action );
596 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_item_edit" ), action );
597 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotEditItem()) );
609 KAction *act = d->mGenericManager->action(type);
611 act = d->mGenericManager->createAction( type );
612 d->updateGenericAction(type);
622 d->mGenericManager->createAllActions();
623 d->updateGenericAllActions();
630 if ( d->mActions.contains( type ) ) {
631 return d->mActions.value( type );
639 return d->mGenericManager->action( type );
644 d->mGenericManager->setActionText( type, text );
650 d->mInterceptedActions.insert( type );
652 d->mInterceptedActions.remove( type );
658 d->mGenericManager->interceptAction( type, intercept );
663 return d->mGenericManager->selectedCollections();
668 return d->mGenericManager->selectedItems();
674 d->mGenericManager->setCollectionPropertiesPageNames( names );
676 #include "moc_standardcontactactionmanager.cpp"