blob: 0690932134b89e5060d15ad984d53b2bdd39a37d [file] [log] [blame]
agsantos3fcc6212021-08-18 17:11:29 -03001/**
2 * Copyright (C) 2021 Savoir-faire Linux Inc.
3 *
4 * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include "PluginPreferenceHandler.h"
22
23#include "pluglog.h"
24
25const char sep = separator();
26const std::string TAG = "BotAcc";
27
28#define NAME "BotAcc"
29
30namespace jami {
31
32PluginPreferenceHandler::PluginPreferenceHandler(
33 const JAMI_PluginAPI* api,
34 std::map<std::string, std::map<std::string, std::string>>&& preferences,
35 const std::string& dataPath)
36 : api_ {api}
37 , datapath_ {dataPath}
38{
39 preferences_ = preferences;
40 setId(datapath_);
41};
42
43std::map<std::string, std::string>
44PluginPreferenceHandler::getHandlerDetails()
45{
46 return {{"name", NAME}, {"iconPath", datapath_ + sep + "icon.svg"}, {"pluginId", id()}};
47}
48
49void
50PluginPreferenceHandler::setPreferenceAttribute(const std::string& accountId,
51 const std::string& key,
52 const std::string& value)
53{
54 auto accIt = preferences_.find("default");
55 if (!accountId.empty())
56 accIt = preferences_.emplace(accountId, preferences_["default"]).first;
57
58 auto it = accIt->second.find(key);
59 if (it != accIt->second.end() && it->second != value) {
60 it->second = value;
61 }
62}
63
64void
65PluginPreferenceHandler::resetPreferenceAttributes(const std::string& accountId)
66{
67 std::lock_guard<std::mutex> lk(mtx_);
68 if (accountId.empty()) {
69 preferences_.clear();
70 api_->invokeService(api_, "getPluginAccPreferences", &preferences_);
71 } else
72 preferences_[accountId] = preferences_["default"];
73}
74
75bool
76PluginPreferenceHandler::preferenceMapHasKey(const std::string& key)
77{
78 return key == "answer" || key == "inText";
79}
80
81std::string
82PluginPreferenceHandler::getPreferences(const std::string& accountId, const std::string& key)
83{
84 std::lock_guard<std::mutex> lk(mtx_);
85 auto accIt = preferences_.emplace(accountId, preferences_["default"]).first;
86 auto valueIt = accIt->second.find(key);
87 if (valueIt != accIt->second.end()) {
88 return valueIt->second;
89 }
90 return "";
91}
92
93PluginPreferenceHandler::~PluginPreferenceHandler()
94{
95 preferences_.clear();
96}
97} // namespace jami