KDECore
kglobal.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Sirtaj Singh Kanq <taj@kde.org> 00003 Copyright (C) 2007 Matthias Kretz <kretz@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 /* 00021 * kglobal.cpp -- Implementation of namespace KGlobal. 00022 * Author: Sirtaj Singh Kang 00023 * Generated: Sat May 1 02:08:43 EST 1999 00024 */ 00025 00026 #undef KDE3_SUPPORT 00027 00028 #include "kglobal.h" 00029 #include "kglobal_p.h" 00030 00031 #include <QtCore/QList> 00032 #include <QtCore/QSet> 00033 00034 #include <kaboutdata.h> 00035 #include <kconfig.h> 00036 #include <klocale.h> 00037 #include <kcharsets.h> 00038 #include <kstandarddirs.h> 00039 #include <kcomponentdata.h> 00040 #include <QtCore/QCoreApplication> 00041 #include <QtCore/QTextCodec> 00042 #include "kcmdlineargs.h" 00043 00044 #ifndef NDEBUG 00045 #define MYASSERT(x) if (!x) \ 00046 qFatal("Fatal error: you need to have a KComponentData object before\n" \ 00047 "you do anything that requires it! Examples of this are config\n" \ 00048 "objects, standard directories or translations."); 00049 #else 00050 #define MYASSERT(x) /* nope */ 00051 #endif 00052 00053 // ~KConfig needs qrand(). qrand() depends on a Q_GLOBAL_STATIC. With this Q_CONSTRUCTOR_FUNCTION we 00054 // try to make qrand() live longer than any KConfig object. 00055 Q_CONSTRUCTOR_FUNCTION(qrand) 00056 00057 typedef QSet<QString> KStringDict; 00058 00059 class KGlobalPrivate 00060 { 00061 public: 00062 inline KGlobalPrivate() 00063 : stringDict(0), 00064 locale(0), 00065 charsets(0) 00066 { 00067 } 00068 00069 inline ~KGlobalPrivate() 00070 { 00071 delete locale; 00072 locale = 0; 00073 delete charsets; 00074 charsets = 0; 00075 delete stringDict; 00076 stringDict = 0; 00077 } 00078 00079 KComponentData activeComponent; 00080 KComponentData mainComponent; // holds a refcount 00081 KStringDict *stringDict; 00082 KLocale *locale; 00083 KCharsets *charsets; 00084 }; 00085 00086 K_GLOBAL_STATIC(KGlobalPrivate, globalData) 00087 00088 #define PRIVATE_DATA KGlobalPrivate *d = globalData 00089 00090 KStandardDirs *KGlobal::dirs() 00091 { 00092 PRIVATE_DATA; 00093 MYASSERT(d->mainComponent.isValid()); 00094 return d->mainComponent.dirs(); 00095 } 00096 00097 KSharedConfig::Ptr KGlobal::config() 00098 { 00099 PRIVATE_DATA; 00100 MYASSERT(d->mainComponent.isValid()); 00101 return d->mainComponent.config(); 00102 } 00103 00104 const KComponentData &KGlobal::mainComponent() 00105 { 00106 PRIVATE_DATA; 00107 MYASSERT(d->mainComponent.isValid()); 00108 return d->mainComponent; 00109 } 00110 00111 bool KGlobal::hasMainComponent() 00112 { 00113 if (globalData.isDestroyed()) { 00114 return false; 00115 } 00116 PRIVATE_DATA; 00117 return d->mainComponent.isValid(); 00118 } 00119 00120 KLocale *KGlobal::locale() 00121 { 00122 PRIVATE_DATA; 00123 if (d->locale == 0) { 00124 if (!d->mainComponent.isValid()) { 00125 return 0; 00126 } 00127 00128 d->locale = new KLocale(d->mainComponent.catalogName()); 00129 QTextCodec::setCodecForLocale(d->locale->codecForEncoding()); 00130 d->mainComponent.aboutData()->translateInternalProgramName(); 00131 } 00132 return d->locale; 00133 } 00134 00135 bool KGlobal::hasLocale() 00136 { 00137 if (globalData.isDestroyed()) { 00138 return false; 00139 } 00140 PRIVATE_DATA; 00141 return (d->locale != 0); 00142 } 00143 00144 KCharsets *KGlobal::charsets() 00145 { 00146 PRIVATE_DATA; 00147 if (d->charsets == 0) { 00148 d->charsets = new KCharsets; 00149 } 00150 00151 return d->charsets; 00152 } 00153 00154 KComponentData KGlobal::activeComponent() 00155 { 00156 PRIVATE_DATA; 00157 MYASSERT(d->activeComponent.isValid()); 00158 return d->activeComponent; 00159 } 00160 00161 void KGlobal::setActiveComponent(const KComponentData &c) 00162 { 00163 PRIVATE_DATA; 00164 d->activeComponent = c; 00165 if (c.isValid() && d->locale) { 00166 d->locale->setActiveCatalog(c.catalogName()); 00167 } 00168 } 00169 00170 void KGlobal::newComponentData(const KComponentData &c) 00171 { 00172 PRIVATE_DATA; 00173 if (d->mainComponent.isValid()) { 00174 return; 00175 } 00176 d->mainComponent = c; 00177 KGlobal::setActiveComponent(c); 00178 } 00179 00180 void KGlobal::setLocale(KLocale *locale, CopyCatalogs copy) 00181 { 00182 PRIVATE_DATA; 00183 if (copy == DoCopyCatalogs && d->locale) 00184 d->locale->copyCatalogsTo(locale); 00185 delete d->locale; 00186 d->locale = locale; 00187 } 00188 00195 const QString &KGlobal::staticQString(const char *str) 00196 { 00197 return staticQString(QLatin1String(str)); 00198 } 00199 00206 const QString &KGlobal::staticQString(const QString &str) 00207 { 00208 PRIVATE_DATA; 00209 if (!d->stringDict) { 00210 d->stringDict = new KStringDict; 00211 } 00212 00213 return *d->stringDict->insert(str); 00214 } 00215 00216 QString KGlobal::caption() 00217 { 00218 PRIVATE_DATA; 00219 // Caption set from command line ? 00220 KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde"); 00221 if (args && args->isSet("caption")) { 00222 return args->getOption("caption"); 00223 } else { 00224 // We have some about data ? 00225 if (d->mainComponent.aboutData()) { 00226 return d->mainComponent.aboutData()->programName(); 00227 } else { 00228 // Last resort : application name 00229 return QCoreApplication::instance()->applicationName(); 00230 } 00231 } 00232 } 00233 00240 static int s_refCount = 1; 00241 00242 void KGlobal::ref() 00243 { 00244 ++s_refCount; 00245 //kDebug() << "KGlobal::ref() : refCount = " << s_refCount; 00246 } 00247 00248 void KGlobal::deref() 00249 { 00250 --s_refCount; 00251 //kDebug() << "KGlobal::deref() : refCount = " << s_refCount; 00252 if (s_refCount <= 0) { 00253 QCoreApplication::instance()->quit(); 00254 } 00255 } 00256 00257 #undef PRIVATE_DATA
KDE 4.0 API Reference