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