23 #include "managerimpl.h"
25 #include <kaboutdata.h>
29 #include <kstandarddirs.h>
30 #include <kconfiggroup.h>
32 #include <QtDBus/QtDBus>
37 #include "kresourcesmanageradaptor.h"
41 class ManagerImpl::ManagerImplPrivate
50 Resource::List mResources;
56 ManagerImpl::ManagerImpl(
ManagerNotifier *notifier,
const QString &family )
57 : d( new ManagerImplPrivate )
59 d->mNotifier = notifier;
65 d->mConfigRead =
false;
67 new KResourcesManagerAdaptor(
this );
68 const QString dBusPath = QLatin1String(
"/ManagerIface_" ) + family;
69 QDBusConnection::sessionBus().registerObject( dBusPath,
this );
72 d->mId = KRandom::randomString( 8 );
75 QDBusConnection::sessionBus().registerService( QLatin1String(
"org.kde.KResourcesManager") );
77 QDBusConnection::sessionBus().connect( QString(), dBusPath,
78 QLatin1String(
"org.kde.KResourcesManager"), QLatin1String(
"signalKResourceAdded"),
79 this, SLOT(dbusKResourceAdded(QString,QString)));
80 QDBusConnection::sessionBus().connect( QString(), dBusPath,
81 QLatin1String(
"org.kde.KResourcesManager"), QLatin1String(
"signalKResourceModified"),
82 this, SLOT(dbusKResourceModified(QString,QString)));
83 QDBusConnection::sessionBus().connect( QString(), dBusPath,
84 QLatin1String(
"org.kde.KResourcesManager"), QLatin1String(
"signalKResourceDeleted"),
85 this, SLOT(dbusKResourceDeleted(QString,QString)));
88 ManagerImpl::~ManagerImpl()
92 qDeleteAll( d->mResources );
97 void ManagerImpl::createStandardConfig()
99 if ( !d->mStdConfig ) {
100 QString file = defaultConfigFile( d->mFamily );
101 d->mStdConfig =
new KConfig( file );
104 d->mConfig = d->mStdConfig;
107 void ManagerImpl::readConfig( KConfig *cfg )
112 d->mFactory->reloadConfig();
118 createStandardConfig();
124 KConfigGroup group = d->mConfig->group(
"General" );
126 QStringList keys = group.readEntry(
"ResourceKeys", QStringList() );
127 keys += group.readEntry(
"PassiveResourceKeys", QStringList() );
129 const QString standardKey = group.readEntry(
"Standard" );
131 for ( QStringList::const_iterator it = keys.constBegin(); it != keys.constEnd(); ++it ) {
132 readResourceConfig( *it,
false );
135 d->mConfigRead =
true;
138 void ManagerImpl::writeConfig( KConfig *cfg )
143 createStandardConfig();
148 QStringList activeKeys;
149 QStringList passiveKeys;
152 Resource::List::Iterator it;
153 for ( it = d->mResources.begin(); it != d->mResources.end(); ++it ) {
154 writeResourceConfig( *it,
false );
156 QString key = ( *it )->identifier();
157 if ( ( *it )->isActive() ) {
158 activeKeys.append( key );
160 passiveKeys.append( key );
166 kDebug() <<
"Saving general info";
167 KConfigGroup group = d->mConfig->group(
"General" );
168 group.writeEntry(
"ResourceKeys", activeKeys );
169 group.writeEntry(
"PassiveResourceKeys", passiveKeys );
170 if ( d->mStandard ) {
171 group.writeEntry(
"Standard", d->mStandard->identifier() );
173 group.writeEntry(
"Standard", QString() );
177 kDebug() <<
"finished";
180 void ManagerImpl::add(
Resource *resource )
184 if ( d->mResources.isEmpty() ) {
185 d->mStandard = resource;
188 d->mResources.append( resource );
190 if ( d->mConfigRead ) {
191 writeResourceConfig( resource,
true );
194 signalKResourceAdded( d->mId, resource->
identifier() );
197 void ManagerImpl::remove(
Resource *resource )
199 if ( d->mStandard == resource ) {
202 removeResource( resource );
204 d->mResources.removeAll( resource );
206 signalKResourceDeleted( d->mId, resource->
identifier() );
210 kDebug() <<
"Finished";
213 void ManagerImpl::change(
Resource *resource )
215 writeResourceConfig( resource,
true );
217 signalKResourceModified( d->mId, resource->
identifier() );
220 void ManagerImpl::setActive(
Resource *resource,
bool active )
222 if ( resource && resource->
isActive() != active ) {
227 Resource *ManagerImpl::standardResource()
232 void ManagerImpl::setStandardResource(
Resource *resource )
234 d->mStandard = resource;
239 void ManagerImpl::dbusKResourceAdded(
const QString &managerId,
240 const QString &resourceId )
242 if ( managerId == d->mId ) {
243 kDebug() <<
"Ignore D-Bus notification to myself";
246 kDebug() <<
"Receive D-Bus call: added resource" << resourceId;
248 if ( getResource( resourceId ) ) {
249 kDebug() <<
"This resource is already known to me.";
253 createStandardConfig();
256 d->mConfig->reparseConfiguration();
257 Resource *resource = readResourceConfig( resourceId,
true );
260 d->mNotifier->notifyResourceAdded( resource );
262 kError() <<
"Received D-Bus: resource added for unknown resource"
267 void ManagerImpl::dbusKResourceModified(
const QString &managerId,
268 const QString &resourceId )
270 if ( managerId == d->mId ) {
271 kDebug() <<
"Ignore D-Bus notification to myself";
274 kDebug() <<
"Receive D-Bus call: modified resource" << resourceId;
276 Resource *resource = getResource( resourceId );
278 d->mNotifier->notifyResourceModified( resource );
280 kError() <<
"Received D-Bus: resource modified for unknown resource"
285 void ManagerImpl::dbusKResourceDeleted(
const QString &managerId,
286 const QString &resourceId )
288 if ( managerId == d->mId ) {
289 kDebug() <<
"Ignore D-Bus notification to myself";
292 kDebug() <<
"Receive D-Bus call: deleted resource" << resourceId;
294 Resource *resource = getResource( resourceId );
296 d->mNotifier->notifyResourceDeleted( resource );
298 kDebug() <<
"Removing item from mResources";
300 if ( d->mStandard == resource ) {
303 d->mResources.removeAll( resource );
305 kError() <<
"Received D-Bus: resource deleted for unknown resource"
310 QStringList ManagerImpl::resourceNames()
314 Resource::List::ConstIterator it;
315 Resource::List::ConstIterator end(d->mResources.constEnd());
316 for ( it = d->mResources.constBegin(); it != end; ++it ) {
317 result.append( ( *it )->resourceName() );
322 Resource::List *ManagerImpl::resourceList()
324 return &d->mResources;
327 QList<Resource *> ManagerImpl::resources()
329 return QList<Resource *>( d->mResources );
332 QList<Resource *> ManagerImpl::resources(
bool active )
334 QList<Resource *> result;
336 for (
int i = 0; i < d->mResources.size(); ++i ) {
337 if ( d->mResources.at( i )->isActive() == active ) {
338 result.append( d->mResources.at( i ) );
344 Resource *ManagerImpl::readResourceConfig(
const QString &identifier,
347 kDebug() << identifier;
349 if ( !d->mFactory ) {
350 kError() <<
"mFactory is 0. Did the app forget to call readConfig?";
354 KConfigGroup group = d->mConfig->group( QLatin1String(
"Resource_") + identifier );
356 QString type = group.readEntry(
"ResourceType" );
357 QString name = group.readEntry(
"ResourceName" );
358 Resource *resource = d->mFactory->resource( type, group );
360 kDebug() <<
"Failed to create resource with id" << identifier;
368 group = d->mConfig->group(
"General" );
370 QString standardKey = group.readEntry(
"Standard" );
371 if ( standardKey == identifier ) {
372 d->mStandard = resource;
376 QStringList activeKeys = group.readEntry(
"ResourceKeys", QStringList() );
377 resource->
setActive( activeKeys.contains( identifier ) );
379 d->mResources.append( resource );
384 void ManagerImpl::writeResourceConfig(
Resource *resource,
bool checkActive )
388 kDebug() <<
"Saving resource" << key;
391 createStandardConfig();
394 KConfigGroup group( d->mConfig, QLatin1String(
"Resource_") + key );
397 group = d->mConfig->group(
"General" );
398 QString standardKey = group.readEntry(
"Standard" );
400 if ( resource == d->mStandard && standardKey != key ) {
401 group.writeEntry(
"Standard", resource->
identifier() );
402 }
else if ( resource != d->mStandard && standardKey == key ) {
403 group.writeEntry(
"Standard",
"" );
407 QStringList activeKeys = group.readEntry(
"ResourceKeys", QStringList() );
408 QStringList passiveKeys = group.readEntry(
"PassiveResourceKeys", QStringList() );
410 if ( passiveKeys.contains( key ) ) {
411 passiveKeys.removeAll( key );
412 group.writeEntry(
"PassiveResourceKeys", passiveKeys );
414 if ( !activeKeys.contains( key ) ) {
415 activeKeys.append( key );
416 group.writeEntry(
"ResourceKeys", activeKeys );
418 }
else if ( !resource->
isActive() ) {
419 if ( activeKeys.contains( key ) ) {
420 activeKeys.removeAll( key );
421 group.writeEntry(
"ResourceKeys", activeKeys );
423 if ( !passiveKeys.contains( key ) ) {
424 passiveKeys.append( key );
425 group.writeEntry(
"PassiveResourceKeys", passiveKeys );
433 void ManagerImpl::removeResource(
Resource *resource )
438 createStandardConfig();
441 KConfigGroup group = d->mConfig->group(
"General" );
442 QStringList activeKeys = group.readEntry(
"ResourceKeys", QStringList() );
443 if ( activeKeys.contains( key ) ) {
444 activeKeys.removeAll( key );
445 group.writeEntry(
"ResourceKeys", activeKeys );
447 QStringList passiveKeys= group.readEntry(
"PassiveResourceKeys", QStringList() );
448 passiveKeys.removeAll( key );
449 group.writeEntry(
"PassiveResourceKeys", passiveKeys );
452 QString standardKey = group.readEntry(
"Standard" );
453 if ( standardKey == key ) {
454 group.writeEntry(
"Standard",
"" );
457 d->mConfig->deleteGroup( QLatin1String(
"Resource_") + resource->
identifier() );
461 Resource *ManagerImpl::getResource(
const QString &identifier )
463 Resource::List::ConstIterator it;
464 for ( it = d->mResources.constBegin(); it != d->mResources.constEnd(); ++it ) {
465 if ( ( *it )->identifier() == identifier ) {
472 QString ManagerImpl::defaultConfigFile(
const QString &family )
474 return KStandardDirs::locateLocal(
"config",
475 QString::fromLatin1(
"kresources/%1/stdrc" ).arg( family ) );
static Factory * self(const QString &resourceFamily)
Returns the global resource factory.
virtual void writeConfig(KConfigGroup &group)
Write configuration information for this resource to a configuration file.
void setIdentifier(const QString &identifier)
Sets the resource unique identifier.
QString identifier() const
Returns a unique identifier.
bool isActive() const
Return true, if the resource is active.
This class provides a resource which is managed in a general way.
This file is part of the KDE resource framework and defines the Factory class.
void setActive(bool active)
Sets, if the resource is active.
A class for loading resource plugins.