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
57 : d( new ManagerImplPrivate )
59 d->mNotifier = notifier;
65 d->mConfigRead =
false;
67 new KResourcesManagerAdaptor(
this );
72 d->mId = KRandom::randomString( 8 );
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" );
127 keys += group.readEntry(
"PassiveResourceKeys",
QStringList() );
129 const QString standardKey = group.readEntry(
"Standard" );
132 readResourceConfig( *it,
false );
135 d->mConfigRead =
true;
138 void ManagerImpl::writeConfig( KConfig *cfg )
143 createStandardConfig();
153 for ( it = d->mResources.begin(); it != d->mResources.end(); ++it ) {
154 writeResourceConfig( *it,
false );
156 QString key = ( *it )->identifier();
157 if ( ( *it )->isActive() ) {
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,
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,
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,
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"
316 for ( it = d->mResources.constBegin(); it != end; ++it ) {
317 result.
append( ( *it )->resourceName() );
324 return &d->mResources;
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 ) );
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" );
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;
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",
"" );
410 if ( passiveKeys.
contains( key ) ) {
412 group.writeEntry(
"PassiveResourceKeys", passiveKeys );
414 if ( !activeKeys.
contains( key ) ) {
416 group.writeEntry(
"ResourceKeys", activeKeys );
418 }
else if ( !resource->
isActive() ) {
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" );
445 group.writeEntry(
"ResourceKeys", activeKeys );
449 group.writeEntry(
"PassiveResourceKeys", passiveKeys );
452 QString standardKey = group.readEntry(
"Standard" );
453 if ( standardKey == key ) {
454 group.writeEntry(
"Standard",
"" );
464 for ( it = d->mResources.constBegin(); it != d->mResources.constEnd(); ++it ) {
465 if ( ( *it )->identifier() == identifier ) {
474 return KStandardDirs::locateLocal(
"config",
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.
bool registerObject(const QString &path, QObject *object, QFlags< QDBusConnection::RegisterOption > options)
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QDBusConnection sessionBus()
const char * name() const
void append(const T &value)
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.
int removeAll(const T &value)
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.
QString fromLatin1(const char *str, int size)
const_iterator constEnd() const
const_iterator constBegin() const
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
A class for loading resource plugins.
bool registerService(const QString &serviceName)