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
|
/*
* 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 "Addon.h"
#include "ServiceBroker.h"
#include "addons/AddonManager.h"
#include "addons/RepositoryUpdater.h"
#include "addons/addoninfo/AddonInfo.h"
#include "addons/addoninfo/AddonType.h"
#include "addons/settings/AddonSettings.h"
#include "filesystem/Directory.h"
#include "filesystem/File.h"
#include "settings/Settings.h"
#include "settings/lib/Setting.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/XMLUtils.h"
#include "utils/log.h"
#include <algorithm>
#include <ostream>
#include <string.h>
#include <utility>
#include <vector>
#ifdef HAS_PYTHON
#include "interfaces/python/XBPython.h"
#endif
using XFILE::CDirectory;
using XFILE::CFile;
namespace ADDON
{
CAddon::CAddon(const AddonInfoPtr& addonInfo, AddonType addonType)
: m_addonInfo(addonInfo),
m_type(addonType == AddonType::UNKNOWN ? addonInfo->MainType() : addonType)
{
}
AddonType CAddon::MainType() const
{
return m_addonInfo->MainType();
}
bool CAddon::HasType(AddonType type) const
{
return m_addonInfo->HasType(type);
}
bool CAddon::HasMainType(AddonType type) const
{
return m_addonInfo->HasType(type, true);
}
const CAddonType* CAddon::Type(AddonType type) const
{
return m_addonInfo->Type(type);
}
std::string CAddon::ID() const
{
return m_addonInfo->ID();
}
std::string CAddon::Name() const
{
return m_addonInfo->Name();
}
bool CAddon::IsBinary() const
{
return m_addonInfo->IsBinary();
}
CAddonVersion CAddon::Version() const
{
return m_addonInfo->Version();
}
CAddonVersion CAddon::MinVersion() const
{
return m_addonInfo->MinVersion();
}
std::string CAddon::Summary() const
{
return m_addonInfo->Summary();
}
std::string CAddon::Description() const
{
return m_addonInfo->Description();
}
std::string CAddon::Path() const
{
return m_addonInfo->Path();
}
std::string CAddon::Profile() const
{
return m_addonInfo->ProfilePath();
}
std::string CAddon::Author() const
{
return m_addonInfo->Author();
}
std::string CAddon::ChangeLog() const
{
return m_addonInfo->ChangeLog();
}
std::string CAddon::Icon() const
{
return m_addonInfo->Icon();
}
ArtMap CAddon::Art() const
{
return m_addonInfo->Art();
}
std::vector<std::string> CAddon::Screenshots() const
{
return m_addonInfo->Screenshots();
}
std::string CAddon::Disclaimer() const
{
return m_addonInfo->Disclaimer();
}
AddonLifecycleState CAddon::LifecycleState() const
{
return m_addonInfo->LifecycleState();
}
std::string CAddon::LifecycleStateDescription() const
{
return m_addonInfo->LifecycleStateDescription();
}
CDateTime CAddon::InstallDate() const
{
return m_addonInfo->InstallDate();
}
CDateTime CAddon::LastUpdated() const
{
return m_addonInfo->LastUpdated();
}
CDateTime CAddon::LastUsed() const
{
return m_addonInfo->LastUsed();
}
std::string CAddon::Origin() const
{
return m_addonInfo->Origin();
}
std::string CAddon::OriginName() const
{
return m_addonInfo->OriginName();
}
uint64_t CAddon::PackageSize() const
{
return m_addonInfo->PackageSize();
}
const InfoMap& CAddon::ExtraInfo() const
{
return m_addonInfo->ExtraInfo();
}
const std::vector<DependencyInfo>& CAddon::GetDependencies() const
{
return m_addonInfo->GetDependencies();
}
std::string CAddon::FanArt() const
{
auto it = m_addonInfo->Art().find("fanart");
return it != m_addonInfo->Art().end() ? it->second : "";
}
bool CAddon::MeetsVersion(const CAddonVersion& versionMin, const CAddonVersion& version) const
{
return m_addonInfo->MeetsVersion(versionMin, version);
}
/**
* Settings Handling
*/
std::vector<AddonInstanceId> CAddon::GetKnownInstanceIds() const
{
return m_addonInfo->GetKnownInstanceIds();
}
bool CAddon::SupportsMultipleInstances() const
{
return m_addonInfo->SupportsMultipleInstances();
}
AddonInstanceSupport CAddon::InstanceUseType() const
{
return m_addonInfo->InstanceUseType();
}
bool CAddon::SupportsInstanceSettings() const
{
return m_addonInfo->SupportsInstanceSettings();
}
bool CAddon::DeleteInstanceSettings(AddonInstanceId instance)
{
if (instance == ADDON_SETTINGS_ID)
return false;
const auto itr = m_settings.find(instance);
if (itr == m_settings.end())
return false;
if (CFile::Exists(itr->second.m_userSettingsPath))
CFile::Delete(itr->second.m_userSettingsPath);
ResetSettings(instance);
return true;
}
bool CAddon::CanHaveAddonOrInstanceSettings()
{
return HasSettings(ADDON_SETTINGS_ID) || SupportsInstanceSettings();
}
bool CAddon::HasSettings(AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return LoadSettings(false, true, id) && m_settings[id].m_addonSettings->HasSettings();
}
bool CAddon::SettingsInitialized(AddonInstanceId id /* = ADDON_SETTINGS_ID */) const
{
const auto addonSettings = FindInstanceSettings(id);
return addonSettings && addonSettings->IsInitialized();
}
bool CAddon::SettingsLoaded(AddonInstanceId id /* = ADDON_SETTINGS_ID */) const
{
const auto addonSettings = FindInstanceSettings(id);
return addonSettings && addonSettings->IsLoaded();
}
bool CAddon::LoadSettings(bool bForce,
bool loadUserSettings,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
if (SettingsInitialized(id) && !bForce)
return true;
const auto itr = m_settings.find(id);
if (itr != m_settings.end())
{
if (itr->second.m_loadSettingsFailed)
return false;
}
else
{
InitSettings(id);
}
// assume loading settings fails
m_settings[id].m_loadSettingsFailed = true;
// reset the settings if we are forced to
if (SettingsInitialized(id) && bForce)
GetSettings(id)->Uninitialize();
// load the settings definition XML file
const auto addonSettingsDefinitionFile = m_settings[id].m_addonSettingsPath;
CXBMCTinyXML addonSettingsDefinitionDoc;
if (!addonSettingsDefinitionDoc.LoadFile(addonSettingsDefinitionFile))
{
if (CFile::Exists(addonSettingsDefinitionFile))
{
CLog::Log(LOGERROR, "CAddon[{}]: unable to load: {}, Line {}\n{}", ID(),
addonSettingsDefinitionFile, addonSettingsDefinitionDoc.ErrorRow(),
addonSettingsDefinitionDoc.ErrorDesc());
}
return false;
}
// initialize the settings definition
if (!GetSettings(id)->Initialize(addonSettingsDefinitionDoc))
{
CLog::Log(LOGERROR, "CAddon[{}]: failed to initialize addon settings", ID());
return false;
}
// loading settings didn't fail
m_settings[id].m_loadSettingsFailed = false;
// load user settings / values
if (loadUserSettings)
LoadUserSettings(id);
return true;
}
bool CAddon::HasUserSettings(AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
if (!LoadSettings(false, true, id))
return false;
return SettingsLoaded(id) && m_settings[id].m_hasUserSettings;
}
bool CAddon::ReloadSettings(AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return LoadSettings(true, true, id);
}
void CAddon::ResetSettings(AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
m_settings.erase(id);
}
bool CAddon::LoadUserSettings(AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
if (!SettingsInitialized(id) && !InitSettings(id))
return false;
CSettingsData& data = m_settings[id];
data.m_hasUserSettings = false;
// there are no user settings
if (!CFile::Exists(data.m_userSettingsPath))
{
// mark the settings as loaded
GetSettings(id)->SetLoaded();
return true;
}
CXBMCTinyXML doc;
if (!doc.LoadFile(data.m_userSettingsPath))
{
CLog::Log(LOGERROR, "CAddon[{}]: failed to load addon settings from {}", ID(),
data.m_userSettingsPath);
return false;
}
return SettingsFromXML(doc, false, id);
}
bool CAddon::HasSettingsToSave(AddonInstanceId id /* = ADDON_SETTINGS_ID */) const
{
return SettingsLoaded(id);
}
void CAddon::SaveSettings(AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
if (!HasSettingsToSave(id))
return; // no settings to save
CSettingsData& data = m_settings[id];
// break down the path into directories
const std::string strAddon = URIUtils::GetDirectory(data.m_userSettingsPath);
const std::string strRoot = URIUtils::GetDirectory(strAddon);
// create the individual folders
if (!CDirectory::Exists(strRoot))
CDirectory::Create(strRoot);
if (!CDirectory::Exists(strAddon))
CDirectory::Create(strAddon);
// create the XML file
CXBMCTinyXML doc;
if (SettingsToXML(doc, id))
doc.SaveFile(data.m_userSettingsPath);
data.m_hasUserSettings = true;
//push the settings changes to the running addon instance
CServiceBroker::GetAddonMgr().ReloadSettings(ID(), id);
#ifdef HAS_PYTHON
CServiceBroker::GetXBPython().OnSettingsChanged(ID());
#endif
}
std::string CAddon::GetSetting(const std::string& key, AddonInstanceId id)
{
if (key.empty() || !LoadSettings(false, true, id))
return ""; // no settings available
auto setting = m_settings[id].m_addonSettings->GetSetting(key);
if (setting != nullptr)
return setting->ToString();
return "";
}
template<class TSetting>
bool GetSettingValue(CAddon& addon,
AddonInstanceId instanceId,
const std::string& key,
typename TSetting::Value& value)
{
if (key.empty() || !addon.HasSettings(instanceId))
return false;
auto setting = addon.GetSettings(instanceId)->GetSetting(key);
if (setting == nullptr || setting->GetType() != TSetting::Type())
return false;
value = std::static_pointer_cast<TSetting>(setting)->GetValue();
return true;
}
bool CAddon::GetSettingBool(const std::string& key,
bool& value,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return GetSettingValue<CSettingBool>(*this, id, key, value);
}
bool CAddon::GetSettingInt(const std::string& key,
int& value,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return GetSettingValue<CSettingInt>(*this, id, key, value);
}
bool CAddon::GetSettingNumber(const std::string& key,
double& value,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return GetSettingValue<CSettingNumber>(*this, id, key, value);
}
bool CAddon::GetSettingString(const std::string& key,
std::string& value,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return GetSettingValue<CSettingString>(*this, id, key, value);
}
void CAddon::UpdateSetting(const std::string& key,
const std::string& value,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
if (key.empty() || !LoadSettings(false, true, id))
return;
// try to get the setting
auto setting = m_settings[id].m_addonSettings->GetSetting(key);
// if the setting doesn't exist, try to add it
if (setting == nullptr)
{
setting = m_settings[id].m_addonSettings->AddSetting(key, value);
if (setting == nullptr)
{
CLog::Log(LOGERROR, "CAddon[{}]: failed to add undefined setting \"{}\"", ID(), key);
return;
}
}
setting->FromString(value);
}
template<class TSetting>
bool UpdateSettingValue(CAddon& addon,
AddonInstanceId instanceId,
const std::string& key,
typename TSetting::Value value)
{
if (key.empty() || !addon.HasSettings(instanceId))
return false;
// try to get the setting
auto setting = addon.GetSettings(instanceId)->GetSetting(key);
// if the setting doesn't exist, try to add it
if (setting == nullptr)
{
setting = addon.GetSettings(instanceId)->AddSetting(key, value);
if (setting == nullptr)
{
CLog::Log(LOGERROR, "CAddon[{}]: failed to add undefined setting \"{}\"", addon.ID(), key);
return false;
}
}
if (setting->GetType() != TSetting::Type())
return false;
return std::static_pointer_cast<TSetting>(setting)->SetValue(value);
}
bool CAddon::UpdateSettingBool(const std::string& key,
bool value,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return UpdateSettingValue<CSettingBool>(*this, id, key, value);
}
bool CAddon::UpdateSettingInt(const std::string& key,
int value,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return UpdateSettingValue<CSettingInt>(*this, id, key, value);
}
bool CAddon::UpdateSettingNumber(const std::string& key,
double value,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return UpdateSettingValue<CSettingNumber>(*this, id, key, value);
}
bool CAddon::UpdateSettingString(const std::string& key,
const std::string& value,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
return UpdateSettingValue<CSettingString>(*this, id, key, value);
}
bool CAddon::SettingsFromXML(const CXBMCTinyXML& doc,
bool loadDefaults,
AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
if (doc.RootElement() == nullptr)
return false;
// if the settings haven't been initialized yet, try it from the given XML
if (!SettingsInitialized(id))
{
if (!GetSettings(id)->Initialize(doc))
{
CLog::Log(LOGERROR, "CAddon[{}]: failed to initialize addon settings", ID());
return false;
}
}
// reset all setting values to their default value
if (loadDefaults)
GetSettings(id)->SetDefaults();
// try to load the setting's values from the given XML
if (!GetSettings(id)->Load(doc))
{
CLog::Log(LOGERROR, "CAddon[{}]: failed to load user settings", ID());
return false;
}
m_settings[id].m_hasUserSettings = true;
return true;
}
bool CAddon::SettingsToXML(CXBMCTinyXML& doc, AddonInstanceId id /* = ADDON_SETTINGS_ID */) const
{
if (!SettingsInitialized(id))
return false;
if (!m_settings[id].m_addonSettings->Save(doc))
{
CLog::Log(LOGERROR, "CAddon[{}]: failed to save addon settings", ID());
return false;
}
return true;
}
bool CAddon::InitSettings(AddonInstanceId id)
{
// initialize addon settings if necessary
if (!FindInstanceSettings(id))
{
CSettingsData data;
data.m_addonSettings =
std::make_shared<CAddonSettings>(enable_shared_from_this::shared_from_this(), id);
if (id == ADDON_SETTINGS_ID)
{
data.m_addonSettingsPath =
URIUtils::AddFileToFolder(m_addonInfo->Path(), "resources", "settings.xml");
data.m_userSettingsPath = URIUtils::AddFileToFolder(Profile(), "settings.xml");
}
else
{
data.m_addonSettingsPath =
URIUtils::AddFileToFolder(m_addonInfo->Path(), "resources", "instance-settings.xml");
data.m_userSettingsPath =
URIUtils::AddFileToFolder(Profile(), StringUtils::Format("instance-settings-{}.xml", id));
}
m_settings[id] = std::move(data);
return true;
}
return false;
}
std::shared_ptr<CAddonSettings> CAddon::FindInstanceSettings(AddonInstanceId id) const
{
const auto itr = m_settings.find(id);
if (itr == m_settings.end())
return nullptr;
return itr->second.m_addonSettings;
}
std::shared_ptr<CAddonSettings> CAddon::GetSettings(AddonInstanceId id /* = ADDON_SETTINGS_ID */)
{
if (InitSettings(id))
LoadSettings(false, true, id);
return m_settings[id].m_addonSettings;
}
std::string CAddon::LibPath() const
{
// Get library related to given type on construction
std::string libName = m_addonInfo->Type(m_type)->LibName();
if (libName.empty())
{
// If not present fallback to master library
libName = m_addonInfo->LibName();
if (libName.empty())
return "";
}
return URIUtils::AddFileToFolder(m_addonInfo->Path(), libName);
}
CAddonVersion CAddon::GetDependencyVersion(const std::string& dependencyID) const
{
return m_addonInfo->DependencyVersion(dependencyID);
}
void OnPreInstall(const AddonPtr& addon)
{
//Fallback to the pre-install callback in the addon.
//! @bug If primary extension point have changed we're calling the wrong method.
addon->OnPreInstall();
}
void OnPostInstall(const AddonPtr& addon, bool update, bool modal)
{
addon->OnPostInstall(update, modal);
}
void OnPreUnInstall(const AddonPtr& addon)
{
addon->OnPreUnInstall();
}
void OnPostUnInstall(const AddonPtr& addon)
{
addon->OnPostUnInstall();
}
} // namespace ADDON
|