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