2 * Copyright (C) 2013-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.
9 #include "SettingConditions.h"
12 #include "addons/AddonManager.h"
13 #include "addons/Skin.h"
14 #include "addons/addoninfo/AddonType.h"
15 #include "application/AppParams.h"
16 #include "cores/AudioEngine/Engines/ActiveAE/ActiveAESettings.h"
17 #include "ServiceBroker.h"
18 #include "GUIPassword.h"
19 #if defined(HAS_WEB_SERVER)
20 #include "network/WebServer.h"
22 #include "peripherals/Peripherals.h"
23 #include "profiles/ProfileManager.h"
24 #include "settings/SettingAddon.h"
25 #include "settings/SettingsComponent.h"
26 #include "utils/FontUtils.h"
27 #include "utils/StringUtils.h"
28 #include "windowing/WinSystem.h"
32 bool AddonHasSettings(const std::string
& condition
,
33 const std::string
& value
,
34 const SettingConstPtr
& setting
,
40 std::shared_ptr
<const CSettingAddon
> settingAddon
= std::dynamic_pointer_cast
<const CSettingAddon
>(setting
);
41 if (settingAddon
== NULL
)
44 ADDON::AddonPtr addon
;
45 if (!CServiceBroker::GetAddonMgr().GetAddon(settingAddon
->GetValue(), addon
,
46 settingAddon
->GetAddonType(),
47 ADDON::OnlyEnabled::CHOICE_YES
) ||
51 if (addon
->Type() == ADDON::AddonType::SKIN
)
52 return ((ADDON::CSkinInfo
*)addon
.get())->HasSkinFile("SkinSettings.xml");
54 return addon
->CanHaveAddonOrInstanceSettings();
57 bool CheckMasterLock(const std::string
& condition
,
58 const std::string
& value
,
59 const SettingConstPtr
& setting
,
62 return g_passwordManager
.IsMasterLockUnlocked(StringUtils::EqualsNoCase(value
, "true"));
65 bool HasPeripherals(const std::string
& condition
,
66 const std::string
& value
,
67 const SettingConstPtr
& setting
,
70 return CServiceBroker::GetPeripherals().GetNumberOfPeripherals() > 0;
73 bool HasPeripheralLibraries(const std::string
& condition
,
74 const std::string
& value
,
75 const SettingConstPtr
& setting
,
78 return CServiceBroker::GetAddonMgr().HasInstalledAddons(ADDON::AddonType::PERIPHERALDLL
);
81 bool HasRumbleFeature(const std::string
& condition
,
82 const std::string
& value
,
83 const SettingConstPtr
& setting
,
86 return CServiceBroker::GetPeripherals().SupportsFeature(PERIPHERALS::FEATURE_RUMBLE
);
89 bool HasRumbleController(const std::string
& condition
,
90 const std::string
& value
,
91 const SettingConstPtr
& setting
,
94 return CServiceBroker::GetPeripherals().HasPeripheralWithFeature(PERIPHERALS::FEATURE_RUMBLE
);
97 bool HasPowerOffFeature(const std::string
& condition
,
98 const std::string
& value
,
99 const SettingConstPtr
& setting
,
102 return CServiceBroker::GetPeripherals().SupportsFeature(PERIPHERALS::FEATURE_POWER_OFF
);
105 bool HasSystemSdrPeakLuminance(const std::string
& condition
,
106 const std::string
& value
,
107 const SettingConstPtr
& setting
,
110 return CServiceBroker::GetWinSystem()->HasSystemSdrPeakLuminance();
113 bool SupportsVideoSuperResolution(const std::string
& condition
,
114 const std::string
& value
,
115 const SettingConstPtr
& setting
,
118 return CServiceBroker::GetWinSystem()->SupportsVideoSuperResolution();
121 bool SupportsDolbyVision(const std::string
& condition
,
122 const std::string
& value
,
123 const SettingConstPtr
& setting
,
126 return CServiceBroker::GetWinSystem()->GetDisplayHDRCapabilities().SupportsDolbyVision();
129 bool SupportsScreenMove(const std::string
& condition
,
130 const std::string
& value
,
131 const SettingConstPtr
& setting
,
134 return CServiceBroker::GetWinSystem()->SupportsScreenMove();
137 bool IsHDRDisplay(const std::string
& condition
,
138 const std::string
& value
,
139 const SettingConstPtr
& setting
,
142 return CServiceBroker::GetWinSystem()->IsHDRDisplay();
145 bool IsMasterUser(const std::string
& condition
,
146 const std::string
& value
,
147 const SettingConstPtr
& setting
,
150 return g_passwordManager
.bMasterUser
;
153 bool HasSubtitlesFontExtensions(const std::string
& condition
,
154 const std::string
& value
,
155 const SettingConstPtr
& setting
,
158 auto settingStr
= std::dynamic_pointer_cast
<const CSettingString
>(setting
);
162 return UTILS::FONT::IsSupportedFontExtension(settingStr
->GetValue());
165 bool ProfileCanWriteDatabase(const std::string
& condition
,
166 const std::string
& value
,
167 const SettingConstPtr
& setting
,
170 return CSettingConditions::GetCurrentProfile().canWriteDatabases();
173 bool ProfileCanWriteSources(const std::string
& condition
,
174 const std::string
& value
,
175 const SettingConstPtr
& setting
,
178 return CSettingConditions::GetCurrentProfile().canWriteSources();
181 bool ProfileHasAddons(const std::string
& condition
,
182 const std::string
& value
,
183 const SettingConstPtr
& setting
,
186 return CSettingConditions::GetCurrentProfile().hasAddons();
189 bool ProfileHasDatabase(const std::string
& condition
,
190 const std::string
& value
,
191 const SettingConstPtr
& setting
,
194 return CSettingConditions::GetCurrentProfile().hasDatabases();
197 bool ProfileHasSources(const std::string
& condition
,
198 const std::string
& value
,
199 const SettingConstPtr
& setting
,
202 return CSettingConditions::GetCurrentProfile().hasSources();
205 bool ProfileHasAddonManagerLocked(const std::string
& condition
,
206 const std::string
& value
,
207 const SettingConstPtr
& setting
,
210 return CSettingConditions::GetCurrentProfile().addonmanagerLocked();
213 bool ProfileHasFilesLocked(const std::string
& condition
,
214 const std::string
& value
,
215 const SettingConstPtr
& setting
,
218 return CSettingConditions::GetCurrentProfile().filesLocked();
221 bool ProfileHasMusicLocked(const std::string
& condition
,
222 const std::string
& value
,
223 const SettingConstPtr
& setting
,
226 return CSettingConditions::GetCurrentProfile().musicLocked();
229 bool ProfileHasPicturesLocked(const std::string
& condition
,
230 const std::string
& value
,
231 const SettingConstPtr
& setting
,
234 return CSettingConditions::GetCurrentProfile().picturesLocked();
237 bool ProfileHasProgramsLocked(const std::string
& condition
,
238 const std::string
& value
,
239 const SettingConstPtr
& setting
,
242 return CSettingConditions::GetCurrentProfile().programsLocked();
245 bool ProfileHasSettingsLocked(const std::string
& condition
,
246 const std::string
& value
,
247 const SettingConstPtr
& setting
,
250 LOCK_LEVEL::SETTINGS_LOCK slValue
=LOCK_LEVEL::ALL
;
251 if (StringUtils::EqualsNoCase(value
, "none"))
252 slValue
= LOCK_LEVEL::NONE
;
253 else if (StringUtils::EqualsNoCase(value
, "standard"))
254 slValue
= LOCK_LEVEL::STANDARD
;
255 else if (StringUtils::EqualsNoCase(value
, "advanced"))
256 slValue
= LOCK_LEVEL::ADVANCED
;
257 else if (StringUtils::EqualsNoCase(value
, "expert"))
258 slValue
= LOCK_LEVEL::EXPERT
;
259 return slValue
<= CSettingConditions::GetCurrentProfile().settingsLockLevel();
262 bool ProfileHasVideosLocked(const std::string
& condition
,
263 const std::string
& value
,
264 const SettingConstPtr
& setting
,
267 return CSettingConditions::GetCurrentProfile().videoLocked();
270 bool ProfileLockMode(const std::string
& condition
,
271 const std::string
& value
,
272 const SettingConstPtr
& setting
,
276 LockType lock
= (LockType
)strtol(value
.c_str(), &tmp
, 0);
277 if (tmp
!= NULL
&& *tmp
!= '\0')
280 return CSettingConditions::GetCurrentProfile().getLockMode() == lock
;
283 bool GreaterThan(const std::string
& condition
,
284 const std::string
& value
,
285 const SettingConstPtr
& setting
,
291 std::shared_ptr
<const CSettingInt
> settingInt
= std::dynamic_pointer_cast
<const CSettingInt
>(setting
);
292 if (settingInt
== NULL
)
297 int lhs
= settingInt
->GetValue();
298 int rhs
= StringUtils::IsInteger(value
) ? (int)strtol(value
.c_str(), &tmp
, 0) : 0;
303 bool GreaterThanOrEqual(const std::string
& condition
,
304 const std::string
& value
,
305 const SettingConstPtr
& setting
,
311 std::shared_ptr
<const CSettingInt
> settingInt
= std::dynamic_pointer_cast
<const CSettingInt
>(setting
);
312 if (settingInt
== NULL
)
317 int lhs
= settingInt
->GetValue();
318 int rhs
= StringUtils::IsInteger(value
) ? (int)strtol(value
.c_str(), &tmp
, 0) : 0;
323 bool LessThan(const std::string
& condition
,
324 const std::string
& value
,
325 const SettingConstPtr
& setting
,
331 std::shared_ptr
<const CSettingInt
> settingInt
= std::dynamic_pointer_cast
<const CSettingInt
>(setting
);
332 if (settingInt
== NULL
)
337 int lhs
= settingInt
->GetValue();
338 int rhs
= StringUtils::IsInteger(value
) ? (int)strtol(value
.c_str(), &tmp
, 0) : 0;
343 bool LessThanOrEqual(const std::string
& condition
,
344 const std::string
& value
,
345 const SettingConstPtr
& setting
,
351 std::shared_ptr
<const CSettingInt
> settingInt
= std::dynamic_pointer_cast
<const CSettingInt
>(setting
);
352 if (settingInt
== NULL
)
357 int lhs
= settingInt
->GetValue();
358 int rhs
= StringUtils::IsInteger(value
) ? (int)strtol(value
.c_str(), &tmp
, 0) : 0;
362 }; // anonymous namespace
364 const CProfileManager
* CSettingConditions::m_profileManager
= nullptr;
365 std::set
<std::string
> CSettingConditions::m_simpleConditions
;
366 std::map
<std::string
, SettingConditionCheck
> CSettingConditions::m_complexConditions
;
368 void CSettingConditions::Initialize()
370 if (!m_simpleConditions
.empty())
373 // add simple conditions
374 m_simpleConditions
.emplace("true");
376 m_simpleConditions
.emplace("has_upnp");
379 m_simpleConditions
.emplace("has_airplay");
382 m_simpleConditions
.emplace("have_x11");
385 m_simpleConditions
.emplace("have_wayland");
388 m_simpleConditions
.emplace("has_gl");
391 m_simpleConditions
.emplace("has_gles");
394 m_simpleConditions
.emplace("has_glesv2");
396 #ifdef HAS_TIME_SERVER
397 m_simpleConditions
.emplace("has_time_server");
399 #ifdef HAS_WEB_SERVER
400 m_simpleConditions
.emplace("has_web_server");
402 #ifdef HAS_FILESYSTEM_SMB
403 m_simpleConditions
.emplace("has_filesystem_smb");
405 #ifdef HAS_FILESYSTEM_NFS
406 m_simpleConditions
.insert("has_filesystem_nfs");
409 m_simpleConditions
.emplace("has_zeroconf");
412 m_simpleConditions
.emplace("have_libva");
415 m_simpleConditions
.emplace("have_libvdpau");
417 #ifdef TARGET_ANDROID
418 m_simpleConditions
.emplace("has_mediacodec");
421 m_simpleConditions
.emplace("HasVTB");
423 #ifdef TARGET_DARWIN_OSX
424 m_simpleConditions
.emplace("have_osx");
426 #ifdef TARGET_DARWIN_IOS
427 m_simpleConditions
.emplace("have_ios");
429 #ifdef TARGET_DARWIN_TVOS
430 m_simpleConditions
.emplace("have_tvos");
432 #if defined(TARGET_WINDOWS)
433 m_simpleConditions
.emplace("has_dx");
434 m_simpleConditions
.emplace("hasdxva2");
436 #if defined(TARGET_WEBOS)
437 m_simpleConditions
.emplace("have_webos");
441 m_simpleConditions
.emplace("have_lcms2");
444 #ifdef TARGET_ANDROID
445 m_simpleConditions
.emplace("isstandalone");
447 if (CServiceBroker::GetAppParams()->IsStandAlone())
448 m_simpleConditions
.emplace("isstandalone");
451 m_simpleConditions
.emplace("has_ae_quality_levels");
453 #ifdef HAS_WEB_SERVER
454 if (CWebServer::WebServerSupportsSSL())
455 m_simpleConditions
.emplace("webserver_has_ssl");
458 #ifdef HAVE_LIBBLURAY
459 m_simpleConditions
.emplace("have_libbluray");
462 #ifdef HAS_CDDA_RIPPER
463 m_simpleConditions
.emplace("has_cdda_ripper");
466 #ifdef HAS_OPTICAL_DRIVE
467 m_simpleConditions
.emplace("has_optical_drive");
470 #ifdef HAS_XBMCHELPER
471 m_simpleConditions
.emplace("has_xbmchelper");
474 // add complex conditions
475 m_complexConditions
.emplace("addonhassettings", AddonHasSettings
);
476 m_complexConditions
.emplace("checkmasterlock", CheckMasterLock
);
477 m_complexConditions
.emplace("hasperipherals", HasPeripherals
);
478 m_complexConditions
.emplace("hasperipherallibraries", HasPeripheralLibraries
);
479 m_complexConditions
.emplace("hasrumblefeature", HasRumbleFeature
);
480 m_complexConditions
.emplace("hasrumblecontroller", HasRumbleController
);
481 m_complexConditions
.emplace("haspowerofffeature", HasPowerOffFeature
);
482 m_complexConditions
.emplace("hassystemsdrpeakluminance", HasSystemSdrPeakLuminance
);
483 m_complexConditions
.emplace("supportsscreenmove", SupportsScreenMove
);
484 m_complexConditions
.emplace("supportsvideosuperresolution", SupportsVideoSuperResolution
);
485 m_complexConditions
.emplace("supportsdolbyvision", SupportsDolbyVision
);
486 m_complexConditions
.emplace("ishdrdisplay", IsHDRDisplay
);
487 m_complexConditions
.emplace("ismasteruser", IsMasterUser
);
488 m_complexConditions
.emplace("hassubtitlesfontextensions", HasSubtitlesFontExtensions
);
489 m_complexConditions
.emplace("profilecanwritedatabase", ProfileCanWriteDatabase
);
490 m_complexConditions
.emplace("profilecanwritesources", ProfileCanWriteSources
);
491 m_complexConditions
.emplace("profilehasaddons", ProfileHasAddons
);
492 m_complexConditions
.emplace("profilehasdatabase", ProfileHasDatabase
);
493 m_complexConditions
.emplace("profilehassources", ProfileHasSources
);
494 m_complexConditions
.emplace("profilehasaddonmanagerlocked", ProfileHasAddonManagerLocked
);
495 m_complexConditions
.emplace("profilehasfileslocked", ProfileHasFilesLocked
);
496 m_complexConditions
.emplace("profilehasmusiclocked", ProfileHasMusicLocked
);
497 m_complexConditions
.emplace("profilehaspictureslocked", ProfileHasPicturesLocked
);
498 m_complexConditions
.emplace("profilehasprogramslocked", ProfileHasProgramsLocked
);
499 m_complexConditions
.emplace("profilehassettingslocked", ProfileHasSettingsLocked
);
500 m_complexConditions
.emplace("profilehasvideoslocked", ProfileHasVideosLocked
);
501 m_complexConditions
.emplace("profilelockmode", ProfileLockMode
);
502 m_complexConditions
.emplace("aesettingvisible", ActiveAE::CActiveAESettings::IsSettingVisible
);
503 m_complexConditions
.emplace("gt", GreaterThan
);
504 m_complexConditions
.emplace("gte", GreaterThanOrEqual
);
505 m_complexConditions
.emplace("lt", LessThan
);
506 m_complexConditions
.emplace("lte", LessThanOrEqual
);
509 void CSettingConditions::Deinitialize()
511 m_profileManager
= nullptr;
514 const CProfile
& CSettingConditions::GetCurrentProfile()
516 if (!m_profileManager
)
517 m_profileManager
= CServiceBroker::GetSettingsComponent()->GetProfileManager().get();
519 if (m_profileManager
)
520 return m_profileManager
->GetCurrentProfile();
522 static CProfile emptyProfile
;
526 bool CSettingConditions::Check(const std::string
& condition
,
527 const std::string
& value
/* = "" */,
528 const SettingConstPtr
& setting
/* = NULL */)
530 if (m_simpleConditions
.find(condition
) != m_simpleConditions
.end())
533 std::map
<std::string
, SettingConditionCheck
>::const_iterator itCondition
= m_complexConditions
.find(condition
);
534 if (itCondition
!= m_complexConditions
.end())
535 return itCondition
->second(condition
, value
, setting
, NULL
);
537 return Check(condition
);