KDELibs4Support

kglobal.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1999 Sirtaj Singh Kanq <[email protected]>
3  Copyright (C) 2007 Matthias Kretz <[email protected]>
4  Copyright (C) 2009 Olivier Goffart <[email protected]>
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 <QThread>
31 
32 #include <config-kdelibs4support.h>
33 
34 #if HAVE_SYS_STAT_H
35 #include <sys/stat.h>
36 #endif
37 
38 #include <QList>
39 #include <QSet>
40 
41 #include <k4aboutdata.h>
42 #include <kconfig.h>
43 #include <klocalizedstring.h>
44 #include <kcharsets.h>
45 #include "kstandarddirs.h"
46 #include <kcomponentdata.h>
47 #undef QT_NO_TRANSLATION
48 #include <QCoreApplication>
49 #define QT_NO_TRANSLATION
50 #include <QTextCodec>
51 #include <unistd.h> // umask
52 
53 // ~KConfig needs qrand(). qrand() depends on a Q_GLOBAL_STATIC. With this Q_CONSTRUCTOR_FUNCTION we
54 // try to make qrand() live longer than any KConfig object.
55 Q_CONSTRUCTOR_FUNCTION(qrand)
56 
58 mode_t s_umsk;
59 
60 class KGlobalPrivate
61 {
62 public:
63  inline KGlobalPrivate()
64  : dirs(nullptr),
65  stringDict(nullptr)
66  {
67  // the umask is read here before any threads are created to avoid race conditions
68  mode_t tmp = 0;
69  s_umsk = umask(tmp);
70  umask(s_umsk);
71  }
72 
73  inline ~KGlobalPrivate()
74  {
75  delete dirs;
76  dirs = nullptr;
77  delete stringDict;
78  stringDict = nullptr;
79  }
80 
82  KStringDict *stringDict;
83 };
84 
85 K_GLOBAL_STATIC(KGlobalPrivate, globalData)
86 
87 #define PRIVATE_DATA KGlobalPrivate *d = globalData
88 
90 {
91  PRIVATE_DATA;
92  if (!d->dirs) {
93  d->dirs = new KStandardDirs;
94  KSharedConfig::Ptr config = KSharedConfig::openConfig();
95  if (d->dirs->addCustomized(config.data())) {
96  config->reparseConfiguration();
97  }
98  }
99  return d->dirs;
100 }
101 
102 KSharedConfig::Ptr KGlobal::config()
103 {
105 }
106 
108 {
110 }
111 
113 {
115 }
116 
117 KGlobal::LocaleWrapper KGlobal::locale()
118 {
119  return KGlobal::LocaleWrapper(KLocale::global());
120 }
121 
123 {
124  return QCoreApplication::instance() != nullptr;
125 }
126 
128 {
129  return KCharsets::charsets();
130 }
131 
133 {
134  // Don't use PRIVATE_DATA here. This is called by ~KGlobalPrivate -> ~KConfig -> sync -> KSaveFile, so there's no KGlobalPrivate anymore.
135  return s_umsk;
136 }
137 
139 {
141 }
142 
144 {
146 }
147 
148 /**
149  * Create a static QString
150  *
151  * To be used inside functions(!) like:
152  * static const QString &myString = KGlobal::staticQString("myText");
153  */
154 const QString &KGlobal::staticQString(const char *str)
155 {
156  return staticQString(QLatin1String(str));
157 }
158 
159 /**
160  * Create a static QString
161  *
162  * To be used inside functions(!) like:
163  * static const QString &myString = KGlobal::staticQString(i18n("My Text"));
164  */
166 {
167  PRIVATE_DATA;
168  if (!d->stringDict) {
169  d->stringDict = new KStringDict;
170  }
171 
172  return *d->stringDict->insert(str);
173 }
174 
176 {
177 #if 0 // Should move to Qt if wanted
178  // Caption set from command line ?
179  KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde");
180  if (args && args->isSet("caption")) {
181  return args->getOption("caption");
182  } else
183 #endif
184  {
185  // We have some about data ?
187  if (cData.isValid() && cData.aboutData()) {
188  return cData.aboutData()->programName();
189  } else {
190  // Last resort : application name
192  }
193  }
194 }
195 
196 /**
197  * This counter indicates when to quit the application.
198  * It starts at 0, is incremented by KMainWindow, systray icons, running jobs, etc.
199  * and decremented again when those things are destroyed.
200  * This mechanism allows dialogs and jobs to outlive the last window closed
201  * e.g. a file copy for a file manager, or 'compacting folders on exit' for a mail client,
202  * the job progress widget with "keep open" checked, etc.
203  */
204 static int s_refCount = 0;
205 static bool s_allowQuit = false;
206 
208 {
209  ++s_refCount;
210  //kDebug() << "KGlobal::ref() : refCount = " << s_refCount;
211 }
212 
214 {
215  --s_refCount;
216  //kDebug() << "KGlobal::deref() : refCount = " << s_refCount;
217  if (s_refCount <= 0 && s_allowQuit) {
219  }
220 }
221 
222 void KGlobal::setAllowQuit(bool allowQuit)
223 {
224  s_allowQuit = allowQuit;
225 }
226 
227 #undef PRIVATE_DATA
228 
230 {
231  if (!parent) {
232  return nullptr;
233  }
234 
235  const QObjectList &children = parent->children();
236  for (int i = 0; i < children.size(); ++i) {
237  QObject *obj = children.at(i);
238  if (mo.cast(obj)) {
239  return obj;
240  }
241  }
242  return nullptr;
243 
244 }
const QString & staticQString(const char *str)
Creates a static QString.
Definition: kglobal.cpp:154
static void setActiveComponent(const KComponentData &d)
Set the active component for use by KAboutDialog and KBugReport.
static const KComponentData & activeComponent()
The component currently active (useful in a multi-component application, such as a KParts application...
QString getOption(const QByteArray &option) const
Read out a string option.
QString programName() const
Returns the translated program name.
void ref()
Tells KGlobal about one more operations that should be finished before the application exits.
Definition: kglobal.cpp:207
#define K_GLOBAL_STATIC(TYPE, NAME)
This macro makes it easy to use non-POD types as global statics.
Definition: kglobal.h:235
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
Access parsed arguments.
KComponentData activeComponent()
The component currently active (useful in a multi-component application, such as a KParts application...
Definition: kglobal.cpp:138
bool isSet(const QByteArray &option) const
Read out a boolean option or check for the presence of string option.
void deref()
Tells KGlobal that one operation such as those described in ref() just finished.
Definition: kglobal.cpp:213
bool isValid() const
Returns whether this is a valid object.
QString caption()
Returns a text for the window caption.
Definition: kglobal.cpp:175
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
const KSharedConfig::Ptr & config() const
Returns the general config object ("appnamerc").
void setAllowQuit(bool allowQuit)
If refcounting reaches 0 (or less), and allowQuit is true, the instance of the application will autom...
Definition: kglobal.cpp:222
KCharsets * charsets()
The global charset manager.
Definition: kglobal.cpp:127
void setActiveComponent(const KComponentData &d)
Set the active component for use by KAboutDialog and KBugReport.
Definition: kglobal.cpp:143
A class for command-line argument handling.
Definition: kcmdlineargs.h:286
static bool hasMainComponent()
const K4AboutData * aboutData() const
Returns the about data of this component.
QCoreApplication * instance()
bool hasMainComponent()
Definition: kglobal.cpp:112
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:102
Per component data.
LocaleWrapper locale()
Returns the global locale object.
Definition: kglobal.cpp:117
const KComponentData & mainComponent()
Returns the global component data.
Definition: kglobal.cpp:107
bool hasLocale()
Definition: kglobal.cpp:122
mode_t umask()
Returns the umask of the process.
Definition: kglobal.cpp:132
static KLocale * global()
Return the global KLocale instance.
Definition: klocale.cpp:309
static KCharsets * charsets()
static const KComponentData & mainComponent()
Returns the global component data, if one was set.
KStandardDirs * dirs()
Returns the application standard dirs object.
Definition: kglobal.cpp:89
Site-independent access to standard KDE directories.
QSet::iterator insert(const T &value)
QObject * findDirectChild_helper(const QObject *parent, const QMetaObject &mo)
Definition: kglobal.cpp:229
const QObjectList & children() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Dec 2 2023 03:59:43 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.