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 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
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
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
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 }