3 * Copyright (C) 2011-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 "utils/FileOperationJob.h"
23 #include "addons/Addon.h"
24 #include "utils/Stopwatch.h"
25 #include "threads/Event.h"
27 class CAddonInstaller : public IJobCallback
30 static CAddonInstaller &Get();
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);
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.
43 bool PromptForInstall(const CStdString &addonID, ADDON::AddonPtr &addon);
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.
53 bool Install(const CStdString &addonID, bool force = false, const CStdString &referer="", bool background = true);
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
60 bool InstallFromZip(const CStdString &path);
62 /*! \brief Install a set of addons from the official repository (if needed)
63 \param addonIDs a set of addon IDs to install
65 void InstallFromXBMCRepo(const std::set<CStdString> &addonIDs);
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.
73 bool CheckDependencies(const ADDON::AddonPtr &addon);
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.
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
87 bool HasJob(const CStdString& ID) const;
89 void UpdateRepos(bool force = false, bool wait = false);
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);
97 CDownloadJob(unsigned int id)
103 unsigned int progress;
106 typedef std::map<CStdString,CDownloadJob> JobMap;
109 // private construction, and no assignements; use the provided singleton methods
111 CAddonInstaller(const CAddonInstaller&);
112 CAddonInstaller const& operator=(CAddonInstaller const&);
113 virtual ~CAddonInstaller();
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.
123 bool DoInstall(const ADDON::AddonPtr &addon, const CStdString &hash = "", bool update = false, const CStdString &referer = "", bool background = true);
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.
132 bool CheckDependencies(const ADDON::AddonPtr &addon,
133 std::vector<std::string>& preDeps);
135 void PrunePackageCache();
136 int64_t EnumeratePackageFolder(std::map<CStdString,CFileItemList*>& result);
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
145 class CAddonInstallJob : public CFileOperationJob
148 CAddonInstallJob(const ADDON::AddonPtr &addon, const CStdString &hash = "", bool update = false, const CStdString &referer = "");
150 virtual bool DoWork();
152 /*! \brief return the id of the addon being installed
153 \return id of the installing addon
155 CStdString AddonID() const;
157 /*! \brief Delete an addon following install failure
158 \param addonFolder - the folder to delete
160 static bool DeleteAddon(const CStdString &addonFolder);
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
166 static ADDON::AddonPtr GetRepoForAddon(const ADDON::AddonPtr& addon);
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);
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
177 void ReportInstallError(const CStdString& addonID, const CStdString& fileName);
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
183 bool CheckHash(const CStdString& addonZip);
185 ADDON::AddonPtr m_addon;
188 CStdString m_referer;
191 class CAddonUnInstallJob : public CFileOperationJob
194 CAddonUnInstallJob(const ADDON::AddonPtr &addon);
196 virtual bool DoWork();
198 void OnPostUnInstall();
200 ADDON::AddonPtr m_addon;