KDECore
kinstance.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "kinstance.h"
00019
00020 #include <stdlib.h>
00021 #include <unistd.h>
00022
00023 #include "kconfig.h"
00024 #include "klocale.h"
00025 #include "kcharsets.h"
00026 #include "kiconloader.h"
00027 #include "kaboutdata.h"
00028 #include "kstandarddirs.h"
00029 #include "kdebug.h"
00030 #include "kglobal.h"
00031 #include "kmimesourcefactory.h"
00032
00033 #include <qfont.h>
00034
00035 #include "config.h"
00036 #ifndef NDEBUG
00037 #include <assert.h>
00038 #include <qptrdict.h>
00039 static QPtrList<KInstance> *allInstances = 0;
00040 static QPtrDict<QCString> *allOldInstances = 0;
00041 #define DEBUG_ADD do { if (!allInstances) { allInstances = new QPtrList<KInstance>(); allOldInstances = new QPtrDict<QCString>(); } allInstances->append(this); allOldInstances->insert( this, new QCString( _name)); } while (false);
00042 #define DEBUG_REMOVE do { allInstances->removeRef(this); } while (false);
00043 #define DEBUG_CHECK_ALIVE do { if (!allInstances->contains((KInstance*)this)) { QCString *old = allOldInstances->find((KInstance*)this); qWarning("ACCESSING DELETED KINSTANCE! (%s)", old ? old->data() : "<unknown>"); assert(false); } } while (false);
00044 #else
00045 #define DEBUG_ADD
00046 #define DEBUG_REMOVE
00047 #define DEBUG_CHECK_ALIVE
00048 #endif
00049
00050 class KInstancePrivate
00051 {
00052 public:
00053 KInstancePrivate ()
00054 {
00055 mimeSourceFactory = 0L;
00056 }
00057
00058 ~KInstancePrivate ()
00059 {
00060 delete mimeSourceFactory;
00061 }
00062
00063 KMimeSourceFactory* mimeSourceFactory;
00064 QString configName;
00065 bool ownAboutdata;
00066 KSharedConfig::Ptr sharedConfig;
00067 };
00068
00069 KInstance::KInstance( const QCString& name)
00070 : _dirs (0L),
00071 _config (0L),
00072 _iconLoader (0L),
00073 _name( name ), _aboutData( new KAboutData( name, "", 0 ) )
00074 {
00075 DEBUG_ADD
00076 Q_ASSERT(!name.isEmpty());
00077 if (!KGlobal::_instance)
00078 {
00079 KGlobal::_instance = this;
00080 KGlobal::setActiveInstance(this);
00081 }
00082
00083 d = new KInstancePrivate ();
00084 d->ownAboutdata = true;
00085 }
00086
00087 KInstance::KInstance( const KAboutData * aboutData )
00088 : _dirs (0L),
00089 _config (0L),
00090 _iconLoader (0L),
00091 _name( aboutData->appName() ), _aboutData( aboutData )
00092 {
00093 DEBUG_ADD
00094 Q_ASSERT(!_name.isEmpty());
00095
00096 if (!KGlobal::_instance)
00097 {
00098 KGlobal::_instance = this;
00099 KGlobal::setActiveInstance(this);
00100 }
00101
00102 d = new KInstancePrivate ();
00103 d->ownAboutdata = false;
00104 }
00105
00106 KInstance::KInstance( KInstance* src )
00107 : _dirs ( src->_dirs ),
00108 _config ( src->_config ),
00109 _iconLoader ( src->_iconLoader ),
00110 _name( src->_name ), _aboutData( src->_aboutData )
00111 {
00112 DEBUG_ADD
00113 Q_ASSERT(!_name.isEmpty());
00114
00115 if (!KGlobal::_instance || KGlobal::_instance == src )
00116 {
00117 KGlobal::_instance = this;
00118 KGlobal::setActiveInstance(this);
00119 }
00120
00121 d = new KInstancePrivate ();
00122 d->ownAboutdata = src->d->ownAboutdata;
00123 d->sharedConfig = src->d->sharedConfig;
00124
00125 src->_dirs = 0L;
00126 src->_config = 0L;
00127 src->_iconLoader = 0L;
00128 src->_aboutData = 0L;
00129 delete src;
00130 }
00131
00132 KInstance::~KInstance()
00133 {
00134 DEBUG_CHECK_ALIVE
00135
00136 if (d->ownAboutdata)
00137 delete _aboutData;
00138 _aboutData = 0;
00139
00140 delete d;
00141 d = 0;
00142
00143 delete _iconLoader;
00144 _iconLoader = 0;
00145
00146
00147 _config = 0;
00148 delete _dirs;
00149 _dirs = 0;
00150
00151 if (KGlobal::_instance == this)
00152 KGlobal::_instance = 0;
00153 if (KGlobal::activeInstance() == this)
00154 KGlobal::setActiveInstance(0);
00155 DEBUG_REMOVE
00156 }
00157
00158
00159 KStandardDirs *KInstance::dirs() const
00160 {
00161 DEBUG_CHECK_ALIVE
00162 if( _dirs == 0 ) {
00163 _dirs = new KStandardDirs( );
00164 if (_config) {
00165 if (_dirs->addCustomized(_config))
00166 _config->reparseConfiguration();
00167 } else
00168 config();
00169 }
00170
00171 return _dirs;
00172 }
00173
00174 extern bool kde_kiosk_exception;
00175 extern bool kde_kiosk_admin;
00176
00177 KConfig *KInstance::config() const
00178 {
00179 DEBUG_CHECK_ALIVE
00180 if( _config == 0 ) {
00181 if ( !d->configName.isEmpty() )
00182 {
00183 d->sharedConfig = KSharedConfig::openConfig( d->configName );
00184
00185
00186 d->sharedConfig->setGroup( "KDE Action Restrictions" );
00187 QString kioskException = d->sharedConfig->readEntry("kiosk_exception");
00188 if (d->sharedConfig->readBoolEntry( "custom_config", true))
00189 {
00190 d->sharedConfig->setGroup(QString::null);
00191 }
00192 else
00193 {
00194 d->sharedConfig = 0;
00195 }
00196
00197 }
00198
00199 if ( d->sharedConfig == 0 )
00200 {
00201 if ( !_name.isEmpty() )
00202 d->sharedConfig = KSharedConfig::openConfig( _name + "rc");
00203 else
00204 d->sharedConfig = KSharedConfig::openConfig( QString::null );
00205 }
00206
00207
00208 if (kde_kiosk_admin && !kde_kiosk_exception && !QCString(getenv("KDE_KIOSK_NO_RESTRICTIONS")).isEmpty())
00209 {
00210 kde_kiosk_exception = true;
00211 d->sharedConfig = 0;
00212 return config();
00213 }
00214
00215 _config = d->sharedConfig;
00216 if (_dirs)
00217 if (_dirs->addCustomized(_config))
00218 _config->reparseConfiguration();
00219 }
00220
00221 return _config;
00222 }
00223
00224 KSharedConfig *KInstance::sharedConfig() const
00225 {
00226 DEBUG_CHECK_ALIVE
00227 if (_config == 0)
00228 (void) config();
00229
00230 return d->sharedConfig;
00231 }
00232
00233 void KInstance::setConfigName(const QString &configName)
00234 {
00235 DEBUG_CHECK_ALIVE
00236 d->configName = configName;
00237 }
00238
00239 KIconLoader *KInstance::iconLoader() const
00240 {
00241 DEBUG_CHECK_ALIVE
00242 if( _iconLoader == 0 ) {
00243 _iconLoader = new KIconLoader( _name, dirs() );
00244 _iconLoader->enableDelayedIconSetLoading( true );
00245 }
00246
00247 return _iconLoader;
00248 }
00249
00250 void KInstance::newIconLoader() const
00251 {
00252 DEBUG_CHECK_ALIVE
00253 KIconTheme::reconfigure();
00254 _iconLoader->reconfigure( _name, dirs() );
00255 }
00256
00257 const KAboutData * KInstance::aboutData() const
00258 {
00259 DEBUG_CHECK_ALIVE
00260 return _aboutData;
00261 }
00262
00263 QCString KInstance::instanceName() const
00264 {
00265 DEBUG_CHECK_ALIVE
00266 return _name;
00267 }
00268
00269 KMimeSourceFactory* KInstance::mimeSourceFactory () const
00270 {
00271 DEBUG_CHECK_ALIVE
00272 if (!d->mimeSourceFactory)
00273 {
00274 d->mimeSourceFactory = new KMimeSourceFactory(_iconLoader);
00275 d->mimeSourceFactory->setInstance(const_cast<KInstance *>(this));
00276 }
00277
00278 return d->mimeSourceFactory;
00279 }
00280
00281 void KInstance::virtual_hook( int, void* )
00282 { }
00283