Line data Source code
1 : /* 2 : * Copyright (C) 2004-2025 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 : #include "pluginpreferencesutils.h" 22 : 23 : #include "manager.h" 24 : #include "sip/sipcall.h" 25 : #include "fileutils.h" 26 : #include "logger.h" 27 : 28 : namespace jami { 29 : 30 32 : PreferenceServicesManager::PreferenceServicesManager(PluginManager& pluginManager) 31 : { 32 32 : registerComponentsLifeCycleManagers(pluginManager); 33 32 : } 34 : 35 32 : PreferenceServicesManager::~PreferenceServicesManager() 36 : { 37 32 : handlers_.clear(); 38 32 : } 39 : 40 : std::vector<std::string> 41 0 : PreferenceServicesManager::getHandlers() const 42 : { 43 0 : std::vector<std::string> res; 44 0 : res.reserve(handlers_.size()); 45 0 : for (const auto& preferenceHandler : handlers_) { 46 0 : res.emplace_back(std::to_string((uintptr_t) preferenceHandler.get())); 47 : } 48 0 : return res; 49 0 : } 50 : 51 : std::map<std::string, std::string> 52 0 : PreferenceServicesManager::getHandlerDetails(const std::string& preferenceHandlerIdStr) const 53 : { 54 0 : auto preferenceHandlerId = std::stoull(preferenceHandlerIdStr); 55 0 : for (auto& preferenceHandler : handlers_) { 56 0 : if ((uintptr_t) preferenceHandler.get() == preferenceHandlerId) { 57 0 : return preferenceHandler->getHandlerDetails(); 58 : } 59 : } 60 0 : return {}; 61 : } 62 : 63 : bool 64 0 : PreferenceServicesManager::setPreference(const std::string& key, 65 : const std::string& value, 66 : const std::string& rootPath, 67 : const std::string& accountId) 68 : { 69 0 : bool status {true}; 70 0 : for (auto& preferenceHandler : handlers_) { 71 0 : if (preferenceHandler->id().find(rootPath) != std::string::npos) { 72 0 : if (preferenceHandler->preferenceMapHasKey(key)) { 73 0 : preferenceHandler->setPreferenceAttribute(accountId, key, value); 74 : // We can return here since we expect plugins to have a single preferencehandler 75 0 : return false; 76 : } 77 : } 78 : } 79 0 : return status; 80 : } 81 : 82 : void 83 0 : PreferenceServicesManager::resetPreferences(const std::string& rootPath, const std::string& accountId) 84 : { 85 0 : for (auto& preferenceHandler : handlers_) { 86 0 : if (preferenceHandler->id().find(rootPath) != std::string::npos) { 87 0 : preferenceHandler->resetPreferenceAttributes(accountId); 88 : } 89 : } 90 0 : } 91 : 92 : void 93 32 : PreferenceServicesManager::registerComponentsLifeCycleManagers(PluginManager& pluginManager) 94 : { 95 : // registerHandler may be called by the PluginManager upon loading a plugin. 96 0 : auto registerHandler = [this](void* data, std::mutex& pmMtx_) { 97 0 : std::lock_guard lk(pmMtx_); 98 0 : PreferenceHandlerPtr ptr {(static_cast<PreferenceHandler*>(data))}; 99 : 100 0 : if (!ptr) 101 0 : return -1; 102 0 : handlers_.emplace_back(std::move(ptr)); 103 0 : return 0; 104 0 : }; 105 : 106 : // unregisterHandler may be called by the PluginManager while unloading. 107 0 : auto unregisterHandler = [this](void* data, std::mutex& pmMtx_) { 108 0 : std::lock_guard lk(pmMtx_); 109 0 : auto handlerIt = std::find_if(handlers_.begin(), handlers_.end(), [data](PreferenceHandlerPtr& handler) { 110 0 : return (handler.get() == data); 111 : }); 112 : 113 0 : if (handlerIt != handlers_.end()) { 114 0 : handlers_.erase(handlerIt); 115 : } 116 0 : return true; 117 0 : }; 118 : 119 : // Services are registered to the PluginManager. 120 32 : pluginManager.registerComponentManager("PreferenceHandlerManager", registerHandler, unregisterHandler); 121 32 : } 122 : } // namespace jami