This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
AddonManager.cpp
1036 lines (921 loc) · 28.2 KB
/
AddonManager.cpp
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "AddonManager.h"
#include "Addon.h"
#include "AudioEncoder.h"
#include "AudioDecoder.h"
#include "DllLibCPluff.h"
#include "utils/StringUtils.h"
#include "utils/JobManager.h"
#include "threads/SingleLock.h"
#include "FileItem.h"
#include "LangInfo.h"
#include "settings/AdvancedSettings.h"
#include "settings/Settings.h"
#include "utils/log.h"
#include "utils/XBMCTinyXML.h"
#ifdef HAS_VISUALISATION
#include "Visualisation.h"
#endif
#ifdef HAS_SCREENSAVER
#include "ScreenSaver.h"
#endif
#ifdef HAS_PVRCLIENTS
#include "DllPVRClient.h"
#include "pvr/addons/PVRClient.h"
#endif
//#ifdef HAS_SCRAPERS
#include "Scraper.h"
//#endif
#include "PluginSource.h"
#include "Repository.h"
#include "Skin.h"
#include "Service.h"
#include "ContextItemAddon.h"
#include "Util.h"
#include "addons/Webinterface.h"
using namespace std;
using namespace XFILE;
namespace ADDON
{
cp_log_severity_t clog_to_cp(int lvl);
void cp_fatalErrorHandler(const char *msg);
void cp_logger(cp_log_severity_t level, const char *msg, const char *apid, void *user_data);
/**********************************************************
* CAddonMgr
*
*/
map<TYPE, IAddonMgrCallback*> CAddonMgr::m_managers;
AddonPtr CAddonMgr::Factory(const cp_extension_t *props)
{
if (!PlatformSupportsAddon(props->plugin))
return AddonPtr();
/* Check if user directories need to be created */
const cp_cfg_element_t *settings = GetExtElement(props->configuration, "settings");
if (settings)
CheckUserDirs(settings);
const TYPE type = TranslateType(props->ext_point_id);
switch (type)
{
case ADDON_PLUGIN:
case ADDON_SCRIPT:
return AddonPtr(new CPluginSource(props));
case ADDON_SCRIPT_LIBRARY:
case ADDON_SCRIPT_LYRICS:
case ADDON_SCRIPT_MODULE:
case ADDON_SUBTITLE_MODULE:
return AddonPtr(new CAddon(props));
case ADDON_WEB_INTERFACE:
return AddonPtr(new CWebinterface(props));
case ADDON_SCRIPT_WEATHER:
{
// Eden (API v2.0) broke old weather add-ons
AddonPtr result(new CAddon(props));
AddonVersion ver1 = result->GetDependencyVersion("xbmc.python");
AddonVersion ver2 = AddonVersion("2.0");
if (ver1 < ver2)
{
CLog::Log(LOGINFO,"%s: Weather add-ons for api < 2.0 unsupported (%s)",__FUNCTION__,result->ID().c_str());
return AddonPtr();
}
return result;
}
case ADDON_SERVICE:
return AddonPtr(new CService(props));
case ADDON_SCRAPER_ALBUMS:
case ADDON_SCRAPER_ARTISTS:
case ADDON_SCRAPER_MOVIES:
case ADDON_SCRAPER_MUSICVIDEOS:
case ADDON_SCRAPER_TVSHOWS:
case ADDON_SCRAPER_LIBRARY:
return AddonPtr(new CScraper(props));
case ADDON_VIZ:
case ADDON_SCREENSAVER:
case ADDON_PVRDLL:
case ADDON_AUDIOENCODER:
case ADDON_AUDIODECODER:
{ // begin temporary platform handling for Dlls
// ideally platforms issues will be handled by C-Pluff
// this is not an attempt at a solution
std::string value;
if (type == ADDON_SCREENSAVER && 0 == strnicmp(props->plugin->identifier, "screensaver.xbmc.builtin.", 25))
{ // built in screensaver
return AddonPtr(new CAddon(props));
}
if (type == ADDON_SCREENSAVER)
{ // Python screensaver
std::string library = CAddonMgr::Get().GetExtValue(props->configuration, "@library");
if (URIUtils::HasExtension(library, ".py"))
return AddonPtr(new CScreenSaver(props));
}
if (type == ADDON_AUDIOENCODER && 0 == strncmp(props->plugin->identifier,
"audioencoder.xbmc.builtin.", 26))
{ // built in audio encoder
return AddonPtr(new CAudioEncoder(props));
}
std::string tograb;
#if defined(TARGET_ANDROID)
tograb = "@library_android";
#elif defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
tograb = "@library_linux";
#elif defined(TARGET_WINDOWS) && defined(HAS_DX)
tograb = "@library_windx";
#elif defined(TARGET_DARWIN)
tograb = "@library_osx";
#endif
value = GetExtValue(props->plugin->extensions->configuration, tograb.c_str());
if (value.empty())
break;
if (type == ADDON_VIZ)
{
#if defined(HAS_VISUALISATION)
return AddonPtr(new CVisualisation(props));
#endif
}
else if (type == ADDON_PVRDLL)
{
#ifdef HAS_PVRCLIENTS
return AddonPtr(new PVR::CPVRClient(props));
#endif
}
else if (type == ADDON_AUDIOENCODER)
return AddonPtr(new CAudioEncoder(props));
else if (type == ADDON_AUDIODECODER)
return AddonPtr(new CAudioDecoder(props));
else
return AddonPtr(new CScreenSaver(props));
}
case ADDON_SKIN:
return AddonPtr(new CSkinInfo(props));
case ADDON_VIZ_LIBRARY:
return AddonPtr(new CAddonLibrary(props));
case ADDON_REPOSITORY:
return AddonPtr(new CRepository(props));
case ADDON_CONTEXT_ITEM:
return AddonPtr(new CContextItemAddon(props));
default:
break;
}
return AddonPtr();
}
bool CAddonMgr::CheckUserDirs(const cp_cfg_element_t *settings)
{
if (!settings)
return false;
const cp_cfg_element_t *userdirs = GetExtElement((cp_cfg_element_t *)settings, "userdirs");
if (!userdirs)
return false;
ELEMENTS elements;
if (!GetExtElements((cp_cfg_element_t *)userdirs, "userdir", elements))
return false;
ELEMENTS::iterator itr = elements.begin();
while (itr != elements.end())
{
std::string path = GetExtValue(*itr++, "@path");
if (!CDirectory::Exists(path))
{
if (!CUtil::CreateDirectoryEx(path))
{
CLog::Log(LOGERROR, "CAddonMgr::CheckUserDirs: Unable to create directory %s.", path.c_str());
return false;
}
}
}
return true;
}
CAddonMgr::CAddonMgr()
{
m_cpluff = NULL;
}
CAddonMgr::~CAddonMgr()
{
DeInit();
}
CAddonMgr &CAddonMgr::Get()
{
static CAddonMgr sAddonMgr;
return sAddonMgr;
}
IAddonMgrCallback* CAddonMgr::GetCallbackForType(TYPE type)
{
if (m_managers.find(type) == m_managers.end())
return NULL;
else
return m_managers[type];
}
bool CAddonMgr::RegisterAddonMgrCallback(const TYPE type, IAddonMgrCallback* cb)
{
if (cb == NULL)
return false;
m_managers.erase(type);
m_managers[type] = cb;
return true;
}
void CAddonMgr::UnregisterAddonMgrCallback(TYPE type)
{
m_managers.erase(type);
}
bool CAddonMgr::Init()
{
m_cpluff = new DllLibCPluff;
m_cpluff->Load();
m_database.Open();
if (!m_cpluff->IsLoaded())
{
CLog::Log(LOGERROR, "ADDONS: Fatal Error, could not load libcpluff");
return false;
}
m_cpluff->set_fatal_error_handler(cp_fatalErrorHandler);
cp_status_t status;
status = m_cpluff->init();
if (status != CP_OK)
{
CLog::Log(LOGERROR, "ADDONS: Fatal Error, cp_init() returned status: %i", status);
return false;
}
//TODO could separate addons into different contexts
// would allow partial unloading of addon framework
m_cp_context = m_cpluff->create_context(&status);
assert(m_cp_context);
status = m_cpluff->register_pcollection(m_cp_context, CSpecialProtocol::TranslatePath("special://home/addons").c_str());
if (status != CP_OK)
{
CLog::Log(LOGERROR, "ADDONS: Fatal Error, cp_register_pcollection() returned status: %i", status);
return false;
}
status = m_cpluff->register_pcollection(m_cp_context, CSpecialProtocol::TranslatePath("special://xbmc/addons").c_str());
if (status != CP_OK)
{
CLog::Log(LOGERROR, "ADDONS: Fatal Error, cp_register_pcollection() returned status: %i", status);
return false;
}
status = m_cpluff->register_pcollection(m_cp_context, CSpecialProtocol::TranslatePath("special://xbmcbin/addons").c_str());
if (status != CP_OK)
{
CLog::Log(LOGERROR, "ADDONS: Fatal Error, cp_register_pcollection() returned status: %i", status);
return false;
}
status = m_cpluff->register_logger(m_cp_context, cp_logger,
&CAddonMgr::Get(), clog_to_cp(g_advancedSettings.m_logLevel));
if (status != CP_OK)
{
CLog::Log(LOGERROR, "ADDONS: Fatal Error, cp_register_logger() returned status: %i", status);
return false;
}
FindAddons();
VECADDONS repos;
if (GetAddons(ADDON_REPOSITORY, repos))
{
VECADDONS::iterator it = repos.begin();
for (;it != repos.end(); ++it)
CLog::Log(LOGNOTICE, "ADDONS: Using repository %s", (*it)->ID().c_str());
}
return true;
}
void CAddonMgr::DeInit()
{
if (m_cpluff)
m_cpluff->destroy();
delete m_cpluff;
m_cpluff = NULL;
m_database.Close();
m_disabled.clear();
}
bool CAddonMgr::HasAddons(const TYPE &type, bool enabled /*= true*/)
{
// TODO: This isn't particularly efficient as we create an addon type for each addon using the Factory, just so
// we can check addon dependencies in the addon constructor.
VECADDONS addons;
return GetAddons(type, addons, enabled);
}
bool CAddonMgr::GetAllAddons(VECADDONS &addons, bool enabled /*= true*/, bool allowRepos /* = false */)
{
for (int i = ADDON_UNKNOWN+1; i < ADDON_MAX; ++i)
{
if (!allowRepos && ADDON_REPOSITORY == (TYPE)i)
continue;
VECADDONS temp;
if (CAddonMgr::Get().GetAddons((TYPE)i, temp, enabled))
addons.insert(addons.end(), temp.begin(), temp.end());
}
return !addons.empty();
}
void CAddonMgr::AddToUpdateableAddons(AddonPtr &pAddon)
{
CSingleLock lock(m_critSection);
m_updateableAddons.push_back(pAddon);
}
void CAddonMgr::RemoveFromUpdateableAddons(AddonPtr &pAddon)
{
CSingleLock lock(m_critSection);
VECADDONS::iterator it = std::find(m_updateableAddons.begin(), m_updateableAddons.end(), pAddon);
if(it != m_updateableAddons.end())
{
m_updateableAddons.erase(it);
}
}
struct AddonIdFinder
{
AddonIdFinder(const std::string& id)
: m_id(id)
{}
bool operator()(const AddonPtr& addon)
{
return m_id == addon->ID();
}
private:
std::string m_id;
};
bool CAddonMgr::ReloadSettings(const std::string &id)
{
CSingleLock lock(m_critSection);
VECADDONS::iterator it = std::find_if(m_updateableAddons.begin(), m_updateableAddons.end(), AddonIdFinder(id));
if( it != m_updateableAddons.end())
{
return (*it)->ReloadSettings();
}
return false;
}
bool CAddonMgr::GetAllOutdatedAddons(VECADDONS &addons, bool getLocalVersion /*= false*/)
{
CSingleLock lock(m_critSection);
for (int i = ADDON_UNKNOWN+1; i < ADDON_MAX; ++i)
{
VECADDONS temp;
if (CAddonMgr::Get().GetAddons((TYPE)i, temp, true))
{
AddonPtr repoAddon;
for (unsigned int j = 0; j < temp.size(); j++)
{
// Ignore duplicates due to add-ons with multiple extension points
bool found = false;
for (VECADDONS::const_iterator addonIt = addons.begin(); addonIt != addons.end(); ++addonIt)
{
if ((*addonIt)->ID() == temp[j]->ID())
found = true;
}
if (found || !m_database.GetAddon(temp[j]->ID(), repoAddon))
continue;
if (temp[j]->Version() < repoAddon->Version() &&
!m_database.IsAddonBlacklisted(temp[j]->ID(),
repoAddon->Version().asString().c_str()))
{
if (getLocalVersion)
repoAddon->Props().version = temp[j]->Version();
addons.push_back(repoAddon);
}
}
}
}
return !addons.empty();
}
bool CAddonMgr::HasOutdatedAddons()
{
VECADDONS dummy;
return GetAllOutdatedAddons(dummy);
}
bool CAddonMgr::GetAddons(const TYPE &type, VECADDONS &addons, bool enabled /* = true */)
{
CSingleLock lock(m_critSection);
addons.clear();
if (!m_cp_context)
return false;
cp_status_t status;
int num;
std::string ext_point(TranslateType(type));
cp_extension_t **exts = m_cpluff->get_extensions_info(m_cp_context, ext_point.c_str(), &status, &num);
for(int i=0; i <num; i++)
{
const cp_extension_t *props = exts[i];
if (IsAddonDisabled(props->plugin->identifier) != enabled)
{
AddonPtr addon(Factory(props));
if (addon)
{
if (enabled)
{
// if the addon has a running instance, grab that
AddonPtr runningAddon = addon->GetRunningInstance();
if (runningAddon)
addon = runningAddon;
}
addons.push_back(addon);
}
}
}
m_cpluff->release_info(m_cp_context, exts);
return addons.size() > 0;
}
bool CAddonMgr::GetAddon(const std::string &str, AddonPtr &addon, const TYPE &type/*=ADDON_UNKNOWN*/, bool enabledOnly /*= true*/)
{
CSingleLock lock(m_critSection);
cp_status_t status;
cp_plugin_info_t *cpaddon = m_cpluff->get_plugin_info(m_cp_context, str.c_str(), &status);
if (status == CP_OK && cpaddon)
{
addon = GetAddonFromDescriptor(cpaddon, type==ADDON_UNKNOWN?"":TranslateType(type));
m_cpluff->release_info(m_cp_context, cpaddon);
if (addon)
{
if (enabledOnly && IsAddonDisabled(addon->ID()))
return false;
// if the addon has a running instance, grab that
AddonPtr runningAddon = addon->GetRunningInstance();
if (runningAddon)
addon = runningAddon;
}
return NULL != addon.get();
}
if (cpaddon)
m_cpluff->release_info(m_cp_context, cpaddon);
return false;
}
//TODO handle all 'default' cases here, not just scrapers & vizs
bool CAddonMgr::GetDefault(const TYPE &type, AddonPtr &addon)
{
std::string setting;
switch (type)
{
case ADDON_VIZ:
setting = CSettings::Get().GetString("musicplayer.visualisation");
break;
case ADDON_SCREENSAVER:
setting = CSettings::Get().GetString("screensaver.mode");
break;
case ADDON_SCRAPER_ALBUMS:
setting = CSettings::Get().GetString("musiclibrary.albumsscraper");
break;
case ADDON_SCRAPER_ARTISTS:
setting = CSettings::Get().GetString("musiclibrary.artistsscraper");
break;
case ADDON_SCRAPER_MOVIES:
setting = CSettings::Get().GetString("scrapers.moviesdefault");
break;
case ADDON_SCRAPER_MUSICVIDEOS:
setting = CSettings::Get().GetString("scrapers.musicvideosdefault");
break;
case ADDON_SCRAPER_TVSHOWS:
setting = CSettings::Get().GetString("scrapers.tvshowsdefault");
break;
case ADDON_WEB_INTERFACE:
setting = CSettings::Get().GetString("services.webskin");
break;
default:
return false;
}
return GetAddon(setting, addon, type);
}
bool CAddonMgr::SetDefault(const TYPE &type, const std::string &addonID)
{
switch (type)
{
case ADDON_VIZ:
CSettings::Get().SetString("musicplayer.visualisation",addonID);
break;
case ADDON_SCREENSAVER:
CSettings::Get().SetString("screensaver.mode",addonID);
break;
case ADDON_SCRAPER_ALBUMS:
CSettings::Get().SetString("musiclibrary.albumsscraper",addonID);
break;
case ADDON_SCRAPER_ARTISTS:
CSettings::Get().SetString("musiclibrary.artistsscraper",addonID);
break;
case ADDON_SCRAPER_MOVIES:
CSettings::Get().SetString("scrapers.moviesdefault",addonID);
break;
case ADDON_SCRAPER_MUSICVIDEOS:
CSettings::Get().SetString("scrapers.musicvideosdefault",addonID);
break;
case ADDON_SCRAPER_TVSHOWS:
CSettings::Get().SetString("scrapers.tvshowsdefault",addonID);
break;
default:
return false;
}
return true;
}
std::string CAddonMgr::GetString(const std::string &id, const int number)
{
AddonPtr addon;
if (GetAddon(id, addon))
return addon->GetString(number);
return "";
}
void CAddonMgr::FindAddons()
{
{
CSingleLock lock(m_critSection);
if (m_cpluff && m_cp_context)
{
m_cpluff->scan_plugins(m_cp_context, CP_SP_UPGRADE);
SetChanged();
}
}
NotifyObservers(ObservableMessageAddons);
}
void CAddonMgr::RemoveAddon(const std::string& ID)
{
if (m_cpluff && m_cp_context)
{
m_cpluff->uninstall_plugin(m_cp_context,ID.c_str());
SetChanged();
NotifyObservers(ObservableMessageAddons);
}
}
bool CAddonMgr::DisableAddon(const std::string& ID, bool disable)
{
CSingleLock lock(m_critSection);
if (m_database.DisableAddon(ID, disable))
{
m_disabled[ID] = disable;
return true;
}
return false;
}
bool CAddonMgr::IsAddonDisabled(const std::string& ID)
{
CSingleLock lock(m_critSection);
std::map<std::string, bool>::const_iterator it = m_disabled.find(ID);
if (it != m_disabled.end())
return it->second;
bool ret = m_database.IsAddonDisabled(ID);
m_disabled.insert(pair<std::string, bool>(ID, ret));
return ret;
}
bool CAddonMgr::CanAddonBeDisabled(const std::string& ID)
{
if (ID.empty())
return false;
CSingleLock lock(m_critSection);
AddonPtr localAddon;
// can't disable an addon that isn't installed
if (!IsAddonInstalled(ID, localAddon))
return false;
// can't disable an addon that is in use
if (localAddon->IsInUse())
return false;
// installed PVR addons can always be disabled
if (localAddon->Type() == ADDON_PVRDLL)
return true;
std::string systemAddonsPath = CSpecialProtocol::TranslatePath("special://xbmc/addons");
// can't disable system addons
if (StringUtils::StartsWith(localAddon->Path(), systemAddonsPath))
return false;
return true;
}
bool CAddonMgr::IsAddonInstalled(const std::string& ID)
{
AddonPtr tmp;
return IsAddonInstalled(ID, tmp);
}
bool CAddonMgr::IsAddonInstalled(const std::string& ID, AddonPtr& addon)
{
return GetAddon(ID, addon, ADDON_UNKNOWN, false);
}
bool CAddonMgr::CanAddonBeInstalled(const std::string& ID)
{
if (ID.empty())
return false;
CSingleLock lock(m_critSection);
// can't install already installed addon
if (IsAddonInstalled(ID))
return false;
// can't install broken addons
if (!m_database.IsAddonBroken(ID).empty())
return false;
return true;
}
bool CAddonMgr::CanAddonBeInstalled(const AddonPtr& addon)
{
if (addon == NULL)
return false;
CSingleLock lock(m_critSection);
// can't install already installed addon
if (IsAddonInstalled(addon->ID()))
return false;
// can't install broken addons
if (!addon->Props().broken.empty())
return false;
return true;
}
std::string CAddonMgr::GetTranslatedString(const cp_cfg_element_t *root, const char *tag)
{
if (!root)
return "";
const cp_cfg_element_t *eng = NULL;
for (unsigned int i = 0; i < root->num_children; i++)
{
const cp_cfg_element_t &child = root->children[i];
if (strcmp(tag, child.name) == 0)
{ // see if we have a "lang" attribute
const char *lang = m_cpluff->lookup_cfg_value((cp_cfg_element_t*)&child, "@lang");
if (lang && 0 == strcmp(lang,g_langInfo.GetLanguageLocale().c_str()))
return child.value ? child.value : "";
if (!lang || 0 == strcmp(lang, "en"))
eng = &child;
}
}
return (eng && eng->value) ? eng->value : "";
}
AddonPtr CAddonMgr::AddonFromProps(AddonProps& addonProps)
{
switch (addonProps.type)
{
case ADDON_PLUGIN:
case ADDON_SCRIPT:
return AddonPtr(new CPluginSource(addonProps));
case ADDON_SCRIPT_LIBRARY:
case ADDON_SCRIPT_LYRICS:
case ADDON_SCRIPT_WEATHER:
case ADDON_SCRIPT_MODULE:
case ADDON_SUBTITLE_MODULE:
return AddonPtr(new CAddon(addonProps));
case ADDON_WEB_INTERFACE:
return AddonPtr(new CWebinterface(addonProps));
case ADDON_SERVICE:
return AddonPtr(new CService(addonProps));
case ADDON_SCRAPER_ALBUMS:
case ADDON_SCRAPER_ARTISTS:
case ADDON_SCRAPER_MOVIES:
case ADDON_SCRAPER_MUSICVIDEOS:
case ADDON_SCRAPER_TVSHOWS:
case ADDON_SCRAPER_LIBRARY:
return AddonPtr(new CScraper(addonProps));
case ADDON_SKIN:
return AddonPtr(new CSkinInfo(addonProps));
#if defined(HAS_VISUALISATION)
case ADDON_VIZ:
return AddonPtr(new CVisualisation(addonProps));
#endif
case ADDON_SCREENSAVER:
return AddonPtr(new CScreenSaver(addonProps));
case ADDON_VIZ_LIBRARY:
return AddonPtr(new CAddonLibrary(addonProps));
case ADDON_PVRDLL:
return AddonPtr(new PVR::CPVRClient(addonProps));
case ADDON_AUDIOENCODER:
return AddonPtr(new CAudioEncoder(addonProps));
case ADDON_AUDIODECODER:
return AddonPtr(new CAudioDecoder(addonProps));
case ADDON_REPOSITORY:
return AddonPtr(new CRepository(addonProps));
case ADDON_CONTEXT_ITEM:
return AddonPtr(new CContextItemAddon(addonProps));
default:
break;
}
return AddonPtr();
}
/*
* libcpluff interaction
*/
bool CAddonMgr::PlatformSupportsAddon(const cp_plugin_info_t *plugin) const
{
if (!plugin || !plugin->num_extensions)
return false;
const cp_extension_t *metadata = GetExtension(plugin, "xbmc.addon.metadata"); //<! backword compatibilty
if (!metadata)
metadata = CAddonMgr::Get().GetExtension(plugin, "kodi.addon.metadata");
if (!metadata)
return false;
vector<std::string> platforms;
if (CAddonMgr::Get().GetExtList(metadata->configuration, "platform", platforms))
{
for (vector<std::string>::const_iterator platform = platforms.begin(); platform != platforms.end(); ++platform)
{
if (*platform == "all")
return true;
#if defined(TARGET_ANDROID)
if (*platform == "android")
#elif defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
if (*platform == "linux")
#elif defined(TARGET_WINDOWS) && defined(HAS_DX)
if (*platform == "windx")
#elif defined(TARGET_DARWIN_OSX)
// Remove this after Frodo and add an architecture filter
// in addition to platform.
#if defined(__x86_64__)
if (*platform == "osx64" || *platform == "osx")
#else
if (*platform == "osx32" || *platform == "osx")
#endif
#elif defined(TARGET_DARWIN_IOS)
if (*platform == "ios")
#endif
return true;
}
return false; // no <platform> works for us
}
return true; // assume no <platform> is equivalent to <platform>all</platform>
}
const cp_cfg_element_t *CAddonMgr::GetExtElement(cp_cfg_element_t *base, const char *path)
{
const cp_cfg_element_t *element = NULL;
if (base)
element = m_cpluff->lookup_cfg_element(base, path);
return element;
}
bool CAddonMgr::GetExtElements(cp_cfg_element_t *base, const char *path, ELEMENTS &elements)
{
if (!base || !path)
return false;
for (unsigned int i = 0; i < base->num_children; i++)
{
std::string temp = base->children[i].name;
if (!temp.compare(path))
elements.push_back(&base->children[i]);
}
return !elements.empty();
}
const cp_extension_t *CAddonMgr::GetExtension(const cp_plugin_info_t *props, const char *extension) const
{
if (!props)
return NULL;
for (unsigned int i = 0; i < props->num_extensions; ++i)
{
if (0 == strcmp(props->extensions[i].ext_point_id, extension))
return &props->extensions[i];
}
return NULL;
}
std::string CAddonMgr::GetExtValue(cp_cfg_element_t *base, const char *path)
{
const char *value = "";
if (base && (value = m_cpluff->lookup_cfg_value(base, path)))
return value;
else
return "";
}
bool CAddonMgr::GetExtList(cp_cfg_element_t *base, const char *path, vector<std::string> &result) const
{
result.clear();
if (!base || !path)
return false;
const char *all = m_cpluff->lookup_cfg_value(base, path);
if (!all || *all == 0)
return false;
StringUtils::Tokenize(all, result, ' ');
return true;
}
AddonPtr CAddonMgr::GetAddonFromDescriptor(const cp_plugin_info_t *info,
const std::string& type)
{
if (!info)
return AddonPtr();
if (!info->extensions && type.empty())
{ // no extensions, so we need only the dep information
return AddonPtr(new CAddon(info));
}
// grab a relevant extension point, ignoring our kodi.addon.metadata extension point
for (unsigned int i = 0; i < info->num_extensions; ++i)
{
if (0 != strcmp("xbmc.addon.metadata" , info->extensions[i].ext_point_id) && //<! backword compatibilty
0 != strcmp("kodi.addon.metadata" , info->extensions[i].ext_point_id) &&
(type.empty() || 0 == strcmp(type.c_str(), info->extensions[i].ext_point_id)))
{ // note that Factory takes care of whether or not we have platform support
return Factory(&info->extensions[i]);
}
}
return AddonPtr();
}
// FIXME: This function may not be required
bool CAddonMgr::LoadAddonDescription(const std::string &path, AddonPtr &addon)
{
cp_status_t status;
cp_plugin_info_t *info = m_cpluff->load_plugin_descriptor(m_cp_context, CSpecialProtocol::TranslatePath(path).c_str(), &status);
if (info)
{
addon = GetAddonFromDescriptor(info);
m_cpluff->release_info(m_cp_context, info);
return NULL != addon.get();
}
return false;
}
bool CAddonMgr::AddonsFromRepoXML(const TiXmlElement *root, VECADDONS &addons)
{
// create a context for these addons
cp_status_t status;
cp_context_t *context = m_cpluff->create_context(&status);
if (!root || !context)
return false;
// each addon XML should have a UTF-8 declaration
TiXmlDeclaration decl("1.0", "UTF-8", "");
const TiXmlElement *element = root->FirstChildElement("addon");
while (element)
{
// dump the XML back to text
std::string xml;
xml << decl;
xml << *element;
cp_status_t status;
cp_plugin_info_t *info = m_cpluff->load_plugin_descriptor_from_memory(context, xml.c_str(), xml.size(), &status);
if (info)
{
AddonPtr addon = GetAddonFromDescriptor(info);
if (addon.get())
addons.push_back(addon);
m_cpluff->release_info(context, info);
}
element = element->NextSiblingElement("addon");
}
m_cpluff->destroy_context(context);
return true;
}
bool CAddonMgr::LoadAddonDescriptionFromMemory(const TiXmlElement *root, AddonPtr &addon)
{
// create a context for these addons
cp_status_t status;
cp_context_t *context = m_cpluff->create_context(&status);
if (!root || !context)
return false;
// dump the XML back to text
std::string xml;
xml << TiXmlDeclaration("1.0", "UTF-8", "");
xml << *root;
cp_plugin_info_t *info = m_cpluff->load_plugin_descriptor_from_memory(context, xml.c_str(), xml.size(), &status);
if (info)
{
addon = GetAddonFromDescriptor(info);
m_cpluff->release_info(context, info);
}
m_cpluff->destroy_context(context);
return addon != NULL;
}
bool CAddonMgr::StartServices(const bool beforelogin)
{
CLog::Log(LOGDEBUG, "ADDON: Starting service addons.");
VECADDONS services;
if (!GetAddons(ADDON_SERVICE, services))
return false;
bool ret = true;
for (IVECADDONS it = services.begin(); it != services.end(); ++it)
{
std::shared_ptr<CService> service = std::dynamic_pointer_cast<CService>(*it);
if (service)
{
if ( (beforelogin && service->GetStartOption() == CService::STARTUP)
|| (!beforelogin && service->GetStartOption() == CService::LOGIN) )
ret &= service->Start();
}
}
return ret;
}
void CAddonMgr::StopServices(const bool onlylogin)
{
CLog::Log(LOGDEBUG, "ADDON: Stopping service addons.");
VECADDONS services;
if (!GetAddons(ADDON_SERVICE, services))
return;
for (IVECADDONS it = services.begin(); it != services.end(); ++it)
{
std::shared_ptr<CService> service = std::dynamic_pointer_cast<CService>(*it);
if (service)
{