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
|
/*
* Copyright (C) 2005-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 "GUIIncludes.h"
#include "GUIInfoManager.h"
#include "addons/Skin.h"
#include "guilib/GUIComponent.h"
#include "guilib/guiinfo/GUIInfoLabel.h"
#include "interfaces/info/SkinVariable.h"
#include "utils/StringUtils.h"
#include "utils/XBMCTinyXML.h"
#include "utils/XMLUtils.h"
#include "utils/log.h"
using namespace KODI::GUILIB;
CGUIIncludes::CGUIIncludes()
{
m_constantAttributes.insert("x");
m_constantAttributes.insert("y");
m_constantAttributes.insert("width");
m_constantAttributes.insert("height");
m_constantAttributes.insert("center");
m_constantAttributes.insert("max");
m_constantAttributes.insert("min");
m_constantAttributes.insert("w");
m_constantAttributes.insert("h");
m_constantAttributes.insert("time");
m_constantAttributes.insert("acceleration");
m_constantAttributes.insert("delay");
m_constantAttributes.insert("start");
m_constantAttributes.insert("end");
m_constantAttributes.insert("center");
m_constantAttributes.insert("border");
m_constantAttributes.insert("repeat");
m_constantNodes.insert("posx");
m_constantNodes.insert("posy");
m_constantNodes.insert("left");
m_constantNodes.insert("centerleft");
m_constantNodes.insert("right");
m_constantNodes.insert("centerright");
m_constantNodes.insert("top");
m_constantNodes.insert("centertop");
m_constantNodes.insert("bottom");
m_constantNodes.insert("centerbottom");
m_constantNodes.insert("width");
m_constantNodes.insert("height");
m_constantNodes.insert("offsetx");
m_constantNodes.insert("offsety");
m_constantNodes.insert("textoffsetx");
m_constantNodes.insert("textoffsety");
m_constantNodes.insert("textwidth");
m_constantNodes.insert("spinposx");
m_constantNodes.insert("spinposy");
m_constantNodes.insert("spinwidth");
m_constantNodes.insert("spinheight");
m_constantNodes.insert("radioposx");
m_constantNodes.insert("radioposy");
m_constantNodes.insert("radiowidth");
m_constantNodes.insert("radioheight");
m_constantNodes.insert("sliderwidth");
m_constantNodes.insert("sliderheight");
m_constantNodes.insert("itemgap");
m_constantNodes.insert("bordersize");
m_constantNodes.insert("timeperimage");
m_constantNodes.insert("fadetime");
m_constantNodes.insert("pauseatend");
m_constantNodes.insert("depth");
m_constantNodes.insert("movement");
m_constantNodes.insert("focusposition");
m_expressionAttributes.insert("condition");
m_expressionNodes.insert("visible");
m_expressionNodes.insert("enable");
m_expressionNodes.insert("usealttexture");
m_expressionNodes.insert("selected");
}
CGUIIncludes::~CGUIIncludes() = default;
void CGUIIncludes::Clear()
{
m_includes.clear();
m_defaults.clear();
m_constants.clear();
m_skinvariables.clear();
m_files.clear();
m_expressions.clear();
}
void CGUIIncludes::Load(const std::string &file)
{
if (!Load_Internal(file))
return;
FlattenExpressions();
FlattenSkinVariableConditions();
}
bool CGUIIncludes::Load_Internal(const std::string &file)
{
// check to see if we already have this loaded
if (HasLoaded(file))
return true;
CXBMCTinyXML doc;
if (!doc.LoadFile(file))
{
CLog::Log(LOGINFO, "Error loading include file {}: {} (row: {}, col: {})", file,
doc.ErrorDesc(), doc.ErrorRow(), doc.ErrorCol());
return false;
}
TiXmlElement *root = doc.RootElement();
if (!root || !StringUtils::EqualsNoCase(root->Value(), "includes"))
{
CLog::Log(LOGERROR, "Error loading include file {}: Root element <includes> required.", file);
return false;
}
// load components
LoadDefaults(root);
LoadConstants(root);
LoadExpressions(root);
LoadVariables(root);
LoadIncludes(root);
m_files.push_back(file);
return true;
}
void CGUIIncludes::LoadDefaults(const TiXmlElement *node)
{
if (!node)
return;
const TiXmlElement* child = node->FirstChildElement("default");
while (child)
{
const char *type = child->Attribute("type");
if (type && child->FirstChild())
m_defaults.insert(std::make_pair(type, *child));
child = child->NextSiblingElement("default");
}
}
void CGUIIncludes::LoadExpressions(const TiXmlElement *node)
{
if (!node)
return;
const TiXmlElement* child = node->FirstChildElement("expression");
while (child)
{
const char *tagName = child->Attribute("name");
if (tagName && child->FirstChild())
m_expressions.insert(std::make_pair(tagName, "[" + child->FirstChild()->ValueStr() + "]"));
child = child->NextSiblingElement("expression");
}
}
void CGUIIncludes::LoadConstants(const TiXmlElement *node)
{
if (!node)
return;
const TiXmlElement* child = node->FirstChildElement("constant");
while (child)
{
const char *tagName = child->Attribute("name");
if (tagName && child->FirstChild())
m_constants.insert(std::make_pair(tagName, child->FirstChild()->ValueStr()));
child = child->NextSiblingElement("constant");
}
}
void CGUIIncludes::LoadVariables(const TiXmlElement *node)
{
if (!node)
return;
const TiXmlElement* child = node->FirstChildElement("variable");
while (child)
{
const char *tagName = child->Attribute("name");
if (tagName && child->FirstChild())
m_skinvariables.insert(std::make_pair(tagName, *child));
child = child->NextSiblingElement("variable");
}
}
void CGUIIncludes::LoadIncludes(const TiXmlElement *node)
{
if (!node)
return;
const TiXmlElement* child = node->FirstChildElement("include");
while (child)
{
const char *tagName = child->Attribute("name");
if (tagName && child->FirstChild())
{
// we'll parse and store parameter list with defaults when include definition is first encountered
// if there's a <definition> tag only use its body as the actually included part
const TiXmlElement *definitionTag = child->FirstChildElement("definition");
const TiXmlElement *includeBody = definitionTag ? definitionTag : child;
// if there's a <param> tag there also must be a <definition> tag
Params defaultParams;
bool haveParamTags = GetParameters(child, "default", defaultParams);
if (haveParamTags && !definitionTag)
CLog::Log(LOGWARNING, "Skin has invalid include definition: {}", tagName);
else
m_includes.insert({ tagName, { *includeBody, std::move(defaultParams) } });
}
else if (child->Attribute("file"))
{
std::string file = g_SkinInfo->GetSkinPath(child->Attribute("file"));
const char *condition = child->Attribute("condition");
if (condition)
{ // load include file if condition evals to true
if (CServiceBroker::GetGUI()->GetInfoManager().Register(condition)->Get(
INFO::DEFAULT_CONTEXT))
Load_Internal(file);
}
else
Load_Internal(file);
}
child = child->NextSiblingElement("include");
}
}
void CGUIIncludes::FlattenExpressions()
{
for (auto& expression : m_expressions)
{
std::vector<std::string> resolved = std::vector<std::string>();
resolved.push_back(expression.first);
FlattenExpression(expression.second, resolved);
}
}
void CGUIIncludes::FlattenExpression(std::string &expression, const std::vector<std::string> &resolved)
{
std::string original(expression);
GUIINFO::CGUIInfoLabel::ReplaceSpecialKeywordReferences(expression, "EXP", [&](const std::string &expressionName) -> std::string {
if (std::find(resolved.begin(), resolved.end(), expressionName) != resolved.end())
{
CLog::Log(LOGERROR, "Skin has a circular expression \"{}\": {}", resolved.back(), original);
return std::string();
}
auto it = m_expressions.find(expressionName);
if (it == m_expressions.end())
return std::string();
std::vector<std::string> rescopy = resolved;
rescopy.push_back(expressionName);
FlattenExpression(it->second, rescopy);
return it->second;
});
}
void CGUIIncludes::FlattenSkinVariableConditions()
{
for (auto& variable : m_skinvariables)
{
TiXmlElement* valueNode = variable.second.FirstChildElement("value");
while (valueNode)
{
const char *condition = valueNode->Attribute("condition");
if (condition)
valueNode->SetAttribute("condition", ResolveExpressions(condition));
valueNode = valueNode->NextSiblingElement("value");
}
}
}
bool CGUIIncludes::HasLoaded(const std::string &file) const
{
for (const auto& loadedFile : m_files)
{
if (loadedFile == file)
return true;
}
return false;
}
void CGUIIncludes::Resolve(TiXmlElement *node, std::map<INFO::InfoPtr, bool>* xmlIncludeConditions /* = NULL */)
{
if (!node)
return;
SetDefaults(node);
ResolveConstants(node);
ResolveExpressions(node);
ResolveIncludes(node, xmlIncludeConditions);
TiXmlElement *child = node->FirstChildElement();
while (child)
{
// recursive call
Resolve(child, xmlIncludeConditions);
child = child->NextSiblingElement();
}
}
void CGUIIncludes::SetDefaults(TiXmlElement *node)
{
if (node->ValueStr() != "control")
return;
std::string type = XMLUtils::GetAttribute(node, "type");
const auto it = m_defaults.find(type);
if (it != m_defaults.end())
{
// we don't insert <left> et. al. if <posx> or <posy> is specified
bool hasPosX(node->FirstChild("posx") != nullptr);
bool hasPosY(node->FirstChild("posy") != nullptr);
const TiXmlElement &element = (*it).second;
const TiXmlElement *tag = element.FirstChildElement();
while (tag)
{
std::string value = tag->ValueStr();
bool skip(false);
if (hasPosX && (value == "left" || value == "right" || value == "centerleft" || value == "centerright"))
skip = true;
if (hasPosY && (value == "top" || value == "bottom" || value == "centertop" || value == "centerbottom"))
skip = true;
// we insert at the end of block
if (!skip)
node->InsertEndChild(*tag);
tag = tag->NextSiblingElement();
}
}
}
void CGUIIncludes::ResolveConstants(TiXmlElement *node)
{
if (!node)
return;
TiXmlNode *child = node->FirstChild();
if (child && child->Type() == TiXmlNode::TINYXML_TEXT && m_constantNodes.count(node->ValueStr()))
{
child->SetValue(ResolveConstant(child->ValueStr()));
}
else
{
TiXmlAttribute *attribute = node->FirstAttribute();
while (attribute)
{
if (m_constantAttributes.count(attribute->Name()))
attribute->SetValue(ResolveConstant(attribute->ValueStr()));
attribute = attribute->Next();
}
}
}
void CGUIIncludes::ResolveExpressions(TiXmlElement *node)
{
if (!node)
return;
TiXmlNode *child = node->FirstChild();
if (child && child->Type() == TiXmlNode::TINYXML_TEXT && m_expressionNodes.count(node->ValueStr()))
{
child->SetValue(ResolveExpressions(child->ValueStr()));
}
else
{
TiXmlAttribute *attribute = node->FirstAttribute();
while (attribute)
{
if (m_expressionAttributes.count(attribute->Name()))
attribute->SetValue(ResolveExpressions(attribute->ValueStr()));
attribute = attribute->Next();
}
}
}
void CGUIIncludes::ResolveIncludes(TiXmlElement *node, std::map<INFO::InfoPtr, bool>* xmlIncludeConditions /* = NULL */)
{
if (!node)
return;
TiXmlElement *include = node->FirstChildElement("include");
while (include)
{
// file: load includes from specified XML file
const char *file = include->Attribute("file");
if (file)
Load(g_SkinInfo->GetSkinPath(file));
// condition: process include if condition evals to true
const char *condition = include->Attribute("condition");
if (condition)
{
INFO::InfoPtr conditionID =
CServiceBroker::GetGUI()->GetInfoManager().Register(ResolveExpressions(condition));
bool value = false;
if (conditionID)
{
value = conditionID->Get(INFO::DEFAULT_CONTEXT);
if (xmlIncludeConditions)
xmlIncludeConditions->insert(std::make_pair(conditionID, value));
}
if (!value)
{
include = include->NextSiblingElement("include");
continue;
}
}
Params params;
std::string tagName;
// determine which form of include call we have
const char *name = include->Attribute("content");
if (name)
{
// <include content="MyControl" />
// or
// <include content="MyControl">
// <param name="posx" value="225" />
// <param name="posy">150</param>
// ...
// </include>
tagName = name;
GetParameters(include, "value", params);
}
else
{
const TiXmlNode *child = include->FirstChild();
if (child && child->Type() == TiXmlNode::TINYXML_TEXT)
{
// <include>MyControl</include>
// old-style includes for backward compatibility
tagName = child->ValueStr();
}
}
// check, whether the include exists and therefore should be replaced by its definition
auto it = m_includes.find(tagName);
if (it != m_includes.end())
{
const TiXmlElement *includeDefinition = &it->second.first;
// combine passed include parameters with their default values into a single list (no overwrites)
const Params& defaultParams = it->second.second;
params.insert(defaultParams.begin(), defaultParams.end());
// process include definition
const TiXmlElement *includeDefinitionChild = includeDefinition->FirstChildElement();
while (includeDefinitionChild)
{
// insert before <include> element to keep order of occurrence in xml file
TiXmlElement *insertedNode = static_cast<TiXmlElement*>(node->InsertBeforeChild(include, *includeDefinitionChild));
// process nested
InsertNested(node, include, insertedNode);
// resolve parameters even if parameter list is empty (to remove param references)
ResolveParametersForNode(insertedNode, params);
includeDefinitionChild = includeDefinitionChild->NextSiblingElement();
}
// remove the include element itself
node->RemoveChild(include);
include = node->FirstChildElement("include");
}
else
{ // invalid include
CLog::Log(LOGWARNING, "Skin has invalid include: {}", tagName);
include = include->NextSiblingElement("include");
}
}
}
void CGUIIncludes::InsertNested(TiXmlElement *controls, TiXmlElement *include, TiXmlElement *node)
{
TiXmlElement *target;
TiXmlElement *nested;
if (node->ValueStr() == "nested")
{
nested = node;
target = controls;
}
else
{
nested = node->FirstChildElement("nested");
target = node;
}
if (nested)
{
// copy all child elements except param elements
const TiXmlElement *child = include->FirstChildElement();
while (child)
{
if (child->ValueStr() != "param")
{
// insert before <nested> element to keep order of occurrence in xml file
target->InsertBeforeChild(nested, *child);
}
child = child->NextSiblingElement();
}
if (nested != node)
target->RemoveChild(nested);
}
}
bool CGUIIncludes::GetParameters(const TiXmlElement *include, const char *valueAttribute, Params& params)
{
bool foundAny = false;
// collect parameters from include tag
// <include content="MyControl">
// <param name="posx" value="225" /> <!-- comments and other tags are ignored here -->
// <param name="posy">150</param>
// ...
// </include>
if (include)
{
const TiXmlElement *param = include->FirstChildElement("param");
foundAny = param != NULL; // doesn't matter if param isn't entirely valid
while (param)
{
std::string paramName = XMLUtils::GetAttribute(param, "name");
if (!paramName.empty())
{
std::string paramValue;
// <param name="posx" value="120" />
const char *value = param->Attribute(valueAttribute); // try attribute first
if (value)
paramValue = value;
else
{
// <param name="posx">120</param>
const TiXmlNode *child = param->FirstChild();
if (child && child->Type() == TiXmlNode::TINYXML_TEXT)
paramValue = child->ValueStr(); // and then tag value
}
params.insert({ paramName, paramValue }); // no overwrites
}
param = param->NextSiblingElement("param");
}
}
return foundAny;
}
void CGUIIncludes::ResolveParametersForNode(TiXmlElement *node, const Params& params)
{
if (!node)
return;
std::string newValue;
// run through this element's attributes, resolving any parameters
TiXmlAttribute *attribute = node->FirstAttribute();
while (attribute)
{
ResolveParamsResult result = ResolveParameters(attribute->ValueStr(), newValue, params);
if (result == SINGLE_UNDEFINED_PARAM_RESOLVED && strcmp(node->Value(), "param") == 0 &&
strcmp(attribute->Name(), "value") == 0 && node->Parent() && strcmp(node->Parent()->Value(), "include") == 0)
{
// special case: passing <param name="someName" value="$PARAM[undefinedParam]" /> to the nested include
// this usually happens when trying to forward a missing parameter from the enclosing include to the nested include
// problem: since 'undefinedParam' is not defined, it expands to <param name="someName" value="" /> and overrides any default value set with <param name="someName" default="someValue" /> in the nested include
// to prevent this, we'll completely remove this parameter from the nested include call so that the default value can be correctly picked up later
node->Parent()->RemoveChild(node);
return;
}
else if (result != NO_PARAMS_FOUND)
attribute->SetValue(newValue);
attribute = attribute->Next();
}
// run through this element's value and children, resolving any parameters
TiXmlNode *child = node->FirstChild();
if (child)
{
if (child->Type() == TiXmlNode::TINYXML_TEXT)
{
ResolveParamsResult result = ResolveParameters(child->ValueStr(), newValue, params);
if (result == SINGLE_UNDEFINED_PARAM_RESOLVED && strcmp(node->Value(), "param") == 0 &&
node->Parent() && strcmp(node->Parent()->Value(), "include") == 0)
{
// special case: passing <param name="someName">$PARAM[undefinedParam]</param> to the nested include
// we'll remove the offending param tag same as above
node->Parent()->RemoveChild(node);
}
else if (result != NO_PARAMS_FOUND)
child->SetValue(newValue);
}
else if (child->Type() == TiXmlNode::TINYXML_ELEMENT ||
child->Type() == TiXmlNode::TINYXML_COMMENT)
{
do
{
// save next as current child might be removed from the tree
TiXmlElement* next = child->NextSiblingElement();
if (child->Type() == TiXmlNode::TINYXML_ELEMENT)
ResolveParametersForNode(static_cast<TiXmlElement*>(child), params);
child = next;
}
while (child);
}
}
}
class ParamReplacer
{
const std::map<std::string, std::string>& m_params;
// keep some stats so that we know exactly what's been resolved
int m_numTotalParams = 0;
int m_numUndefinedParams = 0;
public:
explicit ParamReplacer(const std::map<std::string, std::string>& params) : m_params(params) {}
int GetNumTotalParams() const { return m_numTotalParams; }
int GetNumDefinedParams() const { return m_numTotalParams - m_numUndefinedParams; }
int GetNumUndefinedParams() const { return m_numUndefinedParams; }
std::string operator()(const std::string ¶mName)
{
m_numTotalParams++;
std::map<std::string, std::string>::const_iterator it = m_params.find(paramName);
if (it != m_params.end())
return it->second;
m_numUndefinedParams++;
return "";
}
};
CGUIIncludes::ResolveParamsResult CGUIIncludes::ResolveParameters(const std::string& strInput, std::string& strOutput, const Params& params)
{
ParamReplacer paramReplacer(params);
if (GUIINFO::CGUIInfoLabel::ReplaceSpecialKeywordReferences(strInput, "PARAM", std::ref(paramReplacer), strOutput))
// detect special input values of the form "$PARAM[undefinedParam]" (with no extra characters around)
return paramReplacer.GetNumUndefinedParams() == 1 && paramReplacer.GetNumTotalParams() == 1 && strOutput.empty() ? SINGLE_UNDEFINED_PARAM_RESOLVED : PARAMS_RESOLVED;
return NO_PARAMS_FOUND;
}
std::string CGUIIncludes::ResolveConstant(const std::string &constant) const
{
std::vector<std::string> values = StringUtils::Split(constant, ",");
for (auto& i : values)
{
std::map<std::string, std::string>::const_iterator it = m_constants.find(i);
if (it != m_constants.end())
i = it->second;
}
return StringUtils::Join(values, ",");
}
std::string CGUIIncludes::ResolveExpressions(const std::string &expression) const
{
std::string work(expression);
GUIINFO::CGUIInfoLabel::ReplaceSpecialKeywordReferences(work, "EXP", [&](const std::string &str) -> std::string {
std::map<std::string, std::string>::const_iterator it = m_expressions.find(str);
if (it != m_expressions.end())
return it->second;
return "";
});
return work;
}
const INFO::CSkinVariableString* CGUIIncludes::CreateSkinVariable(const std::string& name, int context)
{
std::map<std::string, TiXmlElement>::const_iterator it = m_skinvariables.find(name);
if (it != m_skinvariables.end())
return INFO::CSkinVariable::CreateFromXML(it->second, context);
return NULL;
}
|