24#include <dcopclient.h>
26#include <tdeaboutdata.h>
27#include <tdeapplication.h>
30#include <tdestandarddirs.h>
35#include "managerimpl.h"
36#include "manageriface_stub.h"
40ManagerImpl::ManagerImpl( ManagerNotifier *
notifier,
const TQString &
family )
41 : DCOPObject(
"ManagerIface_" +
family.
utf8() ),
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 ( !
tdeApp->dcopClient()->isRegistered() ) {
52 tdeApp->dcopClient()->registerAs(
"TDEResourcesManager" );
56 kdDebug(5650) <<
"Connecting DCOP signals..." <<
endl;
58 "signalKResourceAdded( TQString, TQString )",
59 "dcopKResourceAdded( TQString, TQString )",
false ) )
60 kdWarning(5650) <<
"Could not connect ResourceAdded signal!" <<
endl;
63 "signalKResourceModified( TQString, TQString )",
64 "dcopKResourceModified( TQString, TQString )",
false ) )
65 kdWarning(5650) <<
"Could not connect ResourceModified signal!" <<
endl;
68 "signalKResourceDeleted( TQString, TQString )",
69 "dcopKResourceDeleted( TQString, TQString )",
false ) )
70 kdWarning(5650) <<
"Could not connect ResourceDeleted signal!" <<
endl;
72 tdeApp->dcopClient()->setNotifications(
true );
75ManagerImpl::~ManagerImpl()
77 kdDebug(5650) <<
"ManagerImpl::~ManagerImpl()" <<
endl;
79 Resource::List::ConstIterator
it;
87void ManagerImpl::createStandardConfig()
90 TQString
file = defaultConfigFile( mFamily );
99 kdDebug(5650) <<
"ManagerImpl::readConfig()" <<
endl;
105 createStandardConfig();
112 mConfig->setGroup(
"General" );
114 TQStringList
keys = mConfig->readListEntry(
"ResourceKeys" );
115 keys += mConfig->readListEntry(
"PassiveResourceKeys" );
117 TQString
standardKey = mConfig->readEntry(
"Standard" );
120 readResourceConfig( *
it,
false );
128 kdDebug(5650) <<
"ManagerImpl::writeConfig()" <<
endl;
131 createStandardConfig();
140 Resource::List::Iterator
it;
142 writeResourceConfig( *
it,
false );
144 TQString
key = (*it)->identifier();
145 if( (*it)->isActive() )
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;
166void ManagerImpl::add(
Resource *resource )
170 if ( mResources.isEmpty() ) {
171 mStandard = resource;
174 mResources.append( resource );
177 writeResourceConfig( resource,
true );
182void 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;
196void ManagerImpl::change(
Resource *resource )
198 writeResourceConfig( resource,
true );
200 signalKResourceModified( mId, resource->
identifier() );
210Resource *ManagerImpl::standardResource()
215void ManagerImpl::setStandardResource(
Resource *resource )
217 mStandard = resource;
222void ManagerImpl::dcopKResourceAdded( TQString
managerId, TQString resourceId )
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;
246void ManagerImpl::dcopKResourceModified( TQString
managerId, TQString resourceId )
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;
262void ManagerImpl::dcopKResourceDeleted( TQString
managerId, TQString resourceId )
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;
284TQStringList ManagerImpl::resourceNames()
288 Resource::List::ConstIterator
it;
290 result.append( (*it)->resourceName() );
295Resource::List *ManagerImpl::resourceList()
304 Resource::List::ConstIterator
it;
315 Resource::List::ConstIterator
it;
317 if ( (*it)->isActive() ==
active ) {
324Resource *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" );
351 mStandard = resource;
355 TQStringList
activeKeys = mConfig->readListEntry(
"ResourceKeys" );
358 mResources.append( resource );
363void ManagerImpl::writeResourceConfig(
Resource *resource,
bool checkActive )
369 if ( !mConfig ) createStandardConfig();
371 mConfig->setGroup(
"Resource_" +
key );
374 mConfig->setGroup(
"General" );
375 TQString
standardKey = mConfig->readEntry(
"Standard" );
378 mConfig->writeEntry(
"Standard", resource->
identifier() );
380 mConfig->writeEntry(
"Standard",
"" );
383 TQStringList
activeKeys = mConfig->readListEntry(
"ResourceKeys" );
384 TQStringList
passiveKeys = mConfig->readListEntry(
"PassiveResourceKeys" );
388 mConfig->writeEntry(
"PassiveResourceKeys",
passiveKeys );
392 mConfig->writeEntry(
"ResourceKeys",
activeKeys );
394 }
else if ( !resource->
isActive() ) {
397 mConfig->writeEntry(
"ResourceKeys",
activeKeys );
401 mConfig->writeEntry(
"PassiveResourceKeys",
passiveKeys );
409void ManagerImpl::removeResource(
Resource *resource )
413 if ( !mConfig ) createStandardConfig();
415 mConfig->setGroup(
"General" );
416 TQStringList
activeKeys = mConfig->readListEntry(
"ResourceKeys" );
419 mConfig->writeEntry(
"ResourceKeys",
activeKeys );
421 TQStringList
passiveKeys = mConfig->readListEntry(
"PassiveResourceKeys" );
423 mConfig->writeEntry(
"PassiveResourceKeys",
passiveKeys );
426 TQString
standardKey = mConfig->readEntry(
"Standard" );
428 mConfig->writeEntry(
"Standard",
"" );
431 mConfig->deleteGroup(
"Resource_" + resource->
identifier() );
435Resource *ManagerImpl::getResource(
const TQString &identifier )
437 Resource::List::ConstIterator
it;
439 if ( (*it)->identifier() == identifier )
445TQString ManagerImpl::defaultConfigFile(
const TQString &
family )
447 return TQString(
"tderesources/%1/stdrc" ).arg(
family );
static Factory * self(const TQString &resourceFamily)
Returns the global resource factory.
This class provides a manager for resources of a specified family.
Iterator end()
Return Iterator indicating end of resource list.
Iterator begin()
Return Iterator on first resource.
void remove(Resource *resource)
Remove resource from manager.
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.