KDELibs4Support

kcomponentdata.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1999 Torben Weis <[email protected]>
3  Copyright (C) 2007 Matthias Kretz <[email protected]>
4  Copyright (C) 2007 Bernhard Loos <[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 #include "kcomponentdata.h"
22 #include "kcomponentdata_p.h"
23 
24 #include <QCoreApplication>
25 
26 #include "kaboutdata.h"
27 #include "k4aboutdata.h"
28 #include "kconfig.h"
29 #include "kconfiggroup.h"
30 #include <QtDebug>
31 
32 #ifndef NDEBUG
33 #define MYASSERT(x) if (!x) \
34  qFatal("Fatal error: you need to have a KComponentData object before\n" \
35  "you do anything that requires it! Examples of this are config\n" \
36  "objects, standard directories or translations.");
37 #else
38 #define MYASSERT(x) /* nope */
39 #endif
40 
42  : d(nullptr)
43 {
44 }
45 
47  : d(rhs.d)
48 {
49  if (d) {
50  d->ref();
51  }
52 }
53 
55 {
56  if (rhs.d != d) {
57  if (rhs.d) {
58  rhs.d->ref();
59  }
60  if (d) {
61  d->deref();
62  }
63  d = rhs.d;
64  }
65  return *this;
66 }
67 
69 {
70  return d == rhs.d;
71 }
72 
74 {
75  return !operator==(rhs);
76 }
77 
78 enum KdeLibraryPathsAdded {
79  NeedLazyInit,
80  LazyInitDone,
81  KdeLibraryPathsAddedDone
82 };
83 static KdeLibraryPathsAdded kdeLibraryPathsAdded = NeedLazyInit;
84 
85 class KComponentDataStatic
86 {
87 public:
88  KComponentData mainComponent; // holds a refcount
90 
91  void newComponentData(const KComponentData &c)
92  {
93  if (mainComponent.isValid()) {
94  return;
95  }
96  mainComponent = c;
99 #if 0 // no longer available in KF5
100  KLocale::setMainCatalog(c.catalogName());
101 #endif
103  }
104 };
105 
110 static KComponentData initFakeComponent()
111 {
113  if (name.isEmpty() && QCoreApplication::instance()) {
114  name = qAppName();
115  }
116  if (name.isEmpty()) {
117  name = QString::fromLatin1("kde");
118  }
119  return KComponentData(name.toLatin1(), name.toLatin1(),
120  KComponentData::SkipMainComponentRegistration);
121 }
122 
123 Q_GLOBAL_STATIC(KComponentDataStatic, globalStatic)
124 Q_GLOBAL_STATIC_WITH_ARGS(KComponentData, fakeComponent, (initFakeComponent()))
125 
126 KComponentData::KComponentData(const QByteArray &name, const QByteArray &catalog, MainComponentRegistration registerAsMain)
127  : d(new KComponentDataPrivate(K4AboutData(name, catalog, KLocalizedString(), QByteArray())))
128 {
129  Q_ASSERT(!name.isEmpty());
130 
131  if (kdeLibraryPathsAdded == NeedLazyInit) {
132  kdeLibraryPathsAdded = LazyInitDone;
133  d->lazyInit();
134  }
135 
136  if (registerAsMain == RegisterAsMainComponent) {
137  globalStatic()->newComponentData(*this);
138  }
139 }
140 
141 KComponentData::KComponentData(const K4AboutData *aboutData, MainComponentRegistration registerAsMain)
142  : d(new KComponentDataPrivate(*aboutData))
143 {
144  Q_ASSERT(!aboutData->appName().isEmpty());
145 
146  if (kdeLibraryPathsAdded == NeedLazyInit) {
147  kdeLibraryPathsAdded = LazyInitDone;
148  d->lazyInit();
149  }
150 
151  if (registerAsMain == RegisterAsMainComponent) {
152  globalStatic()->newComponentData(*this);
153  }
154 }
155 
156 KComponentData::KComponentData(const K4AboutData &aboutData, MainComponentRegistration registerAsMain)
157  : d(new KComponentDataPrivate(aboutData))
158 {
159  Q_ASSERT(!aboutData.appName().isEmpty());
160 
161  if (kdeLibraryPathsAdded == NeedLazyInit) {
162  kdeLibraryPathsAdded = LazyInitDone;
163  d->lazyInit();
164  }
165 
166  if (registerAsMain == RegisterAsMainComponent) {
167  globalStatic()->newComponentData(*this);
168  }
169 }
170 
172 {
173  if (d) {
174  d->deref();
175  d = nullptr;
176  }
177 }
178 
180 {
181  return (d != nullptr);
182 }
183 
184 void KComponentDataPrivate::lazyInit()
185 {
186  if (!sharedConfig) {
187  configInit();
188  }
189 
190 #if 0 // obsolete in KF5
191 #ifdef Q_OS_WIN
192  if (QCoreApplication::instance() && kdeLibraryPathsAdded != KdeLibraryPathsAddedDone) {
193 #else
194  // the first KComponentData sets the KDE Qt plugin paths
195  if (kdeLibraryPathsAdded != KdeLibraryPathsAddedDone) {
196 #endif
197  kdeLibraryPathsAdded = KdeLibraryPathsAddedDone;
198  const QStringList &plugins = dirs->resourceDirs("qtplugins");
199  QStringList::ConstIterator it = plugins.begin();
200  while (it != plugins.end()) {
202  ++it;
203  }
204  }
205 #endif
206 }
207 
208 extern KCONFIGCORE_EXPORT bool kde_kiosk_exception;
209 bool kde_kiosk_admin = false;
210 
211 void KComponentDataPrivate::configInit()
212 {
213  Q_ASSERT(!sharedConfig);
214 
215  if (!configName.isEmpty()) {
216  sharedConfig = KSharedConfig::openConfig(configName);
217 
218  //FIXME: this is broken and I don't know how to repair it
219  // Check whether custom config files are allowed.
220  KConfigGroup cg(sharedConfig, "KDE Action Restrictions");
221  QString kioskException = cg.readEntry("kiosk_exception");
222  if (!cg.readEntry("custom_config", true)) {
223  sharedConfig = nullptr;
224  }
225  }
226 
227  if (!sharedConfig) {
228  // was: KSharedConfig::openConfig(component)
229  sharedConfig = KSharedConfig::openConfig(aboutData.appName() + QLatin1String("rc"));
230  }
231 
232  // Check if we are excempt from kiosk restrictions
233  if (kde_kiosk_admin && !kde_kiosk_exception && !qgetenv("KDE_KIOSK_NO_RESTRICTIONS").isEmpty()) {
234  kde_kiosk_exception = true;
235  sharedConfig = nullptr;
236  configInit(); // Reread...
237  }
238 }
239 
240 const KSharedConfig::Ptr &KComponentData::config() const
241 {
242  Q_ASSERT(d);
243  d->lazyInit();
244 
245  return d->sharedConfig;
246 }
247 
248 void KComponentData::setConfigName(const QString &configName)
249 {
250  Q_ASSERT(d);
251  d->configName = configName;
252 }
253 
255 {
256  Q_ASSERT(d);
257  return &d->aboutData;
258 }
259 
261 {
262  d->aboutData = aboutData;
263 }
264 
266 {
267  Q_ASSERT(d);
268  return d->aboutData.appName();
269 }
270 
272 {
273  Q_ASSERT(d);
274  return d->aboutData.catalogName();
275 }
276 
278 {
279  KComponentDataStatic *s = globalStatic();
280  return s && s->mainComponent.isValid();
281 }
282 
284 {
285  KComponentDataStatic *s = globalStatic();
286  return s && s->mainComponent.isValid() ? s->mainComponent : *fakeComponent();
287 }
288 
290 {
291  MYASSERT(globalStatic()->activeComponent.isValid());
292  return globalStatic()->activeComponent;
293 }
294 
296 {
297  globalStatic()->activeComponent = c;
298 #if 0 // no longer available in KF5
299  if (c.isValid()) {
300  KLocale::global()->setActiveCatalog(c.catalogName());
301  }
302 #endif
303 }
304 
305 KComponentData::operator KAboutData() const
306 {
307  return KAboutData(*aboutData());
308 }
309 
311 {
312  /*BASE::virtual_hook(id, data);*/
313 }
314 
KComponentData & operator=(const KComponentData &)
Assignment operator.
bool operator!=(const KComponentData &rhs) const
Returns whether two KComponentData objects do not reference the same data.
const K4AboutData * aboutData() const
Returns the about data of this component.
bool operator==(const KComponentData &) const
Returns whether two KComponentData objects reference the same data.
bool isEmpty() const const
virtual void virtual_hook(int id, void *data)
Standard trick to add virtuals later.
QString appName() const
Returns the application&#39;s internal name.
void addLibraryPath(const QString &path)
QString catalogName() const
Returns the name of the translation catalog.
const KSharedConfig::Ptr & config() const
Returns the general config object ("appnamerc").
void setConfigName(const QString &name)
Set name of default config file.
bool isEmpty() const const
KComponentData()
Creates an invalid KComponentData object.
static bool hasMainComponent()
QCoreApplication * instance()
QString componentName() const
Returns the name of the component.
This class is used to store information about a program.
Definition: k4aboutdata.h:199
static KLocale * global()
Return the global KLocale instance.
Definition: klocale.cpp:309
QList::iterator end()
static void setApplicationData(const KAboutData &aboutData)
virtual ~KComponentData()
Destructor.
QStringList resourceDirs(const char *type) const
This function is used internally by almost all other function as it serves and fills the directories ...
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QByteArray toLatin1() const const
typedef ConstIterator
bool isValid() const
Returns whether this is a valid object.
static void setMainConfigName(const QString &str)
QString fromLatin1(const char *str, int size)
void setAboutData(const K4AboutData &aboutData)
Sets the about data of this component.
static const KComponentData & mainComponent()
Returns the global component data, if one was set.
T readEntry(const QString &key, const T &aDefault) const
static void setActiveComponent(const KComponentData &d)
Set the active component for use by KAboutDialog and KBugReport.
QList::iterator begin()
Per component data.
QString applicationName()
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-2021 The KDE developers.
Generated on Tue Jan 19 2021 23:01:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.