• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • kernel
kglobal.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 1999 Sirtaj Singh Kanq <taj@kde.org>
3 Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
4 Copyright (C) 2009 Olivier Goffart <ogoffart@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/*
22 * kglobal.cpp -- Implementation of namespace KGlobal.
23 * Author: Sirtaj Singh Kang
24 * Generated: Sat May 1 02:08:43 EST 1999
25 */
26
27#undef KDE3_SUPPORT
28
29#include "kglobal.h"
30#include "kglobal_p.h"
31#include <QThread>
32#include <QAtomicInt>
33
34#include <config.h>
35
36#ifdef HAVE_SYS_STAT_H
37#include <sys/stat.h>
38#endif
39
40#include <QtCore/QList>
41#include <QtCore/QSet>
42
43#include <kaboutdata.h>
44#include <kconfig.h>
45#include <klocale.h>
46#include <kcharsets.h>
47#include <kstandarddirs.h>
48#include <kcomponentdata.h>
49#undef QT_NO_TRANSLATION
50#include <QtCore/QCoreApplication>
51#define QT_NO_TRANSLATION
52#include <QtCore/QDebug>
53#include <QtCore/QTextCodec>
54#include "kcmdlineargs.h"
55#include <unistd.h> // umask
56
57#ifndef NDEBUG
58#define MYASSERT(x) if (!x) \
59 qFatal("Fatal error: you need to have a KComponentData object before\n" \
60 "you do anything that requires it! Examples of this are config\n" \
61 "objects, standard directories or translations.");
62#else
63#define MYASSERT(x) /* nope */
64#endif
65
66// ~KConfig needs qrand(). qrand() depends on a Q_GLOBAL_STATIC. With this Q_CONSTRUCTOR_FUNCTION we
67// try to make qrand() live longer than any KConfig object.
68Q_CONSTRUCTOR_FUNCTION(qrand)
69
70typedef QSet<QString> KStringDict;
71mode_t s_umsk;
72
73class KGlobalPrivate
74{
75 public:
76 inline KGlobalPrivate()
77 : stringDict(0),
78 locale(0),
79 charsets(0),
80 localeIsFromFakeComponent(false)
81 {
82 // the umask is read here before any threads are created to avoid race conditions
83 mode_t tmp = 0;
84 s_umsk = umask(tmp);
85 umask(s_umsk);
86 }
87
88 inline ~KGlobalPrivate()
89 {
90 delete locale;
91 locale = 0;
92 delete charsets;
93 charsets = 0;
94 delete stringDict;
95 stringDict = 0;
96 }
97
98 KComponentData activeComponent;
99 KComponentData mainComponent; // holds a refcount
100 KStringDict *stringDict;
101 KLocale *locale;
102 KCharsets *charsets;
103 bool localeIsFromFakeComponent;
104 QStringList catalogsToInsert;
105
110 static KComponentData initFakeComponent()
111 {
112 QString name = QCoreApplication::applicationName();
113 if(name.isEmpty() && QCoreApplication::instance())
114 name = qAppName();
115 if(name.isEmpty())
116 name = QString::fromLatin1("kde");
117 return KComponentData(name.toLatin1(), name.toLatin1(),
118 KComponentData::SkipMainComponentRegistration);
119 }
120};
121
122KCatalogLoader::KCatalogLoader(const QString &catalogName)
123{
124 KGlobal::insertCatalog(catalogName);
125}
126
127
128K_GLOBAL_STATIC(KGlobalPrivate, globalData)
129K_GLOBAL_STATIC_WITH_ARGS(KComponentData, fakeComponent, (KGlobalPrivate::initFakeComponent()))
130
131#define PRIVATE_DATA KGlobalPrivate *d = globalData
132
133KStandardDirs *KGlobal::dirs()
134{
135 PRIVATE_DATA;
136 return d->mainComponent.isValid() ? d->mainComponent.dirs() : fakeComponent->dirs();
137}
138
139KSharedConfig::Ptr KGlobal::config()
140{
141 PRIVATE_DATA;
142 return d->mainComponent.isValid() ? d->mainComponent.config() : fakeComponent->config();
143}
144
145const KComponentData &KGlobal::mainComponent()
146{
147 PRIVATE_DATA;
148 return d->mainComponent.isValid() ? d->mainComponent : *fakeComponent;
149}
150
151bool KGlobal::hasMainComponent()
152{
153 if (globalData.isDestroyed()) {
154 return false;
155 }
156 PRIVATE_DATA;
157 return d->mainComponent.isValid();
158}
159
160void KGlobal::insertCatalog(const QString& catalog)
161{
162 PRIVATE_DATA;
163 if (d->locale) {
164 locale()->insertCatalog(catalog);
165 } else {
166 d->catalogsToInsert.append(catalog);
167 }
168}
169
170KLocale *KGlobal::locale()
171{
172 PRIVATE_DATA;
173 if (d->locale == 0 || (d->localeIsFromFakeComponent && d->mainComponent.isValid() && d->mainComponent.config())) {
174 // If you hit the warning below, here's how to debug it in gdb:
175 // (gdb) set auto-solib-add on
176 // (gdb) b i18n
177 // (gdb) b KLocale::KLocale
178 // Function "KLocale::KLocale" not defined.
179 // Make breakpoint pending on future shared library load? (y or [n]) y
180 // (gdb) run
181 // And now it will stop at the first i18n call or more generally at the first construction of the KLocale object,
182 // type bt or go up to find the guilty i18n call.
183 if (d->locale != 0) qWarning("KGlobal::locale(): Warning your global KLocale is being recreated with a valid main component instead of a fake component, this usually means you tried to call i18n related functions before your main component was created. You should not do that since it most likely will not work");
184 delete d->locale;
185 d->locale = 0;
186 d->locale = new KLocale(mainComponent().catalogName());
187 d->localeIsFromFakeComponent = !d->mainComponent.isValid();
188 QTextCodec::setCodecForLocale(d->locale->codecForEncoding());
189 mainComponent().aboutData()->translateInternalProgramName();
190 QCoreApplication* coreApp = QCoreApplication::instance();
191 if (coreApp) { // testcase: kwrite --help: no qcore app
192 if (coreApp->thread() != QThread::currentThread()) {
193 qFatal("KGlobal::locale() must be called from the main thread before using i18n() in threads. KApplication takes care of this. If not using KApplication, call KGlobal::locale() during initialization.");
194 } else {
195 QCoreApplication::installTranslator(new KDETranslator(coreApp));
196 }
197 }
198 foreach(const QString &catalog, d->catalogsToInsert)
199 d->locale->insertCatalog(catalog);
200 d->catalogsToInsert.clear();
201 }
202 return d->locale;
203}
204
205bool KGlobal::hasLocale()
206{
207 if (globalData.isDestroyed()) {
208 return false;
209 }
210 PRIVATE_DATA;
211 return (d->locale != 0);
212}
213
214KCharsets *KGlobal::charsets()
215{
216 PRIVATE_DATA;
217 if (d->charsets == 0) {
218 d->charsets = new KCharsets;
219 }
220
221 return d->charsets;
222}
223
224mode_t KGlobal::umask()
225{
226 // Don't use PRIVATE_DATA here. This is called by ~KGlobalPrivate -> ~KConfig -> sync -> KSaveFile, so there's no KGlobalPrivate anymore.
227 return s_umsk;
228}
229
230KComponentData KGlobal::activeComponent()
231{
232 PRIVATE_DATA;
233 MYASSERT(d->activeComponent.isValid());
234 return d->activeComponent;
235}
236
237void KGlobal::setActiveComponent(const KComponentData &c)
238{
239 PRIVATE_DATA;
240 d->activeComponent = c;
241 if (c.isValid() && d->locale) {
242 locale()->setActiveCatalog(c.catalogName());
243 }
244}
245
246void KGlobal::newComponentData(const KComponentData &c)
247{
248 PRIVATE_DATA;
249 if (d->mainComponent.isValid()) {
250 return;
251 }
252 d->mainComponent = c;
253 KGlobal::setActiveComponent(c);
254}
255
256void KGlobal::setLocale(KLocale *newLocale, CopyCatalogs copy)
257{
258 PRIVATE_DATA;
259 if (copy == DoCopyCatalogs && d->locale)
260 locale()->copyCatalogsTo(newLocale);
261 delete d->locale;
262 d->locale = newLocale;
263}
264
271const QString &KGlobal::staticQString(const char *str)
272{
273 return staticQString(QLatin1String(str));
274}
275
282const QString &KGlobal::staticQString(const QString &str)
283{
284 PRIVATE_DATA;
285 if (!d->stringDict) {
286 d->stringDict = new KStringDict;
287 }
288
289 return *d->stringDict->insert(str);
290}
291
292QString KGlobal::caption()
293{
294 PRIVATE_DATA;
295 // Caption set from command line ?
296 KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde");
297 if (args && args->isSet("caption")) {
298 return args->getOption("caption");
299 } else {
300 // We have some about data ?
301 if (d->mainComponent.isValid() && d->mainComponent.aboutData()) {
302 return d->mainComponent.aboutData()->programName();
303 } else {
304 // Last resort : application name
305 return QCoreApplication::instance()->applicationName();
306 }
307 }
308}
309
318static QBasicAtomicInt s_allowQuit = Q_BASIC_ATOMIC_INITIALIZER(false); // this is used a bool
319static QBasicAtomicInt s_refCount = Q_BASIC_ATOMIC_INITIALIZER(0);
320
321void KGlobal::ref()
322{
323 s_refCount.fetchAndAddOrdered(1);
324}
325
326void KGlobal::deref()
327{
328 const int prevRefCount = s_refCount.fetchAndAddOrdered(-1);
329 if (prevRefCount <= 1 && int(s_allowQuit)) {
330 QCoreApplication::instance()->quit();
331 }
332}
333
334void KGlobal::setAllowQuit(bool allowQuit)
335{
336 if (QThread::currentThread() == qApp->thread()) {
337 s_allowQuit.fetchAndStoreOrdered(int(allowQuit));
338 }
339 else {
340 qWarning() << "KGlobal::setAllowQuit may only be called from the main thread";
341 }
342}
343
344#undef PRIVATE_DATA
345
346QObject* KGlobal::findDirectChild_helper(const QObject* parent, const QMetaObject& mo)
347{
348 if (!parent)
349 return 0;
350
351 const QObjectList &children = parent->children();
352 for (int i = 0; i < children.size(); ++i) {
353 QObject* obj = children.at(i);
354 if (mo.cast(obj)) {
355 return obj;
356 }
357 }
358 return 0;
359
360}
KAboutData::translateInternalProgramName
void translateInternalProgramName() const
Definition: kaboutdata.cpp:709
KCatalogLoader::KCatalogLoader
KCatalogLoader(const QString &catalogName)
Definition: kglobal.cpp:122
KCharsets
Charset font and encoder/decoder handling.
Definition: kcharsets.h:47
KCmdLineArgs
A class for command-line argument handling.
Definition: kcmdlineargs.h:282
KCmdLineArgs::isSet
bool isSet(const QByteArray &option) const
Read out a boolean option or check for the presence of string option.
Definition: kcmdlineargs.cpp:1496
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
Access parsed arguments.
Definition: kcmdlineargs.cpp:611
KCmdLineArgs::getOption
QString getOption(const QByteArray &option) const
Read out a string option.
Definition: kcmdlineargs.cpp:1438
KComponentData
Per component data.
Definition: kcomponentdata.h:47
KComponentData::isValid
bool isValid() const
Returns whether this is a valid object.
Definition: kcomponentdata.cpp:128
KComponentData::SkipMainComponentRegistration
@ SkipMainComponentRegistration
Definition: kcomponentdata.h:86
KComponentData::aboutData
const KAboutData * aboutData() const
Returns the about data of this component.
Definition: kcomponentdata.cpp:215
KComponentData::catalogName
QString catalogName() const
Returns the name of the translation catalog.
Definition: kcomponentdata.cpp:232
KDETranslator
Definition: kglobal_p.h:41
KLocale
KLocale provides support for country specific stuff like the national language.
Definition: klocale.h:70
KLocale::insertCatalog
void insertCatalog(const QString &catalog)
Adds another catalog to search for translation lookup.
Definition: klocale.cpp:136
KLocale::setActiveCatalog
void setActiveCatalog(const QString &catalog)
Sets the active catalog for translation lookup.
Definition: klocale.cpp:146
KLocale::copyCatalogsTo
void copyCatalogsTo(KLocale *locale)
Copies the catalogs of this object to an other KLocale object.
Definition: klocale.cpp:739
KSharedPtr< KSharedConfig >
KStandardDirs
Site-independent access to standard KDE directories.
Definition: kstandarddirs.h:172
QObject
QSet
Definition: k3resolver.h:41
QStringList
QString
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
This macro makes it easy to use non-POD types as global statics.
Definition: kglobal.h:221
K_GLOBAL_STATIC_WITH_ARGS
#define K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: kglobal.h:255
kaboutdata.h
kcharsets.h
kcmdlineargs.h
kcomponentdata.h
kconfig.h
KStringDict
QSet< QString > KStringDict
Definition: kglobal.cpp:70
PRIVATE_DATA
#define PRIVATE_DATA
s_allowQuit
static QBasicAtomicInt s_allowQuit
This counter indicates when to quit the application.
Definition: kglobal.cpp:318
s_refCount
static QBasicAtomicInt s_refCount
Definition: kglobal.cpp:319
MYASSERT
#define MYASSERT(x)
Definition: kglobal.cpp:58
s_umsk
mode_t s_umsk
Definition: kglobal.cpp:71
kglobal.h
kglobal_p.h
klocale.h
kstandarddirs.h
KGlobal::CopyCatalogs
CopyCatalogs
For setLocale.
Definition: kglobal.h:525
KGlobal::DoCopyCatalogs
@ DoCopyCatalogs
Definition: kglobal.h:525
KGlobal::hasMainComponent
bool hasMainComponent()
Definition: kglobal.cpp:151
KGlobal::setAllowQuit
void setAllowQuit(bool allowQuit)
If refcounting reaches 0 (or less), and allowQuit is true, the instance of the application will autom...
Definition: kglobal.cpp:334
KGlobal::staticQString
const QString & staticQString(const char *str)
Creates a static QString.
Definition: kglobal.cpp:271
KGlobal::insertCatalog
void insertCatalog(const QString &catalog)
Inserts the catalog in the main locale object if it exists.
Definition: kglobal.cpp:160
KGlobal::mainComponent
const KComponentData & mainComponent()
Returns the global component data.
Definition: kglobal.cpp:145
KGlobal::dirs
KStandardDirs * dirs()
Returns the application standard dirs object.
KGlobal::findDirectChild_helper
QObject * findDirectChild_helper(const QObject *parent, const QMetaObject &mo)
Definition: kglobal.cpp:346
KGlobal::deref
void deref()
Tells KGlobal that one operation such as those described in ref() just finished.
Definition: kglobal.cpp:326
KGlobal::newComponentData
void newComponentData(const KComponentData &c)
Definition: kglobal.cpp:246
KGlobal::charsets
KCharsets * charsets()
The global charset manager.
Definition: kglobal.cpp:214
KGlobal::locale
KLocale * locale()
Returns the global locale object.
Definition: kglobal.cpp:170
KGlobal::hasLocale
bool hasLocale()
Definition: kglobal.cpp:205
KGlobal::setActiveComponent
void setActiveComponent(const KComponentData &d)
Set the active component for use by KAboutDialog and KBugReport.
Definition: kglobal.cpp:237
KGlobal::umask
mode_t umask()
Returns the umask of the process.
Definition: kglobal.cpp:224
KGlobal::ref
void ref()
Tells KGlobal about one more operations that should be finished before the application exits.
Definition: kglobal.cpp:321
KGlobal::setLocale
void setLocale(KLocale *, CopyCatalogs copy=DoCopyCatalogs)
Definition: kglobal.cpp:256
KGlobal::caption
QString caption()
Returns a text for the window caption.
Definition: kglobal.cpp:292
KGlobal::config
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:139
KGlobal::activeComponent
KComponentData activeComponent()
The component currently active (useful in a multi-component application, such as a KParts application...
Definition: kglobal.cpp:230
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal