| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "session.h" |
| 5 | |
| 6 | #include "helper/ihelpermonitor.h" |
| 7 | #include "iprofileapplicator.h" |
| 8 | #include "iprofilemanager.h" |
| 9 | #include "iprofileview.h" |
| 10 | #include "iprofileviewfactory.h" |
| 11 | #include <algorithm> |
| 12 | #include <format> |
| 13 | #include <iterator> |
| 14 | #include <spdlog/spdlog.h> |
| 15 | #include <sstream> |
| 16 | #include <utility> |
| 17 | |
| 18 | class Session::ProfileManagerObserver : public IProfileManager::Observer |
| 19 | { |
| 20 | public: |
| 21 | ProfileManagerObserver(Session &outer) noexcept |
| 22 | : outer_(outer) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | void profileAdded(std::string const &profileName) override; |
| 27 | void profileRemoved(std::string const &profileName) override; |
| 28 | void profileChanged(std::string const &profileName) override; |
| 29 | void profileActiveChanged(std::string const &profileName, bool active) override; |
| 30 | void profileSaved(std::string const &profileName) override; |
| 31 | void profileInfoChanged(IProfile::Info const &oldInfo, |
| 32 | IProfile::Info const &newInfo) override; |
| 33 | |
| 34 | private: |
| 35 | Session &outer_; |
| 36 | }; |
| 37 | |
| 38 | void Session::ProfileManagerObserver::profileAdded(std::string const &profileName) |
| 39 | { |
| 40 | outer_.profileAdded(profileName); |
| 41 | } |
| 42 | |
| 43 | void Session::ProfileManagerObserver::profileRemoved(std::string const &profileName) |
| 44 | { |
| 45 | outer_.profileRemoved(profileName); |
| 46 | } |
| 47 | |
| 48 | void Session::ProfileManagerObserver::profileChanged(std::string const &profileName) |
| 49 | { |
| 50 | outer_.profileChanged(profileName); |
| 51 | } |
| 52 | |
| 53 | void Session::ProfileManagerObserver::profileActiveChanged( |
| 54 | std::string const &profileName, bool active) |
| 55 | { |
| 56 | outer_.profileActiveChanged(profileName, active); |
| 57 | } |
| 58 | |
| 59 | void Session::ProfileManagerObserver::profileSaved(std::string const &profileName) |
| 60 | { |
| 61 | outer_.profileSaved(profileName); |
| 62 | } |
| 63 | |
| 64 | void Session::ProfileManagerObserver::profileInfoChanged( |
| 65 | IProfile::Info const &oldInfo, IProfile::Info const &newInfo) |
| 66 | { |
| 67 | outer_.profileInfoChanged(oldInfo, newInfo); |
| 68 | } |
| 69 | |
| 70 | class Session::HelperMonitorObserver : public IHelperMonitor::Observer |
| 71 | { |
| 72 | public: |
| 73 | HelperMonitorObserver(Session &outer) noexcept |
| 74 | : outer_(outer) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | void appExec(std::string appExe) override; |
| 79 | void appExit(std::string appExe) override; |
| 80 | |
| 81 | private: |
| 82 | Session &outer_; |
| 83 | }; |
| 84 | |
| 85 | void Session::HelperMonitorObserver::appExec(std::string appExe) |
| 86 | { |
| 87 | outer_.queueProfileViewForExecutable(appExe); |
| 88 | } |
| 89 | |
| 90 | void Session::HelperMonitorObserver::appExit(std::string appExe) |
| 91 | { |
| 92 | outer_.dequeueProfileViewForExecutable(appExe); |
| 93 | } |
| 94 | |
| 95 | Session::Session(std::shared_ptr<IProfileApplicator> profileApplicator, |
| 96 | std::unique_ptr<IProfileManager> &&profileManager, |
| 97 | std::unique_ptr<IProfileViewFactory> &&profileViewFactory, |
| 98 | std::unique_ptr<IHelperMonitor> &&helperMonitor, |
| 99 | bool logProfileStack) noexcept |
| 100 | : profileApplicator_(std::move(profileApplicator)) |
| 101 | , profileManager_(std::move(profileManager)) |
| 102 | , profileViewFactory_(std::move(profileViewFactory)) |
| 103 | , helperMonitor_(std::move(helperMonitor)) |
| 104 | , profileManagerObserver_( |
| 105 | std::make_shared<Session::ProfileManagerObserver>(*this)) |
| 106 | , helperMonitorObserver_(std::make_shared<Session::HelperMonitorObserver>(*this)) |
| 107 | , logProfileStack_(logProfileStack) |
| 108 | { |
| 109 | profileManager_->addObserver(profileManagerObserver_); |
| 110 | helperMonitor_->addObserver(helperMonitorObserver_); |
| 111 | } |
| 112 | |
| 113 | void Session::addManualProfileObserver( |
| 114 | std::shared_ptr<ISession::ManualProfileObserver> observer) |
| 115 | { |
| 116 | std::lock_guard<std::mutex> lock(manualProfileObserversMutex_); |
| 117 | auto const it = std::find(manualProfileObservers_.begin(), |
| 118 | manualProfileObservers_.end(), observer); |
| 119 | if (it == manualProfileObservers_.cend()) |
| 120 | manualProfileObservers_.emplace_back(std::move(observer)); |
| 121 | } |
| 122 | |
| 123 | void Session::removeManualProfileObserver( |
| 124 | std::shared_ptr<ISession::ManualProfileObserver> observer) |
| 125 | { |
| 126 | std::lock_guard<std::mutex> lock(manualProfileObserversMutex_); |
| 127 | std::erase(manualProfileObservers_, observer); |
| 128 | } |
| 129 | |
| 130 | void Session::init(ISysModel const &model) |
| 131 | { |
| 132 | profileManager_->init(model); |
| 133 | |
| 134 | populateProfileExeIndex(); |
| 135 | createProfileViews({}, {std::string(IProfile::Info::GlobalID)}); |
| 136 | profileApplicator_->apply(*pViews_.back()); |
| 137 | |
| 138 | helperMonitor_->init(); |
| 139 | watchProfiles(); |
| 140 | |
| 141 | logProfileStack(); |
| 142 | } |
| 143 | |
| 144 | bool Session::toggleManualProfile(std::string const &profileName) |
| 145 | { |
| 146 | auto profile = profileManager_->profile(profileName); |
| 147 | if (!profile || profile->get().info().exe != IProfile::Info::ManualID) |
| 148 | return false; |
| 149 | |
| 150 | std::lock_guard<std::mutex> lock(pViewsMutex_); |
| 151 | std::lock_guard<std::mutex> mLock(manualProfileMutex_); |
| 152 | |
| 153 | // remove the profile view of the current active manual profile |
| 154 | if (manualProfile_.has_value()) { |
| 155 | pViews_.pop_back(); |
| 156 | notifyManualProfileToggled(*manualProfile_, false); |
| 157 | } |
| 158 | |
| 159 | // update manual profile state |
| 160 | if (manualProfile_.has_value() && manualProfile_ == profileName) |
| 161 | manualProfile_ = std::nullopt; |
| 162 | else |
| 163 | manualProfile_ = profileName; |
| 164 | |
| 165 | // create the profile view of the manual profile |
| 166 | if (manualProfile_.has_value()) { |
| 167 | createProfileViews(*pViews_.back(), {*manualProfile_}); |
| 168 | notifyManualProfileToggled(*manualProfile_, true); |
| 169 | } |
| 170 | |
| 171 | // apply active profile view |
| 172 | profileApplicator_->apply(*pViews_.back()); |
| 173 | |
| 174 | logProfileStack(); |
| 175 | |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | bool Session::activateManualProfile(std::string const &profileName) |
| 180 | { |
| 181 | auto profile = profileManager_->profile(profileName); |
| 182 | if (!profile || profile->get().info().exe != IProfile::Info::ManualID) |
| 183 | return false; |
| 184 | |
| 185 | std::lock_guard<std::mutex> mLock(manualProfileMutex_); |
| 186 | if (manualProfile_.has_value() && manualProfile_ == profileName) |
| 187 | return true; // NOOP |
| 188 | |
| 189 | std::lock_guard<std::mutex> lock(pViewsMutex_); |
| 190 | |
| 191 | // remove the profile view of the current active manual profile |
| 192 | if (manualProfile_.has_value()) { |
| 193 | pViews_.pop_back(); |
| 194 | notifyManualProfileToggled(*manualProfile_, false); |
| 195 | } |
| 196 | |
| 197 | // update manual profile state |
| 198 | manualProfile_ = profileName; |
| 199 | |
| 200 | // create the profile view of the manual profile |
| 201 | createProfileViews(*pViews_.back(), {*manualProfile_}); |
| 202 | notifyManualProfileToggled(*manualProfile_, true); |
| 203 | |
| 204 | // apply active profile view |
| 205 | profileApplicator_->apply(*pViews_.back()); |
| 206 | |
| 207 | logProfileStack(); |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | bool Session::deactivateManualProfile(std::string const &profileName) |
| 213 | { |
| 214 | auto profile = profileManager_->profile(profileName); |
| 215 | if (!profile || profile->get().info().exe != IProfile::Info::ManualID) |
| 216 | return false; |
| 217 | |
| 218 | std::lock_guard<std::mutex> mLock(manualProfileMutex_); |
| 219 | if (!(manualProfile_.has_value() && manualProfile_ == profileName)) |
| 220 | return true; // NOOP |
| 221 | |
| 222 | // remove profile view of the manual profile |
| 223 | pViews_.pop_back(); |
| 224 | notifyManualProfileToggled(*manualProfile_, false); |
| 225 | |
| 226 | // update manual profile state |
| 227 | manualProfile_ = std::nullopt; |
| 228 | |
| 229 | std::lock_guard<std::mutex> lock(pViewsMutex_); |
| 230 | |
| 231 | // apply active profile view |
| 232 | profileApplicator_->apply(*pViews_.back()); |
| 233 | |
| 234 | logProfileStack(); |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | IProfileManager &Session::profileManager() const |
| 240 | { |
| 241 | return *profileManager_; |
| 242 | } |
| 243 | |
| 244 | void Session::logProfileStack(bool enable) |
| 245 | { |
| 246 | logProfileStack_ = enable; |
| 247 | } |
| 248 | |
| 249 | void Session::profileAdded(std::string const &profileName) |
| 250 | { |
| 251 | auto profile = profileManager_->profile(profileName); |
| 252 | if (profile.has_value() && profile->get().active() && |
| 253 | profile->get().info().exe != IProfile::Info::ManualID) { |
| 254 | |
| 255 | auto const &exe = profile->get().info().exe; |
| 256 | std::lock_guard<std::mutex> lock(profileExeIndexMutex_); |
| 257 | |
| 258 | if (profileExeIndex_.find(exe) == profileExeIndex_.cend()) { |
| 259 | profileExeIndex_.emplace(exe, profileName); |
| 260 | helperMonitor_->watchApp(exe); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void Session::profileRemoved(std::string const &profileName) |
| 266 | { |
| 267 | bool isManual = false; |
| 268 | { |
| 269 | std::lock_guard<std::mutex> lock(manualProfileMutex_); |
| 270 | if (manualProfile_ == profileName) { |
| 271 | manualProfile_ = std::nullopt; |
| 272 | isManual = true; |
| 273 | notifyManualProfileToggled(profileName, false); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (!isManual) { // remove from profile executable index |
| 278 | std::lock_guard<std::mutex> lock(profileExeIndexMutex_); |
| 279 | |
| 280 | auto const profileIndexIter = std::find_if( |
| 281 | profileExeIndex_.cbegin(), profileExeIndex_.cend(), |
| 282 | [&](auto const &indexItem) { return indexItem.second == profileName; }); |
| 283 | |
| 284 | if (profileIndexIter != profileExeIndex_.cend()) { |
| 285 | helperMonitor_->forgetApp(profileIndexIter->first); |
| 286 | profileExeIndex_.erase(profileIndexIter); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | dequeueProfileView(profileName); |
| 291 | } |
| 292 | |
| 293 | void Session::profileChanged(std::string const &profileName) |
| 294 | { |
| 295 | std::lock_guard<std::mutex> lock(pViewsMutex_); |
| 296 | |
| 297 | auto const profileViewIter = std::find_if( |
| 298 | pViews_.cbegin(), pViews_.cend(), |
| 299 | [&](auto const &pv) { return pv->name() == profileName; }); |
| 300 | |
| 301 | if (profileViewIter != pViews_.cend()) { |
| 302 | // compute a list with the names of profile views to recreate |
| 303 | std::vector<std::string> pViewsToRecreate; |
| 304 | pViewsToRecreate.reserve(pViews_.size()); |
| 305 | std::transform(profileViewIter, pViews_.cend(), |
| 306 | std::back_inserter(pViewsToRecreate), |
| 307 | [](auto const &pv) { return pv->name(); }); |
| 308 | |
| 309 | // remove outdated profile views |
| 310 | pViews_.erase(profileViewIter, pViews_.cend()); |
| 311 | |
| 312 | // recreate the list of profile views |
| 313 | createProfileViews( |
| 314 | pViews_.empty() |
| 315 | ? std::nullopt |
| 316 | : std::make_optional<std::reference_wrapper<IProfileView>>( |
| 317 | *pViews_.back()), |
| 318 | pViewsToRecreate); |
| 319 | |
| 320 | // apply active profile view |
| 321 | profileApplicator_->apply(*pViews_.back()); |
| 322 | |
| 323 | logProfileStack(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void Session::profileActiveChanged(std::string const &profileName, bool active) |
| 328 | { |
| 329 | auto profile = profileManager_->profile(profileName); |
| 330 | if (profile.has_value() && |
| 331 | profile->get().info().exe != IProfile::Info::ManualID) { |
| 332 | if (active) |
| 333 | profileAdded(profileName); |
| 334 | else |
| 335 | profileRemoved(profileName); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void Session::profileSaved(std::string const &) |
| 340 | { |
| 341 | } |
| 342 | |
| 343 | void Session::profileInfoChanged(IProfile::Info const &oldInfo, |
| 344 | IProfile::Info const &newInfo) |
| 345 | { |
| 346 | if (oldInfo.exe != newInfo.exe || oldInfo.name != newInfo.name) { |
| 347 | |
| 348 | // sync profile executable index |
| 349 | { |
| 350 | std::lock_guard<std::mutex> lock(profileExeIndexMutex_); |
| 351 | |
| 352 | profileExeIndex_.erase(oldInfo.exe); |
| 353 | if (newInfo.exe != IProfile::Info::ManualID) |
| 354 | profileExeIndex_.emplace(newInfo.exe, newInfo.name); |
| 355 | } |
| 356 | |
| 357 | // update monitor |
| 358 | if (oldInfo.exe != newInfo.exe) { |
| 359 | if (oldInfo.exe != IProfile::Info::ManualID) |
| 360 | helperMonitor_->forgetApp(oldInfo.exe); |
| 361 | |
| 362 | if (newInfo.exe != IProfile::Info::ManualID) |
| 363 | helperMonitor_->watchApp(newInfo.exe); |
| 364 | } |
| 365 | |
| 366 | // handle active manual profile |
| 367 | if (oldInfo.exe == IProfile::Info::ManualID) { |
| 368 | std::lock_guard<std::mutex> lock(manualProfileMutex_); |
| 369 | |
| 370 | if (oldInfo.name == manualProfile_) { |
| 371 | // the manual profile has been turned into an automatic profile |
| 372 | if (newInfo.exe != IProfile::Info::ManualID) |
| 373 | manualProfile_ = std::nullopt; |
| 374 | else // only the profile name has changed |
| 375 | manualProfile_ = newInfo.name; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // handle profile view |
| 380 | { |
| 381 | std::lock_guard<std::mutex> lock(pViewsMutex_); |
| 382 | |
| 383 | // find the profile view |
| 384 | auto profileViewIter = std::find_if( |
| 385 | pViews_.cbegin(), pViews_.cend(), |
| 386 | [&](auto const &pv) { return pv->name() == oldInfo.name; }); |
| 387 | |
| 388 | if (profileViewIter != pViews_.end()) { |
| 389 | std::vector<std::string> pViewsToRecreate; |
| 390 | pViewsToRecreate.reserve(pViews_.size()); |
| 391 | |
| 392 | // recreate the profile view when only its name has been changed |
| 393 | if (oldInfo.exe == newInfo.exe && oldInfo.name != newInfo.name) |
| 394 | pViewsToRecreate.push_back(newInfo.name); |
| 395 | |
| 396 | // compute a list with the names of profile views to recreate |
| 397 | auto nextProfileViewIter = std::next(profileViewIter); |
| 398 | if (nextProfileViewIter != pViews_.end()) { |
| 399 | std::transform(nextProfileViewIter, pViews_.cend(), |
| 400 | std::back_inserter(pViewsToRecreate), |
| 401 | [](auto const &pv) { return pv->name(); }); |
| 402 | } |
| 403 | |
| 404 | // remove affected the profile views |
| 405 | pViews_.erase(profileViewIter, pViews_.cend()); |
| 406 | |
| 407 | // recreate the list of profile views |
| 408 | createProfileViews(*pViews_.back(), pViewsToRecreate); |
| 409 | |
| 410 | // apply active profile view |
| 411 | profileApplicator_->apply(*pViews_.back()); |
| 412 | } |
| 413 | |
| 414 | logProfileStack(); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void Session::queueProfileViewForExecutable(std::string const &executableName) |
| 420 | { |
| 421 | std::string profileName; |
| 422 | { |
| 423 | std::lock_guard<std::mutex> lock(profileExeIndexMutex_); |
| 424 | |
| 425 | auto const profileIndexNameIter = profileExeIndex_.find(executableName); |
| 426 | if (profileIndexNameIter != profileExeIndex_.cend()) |
| 427 | profileName = profileIndexNameIter->second; |
| 428 | } |
| 429 | |
| 430 | queueProfileView(profileName); |
| 431 | } |
| 432 | |
| 433 | void Session::dequeueProfileViewForExecutable(std::string const &executableName) |
| 434 | { |
| 435 | std::string profileName; |
| 436 | { |
| 437 | std::lock_guard<std::mutex> lock(profileExeIndexMutex_); |
| 438 | |
| 439 | auto const profileIndexNameIter = profileExeIndex_.find(executableName); |
| 440 | if (profileIndexNameIter != profileExeIndex_.cend()) |
| 441 | profileName = profileIndexNameIter->second; |
| 442 | } |
| 443 | |
| 444 | dequeueProfileView(profileName); |
| 445 | } |
| 446 | |
| 447 | void Session::populateProfileExeIndex() |
| 448 | { |
| 449 | auto profiles = profileManager_->profiles(); |
| 450 | for (auto &profileName : profiles) { |
| 451 | auto profile = profileManager_->profile(profileName); |
| 452 | auto info = profile->get().info(); |
| 453 | if (profile->get().active() && info.exe != IProfile::Info::ManualID) |
| 454 | profileExeIndex_.emplace(info.exe, std::move(profileName)); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void Session::watchProfiles() |
| 459 | { |
| 460 | for (auto const &[exe, name] : profileExeIndex_) { |
| 461 | if (exe != IProfile::Info::GlobalID && exe != IProfile::Info::ManualID) |
| 462 | helperMonitor_->watchApp(exe); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void Session::createProfileViews( |
| 467 | std::optional<std::reference_wrapper<IProfileView>> baseProfileView, |
| 468 | std::vector<std::string> const &profileNames) |
| 469 | { |
| 470 | for (auto const &profileName : profileNames) { |
| 471 | auto profile = profileManager_->profile(profileName); |
| 472 | if (profile.has_value()) { |
| 473 | auto profileView = profileViewFactory_->build(*profile, baseProfileView); |
| 474 | // use the new view as the base for the next profile view |
| 475 | baseProfileView = *profileView; |
| 476 | pViews_.emplace_back(std::move(profileView)); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void Session::queueProfileView(std::string const &profileName) |
| 482 | { |
| 483 | // compute a list of profile views to create including the profile |
| 484 | std::vector<std::string> pViewsToRecreate{profileName}; |
| 485 | |
| 486 | std::lock_guard<std::mutex> lock(pViewsMutex_); |
| 487 | std::lock_guard<std::mutex> mLock(manualProfileMutex_); |
| 488 | |
| 489 | // recreate active manual profile view |
| 490 | if (manualProfile_.has_value()) { |
| 491 | pViewsToRecreate.push_back(*manualProfile_); |
| 492 | pViews_.pop_back(); |
| 493 | } |
| 494 | |
| 495 | createProfileViews(*pViews_.back(), pViewsToRecreate); |
| 496 | |
| 497 | // apply active profile view |
| 498 | profileApplicator_->apply(*pViews_.back()); |
| 499 | |
| 500 | logProfileStack(); |
| 501 | } |
| 502 | |
| 503 | void Session::dequeueProfileView(std::string const &profileName) |
| 504 | { |
| 505 | std::lock_guard<std::mutex> lock(pViewsMutex_); |
| 506 | |
| 507 | auto profileViewIter = std::find_if( |
| 508 | pViews_.cbegin(), pViews_.cend(), |
| 509 | [&](auto const &pv) { return pv->name() == profileName; }); |
| 510 | |
| 511 | if (profileViewIter != pViews_.end()) { |
| 512 | auto nextProfileView = std::next(profileViewIter); |
| 513 | |
| 514 | // compute a list with the names of profile views to recreate |
| 515 | std::vector<std::string> pViewsToRecreate; |
| 516 | if (nextProfileView != pViews_.end()) { |
| 517 | pViewsToRecreate.reserve(pViews_.size()); |
| 518 | std::transform(nextProfileView, pViews_.cend(), |
| 519 | std::back_inserter(pViewsToRecreate), |
| 520 | [](auto const &pv) { return pv->name(); }); |
| 521 | } |
| 522 | |
| 523 | // remove the profile view and the outdated profile views |
| 524 | pViews_.erase(profileViewIter, pViews_.cend()); |
| 525 | |
| 526 | // recreate the list of profile views |
| 527 | createProfileViews(*pViews_.back(), pViewsToRecreate); |
| 528 | |
| 529 | // apply active profile view |
| 530 | profileApplicator_->apply(*pViews_.back()); |
| 531 | |
| 532 | logProfileStack(); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | void Session::notifyManualProfileToggled(std::string const &profileName, |
| 537 | bool active) |
| 538 | { |
| 539 | std::lock_guard<std::mutex> lock(manualProfileObserversMutex_); |
| 540 | for (auto &o : manualProfileObservers_) |
| 541 | o->toggled(profileName, active); |
| 542 | } |
| 543 | |
| 544 | void Session::logProfileStack() const |
| 545 | { |
| 546 | if (logProfileStack_) { |
| 547 | |
| 548 | std::ostringstream os; |
| 549 | for (auto &view : pViews_) |
| 550 | os << std::format("> {} ", view->name()); |
| 551 | |
| 552 | SPDLOG_INFO("{}", os.str()); |
| 553 | } |
| 554 | } |