| 1 |
/* |
| 2 |
* Copyright (C) 2005-2024 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "GUIDialogPeripheralSettings.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "addons/Skin.h" |
| 14 |
#include "dialogs/GUIDialogYesNo.h" |
| 15 |
#include "games/controllers/Controller.h" |
| 16 |
#include "games/controllers/ControllerManager.h" |
| 17 |
#include "guilib/GUIMessage.h" |
| 18 |
#include "peripherals/Peripherals.h" |
| 19 |
#include "settings/SettingAddon.h" |
| 20 |
#include "settings/lib/Setting.h" |
| 21 |
#include "settings/lib/SettingSection.h" |
| 22 |
#include "utils/Variant.h" |
| 23 |
#include "utils/log.h" |
| 24 |
|
| 25 |
#include <string_view> |
| 26 |
#include <utility> |
| 27 |
|
| 28 |
using namespace KODI; |
| 29 |
using namespace PERIPHERALS; |
| 30 |
|
| 31 |
// Settings for peripherals |
| 32 |
constexpr std::string_view SETTING_APPEARANCE = "appearance"; |
| 33 |
|
| 34 |
CGUIDialogPeripheralSettings::CGUIDialogPeripheralSettings() |
| 35 |
: CGUIDialogSettingsManualBase(WINDOW_DIALOG_PERIPHERAL_SETTINGS, "DialogSettings.xml"), |
| 36 |
m_item(NULL) |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
CGUIDialogPeripheralSettings::~CGUIDialogPeripheralSettings() |
| 41 |
{ |
| 42 |
if (m_item != NULL) |
| 43 |
delete m_item; |
| 44 |
|
| 45 |
m_settingsMap.clear(); |
| 46 |
} |
| 47 |
|
| 48 |
bool CGUIDialogPeripheralSettings::OnMessage(CGUIMessage& message) |
| 49 |
{ |
| 50 |
if (message.GetMessage() == GUI_MSG_CLICKED && |
| 51 |
message.GetSenderId() == CONTROL_SETTINGS_CUSTOM_BUTTON) |
| 52 |
{ |
| 53 |
OnResetSettings(); |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
return CGUIDialogSettingsManualBase::OnMessage(message); |
| 58 |
} |
| 59 |
|
| 60 |
void CGUIDialogPeripheralSettings::RegisterPeripheralManager(CPeripherals& manager) |
| 61 |
{ |
| 62 |
m_manager = &manager; |
| 63 |
} |
| 64 |
|
| 65 |
void CGUIDialogPeripheralSettings::UnregisterPeripheralManager() |
| 66 |
{ |
| 67 |
m_manager = nullptr; |
| 68 |
} |
| 69 |
|
| 70 |
void CGUIDialogPeripheralSettings::SetFileItem(const CFileItem* item) |
| 71 |
{ |
| 72 |
if (item == NULL) |
| 73 |
return; |
| 74 |
|
| 75 |
if (m_item != NULL) |
| 76 |
delete m_item; |
| 77 |
|
| 78 |
m_item = new CFileItem(*item); |
| 79 |
} |
| 80 |
|
| 81 |
void CGUIDialogPeripheralSettings::OnSettingChanged(const std::shared_ptr<const CSetting>& setting) |
| 82 |
{ |
| 83 |
if (setting == NULL) |
| 84 |
return; |
| 85 |
|
| 86 |
CGUIDialogSettingsManualBase::OnSettingChanged(setting); |
| 87 |
|
| 88 |
const std::string& settingId = setting->GetId(); |
| 89 |
|
| 90 |
// we need to copy the new value of the setting from the copy to the |
| 91 |
// original setting |
| 92 |
std::map<std::string, std::shared_ptr<CSetting>>::iterator itSetting = |
| 93 |
m_settingsMap.find(settingId); |
| 94 |
if (itSetting == m_settingsMap.end()) |
| 95 |
return; |
| 96 |
|
| 97 |
itSetting->second->FromString(setting->ToString()); |
| 98 |
|
| 99 |
// Get peripheral associated with this setting |
| 100 |
PeripheralPtr peripheral; |
| 101 |
if (m_item != nullptr) |
| 102 |
peripheral = CServiceBroker::GetPeripherals().GetByPath(m_item->GetPath()); |
| 103 |
|
| 104 |
if (!peripheral) |
| 105 |
return; |
| 106 |
|
| 107 |
if (settingId == SETTING_APPEARANCE) |
| 108 |
{ |
| 109 |
// Get the controller profile of the new appearance |
| 110 |
GAME::ControllerPtr controller; |
| 111 |
|
| 112 |
if (setting->GetType() == SettingType::String) |
| 113 |
{ |
| 114 |
std::shared_ptr<const CSettingString> settingString = |
| 115 |
std::static_pointer_cast<const CSettingString>(setting); |
| 116 |
const std::string& addonId = settingString->GetValue(); |
| 117 |
|
| 118 |
if (m_manager != nullptr) |
| 119 |
controller = m_manager->GetControllerProfiles().GetController(addonId); |
| 120 |
} |
| 121 |
|
| 122 |
if (controller) |
| 123 |
peripheral->SetControllerProfile(controller); |
| 124 |
} |
| 125 |
|
| 126 |
peripheral->OnSettingChanged(settingId); |
| 127 |
} |
| 128 |
|
| 129 |
bool CGUIDialogPeripheralSettings::Save() |
| 130 |
{ |
| 131 |
if (m_item == NULL || m_initialising) |
| 132 |
return true; |
| 133 |
|
| 134 |
PeripheralPtr peripheral = CServiceBroker::GetPeripherals().GetByPath(m_item->GetPath()); |
| 135 |
if (!peripheral) |
| 136 |
return true; |
| 137 |
|
| 138 |
peripheral->PersistSettings(); |
| 139 |
|
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
void CGUIDialogPeripheralSettings::OnResetSettings() |
| 144 |
{ |
| 145 |
if (m_item == NULL) |
| 146 |
return; |
| 147 |
|
| 148 |
PeripheralPtr peripheral = CServiceBroker::GetPeripherals().GetByPath(m_item->GetPath()); |
| 149 |
if (!peripheral) |
| 150 |
return; |
| 151 |
|
| 152 |
if (!CGUIDialogYesNo::ShowAndGetInput(CVariant{10041}, CVariant{10042})) |
| 153 |
return; |
| 154 |
|
| 155 |
// reset the settings in the peripheral |
| 156 |
peripheral->ResetDefaultSettings(); |
| 157 |
|
| 158 |
// re-create all settings and their controls |
| 159 |
SetupView(); |
| 160 |
} |
| 161 |
|
| 162 |
void CGUIDialogPeripheralSettings::SetupView() |
| 163 |
{ |
| 164 |
CGUIDialogSettingsManualBase::SetupView(); |
| 165 |
|
| 166 |
SetHeading(m_item->GetLabel()); |
| 167 |
SET_CONTROL_LABEL(CONTROL_SETTINGS_OKAY_BUTTON, 186); |
| 168 |
SET_CONTROL_LABEL(CONTROL_SETTINGS_CANCEL_BUTTON, 222); |
| 169 |
SET_CONTROL_LABEL(CONTROL_SETTINGS_CUSTOM_BUTTON, 409); |
| 170 |
} |
| 171 |
|
| 172 |
void CGUIDialogPeripheralSettings::InitializeSettings() |
| 173 |
{ |
| 174 |
if (m_item == NULL) |
| 175 |
{ |
| 176 |
m_initialising = false; |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
m_initialising = true; |
| 181 |
bool usePopup = g_SkinInfo->HasSkinFile("DialogSlider.xml"); |
| 182 |
|
| 183 |
PeripheralPtr peripheral = CServiceBroker::GetPeripherals().GetByPath(m_item->GetPath()); |
| 184 |
if (!peripheral) |
| 185 |
{ |
| 186 |
CLog::Log(LOGDEBUG, "{} - no peripheral", __FUNCTION__); |
| 187 |
m_initialising = false; |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
m_settingsMap.clear(); |
| 192 |
CGUIDialogSettingsManualBase::InitializeSettings(); |
| 193 |
|
| 194 |
const std::shared_ptr<CSettingCategory> category = AddCategory("peripheralsettings", -1); |
| 195 |
if (category == NULL) |
| 196 |
{ |
| 197 |
CLog::Log(LOGERROR, "CGUIDialogPeripheralSettings: unable to setup settings"); |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
const std::shared_ptr<CSettingGroup> group = AddGroup(category); |
| 202 |
if (group == NULL) |
| 203 |
{ |
| 204 |
CLog::Log(LOGERROR, "CGUIDialogPeripheralSettings: unable to setup settings"); |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
std::vector<SettingPtr> settings = peripheral->GetSettings(); |
| 209 |
for (auto& setting : settings) |
| 210 |
{ |
| 211 |
if (setting == NULL) |
| 212 |
continue; |
| 213 |
|
| 214 |
if (!setting->IsVisible()) |
| 215 |
{ |
| 216 |
CLog::Log(LOGDEBUG, "{} - invisible", __FUNCTION__); |
| 217 |
continue; |
| 218 |
} |
| 219 |
|
| 220 |
// we need to create a copy of the setting because the CSetting instances |
| 221 |
// are destroyed when leaving the dialog |
| 222 |
SettingPtr settingCopy; |
| 223 |
switch (setting->GetType()) |
| 224 |
{ |
| 225 |
case SettingType::Boolean: |
| 226 |
{ |
| 227 |
std::shared_ptr<CSettingBool> settingBool = std::make_shared<CSettingBool>( |
| 228 |
setting->GetId(), *std::static_pointer_cast<CSettingBool>(setting)); |
| 229 |
settingBool->SetControl(GetCheckmarkControl()); |
| 230 |
|
| 231 |
settingCopy = std::static_pointer_cast<CSetting>(settingBool); |
| 232 |
break; |
| 233 |
} |
| 234 |
|
| 235 |
case SettingType::Integer: |
| 236 |
{ |
| 237 |
std::shared_ptr<CSettingInt> settingInt = std::make_shared<CSettingInt>( |
| 238 |
setting->GetId(), *std::static_pointer_cast<CSettingInt>(setting)); |
| 239 |
if (settingInt->GetTranslatableOptions().empty()) |
| 240 |
settingInt->SetControl(GetSliderControl("integer", false, -1, usePopup, -1, "{:d}")); |
| 241 |
else |
| 242 |
settingInt->SetControl(GetSpinnerControl("string")); |
| 243 |
|
| 244 |
settingCopy = std::static_pointer_cast<CSetting>(settingInt); |
| 245 |
break; |
| 246 |
} |
| 247 |
|
| 248 |
case SettingType::Number: |
| 249 |
{ |
| 250 |
std::shared_ptr<CSettingNumber> settingNumber = std::make_shared<CSettingNumber>( |
| 251 |
setting->GetId(), *std::static_pointer_cast<CSettingNumber>(setting)); |
| 252 |
settingNumber->SetControl(GetSliderControl("number", false, -1, usePopup, -1, "{:2.2f}")); |
| 253 |
|
| 254 |
settingCopy = std::static_pointer_cast<CSetting>(settingNumber); |
| 255 |
break; |
| 256 |
} |
| 257 |
|
| 258 |
case SettingType::String: |
| 259 |
{ |
| 260 |
if (auto settingAsAddon = std::dynamic_pointer_cast<const CSettingAddon>(setting)) |
| 261 |
{ |
| 262 |
std::shared_ptr<CSettingAddon> settingAddon = |
| 263 |
std::make_shared<CSettingAddon>(setting->GetId(), *settingAsAddon); |
| 264 |
|
| 265 |
// Control properties |
| 266 |
const std::string format = "addon"; |
| 267 |
const bool delayed = false; |
| 268 |
const int heading = -1; |
| 269 |
const bool hideValue = false; |
| 270 |
const bool showInstalledAddons = true; |
| 271 |
const bool showInstallableAddons = true; |
| 272 |
const bool showMoreAddons = false; |
| 273 |
|
| 274 |
settingAddon->SetControl(GetButtonControl(format, delayed, heading, hideValue, |
| 275 |
showInstalledAddons, showInstallableAddons, |
| 276 |
showMoreAddons)); |
| 277 |
|
| 278 |
GAME::ControllerPtr controller = peripheral->ControllerProfile(); |
| 279 |
if (controller) |
| 280 |
settingAddon->SetValue(controller->ID()); |
| 281 |
|
| 282 |
settingCopy = std::static_pointer_cast<CSetting>(settingAddon); |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
std::shared_ptr<CSettingString> settingString = std::make_shared<CSettingString>( |
| 287 |
setting->GetId(), *std::static_pointer_cast<CSettingString>(setting)); |
| 288 |
settingString->SetControl(GetEditControl("string")); |
| 289 |
|
| 290 |
settingCopy = std::static_pointer_cast<CSetting>(settingString); |
| 291 |
} |
| 292 |
break; |
| 293 |
} |
| 294 |
|
| 295 |
default: |
| 296 |
//! @todo add more types if needed |
| 297 |
CLog::Log(LOGDEBUG, "{} - unknown type", __FUNCTION__); |
| 298 |
break; |
| 299 |
} |
| 300 |
|
| 301 |
if (settingCopy != NULL && settingCopy->GetControl() != NULL) |
| 302 |
{ |
| 303 |
settingCopy->SetLevel(SettingLevel::Basic); |
| 304 |
group->AddSetting(settingCopy); |
| 305 |
m_settingsMap.insert(std::make_pair(setting->GetId(), setting)); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
m_initialising = false; |
| 310 |
} |