• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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 <QtGui/QVBoxLayout>
43 #include <QtGui/QMessageBox>
44 
45 using namespace Akonadi;
46 
47 class ContactEditor::Private
48 {
49  public:
50  Private( ContactEditor::Mode mode, 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();
58 #endif
59 
60  QVBoxLayout *layout = new QVBoxLayout( mParent );
61  layout->setMargin( 0 );
62  layout->setSpacing( 0 );
63  layout->addWidget( mEditorWidget );
64  }
65 
66  ~Private()
67  {
68  delete mMonitor;
69  }
70 
71  void itemFetchDone( KJob* );
72  void parentCollectionFetchDone( KJob* );
73  void storeDone( KJob* );
74  void itemChanged( const Akonadi::Item &item, const QSet<QByteArray>& );
75 
76  void loadContact( const KABC::Addressee &addr, const ContactMetaData &metaData );
77  void storeContact( KABC::Addressee &addr, ContactMetaData &metaData );
78  void setupMonitor();
79 
80  ContactEditor *mParent;
81  ContactEditor::Mode mMode;
82  Akonadi::Item mItem;
83  Akonadi::ContactMetaData mContactMetaData;
84  Akonadi::Monitor *mMonitor;
85  Akonadi::Collection mDefaultCollection;
86  AbstractContactEditorWidget *mEditorWidget;
87  bool mReadOnly;
88 };
89 
90 void ContactEditor::Private::itemFetchDone( KJob *job )
91 {
92  if ( job->error() != KJob::NoError )
93  return;
94 
95  Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
96  if ( !fetchJob )
97  return;
98 
99  if ( fetchJob->items().isEmpty() )
100  return;
101 
102  mItem = fetchJob->items().first();
103 
104  mReadOnly = false;
105  if ( mMode == ContactEditor::EditMode ) {
106  // if in edit mode we have to fetch the parent collection to find out
107  // about the modify rights of the item
108 
109  Akonadi::CollectionFetchJob *collectionFetchJob = new Akonadi::CollectionFetchJob( mItem.parentCollection(),
110  Akonadi::CollectionFetchJob::Base );
111  mParent->connect( collectionFetchJob, SIGNAL(result(KJob*)),
112  SLOT(parentCollectionFetchDone(KJob*)) );
113  } else {
114  const KABC::Addressee addr = mItem.payload<KABC::Addressee>();
115  mContactMetaData.load( mItem );
116  loadContact( addr, mContactMetaData );
117  mEditorWidget->setReadOnly( mReadOnly );
118  }
119 }
120 
121 void ContactEditor::Private::parentCollectionFetchDone( KJob *job )
122 {
123  if ( job->error() )
124  return;
125 
126  Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
127  if ( !fetchJob )
128  return;
129 
130  const Akonadi::Collection parentCollection = fetchJob->collections().first();
131  if ( parentCollection.isValid() )
132  mReadOnly = !(parentCollection.rights() & Collection::CanChangeItem);
133 
134  mEditorWidget->setReadOnly( mReadOnly );
135 
136  const KABC::Addressee addr = mItem.payload<KABC::Addressee>();
137  mContactMetaData.load( mItem );
138  loadContact( addr, mContactMetaData );
139 }
140 
141 void ContactEditor::Private::storeDone( KJob *job )
142 {
143  if ( job->error() != KJob::NoError ) {
144  emit mParent->error( job->errorString() );
145  return;
146  }
147 
148  if ( mMode == EditMode )
149  emit mParent->contactStored( mItem );
150  else if ( mMode == CreateMode )
151  emit mParent->contactStored( static_cast<Akonadi::ItemCreateJob*>( job )->item() );
152 }
153 
154 void ContactEditor::Private::itemChanged( const Akonadi::Item&, const QSet<QByteArray>& )
155 {
156  QPointer<QMessageBox> dlg = new QMessageBox( mParent ); //krazy:exclude=qclasses
157 
158  dlg->setInformativeText( i18n( "The contact has been changed by someone else.\nWhat should be done?" ) );
159  dlg->addButton( i18n( "Take over changes" ), QMessageBox::AcceptRole );
160  dlg->addButton( i18n( "Ignore and Overwrite changes" ), QMessageBox::RejectRole );
161 
162  if ( dlg->exec() == QMessageBox::AcceptRole ) {
163  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mItem );
164  job->fetchScope().fetchFullPayload();
165  job->fetchScope().fetchAttribute<ContactMetaDataAttribute>();
166  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
167 
168  mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
169  }
170 
171  delete dlg;
172 }
173 
174 void ContactEditor::Private::loadContact( const KABC::Addressee &addr, const ContactMetaData &metaData )
175 {
176  mEditorWidget->loadContact( addr, metaData );
177 }
178 
179 void ContactEditor::Private::storeContact( KABC::Addressee &addr, ContactMetaData &metaData )
180 {
181  mEditorWidget->storeContact( addr, metaData );
182 }
183 
184 void ContactEditor::Private::setupMonitor()
185 {
186  delete mMonitor;
187  mMonitor = new Akonadi::Monitor;
188  mMonitor->ignoreSession( Akonadi::Session::defaultSession() );
189 
190  connect( mMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
191  mParent, SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)) );
192 }
193 
194 
195 ContactEditor::ContactEditor( Mode mode, QWidget *parent )
196  : QWidget( parent ), d( new Private( mode, 0, this ) )
197 {
198 }
199 
200 ContactEditor::ContactEditor( Mode mode, AbstractContactEditorWidget *editorWidget, QWidget *parent )
201  : QWidget( parent ), d( new Private( mode, editorWidget, this ) )
202 {
203 }
204 
205 ContactEditor::~ContactEditor()
206 {
207  delete d;
208 }
209 
210 void ContactEditor::loadContact( const Akonadi::Item &item )
211 {
212  if ( d->mMode == CreateMode )
213  Q_ASSERT_X( false, "ContactEditor::loadContact", "You are calling loadContact in CreateMode!" );
214 
215  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( item );
216  job->fetchScope().fetchFullPayload();
217  job->fetchScope().fetchAttribute<ContactMetaDataAttribute>();
218  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
219 
220  connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchDone(KJob*)) );
221 
222  d->setupMonitor();
223  d->mMonitor->setItemMonitored( item );
224 }
225 
226 bool ContactEditor::saveContact()
227 {
228  if ( d->mMode == EditMode ) {
229  if ( !d->mItem.isValid() )
230  return true;
231 
232  if ( d->mReadOnly )
233  return true;
234 
235  KABC::Addressee addr = d->mItem.payload<KABC::Addressee>();
236 
237  d->storeContact( addr, d->mContactMetaData );
238 
239  d->mContactMetaData.store( d->mItem );
240 
241  d->mItem.setPayload<KABC::Addressee>( addr );
242 
243  Akonadi::ItemModifyJob *job = new Akonadi::ItemModifyJob( d->mItem );
244  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
245  } else if ( d->mMode == CreateMode ) {
246  if ( !d->mDefaultCollection.isValid() ) {
247  const QStringList mimeTypeFilter( KABC::Addressee::mimeType() );
248 
249  AutoQPointer<CollectionDialog> dlg = new CollectionDialog( this );
250  dlg->setMimeTypeFilter( mimeTypeFilter );
251  dlg->setAccessRightsFilter( Collection::CanCreateItem );
252  dlg->setCaption( i18n( "Select Address Book" ) );
253  dlg->setDescription( i18n( "Select the address book the new contact shall be saved in:" ) );
254  if ( dlg->exec() == KDialog::Accepted )
255  setDefaultAddressBook( dlg->selectedCollection() );
256  else
257  return false;
258  }
259 
260  KABC::Addressee addr;
261  d->storeContact( addr, d->mContactMetaData );
262 
263  Akonadi::Item item;
264  item.setPayload<KABC::Addressee>( addr );
265  item.setMimeType( KABC::Addressee::mimeType() );
266 
267  d->mContactMetaData.store( item );
268 
269  Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, d->mDefaultCollection );
270  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
271  }
272 
273  return true;
274 }
275 
276 void ContactEditor::setContactTemplate( const KABC::Addressee &contact )
277 {
278  d->loadContact( contact, d->mContactMetaData );
279 }
280 
281 void ContactEditor::setDefaultAddressBook( const Akonadi::Collection &collection )
282 {
283  d->mDefaultCollection = collection;
284 }
285 
286 #include "contacteditor.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 11 2012 12:14:30 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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