forked from OpenZWave/open-zwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalization.cpp
More file actions
executable file
·775 lines (696 loc) · 26.5 KB
/
Copy pathLocalization.cpp
File metadata and controls
executable file
·775 lines (696 loc) · 26.5 KB
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//-----------------------------------------------------------------------------
//
// Localization.cpp
//
// Localization for CC and Value Classes
//
// Copyright (c) 2018 Justin Hammond <justin@dynam.ac>
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include <bitset>
#include <string.h>
#include "Localization.h"
#include "tinyxml.h"
#include "Options.h"
#include "platform/Log.h"
#include "value_classes/ValueBitSet.h"
#include "command_classes/Configuration.h"
#include "command_classes/ThermostatSetpoint.h"
#include "command_classes/SoundSwitch.h"
#include "command_classes/Meter.h"
namespace OpenZWave
{
namespace Internal
{
Localization *Localization::m_instance = NULL;
std::map<uint64, std::shared_ptr<ValueLocalizationEntry> > Localization::m_valueLocalizationMap;
std::map<uint8, std::shared_ptr<LabelLocalizationEntry> > Localization::m_commandClassLocalizationMap;
std::map<std::string, std::shared_ptr<LabelLocalizationEntry> > Localization::m_globalLabelLocalizationMap;
std::string Localization::m_selectedLang = "";
uint32 Localization::m_revision = 0;
LabelLocalizationEntry::LabelLocalizationEntry(uint16 _index, uint32 _pos) :
m_index(_index), m_pos(_pos)
{
}
void LabelLocalizationEntry::AddLabel(string label, string lang)
{
if (lang.empty())
m_defaultLabel = label;
else
m_Label[lang] = label;
}
uint64 LabelLocalizationEntry::GetIdx()
{
uint64 key = ((uint64) m_index << 32) | ((uint64) m_pos);
return key;
}
std::string LabelLocalizationEntry::GetLabel(string lang)
{
if (lang.empty() || (m_Label.find(lang) == m_Label.end()))
return m_defaultLabel;
else
return m_Label[lang];
}
bool LabelLocalizationEntry::HasLabel(string lang)
{
if (m_Label.find(lang) == m_Label.end())
return false;
else
return true;
}
ValueLocalizationEntry::ValueLocalizationEntry(uint8 _commandClass, uint16 _index, uint32 _pos) :
m_commandClass(_commandClass), m_index(_index), m_pos(_pos)
{
}
uint64 ValueLocalizationEntry::GetIdx()
{
uint64 key = ((uint64) m_commandClass << 48) | ((uint64) m_index << 32) | ((uint64) m_pos);
return key;
}
std::string ValueLocalizationEntry::GetHelp(string lang)
{
if (lang.empty() || (m_HelpText.find(lang) == m_HelpText.end()))
return m_DefaultHelpText;
else
return m_HelpText[lang];
}
bool ValueLocalizationEntry::HasHelp(string lang)
{
if (m_HelpText.find(lang) == m_HelpText.end())
return false;
else
return true;
}
void ValueLocalizationEntry::AddHelp(string HelpText, string lang)
{
if (lang.empty())
m_DefaultHelpText = HelpText;
else
m_HelpText[lang] = HelpText;
}
std::string ValueLocalizationEntry::GetLabel(string lang)
{
if (lang.empty() || (m_LabelText.find(lang) == m_LabelText.end()))
return m_DefaultLabelText;
else
return m_LabelText[lang];
}
bool ValueLocalizationEntry::HasLabel(string lang)
{
if (m_LabelText.find(lang) == m_LabelText.end())
return false;
else
return true;
}
void ValueLocalizationEntry::AddLabel(string Label, string lang)
{
if (lang.empty())
m_DefaultLabelText = Label;
else
m_LabelText[lang] = Label;
}
void ValueLocalizationEntry::AddItemLabel(string label, int32 itemindex, string lang)
{
if (lang.empty())
{
m_DefaultItemLabelText[itemindex] = label;
}
else
{
m_ItemLabelText[lang][itemindex] = label;
}
}
std::string ValueLocalizationEntry::GetItemLabel(string lang, int32 itemindex)
{
if (lang.empty() || (m_ItemLabelText.find(lang) == m_ItemLabelText.end()) || m_ItemLabelText[lang].find(itemindex) == m_ItemLabelText[lang].end())
{
if (m_DefaultItemLabelText.find(itemindex) == m_DefaultItemLabelText.end())
{
Log::Write(LogLevel_Warning, "ValueLocalizationEntry::GetItemLabel: Unable to find Default Item Label Text for Index Item %d (%s)", itemindex, m_DefaultLabelText.c_str());
return "undefined";
}
return m_DefaultItemLabelText[itemindex];
}
else
{
return m_ItemLabelText[lang][itemindex];
}
}
bool ValueLocalizationEntry::HasItemLabel(int32 itemIndex, string lang)
{
if (lang.empty() || (m_ItemLabelText.find(lang) == m_ItemLabelText.end()) || m_ItemLabelText[lang].find(itemIndex) == m_ItemLabelText[lang].end())
return false;
return true;
}
void ValueLocalizationEntry::AddItemHelp(string label, int32 itemindex, string lang)
{
if (lang.empty())
{
m_DefaultItemHelpText[itemindex] = label;
}
else
{
m_ItemHelpText[lang][itemindex] = label;
}
}
std::string ValueLocalizationEntry::GetItemHelp(string lang, int32 itemindex)
{
if (lang.empty() && (m_DefaultItemHelpText.find(itemindex) != m_DefaultItemHelpText.end()))
{
return m_DefaultItemHelpText[itemindex];
}
if ((m_ItemHelpText.find(lang) != m_ItemHelpText.end()))
{
if ((m_ItemHelpText.at(lang).find(itemindex) != m_ItemHelpText.at(lang).end()))
{
return m_ItemHelpText.at(lang)[itemindex];
}
}
if (m_DefaultItemHelpText.find(itemindex) != m_DefaultItemHelpText.end())
{
return m_DefaultItemHelpText[itemindex];
}
Log::Write(LogLevel_Warning, "No ItemHelp Entry for Language %s (Index %d)", lang.c_str(), itemindex);
return "Undefined";
}
bool ValueLocalizationEntry::HasItemHelp(int32 itemIndex, string lang)
{
if (lang.empty() && (m_DefaultItemHelpText.find(itemIndex) != m_DefaultItemHelpText.end()))
{
return true;
}
if ((m_ItemHelpText.find(lang) != m_ItemHelpText.end()))
{
if ((m_ItemHelpText.at(lang).find(itemIndex) != m_ItemHelpText.at(lang).end()))
{
return true;
}
return false;
}
return false;
}
Localization::Localization()
{
}
void Localization::ReadXML()
{
// Parse the Z-Wave manufacturer and product XML file.
string configPath;
Options::Get()->GetOptionAsString("ConfigPath", &configPath);
string path = configPath + "Localization.xml";
TiXmlDocument* pDoc = new TiXmlDocument();
if (!pDoc->LoadFile(path.c_str(), TIXML_ENCODING_UTF8))
{
Log::Write(LogLevel_Warning, "Unable to load Localization file %s: %s", path.c_str(), pDoc->ErrorDesc());
delete pDoc;
return;
}
pDoc->SetUserData((void*) path.c_str());
Log::Write(LogLevel_Info, "Loading Localization File %s", path.c_str());
TiXmlElement const* root = pDoc->RootElement();
char const *str = root->Value();
if (str && !strcmp(str, "Localization"))
{
// Read in the revision attributes
str = root->Attribute("Revision");
if (!str)
{
Log::Write(LogLevel_Info, "Error in Product Config file at line %d - missing Revision attribute", root->Row());
delete pDoc;
return;
}
m_revision = atol(str);
}
TiXmlElement const* CCElement = root->FirstChildElement();
while (CCElement)
{
char const* str = CCElement->Value();
char* pStopChar;
if (str && !strcmp(str, "CommandClass"))
{
str = CCElement->Attribute("id");
if (!str)
{
Log::Write(LogLevel_Warning, "Localization::ReadXML: Error in %s at line %d - missing commandclass ID attribute", CCElement->GetDocument()->GetUserData(), CCElement->Row());
CCElement = CCElement->NextSiblingElement();
continue;
}
uint8 ccID = (uint8) strtol(str, &pStopChar, 10);
TiXmlElement const* nextElement = CCElement->FirstChildElement();
while (nextElement)
{
str = nextElement->Value();
if (str && !strcmp(str, "Label"))
{
ReadCCXMLLabel(ccID, nextElement);
}
if (str && !strcmp(str, "Value"))
{
/* when node = 0, its a Localization that applies to all nodes. */
ReadXMLValue(0, ccID, nextElement);
}
nextElement = nextElement->NextSiblingElement();
}
}
else if (str && !strcmp(str, "GlobalText"))
{
TiXmlElement const* nextElement = CCElement->FirstChildElement();
while (nextElement)
{
str = nextElement->Value();
if (str && !strcmp(str, "Label"))
{
ReadGlobalXMLLabel(nextElement);
}
nextElement = nextElement->NextSiblingElement();
}
}
CCElement = CCElement->NextSiblingElement();
}
Log::Write(LogLevel_Info, "Loaded %s With Revision %d", pDoc->GetUserData(), m_revision);
delete pDoc;
}
void Localization::ReadGlobalXMLLabel(const TiXmlElement *labelElement)
{
string Language;
char const *str = labelElement->Attribute("name");
if (!str)
{
Log::Write(LogLevel_Warning, "Localization::ReadGlobalXMLLabel: Error in %s at line %d - missing GlobalText name attribute", labelElement->GetDocument()->GetUserData(), labelElement->Row());
return;
}
if (labelElement->Attribute("lang"))
Language = labelElement->Attribute("lang");
if (m_globalLabelLocalizationMap.find(str) == m_globalLabelLocalizationMap.end())
{
m_globalLabelLocalizationMap[str] = std::shared_ptr<LabelLocalizationEntry>(new LabelLocalizationEntry(0));
}
else if (m_globalLabelLocalizationMap[str]->HasLabel(Language))
{
Log::Write(LogLevel_Warning, "Localization::ReadGlobalXMLLabel: Error in %s at line %d - Duplicate Entry for GlobalText %s: %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), str, labelElement->GetText(), Language.c_str());
return;
}
if (Language.empty())
{
m_globalLabelLocalizationMap[str]->AddLabel(labelElement->GetText());
}
else
{
m_globalLabelLocalizationMap[str]->AddLabel(labelElement->GetText(), Language);
}
}
void Localization::ReadCCXMLLabel(uint8 ccID, const TiXmlElement *labelElement)
{
string Language;
if (labelElement->Attribute("lang"))
Language = labelElement->Attribute("lang");
if (m_commandClassLocalizationMap.find(ccID) == m_commandClassLocalizationMap.end())
{
m_commandClassLocalizationMap[ccID] = std::shared_ptr<LabelLocalizationEntry>(new LabelLocalizationEntry(0));
}
else if (m_commandClassLocalizationMap[ccID]->HasLabel(Language))
{
Log::Write(LogLevel_Warning, "Localization::ReadXMLLabel: Error in %s at line %d - Duplicate Entry for CommandClass %d: %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), ccID, labelElement->GetText(), Language.c_str());
return;
}
if (Language.empty())
{
m_commandClassLocalizationMap[ccID]->AddLabel(labelElement->GetText());
}
else
{
m_commandClassLocalizationMap[ccID]->AddLabel(labelElement->GetText(), Language);
}
}
void Localization::ReadXMLValue(uint8 node, uint8 ccID, const TiXmlElement *valueElement)
{
char const* str = valueElement->Attribute("index");
if (!str)
{
Log::Write(LogLevel_Info, "Localization::ReadXMLValue: Error in %s at line %d - missing Index attribute", valueElement->GetDocument()->GetUserData(), valueElement->Row());
return;
}
char* pStopChar;
uint16 indexId = (uint16) strtol(str, &pStopChar, 10);
uint32 pos = -1;
str = valueElement->Attribute("pos");
if (str)
{
pos = (uint32) strtol(str, &pStopChar, 10);
}
TiXmlElement const* valueIDElement = valueElement->FirstChildElement();
while (valueIDElement)
{
str = valueIDElement->Value();
if (str && !strcmp(str, "Label"))
{
ReadXMLVIDLabel(node, ccID, indexId, pos, valueIDElement);
}
if (str && !strcmp(str, "Help"))
{
ReadXMLVIDHelp(node, ccID, indexId, pos, valueIDElement);
}
if (str && !strcmp(str, "ItemLabel"))
{
ReadXMLVIDItemLabel(node, ccID, indexId, pos, valueIDElement);
}
valueIDElement = valueIDElement->NextSiblingElement();
}
}
void Localization::ReadXMLVIDLabel(uint8 node, uint8 ccID, uint16 indexId, uint32 pos, const TiXmlElement *labelElement)
{
uint64 key = GetValueKey(node, ccID, indexId, pos);
string Language;
if (labelElement->Attribute("lang"))
Language = labelElement->Attribute("lang");
if (!labelElement->GetText())
{
Log::Write(LogLevel_Warning, "Localization::ReadXMLVIDLabel: Error in %s at line %d - No Label Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), ccID, indexId, pos, labelElement->GetText(), Language.c_str());
return;
}
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
m_valueLocalizationMap[key] = std::shared_ptr<ValueLocalizationEntry> (new ValueLocalizationEntry(ccID, indexId, pos));
}
else if (m_valueLocalizationMap[key]->HasLabel(Language))
{
Log::Write(LogLevel_Warning, "Localization::ReadXMLVIDLabel: Error in %s at line %d - Duplicate Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), ccID, indexId, pos, labelElement->GetText(), Language.c_str());
return;
}
if (Language.empty())
{
m_valueLocalizationMap[key]->AddLabel(labelElement->GetText());
}
else
{
m_valueLocalizationMap[key]->AddLabel(labelElement->GetText(), Language);
}
}
void Localization::ReadXMLVIDHelp(uint8 node, uint8 ccID, uint16 indexId, uint32 pos, const TiXmlElement *labelElement)
{
string Language;
if (labelElement->Attribute("lang"))
Language = labelElement->Attribute("lang");
if (!labelElement->GetText())
{
if (ccID != 112)
{
/* Dont Log About the Configuration CC */
Log::Write(LogLevel_Warning, "Localization::ReadXMLVIDHelp: Error in %s at line %d - No Help Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), ccID, indexId, pos, labelElement->GetText(), Language.c_str());
}
return;
}
uint64 key = GetValueKey(node, ccID, indexId, pos);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
m_valueLocalizationMap[key] = std::shared_ptr<ValueLocalizationEntry> (new ValueLocalizationEntry(ccID, indexId, pos));
}
else if (m_valueLocalizationMap[key]->HasLabel(Language))
{
Log::Write(LogLevel_Warning, "Localization::ReadXMLVIDHelp: Error in %s at line %d - Duplicate Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), ccID, indexId, pos, labelElement->GetText(), Language.c_str());
return;
}
if (Language.empty())
{
m_valueLocalizationMap[key]->AddHelp(labelElement->GetText());
}
else
{
m_valueLocalizationMap[key]->AddHelp(labelElement->GetText(), Language);
}
}
void Localization::ReadXMLVIDItemLabel(uint8 node, uint8 ccID, uint16 indexId, uint32 pos, const TiXmlElement *labelElement)
{
uint64 key = GetValueKey(node, ccID, indexId, pos);
string Language;
int32 itemIndex;
if (labelElement->Attribute("lang"))
Language = labelElement->Attribute("lang");
if (!labelElement->GetText())
{
Log::Write(LogLevel_Warning, "Localization::ReadXMLVIDItemLabel: Error in %s at line %d - No ItemIndex Label Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), ccID, indexId, pos, labelElement->GetText(), Language.c_str());
return;
}
if (TIXML_SUCCESS != labelElement->QueryIntAttribute("itemIndex", &itemIndex))
{
Log::Write(LogLevel_Warning, "Localization::ReadXMLVIDItemLabel: Error in %s at line %d - No itemIndex Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), ccID, indexId, pos, labelElement->GetText(), Language.c_str());
return;
}
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
Log::Write(LogLevel_Warning, "Localization::ReadXMLVIDItemLabel: Error in %s at line %d - No Value Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), ccID, indexId, pos, labelElement->GetText(), Language.c_str());
return;
}
else if (m_valueLocalizationMap[key]->HasItemLabel(itemIndex, Language))
{
Log::Write(LogLevel_Warning, "Localization::ReadXMLVIDItemLabel: Error in %s at line %d - Duplicate ItemLabel Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", labelElement->GetDocument()->GetUserData(), labelElement->Row(), ccID, indexId, pos, labelElement->GetText(), Language.c_str());
return;
}
if (Language.empty())
{
m_valueLocalizationMap[key]->AddItemLabel(labelElement->GetText(), itemIndex);
}
else
{
m_valueLocalizationMap[key]->AddItemLabel(labelElement->GetText(), itemIndex, Language);
}
}
uint64 Localization::GetValueKey(uint8 _node, uint8 _commandClass, uint16 _index, uint32 _pos, bool unique)
{
if (unique == true)
{
return ((uint64) _node << 56 | (uint64) _commandClass << 48) | ((uint64) _index << 32) | ((uint64) _pos);
}
/* configuration CC needs its own Storage per Node. */
if (_commandClass == Internal::CC::Configuration::StaticGetCommandClassId())
{
return ((uint64) _node << 56 | (uint64) _commandClass << 48) | ((uint64) _index << 32) | ((uint64) _pos);
}
/* ThermoStatSetpoint index's above 100 are unique per node */
if ((_commandClass == Internal::CC::ThermostatSetpoint::StaticGetCommandClassId()) && (_index >= 100))
{
return ((uint64) _node << 56 | (uint64) _commandClass << 48) | ((uint64) _index << 32) | ((uint64) _pos);
}
if (_commandClass == Internal::CC::Meter::StaticGetCommandClassId())
{
return ((uint64) _node << 56 | (uint64) _commandClass << 48) | ((uint64) _index << 32) | ((uint64) _pos);
}
return ((uint64) _commandClass << 48) | ((uint64) _index << 32) | ((uint64) _pos);
}
void Localization::SetupCommandClass(Internal::CC::CommandClass *cc)
{
uint8 ccID = cc->GetCommandClassId();
if (m_commandClassLocalizationMap.find(ccID) != m_commandClassLocalizationMap.end())
{
cc->SetCommandClassLabel(m_commandClassLocalizationMap[ccID]->GetLabel(m_selectedLang));
}
else
{
Log::Write(LogLevel_Warning, "Localization::SetupCommandClass: Localization Warning: No Entry for CommandClass - CC: %d (%s)", ccID, cc->GetCommandClassName().c_str());
cc->SetCommandClassLabel(cc->GetCommandClassName());
}
}
bool Localization::SetValueHelp(uint8 _node, uint8 ccID, uint16 indexId, uint32 pos, string help, string lang)
{
uint64 key = GetValueKey(_node, ccID, indexId, pos);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
m_valueLocalizationMap[key] = std::shared_ptr<ValueLocalizationEntry> (new ValueLocalizationEntry(ccID, indexId, pos));
}
else if (m_valueLocalizationMap[key]->HasHelp(lang))
{
Log::Write(LogLevel_Warning, "Localization::SetValueHelp: Duplicate Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", ccID, indexId, pos, help.c_str(), lang.c_str());
}
if (lang.empty())
{
m_valueLocalizationMap[key]->AddHelp(help);
}
else
{
m_valueLocalizationMap[key]->AddHelp(help, lang);
}
return true;
}
bool Localization::SetValueLabel(uint8 node, uint8 ccID, uint16 indexId, uint32 pos, string label, string lang)
{
uint64 key = GetValueKey(node, ccID, indexId, pos);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
m_valueLocalizationMap[key] = std::shared_ptr<ValueLocalizationEntry> (new ValueLocalizationEntry(ccID, indexId, pos));
}
else if (m_valueLocalizationMap[key]->HasLabel(lang))
{
Log::Write(LogLevel_Warning, "Localization::SetValueLabel: Duplicate Entry for CommandClass %d, ValueID: %d (%d): %s (Lang: %s)", ccID, indexId, pos, label.c_str(), lang.c_str());
}
if (lang.empty())
{
m_valueLocalizationMap[key]->AddLabel(label);
}
else
{
m_valueLocalizationMap[key]->AddLabel(label, lang);
}
return true;
}
std::string const Localization::GetValueHelp(uint8 node, uint8 ccID, uint16 indexId, uint32 pos)
{
uint64 key = GetValueKey(node, ccID, indexId, pos);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
Log::Write(LogLevel_Warning, "Localization::GetValueHelp: No Help for CommandClass %xd, ValueID: %d (%d)", ccID, indexId, pos);
return "";
}
return m_valueLocalizationMap[key]->GetHelp(m_selectedLang);
}
std::string const Localization::GetValueLabel(uint8 node, uint8 ccID, uint16 indexId, int32 pos) const
{
uint64 key = GetValueKey(node, ccID, indexId, pos);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
Log::Write(LogLevel_Warning, "Localization::GetValueLabel: No Label for CommandClass %xd, ValueID: %d (%d)", ccID, indexId, pos);
return "";
}
return m_valueLocalizationMap[key]->GetLabel(m_selectedLang);
}
std::string const Localization::GetValueItemLabel(uint8 node, uint8 ccID, uint16 indexId, int32 pos, int32 itemIndex) const
{
bool unique = false;
if ((ccID == Internal::CC::SoundSwitch::StaticGetCommandClassId()) && (indexId == 1 || indexId == 3))
{
unique = true;
}
uint64 key = GetValueKey(node, ccID, indexId, pos, unique);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
Log::Write(LogLevel_Warning, "Localization::GetValueItemLabel: No ValueLocalizationMap for CommandClass %xd, ValueID: %d (%d) ItemIndex %d", ccID, indexId, pos, itemIndex);
return "";
}
return m_valueLocalizationMap[key]->GetItemLabel(m_selectedLang, itemIndex);
}
bool Localization::SetValueItemLabel(uint8 node, uint8 ccID, uint16 indexId, int32 pos, int32 itemIndex, string label, string lang)
{
bool unique = false;
if ((ccID == Internal::CC::SoundSwitch::StaticGetCommandClassId()) && (indexId == 1 || indexId == 3))
{
unique = true;
}
uint64 key = GetValueKey(node, ccID, indexId, pos, unique);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
m_valueLocalizationMap[key] = std::shared_ptr<ValueLocalizationEntry> (new ValueLocalizationEntry(ccID, indexId, pos));
}
else if (m_valueLocalizationMap[key]->HasItemLabel(itemIndex, lang))
{
Log::Write(LogLevel_Warning, "Localization::SetValueItemLabel: Duplicate Item Entry for CommandClass %d, ValueID: %d (%d) itemIndex %d: %s (Lang: %s)", ccID, indexId, pos, itemIndex, label.c_str(), lang.c_str());
}
m_valueLocalizationMap[key]->AddItemLabel(label, itemIndex, lang);
return true;
}
std::string const Localization::GetValueItemHelp(uint8 node, uint8 ccID, uint16 indexId, int32 pos, int32 itemIndex) const
{
bool unique = false;
if ((ccID == Internal::CC::SoundSwitch::StaticGetCommandClassId()) && (indexId == 1 || indexId == 3))
{
unique = true;
}
uint64 key = GetValueKey(node, ccID, indexId, pos, unique);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
Log::Write(LogLevel_Warning, "Localization::GetValueItemHelp: No ValueLocalizationMap for CommandClass %xd, ValueID: %d (%d) ItemIndex %d", ccID, indexId, pos, itemIndex);
return "";
}
return m_valueLocalizationMap[key]->GetItemHelp(m_selectedLang, itemIndex);
}
bool Localization::SetValueItemHelp(uint8 node, uint8 ccID, uint16 indexId, int32 pos, int32 itemIndex, string label, string lang)
{
bool unique = false;
if ((ccID == Internal::CC::SoundSwitch::StaticGetCommandClassId()) && (indexId == 1 || indexId == 3))
{
unique = true;
}
uint64 key = GetValueKey(node, ccID, indexId, pos, unique);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
m_valueLocalizationMap[key] = std::shared_ptr<ValueLocalizationEntry> (new ValueLocalizationEntry(ccID, indexId, pos));
}
else if (m_valueLocalizationMap[key]->HasItemHelp(itemIndex, lang))
{
Log::Write(LogLevel_Warning, "Localization::SetValueItemHelp: Duplicate Item Entry for CommandClass %d, ValueID: %d (%d) ItemIndex %d: %s (Lang: %s)", ccID, indexId, pos, itemIndex, label.c_str(), lang.c_str());
}
m_valueLocalizationMap[key]->AddItemHelp(label, itemIndex, lang);
return true;
}
std::string const Localization::GetGlobalLabel(string index)
{
if (m_globalLabelLocalizationMap.find(index) == m_globalLabelLocalizationMap.end())
{
Log::Write(LogLevel_Warning, "Localization::GetGlobalLabel: No globalLabelLocalizationMap for Index %s", index.c_str());
return index;
}
return m_globalLabelLocalizationMap[index]->GetLabel(m_selectedLang);
}
bool Localization::SetGlobalLabel(string index, string text, string lang)
{
if (m_globalLabelLocalizationMap.find(index) == m_globalLabelLocalizationMap.end())
{
m_globalLabelLocalizationMap[index] = std::shared_ptr<LabelLocalizationEntry>(new LabelLocalizationEntry(0));
}
else if (m_globalLabelLocalizationMap[index]->HasLabel(lang))
{
Log::Write(LogLevel_Warning, "Localization::SetGlobalLabel: Duplicate Entry for GlobalText %s: %s (Lang: %s)", index.c_str(), text.c_str(), lang.c_str());
return false;
}
if (lang.empty())
{
m_globalLabelLocalizationMap[index]->AddLabel(text);
}
else
{
m_globalLabelLocalizationMap[index]->AddLabel(text, lang);
}
return true;
}
bool Localization::WriteXMLVIDHelp(uint8 node, uint8 ccID, uint16 indexId, uint32 pos, TiXmlElement *valueElement)
{
uint64 key = GetValueKey(node, ccID, indexId, pos);
if (m_valueLocalizationMap.find(key) == m_valueLocalizationMap.end())
{
Log::Write(LogLevel_Warning, "Localization::WriteXMLVIDHelp: No Help for CommandClass %d, ValueID: %d (%d)", ccID, indexId, pos);
return false;
}
TiXmlElement* helpElement = new TiXmlElement("Help");
valueElement->LinkEndChild(helpElement);
TiXmlText* textElement = new TiXmlText(m_valueLocalizationMap[key]->GetHelp(m_selectedLang).c_str());
helpElement->LinkEndChild(textElement);
return true;
}
Localization *Localization::Get()
{
if (m_instance != NULL)
{
return m_instance;
}
m_instance = new Localization();
ReadXML();
Options::Get()->GetOptionAsString("Language", &m_selectedLang);
return m_instance;
}
} // namespace Internal
} // namespace OpenZWave