1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
* Copyright (C) 2015-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "PVRSettings.h"
#include "ServiceBroker.h"
#include "guilib/LocalizeStrings.h"
#include "pvr/PVRManager.h"
#include "pvr/addons/PVRClients.h"
#include "pvr/guilib/PVRGUIActionsParentalControl.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "settings/lib/SettingsManager.h"
#include "utils/StringUtils.h"
#include "utils/log.h"
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <utility>
using namespace PVR;
unsigned int CPVRSettings::m_iInstances = 0;
CPVRSettings::CPVRSettings(const std::set<std::string>& settingNames)
{
Init(settingNames);
const std::shared_ptr<CSettings> settings = CServiceBroker::GetSettingsComponent()->GetSettings();
settings->GetSettingsManager()->RegisterSettingsHandler(this);
settings->RegisterCallback(this, settingNames);
if (m_iInstances == 0)
{
// statics must only be registered once, not per instance
settings->GetSettingsManager()->RegisterSettingOptionsFiller("pvrrecordmargins", MarginTimeFiller);
settings->GetSettingsManager()->AddDynamicCondition("pvrsettingvisible", IsSettingVisible);
settings->GetSettingsManager()->AddDynamicCondition("checkpvrparentalpin", CheckParentalPin);
}
m_iInstances++;
}
CPVRSettings::~CPVRSettings()
{
const std::shared_ptr<CSettings> settings = CServiceBroker::GetSettingsComponent()->GetSettings();
m_iInstances--;
if (m_iInstances == 0)
{
settings->GetSettingsManager()->RemoveDynamicCondition("checkpvrparentalpin");
settings->GetSettingsManager()->RemoveDynamicCondition("pvrsettingvisible");
settings->GetSettingsManager()->UnregisterSettingOptionsFiller("pvrrecordmargins");
}
settings->UnregisterCallback(this);
settings->GetSettingsManager()->UnregisterSettingsHandler(this);
}
void CPVRSettings::RegisterCallback(ISettingCallback* callback)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
m_callbacks.insert(callback);
}
void CPVRSettings::UnregisterCallback(ISettingCallback* callback)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
m_callbacks.erase(callback);
}
void CPVRSettings::Init(const std::set<std::string>& settingNames)
{
for (const auto& settingName : settingNames)
{
SettingPtr setting = CServiceBroker::GetSettingsComponent()->GetSettings()->GetSetting(settingName);
if (!setting)
{
CLog::LogF(LOGERROR, "Unknown PVR setting '{}'", settingName);
continue;
}
std::unique_lock<CCriticalSection> lock(m_critSection);
m_settings.insert(std::make_pair(settingName, setting->Clone(settingName)));
}
}
void CPVRSettings::OnSettingsLoaded()
{
std::set<std::string> settingNames;
{
std::unique_lock<CCriticalSection> lock(m_critSection);
for (const auto& settingName : m_settings)
settingNames.insert(settingName.first);
m_settings.clear();
}
Init(settingNames);
}
void CPVRSettings::OnSettingChanged(const std::shared_ptr<const CSetting>& setting)
{
if (setting == nullptr)
return;
std::unique_lock<CCriticalSection> lock(m_critSection);
m_settings[setting->GetId()] = setting->Clone(setting->GetId());
const auto callbacks(m_callbacks);
lock.unlock();
for (const auto& callback : callbacks)
callback->OnSettingChanged(setting);
}
bool CPVRSettings::GetBoolValue(const std::string& settingName) const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
auto settingIt = m_settings.find(settingName);
if (settingIt != m_settings.end() && (*settingIt).second->GetType() == SettingType::Boolean)
{
std::shared_ptr<const CSettingBool> setting = std::dynamic_pointer_cast<const CSettingBool>((*settingIt).second);
if (setting)
return setting->GetValue();
}
CLog::LogF(LOGERROR, "PVR setting '{}' not found or wrong type given", settingName);
return false;
}
int CPVRSettings::GetIntValue(const std::string& settingName) const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
auto settingIt = m_settings.find(settingName);
if (settingIt != m_settings.end() && (*settingIt).second->GetType() == SettingType::Integer)
{
std::shared_ptr<const CSettingInt> setting = std::dynamic_pointer_cast<const CSettingInt>((*settingIt).second);
if (setting)
return setting->GetValue();
}
CLog::LogF(LOGERROR, "PVR setting '{}' not found or wrong type given", settingName);
return -1;
}
std::string CPVRSettings::GetStringValue(const std::string& settingName) const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
auto settingIt = m_settings.find(settingName);
if (settingIt != m_settings.end() && (*settingIt).second->GetType() == SettingType::String)
{
std::shared_ptr<const CSettingString> setting = std::dynamic_pointer_cast<const CSettingString>((*settingIt).second);
if (setting)
return setting->GetValue();
}
CLog::LogF(LOGERROR, "PVR setting '{}' not found or wrong type given", settingName);
return "";
}
void CPVRSettings::MarginTimeFiller(const SettingConstPtr& /*setting*/,
std::vector<IntegerSettingOption>& list,
int& current,
void* /*data*/)
{
list.clear();
static const int marginTimeValues[] =
{
0, 1, 3, 5, 10, 15, 20, 30, 60, 90, 120, 180 // minutes
};
for (int iValue : marginTimeValues)
{
list.emplace_back(StringUtils::Format(g_localizeStrings.Get(14044), iValue) /* {} min */,
iValue);
}
}
bool CPVRSettings::IsSettingVisible(const std::string& condition,
const std::string& value,
const std::shared_ptr<const CSetting>& setting,
void* data)
{
if (setting == nullptr)
return false;
const std::string& settingId = setting->GetId();
if (settingId == CSettings::SETTING_PVRMANAGER_USEBACKENDCHANNELNUMBERS)
{
// Setting is only visible if exactly one PVR client is enabled or
// the expert setting to always use backend numbers is enabled
const auto& settings = CServiceBroker::GetSettingsComponent()->GetSettings();
int enabledClientAmount = CServiceBroker::GetPVRManager().Clients()->EnabledClientAmount();
return enabledClientAmount == 1 ||
(settings->GetBool(CSettings::SETTING_PVRMANAGER_USEBACKENDCHANNELNUMBERSALWAYS) &&
enabledClientAmount > 1);
}
else if (settingId == CSettings::SETTING_PVRMANAGER_USEBACKENDCHANNELNUMBERSALWAYS)
{
// Setting is only visible if more than one PVR client is enabled.
return CServiceBroker::GetPVRManager().Clients()->EnabledClientAmount() > 1;
}
else if (settingId == CSettings::SETTING_PVRMANAGER_CLIENTPRIORITIES)
{
// Setting is only visible if more than one PVR client is enabeld.
return CServiceBroker::GetPVRManager().Clients()->EnabledClientAmount() > 1;
}
else
{
// Show all other settings unconditionally.
return true;
}
}
bool CPVRSettings::CheckParentalPin(const std::string& condition,
const std::string& value,
const std::shared_ptr<const CSetting>& setting,
void* data)
{
return CServiceBroker::GetPVRManager().Get<PVR::GUI::Parental>().CheckParentalPIN() ==
ParentalCheckResult::SUCCESS;
}
|