| 1 |
/* |
| 2 |
* Copyright (C) 2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "ControllerInstaller.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "addons/Addon.h" |
| 14 |
#include "addons/AddonInstaller.h" |
| 15 |
#include "addons/AddonManager.h" |
| 16 |
#include "addons/addoninfo/AddonType.h" |
| 17 |
#include "dialogs/GUIDialogProgress.h" |
| 18 |
#include "dialogs/GUIDialogSelect.h" |
| 19 |
#include "guilib/GUIComponent.h" |
| 20 |
#include "guilib/GUIWindowManager.h" |
| 21 |
#include "guilib/LocalizeStrings.h" |
| 22 |
#include "guilib/WindowIDs.h" |
| 23 |
#include "messaging/helpers/DialogOKHelper.h" |
| 24 |
#include "utils/StringUtils.h" |
| 25 |
#include "utils/log.h" |
| 26 |
|
| 27 |
using namespace KODI; |
| 28 |
using namespace GAME; |
| 29 |
|
| 30 |
CControllerInstaller::CControllerInstaller() : CThread("ControllerInstaller") |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
void CControllerInstaller::Process() |
| 35 |
{ |
| 36 |
CGUIComponent* gui = CServiceBroker::GetGUI(); |
| 37 |
if (gui == nullptr) |
| 38 |
return; |
| 39 |
|
| 40 |
CGUIWindowManager& windowManager = gui->GetWindowManager(); |
| 41 |
|
| 42 |
auto pSelectDialog = windowManager.GetWindow<CGUIDialogSelect>(WINDOW_DIALOG_SELECT); |
| 43 |
if (pSelectDialog == nullptr) |
| 44 |
return; |
| 45 |
|
| 46 |
auto pProgressDialog = windowManager.GetWindow<CGUIDialogProgress>(WINDOW_DIALOG_PROGRESS); |
| 47 |
if (pProgressDialog == nullptr) |
| 48 |
return; |
| 49 |
|
| 50 |
ADDON::VECADDONS installableAddons; |
| 51 |
CServiceBroker::GetAddonMgr().GetInstallableAddons(installableAddons, |
| 52 |
ADDON::AddonType::GAME_CONTROLLER); |
| 53 |
if (installableAddons.empty()) |
| 54 |
{ |
| 55 |
// "Controller profiles" |
| 56 |
// "All available controller profiles are installed." |
| 57 |
MESSAGING::HELPERS::ShowOKDialogText(CVariant{35050}, CVariant{35062}); |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
CLog::Log(LOGDEBUG, "Controller installer: Found {} controller add-ons", |
| 62 |
installableAddons.size()); |
| 63 |
|
| 64 |
CFileItemList items; |
| 65 |
for (const auto& addon : installableAddons) |
| 66 |
{ |
| 67 |
CFileItemPtr item(new CFileItem(addon->Name())); |
| 68 |
item->SetArt("icon", addon->Icon()); |
| 69 |
items.Add(std::move(item)); |
| 70 |
} |
| 71 |
|
| 72 |
pSelectDialog->Reset(); |
| 73 |
pSelectDialog->SetHeading(39020); // "The following additional add-ons will be installed" |
| 74 |
pSelectDialog->SetUseDetails(true); |
| 75 |
pSelectDialog->EnableButton(true, 186); // "OK"" |
| 76 |
for (const auto& it : items) |
| 77 |
pSelectDialog->Add(*it); |
| 78 |
pSelectDialog->Open(); |
| 79 |
|
| 80 |
if (!pSelectDialog->IsButtonPressed()) |
| 81 |
{ |
| 82 |
CLog::Log(LOGDEBUG, "Controller installer: User cancelled installation dialog"); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
CLog::Log(LOGDEBUG, "Controller installer: Installing {} controller add-ons", |
| 87 |
installableAddons.size()); |
| 88 |
|
| 89 |
pProgressDialog->SetHeading(CVariant{24086}); // "Installing add-on..." |
| 90 |
pProgressDialog->SetLine(0, CVariant{""}); |
| 91 |
pProgressDialog->SetLine(1, CVariant{""}); |
| 92 |
pProgressDialog->SetLine(2, CVariant{""}); |
| 93 |
|
| 94 |
pProgressDialog->Open(); |
| 95 |
|
| 96 |
unsigned int installedCount = 0; |
| 97 |
while (installedCount < installableAddons.size()) |
| 98 |
{ |
| 99 |
const auto& addon = installableAddons[installedCount]; |
| 100 |
|
| 101 |
// Set dialog text |
| 102 |
const std::string& progressTemplate = g_localizeStrings.Get(24057); // "Installing {0:s}..." |
| 103 |
const std::string progressText = StringUtils::Format(progressTemplate, addon->Name()); |
| 104 |
pProgressDialog->SetLine(0, CVariant{progressText}); |
| 105 |
|
| 106 |
// Set dialog percentage |
| 107 |
const unsigned int percentage = |
| 108 |
100 * (installedCount + 1) / static_cast<unsigned int>(installableAddons.size()); |
| 109 |
pProgressDialog->SetPercentage(percentage); |
| 110 |
|
| 111 |
if (!ADDON::CAddonInstaller::GetInstance().InstallOrUpdate( |
| 112 |
addon->ID(), ADDON::BackgroundJob::CHOICE_NO, ADDON::ModalJob::CHOICE_NO)) |
| 113 |
{ |
| 114 |
CLog::Log(LOGERROR, "Controller installer: Failed to install {}", addon->ID()); |
| 115 |
// "Error" |
| 116 |
// "Failed to install add-on." |
| 117 |
MESSAGING::HELPERS::ShowOKDialogText(257, 35256); |
| 118 |
break; |
| 119 |
} |
| 120 |
|
| 121 |
if (pProgressDialog->IsCanceled()) |
| 122 |
{ |
| 123 |
CLog::Log(LOGDEBUG, "Controller installer: User cancelled add-on installation"); |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
if (windowManager.GetActiveWindowOrDialog() != WINDOW_DIALOG_PROGRESS) |
| 128 |
{ |
| 129 |
CLog::Log(LOGDEBUG, "Controller installer: Progress dialog is hidden, cancelling"); |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
installedCount++; |
| 134 |
} |
| 135 |
|
| 136 |
CLog::Log(LOGDEBUG, "Controller window: Installed {} controller add-ons", installedCount); |
| 137 |
pProgressDialog->Close(); |
| 138 |
} |