1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QCOREAPPLICATION_H
43 #define QCOREAPPLICATION_H
45 #include <QtCore/qobject.h>
46 #include <QtCore/qcoreevent.h>
47 #include <QtCore/qeventloop.h>
49 #if defined(Q_OS_WIN) && !defined(tagMSG)
50 typedef struct tagMSG MSG;
58 class QCoreApplicationPrivate;
63 class QAbstractEventDispatcher;
65 #define qApp QCoreApplication::instance()
67 class Q_CORE_EXPORT QCoreApplication : public QObject
70 Q_PROPERTY(QString applicationName READ applicationName WRITE setApplicationName)
71 Q_PROPERTY(QString applicationVersion READ applicationVersion WRITE setApplicationVersion)
72 Q_PROPERTY(QString organizationName READ organizationName WRITE setOrganizationName)
73 Q_PROPERTY(QString organizationDomain READ organizationDomain WRITE setOrganizationDomain)
74 Q_PROPERTY(bool quitLockEnabled READ isQuitLockEnabled WRITE setQuitLockEnabled)
76 Q_DECLARE_PRIVATE(QCoreApplication)
78 enum { ApplicationFlags = QT_VERSION
84 GuiServer // # deprecated
87 QCoreApplication(int &argc, char **argv, int = ApplicationFlags);
91 static QStringList arguments();
93 static void setAttribute(Qt::ApplicationAttribute attribute, bool on = true);
94 static bool testAttribute(Qt::ApplicationAttribute attribute);
96 static void setOrganizationDomain(const QString &orgDomain);
97 static QString organizationDomain();
98 static void setOrganizationName(const QString &orgName);
99 static QString organizationName();
100 static void setApplicationName(const QString &application);
101 static QString applicationName();
102 static void setApplicationVersion(const QString &version);
103 static QString applicationVersion();
105 static QCoreApplication *instance() { return self; }
108 static void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
109 static void processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime);
110 static void exit(int retcode=0);
112 static bool sendEvent(QObject *receiver, QEvent *event);
113 static void postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority);
114 static void sendPostedEvents(QObject *receiver = 0, int event_type = 0);
115 static void removePostedEvents(QObject *receiver, int eventType = 0);
116 static bool hasPendingEvents();
117 static QAbstractEventDispatcher *eventDispatcher();
118 static void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher);
120 virtual bool notify(QObject *, QEvent *);
122 static bool startingUp();
123 static bool closingDown();
125 static QString applicationDirPath();
126 static QString applicationFilePath();
127 static qint64 applicationPid();
129 #ifndef QT_NO_LIBRARY
130 static void setLibraryPaths(const QStringList &);
131 static QStringList libraryPaths();
132 static void addLibraryPath(const QString &);
133 static void removeLibraryPath(const QString &);
134 #endif // QT_NO_LIBRARY
136 #ifndef QT_NO_TRANSLATION
137 static void installTranslator(QTranslator * messageFile);
138 static void removeTranslator(QTranslator * messageFile);
140 enum Encoding { UnicodeUTF8, Latin1, DefaultCodec = Latin1
141 #if QT_DEPRECATED_SINCE(5, 0)
142 , CodecForTr = Latin1
145 static QString translate(const char * context,
147 const char * disambiguation = 0,
148 Encoding encoding = DefaultCodec,
153 #if defined(Q_OS_WIN)
154 virtual bool winEventFilter(MSG *message, long *result);
157 #if defined(Q_OS_UNIX)
158 static void watchUnixSignal(int signal, bool watch);
161 typedef bool (*EventFilter)(void *message, long *result);
162 EventFilter setEventFilter(EventFilter filter);
163 bool filterEvent(void *message, long *result);
165 static bool isQuitLockEnabled();
166 static void setQuitLockEnabled(bool enabled);
173 void unixSignal(int);
176 bool event(QEvent *);
178 virtual bool compressEvent(QEvent *, QObject *receiver, QPostEventList *);
181 QCoreApplication(QCoreApplicationPrivate &p);
184 Q_PRIVATE_SLOT(d_func(), void _q_initializeProcessManager())
185 static bool sendSpontaneousEvent(QObject *receiver, QEvent *event);
186 bool notifyInternal(QObject *receiver, QEvent *event);
190 static QCoreApplication *self;
192 Q_DISABLE_COPY(QCoreApplication)
194 friend class QEventDispatcherUNIXPrivate;
195 friend class QApplication;
196 friend class QApplicationPrivate;
197 friend class QGuiApplication;
198 friend class QGuiApplicationPrivate;
199 friend class QETWidget;
200 friend class QWidget;
201 friend class QWidgetWindow;
202 friend class QWidgetPrivate;
203 friend class QCocoaEventDispatcherPrivate;
204 friend bool qt_sendSpontaneousEvent(QObject*, QEvent*);
205 friend Q_CORE_EXPORT QString qAppName();
206 friend class QClassFactory;
209 inline bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
210 { if (event) event->spont = false; return self ? self->notifyInternal(receiver, event) : false; }
212 inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
213 { if (event) event->spont = true; return self ? self->notifyInternal(receiver, event) : false; }
215 #ifdef QT_NO_TRANSLATION
217 inline QString QCoreApplication::translate(const char *, const char *sourceText,
218 const char *, Encoding encoding)
220 #ifndef QT_NO_TEXTCODEC
221 if (encoding == UnicodeUTF8)
222 return QString::fromUtf8(sourceText);
226 return QString::fromLatin1(sourceText);
230 inline QString QCoreApplication::translate(const char *, const char *sourceText,
231 const char *, Encoding encoding, int)
233 #ifndef QT_NO_TEXTCODEC
234 if (encoding == UnicodeUTF8)
235 return QString::fromUtf8(sourceText);
239 return QString::fromLatin1(sourceText);
243 #define Q_DECLARE_TR_FUNCTIONS(context) \
245 static inline QString tr(const char *sourceText, const char *disambiguation = 0, int n = -1) \
246 { return QCoreApplication::translate(#context, sourceText, disambiguation, \
247 QCoreApplication::DefaultCodec, n); } \
248 static inline QString trUtf8(const char *sourceText, const char *disambiguation = 0, int n = -1) \
249 { return QCoreApplication::translate(#context, sourceText, disambiguation, \
250 QCoreApplication::UnicodeUTF8, n); } \
253 typedef void (*QtCleanUpFunction)();
255 Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction);
256 Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction);
257 Q_CORE_EXPORT QString qAppName(); // get application name
259 #if defined(Q_OS_WIN) && !defined(QT_NO_DEBUG_STREAM)
260 Q_CORE_EXPORT QString decodeMSG(const MSG &);
261 Q_CORE_EXPORT QDebug operator<<(QDebug, const MSG &);
268 #endif // QCOREAPPLICATION_H