1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QCOREAPPLICATION_H
43 #define QCOREAPPLICATION_H
44 
45 #include <QtCore/qobject.h>
46 #include <QtCore/qcoreevent.h>
47 #include <QtCore/qeventloop.h>
48 
49 #ifdef QT_INCLUDE_COMPAT
50 #include <QtCore/qstringlist.h>
51 #endif
52 
53 #if defined(Q_WS_WIN) && !defined(tagMSG)
54 typedef struct tagMSG MSG;
55 #endif
56 
57 QT_BEGIN_HEADER
58 
59 QT_BEGIN_NAMESPACE
60 
61 QT_MODULE(Core)
62 
63 class QCoreApplicationPrivate;
64 class QTextCodec;
65 class QTranslator;
66 class QPostEventList;
67 class QStringList;
68 
69 #define qApp QCoreApplication::instance()
70 
71 class Q_CORE_EXPORT QCoreApplication : public QObject
72 {
73     Q_OBJECT
74     Q_PROPERTY(QString applicationName READ applicationName WRITE setApplicationName)
75     Q_PROPERTY(QString applicationVersion READ applicationVersion WRITE setApplicationVersion)
76     Q_PROPERTY(QString organizationName READ organizationName WRITE setOrganizationName)
77     Q_PROPERTY(QString organizationDomain READ organizationDomain WRITE setOrganizationDomain)
78 
79     Q_DECLARE_PRIVATE(QCoreApplication)
80 public:
81     enum { ApplicationFlags = QT_VERSION
82 #if !defined(QT3_SUPPORT)
83         | 0x01000000
84 #endif
85     };
86 
87 #if defined(QT_BUILD_CORE_LIB) || defined(qdoc)
88     QCoreApplication(int &argc, char **argv); // ### Qt5 remove
89 #endif
90 #if !defined(qdoc)
91     QCoreApplication(int &argc, char **argv, int
92 #if !defined(QT_BUILD_CORE_LIB)
93         = ApplicationFlags
94 #endif
95         );
96 #endif
97 
98     ~QCoreApplication();
99 
100 #ifdef QT_DEPRECATED
101     QT_DEPRECATED static int argc();
102     QT_DEPRECATED static char **argv();
103 #endif
104     static QStringList arguments();
105 
106     static void setAttribute(Qt::ApplicationAttribute attribute, bool on = true);
107     static bool testAttribute(Qt::ApplicationAttribute attribute);
108 
109     static void setOrganizationDomain(const QString &orgDomain);
110     static QString organizationDomain();
111     static void setOrganizationName(const QString &orgName);
112     static QString organizationName();
113     static void setApplicationName(const QString &application);
114     static QString applicationName();
115     static void setApplicationVersion(const QString &version);
116     static QString applicationVersion();
117 
instance()118     static QCoreApplication *instance() { return self; }
119 
120     static int exec();
121     static void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
122     static void processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime);
123     static void exit(int retcode=0);
124 
125     static bool sendEvent(QObject *receiver, QEvent *event);
126     static void postEvent(QObject *receiver, QEvent *event);
127     static void postEvent(QObject *receiver, QEvent *event, int priority);
128     static void sendPostedEvents(QObject *receiver, int event_type);
129     static void sendPostedEvents();
130     static void removePostedEvents(QObject *receiver);
131     static void removePostedEvents(QObject *receiver, int eventType);
132     static bool hasPendingEvents();
133 
134     virtual bool notify(QObject *, QEvent *);
135 
136     static bool startingUp();
137     static bool closingDown();
138 
139     static QString applicationDirPath();
140     static QString applicationFilePath();
141     static qint64 applicationPid();
142 
143 #ifndef QT_NO_LIBRARY
144     static void setLibraryPaths(const QStringList &);
145     static QStringList libraryPaths();
146     static void addLibraryPath(const QString &);
147     static void removeLibraryPath(const QString &);
148 #endif // QT_NO_LIBRARY
149 
150 #ifndef QT_NO_TRANSLATION
151     static void installTranslator(QTranslator * messageFile);
152     static void removeTranslator(QTranslator * messageFile);
153 #endif
154     enum Encoding { CodecForTr, UnicodeUTF8, DefaultCodec = CodecForTr };
155     // ### Qt 5: merge
156     static QString translate(const char * context,
157                              const char * key,
158                              const char * disambiguation = 0,
159                              Encoding encoding = CodecForTr);
160     static QString translate(const char * context,
161                              const char * key,
162                              const char * disambiguation,
163                              Encoding encoding, int n);
164 
165     static void flush();
166 
167 #if defined(QT3_SUPPORT)
lock()168     inline QT3_SUPPORT void lock() {}
169     inline QT3_SUPPORT void unlock(bool = true) {}
locked()170     inline QT3_SUPPORT bool locked() { return false; }
tryLock()171     inline QT3_SUPPORT bool tryLock() { return false; }
172 
processOneEvent()173     static inline QT3_SUPPORT void processOneEvent()
174     { processEvents(QEventLoop::WaitForMoreEvents); }
175     static QT3_SUPPORT int enter_loop();
176     static QT3_SUPPORT void exit_loop();
177     static QT3_SUPPORT int loopLevel();
178 #endif
179 
180 #if defined(Q_WS_WIN)
181     virtual bool winEventFilter(MSG *message, long *result);
182 #endif
183 
184 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
185     static void watchUnixSignal(int signal, bool watch);
186 #endif
187 
188     typedef bool (*EventFilter)(void *message, long *result);
189     EventFilter setEventFilter(EventFilter filter);
190     bool filterEvent(void *message, long *result);
191 
192 public Q_SLOTS:
193     static void quit();
194 
195 Q_SIGNALS:
196     void aboutToQuit();
197     void unixSignal(int);
198 
199 protected:
200     bool event(QEvent *);
201 
202     virtual bool compressEvent(QEvent *, QObject *receiver, QPostEventList *);
203 
204 protected:
205     QCoreApplication(QCoreApplicationPrivate &p);
206 
207 private:
208     static bool sendSpontaneousEvent(QObject *receiver, QEvent *event);
209     bool notifyInternal(QObject *receiver, QEvent *event);
210 
211     void init();
212 
213     static QCoreApplication *self;
214 
215     Q_DISABLE_COPY(QCoreApplication)
216 
217     friend class QEventDispatcherUNIXPrivate;
218     friend class QApplication;
219     friend class QApplicationPrivate;
220     friend class QETWidget;
221     friend class Q3AccelManager;
222     friend class QShortcutMap;
223     friend class QWidget;
224     friend class QWidgetPrivate;
225     friend bool qt_sendSpontaneousEvent(QObject*, QEvent*);
226     friend Q_CORE_EXPORT QString qAppName();
227     friend class QClassFactory;
228 };
229 
sendEvent(QObject * receiver,QEvent * event)230 inline bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
231 {  if (event) event->spont = false; return self ? self->notifyInternal(receiver, event) : false; }
232 
sendSpontaneousEvent(QObject * receiver,QEvent * event)233 inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
234 { if (event) event->spont = true; return self ? self->notifyInternal(receiver, event) : false; }
235 
sendPostedEvents()236 inline void QCoreApplication::sendPostedEvents() { sendPostedEvents(0, 0); }
237 
238 #ifdef QT_NO_TRANSLATION
239 // Simple versions
translate(const char *,const char * sourceText,const char *,Encoding encoding)240 inline QString QCoreApplication::translate(const char *, const char *sourceText,
241                                            const char *, Encoding encoding)
242 {
243 #ifndef QT_NO_TEXTCODEC
244     if (encoding == UnicodeUTF8)
245         return QString::fromUtf8(sourceText);
246 #else
247     Q_UNUSED(encoding)
248 #endif
249     return QString::fromLatin1(sourceText);
250 }
251 
252 // Simple versions
translate(const char *,const char * sourceText,const char *,Encoding encoding,int)253 inline QString QCoreApplication::translate(const char *, const char *sourceText,
254                                            const char *, Encoding encoding, int)
255 {
256 #ifndef QT_NO_TEXTCODEC
257     if (encoding == UnicodeUTF8)
258         return QString::fromUtf8(sourceText);
259 #else
260     Q_UNUSED(encoding)
261 #endif
262     return QString::fromLatin1(sourceText);
263 }
264 #endif
265 
266 // ### merge the four functions into two (using "int n = -1")
267 #define Q_DECLARE_TR_FUNCTIONS(context) \
268 public: \
269     static inline QString tr(const char *sourceText, const char *disambiguation = 0) \
270         { return QCoreApplication::translate(#context, sourceText, disambiguation); } \
271     static inline QString trUtf8(const char *sourceText, const char *disambiguation = 0) \
272         { return QCoreApplication::translate(#context, sourceText, disambiguation, \
273                                              QCoreApplication::UnicodeUTF8); } \
274     static inline QString tr(const char *sourceText, const char *disambiguation, int n) \
275         { return QCoreApplication::translate(#context, sourceText, disambiguation, \
276                                              QCoreApplication::CodecForTr, n); } \
277     static inline QString trUtf8(const char *sourceText, const char *disambiguation, int n) \
278         { return QCoreApplication::translate(#context, sourceText, disambiguation, \
279                                              QCoreApplication::UnicodeUTF8, n); } \
280 private:
281 
282 typedef void (*QtCleanUpFunction)();
283 
284 Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction);
285 Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction);
286 Q_CORE_EXPORT QString qAppName();                // get application name
287 
288 #if defined(Q_WS_WIN) && !defined(QT_NO_DEBUG_STREAM)
289 Q_CORE_EXPORT QString decodeMSG(const MSG &);
290 Q_CORE_EXPORT QDebug operator<<(QDebug, const MSG &);
291 #endif
292 
293 QT_END_NAMESPACE
294 
295 QT_END_HEADER
296 
297 #endif // QCOREAPPLICATION_H
298