kresources Library API Documentation

configpage.cpp

00001 /*
00002     This file is part of libkresources.
00003 
00004     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
00005     Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
00006     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021     Boston, MA 02111-1307, USA.
00022 */
00023 
00024 #include <qgroupbox.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 
00028 #include <kapplication.h>
00029 #include <kcombobox.h>
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 #include <kmessagebox.h>
00033 #include <ksimpleconfig.h>
00034 #include <kstandarddirs.h>
00035 #include <kurlrequester.h>
00036 #include <klistview.h>
00037 #include <kbuttonbox.h>
00038 #include <ktrader.h>
00039 #include <kinputdialog.h>
00040 
00041 #include "resource.h"
00042 #include "configdialog.h"
00043 
00044 #include "configpage.h"
00045 
00046 namespace KRES {
00047 
00048 ResourcePageInfo::ResourcePageInfo() : KShared() {
00049   mManager = 0L;
00050   mConfig = 0L;
00051 }
00052 
00053 ResourcePageInfo::~ResourcePageInfo() {
00054   //delete mManager;
00055   mManager = 0L;
00056   //delete mConfig;
00057   mConfig = 0L;
00058 }
00059 
00060 
00061 class ConfigViewItem : public QCheckListItem
00062 {
00063   public:
00064     ConfigViewItem( QListView *parent, Resource* resource ) :
00065       QCheckListItem( parent, resource->resourceName(), CheckBox ),
00066       mResource( resource ),
00067       mIsStandard( false )
00068     {
00069       setText( 1, mResource->type() );
00070       setOn( mResource->isActive() );
00071     }
00072 
00073     void setStandard( bool value )
00074     {
00075       setText( 2, ( value ? i18n( "Yes" ) : QString::null ) );
00076       mIsStandard = value;
00077     }
00078 
00079     bool standard() const { return mIsStandard; }
00080     bool readOnly() const { return mResource->readOnly(); }
00081 
00082     Resource *resource() { return mResource; }
00083 
00084     void updateItem()
00085     {
00086       setOn( mResource->isActive() );
00087       setText( 0, mResource->resourceName() );
00088       setText( 1, mResource->type() );
00089       setText( 2, mIsStandard ? i18n( "Yes" ) : QString::null );
00090     }
00091 
00092   private:
00093     Resource* mResource;
00094 
00095     bool mIsStandard;
00096 };
00097 
00098 ConfigPage::ConfigPage( QWidget *parent, const char *name )
00099   : QWidget( parent, name ),
00100     mCurrentManager( 0 ),
00101     mCurrentConfig( 0 )
00102 {
00103   setCaption( i18n( "Resource Configuration" ) );
00104 
00105   QVBoxLayout *mainLayout = new QVBoxLayout( this );
00106 
00107   QGroupBox *groupBox = new QGroupBox( i18n( "Resources" ), this );
00108   groupBox->setColumnLayout(0, Qt::Vertical );
00109   groupBox->layout()->setSpacing( 6 );
00110   groupBox->layout()->setMargin( 11 );
00111   QGridLayout *groupBoxLayout = new QGridLayout( groupBox->layout(), 2, 2 );
00112 
00113   mFamilyCombo = new KComboBox( false, groupBox );
00114   groupBoxLayout->addMultiCellWidget( mFamilyCombo, 0, 0, 0, 1 );
00115 
00116   mListView = new KListView( groupBox );
00117   mListView->setAllColumnsShowFocus( true );
00118   mListView->addColumn( i18n( "Name" ) );
00119   mListView->addColumn( i18n( "Type" ) );
00120   mListView->addColumn( i18n( "Standard" ) );
00121 
00122   groupBoxLayout->addWidget( mListView, 1, 0 );
00123   connect(  mListView, SIGNAL( doubleClicked( QListViewItem *, const QPoint &, int ) ), this, SLOT( slotEdit() ) );
00124   KButtonBox *buttonBox = new KButtonBox( groupBox, Vertical );
00125   mAddButton = buttonBox->addButton( i18n( "&Add..." ), this, SLOT(slotAdd()) );
00126   mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT(slotRemove()) );
00127   mRemoveButton->setEnabled( false );
00128   mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT(slotEdit()) );
00129   mEditButton->setEnabled( false );
00130   mStandardButton = buttonBox->addButton( i18n( "&Use as Standard" ), this, SLOT(slotStandard()) );
00131   mStandardButton->setEnabled( false );
00132   buttonBox->layout();
00133 
00134   groupBoxLayout->addWidget( buttonBox, 1, 1 );
00135 
00136   mainLayout->addWidget( groupBox );
00137 
00138   connect( mFamilyCombo, SIGNAL( activated( int ) ),
00139            SLOT( slotFamilyChanged( int ) ) );
00140   connect( mListView, SIGNAL( selectionChanged() ),
00141            SLOT( slotSelectionChanged() ) );
00142   connect( mListView, SIGNAL( clicked( QListViewItem * ) ),
00143            SLOT( slotItemClicked( QListViewItem * ) ) );
00144 
00145   mLastItem = 0;
00146 
00147   mConfig = new KConfig( "kcmkresourcesrc" );
00148   mConfig->setGroup( "General" );
00149 
00150   load();
00151 }
00152 
00153 ConfigPage::~ConfigPage()
00154 {
00155   QValueList<KSharedPtr<ResourcePageInfo> >::Iterator it;
00156   for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) {
00157     (*it)->mManager->removeObserver( this );
00158   }
00159 
00160   mConfig->writeEntry( "CurrentFamily", mFamilyCombo->currentItem() );
00161   delete mConfig;
00162   mConfig = 0;
00163 }
00164 
00165 void ConfigPage::load()
00166 {
00167   kdDebug(5650) << "ConfigPage::load()" << endl;
00168 
00169   mListView->clear();
00170 
00171   KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin" );
00172   KTrader::OfferList::ConstIterator it;
00173   for ( it = plugins.begin(); it != plugins.end(); ++it ) {
00174     QVariant tmp = (*it)->property( "X-KDE-ResourceFamily" );
00175     QString family = tmp.toString();
00176     if ( !family.isEmpty() ) {
00177       if ( !mFamilyMap.contains( family ) ) {
00178         mCurrentManager = new Manager<Resource>( family );
00179         if ( mCurrentManager ) {
00180           mFamilyMap.append( family );
00181           mCurrentManager->addObserver( this );
00182 
00183           ResourcePageInfo *info = new ResourcePageInfo;
00184           info->mManager = mCurrentManager;
00185           info->mConfig = new KConfig( KRES::ManagerImpl::defaultConfigFile( family ) );
00186           info->mManager->readConfig( info->mConfig );
00187 
00188           mInfoMap.append( KSharedPtr<ResourcePageInfo>(info) );
00189         }
00190       }
00191     }
00192   }
00193   mCurrentManager = 0;
00194 
00195   mFamilyCombo->insertStringList( mFamilyMap );
00196 
00197   int currentFamily = mConfig->readNumEntry( "CurrentFamily", 0 );
00198   mFamilyCombo->setCurrentItem( currentFamily );
00199   slotFamilyChanged( currentFamily );
00200   emit changed( false );
00201 }
00202 
00203 void ConfigPage::save()
00204 {
00205   saveResourceSettings();
00206 
00207   QValueList<KSharedPtr<ResourcePageInfo> >::Iterator it;
00208   for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it )
00209     (*it)->mManager->writeConfig( (*it)->mConfig );
00210 
00211   emit changed( false );
00212 }
00213 
00214 void ConfigPage::defaults()
00215 {
00216 }
00217 
00218 void ConfigPage::slotFamilyChanged( int pos )
00219 {
00220   if ( pos < 0 || pos >= (int)mFamilyMap.count() )
00221     return;
00222 
00223   saveResourceSettings();
00224 
00225   mFamily = mFamilyMap[ pos ];
00226 
00227   mCurrentManager = mInfoMap[ pos ]->mManager;
00228   mCurrentConfig = mInfoMap[ pos ]->mConfig;
00229 
00230   if ( !mCurrentManager )
00231     kdDebug(5650) << "ERROR: cannot create ResourceManager<Resource>( mFamily )" << endl;
00232 
00233   mListView->clear();
00234 
00235   if ( mCurrentManager->isEmpty() )
00236     defaults();
00237 
00238   Resource *standardResource = mCurrentManager->standardResource();
00239 
00240   Manager<Resource>::Iterator it;
00241   for ( it = mCurrentManager->begin(); it != mCurrentManager->end(); ++it ) {
00242     ConfigViewItem *item = new ConfigViewItem( mListView, *it );
00243     if ( *it == standardResource )
00244       item->setStandard( true );
00245   }
00246 
00247   if ( mListView->childCount() == 0 ) {
00248     defaults();
00249     emit changed( true );
00250     mCurrentManager->writeConfig( mCurrentConfig );
00251   } else {
00252     if ( !standardResource )
00253       KMessageBox::sorry( this, i18n( "There is no standard resource! Please select one." ) );
00254 
00255     emit changed( false );
00256   }
00257 }
00258 
00259 void ConfigPage::slotAdd()
00260 {
00261   if ( !mCurrentManager )
00262     return;
00263 
00264   QStringList types = mCurrentManager->resourceTypeNames();
00265   QStringList descs = mCurrentManager->resourceTypeDescriptions();
00266   bool ok = false;
00267   QString desc = KInputDialog::getItem( i18n( "Resource Configuration" ),
00268                     i18n( "Please select type of the new resource:" ), descs,
00269                     0, false, &ok, this );
00270   if ( !ok )
00271     return;
00272 
00273   QString type = types[ descs.findIndex( desc ) ];
00274 
00275   // Create new resource
00276   Resource *resource = mCurrentManager->createResource( type );
00277   if ( !resource ) {
00278     KMessageBox::error( this, i18n("Unable to create resource of type '%1'.")
00279                               .arg( type ) );
00280     return;
00281   }
00282 
00283   resource->setResourceName( type + "-resource" );
00284 
00285   ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" );
00286 
00287   if ( dlg.exec() ) {
00288     mCurrentManager->add( resource );
00289 
00290     ConfigViewItem *item = new ConfigViewItem( mListView, resource );
00291 
00292     mLastItem = item;
00293 
00294     // if there are only read-only resources we'll set this resource
00295     // as standard resource
00296     if ( !resource->readOnly() ) {
00297       bool onlyReadOnly = true;
00298       QListViewItem *it = mListView->firstChild();
00299       while ( it != 0 ) {
00300         ConfigViewItem *confIt = static_cast<ConfigViewItem*>( it );
00301         if ( !confIt->readOnly() && confIt != item )
00302           onlyReadOnly = false;
00303 
00304         it = it->itemBelow();
00305       }
00306 
00307       if ( onlyReadOnly )
00308         item->setStandard( true );
00309     }
00310 
00311     emit changed( true );
00312   } else {
00313     delete resource;
00314     resource = 0;
00315   }
00316 }
00317 
00318 void ConfigPage::slotRemove()
00319 {
00320   if ( !mCurrentManager )
00321     return;
00322 
00323   QListViewItem *item = mListView->currentItem();
00324   ConfigViewItem *confItem = static_cast<ConfigViewItem*>( item );
00325 
00326   if ( !confItem )
00327     return;
00328 
00329   if ( confItem->standard() ) {
00330     KMessageBox::sorry( this, i18n( "You cannot remove your standard resource! Please select a new standard resource first." ) );
00331     return;
00332   }
00333 
00334   mCurrentManager->remove( confItem->resource() );
00335 
00336   if ( item == mLastItem )
00337     mLastItem = 0;
00338 
00339   mListView->takeItem( item );
00340   delete item;
00341 
00342   emit changed( true );
00343 }
00344 
00345 void ConfigPage::slotEdit()
00346 {
00347   if ( !mCurrentManager )
00348     return;
00349 
00350   QListViewItem *item = mListView->currentItem();
00351   ConfigViewItem *configItem = static_cast<ConfigViewItem*>( item );
00352   if ( !configItem )
00353     return;
00354 
00355   Resource *resource = configItem->resource();
00356 
00357   ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" );
00358 
00359   if ( dlg.exec() ) {
00360     configItem->setText( 0, resource->resourceName() );
00361     configItem->setText( 1, resource->type() );
00362 
00363     if ( configItem->standard() && configItem->readOnly() ) {
00364       KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) );
00365       configItem->setStandard( false );
00366     }
00367 
00368     mCurrentManager->change( resource );
00369     emit changed( true );
00370   }
00371 }
00372 
00373 void ConfigPage::slotStandard()
00374 {
00375   if ( !mCurrentManager )
00376     return;
00377 
00378   ConfigViewItem *item = static_cast<ConfigViewItem*>( mListView->currentItem() );
00379   if ( !item )
00380     return;
00381 
00382   if ( item->readOnly() ) {
00383     KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) );
00384     return;
00385   }
00386 
00387   if ( !item->isOn() ) {
00388     KMessageBox::sorry( this, i18n( "You cannot use an inactive resource as standard!" ) );
00389     return;
00390   }
00391 
00392   QListViewItem *it = mListView->firstChild();
00393   while ( it != 0 ) {
00394     ConfigViewItem *configItem = static_cast<ConfigViewItem*>( it );
00395     if ( configItem->standard() )
00396       configItem->setStandard( false );
00397     it = it->itemBelow();
00398   }
00399 
00400   item->setStandard( true );
00401   mCurrentManager->setStandardResource( item->resource() );
00402 
00403   emit changed( true );
00404 }
00405 
00406 void ConfigPage::slotSelectionChanged()
00407 {
00408   bool state = ( mListView->currentItem() != 0 );
00409 
00410   mRemoveButton->setEnabled( state );
00411   mEditButton->setEnabled( state );
00412   mStandardButton->setEnabled( state );
00413 }
00414 
00415 void ConfigPage::resourceAdded( Resource *resource )
00416 {
00417   kdDebug(5650) << "ConfigPage::resourceAdded( " << resource->resourceName()
00418                 << " )" << endl;
00419 
00420   ConfigViewItem *item = new ConfigViewItem( mListView, resource );
00421 
00422   item->setOn( resource->isActive() );
00423 
00424   mLastItem = item;
00425 
00426   emit changed( true );
00427 }
00428 
00429 void ConfigPage::resourceModified( Resource *resource )
00430 {
00431   kdDebug(5650) << "ConfigPage::resourceModified( " << resource->resourceName()
00432                 << " )" << endl;
00433   ConfigViewItem *item = findItem( resource );
00434   if ( !item ) return;
00435 
00436   // TODO: Reread resource config. Otherwise we won't see the modification.
00437 
00438   item->updateItem();
00439 }
00440 
00441 void ConfigPage::resourceDeleted( Resource *resource )
00442 {
00443   kdDebug(5650) << "ConfigPage::resourceDeleted( " << resource->resourceName()
00444                 << " )" << endl;
00445 
00446   ConfigViewItem *item = findItem( resource );
00447   if ( !item ) return;
00448 
00449   delete item;
00450 }
00451 
00452 ConfigViewItem *ConfigPage::findItem( Resource *resource )
00453 {
00454   QListViewItem *i;
00455   for( i = mListView->firstChild(); i; i = i->nextSibling() ) {
00456     ConfigViewItem *item = static_cast<ConfigViewItem *>( i );
00457     if ( item->resource() == resource ) return item;
00458   }
00459   return 0;
00460 }
00461 
00462 void ConfigPage::slotItemClicked( QListViewItem *item )
00463 {
00464   ConfigViewItem *configItem = static_cast<ConfigViewItem *>( item );
00465   if ( !configItem ) return;
00466 
00467   if ( configItem->standard() && !configItem->isOn() ) {
00468     KMessageBox::sorry( this, i18n( "You cannot deactivate the standard resource. Choose another standard resource first." ) );
00469     configItem->setOn( true );
00470     return;
00471   }
00472 
00473   if ( configItem->isOn() != configItem->resource()->isActive() ) {
00474     emit changed( true );
00475   }
00476 }
00477 
00478 void ConfigPage::saveResourceSettings()
00479 {
00480   if ( mCurrentManager ) {
00481     QListViewItem *item = mListView->firstChild();
00482     while ( item ) {
00483       ConfigViewItem *configItem = static_cast<ConfigViewItem *>( item );
00484 
00485       // check if standard resource
00486       if ( configItem->standard() && !configItem->readOnly() &&
00487            configItem->isOn() )
00488         mCurrentManager->setStandardResource( configItem->resource() );
00489 
00490       // check if active or passive resource
00491       configItem->resource()->setActive( configItem->isOn() );
00492 
00493       item = item->nextSibling();
00494     }
00495     mCurrentManager->writeConfig( mCurrentConfig );
00496 
00497     if ( !mCurrentManager->standardResource() )
00498       KMessageBox::sorry( this, i18n( "There is no valid standard resource! Please select one which is neither read-only nor inactive." ) );
00499   }
00500 }
00501 
00502 }
00503 
00504 #include "configpage.moc"
00505 
KDE Logo
This file is part of the documentation for kresources Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun May 16 22:04:19 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003