3 * Copyright (C) 2005-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
22 #include "threads/CriticalSection.h"
23 #include "utils/StdString.h"
24 #include "utils/Observer.h"
28 #include "AddonDatabase.h"
33 #include "lib/cpluff/libcpluff/cpluff.h"
38 typedef std::map<TYPE, VECADDONS> MAPADDONS;
39 typedef std::map<TYPE, VECADDONS>::iterator IMAPADDONS;
40 typedef std::vector<cp_cfg_element_t*> ELEMENTS;
42 const CStdString ADDON_METAFILE = "description.xml";
43 const CStdString ADDON_VIS_EXT = "*.vis";
44 const CStdString ADDON_PYTHON_EXT = "*.py";
45 const CStdString ADDON_SCRAPER_EXT = "*.xml";
46 const CStdString ADDON_SCREENSAVER_EXT = "*.xbs";
47 const CStdString ADDON_PVRDLL_EXT = "*.pvr";
48 const CStdString ADDON_DSP_AUDIO_EXT = "*.adsp";
49 const CStdString ADDON_VERSION_RE = "(?<Major>\\d*)\\.?(?<Minor>\\d*)?\\.?(?<Build>\\d*)?\\.?(?<Revision>\\d*)?";
52 * Class - IAddonMgrCallback
53 * This callback should be inherited by any class which manages
54 * specific addon types. Could be mostly used for Dll addon types to handle
55 * cleanup before restart/removal
57 class IAddonMgrCallback
60 virtual ~IAddonMgrCallback() {};
61 virtual bool RequestRestart(AddonPtr addon, bool datachanged)=0;
62 virtual bool RequestRemoval(AddonPtr addon)=0;
67 * Holds references to all addons, enabled or
68 * otherwise. Services the generic callbacks available
69 * to all addon variants.
71 class CAddonMgr : public Observable
74 static CAddonMgr &Get();
75 bool ReInit() { DeInit(); return Init(); }
79 IAddonMgrCallback* GetCallbackForType(TYPE type);
80 bool RegisterAddonMgrCallback(TYPE type, IAddonMgrCallback* cb);
81 void UnregisterAddonMgrCallback(TYPE type);
84 bool GetDefault(const TYPE &type, AddonPtr &addon);
85 bool SetDefault(const TYPE &type, const CStdString &addonID);
86 /*! \brief Retrieve a specific addon (of a specific type)
87 \param id the id of the addon to retrieve.
88 \param addon [out] the retrieved addon pointer - only use if the function returns true.
89 \param type type of addon to retrieve - defaults to any type.
90 \param enabledOnly whether we only want enabled addons - set to false to allow both enabled and disabled addons - defaults to true.
91 \return true if an addon matching the id of the given type is available and is enabled (if enabledOnly is true).
93 bool GetAddon(const CStdString &id, AddonPtr &addon, const TYPE &type = ADDON_UNKNOWN, bool enabledOnly = true);
94 bool HasAddons(const TYPE &type, bool enabled = true);
95 bool GetAddons(const TYPE &type, VECADDONS &addons, bool enabled = true);
96 bool GetAllAddons(VECADDONS &addons, bool enabled = true, bool allowRepos = false);
97 void AddToUpdateableAddons(AddonPtr &pAddon);
98 void RemoveFromUpdateableAddons(AddonPtr &pAddon);
99 bool ReloadSettings(const CStdString &id);
100 /*! \brief Get all addons with available updates
101 \param addons List to fill with all outdated addons
102 \param enabled Whether to get only enabled or disabled addons
103 \return True if there are outdated addons otherwise false
105 bool GetAllOutdatedAddons(VECADDONS &addons, bool enabled = true);
106 /*! \brief Checks if there is any addon with available updates
107 \param enabled Whether to check only enabled or disabled addons
108 \return True if there are outdated addons otherwise false
110 bool HasOutdatedAddons(bool enabled = true);
111 CStdString GetString(const CStdString &id, const int number);
113 const char *GetTranslatedString(const cp_cfg_element_t *root, const char *tag);
114 static AddonPtr AddonFromProps(AddonProps& props);
116 void RemoveAddon(const CStdString& ID);
118 /* \brief Disable an addon
119 Triggers the database routine and saves the current addon state to cache.
120 \param ID id of the addon
121 \param disable whether to enable or disable. Defaults to true (disable)
124 bool DisableAddon(const std::string& ID, bool disable = true);
126 /* \brief Check whether an addon has been disabled via DisableAddon.
127 In case the disabled cache does not know about the current state the database routine will be used.
128 \param ID id of the addon
131 bool IsAddonDisabled(const std::string& ID);
134 CStdString GetExtValue(cp_cfg_element_t *base, const char *path);
136 /*! \brief Retrieve a vector of repeated elements from a given configuration element
137 \param base the base configuration element.
138 \param path the path to the configuration element from the base element.
139 \param result [out] returned list of elements.
140 \return true if the configuration element is present and the list of elements is non-empty
142 bool GetExtElements(cp_cfg_element_t *base, const char *path, ELEMENTS &result);
144 /*! \brief Retrieve a list of strings from a given configuration element
145 Assumes the configuration element or attribute contains a whitespace separated list of values (eg xs:list schema).
146 \param base the base configuration element.
147 \param path the path to the configuration element or attribute from the base element.
148 \param result [out] returned list of strings.
149 \return true if the configuration element is present and the list of strings is non-empty
151 bool GetExtList(cp_cfg_element_t *base, const char *path, std::vector<CStdString> &result) const;
153 const cp_extension_t *GetExtension(const cp_plugin_info_t *props, const char *extension) const;
155 /*! \brief Load the addon in the given path
156 This loads the addon using c-pluff which parses the addon descriptor file.
157 \param path folder that contains the addon.
158 \param addon [out] returned addon.
159 \return true if addon is set, false otherwise.
161 bool LoadAddonDescription(const CStdString &path, AddonPtr &addon);
163 /*! \brief Load the addon in the given in-memory xml
164 This loads the addon using c-pluff which parses the addon descriptor file.
165 \param root Root element of an XML document.
166 \param addon [out] returned addon.
167 \return true if addon is set, false otherwise.
169 bool LoadAddonDescriptionFromMemory(const TiXmlElement *root, AddonPtr &addon);
171 /*! \brief Parse a repository XML file for addons and load their descriptors
172 A repository XML is essentially a concatenated list of addon descriptors.
173 \param root Root element of an XML document.
174 \param addons [out] returned list of addons.
175 \return true if the repository XML file is parsed, false otherwise.
177 bool AddonsFromRepoXML(const TiXmlElement *root, VECADDONS &addons);
179 /*! \brief Start all services addons.
180 \return True is all addons are started, false otherwise
182 bool StartServices(const bool beforelogin);
183 /*! \brief Stop all services addons.
185 void StopServices(const bool onlylogin);
188 void LoadAddons(const CStdString &path,
189 std::map<CStdString, AddonPtr>& unresolved);
192 const cp_cfg_element_t *GetExtElement(cp_cfg_element_t *base, const char *path);
193 cp_context_t *m_cp_context;
194 DllLibCPluff *m_cpluff;
195 VECADDONS m_updateableAddons;
197 /*! \brief Fetch a (single) addon from a plugin descriptor.
198 Assumes that there is a single (non-trivial) extension point per addon.
199 \param info the plugin descriptor
200 \param type the extension point we want
201 \return an AddonPtr based on the descriptor. May be NULL if no suitable extension point is found.
203 AddonPtr GetAddonFromDescriptor(const cp_plugin_info_t *info,
204 const CStdString& type="");
206 /*! \brief Check whether this addon is supported on the current platform
207 \param info the plugin descriptor
208 \return true if the addon is supported, false otherwise.
210 bool PlatformSupportsAddon(const cp_plugin_info_t *info) const;
212 AddonPtr Factory(const cp_extension_t *props);
213 bool CheckUserDirs(const cp_cfg_element_t *element);
215 // private construction, and no assignements; use the provided singleton methods
217 CAddonMgr(const CAddonMgr&);
218 CAddonMgr const& operator=(CAddonMgr const&);
219 virtual ~CAddonMgr();
221 std::map<std::string, bool> m_disabled;
222 static std::map<TYPE, IAddonMgrCallback*> m_managers;
223 CCriticalSection m_critSection;
224 CAddonDatabase m_database;
227 }; /* namespace ADDON */