Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "preferenceservicesmanager.h"
19 :
20 : #include "pluginmanager.h"
21 :
22 : #include <algorithm>
23 :
24 : namespace jami {
25 :
26 32 : PreferenceServicesManager::PreferenceServicesManager(PluginManager& pluginManager)
27 : {
28 32 : registerComponentsLifeCycleManagers(pluginManager);
29 32 : }
30 :
31 32 : PreferenceServicesManager::~PreferenceServicesManager()
32 : {
33 32 : handlers_.clear();
34 32 : }
35 :
36 : std::vector<std::string>
37 0 : PreferenceServicesManager::getHandlers() const
38 : {
39 0 : std::vector<std::string> res;
40 0 : res.reserve(handlers_.size());
41 0 : for (const auto& preferenceHandler : handlers_) {
42 0 : res.emplace_back(std::to_string((uintptr_t) preferenceHandler.get()));
43 : }
44 0 : return res;
45 0 : }
46 :
47 : std::map<std::string, std::string>
48 0 : PreferenceServicesManager::getHandlerDetails(const std::string& preferenceHandlerIdStr) const
49 : {
50 0 : auto preferenceHandlerId = std::stoull(preferenceHandlerIdStr);
51 0 : for (auto& preferenceHandler : handlers_) {
52 0 : if ((uintptr_t) preferenceHandler.get() == preferenceHandlerId) {
53 0 : return preferenceHandler->getHandlerDetails();
54 : }
55 : }
56 0 : return {};
57 : }
58 :
59 : bool
60 0 : PreferenceServicesManager::setPreference(const std::string& key,
61 : const std::string& value,
62 : const std::string& rootPath,
63 : const std::string& accountId)
64 : {
65 0 : bool status {true};
66 0 : for (auto& preferenceHandler : handlers_) {
67 0 : if (preferenceHandler->id().find(rootPath) != std::string::npos) {
68 0 : if (preferenceHandler->preferenceMapHasKey(key)) {
69 0 : preferenceHandler->setPreferenceAttribute(accountId, key, value);
70 : // We can return here since we expect plugins to have a single preferencehandler
71 0 : return false;
72 : }
73 : }
74 : }
75 0 : return status;
76 : }
77 :
78 : void
79 0 : PreferenceServicesManager::resetPreferences(const std::string& rootPath, const std::string& accountId)
80 : {
81 0 : for (auto& preferenceHandler : handlers_) {
82 0 : if (preferenceHandler->id().find(rootPath) != std::string::npos) {
83 0 : preferenceHandler->resetPreferenceAttributes(accountId);
84 : }
85 : }
86 0 : }
87 :
88 : void
89 32 : PreferenceServicesManager::registerComponentsLifeCycleManagers(PluginManager& pluginManager)
90 : {
91 : // registerHandler may be called by the PluginManager upon loading a plugin.
92 0 : auto registerHandler = [this](void* data, std::mutex& pmMtx_) {
93 0 : std::lock_guard lk(pmMtx_);
94 0 : PreferenceHandlerPtr ptr {(static_cast<PreferenceHandler*>(data))};
95 :
96 0 : if (!ptr)
97 0 : return -1;
98 0 : handlers_.emplace_back(std::move(ptr));
99 0 : return 0;
100 0 : };
101 :
102 : // unregisterHandler may be called by the PluginManager while unloading.
103 0 : auto unregisterHandler = [this](void* data, std::mutex& pmMtx_) {
104 0 : std::lock_guard lk(pmMtx_);
105 0 : auto handlerIt = std::find_if(handlers_.begin(), handlers_.end(), [data](PreferenceHandlerPtr& handler) {
106 0 : return (handler.get() == data);
107 : });
108 :
109 0 : if (handlerIt != handlers_.end()) {
110 0 : handlers_.erase(handlerIt);
111 : }
112 0 : return true;
113 0 : };
114 :
115 : // Services are registered to the PluginManager.
116 96 : pluginManager.registerComponentManager("PreferenceHandlerManager", registerHandler, unregisterHandler);
117 32 : }
118 : } // namespace jami
|