cf76511f254ab4510b1a0e47bc830b32aa87c6ad
[profile/ivi/qtbase.git] / src / corelib / kernel / qcoreapplication.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
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 #if defined(Q_OS_WIN) && !defined(tagMSG)
50 typedef struct tagMSG MSG;
51 #endif
52
53 QT_BEGIN_HEADER
54
55 QT_BEGIN_NAMESPACE
56
57
58 class QCoreApplicationPrivate;
59 class QTextCodec;
60 class QTranslator;
61 class QPostEventList;
62 class QStringList;
63 class QAbstractEventDispatcher;
64
65 #define qApp QCoreApplication::instance()
66
67 class Q_CORE_EXPORT QCoreApplication : public QObject
68 {
69     Q_OBJECT
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)
75
76     Q_DECLARE_PRIVATE(QCoreApplication)
77 public:
78     enum { ApplicationFlags = QT_VERSION
79     };
80
81     enum Type {
82         Tty,
83         GuiClient,
84         GuiServer // # deprecated
85     };
86
87     QCoreApplication(int &argc, char **argv, int = ApplicationFlags);
88
89     ~QCoreApplication();
90
91     static QStringList arguments();
92
93     static void setAttribute(Qt::ApplicationAttribute attribute, bool on = true);
94     static bool testAttribute(Qt::ApplicationAttribute attribute);
95
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();
104
105     static QCoreApplication *instance() { return self; }
106
107     static int exec();
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);
111
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);
119
120     virtual bool notify(QObject *, QEvent *);
121
122     static bool startingUp();
123     static bool closingDown();
124
125     static QString applicationDirPath();
126     static QString applicationFilePath();
127     static qint64 applicationPid();
128
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
135
136 #ifndef QT_NO_TRANSLATION
137     static void installTranslator(QTranslator * messageFile);
138     static void removeTranslator(QTranslator * messageFile);
139 #endif
140     enum Encoding { UnicodeUTF8, Latin1, DefaultCodec = Latin1
141 #if QT_DEPRECATED_SINCE(5, 0)
142                     , CodecForTr = Latin1
143 #endif
144                   };
145     static QString translate(const char * context,
146                              const char * key,
147                              const char * disambiguation = 0,
148                              Encoding encoding = DefaultCodec,
149                              int n = -1);
150
151     static void flush();
152
153 #if defined(Q_OS_WIN)
154     virtual bool winEventFilter(MSG *message, long *result);
155 #endif
156
157 #if defined(Q_OS_UNIX)
158     static void watchUnixSignal(int signal, bool watch);
159 #endif
160
161     typedef bool (*EventFilter)(void *message, long *result);
162     EventFilter setEventFilter(EventFilter filter);
163     bool filterEvent(void *message, long *result);
164
165     static bool isQuitLockEnabled();
166     static void setQuitLockEnabled(bool enabled);
167
168 public Q_SLOTS:
169     static void quit();
170
171 Q_SIGNALS:
172     void aboutToQuit();
173     void unixSignal(int);
174
175 protected:
176     bool event(QEvent *);
177
178     virtual bool compressEvent(QEvent *, QObject *receiver, QPostEventList *);
179
180 protected:
181     QCoreApplication(QCoreApplicationPrivate &p);
182
183 private:
184     Q_PRIVATE_SLOT(d_func(), void _q_initializeProcessManager())
185     static bool sendSpontaneousEvent(QObject *receiver, QEvent *event);
186     bool notifyInternal(QObject *receiver, QEvent *event);
187
188     void init();
189
190     static QCoreApplication *self;
191
192     Q_DISABLE_COPY(QCoreApplication)
193
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;
207 };
208
209 inline bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
210 {  if (event) event->spont = false; return self ? self->notifyInternal(receiver, event) : false; }
211
212 inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
213 { if (event) event->spont = true; return self ? self->notifyInternal(receiver, event) : false; }
214
215 #ifdef QT_NO_TRANSLATION
216 // Simple versions
217 inline QString QCoreApplication::translate(const char *, const char *sourceText,
218                                            const char *, Encoding encoding)
219 {
220 #ifndef QT_NO_TEXTCODEC
221     if (encoding == UnicodeUTF8)
222         return QString::fromUtf8(sourceText);
223 #else
224     Q_UNUSED(encoding)
225 #endif
226     return QString::fromLatin1(sourceText);
227 }
228
229 // Simple versions
230 inline QString QCoreApplication::translate(const char *, const char *sourceText,
231                                            const char *, Encoding encoding, int)
232 {
233 #ifndef QT_NO_TEXTCODEC
234     if (encoding == UnicodeUTF8)
235         return QString::fromUtf8(sourceText);
236 #else
237     Q_UNUSED(encoding)
238 #endif
239     return QString::fromLatin1(sourceText);
240 }
241 #endif
242
243 #define Q_DECLARE_TR_FUNCTIONS(context) \
244 public: \
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); } \
251 private:
252
253 typedef void (*QtCleanUpFunction)();
254
255 Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction);
256 Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction);
257 Q_CORE_EXPORT QString qAppName();                // get application name
258
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 &);
262 #endif
263
264 QT_END_NAMESPACE
265
266 QT_END_HEADER
267
268 #endif // QCOREAPPLICATION_H