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

akonadi

  • 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 <klocale.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  return;
153  }
154 
155  if ( mMode == EditMode ) {
156  emit mParent->contactStored( mItem );
157  } else if ( mMode == CreateMode ) {
158  emit mParent->contactStored( static_cast<Akonadi::ItemCreateJob*>( job )->item() );
159  }
160 }
161 
162 void ContactEditor::Private::itemChanged( const Akonadi::Item&, const QSet<QByteArray>& )
163 {
164  QPointer<QMessageBox> dlg = new QMessageBox( mParent ); //krazy:exclude=qclasses
165 
166  dlg->setInformativeText( i18n( "The contact has been changed by someone else.\nWhat should be done?" ) );
167  dlg->addButton( i18n( "Take over changes" ), QMessageBox::AcceptRole );
168  dlg->addButton( i18n( "Ignore and Overwrite changes" ), QMessageBox::RejectRole );
169 
170  if ( dlg->exec() == QMessageBox::AcceptRole ) {
171  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mItem );
172  job->fetchScope().fetchFullPayload();
173  job->fetchScope().fetchAttribute<ContactMetaDataAttribute>();
174  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
175 
176  mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
177  }
178 
179  delete dlg;
180 }
181 
182 void ContactEditor::Private::loadContact( const KABC::Addressee &addr, const ContactMetaData &metaData )
183 {
184  mEditorWidget->loadContact( addr, metaData );
185 }
186 
187 void ContactEditor::Private::storeContact( KABC::Addressee &addr, ContactMetaData &metaData )
188 {
189  mEditorWidget->storeContact( addr, metaData );
190 }
191 
192 void ContactEditor::Private::setupMonitor()
193 {
194  delete mMonitor;
195  mMonitor = new Akonadi::Monitor;
196  mMonitor->ignoreSession( Akonadi::Session::defaultSession() );
197 
198  connect( mMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
199  mParent, SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)) );
200 }
201 
202 
203 ContactEditor::ContactEditor( Mode mode, QWidget *parent )
204  : QWidget( parent ), d( new Private( mode, FullMode, 0, this ) )
205 {
206 }
207 
208 ContactEditor::ContactEditor( Mode mode, AbstractContactEditorWidget *editorWidget, QWidget *parent )
209  : QWidget( parent ), d( new Private( mode, FullMode, editorWidget, this ) )
210 {
211 }
212 
213 ContactEditor::ContactEditor( Mode mode, DisplayMode displayMode, QWidget *parent )
214  : QWidget( parent ), d( new Private( mode, displayMode, 0, this ) )
215 {
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 bool ContactEditor::saveContact()
249 {
250  if ( d->mMode == EditMode ) {
251  if ( !d->mItem.isValid() ) {
252  return true;
253  }
254 
255  if ( d->mReadOnly ) {
256  return true;
257  }
258 
259  KABC::Addressee addr = d->mItem.payload<KABC::Addressee>();
260 
261  d->storeContact( addr, d->mContactMetaData );
262 
263  d->mContactMetaData.store( d->mItem );
264 
265  d->mItem.setPayload<KABC::Addressee>( addr );
266 
267  Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( d->mItem );
268  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
269  } else if ( d->mMode == CreateMode ) {
270  if ( !d->mDefaultCollection.isValid() ) {
271  const QStringList mimeTypeFilter( KABC::Addressee::mimeType() );
272 
273  AutoQPointer<CollectionDialog> dlg = new CollectionDialog( this );
274  dlg->setMimeTypeFilter( mimeTypeFilter );
275  dlg->setAccessRightsFilter( Collection::CanCreateItem );
276  dlg->setCaption( i18n( "Select Address Book" ) );
277  dlg->setDescription( i18n( "Select the address book the new contact shall be saved in:" ) );
278  if ( dlg->exec() == KDialog::Accepted ) {
279  setDefaultAddressBook( dlg->selectedCollection() );
280  } else {
281  return false;
282  }
283  }
284 
285  KABC::Addressee addr;
286  d->storeContact( addr, d->mContactMetaData );
287 
288  Akonadi::Item item;
289  item.setPayload<KABC::Addressee>( addr );
290  item.setMimeType( KABC::Addressee::mimeType() );
291 
292  d->mContactMetaData.store( item );
293 
294  Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, d->mDefaultCollection );
295  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
296  }
297 
298  return true;
299 }
300 
301 void ContactEditor::setContactTemplate( const KABC::Addressee &contact )
302 {
303  d->loadContact( contact, d->mContactMetaData );
304 }
305 
306 void ContactEditor::setDefaultAddressBook( const Akonadi::Collection &collection )
307 {
308  d->mDefaultCollection = collection;
309 }
310 
311 #include "moc_contacteditor.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:34 by doxygen 1.8.3.1 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-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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