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
|
/*
* 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 "guilib/guiinfo/GUIInfoLabel.h"
#include "FileItem.h"
#include "GUIInfoManager.h"
#include "addons/Skin.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIListItem.h"
#include "guilib/LocalizeStrings.h"
#include "utils/StringUtils.h"
#include "utils/log.h"
using namespace KODI::GUILIB::GUIINFO;
CGUIInfoLabel::CGUIInfoLabel(const std::string &label, const std::string &fallback /*= ""*/, int context /*= 0*/)
{
SetLabel(label, fallback, context);
}
int CGUIInfoLabel::GetIntValue(int contextWindow) const
{
const std::string label = GetLabel(contextWindow);
if (!label.empty())
return strtol(label.c_str(), NULL, 10);
return 0;
}
void CGUIInfoLabel::SetLabel(const std::string &label, const std::string &fallback, int context /*= 0*/)
{
m_fallback = fallback;
Parse(label, context);
}
const std::string &CGUIInfoLabel::GetLabel(int contextWindow, bool preferImage, std::string *fallback /*= NULL*/) const
{
bool needsUpdate = m_dirty;
if (!m_info.empty())
{
CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager();
for (const auto &portion : m_info)
{
if (portion.m_info)
{
std::string infoLabel;
if (preferImage)
infoLabel = infoMgr.GetImage(portion.m_info, contextWindow, fallback);
if (infoLabel.empty())
infoLabel = infoMgr.GetLabel(portion.m_info, contextWindow, fallback);
needsUpdate |= portion.NeedsUpdate(infoLabel);
}
}
}
else
needsUpdate = !m_label.empty();
return CacheLabel(needsUpdate);
}
const std::string &CGUIInfoLabel::GetItemLabel(const CGUIListItem *item, bool preferImages, std::string *fallback /*= NULL*/) const
{
bool needsUpdate = m_dirty;
if (item->IsFileItem() && !m_info.empty())
{
CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager();
for (const auto &portion : m_info)
{
if (portion.m_info)
{
std::string infoLabel;
if (preferImages)
infoLabel = infoMgr.GetItemImage(item, 0, portion.m_info, fallback);
else
infoLabel = infoMgr.GetItemLabel(static_cast<const CFileItem *>(item), 0, portion.m_info, fallback);
needsUpdate |= portion.NeedsUpdate(infoLabel);
}
}
}
else
needsUpdate = !m_label.empty();
return CacheLabel(needsUpdate);
}
const std::string &CGUIInfoLabel::CacheLabel(bool rebuild) const
{
if (rebuild)
{
m_label.clear();
for (const auto &portion : m_info)
m_label += portion.Get();
m_dirty = false;
}
if (m_label.empty()) // empty label, use the fallback
return m_fallback;
return m_label;
}
bool CGUIInfoLabel::IsEmpty() const
{
return m_info.empty();
}
bool CGUIInfoLabel::IsConstant() const
{
return m_info.empty() || (m_info.size() == 1 && m_info[0].m_info == 0);
}
bool CGUIInfoLabel::ReplaceSpecialKeywordReferences(const std::string &strInput, const std::string &strKeyword, const StringReplacerFunc &func, std::string &strOutput)
{
// replace all $strKeyword[value] with resolved strings
const std::string dollarStrPrefix = "$" + strKeyword + "[";
size_t index = 0;
size_t startPos;
while ((startPos = strInput.find(dollarStrPrefix, index)) != std::string::npos)
{
size_t valuePos = startPos + dollarStrPrefix.size();
size_t endPos = StringUtils::FindEndBracket(strInput, '[', ']', valuePos);
if (endPos != std::string::npos)
{
if (index == 0) // first occurrence?
strOutput.clear();
strOutput.append(strInput, index, startPos - index); // append part from the left side
strOutput += func(strInput.substr(valuePos, endPos - valuePos)); // resolve and append value part
index = endPos + 1;
}
else
{
// if closing bracket is missing, report error and leave incomplete reference in
CLog::Log(LOGERROR, "Error parsing value - missing ']' in \"{}\"", strInput);
break;
}
}
if (index) // if we replaced anything
{
strOutput.append(strInput, index, std::string::npos); // append leftover from the right side
return true;
}
return false;
}
bool CGUIInfoLabel::ReplaceSpecialKeywordReferences(std::string &work, const std::string &strKeyword, const StringReplacerFunc &func)
{
std::string output;
if (ReplaceSpecialKeywordReferences(work, strKeyword, func, output))
{
work = std::move(output);
return true;
}
return false;
}
std::string LocalizeReplacer(const std::string &str)
{
std::string replace = g_localizeStringsTemp.Get(atoi(str.c_str()));
if (replace.empty())
replace = g_localizeStrings.Get(atoi(str.c_str()));
return replace;
}
std::string AddonReplacer(const std::string &str)
{
// assumes "addon.id #####"
size_t length = str.find(' ');
const std::string addonid = str.substr(0, length);
int stringid = atoi(str.substr(length + 1).c_str());
return g_localizeStrings.GetAddonString(addonid, stringid);
}
std::string NumberReplacer(const std::string &str)
{
return str;
}
std::string CGUIInfoLabel::ReplaceLocalize(const std::string &label)
{
std::string work(label);
ReplaceSpecialKeywordReferences(work, "LOCALIZE", LocalizeReplacer);
ReplaceSpecialKeywordReferences(work, "NUMBER", NumberReplacer);
return work;
}
std::string CGUIInfoLabel::ReplaceAddonStrings(std::string &&label)
{
ReplaceSpecialKeywordReferences(label, "ADDON", AddonReplacer);
return std::move(label);
}
enum EINFOFORMAT { NONE = 0, FORMATINFO, FORMATESCINFO, FORMATVAR, FORMATESCVAR };
typedef struct
{
const char *str;
EINFOFORMAT val;
} infoformat;
const static infoformat infoformatmap[] = {{ "$INFO[", FORMATINFO},
{ "$ESCINFO[", FORMATESCINFO},
{ "$VAR[", FORMATVAR},
{ "$ESCVAR[", FORMATESCVAR}};
void CGUIInfoLabel::Parse(const std::string &label, int context)
{
m_info.clear();
m_dirty = true;
// Step 1: Replace all $LOCALIZE[number] with the real string
std::string work = ReplaceLocalize(label);
// Step 2: Replace all $ADDON[id number] with the real string
work = ReplaceAddonStrings(std::move(work));
// Step 3: Find all $INFO[info,prefix,postfix] blocks
EINFOFORMAT format;
do
{
format = NONE;
size_t pos1 = work.size();
size_t pos2;
size_t len = 0;
for (const infoformat& infoformat : infoformatmap)
{
pos2 = work.find(infoformat.str);
if (pos2 != std::string::npos && pos2 < pos1)
{
pos1 = pos2;
len = strlen(infoformat.str);
format = infoformat.val;
}
}
if (format != NONE)
{
if (pos1 > 0)
m_info.emplace_back(0, work.substr(0, pos1), "");
pos2 = StringUtils::FindEndBracket(work, '[', ']', pos1 + len);
if (pos2 != std::string::npos)
{
// decipher the block
std::vector<std::string> params = StringUtils::Split(work.substr(pos1 + len, pos2 - pos1 - len), ",");
if (!params.empty())
{
CGUIInfoManager& infoMgr = CServiceBroker::GetGUI()->GetInfoManager();
int info;
if (format == FORMATVAR || format == FORMATESCVAR)
{
info = infoMgr.TranslateSkinVariableString(params[0], context);
if (info == 0)
info = infoMgr.RegisterSkinVariableString(g_SkinInfo->CreateSkinVariable(params[0], context));
if (info == 0) // skinner didn't define this conditional label!
CLog::Log(LOGWARNING, "Label Formatting: $VAR[{}] is not defined", params[0]);
}
else
info = infoMgr.TranslateString(params[0]);
std::string prefix, postfix;
if (params.size() > 1)
prefix = params[1];
if (params.size() > 2)
postfix = params[2];
m_info.emplace_back(info, prefix, postfix, format == FORMATESCINFO || format == FORMATESCVAR);
}
// and delete it from our work string
work.erase(0, pos2 + 1);
}
else
{
CLog::Log(LOGERROR, "Error parsing label - missing ']' in \"{}\"", label);
return;
}
}
}
while (format != NONE);
if (!work.empty())
m_info.emplace_back(0, work, "");
}
CGUIInfoLabel::CInfoPortion::CInfoPortion(int info, const std::string &prefix, const std::string &postfix, bool escaped /*= false */):
m_prefix(prefix),
m_postfix(postfix)
{
m_info = info;
m_escaped = escaped;
// filter our prefix and postfix for comma's
StringUtils::Replace(m_prefix, "$COMMA", ",");
StringUtils::Replace(m_postfix, "$COMMA", ",");
StringUtils::Replace(m_prefix, "$LBRACKET", "["); StringUtils::Replace(m_prefix, "$RBRACKET", "]");
StringUtils::Replace(m_postfix, "$LBRACKET", "["); StringUtils::Replace(m_postfix, "$RBRACKET", "]");
}
bool CGUIInfoLabel::CInfoPortion::NeedsUpdate(const std::string &label) const
{
if (m_label != label)
{
m_label = label;
return true;
}
return false;
}
std::string CGUIInfoLabel::CInfoPortion::Get() const
{
if (!m_info)
return m_prefix;
else if (m_label.empty())
return "";
std::string label = m_prefix + m_label + m_postfix;
if (m_escaped) // escape all quotes and backslashes, then quote
{
StringUtils::Replace(label, "\\", "\\\\");
StringUtils::Replace(label, "\"", "\\\"");
return "\"" + label + "\"";
}
return label;
}
std::string CGUIInfoLabel::GetLabel(const std::string &label, int contextWindow /*= 0*/, bool preferImage /*= false */)
{ // translate the label
const CGUIInfoLabel info(label, "", contextWindow);
return info.GetLabel(contextWindow, preferImage);
}
std::string CGUIInfoLabel::GetItemLabel(const std::string &label, const CGUIListItem *item, bool preferImage /*= false */)
{ // translate the label
const CGUIInfoLabel info(label);
return info.GetItemLabel(item, preferImage);
}
|