Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 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 33 : PreferenceServicesManager::PreferenceServicesManager(PluginManager& pluginManager) 31 : { 32 33 : registerComponentsLifeCycleManagers(pluginManager); 33 33 : } 34 : 35 33 : PreferenceServicesManager::~PreferenceServicesManager() 36 : { 37 33 : handlers_.clear(); 38 33 : } 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 2 : PreferenceServicesManager::setPreference(const std::string& key, 65 : const std::string& value, 66 : const std::string& rootPath, 67 : const std::string& accountId) 68 : { 69 2 : bool status {true}; 70 2 : 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 2 : return status; 80 : } 81 : 82 : void 83 3 : PreferenceServicesManager::resetPreferences(const std::string& rootPath, 84 : const std::string& accountId) 85 : { 86 3 : for (auto& preferenceHandler : handlers_) { 87 0 : if (preferenceHandler->id().find(rootPath) != std::string::npos) { 88 0 : preferenceHandler->resetPreferenceAttributes(accountId); 89 : } 90 : } 91 3 : } 92 : 93 : void 94 33 : PreferenceServicesManager::registerComponentsLifeCycleManagers(PluginManager& pluginManager) 95 : { 96 : // registerHandler may be called by the PluginManager upon loading a plugin. 97 0 : auto registerHandler = [this](void* data, std::mutex& pmMtx_) { 98 0 : std::lock_guard lk(pmMtx_); 99 0 : PreferenceHandlerPtr ptr {(static_cast<PreferenceHandler*>(data))}; 100 : 101 0 : if (!ptr) 102 0 : return -1; 103 0 : handlers_.emplace_back(std::move(ptr)); 104 0 : return 0; 105 0 : }; 106 : 107 : // unregisterHandler may be called by the PluginManager while unloading. 108 0 : auto unregisterHandler = [this](void* data, std::mutex& pmMtx_) { 109 0 : std::lock_guard lk(pmMtx_); 110 0 : auto handlerIt = std::find_if(handlers_.begin(), 111 : handlers_.end(), 112 0 : [data](PreferenceHandlerPtr& handler) { 113 0 : return (handler.get() == data); 114 : }); 115 : 116 0 : if (handlerIt != handlers_.end()) { 117 0 : handlers_.erase(handlerIt); 118 : } 119 0 : return true; 120 0 : }; 121 : 122 : // Services are registered to the PluginManager. 123 33 : pluginManager.registerComponentManager("PreferenceHandlerManager", 124 : registerHandler, 125 : unregisterHandler); 126 33 : } 127 : } // namespace jami