2 * Copyright (C) 2016-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "BinaryAddonCache.h"
11 #include "ServiceBroker.h"
12 #include "addons/AddonEvents.h"
13 #include "addons/AddonManager.h"
14 #include "addons/addoninfo/AddonType.h"
21 const std::vector
<AddonType
> ADDONS_TO_CACHE
= {AddonType::GAMEDLL
};
23 CBinaryAddonCache::~CBinaryAddonCache()
28 void CBinaryAddonCache::Init()
30 CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CBinaryAddonCache::OnEvent
);
34 void CBinaryAddonCache::Deinit()
36 CServiceBroker::GetAddonMgr().Events().Unsubscribe(this);
39 void CBinaryAddonCache::GetAddons(VECADDONS
& addons
, AddonType type
)
42 GetInstalledAddons(myAddons
, type
);
44 for (auto &addon
: myAddons
)
46 if (!CServiceBroker::GetAddonMgr().IsAddonDisabled(addon
->ID()))
47 addons
.emplace_back(std::move(addon
));
51 void CBinaryAddonCache::GetDisabledAddons(VECADDONS
& addons
, AddonType type
)
54 GetInstalledAddons(myAddons
, type
);
56 for (auto &addon
: myAddons
)
58 if (CServiceBroker::GetAddonMgr().IsAddonDisabled(addon
->ID()))
59 addons
.emplace_back(std::move(addon
));
63 void CBinaryAddonCache::GetInstalledAddons(VECADDONS
& addons
, AddonType type
)
65 std::unique_lock
<CCriticalSection
> lock(m_critSection
);
66 auto it
= m_addons
.find(type
);
67 if (it
!= m_addons
.end())
71 AddonPtr
CBinaryAddonCache::GetAddonInstance(const std::string
& strId
, AddonType type
)
75 std::unique_lock
<CCriticalSection
> lock(m_critSection
);
77 auto it
= m_addons
.find(type
);
78 if (it
!= m_addons
.end())
80 VECADDONS
& addons
= it
->second
;
81 auto itAddon
= std::find_if(addons
.begin(), addons
.end(),
82 [&strId
](const AddonPtr
& addon
)
84 return addon
->ID() == strId
;
87 if (itAddon
!= addons
.end())
94 void CBinaryAddonCache::OnEvent(const AddonEvent
& event
)
96 if (typeid(event
) == typeid(AddonEvents::Enabled
) ||
97 typeid(event
) == typeid(AddonEvents::Disabled
) ||
98 typeid(event
) == typeid(AddonEvents::ReInstalled
))
100 for (auto &type
: ADDONS_TO_CACHE
)
102 if (CServiceBroker::GetAddonMgr().HasType(event
.addonId
, type
))
109 else if (typeid(event
) == typeid(AddonEvents::UnInstalled
))
115 void CBinaryAddonCache::Update()
117 using AddonMap
= std::multimap
<AddonType
, VECADDONS
>;
120 for (auto &addonType
: ADDONS_TO_CACHE
)
123 CServiceBroker::GetAddonMgr().GetInstalledAddons(addons
, addonType
);
124 addonmap
.insert(AddonMap::value_type(addonType
, addons
));
128 std::unique_lock
<CCriticalSection
> lock(m_critSection
);
129 m_addons
= std::move(addonmap
);