Merge pull request #26264 from the-black-eagle/mka_end_durations
[xbmc.git] / xbmc / addons / AddonInstaller.cpp
blob25ff2991645d2f4be435cb211338502cec368c6f
1 /*
2 * Copyright (C) 2011-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "AddonInstaller.h"
11 #include "FileItem.h"
12 #include "FilesystemInstaller.h"
13 #include "GUIPassword.h"
14 #include "GUIUserMessages.h" // for callback
15 #include "ServiceBroker.h"
16 #include "URL.h"
17 #include "Util.h"
18 #include "addons/AddonManager.h"
19 #include "addons/AddonRepos.h"
20 #include "addons/Repository.h"
21 #include "addons/RepositoryUpdater.h"
22 #include "addons/addoninfo/AddonInfo.h"
23 #include "addons/addoninfo/AddonType.h"
24 #include "dialogs/GUIDialogExtendedProgressBar.h"
25 #include "events/AddonManagementEvent.h"
26 #include "events/EventLog.h"
27 #include "events/NotificationEvent.h"
28 #include "favourites/FavouritesService.h"
29 #include "filesystem/Directory.h"
30 #include "filesystem/File.h"
31 #include "guilib/GUIComponent.h"
32 #include "guilib/GUIWindowManager.h" // for callback
33 #include "guilib/LocalizeStrings.h"
34 #include "messaging/helpers/DialogHelper.h"
35 #include "messaging/helpers/DialogOKHelper.h"
36 #include "settings/AdvancedSettings.h"
37 #include "settings/Settings.h"
38 #include "settings/SettingsComponent.h"
39 #include "utils/FileOperationJob.h"
40 #include "utils/FileUtils.h"
41 #include "utils/JobManager.h"
42 #include "utils/StringUtils.h"
43 #include "utils/URIUtils.h"
44 #include "utils/Variant.h"
45 #include "utils/XTimeUtils.h"
46 #include "utils/log.h"
48 #include <functional>
49 #include <memory>
50 #include <mutex>
52 using namespace XFILE;
53 using namespace ADDON;
54 using namespace KODI::MESSAGING;
56 using namespace std::chrono_literals;
58 using KODI::MESSAGING::HELPERS::DialogResponse;
59 using KODI::UTILITY::TypedDigest;
61 namespace
63 class CAddonInstallJob : public CFileOperationJob
65 public:
66 CAddonInstallJob(const ADDON::AddonPtr& addon,
67 const ADDON::RepositoryPtr& repo,
68 AutoUpdateJob isAutoUpdate);
70 bool DoWork() override;
72 static constexpr const char* TYPE_DOWNLOAD = "DOWNLOAD";
73 static constexpr const char* TYPE_INSTALL = "INSTALL";
74 /*!
75 * \brief Returns the current processing type in the installation job
77 * \return The current processing type as string, can be \ref TYPE_DOWNLOAD or
78 * \ref TYPE_INSTALL
80 const char* GetType() const override { return m_currentType; }
82 /*! \brief Find the add-on and its repository for the given add-on ID
83 * \param addonID ID of the add-on to find
84 * \param[out] repo the repository to use
85 * \param[out] addon Add-on with the given add-on ID
86 * \return True if the add-on and its repository were found, false otherwise.
88 static bool GetAddon(const std::string& addonID,
89 ADDON::RepositoryPtr& repo,
90 ADDON::AddonPtr& addon);
92 void SetDependsInstall(DependencyJob dependsInstall) { m_dependsInstall = dependsInstall; }
93 void SetAllowCheckForUpdates(AllowCheckForUpdates allowCheckForUpdates)
95 m_allowCheckForUpdates = allowCheckForUpdates;
98 private:
99 void OnPreInstall();
100 void OnPostInstall();
101 bool Install(const std::string& installFrom,
102 const ADDON::RepositoryPtr& repo = ADDON::RepositoryPtr());
103 bool DownloadPackage(const std::string& path, const std::string& dest);
105 bool DoFileOperation(FileAction action,
106 CFileItemList& items,
107 const std::string& file,
108 bool useSameJob = true);
110 /*! \brief Queue a notification for addon installation/update failure
111 \param addonID - addon id
112 \param fileName - filename which is shown in case the addon id is unknown
113 \param message - error message to be displayed
115 void ReportInstallError(const std::string& addonID,
116 const std::string& fileName,
117 const std::string& message = "");
119 ADDON::AddonPtr m_addon;
120 ADDON::RepositoryPtr m_repo;
121 bool m_isUpdate;
122 AutoUpdateJob m_isAutoUpdate;
123 DependencyJob m_dependsInstall = DependencyJob::CHOICE_NO;
124 AllowCheckForUpdates m_allowCheckForUpdates = AllowCheckForUpdates::CHOICE_YES;
125 const char* m_currentType = TYPE_DOWNLOAD;
128 class CAddonUnInstallJob : public CFileOperationJob
130 public:
131 CAddonUnInstallJob(const ADDON::AddonPtr& addon, bool removeData);
133 bool DoWork() override;
134 void SetRecurseOrphaned(RecurseOrphaned recurseOrphaned) { m_recurseOrphaned = recurseOrphaned; };
136 private:
137 void ClearFavourites();
139 ADDON::AddonPtr m_addon;
140 bool m_removeData;
141 RecurseOrphaned m_recurseOrphaned = RecurseOrphaned::CHOICE_YES;
144 } // unnamed namespace
146 CAddonInstaller::CAddonInstaller() : m_idle(true)
149 CAddonInstaller::~CAddonInstaller() = default;
151 CAddonInstaller &CAddonInstaller::GetInstance()
153 static CAddonInstaller addonInstaller;
154 return addonInstaller;
157 void CAddonInstaller::OnJobComplete(unsigned int jobID, bool success, CJob* job)
159 std::unique_lock<CCriticalSection> lock(m_critSection);
160 JobMap::iterator i = find_if(m_downloadJobs.begin(), m_downloadJobs.end(), [jobID](const std::pair<std::string, CDownloadJob>& p) {
161 return p.second.jobID == jobID;
163 if (i != m_downloadJobs.end())
164 m_downloadJobs.erase(i);
165 if (m_downloadJobs.empty())
166 m_idle.Set();
167 lock.unlock();
168 PrunePackageCache();
170 CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE);
171 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
174 void CAddonInstaller::OnJobProgress(unsigned int jobID, unsigned int progress, unsigned int total, const CJob *job)
176 std::unique_lock<CCriticalSection> lock(m_critSection);
177 JobMap::iterator i = find_if(m_downloadJobs.begin(), m_downloadJobs.end(), [jobID](const std::pair<std::string, CDownloadJob>& p) {
178 return p.second.jobID == jobID;
180 if (i != m_downloadJobs.end())
182 // update job progress
183 i->second.progress = 100 / total * progress;
184 i->second.downloadFinshed = std::string(job->GetType()) == CAddonInstallJob::TYPE_INSTALL;
185 CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE_ITEM);
186 msg.SetStringParam(i->first);
187 lock.unlock();
188 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
192 bool CAddonInstaller::IsDownloading() const
194 std::unique_lock<CCriticalSection> lock(m_critSection);
195 return !m_downloadJobs.empty();
198 void CAddonInstaller::GetInstallList(VECADDONS &addons) const
200 std::unique_lock<CCriticalSection> lock(m_critSection);
201 std::vector<std::string> addonIDs;
202 for (JobMap::const_iterator i = m_downloadJobs.begin(); i != m_downloadJobs.end(); ++i)
204 if (i->second.jobID)
205 addonIDs.push_back(i->first);
207 lock.unlock();
209 auto& addonMgr = CServiceBroker::GetAddonMgr();
210 for (const auto& addonId : addonIDs)
212 AddonPtr addon;
213 if (addonMgr.FindInstallableById(addonId, addon))
214 addons.emplace_back(std::move(addon));
218 bool CAddonInstaller::GetProgress(const std::string& addonID, unsigned int& percent, bool& downloadFinshed) const
220 std::unique_lock<CCriticalSection> lock(m_critSection);
221 JobMap::const_iterator i = m_downloadJobs.find(addonID);
222 if (i != m_downloadJobs.end())
224 percent = i->second.progress;
225 downloadFinshed = i->second.downloadFinshed;
226 return true;
228 return false;
231 bool CAddonInstaller::Cancel(const std::string &addonID)
233 std::unique_lock<CCriticalSection> lock(m_critSection);
234 JobMap::iterator i = m_downloadJobs.find(addonID);
235 if (i != m_downloadJobs.end())
237 CServiceBroker::GetJobManager()->CancelJob(i->second.jobID);
238 m_downloadJobs.erase(i);
239 if (m_downloadJobs.empty())
240 m_idle.Set();
241 return true;
244 return false;
247 bool CAddonInstaller::InstallModal(const std::string& addonID,
248 ADDON::AddonPtr& addon,
249 InstallModalPrompt promptForInstall)
251 if (!g_passwordManager.CheckMenuLock(WINDOW_ADDON_BROWSER))
252 return false;
254 // we assume that addons that are enabled don't get to this routine (i.e. that GetAddon() has been called)
255 if (CServiceBroker::GetAddonMgr().GetAddon(addonID, addon, OnlyEnabled::CHOICE_NO))
256 return false; // addon is installed but disabled, and the user has specifically activated something that needs
257 // the addon - should we enable it?
259 // check we have it available
260 if (!CServiceBroker::GetAddonMgr().FindInstallableById(addonID, addon))
261 return false;
263 // if specified ask the user if he wants it installed
264 if (promptForInstall == InstallModalPrompt::CHOICE_YES)
266 if (HELPERS::ShowYesNoDialogLines(CVariant{24076}, CVariant{24100}, CVariant{addon->Name()},
267 CVariant{24101}) != DialogResponse::CHOICE_YES)
269 return false;
273 if (!InstallOrUpdate(addonID, BackgroundJob::CHOICE_NO, ModalJob::CHOICE_YES))
274 return false;
276 return CServiceBroker::GetAddonMgr().GetAddon(addonID, addon, OnlyEnabled::CHOICE_YES);
280 bool CAddonInstaller::InstallOrUpdate(const std::string& addonID,
281 BackgroundJob background,
282 ModalJob modal)
284 AddonPtr addon;
285 RepositoryPtr repo;
286 if (!CAddonInstallJob::GetAddon(addonID, repo, addon))
287 return false;
289 return DoInstall(addon, repo, background, modal, AutoUpdateJob::CHOICE_NO,
290 DependencyJob::CHOICE_NO, AllowCheckForUpdates::CHOICE_YES);
293 bool CAddonInstaller::InstallOrUpdateDependency(const ADDON::AddonPtr& dependsId,
294 const ADDON::RepositoryPtr& repo)
296 return DoInstall(dependsId, repo, BackgroundJob::CHOICE_NO, ModalJob::CHOICE_NO,
297 AutoUpdateJob::CHOICE_NO, DependencyJob::CHOICE_YES,
298 AllowCheckForUpdates::CHOICE_YES);
301 bool CAddonInstaller::RemoveDependency(const std::shared_ptr<IAddon>& dependsId) const
303 const bool removeData = CDirectory::Exists(dependsId->Profile());
304 CAddonUnInstallJob removeDependencyJob(dependsId, removeData);
305 removeDependencyJob.SetRecurseOrphaned(RecurseOrphaned::CHOICE_NO);
307 return removeDependencyJob.DoWork();
310 std::vector<std::string> CAddonInstaller::RemoveOrphanedDepsRecursively() const
312 std::vector<std::string> removedItems;
314 auto toRemove = CServiceBroker::GetAddonMgr().GetOrphanedDependencies();
315 while (toRemove.size() > 0)
317 for (const auto& dep : toRemove)
319 if (RemoveDependency(dep))
321 removedItems.emplace_back(dep->Name()); // successfully removed
323 else
325 CLog::Log(LOGERROR, "CAddonMgr::{}: failed to remove orphaned add-on/dependency: {}",
326 __func__, dep->Name());
330 toRemove = CServiceBroker::GetAddonMgr().GetOrphanedDependencies();
333 return removedItems;
336 bool CAddonInstaller::Install(const std::string& addonId,
337 const CAddonVersion& version,
338 const std::string& repoId)
340 CLog::Log(LOGDEBUG, "CAddonInstaller: installing '{}' version '{}' from repository '{}'", addonId,
341 version.asString(), repoId);
343 AddonPtr addon;
344 CAddonDatabase database;
346 if (!database.Open() || !database.GetAddon(addonId, version, repoId, addon))
347 return false;
349 AddonPtr repo;
350 if (!CServiceBroker::GetAddonMgr().GetAddon(repoId, repo, AddonType::REPOSITORY,
351 OnlyEnabled::CHOICE_YES))
352 return false;
354 return DoInstall(addon, std::static_pointer_cast<CRepository>(repo), BackgroundJob::CHOICE_YES,
355 ModalJob::CHOICE_NO, AutoUpdateJob::CHOICE_NO, DependencyJob::CHOICE_NO,
356 AllowCheckForUpdates::CHOICE_YES);
359 bool CAddonInstaller::DoInstall(const AddonPtr& addon,
360 const RepositoryPtr& repo,
361 BackgroundJob background,
362 ModalJob modal,
363 AutoUpdateJob autoUpdate,
364 DependencyJob dependsInstall,
365 AllowCheckForUpdates allowCheckForUpdates)
367 // check whether we already have the addon installing
368 std::unique_lock<CCriticalSection> lock(m_critSection);
369 if (m_downloadJobs.find(addon->ID()) != m_downloadJobs.end())
370 return false;
372 CAddonInstallJob* installJob = new CAddonInstallJob(addon, repo, autoUpdate);
373 if (background == BackgroundJob::CHOICE_YES)
375 // Workaround: because CAddonInstallJob is blocking waiting for other jobs, it needs to be run
376 // with priority dedicated.
377 unsigned int jobID =
378 CServiceBroker::GetJobManager()->AddJob(installJob, this, CJob::PRIORITY_DEDICATED);
379 m_downloadJobs.insert(make_pair(addon->ID(), CDownloadJob(jobID)));
380 m_idle.Reset();
382 return true;
385 m_downloadJobs.insert(make_pair(addon->ID(), CDownloadJob(0)));
386 m_idle.Reset();
387 lock.unlock();
389 installJob->SetDependsInstall(dependsInstall);
390 installJob->SetAllowCheckForUpdates(allowCheckForUpdates);
392 bool result = false;
393 if (modal == ModalJob::CHOICE_YES)
394 result = installJob->DoModal();
395 else
396 result = installJob->DoWork();
397 delete installJob;
399 lock.lock();
400 JobMap::iterator i = m_downloadJobs.find(addon->ID());
401 m_downloadJobs.erase(i);
402 if (m_downloadJobs.empty())
403 m_idle.Set();
405 return result;
408 bool CAddonInstaller::InstallFromZip(const std::string &path)
410 if (!g_passwordManager.CheckMenuLock(WINDOW_ADDON_BROWSER))
411 return false;
413 CLog::Log(LOGDEBUG, "CAddonInstaller: installing from zip '{}'", CURL::GetRedacted(path));
415 // grab the descriptive XML document from the zip, and read it in
416 CFileItemList items;
417 //! @bug some zip files return a single item (root folder) that we think is stored, so we don't use the zip:// protocol
418 CURL pathToUrl(path);
419 CURL zipDir = URIUtils::CreateArchivePath("zip", pathToUrl, "");
420 auto eventLog = CServiceBroker::GetEventLog();
421 if (!CDirectory::GetDirectory(zipDir, items, "", DIR_FLAG_DEFAULTS) ||
422 items.Size() != 1 || !items[0]->m_bIsFolder)
424 if (eventLog)
425 eventLog->AddWithNotification(EventPtr(
426 new CNotificationEvent(24045, StringUtils::Format(g_localizeStrings.Get(24143), path),
427 "special://xbmc/media/icon256x256.png", EventLevel::Error)));
429 CLog::Log(
430 LOGERROR,
431 "CAddonInstaller: installing addon failed '{}' - itemsize: {}, first item is folder: {}",
432 CURL::GetRedacted(path), items.Size(), items[0]->m_bIsFolder);
433 return false;
436 AddonPtr addon;
437 if (CServiceBroker::GetAddonMgr().LoadAddonDescription(items[0]->GetPath(), addon))
438 return DoInstall(addon, RepositoryPtr(), BackgroundJob::CHOICE_YES, ModalJob::CHOICE_NO,
439 AutoUpdateJob::CHOICE_NO, DependencyJob::CHOICE_NO,
440 AllowCheckForUpdates::CHOICE_YES);
442 if (eventLog)
443 eventLog->AddWithNotification(EventPtr(
444 new CNotificationEvent(24045, StringUtils::Format(g_localizeStrings.Get(24143), path),
445 "special://xbmc/media/icon256x256.png", EventLevel::Error)));
446 return false;
449 bool CAddonInstaller::UnInstall(const AddonPtr& addon, bool removeData)
451 CServiceBroker::GetJobManager()->AddJob(new CAddonUnInstallJob(addon, removeData), this);
452 return true;
455 bool CAddonInstaller::CheckDependencies(const AddonPtr& addon,
456 CAddonDatabase* database /* = nullptr */)
458 std::pair<std::string, std::string> failedDep;
459 return CheckDependencies(addon, failedDep, database);
462 bool CAddonInstaller::CheckDependencies(const AddonPtr& addon,
463 std::pair<std::string, std::string>& failedDep,
464 CAddonDatabase* database /* = nullptr */)
466 std::vector<std::string> preDeps;
467 preDeps.push_back(addon->ID());
468 CAddonDatabase localDB;
469 if (!database)
470 database = &localDB;
472 return CheckDependencies(addon, preDeps, *database, failedDep);
475 bool CAddonInstaller::CheckDependencies(const AddonPtr &addon,
476 std::vector<std::string>& preDeps, CAddonDatabase &database,
477 std::pair<std::string, std::string> &failedDep)
479 if (addon == nullptr)
480 return true; // a nullptr addon has no dependencies
482 for (const auto& it : addon->GetDependencies())
484 const std::string &addonID = it.id;
485 const CAddonVersion& versionMin = it.versionMin;
486 const CAddonVersion& version = it.version;
487 bool optional = it.optional;
488 AddonPtr dep;
489 const bool haveInstalledAddon =
490 CServiceBroker::GetAddonMgr().GetAddon(addonID, dep, OnlyEnabled::CHOICE_NO);
491 if ((haveInstalledAddon && !dep->MeetsVersion(versionMin, version)) ||
492 (!haveInstalledAddon && !optional))
494 // we have it but our version isn't good enough, or we don't have it and we need it
495 if (!CServiceBroker::GetAddonMgr().FindInstallableById(addonID, dep) ||
496 (dep && !dep->MeetsVersion(versionMin, version)))
498 // we don't have it in a repo, or we have it but the version isn't good enough, so dep isn't satisfied.
499 CLog::Log(LOGDEBUG, "CAddonInstallJob[{}]: requires {} version {} which is not available",
500 addon->ID(), addonID, version.asString());
502 // fill in the details of the failed dependency
503 failedDep.first = addonID;
504 failedDep.second = version.asString();
506 return false;
510 // need to enable the dependency
511 if (dep && CServiceBroker::GetAddonMgr().IsAddonDisabled(addonID) &&
512 !CServiceBroker::GetAddonMgr().EnableAddon(addonID))
514 return false;
517 // at this point we have our dep, or the dep is optional (and we don't have it) so check that it's OK as well
518 //! @todo should we assume that installed deps are OK?
519 if (dep && std::find(preDeps.begin(), preDeps.end(), dep->ID()) == preDeps.end())
521 preDeps.push_back(dep->ID());
522 if (!CheckDependencies(dep, preDeps, database, failedDep))
524 database.Close();
525 return false;
529 database.Close();
531 return true;
534 bool CAddonInstaller::HasJob(const std::string& ID) const
536 std::unique_lock<CCriticalSection> lock(m_critSection);
537 return m_downloadJobs.find(ID) != m_downloadJobs.end();
540 void CAddonInstaller::PrunePackageCache()
542 std::map<std::string, std::unique_ptr<CFileItemList>> packs;
543 int64_t size = EnumeratePackageFolder(packs);
544 int64_t limit = static_cast<int64_t>(CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_addonPackageFolderSize) * 1024 * 1024;
545 if (size < limit)
546 return;
548 // Prune packages
549 // 1. Remove the largest packages, leaving at least 2 for each add-on
550 CFileItemList items;
551 CAddonDatabase db;
552 db.Open();
553 for (auto it = packs.begin(); it != packs.end(); ++it)
555 it->second->Sort(SortByLabel, SortOrderDescending);
556 for (int j = 2; j < it->second->Size(); j++)
557 items.Add(std::make_shared<CFileItem>(*it->second->Get(j)));
560 items.Sort(SortBySize, SortOrderDescending);
561 int i = 0;
562 while (size > limit && i < items.Size())
564 size -= items[i]->m_dwSize;
565 db.RemovePackage(items[i]->GetPath());
566 CFileUtils::DeleteItem(items[i++]);
569 if (size > limit)
571 // 2. Remove the oldest packages (leaving least 1 for each add-on)
572 items.Clear();
573 for (auto it = packs.begin(); it != packs.end(); ++it)
575 if (it->second->Size() > 1)
576 items.Add(std::make_shared<CFileItem>(*it->second->Get(1)));
579 items.Sort(SortByDate, SortOrderAscending);
580 i = 0;
581 while (size > limit && i < items.Size())
583 size -= items[i]->m_dwSize;
584 db.RemovePackage(items[i]->GetPath());
585 CFileUtils::DeleteItem(items[i++]);
590 void CAddonInstaller::InstallAddons(const VECADDONS& addons,
591 bool wait,
592 AllowCheckForUpdates allowCheckForUpdates)
594 for (const auto& addon : addons)
596 AddonPtr toInstall;
597 RepositoryPtr repo;
598 if (CAddonInstallJob::GetAddon(addon->ID(), repo, toInstall))
599 DoInstall(toInstall, repo, BackgroundJob::CHOICE_NO, ModalJob::CHOICE_NO,
600 AutoUpdateJob::CHOICE_YES, DependencyJob::CHOICE_NO, allowCheckForUpdates);
602 if (wait)
604 std::unique_lock<CCriticalSection> lock(m_critSection);
605 if (!m_downloadJobs.empty())
607 m_idle.Reset();
608 lock.unlock();
609 m_idle.Wait();
614 int64_t CAddonInstaller::EnumeratePackageFolder(
615 std::map<std::string, std::unique_ptr<CFileItemList>>& result)
617 CFileItemList items;
618 CDirectory::GetDirectory("special://home/addons/packages/",items,".zip",DIR_FLAG_NO_FILE_DIRS);
619 int64_t size = 0;
620 for (int i = 0; i < items.Size(); i++)
622 if (items[i]->m_bIsFolder)
623 continue;
625 size += items[i]->m_dwSize;
626 std::string pack,dummy;
627 CAddonVersion::SplitFileName(pack, dummy, items[i]->GetLabel());
628 result.try_emplace(pack, std::make_unique<CFileItemList>());
629 result[pack]->Add(std::make_shared<CFileItem>(*items[i]));
632 return size;
635 CAddonInstallJob::CAddonInstallJob(const AddonPtr& addon,
636 const RepositoryPtr& repo,
637 AutoUpdateJob isAutoUpdate)
638 : m_addon(addon), m_repo(repo), m_isAutoUpdate(isAutoUpdate)
640 AddonPtr dummy;
641 m_isUpdate = CServiceBroker::GetAddonMgr().GetAddon(addon->ID(), dummy, OnlyEnabled::CHOICE_NO);
644 bool CAddonInstallJob::GetAddon(const std::string& addonID, RepositoryPtr& repo,
645 ADDON::AddonPtr& addon)
647 if (!CServiceBroker::GetAddonMgr().FindInstallableById(addonID, addon))
648 return false;
650 AddonPtr tmp;
651 if (!CServiceBroker::GetAddonMgr().GetAddon(addon->Origin(), tmp, AddonType::REPOSITORY,
652 OnlyEnabled::CHOICE_YES))
653 return false;
655 repo = std::static_pointer_cast<CRepository>(tmp);
657 return true;
660 bool CAddonInstallJob::DoWork()
662 m_currentType = CAddonInstallJob::TYPE_DOWNLOAD;
664 SetTitle(StringUtils::Format(g_localizeStrings.Get(24057), m_addon->Name()));
665 SetProgress(0);
667 // check whether all the dependencies are available or not
668 SetText(g_localizeStrings.Get(24058));
669 std::pair<std::string, std::string> failedDep;
670 if (!CAddonInstaller::GetInstance().CheckDependencies(m_addon, failedDep))
672 std::string details =
673 StringUtils::Format(g_localizeStrings.Get(24142), failedDep.first, failedDep.second);
674 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: {}", m_addon->ID(), details);
675 ReportInstallError(m_addon->ID(), m_addon->ID(), details);
676 return false;
679 std::string installFrom;
681 // Addons are installed by downloading the .zip package on the server to the local
682 // packages folder, then extracting from the local .zip package into the addons folder
683 // Both these functions are achieved by "copying" using the vfs.
685 if (!m_repo && URIUtils::HasSlashAtEnd(m_addon->Path()))
686 { // passed in a folder - all we need do is copy it across
687 installFrom = m_addon->Path();
689 else
691 std::string path{m_addon->Path()};
692 TypedDigest hash;
693 if (m_repo)
695 CRepository::ResolveResult resolvedAddon = m_repo->ResolvePathAndHash(m_addon);
696 path = resolvedAddon.location;
697 hash = resolvedAddon.digest;
698 if (path.empty())
700 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: failed to resolve addon install source path",
701 m_addon->ID());
702 ReportInstallError(m_addon->ID(), m_addon->ID());
703 return false;
707 CAddonDatabase db;
708 if (!db.Open())
710 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: failed to open database", m_addon->ID());
711 ReportInstallError(m_addon->ID(), m_addon->ID());
712 return false;
715 std::string packageOriginalPath, packageFileName;
716 URIUtils::Split(path, packageOriginalPath, packageFileName);
717 // Use ChangeBasePath so the URL is decoded if necessary
718 const std::string packagePath = "special://home/addons/packages/";
719 //!@todo fix design flaw in file copying: We use CFileOperationJob to download the package from the internet
720 // to the local cache. It tries to be "smart" and decode the URL. But it never tells us what the result is,
721 // so if we try for example to download "http://localhost/a+b.zip" the result ends up in "a b.zip".
722 // First bug is that it actually decodes "+", which is not necessary except in query parts. Second bug
723 // is that we cannot know that it does this and what the result is so the package will not be found without
724 // using ChangeBasePath here (which is the same function the copying code uses and performs the translation).
725 std::string package = URIUtils::ChangeBasePath(packageOriginalPath, packageFileName, packagePath);
727 // check that we don't already have a valid copy
728 if (!hash.Empty())
730 std::string hashExisting;
731 if (db.GetPackageHash(m_addon->ID(), package, hashExisting) && hash.value != hashExisting)
733 db.RemovePackage(package);
735 if (CFile::Exists(package))
737 CFile::Delete(package);
741 // zip passed in - download + extract
742 if (!CFile::Exists(package))
744 if (!DownloadPackage(path, packagePath))
746 CFile::Delete(package);
748 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: failed to download {}", m_addon->ID(),
749 package);
750 ReportInstallError(m_addon->ID(), URIUtils::GetFileName(package));
751 return false;
755 // at this point we have the package - check that it is valid
756 SetText(g_localizeStrings.Get(24077));
757 if (!hash.Empty())
759 TypedDigest actualHash{hash.type, CUtil::GetFileDigest(package, hash.type)};
760 if (hash != actualHash)
762 CFile::Delete(package);
764 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: Hash mismatch after download. Expected {}, was {}",
765 m_addon->ID(), hash.value, actualHash.value);
766 ReportInstallError(m_addon->ID(), URIUtils::GetFileName(package));
767 return false;
770 db.AddPackage(m_addon->ID(), package, hash.value);
773 // check if the archive is valid
774 CURL archive = URIUtils::CreateArchivePath("zip", CURL(package), "");
776 CFileItemList archivedFiles;
777 AddonPtr temp;
778 if (!CDirectory::GetDirectory(archive, archivedFiles, "", DIR_FLAG_DEFAULTS) ||
779 archivedFiles.Size() != 1 || !archivedFiles[0]->m_bIsFolder ||
780 !CServiceBroker::GetAddonMgr().LoadAddonDescription(archivedFiles[0]->GetPath(), temp))
782 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: invalid package {}", m_addon->ID(), package);
783 db.RemovePackage(package);
784 CFile::Delete(package);
785 ReportInstallError(m_addon->ID(), URIUtils::GetFileName(package));
786 return false;
789 installFrom = package;
793 m_currentType = CAddonInstallJob::TYPE_INSTALL;
795 // run any pre-install functions
796 ADDON::OnPreInstall(m_addon);
798 if (!CServiceBroker::GetAddonMgr().UnloadAddon(m_addon->ID()))
800 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: failed to unload addon.", m_addon->ID());
801 return false;
804 // perform install
805 if (!Install(installFrom, m_repo))
806 return false;
808 // Load new installed and if successed replace defined m_addon here with new one
809 if (!CServiceBroker::GetAddonMgr().LoadAddon(m_addon->ID(), m_addon->Origin(),
810 m_addon->Version()) ||
811 !CServiceBroker::GetAddonMgr().GetAddon(m_addon->ID(), m_addon, OnlyEnabled::CHOICE_YES))
813 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: failed to reload addon", m_addon->ID());
814 return false;
817 g_localizeStrings.LoadAddonStrings(URIUtils::AddFileToFolder(m_addon->Path(), "resources/language/"),
818 CServiceBroker::GetSettingsComponent()->GetSettings()->GetString(CSettings::SETTING_LOCALE_LANGUAGE), m_addon->ID());
820 ADDON::OnPostInstall(m_addon, m_isUpdate, IsModal());
822 // Write origin to database via addon manager, where this information is up-to-date.
823 // Needed to set origin correctly for new installed addons.
825 std::string origin;
826 if (m_addon->Origin() == ORIGIN_SYSTEM)
828 origin = ORIGIN_SYSTEM; // keep system add-on origin as ORIGIN_SYSTEM
830 else if (m_addon->HasMainType(AddonType::REPOSITORY))
832 origin = m_addon->ID(); // use own id as origin if repository
834 // if a repository is updated during the add-on migration process, we need to skip
835 // calling CheckForUpdates() on the repo to prevent deadlock issues during migration
837 if (m_allowCheckForUpdates == AllowCheckForUpdates::CHOICE_YES)
839 if (m_isUpdate)
841 CLog::Log(LOGDEBUG, "ADDONS: repository [{}] updated. now checking for content updates.",
842 m_addon->ID());
843 CServiceBroker::GetRepositoryUpdater().CheckForUpdates(
844 std::static_pointer_cast<CRepository>(m_addon), false);
847 else
849 CLog::Log(LOGDEBUG, "ADDONS: skipping CheckForUpdates() on repository [{}].", m_addon->ID());
852 else if (m_repo)
854 origin = m_repo->ID(); // use repo id as origin
857 CServiceBroker::GetAddonMgr().SetAddonOrigin(m_addon->ID(), origin, m_isUpdate);
859 if (m_dependsInstall == DependencyJob::CHOICE_YES)
861 CLog::Log(LOGDEBUG, "ADDONS: dependency [{}] will not be version checked and unpinned",
862 m_addon->ID());
864 else
866 // we only do pinning/unpinning for non-system add-ons
867 if (m_addon->Origin() != ORIGIN_SYSTEM)
869 // get all compatible versions of an addon-id regardless of their origin
870 // from all installed repositories
871 std::vector<std::shared_ptr<IAddon>> compatibleVersions =
872 CServiceBroker::GetAddonMgr().GetCompatibleVersions(m_addon->ID());
874 if (!m_addon->Origin().empty())
876 // handle add-ons that originate from a repository
878 // find the latest version for the origin we installed from
879 CAddonVersion latestVersion;
880 for (const auto& compatibleVersion : compatibleVersions)
882 if (compatibleVersion->Origin() == m_addon->Origin() &&
883 compatibleVersion->Version() > latestVersion)
885 latestVersion = compatibleVersion->Version();
889 if (m_addon->Version() == latestVersion)
891 // unpin the installed addon if it's the latest of its origin
892 CServiceBroker::GetAddonMgr().RemoveUpdateRuleFromList(m_addon->ID(),
893 AddonUpdateRule::PIN_OLD_VERSION);
894 CLog::Log(LOGDEBUG, "ADDONS: unpinned Addon: [{}] Origin: [{}] Version: [{}]",
895 m_addon->ID(), m_addon->Origin(), m_addon->Version().asString());
897 else
899 // pin if it is not the latest
900 CServiceBroker::GetAddonMgr().AddUpdateRuleToList(m_addon->ID(),
901 AddonUpdateRule::PIN_OLD_VERSION);
902 CLog::Log(LOGDEBUG, "ADDONS: pinned Addon: [{}] Origin: [{}] Version: [{}]",
903 m_addon->ID(), m_addon->Origin(), m_addon->Version().asString());
906 else
908 // handle manually installed add-ons
910 // find the latest version of any origin/repository
911 CAddonVersion latestVersion;
912 for (const auto& compatibleVersion : compatibleVersions)
914 if (compatibleVersion->Version() > latestVersion)
916 latestVersion = compatibleVersion->Version();
920 if (m_addon->Version() < latestVersion)
922 // pin zip version if it's lesser than latest from repo(s)
923 CServiceBroker::GetAddonMgr().AddUpdateRuleToList(m_addon->ID(),
924 AddonUpdateRule::PIN_ZIP_INSTALL);
925 CLog::Log(LOGDEBUG, "ADDONS: pinned zip installed Addon: [{}] Version: [{}]",
926 m_addon->ID(), m_addon->Version().asString());
928 else
930 // unpin zip version if it's >= the latest from repos
931 CServiceBroker::GetAddonMgr().RemoveUpdateRuleFromList(m_addon->ID(),
932 AddonUpdateRule::PIN_ZIP_INSTALL);
933 CLog::Log(LOGDEBUG, "ADDONS: unpinned zip installed Addon: [{}] Version: [{}]",
934 m_addon->ID(), m_addon->Version().asString());
940 bool notify = (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
941 CSettings::SETTING_ADDONS_NOTIFICATIONS) ||
942 m_isAutoUpdate == AutoUpdateJob::CHOICE_NO) &&
943 !IsModal() && m_dependsInstall == DependencyJob::CHOICE_NO;
944 auto eventLog = CServiceBroker::GetEventLog();
945 if (eventLog)
946 eventLog->Add(EventPtr(new CAddonManagementEvent(m_addon, m_isUpdate ? 24065 : 24084)), notify,
947 false);
949 if (m_isAutoUpdate == AutoUpdateJob::CHOICE_YES &&
950 m_addon->LifecycleState() == AddonLifecycleState::BROKEN)
952 CLog::Log(LOGDEBUG, "CAddonInstallJob[{}]: auto-disabling due to being marked as broken",
953 m_addon->ID());
954 CServiceBroker::GetAddonMgr().DisableAddon(m_addon->ID(), AddonDisabledReason::USER);
955 if (eventLog)
956 eventLog->Add(EventPtr(new CAddonManagementEvent(m_addon, 24094)), true, false);
958 else if (m_addon->LifecycleState() == AddonLifecycleState::DEPRECATED)
960 CLog::Log(LOGDEBUG, "CAddonInstallJob[{}]: installed addon marked as deprecated",
961 m_addon->ID());
962 std::string text =
963 StringUtils::Format(g_localizeStrings.Get(24168), m_addon->LifecycleStateDescription());
964 if (eventLog)
965 eventLog->Add(EventPtr(new CAddonManagementEvent(m_addon, text)), true, false);
968 // and we're done!
969 MarkFinished();
970 return true;
973 bool CAddonInstallJob::DownloadPackage(const std::string &path, const std::string &dest)
975 if (ShouldCancel(0, 1))
976 return false;
978 SetText(g_localizeStrings.Get(24078));
980 // need to download/copy the package first
981 CFileItemList list;
982 list.Add(std::make_shared<CFileItem>(path, false));
983 list[0]->Select(true);
985 return DoFileOperation(CFileOperationJob::ActionReplace, list, dest, true);
988 bool CAddonInstallJob::DoFileOperation(FileAction action, CFileItemList &items, const std::string &file, bool useSameJob /* = true */)
990 bool result = false;
991 if (useSameJob)
993 SetFileOperation(action, items, file);
995 // temporarily disable auto-closing so not to close the current progress indicator
996 bool autoClose = GetAutoClose();
997 if (autoClose)
998 SetAutoClose(false);
999 // temporarily disable updating title or text
1000 bool updateInformation = GetUpdateInformation();
1001 if (updateInformation)
1002 SetUpdateInformation(false);
1004 result = CFileOperationJob::DoWork();
1006 SetUpdateInformation(updateInformation);
1007 SetAutoClose(autoClose);
1009 else
1011 CFileOperationJob job(action, items, file);
1013 // pass our progress indicators to the temporary job and only allow it to
1014 // show progress updates (no title or text changes)
1015 job.SetProgressIndicators(GetProgressBar(), GetProgressDialog(), GetUpdateProgress(), false);
1017 result = job.DoWork();
1020 return result;
1023 bool CAddonInstallJob::Install(const std::string &installFrom, const RepositoryPtr& repo)
1025 const auto& deps = m_addon->GetDependencies();
1027 if (!deps.empty() && m_addon->HasType(AddonType::REPOSITORY))
1029 bool notSystemAddon = std::none_of(deps.begin(), deps.end(), [](const DependencyInfo& dep) {
1030 return CServiceBroker::GetAddonMgr().IsSystemAddon(dep.id);
1033 if (notSystemAddon)
1035 CLog::Log(
1036 LOGERROR,
1037 "CAddonInstallJob::{}: failed to install repository [{}]. It has dependencies defined",
1038 __func__, m_addon->ID());
1039 ReportInstallError(m_addon->ID(), m_addon->ID(), g_localizeStrings.Get(24088));
1040 return false;
1044 SetText(g_localizeStrings.Get(24079));
1045 unsigned int totalSteps = static_cast<unsigned int>(deps.size()) + 1;
1046 if (ShouldCancel(0, totalSteps))
1047 return false;
1049 CAddonRepos addonRepos;
1050 if (!addonRepos.IsValid())
1051 return false;
1053 // The first thing we do is install dependencies
1054 for (auto it = deps.begin(); it != deps.end(); ++it)
1056 if (it->id != "xbmc.metadata")
1058 const std::string &addonID = it->id;
1059 const CAddonVersion& versionMin = it->versionMin;
1060 const CAddonVersion& version = it->version;
1061 bool optional = it->optional;
1062 AddonPtr dependency;
1063 const bool haveInstalledAddon =
1064 CServiceBroker::GetAddonMgr().GetAddon(addonID, dependency, OnlyEnabled::CHOICE_NO);
1065 if ((haveInstalledAddon && !dependency->MeetsVersion(versionMin, version)) ||
1066 (!haveInstalledAddon && !optional))
1068 // we have it but our version isn't good enough, or we don't have it and we need it
1070 // dependency is already queued up for install - ::Install will fail
1071 // instead we wait until the Job has finished. note that we
1072 // recall install on purpose in case prior installation failed
1073 if (CAddonInstaller::GetInstance().HasJob(addonID))
1075 while (CAddonInstaller::GetInstance().HasJob(addonID))
1076 KODI::TIME::Sleep(50ms);
1078 if (!CServiceBroker::GetAddonMgr().IsAddonInstalled(addonID))
1080 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: failed to install dependency {}",
1081 m_addon->ID(), addonID);
1082 ReportInstallError(m_addon->ID(), m_addon->ID(), g_localizeStrings.Get(24085));
1083 return false;
1086 // don't have the addon or the addon isn't new enough - grab it (no new job for these)
1087 else
1089 RepositoryPtr repoForDep;
1090 AddonPtr dependencyToInstall;
1092 // origin of m_addon is empty at least if an addon is installed for the first time
1093 // we need to override "parentRepoId" if the passed in repo is valid.
1095 const std::string& parentRepoId =
1096 m_addon->Origin().empty() && repo ? repo->ID() : m_addon->Origin();
1098 if (!addonRepos.FindDependency(addonID, parentRepoId, dependencyToInstall, repoForDep))
1100 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: failed to find dependency {}", m_addon->ID(),
1101 addonID);
1102 ReportInstallError(m_addon->ID(), m_addon->ID(), g_localizeStrings.Get(24085));
1103 return false;
1105 else
1107 if (!dependencyToInstall->MeetsVersion(versionMin, version))
1109 CLog::Log(LOGERROR,
1110 "CAddonInstallJob[{}]: found dependency [{}/{}] doesn't meet minimum "
1111 "version [{}]",
1112 m_addon->ID(), addonID, dependencyToInstall->Version().asString(),
1113 versionMin.asString());
1114 ReportInstallError(m_addon->ID(), m_addon->ID(), g_localizeStrings.Get(24085));
1115 return false;
1119 if (IsModal())
1121 CAddonInstallJob dependencyJob(dependencyToInstall, repoForDep,
1122 AutoUpdateJob::CHOICE_NO);
1123 dependencyJob.SetDependsInstall(DependencyJob::CHOICE_YES);
1125 // pass our progress indicators to the temporary job and don't allow it to
1126 // show progress or information updates (no progress, title or text changes)
1127 dependencyJob.SetProgressIndicators(GetProgressBar(), GetProgressDialog(), false,
1128 false);
1130 if (!dependencyJob.DoModal())
1132 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: failed to install dependency {}",
1133 m_addon->ID(), addonID);
1134 ReportInstallError(m_addon->ID(), m_addon->ID(), g_localizeStrings.Get(24085));
1135 return false;
1138 else if (!CAddonInstaller::GetInstance().InstallOrUpdateDependency(dependencyToInstall,
1139 repoForDep))
1141 CLog::Log(LOGERROR, "CAddonInstallJob[{}]: failed to install dependency {}",
1142 m_addon->ID(), dependencyToInstall->ID());
1143 ReportInstallError(m_addon->ID(), m_addon->ID(), g_localizeStrings.Get(24085));
1144 return false;
1150 if (ShouldCancel(std::distance(deps.begin(), it), totalSteps))
1151 return false;
1154 SetText(g_localizeStrings.Get(24086));
1155 SetProgress(static_cast<unsigned int>(100.0 * (totalSteps - 1.0) / totalSteps));
1157 CFilesystemInstaller fsInstaller;
1158 if (!fsInstaller.InstallToFilesystem(installFrom, m_addon->ID()))
1160 ReportInstallError(m_addon->ID(), m_addon->ID());
1161 return false;
1164 SetProgress(100);
1166 return true;
1169 void CAddonInstallJob::ReportInstallError(const std::string& addonID, const std::string& fileName, const std::string& message /* = "" */)
1171 AddonPtr addon;
1172 CServiceBroker::GetAddonMgr().FindInstallableById(addonID, addon);
1174 MarkFinished();
1176 std::string msg = message;
1177 EventPtr activity;
1178 if (addon != nullptr)
1180 AddonPtr addon2;
1181 bool success = CServiceBroker::GetAddonMgr().GetAddon(addonID, addon2, OnlyEnabled::CHOICE_YES);
1182 if (msg.empty())
1184 msg = g_localizeStrings.Get(addon2 != nullptr && success ? 113 : 114);
1187 activity = EventPtr(new CAddonManagementEvent(addon, EventLevel::Error, msg));
1188 if (IsModal())
1189 HELPERS::ShowOKDialogText(CVariant{m_addon->Name()}, CVariant{msg});
1191 else
1193 activity = EventPtr(new CNotificationEvent(
1194 24045, !msg.empty() ? msg : StringUtils::Format(g_localizeStrings.Get(24143), fileName),
1195 EventLevel::Error));
1197 if (IsModal())
1198 HELPERS::ShowOKDialogText(CVariant{fileName}, CVariant{msg});
1201 auto eventLog = CServiceBroker::GetEventLog();
1202 if (eventLog)
1203 eventLog->Add(activity, !IsModal(), false);
1206 CAddonUnInstallJob::CAddonUnInstallJob(const AddonPtr &addon, bool removeData)
1207 : m_addon(addon), m_removeData(removeData)
1210 bool CAddonUnInstallJob::DoWork()
1212 ADDON::OnPreUnInstall(m_addon);
1214 //Unregister addon with the manager to ensure nothing tries
1215 //to interact with it while we are uninstalling.
1216 if (!CServiceBroker::GetAddonMgr().UnloadAddon(m_addon->ID()))
1218 CLog::Log(LOGERROR, "CAddonUnInstallJob[{}]: failed to unload addon.", m_addon->ID());
1219 return false;
1222 CFilesystemInstaller fsInstaller;
1223 if (!fsInstaller.UnInstallFromFilesystem(m_addon->Path()))
1225 CLog::Log(LOGERROR, "CAddonUnInstallJob[{}]: could not delete addon data.", m_addon->ID());
1226 return false;
1229 ClearFavourites();
1230 if (m_removeData)
1232 CFileUtils::DeleteItem(m_addon->Profile());
1235 AddonPtr addon;
1237 // try to get the addon object from the repository as the local one does not exist anymore
1238 // if that doesn't work fall back to the local one
1239 if (!CServiceBroker::GetAddonMgr().FindInstallableById(m_addon->ID(), addon) || addon == nullptr)
1241 addon = m_addon;
1244 auto eventLog = CServiceBroker::GetEventLog();
1245 if (eventLog)
1246 eventLog->Add(EventPtr(new CAddonManagementEvent(addon, 24144))); // Add-on uninstalled
1248 CServiceBroker::GetAddonMgr().OnPostUnInstall(m_addon->ID());
1250 CAddonDatabase database;
1251 if (database.Open())
1252 database.OnPostUnInstall(m_addon->ID());
1254 ADDON::OnPostUnInstall(m_addon);
1256 if (m_recurseOrphaned == RecurseOrphaned::CHOICE_YES)
1258 const auto removedItems = CAddonInstaller::GetInstance().RemoveOrphanedDepsRecursively();
1260 if (removedItems.size() > 0)
1262 CLog::Log(LOGINFO, "CAddonUnInstallJob[{}]: removed orphaned dependencies ({})",
1263 m_addon->ID(), StringUtils::Join(removedItems, ", "));
1267 return true;
1270 void CAddonUnInstallJob::ClearFavourites()
1272 bool bSave = false;
1273 CFileItemList items;
1274 CServiceBroker::GetFavouritesService().GetAll(items);
1275 for (int i = 0; i < items.Size(); i++)
1277 if (items[i]->GetPath().find(m_addon->ID()) != std::string::npos)
1279 items.Remove(items[i].get());
1280 bSave = true;
1284 if (bSave)
1285 CServiceBroker::GetFavouritesService().Save(items);