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 
00197   if ( dlg.exec() ) {
00198     core()->addressBook()->addResource( resource );
00199     resource->asyncLoad();
00200 
00201     mLastResource = resource->identifier();
00202     updateView();
00203   } else {
00204     delete resource;
00205     resource = 0;
00206   }
00207 }
00208 
00209 void ResourceSelection::edit()
00210 {
00211   ResourceItem *item = selectedItem();
00212   if ( !item )
00213     return;
00214 
00215   KRES::ConfigDialog dlg( this, QString( "contact" ), item->resource() );
00216 
00217   if ( dlg.exec() ) {
00218     mManager->change( item->resource() );
00219     item->resource()->asyncLoad();
00220 
00221     mLastResource = item->resource()->identifier();
00222     updateView();
00223   }
00224 }
00225 
00226 void ResourceSelection::remove()
00227 {
00228   ResourceItem *item = selectedItem();
00229   if ( !item )
00230     return;
00231 
00232   int result = KMessageBox::warningContinueCancel( this,
00233         i18n( "<qt>Do you really want to remove the address book <b>%1</b>?</qt>" )
00234         .arg( item->resource()->resourceName() ), "",
00235         KGuiItem( i18n( "&Remove" ), "editdelete" ) );
00236   if ( result == KMessageBox::Cancel )
00237     return;
00238 
00239   mLastResource = item->resource()->identifier();
00240 
00241   core()->addressBook()->removeResource( item->resource() );
00242   core()->addressBook()->emitAddressBookChanged();
00243 
00244   updateView();
00245 }
00246 
00247 void ResourceSelection::currentChanged( QListViewItem *item )
00248 {
00249   ResourceItem *resItem = static_cast<ResourceItem*>( item );
00250   bool state = (resItem && !resItem->isSubResource() );
00251 
00252   mEditButton->setEnabled( state );
00253   mRemoveButton->setEnabled( state );
00254 
00255   if ( !resItem )
00256     return;
00257 
00258   KABC::Resource *resource = resItem->resource();
00259 
00260   if ( resItem->checked() != resItem->isOn() ) {
00261     resItem->setChecked( resItem->isOn() );
00262     if ( resItem->isSubResource() ) {
00263       KPIM::ResourceABC *res = dynamic_cast<KPIM::ResourceABC *>( resource );
00264       res->setSubresourceActive( resItem->resourceIdentifier(), resItem->isOn() );
00265       mManager->change( resource );
00266     } else {
00267       resource->setActive( resItem->isOn() );
00268       mManager->change( resource );
00269 
00270       if ( resItem->checked() ) {
00271         if ( !resource->addressBook() )
00272           resource->setAddressBook( core()->addressBook() );
00273 
00274         if ( !resource->isOpen() )
00275           resource->open();
00276 
00277         resource->asyncLoad();
00278       } else {
00279         resource->close();
00280       }
00281     }
00282 
00283     mLastResource = resource->identifier();
00284     core()->addressBook()->emitAddressBookChanged();
00285     //updateView();
00286   }
00287 }
00288 
00289 void ResourceSelection::updateView()
00290 {
00291   if ( !mManager )
00292     return;
00293 
00294   mListView->clear();
00295 
00296   KRES::Manager<KABC::Resource>::Iterator it;
00297   for ( it = mManager->begin(); it != mManager->end(); ++it ) {
00298 
00299     new ResourceItem( mListView, *it );
00300     KPIM::ResourceABC* resource = dynamic_cast<KPIM::ResourceABC *>( *it );
00301     if ( resource ) {
00302       disconnect( resource, 0, this, 0 );
00303       connect( resource, SIGNAL( signalSubresourceAdded( KPIM::ResourceABC *,
00304                                                          const QString &, const QString & ) ),
00305                SLOT( slotSubresourceAdded( KPIM::ResourceABC *,
00306                                            const QString &, const QString & ) ) );
00307 
00308       connect( resource, SIGNAL( signalSubresourceRemoved( KPIM::ResourceABC *,
00309                                                            const QString &, const QString & ) ),
00310                SLOT( slotSubresourceRemoved( KPIM::ResourceABC *,
00311                                              const QString &, const QString & ) ) );
00312       //connect( resource, SIGNAL( resourceSaved( KPIM::ResourceABC * ) ),
00313       //         SLOT( closeResource( KPIM::ResourceABC * ) ) );
00314     }
00315   }
00316 
00317   QListViewItemIterator itemIt( mListView );
00318   while ( itemIt.current() ) {
00319     ResourceItem *item = static_cast<ResourceItem*>( itemIt.current() );
00320     if ( item->resource()->identifier() == mLastResource ) {
00321       mListView->setSelected( item, true );
00322       mListView->ensureItemVisible( item );
00323       break;
00324     }
00325     ++itemIt;
00326   }
00327 
00328   core()->addressBook()->emitAddressBookChanged();
00329 }
00330 
00331 
00332 // Add a new entry
00333 void ResourceSelection::slotSubresourceAdded( KPIM::ResourceABC *resource,
00334                                               const QString& /*type*/,
00335                                               const QString& subResource )
00336 {
00337   kdDebug(5720) << k_funcinfo << resource->resourceName() << " " << subResource << endl;
00338   QListViewItem *i = mListView->findItem( resource->resourceName(), 0 );
00339   if ( !i )
00340     // Not found
00341     return;
00342 
00343   ResourceItem *item = static_cast<ResourceItem *>( i );
00344   (void)new ResourceItem( resource, item, subResource );
00345 }
00346 
00347 // Remove an entry
00348 void ResourceSelection::slotSubresourceRemoved( KPIM::ResourceABC* resource,
00349                                                 const QString& /*type*/,
00350                                                 const QString& subResource )
00351 {
00352   kdDebug(5720) << k_funcinfo << resource->resourceName() << " " << subResource << endl;
00353   // TODO
00354   //delete findItemByIdentifier( resource );
00355   //emitResourcesChanged();
00356 }
00357 
00358 ResourceItem* ResourceSelection::selectedItem() const
00359 {
00360   return static_cast<ResourceItem*>( mListView->selectedItem() );
00361 }
00362 
00363 void ResourceSelection::initGUI()
00364 {
00365   QGridLayout *layout = new QGridLayout( this, 2, 3, 2, 5 );
00366 
00367   mListView = new KListView( this );
00368   mListView->addColumn( i18n( "Address Books" ) );
00369   mListView->setFullWidth( true );
00370   layout->addMultiCellWidget( mListView, 0, 0, 0, 2 );
00371 
00372   mAddButton = new QPushButton( i18n( "Add..." ), this );
00373   mEditButton = new QPushButton( i18n( "Edit..." ), this );
00374   mEditButton->setEnabled( false );
00375   mRemoveButton = new QPushButton( i18n( "Remove" ), this );
00376   mRemoveButton->setEnabled( false );
00377 
00378   layout->addWidget( mAddButton, 1, 0 );
00379   layout->addWidget( mEditButton, 1, 1 );
00380   layout->addWidget( mRemoveButton, 1, 2 );
00381 }
00382 
00383 class ResourceSelectionFactory : public KAB::ExtensionFactory
00384 {
00385   public:
00386     KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
00387     {
00388       return new ResourceSelection( core, parent, name );
00389     }
00390 
00391     QString identifier() const
00392     {
00393       return "resourceselection";
00394     }
00395 };
00396 
00397 extern "C" {
00398   void *init_libkaddrbk_resourceselection()
00399   {
00400     return ( new ResourceSelectionFactory );
00401   }
00402 }
00403 
00404 #include "resourceselection.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys