| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "systray.h" |
| 5 | |
| 6 | #include "app.h" |
| 7 | #include "core/iprofile.h" |
| 8 | #include "core/iprofilemanager.h" |
| 9 | #include "core/isession.h" |
| 10 | #include <QIcon> |
| 11 | #include <QTimer> |
| 12 | #include <algorithm> |
| 13 | |
| 14 | class SysTray::ProfileManagerObserver : public IProfileManager::Observer |
| 15 | { |
| 16 | public: |
| 17 | ProfileManagerObserver(SysTray &outer) noexcept |
| 18 | : outer_(outer) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | void profileAdded(std::string const &profileName) override; |
| 23 | void profileRemoved(std::string const &profileName) override; |
| 24 | void profileInfoChanged(IProfile::Info const &oldInfo, |
| 25 | IProfile::Info const &newInfo) override; |
| 26 | |
| 27 | void profileChanged(std::string const &) override |
| 28 | { |
| 29 | } |
| 30 | void profileActiveChanged(std::string const &, bool) override |
| 31 | { |
| 32 | } |
| 33 | void profileSaved(std::string const &) override |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | SysTray &outer_; |
| 39 | }; |
| 40 | |
| 41 | void SysTray::ProfileManagerObserver::profileAdded(std::string const &profileName) |
| 42 | { |
| 43 | outer_.profileAdded(profileName); |
| 44 | } |
| 45 | |
| 46 | void SysTray::ProfileManagerObserver::profileRemoved(std::string const &profileName) |
| 47 | { |
| 48 | outer_.profileRemoved(profileName); |
| 49 | } |
| 50 | |
| 51 | void SysTray::ProfileManagerObserver::profileInfoChanged( |
| 52 | IProfile::Info const &oldInfo, IProfile::Info const &newInfo) |
| 53 | { |
| 54 | outer_.profileInfoChanged(oldInfo, newInfo); |
| 55 | } |
| 56 | |
| 57 | class SysTray::ManualProfileObserver : public ISession::ManualProfileObserver |
| 58 | { |
| 59 | public: |
| 60 | ManualProfileObserver(SysTray &outer) noexcept |
| 61 | : outer_(outer) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void toggled(std::string const &profileName, bool active) override; |
| 66 | |
| 67 | private: |
| 68 | SysTray &outer_; |
| 69 | }; |
| 70 | |
| 71 | void SysTray::ManualProfileObserver::toggled(const std::string &profileName, |
| 72 | bool active) |
| 73 | { |
| 74 | outer_.manualProfileToggled(profileName, active); |
| 75 | } |
| 76 | |
| 77 | SysTray::SysTray(ISession *session, QObject *parent) |
| 78 | : QObject(parent) |
| 79 | , session_(session) |
| 80 | , profileManager_(&session->profileManager()) |
| 81 | , menu_() |
| 82 | , profileManagerObserver_( |
| 83 | std::make_shared<SysTray::ProfileManagerObserver>(*this)) |
| 84 | , manualProfileObserver_(std::make_shared<SysTray::ManualProfileObserver>(*this)) |
| 85 | { |
| 86 | session_->addManualProfileObserver(manualProfileObserver_); |
| 87 | profileManager_->addObserver(profileManagerObserver_); |
| 88 | |
| 89 | sysTray_ = createSystemTrayIcon(); |
| 90 | } |
| 91 | |
| 92 | bool SysTray::isAvailable() const |
| 93 | { |
| 94 | return QSystemTrayIcon::isSystemTrayAvailable(); |
| 95 | } |
| 96 | |
| 97 | bool SysTray::isVisible() const |
| 98 | { |
| 99 | return isAvailable() && sysTray_->isVisible(); |
| 100 | } |
| 101 | |
| 102 | void SysTray::show() |
| 103 | { |
| 104 | sysTray_->show(); |
| 105 | } |
| 106 | |
| 107 | void SysTray::hide() |
| 108 | { |
| 109 | sysTray_->hide(); |
| 110 | } |
| 111 | |
| 112 | void SysTray::settingChanged(QString const &key, QVariant const &value) |
| 113 | { |
| 114 | if (key == "sysTray") |
| 115 | sysTray_->setVisible(value.toBool()); |
| 116 | } |
| 117 | |
| 118 | void SysTray::onMainWindowVisibleChanged(bool isVisible) |
| 119 | { |
| 120 | showMainWindow_ = isVisible; |
| 121 | showMainWindowAction_->setText(isVisible ? tr("Hide") : tr("Show")); |
| 122 | } |
| 123 | |
| 124 | void SysTray::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason) |
| 125 | { |
| 126 | switch (reason) { |
| 127 | case QSystemTrayIcon::Trigger: |
| 128 | emit activated(); |
| 129 | break; |
| 130 | |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void SysTray::onShowMainWindowActionTriggered() |
| 137 | { |
| 138 | emit showMainWindowToggled(!showMainWindow_); |
| 139 | } |
| 140 | |
| 141 | QSystemTrayIcon *SysTray::createSystemTrayIcon() |
| 142 | { |
| 143 | auto sysTray = new QSystemTrayIcon(this); |
| 144 | |
| 145 | sysTray->setIcon(QIcon::fromTheme(QString(App::Name.data()).toLower())); |
| 146 | sysTray->setContextMenu(menu()); |
| 147 | |
| 148 | connect(sysTray, &QSystemTrayIcon::activated, this, |
| 149 | &SysTray::onTrayIconActivated); |
| 150 | |
| 151 | return sysTray; |
| 152 | } |
| 153 | |
| 154 | QMenu *SysTray::menu() |
| 155 | { |
| 156 | if (menu_.isEmpty()) { |
| 157 | showMainWindowAction_ = new QAction(&menu_); |
| 158 | onMainWindowVisibleChanged(false); // initialize label and state |
| 159 | connect(showMainWindowAction_, &QAction::triggered, this, |
| 160 | &SysTray::onShowMainWindowActionTriggered); |
| 161 | menu_.addAction(showMainWindowAction_); |
| 162 | menu_.addSeparator(); |
| 163 | |
| 164 | manualProfileMenu_ = menu_.addMenu(tr("Manual profiles")); |
| 165 | addManualProfilesTo(manualProfileMenu_); |
| 166 | menu_.addSeparator(); |
| 167 | |
| 168 | QAction *quitAction = new QAction(tr("Quit"), &menu_); |
| 169 | connect(quitAction, &QAction::triggered, this, &SysTray::quit); |
| 170 | menu_.addAction(quitAction); |
| 171 | } |
| 172 | |
| 173 | return &menu_; |
| 174 | } |
| 175 | |
| 176 | QAction *SysTray::createManualProfileAction(QMenu *menu, |
| 177 | std::string const &profileName) |
| 178 | { |
| 179 | |
| 180 | QAction *action = new QAction(QString::fromStdString(profileName), menu); |
| 181 | action->setCheckable(true); |
| 182 | |
| 183 | connect(action, &QAction::triggered, this, |
| 184 | [=, this]() { onManualProfileMenuTriggered(action->text()); }); |
| 185 | |
| 186 | return action; |
| 187 | } |
| 188 | |
| 189 | std::optional<QAction *> |
| 190 | SysTray::findManualProfileAction(std::string const &profileName) |
| 191 | { |
| 192 | auto name = QString::fromStdString(profileName); |
| 193 | auto actions = manualProfileMenu_->actions(); |
| 194 | |
| 195 | auto actionIt = std::find_if( |
| 196 | actions.begin(), actions.end(), |
| 197 | [&](QAction *action) { return action->text() == name; }); |
| 198 | |
| 199 | return actionIt != actions.end() ? std::make_optional(*actionIt) |
| 200 | : std::nullopt; |
| 201 | } |
| 202 | |
| 203 | QAction * |
| 204 | SysTray::findNextManualProfileActionPosition(std::string const &profileName) |
| 205 | { |
| 206 | auto name = QString::fromStdString(profileName); |
| 207 | auto actions = manualProfileMenu_->actions(); |
| 208 | |
| 209 | auto actionIt = std::find_if( |
| 210 | actions.begin(), actions.end(), |
| 211 | [&](QAction *action) { return action->text() > name; }); |
| 212 | |
| 213 | return actionIt != actions.end() ? *actionIt : nullptr; |
| 214 | } |
| 215 | |
| 216 | void SysTray::addManualProfilesTo(QMenu *menu) |
| 217 | { |
| 218 | auto profiles = profileManager_->profiles(); |
| 219 | std::sort(profiles.begin(), profiles.end()); |
| 220 | |
| 221 | for (auto const &profileName : profiles) { |
| 222 | |
| 223 | auto const &profile = profileManager_->profile(profileName); |
| 224 | if (profile->get().info().exe == IProfile::Info::ManualID) { |
| 225 | |
| 226 | auto action = createManualProfileAction(menu, profileName); |
| 227 | menu->addAction(action); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | menu->setDisabled(menu->isEmpty()); |
| 232 | } |
| 233 | |
| 234 | void SysTray::onManualProfileMenuTriggered(QString const &profile) |
| 235 | { |
| 236 | session_->toggleManualProfile(profile.toStdString()); |
| 237 | } |
| 238 | |
| 239 | void SysTray::profileAdded(std::string const &profileName) |
| 240 | { |
| 241 | auto profile = profileManager_->profile(profileName); |
| 242 | if (profile.has_value() && |
| 243 | profile->get().info().exe == IProfile::Info::ManualID) { |
| 244 | |
| 245 | auto action = createManualProfileAction(manualProfileMenu_, profileName); |
| 246 | auto beforeAction = findNextManualProfileActionPosition(profileName); |
| 247 | manualProfileMenu_->insertAction(beforeAction, action); |
| 248 | manualProfileMenu_->setDisabled(false); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void SysTray::profileRemoved(std::string const &profileName) |
| 253 | { |
| 254 | auto action = findManualProfileAction(profileName); |
| 255 | if (action.has_value()) { |
| 256 | manualProfileMenu_->removeAction(*action); |
| 257 | delete *action; |
| 258 | |
| 259 | manualProfileMenu_->setDisabled(manualProfileMenu_->isEmpty()); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void SysTray::profileInfoChanged(IProfile::Info const &oldInfo, |
| 264 | IProfile::Info const &newInfo) |
| 265 | { |
| 266 | if (oldInfo.exe == IProfile::Info::ManualID || |
| 267 | newInfo.exe == IProfile::Info::ManualID) { |
| 268 | |
| 269 | // automatic profile converted to manual profile |
| 270 | if (oldInfo.exe != IProfile::Info::ManualID && |
| 271 | newInfo.exe == IProfile::Info::ManualID) { |
| 272 | auto action = createManualProfileAction(manualProfileMenu_, newInfo.name); |
| 273 | auto beforeAction = findNextManualProfileActionPosition(newInfo.name); |
| 274 | manualProfileMenu_->insertAction(beforeAction, action); |
| 275 | } |
| 276 | // manual profile converted to automatic profile |
| 277 | else if (oldInfo.exe == IProfile::Info::ManualID && |
| 278 | newInfo.exe != IProfile::Info::ManualID) { |
| 279 | profileRemoved(oldInfo.name); |
| 280 | } |
| 281 | // manual profile name has changed |
| 282 | else if (oldInfo.name != newInfo.name) { |
| 283 | auto action = findManualProfileAction(oldInfo.name); |
| 284 | if (action.has_value()) { |
| 285 | |
| 286 | // remove old action |
| 287 | auto isChecked = (*action)->isChecked(); |
| 288 | manualProfileMenu_->removeAction(*action); |
| 289 | delete *action; |
| 290 | |
| 291 | // insert a new action |
| 292 | auto action = createManualProfileAction(manualProfileMenu_, newInfo.name); |
| 293 | action->setChecked(isChecked); |
| 294 | auto beforeAction = findNextManualProfileActionPosition(newInfo.name); |
| 295 | manualProfileMenu_->insertAction(beforeAction, action); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | manualProfileMenu_->setDisabled(manualProfileMenu_->isEmpty()); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void SysTray::manualProfileToggled(std::string const &profileName, bool active) |
| 304 | { |
| 305 | auto action = findManualProfileAction(profileName); |
| 306 | if (action.has_value()) |
| 307 | (*action)->setChecked(active); |
| 308 | } |