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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
  • contact
contacteditor.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "contacteditor.h"
23 
24 #include "abstractcontacteditorwidget_p.h"
25 #include "autoqpointer_p.h"
26 #include "contactmetadata_p.h"
27 #include "contactmetadataattribute_p.h"
28 #include "editor/contacteditorwidget.h"
29 
30 #include <akonadi/collectiondialog.h>
31 #include <akonadi/collectionfetchjob.h>
32 #include <akonadi/itemcreatejob.h>
33 #include <akonadi/itemfetchjob.h>
34 #include <akonadi/itemfetchscope.h>
35 #include <akonadi/itemmodifyjob.h>
36 #include <akonadi/monitor.h>
37 #include <akonadi/session.h>
38 #include <kabc/addressee.h>
39 #include <klocalizedstring.h>
40 
41 #include <QtCore/QPointer>
42 #include <QVBoxLayout>
43 #include <QMessageBox>
44 
45 using namespace Akonadi;
46 
47 class ContactEditor::Private
48 {
49  public:
50  Private( ContactEditor::Mode mode, ContactEditor::DisplayMode displayMode, AbstractContactEditorWidget *editorWidget, ContactEditor *parent )
51  : mParent( parent ), mMode( mode ), mMonitor( 0 ), mReadOnly( false )
52  {
53  if ( editorWidget ) {
54  mEditorWidget = editorWidget;
55 #ifndef DISABLE_EDITOR_WIDGETS
56  } else {
57  mEditorWidget = new ContactEditorWidget(displayMode == FullMode ? ContactEditorWidget::FullMode : ContactEditorWidget::VCardMode, 0);
58 #endif
59  }
60 
61  QVBoxLayout *layout = new QVBoxLayout( mParent );
62  layout->setMargin( 0 );
63  layout->setSpacing( 0 );
64  layout->addWidget( mEditorWidget );
65  }
66 
67  ~Private()
68  {
69  delete mMonitor;
70  }
71 
72  void itemFetchDone( KJob* );
73  void parentCollectionFetchDone( KJob* );
74  void storeDone( KJob* );
75  void itemChanged( const Akonadi::Item &item, const QSet<QByteArray>& );
76 
77  void loadContact( const KABC::Addressee &addr, const ContactMetaData &metaData );
78  void storeContact( KABC::Addressee &addr, ContactMetaData &metaData );
79  void setupMonitor();
80 
81  ContactEditor *mParent;
82  ContactEditor::Mode mMode;
83  Akonadi::Item mItem;
84  Akonadi::ContactMetaData mContactMetaData;
85  Akonadi::Monitor *mMonitor;
86  Akonadi::Collection mDefaultCollection;
87  AbstractContactEditorWidget *mEditorWidget;
88  bool mReadOnly;
89 };
90 
91 void ContactEditor::Private::itemFetchDone( KJob *job )
92 {
93  if ( job->error() != KJob::NoError ) {
94  return;
95  }
96 
97  Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
98  if ( !fetchJob ) {
99  return;
100  }
101 
102  if ( fetchJob->items().isEmpty() ) {
103  return;
104  }
105 
106  mItem = fetchJob->items().first();
107 
108  mReadOnly = false;
109  if ( mMode == ContactEditor::EditMode ) {
110  // if in edit mode we have to fetch the parent collection to find out
111  // about the modify rights of the item
112 
113  Akonadi::CollectionFetchJob *collectionFetchJob = new Akonadi::CollectionFetchJob( mItem.parentCollection(),
114  Akonadi::CollectionFetchJob::Base );
115  mParent->connect( collectionFetchJob, SIGNAL(result(KJob*)),
116  SLOT(parentCollectionFetchDone(KJob*)) );
117  } else {
118  const KABC::Addressee addr = mItem.payload<KABC::Addressee>();
119  mContactMetaData.load( mItem );
120  loadContact( addr, mContactMetaData );
121  mEditorWidget->setReadOnly( mReadOnly );
122  }
123 }
124 
125 void ContactEditor::Private::parentCollectionFetchDone( KJob *job )
126 {
127  if ( job->error() ) {
128  return;
129  }
130 
131  Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
132  if ( !fetchJob ) {
133  return;
134  }
135 
136  const Akonadi::Collection parentCollection = fetchJob->collections().first();
137  if ( parentCollection.isValid() ) {
138  mReadOnly = !( parentCollection.rights() & Collection::CanChangeItem );
139  }
140 
141  mEditorWidget->setReadOnly( mReadOnly );
142 
143  const KABC::Addressee addr = mItem.payload<KABC::Addressee>();
144  mContactMetaData.load( mItem );
145  loadContact( addr, mContactMetaData );
146 }
147 
148 void ContactEditor::Private::storeDone( KJob *job )
149 {
150  if ( job->error() != KJob::NoError ) {
151  emit mParent->error( job->errorString() );
152  emit mParent->finished();
153  return;
154  }
155 
156  if ( mMode == EditMode ) {
157  emit mParent->contactStored( mItem );
158  } else if ( mMode == CreateMode ) {
159  emit mParent->contactStored( static_cast<Akonadi::ItemCreateJob*>( job )->item() );
160  }
161  emit mParent->finished();
162 }
163 
164 void ContactEditor::Private::itemChanged( const Akonadi::Item&, const QSet<QByteArray>& )
165 {
166  QPointer<QMessageBox> dlg = new QMessageBox( mParent ); //krazy:exclude=qclasses
167 
168  dlg->setInformativeText( i18n( "The contact has been changed by someone else.\nWhat should be done?" ) );
169  dlg->addButton( i18n( "Take over changes" ), QMessageBox::AcceptRole );
170  dlg->addButton( i18n( "Ignore and Overwrite changes" ), QMessageBox::RejectRole );
171 
172  if ( dlg->exec() == QMessageBox::AcceptRole ) {
173  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mItem );
174  job->fetchScope().fetchFullPayload();
175  job->fetchScope().fetchAttribute<ContactMetaDataAttribute>();
176  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
177 
178  mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
179  }
180 
181  delete dlg;
182 }
183 
184 void ContactEditor::Private::loadContact( const KABC::Addressee &addr, const ContactMetaData &metaData )
185 {
186  mEditorWidget->loadContact( addr, metaData );
187 }
188 
189 void ContactEditor::Private::storeContact( KABC::Addressee &addr, ContactMetaData &metaData )
190 {
191  mEditorWidget->storeContact( addr, metaData );
192 }
193 
194 void ContactEditor::Private::setupMonitor()
195 {
196  delete mMonitor;
197  mMonitor = new Akonadi::Monitor;
198  mMonitor->ignoreSession( Akonadi::Session::defaultSession() );
199 
200  connect( mMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
201  mParent, SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)) );
202 }
203 
204 ContactEditor::ContactEditor( Mode mode, QWidget *parent )
205  : QWidget( parent ), d( new Private( mode, FullMode, 0, this ) )
206 {
207 }
208 
209 ContactEditor::ContactEditor( Mode mode, AbstractContactEditorWidget *editorWidget, QWidget *parent )
210  : QWidget( parent ), d( new Private( mode, FullMode, editorWidget, this ) )
211 {
212 }
213 
214 ContactEditor::ContactEditor( Mode mode, DisplayMode displayMode, QWidget *parent )
215  : QWidget( parent ), d( new Private( mode, displayMode, 0, this ) )
216 {
217 }
218 
219 ContactEditor::~ContactEditor()
220 {
221  delete d;
222 }
223 
224 void ContactEditor::loadContact( const Akonadi::Item &item )
225 {
226  if ( d->mMode == CreateMode ) {
227  Q_ASSERT_X( false, "ContactEditor::loadContact", "You are calling loadContact in CreateMode!" );
228  }
229 
230  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( item );
231  job->fetchScope().fetchFullPayload();
232  job->fetchScope().fetchAttribute<ContactMetaDataAttribute>();
233  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
234 
235  connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchDone(KJob*)) );
236 
237  d->setupMonitor();
238  d->mMonitor->setItemMonitored( item );
239 }
240 
241 KABC::Addressee ContactEditor::contact()
242 {
243  KABC::Addressee addr;
244  d->storeContact( addr, d->mContactMetaData );
245  return addr;
246 }
247 
248 void ContactEditor::saveContactInAddressBook()
249 {
250  if ( d->mMode == EditMode ) {
251  if ( !d->mItem.isValid() || d->mReadOnly ) {
252  emit finished();
253  return;
254  }
255 
256  KABC::Addressee addr = d->mItem.payload<KABC::Addressee>();
257 
258  d->storeContact( addr, d->mContactMetaData );
259 
260  d->mContactMetaData.store( d->mItem );
261 
262  d->mItem.setPayload<KABC::Addressee>( addr );
263 
264  Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( d->mItem );
265  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
266  } else if ( d->mMode == CreateMode ) {
267  if ( !d->mDefaultCollection.isValid() ) {
268  const QStringList mimeTypeFilter( KABC::Addressee::mimeType() );
269 
270  AutoQPointer<CollectionDialog> dlg = new CollectionDialog( this );
271  dlg->setMimeTypeFilter( mimeTypeFilter );
272  dlg->setAccessRightsFilter( Collection::CanCreateItem );
273  dlg->setCaption( i18n( "Select Address Book" ) );
274  dlg->setDescription( i18n( "Select the address book the new contact shall be saved in:" ) );
275  if ( dlg->exec() == KDialog::Accepted ) {
276  setDefaultAddressBook( dlg->selectedCollection() );
277  } else {
278  return;
279  }
280  }
281 
282  KABC::Addressee addr;
283  d->storeContact( addr, d->mContactMetaData );
284 
285  Akonadi::Item item;
286  item.setPayload<KABC::Addressee>( addr );
287  item.setMimeType( KABC::Addressee::mimeType() );
288 
289  d->mContactMetaData.store( item );
290 
291  Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, d->mDefaultCollection );
292  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
293  }
294 }
295 
296 bool ContactEditor::saveContact()
297 {
298  if ( d->mMode == EditMode ) {
299  if ( !d->mItem.isValid() ) {
300  return true;
301  }
302 
303  if ( d->mReadOnly ) {
304  return true;
305  }
306 
307  KABC::Addressee addr = d->mItem.payload<KABC::Addressee>();
308 
309  d->storeContact( addr, d->mContactMetaData );
310 
311  d->mContactMetaData.store( d->mItem );
312 
313  d->mItem.setPayload<KABC::Addressee>( addr );
314 
315  Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( d->mItem );
316  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
317  } else if ( d->mMode == CreateMode ) {
318  if ( !d->mDefaultCollection.isValid() ) {
319  const QStringList mimeTypeFilter( KABC::Addressee::mimeType() );
320 
321  AutoQPointer<CollectionDialog> dlg = new CollectionDialog( this );
322  dlg->setMimeTypeFilter( mimeTypeFilter );
323  dlg->setAccessRightsFilter( Collection::CanCreateItem );
324  dlg->setCaption( i18n( "Select Address Book" ) );
325  dlg->setDescription( i18n( "Select the address book the new contact shall be saved in:" ) );
326  if ( dlg->exec() == KDialog::Accepted ) {
327  setDefaultAddressBook( dlg->selectedCollection() );
328  } else {
329  return false;
330  }
331  }
332 
333  KABC::Addressee addr;
334  d->storeContact( addr, d->mContactMetaData );
335 
336  Akonadi::Item item;
337  item.setPayload<KABC::Addressee>( addr );
338  item.setMimeType( KABC::Addressee::mimeType() );
339 
340  d->mContactMetaData.store( item );
341 
342  Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, d->mDefaultCollection );
343  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
344  }
345 
346  return true;
347 }
348 
349 void ContactEditor::setContactTemplate( const KABC::Addressee &contact )
350 {
351  d->loadContact( contact, d->mContactMetaData );
352 }
353 
354 void ContactEditor::setDefaultAddressBook( const Akonadi::Collection &collection )
355 {
356  d->mDefaultCollection = collection;
357 }
358 
359 #include "moc_contacteditor.cpp"
Akonadi::ItemFetchScope::fetchAttribute
void fetchAttribute(const QByteArray &type, bool fetch=true)
Sets whether the attribute of the given type should be fetched.
Definition: itemfetchscope.cpp:78
Akonadi::ContactEditor::Mode
Mode
Describes the mode of the editor.
Definition: contacteditor.h:88
Akonadi::CollectionFetchJob::collections
Collection::List collections() const
Returns the list of fetched collection.
Definition: collectionfetchjob.cpp:175
Akonadi::ContactMetaDataAttribute
Attribute to store contact specific meta data.
Definition: contactmetadataattribute_p.h:38
Akonadi::ContactEditor::finished
void finished()
finished
Akonadi::ContactEditor::EditMode
Edits an existing contact.
Definition: contacteditor.h:90
ContactEditorWidget
A widget for editing a contact.
Definition: contacteditorwidget.h:37
Akonadi::CollectionDialog
A collection selection dialog.
Definition: collectiondialog.h:67
Akonadi::ContactEditor::CreateMode
Creates a new contact.
Definition: contacteditor.h:89
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::CollectionFetchJob
Job that fetches collections from the Akonadi storage.
Definition: collectionfetchjob.h:53
Akonadi::Monitor::ignoreSession
void ignoreSession(Session *session)
Ignores all change notifications caused by the given session.
Definition: monitor.cpp:166
Akonadi::ContactEditor::saveContact
AKONADI_CONTACT_DEPRECATED bool saveContact()
Saves the contact from the editor back to the storage.
Definition: contacteditor.cpp:296
Akonadi::Collection::CanChangeItem
Can change items in this collection.
Definition: collection.h:88
Akonadi::ContactEditor::FullMode
Show all pages.
Definition: contacteditor.h:94
Akonadi::ItemFetchScope::fetchFullPayload
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
Definition: itemfetchscope.cpp:68
Akonadi::Session::defaultSession
static Session * defaultSession()
Returns the default session for this thread.
Definition: session.cpp:444
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched items.
Definition: itemfetchjob.cpp:228
Akonadi::CollectionFetchJob::Base
Only fetch the base collection.
Definition: collectionfetchjob.h:62
Akonadi::ItemFetchJob::fetchScope
ItemFetchScope & fetchScope()
Returns the item fetch scope.
Definition: itemfetchjob.cpp:256
Akonadi::ContactMetaData
A helper class for storing contact specific settings.
Definition: contactmetadata_p.h:36
Akonadi::ContactEditor
An widget to edit contacts in Akonadi.
Definition: contacteditor.h:80
Akonadi::ContactEditor::setContactTemplate
void setContactTemplate(const KABC::Addressee &contact)
Sets a contact that is used as template in create mode.
Definition: contacteditor.cpp:349
Akonadi::ContactEditor::~ContactEditor
virtual ~ContactEditor()
Destroys the contact editor.
Definition: contacteditor.cpp:219
Akonadi::ContactEditor::loadContact
void loadContact(const Akonadi::Item &contact)
Loads the contact into the editor.
Definition: contacteditor.cpp:224
Akonadi::ItemFetchScope::Parent
Only retrieve the immediate parent collection.
Definition: itemfetchscope.h:77
Akonadi::Collection::CanCreateItem
Can create new items in this collection.
Definition: collection.h:89
Akonadi::ContactEditor::DisplayMode
DisplayMode
Definition: contacteditor.h:93
Akonadi::ContactEditor::setDefaultAddressBook
void setDefaultAddressBook(const Akonadi::Collection &addressbook)
Sets the addressbook which shall be used to store new contacts.
Definition: contacteditor.cpp:354
Akonadi::ItemCreateJob
Job that creates a new item in the Akonadi storage.
Definition: itemcreatejob.h:73
Akonadi::Collection::rights
Rights rights() const
Returns the rights the user has on the collection.
Definition: collection.cpp:99
AutoQPointer
A QPointer which when destructed, deletes the object it points to.
Definition: autoqpointer_p.h:34
Akonadi::ItemFetchScope::setAncestorRetrieval
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval. ...
Definition: itemfetchscope.cpp:128
Akonadi::ContactEditor::ContactEditor
ContactEditor(Mode mode, QWidget *parent=0)
Creates a new contact editor with the standard editor widget.
Definition: contacteditor.cpp:204
Akonadi::Monitor
Monitors an item or collection for changes.
Definition: monitor.h:72
Akonadi::ItemModifyJob
Job that modifies an existing item in the Akonadi storage.
Definition: itemmodifyjob.h:97
Akonadi::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:82
Akonadi::ContactEditor::saveContactInAddressBook
void saveContactInAddressBook()
Save the contact from the editor back to the storage.
Definition: contacteditor.cpp:248
Akonadi::ContactEditor::contact
KABC::Addressee contact()
ContactEditor::contact.
Definition: contacteditor.cpp:241
Akonadi::Entity::isValid
bool isValid() const
Returns whether the entity is valid.
Definition: entity.cpp:97
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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