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

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