| 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 "iprofilemanager.h" |
| 8 | #include "iprofilestorage.h" |
| 9 | #include <memory> |
| 10 | #include <mutex> |
| 11 | #include <string> |
| 12 | #include <unordered_map> |
| 13 | #include <unordered_set> |
| 14 | |
| 15 | class ProfileManager final : public IProfileManager |
| 16 | { |
| 17 | public: |
| 18 | ProfileManager(std::unique_ptr<IProfile> &&defaultProfile, |
| 19 | std::unique_ptr<IProfileStorage> &&profileStorage) noexcept; |
| 20 | |
| 21 | void addObserver(std::shared_ptr<IProfileManager::Observer> observer) override; |
| 22 | void removeObserver( |
| 23 | std::shared_ptr<IProfileManager::Observer> const &observer) override; |
| 24 | |
| 25 | void init(ISysModel const &model) override; |
| 26 | |
| 27 | std::vector<std::string> profiles() const override; |
| 28 | std::optional<std::reference_wrapper<IProfile const>> |
| 29 | profile(std::string const &profileName) const override; |
| 30 | |
| 31 | std::vector<std::string> unsavedProfiles() const override; |
| 32 | bool unsaved(std::string const &profileName) const override; |
| 33 | |
| 34 | void add(IProfile::Info const &info) override; |
| 35 | void clone(IProfile::Info const &cloneInfo, |
| 36 | std::string const &baseProfileName = |
| 37 | std::string(IProfile::Info::GlobalID)) override; |
| 38 | void remove(std::string const &profileName) override; |
| 39 | void activate(std::string const &profileName, bool active) override; |
| 40 | void reset(std::string const &profileName) override; |
| 41 | void restore(std::string const &profileName) override; |
| 42 | void update(std::string const &profileName, |
| 43 | IProfile::Info const &newInfo) override; |
| 44 | void update(std::string const &profileName, |
| 45 | Importable::Importer &importer) override; |
| 46 | bool loadFrom(std::string const &profileName, |
| 47 | std::filesystem::path const &path) override; |
| 48 | void save(std::string const &profileName) override; |
| 49 | bool exportTo(std::string const &profileName, |
| 50 | std::filesystem::path const &path) override; |
| 51 | |
| 52 | private: |
| 53 | void notifyProfileAdded(std::string const &profileName); |
| 54 | void notifyProfileRemoved(std::string const &profileName); |
| 55 | void notifyProfileChanged(std::string const &profileName); |
| 56 | void notifyProfileActiveChanged(std::string const &profileName, bool active); |
| 57 | void notifyProfileSaved(std::string const &profileName); |
| 58 | void notifyProfileInfoChanged(IProfile::Info const &oldInfo, |
| 59 | IProfile::Info const &newInfo); |
| 60 | |
| 61 | std::unique_ptr<IProfile> defaultProfile_; |
| 62 | std::unique_ptr<IProfileStorage> profileStorage_; |
| 63 | std::unordered_map<std::string, std::unique_ptr<IProfile>> profiles_; |
| 64 | std::unordered_set<std::string> unsavedProfiles_; |
| 65 | |
| 66 | std::vector<std::shared_ptr<IProfileManager::Observer>> observers_; |
| 67 | std::mutex obMutex_; |
| 68 | }; |