Merge pull request #24470 from fuzzard/release_20.3
[xbmc.git] / xbmc / addons / AddonManager.h
blobcdce156e0a3267058077f0fffe01e0df0dbe85f3
1 /*
2 * Copyright (C) 2005-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.
7 */
9 #pragma once
11 #include "threads/CriticalSection.h"
12 #include "utils/EventStream.h"
14 #include <map>
15 #include <memory>
16 #include <mutex>
17 #include <set>
18 #include <string>
19 #include <vector>
21 namespace ADDON
23 enum class AddonDisabledReason;
24 enum class AddonOriginType;
25 enum class AddonType;
26 enum class AddonUpdateRule;
27 enum class AllowCheckForUpdates : bool;
29 class CAddonDatabase;
30 class CAddonUpdateRules;
31 class CAddonVersion;
32 class IAddonMgrCallback;
34 class CAddonInfo;
35 using AddonInfoPtr = std::shared_ptr<CAddonInfo>;
36 using ADDON_INFO_LIST = std::map<std::string, AddonInfoPtr>;
38 class IAddon;
39 using AddonPtr = std::shared_ptr<IAddon>;
40 using VECADDONS = std::vector<AddonPtr>;
42 struct AddonEvent;
43 struct AddonWithUpdate;
44 struct DependencyInfo;
45 struct RepositoryDirInfo;
47 using AddonInstanceId = uint32_t;
49 enum class AddonCheckType : bool
51 OUTDATED_ADDONS,
52 AVAILABLE_UPDATES,
55 enum class OnlyEnabled : bool
57 CHOICE_YES = true,
58 CHOICE_NO = false,
61 enum class OnlyEnabledRootAddon : bool
63 CHOICE_YES = true,
64 CHOICE_NO = false,
67 enum class CheckIncompatible : bool
69 CHOICE_YES = true,
70 CHOICE_NO = false,
73 /**
74 * Class - CAddonMgr
75 * Holds references to all addons, enabled or
76 * otherwise. Services the generic callbacks available
77 * to all addon variants.
79 class CAddonMgr
81 public:
82 bool ReInit()
84 DeInit();
85 return Init();
87 bool Init();
88 void DeInit();
90 CAddonMgr();
91 CAddonMgr(const CAddonMgr&) = delete;
92 virtual ~CAddonMgr();
94 CEventStream<AddonEvent>& Events() { return m_events; }
95 CEventStream<AddonEvent>& UnloadEvents() { return m_unloadEvents; }
97 IAddonMgrCallback* GetCallbackForType(AddonType type);
98 bool RegisterAddonMgrCallback(AddonType type, IAddonMgrCallback* cb);
99 void UnregisterAddonMgrCallback(AddonType type);
101 /*! \brief Retrieve a specific addon (of a specific type)
102 \param id the id of the addon to retrieve.
103 \param addon[out] the retrieved addon pointer - only use if the function returns true.
104 \param type type of addon to retrieve - defaults to any type.
105 \param onlyEnabled whether we only want enabled addons - set to false to allow both enabled and disabled addons - defaults to true.
106 \return true if an addon matching the id of the given type is available and is enabled (if onlyEnabled is true).
108 bool GetAddon(const std::string& id,
109 AddonPtr& addon,
110 AddonType type,
111 OnlyEnabled onlyEnabled) const;
113 /*! \brief Retrieve a specific addon (of no specific type)
114 \param id the id of the addon to retrieve.
115 \param addon[out] the retrieved addon pointer - only use if the function returns true.
116 \param onlyEnabled whether we only want enabled addons - set to false to allow both enabled and disabled addons - defaults to true.
117 \return true if an addon matching the id of any type is available and is enabled (if onlyEnabled is true).
119 bool GetAddon(const std::string& id, AddonPtr& addon, OnlyEnabled onlyEnabled) const;
121 bool HasType(const std::string& id, AddonType type);
123 bool HasAddons(AddonType type);
125 bool HasInstalledAddons(AddonType type);
127 /*! Returns all installed, enabled and incompatible (and disabled) add-ons. */
128 bool GetAddonsForUpdate(VECADDONS& addons) const;
130 /*! Returns all installed, enabled add-ons. */
131 bool GetAddons(VECADDONS& addons) const;
133 /*! Returns enabled add-ons with given type. */
134 bool GetAddons(VECADDONS& addons, AddonType type);
136 /*! Returns all installed, including disabled. */
137 bool GetInstalledAddons(VECADDONS& addons);
139 /*! Returns installed add-ons, including disabled, with given type. */
140 bool GetInstalledAddons(VECADDONS& addons, AddonType type);
142 bool GetDisabledAddons(VECADDONS& addons);
144 bool GetDisabledAddons(VECADDONS& addons, AddonType type);
146 /*! Get all installable addons */
147 bool GetInstallableAddons(VECADDONS& addons);
149 bool GetInstallableAddons(VECADDONS& addons, AddonType type);
151 /*! \brief Get the installable addon depending on install rules
152 * or fall back to highest version.
153 * \note This function gets called in different contexts. If it's
154 * called for checking possible updates for already installed addons
155 * our update restriction rules apply.
156 * If it's called to (for example) populate an addon-select-dialog
157 * the addon is not installed yet, and we have to fall back to the
158 * highest version.
159 * \param addonId addon to check for update or installation
160 * \param addon[out] the retrieved addon pointer - only use if the function returns true.
161 * \return true if an addon matching the id is available.
163 bool FindInstallableById(const std::string& addonId, AddonPtr& addon);
165 void AddToUpdateableAddons(AddonPtr& pAddon);
166 void RemoveFromUpdateableAddons(AddonPtr& pAddon);
167 bool ReloadSettings(const std::string& addonId, AddonInstanceId instanceId);
169 /*! Get addons with available updates */
170 std::vector<std::shared_ptr<IAddon>> GetAvailableUpdates() const;
172 /*! Get addons that are outdated */
173 std::vector<std::shared_ptr<IAddon>> GetOutdatedAddons() const;
175 /*! Returns true if there is any addon with available updates, otherwise false */
176 bool HasAvailableUpdates();
179 * \brief Checks if the passed in addon is an orphaned dependency
180 * \param addon the add-on/dependency to check
181 * \param allAddons vector of all installed add-ons
182 * \return true or false
184 bool IsOrphaned(const std::shared_ptr<IAddon>& addon,
185 const std::vector<std::shared_ptr<IAddon>>& allAddons) const;
187 /*! \brief Checks for new / updated add-ons
188 \return True if everything went ok, false otherwise
190 bool FindAddons();
192 /*! \brief Checks whether given addon with given origin/version is installed
193 * \param addonId addon to check
194 * \param origin origin to check
195 * \param addonVersion version to check
196 * \return True if installed, false otherwise
198 bool FindAddon(const std::string& addonId,
199 const std::string& origin,
200 const CAddonVersion& addonVersion);
203 * @brief Fills the the provided vector with the list of incompatible
204 * enabled addons and returns if there's any.
206 * @param[out] incompatible List of incompatible addons
207 * @return true if there are incompatible addons
209 bool GetIncompatibleEnabledAddonInfos(std::vector<AddonInfoPtr>& incompatible) const;
212 * Migrate all the addons (updates all addons that have an update pending and disables those
213 * that got incompatible)
215 * @return list of all addons (infos) that were modified.
217 std::vector<AddonInfoPtr> MigrateAddons();
220 * @brief Try to disable addons in the given list.
222 * @param[in] incompatible List of incompatible addon infos
223 * @return list of all addon Infos that were disabled
225 std::vector<AddonInfoPtr> DisableIncompatibleAddons(
226 const std::vector<AddonInfoPtr>& incompatible);
229 * Install available addon updates, if any.
230 * @param wait If kodi should wait for all updates to download and install before returning
232 void CheckAndInstallAddonUpdates(bool wait) const;
235 * @note: should only be called by AddonInstaller
237 * Unload addon from the system. Returns true if it was unloaded, otherwise false.
239 bool UnloadAddon(const std::string& addonId);
242 * @note: should only be called by AddonInstaller
244 * Returns true if the addon was successfully loaded and enabled; otherwise false.
246 bool LoadAddon(const std::string& addonId,
247 const std::string& origin,
248 const CAddonVersion& addonVersion);
250 /*! @note: should only be called by AddonInstaller
252 * Hook for clearing internal state after uninstall.
254 void OnPostUnInstall(const std::string& id);
256 /*! \brief Disable an addon. Returns true on success, false on failure. */
257 bool DisableAddon(const std::string& ID, AddonDisabledReason disabledReason);
259 /*! \brief Updates reason for a disabled addon. Returns true on success, false on failure. */
260 bool UpdateDisabledReason(const std::string& id, AddonDisabledReason newDisabledReason);
262 /*! \brief Enable an addon. Returns true on success, false on failure. */
263 bool EnableAddon(const std::string& ID);
265 /* \brief Check whether an addon has been disabled via DisableAddon.
266 In case the disabled cache does not know about the current state the database routine will be used.
267 \param ID id of the addon
268 \sa DisableAddon
270 bool IsAddonDisabled(const std::string& ID) const;
273 * @brief Check whether an addon has been disabled via DisableAddon except for a particular
274 * reason In case the disabled cache does not know about the current state the database routine
275 * will be used.
276 * @param[in] ID id of the addon
277 * @param[in] disabledReason the reason that will be an exception to being disabled
278 * @return true if the addon was disabled except for the specified reason
279 * @sa DisableAddon
281 bool IsAddonDisabledExcept(const std::string& ID, AddonDisabledReason disabledReason) const;
283 /* \brief Checks whether an addon can be disabled via DisableAddon.
284 \param ID id of the addon
285 \sa DisableAddon
287 bool CanAddonBeDisabled(const std::string& ID);
289 bool CanAddonBeEnabled(const std::string& id);
291 /* \brief Checks whether an addon is installed.
292 \param ID id of the addon
294 bool IsAddonInstalled(const std::string& ID);
296 /* \brief Checks whether an addon is installed from a
297 * particular origin repo
298 * \note if checked for an origin defined as official (i.e. repository.xbmc.org)
299 * this function will return true even if the addon is a shipped system add-on
300 * \param ID id of the addon
301 * \param origin origin repository id
303 bool IsAddonInstalled(const std::string& ID, const std::string& origin) const;
305 /* \brief Checks whether an addon is installed from a
306 * particular origin repo and version
307 * \note if checked for an origin defined as official (i.e. repository.xbmc.org)
308 * this function will return true even if the addon is a shipped system add-on
309 * \param ID id of the addon
310 * \param origin origin repository id
311 * \param version the version of the addon
313 bool IsAddonInstalled(const std::string& ID,
314 const std::string& origin,
315 const CAddonVersion& version);
317 /* \brief Checks whether an addon can be installed. Broken addons can't be installed.
318 \param addon addon to be checked
320 bool CanAddonBeInstalled(const AddonPtr& addon);
322 bool CanUninstall(const AddonPtr& addon);
325 * @brief Checks whether an addon is a bundled addon
327 * @param[in] id id of the addon
328 * @return true if addon is bundled addon, false otherwise.
330 bool IsBundledAddon(const std::string& id);
333 * @brief Checks whether an addon is a system addon
335 * @param[in] id id of the addon
336 * @return true if addon is system addon, false otherwise.
338 bool IsSystemAddon(const std::string& id);
341 * @brief Checks whether an addon is a required system addon
343 * @param[in] id id of the addon
344 * @return true if addon is a required system addon, false otherwise.
346 bool IsRequiredSystemAddon(const std::string& id);
349 * @brief Checks whether an addon is an optional system addon
351 * @param[in] id id of the addon
352 * @return true if addon is an optional system addon, false otherwise.
354 bool IsOptionalSystemAddon(const std::string& id);
357 * @brief Addon update rules.
359 * member functions for handling and querying add-on update rules
361 * @warning This should be never used from other places outside of addon
362 * system directory.
365 /*@{{{*/
367 /* \brief Add a single update rule to the list for an addon
368 * \sa CAddonUpdateRules::AddUpdateRuleToList()
370 bool AddUpdateRuleToList(const std::string& id, AddonUpdateRule updateRule);
372 /* \brief Remove all rules from update rules list for an addon
373 * \sa CAddonUpdateRules::RemoveAllUpdateRulesFromList()
375 bool RemoveAllUpdateRulesFromList(const std::string& id);
377 /* \brief Remove a specific rule from update rules list for an addon
378 * \sa CAddonUpdateRules::RemoveUpdateRuleFromList()
380 bool RemoveUpdateRuleFromList(const std::string& id, AddonUpdateRule updateRule);
382 /* \brief Check if an addon version is auto-updateable
383 * \param id addon id to be checked
384 * \return true is addon is auto-updateable, false otherwise
385 * \sa CAddonUpdateRules::IsAutoUpdateable()
387 bool IsAutoUpdateable(const std::string& id) const;
389 /*@}}}*/
391 /* \brief Launches event AddonEvent::AutoUpdateStateChanged
392 * \param id addon id to pass through
393 * \sa CGUIDialogAddonInfo::OnToggleAutoUpdates()
395 void PublishEventAutoUpdateStateChanged(const std::string& id);
396 void UpdateLastUsed(const std::string& id);
399 * \brief Launches event @ref AddonEvent::InstanceAdded
401 * This is called when a new instance is added in add-on settings.
403 * \param[in] addonId Add-on id to pass through
404 * \param[in] instanceId Identifier of the add-on instance
406 void PublishInstanceAdded(const std::string& addonId, AddonInstanceId instanceId);
409 * \brief Launches event @ref AddonEvent::InstanceRemoved
411 * This is called when an instance is removed in add-on settings.
413 * \param[in] addonId Add-on id to pass through
414 * \param[in] instanceId Identifier of the add-on instance
416 void PublishInstanceRemoved(const std::string& addonId, AddonInstanceId instanceId);
418 /*! \brief Load the addon in the given path
419 This loads the addon using c-pluff which parses the addon descriptor file.
420 \param path folder that contains the addon.
421 \param addon [out] returned addon.
422 \return true if addon is set, false otherwise.
424 bool LoadAddonDescription(const std::string& path, AddonPtr& addon);
426 bool ServicesHasStarted() const;
429 * @deprecated This addon function should no more used and becomes replaced
430 * in future with the other below by his callers.
432 bool IsCompatible(const IAddon& addon) const;
435 * @brief Check given addon information is compatible with Kodi.
437 * @param[in] addonInfo Addon information to check
438 * @return true if compatible, false if not
440 bool IsCompatible(const AddonInfoPtr& addonInfo) const;
442 /*! \brief Recursively get dependencies for an add-on
443 * \param id the id of the root addon
444 * \param onlyEnabledRootAddon whether look for enabled root add-ons only
446 std::vector<DependencyInfo> GetDepsRecursive(const std::string& id,
447 OnlyEnabledRootAddon onlyEnabledRootAddon);
450 * @brief Get a list of add-on's with info's for the on system available
451 * ones.
453 * @param[out] addonInfos list where finded addon information becomes stored
454 * @param[in] onlyEnabled If true are only enabled ones given back,
455 * if false all on system available. Default is true.
456 * @param[in] type The requested type, with "ADDON_UNKNOWN" are all add-on
457 * types given back who match the case with value before.
458 * If a type id becomes added are only add-ons returned who
459 * match them. Default is for all types.
460 * @return true if the list contains entries
462 bool GetAddonInfos(std::vector<AddonInfoPtr>& addonInfos, bool onlyEnabled, AddonType type) const;
465 * @brief Get a list of add-on's with info's for the on system available
466 * ones.
468 * @param[in] onlyEnabled If true are only enabled ones given back,
469 * if false all on system available. Default is true.
470 * @param[in] types List about requested types.
471 * @return List where finded addon information becomes returned.
473 * @note @ref ADDON_UNKNOWN should not used for here!
475 std::vector<AddonInfoPtr> GetAddonInfos(bool onlyEnabled,
476 const std::vector<AddonType>& types) const;
479 * @brief Get a list of disabled add-on's with info's
481 * @param[out] addonInfos list where finded addon information becomes stored
482 * @param[in] type The requested type, with "ADDON_UNKNOWN"
483 * are all add-on types given back who match the case
484 * with value before.
485 * If a type id becomes added are only add-ons
486 * returned who match them. Default is for all types.
487 * @return true if the list contains entries
489 bool GetDisabledAddonInfos(std::vector<AddonInfoPtr>& addonInfos, AddonType type) const;
492 * @brief Get a list of disabled add-on's with info's for the on system
493 * available ones with a specific disabled reason.
495 * @param[out] addonInfos list where finded addon information becomes stored
496 * @param[in] type The requested type, with "ADDON_UNKNOWN"
497 * are all add-on types given back who match the case
498 * with value before.
499 * If a type id becomes added are only add-ons
500 * returned who match them. Default is for all types.
501 * @param[in] disabledReason To get all disabled addons use the value
502 * "AddonDiasbledReason::NONE". If any other value
503 * is supplied only addons with that reason will be
504 * returned.
505 * @return true if the list contains entries
507 bool GetDisabledAddonInfos(std::vector<AddonInfoPtr>& addonInfos,
508 AddonType type,
509 AddonDisabledReason disabledReason) const;
511 const AddonInfoPtr GetAddonInfo(const std::string& id, AddonType type) const;
514 * @brief Get the path where temporary add-on files are stored
516 * @return the base path used for temporary addon paths
518 * @warning the folder and its contents are deleted when Kodi is closed
520 const std::string& GetTempAddonBasePath() { return m_tempAddonBasePath; }
522 AddonOriginType GetAddonOriginType(const AddonPtr& addon) const;
525 * \brief Check whether an addon has been disabled with a special reason.
526 * \param ID id of the addon
527 * \param disabledReason reason we want to check for (NONE, USER, INCOMPATIBLE, PERMANENT_FAILURE)
528 * \return true or false
530 bool IsAddonDisabledWithReason(const std::string& ID, AddonDisabledReason disabledReason) const;
533 * @brief Addon update and install management.
535 * Parts inside here are used for changes about addon system.
537 * @warning This should be never used from other places outside of addon
538 * system directory.
540 /*@{{{*/
543 * @brief Update addon origin data.
545 * This becomes called from @ref CAddonInstallJob to set the source repo and
546 * if update, to set also the date.
548 * @note This must be called after the addon manager has inserted a new addon
549 * with @ref FindAddons() into database.
551 * @param[in] addonId Identifier of addon
552 * @param[in] repoAddonId Identifier of related repository addon
553 * @param[in] isUpdate If call becomes done on already installed addon and
554 * update only.
555 * @return True if successfully done, otherwise false
557 * Currently listed call sources:
558 * - @ref CAddonInstallJob::DoWork
560 bool SetAddonOrigin(const std::string& addonId, const std::string& repoAddonId, bool isUpdate);
563 * @brief Parse a repository XML file for addons and load their descriptors.
565 * A repository XML is essentially a concatenated list of addon descriptors.
567 * @param[in] repo The repository info.
568 * @param[in] xml The XML document from repository.
569 * @param[out] addons returned list of addons.
570 * @return true if the repository XML file is parsed, false otherwise.
572 * Currently listed call sources:
573 * - @ref CRepository::FetchIndex
575 bool AddonsFromRepoXML(const RepositoryDirInfo& repo,
576 const std::string& xml,
577 std::vector<AddonInfoPtr>& addons);
579 /*@}}}*/
582 * \brief Retrieves list of outdated addons as well as their related
583 * available updates and stores them into map.
584 * \return map of outdated addons with their update
586 std::map<std::string, AddonWithUpdate> GetAddonsWithAvailableUpdate() const;
589 * \brief Retrieves list of compatible addon versions of all origins
590 * \param[in] addonId addon to look up
591 * \return vector containing compatible addon versions
593 std::vector<std::shared_ptr<IAddon>> GetCompatibleVersions(const std::string& addonId) const;
596 * \brief Return number of available updates formatted as string
597 * this can be used as a lightweight method of retrieving the number of updates
598 * rather than using the expensive GetAvailableUpdates call
599 * \return number of available updates
601 const std::string& GetLastAvailableUpdatesCountAsString() const;
604 * \brief returns a vector with all found orphaned dependencies.
605 * \return the vector
607 std::vector<std::shared_ptr<IAddon>> GetOrphanedDependencies() const;
609 private:
610 CAddonMgr& operator=(CAddonMgr const&) = delete;
612 VECADDONS m_updateableAddons;
615 * \brief returns a vector with either available updates or outdated addons.
616 * usually called by its wrappers GetAvailableUpdates() or
617 * GetOutdatedAddons()
618 * \param[in] true to return outdated addons, false to return available updates
619 * \return vector filled with either available updates or outdated addons
621 std::vector<std::shared_ptr<IAddon>> GetAvailableUpdatesOrOutdatedAddons(
622 AddonCheckType addonCheckType) const;
624 bool GetAddonsInternal(AddonType type,
625 VECADDONS& addons,
626 OnlyEnabled onlyEnabled,
627 CheckIncompatible checkIncompatible) const;
629 bool EnableSingle(const std::string& id);
631 void FindAddons(ADDON_INFO_LIST& addonmap, const std::string& path);
634 * @brief Fills the the provided vector with the list of incompatible
635 * addons and returns if there's any.
637 * @param[out] incompatible List of incompatible addons
638 * @param[in] whether or not to include incompatible addons that are disabled
639 * @return true if there are incompatible addons
641 bool GetIncompatibleAddonInfos(std::vector<AddonInfoPtr>& incompatible,
642 bool includeDisabled) const;
645 * Get the list of of available updates
646 * \param[in,out] updates the vector of addons to be filled with addons that need to be updated (not blacklisted)
647 * \return if there are any addons needing updates
649 bool GetAddonUpdateCandidates(VECADDONS& updates) const;
651 /*!\brief Sort a list of addons for installation, i.e., defines the order of installation depending
652 * of each addon dependencies.
653 * \param[in,out] updates the vector of addons to sort
655 void SortByDependencies(VECADDONS& updates) const;
658 * Install the list of addon updates via AddonInstaller
659 * \param[in,out] updates the vector of addons to install (will be sorted)
660 * \param wait if the process should wait for all addons to install
661 * \param allowCheckForUpdates indicates if content update checks are allowed
662 * after installation of a repository addon from the list
664 void InstallAddonUpdates(VECADDONS& updates,
665 bool wait,
666 AllowCheckForUpdates allowCheckForUpdates) const;
668 // This guards the addon installation process to make sure
669 // addon updates are not installed concurrently
670 // while the migration is running. Addon updates can be triggered
671 // as a result of a repository update event.
672 // (migration will install any available update anyway)
673 mutable std::mutex m_installAddonsMutex;
675 std::map<std::string, AddonDisabledReason> m_disabled;
676 static std::map<AddonType, IAddonMgrCallback*> m_managers;
677 mutable CCriticalSection m_critSection;
678 std::unique_ptr<CAddonDatabase> m_database;
679 std::unique_ptr<CAddonUpdateRules> m_updateRules;
680 CEventSource<AddonEvent> m_events;
681 CBlockingEventSource<AddonEvent> m_unloadEvents;
682 std::set<std::string> m_systemAddons;
683 std::set<std::string> m_optionalSystemAddons;
684 ADDON_INFO_LIST m_installedAddons;
686 // Temporary path given to add-ons, whose content is deleted when Kodi is stopped
687 const std::string m_tempAddonBasePath = "special://temp/addons";
690 * latest count of available updates
692 mutable std::string m_lastAvailableUpdatesCountAsString;
693 mutable std::mutex m_lastAvailableUpdatesCountMutex;
696 }; /* namespace ADDON */