| 1 |
/* |
| 2 |
* Copyright (C) 2022 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 "SkinTimerManager.h" |
| 10 |
|
| 11 |
#include "GUIInfoManager.h" |
| 12 |
#include "guilib/GUIAction.h" |
| 13 |
#include "guilib/GUIComponent.h" |
| 14 |
#include "utils/StringUtils.h" |
| 15 |
#include "utils/XBMCTinyXML2.h" |
| 16 |
#include "utils/log.h" |
| 17 |
|
| 18 |
#include <chrono> |
| 19 |
|
| 20 |
using namespace std::chrono_literals; |
| 21 |
|
| 22 |
CSkinTimerManager::CSkinTimerManager(CGUIInfoManager& infoMgr) : m_infoMgr{infoMgr} |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
void CSkinTimerManager::LoadTimers(const std::string& path) |
| 27 |
{ |
| 28 |
CXBMCTinyXML2 doc; |
| 29 |
if (!doc.LoadFile(path)) |
| 30 |
{ |
| 31 |
CLog::LogF(LOGWARNING, "Could not load timers file {}: {} (Line: {})", path, doc.ErrorStr(), |
| 32 |
doc.ErrorLineNum()); |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
auto* root = doc.RootElement(); |
| 37 |
if (!root || !StringUtils::EqualsNoCase(root->Value(), "timers")) |
| 38 |
{ |
| 39 |
CLog::LogF(LOGERROR, "Error loading timers file {}: Root element <timers> required.", path); |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
const auto* timerNode = root->FirstChildElement("timer"); |
| 44 |
while (timerNode) |
| 45 |
{ |
| 46 |
LoadTimerInternal(timerNode); |
| 47 |
timerNode = timerNode->NextSiblingElement("timer"); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
void CSkinTimerManager::LoadTimerInternal(const tinyxml2::XMLNode* node) |
| 52 |
{ |
| 53 |
if ((!node->FirstChildElement("name") || !node->FirstChildElement("name")->FirstChild())) |
| 54 |
{ |
| 55 |
CLog::LogF(LOGERROR, "Missing required field 'name' for valid skin timer. Ignoring timer."); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
std::string timerName = node->FirstChildElement("name")->FirstChild()->Value(); |
| 60 |
if (TimerExists(timerName)) |
| 61 |
{ |
| 62 |
CLog::LogF(LOGWARNING, |
| 63 |
"Ignoring timer with name {} - another timer with the same name already exists", |
| 64 |
timerName); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
// timer start |
| 69 |
INFO::InfoPtr startInfo{nullptr}; |
| 70 |
bool resetOnStart{false}; |
| 71 |
if (node->FirstChildElement("start") && node->FirstChildElement("start")->FirstChild()) |
| 72 |
{ |
| 73 |
startInfo = m_infoMgr.Register(node->FirstChildElement("start")->FirstChild()->Value()); |
| 74 |
// check if timer needs to be reset after start |
| 75 |
if (node->FirstChildElement("start")->Attribute("reset") && |
| 76 |
StringUtils::EqualsNoCase(node->FirstChildElement("start")->Attribute("reset"), "true")) |
| 77 |
{ |
| 78 |
resetOnStart = true; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// timer reset |
| 83 |
INFO::InfoPtr resetInfo{nullptr}; |
| 84 |
if (node->FirstChildElement("reset") && node->FirstChildElement("reset")->FirstChild()) |
| 85 |
{ |
| 86 |
resetInfo = m_infoMgr.Register(node->FirstChildElement("reset")->FirstChild()->Value()); |
| 87 |
} |
| 88 |
// timer stop |
| 89 |
INFO::InfoPtr stopInfo{nullptr}; |
| 90 |
if (node->FirstChildElement("stop") && node->FirstChildElement("stop")->FirstChild()) |
| 91 |
{ |
| 92 |
stopInfo = m_infoMgr.Register(node->FirstChildElement("stop")->FirstChild()->Value()); |
| 93 |
} |
| 94 |
|
| 95 |
// process onstart actions |
| 96 |
CGUIAction startActions; |
| 97 |
startActions.EnableSendThreadMessageMode(); |
| 98 |
const auto* onStartElement = node->FirstChildElement("onstart"); |
| 99 |
while (onStartElement) |
| 100 |
{ |
| 101 |
if (onStartElement->FirstChild()) |
| 102 |
{ |
| 103 |
const std::string conditionalActionAttribute = |
| 104 |
onStartElement->Attribute("condition") != nullptr ? onStartElement->Attribute("condition") |
| 105 |
: ""; |
| 106 |
startActions.Append(CGUIAction::CExecutableAction{conditionalActionAttribute, |
| 107 |
onStartElement->FirstChild()->Value()}); |
| 108 |
} |
| 109 |
onStartElement = onStartElement->NextSiblingElement("onstart"); |
| 110 |
} |
| 111 |
|
| 112 |
// process onstop actions |
| 113 |
CGUIAction stopActions; |
| 114 |
stopActions.EnableSendThreadMessageMode(); |
| 115 |
const auto* onStopElement = node->FirstChildElement("onstop"); |
| 116 |
while (onStopElement) |
| 117 |
{ |
| 118 |
if (onStopElement->FirstChild()) |
| 119 |
{ |
| 120 |
const std::string conditionalActionAttribute = |
| 121 |
onStopElement->Attribute("condition") != nullptr ? onStopElement->Attribute("condition") |
| 122 |
: ""; |
| 123 |
stopActions.Append(CGUIAction::CExecutableAction{conditionalActionAttribute, |
| 124 |
onStopElement->FirstChild()->Value()}); |
| 125 |
} |
| 126 |
onStopElement = onStopElement->NextSiblingElement("onstop"); |
| 127 |
} |
| 128 |
|
| 129 |
m_timers[timerName] = std::make_unique<CSkinTimer>(CSkinTimer( |
| 130 |
timerName, startInfo, resetInfo, stopInfo, startActions, stopActions, resetOnStart)); |
| 131 |
} |
| 132 |
|
| 133 |
bool CSkinTimerManager::TimerIsRunning(const std::string& timer) const |
| 134 |
{ |
| 135 |
if (auto iter = m_timers.find(timer); iter != m_timers.end()) |
| 136 |
{ |
| 137 |
return iter->second->IsRunning(); |
| 138 |
} |
| 139 |
CLog::LogF(LOGERROR, "Couldn't find Skin Timer with name: {}", timer); |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
float CSkinTimerManager::GetTimerElapsedSeconds(const std::string& timer) const |
| 144 |
{ |
| 145 |
if (auto iter = m_timers.find(timer); iter != m_timers.end()) |
| 146 |
{ |
| 147 |
return iter->second->GetElapsedSeconds(); |
| 148 |
} |
| 149 |
CLog::LogF(LOGERROR, "Couldn't find Skin Timer with name: {}", timer); |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
void CSkinTimerManager::TimerStart(const std::string& timer) const |
| 154 |
{ |
| 155 |
if (auto iter = m_timers.find(timer); iter != m_timers.end()) |
| 156 |
{ |
| 157 |
return iter->second->Start(); |
| 158 |
} |
| 159 |
CLog::LogF(LOGERROR, "Couldn't find Skin Timer with name: {}", timer); |
| 160 |
} |
| 161 |
|
| 162 |
void CSkinTimerManager::TimerStop(const std::string& timer) const |
| 163 |
{ |
| 164 |
if (auto iter = m_timers.find(timer); iter != m_timers.end()) |
| 165 |
{ |
| 166 |
return iter->second->Stop(); |
| 167 |
} |
| 168 |
CLog::LogF(LOGERROR, "Couldn't find Skin Timer with name: {}", timer); |
| 169 |
} |
| 170 |
|
| 171 |
size_t CSkinTimerManager::GetTimerCount() const |
| 172 |
{ |
| 173 |
return m_timers.size(); |
| 174 |
} |
| 175 |
|
| 176 |
bool CSkinTimerManager::TimerExists(const std::string& timer) const |
| 177 |
{ |
| 178 |
return m_timers.count(timer) != 0; |
| 179 |
} |
| 180 |
|
| 181 |
std::unique_ptr<CSkinTimer> CSkinTimerManager::GrabTimer(const std::string& timer) |
| 182 |
{ |
| 183 |
if (auto iter = m_timers.find(timer); iter != m_timers.end()) |
| 184 |
{ |
| 185 |
auto timerInstance = std::move(iter->second); |
| 186 |
m_timers.erase(iter); |
| 187 |
return timerInstance; |
| 188 |
} |
| 189 |
return {}; |
| 190 |
} |
| 191 |
|
| 192 |
void CSkinTimerManager::Stop() |
| 193 |
{ |
| 194 |
// skintimers, as infomanager clients register info conditions/expressions in the infomanager. |
| 195 |
// The infomanager is linked to skins, being initialized or cleared when |
| 196 |
// skins are loaded (or unloaded). All the registered boolean conditions from |
| 197 |
// skin timers will end up being removed when the skin is unloaded. However, to |
| 198 |
// self-contain this component unregister them all here. |
| 199 |
for (auto const& [key, val] : m_timers) |
| 200 |
{ |
| 201 |
const std::unique_ptr<CSkinTimer>::pointer timer = val.get(); |
| 202 |
if (timer->GetStartCondition()) |
| 203 |
{ |
| 204 |
m_infoMgr.UnRegister(timer->GetStartCondition()); |
| 205 |
} |
| 206 |
if (timer->GetStopCondition()) |
| 207 |
{ |
| 208 |
m_infoMgr.UnRegister(timer->GetStopCondition()); |
| 209 |
} |
| 210 |
if (timer->GetResetCondition()) |
| 211 |
{ |
| 212 |
m_infoMgr.UnRegister(timer->GetResetCondition()); |
| 213 |
} |
| 214 |
} |
| 215 |
m_timers.clear(); |
| 216 |
} |
| 217 |
|
| 218 |
void CSkinTimerManager::Process() |
| 219 |
{ |
| 220 |
for (const auto& [key, val] : m_timers) |
| 221 |
{ |
| 222 |
const std::unique_ptr<CSkinTimer>::pointer timer = val.get(); |
| 223 |
if (!timer->IsRunning() && timer->VerifyStartCondition()) |
| 224 |
{ |
| 225 |
timer->Start(); |
| 226 |
} |
| 227 |
else if (timer->IsRunning() && timer->VerifyStopCondition()) |
| 228 |
{ |
| 229 |
timer->Stop(); |
| 230 |
} |
| 231 |
if (timer->GetElapsedSeconds() > 0 && timer->VerifyResetCondition()) |
| 232 |
{ |
| 233 |
timer->Reset(); |
| 234 |
} |
| 235 |
} |
| 236 |
} |