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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • 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.
68 Q_CONSTRUCTOR_FUNCTION(qrand)
69 
70 typedef QSet<QString> KStringDict;
71 mode_t s_umsk;
72 
73 class 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 
122 KCatalogLoader::KCatalogLoader(const QString &catalogName)
123 {
124  KGlobal::insertCatalog(catalogName);
125 }
126 
127 
128 K_GLOBAL_STATIC(KGlobalPrivate, globalData)
129 K_GLOBAL_STATIC_WITH_ARGS(KComponentData, fakeComponent, (KGlobalPrivate::initFakeComponent()))
130 
131 #define PRIVATE_DATA KGlobalPrivate *d = globalData
132 
133 KStandardDirs *KGlobal::dirs()
134 {
135  PRIVATE_DATA;
136  return d->mainComponent.isValid() ? d->mainComponent.dirs() : fakeComponent->dirs();
137 }
138 
139 KSharedConfig::Ptr KGlobal::config()
140 {
141  PRIVATE_DATA;
142  return d->mainComponent.isValid() ? d->mainComponent.config() : fakeComponent->config();
143 }
144 
145 const KComponentData &KGlobal::mainComponent()
146 {
147  PRIVATE_DATA;
148  return d->mainComponent.isValid() ? d->mainComponent : *fakeComponent;
149 }
150 
151 bool KGlobal::hasMainComponent()
152 {
153  if (globalData.isDestroyed()) {
154  return false;
155  }
156  PRIVATE_DATA;
157  return d->mainComponent.isValid();
158 }
159 
160 void 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 
170 KLocale *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 
205 bool KGlobal::hasLocale()
206 {
207  if (globalData.isDestroyed()) {
208  return false;
209  }
210  PRIVATE_DATA;
211  return (d->locale != 0);
212 }
213 
214 KCharsets *KGlobal::charsets()
215 {
216  PRIVATE_DATA;
217  if (d->charsets == 0) {
218  d->charsets = new KCharsets;
219  }
220 
221  return d->charsets;
222 }
223 
224 mode_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 
230 KComponentData KGlobal::activeComponent()
231 {
232  PRIVATE_DATA;
233  MYASSERT(d->activeComponent.isValid());
234  return d->activeComponent;
235 }
236 
237 void 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 
246 void 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 
256 void 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 
271 const QString &KGlobal::staticQString(const char *str)
272 {
273  return staticQString(QLatin1String(str));
274 }
275 
282 const 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 
292 QString 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 
318 static QBasicAtomicInt s_allowQuit = Q_BASIC_ATOMIC_INITIALIZER(false); // this is used a bool
319 static QBasicAtomicInt s_refCount = Q_BASIC_ATOMIC_INITIALIZER(0);
320 
321 void KGlobal::ref()
322 {
323  s_refCount.fetchAndAddOrdered(1);
324 }
325 
326 void 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 
334 void 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 
346 QObject* 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 }
KGlobal::caption
QString caption()
Returns a text for the window caption.
Definition: kglobal.cpp:292
KSharedPtr< KSharedConfig >
KCatalogLoader::KCatalogLoader
KCatalogLoader(const QString &catalogName)
Definition: kglobal.cpp:122
kcharsets.h
KStringDict
QSet< QString > KStringDict
Definition: kglobal.cpp:70
KCharsets
Charset font and encoder/decoder handling.
Definition: kcharsets.h:46
PRIVATE_DATA
#define PRIVATE_DATA
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
Access parsed arguments.
Definition: kcmdlineargs.cpp:611
QCoreApplication
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
kconfig.h
KCmdLineArgs
A class for command-line argument handling.
Definition: kcmdlineargs.h:281
QObject::children
const QObjectList & children() const
KGlobal::DoCopyCatalogs
Definition: kglobal.h:525
KGlobal::dirs
KStandardDirs * dirs()
Returns the application standard dirs object.
s_refCount
static QBasicAtomicInt s_refCount
Definition: kglobal.cpp:319
KGlobal::CopyCatalogs
CopyCatalogs
For setLocale.
Definition: kglobal.h:525
KGlobal::newComponentData
void newComponentData(const KComponentData &c)
Definition: kglobal.cpp:246
KComponentData::aboutData
const KAboutData * aboutData() const
Returns the about data of this component.
Definition: kcomponentdata.cpp:215
typedef
QSet::insert
const_iterator insert(const T &value)
QObject::thread
QThread * thread() const
klocale.h
KDETranslator
Definition: kglobal_p.h:40
K_GLOBAL_STATIC_WITH_ARGS
K_GLOBAL_STATIC_WITH_ARGS(KComponentData, fakeComponent,(KGlobalPrivate::initFakeComponent())) KStandardDirs *KGlobal
Definition: kglobal.cpp:129
KGlobal::ref
void ref()
Tells KGlobal about one more operations that should be finished before the application exits...
Definition: kglobal.cpp:321
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
KGlobal::config
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:139
KLocale::setActiveCatalog
void setActiveCatalog(const QString &catalog)
Sets the active catalog for translation lookup.
Definition: klocale.cpp:146
KComponentData::catalogName
QString catalogName() const
Returns the name of the translation catalog.
Definition: kcomponentdata.cpp:232
kglobal.h
QMetaObject
KGlobal::deref
void deref()
Tells KGlobal that one operation such as those described in ref() just finished.
Definition: kglobal.cpp:326
KGlobal::setLocale
void setLocale(KLocale *, CopyCatalogs copy=DoCopyCatalogs)
Definition: kglobal.cpp:256
QObject
KStandardDirs
Site-independent access to standard KDE directories.
Definition: kstandarddirs.h:171
KGlobal::umask
mode_t umask()
Returns the umask of the process.
Definition: kglobal.cpp:224
kcmdlineargs.h
QString::isEmpty
bool isEmpty() const
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::activeComponent
KComponentData activeComponent()
The component currently active (useful in a multi-component application, such as a KParts application...
Definition: kglobal.cpp:230
KGlobal::charsets
KCharsets * charsets()
The global charset manager.
Definition: kglobal.cpp:214
kglobal_p.h
QCoreApplication::instance
QCoreApplication * instance()
QSet
Definition: k3resolver.h:41
QString
QCoreApplication::quit
void quit()
QStringList
KLocale::insertCatalog
void insertCatalog(const QString &catalog)
Adds another catalog to search for translation lookup.
Definition: klocale.cpp:136
KGlobal::insertCatalog
void insertCatalog(const QString &catalog)
Inserts the catalog in the main locale object if it exists.
Definition: kglobal.cpp:160
s_allowQuit
static QBasicAtomicInt s_allowQuit
This counter indicates when to quit the application.
Definition: kglobal.cpp:318
KGlobal::setActiveComponent
void setActiveComponent(const KComponentData &d)
Set the active component for use by KAboutDialog and KBugReport.
Definition: kglobal.cpp:237
KGlobal::locale
KLocale * locale()
Returns the global locale object.
Definition: kglobal.cpp:170
QCoreApplication::installTranslator
void installTranslator(QTranslator *translationFile)
QString::toLatin1
QByteArray toLatin1() const
KLocale
KLocale provides support for country specific stuff like the national language.
Definition: klocale.h:69
KGlobal::findDirectChild_helper
QObject * findDirectChild_helper(const QObject *parent, const QMetaObject &mo)
Definition: kglobal.cpp:346
QLatin1String
QTextCodec::setCodecForLocale
void setCodecForLocale(QTextCodec *c)
QThread::currentThread
QThread * currentThread()
kstandarddirs.h
KComponentData::SkipMainComponentRegistration
Definition: kcomponentdata.h:86
KComponentData::isValid
bool isValid() const
Returns whether this is a valid object.
Definition: kcomponentdata.cpp:128
KGlobal::mainComponent
const KComponentData & mainComponent()
Returns the global component data.
Definition: kglobal.cpp:145
KGlobal::hasMainComponent
bool hasMainComponent()
Definition: kglobal.cpp:151
MYASSERT
#define MYASSERT(x)
Definition: kglobal.cpp:58
QString::fromLatin1
QString fromLatin1(const char *str, int size)
KCmdLineArgs::getOption
QString getOption(const QByteArray &option) const
Read out a string option.
Definition: kcmdlineargs.cpp:1438
KGlobal::staticQString
const QString & staticQString(const char *str)
Creates a static QString.
Definition: kglobal.cpp:271
kaboutdata.h
KLocale::copyCatalogsTo
void copyCatalogsTo(KLocale *locale)
Copies the catalogs of this object to an other KLocale object.
Definition: klocale.cpp:739
kcomponentdata.h
KGlobal::hasLocale
bool hasLocale()
Definition: kglobal.cpp:205
KComponentData
Per component data.
Definition: kcomponentdata.h:46
QCoreApplication::applicationName
QString applicationName()
s_umsk
mode_t s_umsk
Definition: kglobal.cpp:71
KAboutData::translateInternalProgramName
void translateInternalProgramName() const
Definition: kaboutdata.cpp:709
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:11 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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