# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kglobal.cpp

    00001 /* This file is part of the KDE libraries
    00002    Copyright (C) 1999 Sirtaj Singh Kanq <taj@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 /*
    00019 * kglobal.cpp -- Implementation of class KGlobal.
    00020 * Author:       Sirtaj Singh Kang
    00021 * Version:      $Id: kglobal.cpp,v 1.66 2002/01/26 12:14:35 coolo Exp $
    00022 * Generated:    Sat May  1 02:08:43 EST 1999
    00023 */
    00024
    00025 #include <qglobal.h>
    00026 #include <qdict.h>
    00027 #include <qptrlist.h>
    00028 #include "kglobal.h"
    00029
    00030 #include <kapplication.h>
    00031
    00032 #include <kconfig.h>
    00033 #include <klocale.h>
    00034 #include <kcharsets.h>
    00035 #include <kiconloader.h>
    00036 #include <kstandarddirs.h>
    00037 #include <kinstance.h>
    00038 #include "kstaticdeleter.h"
    00039
    00040 #include <qfont.h>
    00041
    00042 #ifndef NDEBUG
    00043 #define MYASSERT(x) if (!x) \
    00044    qFatal("Fatal error: you need to have a KInstance object before\n" \
    00045          "you do anything that requires it! Examples of this are config\n" \
    00046          "objects, standard directories or translations.");
    00047 #else
    00048 #define MYASSERT(x) /* nope */
    00049 #endif
    00050
    00051 static void kglobal_init();
    00052
    00053 KStandardDirs *KGlobal::dirs()
    00054 {
    00055     MYASSERT(_instance);
    00056
    00057     return _instance->dirs();
    00058 }
    00059
    00060 KConfig *KGlobal::config()
    00061 {
    00062     MYASSERT(_instance);
    00063
    00064     return _instance->config();
    00065 }
    00066
    00067 KIconLoader *KGlobal::iconLoader()
    00068 {
    00069     MYASSERT(_instance);
    00070
    00071     return _instance->iconLoader();
    00072 }
    00073
    00074 KInstance *KGlobal::instance()
    00075 {
    00076     MYASSERT(_instance);
    00077     return _instance;
    00078 }
    00079
    00080 KLocale *KGlobal::locale()
    00081 {
    00082     if( _locale == 0 ) {
    00083         if (!_instance)
    00084            return 0;
    00085         kglobal_init();
    00086
    00087         // will set _locale if it works - otherwise 0 is returned
    00088         KLocale::initInstance();
    00089     }
    00090
    00091     return _locale;
    00092 }
    00093
    00094 KCharsets *KGlobal::charsets()
    00095 {
    00096     if( _charsets == 0 ) {
    00097         _charsets =new KCharsets();
    00098         kglobal_init();
    00099     }
    00100
    00101     return _charsets;
    00102 }
    00103
    00104 void KGlobal::setActiveInstance(KInstance *i)
    00105 {
    00106     _activeInstance = i;
    00107     if (i && _locale)
    00108         _locale->setActiveCatalogue(QString::fromUtf8(i->instanceName()));
    00109 }
    00110
    00117 const QString &
    00118 KGlobal::staticQString(const char *str)
    00119 {
    00120    return staticQString(QString::fromLatin1(str));
    00121 }
    00122
    00123 class KStringDict : public QDict<QString>
    00124 {
    00125 public:
    00126    KStringDict() : QDict<QString>() { };
    00127 };
    00128
    00135 const QString &
    00136 KGlobal::staticQString(const QString &str)
    00137 {
    00138     if (!_stringDict) {
    00139       _stringDict = new KStringDict;
    00140       _stringDict->setAutoDelete( true );
    00141       kglobal_init();
    00142     }
    00143    QString *result = _stringDict->find(str);
    00144    if (!result)
    00145    {
    00146       result = new QString(str);
    00147       _stringDict->insert(str, result);
    00148    }
    00149    return *result;
    00150 }
    00151
    00152 class KStaticDeleterList: public QPtrList<KStaticDeleterBase>
    00153 {
    00154 public:
    00155    KStaticDeleterList() { };
    00156 };
    00157
    00158 void
    00159 KGlobal::registerStaticDeleter(KStaticDeleterBase *obj)
    00160 {
    00161    if (!_staticDeleters)
    00162       kglobal_init();
    00163    if (_staticDeleters->find(obj) == -1)
    00164       _staticDeleters->append(obj);
    00165 }
    00166
    00167 void
    00168 KGlobal::unregisterStaticDeleter(KStaticDeleterBase *obj)
    00169 {
    00170    if (_staticDeleters)
    00171       _staticDeleters->removeRef(obj);
    00172 }
    00173
    00174 void
    00175 KGlobal::deleteStaticDeleters()
    00176 {
    00177     if (!KGlobal::_staticDeleters)
    00178         return;
    00179
    00180     for(;_staticDeleters->count();)
    00181     {
    00182         _staticDeleters->take(0)->destructObject();
    00183     }
    00184
    00185     delete KGlobal::_staticDeleters;
    00186     KGlobal::_staticDeleters = 0;
    00187 }
    00188
    00189 // The Variables
    00190
    00191 KStringDict     *KGlobal::_stringDict   = 0;
    00192 KInstance       *KGlobal::_instance     = 0;
    00193 KInstance       *KGlobal::_activeInstance = 0;
    00194 KLocale         *KGlobal::_locale       = 0;
    00195 KCharsets       *KGlobal::_charsets     = 0;
    00196 KStaticDeleterList *KGlobal::_staticDeleters = 0;
    00197
    00198 static void kglobal_freeAll()
    00199 {
    00200     delete KGlobal::_locale;
    00201     KGlobal::_locale = 0;
    00202     delete KGlobal::_charsets;
    00203     KGlobal::_charsets = 0;
    00204     delete KGlobal::_stringDict;
    00205     KGlobal::_stringDict = 0;
    00206     KGlobal::deleteStaticDeleters();
    00207     // so that we don't hold a reference and see memory leaks :/
    00208     KGlobal::setActiveInstance(0);
    00209 }
    00210
    00211 static bool addedFreeAll = false;
    00212
    00213 static void kglobal_init()
    00214 {
    00215     if (addedFreeAll)
    00216         return;
    00217
    00218     addedFreeAll = true;
    00219     KGlobal::_staticDeleters = new KStaticDeleterList;
    00220
    00221     qAddPostRoutine( kglobal_freeAll );
    00222 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:30 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001