00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qlayout.h>
00025 #include <qpushbutton.h>
00026 #include <qtimer.h>
00027 #include <qlabel.h>
00028 #include <qheader.h>
00029 #include <qtooltip.h>
00030
00031 #include <kabc/resource.h>
00032 #include <kdialog.h>
00033 #include <kglobal.h>
00034 #include <kiconloader.h>
00035 #include <kinputdialog.h>
00036 #include <klocale.h>
00037 #include <kmessagebox.h>
00038 #include <kresources/configdialog.h>
00039
00040 #include "core.h"
00041
00042 #include "resourceselection.h"
00043 #include <libkdepim/resourceabc.h>
00044
00045 class AddressBookWrapper : public KABC::AddressBook
00046 {
00047 public:
00048 AddressBookWrapper( KABC::AddressBook* );
00049
00050 KRES::Manager<KABC::Resource>* getResourceManager()
00051 {
00052 return resourceManager();
00053 }
00054 };
00055
00056 class ResourceItem : public QCheckListItem
00057 {
00058 public:
00059 ResourceItem( KListView *parent, KABC::Resource *resource )
00060 : QCheckListItem( parent, resource->resourceName(), CheckBox ),
00061 mResource( resource ), mChecked( false ),
00062 mIsSubresource( false ), mSubItemsCreated( false ),
00063 mResourceIdentifier()
00064 {
00065 setOn( resource->isActive() );
00066 setPixmap( 0, KGlobal::iconLoader()->loadIcon( "contents", KIcon::Small ) );
00067 mChecked = isOn();
00068 }
00069
00070 ResourceItem( KPIM::ResourceABC *resourceABC, ResourceItem* parent,
00071 const QString& resourceIdent )
00072 : QCheckListItem( parent, resourceABC->subresourceLabel( resourceIdent ), CheckBox ),
00073 mResource( resourceABC ), mChecked( false ),
00074 mIsSubresource( true ), mSubItemsCreated( false ),
00075 mResourceIdentifier( resourceIdent )
00076 {
00077 KPIM::ResourceABC* res = dynamic_cast<KPIM::ResourceABC *>( mResource );
00078 setOn( res->subresourceActive( mResourceIdentifier ) );
00079 setPixmap( 0, KGlobal::iconLoader()->loadIcon( "contents", KIcon::Small ) );
00080 mChecked = isOn();
00081 }
00082
00083 void createSubresourceItems();
00084
00085 void setChecked( bool state ) { mChecked = state; }
00086 bool checked() const { return mChecked; }
00087 KABC::Resource *resource() const { return mResource; }
00088 QString resourceIdentifier() const { return mResourceIdentifier; }
00089 bool isSubResource() const { return mIsSubresource; }
00090
00091 virtual void stateChange( bool active );
00092
00093 private:
00094 KABC::Resource * const mResource;
00095 bool mChecked;
00096 const bool mIsSubresource;
00097 bool mSubItemsCreated;
00098 const QString mResourceIdentifier;
00099 };
00100
00101
00102 void ResourceItem::createSubresourceItems()
00103 {
00104 KPIM::ResourceABC* res = dynamic_cast<KPIM::ResourceABC *>( mResource );
00105 QStringList subresources;
00106 if ( res )
00107 subresources = res->subresources();
00108 if ( !subresources.isEmpty() ) {
00109 setOpen( true );
00110 setExpandable( true );
00111
00112 QStringList::ConstIterator it;
00113 for ( it = subresources.begin(); it != subresources.end(); ++it ) {
00114 (void)new ResourceItem( res, this, *it );
00115 }
00116 }
00117 mSubItemsCreated = true;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 void ResourceItem::stateChange( bool active )
00130 {
00131
00132 if ( active && !mIsSubresource ) {
00133 if ( !mSubItemsCreated )
00134 createSubresourceItems();
00135 }
00136
00137 setOpen( active && childCount() > 0 );
00138 }
00139
00141
00142 ResourceSelection::ResourceSelection( KAB::Core *core, QWidget *parent, const char *name )
00143 : KAB::ExtensionWidget( core, parent, name ), mManager( 0 )
00144 {
00145 initGUI();
00146
00147 AddressBookWrapper *wrapper = static_cast<AddressBookWrapper*>( core->addressBook() );
00148 mManager = wrapper->getResourceManager();
00149
00150 connect( mAddButton, SIGNAL( clicked() ), SLOT( add() ) );
00151 connect( mEditButton, SIGNAL( clicked() ), SLOT( edit() ) );
00152 connect( mRemoveButton, SIGNAL( clicked() ), SLOT( remove() ) );
00153
00154 connect( mListView, SIGNAL( clicked( QListViewItem* ) ),
00155 SLOT( currentChanged( QListViewItem* ) ) );
00156
00157 QTimer::singleShot( 0, this, SLOT( updateView() ) );
00158 }
00159
00160 ResourceSelection::~ResourceSelection()
00161 {
00162 }
00163
00164 QString ResourceSelection::title() const
00165 {
00166 return i18n( "Address Books" );
00167 }
00168
00169 QString ResourceSelection::identifier() const
00170 {
00171 return "resourceselection";
00172 }
00173
00174 void ResourceSelection::add()
00175 {
00176 QStringList types = mManager->resourceTypeNames();
00177 QStringList descs = mManager->resourceTypeDescriptions();
00178
00179 bool ok = false;
00180 QString desc = KInputDialog::getItem( i18n( "Add Address Book" ),
00181 i18n( "Please select type of the new address book:" ),
00182 descs, 0, false, &ok, this );
00183 if ( !ok )
00184 return;
00185
00186 QString type = types[ descs.findIndex( desc ) ];
00187
00188
00189 KABC::Resource *resource = mManager->createResource( type );
00190 if ( !resource ) {
00191 KMessageBox::error( this, i18n("<qt>Unable to create an address book of type <b>%1</b>.</qt>")
00192 .arg( type ) );
00193 return;
00194 }
00195
00196 resource->setResourceName( i18n( "%1 address book" ).arg( type ) );
00197 resource->setAddressBook(core()->addressBook());
00198
00199 KRES::ConfigDialog dlg( this, QString( "contact" ), resource );
00200
00201 if ( dlg.exec() ) {
00202 core()->addressBook()->addResource( resource );
00203 resource->asyncLoad();
00204
00205 mLastResource = resource->identifier();
00206 updateView();
00207 } else {
00208 delete resource;
00209 resource = 0;
00210 }
00211 }
00212
00213 void ResourceSelection::edit()
00214 {
00215 ResourceItem *item = selectedItem();
00216 if ( !item )
00217 return;
00218
00219 KRES::ConfigDialog dlg( this, QString( "contact" ), item->resource() );
00220
00221 if ( dlg.exec() ) {
00222 mManager->change( item->resource() );
00223 item->resource()->asyncLoad();
00224
00225 mLastResource = item->resource()->identifier();
00226 updateView();
00227 }
00228 }
00229
00230 void ResourceSelection::remove()
00231 {
00232 ResourceItem *item = selectedItem();
00233 if ( !item )
00234 return;
00235
00236 int result = KMessageBox::warningContinueCancel( this,
00237 i18n( "<qt>Do you really want to remove the address book <b>%1</b>?</qt>" )
00238 .arg( item->resource()->resourceName() ), "",
00239 KGuiItem( i18n( "&Remove" ), "editdelete" ) );
00240 if ( result == KMessageBox::Cancel )
00241 return;
00242
00243 mLastResource = item->resource()->identifier();
00244
00245 core()->addressBook()->removeResource( item->resource() );
00246 core()->addressBook()->emitAddressBookChanged();
00247
00248 updateView();
00249 }
00250
00251 void ResourceSelection::currentChanged( QListViewItem *item )
00252 {
00253 ResourceItem *resItem = static_cast<ResourceItem*>( item );
00254 bool state = (resItem && !resItem->isSubResource() );
00255
00256 mEditButton->setEnabled( state );
00257 mRemoveButton->setEnabled( state );
00258
00259 if ( !resItem )
00260 return;
00261
00262 KABC::Resource *resource = resItem->resource();
00263
00264 if ( resItem->checked() != resItem->isOn() ) {
00265 resItem->setChecked( resItem->isOn() );
00266 if ( resItem->isSubResource() ) {
00267 KPIM::ResourceABC *res = dynamic_cast<KPIM::ResourceABC *>( resource );
00268 res->setSubresourceActive( resItem->resourceIdentifier(), resItem->isOn() );
00269 mManager->change( resource );
00270 } else {
00271 resource->setActive( resItem->isOn() );
00272 mManager->change( resource );
00273
00274 if ( resItem->checked() ) {
00275 if ( !resource->addressBook() )
00276 resource->setAddressBook( core()->addressBook() );
00277
00278 if ( !resource->isOpen() )
00279 resource->open();
00280
00281 resource->asyncLoad();
00282 } else {
00283 resource->close();
00284 }
00285 }
00286
00287 mLastResource = resource->identifier();
00288 core()->addressBook()->emitAddressBookChanged();
00289
00290 }
00291 }
00292
00293 void ResourceSelection::updateView()
00294 {
00295 if ( !mManager )
00296 return;
00297
00298 mListView->clear();
00299
00300 KRES::Manager<KABC::Resource>::Iterator it;
00301 for ( it = mManager->begin(); it != mManager->end(); ++it ) {
00302
00303 new ResourceItem( mListView, *it );
00304 KPIM::ResourceABC* resource = dynamic_cast<KPIM::ResourceABC *>( *it );
00305 if ( resource ) {
00306 disconnect( resource, 0, this, 0 );
00307 connect( resource, SIGNAL( signalSubresourceAdded( KPIM::ResourceABC *,
00308 const QString &, const QString & ) ),
00309 SLOT( slotSubresourceAdded( KPIM::ResourceABC *,
00310 const QString &, const QString & ) ) );
00311
00312 connect( resource, SIGNAL( signalSubresourceRemoved( KPIM::ResourceABC *,
00313 const QString &, const QString & ) ),
00314 SLOT( slotSubresourceRemoved( KPIM::ResourceABC *,
00315 const QString &, const QString & ) ) );
00316
00317
00318 }
00319 }
00320
00321 QListViewItemIterator itemIt( mListView );
00322 while ( itemIt.current() ) {
00323 ResourceItem *item = static_cast<ResourceItem*>( itemIt.current() );
00324 if ( item->resource()->identifier() == mLastResource ) {
00325 mListView->setSelected( item, true );
00326 mListView->ensureItemVisible( item );
00327 break;
00328 }
00329 ++itemIt;
00330 }
00331
00332 core()->addressBook()->emitAddressBookChanged();
00333 }
00334
00335
00336
00337 void ResourceSelection::slotSubresourceAdded( KPIM::ResourceABC *resource,
00338 const QString& ,
00339 const QString& subResource )
00340 {
00341 kdDebug(5720) << k_funcinfo << resource->resourceName() << " " << subResource << endl;
00342 QListViewItem *i = mListView->findItem( resource->resourceName(), 0 );
00343 if ( !i )
00344
00345 return;
00346
00347 ResourceItem *item = static_cast<ResourceItem *>( i );
00348 (void)new ResourceItem( resource, item, subResource );
00349 }
00350
00351
00352 void ResourceSelection::slotSubresourceRemoved( KPIM::ResourceABC* resource,
00353 const QString& ,
00354 const QString& subResource )
00355 {
00356 core()->addressBook()->emitAddressBookChanged();
00357 updateView();
00358 }
00359
00360 ResourceItem* ResourceSelection::selectedItem() const
00361 {
00362 return static_cast<ResourceItem*>( mListView->selectedItem() );
00363 }
00364
00365 void ResourceSelection::initGUI()
00366 {
00367 QBoxLayout *topLayout = new QVBoxLayout( this );
00368 topLayout->setSpacing( KDialog::spacingHint() );
00369
00370 QBoxLayout *buttonLayout = new QHBoxLayout();
00371 buttonLayout->setSpacing( KDialog::spacingHint() );
00372 topLayout->addLayout( buttonLayout );
00373
00374 QLabel *abLabel = new QLabel( i18n( "Address Books" ), this );
00375 buttonLayout->addWidget( abLabel );
00376 buttonLayout->addStretch( 1 );
00377
00378 mAddButton = new QPushButton( this );
00379 mAddButton->setIconSet( SmallIconSet( "add" ) );
00380 QToolTip::add( mAddButton, i18n( "Add addressbook" ) );
00381 buttonLayout->addWidget( mAddButton );
00382 mEditButton = new QPushButton( this );
00383 mEditButton->setIconSet( SmallIconSet( "edit" ) );
00384 mEditButton->setEnabled( false );
00385 QToolTip::add( mEditButton, i18n( "Edit addressbook settings" ) );
00386 buttonLayout->addWidget( mEditButton );
00387 mRemoveButton = new QPushButton( this );
00388 mRemoveButton->setIconSet( SmallIconSet( "remove" ) );
00389 mRemoveButton->setEnabled( false );
00390 QToolTip::add( mRemoveButton, i18n( "Remove addressbook" ) );
00391 buttonLayout->addWidget( mRemoveButton );
00392
00393 mListView = new KListView( this );
00394 mListView->header()->hide();
00395 mListView->addColumn( i18n( "Address Books" ) );
00396 mListView->setFullWidth( true );
00397 topLayout->addWidget( mListView );
00398 }
00399
00400 class ResourceSelectionFactory : public KAB::ExtensionFactory
00401 {
00402 public:
00403 KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
00404 {
00405 return new ResourceSelection( core, parent, name );
00406 }
00407
00408 QString identifier() const
00409 {
00410 return "resourceselection";
00411 }
00412 };
00413
00414 extern "C" {
00415 void *init_libkaddrbk_resourceselection()
00416 {
00417 return ( new ResourceSelectionFactory );
00418 }
00419 }
00420
00421 #include "resourceselection.moc"