1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*
This file is part of libkresources.
Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <dcopclient.h>
#include <kaboutdata.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kconfig.h>
#include <kstandarddirs.h>
#include "resource.h"
#include "factory.h"
#include "manager.h"
#include "managerimpl.h"
#include "manageriface_stub.h"
using namespace KRES;
ManagerImpl::ManagerImpl( ManagerNotifier *notifier, const TQString &family )
: DCOPObject( "ManagerIface_" + family.utf8() ),
mNotifier( notifier ),
mFamily( family ), mConfig( 0 ), mStdConfig( 0 ), mStandard( 0 ),
mFactory( 0 ), mConfigRead( false )
{
kdDebug(5650) << "ManagerImpl::ManagerImpl()" << endl;
mId = TDEApplication::randomString( 8 );
// Register with DCOP
if ( !kapp->dcopClient()->isRegistered() ) {
kapp->dcopClient()->registerAs( "KResourcesManager" );
kapp->dcopClient()->setDefaultObject( objId() );
}
kdDebug(5650) << "Connecting DCOP signals..." << endl;
if ( !connectDCOPSignal( 0, "ManagerIface_" + family.utf8(),
"signalKResourceAdded( TQString, TQString )",
"dcopKResourceAdded( TQString, TQString )", false ) )
kdWarning(5650) << "Could not connect ResourceAdded signal!" << endl;
if ( !connectDCOPSignal( 0, "ManagerIface_" + family.utf8(),
"signalKResourceModified( TQString, TQString )",
"dcopKResourceModified( TQString, TQString )", false ) )
kdWarning(5650) << "Could not connect ResourceModified signal!" << endl;
if ( !connectDCOPSignal( 0, "ManagerIface_" + family.utf8(),
"signalKResourceDeleted( TQString, TQString )",
"dcopKResourceDeleted( TQString, TQString )", false ) )
kdWarning(5650) << "Could not connect ResourceDeleted signal!" << endl;
kapp->dcopClient()->setNotifications( true );
}
ManagerImpl::~ManagerImpl()
{
kdDebug(5650) << "ManagerImpl::~ManagerImpl()" << endl;
Resource::List::ConstIterator it;
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
delete *it;
}
delete mStdConfig;
}
void ManagerImpl::createStandardConfig()
{
if ( !mStdConfig ) {
TQString file = defaultConfigFile( mFamily );
mStdConfig = new TDEConfig( file );
}
mConfig = mStdConfig;
}
void ManagerImpl::readConfig( TDEConfig *cfg )
{
kdDebug(5650) << "ManagerImpl::readConfig()" << endl;
delete mFactory;
mFactory = Factory::self( mFamily );
if ( !cfg ) {
createStandardConfig();
} else {
mConfig = cfg;
}
mStandard = 0;
mConfig->setGroup( "General" );
TQStringList keys = mConfig->readListEntry( "ResourceKeys" );
keys += mConfig->readListEntry( "PassiveResourceKeys" );
TQString standardKey = mConfig->readEntry( "Standard" );
for ( TQStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
readResourceConfig( *it, false );
}
mConfigRead = true;
}
void ManagerImpl::writeConfig( TDEConfig *cfg )
{
kdDebug(5650) << "ManagerImpl::writeConfig()" << endl;
if ( !cfg ) {
createStandardConfig();
} else {
mConfig = cfg;
}
TQStringList activeKeys;
TQStringList passiveKeys;
// First write all keys, collect active and passive keys on the way
Resource::List::Iterator it;
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
writeResourceConfig( *it, false );
TQString key = (*it)->identifier();
if( (*it)->isActive() )
activeKeys.append( key );
else
passiveKeys.append( key );
}
// And then the general group
kdDebug(5650) << "Saving general info" << endl;
mConfig->setGroup( "General" );
mConfig->writeEntry( "ResourceKeys", activeKeys );
mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
if ( mStandard )
mConfig->writeEntry( "Standard", mStandard->identifier() );
else
mConfig->writeEntry( "Standard", "" );
mConfig->sync();
kdDebug(5650) << "ManagerImpl::save() finished" << endl;
}
void ManagerImpl::add( Resource *resource )
{
resource->setActive( true );
if ( mResources.isEmpty() ) {
mStandard = resource;
}
mResources.append( resource );
if ( mConfigRead )
writeResourceConfig( resource, true );
signalKResourceAdded( mId, resource->identifier() );
}
void ManagerImpl::remove( Resource *resource )
{
if ( mStandard == resource ) mStandard = 0;
removeResource( resource );
mResources.remove( resource );
signalKResourceDeleted( mId, resource->identifier() );
delete resource;
kdDebug(5650) << "Finished ManagerImpl::remove()" << endl;
}
void ManagerImpl::change( Resource *resource )
{
writeResourceConfig( resource, true );
signalKResourceModified( mId, resource->identifier() );
}
void ManagerImpl::setActive( Resource *resource, bool active )
{
if ( resource && resource->isActive() != active ) {
resource->setActive( active );
}
}
Resource *ManagerImpl::standardResource()
{
return mStandard;
}
void ManagerImpl::setStandardResource( Resource *resource )
{
mStandard = resource;
}
// DCOP asynchronous functions
void ManagerImpl::dcopKResourceAdded( TQString managerId, TQString resourceId )
{
if ( managerId == mId ) {
kdDebug(5650) << "Ignore DCOP notification to myself" << endl;
return;
}
kdDebug(5650) << "Receive DCOP call: added resource " << resourceId << endl;
if ( getResource( resourceId ) ) {
kdDebug(5650) << "This resource is already known to me." << endl;
}
if ( !mConfig ) createStandardConfig();
mConfig->reparseConfiguration();
Resource *resource = readResourceConfig( resourceId, true );
if ( resource ) {
mNotifier->notifyResourceAdded( resource );
} else
kdError() << "Received DCOP: resource added for unknown resource "
<< resourceId << endl;
}
void ManagerImpl::dcopKResourceModified( TQString managerId, TQString resourceId )
{
if ( managerId == mId ) {
kdDebug(5650) << "Ignore DCOP notification to myself" << endl;
return;
}
kdDebug(5650) << "Receive DCOP call: modified resource " << resourceId << endl;
Resource *resource = getResource( resourceId );
if ( resource ) {
mNotifier->notifyResourceModified( resource );
} else
kdError() << "Received DCOP: resource modified for unknown resource "
<< resourceId << endl;
}
void ManagerImpl::dcopKResourceDeleted( TQString managerId, TQString resourceId )
{
if ( managerId == mId ) {
kdDebug(5650) << "Ignore DCOP notification to myself" << endl;
return;
}
kdDebug(5650) << "Receive DCOP call: deleted resource " << resourceId << endl;
Resource *resource = getResource( resourceId );
if ( resource ) {
mNotifier->notifyResourceDeleted( resource );
kdDebug(5650) << "Removing item from mResources" << endl;
// Now delete item
if ( mStandard == resource )
mStandard = 0;
mResources.remove( resource );
} else
kdError() << "Received DCOP: resource deleted for unknown resource "
<< resourceId << endl;
}
TQStringList ManagerImpl::resourceNames()
{
TQStringList result;
Resource::List::ConstIterator it;
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
result.append( (*it)->resourceName() );
}
return result;
}
Resource::List *ManagerImpl::resourceList()
{
return &mResources;
}
TQPtrList<Resource> ManagerImpl::resources()
{
TQPtrList<Resource> result;
Resource::List::ConstIterator it;
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
result.append( *it );
}
return result;
}
TQPtrList<Resource> ManagerImpl::resources( bool active )
{
TQPtrList<Resource> result;
Resource::List::ConstIterator it;
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
if ( (*it)->isActive() == active ) {
result.append( *it );
}
}
return result;
}
Resource *ManagerImpl::readResourceConfig( const TQString &identifier,
bool checkActive )
{
kdDebug(5650) << "ManagerImpl::readResourceConfig() " << identifier << endl;
if ( !mFactory ) {
kdError(5650) << "ManagerImpl::readResourceConfig: mFactory is 0. Did the app forget to call readConfig?" << endl;
return 0;
}
mConfig->setGroup( "Resource_" + identifier );
TQString type = mConfig->readEntry( "ResourceType" );
TQString name = mConfig->readEntry( "ResourceName" );
Resource *resource = mFactory->resource( type, mConfig );
if ( !resource ) {
kdDebug(5650) << "Failed to create resource with id " << identifier << endl;
return 0;
}
if ( resource->identifier().isEmpty() )
resource->setIdentifier( identifier );
mConfig->setGroup( "General" );
TQString standardKey = mConfig->readEntry( "Standard" );
if ( standardKey == identifier ) {
mStandard = resource;
}
if ( checkActive ) {
TQStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
resource->setActive( activeKeys.contains( identifier ) );
}
mResources.append( resource );
return resource;
}
void ManagerImpl::writeResourceConfig( Resource *resource, bool checkActive )
{
TQString key = resource->identifier();
kdDebug(5650) << "Saving resource " << key << endl;
if ( !mConfig ) createStandardConfig();
mConfig->setGroup( "Resource_" + key );
resource->writeConfig( mConfig );
mConfig->setGroup( "General" );
TQString standardKey = mConfig->readEntry( "Standard" );
if ( resource == mStandard && standardKey != key )
mConfig->writeEntry( "Standard", resource->identifier() );
else if ( resource != mStandard && standardKey == key )
mConfig->writeEntry( "Standard", "" );
if ( checkActive ) {
TQStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
TQStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" );
if ( resource->isActive() ) {
if ( passiveKeys.contains( key ) ) { // remove it from passive list
passiveKeys.remove( key );
mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
}
if ( !activeKeys.contains( key ) ) { // add it to active list
activeKeys.append( key );
mConfig->writeEntry( "ResourceKeys", activeKeys );
}
} else if ( !resource->isActive() ) {
if ( activeKeys.contains( key ) ) { // remove it from active list
activeKeys.remove( key );
mConfig->writeEntry( "ResourceKeys", activeKeys );
}
if ( !passiveKeys.contains( key ) ) { // add it to passive list
passiveKeys.append( key );
mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
}
}
}
mConfig->sync();
}
void ManagerImpl::removeResource( Resource *resource )
{
TQString key = resource->identifier();
if ( !mConfig ) createStandardConfig();
mConfig->setGroup( "General" );
TQStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
if ( activeKeys.contains( key ) ) {
activeKeys.remove( key );
mConfig->writeEntry( "ResourceKeys", activeKeys );
} else {
TQStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" );
passiveKeys.remove( key );
mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
}
TQString standardKey = mConfig->readEntry( "Standard" );
if ( standardKey == key ) {
mConfig->writeEntry( "Standard", "" );
}
mConfig->deleteGroup( "Resource_" + resource->identifier() );
mConfig->sync();
}
Resource *ManagerImpl::getResource( const TQString &identifier )
{
Resource::List::ConstIterator it;
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
if ( (*it)->identifier() == identifier )
return *it;
}
return 0;
}
TQString ManagerImpl::defaultConfigFile( const TQString &family )
{
return TQString( "kresources/%1/stdrc" ).arg( family );
}
|