Merge pull request #3819 from arnova/subtitles_for_stacks
[vuplus_xbmc] / xbmc / addons / AddonInstaller.h
1 #pragma once
2 /*
3  *      Copyright (C) 2011-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 "utils/FileOperationJob.h"
23 #include "addons/Addon.h"
24 #include "utils/Stopwatch.h"
25 #include "threads/Event.h"
26
27 class CAddonDatabase;
28
29 class CAddonInstaller : public IJobCallback
30 {
31 public:
32   static CAddonInstaller &Get();
33
34   bool IsDownloading() const;
35   void GetInstallList(ADDON::VECADDONS &addons) const;
36   bool GetProgress(const CStdString &addonID, unsigned int &percent) const;
37   bool Cancel(const CStdString &addonID);
38
39   /*! \brief Prompt the user as to whether they wish to install an addon.
40    \param addonID the addon ID of the item to install.
41    \param addon [out] the installed addon for later use.
42    \return true on successful install, false otherwise.
43    \sa Install
44    */
45   bool PromptForInstall(const CStdString &addonID, ADDON::AddonPtr &addon);
46
47   /*! \brief Install an addon if it is available in a repository
48    \param addonID the addon ID of the item to install
49    \param force whether to force the install even if the addon is already installed (eg for updating). Defaults to false.
50    \param referer string to use for referer for http fetch. Set to previous version when updating, parent when fetching a dependency
51    \param background whether to install in the background or not. Defaults to true.
52    \return true on successful install, false on failure.
53    \sa DoInstall
54    */
55   bool Install(const CStdString &addonID, bool force = false, const CStdString &referer="", bool background = true);
56
57   /*! \brief Install an addon from the given zip path
58    \param path the zip file to install from
59    \return true if successful, false otherwise
60    \sa DoInstall
61    */
62   bool InstallFromZip(const CStdString &path);
63
64   /*! \brief Install a set of addons from the official repository (if needed)
65    \param addonIDs a set of addon IDs to install
66    */
67   void InstallFromXBMCRepo(const std::set<CStdString> &addonIDs);
68
69   /*! \brief Check whether dependencies of an addon exist or are installable.
70    Iterates through the addon's dependencies, checking they're installed or installable.
71    Each dependency must also satisfies CheckDependencies in turn.
72    \param addon the addon to check
73    \param database the database instance to update. Defaults to NULL.
74    \return true if dependencies are available, false otherwise.
75    */
76   bool CheckDependencies(const ADDON::AddonPtr &addon, CAddonDatabase *database = NULL);
77
78   /*! \brief Update all repositories (if needed)
79    Runs through all available repositories and queues an update of them if they
80    need it (according to the set timeouts) or if forced.  Optionally busy wait
81    until the repository updates are complete.
82    \param force whether we should run an update regardless of the normal update cycle. Defaults to false.
83    \param wait whether we should busy wait for the updates to be performed. Defaults to false.
84    */
85
86   /*! \brief Check if an installation job for a given add-on is already queued up
87    *  \param ID The ID of the add-on
88    *  \return true if a job exists, false otherwise
89    */
90   bool HasJob(const CStdString& ID) const;
91
92   void UpdateRepos(bool force = false, bool wait = false);
93
94   void OnJobComplete(unsigned int jobID, bool success, CJob* job);
95   void OnJobProgress(unsigned int jobID, unsigned int progress, unsigned int total, const CJob *job);
96
97   class CDownloadJob
98   {
99   public:
100     CDownloadJob(unsigned int id)
101     {
102       jobID = id;
103       progress = 0;
104     }
105     unsigned int jobID;
106     unsigned int progress;
107   };
108
109   typedef std::map<CStdString,CDownloadJob> JobMap;
110
111 private:
112   // private construction, and no assignements; use the provided singleton methods
113   CAddonInstaller();
114   CAddonInstaller(const CAddonInstaller&);
115   CAddonInstaller const& operator=(CAddonInstaller const&);
116   virtual ~CAddonInstaller();
117
118   /*! \brief Install an addon from a repository or zip
119    \param addon the AddonPtr describing the addon
120    \param hash the hash to verify the install. Defaults to "".
121    \param update whether this is an update of an existing addon, or a new install. Defaults to false.
122    \param referer string to use for referer for http fetch. Defaults to "".
123    \param background whether to install in the background or not. Defaults to true.
124    \return true on successful install, false on failure.
125    */
126   bool DoInstall(const ADDON::AddonPtr &addon, const CStdString &hash = "", bool update = false, const CStdString &referer = "", bool background = true);
127
128   /*! \brief Check whether dependencies of an addon exist or are installable.
129    Iterates through the addon's dependencies, checking they're installed or installable.
130    Each dependency must also satisfies CheckDependencies in turn.
131    \param addon the addon to check
132    \param preDeps previous dependencies encountered during recursion. aids in avoiding infinite recursion
133    \param database database instance to update
134    \return true if dependencies are available, false otherwise.
135    */
136   bool CheckDependencies(const ADDON::AddonPtr &addon,
137                          std::vector<std::string>& preDeps, CAddonDatabase &database);
138
139   void PrunePackageCache();
140   int64_t EnumeratePackageFolder(std::map<CStdString,CFileItemList*>& result);
141
142   CCriticalSection m_critSection;
143   JobMap m_downloadJobs;
144   CStopWatch m_repoUpdateWatch;   ///< repository updates are done based on this counter
145   unsigned int m_repoUpdateJob;   ///< the job ID of the repository updates
146   CEvent m_repoUpdateDone;        ///< event set when the repository updates are complete
147 };
148
149 class CAddonInstallJob : public CFileOperationJob
150 {
151 public:
152   CAddonInstallJob(const ADDON::AddonPtr &addon, const CStdString &hash = "", bool update = false, const CStdString &referer = "");
153
154   virtual bool DoWork();
155
156   /*! \brief return the id of the addon being installed
157    \return id of the installing addon
158    */
159   CStdString AddonID() const;
160
161   /*! \brief Delete an addon following install failure
162    \param addonFolder - the folder to delete
163    */
164   static bool DeleteAddon(const CStdString &addonFolder);
165
166   /*! \brief Find which repository hosts an add-on
167    *  \param addon The add-on to find the repository for
168    *  \return The hosting repository
169    */
170   static ADDON::AddonPtr GetRepoForAddon(const ADDON::AddonPtr& addon);
171 private:
172   bool OnPreInstall();
173   void OnPostInstall(bool reloadAddon);
174   bool Install(const CStdString &installFrom, const ADDON::AddonPtr& repo=ADDON::AddonPtr());
175   bool DownloadPackage(const CStdString &path, const CStdString &dest);
176
177   /*! \brief Queue a notification for addon installation/update failure
178    \param addonID - addon id
179    \param fileName - filename which is shown in case the addon id is unknown
180    */
181   void ReportInstallError(const CStdString& addonID, const CStdString& fileName);
182
183   ADDON::AddonPtr m_addon;
184   CStdString m_hash;
185   bool m_update;
186   CStdString m_referer;
187 };
188
189 class CAddonUnInstallJob : public CFileOperationJob
190 {
191 public:
192   CAddonUnInstallJob(const ADDON::AddonPtr &addon);
193
194   virtual bool DoWork();
195 private:
196   void OnPostUnInstall();
197
198   ADDON::AddonPtr m_addon;
199 };