kaddressbook

resourceselection.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
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 // Comes from korganizer/resourceview.cpp
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     // This resource has subresources
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 // TODO: connect this to some signalResourceModified
00121 // void ResourceItem::setGuiState()
00122 // {
00123 //   if ( mIsSubresource )
00124 //     setOn( mResource->subresourceActive( mResourceIdentifier ) );
00125 //   else
00126 //     setOn( mResource->isActive() );
00127 // }
00128 
00129 void ResourceItem::stateChange( bool active )
00130 {
00131   //kdDebug(5720) << k_funcinfo << this << " " << text( 0 ) << " active=" << active << endl;
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   // Create new resource
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   resource->setAddressBook( core()->addressBook() );
00201 
00202   if ( dlg.exec() ) {
00203     core()->addressBook()->addResource( resource );
00204     resource->asyncLoad();
00205 
00206     mLastResource = resource->identifier();
00207     updateView();
00208   } else {
00209     delete resource;
00210     resource = 0;
00211   }
00212 }
00213 
00214 void ResourceSelection::edit()
00215 {
00216   ResourceItem *item = selectedItem();
00217   if ( !item )
00218     return;
00219 
00220   KRES::ConfigDialog dlg( this, QString( "contact" ), item->resource() );
00221 
00222   if ( dlg.exec() ) {
00223     mManager->change( item->resource() );
00224     item->resource()->asyncLoad();
00225 
00226     mLastResource = item->resource()->identifier();
00227     updateView();
00228   }
00229 }
00230 
00231 void ResourceSelection::remove()
00232 {
00233   ResourceItem *item = selectedItem();
00234   if ( !item )
00235     return;
00236 
00237   int result = KMessageBox::warningContinueCancel( this,
00238         i18n( "<qt>Do you really want to remove the address book <b>%1</b>?</qt>" )
00239         .arg( item->resource()->resourceName() ), "",
00240         KGuiItem( i18n( "&Remove" ), "editdelete" ) );
00241   if ( result == KMessageBox::Cancel )
00242     return;
00243 
00244   mLastResource = item->resource()->identifier();
00245 
00246   core()->addressBook()->removeResource( item->resource() );
00247   core()->addressBook()->emitAddressBookChanged();
00248 
00249   updateView();
00250 }
00251 
00252 void ResourceSelection::currentChanged( QListViewItem *item )
00253 {
00254   ResourceItem *resItem = static_cast<ResourceItem*>( item );
00255   bool state = (resItem && !resItem->isSubResource() );
00256 
00257   mEditButton->setEnabled( state );
00258   mRemoveButton->setEnabled( state );
00259 
00260   if ( !resItem )
00261     return;
00262 
00263   KABC::Resource *resource = resItem->resource();
00264 
00265   if ( resItem->checked() != resItem->isOn() ) {
00266     resItem->setChecked( resItem->isOn() );
00267     if ( resItem->isSubResource() ) {
00268       KPIM::ResourceABC *res = dynamic_cast<KPIM::ResourceABC *>( resource );
00269       res->setSubresourceActive( resItem->resourceIdentifier(), resItem->isOn() );
00270       mManager->change( resource );
00271     } else {
00272       resource->setActive( resItem->isOn() );
00273       mManager->change( resource );
00274 
00275       if ( resItem->checked() ) {
00276         if ( !resource->addressBook() )
00277           resource->setAddressBook( core()->addressBook() );
00278 
00279         if ( !resource->isOpen() )
00280           resource->open();
00281 
00282         resource->asyncLoad();
00283       } else {
00284         resource->close();
00285       }
00286     }
00287 
00288     mLastResource = resource->identifier();
00289     core()->addressBook()->emitAddressBookChanged();
00290     //updateView();
00291   }
00292 }
00293 
00294 void ResourceSelection::updateView()
00295 {
00296   if ( !mManager )
00297     return;
00298 
00299   mListView->clear();
00300 
00301   KRES::Manager<KABC::Resource>::Iterator it;
00302   for ( it = mManager->begin(); it != mManager->end(); ++it ) {
00303 
00304     new ResourceItem( mListView, *it );
00305     KPIM::ResourceABC* resource = dynamic_cast<KPIM::ResourceABC *>( *it );
00306     if ( resource ) {
00307       disconnect( resource, 0, this, 0 );
00308       connect( resource, SIGNAL( signalSubresourceAdded( KPIM::ResourceABC *,
00309                                                          const QString &, const QString & ) ),
00310                SLOT( slotSubresourceAdded( KPIM::ResourceABC *,
00311                                            const QString &, const QString & ) ) );
00312 
00313       connect( resource, SIGNAL( signalSubresourceRemoved( KPIM::ResourceABC *,
00314                                                            const QString &, const QString & ) ),
00315                SLOT( slotSubresourceRemoved( KPIM::ResourceABC *,
00316                                              const QString &, const QString & ) ) );
00317       //connect( resource, SIGNAL( resourceSaved( KPIM::ResourceABC * ) ),
00318       //         SLOT( closeResource( KPIM::ResourceABC * ) ) );
00319     }
00320   }
00321 
00322   QListViewItemIterator itemIt( mListView );
00323   while ( itemIt.current() ) {
00324     ResourceItem *item = static_cast<ResourceItem*>( itemIt.current() );
00325     if ( item->resource()->identifier() == mLastResource ) {
00326       mListView->setSelected( item, true );
00327       mListView->ensureItemVisible( item );
00328       break;
00329     }
00330     ++itemIt;
00331   }
00332 
00333   core()->addressBook()->emitAddressBookChanged();
00334 }
00335 
00336 
00337 // Add a new entry
00338 void ResourceSelection::slotSubresourceAdded( KPIM::ResourceABC *resource,
00339                                               const QString& /*type*/,
00340                                               const QString& subResource )
00341 {
00342   kdDebug(5720) << k_funcinfo << resource->resourceName() << " " << subResource << endl;
00343   QListViewItem *i = mListView->findItem( resource->resourceName(), 0 );
00344   if ( !i )
00345     // Not found
00346     return;
00347 
00348   ResourceItem *item = static_cast<ResourceItem *>( i );
00349   (void)new ResourceItem( resource, item, subResource );
00350 }
00351 
00352 // Remove an entry
00353 void ResourceSelection::slotSubresourceRemoved( KPIM::ResourceABC* resource,
00354                                                 const QString& /*type*/,
00355                                                 const QString& subResource )
00356 {
00357   core()->addressBook()->emitAddressBookChanged();
00358   updateView();
00359 }
00360 
00361 ResourceItem* ResourceSelection::selectedItem() const
00362 {
00363   return static_cast<ResourceItem*>( mListView->selectedItem() );
00364 }
00365 
00366 void ResourceSelection::initGUI()
00367 {
00368   QBoxLayout *topLayout = new QVBoxLayout( this );
00369   topLayout->setSpacing( KDialog::spacingHint() );
00370 
00371   QBoxLayout *buttonLayout = new QHBoxLayout();
00372   buttonLayout->setSpacing( KDialog::spacingHint() );
00373   topLayout->addLayout( buttonLayout );
00374 
00375   QLabel *abLabel = new QLabel( i18n( "Address Books" ), this );
00376   buttonLayout->addWidget( abLabel );
00377   buttonLayout->addStretch( 1 );
00378 
00379   mAddButton = new QPushButton( this );
00380   mAddButton->setIconSet( SmallIconSet( "add" ) );
00381   QToolTip::add( mAddButton, i18n( "Add addressbook" ) );
00382   buttonLayout->addWidget( mAddButton );
00383   mEditButton = new QPushButton( this );
00384   mEditButton->setIconSet( SmallIconSet( "edit" ) );
00385   mEditButton->setEnabled( false );
00386   QToolTip::add( mEditButton, i18n( "Edit addressbook settings" ) );
00387   buttonLayout->addWidget( mEditButton );
00388   mRemoveButton = new QPushButton( this );
00389   mRemoveButton->setIconSet( SmallIconSet( "remove" ) );
00390   mRemoveButton->setEnabled( false );
00391   QToolTip::add( mRemoveButton, i18n( "Remove addressbook" ) );
00392   buttonLayout->addWidget( mRemoveButton );
00393 
00394   mListView = new KListView( this );
00395   mListView->header()->hide();
00396   mListView->addColumn( i18n( "Address Books" ) );
00397   mListView->setFullWidth( true );
00398   topLayout->addWidget( mListView );
00399 }
00400 
00401 class ResourceSelectionFactory : public KAB::ExtensionFactory
00402 {
00403   public:
00404     KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
00405     {
00406       return new ResourceSelection( core, parent, name );
00407     }
00408 
00409     QString identifier() const
00410     {
00411       return "resourceselection";
00412     }
00413 };
00414 
00415 extern "C" {
00416   void *init_libkaddrbk_resourceselection()
00417   {
00418     return ( new ResourceSelectionFactory );
00419   }
00420 }
00421 
00422 #include "resourceselection.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys