1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QCOREAPPLICATION_H
41#define QCOREAPPLICATION_H
42
43#include <QtCore/qglobal.h>
44#include <QtCore/qstring.h>
45#ifndef QT_NO_QOBJECT
46#include <QtCore/qobject.h>
47#include <QtCore/qcoreevent.h>
48#include <QtCore/qeventloop.h>
49#else
50#include <QtCore/qscopedpointer.h>
51#endif
52
53#ifndef QT_NO_QOBJECT
54#if defined(Q_OS_WIN) && !defined(tagMSG)
55typedef struct tagMSG MSG;
56#endif
57#endif
58
59QT_BEGIN_NAMESPACE
60
61
62class QCoreApplicationPrivate;
63class QTextCodec;
64class QTranslator;
65class QPostEventList;
66class QStringList;
67class QAbstractEventDispatcher;
68class QAbstractNativeEventFilter;
69
70#define qApp QCoreApplication::instance()
71
72class Q_CORE_EXPORT QCoreApplication
73#ifndef QT_NO_QOBJECT
74 : public QObject
75#endif
76{
77#ifndef QT_NO_QOBJECT
78 Q_OBJECT
79 Q_PROPERTY(QString applicationName READ applicationName WRITE setApplicationName NOTIFY applicationNameChanged)
80 Q_PROPERTY(QString applicationVersion READ applicationVersion WRITE setApplicationVersion NOTIFY applicationVersionChanged)
81 Q_PROPERTY(QString organizationName READ organizationName WRITE setOrganizationName NOTIFY organizationNameChanged)
82 Q_PROPERTY(QString organizationDomain READ organizationDomain WRITE setOrganizationDomain NOTIFY organizationDomainChanged)
83 Q_PROPERTY(bool quitLockEnabled READ isQuitLockEnabled WRITE setQuitLockEnabled)
84#endif
85
86 Q_DECLARE_PRIVATE(QCoreApplication)
87public:
88 enum { ApplicationFlags = QT_VERSION
89 };
90
91 QCoreApplication(int &argc, char **argv
92#ifndef Q_QDOC
93 , int = ApplicationFlags
94#endif
95 );
96
97 ~QCoreApplication();
98
99 static QStringList arguments();
100
101 static void setAttribute(Qt::ApplicationAttribute attribute, bool on = true);
102 static bool testAttribute(Qt::ApplicationAttribute attribute);
103
104 static void setOrganizationDomain(const QString &orgDomain);
105 static QString organizationDomain();
106 static void setOrganizationName(const QString &orgName);
107 static QString organizationName();
108 static void setApplicationName(const QString &application);
109 static QString applicationName();
110 static void setApplicationVersion(const QString &version);
111 static QString applicationVersion();
112
113 static void setSetuidAllowed(bool allow);
114 static bool isSetuidAllowed();
115
116 static QCoreApplication *instance() { return self; }
117
118#ifndef QT_NO_QOBJECT
119 static int exec();
120 static void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
121 static void processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime);
122 static void exit(int retcode=0);
123
124 static bool sendEvent(QObject *receiver, QEvent *event);
125 static void postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority);
126 static void sendPostedEvents(QObject *receiver = nullptr, int event_type = 0);
127 static void removePostedEvents(QObject *receiver, int eventType = 0);
128#if QT_DEPRECATED_SINCE(5, 3)
129 QT_DEPRECATED static bool hasPendingEvents();
130#endif
131 static QAbstractEventDispatcher *eventDispatcher();
132 static void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher);
133
134 virtual bool notify(QObject *, QEvent *);
135
136 static bool startingUp();
137 static bool closingDown();
138#endif
139
140 static QString applicationDirPath();
141 static QString applicationFilePath();
142 static qint64 applicationPid() Q_DECL_CONST_FUNCTION;
143
144#if QT_CONFIG(library)
145 static void setLibraryPaths(const QStringList &);
146 static QStringList libraryPaths();
147 static void addLibraryPath(const QString &);
148 static void removeLibraryPath(const QString &);
149#endif // QT_CONFIG(library)
150
151#ifndef QT_NO_TRANSLATION
152 static bool installTranslator(QTranslator * messageFile);
153 static bool removeTranslator(QTranslator * messageFile);
154#endif
155
156 static QString translate(const char * context,
157 const char * key,
158 const char * disambiguation = nullptr,
159 int n = -1);
160#if QT_DEPRECATED_SINCE(5, 0)
161 enum Encoding { UnicodeUTF8, Latin1, DefaultCodec = UnicodeUTF8, CodecForTr = UnicodeUTF8 };
162 QT_DEPRECATED static inline QString translate(const char * context, const char * key,
163 const char * disambiguation, Encoding, int n = -1)
164 { return translate(context, key, disambiguation, n); }
165#endif
166
167#ifndef QT_NO_QOBJECT
168# if QT_DEPRECATED_SINCE(5, 9)
169 QT_DEPRECATED static void flush();
170# endif
171
172 void installNativeEventFilter(QAbstractNativeEventFilter *filterObj);
173 void removeNativeEventFilter(QAbstractNativeEventFilter *filterObj);
174
175 static bool isQuitLockEnabled();
176 static void setQuitLockEnabled(bool enabled);
177
178public Q_SLOTS:
179 static void quit();
180
181Q_SIGNALS:
182 void aboutToQuit(QPrivateSignal);
183
184 void organizationNameChanged();
185 void organizationDomainChanged();
186 void applicationNameChanged();
187 void applicationVersionChanged();
188
189protected:
190 bool event(QEvent *) override;
191
192 virtual bool compressEvent(QEvent *, QObject *receiver, QPostEventList *);
193#endif // QT_NO_QOBJECT
194
195protected:
196 QCoreApplication(QCoreApplicationPrivate &p);
197
198#ifdef QT_NO_QOBJECT
199 QScopedPointer<QCoreApplicationPrivate> d_ptr;
200#endif
201
202private:
203#ifndef QT_NO_QOBJECT
204 static bool sendSpontaneousEvent(QObject *receiver, QEvent *event);
205# if QT_DEPRECATED_SINCE(5,6)
206 QT_DEPRECATED bool notifyInternal(QObject *receiver, QEvent *event); // ### Qt6 BIC: remove me
207# endif
208 static bool notifyInternal2(QObject *receiver, QEvent *);
209 static bool forwardEvent(QObject *receiver, QEvent *event, QEvent *originatingEvent = nullptr);
210#endif
211#if QT_CONFIG(library)
212 static QStringList libraryPathsLocked();
213#endif
214
215 static QCoreApplication *self;
216
217 Q_DISABLE_COPY(QCoreApplication)
218
219 friend class QApplication;
220 friend class QApplicationPrivate;
221 friend class QGuiApplication;
222 friend class QGuiApplicationPrivate;
223 friend class QWidget;
224 friend class QWidgetWindow;
225 friend class QWidgetPrivate;
226#ifndef QT_NO_QOBJECT
227 friend class QEventDispatcherUNIXPrivate;
228 friend class QCocoaEventDispatcherPrivate;
229 friend bool qt_sendSpontaneousEvent(QObject*, QEvent*);
230#endif
231 friend Q_CORE_EXPORT QString qAppName();
232 friend class QClassFactory;
233 friend class QCommandLineParserPrivate;
234};
235
236#ifdef QT_NO_DEPRECATED
237# define QT_DECLARE_DEPRECATED_TR_FUNCTIONS(context)
238#else
239# define QT_DECLARE_DEPRECATED_TR_FUNCTIONS(context) \
240 QT_DEPRECATED static inline QString trUtf8(const char *sourceText, const char *disambiguation = nullptr, int n = -1) \
241 { return QCoreApplication::translate(#context, sourceText, disambiguation, n); }
242#endif
243
244#define Q_DECLARE_TR_FUNCTIONS(context) \
245public: \
246 static inline QString tr(const char *sourceText, const char *disambiguation = nullptr, int n = -1) \
247 { return QCoreApplication::translate(#context, sourceText, disambiguation, n); } \
248 QT_DECLARE_DEPRECATED_TR_FUNCTIONS(context) \
249private:
250
251typedef void (*QtStartUpFunction)();
252typedef void (*QtCleanUpFunction)();
253
254Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction);
255Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction);
256Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction);
257Q_CORE_EXPORT QString qAppName(); // get application name
258
259#define Q_COREAPP_STARTUP_FUNCTION(AFUNC) \
260 static void AFUNC ## _ctor_function() { \
261 qAddPreRoutine(AFUNC); \
262 } \
263 Q_CONSTRUCTOR_FUNCTION(AFUNC ## _ctor_function)
264
265#ifndef QT_NO_QOBJECT
266#if defined(Q_OS_WIN) && !defined(QT_NO_DEBUG_STREAM)
267Q_CORE_EXPORT QString decodeMSG(const MSG &);
268Q_CORE_EXPORT QDebug operator<<(QDebug, const MSG &);
269#endif
270#endif
271
272QT_END_NAMESPACE
273
274#endif // QCOREAPPLICATION_H
275