24 #include <dcopclient.h>
26 #include <tdeaboutdata.h>
27 #include <tdeapplication.h>
29 #include <tdeconfig.h>
30 #include <kstandarddirs.h>
35 #include "managerimpl.h"
36 #include "manageriface_stub.h"
40 ManagerImpl::ManagerImpl( ManagerNotifier *notifier,
const TQString &family )
41 : DCOPObject(
"ManagerIface_" + family.utf8() ),
42 mNotifier( notifier ),
43 mFamily( family ), mConfig( 0 ), mStdConfig( 0 ), mStandard( 0 ),
44 mFactory( 0 ), mConfigRead( false )
46 kdDebug(5650) <<
"ManagerImpl::ManagerImpl()" << endl;
48 mId = TDEApplication::randomString( 8 );
51 if ( !kapp->dcopClient()->isRegistered() ) {
52 kapp->dcopClient()->registerAs(
"TDEResourcesManager" );
53 kapp->dcopClient()->setDefaultObject( objId() );
56 kdDebug(5650) <<
"Connecting DCOP signals..." << endl;
57 if ( !connectDCOPSignal( 0,
"ManagerIface_" + family.utf8(),
58 "signalKResourceAdded( TQString, TQString )",
59 "dcopKResourceAdded( TQString, TQString )",
false ) )
60 kdWarning(5650) <<
"Could not connect ResourceAdded signal!" << endl;
62 if ( !connectDCOPSignal( 0,
"ManagerIface_" + family.utf8(),
63 "signalKResourceModified( TQString, TQString )",
64 "dcopKResourceModified( TQString, TQString )",
false ) )
65 kdWarning(5650) <<
"Could not connect ResourceModified signal!" << endl;
67 if ( !connectDCOPSignal( 0,
"ManagerIface_" + family.utf8(),
68 "signalKResourceDeleted( TQString, TQString )",
69 "dcopKResourceDeleted( TQString, TQString )",
false ) )
70 kdWarning(5650) <<
"Could not connect ResourceDeleted signal!" << endl;
72 kapp->dcopClient()->setNotifications(
true );
75 ManagerImpl::~ManagerImpl()
77 kdDebug(5650) <<
"ManagerImpl::~ManagerImpl()" << endl;
79 Resource::List::ConstIterator it;
80 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
87 void ManagerImpl::createStandardConfig()
90 TQString file = defaultConfigFile( mFamily );
91 mStdConfig =
new TDEConfig( file );
97 void ManagerImpl::readConfig( TDEConfig *cfg )
99 kdDebug(5650) <<
"ManagerImpl::readConfig()" << endl;
102 mFactory = Factory::self( mFamily );
105 createStandardConfig();
112 mConfig->setGroup(
"General" );
114 TQStringList keys = mConfig->readListEntry(
"ResourceKeys" );
115 keys += mConfig->readListEntry(
"PassiveResourceKeys" );
117 TQString standardKey = mConfig->readEntry(
"Standard" );
119 for ( TQStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
120 readResourceConfig( *it,
false );
126 void ManagerImpl::writeConfig( TDEConfig *cfg )
128 kdDebug(5650) <<
"ManagerImpl::writeConfig()" << endl;
131 createStandardConfig();
136 TQStringList activeKeys;
137 TQStringList passiveKeys;
140 Resource::List::Iterator it;
141 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
142 writeResourceConfig( *it,
false );
144 TQString key = (*it)->identifier();
145 if( (*it)->isActive() )
146 activeKeys.append( key );
148 passiveKeys.append( key );
153 kdDebug(5650) <<
"Saving general info" << endl;
154 mConfig->setGroup(
"General" );
155 mConfig->writeEntry(
"ResourceKeys", activeKeys );
156 mConfig->writeEntry(
"PassiveResourceKeys", passiveKeys );
158 mConfig->writeEntry(
"Standard", mStandard->identifier() );
160 mConfig->writeEntry(
"Standard",
"" );
163 kdDebug(5650) <<
"ManagerImpl::save() finished" << endl;
166 void ManagerImpl::add(
Resource *resource )
170 if ( mResources.isEmpty() ) {
171 mStandard = resource;
174 mResources.append( resource );
177 writeResourceConfig( resource,
true );
179 signalKResourceAdded( mId, resource->
identifier() );
182 void ManagerImpl::remove(
Resource *resource )
184 if ( mStandard == resource ) mStandard = 0;
185 removeResource( resource );
187 mResources.remove( resource );
189 signalKResourceDeleted( mId, resource->
identifier() );
193 kdDebug(5650) <<
"Finished ManagerImpl::remove()" << endl;
196 void ManagerImpl::change(
Resource *resource )
198 writeResourceConfig( resource,
true );
200 signalKResourceModified( mId, resource->
identifier() );
203 void ManagerImpl::setActive(
Resource *resource,
bool active )
205 if ( resource && resource->
isActive() != active ) {
210 Resource *ManagerImpl::standardResource()
215 void ManagerImpl::setStandardResource(
Resource *resource )
217 mStandard = resource;
222 void ManagerImpl::dcopKResourceAdded( TQString managerId, TQString resourceId )
224 if ( managerId == mId ) {
225 kdDebug(5650) <<
"Ignore DCOP notification to myself" << endl;
228 kdDebug(5650) <<
"Receive DCOP call: added resource " << resourceId << endl;
230 if ( getResource( resourceId ) ) {
231 kdDebug(5650) <<
"This resource is already known to me." << endl;
234 if ( !mConfig ) createStandardConfig();
236 mConfig->reparseConfiguration();
237 Resource *resource = readResourceConfig( resourceId,
true );
240 mNotifier->notifyResourceAdded( resource );
242 kdError() <<
"Received DCOP: resource added for unknown resource "
243 << resourceId << endl;
246 void ManagerImpl::dcopKResourceModified( TQString managerId, TQString resourceId )
248 if ( managerId == mId ) {
249 kdDebug(5650) <<
"Ignore DCOP notification to myself" << endl;
252 kdDebug(5650) <<
"Receive DCOP call: modified resource " << resourceId << endl;
254 Resource *resource = getResource( resourceId );
256 mNotifier->notifyResourceModified( resource );
258 kdError() <<
"Received DCOP: resource modified for unknown resource "
259 << resourceId << endl;
262 void ManagerImpl::dcopKResourceDeleted( TQString managerId, TQString resourceId )
264 if ( managerId == mId ) {
265 kdDebug(5650) <<
"Ignore DCOP notification to myself" << endl;
268 kdDebug(5650) <<
"Receive DCOP call: deleted resource " << resourceId << endl;
270 Resource *resource = getResource( resourceId );
272 mNotifier->notifyResourceDeleted( resource );
274 kdDebug(5650) <<
"Removing item from mResources" << endl;
276 if ( mStandard == resource )
278 mResources.remove( resource );
280 kdError() <<
"Received DCOP: resource deleted for unknown resource "
281 << resourceId << endl;
284 TQStringList ManagerImpl::resourceNames()
288 Resource::List::ConstIterator it;
289 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
290 result.append( (*it)->resourceName() );
295 Resource::List *ManagerImpl::resourceList()
300 TQPtrList<Resource> ManagerImpl::resources()
302 TQPtrList<Resource> result;
304 Resource::List::ConstIterator it;
305 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
306 result.append( *it );
311 TQPtrList<Resource> ManagerImpl::resources(
bool active )
313 TQPtrList<Resource> result;
315 Resource::List::ConstIterator it;
316 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
317 if ( (*it)->isActive() == active ) {
318 result.append( *it );
324 Resource *ManagerImpl::readResourceConfig(
const TQString &identifier,
327 kdDebug(5650) <<
"ManagerImpl::readResourceConfig() " << identifier << endl;
330 kdError(5650) <<
"ManagerImpl::readResourceConfig: mFactory is 0. Did the app forget to call readConfig?" << endl;
334 mConfig->setGroup(
"Resource_" + identifier );
336 TQString type = mConfig->readEntry(
"ResourceType" );
337 TQString name = mConfig->readEntry(
"ResourceName" );
338 Resource *resource = mFactory->resource( type, mConfig );
340 kdDebug(5650) <<
"Failed to create resource with id " << identifier << endl;
345 resource->setIdentifier( identifier );
347 mConfig->setGroup(
"General" );
349 TQString standardKey = mConfig->readEntry(
"Standard" );
350 if ( standardKey == identifier ) {
351 mStandard = resource;
355 TQStringList activeKeys = mConfig->readListEntry(
"ResourceKeys" );
356 resource->
setActive( activeKeys.contains( identifier ) );
358 mResources.append( resource );
363 void ManagerImpl::writeResourceConfig(
Resource *resource,
bool checkActive )
367 kdDebug(5650) <<
"Saving resource " << key << endl;
369 if ( !mConfig ) createStandardConfig();
371 mConfig->setGroup(
"Resource_" + key );
374 mConfig->setGroup(
"General" );
375 TQString standardKey = mConfig->readEntry(
"Standard" );
377 if ( resource == mStandard && standardKey != key )
378 mConfig->writeEntry(
"Standard", resource->
identifier() );
379 else if ( resource != mStandard && standardKey == key )
380 mConfig->writeEntry(
"Standard",
"" );
383 TQStringList activeKeys = mConfig->readListEntry(
"ResourceKeys" );
384 TQStringList passiveKeys = mConfig->readListEntry(
"PassiveResourceKeys" );
386 if ( passiveKeys.contains( key ) ) {
387 passiveKeys.remove( key );
388 mConfig->writeEntry(
"PassiveResourceKeys", passiveKeys );
390 if ( !activeKeys.contains( key ) ) {
391 activeKeys.append( key );
392 mConfig->writeEntry(
"ResourceKeys", activeKeys );
394 }
else if ( !resource->
isActive() ) {
395 if ( activeKeys.contains( key ) ) {
396 activeKeys.remove( key );
397 mConfig->writeEntry(
"ResourceKeys", activeKeys );
399 if ( !passiveKeys.contains( key ) ) {
400 passiveKeys.append( key );
401 mConfig->writeEntry(
"PassiveResourceKeys", passiveKeys );
409 void ManagerImpl::removeResource(
Resource *resource )
413 if ( !mConfig ) createStandardConfig();
415 mConfig->setGroup(
"General" );
416 TQStringList activeKeys = mConfig->readListEntry(
"ResourceKeys" );
417 if ( activeKeys.contains( key ) ) {
418 activeKeys.remove( key );
419 mConfig->writeEntry(
"ResourceKeys", activeKeys );
421 TQStringList passiveKeys = mConfig->readListEntry(
"PassiveResourceKeys" );
422 passiveKeys.remove( key );
423 mConfig->writeEntry(
"PassiveResourceKeys", passiveKeys );
426 TQString standardKey = mConfig->readEntry(
"Standard" );
427 if ( standardKey == key ) {
428 mConfig->writeEntry(
"Standard",
"" );
431 mConfig->deleteGroup(
"Resource_" + resource->
identifier() );
435 Resource *ManagerImpl::getResource(
const TQString &identifier )
437 Resource::List::ConstIterator it;
438 for ( it = mResources.begin(); it != mResources.end(); ++it ) {
439 if ( (*it)->identifier() == identifier )
445 TQString ManagerImpl::defaultConfigFile(
const TQString &family )
447 return TQString(
"tderesources/%1/stdrc" ).arg( family );
This class provides a resource which is managed in a general way.
bool isActive() const
Return true, if the resource is active.
virtual void writeConfig(TDEConfig *config)
Write configuration information for this resource to a configuration file.
void setActive(bool active)
Sets, if the resource is active.
TQString identifier() const
Returns a unique identifier.