kdecore Library API Documentation

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., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 #include "kinstance.h"
00019 
00020 #include "kconfig.h"
00021 #include "klocale.h"
00022 #include "kcharsets.h"
00023 #include "kiconloader.h"
00024 #include "kaboutdata.h"
00025 #include "kstandarddirs.h"
00026 #include "kdebug.h"
00027 #include "kglobal.h"
00028 #include "kmimesourcefactory.h"
00029 
00030 #include <qfont.h>
00031 
00032 #include "config.h"
00033 #ifndef NDEBUG
00034   #include <assert.h>
00035   #include <qptrdict.h>
00036   static QPtrList<KInstance> *allInstances = 0;
00037   static QPtrDict<QCString> *allOldInstances = 0;
00038   #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);
00039   #define DEBUG_REMOVE do { allInstances->removeRef(this); } while (false);
00040   #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);
00041 #else
00042   #define DEBUG_ADD
00043   #define DEBUG_REMOVE
00044   #define DEBUG_CHECK_ALIVE
00045 #endif
00046 
00047 class KInstancePrivate
00048 {
00049 public:
00050     KInstancePrivate ()
00051     {
00052         mimeSourceFactory = 0L;
00053     }
00054 
00055     ~KInstancePrivate ()
00056     {
00057         delete mimeSourceFactory;
00058     }
00059 
00060     KMimeSourceFactory* mimeSourceFactory;
00061     QString configName;
00062     bool ownAboutdata;
00063     KSharedConfig::Ptr sharedConfig;
00064 };
00065 
00066 KInstance::KInstance( const QCString& name)
00067   : _dirs (0L),
00068     _config (0L),
00069     _iconLoader (0L),
00070     _name( name ), _aboutData( new KAboutData( name, "", 0 ) )
00071 {
00072     DEBUG_ADD
00073     Q_ASSERT(!name.isEmpty());
00074     if (!KGlobal::_instance)
00075     {
00076       KGlobal::_instance = this;
00077       KGlobal::setActiveInstance(this);
00078     }
00079 
00080     d = new KInstancePrivate ();
00081     d->ownAboutdata = true;
00082 }
00083 
00084 KInstance::KInstance( const KAboutData * aboutData )
00085   : _dirs (0L),
00086     _config (0L),
00087     _iconLoader (0L),
00088     _name( aboutData->appName() ), _aboutData( aboutData )
00089 {
00090     DEBUG_ADD
00091     Q_ASSERT(!_name.isEmpty());
00092 
00093     if (!KGlobal::_instance)
00094     {
00095       KGlobal::_instance = this;
00096       KGlobal::setActiveInstance(this);
00097     }
00098 
00099     d = new KInstancePrivate ();
00100     d->ownAboutdata = false;
00101 }
00102 
00103 KInstance::KInstance( KInstance* src )
00104   : _dirs ( src->_dirs ),
00105     _config ( src->_config ),
00106     _iconLoader ( src->_iconLoader ),
00107     _name( src->_name ), _aboutData( src->_aboutData )
00108 {
00109     DEBUG_ADD
00110     Q_ASSERT(!_name.isEmpty());
00111 
00112     if (!KGlobal::_instance || KGlobal::_instance == src )
00113     {
00114       KGlobal::_instance = this;
00115       KGlobal::setActiveInstance(this);
00116     }
00117 
00118     d = new KInstancePrivate ();
00119     d->ownAboutdata = src->d->ownAboutdata;
00120     d->sharedConfig = src->d->sharedConfig;
00121 
00122     src->_dirs = 0L;
00123     src->_config = 0L;
00124     src->_iconLoader = 0L;
00125     src->_aboutData = 0L;
00126     delete src;
00127 }
00128 
00129 KInstance::~KInstance()
00130 {
00131     DEBUG_CHECK_ALIVE
00132     DEBUG_REMOVE
00133 
00134     if (d->ownAboutdata)
00135         delete _aboutData;
00136     _aboutData = 0;
00137 
00138     delete d;
00139     d = 0;
00140 
00141     delete _iconLoader;
00142     _iconLoader = 0;
00143 
00144     // delete _config; // Do not delete, stored in d->sharedConfig
00145     _config = 0;
00146     delete _dirs;
00147     _dirs = 0;
00148 
00149     if (KGlobal::_instance == this)
00150         KGlobal::_instance = 0;
00151     if (KGlobal::activeInstance() == this)
00152         KGlobal::setActiveInstance(0);
00153 }
00154 
00155 
00156 KStandardDirs *KInstance::dirs() const
00157 {
00158     DEBUG_CHECK_ALIVE
00159     if( _dirs == 0 ) {
00160     _dirs = new KStandardDirs( );
00161         if (_config)
00162             if (_dirs->addCustomized(_config))
00163                 _config->reparseConfiguration();
00164     }
00165 
00166     return _dirs;
00167 }
00168 
00169 KConfig *KInstance::config() const
00170 {
00171     DEBUG_CHECK_ALIVE
00172     if( _config == 0 ) {
00173         if ( !d->configName.isEmpty() )
00174         {
00175             d->sharedConfig = KSharedConfig::openConfig( d->configName );
00176 
00177             // Check whether custom config files are allowed.
00178             d->sharedConfig->setGroup( "KDE Action Restrictions" );
00179             if (d->sharedConfig->readBoolEntry( "custom_config", true))
00180             {
00181                d->sharedConfig->setGroup(QString::null);
00182             }
00183             else
00184             {
00185                d->sharedConfig = 0;
00186             }
00187 
00188         }
00189 
00190         if ( d->sharedConfig == 0 )
00191         {
00192         if ( !_name.isEmpty() )
00193             d->sharedConfig = KSharedConfig::openConfig( _name + "rc");
00194         else
00195             d->sharedConfig = KSharedConfig::openConfig( QString::null );
00196     }
00197     _config = d->sharedConfig;
00198         if (_dirs)
00199             if (_dirs->addCustomized(_config))
00200                 _config->reparseConfiguration();
00201     }
00202 
00203     return _config;
00204 }
00205 
00206 KSharedConfig *KInstance::sharedConfig() const
00207 {
00208     DEBUG_CHECK_ALIVE
00209     if (_config == 0)
00210        (void) config(); // Initialize config
00211 
00212     return d->sharedConfig;
00213 }
00214 
00215 void KInstance::setConfigName(const QString &configName)
00216 {
00217     DEBUG_CHECK_ALIVE
00218     d->configName = configName;
00219 }
00220 
00221 KIconLoader *KInstance::iconLoader() const
00222 {
00223     DEBUG_CHECK_ALIVE
00224     if( _iconLoader == 0 ) {
00225     _iconLoader = new KIconLoader( _name, dirs() );
00226     _iconLoader->enableDelayedIconSetLoading( true );
00227     }
00228 
00229     return _iconLoader;
00230 }
00231 
00232 void KInstance::newIconLoader() const
00233 {
00234     DEBUG_CHECK_ALIVE
00235     KIconTheme::reconfigure();
00236     _iconLoader->reconfigure( _name, dirs() );
00237 }
00238 
00239 const KAboutData * KInstance::aboutData() const
00240 {
00241     DEBUG_CHECK_ALIVE
00242     return _aboutData;
00243 }
00244 
00245 QCString KInstance::instanceName() const
00246 {
00247     DEBUG_CHECK_ALIVE
00248     return _name;
00249 }
00250 
00251 KMimeSourceFactory* KInstance::mimeSourceFactory () const
00252 {
00253   DEBUG_CHECK_ALIVE
00254   if (!d->mimeSourceFactory)
00255   {
00256     d->mimeSourceFactory = new KMimeSourceFactory(iconLoader());
00257   }
00258 
00259   return d->mimeSourceFactory;
00260 }
00261 
00262 void KInstance::virtual_hook( int, void* )
00263 { /*BASE::virtual_hook( id, data );*/ }
00264 
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:33:49 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003