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 <klocale.h>
44 #include <klocalizedstring.h>
45 #include <kcharsets.h>
46 #include "kstandarddirs.h"
47 #include <kcomponentdata.h>
48 #undef QT_NO_TRANSLATION
49 #include <QCoreApplication>
50 #define QT_NO_TRANSLATION
51 #include <QDebug>
52 #include <QTextCodec>
53 #include <unistd.h> // umask
54 
55 // ~KConfig needs qrand(). qrand() depends on a Q_GLOBAL_STATIC. With this Q_CONSTRUCTOR_FUNCTION we
56 // try to make qrand() live longer than any KConfig object.
57 Q_CONSTRUCTOR_FUNCTION(qrand)
58 
59 typedef QSet<QString> KStringDict;
60 mode_t s_umsk;
61 
62 class KGlobalPrivate
63 {
64 public:
65  inline KGlobalPrivate()
66  : dirs(nullptr),
67  stringDict(nullptr)
68  {
69  // the umask is read here before any threads are created to avoid race conditions
70  mode_t tmp = 0;
71  s_umsk = umask(tmp);
72  umask(s_umsk);
73  }
74 
75  inline ~KGlobalPrivate()
76  {
77  delete dirs;
78  dirs = nullptr;
79  delete stringDict;
80  stringDict = nullptr;
81  }
82 
84  KStringDict *stringDict;
85 };
86 
87 K_GLOBAL_STATIC(KGlobalPrivate, globalData)
88 
89 #define PRIVATE_DATA KGlobalPrivate *d = globalData
90 
92 {
93  PRIVATE_DATA;
94  if (!d->dirs) {
95  d->dirs = new KStandardDirs;
96  KSharedConfig::Ptr config = KSharedConfig::openConfig();
97  if (d->dirs->addCustomized(config.data())) {
98  config->reparseConfiguration();
99  }
100  }
101  return d->dirs;
102 }
103 
104 KSharedConfig::Ptr KGlobal::config()
105 {
107 }
108 
110 {
112 }
113 
115 {
117 }
118 
119 KGlobal::LocaleWrapper KGlobal::locale()
120 {
121  return KGlobal::LocaleWrapper(KLocale::global());
122 }
123 
125 {
126  return QCoreApplication::instance() != nullptr;
127 }
128 
130 {
131  return KCharsets::charsets();
132 }
133 
135 {
136  // Don't use PRIVATE_DATA here. This is called by ~KGlobalPrivate -> ~KConfig -> sync -> KSaveFile, so there's no KGlobalPrivate anymore.
137  return s_umsk;
138 }
139 
141 {
143 }
144 
146 {
148 }
149 
156 const QString &KGlobal::staticQString(const char *str)
157 {
158  return staticQString(QLatin1String(str));
159 }
160 
168 {
169  PRIVATE_DATA;
170  if (!d->stringDict) {
171  d->stringDict = new KStringDict;
172  }
173 
174  return *d->stringDict->insert(str);
175 }
176 
178 {
179 #if 0 // Should move to Qt if wanted
180  // Caption set from command line ?
181  KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde");
182  if (args && args->isSet("caption")) {
183  return args->getOption("caption");
184  } else
185 #endif
186  {
187  // We have some about data ?
189  if (cData.isValid() && cData.aboutData()) {
190  return cData.aboutData()->programName();
191  } else {
192  // Last resort : application name
194  }
195  }
196 }
197 
206 static int s_refCount = 0;
207 static bool s_allowQuit = false;
208 
210 {
211  ++s_refCount;
212  //kDebug() << "KGlobal::ref() : refCount = " << s_refCount;
213 }
214 
216 {
217  --s_refCount;
218  //kDebug() << "KGlobal::deref() : refCount = " << s_refCount;
219  if (s_refCount <= 0 && s_allowQuit) {
221  }
222 }
223 
224 void KGlobal::setAllowQuit(bool allowQuit)
225 {
226  s_allowQuit = allowQuit;
227 }
228 
229 #undef PRIVATE_DATA
230 
232 {
233  if (!parent) {
234  return nullptr;
235  }
236 
237  const QObjectList &children = parent->children();
238  for (int i = 0; i < children.size(); ++i) {
239  QObject *obj = children.at(i);
240  if (mo.cast(obj)) {
241  return obj;
242  }
243  }
244  return nullptr;
245 
246 }
QString caption()
Returns a text for the window caption.
Definition: kglobal.cpp:177
const K4AboutData * aboutData() const
Returns the about data of this component.
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
Access parsed arguments.
A class for command-line argument handling.
Definition: kcmdlineargs.h:286
const QObjectList & children() const const
QSet::iterator insert(const T &value)
void ref()
Tells KGlobal about one more operations that should be finished before the application exits...
Definition: kglobal.cpp:209
bool isSet(const QByteArray &option) const
Read out a boolean option or check for the presence of string option.
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:104
const KSharedConfig::Ptr & config() const
Returns the general config object ("appnamerc").
void deref()
Tells KGlobal that one operation such as those described in ref() just finished.
Definition: kglobal.cpp:215
Site-independent access to standard KDE directories.
mode_t umask()
Returns the umask of the process.
Definition: kglobal.cpp:134
void setAllowQuit(bool allowQuit)
If refcounting reaches 0 (or less), and allowQuit is true, the instance of the application will autom...
Definition: kglobal.cpp:224
KComponentData activeComponent()
The component currently active (useful in a multi-component application, such as a KParts application...
Definition: kglobal.cpp:140
KCharsets * charsets()
The global charset manager.
Definition: kglobal.cpp:129
static bool hasMainComponent()
QString programName() const
Returns the translated program name.
QCoreApplication * instance()
static KLocale * global()
Return the global KLocale instance.
Definition: klocale.cpp:309
static KCharsets * charsets()
void setActiveComponent(const KComponentData &d)
Set the active component for use by KAboutDialog and KBugReport.
Definition: kglobal.cpp:145
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QObject * findDirectChild_helper(const QObject *parent, const QMetaObject &mo)
Definition: kglobal.cpp:231
bool isValid() const
Returns whether this is a valid object.
const KComponentData & mainComponent()
Returns the global component data.
Definition: kglobal.cpp:109
bool hasMainComponent()
Definition: kglobal.cpp:114
QString getOption(const QByteArray &option) const
Read out a string option.
const QString & staticQString(const char *str)
Creates a static QString.
Definition: kglobal.cpp:156
static const KComponentData & mainComponent()
Returns the global component data, if one was set.
LocaleWrapper locale()
Returns the global locale object.
Definition: kglobal.cpp:119
static void setActiveComponent(const KComponentData &d)
Set the active component for use by KAboutDialog and KBugReport.
bool hasLocale()
Definition: kglobal.cpp:124
Per component data.
KStandardDirs * dirs()
Returns the application standard dirs object.
Definition: kglobal.cpp:91
static const KComponentData & activeComponent()
The component currently active (useful in a multi-component application, such as a KParts application...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 12 2020 22:58:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.