| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "iprofile.h" |
| 7 | #include "isession.h" |
| 8 | #include <deque> |
| 9 | #include <functional> |
| 10 | #include <memory> |
| 11 | #include <mutex> |
| 12 | #include <optional> |
| 13 | #include <string> |
| 14 | #include <unordered_map> |
| 15 | #include <vector> |
| 16 | |
| 17 | class IProfileApplicator; |
| 18 | class IProfileView; |
| 19 | class IProfileViewFactory; |
| 20 | class IHelperMonitor; |
| 21 | |
| 22 | class Session final : public ISession |
| 23 | { |
| 24 | public: |
| 25 | Session(std::shared_ptr<IProfileApplicator> profileApplicator, |
| 26 | std::unique_ptr<IProfileManager> &&profileManager, |
| 27 | std::unique_ptr<IProfileViewFactory> &&profileViewFactory, |
| 28 | std::unique_ptr<IHelperMonitor> &&helperMonitor, |
| 29 | bool logProfileStack = false) noexcept; |
| 30 | |
| 31 | void addManualProfileObserver( |
| 32 | std::shared_ptr<ISession::ManualProfileObserver> observer) override; |
| 33 | void removeManualProfileObserver( |
| 34 | std::shared_ptr<ISession::ManualProfileObserver> observer) override; |
| 35 | |
| 36 | void init(ISysModel const &model) override; |
| 37 | bool toggleManualProfile(std::string const &profileName) override; |
| 38 | bool activateManualProfile(std::string const &profileName) override; |
| 39 | bool deactivateManualProfile(std::string const &profileName) override; |
| 40 | IProfileManager &profileManager() const override; |
| 41 | |
| 42 | void logProfileStack(bool enable) override; |
| 43 | |
| 44 | private: |
| 45 | void profileAdded(std::string const &profileName); |
| 46 | void profileRemoved(std::string const &profileName); |
| 47 | void profileChanged(std::string const &profileName); |
| 48 | void profileActiveChanged(std::string const &profileName, bool active); |
| 49 | void profileSaved(std::string const &profileName); |
| 50 | void profileInfoChanged(IProfile::Info const &oldInfo, |
| 51 | IProfile::Info const &newInfo); |
| 52 | |
| 53 | void queueProfileViewForExecutable(std::string const &executableName); |
| 54 | void dequeueProfileViewForExecutable(std::string const &executableName); |
| 55 | |
| 56 | void populateProfileExeIndex(); |
| 57 | void watchProfiles(); |
| 58 | |
| 59 | void createProfileViews( |
| 60 | std::optional<std::reference_wrapper<IProfileView>> baseProfileView, |
| 61 | std::vector<std::string> const &profileNames); |
| 62 | |
| 63 | void queueProfileView(std::string const &profileName); |
| 64 | void dequeueProfileView(std::string const &profileName); |
| 65 | |
| 66 | void notifyManualProfileToggled(std::string const &profileName, bool active); |
| 67 | |
| 68 | void logProfileStack() const; |
| 69 | |
| 70 | std::shared_ptr<IProfileApplicator> profileApplicator_; |
| 71 | std::unique_ptr<IProfileManager> profileManager_; |
| 72 | std::unique_ptr<IProfileViewFactory> profileViewFactory_; |
| 73 | std::unique_ptr<IHelperMonitor> helperMonitor_; |
| 74 | |
| 75 | class ProfileManagerObserver; |
| 76 | std::shared_ptr<ProfileManagerObserver> const profileManagerObserver_; |
| 77 | |
| 78 | class HelperMonitorObserver; |
| 79 | std::shared_ptr<HelperMonitorObserver> const helperMonitorObserver_; |
| 80 | |
| 81 | std::optional<std::string> manualProfile_; |
| 82 | std::mutex manualProfileMutex_; |
| 83 | |
| 84 | std::deque<std::unique_ptr<IProfileView>> pViews_; |
| 85 | std::mutex pViewsMutex_; |
| 86 | |
| 87 | using executableName = std::string; |
| 88 | using profileName = std::string; |
| 89 | std::unordered_map<executableName, profileName> profileExeIndex_; |
| 90 | std::mutex profileExeIndexMutex_; |
| 91 | |
| 92 | std::vector<std::shared_ptr<ISession::ManualProfileObserver>> manualProfileObservers_; |
| 93 | std::mutex manualProfileObserversMutex_; |
| 94 | |
| 95 | bool logProfileStack_; |
| 96 | }; |