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 
00201   if ( dlg.exec() ) {
00202     core()->addressBook()->addResource( resource );
00203     resource->asyncLoad();
00204 
00205     mLastResource = resource->identifier();
00206     updateView();
00207     currentChanged(mListView->currentItem() );
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   currentChanged(mListView->currentItem() );
00251 }
00252 
00253 void ResourceSelection::currentChanged( QListViewItem *item )
00254 {
00255   ResourceItem *resItem = static_cast<ResourceItem*>( item );
00256   bool state = (resItem && !resItem->isSubResource() );
00257 
00258   mEditButton->setEnabled( state );
00259   mRemoveButton->setEnabled( state );
00260 
00261   if ( !resItem )
00262     return;
00263 
00264   KABC::Resource *resource = resItem->resource();
00265 
00266   if ( resItem->checked() != resItem->isOn() ) {
00267     resItem->setChecked( resItem->isOn() );
00268     if ( resItem->isSubResource() ) {
00269       KPIM::ResourceABC *res = dynamic_cast<KPIM::ResourceABC *>( resource );
00270       res->setSubresourceActive( resItem->resourceIdentifier(), resItem->isOn() );
00271       mManager->change( resource );
00272     } else {
00273       resource->setActive( resItem->isOn() );
00274       mManager->change( resource );
00275 
00276       if ( resItem->checked() ) {
00277         if ( !resource->addressBook() )
00278           resource->setAddressBook( core()->addressBook() );
00279 
00280         if ( !resource->isOpen() )
00281           resource->open();
00282 
00283         resource->asyncLoad();
00284       } else {
00285         resource->close();
00286       }
00287     }
00288 
00289     mLastResource = resource->identifier();
00290     core()->addressBook()->emitAddressBookChanged();
00291     //updateView();
00292   }
00293 }
00294 
00295 void ResourceSelection::updateView()
00296 {
00297   if ( !mManager )
00298     return;
00299 
00300   mListView->clear();
00301 
00302   KRES::Manager<KABC::Resource>::Iterator it;
00303   for ( it = mManager->begin(); it != mManager->end(); ++it ) {
00304 
00305     new ResourceItem( mListView, *it );
00306     KPIM::ResourceABC* resource = dynamic_cast<KPIM::ResourceABC *>( *it );
00307     if ( resource ) {
00308       disconnect( resource, 0, this, 0 );
00309       connect( resource, SIGNAL( signalSubresourceAdded( KPIM::ResourceABC *,
00310                                                          const QString &, const QString & ) ),
00311                SLOT( slotSubresourceAdded( KPIM::ResourceABC *,
00312                                            const QString &, const QString & ) ) );
00313 
00314       connect( resource, SIGNAL( signalSubresourceRemoved( KPIM::ResourceABC *,
00315                                                            const QString &, const QString & ) ),
00316                SLOT( slotSubresourceRemoved( KPIM::ResourceABC *,
00317                                              const QString &, const QString & ) ) );
00318       //connect( resource, SIGNAL( resourceSaved( KPIM::ResourceABC * ) ),
00319       //         SLOT( closeResource( KPIM::ResourceABC * ) ) );
00320     }
00321   }
00322 
00323   QListViewItemIterator itemIt( mListView );
00324   while ( itemIt.current() ) {
00325     ResourceItem *item = static_cast<ResourceItem*>( itemIt.current() );
00326     if ( item->resource()->identifier() == mLastResource ) {
00327       mListView->setSelected( item, true );
00328       mListView->ensureItemVisible( item );
00329       break;
00330     }
00331     ++itemIt;
00332   }
00333 
00334   core()->addressBook()->emitAddressBookChanged();
00335 }
00336 
00337 
00338 // Add a new entry
00339 void ResourceSelection::slotSubresourceAdded( KPIM::ResourceABC *resource,
00340                                               const QString& /*type*/,
00341                                               const QString& subResource )
00342 {
00343   kdDebug(5720) << k_funcinfo << resource->resourceName() << " " << subResource << endl;
00344   QListViewItem *i = mListView->findItem( resource->resourceName(), 0 );
00345   if ( !i )
00346     // Not found
00347     return;
00348 
00349   ResourceItem *item = static_cast<ResourceItem *>( i );
00350   (void)new ResourceItem( resource, item, subResource );
00351 }
00352 
00353 // Remove an entry
00354 void ResourceSelection::slotSubresourceRemoved( KPIM::ResourceABC* resource,
00355                                                 const QString& /*type*/,
00356                                                 const QString& subResource )
00357 {
00358   core()->addressBook()->emitAddressBookChanged();
00359   updateView();
00360 }
00361 
00362 ResourceItem* ResourceSelection::selectedItem() const
00363 {
00364   return static_cast<ResourceItem*>( mListView->selectedItem() );
00365 }
00366 
00367 void ResourceSelection::initGUI()
00368 {
00369   QBoxLayout *topLayout = new QVBoxLayout( this );
00370   topLayout->setSpacing( KDialog::spacingHint() );
00371 
00372   QBoxLayout *buttonLayout = new QHBoxLayout();
00373   buttonLayout->setSpacing( KDialog::spacingHint() );
00374   topLayout->addLayout( buttonLayout );
00375 
00376   QLabel *abLabel = new QLabel( i18n( "Address Books" ), this );
00377   buttonLayout->addWidget( abLabel );
00378   buttonLayout->addStretch( 1 );
00379 
00380   mAddButton = new QPushButton( this );
00381   mAddButton->setIconSet( SmallIconSet( "add" ) );
00382   QToolTip::add( mAddButton, i18n( "Add addressbook" ) );
00383   buttonLayout->addWidget( mAddButton );
00384   mEditButton = new QPushButton( this );
00385   mEditButton->setIconSet( SmallIconSet( "edit" ) );
00386   mEditButton->setEnabled( false );
00387   QToolTip::add( mEditButton, i18n( "Edit addressbook settings" ) );
00388   buttonLayout->addWidget( mEditButton );
00389   mRemoveButton = new QPushButton( this );
00390   mRemoveButton->setIconSet( SmallIconSet( "remove" ) );
00391   mRemoveButton->setEnabled( false );
00392   QToolTip::add( mRemoveButton, i18n( "Remove addressbook" ) );
00393   buttonLayout->addWidget( mRemoveButton );
00394 
00395   mListView = new KListView( this );
00396   mListView->header()->hide();
00397   mListView->addColumn( i18n( "Address Books" ) );
00398   mListView->setFullWidth( true );
00399   topLayout->addWidget( mListView );
00400 }
00401 
00402 class ResourceSelectionFactory : public KAB::ExtensionFactory
00403 {
00404   public:
00405     KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
00406     {
00407       return new ResourceSelection( core, parent, name );
00408     }
00409 
00410     QString identifier() const
00411     {
00412       return "resourceselection";
00413     }
00414 };
00415 
00416 extern "C" {
00417   void *init_libkaddrbk_resourceselection()
00418   {
00419     return ( new ResourceSelectionFactory );
00420   }
00421 }
00422 
00423 #include "resourceselection.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys