summaryrefslogtreecommitdiffstats
path: root/kaddressbook/features/resourceselection.cpp
blob: 4a16c598810ba8668df6003c02c309df90baa54e (plain)
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
    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 TQt, and distribute the resulting executable,
    without including the source code for TQt in the source distribution.
*/

#include <tqlayout.h>
#include <tqpopupmenu.h>
#include <tqpushbutton.h>
#include <tqtimer.h>
#include <tqlabel.h>
#include <tqheader.h>
#include <tqtooltip.h>

#include <tdeabc/resource.h>
#include <kdialog.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tderesources/configdialog.h>

#include "core.h"

#include "resourceselection.h"
#include <libtdepim/resourceabc.h>

class AddressBookWrapper : public TDEABC::AddressBook
{
  public:
    AddressBookWrapper( TDEABC::AddressBook* );

    KRES::Manager<TDEABC::Resource>* getResourceManager()
    {
      return resourceManager();
    }
};

class ResourceItem : public TQCheckListItem
{
  public:
    ResourceItem( TDEListView *parent, TDEABC::Resource *resource )
      : TQCheckListItem( parent, resource->resourceName(), CheckBox ),
        mResource( resource ), mChecked( false ),
        mIsSubresource( false ), mSubItemsCreated( false ),
        mResourceIdentifier()
    {
      setOn( resource->isActive() );
      setPixmap( 0, TDEGlobal::iconLoader()->loadIcon( "contents", TDEIcon::Small ) );
      mChecked = isOn();
    }

    ResourceItem( KPIM::ResourceABC *resourceABC, ResourceItem* parent,
                  const TQString& resourceIdent )
      : TQCheckListItem( 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, TDEGlobal::iconLoader()->loadIcon( "contents", TDEIcon::Small ) );
      mChecked = isOn();
    }

    void createSubresourceItems();

    void setChecked( bool state ) {
        mChecked = state;
        setOn(state);
    }
    bool checked() const { return mChecked; }
    TDEABC::Resource *resource() const { return mResource; }
    TQString resourceIdentifier() const { return mResourceIdentifier; }
    bool isSubResource() const { return mIsSubresource; }

    virtual void stateChange( bool active );

  private:
    TDEABC::Resource * const mResource;
    bool mChecked;
    const bool mIsSubresource;
    bool mSubItemsCreated;
    const TQString mResourceIdentifier;
};

// Comes from korganizer/resourceview.cpp
void ResourceItem::createSubresourceItems()
{
  KPIM::ResourceABC* res = dynamic_cast<KPIM::ResourceABC *>( mResource );
  TQStringList subresources;
  if ( res )
    subresources = res->subresources();
  if ( !subresources.isEmpty() ) {
    setOpen( true );
    setExpandable( true );
    // This resource has subresources
    TQStringList::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, TQWidget *parent, const char *name )
  : KAB::ExtensionWidget( core, parent, name ), mManager( 0 )
{
  initGUI();

  AddressBookWrapper *wrapper = static_cast<AddressBookWrapper*>( core->addressBook() );
  mManager = wrapper->getResourceManager();

  connect( mAddButton, TQ_SIGNAL( clicked() ), TQ_SLOT( add() ) );
  connect( mEditButton, TQ_SIGNAL( clicked() ), TQ_SLOT( edit() ) );
  connect( mRemoveButton, TQ_SIGNAL( clicked() ), TQ_SLOT( remove() ) );

  connect( mListView, TQ_SIGNAL( clicked( TQListViewItem* ) ),
           TQ_SLOT( currentChanged( TQListViewItem* ) ) );

  connect( mListView, TQ_SIGNAL( contextMenuRequested ( TQListViewItem *,
                                                     const TQPoint &, int ) ),
           TQ_SLOT( contextMenuRequested( TQListViewItem *, const TQPoint &,
                                       int ) ) );

  TQTimer::singleShot( 0, this, TQ_SLOT( updateView() ) );
}

ResourceSelection::~ResourceSelection()
{
}

void ResourceSelection::contextMenuRequested ( TQListViewItem *i,
                                          const TQPoint &pos, int )
{
  ResourceItem *item = static_cast<ResourceItem *>( i );

  TQPopupMenu *menu = new TQPopupMenu( this );
  connect( menu, TQ_SIGNAL( aboutToHide() ), menu, TQ_SLOT( deleteLater() ) );
  if ( item ) {
    int reloadId = menu->insertItem( i18n("Re&load"), this,
                                     TQ_SLOT( reloadResource() ) );
    menu->setItemEnabled( reloadId, item->resource()->isActive() );
    int saveId = menu->insertItem( i18n("&Save"), this,
                                   TQ_SLOT( saveResource() ) );
    menu->setItemEnabled( saveId, item->resource()->isActive() );
    menu->insertSeparator();

//     menu->insertItem( i18n("Show &Info"), this, TQ_SLOT( showInfo() ) );

    menu->insertItem( i18n("&Edit..."), this, TQ_SLOT( edit() ) );
    menu->insertItem( i18n("&Remove"), this, TQ_SLOT( remove() ) );

    menu->insertSeparator();
 }
  menu->insertItem( i18n("&Add..."), this, TQ_SLOT( add() ) );

  menu->popup( pos );
}

void ResourceSelection::reloadResource()
{
  ResourceItem *item = selectedItem();
  if ( !item ) return;

  TDEABC::Resource *r = item->resource();
  r->load();
}

void ResourceSelection::saveResource()
{
  ResourceItem *item = selectedItem();
  if ( !item ) return;

  TDEABC::Resource *r = item->resource();
  TDEABC::Ticket *ticket = core()->addressBook()->requestSaveTicket( r );
  if ( ticket ) {
      r->save( ticket );
  }
}

void ResourceSelection::showInfo()
{
  ResourceItem *item = selectedItem();
  if ( !item ) return;

//   TQString txt = "<qt>" + item->resource()->infoText() + "</qt>";
//   KMessageBox::information( this, txt );
}

TQString ResourceSelection::title() const
{
  return i18n( "Address Books" );
}

TQString ResourceSelection::identifier() const
{
  return "resourceselection";
}

void ResourceSelection::add()
{
  TQStringList types = mManager->resourceTypeNames();
  TQStringList descs = mManager->resourceTypeDescriptions();

  bool ok = false;
  TQString desc = KInputDialog::getItem( i18n( "Add Address Book" ),
                                        i18n( "Please select type of the new address book:" ),
                                        descs, 0, false, &ok, this );
  if ( !ok )
    return;

  TQString type = types[ descs.findIndex( desc ) ];

  // Create new resource
  TDEABC::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->setAddressBook(core()->addressBook());

  KRES::ConfigDialog dlg( this, TQString( "contact" ), resource );

  if ( dlg.exec() ) {
    core()->addressBook()->addResource( resource );
    resource->asyncLoad();

    mLastResource = resource->identifier();
    updateView();
    currentChanged(mListView->currentItem() );
  } 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
  TDEABC::Resource *resource = item->resource();

  KRES::ConfigDialog dlg( this, TQString( "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" ), "edit-delete" ) );
  if ( result == KMessageBox::Cancel )
    return;

  mLastResource = item->resource()->identifier();

  core()->addressBook()->removeResource( item->resource() );
  core()->addressBook()->emitAddressBookChanged();

  updateView();
  currentChanged(mListView->currentItem() );
}

void ResourceSelection::currentChanged( TQListViewItem *item )
{
  ResourceItem *resItem = static_cast<ResourceItem*>( item );
  bool state = (resItem && !resItem->isSubResource() );

  mEditButton->setEnabled( state );
  mRemoveButton->setEnabled( state );

  if ( !resItem )
    return;

  TDEABC::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<TDEABC::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, TQ_SIGNAL( signalSubresourceAdded( KPIM::ResourceABC *,
                                                         const TQString &, const TQString & ) ),
               TQ_SLOT( slotSubresourceAdded( KPIM::ResourceABC *,
                                           const TQString &, const TQString & ) ) );

      connect( resource, TQ_SIGNAL( signalSubresourceRemoved( KPIM::ResourceABC *,
                                                           const TQString &, const TQString & ) ),
               TQ_SLOT( slotSubresourceRemoved( KPIM::ResourceABC *,
                                             const TQString &, const TQString & ) ) );

      connect( resource, TQ_SIGNAL( signalSubresourceChanged( KPIM::ResourceABC *,
                                                           const TQString &, const TQString & ) ),
               TQ_SLOT( slotSubresourceChanged( KPIM::ResourceABC *,
                                             const TQString &, const TQString & ) ) );

      //connect( resource, TQ_SIGNAL( resourceSaved( KPIM::ResourceABC * ) ),
      //         TQ_SLOT( closeResource( KPIM::ResourceABC * ) ) );
      item->createSubresourceItems();
    }
  }

  TQListViewItemIterator 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 TQString& /*type*/,
                                              const TQString& subResource )
{
  kdDebug(5720) << k_funcinfo << resource->resourceName() << " " << subResource << endl;
  TQListViewItem *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 TQString& /*type*/,
                                                const TQString& subResource )
{
  ResourceItem *item = findSubResourceItem( resource, subResource );
  delete item;
  core()->addressBook()->emitAddressBookChanged();
  updateView();
}

// change an entry
void ResourceSelection::slotSubresourceChanged( KPIM::ResourceABC* resource,
                                                const TQString& type,
                                                const TQString& 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 TQString &subResource )
{
    TQListViewItemIterator parentIt( mListView );
    for ( ; *parentIt; ++parentIt ) {
        if ( static_cast<ResourceItem*>(*parentIt)->resource() != resource )
            continue;

        TQListViewItemIterator childIt( *parentIt );
        for ( ; *childIt; ++childIt ) {
            ResourceItem *item = static_cast<ResourceItem*>(*childIt);
            if ( item->resourceIdentifier() == subResource )
                return item;
        }
    }

    return 0;
}

void ResourceSelection::initGUI()
{
  TQBoxLayout *topLayout = new TQVBoxLayout( this );
  topLayout->setSpacing( KDialog::spacingHint() );

  TQBoxLayout *buttonLayout = new TQHBoxLayout();
  buttonLayout->setSpacing( KDialog::spacingHint() );
  topLayout->addLayout( buttonLayout );

  TQLabel *abLabel = new TQLabel( i18n( "Address Books" ), this );
  buttonLayout->addWidget( abLabel );
  buttonLayout->addStretch( 1 );

  mAddButton = new TQPushButton( this );
  mAddButton->setIconSet( SmallIconSet( "add" ) );
  TQToolTip::add( mAddButton, i18n( "Add addressbook" ) );
  buttonLayout->addWidget( mAddButton );
  mEditButton = new TQPushButton( this );
  mEditButton->setIconSet( SmallIconSet( "edit" ) );
  mEditButton->setEnabled( false );
  TQToolTip::add( mEditButton, i18n( "Edit addressbook settings" ) );
  buttonLayout->addWidget( mEditButton );
  mRemoveButton = new TQPushButton( this );
  mRemoveButton->setIconSet( SmallIconSet( "remove" ) );
  mRemoveButton->setEnabled( false );
  TQToolTip::add( mRemoveButton, i18n( "Remove addressbook" ) );
  buttonLayout->addWidget( mRemoveButton );

  mListView = new TDEListView( 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, TQWidget *parent, const char *name )
    {
      return new ResourceSelection( core, parent, name );
    }

    TQString identifier() const
    {
      return "resourceselection";
    }
};

extern "C" {
  void *init_libkaddrbk_resourceselection()
  {
    return ( new ResourceSelectionFactory );
  }
}

#include "resourceselection.moc"