• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tderesources
 

tderesources

  • tderesources
configpage.cpp
1/*
2 This file is part of libtderesources.
3
4 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
5 Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
6 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22*/
23
24#include <tqgroupbox.h>
25#include <tqlabel.h>
26#include <tqlayout.h>
27
28#include <tdeapplication.h>
29#include <kcombobox.h>
30#include <kdebug.h>
31#include <tdelocale.h>
32#include <tdemessagebox.h>
33#include <ksimpleconfig.h>
34#include <tdestandarddirs.h>
35#include <kurlrequester.h>
36#include <tdelistview.h>
37#include <kbuttonbox.h>
38#include <ktrader.h>
39#include <kinputdialog.h>
40
41#include "resource.h"
42#include "configdialog.h"
43
44#include "configpage.h"
45
46namespace KRES {
47
48ResourcePageInfo::ResourcePageInfo() : TDEShared() {
49 mManager = 0L;
50 mConfig = 0L;
51}
52
53ResourcePageInfo::~ResourcePageInfo() {
54 //delete mManager;
55 mManager = 0L;
56 //delete mConfig;
57 mConfig = 0L;
58}
59
60
61class ConfigViewItem : public TQCheckListItem
62{
63 public:
64 ConfigViewItem( TQListView *parent, Resource* resource ) :
65 TQCheckListItem( parent, resource->resourceName(), CheckBox ),
66 mResource( resource ),
67 mIsStandard( false )
68 {
69 setText( 1, mResource->type() );
70 setOn( mResource->isActive() );
71 }
72
73 void setStandard( bool value )
74 {
75 setText( 2, ( value ? i18n( "Yes" ) : TQString::null ) );
76 mIsStandard = value;
77 }
78
79 bool standard() const { return mIsStandard; }
80 bool readOnly() const { return mResource->readOnly(); }
81
82 Resource *resource() { return mResource; }
83
84 void updateItem()
85 {
86 setOn( mResource->isActive() );
87 setText( 0, mResource->resourceName() );
88 setText( 1, mResource->type() );
89 setText( 2, mIsStandard ? i18n( "Yes" ) : TQString::null );
90 }
91
92 private:
93 Resource* mResource;
94
95 bool mIsStandard;
96};
97
98ConfigPage::ConfigPage( TQWidget *parent, const char *name )
99 : TQWidget( parent, name ),
100 mCurrentManager( 0 ),
101 mCurrentConfig( 0 )
102{
103 setCaption( i18n( "Resource Configuration" ) );
104
105 TQVBoxLayout *mainLayout = new TQVBoxLayout( this );
106
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 );
112
113 mFamilyCombo = new KComboBox( false, groupBox );
114 groupBoxLayout->addMultiCellWidget( mFamilyCombo, 0, 0, 0, 1 );
115
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" ) );
122
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 );
133 buttonBox->layout();
134
135 groupBoxLayout->addWidget( buttonBox, 1, 1 );
136
137 mainLayout->addWidget( groupBox );
138
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 * ) ) );
145
146 mLastItem = 0;
147
148 mConfig = new TDEConfig( "kcmtderesourcesrc" );
149 mConfig->setGroup( "General" );
150
151 load();
152}
153
154ConfigPage::~ConfigPage()
155{
156 TQValueList<TDESharedPtr<ResourcePageInfo> >::Iterator it;
157 for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) {
158 (*it)->mManager->removeObserver( this );
159 }
160
161 mConfig->writeEntry( "CurrentFamily", mFamilyCombo->currentItem() );
162 delete mConfig;
163 mConfig = 0;
164}
165
166void ConfigPage::load()
167{
168 kdDebug(5650) << "ConfigPage::load()" << endl;
169
170 mListView->clear();
171 mFamilyMap.clear();
172 mInfoMap.clear();
173 TQStringList familyDisplayNames;
174
175 // KDE-3.3 compatibility code: get families from the plugins
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 );
184 }
185
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 );
196 }
197 }
198
199 // Rest of the kde-3.3 compat code
200 TQStringList::ConstIterator cfit = compatFamilyNames.begin();
201 for ( ; cfit != compatFamilyNames.end(); ++cfit ) {
202 mFamilyMap.append( *cfit );
203 familyDisplayNames.append( *cfit );
204 loadManager( *cfit );
205 }
206
207 mCurrentManager = 0;
208
209 mFamilyCombo->clear();
210 mFamilyCombo->insertStringList( familyDisplayNames );
211
212 int currentFamily = mConfig->readNumEntry( "CurrentFamily", 0 );
213 mFamilyCombo->setCurrentItem( currentFamily );
214 slotFamilyChanged( currentFamily );
215 emit changed( false );
216}
217
218void ConfigPage::loadManager( const TQString& family )
219{
220 mCurrentManager = new Manager<Resource>( family );
221 if ( mCurrentManager ) {
222 mCurrentManager->addObserver( this );
223
224 ResourcePageInfo *info = new ResourcePageInfo;
225 info->mManager = mCurrentManager;
226 info->mConfig = new TDEConfig( KRES::ManagerImpl::defaultConfigFile( family ) );
227 info->mManager->readConfig( info->mConfig );
228
229 mInfoMap.append( TDESharedPtr<ResourcePageInfo>(info) );
230 }
231}
232
233void ConfigPage::save()
234{
235 saveResourceSettings();
236
237 TQValueList<TDESharedPtr<ResourcePageInfo> >::Iterator it;
238 for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it )
239 (*it)->mManager->writeConfig( (*it)->mConfig );
240
241 emit changed( false );
242}
243
244void ConfigPage::defaults()
245{
246}
247
248void ConfigPage::slotFamilyChanged( int pos )
249{
250 if ( pos < 0 || pos >= (int)mFamilyMap.count() )
251 return;
252
253 saveResourceSettings();
254
255 mFamily = mFamilyMap[ pos ];
256
257 mCurrentManager = mInfoMap[ pos ]->mManager;
258 mCurrentConfig = mInfoMap[ pos ]->mConfig;
259
260 if ( !mCurrentManager )
261 kdDebug(5650) << "ERROR: cannot create ResourceManager<Resource>( mFamily )" << endl;
262
263 mListView->clear();
264
265 if ( mCurrentManager->isEmpty() )
266 defaults();
267
268 Resource *standardResource = mCurrentManager->standardResource();
269
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 );
275 }
276
277 if ( mListView->childCount() == 0 ) {
278 defaults();
279 emit changed( true );
280 mCurrentManager->writeConfig( mCurrentConfig );
281 } else {
282 if ( !standardResource )
283 KMessageBox::sorry( this, i18n( "There is no standard resource! Please select one." ) );
284
285 emit changed( false );
286 }
287}
288
289void ConfigPage::slotAdd()
290{
291 if ( !mCurrentManager )
292 return;
293
294 TQStringList types = mCurrentManager->resourceTypeNames();
295 TQStringList descs = mCurrentManager->resourceTypeDescriptions();
296 bool ok = false;
297 TQString desc = KInputDialog::getItem( i18n( "Resource Configuration" ),
298 i18n( "Please select type of the new resource:" ), descs,
299 0, false, &ok, this );
300 if ( !ok )
301 return;
302
303 TQString type = types[ descs.findIndex( desc ) ];
304
305 // Create new resource
306 Resource *resource = mCurrentManager->createResource( type );
307 if ( !resource ) {
308 KMessageBox::error( this, i18n("Unable to create resource of type '%1'.")
309 .arg( type ) );
310 return;
311 }
312
313 resource->setResourceName( type + "-resource" );
314
315 ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" );
316
317 if ( dlg.exec() ) {
318 mCurrentManager->add( resource );
319
320 ConfigViewItem *item = new ConfigViewItem( mListView, resource );
321
322 mLastItem = item;
323
324 // if there are only read-only resources we'll set this resource
325 // as standard resource
326 if ( !resource->readOnly() ) {
327 bool onlyReadOnly = true;
328 TQListViewItem *it = mListView->firstChild();
329 while ( it != 0 ) {
330 ConfigViewItem *confIt = static_cast<ConfigViewItem*>( it );
331 if ( !confIt->readOnly() && confIt != item )
332 onlyReadOnly = false;
333
334 it = it->itemBelow();
335 }
336
337 if ( onlyReadOnly )
338 item->setStandard( true );
339 }
340
341 emit changed( true );
342 } else {
343 delete resource;
344 resource = 0;
345 }
346}
347
348void ConfigPage::slotRemove()
349{
350 if ( !mCurrentManager )
351 return;
352
353 TQListViewItem *item = mListView->currentItem();
354 ConfigViewItem *confItem = static_cast<ConfigViewItem*>( item );
355
356 if ( !confItem )
357 return;
358
359 if ( confItem->standard() ) {
360 KMessageBox::sorry( this, i18n( "You cannot remove your standard resource! Please select a new standard resource first." ) );
361 return;
362 }
363
364 mCurrentManager->remove( confItem->resource() );
365
366 if ( item == mLastItem )
367 mLastItem = 0;
368
369 mListView->takeItem( item );
370 delete item;
371
372 emit changed( true );
373}
374
375void ConfigPage::slotEdit()
376{
377 if ( !mCurrentManager )
378 return;
379
380 TQListViewItem *item = mListView->currentItem();
381 ConfigViewItem *configItem = static_cast<ConfigViewItem*>( item );
382 if ( !configItem )
383 return;
384
385 Resource *resource = configItem->resource();
386
387 ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" );
388
389 if ( dlg.exec() ) {
390 configItem->setText( 0, resource->resourceName() );
391 configItem->setText( 1, resource->type() );
392
393 if ( configItem->standard() && configItem->readOnly() ) {
394 KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) );
395 configItem->setStandard( false );
396 }
397
398 mCurrentManager->change( resource );
399 emit changed( true );
400 }
401}
402
403void ConfigPage::slotStandard()
404{
405 if ( !mCurrentManager )
406 return;
407
408 ConfigViewItem *item = static_cast<ConfigViewItem*>( mListView->currentItem() );
409 if ( !item )
410 return;
411
412 if ( item->readOnly() ) {
413 KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) );
414 return;
415 }
416
417 if ( !item->isOn() ) {
418 KMessageBox::sorry( this, i18n( "You cannot use an inactive resource as standard!" ) );
419 return;
420 }
421
422 TQListViewItem *it = mListView->firstChild();
423 while ( it != 0 ) {
424 ConfigViewItem *configItem = static_cast<ConfigViewItem*>( it );
425 if ( configItem->standard() )
426 configItem->setStandard( false );
427 it = it->itemBelow();
428 }
429
430 item->setStandard( true );
431 mCurrentManager->setStandardResource( item->resource() );
432
433 emit changed( true );
434}
435
436void ConfigPage::slotSelectionChanged()
437{
438 bool state = ( mListView->currentItem() != 0 );
439
440 mRemoveButton->setEnabled( state );
441 mEditButton->setEnabled( state );
442 mStandardButton->setEnabled( state );
443}
444
445void ConfigPage::resourceAdded( Resource *resource )
446{
447 kdDebug(5650) << "ConfigPage::resourceAdded( " << resource->resourceName()
448 << " )" << endl;
449
450 ConfigViewItem *item = new ConfigViewItem( mListView, resource );
451
452 item->setOn( resource->isActive() );
453
454 mLastItem = item;
455
456 emit changed( true );
457}
458
459void ConfigPage::resourceModified( Resource *resource )
460{
461 kdDebug(5650) << "ConfigPage::resourceModified( " << resource->resourceName()
462 << " )" << endl;
463 ConfigViewItem *item = findItem( resource );
464 if ( !item ) return;
465
466 // TODO: Reread resource config. Otherwise we won't see the modification.
467
468 item->updateItem();
469}
470
471void ConfigPage::resourceDeleted( Resource *resource )
472{
473 kdDebug(5650) << "ConfigPage::resourceDeleted( " << resource->resourceName()
474 << " )" << endl;
475
476 ConfigViewItem *item = findItem( resource );
477 if ( !item ) return;
478
479 delete item;
480}
481
482ConfigViewItem *ConfigPage::findItem( Resource *resource )
483{
484 TQListViewItem *i;
485 for( i = mListView->firstChild(); i; i = i->nextSibling() ) {
486 ConfigViewItem *item = static_cast<ConfigViewItem *>( i );
487 if ( item->resource() == resource ) return item;
488 }
489 return 0;
490}
491
492void ConfigPage::slotItemClicked( TQListViewItem *item )
493{
494 ConfigViewItem *configItem = static_cast<ConfigViewItem *>( item );
495 if ( !configItem ) return;
496
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 );
500 return;
501 }
502
503 if ( configItem->isOn() != configItem->resource()->isActive() ) {
504 emit changed( true );
505 }
506}
507
508void ConfigPage::saveResourceSettings()
509{
510 if ( mCurrentManager ) {
511 TQListViewItem *item = mListView->firstChild();
512 while ( item ) {
513 ConfigViewItem *configItem = static_cast<ConfigViewItem *>( item );
514
515 // check if standard resource
516 if ( configItem->standard() && !configItem->readOnly() &&
517 configItem->isOn() )
518 mCurrentManager->setStandardResource( configItem->resource() );
519
520 // check if active or passive resource
521 configItem->resource()->setActive( configItem->isOn() );
522
523 item = item->nextSibling();
524 }
525 mCurrentManager->writeConfig( mCurrentConfig );
526
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." ) );
529 }
530}
531
532}
533
534#include "configpage.moc"
535

tderesources

Skip menu "tderesources"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

tderesources

Skip menu "tderesources"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tderesources by doxygen 1.9.4
This website is maintained by Timothy Pearson.