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"
29 class CAddonInstaller : public IJobCallback
32 static CAddonInstaller &Get();
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);
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.
45 bool PromptForInstall(const CStdString &addonID, ADDON::AddonPtr &addon);
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.
55 bool Install(const CStdString &addonID, bool force = false, const CStdString &referer="", bool background = true);
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
62 bool InstallFromZip(const CStdString &path);
64 /*! \brief Install a set of addons from the official repository (if needed)
65 \param addonIDs a set of addon IDs to install
67 void InstallFromXBMCRepo(const std::set<CStdString> &addonIDs);
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.
76 bool CheckDependencies(const ADDON::AddonPtr &addon, CAddonDatabase *database = NULL);
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.
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
90 bool HasJob(const CStdString& ID) const;
92 void UpdateRepos(bool force = false, bool wait = false);
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);
100 CDownloadJob(unsigned int id)
106 unsigned int progress;
109 typedef std::map<CStdString,CDownloadJob> JobMap;
112 // private construction, and no assignements; use the provided singleton methods
114 CAddonInstaller(const CAddonInstaller&);
115 CAddonInstaller const& operator=(CAddonInstaller const&);
116 virtual ~CAddonInstaller();
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.
126 bool DoInstall(const ADDON::AddonPtr &addon, const CStdString &hash = "", bool update = false, const CStdString &referer = "", bool background = true);
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.
136 bool CheckDependencies(const ADDON::AddonPtr &addon,
137 std::vector<std::string>& preDeps, CAddonDatabase &database);
139 void PrunePackageCache();
140 int64_t EnumeratePackageFolder(std::map<CStdString,CFileItemList*>& result);
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
149 class CAddonInstallJob : public CFileOperationJob
152 CAddonInstallJob(const ADDON::AddonPtr &addon, const CStdString &hash = "", bool update = false, const CStdString &referer = "");
154 virtual bool DoWork();
156 /*! \brief return the id of the addon being installed
157 \return id of the installing addon
159 CStdString AddonID() const;
161 /*! \brief Delete an addon following install failure
162 \param addonFolder - the folder to delete
164 static bool DeleteAddon(const CStdString &addonFolder);
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
170 static ADDON::AddonPtr GetRepoForAddon(const ADDON::AddonPtr& addon);
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);
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
181 void ReportInstallError(const CStdString& addonID, const CStdString& fileName);
183 ADDON::AddonPtr m_addon;
186 CStdString m_referer;
189 class CAddonUnInstallJob : public CFileOperationJob
192 CAddonUnInstallJob(const ADDON::AddonPtr &addon);
194 virtual bool DoWork();
196 void OnPostUnInstall();
198 ADDON::AddonPtr m_addon;