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->setFullWidth( true ); 00119 mListView->addColumn( i18n( "Name" ) ); 00120 mListView->addColumn( i18n( "Type" ) ); 00121 mListView->addColumn( i18n( "Standard" ) ); 00122 00123 groupBoxLayout->addWidget( mListView, 1, 0 ); 00124 connect( mListView, SIGNAL( doubleClicked( QListViewItem *, const QPoint &, int ) ), this, SLOT( slotEdit() ) ); 00125 KButtonBox *buttonBox = new KButtonBox( groupBox, Vertical ); 00126 mAddButton = buttonBox->addButton( i18n( "&Add..." ), this, SLOT(slotAdd()) ); 00127 mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT(slotRemove()) ); 00128 mRemoveButton->setEnabled( false ); 00129 mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT(slotEdit()) ); 00130 mEditButton->setEnabled( false ); 00131 mStandardButton = buttonBox->addButton( i18n( "&Use as Standard" ), this, SLOT(slotStandard()) ); 00132 mStandardButton->setEnabled( false ); 00133 buttonBox->layout(); 00134 00135 groupBoxLayout->addWidget( buttonBox, 1, 1 ); 00136 00137 mainLayout->addWidget( groupBox ); 00138 00139 connect( mFamilyCombo, SIGNAL( activated( int ) ), 00140 SLOT( slotFamilyChanged( int ) ) ); 00141 connect( mListView, SIGNAL( selectionChanged() ), 00142 SLOT( slotSelectionChanged() ) ); 00143 connect( mListView, SIGNAL( clicked( QListViewItem * ) ), 00144 SLOT( slotItemClicked( QListViewItem * ) ) ); 00145 00146 mLastItem = 0; 00147 00148 mConfig = new KConfig( "kcmkresourcesrc" ); 00149 mConfig->setGroup( "General" ); 00150 00151 load(); 00152 } 00153 00154 ConfigPage::~ConfigPage() 00155 { 00156 QValueList<KSharedPtr<ResourcePageInfo> >::Iterator it; 00157 for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) { 00158 (*it)->mManager->removeObserver( this ); 00159 } 00160 00161 mConfig->writeEntry( "CurrentFamily", mFamilyCombo->currentItem() ); 00162 delete mConfig; 00163 mConfig = 0; 00164 } 00165 00166 void ConfigPage::load() 00167 { 00168 kdDebug(5650) << "ConfigPage::load()" << endl; 00169 00170 mListView->clear(); 00171 mFamilyMap.clear(); 00172 mInfoMap.clear(); 00173 00174 KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin" ); 00175 KTrader::OfferList::ConstIterator it; 00176 for ( it = plugins.begin(); it != plugins.end(); ++it ) { 00177 QVariant tmp = (*it)->property( "X-KDE-ResourceFamily" ); 00178 QString family = tmp.toString(); 00179 if ( !family.isEmpty() ) { 00180 if ( !mFamilyMap.contains( family ) ) { 00181 mCurrentManager = new Manager<Resource>( family ); 00182 if ( mCurrentManager ) { 00183 mFamilyMap.append( family ); 00184 mCurrentManager->addObserver( this ); 00185 00186 ResourcePageInfo *info = new ResourcePageInfo; 00187 info->mManager = mCurrentManager; 00188 info->mConfig = new KConfig( KRES::ManagerImpl::defaultConfigFile( family ) ); 00189 info->mManager->readConfig( info->mConfig ); 00190 00191 mInfoMap.append( KSharedPtr<ResourcePageInfo>(info) ); 00192 } 00193 } 00194 } 00195 } 00196 mCurrentManager = 0; 00197 00198 mFamilyCombo->clear(); 00199 mFamilyCombo->insertStringList( mFamilyMap ); 00200 00201 int currentFamily = mConfig->readNumEntry( "CurrentFamily", 0 ); 00202 mFamilyCombo->setCurrentItem( currentFamily ); 00203 slotFamilyChanged( currentFamily ); 00204 emit changed( false ); 00205 } 00206 00207 void ConfigPage::save() 00208 { 00209 saveResourceSettings(); 00210 00211 QValueList<KSharedPtr<ResourcePageInfo> >::Iterator it; 00212 for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) 00213 (*it)->mManager->writeConfig( (*it)->mConfig ); 00214 00215 emit changed( false ); 00216 } 00217 00218 void ConfigPage::defaults() 00219 { 00220 } 00221 00222 void ConfigPage::slotFamilyChanged( int pos ) 00223 { 00224 if ( pos < 0 || pos >= (int)mFamilyMap.count() ) 00225 return; 00226 00227 saveResourceSettings(); 00228 00229 mFamily = mFamilyMap[ pos ]; 00230 00231 mCurrentManager = mInfoMap[ pos ]->mManager; 00232 mCurrentConfig = mInfoMap[ pos ]->mConfig; 00233 00234 if ( !mCurrentManager ) 00235 kdDebug(5650) << "ERROR: cannot create ResourceManager<Resource>( mFamily )" << endl; 00236 00237 mListView->clear(); 00238 00239 if ( mCurrentManager->isEmpty() ) 00240 defaults(); 00241 00242 Resource *standardResource = mCurrentManager->standardResource(); 00243 00244 Manager<Resource>::Iterator it; 00245 for ( it = mCurrentManager->begin(); it != mCurrentManager->end(); ++it ) { 00246 ConfigViewItem *item = new ConfigViewItem( mListView, *it ); 00247 if ( *it == standardResource ) 00248 item->setStandard( true ); 00249 } 00250 00251 if ( mListView->childCount() == 0 ) { 00252 defaults(); 00253 emit changed( true ); 00254 mCurrentManager->writeConfig( mCurrentConfig ); 00255 } else { 00256 if ( !standardResource ) 00257 KMessageBox::sorry( this, i18n( "There is no standard resource! Please select one." ) ); 00258 00259 emit changed( false ); 00260 } 00261 } 00262 00263 void ConfigPage::slotAdd() 00264 { 00265 if ( !mCurrentManager ) 00266 return; 00267 00268 QStringList types = mCurrentManager->resourceTypeNames(); 00269 QStringList descs = mCurrentManager->resourceTypeDescriptions(); 00270 bool ok = false; 00271 QString desc = KInputDialog::getItem( i18n( "Resource Configuration" ), 00272 i18n( "Please select type of the new resource:" ), descs, 00273 0, false, &ok, this ); 00274 if ( !ok ) 00275 return; 00276 00277 QString type = types[ descs.findIndex( desc ) ]; 00278 00279 // Create new resource 00280 Resource *resource = mCurrentManager->createResource( type ); 00281 if ( !resource ) { 00282 KMessageBox::error( this, i18n("Unable to create resource of type '%1'.") 00283 .arg( type ) ); 00284 return; 00285 } 00286 00287 resource->setResourceName( type + "-resource" ); 00288 00289 ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" ); 00290 00291 if ( dlg.exec() ) { 00292 mCurrentManager->add( resource ); 00293 00294 ConfigViewItem *item = new ConfigViewItem( mListView, resource ); 00295 00296 mLastItem = item; 00297 00298 // if there are only read-only resources we'll set this resource 00299 // as standard resource 00300 if ( !resource->readOnly() ) { 00301 bool onlyReadOnly = true; 00302 QListViewItem *it = mListView->firstChild(); 00303 while ( it != 0 ) { 00304 ConfigViewItem *confIt = static_cast<ConfigViewItem*>( it ); 00305 if ( !confIt->readOnly() && confIt != item ) 00306 onlyReadOnly = false; 00307 00308 it = it->itemBelow(); 00309 } 00310 00311 if ( onlyReadOnly ) 00312 item->setStandard( true ); 00313 } 00314 00315 emit changed( true ); 00316 } else { 00317 delete resource; 00318 resource = 0; 00319 } 00320 } 00321 00322 void ConfigPage::slotRemove() 00323 { 00324 if ( !mCurrentManager ) 00325 return; 00326 00327 QListViewItem *item = mListView->currentItem(); 00328 ConfigViewItem *confItem = static_cast<ConfigViewItem*>( item ); 00329 00330 if ( !confItem ) 00331 return; 00332 00333 if ( confItem->standard() ) { 00334 KMessageBox::sorry( this, i18n( "You cannot remove your standard resource! Please select a new standard resource first." ) ); 00335 return; 00336 } 00337 00338 mCurrentManager->remove( confItem->resource() ); 00339 00340 if ( item == mLastItem ) 00341 mLastItem = 0; 00342 00343 mListView->takeItem( item ); 00344 delete item; 00345 00346 emit changed( true ); 00347 } 00348 00349 void ConfigPage::slotEdit() 00350 { 00351 if ( !mCurrentManager ) 00352 return; 00353 00354 QListViewItem *item = mListView->currentItem(); 00355 ConfigViewItem *configItem = static_cast<ConfigViewItem*>( item ); 00356 if ( !configItem ) 00357 return; 00358 00359 Resource *resource = configItem->resource(); 00360 00361 ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" ); 00362 00363 if ( dlg.exec() ) { 00364 configItem->setText( 0, resource->resourceName() ); 00365 configItem->setText( 1, resource->type() ); 00366 00367 if ( configItem->standard() && configItem->readOnly() ) { 00368 KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) ); 00369 configItem->setStandard( false ); 00370 } 00371 00372 mCurrentManager->change( resource ); 00373 emit changed( true ); 00374 } 00375 } 00376 00377 void ConfigPage::slotStandard() 00378 { 00379 if ( !mCurrentManager ) 00380 return; 00381 00382 ConfigViewItem *item = static_cast<ConfigViewItem*>( mListView->currentItem() ); 00383 if ( !item ) 00384 return; 00385 00386 if ( item->readOnly() ) { 00387 KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) ); 00388 return; 00389 } 00390 00391 if ( !item->isOn() ) { 00392 KMessageBox::sorry( this, i18n( "You cannot use an inactive resource as standard!" ) ); 00393 return; 00394 } 00395 00396 QListViewItem *it = mListView->firstChild(); 00397 while ( it != 0 ) { 00398 ConfigViewItem *configItem = static_cast<ConfigViewItem*>( it ); 00399 if ( configItem->standard() ) 00400 configItem->setStandard( false ); 00401 it = it->itemBelow(); 00402 } 00403 00404 item->setStandard( true ); 00405 mCurrentManager->setStandardResource( item->resource() ); 00406 00407 emit changed( true ); 00408 } 00409 00410 void ConfigPage::slotSelectionChanged() 00411 { 00412 bool state = ( mListView->currentItem() != 0 ); 00413 00414 mRemoveButton->setEnabled( state ); 00415 mEditButton->setEnabled( state ); 00416 mStandardButton->setEnabled( state ); 00417 } 00418 00419 void ConfigPage::resourceAdded( Resource *resource ) 00420 { 00421 kdDebug(5650) << "ConfigPage::resourceAdded( " << resource->resourceName() 00422 << " )" << endl; 00423 00424 ConfigViewItem *item = new ConfigViewItem( mListView, resource ); 00425 00426 item->setOn( resource->isActive() ); 00427 00428 mLastItem = item; 00429 00430 emit changed( true ); 00431 } 00432 00433 void ConfigPage::resourceModified( Resource *resource ) 00434 { 00435 kdDebug(5650) << "ConfigPage::resourceModified( " << resource->resourceName() 00436 << " )" << endl; 00437 ConfigViewItem *item = findItem( resource ); 00438 if ( !item ) return; 00439 00440 // TODO: Reread resource config. Otherwise we won't see the modification. 00441 00442 item->updateItem(); 00443 } 00444 00445 void ConfigPage::resourceDeleted( Resource *resource ) 00446 { 00447 kdDebug(5650) << "ConfigPage::resourceDeleted( " << resource->resourceName() 00448 << " )" << endl; 00449 00450 ConfigViewItem *item = findItem( resource ); 00451 if ( !item ) return; 00452 00453 delete item; 00454 } 00455 00456 ConfigViewItem *ConfigPage::findItem( Resource *resource ) 00457 { 00458 QListViewItem *i; 00459 for( i = mListView->firstChild(); i; i = i->nextSibling() ) { 00460 ConfigViewItem *item = static_cast<ConfigViewItem *>( i ); 00461 if ( item->resource() == resource ) return item; 00462 } 00463 return 0; 00464 } 00465 00466 void ConfigPage::slotItemClicked( QListViewItem *item ) 00467 { 00468 ConfigViewItem *configItem = static_cast<ConfigViewItem *>( item ); 00469 if ( !configItem ) return; 00470 00471 if ( configItem->standard() && !configItem->isOn() ) { 00472 KMessageBox::sorry( this, i18n( "You cannot deactivate the standard resource. Choose another standard resource first." ) ); 00473 configItem->setOn( true ); 00474 return; 00475 } 00476 00477 if ( configItem->isOn() != configItem->resource()->isActive() ) { 00478 emit changed( true ); 00479 } 00480 } 00481 00482 void ConfigPage::saveResourceSettings() 00483 { 00484 if ( mCurrentManager ) { 00485 QListViewItem *item = mListView->firstChild(); 00486 while ( item ) { 00487 ConfigViewItem *configItem = static_cast<ConfigViewItem *>( item ); 00488 00489 // check if standard resource 00490 if ( configItem->standard() && !configItem->readOnly() && 00491 configItem->isOn() ) 00492 mCurrentManager->setStandardResource( configItem->resource() ); 00493 00494 // check if active or passive resource 00495 configItem->resource()->setActive( configItem->isOn() ); 00496 00497 item = item->nextSibling(); 00498 } 00499 mCurrentManager->writeConfig( mCurrentConfig ); 00500 00501 if ( !mCurrentManager->standardResource() ) 00502 KMessageBox::sorry( this, i18n( "There is no valid standard resource! Please select one which is neither read-only nor inactive." ) ); 00503 } 00504 } 00505 00506 } 00507 00508 #include "configpage.moc" 00509
KDE Logo
This file is part of the documentation for kresources Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 17 11:31:42 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003