# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | 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 365675 2004-11-23 23:33:45Z kloecker $
    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 KSharedConfig *KGlobal::sharedConfig()
    00068 {
    00069     MYASSERT(_instance);
    00070
    00071     return _instance->sharedConfig();
    00072 }
    00073
    00074 KIconLoader *KGlobal::iconLoader()
    00075 {
    00076     MYASSERT(_instance);
    00077
    00078     return _instance->iconLoader();
    00079 }
    00080
    00081 KInstance *KGlobal::instance()
    00082 {
    00083     MYASSERT(_instance);
    00084     return _instance;
    00085 }
    00086
    00087 KLocale *KGlobal::locale()
    00088 {
    00089     if( _locale == 0 ) {
    00090     if (!_instance)
    00091        return 0;
    00092         kglobal_init();
    00093
    00094         // will set _locale if it works - otherwise 0 is returned
    00095         KLocale::initInstance();
    00096     }
    00097
    00098     return _locale;
    00099 }
    00100
    00101 KCharsets *KGlobal::charsets()
    00102 {
    00103     if( _charsets == 0 ) {
    00104         _charsets =new KCharsets();
    00105         kglobal_init();
    00106     }
    00107
    00108     return _charsets;
    00109 }
    00110
    00111 void KGlobal::setActiveInstance(KInstance *i)
    00112 {
    00113     _activeInstance = i;
    00114     if (i && _locale)
    00115     _locale->setActiveCatalogue(QString::fromUtf8(i->instanceName()));
    00116 }
    00117
    00124 const QString &
    00125 KGlobal::staticQString(const char *str)
    00126 {
    00127    return staticQString(QString::fromLatin1(str));
    00128 }
    00129
    00130 class KStringDict : public QDict<QString>
    00131 {
    00132 public:
    00133    KStringDict() : QDict<QString>(139) { };
    00134 };
    00135
    00142 const QString &
    00143 KGlobal::staticQString(const QString &str)
    00144 {
    00145     if (!_stringDict) {
    00146       _stringDict = new KStringDict;
    00147       _stringDict->setAutoDelete( true );
    00148       kglobal_init();
    00149     }
    00150    QString *result = _stringDict->find(str);
    00151    if (!result)
    00152    {
    00153       result = new QString(str);
    00154       _stringDict->insert(str, result);
    00155    }
    00156    return *result;
    00157 }
    00158
    00159 class KStaticDeleterList: public QPtrList<KStaticDeleterBase>
    00160 {
    00161 public:
    00162    KStaticDeleterList() { }
    00163 };
    00164
    00165 void
    00166 KGlobal::registerStaticDeleter(KStaticDeleterBase *obj)
    00167 {
    00168    if (!_staticDeleters)
    00169       kglobal_init();
    00170    if (_staticDeleters->find(obj) == -1)
    00171       _staticDeleters->append(obj);
    00172 }
    00173
    00174 void
    00175 KGlobal::unregisterStaticDeleter(KStaticDeleterBase *obj)
    00176 {
    00177    if (_staticDeleters)
    00178       _staticDeleters->removeRef(obj);
    00179 }
    00180
    00181 void
    00182 KGlobal::deleteStaticDeleters()
    00183 {
    00184     if (!KGlobal::_staticDeleters)
    00185         return;
    00186
    00187     for(;_staticDeleters->count();)
    00188     {
    00189         _staticDeleters->take(0)->destructObject();
    00190     }
    00191
    00192     delete KGlobal::_staticDeleters;
    00193     KGlobal::_staticDeleters = 0;
    00194 }
    00195
    00196 // The Variables
    00197
    00198 KStringDict     *KGlobal::_stringDict   = 0;
    00199 KInstance       *KGlobal::_instance     = 0;
    00200 KInstance       *KGlobal::_activeInstance = 0;
    00201 KLocale         *KGlobal::_locale   = 0;
    00202 KCharsets       *KGlobal::_charsets = 0;
    00203 KStaticDeleterList *KGlobal::_staticDeleters = 0;
    00204
    00205 static void kglobal_freeAll()
    00206 {
    00207     delete KGlobal::_locale;
    00208     KGlobal::_locale = 0;
    00209     delete KGlobal::_charsets;
    00210     KGlobal::_charsets = 0;
    00211     delete KGlobal::_stringDict;
    00212     KGlobal::_stringDict = 0;
    00213     KGlobal::deleteStaticDeleters();
    00214     // so that we don't hold a reference and see memory leaks :/
    00215     KGlobal::setActiveInstance(0);
    00216 }
    00217
    00218 static bool addedFreeAll = false;
    00219
    00220 static void kglobal_init()
    00221 {
    00222     if (addedFreeAll)
    00223         return;
    00224
    00225     addedFreeAll = true;
    00226     KGlobal::_staticDeleters = new KStaticDeleterList;
    00227
    00228     qAddPostRoutine( kglobal_freeAll );
    00229 }
    00230
    00231 int kasciistricmp( const char *str1, const char *str2 )
    00232 {
    00233     const unsigned char *s1 = (const unsigned char *)str1;
    00234     const unsigned char *s2 = (const unsigned char *)str2;
    00235     int res;
    00236     unsigned char c1, c2;
    00237
    00238     if ( !s1 || !s2 )
    00239         return s1 ? 1 : (s2 ? -1 : 0);
    00240     if ( !*s1 || !*s2 )
    00241         return *s1 ? 1 : (*s2 ? -1 : 0);
    00242     for (;*s1; ++s1, ++s2) {
    00243         c1 = *s1; c2 = *s2;
    00244         if (c1 >= 'A' && c1 <= 'Z')
    00245             c1 += 'a' - 'A';
    00246         if (c2 >= 'A' && c2 <= 'Z')
    00247             c2 += 'a' - 'A';
    00248
    00249         if ((res = c1 - c2))
    00250             break;
    00251     }
    00252     return *s1 ? res : (*s2 ? -1 : 0);
    00253 }
    00254

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:18:04 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003