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

KDECore

  • sources
  • kde-4.12
  • 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 
33 #include <config.h>
34 
35 #ifdef HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
38 
39 #include <QtCore/QList>
40 #include <QtCore/QSet>
41 
42 #include <kaboutdata.h>
43 #include <kconfig.h>
44 #include <klocale.h>
45 #include <kcharsets.h>
46 #include <kstandarddirs.h>
47 #include <kcomponentdata.h>
48 #undef QT_NO_TRANSLATION
49 #include <QtCore/QCoreApplication>
50 #define QT_NO_TRANSLATION
51 #include <QtCore/QDebug>
52 #include <QtCore/QTextCodec>
53 #include "kcmdlineargs.h"
54 #include <unistd.h> // umask
55 
56 #ifndef NDEBUG
57 #define MYASSERT(x) if (!x) \
58  qFatal("Fatal error: you need to have a KComponentData object before\n" \
59  "you do anything that requires it! Examples of this are config\n" \
60  "objects, standard directories or translations.");
61 #else
62 #define MYASSERT(x) /* nope */
63 #endif
64 
65 // ~KConfig needs qrand(). qrand() depends on a Q_GLOBAL_STATIC. With this Q_CONSTRUCTOR_FUNCTION we
66 // try to make qrand() live longer than any KConfig object.
67 Q_CONSTRUCTOR_FUNCTION(qrand)
68 
69 typedef QSet<QString> KStringDict;
70 mode_t s_umsk;
71 
72 class KGlobalPrivate
73 {
74  public:
75  inline KGlobalPrivate()
76  : stringDict(0),
77  locale(0),
78  charsets(0),
79  localeIsFromFakeComponent(false)
80  {
81  // the umask is read here before any threads are created to avoid race conditions
82  mode_t tmp = 0;
83  s_umsk = umask(tmp);
84  umask(s_umsk);
85  }
86 
87  inline ~KGlobalPrivate()
88  {
89  delete locale;
90  locale = 0;
91  delete charsets;
92  charsets = 0;
93  delete stringDict;
94  stringDict = 0;
95  }
96 
97  KComponentData activeComponent;
98  KComponentData mainComponent; // holds a refcount
99  KStringDict *stringDict;
100  KLocale *locale;
101  KCharsets *charsets;
102  bool localeIsFromFakeComponent;
103  QStringList catalogsToInsert;
104 
109  static KComponentData initFakeComponent()
110  {
111  QString name = QCoreApplication::applicationName();
112  if(name.isEmpty() && QCoreApplication::instance())
113  name = qAppName();
114  if(name.isEmpty())
115  name = QString::fromLatin1("kde");
116  return KComponentData(name.toLatin1(), name.toLatin1(),
117  KComponentData::SkipMainComponentRegistration);
118  }
119 };
120 
121 KCatalogLoader::KCatalogLoader(const QString &catalogName)
122 {
123  KGlobal::insertCatalog(catalogName);
124 }
125 
126 
127 K_GLOBAL_STATIC(KGlobalPrivate, globalData)
128 K_GLOBAL_STATIC_WITH_ARGS(KComponentData, fakeComponent, (KGlobalPrivate::initFakeComponent()))
129 
130 #define PRIVATE_DATA KGlobalPrivate *d = globalData
131 
132 KStandardDirs *KGlobal::dirs()
133 {
134  PRIVATE_DATA;
135  return d->mainComponent.isValid() ? d->mainComponent.dirs() : fakeComponent->dirs();
136 }
137 
138 KSharedConfig::Ptr KGlobal::config()
139 {
140  PRIVATE_DATA;
141  return d->mainComponent.isValid() ? d->mainComponent.config() : fakeComponent->config();
142 }
143 
144 const KComponentData &KGlobal::mainComponent()
145 {
146  PRIVATE_DATA;
147  return d->mainComponent.isValid() ? d->mainComponent : *fakeComponent;
148 }
149 
150 bool KGlobal::hasMainComponent()
151 {
152  if (globalData.isDestroyed()) {
153  return false;
154  }
155  PRIVATE_DATA;
156  return d->mainComponent.isValid();
157 }
158 
159 void KGlobal::insertCatalog(const QString& catalog)
160 {
161  PRIVATE_DATA;
162  if (d->locale) {
163  locale()->insertCatalog(catalog);
164  } else {
165  d->catalogsToInsert.append(catalog);
166  }
167 }
168 
169 KLocale *KGlobal::locale()
170 {
171  PRIVATE_DATA;
172  if (d->locale == 0 || (d->localeIsFromFakeComponent && d->mainComponent.isValid() && d->mainComponent.config())) {
173  // If you hit the warning below, here's how to debug it in gdb:
174  // (gdb) set auto-solib-add on
175  // (gdb) b i18n
176  // (gdb) b KLocale::KLocale
177  // Function "KLocale::KLocale" not defined.
178  // Make breakpoint pending on future shared library load? (y or [n]) y
179  // (gdb) run
180  // And now it will stop at the first i18n call or more generally at the first construction of the KLocale object,
181  // type bt or go up to find the guilty i18n call.
182  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");
183  delete d->locale;
184  d->locale = 0;
185  d->locale = new KLocale(mainComponent().catalogName());
186  d->localeIsFromFakeComponent = !d->mainComponent.isValid();
187  QTextCodec::setCodecForLocale(d->locale->codecForEncoding());
188  mainComponent().aboutData()->translateInternalProgramName();
189  QCoreApplication* coreApp = QCoreApplication::instance();
190  if (coreApp) { // testcase: kwrite --help: no qcore app
191  if (coreApp->thread() != QThread::currentThread()) {
192  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.");
193  } else {
194  QCoreApplication::installTranslator(new KDETranslator(coreApp));
195  }
196  }
197  foreach(const QString &catalog, d->catalogsToInsert)
198  d->locale->insertCatalog(catalog);
199  d->catalogsToInsert.clear();
200  }
201  return d->locale;
202 }
203 
204 bool KGlobal::hasLocale()
205 {
206  if (globalData.isDestroyed()) {
207  return false;
208  }
209  PRIVATE_DATA;
210  return (d->locale != 0);
211 }
212 
213 KCharsets *KGlobal::charsets()
214 {
215  PRIVATE_DATA;
216  if (d->charsets == 0) {
217  d->charsets = new KCharsets;
218  }
219 
220  return d->charsets;
221 }
222 
223 mode_t KGlobal::umask()
224 {
225  // Don't use PRIVATE_DATA here. This is called by ~KGlobalPrivate -> ~KConfig -> sync -> KSaveFile, so there's no KGlobalPrivate anymore.
226  return s_umsk;
227 }
228 
229 KComponentData KGlobal::activeComponent()
230 {
231  PRIVATE_DATA;
232  MYASSERT(d->activeComponent.isValid());
233  return d->activeComponent;
234 }
235 
236 void KGlobal::setActiveComponent(const KComponentData &c)
237 {
238  PRIVATE_DATA;
239  d->activeComponent = c;
240  if (c.isValid() && d->locale) {
241  locale()->setActiveCatalog(c.catalogName());
242  }
243 }
244 
245 void KGlobal::newComponentData(const KComponentData &c)
246 {
247  PRIVATE_DATA;
248  if (d->mainComponent.isValid()) {
249  return;
250  }
251  d->mainComponent = c;
252  KGlobal::setActiveComponent(c);
253 }
254 
255 void KGlobal::setLocale(KLocale *newLocale, CopyCatalogs copy)
256 {
257  PRIVATE_DATA;
258  if (copy == DoCopyCatalogs && d->locale)
259  locale()->copyCatalogsTo(newLocale);
260  delete d->locale;
261  d->locale = newLocale;
262 }
263 
270 const QString &KGlobal::staticQString(const char *str)
271 {
272  return staticQString(QLatin1String(str));
273 }
274 
281 const QString &KGlobal::staticQString(const QString &str)
282 {
283  PRIVATE_DATA;
284  if (!d->stringDict) {
285  d->stringDict = new KStringDict;
286  }
287 
288  return *d->stringDict->insert(str);
289 }
290 
291 QString KGlobal::caption()
292 {
293  PRIVATE_DATA;
294  // Caption set from command line ?
295  KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde");
296  if (args && args->isSet("caption")) {
297  return args->getOption("caption");
298  } else {
299  // We have some about data ?
300  if (d->mainComponent.isValid() && d->mainComponent.aboutData()) {
301  return d->mainComponent.aboutData()->programName();
302  } else {
303  // Last resort : application name
304  return QCoreApplication::instance()->applicationName();
305  }
306  }
307 }
308 
317 static int s_refCount = 0;
318 static bool s_allowQuit = false;
319 
320 void KGlobal::ref()
321 {
322  ++s_refCount;
323  //kDebug() << "KGlobal::ref() : refCount = " << s_refCount;
324 }
325 
326 void KGlobal::deref()
327 {
328  --s_refCount;
329  //kDebug() << "KGlobal::deref() : refCount = " << s_refCount;
330  if (s_refCount <= 0 && s_allowQuit) {
331  QCoreApplication::instance()->quit();
332  }
333 }
334 
335 void KGlobal::setAllowQuit(bool allowQuit)
336 {
337  s_allowQuit = allowQuit;
338 }
339 
340 #undef PRIVATE_DATA
341 
342 QObject* KGlobal::findDirectChild_helper(const QObject* parent, const QMetaObject& mo)
343 {
344  if (!parent)
345  return 0;
346 
347  const QObjectList &children = parent->children();
348  for (int i = 0; i < children.size(); ++i) {
349  QObject* obj = children.at(i);
350  if (mo.cast(obj)) {
351  return obj;
352  }
353  }
354  return 0;
355 
356 }
KGlobal::caption
QString caption()
Returns a text for the window caption.
Definition: kglobal.cpp:291
KSharedPtr< KSharedConfig >
KCatalogLoader::KCatalogLoader
KCatalogLoader(const QString &catalogName)
Definition: kglobal.cpp:121
kcharsets.h
s_allowQuit
static bool s_allowQuit
Definition: kglobal.cpp:318
KStringDict
QSet< QString > KStringDict
Definition: kglobal.cpp:69
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
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
KGlobal::DoCopyCatalogs
Definition: kglobal.h:525
KGlobal::dirs
KStandardDirs * dirs()
Returns the application standard dirs object.
KGlobal::CopyCatalogs
CopyCatalogs
For setLocale.
Definition: kglobal.h:525
KGlobal::newComponentData
void newComponentData(const KComponentData &c)
Definition: kglobal.cpp:245
KComponentData::aboutData
const KAboutData * aboutData() const
Returns the about data of this component.
Definition: kcomponentdata.cpp:215
QString
QObject
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:128
KGlobal::ref
void ref()
Tells KGlobal about one more operations that should be finished before the application exits...
Definition: kglobal.cpp:320
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:138
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
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:255
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:223
kcmdlineargs.h
QStringList
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:335
KGlobal::activeComponent
KComponentData activeComponent()
The component currently active (useful in a multi-component application, such as a KParts application...
Definition: kglobal.cpp:229
KGlobal::charsets
KCharsets * charsets()
The global charset manager.
Definition: kglobal.cpp:213
kglobal_p.h
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:159
QSet
Definition: k3resolver.h:41
KGlobal::setActiveComponent
void setActiveComponent(const KComponentData &d)
Set the active component for use by KAboutDialog and KBugReport.
Definition: kglobal.cpp:236
KGlobal::locale
KLocale * locale()
Returns the global locale object.
Definition: kglobal.cpp:169
KLocale
KLocale provides support for country specific stuff like the national language.
Definition: klocale.h:69
s_refCount
static int s_refCount
This counter indicates when to quit the application.
Definition: kglobal.cpp:317
KGlobal::findDirectChild_helper
QObject * findDirectChild_helper(const QObject *parent, const QMetaObject &mo)
Definition: kglobal.cpp:342
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:144
KGlobal::hasMainComponent
bool hasMainComponent()
Definition: kglobal.cpp:150
MYASSERT
#define MYASSERT(x)
Definition: kglobal.cpp:57
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:270
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:204
KComponentData
Per component data.
Definition: kcomponentdata.h:46
s_umsk
mode_t s_umsk
Definition: kglobal.cpp:70
KAboutData::translateInternalProgramName
void translateInternalProgramName() const
Definition: kaboutdata.cpp:709
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:08 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • 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