summaryrefslogtreecommitdiffstats
path: root/tdecore/kglobal.cpp
blob: f8639d526ce41514f1ea820583c417c5e7c8232d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* This file is part of the KDE libraries
   Copyright (C) 1999 Sirtaj Singh Kanq <taj@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
/*
* tdeglobal.cpp -- Implementation of class TDEGlobal.
* Author:	Sirtaj Singh Kang
* Version:	$Id$
* Generated:	Sat May  1 02:08:43 EST 1999
*/

#include <tqglobal.h>
#include <tqdict.h>
#include <tqptrlist.h>
#include "tdeglobal.h"

#include <tdeapplication.h>
#include <tdeaboutdata.h>
#include <kdebug.h>
#include <tdeconfig.h>
#include <tdelocale.h>
#include <kcharsets.h>
#include <kiconloader.h>
#include <tdehardwaredevices.h>
#include <tdenetworkconnections.h>
#include <kstandarddirs.h>
#include <kinstance.h>
#include "kstaticdeleter.h"

#include <tqfont.h>

#ifndef NDEBUG
#define MYASSERT(x) if (!x) \
   tqFatal("Fatal error: you need to have a TDEInstance object before\n" \
         "you do anything that requires it! Examples of this are config\n" \
         "objects, standard directories or translations.");
#else
#define MYASSERT(x) /* nope */
#endif

static void kglobal_init();

TDEStandardDirs *TDEGlobal::dirs()
{
    MYASSERT(_instance);

    return _instance->dirs();
}

TDEConfig	*TDEGlobal::config()
{
    MYASSERT(_instance);

    return _instance->config();
}

TDESharedConfig *TDEGlobal::sharedConfig()
{
    MYASSERT(_instance);

    return _instance->sharedConfig();
}

TDEIconLoader *TDEGlobal::iconLoader()
{
    MYASSERT(_instance);

    return _instance->iconLoader();
}

TDEHardwareDevices *TDEGlobal::hardwareDevices()
{
    MYASSERT(_instance);

    return _instance->hardwareDevices();
}

TDEGlobalNetworkManager *TDEGlobal::networkManager()
{
    MYASSERT(_instance);

    return _instance->networkManager();
}

TDEInstance *TDEGlobal::instance()
{
    MYASSERT(_instance);
    return _instance;
}

TDELocale	*TDEGlobal::locale()
{
    if( _locale == 0 ) {
	if (!_instance)
	   return 0;
        kglobal_init();

        // will set _locale if it works - otherwise 0 is returned
        TDELocale::initInstance();
        if( _instance->aboutData())
            _instance->aboutData()->translateInternalProgramName();
    }

    return _locale;
}

KCharsets *TDEGlobal::charsets()
{
    if( _charsets == 0 ) {
        _charsets =new KCharsets();
        kglobal_init();
    }

    return _charsets;
}

void TDEGlobal::setActiveInstance(TDEInstance *i)
{
    _activeInstance = i;
    if (i && _locale)
	_locale->setActiveCatalogue(TQString::fromUtf8(i->instanceName()));
}

/**
 * Create a static QString
 *
 * To be used inside functions(!) like:
 * static const TQString &myString = TDEGlobal::staticQString("myText");
 */
const TQString &
TDEGlobal::staticQString(const char *str)
{
   return staticQString(TQString::fromLatin1(str));
}

class KStringDict : public TQDict<TQString>
{
public:
   KStringDict() : TQDict<TQString>(139) { }
};

/**
 * Create a static QString
 *
 * To be used inside functions(!) like:
 * static const TQString &myString = TDEGlobal::staticQString(i18n("My Text"));
 */
const TQString &
TDEGlobal::staticQString(const TQString &str)
{
    if (!_stringDict) {
      _stringDict = new KStringDict;
      _stringDict->setAutoDelete( true );
      kglobal_init();
    }
   TQString *result = _stringDict->find(str);
   if (!result)
   {
      result = new TQString(str);
      _stringDict->insert(str, result);
   }
   return *result;
}

class KStaticDeleterList: public TQPtrList<KStaticDeleterBase>
{
public:
   KStaticDeleterList() { }
};

void
TDEGlobal::registerStaticDeleter(KStaticDeleterBase *obj)
{
   if (!_staticDeleters)
      kglobal_init();
   if (_staticDeleters->find(obj) == -1)
      _staticDeleters->append(obj);
}

void
TDEGlobal::unregisterStaticDeleter(KStaticDeleterBase *obj)
{
   if (_staticDeleters)
      _staticDeleters->removeRef(obj);
}

void
TDEGlobal::deleteStaticDeleters()
{
    if (!TDEGlobal::_staticDeleters)
        return;

    for(;_staticDeleters->count();)
    {
        _staticDeleters->take(0)->destructObject();
    }

    delete TDEGlobal::_staticDeleters;
    TDEGlobal::_staticDeleters = 0;
}

// The Variables

KStringDict     *TDEGlobal::_stringDict   = 0;
TDEInstance       *TDEGlobal::_instance     = 0;
TDEInstance       *TDEGlobal::_activeInstance = 0;
TDELocale         *TDEGlobal::_locale	= 0;
KCharsets       *TDEGlobal::_charsets	= 0;
KStaticDeleterList *TDEGlobal::_staticDeleters = 0;

#ifdef WIN32
#include <windows.h>
static void kglobal_freeAll();
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID impLoad )
{
    if (reason == DLL_PROCESS_DETACH)
        kglobal_freeAll();
    return TRUE;
}
#else
__attribute__((destructor))
#endif
static void kglobal_freeAll()
{
    delete TDEGlobal::_locale;
    TDEGlobal::_locale = 0;
    delete TDEGlobal::_charsets;
    TDEGlobal::_charsets = 0;
    delete TDEGlobal::_stringDict;
    TDEGlobal::_stringDict = 0;
    TDEGlobal::deleteStaticDeleters();
    // so that we don't hold a reference and see memory leaks :/
    TDEGlobal::setActiveInstance(0);
}

static void kglobal_init()
{
    if (TDEGlobal::_staticDeleters)
        return;

    TDEGlobal::_staticDeleters = new KStaticDeleterList;
}

int kasciistricmp( const char *str1, const char *str2 )
{
    const unsigned char *s1 = (const unsigned char *)str1;
    const unsigned char *s2 = (const unsigned char *)str2;
    int res;
    unsigned char c1, c2;

    if ( !s1 || !s2 )
        return s1 ? 1 : (s2 ? -1 : 0);
    if ( !*s1 || !*s2 )
        return *s1 ? 1 : (*s2 ? -1 : 0);
    for (;*s1; ++s1, ++s2) {
        c1 = *s1; c2 = *s2;
        if (c1 >= 'A' && c1 <= 'Z')
            c1 += 'a' - 'A';
        if (c2 >= 'A' && c2 <= 'Z')
            c2 += 'a' - 'A';

        if ((res = c1 - c2))
            break;
    }
    return *s1 ? res : (*s2 ? -1 : 0);
}