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

source code of qtbase/src/corelib/kernel/qcoreapplication.h