kresources Library API Documentation

managerimpl.cpp

00001 /* 00002 This file is part of libkresources. 00003 00004 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 00005 Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> 00006 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Library General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Library General Public License for more details. 00017 00018 You should have received a copy of the GNU Library General Public License 00019 along with this library; see the file COPYING.LIB. If not, write to 00020 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00021 Boston, MA 02111-1307, USA. 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 // Register with DCOP 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 // First write all keys, collect active and passive keys on the way 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 // And then the general group 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 // DCOP asynchronous functions 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 // Now delete item 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 QStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" ); 00380 if ( resource->isActive() ) { 00381 if ( passiveKeys.contains( key ) ) { // remove it from passive list 00382 passiveKeys.remove( key ); 00383 mConfig->writeEntry( "PassiveResourceKeys", passiveKeys ); 00384 } 00385 if ( !activeKeys.contains( key ) ) { // add it to active list 00386 activeKeys.append( key ); 00387 mConfig->writeEntry( "ResourceKeys", activeKeys ); 00388 } 00389 } else if ( !resource->isActive() ) { 00390 if ( activeKeys.contains( key ) ) { // remove it from active list 00391 activeKeys.remove( key ); 00392 mConfig->writeEntry( "ResourceKeys", activeKeys ); 00393 } 00394 if ( !passiveKeys.contains( key ) ) { // add it to passive list 00395 passiveKeys.append( key ); 00396 mConfig->writeEntry( "PassiveResourceKeys", passiveKeys ); 00397 } 00398 } 00399 } 00400 00401 mConfig->sync(); 00402 } 00403 00404 void ManagerImpl::removeResource( Resource *resource ) 00405 { 00406 QString key = resource->identifier(); 00407 00408 if ( !mConfig ) createStandardConfig(); 00409 00410 mConfig->setGroup( "General" ); 00411 QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" ); 00412 if ( activeKeys.contains( key ) ) { 00413 activeKeys.remove( key ); 00414 mConfig->writeEntry( "ResourceKeys", activeKeys ); 00415 } else { 00416 QStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" ); 00417 passiveKeys.remove( key ); 00418 mConfig->writeEntry( "PassiveResourceKeys", passiveKeys ); 00419 } 00420 00421 QString standardKey = mConfig->readEntry( "Standard" ); 00422 if ( standardKey == key ) { 00423 mConfig->writeEntry( "Standard", "" ); 00424 } 00425 00426 mConfig->deleteGroup( "Resource_" + resource->identifier() ); 00427 mConfig->sync(); 00428 } 00429 00430 Resource *ManagerImpl::getResource( const QString &identifier ) 00431 { 00432 Resource::List::ConstIterator it; 00433 for ( it = mResources.begin(); it != mResources.end(); ++it ) { 00434 if ( (*it)->identifier() == identifier ) 00435 return *it; 00436 } 00437 return 0; 00438 } 00439 00440 QString ManagerImpl::defaultConfigFile( const QString &family ) 00441 { 00442 return locateLocal( "config", 00443 QString( "kresources/%1/stdrc" ).arg( family ) ); 00444 }
KDE Logo
This file is part of the documentation for kresources Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 14 00:32:35 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003