41 #include <QTreeWidget>
42 #include <QTreeWidgetItem>
44 #include <kcombobox.h>
46 #include <klocalizedstring.h>
47 #include <kmessagebox.h>
49 #include <kstandarddirs.h>
50 #include <kurlrequester.h>
51 #include <kdialogbuttonbox.h>
52 #include <kservicetypetrader.h>
53 #include <kinputdialog.h>
54 #include <QtCore/QList>
61 class ResourcePageInfo::Private
65 ResourcePageInfo::ResourcePageInfo() : d( new KRES::ResourcePageInfo::Private )
71 ResourcePageInfo::~ResourcePageInfo()
83 ConfigViewItem(
QTreeWidget *parent, Resource *resource ) :
89 void setStandard(
bool value )
91 setText( 2, ( value ? i18nc(
"yes, a standard resource",
"Yes" ) :
QString() ) );
95 bool standard()
const {
return mIsStandard; }
96 bool readOnly()
const {
return mResource->readOnly(); }
98 Resource *resource() {
return mResource; }
102 setCheckState( 0, mResource->isActive() ? Qt::Checked : Qt::Unchecked );
103 setText( 0, mResource->resourceName() );
104 setText( 1, mResource->type() );
105 setText( 2, mIsStandard ? i18nc(
"yes, a standard resource",
"Yes" ) :
QString() );
118 class ConfigPage::Private
121 void loadManager(
const QString &family, ConfigPage *page );
122 void saveResourceSettings( ConfigPage *page );
124 Manager<Resource> *mCurrentManager;
125 KConfig *mCurrentConfig;
126 KConfigGroup *mConfigGroup;
131 KComboBox *mFamilyCombo;
141 ConfigPage::ConfigPage(
QWidget *parent )
142 :
QWidget( parent ), d( new KRES::ConfigPage::Private )
144 setWindowTitle( i18n(
"Resource Configuration" ) );
153 d->mFamilyCombo =
new KComboBox(
false, groupBox );
154 groupBoxLayout->
addWidget( d->mFamilyCombo, 0, 0, 1, 2 );
156 d->mCurrentManager = 0;
157 d->mCurrentConfig = 0;
159 d->mListView->setColumnCount( 3 );
161 headerLabels << i18nc(
"@title:column resource name",
"Name" )
162 << i18nc(
"@title:column resource type",
"Type" )
163 << i18nc(
"@title:column a standard resource?",
"Standard" );
166 groupBoxLayout->
addWidget( d->mListView, 1, 0 );
167 connect( d->mListView, SIGNAL(itemDoubleClicked(
QTreeWidgetItem*,
int)),
168 this, SLOT(slotEdit()) );
170 KDialogButtonBox *buttonBox =
new KDialogButtonBox( groupBox, Qt::Vertical );
171 d->mAddButton = buttonBox->addButton( i18n(
"&Add..." ),
172 KDialogButtonBox::ActionRole,
173 this, SLOT(slotAdd()) );
175 d->mRemoveButton = buttonBox->addButton( i18n(
"&Remove" ),
176 KDialogButtonBox::ActionRole,
177 this, SLOT(slotRemove()) );
178 d->mRemoveButton->setEnabled(
false );
180 d->mEditButton = buttonBox->addButton( i18n(
"&Edit..." ),
181 KDialogButtonBox::ActionRole,
182 this, SLOT(slotEdit()) );
183 d->mEditButton->setEnabled(
false );
185 d->mStandardButton = buttonBox->addButton( i18n(
"&Use as Standard" ),
186 KDialogButtonBox::ActionRole,
187 this, SLOT(slotStandard()) );
188 d->mStandardButton->setEnabled(
false );
191 groupBoxLayout->
addWidget( buttonBox, 1, 1 );
195 connect( d->mFamilyCombo, SIGNAL(activated(
int)),
196 SLOT(slotFamilyChanged(
int)) );
197 connect( d->mListView, SIGNAL(itemSelectionChanged()),
198 SLOT(slotSelectionChanged()) );
204 d->mConfigGroup =
new KConfigGroup(
new KConfig(
QLatin1String(
"kcmkresourcesrc") ),
"General" );
209 ConfigPage::~ConfigPage()
212 for ( it = d->mInfoMap.begin(); it != d->mInfoMap.end(); ++it ) {
213 ( *it )->mManager->removeObserver(
this );
216 d->mConfigGroup->writeEntry(
"CurrentFamily", d->mFamilyCombo->currentIndex() );
217 delete d->mConfigGroup->config();
218 delete d->mConfigGroup;
223 void ConfigPage::load()
227 d->mListView->clear();
228 d->mFamilyMap.clear();
234 const KService::List plugins = KServiceTypeTrader::self()->query(
QLatin1String(
"KResources/Plugin") );
235 KService::List::ConstIterator it = plugins.
begin();
236 KService::List::ConstIterator end = plugins.end();
237 for ( ; it != end; ++it ) {
239 if ( compatFamilyNames.
indexOf( family ) == -1 ) {
240 compatFamilyNames.
append( family );
244 const KService::List managers = KServiceTypeTrader::self()->query(
QLatin1String(
"KResources/Manager") );
245 KService::List::ConstIterator m_it;
246 for ( m_it = managers.begin(); m_it != managers.end(); ++m_it ) {
248 familyDisplayNames.
append( displayName );
252 d->mFamilyMap.append( family );
253 kDebug() <<
"Loading manager for family " << family;
254 d->loadManager( family,
this );
255 kDebug() <<
"Manager for family " << family <<
" loaded.";
261 for ( ; cfit != compatFamilyNames.
constEnd(); ++cfit ) {
262 d->mFamilyMap.append( *cfit );
263 familyDisplayNames.
append( *cfit );
264 d->loadManager( *cfit,
this );
267 d->mCurrentManager = 0;
269 d->mFamilyCombo->clear();
270 d->mFamilyCombo->insertItems( 0, familyDisplayNames );
272 int currentFamily = d->mConfigGroup->readEntry(
"CurrentFamily", 0 );
273 d->mFamilyCombo->setCurrentIndex( currentFamily );
274 slotFamilyChanged( currentFamily );
275 emit changed(
false );
278 void ConfigPage::Private::loadManager(
const QString &family, ConfigPage *page )
280 mCurrentManager =
new Manager<Resource>( family );
281 if ( mCurrentManager ) {
282 mCurrentManager->addObserver( page );
284 ResourcePageInfo *info =
new ResourcePageInfo;
285 info->mManager = mCurrentManager;
286 info->mConfig =
new KConfig( KRES::ManagerImpl::defaultConfigFile( family ) );
287 info->mManager->readConfig( info->mConfig );
289 mInfoMap.append( KSharedPtr<ResourcePageInfo>( info ) );
293 void ConfigPage::save()
295 d->saveResourceSettings(
this );
298 for ( it = d->mInfoMap.begin(); it != d->mInfoMap.end(); ++it ) {
299 ( *it )->mManager->writeConfig( ( *it )->mConfig );
302 emit changed(
false );
305 void ConfigPage::defaults()
309 void ConfigPage::slotFamilyChanged(
int pos )
311 if ( pos < 0 || pos >= (
int)d->mFamilyMap.count() ) {
315 d->saveResourceSettings(
this );
317 d->mFamily = d->mFamilyMap[
pos ];
319 d->mCurrentManager = d->mInfoMap[
pos ]->mManager;
320 d->mCurrentConfig = d->mInfoMap[
pos ]->mConfig;
322 if ( !d->mCurrentManager ) {
323 kDebug() <<
"ERROR: cannot create ResourceManager<Resource>( mFamily )";
326 d->mListView->clear();
328 if ( d->mCurrentManager->isEmpty() ) {
332 Resource *standardResource = d->mCurrentManager->standardResource();
334 Manager<Resource>::Iterator it;
335 for ( it = d->mCurrentManager->begin(); it != d->mCurrentManager->end(); ++it ) {
336 ConfigViewItem *item =
new ConfigViewItem( d->mListView, *it );
337 if ( *it == standardResource ) {
338 item->setStandard(
true );
342 if ( d->mListView->topLevelItemCount() == 0 ) {
344 emit changed(
true );
345 d->mCurrentManager->writeConfig( d->mCurrentConfig );
347 if ( !standardResource ) {
348 KMessageBox::sorry(
this, i18n(
"There is no standard resource. Please select one." ) );
351 emit changed(
false );
355 void ConfigPage::slotAdd()
357 if ( !d->mCurrentManager ) {
361 QStringList types = d->mCurrentManager->resourceTypeNames();
362 QStringList descs = d->mCurrentManager->resourceTypeDescriptions();
364 QString desc = KInputDialog::getItem( i18n(
"Resource Configuration" ),
365 i18n(
"Please select type of the new resource:" ), descs,
366 0,
false, &ok,
this );
374 Resource *resource = d->mCurrentManager->createResource( type );
377 this, i18n(
"Unable to create resource of type '%1'.", type ) );
381 resource->setResourceName( type +
QLatin1String(
"-resource") );
383 ConfigDialog dlg(
this, d->mFamily, resource );
386 d->mCurrentManager->add( resource );
388 ConfigViewItem *item =
new ConfigViewItem( d->mListView, resource );
394 if ( !resource->readOnly() ) {
395 bool onlyReadOnly =
true;
396 for (
int i = 0; i < d->mListView->topLevelItemCount(); ++i ) {
397 ConfigViewItem *confIt =
static_cast<ConfigViewItem*
>( d->mListView->topLevelItem( i ) );
398 if ( !confIt->readOnly() && confIt != item ) {
399 onlyReadOnly =
false;
403 if ( onlyReadOnly ) {
404 item->setStandard(
true );
408 emit changed(
true );
415 void ConfigPage::slotRemove()
417 if ( !d->mCurrentManager ) {
422 ConfigViewItem *confItem =
static_cast<ConfigViewItem*
>( item );
428 if ( confItem->standard() ) {
429 KMessageBox::sorry(
this,
430 i18n(
"You cannot remove your standard resource. "
431 "Please select a new standard resource first." ) );
435 d->mCurrentManager->remove( confItem->resource() );
437 if ( item == d->mLastItem ) {
441 d->mListView->takeTopLevelItem( d->mListView->indexOfTopLevelItem( item ) );
444 emit changed(
true );
447 void ConfigPage::slotEdit()
449 if ( !d->mCurrentManager ) {
454 ConfigViewItem *configItem =
static_cast<ConfigViewItem*
>( item );
459 Resource *resource = configItem->resource();
461 ConfigDialog dlg(
this, d->mFamily, resource );
464 configItem->setText( 0, resource->resourceName() );
465 configItem->setText( 1, resource->type() );
467 if ( configItem->standard() && configItem->readOnly() ) {
468 KMessageBox::sorry(
this, i18n(
"You cannot use a read-only resource as standard." ) );
469 configItem->setStandard(
false );
472 d->mCurrentManager->change( resource );
473 emit changed(
true );
477 void ConfigPage::slotStandard()
479 if ( !d->mCurrentManager ) {
483 ConfigViewItem *item =
static_cast<ConfigViewItem*
>( d->mListView->currentItem() );
488 if ( item->readOnly() ) {
489 KMessageBox::sorry(
this, i18n(
"You cannot use a read-only resource as standard." ) );
493 if ( !item->isOn() ) {
494 KMessageBox::sorry(
this, i18n(
"You cannot use an inactive resource as standard." ) );
498 for (
int i = 0; i < d->mListView->topLevelItemCount(); ++i ) {
499 ConfigViewItem *configItem =
static_cast<ConfigViewItem*
>( d->mListView->topLevelItem( i ) );
500 if ( configItem->standard() ) {
501 configItem->setStandard(
false );
505 item->setStandard(
true );
506 d->mCurrentManager->setStandardResource( item->resource() );
508 emit changed(
true );
511 void ConfigPage::slotSelectionChanged()
513 bool state = ( d->mListView->currentItem() != 0 );
515 d->mRemoveButton->setEnabled( state );
516 d->mEditButton->setEnabled( state );
517 d->mStandardButton->setEnabled( state );
520 void ConfigPage::resourceAdded( Resource *resource )
522 kDebug() << resource->resourceName();
524 ConfigViewItem *item =
new ConfigViewItem( d->mListView, resource );
526 item->setCheckState( 0, resource->isActive()? Qt::Checked : Qt::Unchecked );
530 emit changed(
true );
533 void ConfigPage::resourceModified( Resource *resource )
535 kDebug() << resource->resourceName();
536 ConfigViewItem *item = findItem( resource );
546 void ConfigPage::resourceDeleted( Resource *resource )
548 kDebug() << resource->resourceName();
550 ConfigViewItem *item = findItem( resource );
558 ConfigViewItem *ConfigPage::findItem( Resource *resource )
560 for (
int i = 0; i < d->mListView->topLevelItemCount(); ++i ) {
561 ConfigViewItem *item =
static_cast<ConfigViewItem *
>( d->mListView->topLevelItem( i ) );
562 if ( item->resource() == resource ) {
571 ConfigViewItem *configItem =
static_cast<ConfigViewItem *
>( item );
576 if ( configItem->standard() && !configItem->isOn() ) {
577 KMessageBox::sorry(
this,
578 i18n(
"You cannot deactivate the standard resource. "
579 "Choose another standard resource first." ) );
580 configItem->setCheckState( 0, Qt::Checked );
584 if ( configItem->isOn() != configItem->resource()->isActive() ) {
585 emit changed(
true );
589 void ConfigPage::Private::saveResourceSettings( ConfigPage *page )
591 if ( mCurrentManager ) {
592 for (
int i = 0; i < mListView->topLevelItemCount(); ++i ) {
593 ConfigViewItem *configItem =
static_cast<ConfigViewItem *
>( mListView->topLevelItem( i ) );
595 if ( configItem->standard() && !configItem->readOnly() &&
596 configItem->isOn() ) {
597 mCurrentManager->setStandardResource( configItem->resource() );
601 configItem->resource()->setActive( configItem->isOn() );
603 mCurrentManager->writeConfig( mCurrentConfig );
605 if ( !mCurrentManager->standardResource() ) {
606 KMessageBox::sorry( page,
607 i18n(
"There is no valid standard resource. "
608 "Please select one which is neither read-only nor inactive." ) );
This file is part of the KDE resource framework and defines the ConfigDialog class.
void append(const T &value)
int removeAll(const T &value)
void setMargin(int margin)
void setText(int column, const QString &text)
int indexOf(const QRegExp &rx, int from) const
const_iterator constEnd() const
const_iterator constBegin() const
This file is part of the KDE resource framework and defines the ConfigPage class. ...