kglobal.cpp
00001
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
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)
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
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
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
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.4.