kinstance.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Torben Weis <weis@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 #include "kinstance.h" 00019 00020 #include <stdlib.h> 00021 #include <unistd.h> 00022 00023 #include "tdeconfig.h" 00024 #include "tdelocale.h" 00025 #include "kcharsets.h" 00026 #include "kiconloader.h" 00027 #ifdef __TDE_HAVE_TDEHWLIB 00028 #include "tdehardwaredevices.h" 00029 #include "tdenetworkconnections.h" 00030 #endif 00031 #include "tdeaboutdata.h" 00032 #include "kstandarddirs.h" 00033 #include "kdebug.h" 00034 #include "tdeglobal.h" 00035 #include "kmimesourcefactory.h" 00036 00037 #include <tqfont.h> 00038 00039 #include "config.h" 00040 #ifndef NDEBUG 00041 #include <assert.h> 00042 #include <tqptrdict.h> 00043 static TQPtrList<TDEInstance> *allInstances = 0; 00044 static TQPtrDict<TQCString> *allOldInstances = 0; 00045 #define DEBUG_ADD do { if (!allInstances) { allInstances = new TQPtrList<TDEInstance>(); allOldInstances = new TQPtrDict<TQCString>(); } allInstances->append(this); allOldInstances->insert( this, new TQCString( _name)); } while (false); 00046 #define DEBUG_REMOVE do { allInstances->removeRef(this); } while (false); 00047 #define DEBUG_CHECK_ALIVE do { if (!allInstances->contains((TDEInstance*)this)) { TQCString *old = allOldInstances->find((TDEInstance*)this); tqWarning("ACCESSING DELETED KINSTANCE! (%s)", old ? old->data() : "<unknown>"); assert(false); } } while (false); 00048 #else 00049 #define DEBUG_ADD 00050 #define DEBUG_REMOVE 00051 #define DEBUG_CHECK_ALIVE 00052 #endif 00053 00054 class TDEInstancePrivate 00055 { 00056 public: 00057 TDEInstancePrivate () 00058 { 00059 mimeSourceFactory = 0L; 00060 } 00061 00062 ~TDEInstancePrivate () 00063 { 00064 delete mimeSourceFactory; 00065 } 00066 00067 KMimeSourceFactory* mimeSourceFactory; 00068 TQString configName; 00069 bool ownAboutdata; 00070 TDESharedConfig::Ptr sharedConfig; 00071 }; 00072 00073 TDEInstance::TDEInstance( const TQCString& name) 00074 : _dirs (0L), 00075 _config (0L), 00076 _iconLoader (0L), 00077 #ifdef __TDE_HAVE_TDEHWLIB 00078 _hardwaredevices (0L), 00079 _networkmanager (0L), 00080 #endif 00081 _name( name ), _aboutData( new TDEAboutData( name, "", 0 ) ), m_configReadOnly(false) 00082 { 00083 DEBUG_ADD 00084 Q_ASSERT(!name.isEmpty()); 00085 if (!TDEGlobal::_instance) 00086 { 00087 TDEGlobal::_instance = this; 00088 TDEGlobal::setActiveInstance(this); 00089 } 00090 00091 d = new TDEInstancePrivate (); 00092 d->ownAboutdata = true; 00093 } 00094 00095 TDEInstance::TDEInstance( const TDEAboutData * aboutData ) 00096 : _dirs (0L), 00097 _config (0L), 00098 _iconLoader (0L), 00099 #ifdef __TDE_HAVE_TDEHWLIB 00100 _hardwaredevices (0L), 00101 _networkmanager (0L), 00102 #endif 00103 _name( aboutData->appName() ), _aboutData( aboutData ), m_configReadOnly(false) 00104 { 00105 DEBUG_ADD 00106 Q_ASSERT(!_name.isEmpty()); 00107 00108 if (!TDEGlobal::_instance) 00109 { 00110 TDEGlobal::_instance = this; 00111 TDEGlobal::setActiveInstance(this); 00112 } 00113 00114 d = new TDEInstancePrivate (); 00115 d->ownAboutdata = false; 00116 } 00117 00118 TDEInstance::TDEInstance( TDEInstance* src ) 00119 : _dirs ( src->_dirs ), 00120 _config ( src->_config ), 00121 _iconLoader ( src->_iconLoader ), 00122 #ifdef __TDE_HAVE_TDEHWLIB 00123 _hardwaredevices ( src->_hardwaredevices ), 00124 _networkmanager ( src->_networkmanager ), 00125 #endif 00126 _name( src->_name ), _aboutData( src->_aboutData ), m_configReadOnly(false) 00127 { 00128 DEBUG_ADD 00129 Q_ASSERT(!_name.isEmpty()); 00130 00131 if (!TDEGlobal::_instance || TDEGlobal::_instance == src ) 00132 { 00133 TDEGlobal::_instance = this; 00134 TDEGlobal::setActiveInstance(this); 00135 } 00136 00137 d = new TDEInstancePrivate (); 00138 d->ownAboutdata = src->d->ownAboutdata; 00139 d->sharedConfig = src->d->sharedConfig; 00140 00141 src->_dirs = 0L; 00142 src->_config = 0L; 00143 src->_iconLoader = 0L; 00144 #ifdef __TDE_HAVE_TDEHWLIB 00145 src->_hardwaredevices = 0L; 00146 src->_networkmanager = 0L; 00147 #endif 00148 src->_aboutData = 0L; 00149 delete src; 00150 } 00151 00152 TDEInstance::~TDEInstance() 00153 { 00154 DEBUG_CHECK_ALIVE 00155 00156 if (d->ownAboutdata) 00157 delete _aboutData; 00158 _aboutData = 0; 00159 00160 delete d; 00161 d = 0; 00162 00163 delete _iconLoader; 00164 _iconLoader = 0; 00165 00166 #ifdef __TDE_HAVE_TDEHWLIB 00167 delete _hardwaredevices; 00168 _hardwaredevices = 0; 00169 00170 delete _networkmanager; 00171 _networkmanager = 0; 00172 #endif 00173 00174 // delete _config; // Do not delete, stored in d->sharedConfig 00175 _config = 0; 00176 delete _dirs; 00177 _dirs = 0; 00178 00179 if (TDEGlobal::_instance == this) 00180 TDEGlobal::_instance = 0; 00181 if (TDEGlobal::activeInstance() == this) 00182 TDEGlobal::setActiveInstance(0); 00183 DEBUG_REMOVE 00184 } 00185 00186 00187 TDEStandardDirs *TDEInstance::dirs() const 00188 { 00189 DEBUG_CHECK_ALIVE 00190 if( _dirs == 0 ) { 00191 _dirs = new TDEStandardDirs( ); 00192 if (_config) { 00193 if (_dirs->addCustomized(_config)) 00194 _config->reparseConfiguration(); 00195 } else 00196 config(); // trigger adding of possible customized dirs 00197 } 00198 00199 return _dirs; 00200 } 00201 00202 extern bool kde_kiosk_exception; 00203 extern bool kde_kiosk_admin; 00204 00205 void TDEInstance::setConfigReadOnly(bool ro) 00206 { 00207 m_configReadOnly = ro; 00208 } 00209 00210 TDEConfig *TDEInstance::config() const 00211 { 00212 DEBUG_CHECK_ALIVE 00213 if( _config == 0 ) { 00214 if ( !d->configName.isEmpty() ) 00215 { 00216 d->sharedConfig = TDESharedConfig::openConfig( d->configName ); 00217 00218 // Check whether custom config files are allowed. 00219 d->sharedConfig->setGroup( "KDE Action Restrictions" ); 00220 TQString kioskException = d->sharedConfig->readEntry("kiosk_exception"); 00221 if (d->sharedConfig->readBoolEntry( "custom_config", true)) 00222 { 00223 d->sharedConfig->setGroup(TQString::null); 00224 } 00225 else 00226 { 00227 d->sharedConfig = 0; 00228 } 00229 00230 } 00231 00232 if ( d->sharedConfig == 0 ) 00233 { 00234 if ( !_name.isEmpty() ) { 00235 d->sharedConfig = TDESharedConfig::openConfig( _name + "rc", m_configReadOnly ); 00236 } 00237 else { 00238 d->sharedConfig = TDESharedConfig::openConfig( TQString::null ); 00239 } 00240 } 00241 00242 // Check if we are excempt from kiosk restrictions 00243 if (kde_kiosk_admin && !kde_kiosk_exception && !TQCString(getenv("TDE_KIOSK_NO_RESTRICTIONS")).isEmpty()) 00244 { 00245 kde_kiosk_exception = true; 00246 d->sharedConfig = 0; 00247 return config(); // Reread... 00248 } 00249 00250 _config = d->sharedConfig; 00251 if (_dirs) 00252 if (_dirs->addCustomized(_config)) 00253 _config->reparseConfiguration(); 00254 } 00255 00256 return _config; 00257 } 00258 00259 TDESharedConfig *TDEInstance::sharedConfig() const 00260 { 00261 DEBUG_CHECK_ALIVE 00262 if (_config == 0) 00263 (void) config(); // Initialize config 00264 00265 return d->sharedConfig; 00266 } 00267 00268 void TDEInstance::setConfigName(const TQString &configName) 00269 { 00270 DEBUG_CHECK_ALIVE 00271 d->configName = configName; 00272 } 00273 00274 TDEIconLoader *TDEInstance::iconLoader() const 00275 { 00276 DEBUG_CHECK_ALIVE 00277 if( _iconLoader == 0 ) { 00278 _iconLoader = new TDEIconLoader( _name, dirs() ); 00279 _iconLoader->enableDelayedIconSetLoading( true ); 00280 } 00281 00282 return _iconLoader; 00283 } 00284 00285 #ifdef __TDE_HAVE_TDEHWLIB 00286 TDEHardwareDevices *TDEInstance::hardwareDevices() const 00287 { 00288 DEBUG_CHECK_ALIVE 00289 if( _hardwaredevices == 0 ) { 00290 _hardwaredevices = new TDEHardwareDevices( ); 00291 } 00292 00293 return _hardwaredevices; 00294 } 00295 00296 TDEGlobalNetworkManager *TDEInstance::networkManager() const 00297 { 00298 DEBUG_CHECK_ALIVE 00299 if( _networkmanager == 0 ) { 00300 _networkmanager = new TDEGlobalNetworkManager( ); 00301 } 00302 00303 return _networkmanager; 00304 } 00305 #endif 00306 00307 void TDEInstance::newIconLoader() const 00308 { 00309 DEBUG_CHECK_ALIVE 00310 TDEIconTheme::reconfigure(); 00311 _iconLoader->reconfigure( _name, dirs() ); 00312 } 00313 00314 const TDEAboutData * TDEInstance::aboutData() const 00315 { 00316 DEBUG_CHECK_ALIVE 00317 return _aboutData; 00318 } 00319 00320 TQCString TDEInstance::instanceName() const 00321 { 00322 DEBUG_CHECK_ALIVE 00323 return _name; 00324 } 00325 00326 KMimeSourceFactory* TDEInstance::mimeSourceFactory () const 00327 { 00328 DEBUG_CHECK_ALIVE 00329 if (!d->mimeSourceFactory) 00330 { 00331 d->mimeSourceFactory = new KMimeSourceFactory(_iconLoader); 00332 d->mimeSourceFactory->setInstance(const_cast<TDEInstance *>(this)); 00333 } 00334 00335 return d->mimeSourceFactory; 00336 } 00337 00338 void TDEInstance::virtual_hook( int, void* ) 00339 { /*BASE::virtual_hook( id, data );*/ } 00340