[release] version bump to 13.0 beta1
[vuplus_xbmc] / xbmc / addons / AddonManager.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
5  *
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)
9  *  any later version.
10  *
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.
15  *
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/>.
19  *
20  */
21 #include "Addon.h"
22 #include "threads/CriticalSection.h"
23 #include "utils/StdString.h"
24 #include "utils/Observer.h"
25 #include <vector>
26 #include <map>
27 #include <deque>
28 #include "AddonDatabase.h"
29
30 class DllLibCPluff;
31 extern "C"
32 {
33 #include "lib/cpluff/libcpluff/cpluff.h"
34 }
35
36 namespace ADDON
37 {
38   typedef std::map<TYPE, VECADDONS> MAPADDONS;
39   typedef std::map<TYPE, VECADDONS>::iterator IMAPADDONS;
40   typedef std::vector<cp_cfg_element_t*> ELEMENTS;
41
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*)?";
50
51   /**
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
56   */
57   class IAddonMgrCallback
58   {
59     public:
60       virtual ~IAddonMgrCallback() {};
61       virtual bool RequestRestart(AddonPtr addon, bool datachanged)=0;
62       virtual bool RequestRemoval(AddonPtr addon)=0;
63   };
64
65   /**
66   * Class - CAddonMgr
67   * Holds references to all addons, enabled or
68   * otherwise. Services the generic callbacks available
69   * to all addon variants.
70   */
71   class CAddonMgr : public Observable
72   {
73   public:
74     static CAddonMgr &Get();
75     bool ReInit() { DeInit(); return Init(); }
76     bool Init();
77     void DeInit();
78
79     IAddonMgrCallback* GetCallbackForType(TYPE type);
80     bool RegisterAddonMgrCallback(TYPE type, IAddonMgrCallback* cb);
81     void UnregisterAddonMgrCallback(TYPE type);
82
83     /* Addon access */
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).
92      */
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
104      */
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
109      */
110     bool HasOutdatedAddons(bool enabled = true);
111     CStdString GetString(const CStdString &id, const int number);
112
113     const char *GetTranslatedString(const cp_cfg_element_t *root, const char *tag);
114     static AddonPtr AddonFromProps(AddonProps& props);
115     void FindAddons();
116     void RemoveAddon(const CStdString& ID);
117
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)
122      \sa IsAddonDisabled,
123      */
124     bool DisableAddon(const std::string& ID, bool disable = true);
125
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
129      \sa DisableAddon
130      */
131     bool IsAddonDisabled(const std::string& ID);
132
133     /* libcpluff */
134     CStdString GetExtValue(cp_cfg_element_t *base, const char *path);
135
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
141      */
142     bool GetExtElements(cp_cfg_element_t *base, const char *path, ELEMENTS &result);
143
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
150      */
151     bool GetExtList(cp_cfg_element_t *base, const char *path, std::vector<CStdString> &result) const;
152
153     const cp_extension_t *GetExtension(const cp_plugin_info_t *props, const char *extension) const;
154
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.
160      */
161     bool LoadAddonDescription(const CStdString &path, AddonPtr &addon);
162
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.
168      */
169     bool LoadAddonDescriptionFromMemory(const TiXmlElement *root, AddonPtr &addon);
170
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.
176      */
177     bool AddonsFromRepoXML(const TiXmlElement *root, VECADDONS &addons);
178
179     /*! \brief Start all services addons.
180         \return True is all addons are started, false otherwise
181     */
182     bool StartServices(const bool beforelogin);
183     /*! \brief Stop all services addons.
184     */
185     void StopServices(const bool onlylogin);
186
187   private:
188     void LoadAddons(const CStdString &path, 
189                     std::map<CStdString, AddonPtr>& unresolved);
190
191     /* libcpluff */
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;
196
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.
202      */
203     AddonPtr GetAddonFromDescriptor(const cp_plugin_info_t *info,
204                                     const CStdString& type="");
205
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.
209      */
210     bool PlatformSupportsAddon(const cp_plugin_info_t *info) const;
211
212     AddonPtr Factory(const cp_extension_t *props);
213     bool CheckUserDirs(const cp_cfg_element_t *element);
214
215     // private construction, and no assignements; use the provided singleton methods
216     CAddonMgr();
217     CAddonMgr(const CAddonMgr&);
218     CAddonMgr const& operator=(CAddonMgr const&);
219     virtual ~CAddonMgr();
220
221     std::map<std::string, bool> m_disabled;
222     static std::map<TYPE, IAddonMgrCallback*> m_managers;
223     CCriticalSection m_critSection;
224     CAddonDatabase m_database;
225   };
226
227 }; /* namespace ADDON */