Ring Daemon
Loading...
Searching...
No Matches
preferenceservicesmanager.cpp
Go to the documentation of this file.
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
19
20#include "pluginmanager.h"
21
22#include <algorithm>
23
24namespace jami {
25
30
35
36std::vector<std::string>
38{
39 std::vector<std::string> res;
40 res.reserve(handlers_.size());
41 for (const auto& preferenceHandler : handlers_) {
42 res.emplace_back(std::to_string((uintptr_t) preferenceHandler.get()));
43 }
44 return res;
45}
46
47std::map<std::string, std::string>
49{
51 for (auto& preferenceHandler : handlers_) {
53 return preferenceHandler->getHandlerDetails();
54 }
55 }
56 return {};
57}
58
59bool
61 const std::string& value,
62 const std::string& rootPath,
63 const std::string& accountId)
64{
65 bool status {true};
66 for (auto& preferenceHandler : handlers_) {
67 if (preferenceHandler->id().find(rootPath) != std::string::npos) {
68 if (preferenceHandler->preferenceMapHasKey(key)) {
69 preferenceHandler->setPreferenceAttribute(accountId, key, value);
70 // We can return here since we expect plugins to have a single preferencehandler
71 return false;
72 }
73 }
74 }
75 return status;
76}
77
78void
79PreferenceServicesManager::resetPreferences(const std::string& rootPath, const std::string& accountId)
80{
81 for (auto& preferenceHandler : handlers_) {
82 if (preferenceHandler->id().find(rootPath) != std::string::npos) {
83 preferenceHandler->resetPreferenceAttributes(accountId);
84 }
85 }
86}
87
88void
89PreferenceServicesManager::registerComponentsLifeCycleManagers(PluginManager& pluginManager)
90{
91 // registerHandler may be called by the PluginManager upon loading a plugin.
92 auto registerHandler = [this](void* data, std::mutex& pmMtx_) {
93 std::lock_guard lk(pmMtx_);
94 PreferenceHandlerPtr ptr {(static_cast<PreferenceHandler*>(data))};
95
96 if (!ptr)
97 return -1;
98 handlers_.emplace_back(std::move(ptr));
99 return 0;
100 };
101
102 // unregisterHandler may be called by the PluginManager while unloading.
103 auto unregisterHandler = [this](void* data, std::mutex& pmMtx_) {
104 std::lock_guard lk(pmMtx_);
105 auto handlerIt = std::find_if(handlers_.begin(), handlers_.end(), [data](PreferenceHandlerPtr& handler) {
106 return (handler.get() == data);
107 });
108
109 if (handlerIt != handlers_.end()) {
110 handlers_.erase(handlerIt);
111 }
112 return true;
113 };
114
115 // Services are registered to the PluginManager.
116 pluginManager.registerComponentManager("PreferenceHandlerManager", registerHandler, unregisterHandler);
117}
118} // namespace jami
This class manages plugin (un)loading.
This abstract class is an API we need to implement from plugin side.
PreferenceServicesManager(PluginManager &pluginManager)
Constructor registers PreferenceHandler API services to the PluginManager instance.
void resetPreferences(const std::string &rootPath, const std::string &accountId)
Resets acc preferences to default values.
bool setPreference(const std::string &key, const std::string &value, const std::string &rootPath, const std::string &accountId)
Sets a preference.
std::vector< std::string > getHandlers() const
List all PreferenceHandlers available.
std::map< std::string, std::string > getHandlerDetails(const std::string &preferenceHandlerIdStr) const
Returns details Map from s implementation.
void emitSignal(Args... args)
Definition jami_signal.h:64
std::unique_ptr< PreferenceHandler > PreferenceHandlerPtr