24 #include <tqgroupbox.h>
28 #include <tdeapplication.h>
29 #include <kcombobox.h>
31 #include <tdelocale.h>
32 #include <tdemessagebox.h>
33 #include <ksimpleconfig.h>
34 #include <kstandarddirs.h>
35 #include <kurlrequester.h>
36 #include <tdelistview.h>
37 #include <kbuttonbox.h>
39 #include <kinputdialog.h>
42 #include "configdialog.h"
44 #include "configpage.h"
48 ResourcePageInfo::ResourcePageInfo() : TDEShared() {
53 ResourcePageInfo::~ResourcePageInfo() {
61 class ConfigViewItem :
public TQCheckListItem
64 ConfigViewItem( TQListView *parent, Resource* resource ) :
65 TQCheckListItem( parent, resource->resourceName(), CheckBox ),
66 mResource( resource ),
69 setText( 1, mResource->type() );
70 setOn( mResource->isActive() );
73 void setStandard(
bool value )
75 setText( 2, ( value ? i18n(
"Yes" ) : TQString::null ) );
79 bool standard()
const {
return mIsStandard; }
80 bool readOnly()
const {
return mResource->readOnly(); }
82 Resource *resource() {
return mResource; }
86 setOn( mResource->isActive() );
87 setText( 0, mResource->resourceName() );
88 setText( 1, mResource->type() );
89 setText( 2, mIsStandard ? i18n(
"Yes" ) : TQString::null );
98 ConfigPage::ConfigPage( TQWidget *parent,
const char *name )
99 : TQWidget( parent, name ),
100 mCurrentManager( 0 ),
103 setCaption( i18n(
"Resource Configuration" ) );
105 TQVBoxLayout *mainLayout =
new TQVBoxLayout(
this );
107 TQGroupBox *groupBox =
new TQGroupBox( i18n(
"Resources" ),
this );
108 groupBox->setColumnLayout(0, TQt::Vertical );
109 groupBox->layout()->setSpacing( 6 );
110 groupBox->layout()->setMargin( 11 );
111 TQGridLayout *groupBoxLayout =
new TQGridLayout( groupBox->layout(), 2, 2 );
113 mFamilyCombo =
new KComboBox(
false, groupBox );
114 groupBoxLayout->addMultiCellWidget( mFamilyCombo, 0, 0, 0, 1 );
116 mListView =
new TDEListView( groupBox );
117 mListView->setAllColumnsShowFocus(
true );
118 mListView->setFullWidth(
true );
119 mListView->addColumn( i18n(
"Name" ) );
120 mListView->addColumn( i18n(
"Type" ) );
121 mListView->addColumn( i18n(
"Standard" ) );
123 groupBoxLayout->addWidget( mListView, 1, 0 );
124 connect( mListView, TQ_SIGNAL( doubleClicked( TQListViewItem *,
const TQPoint &,
int ) ),
this, TQ_SLOT( slotEdit() ) );
125 KButtonBox *buttonBox =
new KButtonBox( groupBox, TQt::Vertical );
126 mAddButton = buttonBox->addButton( i18n(
"&Add..." ),
this, TQ_SLOT(slotAdd()) );
127 mRemoveButton = buttonBox->addButton( i18n(
"&Remove" ),
this, TQ_SLOT(slotRemove()) );
128 mRemoveButton->setEnabled(
false );
129 mEditButton = buttonBox->addButton( i18n(
"&Edit..." ),
this, TQ_SLOT(slotEdit()) );
130 mEditButton->setEnabled(
false );
131 mStandardButton = buttonBox->addButton( i18n(
"&Use as Standard" ),
this, TQ_SLOT(slotStandard()) );
132 mStandardButton->setEnabled(
false );
135 groupBoxLayout->addWidget( buttonBox, 1, 1 );
137 mainLayout->addWidget( groupBox );
139 connect( mFamilyCombo, TQ_SIGNAL( activated(
int ) ),
140 TQ_SLOT( slotFamilyChanged(
int ) ) );
141 connect( mListView, TQ_SIGNAL( selectionChanged() ),
142 TQ_SLOT( slotSelectionChanged() ) );
143 connect( mListView, TQ_SIGNAL( clicked( TQListViewItem * ) ),
144 TQ_SLOT( slotItemClicked( TQListViewItem * ) ) );
148 mConfig =
new TDEConfig(
"kcmtderesourcesrc" );
149 mConfig->setGroup(
"General" );
154 ConfigPage::~ConfigPage()
156 TQValueList<TDESharedPtr<ResourcePageInfo> >::Iterator it;
157 for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) {
158 (*it)->mManager->removeObserver(
this );
161 mConfig->writeEntry(
"CurrentFamily", mFamilyCombo->currentItem() );
166 void ConfigPage::load()
168 kdDebug(5650) <<
"ConfigPage::load()" << endl;
173 TQStringList familyDisplayNames;
176 TQStringList compatFamilyNames;
177 const TDETrader::OfferList plugins = TDETrader::self()->query(
"TDEResources/Plugin" );
178 TDETrader::OfferList::ConstIterator it = plugins.begin();
179 TDETrader::OfferList::ConstIterator end = plugins.end();
180 for ( ; it != end; ++it ) {
181 const TQString family = (*it)->property(
"X-TDE-ResourceFamily" ).toString();
182 if ( compatFamilyNames.find( family ) == compatFamilyNames.end() )
183 compatFamilyNames.append( family );
186 const TDETrader::OfferList managers = TDETrader::self()->query(
"TDEResources/Manager" );
187 TDETrader::OfferList::ConstIterator m_it;
188 for( m_it = managers.begin(); m_it != managers.end(); ++m_it ) {
189 TQString displayName = (*m_it)->property(
"Name" ).toString();
190 familyDisplayNames.append( displayName );
191 TQString family = (*m_it)->property(
"X-TDE-ResourceFamily" ).toString();
192 if ( !family.isEmpty() ) {
193 compatFamilyNames.remove( family );
194 mFamilyMap.append( family );
195 loadManager( family );
200 TQStringList::ConstIterator cfit = compatFamilyNames.begin();
201 for ( ; cfit != compatFamilyNames.end(); ++cfit ) {
202 mFamilyMap.append( *cfit );
203 familyDisplayNames.append( *cfit );
204 loadManager( *cfit );
209 mFamilyCombo->clear();
210 mFamilyCombo->insertStringList( familyDisplayNames );
212 int currentFamily = mConfig->readNumEntry(
"CurrentFamily", 0 );
213 mFamilyCombo->setCurrentItem( currentFamily );
214 slotFamilyChanged( currentFamily );
215 emit changed(
false );
218 void ConfigPage::loadManager(
const TQString& family )
220 mCurrentManager =
new Manager<Resource>( family );
221 if ( mCurrentManager ) {
222 mCurrentManager->addObserver(
this );
224 ResourcePageInfo *info =
new ResourcePageInfo;
225 info->mManager = mCurrentManager;
226 info->mConfig =
new TDEConfig( KRES::ManagerImpl::defaultConfigFile( family ) );
227 info->mManager->readConfig( info->mConfig );
229 mInfoMap.append( TDESharedPtr<ResourcePageInfo>(info) );
233 void ConfigPage::save()
235 saveResourceSettings();
237 TQValueList<TDESharedPtr<ResourcePageInfo> >::Iterator it;
238 for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it )
239 (*it)->mManager->writeConfig( (*it)->mConfig );
241 emit changed(
false );
244 void ConfigPage::defaults()
248 void ConfigPage::slotFamilyChanged(
int pos )
250 if ( pos < 0 || pos >= (
int)mFamilyMap.count() )
253 saveResourceSettings();
255 mFamily = mFamilyMap[ pos ];
257 mCurrentManager = mInfoMap[ pos ]->mManager;
258 mCurrentConfig = mInfoMap[ pos ]->mConfig;
260 if ( !mCurrentManager )
261 kdDebug(5650) <<
"ERROR: cannot create ResourceManager<Resource>( mFamily )" << endl;
265 if ( mCurrentManager->isEmpty() )
268 Resource *standardResource = mCurrentManager->standardResource();
270 Manager<Resource>::Iterator it;
271 for ( it = mCurrentManager->begin(); it != mCurrentManager->end(); ++it ) {
272 ConfigViewItem *item =
new ConfigViewItem( mListView, *it );
273 if ( *it == standardResource )
274 item->setStandard(
true );
277 if ( mListView->childCount() == 0 ) {
279 emit changed(
true );
280 mCurrentManager->writeConfig( mCurrentConfig );
282 if ( !standardResource )
283 KMessageBox::sorry(
this, i18n(
"There is no standard resource! Please select one." ) );
285 emit changed(
false );
289 void ConfigPage::slotAdd()
291 if ( !mCurrentManager )
294 TQStringList types = mCurrentManager->resourceTypeNames();
295 TQStringList descs = mCurrentManager->resourceTypeDescriptions();
297 TQString desc = KInputDialog::getItem( i18n(
"Resource Configuration" ),
298 i18n(
"Please select type of the new resource:" ), descs,
299 0,
false, &ok,
this );
303 TQString type = types[ descs.findIndex( desc ) ];
306 Resource *resource = mCurrentManager->createResource( type );
308 KMessageBox::error(
this, i18n(
"Unable to create resource of type '%1'.")
313 resource->setResourceName( type +
"-resource" );
315 ConfigDialog dlg(
this, mFamily, resource,
"KRES::ConfigDialog" );
318 mCurrentManager->add( resource );
320 ConfigViewItem *item =
new ConfigViewItem( mListView, resource );
326 if ( !resource->readOnly() ) {
327 bool onlyReadOnly =
true;
328 TQListViewItem *it = mListView->firstChild();
330 ConfigViewItem *confIt =
static_cast<ConfigViewItem*
>( it );
331 if ( !confIt->readOnly() && confIt != item )
332 onlyReadOnly =
false;
334 it = it->itemBelow();
338 item->setStandard(
true );
341 emit changed(
true );
348 void ConfigPage::slotRemove()
350 if ( !mCurrentManager )
353 TQListViewItem *item = mListView->currentItem();
354 ConfigViewItem *confItem =
static_cast<ConfigViewItem*
>( item );
359 if ( confItem->standard() ) {
360 KMessageBox::sorry(
this, i18n(
"You cannot remove your standard resource! Please select a new standard resource first." ) );
364 mCurrentManager->remove( confItem->resource() );
366 if ( item == mLastItem )
369 mListView->takeItem( item );
372 emit changed(
true );
375 void ConfigPage::slotEdit()
377 if ( !mCurrentManager )
380 TQListViewItem *item = mListView->currentItem();
381 ConfigViewItem *configItem =
static_cast<ConfigViewItem*
>( item );
385 Resource *resource = configItem->resource();
387 ConfigDialog dlg(
this, mFamily, resource,
"KRES::ConfigDialog" );
390 configItem->setText( 0, resource->resourceName() );
391 configItem->setText( 1, resource->type() );
393 if ( configItem->standard() && configItem->readOnly() ) {
394 KMessageBox::sorry(
this, i18n(
"You cannot use a read-only resource as standard!" ) );
395 configItem->setStandard(
false );
398 mCurrentManager->change( resource );
399 emit changed(
true );
403 void ConfigPage::slotStandard()
405 if ( !mCurrentManager )
408 ConfigViewItem *item =
static_cast<ConfigViewItem*
>( mListView->currentItem() );
412 if ( item->readOnly() ) {
413 KMessageBox::sorry(
this, i18n(
"You cannot use a read-only resource as standard!" ) );
417 if ( !item->isOn() ) {
418 KMessageBox::sorry(
this, i18n(
"You cannot use an inactive resource as standard!" ) );
422 TQListViewItem *it = mListView->firstChild();
424 ConfigViewItem *configItem =
static_cast<ConfigViewItem*
>( it );
425 if ( configItem->standard() )
426 configItem->setStandard(
false );
427 it = it->itemBelow();
430 item->setStandard(
true );
431 mCurrentManager->setStandardResource( item->resource() );
433 emit changed(
true );
436 void ConfigPage::slotSelectionChanged()
438 bool state = ( mListView->currentItem() != 0 );
440 mRemoveButton->setEnabled( state );
441 mEditButton->setEnabled( state );
442 mStandardButton->setEnabled( state );
445 void ConfigPage::resourceAdded( Resource *resource )
447 kdDebug(5650) <<
"ConfigPage::resourceAdded( " << resource->resourceName()
450 ConfigViewItem *item =
new ConfigViewItem( mListView, resource );
452 item->setOn( resource->isActive() );
456 emit changed(
true );
459 void ConfigPage::resourceModified( Resource *resource )
461 kdDebug(5650) <<
"ConfigPage::resourceModified( " << resource->resourceName()
463 ConfigViewItem *item = findItem( resource );
471 void ConfigPage::resourceDeleted( Resource *resource )
473 kdDebug(5650) <<
"ConfigPage::resourceDeleted( " << resource->resourceName()
476 ConfigViewItem *item = findItem( resource );
482 ConfigViewItem *ConfigPage::findItem( Resource *resource )
485 for( i = mListView->firstChild(); i; i = i->nextSibling() ) {
486 ConfigViewItem *item =
static_cast<ConfigViewItem *
>( i );
487 if ( item->resource() == resource )
return item;
492 void ConfigPage::slotItemClicked( TQListViewItem *item )
494 ConfigViewItem *configItem =
static_cast<ConfigViewItem *
>( item );
495 if ( !configItem )
return;
497 if ( configItem->standard() && !configItem->isOn() ) {
498 KMessageBox::sorry(
this, i18n(
"You cannot deactivate the standard resource. Choose another standard resource first." ) );
499 configItem->setOn(
true );
503 if ( configItem->isOn() != configItem->resource()->isActive() ) {
504 emit changed(
true );
508 void ConfigPage::saveResourceSettings()
510 if ( mCurrentManager ) {
511 TQListViewItem *item = mListView->firstChild();
513 ConfigViewItem *configItem =
static_cast<ConfigViewItem *
>( item );
516 if ( configItem->standard() && !configItem->readOnly() &&
518 mCurrentManager->setStandardResource( configItem->resource() );
521 configItem->resource()->setActive( configItem->isOn() );
523 item = item->nextSibling();
525 mCurrentManager->writeConfig( mCurrentConfig );
527 if ( !mCurrentManager->standardResource() )
528 KMessageBox::sorry(
this, i18n(
"There is no valid standard resource! Please select one which is neither read-only nor inactive." ) );
534 #include "configpage.moc"