• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tderesources
 

tderesources

managerimpl.cpp

00001 /*
00002     This file is part of libtderesources.
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 <tdeaboutdata.h>
00027 #include <tdeapplication.h>
00028 #include <kdebug.h>
00029 #include <tdeconfig.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 TQString &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 = TDEApplication::randomString( 8 );
00049 
00050   // Register with DCOP
00051   if ( !kapp->dcopClient()->isRegistered() ) {
00052     kapp->dcopClient()->registerAs( "TDEResourcesManager" );
00053     kapp->dcopClient()->setDefaultObject( objId() );
00054   }
00055 
00056   kdDebug(5650) << "Connecting DCOP signals..." << endl;
00057   if ( !connectDCOPSignal( 0, "ManagerIface_" + family.utf8(),
00058                            "signalKResourceAdded( TQString, TQString )",
00059                            "dcopKResourceAdded( TQString, TQString )", false ) )
00060     kdWarning(5650) << "Could not connect ResourceAdded signal!" << endl;
00061 
00062   if ( !connectDCOPSignal( 0, "ManagerIface_" + family.utf8(),
00063                            "signalKResourceModified( TQString, TQString )",
00064                            "dcopKResourceModified( TQString, TQString )", false ) )
00065     kdWarning(5650) << "Could not connect ResourceModified signal!" << endl;
00066 
00067   if ( !connectDCOPSignal( 0, "ManagerIface_" + family.utf8(),
00068                            "signalKResourceDeleted( TQString, TQString )",
00069                            "dcopKResourceDeleted( TQString, TQString )", 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     TQString file = defaultConfigFile( mFamily );
00091     mStdConfig = new TDEConfig( file );
00092   }
00093   
00094   mConfig = mStdConfig;
00095 }
00096 
00097 void ManagerImpl::readConfig( TDEConfig *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   TQStringList keys = mConfig->readListEntry( "ResourceKeys" );
00115   keys += mConfig->readListEntry( "PassiveResourceKeys" );
00116 
00117   TQString standardKey = mConfig->readEntry( "Standard" );
00118 
00119   for ( TQStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
00120     readResourceConfig( *it, false );
00121   }
00122 
00123   mConfigRead = true;
00124 }
00125 
00126 void ManagerImpl::writeConfig( TDEConfig *cfg )
00127 {
00128   kdDebug(5650) << "ManagerImpl::writeConfig()" << endl;
00129 
00130   if ( !cfg ) {
00131     createStandardConfig();
00132   } else {
00133     mConfig = cfg;
00134   }
00135 
00136   TQStringList activeKeys;
00137   TQStringList 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     TQString 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( TQString managerId, TQString 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( TQString managerId, TQString 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( TQString managerId, TQString 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 TQStringList ManagerImpl::resourceNames()
00285 {
00286   TQStringList 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 TQPtrList<Resource> ManagerImpl::resources()
00301 {
00302   TQPtrList<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 TQPtrList<Resource> ManagerImpl::resources( bool active )
00312 {
00313   TQPtrList<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 TQString &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   TQString type = mConfig->readEntry( "ResourceType" );
00337   TQString 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   TQString standardKey = mConfig->readEntry( "Standard" );
00350   if ( standardKey == identifier ) {
00351     mStandard = resource;
00352   }
00353 
00354   if ( checkActive ) {
00355     TQStringList 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   TQString 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   TQString 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     TQStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
00384     TQStringList 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   TQString key = resource->identifier();
00412 
00413   if ( !mConfig ) createStandardConfig();
00414   
00415   mConfig->setGroup( "General" );
00416   TQStringList activeKeys = mConfig->readListEntry( "ResourceKeys" );
00417   if ( activeKeys.contains( key ) ) {
00418     activeKeys.remove( key );
00419     mConfig->writeEntry( "ResourceKeys", activeKeys );
00420   } else {
00421     TQStringList passiveKeys = mConfig->readListEntry( "PassiveResourceKeys" );
00422     passiveKeys.remove( key );
00423     mConfig->writeEntry( "PassiveResourceKeys", passiveKeys );
00424   }
00425 
00426   TQString 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 TQString &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 TQString ManagerImpl::defaultConfigFile( const TQString &family )
00446 {
00447   return TQString( "tderesources/%1/stdrc" ).arg( family );
00448 }

tderesources

Skip menu "tderesources"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

tderesources

Skip menu "tderesources"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tderesources by doxygen 1.7.1
This website is maintained by Timothy Pearson.