2 * Copyright (C) 2013-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "SettingUtils.h"
11 #include "settings/lib/Setting.h"
12 #include "utils/StringUtils.h"
13 #include "utils/Variant.h"
17 std::vector
<CVariant
> CSettingUtils::GetList(const std::shared_ptr
<const CSettingList
>& settingList
)
19 return ListToValues(settingList
, settingList
->GetValue());
22 bool CSettingUtils::SetList(const std::shared_ptr
<CSettingList
>& settingList
,
23 const std::vector
<CVariant
>& value
)
25 SettingList newValues
;
26 if (!ValuesToList(settingList
, value
, newValues
))
29 return settingList
->SetValue(newValues
);
32 std::vector
<CVariant
> CSettingUtils::ListToValues(
33 const std::shared_ptr
<const CSettingList
>& setting
,
34 const std::vector
<std::shared_ptr
<CSetting
>>& values
)
36 std::vector
<CVariant
> realValues
;
41 for (const auto& value
: values
)
43 switch (setting
->GetElementType())
45 case SettingType::Boolean
:
46 realValues
.emplace_back(std::static_pointer_cast
<const CSettingBool
>(value
)->GetValue());
49 case SettingType::Integer
:
50 realValues
.emplace_back(std::static_pointer_cast
<const CSettingInt
>(value
)->GetValue());
53 case SettingType::Number
:
54 realValues
.emplace_back(std::static_pointer_cast
<const CSettingNumber
>(value
)->GetValue());
57 case SettingType::String
:
58 realValues
.emplace_back(std::static_pointer_cast
<const CSettingString
>(value
)->GetValue());
69 bool CSettingUtils::ValuesToList(const std::shared_ptr
<const CSettingList
>& setting
,
70 const std::vector
<CVariant
>& values
,
71 std::vector
<std::shared_ptr
<CSetting
>>& newValues
)
78 for (const auto& value
: values
)
80 SettingPtr settingValue
=
81 setting
->GetDefinition()->Clone(StringUtils::Format("{}.{}", setting
->GetId(), index
++));
82 if (settingValue
== NULL
)
85 switch (setting
->GetElementType())
87 case SettingType::Boolean
:
88 if (!value
.isBoolean())
91 ret
= std::static_pointer_cast
<CSettingBool
>(settingValue
)->SetValue(value
.asBoolean());
94 case SettingType::Integer
:
95 if (!value
.isInteger())
98 ret
= std::static_pointer_cast
<CSettingInt
>(settingValue
)->SetValue(static_cast<int>(value
.asInteger()));
101 case SettingType::Number
:
102 if (!value
.isDouble())
105 ret
= std::static_pointer_cast
<CSettingNumber
>(settingValue
)->SetValue(value
.asDouble());
108 case SettingType::String
:
109 if (!value
.isString())
112 ret
= std::static_pointer_cast
<CSettingString
>(settingValue
)->SetValue(value
.asString());
123 newValues
.push_back(std::const_pointer_cast
<CSetting
>(settingValue
));
129 bool CSettingUtils::FindIntInList(const std::shared_ptr
<const CSettingList
>& settingList
, int value
)
131 if (settingList
== nullptr || settingList
->GetElementType() != SettingType::Integer
)
134 const auto values
= settingList
->GetValue();
135 const auto matchingValue
=
136 std::find_if(values
.begin(), values
.end(), [value
](const SettingPtr
& setting
) {
137 return std::static_pointer_cast
<CSettingInt
>(setting
)->GetValue() == value
;
139 return matchingValue
!= values
.end();