Merge pull request #5039 from CEikermann/patch-1
[vuplus_xbmc] / xbmc / addons / Addon.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
22 #include "IAddon.h"
23 #include "addons/AddonVersion.h"
24 #include "utils/XBMCTinyXML.h"
25 #include "guilib/LocalizeStrings.h"
26 #include "utils/ISerializable.h"
27
28 class TiXmlElement;
29 class CAddonCallbacksAddon;
30
31 typedef struct cp_plugin_info_t cp_plugin_info_t;
32 typedef struct cp_extension_t cp_extension_t;
33
34 namespace ADDON
35 {
36   typedef std::vector<AddonPtr> VECADDONS;
37   typedef std::vector<AddonPtr>::iterator IVECADDONS;
38
39 // utils
40 const CStdString    TranslateType(const TYPE &type, bool pretty=false);
41 const CStdString    GetIcon(const TYPE &type);
42       TYPE          TranslateType(const CStdString &string);
43 const CStdString    UpdateVideoScraper(const CStdString &scraper);
44 const CStdString    UpdateMusicScraper(const CStdString &scraper);
45
46 class AddonProps : public ISerializable
47 {
48 public:
49   AddonProps(const CStdString &id, TYPE type, const CStdString &versionstr, const CStdString &minversionstr)
50     : id(id)
51     , type(type)
52     , version(versionstr)
53     , minversion(minversionstr)
54     , stars(0)
55   {
56   }
57
58   AddonProps(const cp_extension_t *ext);
59   AddonProps(const cp_plugin_info_t *plugin);
60
61   bool operator==(const AddonProps &rhs)
62   { 
63     return    (*this).id == rhs.id
64            && (*this).type == rhs.type
65            && (*this).version == rhs.version;
66   }
67   
68   void Serialize(CVariant &variant) const;
69
70   CStdString id;
71   TYPE type;
72   AddonVersion version;
73   AddonVersion minversion;
74   CStdString name;
75   CStdString license;
76   CStdString summary;
77   CStdString description;
78   CStdString path;
79   CStdString libname;
80   CStdString author;
81   CStdString source;
82   CStdString icon;
83   CStdString disclaimer;
84   CStdString changelog;
85   CStdString fanart;
86   ADDONDEPS dependencies;
87   CStdString broken;
88   InfoMap    extrainfo;
89   int        stars;
90 private:
91   void BuildDependencies(const cp_plugin_info_t *plugin);
92 };
93
94 typedef std::vector<class AddonProps> VECADDONPROPS;
95
96 class CAddon : public IAddon
97 {
98 public:
99   CAddon(const AddonProps &addonprops);
100   CAddon(const cp_extension_t *ext);
101   CAddon(const cp_plugin_info_t *plugin);
102   virtual ~CAddon() {}
103   virtual AddonPtr Clone() const;
104
105   /*! \brief Check whether the this addon can be configured or not
106    \return true if the addon has settings, false otherwise
107    \sa LoadSettings, LoadUserSettings, SaveSettings, HasUserSettings, GetSetting, UpdateSetting
108    */
109   bool HasSettings();
110
111   /*! \brief Check whether the user has configured this addon or not
112    \return true if previously saved settings are found, false otherwise
113    \sa LoadSettings, LoadUserSettings, SaveSettings, HasSettings, GetSetting, UpdateSetting
114    */
115   bool HasUserSettings();
116
117   /*! \brief Save any user configured settings
118    \sa LoadSettings, LoadUserSettings, HasSettings, HasUserSettings, GetSetting, UpdateSetting
119    */
120   virtual void SaveSettings();
121
122   /*! \brief Update a user-configured setting with a new value
123    \param key the id of the setting to update
124    \param value the value that the setting should take
125    \sa LoadSettings, LoadUserSettings, SaveSettings, HasSettings, HasUserSettings, GetSetting
126    */
127   void UpdateSetting(const CStdString& key, const CStdString& value);
128
129   /*! \brief Retrieve a particular settings value
130    If a previously configured user setting is available, we return it's value, else we return the default (if available)
131    \param key the id of the setting to retrieve
132    \return the current value of the setting, or the default if the setting has yet to be configured.
133    \sa LoadSettings, LoadUserSettings, SaveSettings, HasSettings, HasUserSettings, UpdateSetting
134    */
135   virtual CStdString GetSetting(const CStdString& key);
136
137   TiXmlElement* GetSettingsXML();
138   virtual CStdString GetString(uint32_t id);
139
140   // properties
141   TYPE Type() const { return m_props.type; }
142   bool IsType(TYPE type) const { return type == m_props.type; }
143   AddonProps Props() const { return m_props; }
144   AddonProps& Props() { return m_props; }
145   const CStdString ID() const { return m_props.id; }
146   const CStdString Name() const { return m_props.name; }
147   bool Enabled() const { return m_enabled; }
148   virtual bool IsInUse() const { return false; };
149   const AddonVersion Version() const { return m_props.version; }
150   const AddonVersion MinVersion() const { return m_props.minversion; }
151   const CStdString Summary() const { return m_props.summary; }
152   const CStdString Description() const { return m_props.description; }
153   const CStdString Path() const { return m_props.path; }
154   const CStdString Profile() const { return m_profile; }
155   const CStdString LibPath() const;
156   const CStdString Author() const { return m_props.author; }
157   const CStdString ChangeLog() const { return m_props.changelog; }
158   const CStdString FanArt() const { return m_props.fanart; }
159   const CStdString Icon() const;
160   int Stars() const { return m_props.stars; }
161   const CStdString Disclaimer() const { return m_props.disclaimer; }
162   const InfoMap &ExtraInfo() const { return m_props.extrainfo; }
163   const ADDONDEPS &GetDeps() const { return m_props.dependencies; }
164
165   /*! \brief return whether or not this addon satisfies the given version requirements
166    \param version the version to meet.
167    \return true if  min_version <= version <= current_version, false otherwise.
168    */
169   bool MeetsVersion(const AddonVersion &version) const;
170   virtual bool ReloadSettings();
171
172   void MarkAsDisabled() { m_enabled = false; }
173
174 protected:
175   friend class CAddonCallbacksAddon;
176
177   CAddon(const CAddon &rhs); // protected as all copying is handled by Clone()
178   virtual void BuildLibName(const cp_extension_t *ext = NULL);
179
180   /*! \brief Load the default settings and override these with any previously configured user settings
181    \param bForce force the load of settings even if they are already loaded (reload)
182    \return true if settings exist, false otherwise
183    \sa LoadUserSettings, SaveSettings, HasSettings, HasUserSettings, GetSetting, UpdateSetting
184    */
185   virtual bool LoadSettings(bool bForce = false);
186
187   /*! \brief Load the user settings
188    \return true if user settings exist, false otherwise
189    \sa LoadSettings, SaveSettings, HasSettings, HasUserSettings, GetSetting, UpdateSetting
190    */
191   bool LoadUserSettings();
192
193   /*! \brief Parse settings from an XML document
194    \param doc XML document to parse for settings
195    \param loadDefaults if true, the default attribute is used and settings are reset prior to parsing, else the value attribute is used.
196    \return true if settings are loaded, false otherwise
197    \sa SettingsToXML
198    */
199   bool SettingsFromXML(const CXBMCTinyXML &doc, bool loadDefaults = false);
200
201   /*! \brief Parse settings into an XML document
202    \param doc XML document to receive the settings
203    \sa SettingsFromXML
204    */
205   void SettingsToXML(CXBMCTinyXML &doc) const;
206
207   CXBMCTinyXML      m_addonXmlDoc;
208   CStdString        m_strLibName;
209   bool              m_settingsLoaded;
210   bool              m_userSettingsLoaded;
211
212 private:
213   friend class CAddonMgr;
214   AddonProps m_props;
215   CStdString        m_userSettingsPath;
216   void BuildProfilePath();
217
218   virtual bool IsAddonLibrary() { return false; }
219
220   void Enable() { LoadStrings(); m_enabled = true; }
221   void Disable() { m_enabled = false; ClearStrings();}
222
223   virtual bool LoadStrings();
224   virtual void ClearStrings();
225   bool m_hasStrings;
226   bool m_checkedStrings;
227   bool m_hasSettings;
228
229   CStdString  m_profile;
230   bool        m_enabled;
231   CLocalizeStrings  m_strings;
232   std::map<CStdString, CStdString> m_settings;
233 };
234
235 class CAddonLibrary : public CAddon
236 {
237 public:
238   CAddonLibrary(const AddonProps &props);
239   CAddonLibrary(const cp_extension_t *ext);
240
241   virtual AddonPtr Clone() const;
242
243 private:
244   virtual bool IsAddonLibrary() { return true; }
245   TYPE SetAddonType();
246   const TYPE m_addonType; // addon type this library enhances
247 };
248
249 }; /* namespace ADDON */
250