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" ) );
360 }
else if ( mimeType == KABC::ContactGroup::mimeType() ) {
363 ki18np(
"Copy Group",
"Copy %1 Groups" ) );
373 ki18np(
"Delete Group",
"Delete %1 Groups" ) );
377 ki18np(
"Cut Group",
"Cut %1 Groups" ) );
401 bool canEditItem =
true;
404 canEditItem = canEditItem && ( itemCount == 1 );
407 const QModelIndexList rows = mItemSelectionModel->selectedRows();
408 if ( rows.count() == 1 ) {
409 const QModelIndex index = rows.first();
411 if ( parentCollection.
isValid() ) {
419 emit mParent->actionStateUpdated();
424 if ( !mCollectionSelectionModel ) {
428 if ( mCollectionSelectionModel->selectedIndexes().isEmpty() ) {
432 const QModelIndex index = mCollectionSelectionModel->selectedIndexes().first();
433 if ( !index.isValid() ) {
440 void slotCreateContact()
447 dlg.setDefaultAddressBook( selectedCollection() );
452 void slotCreateContactGroup()
459 dlg.setDefaultAddressBook( selectedCollection() );
470 if ( !mItemSelectionModel ) {
474 if ( mItemSelectionModel->selectedIndexes().isEmpty() ) {
478 const QModelIndex index = mItemSelectionModel->selectedIndexes().first();
479 if ( !index.isValid() ) {
490 dlg.setContact( item );
494 dlg.setContactGroup( item );
499 KActionCollection *mActionCollection;
500 QWidget *mParentWidget;
502 QItemSelectionModel *mCollectionSelectionModel;
503 QItemSelectionModel *mItemSelectionModel;
504 QHash<StandardContactActionManager::Type, KAction*> mActions;
505 QSet<StandardContactActionManager::Type> mInterceptedActions;
510 : QObject( parent ), d( new Private( actionCollection, parent, this ) )
521 d->mCollectionSelectionModel = selectionModel;
522 d->mGenericManager->setCollectionSelectionModel( selectionModel );
524 connect( selectionModel->model(), SIGNAL(rowsInserted(QModelIndex,
int,
int)),
525 SLOT(updateActions()) );
526 connect( selectionModel->model(), SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
527 SLOT(updateActions()) );
528 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
529 SLOT(updateActions()) );
536 d->mItemSelectionModel = selectionModel;
537 d->mGenericManager->setItemSelectionModel( selectionModel );
539 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
540 SLOT(updateActions()) );
547 if ( d->mActions.contains( type ) ) {
548 return d->mActions.value( type );
555 action =
new KAction( d->mParentWidget );
556 action->setIcon( KIcon( QLatin1String(
"contact-new" ) ) );
557 action->setText( i18n(
"New &Contact..." ) );
558 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_N ) );
559 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>" ) );
561 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_create" ), action );
562 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContact()) );
565 action =
new KAction( d->mParentWidget );
566 action->setIcon( KIcon( QLatin1String(
"user-group-new" ) ) );
567 action->setText( i18n(
"New &Group..." ) );
568 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_G ) );
569 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>" ) );
571 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_group_create" ), action );
572 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotCreateContactGroup()) );
575 action =
new KAction( d->mParentWidget );
576 action->setIcon( KIcon( QLatin1String(
"document-edit" ) ) );
577 action->setText( i18n(
"Edit Contact..." ) );
578 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>" ) );
579 action->setEnabled(
false );
580 d->mActions.insert(
EditItem, action );
581 d->mActionCollection->addAction( QString::fromLatin1(
"akonadi_contact_item_edit" ), action );
582 connect( action, SIGNAL(triggered(
bool)),
this, SLOT(slotEditItem()) );
594 KAction *act = d->mGenericManager->action(type);
596 act = d->mGenericManager->createAction( type );
597 d->updateGenericAction(type);
607 d->mGenericManager->createAllActions();
608 d->updateGenericAllActions();
615 if ( d->mActions.contains( type ) ) {
616 return d->mActions.value( type );
624 return d->mGenericManager->action( type );
629 d->mGenericManager->setActionText( type, text );
635 d->mInterceptedActions.insert( type );
637 d->mInterceptedActions.remove( type );
643 d->mGenericManager->interceptAction( type, intercept );
648 return d->mGenericManager->selectedCollections();
653 return d->mGenericManager->selectedItems();
659 d->mGenericManager->setCollectionPropertiesPageNames( names );
661 #include "standardcontactactionmanager.moc"