KDELibs4Support

kglobal.h
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 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #ifndef _KGLOBAL_H
20 #define _KGLOBAL_H
21 
22 #include <kdelibs4support_export.h>
23 
24 #ifdef KDELIBS4SUPPORT_NO_DEPRECATED_NOISE
25 #warning "This file is deprecated."
26 #endif
27 
28 #include <QAtomicPointer>
29 #include <sys/types.h>
30 #include <QDebug>
31 // To simplify Qt5 porting in KDE code not yet ported to frameworks.
32 #include <QMimeData>
33 #include <klocale.h>
34 
35 // TODO: Re-add for source compat: #include <kdemacros.h>
36 
37 //
38 // WARNING!!
39 // This code uses undocumented Qt API
40 // Do not copy it to your application! Use only the functions that are here!
41 // Otherwise, it could break when a new version of Qt ships.
42 //
43 
44 class KComponentData;
45 class KCharsets;
46 class KConfig;
47 class KStandardDirs;
48 class KSharedConfig;
49 template <typename T>
52 
54 
58 typedef void (*KdeCleanUpFunction)();
59 
66 class KCleanUpGlobalStatic
67 {
68 public:
69  KdeCleanUpFunction func;
70 
71  inline ~KCleanUpGlobalStatic()
72  {
73  func();
74  }
75 };
76 
77 #ifdef Q_CC_MSVC
78 
84 # define K_GLOBAL_STATIC_STRUCT_NAME(NAME) _k_##NAME##__LINE__
85 #else
86 
91 # define K_GLOBAL_STATIC_STRUCT_NAME(NAME)
92 #endif
93 
95 
233 #define K_GLOBAL_STATIC(TYPE, NAME) K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
234 
268 // In Qt5 QBasicAtomicPointer<T> no longer implicit casts to T*
269 // Instead it has load() and store() methods which do not exist in Qt4.
270 // In practice, we should be porting frameworks to the new Q_GLOBAL_STATIC
271 // which isn't in Qt5 yet, so duplicate for now.
272 
273 #define K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \
274  static QBasicAtomicPointer<TYPE > _k_static_##NAME = Q_BASIC_ATOMIC_INITIALIZER(0); \
275  static bool _k_static_##NAME##_destroyed; \
276  static struct K_GLOBAL_STATIC_STRUCT_NAME(NAME) \
277  { \
278  inline bool isDestroyed() const \
279  { \
280  return _k_static_##NAME##_destroyed; \
281  } \
282  inline bool exists() const \
283  { \
284  return _k_static_##NAME.load() != 0; \
285  } \
286  inline operator TYPE*() \
287  { \
288  return operator->(); \
289  } \
290  inline TYPE *operator->() \
291  { \
292  if (!_k_static_##NAME.load()) { \
293  if (isDestroyed()) { \
294  qFatal("Fatal Error: Accessed global static '%s *%s()' after destruction. " \
295  "Defined at %s:%d", #TYPE, #NAME, __FILE__, __LINE__); \
296  } \
297  TYPE *x = new TYPE ARGS; \
298  if (!_k_static_##NAME.testAndSetOrdered(0, x) \
299  && _k_static_##NAME.load() != x ) { \
300  delete x; \
301  } else { \
302  static KCleanUpGlobalStatic cleanUpObject = { destroy }; \
303  } \
304  } \
305  return _k_static_##NAME.load(); \
306  } \
307  inline TYPE &operator*() \
308  { \
309  return *operator->(); \
310  } \
311  static void destroy() \
312  { \
313  _k_static_##NAME##_destroyed = true; \
314  TYPE *x = _k_static_##NAME.load(); \
315  _k_static_##NAME.store(0); \
316  delete x; \
317  } \
318  } NAME;
319 
329 namespace KGlobal
330 {
331 
332 struct KDELIBS4SUPPORT_DEPRECATED_EXPORT_NOISE LocaleWrapper : public KLocale {
333  KDELIBS4SUPPORT_DEPRECATED explicit LocaleWrapper(KLocale *locale)
334  : KLocale(*locale)
335  {
336 
337  }
338 
339  KDELIBS4SUPPORT_DEPRECATED static void insertCatalog(const QString &)
340  {
341  qWarning() << "Your code needs to be ported in KF5. See the Ki18n programmers guide.";
342  }
343 
344  LocaleWrapper *operator->()
345  {
346  return this;
347  }
348 
349  operator KLocale *()
350  {
351  return this;
352  }
353 };
354 
362 KDELIBS4SUPPORT_DEPRECATED_EXPORT const KComponentData &mainComponent(); //krazy:exclude=constref (don't mess up ref-counting)
363 
369 KDELIBS4SUPPORT_DEPRECATED_EXPORT bool hasMainComponent();
370 
375 KDELIBS4SUPPORT_DEPRECATED_EXPORT KStandardDirs *dirs();
376 
382 KDELIBS4SUPPORT_DEPRECATED_EXPORT KSharedConfigPtr config();
383 
396 KDELIBS4SUPPORT_DEPRECATED_EXPORT LocaleWrapper locale();
402 KDELIBS4SUPPORT_DEPRECATED_EXPORT bool hasLocale();
403 
409 KDELIBS4SUPPORT_DEPRECATED_EXPORT KCharsets *charsets();
410 
415 KDELIBS4SUPPORT_DEPRECATED_EXPORT mode_t umask();
416 
435 KDELIBS4SUPPORT_DEPRECATED_EXPORT const QString &staticQString(const char *str); //krazy:exclude=constref (doesn't make sense otherwise)
436 
455 KDELIBS4SUPPORT_DEPRECATED_EXPORT const QString &staticQString(const QString &str); //krazy:exclude=constref (doesn't make sense otherwise)
456 
480 KDELIBS4SUPPORT_DEPRECATED_EXPORT void ref();
481 
488 KDELIBS4SUPPORT_DEPRECATED_EXPORT void deref();
489 
506 KDELIBS4SUPPORT_DEPRECATED_EXPORT void setAllowQuit(bool allowQuit);
507 
517 KDELIBS4SUPPORT_DEPRECATED_EXPORT KComponentData activeComponent();
518 
527 KDELIBS4SUPPORT_DEPRECATED_EXPORT void setActiveComponent(const KComponentData &d);
528 
539 KDELIBS4SUPPORT_DEPRECATED_EXPORT QString caption();
540 
542 KDELIBS4SUPPORT_DEPRECATED_EXPORT QObject *findDirectChild_helper(const QObject *parent, const QMetaObject &mo);
543 
550 template<typename T>
551 KDELIBS4SUPPORT_DEPRECATED inline T findDirectChild(const QObject *object)
552 {
553  return static_cast<T>(findDirectChild_helper(object, (static_cast<T>(nullptr))->staticMetaObject));
554 }
555 }
556 
557 struct KCatalogLoader {
558  KDELIBS4SUPPORT_DEPRECATED KCatalogLoader(const QString &)
559  {
560  qWarning() << "Your code needs to be ported in KF5. See the Ki18n programmers guide.";
561  }
562 };
563 
564 #endif // _KGLOBAL_H
565 
Access to the KDE global objects.
Definition: kglobal.h:329
QString caption()
Returns a text for the window caption.
Definition: kglobal.cpp:177
T findDirectChild(const QObject *object)
Returns the child of the given object that can be cast into type T, or 0 if there is no such object...
Definition: kglobal.h:551
void ref()
Tells KGlobal about one more operations that should be finished before the application exits...
Definition: kglobal.cpp:209
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:104
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
void setActiveComponent(const KComponentData &d)
Set the active component for use by KAboutDialog and KBugReport.
Definition: kglobal.cpp:145
KLocale provides support for language and country specific stuff.
Definition: klocale.h:75
QObject * findDirectChild_helper(const QObject *parent, const QMetaObject &mo)
Definition: kglobal.cpp:231
const KComponentData & mainComponent()
Returns the global component data.
Definition: kglobal.cpp:109
bool hasMainComponent()
Definition: kglobal.cpp:114
const QString & staticQString(const char *str)
Creates a static QString.
Definition: kglobal.cpp:156
LocaleWrapper locale()
Returns the global locale object.
Definition: kglobal.cpp:119
bool hasLocale()
Definition: kglobal.cpp:124
Per component data.
KStandardDirs * dirs()
Returns the application standard dirs object.
Definition: kglobal.cpp:91
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 14 2020 22:58:39 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.