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., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 /*
00019 * kglobal.cpp -- Implementation of class KGlobal.
00020 * Author:   Sirtaj Singh Kang
00021 * Version:  $Id: kglobal.cpp 465272 2005-09-29 09:47:40Z mueller $
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 <kdebug.h>
00033 #include <kconfig.h>
00034 #include <klocale.h>
00035 #include <kcharsets.h>
00036 #include <kiconloader.h>
00037 #include <kstandarddirs.h>
00038 #include <kinstance.h>
00039 #include "kstaticdeleter.h"
00040 
00041 #include <qfont.h>
00042 
00043 #ifndef NDEBUG
00044 #define MYASSERT(x) if (!x) \
00045    qFatal("Fatal error: you need to have a KInstance object before\n" \
00046          "you do anything that requires it! Examples of this are config\n" \
00047          "objects, standard directories or translations.");
00048 #else
00049 #define MYASSERT(x) /* nope */
00050 #endif
00051 
00052 static void kglobal_init();
00053 
00054 KStandardDirs *KGlobal::dirs()
00055 {
00056     MYASSERT(_instance);
00057 
00058     return _instance->dirs();
00059 }
00060 
00061 KConfig *KGlobal::config()
00062 {
00063     MYASSERT(_instance);
00064 
00065     return _instance->config();
00066 }
00067 
00068 KSharedConfig *KGlobal::sharedConfig()
00069 {
00070     MYASSERT(_instance);
00071 
00072     return _instance->sharedConfig();
00073 }
00074 
00075 KIconLoader *KGlobal::iconLoader()
00076 {
00077     MYASSERT(_instance);
00078 
00079     return _instance->iconLoader();
00080 }
00081 
00082 KInstance *KGlobal::instance()
00083 {
00084     MYASSERT(_instance);
00085     return _instance;
00086 }
00087 
00088 KLocale *KGlobal::locale()
00089 {
00090     if( _locale == 0 ) {
00091     if (!_instance)
00092        return 0;
00093         kglobal_init();
00094 
00095         // will set _locale if it works - otherwise 0 is returned
00096         KLocale::initInstance();
00097     }
00098 
00099     return _locale;
00100 }
00101 
00102 KCharsets *KGlobal::charsets()
00103 {
00104     if( _charsets == 0 ) {
00105         _charsets =new KCharsets();
00106         kglobal_init();
00107     }
00108 
00109     return _charsets;
00110 }
00111 
00112 void KGlobal::setActiveInstance(KInstance *i)
00113 {
00114     _activeInstance = i;
00115     if (i && _locale)
00116     _locale->setActiveCatalogue(QString::fromUtf8(i->instanceName()));
00117 }
00118 
00125 const QString &
00126 KGlobal::staticQString(const char *str)
00127 {
00128    return staticQString(QString::fromLatin1(str));
00129 }
00130 
00131 class KStringDict : public QDict<QString>
00132 {
00133 public:
00134    KStringDict() : QDict<QString>(139) { };
00135 };
00136 
00143 const QString &
00144 KGlobal::staticQString(const QString &str)
00145 {
00146     if (!_stringDict) {
00147       _stringDict = new KStringDict;
00148       _stringDict->setAutoDelete( true );
00149       kglobal_init();
00150     }
00151    QString *result = _stringDict->find(str);
00152    if (!result)
00153    {
00154       result = new QString(str);
00155       _stringDict->insert(str, result);
00156    }
00157    return *result;
00158 }
00159 
00160 class KStaticDeleterList: public QPtrList<KStaticDeleterBase>
00161 {
00162 public:
00163    KStaticDeleterList() { }
00164 };
00165 
00166 void
00167 KGlobal::registerStaticDeleter(KStaticDeleterBase *obj)
00168 {
00169    if (!_staticDeleters)
00170       kglobal_init();
00171    if (_staticDeleters->find(obj) == -1)
00172       _staticDeleters->append(obj);
00173 }
00174 
00175 void
00176 KGlobal::unregisterStaticDeleter(KStaticDeleterBase *obj)
00177 {
00178    if (_staticDeleters)
00179       _staticDeleters->removeRef(obj);
00180 }
00181 
00182 void
00183 KGlobal::deleteStaticDeleters()
00184 {
00185     if (!KGlobal::_staticDeleters)
00186         return;
00187 
00188     for(;_staticDeleters->count();)
00189     {
00190         _staticDeleters->take(0)->destructObject();
00191     }
00192 
00193     delete KGlobal::_staticDeleters;
00194     KGlobal::_staticDeleters = 0;
00195 }
00196 
00197 // The Variables
00198 
00199 KStringDict     *KGlobal::_stringDict   = 0;
00200 KInstance       *KGlobal::_instance     = 0;
00201 KInstance       *KGlobal::_activeInstance = 0;
00202 KLocale         *KGlobal::_locale   = 0;
00203 KCharsets       *KGlobal::_charsets = 0;
00204 KStaticDeleterList *KGlobal::_staticDeleters = 0;
00205 
00206 #ifdef WIN32
00207 #include <windows.h>
00208 static void kglobal_freeAll();
00209 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID impLoad )
00210 {
00211     if (reason == DLL_PROCESS_DETACH)
00212         kglobal_freeAll();
00213     return TRUE;
00214 }
00215 #else
00216 __attribute__((destructor))
00217 #endif
00218 static void kglobal_freeAll()
00219 {
00220     delete KGlobal::_locale;
00221     KGlobal::_locale = 0;
00222     delete KGlobal::_charsets;
00223     KGlobal::_charsets = 0;
00224     delete KGlobal::_stringDict;
00225     KGlobal::_stringDict = 0;
00226     KGlobal::deleteStaticDeleters();
00227     // so that we don't hold a reference and see memory leaks :/
00228     KGlobal::setActiveInstance(0);
00229 }
00230 
00231 static void kglobal_init()
00232 {
00233     if (KGlobal::_staticDeleters)
00234         return;
00235 
00236     KGlobal::_staticDeleters = new KStaticDeleterList;
00237 }
00238 
00239 int kasciistricmp( const char *str1, const char *str2 )
00240 {
00241     const unsigned char *s1 = (const unsigned char *)str1;
00242     const unsigned char *s2 = (const unsigned char *)str2;
00243     int res;
00244     unsigned char c1, c2;
00245 
00246     if ( !s1 || !s2 )
00247         return s1 ? 1 : (s2 ? -1 : 0);
00248     if ( !*s1 || !*s2 )
00249         return *s1 ? 1 : (*s2 ? -1 : 0);
00250     for (;*s1; ++s1, ++s2) {
00251         c1 = *s1; c2 = *s2;
00252         if (c1 >= 'A' && c1 <= 'Z')
00253             c1 += 'a' - 'A';
00254         if (c2 >= 'A' && c2 <= 'Z')
00255             c2 += 'a' - 'A';
00256 
00257         if ((res = c1 - c2))
00258             break;
00259     }
00260     return *s1 ? res : (*s2 ? -1 : 0);
00261 }
00262 
KDE Home | KDE Accessibility Home | Description of Access Keys