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