tdeglobal.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 * tdeglobal.cpp -- Implementation of class TDEGlobal. 00020 * Author: Sirtaj Singh Kang 00021 * Version: $Id$ 00022 * Generated: Sat May 1 02:08:43 EST 1999 00023 */ 00024 00025 #include <tqglobal.h> 00026 #include <tqdict.h> 00027 #include <tqptrlist.h> 00028 #include "tdeglobal.h" 00029 00030 #include <tdeapplication.h> 00031 #include <tdeaboutdata.h> 00032 #include <kdebug.h> 00033 #include <tdeconfig.h> 00034 #include <tdelocale.h> 00035 #include <kcharsets.h> 00036 #include <kiconloader.h> 00037 #ifdef __TDE_HAVE_TDEHWLIB 00038 #include <tdehardwaredevices.h> 00039 #include <tdenetworkconnections.h> 00040 #endif 00041 #include <kstandarddirs.h> 00042 #include <kinstance.h> 00043 #include "kstaticdeleter.h" 00044 00045 #include <tqfont.h> 00046 00047 #ifndef NDEBUG 00048 #define MYASSERT(x) if (!x) \ 00049 tqFatal("Fatal error: you need to have a TDEInstance object before\n" \ 00050 "you do anything that requires it! Examples of this are config\n" \ 00051 "objects, standard directories or translations."); 00052 #else 00053 #define MYASSERT(x) /* nope */ 00054 #endif 00055 00056 static void kglobal_init(); 00057 00058 TDEStandardDirs *TDEGlobal::dirs() 00059 { 00060 MYASSERT(_instance); 00061 00062 return _instance->dirs(); 00063 } 00064 00065 TDEConfig *TDEGlobal::config() 00066 { 00067 MYASSERT(_instance); 00068 00069 return _instance->config(); 00070 } 00071 00072 TDESharedConfig *TDEGlobal::sharedConfig() 00073 { 00074 MYASSERT(_instance); 00075 00076 return _instance->sharedConfig(); 00077 } 00078 00079 TDEIconLoader *TDEGlobal::iconLoader() 00080 { 00081 MYASSERT(_instance); 00082 00083 return _instance->iconLoader(); 00084 } 00085 00086 #ifdef __TDE_HAVE_TDEHWLIB 00087 TDEHardwareDevices *TDEGlobal::hardwareDevices() 00088 { 00089 MYASSERT(_instance); 00090 00091 return _instance->hardwareDevices(); 00092 } 00093 00094 TDEGlobalNetworkManager *TDEGlobal::networkManager() 00095 { 00096 MYASSERT(_instance); 00097 00098 return _instance->networkManager(); 00099 } 00100 #endif 00101 00102 TDEInstance *TDEGlobal::instance() 00103 { 00104 MYASSERT(_instance); 00105 return _instance; 00106 } 00107 00108 TDELocale *TDEGlobal::locale() 00109 { 00110 if( _locale == 0 ) { 00111 if (!_instance) 00112 return 0; 00113 kglobal_init(); 00114 00115 // will set _locale if it works - otherwise 0 is returned 00116 TDELocale::initInstance(); 00117 if( _instance->aboutData()) 00118 _instance->aboutData()->translateInternalProgramName(); 00119 } 00120 00121 return _locale; 00122 } 00123 00124 KCharsets *TDEGlobal::charsets() 00125 { 00126 if( _charsets == 0 ) { 00127 _charsets =new KCharsets(); 00128 kglobal_init(); 00129 } 00130 00131 return _charsets; 00132 } 00133 00134 void TDEGlobal::setActiveInstance(TDEInstance *i) 00135 { 00136 _activeInstance = i; 00137 if (i && _locale) 00138 _locale->setActiveCatalogue(TQString::fromUtf8(i->instanceName())); 00139 } 00140 00147 const TQString & 00148 TDEGlobal::staticQString(const char *str) 00149 { 00150 return staticQString(TQString::fromLatin1(str)); 00151 } 00152 00153 class KStringDict : public TQDict<TQString> 00154 { 00155 public: 00156 KStringDict() : TQDict<TQString>(139) { } 00157 }; 00158 00165 const TQString & 00166 TDEGlobal::staticQString(const TQString &str) 00167 { 00168 if (!_stringDict) { 00169 _stringDict = new KStringDict; 00170 _stringDict->setAutoDelete( true ); 00171 kglobal_init(); 00172 } 00173 TQString *result = _stringDict->find(str); 00174 if (!result) 00175 { 00176 result = new TQString(str); 00177 _stringDict->insert(str, result); 00178 } 00179 return *result; 00180 } 00181 00182 class KStaticDeleterList: public TQPtrList<KStaticDeleterBase> 00183 { 00184 public: 00185 KStaticDeleterList() { } 00186 }; 00187 00188 void 00189 TDEGlobal::registerStaticDeleter(KStaticDeleterBase *obj) 00190 { 00191 if (!_staticDeleters) 00192 kglobal_init(); 00193 if (_staticDeleters->find(obj) == -1) 00194 _staticDeleters->append(obj); 00195 } 00196 00197 void 00198 TDEGlobal::unregisterStaticDeleter(KStaticDeleterBase *obj) 00199 { 00200 if (_staticDeleters) 00201 _staticDeleters->removeRef(obj); 00202 } 00203 00204 void 00205 TDEGlobal::deleteStaticDeleters() 00206 { 00207 if (!TDEGlobal::_staticDeleters) 00208 return; 00209 00210 for(;_staticDeleters->count();) 00211 { 00212 _staticDeleters->take(0)->destructObject(); 00213 } 00214 00215 delete TDEGlobal::_staticDeleters; 00216 TDEGlobal::_staticDeleters = 0; 00217 } 00218 00219 // The Variables 00220 00221 KStringDict *TDEGlobal::_stringDict = 0; 00222 TDEInstance *TDEGlobal::_instance = 0; 00223 TDEInstance *TDEGlobal::_activeInstance = 0; 00224 TDELocale *TDEGlobal::_locale = 0; 00225 KCharsets *TDEGlobal::_charsets = 0; 00226 KStaticDeleterList *TDEGlobal::_staticDeleters = 0; 00227 00228 #ifdef WIN32 00229 #include <windows.h> 00230 static void kglobal_freeAll(); 00231 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID impLoad ) 00232 { 00233 if (reason == DLL_PROCESS_DETACH) 00234 kglobal_freeAll(); 00235 return TRUE; 00236 } 00237 #else 00238 __attribute__((destructor)) 00239 #endif 00240 static void kglobal_freeAll() 00241 { 00242 delete TDEGlobal::_locale; 00243 TDEGlobal::_locale = 0; 00244 delete TDEGlobal::_charsets; 00245 TDEGlobal::_charsets = 0; 00246 delete TDEGlobal::_stringDict; 00247 TDEGlobal::_stringDict = 0; 00248 TDEGlobal::deleteStaticDeleters(); 00249 // so that we don't hold a reference and see memory leaks :/ 00250 TDEGlobal::setActiveInstance(0); 00251 } 00252 00253 static void kglobal_init() 00254 { 00255 if (TDEGlobal::_staticDeleters) 00256 return; 00257 00258 TDEGlobal::_staticDeleters = new KStaticDeleterList; 00259 } 00260 00261 int kasciistricmp( const char *str1, const char *str2 ) 00262 { 00263 const unsigned char *s1 = (const unsigned char *)str1; 00264 const unsigned char *s2 = (const unsigned char *)str2; 00265 int res; 00266 unsigned char c1, c2; 00267 00268 if ( !s1 || !s2 ) 00269 return s1 ? 1 : (s2 ? -1 : 0); 00270 if ( !*s1 || !*s2 ) 00271 return *s1 ? 1 : (*s2 ? -1 : 0); 00272 for (;*s1; ++s1, ++s2) { 00273 c1 = *s1; c2 = *s2; 00274 if (c1 >= 'A' && c1 <= 'Z') 00275 c1 += 'a' - 'A'; 00276 if (c2 >= 'A' && c2 <= 'Z') 00277 c2 += 'a' - 'A'; 00278 00279 if ((res = c1 - c2)) 00280 break; 00281 } 00282 return *s1 ? res : (*s2 ? -1 : 0); 00283 } 00284