author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /microkde/kresources/configpage.cpp | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
Diffstat (limited to 'microkde/kresources/configpage.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | microkde/kresources/configpage.cpp | 507 |
1 files changed, 507 insertions, 0 deletions
diff --git a/microkde/kresources/configpage.cpp b/microkde/kresources/configpage.cpp new file mode 100644 index 0000000..0f1469d --- a/dev/null +++ b/microkde/kresources/configpage.cpp | |||
@@ -0,0 +1,507 @@ | |||
1 | /* | ||
2 | This file is part of libkresources. | ||
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., 59 Temple Place - Suite 330, | ||
21 | Boston, MA 02111-1307, USA. | ||
22 | */ | ||
23 | |||
24 | #include <qgroupbox.h> | ||
25 | #include <qinputdialog.h> | ||
26 | #include <qlabel.h> | ||
27 | #include <qlayout.h> | ||
28 | |||
29 | #include <kapplication.h> | ||
30 | #include <kcombobox.h> | ||
31 | #include <kdebug.h> | ||
32 | #include <klocale.h> | ||
33 | #include <kmessagebox.h> | ||
34 | #include <ksimpleconfig.h> | ||
35 | #include <kstandarddirs.h> | ||
36 | #include <kurlrequester.h> | ||
37 | #include <klistview.h> | ||
38 | #include <kbuttonbox.h> | ||
39 | //US #include <ktrader.h> | ||
40 | |||
41 | #include "resource.h" | ||
42 | #include "configdialog.h" | ||
43 | |||
44 | #include "configpage.h" | ||
45 | |||
46 | //US | ||
47 | #include <qpushbutton.h> | ||
48 | #include <qfile.h> | ||
49 | #include <kglobal.h> | ||
50 | |||
51 | using namespace KRES; | ||
52 | |||
53 | class ConfigViewItem : public QCheckListItem | ||
54 | { | ||
55 | public: | ||
56 | ConfigViewItem( QListView *parent, Resource* resource ) : | ||
57 | QCheckListItem( parent, resource->resourceName(), CheckBox ), | ||
58 | mResource( resource ), | ||
59 | mIsStandard( false ) | ||
60 | { | ||
61 | setText( 1, mResource->type() ); | ||
62 | setOn( mResource->isActive() ); | ||
63 | } | ||
64 | |||
65 | void setStandard( bool value ) | ||
66 | { | ||
67 | setText( 2, ( value ? i18n( "Yes" ) : QString::null ) ); | ||
68 | mIsStandard = value; | ||
69 | } | ||
70 | |||
71 | bool standard() const { return mIsStandard; } | ||
72 | bool readOnly() const { return mResource->readOnly(); } | ||
73 | |||
74 | Resource *resource() { return mResource; } | ||
75 | |||
76 | private: | ||
77 | Resource* mResource; | ||
78 | |||
79 | bool mIsStandard; | ||
80 | }; | ||
81 | |||
82 | ConfigPage::ConfigPage( QWidget *parent, const char *name ) | ||
83 | : QWidget( parent, name ), | ||
84 | mCurrentManager( 0 ), | ||
85 | mCurrentConfig( 0 ) | ||
86 | { | ||
87 | setCaption( i18n( "Resource Configuration" ) ); | ||
88 | |||
89 | QVBoxLayout *mainLayout = new QVBoxLayout( this ); | ||
90 | |||
91 | QGroupBox *groupBox = new QGroupBox( i18n( "Resources" ), this ); | ||
92 | groupBox->setColumnLayout(0, Qt::Vertical ); | ||
93 | groupBox->layout()->setSpacing( 6 ); | ||
94 | groupBox->layout()->setMargin( 11 ); | ||
95 | QGridLayout *groupBoxLayout = new QGridLayout( groupBox->layout(), 2, 2 ); | ||
96 | |||
97 | //US mFamilyCombo = new KComboBox( false, groupBox ); | ||
98 | mFamilyCombo = new KComboBox( groupBox ); | ||
99 | groupBoxLayout->addMultiCellWidget( mFamilyCombo, 0, 0, 0, 1 ); | ||
100 | |||
101 | mListView = new KListView( groupBox ); | ||
102 | mListView->setAllColumnsShowFocus( true ); | ||
103 | mListView->addColumn( i18n( "Name" ) ); | ||
104 | mListView->addColumn( i18n( "Type" ) ); | ||
105 | mListView->addColumn( i18n( "Standard" ) ); | ||
106 | |||
107 | groupBoxLayout->addWidget( mListView, 1, 0 ); | ||
108 | |||
109 | KButtonBox *buttonBox = new KButtonBox( groupBox, Vertical ); | ||
110 | mAddButton = buttonBox->addButton( i18n( "&Add..." ), this, SLOT(slotAdd()) ); | ||
111 | mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT(slotRemove()) ); | ||
112 | mRemoveButton->setEnabled( false ); | ||
113 | mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT(slotEdit()) ); | ||
114 | mEditButton->setEnabled( false ); | ||
115 | mStandardButton = buttonBox->addButton( i18n( "&Use as Standard" ), this, SLOT(slotStandard()) ); | ||
116 | mStandardButton->setEnabled( false ); | ||
117 | buttonBox->layout(); | ||
118 | |||
119 | groupBoxLayout->addWidget( buttonBox, 1, 1 ); | ||
120 | |||
121 | mainLayout->addWidget( groupBox ); | ||
122 | |||
123 | connect( mFamilyCombo, SIGNAL( activated( int ) ), | ||
124 | SLOT( slotFamilyChanged( int ) ) ); | ||
125 | connect( mListView, SIGNAL( selectionChanged() ), | ||
126 | SLOT( slotSelectionChanged() ) ); | ||
127 | connect( mListView, SIGNAL( clicked( QListViewItem * ) ), | ||
128 | SLOT( slotItemClicked( QListViewItem * ) ) ); | ||
129 | |||
130 | mLastItem = 0; | ||
131 | |||
132 | //US mConfig = new KConfig( "kcmkresourcesrc" ); | ||
133 | mConfig = new KConfig( locateLocal( "config", "kcmkresourcesrc") ); | ||
134 | mConfig->setGroup( "General" ); | ||
135 | |||
136 | load(); | ||
137 | } | ||
138 | |||
139 | ConfigPage::~ConfigPage() | ||
140 | { | ||
141 | QValueList<ResourcePageInfo>::Iterator it; | ||
142 | for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) { | ||
143 | (*it).mManager->removeListener( this ); | ||
144 | delete (*it).mManager; | ||
145 | delete (*it).mConfig; | ||
146 | } | ||
147 | |||
148 | mConfig->writeEntry( "CurrentFamily", mFamilyCombo->currentItem() ); | ||
149 | delete mConfig; | ||
150 | mConfig = 0; | ||
151 | } | ||
152 | |||
153 | void ConfigPage::load() | ||
154 | { | ||
155 | kdDebug(5650) << "ConfigPage::load()" << endl; | ||
156 | |||
157 | mListView->clear(); | ||
158 | |||
159 | //US we remove the dynamic pluginloader, and set the one family we need (contact) manually. | ||
160 | |||
161 | //US KTrader::OfferList plugins = KTrader::self()->query( "KResources/Plugin" ); | ||
162 | //US KTrader::OfferList::ConstIterator it; | ||
163 | //US for ( it = plugins.begin(); it != plugins.end(); ++it ) { | ||
164 | //US QVariant tmp = (*it)->property( "X-KDE-ResourceFamily" ); | ||
165 | //US QString family = tmp.toString(); | ||
166 | |||
167 | QString family = "contact"; | ||
168 | if ( !family.isEmpty() ) { | ||
169 | if ( !mFamilyMap.contains( family ) ) { | ||
170 | mCurrentManager = new Manager<Resource>( family ); | ||
171 | if ( mCurrentManager ) { | ||
172 | mFamilyMap.append( family ); | ||
173 | mCurrentManager->addListener( this ); | ||
174 | |||
175 | ResourcePageInfo info; | ||
176 | info.mManager = mCurrentManager; | ||
177 | QString configDir = KGlobal::dirs()->saveLocation( "config" ); | ||
178 | //QString configDir = KStandardDirs::appDir() + "/config"; | ||
179 | if ( family == "contact" && QFile::exists( configDir + "/kabcrc" ) ) { | ||
180 | info.mConfig = new KConfig( locateLocal( "config", "kabcrc" ) ); | ||
181 | } else if ( family == "calendar" && QFile::exists( configDir + "/kcalrc" ) ) { | ||
182 | info.mConfig = new KConfig( locateLocal( "config", "kcalrc" ) ); | ||
183 | } else { | ||
184 | QString configFile = locateLocal( "config", QString( "kresources/%1/stdrc" ).arg( family ) ); | ||
185 | info.mConfig = new KConfig( configFile ); | ||
186 | } | ||
187 | info.mManager->readConfig( info.mConfig ); | ||
188 | |||
189 | mInfoMap.append( info ); | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | //US } | ||
194 | mCurrentManager = 0; | ||
195 | |||
196 | mFamilyCombo->insertStringList( mFamilyMap ); | ||
197 | |||
198 | int currentFamily = mConfig->readNumEntry( "CurrentFamily", 0 ); | ||
199 | mFamilyCombo->setCurrentItem( currentFamily ); | ||
200 | slotFamilyChanged( currentFamily ); | ||
201 | } | ||
202 | |||
203 | void ConfigPage::save() | ||
204 | { | ||
205 | saveResourceSettings(); | ||
206 | |||
207 | QValueList<ResourcePageInfo>::Iterator it; | ||
208 | for ( it = mInfoMap.begin(); it != mInfoMap.end(); ++it ) | ||
209 | (*it).mManager->writeConfig( (*it).mConfig ); | ||
210 | |||
211 | emit changed( false ); | ||
212 | } | ||
213 | |||
214 | void ConfigPage::defaults() | ||
215 | { | ||
216 | } | ||
217 | |||
218 | void ConfigPage::slotFamilyChanged( int pos ) | ||
219 | { | ||
220 | if ( pos < 0 || pos >= (int)mFamilyMap.count() ) | ||
221 | return; | ||
222 | |||
223 | saveResourceSettings(); | ||
224 | |||
225 | mFamily = mFamilyMap[ pos ]; | ||
226 | |||
227 | //US qDebug("ConfigPage::slotFamilyChanged 4 family=%s", mFamily.latin1()); | ||
228 | |||
229 | mCurrentManager = mInfoMap[ pos ].mManager; | ||
230 | mCurrentConfig = mInfoMap[ pos ].mConfig; | ||
231 | |||
232 | if ( !mCurrentManager ) | ||
233 | kdDebug(5650) << "ERROR: cannot create ResourceManager<Resource>( mFamily )" << endl; | ||
234 | |||
235 | mListView->clear(); | ||
236 | |||
237 | if ( mCurrentManager->isEmpty() ) { | ||
238 | //US qDebug("ConfigPage::slotFamilyChanged 4.1 mCurrentManager=%ul", mCurrentManager ); | ||
239 | |||
240 | defaults(); | ||
241 | } | ||
242 | |||
243 | Resource *standardResource = mCurrentManager->standardResource(); | ||
244 | |||
245 | //US qDebug("ConfigPage::slotFamilyChanged 4.4 resourcename=%s", standardResource->resourceName().latin1()); | ||
246 | |||
247 | |||
248 | Manager<Resource>::Iterator it; | ||
249 | for ( it = mCurrentManager->begin(); it != mCurrentManager->end(); ++it ) { | ||
250 | ConfigViewItem *item = new ConfigViewItem( mListView, *it ); | ||
251 | if ( *it == standardResource ) | ||
252 | item->setStandard( true ); | ||
253 | } | ||
254 | |||
255 | if ( mListView->childCount() == 0 ) { | ||
256 | //US qDebug("ConfigPage::slotFamilyChanged 4.5 "); | ||
257 | |||
258 | defaults(); | ||
259 | emit changed( true ); | ||
260 | mCurrentManager->writeConfig( mCurrentConfig ); | ||
261 | } else { | ||
262 | //US qDebug("ConfigPage::slotFamilyChanged 4.6 "); | ||
263 | |||
264 | if ( !standardResource ) { | ||
265 | KMessageBox::sorry( this, i18n( "There is no standard resource! Please select one." ) ); | ||
266 | |||
267 | //US qDebug("ConfigPage::slotFamilyChanged 4.7" ); | ||
268 | |||
269 | } | ||
270 | |||
271 | emit changed( false ); | ||
272 | } | ||
273 | } | ||
274 | |||
275 | void ConfigPage::slotAdd() | ||
276 | { | ||
277 | if ( !mCurrentManager ) | ||
278 | return; | ||
279 | |||
280 | QStringList types = mCurrentManager->resourceTypeNames(); | ||
281 | QStringList descs = mCurrentManager->resourceTypeDescriptions(); | ||
282 | bool ok = false; | ||
283 | QString desc = QInputDialog::getItem( i18n( "Resource Configuration" ), | ||
284 | i18n( "Please select type of the new resource:" ), descs, 0, | ||
285 | false, &ok, this ); | ||
286 | if ( !ok ) | ||
287 | return; | ||
288 | |||
289 | QString type = types[ descs.findIndex( desc ) ]; | ||
290 | |||
291 | // Create new resource | ||
292 | Resource *resource = mCurrentManager->createResource( type ); | ||
293 | if ( !resource ) { | ||
294 | KMessageBox::error( this, i18n("Unable to create resource of type '%1'.") | ||
295 | .arg( type ) ); | ||
296 | return; | ||
297 | } | ||
298 | |||
299 | resource->setResourceName( type + "-resource" ); | ||
300 | |||
301 | ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" ); | ||
302 | |||
303 | if ( dlg.exec() ) { | ||
304 | mCurrentManager->add( resource ); | ||
305 | |||
306 | ConfigViewItem *item = new ConfigViewItem( mListView, resource ); | ||
307 | |||
308 | mLastItem = item; | ||
309 | |||
310 | // if there are only read-only resources we'll set this resource | ||
311 | // as standard resource | ||
312 | if ( !resource->readOnly() ) { | ||
313 | bool onlyReadOnly = true; | ||
314 | QListViewItem *it = mListView->firstChild(); | ||
315 | while ( it != 0 ) { | ||
316 | ConfigViewItem *confIt = static_cast<ConfigViewItem*>( it ); | ||
317 | if ( !confIt->readOnly() && confIt != item ) | ||
318 | onlyReadOnly = false; | ||
319 | |||
320 | it = it->itemBelow(); | ||
321 | } | ||
322 | |||
323 | if ( onlyReadOnly ) | ||
324 | item->setStandard( true ); | ||
325 | } | ||
326 | |||
327 | emit changed( true ); | ||
328 | } else { | ||
329 | delete resource; | ||
330 | resource = 0; | ||
331 | } | ||
332 | } | ||
333 | |||
334 | void ConfigPage::slotRemove() | ||
335 | { | ||
336 | if ( !mCurrentManager ) | ||
337 | return; | ||
338 | |||
339 | QListViewItem *item = mListView->currentItem(); | ||
340 | ConfigViewItem *confItem = static_cast<ConfigViewItem*>( item ); | ||
341 | |||
342 | if ( !confItem ) | ||
343 | return; | ||
344 | |||
345 | if ( confItem->standard() ) { | ||
346 | KMessageBox::sorry( this, i18n( "You cannot remove your standard resource!\n Please select a new standard resource first." ) ); | ||
347 | return; | ||
348 | } | ||
349 | |||
350 | mCurrentManager->remove( confItem->resource() ); | ||
351 | |||
352 | if ( item == mLastItem ) | ||
353 | mLastItem = 0; | ||
354 | |||
355 | mListView->takeItem( item ); | ||
356 | delete item; | ||
357 | |||
358 | emit changed( true ); | ||
359 | } | ||
360 | |||
361 | void ConfigPage::slotEdit() | ||
362 | { | ||
363 | if ( !mCurrentManager ) | ||
364 | return; | ||
365 | |||
366 | QListViewItem *item = mListView->currentItem(); | ||
367 | ConfigViewItem *configItem = static_cast<ConfigViewItem*>( item ); | ||
368 | if ( !configItem ) | ||
369 | return; | ||
370 | |||
371 | Resource *resource = configItem->resource(); | ||
372 | |||
373 | ConfigDialog dlg( this, mFamily, resource, "KRES::ConfigDialog" ); | ||
374 | |||
375 | if ( dlg.exec() ) { | ||
376 | configItem->setText( 0, resource->resourceName() ); | ||
377 | configItem->setText( 1, resource->type() ); | ||
378 | |||
379 | if ( configItem->standard() && configItem->readOnly() ) { | ||
380 | KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) ); | ||
381 | configItem->setStandard( false ); | ||
382 | } | ||
383 | |||
384 | mCurrentManager->resourceChanged( resource ); | ||
385 | emit changed( true ); | ||
386 | } | ||
387 | } | ||
388 | |||
389 | void ConfigPage::slotStandard() | ||
390 | { | ||
391 | if ( !mCurrentManager ) | ||
392 | return; | ||
393 | |||
394 | ConfigViewItem *item = static_cast<ConfigViewItem*>( mListView->currentItem() ); | ||
395 | if ( !item ) | ||
396 | return; | ||
397 | |||
398 | if ( item->readOnly() ) { | ||
399 | KMessageBox::sorry( this, i18n( "You cannot use a read-only resource as standard!" ) ); | ||
400 | return; | ||
401 | } | ||
402 | |||
403 | if ( !item->isOn() ) { | ||
404 | KMessageBox::sorry( this, i18n( "You cannot use an inactive resource as standard!" ) ); | ||
405 | return; | ||
406 | } | ||
407 | |||
408 | QListViewItem *it = mListView->firstChild(); | ||
409 | while ( it != 0 ) { | ||
410 | ConfigViewItem *configItem = static_cast<ConfigViewItem*>( it ); | ||
411 | if ( configItem->standard() ) | ||
412 | configItem->setStandard( false ); | ||
413 | it = it->itemBelow(); | ||
414 | } | ||
415 | |||
416 | item->setStandard( true ); | ||
417 | mCurrentManager->setStandardResource( item->resource() ); | ||
418 | emit changed( true ); | ||
419 | |||
420 | } | ||
421 | |||
422 | void ConfigPage::slotSelectionChanged() | ||
423 | { | ||
424 | bool state = ( mListView->currentItem() != 0 ); | ||
425 | |||
426 | mRemoveButton->setEnabled( state ); | ||
427 | mEditButton->setEnabled( state ); | ||
428 | mStandardButton->setEnabled( state ); | ||
429 | } | ||
430 | |||
431 | void ConfigPage::resourceAdded( Resource* resource ) | ||
432 | { | ||
433 | qDebug("ConfigPage::resourceAdded : %s", resource->resourceName().latin1()); | ||
434 | kdDebug(5650) << "ConfigPage::resourceAdded( " << resource->resourceName() << " )" << endl; | ||
435 | ConfigViewItem *item = new ConfigViewItem( mListView, resource ); | ||
436 | |||
437 | // FIXME: this sucks. This should be in the config file, | ||
438 | // or application-dependent, in which case it's always Off | ||
439 | item->setOn( false ); | ||
440 | |||
441 | mLastItem = item; | ||
442 | |||
443 | emit changed( true ); | ||
444 | } | ||
445 | |||
446 | void ConfigPage::resourceModified( Resource* resource ) | ||
447 | { | ||
448 | qDebug("ConfigPage::resourceModified : %s", resource->resourceName().latin1()); | ||
449 | kdDebug(5650) << "ConfigPage::resourceModified( " << resource->resourceName() << " )" << endl; | ||
450 | } | ||
451 | |||
452 | void ConfigPage::resourceDeleted( Resource* resource ) | ||
453 | { | ||
454 | qDebug("ConfigPage::resourceDeleted : %s", resource->resourceName().latin1()); | ||
455 | kdDebug(5650) << "ConfigPage::resourceDeleted( " << resource->resourceName() << " )" << endl; | ||
456 | } | ||
457 | |||
458 | void ConfigPage::slotItemClicked( QListViewItem *item ) | ||
459 | { | ||
460 | ConfigViewItem *configItem = static_cast<ConfigViewItem *>( item ); | ||
461 | if ( !configItem ) return; | ||
462 | |||
463 | if ( configItem->standard() && !configItem->isOn() ) { | ||
464 | KMessageBox::sorry( this, i18n( "You cannot deactivate the standard resource. Choose another standard resource first." ) ); | ||
465 | configItem->setOn( true ); | ||
466 | return; | ||
467 | } | ||
468 | |||
469 | if ( configItem->isOn() != configItem->resource()->isActive() ) { | ||
470 | emit changed( true ); | ||
471 | } | ||
472 | } | ||
473 | |||
474 | void ConfigPage::saveResourceSettings() | ||
475 | { | ||
476 | qDebug("ConfigPage::saveResourceSettings() begin"); | ||
477 | |||
478 | if ( mCurrentManager ) { | ||
479 | |||
480 | QListViewItem *item = mListView->firstChild(); | ||
481 | while ( item ) { | ||
482 | ConfigViewItem *configItem = static_cast<ConfigViewItem*>( item ); | ||
483 | |||
484 | // check if standard resource | ||
485 | if ( configItem->standard() && !configItem->readOnly() && | ||
486 | configItem->isOn() ) { | ||
487 | |||
488 | mCurrentManager->setStandardResource( configItem->resource() ); | ||
489 | } | ||
490 | |||
491 | // check if active or passive resource | ||
492 | configItem->resource()->setActive( configItem->isOn() ); | ||
493 | |||
494 | item = item->nextSibling(); | ||
495 | } | ||
496 | mCurrentManager->writeConfig( mCurrentConfig ); | ||
497 | |||
498 | if ( !mCurrentManager->standardResource() ) | ||
499 | KMessageBox::sorry( this, i18n( "There is no valid standard resource! Please select one which is neither read-only nor inactive." ) ); | ||
500 | } | ||
501 | |||
502 | qDebug("ConfigPage::saveResourceSettings() end"); | ||
503 | |||
504 | } | ||
505 | |||
506 | //US #include "configpage.moc" | ||
507 | |||