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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
|
/*
* Copyright (C) 2017-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 "AddonSettings.h"
#include "FileItem.h"
#include "GUIInfoManager.h"
#include "LangInfo.h"
#include "ServiceBroker.h"
#include "addons/addoninfo/AddonInfo.h"
#include "addons/addoninfo/AddonType.h"
#include "addons/binary-addons/AddonInstanceHandler.h"
#include "addons/gui/GUIDialogAddonSettings.h"
#include "addons/settings/SettingUrlEncodedString.h"
#include "filesystem/Directory.h"
#include "guilib/GUIComponent.h"
#include "guilib/LocalizeStrings.h"
#include "messaging/ApplicationMessenger.h"
#include "settings/SettingAddon.h"
#include "settings/SettingConditions.h"
#include "settings/SettingControl.h"
#include "settings/SettingDateTime.h"
#include "settings/SettingPath.h"
#include "settings/lib/Setting.h"
#include "settings/lib/SettingDefinitions.h"
#include "settings/lib/SettingSection.h"
#include "settings/lib/SettingsManager.h"
#include "utils/FileExtensionProvider.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/XBMCTinyXML.h"
#include "utils/XMLUtils.h"
#include "utils/log.h"
#include <algorithm>
#include <mutex>
#include <vector>
namespace
{
constexpr auto OldSettingValuesSeparator = "|";
constexpr int UnknownSettingLabelIdStart = 100000;
bool InfoBool(const std::string& condition,
const std::string& value,
const SettingConstPtr& setting,
void* data)
{
return CServiceBroker::GetGUI()->GetInfoManager().EvaluateBool(value, INFO::DEFAULT_CONTEXT);
}
template<class TSetting>
SettingPtr InitializeFromOldSettingWithoutDefinition(ADDON::CAddonSettings& settings,
const std::string& settingId,
const typename TSetting::Value& defaultValue)
{
std::shared_ptr<TSetting> setting =
std::make_shared<TSetting>(settingId, settings.GetSettingsManager());
setting->SetLevel(SettingLevel::Internal);
setting->SetVisible(false);
setting->SetDefault(defaultValue);
return setting;
}
template<>
SettingPtr InitializeFromOldSettingWithoutDefinition<CSettingString>(
ADDON::CAddonSettings& settings,
const std::string& settingId,
const typename CSettingString::Value& defaultValue)
{
std::shared_ptr<CSettingString> setting =
std::make_shared<CSettingString>(settingId, settings.GetSettingsManager());
setting->SetLevel(SettingLevel::Internal);
setting->SetVisible(false);
setting->SetDefault(defaultValue);
setting->SetAllowEmpty(true);
return setting;
}
template<class TSetting>
SettingPtr AddSettingWithoutDefinition(ADDON::CAddonSettings& settings,
const std::string& settingId,
typename TSetting::Value defaultValue,
const Logger& logger)
{
if (settingId.empty())
return nullptr;
// if necessary try to initialize the settings manager on-the-fly without any definitions
if (!settings.IsInitialized() && !settings.Initialize(CXBMCTinyXML(), true))
{
logger->warn("failed to initialize settings on-the-fly");
return nullptr;
}
// check if we need to add a section on-the-fly
auto sections = settings.GetSettingsManager()->GetSections();
SettingSectionPtr section;
if (sections.empty())
section =
std::make_shared<CSettingSection>(settings.GetAddonId(), settings.GetSettingsManager());
else
section = sections.back();
// check if we need to add a category on-the-fly
auto categories = section->GetCategories();
SettingCategoryPtr category;
if (categories.empty())
category = std::make_shared<CSettingCategory>("category0", settings.GetSettingsManager());
else
category = categories.back();
// check if we need to add a group on-the-fly
auto groups = category->GetGroups();
SettingGroupPtr group;
if (groups.empty())
group = std::make_shared<CSettingGroup>("0", settings.GetSettingsManager());
else
group = groups.back();
// create a new setting on-the-fly
auto setting =
InitializeFromOldSettingWithoutDefinition<TSetting>(settings, settingId, defaultValue);
if (setting == nullptr)
{
logger->warn("failed to create setting \"{}\" on-the-fly", settingId);
return nullptr;
}
// add the setting (and if necessary the section, category and/or group)
if (!settings.GetSettingsManager()->AddSetting(setting, section, category, group))
{
logger->warn("failed to add setting \"{}\" on-the-fly", settingId);
return nullptr;
}
return setting;
}
} // namespace
namespace ADDON
{
CAddonSettings::CAddonSettings(const std::shared_ptr<const IAddon>& addon,
AddonInstanceId instanceId)
: CSettingsBase(),
m_addonId(addon->ID()),
m_addonPath(addon->Path()),
m_addonProfile(addon->Profile()),
m_instanceId(instanceId),
m_unidentifiedSettingId(0),
m_unknownSettingLabelId(UnknownSettingLabelIdStart),
m_logger(CServiceBroker::GetLogging().GetLogger(
StringUtils::Format("CAddonSettings[{}@{}]", m_instanceId, m_addonId)))
{
}
std::shared_ptr<CSetting> CAddonSettings::CreateSetting(
const std::string& settingType,
const std::string& settingId,
CSettingsManager* settingsManager /* = nullptr */) const
{
if (StringUtils::EqualsNoCase(settingType, "urlencodedstring"))
return std::make_shared<CSettingUrlEncodedString>(settingId, settingsManager);
return CSettingCreator::CreateSetting(settingType, settingId, settingsManager);
}
void CAddonSettings::OnSettingAction(const std::shared_ptr<const CSetting>& setting)
{
std::string actionData;
bool closeDialog = false;
// check if it's an action setting
if (setting->GetType() == SettingType::Action)
{
auto settingAction = std::dynamic_pointer_cast<const CSettingAction>(setting);
if (settingAction != nullptr && settingAction->HasData())
{
actionData = settingAction->GetData();
// replace $CWD with the url of the add-on
StringUtils::Replace(actionData, "$CWD", m_addonPath);
// replace $ID with the id of the add-on
StringUtils::Replace(actionData, "$ID", m_addonId);
}
}
// check if the setting control's is a button and its format is action
if (setting->GetControl()->GetType() == "button" &&
setting->GetControl()->GetFormat() == "action")
{
auto controlButton =
std::dynamic_pointer_cast<const CSettingControlButton>(setting->GetControl());
if (controlButton != nullptr)
{
if (actionData.empty() && controlButton->HasActionData())
actionData = controlButton->GetActionData();
closeDialog = controlButton->CloseDialog();
}
}
if (actionData.empty())
return;
if (closeDialog)
CGUIDialogAddonSettings::SaveAndClose();
CServiceBroker::GetAppMessenger()->SendMsg(TMSG_EXECUTE_BUILT_IN, -1, -1, nullptr, actionData);
}
bool CAddonSettings::AddInstanceSettings()
{
if (GetSetting(ADDON_SETTING_INSTANCE_NAME_VALUE) ||
GetSetting(ADDON_SETTING_INSTANCE_ENABLED_VALUE))
{
CLog::Log(
LOGDEBUG,
"CAddonSettings::{} - Add-on {} using instance setting values byself, Kodi's add ignored",
__func__, m_addonId);
return true;
}
auto mgr = GetSettingsManager();
if (!mgr)
return false;
auto sections = mgr->GetSections();
if (sections.empty())
return false;
SettingSectionPtr section = *sections.begin();
auto categories = section->GetCategories();
if (categories.empty())
return false;
SettingCategoryPtr category = *categories.begin();
auto groups = category->GetGroups();
auto itr = std::find_if(groups.begin(), groups.end(),
[](const SettingGroupPtr& group)
{ return group->GetId() == ADDON_SETTING_INSTANCE_GROUP; });
SettingGroupPtr group;
if (itr != groups.end())
{
group = *itr;
}
else
{
group = std::make_shared<CSettingGroup>(ADDON_SETTING_INSTANCE_GROUP, mgr);
group->SetLabel(10017); // Add-on configuration
category->AddGroupToFront(group);
}
const std::shared_ptr<CSettingString> name =
std::make_shared<CSettingString>(ADDON_SETTING_INSTANCE_NAME_VALUE, 551, "", mgr); // Name
name->SetAllowEmpty(false);
name->SetControl(std::make_shared<CSettingControlEdit>());
if (!mgr->AddSetting(name, section, category, group))
return false;
const std::shared_ptr<CSettingBool> enabled = std::make_shared<CSettingBool>(
ADDON_SETTING_INSTANCE_ENABLED_VALUE, 305, true, mgr); // Enabled
enabled->SetControl(std::make_shared<CSettingControlCheckmark>());
if (!mgr->AddSetting(enabled, section, category, group))
return false;
return true;
}
bool CAddonSettings::Initialize(const CXBMCTinyXML& doc, bool allowEmpty /* = false */)
{
std::unique_lock<CCriticalSection> lock(m_critical);
if (m_initialized)
return false;
// register custom setting types
InitializeSettingTypes();
// register custom setting controls
InitializeControls();
// conditions need to be initialized before the setting definitions
InitializeConditions();
// load the settings definitions
if (!InitializeDefinitions(doc) && !allowEmpty)
return false;
// Add internal settings to set values about instance set
if (m_instanceId > 0 && !AddInstanceSettings())
return false;
GetSettingsManager()->SetInitialized();
m_initialized = true;
return true;
}
bool CAddonSettings::Load(const CXBMCTinyXML& doc)
{
std::unique_lock<CCriticalSection> lock(m_critical);
if (!m_initialized)
return false;
// figure out the version of the setting definitions
uint32_t version = 0;
if (!ParseSettingVersion(doc, version))
{
m_logger->error("failed to determine setting values version");
return false;
}
std::map<std::string, std::string> settingValues;
// for new/"normal" setting values use the standard process
if (version != 0)
{
bool updated;
if (!LoadValuesFromXml(doc, updated))
return false;
// helper lambda for parsing a setting's ID and value from XML
auto parseSettingValue = [&settingValues](const TiXmlNode* setting,
const std::string& categoryId = "") {
// put together the setting ID
auto settingId = categoryId;
if (!settingId.empty())
settingId += ".";
auto id = setting->ToElement()->Attribute("id");
if (id)
settingId += id;
// parse the setting value
std::string settingValue;
if (setting->FirstChild())
settingValue = setting->FirstChild()->ValueStr();
// add the setting to the map
settingValues.emplace(std::make_pair(settingId, settingValue));
};
// check if there were any setting values without a definition
auto category = doc.RootElement()->FirstChild();
while (category != nullptr)
{
// check if this really is a category with setting elements
if (category->FirstChild() && category->FirstChild()->Type() == CXBMCTinyXML::TINYXML_ELEMENT)
{
const auto& categoryId = category->ValueStr();
auto setting = category->FirstChild();
while (setting != nullptr)
{
parseSettingValue(setting, categoryId);
setting = setting->NextSibling();
}
}
else
parseSettingValue(category);
category = category->NextSibling();
}
}
// for old setting values do it manually
else if (!LoadOldSettingValues(doc, settingValues))
{
m_logger->error("failed to determine setting values from old format");
return false;
}
// process all settings
for (const auto& setting : settingValues)
{
// ignore setting values without a setting identifier
if (setting.first.empty())
continue;
// try to find a matching setting
SettingPtr newSetting = GetSetting(setting.first);
if (newSetting == nullptr)
{
// create a hidden/internal string setting on-the-fly
newSetting = AddSettingWithoutDefinition<CSettingString>(*this, setting.first, setting.second,
m_logger);
}
// try to load the old setting value
if (!newSetting)
{
m_logger->error("had null newSetting for value \"{}\" for setting {}", setting.second,
setting.first);
}
else if (!newSetting->FromString(setting.second))
{
m_logger->warn("failed to load value \"{}\" for setting {}", setting.second, setting.first);
}
}
SetLoaded();
return true;
}
bool CAddonSettings::Save(CXBMCTinyXML& doc) const
{
std::unique_lock<CCriticalSection> lock(m_critical);
if (!m_initialized)
return false;
if (!SaveValuesToXml(doc))
{
m_logger->error("failed to save settings");
return false;
}
return true;
}
bool CAddonSettings::HasSettings() const
{
return IsInitialized() && GetSettingsManager()->HasSettings();
}
std::string CAddonSettings::GetSettingLabel(int label) const
{
if (label < UnknownSettingLabelIdStart || label >= m_unknownSettingLabelId)
return "";
const auto labelIt = m_unknownSettingLabels.find(label);
if (labelIt == m_unknownSettingLabels.end())
return "";
return labelIt->second;
}
std::shared_ptr<CSetting> CAddonSettings::AddSetting(const std::string& settingId, bool value)
{
return AddSettingWithoutDefinition<CSettingBool>(*this, settingId, value, m_logger);
}
std::shared_ptr<CSetting> CAddonSettings::AddSetting(const std::string& settingId, int value)
{
return AddSettingWithoutDefinition<CSettingInt>(*this, settingId, value, m_logger);
}
std::shared_ptr<CSetting> CAddonSettings::AddSetting(const std::string& settingId, double value)
{
return AddSettingWithoutDefinition<CSettingNumber>(*this, settingId, value, m_logger);
}
std::shared_ptr<CSetting> CAddonSettings::AddSetting(const std::string& settingId,
const std::string& value)
{
return AddSettingWithoutDefinition<CSettingString>(*this, settingId, value, m_logger);
}
void CAddonSettings::InitializeSettingTypes()
{
GetSettingsManager()->RegisterSettingType("addon", this);
GetSettingsManager()->RegisterSettingType("date", this);
GetSettingsManager()->RegisterSettingType("path", this);
GetSettingsManager()->RegisterSettingType("time", this);
GetSettingsManager()->RegisterSettingType("urlencodedstring", this);
}
void CAddonSettings::InitializeControls()
{
GetSettingsManager()->RegisterSettingControl("toggle", this);
GetSettingsManager()->RegisterSettingControl("spinner", this);
GetSettingsManager()->RegisterSettingControl("edit", this);
GetSettingsManager()->RegisterSettingControl("button", this);
GetSettingsManager()->RegisterSettingControl("list", this);
GetSettingsManager()->RegisterSettingControl("slider", this);
GetSettingsManager()->RegisterSettingControl("range", this);
GetSettingsManager()->RegisterSettingControl("title", this);
GetSettingsManager()->RegisterSettingControl("colorbutton", this);
}
void CAddonSettings::InitializeConditions()
{
CSettingConditions::Initialize();
// add basic conditions
const std::set<std::string>& simpleConditions = CSettingConditions::GetSimpleConditions();
for (const auto& condition : simpleConditions)
GetSettingsManager()->AddCondition(condition);
GetSettingsManager()->AddDynamicCondition("InfoBool", InfoBool);
}
bool CAddonSettings::InitializeDefinitions(const CXBMCTinyXML& doc)
{
// figure out the version of the setting definitions
uint32_t version = 0;
if (!ParseSettingVersion(doc, version))
{
m_logger->error("failed to determine setting definitions version");
return false;
}
// for new/"normal" setting definitions use the standard process
if (version != 0)
return InitializeDefinitionsFromXml(doc);
// for old setting definitions do it manually
return InitializeFromOldSettingDefinitions(doc);
}
bool CAddonSettings::ParseSettingVersion(const CXBMCTinyXML& doc, uint32_t& version) const
{
const TiXmlElement* root = doc.RootElement();
if (root == nullptr)
return false;
if (!StringUtils::EqualsNoCase(root->ValueStr(), SETTING_XML_ROOT))
{
m_logger->error("error reading setting definitions: no <settings> tag");
return false;
}
version = GetSettingsManager()->ParseVersion(root);
return true;
}
std::shared_ptr<CSettingGroup> CAddonSettings::ParseOldSettingElement(
const TiXmlElement* categoryElement,
const std::shared_ptr<CSettingCategory>& category,
std::set<std::string>& settingIds)
{
// build a vector of settings from the same category
std::vector<std::shared_ptr<const CSetting>> categorySettings;
// prepare for settings with enable/visible conditions
struct SettingWithConditions
{
SettingPtr setting;
std::string enableCondition;
std::string visibleCondition;
SettingDependencies deps;
};
std::vector<SettingWithConditions> settingsWithConditions;
auto group = std::make_shared<CSettingGroup>("0", GetSettingsManager());
uint32_t groupId = 1;
// go through all settings in the category
const TiXmlElement* settingElement = categoryElement->FirstChildElement("setting");
while (settingElement != nullptr)
{
// read the possible attributes
const auto settingType = XMLUtils::GetAttribute(settingElement, "type");
const auto settingId = XMLUtils::GetAttribute(settingElement, "id");
const auto defaultValue = XMLUtils::GetAttribute(settingElement, "default");
const auto settingValues = XMLUtils::GetAttribute(settingElement, "values");
const auto settingLValues = StringUtils::Split(
XMLUtils::GetAttribute(settingElement, "lvalues"), OldSettingValuesSeparator);
int settingLabel = -1;
bool settingLabelParsed = ParseOldLabel(settingElement, settingId, settingLabel);
SettingPtr setting;
if (settingType == "sep" || settingType == "lsep")
{
// check if we need to create a new group
if (!group->GetSettings().empty())
{
// add the current group to the category
category->AddGroup(group);
// and create a new one
group.reset(new CSettingGroup(std::to_string(groupId), GetSettingsManager()));
groupId += 1;
}
if (settingType == "lsep" && settingLabelParsed)
group->SetLabel(settingLabel);
}
else if (settingId.empty() || settingType == "action")
{
if (settingType == "action")
setting = InitializeFromOldSettingAction(settingId, settingElement, defaultValue);
else
setting = InitializeFromOldSettingLabel();
}
else if (settingType == "bool")
setting = InitializeFromOldSettingBool(settingId, settingElement, defaultValue);
else if (settingType == "text" || settingType == "ipaddress")
setting = InitializeFromOldSettingTextIpAddress(settingId, settingType, settingElement,
defaultValue, settingLabel);
else if (settingType == "number")
setting =
InitializeFromOldSettingNumber(settingId, settingElement, defaultValue, settingLabel);
else if (settingType == "video" || settingType == "audio" || settingType == "image" ||
settingType == "executable" || settingType == "file" || settingType == "folder")
setting = InitializeFromOldSettingPath(settingId, settingType, settingElement, defaultValue,
settingLabel);
else if (settingType == "date")
setting = InitializeFromOldSettingDate(settingId, settingElement, defaultValue, settingLabel);
else if (settingType == "time")
setting = InitializeFromOldSettingTime(settingId, settingElement, defaultValue, settingLabel);
else if (settingType == "select")
setting = InitializeFromOldSettingSelect(settingId, settingElement, defaultValue,
settingLabel, settingValues, settingLValues);
else if (settingType == "addon")
setting =
InitializeFromOldSettingAddon(settingId, settingElement, defaultValue, settingLabel);
else if (settingType == "enum" || settingType == "labelenum")
setting = InitializeFromOldSettingEnums(settingId, settingType, settingElement, defaultValue,
settingValues, settingLValues);
else if (settingType == "fileenum")
setting =
InitializeFromOldSettingFileEnum(settingId, settingElement, defaultValue, settingValues);
else if (settingType == "rangeofnum")
setting = InitializeFromOldSettingRangeOfNum(settingId, settingElement, defaultValue);
else if (settingType == "slider")
setting = InitializeFromOldSettingSlider(settingId, settingElement, defaultValue);
else if (settingType.empty())
{
// setting definitions without a type are considered as "text" / strings but are hidden
setting = InitializeFromOldSettingTextIpAddress(settingId, "text", settingElement,
defaultValue, settingLabel);
setting->SetLevel(SettingLevel::Internal);
}
else
{
m_logger->warn("failed to parse old setting definition for \"{}\" of type \"{}\"", settingId,
settingType);
}
// process general properties
if (setting != nullptr)
{
// set the default level to be Basic
if (setting->GetLevel() != SettingLevel::Internal)
{
setting->SetLevel(SettingLevel::Basic);
}
// use the setting's ID if there's no label
if (settingLabel < 0)
{
settingLabel = m_unknownSettingLabelId;
m_unknownSettingLabelId += 1;
m_unknownSettingLabels.emplace(settingLabel, settingId);
}
// set the setting's label
setting->SetLabel(settingLabel);
// handle subsettings
bool isSubsetting = false;
if (settingElement->QueryBoolAttribute("subsetting", &isSubsetting) == TIXML_SUCCESS &&
isSubsetting)
{
// find the last non-subsetting in the current group and use that as the parent setting
const auto groupSettings = group->GetSettings();
const auto parentSetting = std::find_if(
groupSettings.crbegin(), groupSettings.crend(),
[](const SettingConstPtr& setting) { return setting->GetParent().empty(); });
if (parentSetting != groupSettings.crend())
{
if ((*parentSetting)->IsReference())
setting->SetParent((*parentSetting)->GetReferencedId());
else
setting->SetParent((*parentSetting)->GetId());
}
}
SettingWithConditions settingWithConditions;
// parse enable status
const auto conditionEnable = XMLUtils::GetAttribute(settingElement, "enable");
if (StringUtils::EqualsNoCase(conditionEnable, "true"))
setting->SetEnabled(true);
else if (StringUtils::EqualsNoCase(conditionEnable, "false"))
setting->SetEnabled(false);
else if (!conditionEnable.empty())
settingWithConditions.enableCondition = conditionEnable;
// parse visible status
const auto conditionVisible = XMLUtils::GetAttribute(settingElement, "visible");
if (StringUtils::EqualsNoCase(conditionVisible, "true"))
setting->SetVisible(true);
else if (StringUtils::EqualsNoCase(conditionVisible, "false"))
setting->SetVisible(false);
else if (!conditionVisible.empty())
settingWithConditions.visibleCondition = conditionVisible;
// check if there already is a setting with the setting identifier
if (settingIds.find(settingId) != settingIds.end())
{
// turn the setting into a reference setting
setting->MakeReference();
}
else
{
// add the setting's identifier to the list of all identifiers
settingIds.insert(setting->GetId());
}
if (!settingWithConditions.enableCondition.empty() ||
!settingWithConditions.visibleCondition.empty())
{
settingWithConditions.setting = setting;
settingsWithConditions.push_back(settingWithConditions);
}
// add the setting to the list of settings from the same category
categorySettings.push_back(setting);
// add the setting to the current group
group->AddSetting(setting);
}
else
{
// add a dummy setting for the group / separator to the list of settings from the same category
categorySettings.push_back(nullptr);
}
// look for the next setting
settingElement = settingElement->NextSiblingElement("setting");
}
// process settings with enable/visible conditions
for (auto setting : settingsWithConditions)
{
if (!setting.enableCondition.empty())
{
CSettingDependency dependencyEnable(SettingDependencyType::Enable, GetSettingsManager());
if (ParseOldCondition(setting.setting, categorySettings, setting.enableCondition,
dependencyEnable))
setting.deps.push_back(dependencyEnable);
else
{
m_logger->warn(
"failed to parse enable condition \"{}\" of old setting definition for \"{}\"",
setting.enableCondition, setting.setting->GetId());
}
}
if (!setting.visibleCondition.empty())
{
CSettingDependency dependencyVisible(SettingDependencyType::Visible, GetSettingsManager());
if (ParseOldCondition(setting.setting, categorySettings, setting.visibleCondition,
dependencyVisible))
setting.deps.push_back(dependencyVisible);
else
{
m_logger->warn(
"failed to parse visible condition \"{}\" of old setting definition for \"{}\"",
setting.visibleCondition, setting.setting->GetId());
}
}
// set dependencies
setting.setting->SetDependencies(setting.deps);
}
return group;
}
std::shared_ptr<CSettingCategory> CAddonSettings::ParseOldCategoryElement(
uint32_t& categoryId, const TiXmlElement* categoryElement, std::set<std::string>& settingIds)
{
// create the category
auto category = std::make_shared<CSettingCategory>(StringUtils::Format("category{}", categoryId),
GetSettingsManager());
categoryId += 1;
// try to get the category's label and fall back to "General"
int categoryLabel = 128;
ParseOldLabel(categoryElement, g_localizeStrings.Get(categoryLabel), categoryLabel);
category->SetLabel(categoryLabel);
// prepare a setting group
auto group = ParseOldSettingElement(categoryElement, category, settingIds);
// add the group to the category
category->AddGroup(group);
return category;
}
bool CAddonSettings::InitializeFromOldSettingDefinitions(const CXBMCTinyXML& doc)
{
m_logger->debug("trying to load setting definitions from old format...");
const TiXmlElement* root = doc.RootElement();
if (root == nullptr)
return false;
std::shared_ptr<CSettingSection> section =
std::make_shared<CSettingSection>(m_addonId, GetSettingsManager());
std::shared_ptr<CSettingCategory> category;
uint32_t categoryId = 0;
// Settings id set
std::set<std::string> settingIds;
// Special case for no category settings
section->AddCategory(ParseOldCategoryElement(categoryId, root, settingIds));
const TiXmlElement* categoryElement = root->FirstChildElement("category");
while (categoryElement != nullptr)
{
section->AddCategory(ParseOldCategoryElement(categoryId, categoryElement, settingIds));
// look for the next category
categoryElement = categoryElement->NextSiblingElement("category");
}
// add the section to the settingsmanager
GetSettingsManager()->AddSection(section);
return true;
}
SettingPtr CAddonSettings::InitializeFromOldSettingAction(const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue)
{
// parse the action attribute
std::string action = XMLUtils::GetAttribute(settingElement, "action");
// replace $CWD with the url of the add-on
StringUtils::Replace(action, "$CWD", m_addonPath);
// replace $ID with the id of the add-on
StringUtils::Replace(action, "$ID", m_addonId);
// prepare the setting's control
auto control = std::make_shared<CSettingControlButton>();
control->SetFormat("action");
SettingPtr setting = nullptr;
// action settings don't require a setting id
if (settingId.empty())
{
auto actionSettingId = StringUtils::Format("action{}", m_unidentifiedSettingId);
m_unidentifiedSettingId += 1;
auto settingAction = std::make_shared<CSettingAction>(actionSettingId, GetSettingsManager());
settingAction->SetData(action);
setting = settingAction;
}
else
{
// assume that the setting might store a value as a string
auto settingString = std::make_shared<CSettingString>(settingId, GetSettingsManager());
settingString->SetDefault(defaultValue);
settingString->SetAllowEmpty(true);
control->SetActionData(action);
setting = settingString;
}
// get any options
std::string option = XMLUtils::GetAttribute(settingElement, "option");
// handle the "close" option
if (StringUtils::EqualsNoCase(option, "close"))
control->SetCloseDialog(true);
setting->SetControl(control);
return setting;
}
std::shared_ptr<CSetting> CAddonSettings::InitializeFromOldSettingLabel()
{
// label settings don't require a setting id
auto labelSettingId = StringUtils::Format("label{}", m_unidentifiedSettingId);
m_unidentifiedSettingId += 1;
auto settingLabel = std::make_shared<CSettingString>(labelSettingId, GetSettingsManager());
// create the setting's control
settingLabel->SetControl(std::make_shared<CSettingControlLabel>());
return settingLabel;
}
SettingPtr CAddonSettings::InitializeFromOldSettingBool(const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue)
{
auto setting = std::make_shared<CSettingBool>(settingId, GetSettingsManager());
if (setting->FromString(defaultValue))
setting->SetDefault(setting->GetValue());
setting->SetControl(std::make_shared<CSettingControlCheckmark>());
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingTextIpAddress(const std::string& settingId,
const std::string& settingType,
const TiXmlElement* settingElement,
const std::string& defaultValue,
const int settingLabel)
{
std::shared_ptr<CSettingString> setting;
auto control = std::make_shared<CSettingControlEdit>();
control->SetHeading(settingLabel);
// get any options
std::string option = XMLUtils::GetAttribute(settingElement, "option");
if (settingType == "ipaddress")
{
setting = std::make_shared<CSettingString>(settingId, GetSettingsManager());
control->SetFormat("ip");
}
else if (settingType == "text")
{
if (StringUtils::EqualsNoCase(option, "urlencoded"))
{
setting = std::make_shared<CSettingUrlEncodedString>(settingId, GetSettingsManager());
control->SetFormat("urlencoded");
}
else
{
setting = std::make_shared<CSettingString>(settingId, GetSettingsManager());
control->SetFormat("string");
control->SetHidden(StringUtils::EqualsNoCase(option, "hidden"));
}
}
setting->SetDefault(defaultValue);
setting->SetAllowEmpty(true);
setting->SetControl(control);
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingNumber(const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue,
const int settingLabel)
{
auto setting = std::make_shared<CSettingInt>(settingId, GetSettingsManager());
if (setting->FromString(defaultValue))
setting->SetDefault(setting->GetValue());
auto control = std::make_shared<CSettingControlEdit>();
control->SetHeading(settingLabel);
control->SetFormat("integer");
setting->SetControl(control);
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingPath(const std::string& settingId,
const std::string& settingType,
const TiXmlElement* settingElement,
const std::string& defaultValue,
const int settingLabel)
{
auto setting = std::make_shared<CSettingPath>(settingId, GetSettingsManager());
setting->SetDefault(defaultValue);
// parse sources/shares
const auto source = XMLUtils::GetAttribute(settingElement, "source");
if (!source.empty())
setting->SetSources({source});
// setup masking
const auto audioMask = CServiceBroker::GetFileExtensionProvider().GetMusicExtensions();
const auto videoMask = CServiceBroker::GetFileExtensionProvider().GetVideoExtensions();
const auto imageMask = CServiceBroker::GetFileExtensionProvider().GetPictureExtensions();
auto execMask = "";
#if defined(TARGET_WINDOWS)
execMask = ".exe|.bat|.cmd|.py";
#endif // defined(TARGET_WINDOWS)
std::string mask = XMLUtils::GetAttribute(settingElement, "mask");
if (!mask.empty())
{
// convert mask qualifiers
StringUtils::Replace(mask, "$AUDIO", audioMask);
StringUtils::Replace(mask, "$VIDEO", videoMask);
StringUtils::Replace(mask, "$IMAGE", imageMask);
StringUtils::Replace(mask, "$EXECUTABLE", execMask);
}
else
{
if (settingType == "video")
mask = videoMask;
else if (settingType == "audio")
mask = audioMask;
else if (settingType == "image")
mask = imageMask;
else if (settingType == "executable")
mask = execMask;
}
setting->SetMasking(mask);
// parse options
const auto option = XMLUtils::GetAttribute(settingElement, "option");
setting->SetWritable(StringUtils::EqualsNoCase(option, "writeable"));
auto control = std::make_shared<CSettingControlButton>();
if (settingType == "folder")
control->SetFormat("path");
else if (settingType == "image")
control->SetFormat("image");
else
{
control->SetFormat("file");
// parse the options
const auto options = StringUtils::Split(option, OldSettingValuesSeparator);
control->SetUseImageThumbs(std::find(options.cbegin(), options.cend(), "usethumbs") !=
options.cend());
control->SetUseFileDirectories(std::find(options.cbegin(), options.cend(), "treatasfolder") !=
options.cend());
}
control->SetHeading(settingLabel);
setting->SetControl(control);
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingDate(const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue,
const int settingLabel)
{
auto setting = std::make_shared<CSettingDate>(settingId, GetSettingsManager());
if (setting->FromString(defaultValue))
setting->SetDefault(setting->GetValue());
auto control = std::make_shared<CSettingControlButton>();
control->SetFormat("date");
control->SetHeading(settingLabel);
setting->SetControl(control);
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingTime(const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue,
const int settingLabel)
{
auto setting = std::make_shared<CSettingTime>(settingId, GetSettingsManager());
if (setting->FromString(defaultValue))
setting->SetDefault(setting->GetValue());
auto control = std::make_shared<CSettingControlButton>();
control->SetFormat("time");
control->SetHeading(settingLabel);
setting->SetControl(control);
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingSelect(
const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue,
const int settingLabel,
const std::string& settingValues,
const std::vector<std::string>& settingLValues)
{
// process values and lvalues
std::vector<std::string> values;
if (!settingLValues.empty())
values = settingLValues;
else
values = StringUtils::Split(settingValues, OldSettingValuesSeparator);
SettingPtr setting = nullptr;
if (!values.empty())
{
if (settingLValues.empty())
{
auto settingString = std::make_shared<CSettingString>(settingId, GetSettingsManager());
settingString->SetDefault(defaultValue);
StringSettingOptions options;
for (const auto& value : values)
options.push_back(StringSettingOption(value, value));
settingString->SetOptions(options);
setting = settingString;
}
else
{
auto settingInt = std::make_shared<CSettingInt>(settingId, GetSettingsManager());
if (settingInt->FromString(defaultValue))
settingInt->SetDefault(settingInt->GetValue());
TranslatableIntegerSettingOptions options;
for (uint32_t i = 0; i < values.size(); ++i)
options.push_back(TranslatableIntegerSettingOption(
static_cast<int>(strtol(values[i].c_str(), nullptr, 0)), i));
settingInt->SetTranslatableOptions(options);
setting = settingInt;
}
}
else
{
// parse sources/shares
const auto source = XMLUtils::GetAttribute(settingElement, "source");
if (!source.empty())
setting = InitializeFromOldSettingFileWithSource(settingId, settingElement, defaultValue,
settingValues);
else
m_logger->warn("failed to parse old setting definition for \"{}\" of type \"select\"",
settingId);
}
if (setting != nullptr)
{
auto control = std::make_shared<CSettingControlList>();
control->SetHeading(settingLabel);
control->SetFormat("string");
setting->SetControl(control);
}
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingAddon(const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue,
const int settingLabel)
{
// get addon types
std::string addonTypeStr = XMLUtils::GetAttribute(settingElement, "addontype");
const auto addonTypesStr = StringUtils::Split(addonTypeStr, ",");
std::set<AddonType> addonTypes;
for (auto addonType : addonTypesStr)
{
auto type = ADDON::CAddonInfo::TranslateType(StringUtils::Trim(addonType));
if (type != ADDON::AddonType::UNKNOWN)
addonTypes.insert(type);
}
if (addonTypes.empty())
{
m_logger->error("missing addon type for addon setting \"{}\"", settingId);
return nullptr;
}
// TODO: support multiple addon types
if (addonTypes.size() > 1)
{
m_logger->error("multiple addon types are not supported (addon setting \"{}\")", settingId);
return nullptr;
}
// parse addon ids
auto addonIds = StringUtils::Split(defaultValue, ",");
// parse multiselect option
bool multiselect = false;
settingElement->QueryBoolAttribute("multiselect", &multiselect);
// sanity check
if (addonIds.size() > 1 && !multiselect)
{
m_logger->warn("multiple default addon ids on non-multiselect addon setting \"{}\"", settingId);
addonIds.erase(++addonIds.begin(), addonIds.end());
}
auto settingAddon = std::make_shared<CSettingAddon>(settingId, GetSettingsManager());
settingAddon->SetAddonType(*addonTypes.begin());
SettingPtr setting = settingAddon;
if (multiselect)
{
auto settingList =
std::make_shared<CSettingList>(settingId, settingAddon, GetSettingsManager());
settingList->SetDelimiter(",");
if (settingList->FromString(addonIds))
settingList->SetDefault(settingList->GetValue());
setting = settingList;
}
else if (!addonIds.empty())
settingAddon->SetDefault(addonIds.front());
auto control = std::make_shared<CSettingControlButton>();
control->SetFormat("addon");
control->SetHeading(settingLabel);
setting->SetControl(control);
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingEnums(
const std::string& settingId,
const std::string& settingType,
const TiXmlElement* settingElement,
const std::string& defaultValue,
const std::string& settingValues,
const std::vector<std::string>& settingLValues)
{
// process values and lvalues
std::vector<std::string> values;
if (!settingLValues.empty())
values = settingLValues;
else if (settingValues == "$HOURS")
{
for (uint32_t hour = 0; hour < 24; hour++)
values.push_back(
CDateTime(2000, 1, 1, hour, 0, 0).GetAsLocalizedTime(g_langInfo.GetTimeFormat(), false));
}
else
values = StringUtils::Split(settingValues, OldSettingValuesSeparator);
// process entries
const auto settingEntries = StringUtils::Split(XMLUtils::GetAttribute(settingElement, "entries"),
OldSettingValuesSeparator);
// process sort
bool sortAscending = false;
std::string sort = XMLUtils::GetAttribute(settingElement, "sort");
if (sort == "true" || sort == "yes")
sortAscending = true;
SettingPtr setting = nullptr;
if (settingType == "enum")
{
auto settingInt = std::make_shared<CSettingInt>(settingId, GetSettingsManager());
if (settingLValues.empty())
{
IntegerSettingOptions options;
for (uint32_t i = 0; i < values.size(); ++i)
{
std::string label = values[i];
int value = i;
if (settingEntries.size() > i)
value = static_cast<int>(strtol(settingEntries[i].c_str(), nullptr, 0));
options.push_back(IntegerSettingOption(label, value));
}
settingInt->SetOptions(options);
}
else
{
TranslatableIntegerSettingOptions options;
for (uint32_t i = 0; i < values.size(); ++i)
{
int label = static_cast<int>(strtol(values[i].c_str(), nullptr, 0));
int value = i;
if (settingEntries.size() > i)
value = static_cast<int>(strtol(settingEntries[i].c_str(), nullptr, 0));
options.push_back(TranslatableIntegerSettingOption(label, value));
}
settingInt->SetTranslatableOptions(options);
}
if (sortAscending)
settingInt->SetOptionsSort(SettingOptionsSort::Ascending);
// set the default value
if (settingInt->FromString(defaultValue))
settingInt->SetDefault(settingInt->GetValue());
setting = settingInt;
}
else
{
auto settingString = std::make_shared<CSettingString>(settingId, GetSettingsManager());
if (settingLValues.empty())
{
StringSettingOptions options;
for (uint32_t i = 0; i < values.size(); ++i)
{
std::string value = values[i];
if (settingEntries.size() > i)
value = settingEntries[i];
options.push_back(StringSettingOption(value, value));
}
settingString->SetOptions(options);
}
else
{
TranslatableStringSettingOptions options;
for (uint32_t i = 0; i < values.size(); ++i)
{
int label = static_cast<int>(strtol(values[i].c_str(), nullptr, 0));
std::string value = g_localizeStrings.GetAddonString(m_addonId, label);
if (settingEntries.size() > i)
value = settingEntries[i];
options.push_back(std::make_pair(label, value));
}
settingString->SetTranslatableOptions(options);
}
if (sortAscending)
settingString->SetOptionsSort(SettingOptionsSort::Ascending);
// set the default value
settingString->SetDefault(defaultValue);
setting = settingString;
}
auto control = std::make_shared<CSettingControlSpinner>();
control->SetFormat("string");
setting->SetControl(control);
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingFileEnum(const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue,
const std::string& settingValues)
{
auto setting = InitializeFromOldSettingFileWithSource(settingId, settingElement, defaultValue,
settingValues);
auto control = std::make_shared<CSettingControlSpinner>();
control->SetFormat("string");
setting->SetControl(control);
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingRangeOfNum(const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue)
{
auto setting = std::make_shared<CSettingNumber>(settingId, GetSettingsManager());
if (setting->FromString(defaultValue))
setting->SetDefault(setting->GetValue());
// parse rangestart and rangeend
double rangeStart = 0.0, rangeEnd = 1.0;
settingElement->QueryDoubleAttribute("rangestart", &rangeStart);
settingElement->QueryDoubleAttribute("rangeend", &rangeEnd);
setting->SetMinimum(rangeStart);
setting->SetMaximum(rangeEnd);
// parse elements
uint32_t elements = 2;
settingElement->QueryUnsignedAttribute("elements", &elements);
if (elements > 1)
setting->SetStep((rangeEnd - rangeStart) / (elements - 1));
// parse valueformat
int valueFormat = -1;
settingElement->QueryIntAttribute("valueformat", &valueFormat);
auto control = std::make_shared<CSettingControlSpinner>();
control->SetFormat("string");
control->SetFormatLabel(valueFormat);
setting->SetControl(control);
return setting;
}
SettingPtr CAddonSettings::InitializeFromOldSettingSlider(const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue)
{
// parse range
double min = 0.0, max = 100.0, step = 1.0;
const auto range = StringUtils::Split(XMLUtils::GetAttribute(settingElement, "range"), ',');
if (range.size() > 1)
{
min = strtod(range[0].c_str(), nullptr);
if (range.size() > 2)
{
max = strtod(range[2].c_str(), nullptr);
step = strtod(range[1].c_str(), nullptr);
}
else
max = strtod(range[1].c_str(), nullptr);
}
// parse option
auto option = XMLUtils::GetAttribute(settingElement, "option");
if (option.empty() || StringUtils::EqualsNoCase(option, "float"))
{
auto setting = std::make_shared<CSettingNumber>(settingId, GetSettingsManager());
if (setting->FromString(defaultValue))
setting->SetDefault(setting->GetValue());
setting->SetMinimum(min);
setting->SetStep(step);
setting->SetMaximum(max);
auto control = std::make_shared<CSettingControlSlider>();
control->SetFormat("number");
control->SetPopup(false);
setting->SetControl(control);
return setting;
}
if (StringUtils::EqualsNoCase(option, "int") || StringUtils::EqualsNoCase(option, "percent"))
{
auto setting = std::make_shared<CSettingInt>(settingId, GetSettingsManager());
if (setting->FromString(defaultValue))
setting->SetDefault(setting->GetValue());
setting->SetMinimum(static_cast<int>(min));
setting->SetStep(static_cast<int>(step));
setting->SetMaximum(static_cast<int>(max));
auto control = std::make_shared<CSettingControlSlider>();
control->SetFormat(StringUtils::EqualsNoCase(option, "int") ? "integer" : "percentage");
control->SetPopup(false);
setting->SetControl(control);
return setting;
}
m_logger->warn("ignoring old setting definition for \"{}\" of type \"slider\" because of unknown "
"option \"{}\"",
settingId, option);
return nullptr;
}
SettingPtr CAddonSettings::InitializeFromOldSettingFileWithSource(
const std::string& settingId,
const TiXmlElement* settingElement,
const std::string& defaultValue,
std::string source)
{
auto setting = std::make_shared<CSettingPath>(settingId, GetSettingsManager());
setting->SetDefault(defaultValue);
if (source.find("$PROFILE") != std::string::npos)
StringUtils::Replace(source, "$PROFILE", m_addonProfile);
else
source = URIUtils::AddFileToFolder(m_addonPath, source);
setting->SetSources({source});
// process the path/file mask
setting->SetMasking(XMLUtils::GetAttribute(settingElement, "mask"));
// process option
std::string option = XMLUtils::GetAttribute(settingElement, "option");
setting->SetHideExtension(StringUtils::EqualsNoCase(option, "hideext"));
setting->SetOptionsFiller(FileEnumSettingOptionsFiller);
return setting;
}
bool CAddonSettings::LoadOldSettingValues(const CXBMCTinyXML& doc,
std::map<std::string, std::string>& settings) const
{
if (!doc.RootElement())
return false;
const TiXmlElement* category = doc.RootElement()->FirstChildElement("category");
if (category == nullptr)
category = doc.RootElement();
while (category != nullptr)
{
const TiXmlElement* setting = category->FirstChildElement("setting");
while (setting != nullptr)
{
const char* id = setting->Attribute("id");
const char* value = setting->Attribute("value");
if (id != nullptr && value != nullptr)
settings[id] = value;
setting = setting->NextSiblingElement("setting");
}
category = category->NextSiblingElement("category");
}
return !settings.empty();
}
bool CAddonSettings::ParseOldLabel(const TiXmlElement* element,
const std::string& settingId,
int& labelId)
{
labelId = -1;
if (element == nullptr)
return false;
// label value as a string
std::string labelString;
element->QueryStringAttribute("label", &labelString);
bool parsed = !labelString.empty();
// try to parse the label as a pure number, i.e. a localized string
if (parsed)
{
char* endptr;
labelId = std::strtol(labelString.c_str(), &endptr, 10);
if (endptr == nullptr || *endptr == '\0')
return true;
}
// make sure the label string is not empty
else
labelString = " ";
labelId = m_unknownSettingLabelId;
m_unknownSettingLabelId += 1;
m_unknownSettingLabels.emplace(labelId, labelString);
return parsed;
}
bool CAddonSettings::ParseOldCondition(const std::shared_ptr<const CSetting>& setting,
const std::vector<std::shared_ptr<const CSetting>>& settings,
const std::string& condition,
CSettingDependency& dependeny) const
{
if (setting == nullptr)
return false;
if (condition.empty())
return true;
// find the index of the setting in the list of all settings of the category
auto settingIt = std::find_if(settings.cbegin(), settings.cend(),
[setting](const SettingConstPtr& otherSetting) {
if (otherSetting == nullptr)
return false;
return setting->GetId() == otherSetting->GetId();
});
if (settingIt == settings.cend())
{
m_logger->warn("failed to parse old setting conditions \"{}\" for \"{}\"", condition,
setting->GetId());
return false;
}
int32_t currentSettingIndex = std::distance(settings.cbegin(), settingIt);
CSettingDependencyConditionCombinationPtr dependencyCombination;
std::vector<std::string> conditions;
if (condition.find('+') != std::string::npos)
{
StringUtils::Tokenize(condition, conditions, '+');
dependencyCombination = dependeny.And();
}
else
{
StringUtils::Tokenize(condition, conditions, '|');
dependencyCombination = dependeny.Or();
}
bool error = false;
for (const auto& cond : conditions)
{
ConditionExpression expression;
if (!ParseOldConditionExpression(cond, expression))
continue;
// determine the absolute setting index
int32_t absoluteSettingIndex = currentSettingIndex + expression.m_relativeSettingIndex;
// we cannot handle relative indices pointing to settings not belonging to the same category
if (absoluteSettingIndex < 0 || static_cast<size_t>(absoluteSettingIndex) >= settings.size())
{
m_logger->warn("cannot reference setting (relative index: {}; absolute index: {}) in another "
"category in old setting condition \"{}\" for \"{}\"",
expression.m_relativeSettingIndex, absoluteSettingIndex, cond,
setting->GetId());
error = true;
continue;
}
const SettingConstPtr& referencedSetting = settings.at(absoluteSettingIndex);
if (referencedSetting == nullptr)
{
m_logger->warn(
"cannot reference separator setting in old setting condition \"{}\" for \"{}\"", cond,
setting->GetId());
error = true;
continue;
}
// try to handle some odd cases where the setting is of type string but the comparison value references the index of the value in the list of options
if (referencedSetting->GetType() == SettingType::String &&
StringUtils::IsNaturalNumber(expression.m_value))
{
// try to parse the comparison value
size_t valueIndex = static_cast<size_t>(strtoul(expression.m_value.c_str(), nullptr, 10));
const auto referencedSettingString =
std::static_pointer_cast<const CSettingString>(referencedSetting);
switch (referencedSettingString->GetOptionsType())
{
case SettingOptionsType::Static:
{
const auto& options = referencedSettingString->GetOptions();
if (options.size() > valueIndex)
expression.m_value = options.at(valueIndex).value;
break;
}
case SettingOptionsType::StaticTranslatable:
{
const auto& options = referencedSettingString->GetTranslatableOptions();
if (options.size() > valueIndex)
expression.m_value = options.at(valueIndex).second;
break;
}
default:
break;
}
}
// add the condition to the value of the referenced setting
dependencyCombination->Add(std::make_shared<CSettingDependencyCondition>(
referencedSetting->GetId(), expression.m_value, expression.m_operator, expression.m_negated,
GetSettingsManager()));
}
// if the condition doesn't depend on other settings it might be an infobool expression
if (!error && dependencyCombination->GetOperations().empty() &&
dependencyCombination->GetValues().empty())
dependencyCombination->Add(std::make_shared<CSettingDependencyCondition>(
"InfoBool", condition, "", false, GetSettingsManager()));
return !error;
}
bool CAddonSettings::ParseOldConditionExpression(std::string str, ConditionExpression& expression)
{
StringUtils::Trim(str);
size_t posOpen = str.find('(');
size_t posSep = str.find(',', posOpen);
size_t posClose = str.find(')', posSep);
if (posOpen == std::string::npos || posSep == std::string::npos || posClose == std::string::npos)
return false;
auto op = str.substr(0, posOpen);
// check if the operator is negated
expression.m_negated = StringUtils::StartsWith(op, "!");
if (expression.m_negated)
op = op.substr(1);
// parse the operator
if (StringUtils::EqualsNoCase(op, "eq"))
expression.m_operator = SettingDependencyOperator::Equals;
else if (StringUtils::EqualsNoCase(op, "gt"))
expression.m_operator = SettingDependencyOperator::GreaterThan;
else if (StringUtils::EqualsNoCase(op, "lt"))
expression.m_operator = SettingDependencyOperator::LessThan;
else
return false;
expression.m_relativeSettingIndex = static_cast<int32_t>(
strtol(str.substr(posOpen + 1, posSep - posOpen - 1).c_str(), nullptr, 10));
expression.m_value = str.substr(posSep + 1, posClose - posSep - 1);
return true;
}
void CAddonSettings::FileEnumSettingOptionsFiller(const std::shared_ptr<const CSetting>& setting,
std::vector<StringSettingOption>& list,
std::string& current,
void* data)
{
if (setting == nullptr)
return;
auto settingPath = std::dynamic_pointer_cast<const CSettingPath>(setting);
if (settingPath == nullptr)
return;
if (settingPath->GetSources().empty())
return;
const std::string& masking = settingPath->GetMasking(CServiceBroker::GetFileExtensionProvider());
// fetch the matching files/directories
CFileItemList items;
XFILE::CDirectory::GetDirectory(settingPath->GetSources().front(), items, masking,
XFILE::DIR_FLAG_NO_FILE_DIRS);
// process the matching files/directories
for (const auto& item : items)
{
if ((masking == "/" && item->m_bIsFolder) || !item->m_bIsFolder)
{
if (settingPath->HideExtension())
item->RemoveExtension();
list.emplace_back(item->GetLabel(), item->GetLabel());
}
}
}
} // namespace ADDON
|