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