summaryrefslogtreecommitdiffstats
path: root/xbmc/settings/SettingConditions.cpp
blob: bb85e97b67adcd9c6af88cf27df4d93808f2b4ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 *  Copyright (C) 2013-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "SettingConditions.h"

#include "LockType.h"
#include "addons/AddonManager.h"
#include "addons/Skin.h"
#include "addons/addoninfo/AddonType.h"
#include "application/AppParams.h"
#include "cores/AudioEngine/Engines/ActiveAE/ActiveAESettings.h"
#include "ServiceBroker.h"
#include "GUIPassword.h"
#if defined(HAS_WEB_SERVER)
#include "network/WebServer.h"
#endif
#include "peripherals/Peripherals.h"
#include "profiles/ProfileManager.h"
#include "settings/SettingAddon.h"
#include "settings/SettingsComponent.h"
#include "utils/FontUtils.h"
#include "utils/StringUtils.h"
#include "windowing/WinSystem.h"

namespace
{
bool AddonHasSettings(const std::string& condition,
                      const std::string& value,
                      const SettingConstPtr& setting,
                      void* data)
{
  if (setting == NULL)
    return false;

  std::shared_ptr<const CSettingAddon> settingAddon = std::dynamic_pointer_cast<const CSettingAddon>(setting);
  if (settingAddon == NULL)
    return false;

  ADDON::AddonPtr addon;
  if (!CServiceBroker::GetAddonMgr().GetAddon(settingAddon->GetValue(), addon,
                                              settingAddon->GetAddonType(),
                                              ADDON::OnlyEnabled::CHOICE_YES) ||
      addon == NULL)
    return false;

  if (addon->Type() == ADDON::AddonType::SKIN)
    return ((ADDON::CSkinInfo*)addon.get())->HasSkinFile("SkinSettings.xml");

  return addon->CanHaveAddonOrInstanceSettings();
}

bool CheckMasterLock(const std::string& condition,
                     const std::string& value,
                     const SettingConstPtr& setting,
                     void* data)
{
  return g_passwordManager.IsMasterLockUnlocked(StringUtils::EqualsNoCase(value, "true"));
}

bool HasPeripherals(const std::string& condition,
                    const std::string& value,
                    const SettingConstPtr& setting,
                    void* data)
{
  return CServiceBroker::GetPeripherals().GetNumberOfPeripherals() > 0;
}

bool HasPeripheralLibraries(const std::string& condition,
                            const std::string& value,
                            const SettingConstPtr& setting,
                            void* data)
{
  return CServiceBroker::GetAddonMgr().HasInstalledAddons(ADDON::AddonType::PERIPHERALDLL);
}

bool HasRumbleFeature(const std::string& condition,
                      const std::string& value,
                      const SettingConstPtr& setting,
                      void* data)
{
  return CServiceBroker::GetPeripherals().SupportsFeature(PERIPHERALS::FEATURE_RUMBLE);
}

bool HasRumbleController(const std::string& condition,
                         const std::string& value,
                         const SettingConstPtr& setting,
                         void* data)
{
  return CServiceBroker::GetPeripherals().HasPeripheralWithFeature(PERIPHERALS::FEATURE_RUMBLE);
}

bool HasPowerOffFeature(const std::string& condition,
                        const std::string& value,
                        const SettingConstPtr& setting,
                        void* data)
{
  return CServiceBroker::GetPeripherals().SupportsFeature(PERIPHERALS::FEATURE_POWER_OFF);
}

bool IsHDRDisplay(const std::string& condition,
                  const std::string& value,
                  const SettingConstPtr& setting,
                  void* data)
{
  return CServiceBroker::GetWinSystem()->IsHDRDisplay();
}

bool IsMasterUser(const std::string& condition,
                  const std::string& value,
                  const SettingConstPtr& setting,
                  void* data)
{
  return g_passwordManager.bMasterUser;
}

bool HasSubtitlesFontExtensions(const std::string& condition,
                                const std::string& value,
                                const SettingConstPtr& setting,
                                void* data)
{
  auto settingStr = std::dynamic_pointer_cast<const CSettingString>(setting);
  if (!settingStr)
    return false;

  return UTILS::FONT::IsSupportedFontExtension(settingStr->GetValue());
}

bool ProfileCanWriteDatabase(const std::string& condition,
                             const std::string& value,
                             const SettingConstPtr& setting,
                             void* data)
{
  return CSettingConditions::GetCurrentProfile().canWriteDatabases();
}

bool ProfileCanWriteSources(const std::string& condition,
                            const std::string& value,
                            const SettingConstPtr& setting,
                            void* data)
{
  return CSettingConditions::GetCurrentProfile().canWriteSources();
}

bool ProfileHasAddons(const std::string& condition,
                      const std::string& value,
                      const SettingConstPtr& setting,
                      void* data)
{
  return CSettingConditions::GetCurrentProfile().hasAddons();
}

bool ProfileHasDatabase(const std::string& condition,
                        const std::string& value,
                        const SettingConstPtr& setting,
                        void* data)
{
  return CSettingConditions::GetCurrentProfile().hasDatabases();
}

bool ProfileHasSources(const std::string& condition,
                       const std::string& value,
                       const SettingConstPtr& setting,
                       void* data)
{
  return CSettingConditions::GetCurrentProfile().hasSources();
}

bool ProfileHasAddonManagerLocked(const std::string& condition,
                                  const std::string& value,
                                  const SettingConstPtr& setting,
                                  void* data)
{
  return CSettingConditions::GetCurrentProfile().addonmanagerLocked();
}

bool ProfileHasFilesLocked(const std::string& condition,
                           const std::string& value,
                           const SettingConstPtr& setting,
                           void* data)
{
  return CSettingConditions::GetCurrentProfile().filesLocked();
}

bool ProfileHasMusicLocked(const std::string& condition,
                           const std::string& value,
                           const SettingConstPtr& setting,
                           void* data)
{
  return CSettingConditions::GetCurrentProfile().musicLocked();
}

bool ProfileHasPicturesLocked(const std::string& condition,
                              const std::string& value,
                              const SettingConstPtr& setting,
                              void* data)
{
  return CSettingConditions::GetCurrentProfile().picturesLocked();
}

bool ProfileHasProgramsLocked(const std::string& condition,
                              const std::string& value,
                              const SettingConstPtr& setting,
                              void* data)
{
  return CSettingConditions::GetCurrentProfile().programsLocked();
}

bool ProfileHasSettingsLocked(const std::string& condition,
                              const std::string& value,
                              const SettingConstPtr& setting,
                              void* data)
{
  LOCK_LEVEL::SETTINGS_LOCK slValue=LOCK_LEVEL::ALL;
  if (StringUtils::EqualsNoCase(value, "none"))
    slValue = LOCK_LEVEL::NONE;
  else if (StringUtils::EqualsNoCase(value, "standard"))
    slValue = LOCK_LEVEL::STANDARD;
  else if (StringUtils::EqualsNoCase(value, "advanced"))
    slValue = LOCK_LEVEL::ADVANCED;
  else if (StringUtils::EqualsNoCase(value, "expert"))
    slValue = LOCK_LEVEL::EXPERT;
  return slValue <= CSettingConditions::GetCurrentProfile().settingsLockLevel();
}

bool ProfileHasVideosLocked(const std::string& condition,
                            const std::string& value,
                            const SettingConstPtr& setting,
                            void* data)
{
  return CSettingConditions::GetCurrentProfile().videoLocked();
}

bool ProfileLockMode(const std::string& condition,
                     const std::string& value,
                     const SettingConstPtr& setting,
                     void* data)
{
  char* tmp = nullptr;
  LockType lock = (LockType)strtol(value.c_str(), &tmp, 0);
  if (tmp != NULL && *tmp != '\0')
    return false;

  return CSettingConditions::GetCurrentProfile().getLockMode() == lock;
}

bool GreaterThan(const std::string& condition,
                 const std::string& value,
                 const SettingConstPtr& setting,
                 void* data)
{
  if (setting == NULL)
    return false;

  std::shared_ptr<const CSettingInt> settingInt = std::dynamic_pointer_cast<const CSettingInt>(setting);
  if (settingInt == NULL)
    return false;

  char* tmp = nullptr;

  int lhs = settingInt->GetValue();
  int rhs = StringUtils::IsInteger(value) ? (int)strtol(value.c_str(), &tmp, 0) : 0;

  return lhs > rhs;
}

bool GreaterThanOrEqual(const std::string& condition,
                        const std::string& value,
                        const SettingConstPtr& setting,
                        void* data)
{
  if (setting == NULL)
    return false;

  std::shared_ptr<const CSettingInt> settingInt = std::dynamic_pointer_cast<const CSettingInt>(setting);
  if (settingInt == NULL)
    return false;

  char* tmp = nullptr;

  int lhs = settingInt->GetValue();
  int rhs = StringUtils::IsInteger(value) ? (int)strtol(value.c_str(), &tmp, 0) : 0;

  return lhs >= rhs;
}

bool LessThan(const std::string& condition,
              const std::string& value,
              const SettingConstPtr& setting,
              void* data)
{
  if (setting == NULL)
    return false;

  std::shared_ptr<const CSettingInt> settingInt = std::dynamic_pointer_cast<const CSettingInt>(setting);
  if (settingInt == NULL)
    return false;

  char* tmp = nullptr;

  int lhs = settingInt->GetValue();
  int rhs = StringUtils::IsInteger(value) ? (int)strtol(value.c_str(), &tmp, 0) : 0;

  return lhs < rhs;
}

bool LessThanOrEqual(const std::string& condition,
                     const std::string& value,
                     const SettingConstPtr& setting,
                     void* data)
{
  if (setting == NULL)
    return false;

  std::shared_ptr<const CSettingInt> settingInt = std::dynamic_pointer_cast<const CSettingInt>(setting);
  if (settingInt == NULL)
    return false;

  char* tmp = nullptr;

  int lhs = settingInt->GetValue();
  int rhs = StringUtils::IsInteger(value) ? (int)strtol(value.c_str(), &tmp, 0) : 0;

  return lhs <= rhs;
}
}; // anonymous namespace

const CProfileManager* CSettingConditions::m_profileManager = nullptr;
std::set<std::string> CSettingConditions::m_simpleConditions;
std::map<std::string, SettingConditionCheck> CSettingConditions::m_complexConditions;

void CSettingConditions::Initialize()
{
  if (!m_simpleConditions.empty())
    return;

  // add simple conditions
  m_simpleConditions.emplace("true");
#ifdef HAS_UPNP
  m_simpleConditions.emplace("has_upnp");
#endif
#ifdef HAS_AIRPLAY
  m_simpleConditions.emplace("has_airplay");
#endif
#ifdef HAVE_X11
  m_simpleConditions.emplace("have_x11");
#endif
#ifdef HAVE_WAYLAND
  m_simpleConditions.emplace("have_wayland");
#endif
#ifdef HAS_GL
  m_simpleConditions.emplace("has_gl");
#endif
#ifdef HAS_GLES
  m_simpleConditions.emplace("has_gles");
#endif
#if HAS_GLES >= 2
  m_simpleConditions.emplace("has_glesv2");
#endif
#ifdef HAS_TIME_SERVER
  m_simpleConditions.emplace("has_time_server");
#endif
#ifdef HAS_WEB_SERVER
  m_simpleConditions.emplace("has_web_server");
#endif
#ifdef HAS_FILESYSTEM_SMB
  m_simpleConditions.emplace("has_filesystem_smb");
#endif
#ifdef HAS_FILESYSTEM_NFS
  m_simpleConditions.insert("has_filesystem_nfs");
#endif
#ifdef HAS_ZEROCONF
  m_simpleConditions.emplace("has_zeroconf");
#endif
#ifdef HAVE_LIBVA
  m_simpleConditions.emplace("have_libva");
#endif
#ifdef HAVE_LIBVDPAU
  m_simpleConditions.emplace("have_libvdpau");
#endif
#ifdef TARGET_ANDROID
  m_simpleConditions.emplace("has_mediacodec");
#endif
#ifdef TARGET_DARWIN
  m_simpleConditions.emplace("HasVTB");
#endif
#ifdef TARGET_DARWIN_OSX
  m_simpleConditions.emplace("have_osx");
#endif
#ifdef TARGET_DARWIN_IOS
  m_simpleConditions.emplace("have_ios");
#endif
#ifdef TARGET_DARWIN_TVOS
  m_simpleConditions.emplace("have_tvos");
#endif
#if defined(TARGET_WINDOWS)
  m_simpleConditions.emplace("has_dx");
  m_simpleConditions.emplace("hasdxva2");
#endif
#ifdef HAVE_LCMS2
  m_simpleConditions.emplace("have_lcms2");
#endif

#ifdef TARGET_ANDROID
  m_simpleConditions.emplace("isstandalone");
#else
  if (CServiceBroker::GetAppParams()->IsStandAlone())
    m_simpleConditions.emplace("isstandalone");
#endif

  m_simpleConditions.emplace("has_ae_quality_levels");

#ifdef HAS_WEB_SERVER
  if (CWebServer::WebServerSupportsSSL())
    m_simpleConditions.emplace("webserver_has_ssl");
#endif

#ifdef HAVE_LIBBLURAY
  m_simpleConditions.emplace("have_libbluray");
#endif

#ifdef HAS_CDDA_RIPPER
  m_simpleConditions.emplace("has_cdda_ripper");
#endif

#ifdef HAS_DVD_DRIVE
  m_simpleConditions.emplace("has_dvd_drive");
#endif

  // add complex conditions
  m_complexConditions.emplace("addonhassettings", AddonHasSettings);
  m_complexConditions.emplace("checkmasterlock", CheckMasterLock);
  m_complexConditions.emplace("hasperipherals", HasPeripherals);
  m_complexConditions.emplace("hasperipherallibraries", HasPeripheralLibraries);
  m_complexConditions.emplace("hasrumblefeature", HasRumbleFeature);
  m_complexConditions.emplace("hasrumblecontroller", HasRumbleController);
  m_complexConditions.emplace("haspowerofffeature", HasPowerOffFeature);
  m_complexConditions.emplace("ishdrdisplay", IsHDRDisplay);
  m_complexConditions.emplace("ismasteruser", IsMasterUser);
  m_complexConditions.emplace("hassubtitlesfontextensions", HasSubtitlesFontExtensions);
  m_complexConditions.emplace("profilecanwritedatabase", ProfileCanWriteDatabase);
  m_complexConditions.emplace("profilecanwritesources", ProfileCanWriteSources);
  m_complexConditions.emplace("profilehasaddons", ProfileHasAddons);
  m_complexConditions.emplace("profilehasdatabase", ProfileHasDatabase);
  m_complexConditions.emplace("profilehassources", ProfileHasSources);
  m_complexConditions.emplace("profilehasaddonmanagerlocked", ProfileHasAddonManagerLocked);
  m_complexConditions.emplace("profilehasfileslocked", ProfileHasFilesLocked);
  m_complexConditions.emplace("profilehasmusiclocked", ProfileHasMusicLocked);
  m_complexConditions.emplace("profilehaspictureslocked", ProfileHasPicturesLocked);
  m_complexConditions.emplace("profilehasprogramslocked", ProfileHasProgramsLocked);
  m_complexConditions.emplace("profilehassettingslocked", ProfileHasSettingsLocked);
  m_complexConditions.emplace("profilehasvideoslocked", ProfileHasVideosLocked);
  m_complexConditions.emplace("profilelockmode", ProfileLockMode);
  m_complexConditions.emplace("aesettingvisible", ActiveAE::CActiveAESettings::IsSettingVisible);
  m_complexConditions.emplace("gt", GreaterThan);
  m_complexConditions.emplace("gte", GreaterThanOrEqual);
  m_complexConditions.emplace("lt", LessThan);
  m_complexConditions.emplace("lte", LessThanOrEqual);
}

void CSettingConditions::Deinitialize()
{
  m_profileManager = nullptr;
}

const CProfile& CSettingConditions::GetCurrentProfile()
{
  if (!m_profileManager)
    m_profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager().get();

  if (m_profileManager)
    return m_profileManager->GetCurrentProfile();

  static CProfile emptyProfile;
  return emptyProfile;
}

bool CSettingConditions::Check(const std::string& condition,
                               const std::string& value /* = "" */,
                               const SettingConstPtr& setting /* = NULL */)
{
  if (m_simpleConditions.find(condition) != m_simpleConditions.end())
    return true;

  std::map<std::string, SettingConditionCheck>::const_iterator itCondition = m_complexConditions.find(condition);
  if (itCondition != m_complexConditions.end())
    return itCondition->second(condition, value, setting, NULL);

  return Check(condition);
}