Debian Codegraph @

xbmc/guilib/guiinfo/GUIInfoLabel.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2005-2018 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 "guilib/guiinfo/GUIInfoLabel.h"
10
11 #include "FileItem.h"
12 #include "GUIInfoManager.h"
13 #include "ServiceBroker.h"
14 #include "addons/Skin.h"
15 #include "games/GameServices.h"
16 #include "guilib/GUIComponent.h"
17 #include "guilib/GUIListItem.h"
18 #include "guilib/LocalizeStrings.h"
19 #include "utils/StringUtils.h"
20 #include "utils/log.h"
21
22 using namespace KODI::GUILIB::GUIINFO;
23
24 CGUIInfoLabel::CGUIInfoLabel(const std::string &label, const std::string &fallback /*= ""*/, int context /*= 0*/)
25 {
26 SetLabel(label, fallback, context);
27 }
28
29 int CGUIInfoLabel::GetIntValue(int contextWindow) const
30 {
31 const std::string label = GetLabel(contextWindow);
32 if (!label.empty())
33 return strtol(label.c_str(), NULL, 10);
34
35 return 0;
36 }
37
38 void CGUIInfoLabel::SetLabel(const std::string &label, const std::string &fallback, int context /*= 0*/)
39 {
40 Parse(label, m_infoLabel, context);
41 Parse(fallback, m_infoFallback, context);
42 m_fallback = fallback;
43 }
44
45 bool CGUIInfoLabel::LabelNeedsUpdate(int context,
46 bool preferImages,
47 std::string* fallback,
48 const std::vector<CInfoPortion>& infoPortion) const
49 {
50 bool needsUpdate{false};
51 CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager();
52 for (const auto& portion : infoPortion)
53 {
54 if (portion.m_info)
55 {
56 std::string infoLabel;
57 if (preferImages)
58 infoLabel = infoMgr.GetImage(portion.m_info, context, fallback);
59 if (infoLabel.empty())
60 infoLabel = infoMgr.GetLabel(portion.m_info, context, fallback);
61 needsUpdate |= portion.NeedsUpdate(infoLabel);
62 }
63 }
64 return needsUpdate;
65 }
66
67 const std::string& CGUIInfoLabel::GetLabel(int contextWindow,
68 bool preferImage,
69 std::string* fallback /*= NULL*/) const
70 {
71 bool needsUpdate = m_dirty;
72 if (!m_infoLabel.empty())
73 {
74 needsUpdate |= LabelNeedsUpdate(contextWindow, preferImage, fallback, m_infoLabel);
75 }
76 else
77 {
78 needsUpdate = !m_label.empty();
79 }
80
81 if (!m_infoFallback.empty())
82 {
83 needsUpdate |=
84 LabelNeedsUpdate(contextWindow, preferImage, fallback, m_infoFallback) && m_label.empty();
85 }
86
87 return CacheLabel(needsUpdate);
88 }
89
90 bool CGUIInfoLabel::ItemLabelNeedsUpdate(const CGUIListItem* item,
91 bool preferImages,
92 std::string* fallback,
93 const std::vector<CInfoPortion>& infoPortion) const
94 {
95 bool needsUpdate{false};
96 CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager();
97 for (const auto& portion : infoPortion)
98 {
99 if (portion.m_info)
100 {
101 std::string infoLabel;
102 if (preferImages)
103 infoLabel = infoMgr.GetItemImage(item, 0, portion.m_info, fallback);
104 else
105 infoLabel =
106 infoMgr.GetItemLabel(static_cast<const CFileItem*>(item), 0, portion.m_info, fallback);
107 needsUpdate |= portion.NeedsUpdate(infoLabel);
108 }
109 }
110 return needsUpdate;
111 }
112
113 const std::string& CGUIInfoLabel::GetItemLabel(const CGUIListItem* item,
114 bool preferImages,
115 std::string* fallback /*= nullptr */) const
116 {
117 bool needsUpdate = m_dirty;
118 if (item->IsFileItem())
119 {
120 if (!m_infoLabel.empty())
121 needsUpdate |= ItemLabelNeedsUpdate(item, preferImages, fallback, m_infoLabel);
122
123 if (!m_infoFallback.empty())
124 needsUpdate |= ItemLabelNeedsUpdate(item, preferImages, fallback, m_infoFallback);
125 }
126 else
127 {
128 needsUpdate = !m_label.empty();
129 }
130
131 return CacheLabel(needsUpdate);
132 }
133
134 void CGUIInfoLabel::RebuildLabel(std::string& label,
135 const std::vector<CInfoPortion>& infoPortion) const
136 {
137 label.clear();
138 for (const auto& portion : infoPortion)
139 {
140 label += portion.Get();
141 }
142 }
143
144 const std::string &CGUIInfoLabel::CacheLabel(bool rebuild) const
145 {
146 if (rebuild)
147 {
148 RebuildLabel(m_label, m_infoLabel);
149 RebuildLabel(m_fallback, m_infoFallback);
150 m_dirty = false;
151 }
152 if (m_label.empty()) // empty label, use the fallback
153 {
154 return m_fallback;
155 }
156
157 return m_label;
158 }
159
160 bool CGUIInfoLabel::IsEmpty() const
161 {
162 return m_infoLabel.empty();
163 }
164
165 bool CGUIInfoLabel::IsConstant() const
166 {
167 return m_infoLabel.empty() || (m_infoLabel.size() == 1 && m_infoLabel[0].m_info == 0);
168 }
169
170 bool CGUIInfoLabel::ReplaceSpecialKeywordReferences(const std::string &strInput, const std::string &strKeyword, const StringReplacerFunc &func, std::string &strOutput)
171 {
172 // replace all $strKeyword[value] with resolved strings
173 const std::string dollarStrPrefix = "$" + strKeyword + "[";
174
175 size_t index = 0;
176 size_t startPos;
177
178 while ((startPos = strInput.find(dollarStrPrefix, index)) != std::string::npos)
179 {
180 size_t valuePos = startPos + dollarStrPrefix.size();
181 size_t endPos = StringUtils::FindEndBracket(strInput, '[', ']', valuePos);
182 if (endPos != std::string::npos)
183 {
184 if (index == 0) // first occurrence?
185 strOutput.clear();
186 strOutput.append(strInput, index, startPos - index); // append part from the left side
187 strOutput += func(strInput.substr(valuePos, endPos - valuePos)); // resolve and append value part
188 index = endPos + 1;
189 }
190 else
191 {
192 // if closing bracket is missing, report error and leave incomplete reference in
193 CLog::Log(LOGERROR, "Error parsing value - missing ']' in \"{}\"", strInput);
194 break;
195 }
196 }
197
198 if (index) // if we replaced anything
199 {
200 strOutput.append(strInput, index, std::string::npos); // append leftover from the right side
201 return true;
202 }
203
204 return false;
205 }
206
207 bool CGUIInfoLabel::ReplaceSpecialKeywordReferences(std::string &work, const std::string &strKeyword, const StringReplacerFunc &func)
208 {
209 std::string output;
210 if (ReplaceSpecialKeywordReferences(work, strKeyword, func, output))
211 {
212 work = std::move(output);
213 return true;
214 }
215 return false;
216 }
217
218 std::string LocalizeReplacer(const std::string &str)
219 {
220 std::string replace = g_localizeStringsTemp.Get(atoi(str.c_str()));
221 if (replace.empty())
222 replace = g_localizeStrings.Get(atoi(str.c_str()));
223 return replace;
224 }
225
226 std::string AddonReplacer(const std::string &str)
227 {
228 // assumes "addon.id #####"
229 size_t length = str.find(' ');
230 const std::string addonid = str.substr(0, length);
231 int stringid = atoi(str.substr(length + 1).c_str());
232 return g_localizeStrings.GetAddonString(addonid, stringid);
233 }
234
235 std::string ControllerFeatureReplacer(const std::string& str)
236 {
237 // assumes "feature name,controller ID"
238 const size_t length = str.find(',');
239 const std::string featureName = str.substr(0, length);
240 const std::string controllerId = str.substr(length + 1);
241
242 return CServiceBroker::GetGameServices().TranslateFeature(controllerId, featureName);
243 }
244
245 std::string NumberReplacer(const std::string &str)
246 {
247 return str;
248 }
249
250 std::string CGUIInfoLabel::ReplaceLocalize(const std::string &label)
251 {
252 std::string work(label);
253 ReplaceSpecialKeywordReferences(work, "LOCALIZE", LocalizeReplacer);
254 ReplaceSpecialKeywordReferences(work, "NUMBER", NumberReplacer);
255 return work;
256 }
257
258 std::string CGUIInfoLabel::ReplaceAddonStrings(std::string &&label)
259 {
260 ReplaceSpecialKeywordReferences(label, "ADDON", AddonReplacer);
261 return std::move(label);
262 }
263
264 std::string CGUIInfoLabel::ReplaceControllerStrings(std::string&& label)
265 {
266 ReplaceSpecialKeywordReferences(label, "FEATURE", ControllerFeatureReplacer);
267 return std::move(label);
268 }
269
270 enum EINFOFORMAT { NONE = 0, FORMATINFO, FORMATESCINFO, FORMATVAR, FORMATESCVAR };
271
272 typedef struct
273 {
274 const char *str;
275 EINFOFORMAT val;
276 } infoformat;
277
278 const static infoformat infoformatmap[] = {{ "$INFO[", FORMATINFO},
279 { "$ESCINFO[", FORMATESCINFO},
280 { "$VAR[", FORMATVAR},
281 { "$ESCVAR[", FORMATESCVAR}};
282
283 void CGUIInfoLabel::Parse(const std::string& label,
284 std::vector<CInfoPortion>& infoPortion,
285 int context)
286 {
287 infoPortion.clear();
288 m_dirty = true;
289 // Step 1: Replace all $LOCALIZE[number] with the real string
290 std::string work = ReplaceLocalize(label);
291 // Step 2: Replace all $ADDON[id number] with the real string
292 work = ReplaceAddonStrings(std::move(work));
293 // Step 3: Replace all game controller strings with the real string
294 work = ReplaceControllerStrings(std::move(work));
295 // Step 4: Find all $INFO[info,prefix,postfix] blocks
296 EINFOFORMAT format;
297 do
298 {
299 format = NONE;
300 size_t pos1 = work.size();
301 size_t pos2;
302 size_t len = 0;
303 for (const infoformat& infoformat : infoformatmap)
304 {
305 pos2 = work.find(infoformat.str);
306 if (pos2 != std::string::npos && pos2 < pos1)
307 {
308 pos1 = pos2;
309 len = strlen(infoformat.str);
310 format = infoformat.val;
311 }
312 }
313
314 if (format != NONE)
315 {
316 if (pos1 > 0)
317 infoPortion.emplace_back(0, work.substr(0, pos1), "");
318
319 pos2 = StringUtils::FindEndBracket(work, '[', ']', pos1 + len);
320 if (pos2 != std::string::npos)
321 {
322 // decipher the block
323 std::vector<std::string> params = StringUtils::Split(work.substr(pos1 + len, pos2 - pos1 - len), ",");
324 if (!params.empty())
325 {
326 CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager();
327
328 int info;
329 if (format == FORMATVAR || format == FORMATESCVAR)
330 {
331 info = infoMgr.TranslateSkinVariableString(params[0], context);
332 if (info == 0)
333 info = infoMgr.RegisterSkinVariableString(g_SkinInfo->CreateSkinVariable(params[0], context));
334 if (info == 0) // skinner didn't define this conditional label!
335 CLog::Log(LOGWARNING, "Label Formatting: $VAR[{}] is not defined", params[0]);
336 }
337 else
338 info = infoMgr.TranslateString(params[0]);
339 std::string prefix, postfix;
340 if (params.size() > 1)
341 prefix = params[1];
342 if (params.size() > 2)
343 postfix = params[2];
344 infoPortion.emplace_back(info, prefix, postfix,
345 format == FORMATESCINFO || format == FORMATESCVAR);
346 }
347 // and delete it from our work string
348 work.erase(0, pos2 + 1);
349 }
350 else
351 {
352 CLog::Log(LOGERROR, "Error parsing label - missing ']' in \"{}\"", label);
353 return;
354 }
355 }
356 }
357 while (format != NONE);
358
359 if (!work.empty())
360 infoPortion.emplace_back(0, work, "");
361 }
362
363 CGUIInfoLabel::CInfoPortion::CInfoPortion(int info, const std::string &prefix, const std::string &postfix, bool escaped /*= false */):
364 m_prefix(prefix),
365 m_postfix(postfix)
366 {
367 m_info = info;
368 m_escaped = escaped;
369 // filter our prefix and postfix for comma's
370 StringUtils::Replace(m_prefix, "$COMMA", ",");
371 StringUtils::Replace(m_postfix, "$COMMA", ",");
372 StringUtils::Replace(m_prefix, "$LBRACKET", "["); StringUtils::Replace(m_prefix, "$RBRACKET", "]");
373 StringUtils::Replace(m_postfix, "$LBRACKET", "["); StringUtils::Replace(m_postfix, "$RBRACKET", "]");
374 }
375
376 bool CGUIInfoLabel::CInfoPortion::NeedsUpdate(const std::string &label) const
377 {
378 if (m_label != label)
379 {
380 m_label = label;
381 return true;
382 }
383 return false;
384 }
385
386 std::string CGUIInfoLabel::CInfoPortion::Get() const
387 {
388 if (!m_info)
389 return m_prefix;
390 else if (m_label.empty())
391 return "";
392 std::string label = m_prefix + m_label + m_postfix;
393 if (m_escaped) // escape all quotes and backslashes, then quote
394 {
395 StringUtils::Replace(label, "\\", "\\\\");
396 StringUtils::Replace(label, "\"", "\\\"");
397 return "\"" + label + "\"";
398 }
399 return label;
400 }
401
402 std::string CGUIInfoLabel::GetLabel(const std::string &label, int contextWindow /*= 0*/, bool preferImage /*= false */)
403 { // translate the label
404 const CGUIInfoLabel info(label, "", contextWindow);
405 return info.GetLabel(contextWindow, preferImage);
406 }
407
408 std::string CGUIInfoLabel::GetItemLabel(const std::string &label, const CGUIListItem *item, bool preferImage /*= false */)
409 { // translate the label
410 const CGUIInfoLabel info(label);
411 return info.GetItemLabel(item, preferImage);
412 }