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

akonadi

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

Skip menu "kdepimlibs-4.9.5 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