/xbmc/addons/AddonManager.h

http://github.com/xbmc/xbmc · C Header · 299 lines · 105 code · 61 blank · 133 comment · 0 complexity · 2422c8bea6567c65fcaeab124d913f61 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #pragma once
  9. #include "Addon.h"
  10. #include "AddonDatabase.h"
  11. #include "Repository.h"
  12. #include "threads/CriticalSection.h"
  13. #include "utils/EventStream.h"
  14. namespace ADDON
  15. {
  16. typedef std::map<TYPE, VECADDONS> MAPADDONS;
  17. typedef std::map<TYPE, VECADDONS>::iterator IMAPADDONS;
  18. typedef std::map<std::string, AddonInfoPtr> ADDON_INFO_LIST;
  19. /*!
  20. * @brief The value binaryAddonList use a tuple in following construct:
  21. * | Number | Type | Description
  22. * |:------:|------------:|:------------------------------------------------
  23. * | first | boolean | If true addon is enabled, otherwise disabled
  24. * | second | CAddonInfo | Information data of addon
  25. */
  26. typedef std::pair<bool, AddonInfoPtr> BINARY_ADDON_LIST_ENTRY;
  27. typedef std::vector<BINARY_ADDON_LIST_ENTRY> BINARY_ADDON_LIST;
  28. const std::string ADDON_PYTHON_EXT = "*.py";
  29. /**
  30. * Class - IAddonMgrCallback
  31. * This callback should be inherited by any class which manages
  32. * specific addon types. Could be mostly used for Dll addon types to handle
  33. * cleanup before restart/removal
  34. */
  35. class IAddonMgrCallback
  36. {
  37. public:
  38. virtual ~IAddonMgrCallback() = default;
  39. virtual bool RequestRestart(const std::string& id, bool datachanged)=0;
  40. };
  41. /**
  42. * Class - CAddonMgr
  43. * Holds references to all addons, enabled or
  44. * otherwise. Services the generic callbacks available
  45. * to all addon variants.
  46. */
  47. class CAddonMgr
  48. {
  49. public:
  50. bool ReInit() { DeInit(); return Init(); }
  51. bool Init();
  52. void DeInit();
  53. CAddonMgr() = default;
  54. CAddonMgr(const CAddonMgr&) = delete;
  55. virtual ~CAddonMgr();
  56. CEventStream<AddonEvent>& Events() { return m_events; }
  57. CEventStream<AddonEvent>& UnloadEvents() { return m_unloadEvents; }
  58. IAddonMgrCallback* GetCallbackForType(TYPE type);
  59. bool RegisterAddonMgrCallback(TYPE type, IAddonMgrCallback* cb);
  60. void UnregisterAddonMgrCallback(TYPE type);
  61. /*! \brief Retrieve a specific addon (of a specific type)
  62. \param id the id of the addon to retrieve.
  63. \param addon [out] the retrieved addon pointer - only use if the function returns true.
  64. \param type type of addon to retrieve - defaults to any type.
  65. \param enabledOnly whether we only want enabled addons - set to false to allow both enabled and disabled addons - defaults to true.
  66. \return true if an addon matching the id of the given type is available and is enabled (if enabledOnly is true).
  67. */
  68. bool GetAddon(const std::string& id,
  69. AddonPtr& addon,
  70. const TYPE& type = ADDON_UNKNOWN,
  71. bool enabledOnly = true) const;
  72. bool HasType(const std::string &id, const TYPE &type);
  73. bool HasAddons(const TYPE &type);
  74. bool HasInstalledAddons(const TYPE &type);
  75. /*! Returns all installed, enabled add-ons. */
  76. bool GetAddons(VECADDONS& addons) const;
  77. /*! Returns enabled add-ons with given type. */
  78. bool GetAddons(VECADDONS& addons, const TYPE& type);
  79. /*! Returns all installed, including disabled. */
  80. bool GetInstalledAddons(VECADDONS& addons);
  81. /*! Returns installed add-ons, including disabled, with given type. */
  82. bool GetInstalledAddons(VECADDONS& addons, const TYPE& type);
  83. bool GetDisabledAddons(VECADDONS& addons);
  84. bool GetDisabledAddons(VECADDONS& addons, const TYPE& type);
  85. /*! Get all installable addons */
  86. bool GetInstallableAddons(VECADDONS& addons);
  87. bool GetInstallableAddons(VECADDONS& addons, const TYPE &type);
  88. /*!
  89. * @brief To get all installed binary addon on Kodi
  90. *
  91. * This function becomes used from ADDON::CBinaryAddonManager to get his
  92. * related addons (whether enabled or disabled).
  93. *
  94. * @param[out] binaryAddonList The list where from here the binary addons
  95. * becomes stored.
  96. * @return If list is not empty becomes true returned
  97. */
  98. bool GetInstalledBinaryAddons(BINARY_ADDON_LIST& binaryAddonList);
  99. /*!
  100. * @brief To get requested installed binary addon on Kodi
  101. *
  102. * This function is used by ADDON::CBinaryAddonManager to obtain the add-on
  103. * with the given id, regardless the add-on is disabled or enabled.
  104. *
  105. * @param[in] addonId Id to get
  106. * @param[out] binaryAddon Addon info returned
  107. * @return True, if the requested add-on was found, false otherwise
  108. */
  109. bool GetInstalledBinaryAddon(const std::string& addonId, BINARY_ADDON_LIST_ENTRY& binaryAddon);
  110. /*! Get the installable addon with the highest version. */
  111. bool FindInstallableById(const std::string& addonId, AddonPtr& addon);
  112. void AddToUpdateableAddons(AddonPtr &pAddon);
  113. void RemoveFromUpdateableAddons(AddonPtr &pAddon);
  114. bool ReloadSettings(const std::string &id);
  115. /*! Get addons with available updates */
  116. VECADDONS GetAvailableUpdates() const;
  117. /*! Returns true if there is any addon with available updates, otherwise false */
  118. bool HasAvailableUpdates();
  119. static AddonPtr AddonFromProps(const AddonInfoPtr& addonInfo);
  120. /*! \brief Checks for new / updated add-ons
  121. \return True if everything went ok, false otherwise
  122. */
  123. bool FindAddons();
  124. /*!
  125. * Fills the the provided vector with the list of incompatible addons and returns if there's any.
  126. *
  127. * @return true if there are incompatible addons
  128. */
  129. bool GetIncompatibleAddons(VECADDONS& incompatible) const;
  130. /*!
  131. * Migrate all the addons (updates all addons that have an update pending and disables those
  132. * that got incompatible)
  133. *
  134. * @return list of all addons that were modified.
  135. */
  136. std::vector<std::string> MigrateAddons();
  137. /*!
  138. * Install available addon updates, if any.
  139. * @param wait If kodi should wait for all updates to download and install before returning
  140. */
  141. void CheckAndInstallAddonUpdates(bool wait) const;
  142. /*!
  143. * @note: should only be called by AddonInstaller
  144. *
  145. * Unload addon from the system. Returns true if it was unloaded, otherwise false.
  146. */
  147. bool UnloadAddon(const std::string& addonId);
  148. /*!
  149. * @note: should only be called by AddonInstaller
  150. *
  151. * Returns true if the addon was successfully loaded and enabled; otherwise false.
  152. */
  153. bool LoadAddon(const std::string& addonId);
  154. /*! @note: should only be called by AddonInstaller
  155. *
  156. * Hook for clearing internal state after uninstall.
  157. */
  158. void OnPostUnInstall(const std::string& id);
  159. /*! \brief Disable an addon. Returns true on success, false on failure. */
  160. bool DisableAddon(const std::string& ID);
  161. /*! \brief Enable an addon. Returns true on success, false on failure. */
  162. bool EnableAddon(const std::string& ID);
  163. /* \brief Check whether an addon has been disabled via DisableAddon.
  164. In case the disabled cache does not know about the current state the database routine will be used.
  165. \param ID id of the addon
  166. \sa DisableAddon
  167. */
  168. bool IsAddonDisabled(const std::string& ID) const;
  169. /* \brief Checks whether an addon can be disabled via DisableAddon.
  170. \param ID id of the addon
  171. \sa DisableAddon
  172. */
  173. bool CanAddonBeDisabled(const std::string& ID);
  174. bool CanAddonBeEnabled(const std::string& id);
  175. /* \brief Checks whether an addon is installed.
  176. \param ID id of the addon
  177. */
  178. bool IsAddonInstalled(const std::string& ID);
  179. /* \brief Checks whether an addon can be installed. Broken addons can't be installed.
  180. \param addon addon to be checked
  181. */
  182. bool CanAddonBeInstalled(const AddonPtr& addon);
  183. bool CanUninstall(const AddonPtr& addon);
  184. bool IsSystemAddon(const std::string& id);
  185. bool AddToUpdateBlacklist(const std::string& id);
  186. bool RemoveFromUpdateBlacklist(const std::string& id);
  187. bool IsBlacklisted(const std::string& id) const;
  188. void UpdateLastUsed(const std::string& id);
  189. /*! \brief Load the addon in the given path
  190. This loads the addon using c-pluff which parses the addon descriptor file.
  191. \param path folder that contains the addon.
  192. \param addon [out] returned addon.
  193. \return true if addon is set, false otherwise.
  194. */
  195. bool LoadAddonDescription(const std::string &path, AddonPtr &addon);
  196. /*! \brief Parse a repository XML file for addons and load their descriptors
  197. A repository XML is essentially a concatenated list of addon descriptors.
  198. \param repo The repository info.
  199. \param xml The XML document from repository.
  200. \param addons [out] returned list of addons.
  201. \return true if the repository XML file is parsed, false otherwise.
  202. */
  203. bool AddonsFromRepoXML(const CRepository::DirInfo& repo, const std::string& xml, VECADDONS& addons);
  204. bool ServicesHasStarted() const;
  205. bool IsCompatible(const IAddon& addon) const;
  206. /*! \brief Recursively get dependencies for an add-on
  207. */
  208. std::vector<DependencyInfo> GetDepsRecursive(const std::string& id);
  209. bool GetAddonInfos(AddonInfos& addonInfos, TYPE type) const;
  210. const AddonInfoPtr GetAddonInfo(const std::string& id, TYPE type = ADDON_UNKNOWN) const;
  211. /*!
  212. * @brief Get the path where temporary add-on files are stored
  213. *
  214. * @return the base path used for temporary addon paths
  215. *
  216. * @warning the folder and its contents are deleted when Kodi is closed
  217. */
  218. const std::string& GetTempAddonBasePath() { return m_tempAddonBasePath; }
  219. private:
  220. CAddonMgr& operator=(CAddonMgr const&) = delete;
  221. VECADDONS m_updateableAddons;
  222. bool GetAddonsInternal(const TYPE& type, VECADDONS& addons, bool enabledOnly) const;
  223. bool EnableSingle(const std::string& id);
  224. void FindAddons(ADDON_INFO_LIST& addonmap, const std::string& path);
  225. std::set<std::string> m_disabled;
  226. std::set<std::string> m_updateBlacklist;
  227. static std::map<TYPE, IAddonMgrCallback*> m_managers;
  228. mutable CCriticalSection m_critSection;
  229. CAddonDatabase m_database;
  230. CEventSource<AddonEvent> m_events;
  231. CBlockingEventSource<AddonEvent> m_unloadEvents;
  232. std::set<std::string> m_systemAddons;
  233. std::set<std::string> m_optionalAddons;
  234. ADDON_INFO_LIST m_installedAddons;
  235. // Temporary path given to add-ons, whose content is deleted when Kodi is stopped
  236. const std::string m_tempAddonBasePath = "special://temp/addons";
  237. };
  238. }; /* namespace ADDON */