# kabc Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kcmkabc.cpp

    00001 /*
    00002     This file is part of libkabc.
    00003     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library 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 GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public License
    00016     along with this library; see the file COPYING.LIB.  If not, write to
    00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include <qgroupbox.h>
    00022 #include <qinputdialog.h>
    00023 #include <qlabel.h>
    00024 #include <qlayout.h>
    00025
    00026 #include <kapplication.h>
    00027 #include <kcombobox.h>
    00028 #include <kdebug.h>
    00029 #include <klocale.h>
    00030 #include <kmessagebox.h>
    00031 #include <ksimpleconfig.h>
    00032 #include <kstandarddirs.h>
    00033 #include <kurlrequester.h>
    00034 #include <kaboutdata.h>
    00035
    00036 #include "kcmkabc.h"
    00037
    00038 #include "resource.h"
    00039 #include "resourceconfigdlg.h"
    00040 #include "resourcefactory.h"
    00041 #include "stdaddressbook.h"
    00042
    00043 ConfigViewItem::ConfigViewItem( QListView *parent, QString name,
    00044     QString type, QString )
    00045   : QObject( 0, "" ), QCheckListItem( parent, name, CheckBox )
    00046 {
    00047   isStandard = false;
    00048   readOnly = false;
    00049   setText( 1, type );
    00050 }
    00051
    00052 void ConfigViewItem::setStandard( bool value )
    00053 {
    00054   setText( 2, ( value ? i18n( "yes" ) : "" ) );
    00055   isStandard = value;
    00056 }
    00057
    00058 bool ConfigViewItem::standard()
    00059 {
    00060   return isStandard;
    00061 }
    00062
    00063 void ConfigViewItem::stateChange( bool )
    00064 {
    00065   emit changed( true );
    00066 }
    00067
    00068 ConfigPage::ConfigPage( QWidget *parent, const char *name )
    00069   : QWidget( parent, name )
    00070 {
    00071   setCaption( i18n( "Resource Configuration" ) );
    00072
    00073   QVBoxLayout *mainLayout = new QVBoxLayout( this );
    00074
    00075   QGroupBox *groupBox = new QGroupBox( i18n( "Resources" ), this );
    00076   groupBox->setColumnLayout(0, Qt::Vertical );
    00077   groupBox->layout()->setSpacing( 6 );
    00078   groupBox->layout()->setMargin( 11 );
    00079   QHBoxLayout *groupBoxLayout = new QHBoxLayout( groupBox->layout() );
    00080
    00081   mListView = new KListView( groupBox );
    00082   mListView->setAllColumnsShowFocus( true );
    00083   mListView->addColumn( i18n( "Name" ) );
    00084   mListView->addColumn( i18n( "Type" ) );
    00085   mListView->addColumn( i18n( "Standard" ) );
    00086   // Will add it later, when locations are supported by resources
    00087   // mListView->addColumn( i18n( "Location" ) );
    00088
    00089   groupBoxLayout->addWidget( mListView );
    00090
    00091   KButtonBox *buttonBox = new KButtonBox( groupBox, Vertical );
    00092   mAddButton = buttonBox->addButton( i18n( "&Add..." ), this, SLOT(slotAdd()) );
    00093   mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT(slotRemove()) );
    00094   mRemoveButton->setEnabled( false );
    00095   mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT(slotEdit()) );
    00096   mEditButton->setEnabled( false );
    00097   mConvertButton = buttonBox->addButton( i18n( "&Convert..." ), this, SLOT(slotConvert()) );
    00098   mConvertButton->setEnabled( false );
    00099   mStandardButton = buttonBox->addButton( i18n( "&Use as Standard" ), this, SLOT(slotStandard()) );
    00100   mStandardButton->setEnabled( false );
    00101   buttonBox->layout();
    00102
    00103   groupBoxLayout->addWidget( buttonBox );
    00104
    00105   mainLayout->addWidget( groupBox );
    00106
    00107   connect( mListView, SIGNAL(selectionChanged()), this, SLOT(slotSelectionChanged()) );
    00108
    00109   connect( mListView, SIGNAL( doubleClicked( QListViewItem *, const QPoint &, int )), this, SLOT( slotEdit()));
    00110
    00111   config = 0;
    00112   mLastItem = 0;
    00113
    00114   load();
    00115 }
    00116
    00117 void ConfigPage::load()
    00118 {
    00119   delete config;
    00120   config = new KConfig( "kabcrc" );
    00121
    00122   config->setGroup( "General" );
    00123
    00124   QStringList keys = config->readListEntry( "ResourceKeys" );
    00125   uint numActiveKeys = keys.count();
    00126   keys += config->readListEntry( "PassiveResourceKeys" );
    00127
    00128   QString standardKey = config->readEntry( "Standard" );
    00129
    00130   mListView->clear();
    00131
    00132   uint counter = 0;
    00133   bool haveStandardResource = false;
    00134   for ( QStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
    00135     config->setGroup( "Resource_" + (*it) );
    00136     ConfigViewItem *item = new ConfigViewItem( mListView,
    00137     config->readEntry( "ResourceName" ),
    00138     config->readEntry( "ResourceType" ) );
    00139     connect( item, SIGNAL( changed( bool ) ), SIGNAL( changed( bool ) ) );
    00140
    00141     item->key = (*it);
    00142     item->type = config->readEntry( "ResourceType" );
    00143     item->readOnly = config->readBoolEntry( "ResourceIsReadOnly" );
    00144     if ( standardKey == (*it) ) {
    00145       item->setStandard( true );
    00146       haveStandardResource = true;
    00147     }
    00148
    00149     item->setOn( counter < numActiveKeys );
    00150
    00151     counter++;
    00152   }
    00153
    00154   if ( mListView->childCount() == 0 ) {
    00155     defaults();
    00156     config->sync();
    00157   } else {
    00158     if ( !haveStandardResource )
    00159       KMessageBox::error( this, i18n( "There is no standard resource! Please select one." ) );
    00160
    00161     emit changed( false );
    00162   }
    00163 }
    00164
    00165 void ConfigPage::save()
    00166 {
    00167   QStringList activeKeys;
    00168   QStringList passiveKeys;
    00169   QString standardKey;
    00170
    00171   config->setGroup( "General" );
    00172
    00173   QListViewItem *item = mListView->firstChild();
    00174   while ( item != 0 ) {
    00175     ConfigViewItem *configItem = dynamic_cast<ConfigViewItem*>( item );
    00176
    00177     // check if standard resource
    00178     if ( configItem->standard() && !configItem->readOnly && configItem->isOn() )
    00179       standardKey = configItem->key;
    00180
    00181     // check if active or passive resource
    00182     if ( ( (QCheckListItem*)item )->isOn() )
    00183       activeKeys.append( configItem->key );
    00184     else
    00185       passiveKeys.append( configItem->key );
    00186
    00187     item = item->itemBelow();
    00188   }
    00189
    00190   config->writeEntry( "ResourceKeys", activeKeys );
    00191   config->writeEntry( "PassiveResourceKeys", passiveKeys );
    00192   config->writeEntry( "Standard", standardKey );
    00193
    00194   config->sync();
    00195
    00196   if ( standardKey.isEmpty() )
    00197     KMessageBox::error( this, i18n( "There is no valid standard resource! Please select one which is neither read-only nor inactive." ) );
    00198
    00199   emit changed( false );
    00200 }
    00201
    00202 void ConfigPage::defaults()
    00203 {
    00204   QStringList groups = config->groupList();
    00205   QStringList::Iterator it;
    00206   for ( it = groups.begin(); it != groups.end(); ++it )
    00207     config->deleteGroup( (*it) );
    00208
    00209   QString key = KApplication::randomString( 10 );
    00210   QString type = "file";
    00211
    00212   groups.clear();
    00213   groups << key;
    00214
    00215   config->setGroup( "General" );
    00216   config->writeEntry( "ResourceKeys", groups );
    00217   config->writeEntry( "Standard", key );
    00218
    00219   config->setGroup( "Resource_" + key );
    00220   config->writeEntry( "ResourceName", "Default" );
    00221   config->writeEntry( "ResourceType", type );
    00222   config->writeEntry( "ResourceIsReadOnly", false );
    00223   config->writeEntry( "ResourceIsFast", true );
    00224   config->writeEntry( "FileFormat", 0 );
    00225   config->writeEntry( "FileName", KABC::StdAddressBook::fileName() );
    00226
    00227   mListView->clear();
    00228
    00229   ConfigViewItem *item = new ConfigViewItem( mListView, "Default", type );
    00230   item->key = key;
    00231   item->type = type;
    00232   item->setStandard(true);
    00233   item->setOn( true );
    00234   connect( item, SIGNAL( changed( bool ) ), SIGNAL( changed( bool ) ) );
    00235
    00236   mLastItem = item;
    00237
    00238   emit changed( true );
    00239 }
    00240
    00241 void ConfigPage::slotAdd()
    00242 {
    00243   KABC::ResourceFactory *factory = KABC::ResourceFactory::self();
    00244   QString key = KApplication::randomString( 10 );
    00245
    00246   QStringList types = factory->resources();
    00247   bool ok = false;
    00248   QString type = QInputDialog::getItem( i18n( "Resource Configuration" ),
    00249             i18n( "Please select type of the new resource:" ), types, 0, false, &ok, this );
    00250   if ( !ok )
    00251     return;
    00252
    00253   config->setGroup( "Resource_" + key );
    00254
    00255   ResourceConfigDlg dlg( this, type, config, "ResourceConfigDlg" );
    00256
    00257   dlg.setResourceName( type + "-resource" );
    00258   dlg.setFast( true );
    00259
    00260   if ( dlg.exec() ) {
    00261     config->writeEntry( "ResourceName", dlg.resourceName() );
    00262     config->writeEntry( "ResourceType", type );
    00263     config->writeEntry( "ResourceIsReadOnly", dlg.readOnly() );
    00264     config->writeEntry( "ResourceIsFast", dlg.fast() );
    00265
    00266     ConfigViewItem *item = new ConfigViewItem( mListView, dlg.resourceName(), type );
    00267     item->key = key;
    00268     item->type = type;
    00269     item->readOnly = dlg.readOnly();
    00270     item->setOn( true );
    00271     connect( item, SIGNAL( changed( bool ) ), SIGNAL( changed( bool ) ) );
    00272
    00273     mLastItem = item;
    00274
    00275     // if there are only read-only resources we'll set this resource
    00276     // as standard resource
    00277     if ( !item->readOnly ) {
    00278       bool onlyReadOnly = true;
    00279       QListViewItem *it = mListView->firstChild();
    00280       while ( it != 0 ) {
    00281         ConfigViewItem *confIt = dynamic_cast<ConfigViewItem*>( it );
    00282         if ( !confIt->readOnly && confIt != item )
    00283           onlyReadOnly = false;
    00284
    00285         it = it->itemBelow();
    00286       }
    00287
    00288       if ( onlyReadOnly )
    00289         item->setStandard( true );
    00290     }
    00291
    00292     emit changed( true );
    00293   } else {
    00294     config->deleteGroup( "Resource_" + key );
    00295   }
    00296 }
    00297
    00298 void ConfigPage::slotRemove()
    00299 {
    00300   QListViewItem *item = mListView->currentItem();
    00301   ConfigViewItem *confItem = dynamic_cast<ConfigViewItem*>( item );
    00302
    00303   if ( !confItem )
    00304     return;
    00305
    00306   if ( confItem->standard() ) {
    00307     KMessageBox::error( this, i18n( "You cannot remove your standard resource! Please select a new standard resource first." ) );
    00308     return;
    00309   }
    00310
    00311   config->deleteGroup( "Resource_" + confItem->key );
    00312
    00313   if ( item == mLastItem )
    00314     mLastItem = 0;
    00315
    00316   mListView->takeItem( item );
    00317   delete item;
    00318
    00319   emit changed( true );
    00320 }
    00321
    00322 void ConfigPage::slotEdit()
    00323 {
    00324   QListViewItem *item = mListView->currentItem();
    00325   ConfigViewItem *configItem = dynamic_cast<ConfigViewItem*>( item );
    00326   if ( !configItem )
    00327     return;
    00328
    00329   QString key = configItem->key;
    00330   QString type = configItem->type;
    00331
    00332   config->setGroup( "Resource_" + key );
    00333
    00334   ResourceConfigDlg dlg( this, type, config, "ResourceConfigDlg" );
    00335
    00336   dlg.setResourceName( config->readEntry( "ResourceName" ) );
    00337   dlg.setReadOnly( config->readBoolEntry( "ResourceIsReadOnly" ) );
    00338   dlg.setFast( config->readBoolEntry( "ResourceIsFast" ) );
    00339   dlg.setEditMode( true );
    00340
    00341   if ( dlg.exec() ) {
    00342     config->writeEntry( "ResourceName", dlg.resourceName() );
    00343     config->writeEntry( "ResourceType", type );
    00344     config->writeEntry( "ResourceIsReadOnly", dlg.readOnly() );
    00345     config->writeEntry( "ResourceIsFast", dlg.fast() );
    00346
    00347     configItem->setText( 0, dlg.resourceName() );
    00348     configItem->setText( 1, type );
    00349     configItem->readOnly = dlg.readOnly();
    00350
    00351     if ( configItem->standard() && configItem->readOnly ) {
    00352       KMessageBox::error( this, i18n( "You cannot use a read-only resource as standard!" ) );
    00353       configItem->setStandard( false );
    00354     }
    00355
    00356     emit changed( true );
    00357   }
    00358 }
    00359
    00360 void ConfigPage::slotConvert()
    00361 {
    00362   QListViewItem *item = mListView->currentItem();
    00363   QListViewItem *oldItem = item;
    00364   ConfigViewItem *oldConfigItem = dynamic_cast<ConfigViewItem*>( item );
    00365   if ( !oldConfigItem )
    00366     return;
    00367
    00368   bool isStandard = oldConfigItem->standard();
    00369
    00370   KABC::ResourceFactory *factory = KABC::ResourceFactory::self();
    00371   KABC::AddressBook ab;
    00372
    00373   // ask for target resource
    00374   int numItems = mListView->childCount();
    00375   slotAdd();
    00376   if ( numItems == mListView->childCount() || mLastItem == 0 ) // user clicked cancel button
    00377     return;
    00378
    00379   // create old resource
    00380   QString oldKey = oldConfigItem->key;
    00381   QString oldType = oldConfigItem->type;
    00382
    00383   config->setGroup( "Resource_" + oldKey );
    00384   KABC::Resource *oldResource = factory->resource( oldType, &ab, config );
    00385   if ( !oldResource ) {
    00386     KMessageBox::error( this, i18n( "Unable to create resource from type '%1'." ).arg( oldType ) );
    00387     mListView->takeItem( mLastItem );
    00388     delete mLastItem;
    00389     mLastItem = 0;
    00390     return;
    00391   }
    00392
    00393   if ( !ab.addResource( oldResource ) ) {
    00394     KMessageBox::error( this, i18n( "Unable to add resource '%1' to address book." ).arg( oldResource->name() ) );
    00395     delete oldResource;
    00396     mListView->takeItem( mLastItem );
    00397     delete mLastItem;
    00398     mLastItem = 0;
    00399     return;
    00400   }
    00401
    00402   // load addressees from old resource
    00403   ab.load();
    00404
    00405   // remove old resource
    00406   ab.removeResource( oldResource );
    00407
    00408   // create new Resource
    00409   QString newKey, newType;
    00410
    00411   ConfigViewItem *newConfigItem = dynamic_cast<ConfigViewItem*>( mLastItem );
    00412   newConfigItem->setStandard( isStandard );
    00413   newType = newConfigItem->type;
    00414   newKey = newConfigItem->key;
    00415
    00416   config->setGroup( "Resource_" + newKey );
    00417   KABC::Resource *newResource = factory->resource( newType, &ab, config );
    00418   if ( !newResource ) {
    00419     KMessageBox::error( this, i18n( "Unable to create resource from type '%1'." ).arg( newType ) );
    00420     mListView->takeItem( mLastItem );
    00421     delete mLastItem;
    00422     mLastItem = 0;
    00423     return;
    00424   }
    00425
    00426   if ( !ab.addResource( newResource ) ) {
    00427     KMessageBox::error( this, i18n( "Unable to add resource '%1' to address book." ).arg( newResource->name() ) );
    00428     delete newResource;
    00429     mListView->takeItem( mLastItem );
    00430     delete mLastItem;
    00431     mLastItem = 0;
    00432     return;
    00433   }
    00434
    00435   KABC::AddressBook::Iterator it;
    00436   for ( it = ab.begin(); it != ab.end(); ++it ) {
    00437     (*it).setResource( newResource );
    00438     (*it).setChanged( true );
    00439   }
    00440
    00441   KABC::Ticket *ticket = ab.requestSaveTicket( newResource );
    00442   ab.save( ticket );
    00443
    00444   // remove old resource
    00445   config->deleteGroup( "Resource_" + oldKey );
    00446   mListView->takeItem( oldItem );
    00447   delete oldItem;
    00448
    00449   emit changed( true );
    00450   return;
    00451 }
    00452
    00453 void ConfigPage::slotStandard()
    00454 {
    00455   ConfigViewItem *item = dynamic_cast<ConfigViewItem*>( mListView->currentItem() );
    00456   if ( !item )
    00457     return;
    00458
    00459   if ( item->readOnly ) {
    00460     KMessageBox::error( this, i18n( "You cannot use a read-only resource as standard!" ) );
    00461     return;
    00462   }
    00463
    00464   if ( !item->isOn() ) {
    00465     KMessageBox::error( this, i18n( "You cannot use an inactive resource as standard!" ) );
    00466     return;
    00467   }
    00468
    00469   QListViewItem *it = mListView->firstChild();
    00470   while ( it != 0 ) {
    00471     ConfigViewItem *configItem = dynamic_cast<ConfigViewItem*>( it );
    00472     if ( configItem->standard() )
    00473       configItem->setStandard( false );
    00474     it = it->itemBelow();
    00475   }
    00476
    00477   item->setStandard( true );
    00478 }
    00479
    00480 void ConfigPage::slotSelectionChanged()
    00481 {
    00482   bool state = ( mListView->currentItem() != 0 );
    00483
    00484   mRemoveButton->setEnabled( state );
    00485   mEditButton->setEnabled( state );
    00486   mConvertButton->setEnabled( state );
    00487   mStandardButton->setEnabled( state );
    00488 }
    00489
    00490 KCMkabc::KCMkabc( QWidget *parent, const char *name )
    00491   : KCModule( parent, name )
    00492 {
    00493   QVBoxLayout *layout = new QVBoxLayout( this );
    00494   mConfigPage = new ConfigPage( this );
    00495   layout->addWidget( mConfigPage );
    00496   connect( mConfigPage, SIGNAL( changed( bool ) ), SIGNAL( changed( bool ) ) );
    00497 }
    00498
    00499 void KCMkabc::load()
    00500 {
    00501   mConfigPage->load();
    00502 }
    00503
    00504 void KCMkabc::save()
    00505 {
    00506   mConfigPage->save();
    00507 }
    00508
    00509 void KCMkabc::defaults()
    00510 {
    00511   mConfigPage->defaults();
    00512 }
    00513
    00514 const KAboutData* KCMkabc::aboutData() const
    00515 {
    00516    KAboutData *about =
    00517    new KAboutData(I18N_NOOP("kcmkabc"), I18N_NOOP("Address book configuration module"),
    00518                   0, 0, KAboutData::License_GPL,
    00519                   I18N_NOOP("(c), 2002 Tobias Koenig"));
    00520
    00521    about->addAuthor("Tobias Koenig",0 , "tokoe@kde.org");
    00522
    00523    return about;
    00524 }
    00525
    00526
    00527 extern "C"
    00528 {
    00529   KCModule *create_kabc( QWidget *parent, const char * ) {
    00530     return new KCMkabc( parent, "kcmkabc" );
    00531   }
    00532 }
    00533
    00534 #include "kcmkabc.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:29:20 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001