Merge pull request #24470 from fuzzard/release_20.3
[xbmc.git] / xbmc / addons / AddonSystemSettings.cpp
blob30f3aaf66b0f54f604a61db9bdbede966cf50c38
1 /*
2 * Copyright (C) 2015-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 "AddonSystemSettings.h"
11 #include "ServiceBroker.h"
12 #include "addons/AddonInstaller.h"
13 #include "addons/AddonManager.h"
14 #include "addons/IAddon.h"
15 #include "addons/addoninfo/AddonInfo.h"
16 #include "addons/addoninfo/AddonType.h"
17 #include "guilib/GUIComponent.h"
18 #include "guilib/GUIWindowManager.h"
19 #include "guilib/LocalizeStrings.h"
20 #include "messaging/helpers/DialogHelper.h"
21 #include "messaging/helpers/DialogOKHelper.h"
22 #include "settings/Settings.h"
23 #include "settings/SettingsComponent.h"
24 #include "settings/lib/Setting.h"
25 #include "utils/StringUtils.h"
26 #include "utils/log.h"
28 namespace ADDON
31 CAddonSystemSettings::CAddonSystemSettings()
32 : m_activeSettings{
33 {AddonType::AUDIOENCODER, CSettings::SETTING_AUDIOCDS_ENCODER},
34 {AddonType::RESOURCE_LANGUAGE, CSettings::SETTING_LOCALE_LANGUAGE},
35 {AddonType::RESOURCE_UISOUNDS, CSettings::SETTING_LOOKANDFEEL_SOUNDSKIN},
36 {AddonType::SCRAPER_ALBUMS, CSettings::SETTING_MUSICLIBRARY_ALBUMSSCRAPER},
37 {AddonType::SCRAPER_ARTISTS, CSettings::SETTING_MUSICLIBRARY_ARTISTSSCRAPER},
38 {AddonType::SCRAPER_MOVIES, CSettings::SETTING_SCRAPERS_MOVIESDEFAULT},
39 {AddonType::SCRAPER_MUSICVIDEOS, CSettings::SETTING_SCRAPERS_MUSICVIDEOSDEFAULT},
40 {AddonType::SCRAPER_TVSHOWS, CSettings::SETTING_SCRAPERS_TVSHOWSDEFAULT},
41 {AddonType::SCREENSAVER, CSettings::SETTING_SCREENSAVER_MODE},
42 {AddonType::SCRIPT_WEATHER, CSettings::SETTING_WEATHER_ADDON},
43 {AddonType::SKIN, CSettings::SETTING_LOOKANDFEEL_SKIN},
44 {AddonType::WEB_INTERFACE, CSettings::SETTING_SERVICES_WEBSKIN},
45 {AddonType::VISUALIZATION, CSettings::SETTING_MUSICPLAYER_VISUALISATION},
49 CAddonSystemSettings& CAddonSystemSettings::GetInstance()
51 static CAddonSystemSettings inst;
52 return inst;
55 void CAddonSystemSettings::OnSettingAction(const std::shared_ptr<const CSetting>& setting)
57 if (setting->GetId() == CSettings::SETTING_ADDONS_MANAGE_DEPENDENCIES)
59 std::vector<std::string> params{"addons://dependencies/", "return"};
60 CServiceBroker::GetGUI()->GetWindowManager().ActivateWindow(WINDOW_ADDON_BROWSER, params);
62 else if (setting->GetId() == CSettings::SETTING_ADDONS_SHOW_RUNNING)
64 std::vector<std::string> params{"addons://running/", "return"};
65 CServiceBroker::GetGUI()->GetWindowManager().ActivateWindow(WINDOW_ADDON_BROWSER, params);
67 else if (setting->GetId() == CSettings::SETTING_ADDONS_REMOVE_ORPHANED_DEPENDENCIES)
69 using namespace KODI::MESSAGING::HELPERS;
71 const auto removedItems = CAddonInstaller::GetInstance().RemoveOrphanedDepsRecursively();
72 if (removedItems.size() > 0)
74 const auto message =
75 StringUtils::Format(g_localizeStrings.Get(36641), StringUtils::Join(removedItems, ", "));
77 ShowOKDialogText(CVariant{36640}, CVariant{message}); // "following orphaned were removed..."
79 else
81 ShowOKDialogText(CVariant{36640}, CVariant{36642}); // "no orphaned found / removed"
86 void CAddonSystemSettings::OnSettingChanged(const std::shared_ptr<const CSetting>& setting)
88 using namespace KODI::MESSAGING::HELPERS;
90 if (setting->GetId() == CSettings::SETTING_ADDONS_ALLOW_UNKNOWN_SOURCES &&
91 CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
92 CSettings::SETTING_ADDONS_ALLOW_UNKNOWN_SOURCES) &&
93 ShowYesNoDialogText(19098, 36618) != DialogResponse::CHOICE_YES)
95 CServiceBroker::GetSettingsComponent()->GetSettings()->SetBool(CSettings::SETTING_ADDONS_ALLOW_UNKNOWN_SOURCES, false);
99 bool CAddonSystemSettings::GetActive(AddonType type, AddonPtr& addon)
101 auto it = m_activeSettings.find(type);
102 if (it != m_activeSettings.end())
104 auto settingValue = CServiceBroker::GetSettingsComponent()->GetSettings()->GetString(it->second);
105 return CServiceBroker::GetAddonMgr().GetAddon(settingValue, addon, type,
106 OnlyEnabled::CHOICE_YES);
108 return false;
111 bool CAddonSystemSettings::SetActive(AddonType type, const std::string& addonID)
113 auto it = m_activeSettings.find(type);
114 if (it != m_activeSettings.end())
116 CServiceBroker::GetSettingsComponent()->GetSettings()->SetString(it->second, addonID);
117 return true;
119 return false;
122 bool CAddonSystemSettings::IsActive(const IAddon& addon)
124 AddonPtr active;
125 return GetActive(addon.Type(), active) && active->ID() == addon.ID();
128 bool CAddonSystemSettings::UnsetActive(const AddonInfoPtr& addon)
130 auto it = m_activeSettings.find(addon->MainType());
131 if (it == m_activeSettings.end())
132 return true;
134 auto setting = std::static_pointer_cast<CSettingString>(CServiceBroker::GetSettingsComponent()->GetSettings()->GetSetting(it->second));
135 if (setting->GetValue() != addon->ID())
136 return true;
138 if (setting->GetDefault() == addon->ID())
139 return false; // Cant unset defaults
141 setting->Reset();
142 return true;
145 int CAddonSystemSettings::GetAddonAutoUpdateMode() const
147 return CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(
148 CSettings::SETTING_ADDONS_AUTOUPDATES);
151 AddonRepoUpdateMode CAddonSystemSettings::GetAddonRepoUpdateMode() const
153 const int updateMode = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(
154 CSettings::SETTING_ADDONS_UPDATEMODE);
155 return static_cast<AddonRepoUpdateMode>(updateMode);