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   } 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     //updateView();
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       //connect( resource, SIGNAL( resourceSaved( KPIM::ResourceABC * ) ),
00317       //         SLOT( closeResource( KPIM::ResourceABC * ) ) );
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 // Add a new entry
00337 void ResourceSelection::slotSubresourceAdded( KPIM::ResourceABC *resource,
00338                                               const QString& /*type*/,
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     // Not found
00345     return;
00346 
00347   ResourceItem *item = static_cast<ResourceItem *>( i );
00348   (void)new ResourceItem( resource, item, subResource );
00349 }
00350 
00351 // Remove an entry
00352 void ResourceSelection::slotSubresourceRemoved( KPIM::ResourceABC* resource,
00353                                                 const QString& /*type*/,
00354                                                 const QString& subResource )
00355 {
00356   kdDebug(5720) << k_funcinfo << resource->resourceName() << " " << subResource << endl;
00357   // TODO
00358   //delete findItemByIdentifier( resource );
00359   //emitResourcesChanged();
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