• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kmail

  • sources
  • kde-4.12
  • kdepim
  • kmail
  • job
createnewcontactjob.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012-2013 Montel Laurent <montel@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 
19 */
20 
21 #include "createnewcontactjob.h"
22 #include "util.h"
23 
24 #include <libkdepim/misc/broadcaststatus.h>
25 
26 #include <KABC/Addressee>
27 #include <KABC/ContactGroup>
28 
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>
35 
36 #include <KLocale>
37 #include <KMessageBox>
38 
39 CreateNewContactJob::CreateNewContactJob(QWidget *parentWidget, QObject *parent)
40  : KJob(parent),
41  mParentWidget(parentWidget)
42 {
43 }
44 
45 CreateNewContactJob::~CreateNewContactJob()
46 {
47 }
48 
49 void CreateNewContactJob::start()
50 {
51  Akonadi::CollectionFetchJob * const addressBookJob =
52  new Akonadi::CollectionFetchJob( Akonadi::Collection::root(),
53  Akonadi::CollectionFetchJob::Recursive );
54 
55  addressBookJob->fetchScope().setContentMimeTypes( QStringList() << KABC::Addressee::mimeType() );
56  connect( addressBookJob, SIGNAL(result(KJob*)), SLOT(slotCollectionsFetched(KJob*)) );
57 }
58 
59 
60 void CreateNewContactJob::slotCollectionsFetched(KJob*job)
61 {
62  if ( job->error() ) {
63  setError( job->error() );
64  setErrorText( job->errorText() );
65  emitResult();
66  return;
67  }
68 
69  const Akonadi::CollectionFetchJob *addressBookJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
70 
71  Akonadi::Collection::List canCreateItemCollections ;
72 
73  foreach ( const Akonadi::Collection &collection, addressBookJob->collections() ) {
74  if ( Akonadi::Collection::CanCreateItem & collection.rights() ) {
75  canCreateItemCollections.append(collection);
76  }
77  }
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" ) );
84 
85  if ( dlg.exec() ) {
86  const Akonadi::AgentType agentType = dlg.agentType();
87 
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 );
92  job->start();
93  return;
94  } else { //if agent is not valid => return error and finish job
95  setError( UserDefinedError );
96  emitResult();
97  return;
98  }
99  } else { //dialog canceled => return error and finish job
100  setError( UserDefinedError );
101  emitResult();
102  return;
103  }
104  }
105  createContact();
106  emitResult();
107 }
108 
109 void CreateNewContactJob::slotResourceCreationDone(KJob* job)
110 {
111  if ( job->error() ) {
112  setError( job->error() );
113  setErrorText( job->errorText() );
114  emitResult();
115  return;
116  }
117  createContact();
118  emitResult();
119 }
120 
121 void CreateNewContactJob::createContact()
122 {
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)) );
126  dlg.exec();
127 }
128 
129 void CreateNewContactJob::contactStored( const Akonadi::Item &item )
130 {
131  Q_UNUSED( item );
132  KPIM::BroadcastStatus::instance()->setStatusMsg( i18n( "Contact created successfully" ) );
133 }
134 
135 void CreateNewContactJob::slotContactEditorError(const QString &error)
136 {
137  KMessageBox::error(mParentWidget, i18n("Contact cannot be stored: %1", error), i18n("Failed to store contact"));
138 }
139 
140 
141 #include "createnewcontactjob.moc"
KPIM::BroadcastStatus::instance
static BroadcastStatus * instance()
QWidget
CreateNewContactJob::~CreateNewContactJob
~CreateNewContactJob()
Destroys the new contact job.
Definition: createnewcontactjob.cpp:45
QObject
KPIM::BroadcastStatus::setStatusMsg
void setStatusMsg(const QString &message)
createnewcontactjob.h
util.h
CreateNewContactJob::CreateNewContactJob
CreateNewContactJob(QWidget *parentWidget, QObject *parent=0)
CreateNewContactJob create a new contact job.
Definition: createnewcontactjob.cpp:39
CreateNewContactJob::start
void start()
start the job
Definition: createnewcontactjob.cpp:49
broadcaststatus.h
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal