| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "config.h" |
| 7 | #include "singleinstance.h" |
| 8 | #include <QCommandLineParser> |
| 9 | #include <QObject> |
| 10 | #include <QRect> |
| 11 | #include <QString> |
| 12 | #include <QStringList> |
| 13 | #include <QTimer> |
| 14 | #include <QVariant> |
| 15 | #include <memory> |
| 16 | #include <string_view> |
| 17 | |
| 18 | class IHelperControl; |
| 19 | class ISession; |
| 20 | class ISysModelSyncer; |
| 21 | class IUIFactory; |
| 22 | class QApplication; |
| 23 | class QQmlApplicationEngine; |
| 24 | class QQuickWindow; |
| 25 | class QTranslator; |
| 26 | class Settings; |
| 27 | class SysTray; |
| 28 | |
| 29 | class AppInfo final : public QObject |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | Q_PROPERTY(QString name READ getName CONSTANT) |
| 33 | Q_PROPERTY(QString version READ getVersion CONSTANT) |
| 34 | |
| 35 | public: |
| 36 | AppInfo(std::string_view appName, std::string_view appVersion) noexcept |
| 37 | : QObject() |
| 38 | , name_(appName.data()) |
| 39 | , version_(appVersion.data()) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | QString const &getName() const |
| 44 | { |
| 45 | return name_; |
| 46 | } |
| 47 | |
| 48 | QString const &getVersion() const |
| 49 | { |
| 50 | return version_; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | QString const name_; |
| 55 | QString const version_; |
| 56 | }; |
| 57 | |
| 58 | class App final : public QObject |
| 59 | { |
| 60 | Q_OBJECT |
| 61 | |
| 62 | public: |
| 63 | static constexpr std::string_view Name{PROJECT_NAME}; |
| 64 | static constexpr std::string_view VersionStr{PROJECT_VERSION}; |
| 65 | static constexpr std::string_view Fqdn{PROJECT_FQDN}; |
| 66 | static constexpr QRect DefaultWindowGeometry{0, 0, 970, 600}; |
| 67 | |
| 68 | App() noexcept; |
| 69 | ~App(); |
| 70 | |
| 71 | int exec(int argc, char **argv); |
| 72 | |
| 73 | private slots: |
| 74 | void exit(); |
| 75 | void showMainWindow(bool show); |
| 76 | void onNewInstance(QStringList args); |
| 77 | void onSysTrayActivated(); |
| 78 | void onSettingChanged(QString const &key, QVariant const &value); |
| 79 | void saveMainWindowGeometry(); |
| 80 | |
| 81 | private: |
| 82 | std::unique_ptr<QApplication> createApplication(int &argc, char **argv); |
| 83 | bool buildComponents(QQmlApplicationEngine &qmlEngine, int helperTimeout, |
| 84 | bool logCommands, bool logProfileStack); |
| 85 | void buildUI(std::unique_ptr<IUIFactory> &&uiFactory, |
| 86 | QQmlApplicationEngine &qmlEngine); |
| 87 | void loadTranslation(QApplication &app, QTranslator &translator); |
| 88 | int helperTimeout(int defaultTimeout) const; |
| 89 | void setupCmdParser(QApplication &app, int defaultHelperTimeout); |
| 90 | void setupSysTrayWindowState(); |
| 91 | void setupMainWindowGeometry(); |
| 92 | void restoreMainWindowGeometry(); |
| 93 | bool handleLoggingCmds(); |
| 94 | bool handleManualProfileCmd(); |
| 95 | bool handleWindowVisibilityCmds(); |
| 96 | |
| 97 | AppInfo appInfo_; |
| 98 | SingleInstance singleInstance_; |
| 99 | QCommandLineParser cmdParser_; |
| 100 | QTimer geometrySaveTimer_; |
| 101 | |
| 102 | std::unique_ptr<IHelperControl> helperControl_; |
| 103 | std::shared_ptr<ISysModelSyncer> sysSyncer_; |
| 104 | std::unique_ptr<ISession> session_; |
| 105 | std::unique_ptr<Settings> settings_; |
| 106 | |
| 107 | bool noop_{false}; |
| 108 | QQuickWindow *mainWindow_{nullptr}; |
| 109 | SysTray *sysTray_{nullptr}; |
| 110 | }; |