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

akonadi/contact

  • akonadi
  • contact
contactgroupeditor.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2007-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 "contactgroupeditor.h"
23 #include "contactgroupeditor_p.h"
24 
25 #include "autoqpointer_p.h"
26 #include "contactgroupmodel_p.h"
27 #include "contactgroupeditordelegate_p.h"
28 #include "waitingoverlay_p.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/contactgroup.h>
39 #include <klocale.h>
40 #include <klineedit.h>
41 #include <kmessagebox.h>
42 #include <KColorScheme>
43 
44 
45 #include <QtCore/QTimer>
46 #include <QGridLayout>
47 #include <QMessageBox>
48 #include <QTableView>
49 
50 using namespace Akonadi;
51 
52 ContactGroupEditor::Private::Private( ContactGroupEditor *parent )
53  : mParent( parent ), mMonitor( 0 ), mReadOnly( false )
54 {
55 }
56 
57 ContactGroupEditor::Private::~Private()
58 {
59  delete mMonitor;
60 }
61 
62 void ContactGroupEditor::Private::adaptHeaderSizes()
63 {
64  mGui.membersView->header()->setDefaultSectionSize( mGui.membersView->header()->width() / 2 );
65  mGui.membersView->header()->resizeSections( QHeaderView::Interactive );
66 }
67 
68 void ContactGroupEditor::Private::itemFetchDone( KJob *job )
69 {
70  if ( job->error() ) {
71  return;
72  }
73 
74  ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
75  if ( !fetchJob ) {
76  return;
77  }
78 
79  if ( fetchJob->items().isEmpty() ) {
80  return;
81  }
82 
83  mItem = fetchJob->items().first();
84 
85  mReadOnly = false;
86  if ( mMode == ContactGroupEditor::EditMode ) {
87  // if in edit mode we have to fetch the parent collection to find out
88  // about the modify rights of the item
89 
90  Akonadi::CollectionFetchJob *collectionFetchJob = new Akonadi::CollectionFetchJob( mItem.parentCollection(),
91  Akonadi::CollectionFetchJob::Base );
92  mParent->connect( collectionFetchJob, SIGNAL(result(KJob*)),
93  SLOT(parentCollectionFetchDone(KJob*)) );
94  } else {
95  const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
96  loadContactGroup( group );
97 
98  setReadOnly( mReadOnly );
99 
100  QTimer::singleShot( 0, mParent, SLOT(adaptHeaderSizes()) );
101  }
102 }
103 
104 void ContactGroupEditor::Private::parentCollectionFetchDone( KJob *job )
105 {
106  if ( job->error() ) {
107  return;
108  }
109 
110  Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob*>( job );
111  if ( !fetchJob ) {
112  return;
113  }
114 
115  const Akonadi::Collection parentCollection = fetchJob->collections().first();
116  if ( parentCollection.isValid() ) {
117  mReadOnly = !( parentCollection.rights() & Collection::CanChangeItem );
118  }
119 
120  const KABC::ContactGroup group = mItem.payload<KABC::ContactGroup>();
121  loadContactGroup( group );
122 
123  setReadOnly( mReadOnly );
124 
125  QTimer::singleShot( 0, mParent, SLOT(adaptHeaderSizes()) );
126 }
127 
128 void ContactGroupEditor::Private::storeDone( KJob *job )
129 {
130  if ( job->error() ) {
131  emit mParent->error( job->errorString() );
132  return;
133  }
134 
135  if ( mMode == EditMode ) {
136  emit mParent->contactGroupStored( mItem );
137  } else if ( mMode == CreateMode ) {
138  emit mParent->contactGroupStored( static_cast<ItemCreateJob*>( job )->item() );
139  }
140 }
141 
142 void ContactGroupEditor::Private::itemChanged( const Item&, const QSet<QByteArray>& )
143 {
144  AutoQPointer<QMessageBox> dlg = new QMessageBox( mParent ); //krazy:exclude=qclasses
145 
146  dlg->setInformativeText( i18n( "The contact group has been changed by someone else.\nWhat should be done?" ) );
147  dlg->addButton( i18n( "Take over changes" ), QMessageBox::AcceptRole );
148  dlg->addButton( i18n( "Ignore and Overwrite changes" ), QMessageBox::RejectRole );
149 
150  if ( dlg->exec() == QMessageBox::AcceptRole ) {
151  ItemFetchJob *job = new ItemFetchJob( mItem );
152  job->fetchScope().fetchFullPayload();
153  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
154 
155  mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(itemFetchDone(KJob*)) );
156  new WaitingOverlay( job, mParent );
157  }
158 }
159 
160 void ContactGroupEditor::Private::loadContactGroup( const KABC::ContactGroup &group )
161 {
162  mGui.groupName->setText( group.name() );
163 
164  mGroupModel->loadContactGroup( group );
165 
166  const QAbstractItemModel *model = mGui.membersView->model();
167  mGui.membersView->setCurrentIndex( model->index( model->rowCount() - 1, 0 ) );
168 
169  if ( mMode == EditMode ) {
170  mGui.membersView->setFocus();
171  }
172 
173  mGui.membersView->header()->resizeSections( QHeaderView::Stretch );
174 }
175 
176 bool ContactGroupEditor::Private::storeContactGroup( KABC::ContactGroup &group )
177 {
178  if ( mGui.groupName->text().isEmpty() ) {
179  KMessageBox::error( mParent, i18n( "The name of the contact group must not be empty." ) );
180  return false;
181  }
182 
183  group.setName( mGui.groupName->text() );
184 
185  if ( !mGroupModel->storeContactGroup( group ) ) {
186  KMessageBox::error( mParent, mGroupModel->lastErrorMessage() );
187  return false;
188  }
189 
190  return true;
191 }
192 
193 void ContactGroupEditor::Private::setupMonitor()
194 {
195  delete mMonitor;
196  mMonitor = new Monitor;
197  mMonitor->ignoreSession( Session::defaultSession() );
198 
199  connect( mMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
200  mParent, SLOT(itemChanged(Akonadi::Item,QSet<QByteArray>)) );
201 }
202 
203 void ContactGroupEditor::Private::setReadOnly( bool readOnly )
204 {
205  mGui.groupName->setReadOnly( readOnly );
206  mGui.membersView->setEnabled( !readOnly );
207 }
208 
209 
210 ContactGroupEditor::ContactGroupEditor( Mode mode, QWidget *parent )
211  : QWidget( parent ), d( new Private( this ) )
212 {
213  d->mMode = mode;
214  d->mGui.setupUi( this );
215 
216  d->mGui.membersView->setEditTriggers( QAbstractItemView::AllEditTriggers );
217 
218  d->mGroupModel = new ContactGroupModel( this );
219  d->mGui.membersView->setModel( d->mGroupModel );
220  d->mGui.membersView->setItemDelegate( new ContactGroupEditorDelegate( d->mGui.membersView, this ) );
221 
222  if ( mode == CreateMode ) {
223  KABC::ContactGroup dummyGroup;
224  d->mGroupModel->loadContactGroup( dummyGroup );
225 
226  QTimer::singleShot( 0, this, SLOT(adaptHeaderSizes()) );
227  QTimer::singleShot( 0, d->mGui.groupName, SLOT(setFocus()) );
228  }
229 
230  d->mGui.membersView->header()->setStretchLastSection( true );
231 }
232 
233 ContactGroupEditor::~ContactGroupEditor()
234 {
235  delete d;
236 }
237 
238 void ContactGroupEditor::loadContactGroup( const Akonadi::Item &item )
239 {
240  if ( d->mMode == CreateMode ) {
241  Q_ASSERT_X( false, "ContactGroupEditor::loadContactGroup", "You are calling loadContactGroup in CreateMode!" );
242  }
243 
244  ItemFetchJob *job = new ItemFetchJob( item );
245  job->fetchScope().fetchFullPayload();
246  job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
247 
248  connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchDone(KJob*)) );
249 
250  d->setupMonitor();
251  d->mMonitor->setItemMonitored( item );
252 
253  new WaitingOverlay( job, this );
254 }
255 
256 bool ContactGroupEditor::saveContactGroup()
257 {
258  if ( d->mMode == EditMode ) {
259  if ( !d->mItem.isValid() ) {
260  return false;
261  }
262 
263  if ( d->mReadOnly ) {
264  return true;
265  }
266 
267  KABC::ContactGroup group = d->mItem.payload<KABC::ContactGroup>();
268 
269  if ( !d->storeContactGroup( group ) ) {
270  return false;
271  }
272 
273  d->mItem.setPayload<KABC::ContactGroup>( group );
274 
275  ItemModifyJob *job = new ItemModifyJob( d->mItem );
276  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
277  } else if ( d->mMode == CreateMode ) {
278  if ( !d->mDefaultCollection.isValid() ) {
279  const QStringList mimeTypeFilter( KABC::ContactGroup::mimeType() );
280 
281  AutoQPointer<CollectionDialog> dlg = new CollectionDialog( this );
282  dlg->setMimeTypeFilter( mimeTypeFilter );
283  dlg->setAccessRightsFilter( Collection::CanCreateItem );
284  dlg->setCaption( i18n( "Select Address Book" ) );
285  dlg->setDescription( i18n( "Select the address book the new contact group shall be saved in:" ) );
286 
287  if ( dlg->exec() == KDialog::Accepted ) {
288  setDefaultAddressBook( dlg->selectedCollection() );
289  } else {
290  return false;
291  }
292  }
293 
294  KABC::ContactGroup group;
295  if ( !d->storeContactGroup( group ) ) {
296  return false;
297  }
298 
299  Item item;
300  item.setPayload<KABC::ContactGroup>( group );
301  item.setMimeType( KABC::ContactGroup::mimeType() );
302 
303  ItemCreateJob *job = new ItemCreateJob( item, d->mDefaultCollection );
304  connect( job, SIGNAL(result(KJob*)), SLOT(storeDone(KJob*)) );
305  }
306 
307  return true;
308 }
309 
310 void ContactGroupEditor::setContactGroupTemplate( const KABC::ContactGroup &group )
311 {
312  d->mGroupModel->loadContactGroup( group );
313  d->mGui.membersView->header()->setDefaultSectionSize( d->mGui.membersView->header()->width() / 2 );
314  d->mGui.membersView->header()->resizeSections( QHeaderView::Interactive );
315 }
316 
317 void ContactGroupEditor::setDefaultAddressBook( const Akonadi::Collection &collection )
318 {
319  d->mDefaultCollection = collection;
320 }
321 
322 void ContactGroupEditor::groupNameIsValid(bool isValid)
323 {
324 #ifndef QT_NO_STYLE_STYLESHEET
325  QString styleSheet;
326  if ( !isValid ) {
327  const KColorScheme::BackgroundRole bgColorScheme( KColorScheme::NegativeBackground );
328  KStatefulBrush bgBrush( KColorScheme::View, bgColorScheme );
329  styleSheet = QString::fromLatin1( "QLineEdit{ background-color:%1 }" ).
330  arg( bgBrush.brush( this ).color().name() );
331  }
332  d->mGui.groupName->setStyleSheet( styleSheet );
333 #endif
334 }
335 
336 #include "moc_contactgroupeditor.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:28:41 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • 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