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., 51 Franklin Street, Fifth Floor,
00021     Boston, MA 02110-1301, 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   if ( !mFactory ) {
00330     kdError(5650) << "ManagerImpl::readResourceConfig: mFactory is 0. Did the app forget to call readConfig?" << endl;
00331     return 0;
00332   }
00333 
00334   mConfig->setGroup( "Resource_" + identifier );
00335 
00336   QString type = mConfig->readEntry( "ResourceType" );
00337   QString name = mConfig->readEntry( "ResourceName" );
00338   Resource *resource = mFactory->resource( type, mConfig );
00339   if ( !resource ) {
00340     kdDebug(5650) << "Failed to create resource with id " << identifier << endl;
00341     return 0;
00342   }
00343 
00344   if ( resource->identifier().isEmpty() )
00345     resource->setIdentifier( identifier );
00346 
00347   mConfig->setGroup( "General" );
00348 
00349   QString standardKey = mConfig->readEntry( "Standard" );
00350   if ( standardKey == identifier ) {
00351     mStandard = resource;
00352   }
00353 
00354   if ( checkActive ) {
00355     QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
00356     resource->setActive( activeKeys.contains( identifier ) );
00357   }
00358   mResources.append( resource );
00359 
00360   return resource;
00361 }
00362 
00363 void ManagerImpl::writeResourceConfig( Resource *resource, bool checkActive )
00364 {
00365   QString key = resource->identifier();
00366 
00367   kdDebug(5650) << "Saving resource " << key << endl;
00368 
00369   if ( !mConfig ) createStandardConfig();
00370 
00371   mConfig->setGroup( "Resource_" + key );
00372   resource->writeConfig( mConfig );
00373 
00374   mConfig->setGroup( "General" );
00375   QString standardKey = mConfig->readEntry( "Standard" );
00376 
00377   if ( resource == mStandard  && standardKey != key )
00378     mConfig->writeEntry( "Standard", resource->identifier() );
00379   else if ( resource != mStandard && standardKey == key )
00380     mConfig->writeEntry( "Standard", "" );
00381   
00382   if ( checkActive ) {
00383     QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
00384     QStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" );
00385     if ( resource->isActive() ) {
00386       if ( passiveKeys.contains( key ) ) { // remove it from passive list
00387         passiveKeys.remove( key );
00388         mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
00389       }
00390       if ( !activeKeys.contains( key ) ) { // add it to active list
00391         activeKeys.append( key );
00392         mConfig->writeEntry( "ResourceKeys", activeKeys );
00393       }
00394     } else if ( !resource->isActive() ) {
00395       if ( activeKeys.contains( key ) ) { // remove it from active list
00396         activeKeys.remove( key );
00397         mConfig->writeEntry( "ResourceKeys", activeKeys );
00398       }
00399       if ( !passiveKeys.contains( key ) ) { // add it to passive list
00400         passiveKeys.append( key );
00401         mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
00402       }
00403     }
00404   }
00405 
00406   mConfig->sync();
00407 }
00408 
00409 void ManagerImpl::removeResource( Resource *resource )
00410 {
00411   QString key = resource->identifier();
00412 
00413   if ( !mConfig ) createStandardConfig();
00414   
00415   mConfig->setGroup( "General" );
00416   QStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
00417   if ( activeKeys.contains( key ) ) {
00418     activeKeys.remove( key );
00419     mConfig->writeEntry( "ResourceKeys", activeKeys );
00420   } else {
00421     QStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" );
00422     passiveKeys.remove( key );
00423     mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
00424   }
00425 
00426   QString standardKey = mConfig->readEntry( "Standard" );
00427   if ( standardKey == key ) {
00428     mConfig->writeEntry( "Standard", "" );
00429   }
00430 
00431   mConfig->deleteGroup( "Resource_" + resource->identifier() );
00432   mConfig->sync();
00433 }
00434 
00435 Resource *ManagerImpl::getResource( const QString &identifier )
00436 {
00437   Resource::List::ConstIterator it;
00438   for ( it = mResources.begin(); it != mResources.end(); ++it ) {
00439     if ( (*it)->identifier() == identifier )
00440       return *it;
00441   }
00442   return 0;
00443 }
00444 
00445 QString ManagerImpl::defaultConfigFile( const QString &family )
00446 {
00447   return QString( "kresources/%1/stdrc" ).arg( family );
00448 }
KDE Home | KDE Accessibility Home | Description of Access Keys