00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#include <dcopclient.h>
00025
00026
#include <kaboutdata.h>
00027
#include <kapplication.h>
00028
#include <kdebug.h>
00029
#include <kconfig.h>
00030
#include <kstandarddirs.h>
00031
00032
#include "resource.h"
00033
#include "factory.h"
00034
#include "manager.h"
00035
#include "managerimpl.h"
00036
#include "manageriface_stub.h"
00037
00038
using namespace KRES;
00039
00040 ManagerImpl::ManagerImpl( ManagerNotifier *notifier,
const QString &family )
00041 : DCOPObject( "ManagerIface_" + family.utf8() ),
00042 mNotifier( notifier ),
00043 mFamily( family ), mConfig( 0 ), mStdConfig( 0 ), mStandard( 0 ),
00044 mFactory( 0 ), mConfigRead( false )
00045 {
00046 kdDebug(5650) <<
"ManagerImpl::ManagerImpl()" << endl;
00047
00048 mId = KApplication::randomString( 8 );
00049
00050
00051
if ( !kapp->dcopClient()->isRegistered() ) {
00052 kapp->dcopClient()->registerAs(
"KResourcesManager" );
00053 kapp->dcopClient()->setDefaultObject( objId() );
00054 }
00055
00056 kdDebug(5650) <<
"Connecting DCOP signals..." << endl;
00057
if ( !connectDCOPSignal( 0,
"ManagerIface_" + family.utf8(),
00058
"signalKResourceAdded( QString, QString )",
00059
"dcopKResourceAdded( QString, QString )",
false ) )
00060 kdWarning(5650) <<
"Could not connect ResourceAdded signal!" << endl;
00061
00062
if ( !connectDCOPSignal( 0,
"ManagerIface_" + family.utf8(),
00063
"signalKResourceModified( QString, QString )",
00064
"dcopKResourceModified( QString, QString )",
false ) )
00065 kdWarning(5650) <<
"Could not connect ResourceModified signal!" << endl;
00066
00067
if ( !connectDCOPSignal( 0,
"ManagerIface_" + family.utf8(),
00068
"signalKResourceDeleted( QString, QString )",
00069
"dcopKResourceDeleted( QString, QString )",
false ) )
00070 kdWarning(5650) <<
"Could not connect ResourceDeleted signal!" << endl;
00071
00072 kapp->dcopClient()->setNotifications(
true );
00073 }
00074
00075 ManagerImpl::~ManagerImpl()
00076 {
00077 kdDebug(5650) <<
"ManagerImpl::~ManagerImpl()" << endl;
00078
00079 Resource::List::ConstIterator it;
00080
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
00081
delete *it;
00082 }
00083
00084
delete mStdConfig;
00085 }
00086
00087
void ManagerImpl::createStandardConfig()
00088 {
00089
if ( !mStdConfig ) {
00090
QString file = defaultConfigFile( mFamily );
00091 mStdConfig =
new KConfig( file );
00092 }
00093
00094 mConfig = mStdConfig;
00095 }
00096
00097
void ManagerImpl::readConfig( KConfig *cfg )
00098 {
00099 kdDebug(5650) <<
"ManagerImpl::readConfig()" << endl;
00100
00101
delete mFactory;
00102 mFactory = Factory::self( mFamily );
00103
00104
if ( !cfg ) {
00105 createStandardConfig();
00106 }
else {
00107 mConfig = cfg;
00108 }
00109
00110 mStandard = 0;
00111
00112 mConfig->setGroup(
"General" );
00113
00114
QStringList keys = mConfig->readListEntry(
"ResourceKeys" );
00115 keys += mConfig->readListEntry(
"PassiveResourceKeys" );
00116
00117
QString standardKey = mConfig->readEntry(
"Standard" );
00118
00119
for ( QStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
00120 readResourceConfig( *it,
false );
00121 }
00122
00123 mConfigRead =
true;
00124 }
00125
00126
void ManagerImpl::writeConfig( KConfig *cfg )
00127 {
00128 kdDebug(5650) <<
"ManagerImpl::writeConfig()" << endl;
00129
00130
if ( !cfg ) {
00131 createStandardConfig();
00132 }
else {
00133 mConfig = cfg;
00134 }
00135
00136
QStringList activeKeys;
00137
QStringList passiveKeys;
00138
00139
00140 Resource::List::Iterator it;
00141
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
00142 writeResourceConfig( *it,
false );
00143
00144
QString key = (*it)->identifier();
00145
if( (*it)->isActive() )
00146 activeKeys.append( key );
00147
else
00148 passiveKeys.append( key );
00149 }
00150
00151
00152
00153 kdDebug(5650) <<
"Saving general info" << endl;
00154 mConfig->setGroup(
"General" );
00155 mConfig->writeEntry(
"ResourceKeys", activeKeys );
00156 mConfig->writeEntry(
"PassiveResourceKeys", passiveKeys );
00157
if ( mStandard )
00158 mConfig->writeEntry(
"Standard", mStandard->identifier() );
00159
else
00160 mConfig->writeEntry(
"Standard",
"" );
00161
00162 mConfig->sync();
00163 kdDebug(5650) <<
"ManagerImpl::save() finished" << endl;
00164 }
00165
00166
void ManagerImpl::add( Resource *resource )
00167 {
00168 resource->setActive(
true );
00169
00170
if ( mResources.isEmpty() ) {
00171 mStandard = resource;
00172 }
00173
00174 mResources.append( resource );
00175
00176
if ( mConfigRead )
00177 writeResourceConfig( resource,
true );
00178
00179 signalKResourceAdded( mId, resource->identifier() );
00180 }
00181
00182
void ManagerImpl::remove( Resource *resource )
00183 {
00184
if ( mStandard == resource ) mStandard = 0;
00185 removeResource( resource );
00186
00187 mResources.remove( resource );
00188
00189 signalKResourceDeleted( mId, resource->identifier() );
00190
00191
delete resource;
00192
00193 kdDebug(5650) <<
"Finished ManagerImpl::remove()" << endl;
00194 }
00195
00196
void ManagerImpl::change( Resource *resource )
00197 {
00198 writeResourceConfig( resource,
true );
00199
00200 signalKResourceModified( mId, resource->identifier() );
00201 }
00202
00203
void ManagerImpl::setActive( Resource *resource,
bool active )
00204 {
00205
if ( resource && resource->isActive() != active ) {
00206 resource->setActive( active );
00207 }
00208 }
00209
00210 Resource *ManagerImpl::standardResource()
00211 {
00212
return mStandard;
00213 }
00214
00215
void ManagerImpl::setStandardResource( Resource *resource )
00216 {
00217 mStandard = resource;
00218 }
00219
00220
00221
00222
void ManagerImpl::dcopKResourceAdded(
QString managerId,
QString resourceId )
00223 {
00224
if ( managerId == mId ) {
00225 kdDebug(5650) <<
"Ignore DCOP notification to myself" << endl;
00226
return;
00227 }
00228 kdDebug(5650) <<
"Receive DCOP call: added resource " << resourceId << endl;
00229
00230
if ( getResource( resourceId ) ) {
00231 kdDebug(5650) <<
"This resource is already known to me." << endl;
00232 }
00233
00234
if ( !mConfig ) createStandardConfig();
00235
00236 mConfig->reparseConfiguration();
00237 Resource *resource = readResourceConfig( resourceId,
true );
00238
00239
if ( resource ) {
00240 mNotifier->notifyResourceAdded( resource );
00241 }
else
00242 kdError() <<
"Received DCOP: resource added for unknown resource "
00243 << resourceId << endl;
00244 }
00245
00246
void ManagerImpl::dcopKResourceModified(
QString managerId,
QString resourceId )
00247 {
00248
if ( managerId == mId ) {
00249 kdDebug(5650) <<
"Ignore DCOP notification to myself" << endl;
00250
return;
00251 }
00252 kdDebug(5650) <<
"Receive DCOP call: modified resource " << resourceId << endl;
00253
00254 Resource *resource = getResource( resourceId );
00255
if ( resource ) {
00256 mNotifier->notifyResourceModified( resource );
00257 }
else
00258 kdError() <<
"Received DCOP: resource modified for unknown resource "
00259 << resourceId << endl;
00260 }
00261
00262
void ManagerImpl::dcopKResourceDeleted(
QString managerId,
QString resourceId )
00263 {
00264
if ( managerId == mId ) {
00265 kdDebug(5650) <<
"Ignore DCOP notification to myself" << endl;
00266
return;
00267 }
00268 kdDebug(5650) <<
"Receive DCOP call: deleted resource " << resourceId << endl;
00269
00270 Resource *resource = getResource( resourceId );
00271
if ( resource ) {
00272 mNotifier->notifyResourceDeleted( resource );
00273
00274 kdDebug(5650) <<
"Removing item from mResources" << endl;
00275
00276
if ( mStandard == resource )
00277 mStandard = 0;
00278 mResources.remove( resource );
00279 }
else
00280 kdError() <<
"Received DCOP: resource deleted for unknown resource "
00281 << resourceId << endl;
00282 }
00283
00284
QStringList ManagerImpl::resourceNames()
00285 {
00286
QStringList result;
00287
00288 Resource::List::ConstIterator it;
00289
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
00290 result.append( (*it)->resourceName() );
00291 }
00292
return result;
00293 }
00294
00295 Resource::List *ManagerImpl::resourceList()
00296 {
00297
return &mResources;
00298 }
00299
00300
QPtrList<Resource> ManagerImpl::resources()
00301 {
00302
QPtrList<Resource> result;
00303
00304 Resource::List::ConstIterator it;
00305
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
00306 result.append( *it );
00307 }
00308
return result;
00309 }
00310
00311
QPtrList<Resource> ManagerImpl::resources(
bool active )
00312 {
00313
QPtrList<Resource> result;
00314
00315 Resource::List::ConstIterator it;
00316
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
00317
if ( (*it)->isActive() == active ) {
00318 result.append( *it );
00319 }
00320 }
00321
return result;
00322 }
00323
00324 Resource *ManagerImpl::readResourceConfig(
const QString &identifier,
00325
bool checkActive )
00326 {
00327 kdDebug(5650) <<
"ManagerImpl::readResourceConfig() " << identifier << endl;
00328
00329 mConfig->setGroup(
"Resource_" + identifier );
00330
00331
QString type = mConfig->readEntry(
"ResourceType" );
00332
QString name = mConfig->readEntry(
"ResourceName" );
00333 Resource *resource = mFactory->
resource( type, mConfig );
00334
if ( !resource ) {
00335 kdDebug(5650) <<
"Failed to create resource with id " << identifier << endl;
00336
return 0;
00337 }
00338
00339
if ( resource->identifier().isEmpty() )
00340 resource->setIdentifier( identifier );
00341
00342 mConfig->setGroup(
"General" );
00343
00344
QString standardKey = mConfig->readEntry(
"Standard" );
00345
if ( standardKey == identifier ) {
00346 mStandard = resource;
00347 }
00348
00349
if ( checkActive ) {
00350
QStringList activeKeys = mConfig->readListEntry(
"ResourceKeys" );
00351 resource->setActive( activeKeys.contains( identifier ) );
00352 }
00353 mResources.append( resource );
00354
00355
return resource;
00356 }
00357
00358
void ManagerImpl::writeResourceConfig( Resource *resource,
bool checkActive )
00359 {
00360
QString key = resource->identifier();
00361
00362 kdDebug(5650) <<
"Saving resource " << key << endl;
00363
00364
if ( !mConfig ) createStandardConfig();
00365
00366 mConfig->setGroup(
"Resource_" + key );
00367 resource->writeConfig( mConfig );
00368
00369 mConfig->setGroup(
"General" );
00370
QString standardKey = mConfig->readEntry(
"Standard" );
00371
00372
if ( resource == mStandard && standardKey != key )
00373 mConfig->writeEntry(
"Standard", resource->identifier() );
00374
else if ( resource != mStandard && standardKey == key )
00375 mConfig->writeEntry(
"Standard",
"" );
00376
00377
if ( checkActive ) {
00378
QStringList activeKeys = mConfig->readListEntry(
"ResourceKeys" );
00379
if ( resource->isActive() && !activeKeys.contains( key ) ) {
00380 activeKeys.append( resource->identifier() );
00381 mConfig->writeEntry(
"ResourceKeys", activeKeys );
00382 }
else if ( !resource->isActive() && activeKeys.contains( key ) ) {
00383 activeKeys.remove( key );
00384 mConfig->writeEntry(
"ResourceKeys", activeKeys );
00385 }
00386 }
00387
00388 mConfig->sync();
00389 }
00390
00391
void ManagerImpl::removeResource( Resource *resource )
00392 {
00393
QString key = resource->identifier();
00394
00395
if ( !mConfig ) createStandardConfig();
00396
00397 mConfig->setGroup(
"General" );
00398
QStringList activeKeys = mConfig->readListEntry(
"ResourceKeys" );
00399
if ( activeKeys.contains( key ) ) {
00400 activeKeys.remove( key );
00401 mConfig->writeEntry(
"ResourceKeys", activeKeys );
00402 }
else {
00403
QStringList passiveKeys = mConfig->readListEntry(
"PassiveResourceKeys" );
00404 passiveKeys.remove( key );
00405 mConfig->writeEntry(
"PassiveResourceKeys", passiveKeys );
00406 }
00407
00408
QString standardKey = mConfig->readEntry(
"Standard" );
00409
if ( standardKey == key ) {
00410 mConfig->writeEntry(
"Standard",
"" );
00411 }
00412
00413 mConfig->deleteGroup(
"Resource_" + resource->identifier() );
00414 mConfig->sync();
00415 }
00416
00417 Resource *ManagerImpl::getResource(
const QString &identifier )
00418 {
00419 Resource::List::ConstIterator it;
00420
for ( it = mResources.begin(); it != mResources.end(); ++it ) {
00421
if ( (*it)->identifier() == identifier )
00422
return *it;
00423 }
00424
return 0;
00425 }
00426
00427
QString ManagerImpl::defaultConfigFile(
const QString &family )
00428 {
00429
return locateLocal(
"config",
00430
QString(
"kresources/%1/stdrc" ).arg( family ) );
00431 }