1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QCOREAPPLICATION_H
5#define QCOREAPPLICATION_H
6
7#include <QtCore/qglobal.h>
8#include <QtCore/qstring.h>
9#ifndef QT_NO_QOBJECT
10#include <QtCore/qcoreevent.h>
11#include <QtCore/qdeadlinetimer.h>
12#include <QtCore/qeventloop.h>
13#include <QtCore/qobject.h>
14#else
15#include <QtCore/qscopedpointer.h>
16#endif
17#include <QtCore/qnativeinterface.h>
18#ifndef QT_NO_DEBUGSTREAM
19#include <QtCore/qdebug.h>
20#endif
21
22#ifndef QT_NO_QOBJECT
23#if defined(Q_OS_WIN) && !defined(tagMSG)
24typedef struct tagMSG MSG;
25#endif
26#endif
27
28QT_BEGIN_NAMESPACE
29
30
31class QCoreApplicationPrivate;
32class QTranslator;
33class QPostEventList;
34class QAbstractEventDispatcher;
35class QAbstractNativeEventFilter;
36class QEventLoopLocker;
37
38#if QT_CONFIG(permissions) || defined(Q_QDOC)
39class QPermission;
40#endif
41
42#define qApp QCoreApplication::instance()
43
44class Q_CORE_EXPORT QCoreApplication
45#ifndef QT_NO_QOBJECT
46 : public QObject
47#endif
48{
49#ifndef QT_NO_QOBJECT
50 Q_OBJECT
51 Q_PROPERTY(QString applicationName READ applicationName WRITE setApplicationName
52 NOTIFY applicationNameChanged)
53 Q_PROPERTY(QString applicationVersion READ applicationVersion WRITE setApplicationVersion
54 NOTIFY applicationVersionChanged)
55 Q_PROPERTY(QString organizationName READ organizationName WRITE setOrganizationName
56 NOTIFY organizationNameChanged)
57 Q_PROPERTY(QString organizationDomain READ organizationDomain WRITE setOrganizationDomain
58 NOTIFY organizationDomainChanged)
59 Q_PROPERTY(bool quitLockEnabled READ isQuitLockEnabled WRITE setQuitLockEnabled)
60#endif
61
62 Q_DECLARE_PRIVATE(QCoreApplication)
63 friend class QEventLoopLocker;
64#if QT_CONFIG(permissions)
65 using RequestPermissionPrototype = void(*)(QPermission);
66#endif
67
68public:
69 enum { ApplicationFlags = QT_VERSION
70 };
71
72 QCoreApplication(int &argc, char **argv
73#ifndef Q_QDOC
74 , int = ApplicationFlags
75#endif
76 );
77
78 ~QCoreApplication();
79
80 static QStringList arguments();
81
82 static void setAttribute(Qt::ApplicationAttribute attribute, bool on = true);
83 static bool testAttribute(Qt::ApplicationAttribute attribute);
84
85 static void setOrganizationDomain(const QString &orgDomain);
86 static QString organizationDomain();
87 static void setOrganizationName(const QString &orgName);
88 static QString organizationName();
89 static void setApplicationName(const QString &application);
90 static QString applicationName();
91 static void setApplicationVersion(const QString &version);
92 static QString applicationVersion();
93
94 static void setSetuidAllowed(bool allow);
95 static bool isSetuidAllowed();
96
97 static QCoreApplication *instance() noexcept { return self; }
98
99#ifndef QT_NO_QOBJECT
100 static int exec();
101 static void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
102 static void processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime);
103 static void processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline);
104
105 static bool sendEvent(QObject *receiver, QEvent *event);
106 static void postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority);
107 static void sendPostedEvents(QObject *receiver = nullptr, int event_type = 0);
108 static void removePostedEvents(QObject *receiver, int eventType = 0);
109 static QAbstractEventDispatcher *eventDispatcher();
110 static void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher);
111
112 virtual bool notify(QObject *, QEvent *);
113
114 static bool startingUp();
115 static bool closingDown();
116#endif
117
118 static QString applicationDirPath();
119 static QString applicationFilePath();
120 static qint64 applicationPid() Q_DECL_CONST_FUNCTION;
121
122#if QT_CONFIG(permissions) || defined(Q_QDOC)
123 Qt::PermissionStatus checkPermission(const QPermission &permission);
124
125# ifdef Q_QDOC
126 template <typename Functor>
127 void requestPermission(const QPermission &permission, const QObject *context, Functor functor);
128# else
129 // requestPermission with context or receiver object; need to require here that receiver is the
130 // right type to avoid ambiguity with the private implementation function.
131 template <typename Functor,
132 std::enable_if_t<
133 QtPrivate::AreFunctionsCompatible<RequestPermissionPrototype, Functor>::value,
134 bool> = true>
135 void requestPermission(const QPermission &permission,
136 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
137 Functor &&func)
138 {
139 requestPermission(permission,
140 QtPrivate::makeCallableObject<RequestPermissionPrototype>(std::forward<Functor>(func)),
141 receiver);
142 }
143# endif // Q_QDOC
144
145#ifndef QT_NO_CONTEXTLESS_CONNECT
146 #ifdef Q_QDOC
147 template <typename Functor>
148 #else
149 // requestPermission to a functor or function pointer (without context)
150 template <typename Functor,
151 std::enable_if_t<
152 QtPrivate::AreFunctionsCompatible<RequestPermissionPrototype, Functor>::value,
153 bool> = true>
154 #endif
155 void requestPermission(const QPermission &permission, Functor &&func)
156 {
157 requestPermission(permission, nullptr, std::forward<Functor>(func));
158 }
159#endif // QT_NO_CONTEXTLESS_CONNECT
160
161private:
162 // ### Qt 7: rename to requestPermissionImpl to avoid ambiguity
163 void requestPermission(const QPermission &permission,
164 QtPrivate::QSlotObjectBase *slotObj, const QObject *context);
165public:
166
167#endif // QT_CONFIG(permission)
168
169#if QT_CONFIG(library)
170 static void setLibraryPaths(const QStringList &);
171 static QStringList libraryPaths();
172 static void addLibraryPath(const QString &);
173 static void removeLibraryPath(const QString &);
174#endif // QT_CONFIG(library)
175
176#ifndef QT_NO_TRANSLATION
177 static bool installTranslator(QTranslator * messageFile);
178 static bool removeTranslator(QTranslator * messageFile);
179#endif
180
181 static QString translate(const char * context,
182 const char * key,
183 const char * disambiguation = nullptr,
184 int n = -1);
185
186 QT_DECLARE_NATIVE_INTERFACE_ACCESSOR(QCoreApplication)
187
188#ifndef QT_NO_QOBJECT
189 void installNativeEventFilter(QAbstractNativeEventFilter *filterObj);
190 void removeNativeEventFilter(QAbstractNativeEventFilter *filterObj);
191
192 static bool isQuitLockEnabled();
193 static void setQuitLockEnabled(bool enabled);
194
195public Q_SLOTS:
196 static void quit();
197 static void exit(int retcode = 0);
198
199Q_SIGNALS:
200 void aboutToQuit(QPrivateSignal);
201
202 void organizationNameChanged();
203 void organizationDomainChanged();
204 void applicationNameChanged();
205 void applicationVersionChanged();
206
207protected:
208 bool event(QEvent *) override;
209
210 virtual bool compressEvent(QEvent *, QObject *receiver, QPostEventList *);
211#endif // QT_NO_QOBJECT
212
213protected:
214 QCoreApplication(QCoreApplicationPrivate &p);
215
216#ifdef QT_NO_QOBJECT
217 QScopedPointer<QCoreApplicationPrivate> d_ptr;
218#endif
219
220private:
221#ifndef QT_NO_QOBJECT
222 static bool sendSpontaneousEvent(QObject *receiver, QEvent *event);
223 static bool notifyInternal2(QObject *receiver, QEvent *);
224 static bool forwardEvent(QObject *receiver, QEvent *event, QEvent *originatingEvent = nullptr);
225#endif
226#if QT_CONFIG(library)
227 static QStringList libraryPathsLocked();
228#endif
229
230 static QCoreApplication *self;
231
232 Q_DISABLE_COPY(QCoreApplication)
233
234 friend class QApplication;
235 friend class QApplicationPrivate;
236 friend class QGuiApplication;
237 friend class QGuiApplicationPrivate;
238 friend class QWidget;
239 friend class QWidgetWindow;
240 friend class QWidgetPrivate;
241#ifndef QT_NO_QOBJECT
242 friend class QEventDispatcherUNIXPrivate;
243 friend class QCocoaEventDispatcherPrivate;
244 friend bool qt_sendSpontaneousEvent(QObject *, QEvent *);
245#endif
246 friend Q_CORE_EXPORT QString qAppName();
247 friend class QCommandLineParserPrivate;
248};
249
250#define Q_DECLARE_TR_FUNCTIONS(context) \
251public: \
252 static inline QString tr(const char *sourceText, const char *disambiguation = nullptr, int n = -1) \
253 { return QCoreApplication::translate(#context, sourceText, disambiguation, n); } \
254private:
255
256typedef void (*QtStartUpFunction)();
257typedef void (*QtCleanUpFunction)();
258
259Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction);
260Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction);
261Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction);
262Q_CORE_EXPORT QString qAppName(); // get application name
263
264#define Q_COREAPP_STARTUP_FUNCTION(AFUNC) \
265 static void AFUNC ## _ctor_function() { \
266 qAddPreRoutine(AFUNC); \
267 } \
268 Q_CONSTRUCTOR_FUNCTION(AFUNC ## _ctor_function)
269
270#ifndef QT_NO_QOBJECT
271#if defined(Q_OS_WIN) && !defined(QT_NO_DEBUG_STREAM)
272Q_CORE_EXPORT QString decodeMSG(const MSG &);
273Q_CORE_EXPORT QDebug operator<<(QDebug, const MSG &);
274#endif
275#endif
276
277QT_END_NAMESPACE
278
279#include <QtCore/qcoreapplication_platform.h>
280
281#endif // QCOREAPPLICATION_H
282