26 #include <KABC/Addressee>
27 #include <KABC/ContactGroup>
29 #include <Akonadi/CollectionFetchJob>
30 #include <Akonadi/AgentInstanceCreateJob>
31 #include <Akonadi/AgentTypeDialog>
32 #include <Akonadi/CollectionFetchScope>
33 #include <Akonadi/AgentFilterProxyModel>
34 #include <Akonadi/Contact/ContactEditorDialog>
36 #include <KLocalizedString>
37 #include <KMessageBox>
41 mParentWidget(parentWidget)
51 Akonadi::CollectionFetchJob *
const addressBookJob =
52 new Akonadi::CollectionFetchJob( Akonadi::Collection::root(),
53 Akonadi::CollectionFetchJob::Recursive );
55 addressBookJob->fetchScope().setContentMimeTypes(
QStringList() << KABC::Addressee::mimeType() );
56 connect( addressBookJob, SIGNAL(result(
KJob*)), SLOT(slotCollectionsFetched(
KJob*)) );
60 void CreateNewContactJob::slotCollectionsFetched(
KJob*job)
63 setError( job->error() );
64 setErrorText( job->errorText() );
69 const Akonadi::CollectionFetchJob *addressBookJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
71 Akonadi::Collection::List canCreateItemCollections ;
73 foreach (
const Akonadi::Collection &collection, addressBookJob->collections() ) {
74 if ( Akonadi::Collection::CanCreateItem & collection.rights() ) {
75 canCreateItemCollections.append(collection);
78 if ( canCreateItemCollections.isEmpty() ) {
79 Akonadi::AgentTypeDialog dlg( mParentWidget );
80 dlg.setCaption( i18n(
"Add to Address Book") );
81 dlg.agentFilterProxyModel()->addMimeTypeFilter(KABC::Addressee::mimeType());
82 dlg.agentFilterProxyModel()->addMimeTypeFilter(KABC::ContactGroup::mimeType());
83 dlg.agentFilterProxyModel()->addCapabilityFilter(
QLatin1String(
"Resource" ) );
86 const Akonadi::AgentType agentType = dlg.agentType();
88 if ( agentType.isValid() ) {
89 Akonadi::AgentInstanceCreateJob *job =
new Akonadi::AgentInstanceCreateJob( agentType,
this );
90 connect( job, SIGNAL(result(
KJob*)), SLOT(slotResourceCreationDone(
KJob*)) );
91 job->configure( mParentWidget );
95 setError( UserDefinedError );
100 setError( UserDefinedError );
109 void CreateNewContactJob::slotResourceCreationDone(
KJob* job)
111 if ( job->error() ) {
112 setError( job->error() );
113 setErrorText( job->errorText() );
121 void CreateNewContactJob::createContact()
123 Akonadi::ContactEditorDialog dlg( Akonadi::ContactEditorDialog::CreateMode, mParentWidget );
124 connect(&dlg, SIGNAL(contactStored(Akonadi::Item)),
this, SLOT(contactStored(Akonadi::Item)) );
125 connect(&dlg, SIGNAL(error(
QString)),
this, SLOT(slotContactEditorError(
QString)) );
129 void CreateNewContactJob::contactStored(
const Akonadi::Item &item )
135 void CreateNewContactJob::slotContactEditorError(
const QString &error)
137 KMessageBox::error(mParentWidget, i18n(
"Contact cannot be stored: %1", error), i18n(
"Failed to store contact"));
static BroadcastStatus * instance()
void setStatusMsg(const QString &message)