kglobal.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <qglobal.h>
00026 #include <qdict.h>
00027 #include <qptrlist.h>
00028 #include "kglobal.h"
00029
00030 #include <kapplication.h>
00031 #include <kaboutdata.h>
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)
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
00096 KLocale::initInstance();
00097 if( _instance->aboutData())
00098 _instance->aboutData()->translateInternalProgramName();
00099 }
00100
00101 return _locale;
00102 }
00103
00104 KCharsets *KGlobal::charsets()
00105 {
00106 if( _charsets == 0 ) {
00107 _charsets =new KCharsets();
00108 kglobal_init();
00109 }
00110
00111 return _charsets;
00112 }
00113
00114 void KGlobal::setActiveInstance(KInstance *i)
00115 {
00116 _activeInstance = i;
00117 if (i && _locale)
00118 _locale->setActiveCatalogue(QString::fromUtf8(i->instanceName()));
00119 }
00120
00127 const QString &
00128 KGlobal::staticQString(const char *str)
00129 {
00130 return staticQString(QString::fromLatin1(str));
00131 }
00132
00133 class KStringDict : public QDict<QString>
00134 {
00135 public:
00136 KStringDict() : QDict<QString>(139) { }
00137 };
00138
00145 const QString &
00146 KGlobal::staticQString(const QString &str)
00147 {
00148 if (!_stringDict) {
00149 _stringDict = new KStringDict;
00150 _stringDict->setAutoDelete( true );
00151 kglobal_init();
00152 }
00153 QString *result = _stringDict->find(str);
00154 if (!result)
00155 {
00156 result = new QString(str);
00157 _stringDict->insert(str, result);
00158 }
00159 return *result;
00160 }
00161
00162 class KStaticDeleterList: public QPtrList<KStaticDeleterBase>
00163 {
00164 public:
00165 KStaticDeleterList() { }
00166 };
00167
00168 void
00169 KGlobal::registerStaticDeleter(KStaticDeleterBase *obj)
00170 {
00171 if (!_staticDeleters)
00172 kglobal_init();
00173 if (_staticDeleters->find(obj) == -1)
00174 _staticDeleters->append(obj);
00175 }
00176
00177 void
00178 KGlobal::unregisterStaticDeleter(KStaticDeleterBase *obj)
00179 {
00180 if (_staticDeleters)
00181 _staticDeleters->removeRef(obj);
00182 }
00183
00184 void
00185 KGlobal::deleteStaticDeleters()
00186 {
00187 if (!KGlobal::_staticDeleters)
00188 return;
00189
00190 for(;_staticDeleters->count();)
00191 {
00192 _staticDeleters->take(0)->destructObject();
00193 }
00194
00195 delete KGlobal::_staticDeleters;
00196 KGlobal::_staticDeleters = 0;
00197 }
00198
00199
00200
00201 KStringDict *KGlobal::_stringDict = 0;
00202 KInstance *KGlobal::_instance = 0;
00203 KInstance *KGlobal::_activeInstance = 0;
00204 KLocale *KGlobal::_locale = 0;
00205 KCharsets *KGlobal::_charsets = 0;
00206 KStaticDeleterList *KGlobal::_staticDeleters = 0;
00207
00208 #ifdef WIN32
00209 #include <windows.h>
00210 static void kglobal_freeAll();
00211 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID impLoad )
00212 {
00213 if (reason == DLL_PROCESS_DETACH)
00214 kglobal_freeAll();
00215 return TRUE;
00216 }
00217 #else
00218 __attribute__((destructor))
00219 #endif
00220 static void kglobal_freeAll()
00221 {
00222 delete KGlobal::_locale;
00223 KGlobal::_locale = 0;
00224 delete KGlobal::_charsets;
00225 KGlobal::_charsets = 0;
00226 delete KGlobal::_stringDict;
00227 KGlobal::_stringDict = 0;
00228 KGlobal::deleteStaticDeleters();
00229
00230 KGlobal::setActiveInstance(0);
00231 }
00232
00233 static void kglobal_init()
00234 {
00235 if (KGlobal::_staticDeleters)
00236 return;
00237
00238 KGlobal::_staticDeleters = new KStaticDeleterList;
00239 }
00240
00241 int kasciistricmp( const char *str1, const char *str2 )
00242 {
00243 const unsigned char *s1 = (const unsigned char *)str1;
00244 const unsigned char *s2 = (const unsigned char *)str2;
00245 int res;
00246 unsigned char c1, c2;
00247
00248 if ( !s1 || !s2 )
00249 return s1 ? 1 : (s2 ? -1 : 0);
00250 if ( !*s1 || !*s2 )
00251 return *s1 ? 1 : (*s2 ? -1 : 0);
00252 for (;*s1; ++s1, ++s2) {
00253 c1 = *s1; c2 = *s2;
00254 if (c1 >= 'A' && c1 <= 'Z')
00255 c1 += 'a' - 'A';
00256 if (c2 >= 'A' && c2 <= 'Z')
00257 c2 += 'a' - 'A';
00258
00259 if ((res = c1 - c2))
00260 break;
00261 }
00262 return *s1 ? res : (*s2 ? -1 : 0);
00263 }
00264
|