• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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 <config.h>
00032 
00033 #ifdef HAVE_SYS_STAT_H
00034 #include <sys/stat.h>
00035 #endif
00036 
00037 #include <QtCore/QList>
00038 #include <QtCore/QSet>
00039 
00040 #include <kaboutdata.h>
00041 #include <kconfig.h>
00042 #include <klocale.h>
00043 #include <kcharsets.h>
00044 #include <kstandarddirs.h>
00045 #include <kcomponentdata.h>
00046 #include <QtCore/QCoreApplication>
00047 #include <QtCore/QTextCodec>
00048 #include "kcmdlineargs.h"
00049 #include <unistd.h> // umask
00050 
00051 #ifndef NDEBUG
00052 #define MYASSERT(x) if (!x) \
00053    qFatal("Fatal error: you need to have a KComponentData object before\n" \
00054          "you do anything that requires it! Examples of this are config\n" \
00055          "objects, standard directories or translations.");
00056 #else
00057 #define MYASSERT(x) /* nope */
00058 #endif
00059 
00060 // ~KConfig needs qrand(). qrand() depends on a Q_GLOBAL_STATIC. With this Q_CONSTRUCTOR_FUNCTION we
00061 // try to make qrand() live longer than any KConfig object.
00062 Q_CONSTRUCTOR_FUNCTION(qrand)
00063 
00064 typedef QSet<QString> KStringDict;
00065 mode_t s_umsk;
00066 
00067 class KGlobalPrivate
00068 {
00069     public:
00070         inline KGlobalPrivate()
00071             : stringDict(0),
00072             locale(0),
00073             charsets(0)
00074         {
00075             // the umask is read here before any threads are created to avoid race conditions
00076             mode_t tmp = 0;
00077             s_umsk = umask(tmp);
00078             umask(s_umsk);
00079         }
00080 
00081         inline ~KGlobalPrivate()
00082         {
00083             delete locale;
00084             locale = 0;
00085             delete charsets;
00086             charsets = 0;
00087             delete stringDict;
00088             stringDict = 0;
00089         }
00090 
00091         KComponentData activeComponent;
00092         KComponentData mainComponent; // holds a refcount
00093         KStringDict *stringDict;
00094         KLocale *locale;
00095         KCharsets *charsets;
00096 };
00097 
00098 K_GLOBAL_STATIC(KGlobalPrivate, globalData)
00099 
00100 #define PRIVATE_DATA KGlobalPrivate *d = globalData
00101 
00102 KStandardDirs *KGlobal::dirs()
00103 {
00104     PRIVATE_DATA;
00105     MYASSERT(d->mainComponent.isValid());
00106     return d->mainComponent.dirs();
00107 }
00108 
00109 KSharedConfig::Ptr KGlobal::config()
00110 {
00111     PRIVATE_DATA;
00112     MYASSERT(d->mainComponent.isValid());
00113     return d->mainComponent.config();
00114 }
00115 
00116 const KComponentData &KGlobal::mainComponent()
00117 {
00118     PRIVATE_DATA;
00119     MYASSERT(d->mainComponent.isValid());
00120     return d->mainComponent;
00121 }
00122 
00123 bool KGlobal::hasMainComponent()
00124 {
00125     if (globalData.isDestroyed()) {
00126         return false;
00127     }
00128     PRIVATE_DATA;
00129     return d->mainComponent.isValid();
00130 }
00131 
00132 KLocale *KGlobal::locale()
00133 {
00134     PRIVATE_DATA;
00135     if (d->locale == 0) {
00136         if (!d->mainComponent.isValid()) {
00137             return 0;
00138         }
00139 
00140         d->locale = new KLocale(d->mainComponent.catalogName());
00141         QTextCodec::setCodecForLocale(d->locale->codecForEncoding());
00142         d->mainComponent.aboutData()->translateInternalProgramName();
00143     }
00144     return d->locale;
00145 }
00146 
00147 bool KGlobal::hasLocale()
00148 {
00149     if (globalData.isDestroyed()) {
00150         return false;
00151     }
00152     PRIVATE_DATA;
00153     return (d->locale != 0);
00154 }
00155 
00156 KCharsets *KGlobal::charsets()
00157 {
00158     PRIVATE_DATA;
00159     if (d->charsets == 0) {
00160         d->charsets = new KCharsets;
00161     }
00162 
00163     return d->charsets;
00164 }
00165 
00166 mode_t KGlobal::umask()
00167 {
00168     // Don't use PRIVATE_DATA here. This is called by ~KGlobalPrivate -> ~KConfig -> sync -> KSaveFile, so there's no KGlobalPrivate anymore.
00169     return s_umsk;
00170 }
00171 
00172 KComponentData KGlobal::activeComponent()
00173 {
00174     PRIVATE_DATA;
00175     MYASSERT(d->activeComponent.isValid());
00176     return d->activeComponent;
00177 }
00178 
00179 void KGlobal::setActiveComponent(const KComponentData &c)
00180 {
00181     PRIVATE_DATA;
00182     d->activeComponent = c;
00183     if (c.isValid() && d->locale) {
00184         d->locale->setActiveCatalog(c.catalogName());
00185     }
00186 }
00187 
00188 void KGlobal::newComponentData(const KComponentData &c)
00189 {
00190     PRIVATE_DATA;
00191     if (d->mainComponent.isValid()) {
00192         return;
00193     }
00194     d->mainComponent = c;
00195     KGlobal::setActiveComponent(c);
00196 }
00197 
00198 void KGlobal::setLocale(KLocale *locale, CopyCatalogs copy)
00199 {
00200     PRIVATE_DATA;
00201     if (copy == DoCopyCatalogs && d->locale)
00202         d->locale->copyCatalogsTo(locale);
00203     delete d->locale;
00204     d->locale = locale;
00205 }
00206 
00213 const QString &KGlobal::staticQString(const char *str)
00214 {
00215     return staticQString(QLatin1String(str));
00216 }
00217 
00224 const QString &KGlobal::staticQString(const QString &str)
00225 {
00226     PRIVATE_DATA;
00227     if (!d->stringDict) {
00228         d->stringDict = new KStringDict;
00229     }
00230 
00231    return *d->stringDict->insert(str);
00232 }
00233 
00234 QString KGlobal::caption()
00235 {
00236     PRIVATE_DATA;
00237     // Caption set from command line ?
00238     KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde");
00239     if (args && args->isSet("caption")) {
00240         return args->getOption("caption");
00241     } else {
00242         // We have some about data ?
00243         if (d->mainComponent.aboutData()) {
00244             return d->mainComponent.aboutData()->programName();
00245         } else {
00246             // Last resort : application name
00247             return QCoreApplication::instance()->applicationName();
00248         }
00249     }
00250 }
00251 
00260 static int s_refCount = 0;
00261 static bool s_allowQuit = false;
00262 
00263 void KGlobal::ref()
00264 {
00265     ++s_refCount;
00266     //kDebug() << "KGlobal::ref() : refCount = " << s_refCount;
00267 }
00268 
00269 void KGlobal::deref()
00270 {
00271     --s_refCount;
00272     //kDebug() << "KGlobal::deref() : refCount = " << s_refCount;
00273     if (s_refCount <= 0 && s_allowQuit) {
00274         QCoreApplication::instance()->quit();
00275     }
00276 }
00277 
00278 void KGlobal::setAllowQuit(bool allowQuit)
00279 {
00280     s_allowQuit = allowQuit;
00281 }
00282 
00283 #undef PRIVATE_DATA

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal