25 #include <tqpopupmenu.h>
26 #include <tqpushbutton.h>
30 #include <tqtooltip.h>
32 #include <tdeabc/resource.h>
34 #include <tdeglobal.h>
35 #include <kiconloader.h>
36 #include <kinputdialog.h>
37 #include <tdelocale.h>
38 #include <tdemessagebox.h>
39 #include <tderesources/configdialog.h>
43 #include "resourceselection.h"
44 #include <libtdepim/resourceabc.h>
46 class AddressBookWrapper :
public TDEABC::AddressBook
49 AddressBookWrapper( TDEABC::AddressBook* );
51 KRES::Manager<TDEABC::Resource>* getResourceManager()
53 return resourceManager();
57 class ResourceItem :
public TQCheckListItem
60 ResourceItem( TDEListView *parent, TDEABC::Resource *resource )
61 : TQCheckListItem( parent, resource->resourceName(), CheckBox ),
62 mResource( resource ), mChecked( false ),
63 mIsSubresource( false ), mSubItemsCreated( false ),
66 setOn( resource->isActive() );
67 setPixmap( 0, TDEGlobal::iconLoader()->loadIcon(
"contents", TDEIcon::Small ) );
71 ResourceItem( KPIM::ResourceABC *resourceABC, ResourceItem* parent,
72 const TQString& resourceIdent )
73 : TQCheckListItem( parent, resourceABC->subresourceLabel( resourceIdent ), CheckBox ),
74 mResource( resourceABC ), mChecked( false ),
75 mIsSubresource( true ), mSubItemsCreated( false ),
76 mResourceIdentifier( resourceIdent )
78 KPIM::ResourceABC* res =
dynamic_cast<KPIM::ResourceABC *
>( mResource );
79 setOn( res->subresourceActive( mResourceIdentifier ) );
80 setPixmap( 0, TDEGlobal::iconLoader()->loadIcon(
"contents", TDEIcon::Small ) );
84 void createSubresourceItems();
86 void setChecked(
bool state ) {
90 bool checked()
const {
return mChecked; }
91 TDEABC::Resource *resource()
const {
return mResource; }
92 TQString resourceIdentifier()
const {
return mResourceIdentifier; }
93 bool isSubResource()
const {
return mIsSubresource; }
95 virtual void stateChange(
bool active );
98 TDEABC::Resource *
const mResource;
100 const bool mIsSubresource;
101 bool mSubItemsCreated;
102 const TQString mResourceIdentifier;
106 void ResourceItem::createSubresourceItems()
108 KPIM::ResourceABC* res =
dynamic_cast<KPIM::ResourceABC *
>( mResource );
109 TQStringList subresources;
111 subresources = res->subresources();
112 if ( !subresources.isEmpty() ) {
114 setExpandable(
true );
116 TQStringList::ConstIterator it;
117 for ( it = subresources.begin(); it != subresources.end(); ++it ) {
118 (void)
new ResourceItem( res,
this, *it );
121 mSubItemsCreated =
true;
124 void ResourceItem::stateChange(
bool active )
127 if ( active && !mIsSubresource ) {
128 if ( !mSubItemsCreated )
129 createSubresourceItems();
132 setOpen( active && childCount() > 0 );
137 ResourceSelection::ResourceSelection( KAB::Core *core, TQWidget *parent,
const char *name )
138 : KAB::ExtensionWidget( core, parent, name ), mManager( 0 )
142 AddressBookWrapper *wrapper =
static_cast<AddressBookWrapper*
>( core->addressBook() );
143 mManager = wrapper->getResourceManager();
145 connect( mAddButton, TQ_SIGNAL( clicked() ), TQ_SLOT( add() ) );
146 connect( mEditButton, TQ_SIGNAL( clicked() ), TQ_SLOT( edit() ) );
147 connect( mRemoveButton, TQ_SIGNAL( clicked() ), TQ_SLOT( remove() ) );
149 connect( mListView, TQ_SIGNAL( clicked( TQListViewItem* ) ),
150 TQ_SLOT( currentChanged( TQListViewItem* ) ) );
152 connect( mListView, TQ_SIGNAL( contextMenuRequested ( TQListViewItem *,
153 const TQPoint &,
int ) ),
154 TQ_SLOT( contextMenuRequested( TQListViewItem *,
const TQPoint &,
157 TQTimer::singleShot( 0,
this, TQ_SLOT( updateView() ) );
160 ResourceSelection::~ResourceSelection()
164 void ResourceSelection::contextMenuRequested ( TQListViewItem *i,
165 const TQPoint &pos,
int )
167 ResourceItem *item =
static_cast<ResourceItem *
>( i );
169 TQPopupMenu *menu =
new TQPopupMenu(
this );
170 connect( menu, TQ_SIGNAL( aboutToHide() ), menu, TQ_SLOT( deleteLater() ) );
172 int reloadId = menu->insertItem( i18n(
"Re&load"),
this,
173 TQ_SLOT( reloadResource() ) );
174 menu->setItemEnabled( reloadId, item->resource()->isActive() );
175 int saveId = menu->insertItem( i18n(
"&Save"),
this,
176 TQ_SLOT( saveResource() ) );
177 menu->setItemEnabled( saveId, item->resource()->isActive() );
178 menu->insertSeparator();
182 menu->insertItem( i18n(
"&Edit..."),
this, TQ_SLOT( edit() ) );
183 menu->insertItem( i18n(
"&Remove"),
this, TQ_SLOT( remove() ) );
185 menu->insertSeparator();
187 menu->insertItem( i18n(
"&Add..."),
this, TQ_SLOT( add() ) );
192 void ResourceSelection::reloadResource()
194 ResourceItem *item = selectedItem();
197 TDEABC::Resource *r = item->resource();
201 void ResourceSelection::saveResource()
203 ResourceItem *item = selectedItem();
206 TDEABC::Resource *r = item->resource();
207 TDEABC::Ticket *ticket = core()->addressBook()->requestSaveTicket( r );
213 void ResourceSelection::showInfo()
215 ResourceItem *item = selectedItem();
222 TQString ResourceSelection::title()
const
224 return i18n(
"Address Books" );
227 TQString ResourceSelection::identifier()
const
229 return "resourceselection";
232 void ResourceSelection::add()
234 TQStringList types = mManager->resourceTypeNames();
235 TQStringList descs = mManager->resourceTypeDescriptions();
238 TQString desc = KInputDialog::getItem( i18n(
"Add Address Book" ),
239 i18n(
"Please select type of the new address book:" ),
240 descs, 0,
false, &ok,
this );
244 TQString type = types[ descs.findIndex( desc ) ];
247 TDEABC::Resource *resource = mManager->createResource( type );
249 KMessageBox::error(
this, i18n(
"<qt>Unable to create an address book of type <b>%1</b>.</qt>")
254 resource->setAddressBook(core()->addressBook());
256 KRES::ConfigDialog dlg(
this, TQString(
"contact" ), resource );
259 core()->addressBook()->addResource( resource );
260 resource->asyncLoad();
262 mLastResource = resource->identifier();
264 currentChanged(mListView->currentItem() );
271 void ResourceSelection::edit()
273 ResourceItem *item = selectedItem();
280 TDEABC::Resource *resource = item->resource();
282 KRES::ConfigDialog dlg(
this, TQString(
"contact" ), resource );
285 mManager->change( resource );
286 resource->asyncLoad();
288 mLastResource = resource->identifier();
293 void ResourceSelection::remove()
295 ResourceItem *item = selectedItem();
299 int result = KMessageBox::warningContinueCancel(
this,
300 i18n(
"<qt>Do you really want to remove the address book <b>%1</b>?</qt>" )
301 .arg( item->resource()->resourceName() ),
"",
302 KGuiItem( i18n(
"&Remove" ),
"edit-delete" ) );
303 if ( result == KMessageBox::Cancel )
306 mLastResource = item->resource()->identifier();
308 core()->addressBook()->removeResource( item->resource() );
309 core()->addressBook()->emitAddressBookChanged();
312 currentChanged(mListView->currentItem() );
315 void ResourceSelection::currentChanged( TQListViewItem *item )
317 ResourceItem *resItem =
static_cast<ResourceItem*
>( item );
318 bool state = (resItem && !resItem->isSubResource() );
320 mEditButton->setEnabled( state );
321 mRemoveButton->setEnabled( state );
326 TDEABC::Resource *resource = resItem->resource();
328 if ( resItem->checked() != resItem->isOn() ) {
329 resItem->setChecked( resItem->isOn() );
330 if ( resItem->isSubResource() ) {
331 KPIM::ResourceABC *res =
dynamic_cast<KPIM::ResourceABC *
>( resource );
332 res->setSubresourceActive( resItem->resourceIdentifier(), resItem->isOn() );
333 mManager->change( resource );
335 resource->setActive( resItem->isOn() );
336 mManager->change( resource );
338 if ( resItem->checked() ) {
339 if ( !resource->addressBook() )
340 resource->setAddressBook( core()->addressBook() );
342 if ( !resource->isOpen() )
345 resource->asyncLoad();
351 mLastResource = resource->identifier();
352 core()->addressBook()->emitAddressBookChanged();
357 void ResourceSelection::updateView()
364 KRES::Manager<TDEABC::Resource>::Iterator it;
365 for ( it = mManager->begin(); it != mManager->end(); ++it ) {
367 ResourceItem *item =
new ResourceItem( mListView, *it );
368 KPIM::ResourceABC* resource =
dynamic_cast<KPIM::ResourceABC *
>( *it );
370 disconnect( resource, 0,
this, 0 );
371 connect( resource, TQ_SIGNAL( signalSubresourceAdded( KPIM::ResourceABC *,
372 const TQString &,
const TQString & ) ),
373 TQ_SLOT( slotSubresourceAdded( KPIM::ResourceABC *,
374 const TQString &,
const TQString & ) ) );
376 connect( resource, TQ_SIGNAL( signalSubresourceRemoved( KPIM::ResourceABC *,
377 const TQString &,
const TQString & ) ),
378 TQ_SLOT( slotSubresourceRemoved( KPIM::ResourceABC *,
379 const TQString &,
const TQString & ) ) );
381 connect( resource, TQ_SIGNAL( signalSubresourceChanged( KPIM::ResourceABC *,
382 const TQString &,
const TQString & ) ),
383 TQ_SLOT( slotSubresourceChanged( KPIM::ResourceABC *,
384 const TQString &,
const TQString & ) ) );
388 item->createSubresourceItems();
392 TQListViewItemIterator itemIt( mListView );
393 while ( itemIt.current() ) {
394 ResourceItem *item =
static_cast<ResourceItem*
>( itemIt.current() );
395 if ( item->resource()->identifier() == mLastResource ) {
396 mListView->setSelected( item,
true );
397 mListView->ensureItemVisible( item );
403 core()->addressBook()->emitAddressBookChanged();
408 void ResourceSelection::slotSubresourceAdded( KPIM::ResourceABC *resource,
410 const TQString& subResource )
412 kdDebug(5720) << k_funcinfo << resource->resourceName() <<
" " << subResource << endl;
413 TQListViewItem *i = mListView->findItem( resource->resourceName(), 0 );
418 ResourceItem *item =
static_cast<ResourceItem *
>( i );
420 item->createSubresourceItems();
423 if ( !findSubResourceItem( resource, subResource ) ) {
424 (void)
new ResourceItem( resource, item, subResource );
429 void ResourceSelection::slotSubresourceRemoved( KPIM::ResourceABC* resource,
431 const TQString& subResource )
433 ResourceItem *item = findSubResourceItem( resource, subResource );
435 core()->addressBook()->emitAddressBookChanged();
440 void ResourceSelection::slotSubresourceChanged( KPIM::ResourceABC* resource,
441 const TQString& type,
442 const TQString& subResource )
444 kdDebug(5720) << resource->resourceName() << subResource;
446 ResourceItem *item = findSubResourceItem( resource, subResource );
448 kdWarning(5720) <<
"Changed before it was added?";
449 slotSubresourceAdded( resource, type, subResource );
453 item->setText( 0, resource->subresourceLabel( subResource ) );
454 item->setChecked( resource->subresourceActive( subResource ) ?
true :
false );
457 core()->addressBook()->emitAddressBookChanged();
461 ResourceItem* ResourceSelection::selectedItem()
const
463 return static_cast<ResourceItem*
>( mListView->selectedItem() );
466 ResourceItem* ResourceSelection::findSubResourceItem( KPIM::ResourceABC *resource,
467 const TQString &subResource )
469 TQListViewItemIterator parentIt( mListView );
470 for ( ; *parentIt; ++parentIt ) {
471 if (
static_cast<ResourceItem*
>(*parentIt)->resource() != resource )
474 TQListViewItemIterator childIt( *parentIt );
475 for ( ; *childIt; ++childIt ) {
476 ResourceItem *item =
static_cast<ResourceItem*
>(*childIt);
477 if ( item->resourceIdentifier() == subResource )
485 void ResourceSelection::initGUI()
487 TQBoxLayout *topLayout =
new TQVBoxLayout(
this );
488 topLayout->setSpacing( KDialog::spacingHint() );
490 TQBoxLayout *buttonLayout =
new TQHBoxLayout();
491 buttonLayout->setSpacing( KDialog::spacingHint() );
492 topLayout->addLayout( buttonLayout );
494 TQLabel *abLabel =
new TQLabel( i18n(
"Address Books" ),
this );
495 buttonLayout->addWidget( abLabel );
496 buttonLayout->addStretch( 1 );
498 mAddButton =
new TQPushButton(
this );
499 mAddButton->setIconSet( SmallIconSet(
"add" ) );
500 TQToolTip::add( mAddButton, i18n(
"Add addressbook" ) );
501 buttonLayout->addWidget( mAddButton );
502 mEditButton =
new TQPushButton(
this );
503 mEditButton->setIconSet( SmallIconSet(
"edit" ) );
504 mEditButton->setEnabled(
false );
505 TQToolTip::add( mEditButton, i18n(
"Edit addressbook settings" ) );
506 buttonLayout->addWidget( mEditButton );
507 mRemoveButton =
new TQPushButton(
this );
508 mRemoveButton->setIconSet( SmallIconSet(
"remove" ) );
509 mRemoveButton->setEnabled(
false );
510 TQToolTip::add( mRemoveButton, i18n(
"Remove addressbook" ) );
511 buttonLayout->addWidget( mRemoveButton );
513 mListView =
new TDEListView(
this );
514 mListView->header()->hide();
515 mListView->addColumn( i18n(
"Address Books" ) );
516 mListView->setFullWidth(
true );
517 topLayout->addWidget( mListView );
520 class ResourceSelectionFactory :
public KAB::ExtensionFactory
523 KAB::ExtensionWidget *extension( KAB::Core *core, TQWidget *parent,
const char *name )
525 return new ResourceSelection( core, parent, name );
528 TQString identifier()
const
530 return "resourceselection";
535 void *init_libkaddrbk_resourceselection()
537 return (
new ResourceSelectionFactory );
541 #include "resourceselection.moc"