CUserSettingBoolean.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "CUserSettingBoolean.h"
  2. #include "WallpaperEngine/Core/Core.h"
  3. #include "WallpaperEngine/Core/Projects/CProperty.h"
  4. #include "WallpaperEngine/Core/Projects/CPropertyBoolean.h"
  5. #include "WallpaperEngine/Core/Projects/CPropertyCombo.h"
  6. #include "WallpaperEngine/Core/Projects/CPropertySlider.h"
  7. #include "WallpaperEngine/Core/Projects/CPropertyText.h"
  8. using namespace WallpaperEngine::Core;
  9. using namespace WallpaperEngine::Core::Projects;
  10. using namespace WallpaperEngine::Core::UserSettings;
  11. CUserSettingBoolean::CUserSettingBoolean (bool hasCondition, bool hasSource, bool defaultValue, std::string source, std::string expectedValue) :
  12. CUserSettingValue (Type),
  13. m_hasCondition (hasCondition),
  14. m_hasSource(hasSource),
  15. m_default(defaultValue),
  16. m_source (std::move(source)),
  17. m_expectedValue(std::move(expectedValue))
  18. {
  19. }
  20. CUserSettingBoolean* CUserSettingBoolean::fromJSON (nlohmann::json& data)
  21. {
  22. bool hasCondition = false;
  23. bool hasSource = false;
  24. bool defaultValue = false;
  25. std::string source;
  26. std::string expectedValue;
  27. if (data.is_object () == true)
  28. {
  29. hasSource = true;
  30. auto userIt = data.find ("user");
  31. defaultValue = jsonFindDefault (data, "value", false); // is this default value right?
  32. if (userIt != data.end ())
  33. {
  34. if (userIt->is_string ())
  35. {
  36. source = *userIt;
  37. }
  38. else
  39. {
  40. hasCondition = true;
  41. source = *jsonFindRequired (userIt, "name", "Name for conditional setting must be present");
  42. expectedValue = *jsonFindRequired (userIt, "condition", "Condition for conditional setting must be present");
  43. }
  44. }
  45. else
  46. {
  47. fprintf (stderr, "Boolean property doesn't have user member, this could mean an scripted value\n");
  48. }
  49. }
  50. else
  51. {
  52. if (data.is_boolean () == false)
  53. throw std::runtime_error ("Expected boolean value on user settings");
  54. defaultValue = data.get<bool> ();
  55. }
  56. return new CUserSettingBoolean (hasCondition, hasSource, defaultValue, source, expectedValue);
  57. }
  58. CUserSettingBoolean* CUserSettingBoolean::fromScalar (bool value)
  59. {
  60. return new CUserSettingBoolean (false, false, value, "", "");
  61. }
  62. bool CUserSettingBoolean::getDefaultValue ()
  63. {
  64. return this->m_default;
  65. }
  66. bool CUserSettingBoolean::processValue (const std::vector<Projects::CProperty*>& properties)
  67. {
  68. if (this->m_hasSource == false && this->m_hasCondition == false)
  69. return this->getDefaultValue ();
  70. for (auto cur : properties)
  71. {
  72. if (cur->getName () != this->m_source)
  73. continue;
  74. if (this->m_hasCondition == false)
  75. {
  76. if (cur->is <CPropertyBoolean> ())
  77. return cur->as <CPropertyBoolean> ()->getValue ();
  78. throw std::runtime_error ("Property without condition must match type (boolean)");
  79. }
  80. // TODO: properly validate this as the combos might be more than just strings?
  81. if (cur->is <CPropertyCombo> ())
  82. return cur->as <CPropertyCombo> ()->getValue () == this->m_expectedValue;
  83. throw std::runtime_error ("Boolean property with condition doesn't match against combo value");
  84. }
  85. return this->m_default;
  86. }
  87. std::string CUserSettingBoolean::Type = "boolean";