1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/*
This file is part of KAddressBook.
Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#include <qlayout.h>
#include <qpushbutton.h>
#include <qtimer.h>
#include <qlabel.h>
#include <qheader.h>
#include <qtooltip.h>
#include <kabc/resource.h>
#include <kdialog.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kresources/configdialog.h>
#include "core.h"
#include "resourceselection.h"
#include <libkdepim/resourceabc.h>
class AddressBookWrapper : public KABC::AddressBook
{
public:
AddressBookWrapper( KABC::AddressBook* );
KRES::Manager<KABC::Resource>* getResourceManager()
{
return resourceManager();
}
};
class ResourceItem : public QCheckListItem
{
public:
ResourceItem( KListView *parent, KABC::Resource *resource )
: QCheckListItem( parent, resource->resourceName(), CheckBox ),
mResource( resource ), mChecked( false ),
mIsSubresource( false ), mSubItemsCreated( false ),
mResourceIdentifier()
{
setOn( resource->isActive() );
setPixmap( 0, KGlobal::iconLoader()->loadIcon( "contents", KIcon::Small ) );
mChecked = isOn();
}
ResourceItem( KPIM::ResourceABC *resourceABC, ResourceItem* parent,
const QString& resourceIdent )
: QCheckListItem( parent, resourceABC->subresourceLabel( resourceIdent ), CheckBox ),
mResource( resourceABC ), mChecked( false ),
mIsSubresource( true ), mSubItemsCreated( false ),
mResourceIdentifier( resourceIdent )
{
KPIM::ResourceABC* res = dynamic_cast<KPIM::ResourceABC *>( mResource );
setOn( res->subresourceActive( mResourceIdentifier ) );
setPixmap( 0, KGlobal::iconLoader()->loadIcon( "contents", KIcon::Small ) );
mChecked = isOn();
}
void createSubresourceItems();
void setChecked( bool state ) {
mChecked = state;
setOn(state);
}
bool checked() const { return mChecked; }
KABC::Resource *resource() const { return mResource; }
QString resourceIdentifier() const { return mResourceIdentifier; }
bool isSubResource() const { return mIsSubresource; }
virtual void stateChange( bool active );
private:
KABC::Resource * const mResource;
bool mChecked;
const bool mIsSubresource;
bool mSubItemsCreated;
const QString mResourceIdentifier;
};
// Comes from korganizer/resourceview.cpp
void ResourceItem::createSubresourceItems()
{
KPIM::ResourceABC* res = dynamic_cast<KPIM::ResourceABC *>( mResource );
QStringList subresources;
if ( res )
subresources = res->subresources();
if ( !subresources.isEmpty() ) {
setOpen( true );
setExpandable( true );
// This resource has subresources
QStringList::ConstIterator it;
for ( it = subresources.begin(); it != subresources.end(); ++it ) {
(void)new ResourceItem( res, this, *it );
}
}
mSubItemsCreated = true;
}
void ResourceItem::stateChange( bool active )
{
//kdDebug(5720) << k_funcinfo << this << " " << text( 0 ) << " active=" << active << endl;
if ( active && !mIsSubresource ) {
if ( !mSubItemsCreated )
createSubresourceItems();
}
setOpen( active && childCount() > 0 );
}
////
ResourceSelection::ResourceSelection( KAB::Core *core, QWidget *parent, const char *name )
: KAB::ExtensionWidget( core, parent, name ), mManager( 0 )
{
initGUI();
AddressBookWrapper *wrapper = static_cast<AddressBookWrapper*>( core->addressBook() );
mManager = wrapper->getResourceManager();
connect( mAddButton, SIGNAL( clicked() ), SLOT( add() ) );
connect( mEditButton, SIGNAL( clicked() ), SLOT( edit() ) );
connect( mRemoveButton, SIGNAL( clicked() ), SLOT( remove() ) );
connect( mListView, SIGNAL( clicked( QListViewItem* ) ),
SLOT( currentChanged( QListViewItem* ) ) );
QTimer::singleShot( 0, this, SLOT( updateView() ) );
}
ResourceSelection::~ResourceSelection()
{
}
QString ResourceSelection::title() const
{
return i18n( "Address Books" );
}
QString ResourceSelection::identifier() const
{
return "resourceselection";
}
void ResourceSelection::add()
{
QStringList types = mManager->resourceTypeNames();
QStringList descs = mManager->resourceTypeDescriptions();
bool ok = false;
QString desc = KInputDialog::getItem( i18n( "Add Address Book" ),
i18n( "Please select type of the new address book:" ),
descs, 0, false, &ok, this );
if ( !ok )
return;
QString type = types[ descs.findIndex( desc ) ];
// Create new resource
KABC::Resource *resource = mManager->createResource( type );
if ( !resource ) {
KMessageBox::error( this, i18n("<qt>Unable to create an address book of type <b>%1</b>.</qt>")
.arg( type ) );
return;
}
resource->setResourceName( i18n( "%1 address book" ).arg( type ) );
resource->setAddressBook(core()->addressBook());
KRES::ConfigDialog dlg( this, QString( "contact" ), resource );
if ( dlg.exec() ) {
core()->addressBook()->addResource( resource );
resource->asyncLoad();
mLastResource = resource->identifier();
updateView();
} else {
delete resource;
resource = 0;
}
}
void ResourceSelection::edit()
{
ResourceItem *item = selectedItem();
if ( !item )
return;
// view items can change during "edit", e.g. sub resources being removed ->
// sub resource item removed
// thus keep their data rather than their pointer
KABC::Resource *resource = item->resource();
KRES::ConfigDialog dlg( this, QString( "contact" ), resource );
if ( dlg.exec() ) {
mManager->change( resource );
resource->asyncLoad();
mLastResource = resource->identifier();
updateView();
}
}
void ResourceSelection::remove()
{
ResourceItem *item = selectedItem();
if ( !item )
return;
int result = KMessageBox::warningContinueCancel( this,
i18n( "<qt>Do you really want to remove the address book <b>%1</b>?</qt>" )
.arg( item->resource()->resourceName() ), "",
KGuiItem( i18n( "&Remove" ), "editdelete" ) );
if ( result == KMessageBox::Cancel )
return;
mLastResource = item->resource()->identifier();
core()->addressBook()->removeResource( item->resource() );
core()->addressBook()->emitAddressBookChanged();
updateView();
}
void ResourceSelection::currentChanged( QListViewItem *item )
{
ResourceItem *resItem = static_cast<ResourceItem*>( item );
bool state = (resItem && !resItem->isSubResource() );
mEditButton->setEnabled( state );
mRemoveButton->setEnabled( state );
if ( !resItem )
return;
KABC::Resource *resource = resItem->resource();
if ( resItem->checked() != resItem->isOn() ) {
resItem->setChecked( resItem->isOn() );
if ( resItem->isSubResource() ) {
KPIM::ResourceABC *res = dynamic_cast<KPIM::ResourceABC *>( resource );
res->setSubresourceActive( resItem->resourceIdentifier(), resItem->isOn() );
mManager->change( resource );
} else {
resource->setActive( resItem->isOn() );
mManager->change( resource );
if ( resItem->checked() ) {
if ( !resource->addressBook() )
resource->setAddressBook( core()->addressBook() );
if ( !resource->isOpen() )
resource->open();
resource->asyncLoad();
} else {
resource->close();
}
}
mLastResource = resource->identifier();
core()->addressBook()->emitAddressBookChanged();
//updateView();
}
}
void ResourceSelection::updateView()
{
if ( !mManager )
return;
mListView->clear();
KRES::Manager<KABC::Resource>::Iterator it;
for ( it = mManager->begin(); it != mManager->end(); ++it ) {
ResourceItem *item = new ResourceItem( mListView, *it );
KPIM::ResourceABC* resource = dynamic_cast<KPIM::ResourceABC *>( *it );
if ( resource ) {
disconnect( resource, 0, this, 0 );
connect( resource, SIGNAL( signalSubresourceAdded( KPIM::ResourceABC *,
const QString &, const QString & ) ),
SLOT( slotSubresourceAdded( KPIM::ResourceABC *,
const QString &, const QString & ) ) );
connect( resource, SIGNAL( signalSubresourceRemoved( KPIM::ResourceABC *,
const QString &, const QString & ) ),
SLOT( slotSubresourceRemoved( KPIM::ResourceABC *,
const QString &, const QString & ) ) );
connect( resource, SIGNAL( signalSubresourceChanged( KPIM::ResourceABC *,
const QString &, const QString & ) ),
SLOT( slotSubresourceChanged( KPIM::ResourceABC *,
const QString &, const QString & ) ) );
//connect( resource, SIGNAL( resourceSaved( KPIM::ResourceABC * ) ),
// SLOT( closeResource( KPIM::ResourceABC * ) ) );
item->createSubresourceItems();
}
}
QListViewItemIterator itemIt( mListView );
while ( itemIt.current() ) {
ResourceItem *item = static_cast<ResourceItem*>( itemIt.current() );
if ( item->resource()->identifier() == mLastResource ) {
mListView->setSelected( item, true );
mListView->ensureItemVisible( item );
break;
}
++itemIt;
}
core()->addressBook()->emitAddressBookChanged();
}
// Add a new entry
void ResourceSelection::slotSubresourceAdded( KPIM::ResourceABC *resource,
const QString& /*type*/,
const QString& subResource )
{
kdDebug(5720) << k_funcinfo << resource->resourceName() << " " << subResource << endl;
QListViewItem *i = mListView->findItem( resource->resourceName(), 0 );
if ( !i )
// Not found
return;
ResourceItem *item = static_cast<ResourceItem *>( i );
// Make sure all other sub items have already been created
item->createSubresourceItems();
// check if we already have an item for it
if ( !findSubResourceItem( resource, subResource ) ) {
(void)new ResourceItem( resource, item, subResource );
}
}
// Remove an entry
void ResourceSelection::slotSubresourceRemoved( KPIM::ResourceABC* resource,
const QString& /*type*/,
const QString& subResource )
{
ResourceItem *item = findSubResourceItem( resource, subResource );
delete item;
core()->addressBook()->emitAddressBookChanged();
updateView();
}
// change an entry
void ResourceSelection::slotSubresourceChanged( KPIM::ResourceABC* resource,
const QString& type,
const QString& subResource )
{
kdDebug(5720) << resource->resourceName() << subResource;
ResourceItem *item = findSubResourceItem( resource, subResource );
if ( item == 0 ) {
kdWarning(5720) << "Changed before it was added?";
slotSubresourceAdded( resource, type, subResource );
return;
}
item->setText( 0, resource->subresourceLabel( subResource ) );
item->setChecked( resource->subresourceActive( subResource ) ? true : false );
// TODO
//emitResourcesChanged();
core()->addressBook()->emitAddressBookChanged();
updateView();
}
ResourceItem* ResourceSelection::selectedItem() const
{
return static_cast<ResourceItem*>( mListView->selectedItem() );
}
ResourceItem* ResourceSelection::findSubResourceItem( KPIM::ResourceABC *resource,
const QString &subResource )
{
QListViewItemIterator parentIt( mListView );
for ( ; *parentIt; ++parentIt ) {
if ( static_cast<ResourceItem*>(*parentIt)->resource() != resource )
continue;
QListViewItemIterator childIt( *parentIt );
for ( ; *childIt; ++childIt ) {
ResourceItem *item = static_cast<ResourceItem*>(*childIt);
if ( item->resourceIdentifier() == subResource )
return item;
}
}
return 0;
}
void ResourceSelection::initGUI()
{
QBoxLayout *topLayout = new QVBoxLayout( this );
topLayout->setSpacing( KDialog::spacingHint() );
QBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->setSpacing( KDialog::spacingHint() );
topLayout->addLayout( buttonLayout );
QLabel *abLabel = new QLabel( i18n( "Address Books" ), this );
buttonLayout->addWidget( abLabel );
buttonLayout->addStretch( 1 );
mAddButton = new QPushButton( this );
mAddButton->setIconSet( SmallIconSet( "add" ) );
QToolTip::add( mAddButton, i18n( "Add addressbook" ) );
buttonLayout->addWidget( mAddButton );
mEditButton = new QPushButton( this );
mEditButton->setIconSet( SmallIconSet( "edit" ) );
mEditButton->setEnabled( false );
QToolTip::add( mEditButton, i18n( "Edit addressbook settings" ) );
buttonLayout->addWidget( mEditButton );
mRemoveButton = new QPushButton( this );
mRemoveButton->setIconSet( SmallIconSet( "remove" ) );
mRemoveButton->setEnabled( false );
QToolTip::add( mRemoveButton, i18n( "Remove addressbook" ) );
buttonLayout->addWidget( mRemoveButton );
mListView = new KListView( this );
mListView->header()->hide();
mListView->addColumn( i18n( "Address Books" ) );
mListView->setFullWidth( true );
topLayout->addWidget( mListView );
}
class ResourceSelectionFactory : public KAB::ExtensionFactory
{
public:
KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
{
return new ResourceSelection( core, parent, name );
}
QString identifier() const
{
return "resourceselection";
}
};
extern "C" {
void *init_libkaddrbk_resourceselection()
{
return ( new ResourceSelectionFactory );
}
}
#include "resourceselection.moc"
|