| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "core/iprofile.h" |
| 7 | #include <QAction> |
| 8 | #include <QMenu> |
| 9 | #include <QObject> |
| 10 | #include <QString> |
| 11 | #include <QSystemTrayIcon> |
| 12 | #include <QVariant> |
| 13 | #include <memory> |
| 14 | #include <optional> |
| 15 | #include <string> |
| 16 | |
| 17 | class IProfileManager; |
| 18 | class ISession; |
| 19 | |
| 20 | class SysTray : public QObject |
| 21 | { |
| 22 | Q_OBJECT |
| 23 | public: |
| 24 | explicit SysTray(ISession *session, QObject *parent = nullptr); |
| 25 | |
| 26 | Q_INVOKABLE bool isAvailable() const; |
| 27 | Q_INVOKABLE bool isVisible() const; |
| 28 | |
| 29 | signals: |
| 30 | void activated(); |
| 31 | void quit(); |
| 32 | void showMainWindowToggled(bool visible); |
| 33 | |
| 34 | public slots: |
| 35 | void show(); |
| 36 | void hide(); |
| 37 | void settingChanged(QString const &key, QVariant const &value); |
| 38 | void onMainWindowVisibleChanged(bool isVisible); |
| 39 | |
| 40 | private slots: |
| 41 | void onTrayIconActivated(QSystemTrayIcon::ActivationReason reason); |
| 42 | void onShowMainWindowActionTriggered(); |
| 43 | |
| 44 | private: |
| 45 | void profileAdded(std::string const &profileName); |
| 46 | void profileRemoved(std::string const &profileName); |
| 47 | void profileInfoChanged(IProfile::Info const &oldInfo, |
| 48 | IProfile::Info const &newInfo); |
| 49 | void manualProfileToggled(std::string const &profileName, bool active); |
| 50 | |
| 51 | QSystemTrayIcon *createSystemTrayIcon(); |
| 52 | QMenu *menu(); |
| 53 | QAction *createManualProfileAction(QMenu *menu, std::string const &profileName); |
| 54 | std::optional<QAction *> findManualProfileAction(std::string const &profileName); |
| 55 | QAction *findNextManualProfileActionPosition(std::string const &profileName); |
| 56 | void addManualProfilesTo(QMenu *menu); |
| 57 | void onManualProfileMenuTriggered(QString const &profile); |
| 58 | |
| 59 | ISession *session_; |
| 60 | IProfileManager *profileManager_; |
| 61 | QSystemTrayIcon *sysTray_{nullptr}; |
| 62 | QMenu menu_; |
| 63 | QMenu *manualProfileMenu_{nullptr}; |
| 64 | QAction *showMainWindowAction_{nullptr}; |
| 65 | bool showMainWindow_; |
| 66 | |
| 67 | class ProfileManagerObserver; |
| 68 | std::shared_ptr<ProfileManagerObserver> profileManagerObserver_; |
| 69 | |
| 70 | class ManualProfileObserver; |
| 71 | std::shared_ptr<ManualProfileObserver> manualProfileObserver_; |
| 72 | }; |