summaryrefslogtreecommitdiffstats
path: root/xbmc/settings/lib/SettingConditions.cpp
blob: 7146834f742b803b5c6a09b2bfb7f4d03545de4e (plain)
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
/*
 *  Copyright (C) 2013-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 "SettingConditions.h"

#include "SettingDefinitions.h"
#include "SettingsManager.h"
#include "utils/StringUtils.h"
#include "utils/XBMCTinyXML.h"

bool CSettingConditionItem::Deserialize(const TiXmlNode *node)
{
  if (!CBooleanLogicValue::Deserialize(node))
    return false;

  auto elem = node->ToElement();
  if (elem == nullptr)
    return false;

  // get the "name" attribute
  auto strAttribute = elem->Attribute(SETTING_XML_ATTR_NAME);
  if (strAttribute != nullptr)
    m_name = strAttribute;

  // get the "setting" attribute
  strAttribute = elem->Attribute(SETTING_XML_ATTR_SETTING);
  if (strAttribute != nullptr)
    m_setting = strAttribute;

  return true;
}

bool CSettingConditionItem::Check() const
{
  if (m_settingsManager == nullptr)
    return false;

  return m_settingsManager->GetConditions().Check(m_name, m_value, m_settingsManager->GetSetting(m_setting)) == !m_negated;
}

bool CSettingConditionCombination::Check() const
{
  bool ok = false;
  for (const auto& operation : m_operations)
  {
    if (operation == nullptr)
      continue;

    const auto combination = std::static_pointer_cast<const CSettingConditionCombination>(operation);
    if (combination == nullptr)
      continue;

    if (combination->Check())
      ok = true;
    else if (m_operation == BooleanLogicOperationAnd)
      return false;
  }

  for (const auto& value : m_values)
  {
    if (value == nullptr)
      continue;

    const auto condition = std::static_pointer_cast<const CSettingConditionItem>(value);
    if (condition == nullptr)
      continue;

    if (condition->Check())
      ok = true;
    else if (m_operation == BooleanLogicOperationAnd)
      return false;
  }

  return ok;
}

CSettingCondition::CSettingCondition(CSettingsManager *settingsManager /* = nullptr */)
  : ISettingCondition(settingsManager)
{
  m_operation = CBooleanLogicOperationPtr(new CSettingConditionCombination(settingsManager));
}

bool CSettingCondition::Check() const
{
  auto combination = std::static_pointer_cast<CSettingConditionCombination>(m_operation);
  if (combination == nullptr)
    return false;

  return combination->Check();
}

void CSettingConditionsManager::AddCondition(std::string condition)
{
  if (condition.empty())
    return;

  StringUtils::ToLower(condition);

  m_defines.insert(condition);
}

void CSettingConditionsManager::AddDynamicCondition(std::string identifier, SettingConditionCheck condition, void *data /*= nullptr*/)
{
  if (identifier.empty() || condition == nullptr)
    return;

  StringUtils::ToLower(identifier);

  m_conditions.emplace(identifier, std::make_pair(condition, data));
}

void CSettingConditionsManager::RemoveDynamicCondition(std::string identifier)
{
  if (identifier.empty())
    return;

  StringUtils::ToLower(identifier);

  auto it = m_conditions.find(identifier);
  if (it != m_conditions.end())
    m_conditions.erase(it);
}

bool CSettingConditionsManager::Check(
    std::string condition,
    const std::string& value /* = "" */,
    const std::shared_ptr<const CSetting>& setting /* = nullptr */) const
{
  if (condition.empty())
    return false;

  StringUtils::ToLower(condition);

  // special handling of "isdefined" conditions
  if (condition == "isdefined")
  {
    std::string tmpValue = value;
    StringUtils::ToLower(tmpValue);

    return m_defines.find(tmpValue) != m_defines.end();
  }

  auto conditionIt = m_conditions.find(condition);
  if (conditionIt == m_conditions.end())
    return false;

  return conditionIt->second.first(condition, value, setting, conditionIt->second.second);
}