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 <unistd.h>
00021 
00022 #include "kconfig.h"
00023 #include "klocale.h"
00024 #include "kcharsets.h"
00025 #include "kiconloader.h"
00026 #include "kaboutdata.h"
00027 #include "kstandarddirs.h"
00028 #include "kuser.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     DEBUG_REMOVE
00136 
00137     if (d->ownAboutdata)
00138         delete _aboutData;
00139     _aboutData = 0;
00140 
00141     delete d;
00142     d = 0;
00143 
00144     delete _iconLoader;
00145     _iconLoader = 0;
00146 
00147     // delete _config; // Do not delete, stored in d->sharedConfig
00148     _config = 0;
00149     delete _dirs;
00150     _dirs = 0;
00151 
00152     if (KGlobal::_instance == this)
00153         KGlobal::_instance = 0;
00154     if (KGlobal::activeInstance() == this)
00155         KGlobal::setActiveInstance(0);
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     }
00168 
00169     return _dirs;
00170 }
00171 
00172 extern bool kde_kiosk_exception;
00173 
00174 KConfig *KInstance::config() const
00175 {
00176     DEBUG_CHECK_ALIVE
00177     if( _config == 0 ) {
00178         if ( !d->configName.isEmpty() )
00179         {
00180             d->sharedConfig = KSharedConfig::openConfig( d->configName );
00181 
00182             // Check whether custom config files are allowed.
00183             d->sharedConfig->setGroup( "KDE Action Restrictions" );
00184             QString kioskException = d->sharedConfig->readEntry("kiosk_exception");
00185             if (d->sharedConfig->readBoolEntry( "custom_config", true))
00186             {
00187                d->sharedConfig->setGroup(QString::null);
00188             }
00189             else
00190             {
00191                d->sharedConfig = 0;
00192             }
00193 
00194         }
00195 
00196         if ( d->sharedConfig == 0 )
00197         {
00198         if ( !_name.isEmpty() )
00199             d->sharedConfig = KSharedConfig::openConfig( _name + "rc");
00200         else
00201             d->sharedConfig = KSharedConfig::openConfig( QString::null );
00202     }
00203         d->sharedConfig->setGroup( "KDE Action Restrictions" );
00204         QString kioskException = d->sharedConfig->readEntry("kiosk_exception");
00205         d->sharedConfig->setGroup( QString::null );
00206         if (!kioskException.isEmpty() && !kde_kiosk_exception)
00207         {
00208             int i = kioskException.find(':');
00209             QString user = kioskException.left(i);
00210             QString host = kioskException.mid(i+1);
00211 
00212             KUser thisUser;
00213             char hostname[ 256 ];
00214             hostname[ 0 ] = '\0';
00215             if (!gethostname( hostname, 255 ))
00216                 hostname[sizeof(hostname)-1] = '\0';
00217                        
00218             if ((user == thisUser.loginName()) &&
00219                 (host.isEmpty() || (host == hostname)))
00220             {
00221                 kde_kiosk_exception = true;
00222                 d->sharedConfig = 0;
00223                 return config(); // Reread...
00224             }
00225         }
00226     
00227     _config = d->sharedConfig;
00228         if (_dirs)
00229             if (_dirs->addCustomized(_config))
00230                 _config->reparseConfiguration();
00231     }
00232 
00233     return _config;
00234 }
00235 
00236 KSharedConfig *KInstance::sharedConfig() const
00237 {
00238     DEBUG_CHECK_ALIVE
00239     if (_config == 0)
00240        (void) config(); // Initialize config
00241 
00242     return d->sharedConfig;
00243 }
00244 
00245 void KInstance::setConfigName(const QString &configName)
00246 {
00247     DEBUG_CHECK_ALIVE
00248     d->configName = configName;
00249 }
00250 
00251 KIconLoader *KInstance::iconLoader() const
00252 {
00253     DEBUG_CHECK_ALIVE
00254     if( _iconLoader == 0 ) {
00255     _iconLoader = new KIconLoader( _name, dirs() );
00256     _iconLoader->enableDelayedIconSetLoading( true );
00257     }
00258 
00259     return _iconLoader;
00260 }
00261 
00262 void KInstance::newIconLoader() const
00263 {
00264     DEBUG_CHECK_ALIVE
00265     KIconTheme::reconfigure();
00266     _iconLoader->reconfigure( _name, dirs() );
00267 }
00268 
00269 const KAboutData * KInstance::aboutData() const
00270 {
00271     DEBUG_CHECK_ALIVE
00272     return _aboutData;
00273 }
00274 
00275 QCString KInstance::instanceName() const
00276 {
00277     DEBUG_CHECK_ALIVE
00278     return _name;
00279 }
00280 
00281 KMimeSourceFactory* KInstance::mimeSourceFactory () const
00282 {
00283   DEBUG_CHECK_ALIVE
00284   if (!d->mimeSourceFactory)
00285   {
00286     d->mimeSourceFactory = new KMimeSourceFactory(iconLoader());
00287   }
00288 
00289   return d->mimeSourceFactory;
00290 }
00291 
00292 void KInstance::virtual_hook( int, void* )
00293 { /*BASE::virtual_hook( id, data );*/ }
00294 
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Jan 21 09:57:09 2005 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003