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

libkdepim

  • sources
  • kde-4.12
  • kdepim
  • libkdepim
  • job
addcontactjob.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2010 Tobias Koenig <tokoe@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public 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
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "addcontactjob.h"
21 #include <Akonadi/Collection>
22 #include "widgets/selectedcollectiondialog.h"
23 
24 #include <akonadi/contact/contactsearchjob.h>
25 #include <akonadi/item.h>
26 #include <akonadi/itemcreatejob.h>
27 #include <kabc/addressee.h>
28 #include <klocale.h>
29 #include <kmessagebox.h>
30 
31 #include <QtCore/QPointer>
32 
33 using namespace KPIM;
34 
35 class AddContactJob::Private
36 {
37  public:
38  Private( AddContactJob *qq, const KABC::Addressee &contact, QWidget *parentWidget )
39  : q( qq ), mContact( contact ), mParentWidget( parentWidget ), mShowMessageBox(true)
40  {
41  }
42 
43  Private( AddContactJob *qq, const KABC::Addressee &contact, const Akonadi::Collection &collection )
44  : q( qq ), mContact( contact ), mParentWidget( 0 ), mCollection( collection ), mShowMessageBox(true)
45  {
46  }
47 
48  void slotSearchDone( KJob *job )
49  {
50  if ( job->error() ) {
51  q->setError( job->error() );
52  q->setErrorText( job->errorText() );
53  q->emitResult();
54  return;
55  }
56 
57  const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
58 
59  const KABC::Addressee::List contacts = searchJob->contacts();
60 
61  if ( !contacts.isEmpty() ) { // contact is already part of the address book...
62  if(mShowMessageBox) {
63  const QString text =
64  i18nc( "@info",
65  "The vCard's primary email address is already in "
66  "your address book; however, you may save the vCard into "
67  "a file and import it into the address book manually." );
68  KMessageBox::information( mParentWidget, text );
69  }
70  q->setError( UserDefinedError );
71  q->emitResult();
72  return;
73  }
74 
75  if ( !mCollection.isValid() ) {
76  // ask user in which address book the new contact shall be stored
77  QPointer<SelectedCollectionDialog> dlg = new SelectedCollectionDialog( mParentWidget );
78 
79  bool gotIt = true;
80  if ( !dlg->exec() ) {
81  q->setError( UserDefinedError );
82  q->emitResult();
83  gotIt = false;
84  } else {
85  mCollection = dlg->selectedCollection();
86  }
87  delete dlg;
88  if ( !gotIt ) {
89  return;
90  }
91  }
92 
93  if ( mCollection.isValid() ) {
94  // create the new item
95  Akonadi::Item item;
96  item.setMimeType( KABC::Addressee::mimeType() );
97  item.setPayload<KABC::Addressee>( mContact );
98 
99  // save the new item in akonadi storage
100  Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, mCollection );
101  q->connect( job, SIGNAL(result(KJob*)), SLOT(slotAddContactDone(KJob*)) );
102  } else {
103  q->setError( UserDefinedError );
104  q->emitResult();
105  }
106  }
107 
108  void slotAddContactDone( KJob *job )
109  {
110  if ( job->error() ) {
111  q->setError( job->error() );
112  q->setErrorText( job->errorText() );
113  q->emitResult();
114  return;
115  }
116 
117  if(mShowMessageBox) {
118  const QString text =
119  i18nc( "@info",
120  "The vCard was added to your address book; "
121  "you can add more information to this "
122  "entry by opening the address book." );
123  KMessageBox::information(
124  mParentWidget,
125  text,
126  QString(),
127  QLatin1String("addedtokabc") );
128  }
129  q->emitResult();
130  }
131 
132  AddContactJob *q;
133  KABC::Addressee mContact;
134  QWidget *mParentWidget;
135  Akonadi::Collection mCollection;
136  bool mShowMessageBox;
137 };
138 
139 AddContactJob::AddContactJob( const KABC::Addressee &contact, QWidget *parentWidget, QObject *parent )
140  : KJob( parent ), d( new Private( this, contact, parentWidget ) )
141 {
142 }
143 
144 AddContactJob::AddContactJob( const KABC::Addressee &contact, const Akonadi::Collection &collection, QObject *parent )
145  : KJob( parent ), d( new Private( this, contact, collection ) )
146 {
147 }
148 
149 AddContactJob::~AddContactJob()
150 {
151  delete d;
152 }
153 
154 void AddContactJob::showMessageBox(bool b)
155 {
156  d->mShowMessageBox = b;
157 }
158 
159 void AddContactJob::start()
160 {
161  // first check whether a contact with the same email exists already
162  Akonadi::ContactSearchJob *searchJob = new Akonadi::ContactSearchJob( this );
163  searchJob->setLimit( 1 );
164  searchJob->setQuery( Akonadi::ContactSearchJob::Email, d->mContact.preferredEmail().toLower(),
165  Akonadi::ContactSearchJob::ExactMatch );
166 
167  connect( searchJob, SIGNAL(result(KJob*)), SLOT(slotSearchDone(KJob*)) );
168 }
169 
170 #include "addcontactjob.moc"
selectedcollectiondialog.h
QWidget
QObject
KPIM::AddContactJob::showMessageBox
void showMessageBox(bool b)
Definition: addcontactjob.cpp:154
KPIM::AddContactJob::start
virtual void start()
Starts the job.
Definition: addcontactjob.cpp:159
KPIM::AddContactJob
A job to add a new contact to Akonadi.
Definition: addcontactjob.h:43
KPIM::SelectedCollectionDialog
Definition: selectedcollectiondialog.h:36
addcontactjob.h
KPIM::AddContactJob::~AddContactJob
~AddContactJob()
Destroys the add email address job.
Definition: addcontactjob.cpp:149
KPIM::AddContactJob::AddContactJob
AddContactJob(const KABC::Addressee &contact, QWidget *parentWidget, QObject *parent=0)
Creates a new add contact job.
Definition: addcontactjob.cpp:139
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

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

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