blob: e4d6ec06e215730ab85ce65dbe8597ffb604f9db [file] [log] [blame]
Aline Gondim Santos329f8622022-11-08 08:04:22 -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 "TranscriptMediaHandler.h"
24#include "pluglog.h"
25
26const char sep = separator();
27const std::string TAG = "TranscriptAcc";
28
29#define NAME "TranscriptAcc"
30
31namespace jami {
32
33PluginPreferenceHandler::PluginPreferenceHandler(
34 const JAMI_PluginAPI* api,
35 std::map<std::string, std::map<std::string, std::string>>&& preferences,
36 const std::string& dataPath)
37 : api_ {api}
38 , datapath_ {dataPath}
39{
40 preferences_ = preferences;
41 setId(datapath_);
42};
43
44std::map<std::string, std::string>
45PluginPreferenceHandler::getHandlerDetails()
46{
47 return {{"name", NAME}, {"iconPath", datapath_ + sep + "icon.svg"}, {"pluginId", id()}};
48}
49
50void
51PluginPreferenceHandler::setPreferenceAttribute(const std::string& accountId,
52 const std::string& key,
53 const std::string& value)
54{
55 if (accountId.empty()) {
56 for (auto& prefMap : preferences_) {
57 auto it = prefMap.second.find(key);
58 if (it != prefMap.second.end() && it->second != value) {
59 it->second = value;
60 }
61 }
62 } else {
63 auto accIt = preferences_.find("default");
64 accIt = preferences_.emplace(accountId, preferences_["default"]).first;
65 auto it = accIt->second.find(key);
66 if (it != accIt->second.end() && it->second != value) {
67 it->second = value;
68 }
69 }
70
71 tmh_->setParameters(accountId);
72}
73
74void
75PluginPreferenceHandler::resetPreferenceAttributes(const std::string& accountId)
76{
77 std::lock_guard<std::mutex> lk(mtx_);
78 if (accountId.empty()) {
79 preferences_.clear();
80 api_->invokeService(api_, "getPluginAccPreferences", &preferences_);
81 } else
82 preferences_[accountId] = preferences_["default"];
83 tmh_->setParameters(accountId);
84}
85
86bool
87PluginPreferenceHandler::preferenceMapHasKey(const std::string& key)
88{
Aline Gondim Santos440e8122023-01-09 14:03:49 -030089 return (key == "background"
90 || key == "position"
91 || key == "fontsize"
Adrien Beraud087d5592023-03-06 11:22:33 -050092 || key == "language");
Aline Gondim Santos440e8122023-01-09 14:03:49 -030093}
94
95std::string
96PluginPreferenceHandler::getPreferences(const std::string& accountId, const std::string& key)
97{
98 std::lock_guard<std::mutex> lk(mtx_);
99 auto accIt = preferences_.emplace(accountId, preferences_["default"]).first;
100 auto valueIt = accIt->second.find(key);
101 if (valueIt != accIt->second.end()) {
102 return valueIt->second;
103 }
104 return "";
Aline Gondim Santos329f8622022-11-08 08:04:22 -0300105}
106
107std::map<std::string, std::string>
108PluginPreferenceHandler::getPreferences(const std::string& accountId)
109{
110 std::lock_guard<std::mutex> lk(mtx_);
Adrien Beraud087d5592023-03-06 11:22:33 -0500111 return preferences_.emplace(accountId, preferences_["default"]).first->second;
Aline Gondim Santos329f8622022-11-08 08:04:22 -0300112}
113
114PluginPreferenceHandler::~PluginPreferenceHandler()
115{
116 preferences_.clear();
117}
118
119void
120PluginPreferenceHandler::setTranscriptHandler(TranscriptMediaHandler* handler)
121{
122 tmh_ = handler;
123}
124} // namespace jami