The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. PropertySet::PropertySet (const bool ignoreCaseOfKeyNames)
  24. : properties (ignoreCaseOfKeyNames),
  25. fallbackProperties (nullptr),
  26. ignoreCaseOfKeys (ignoreCaseOfKeyNames)
  27. {
  28. }
  29. PropertySet::PropertySet (const PropertySet& other)
  30. : properties (other.properties),
  31. fallbackProperties (other.fallbackProperties),
  32. ignoreCaseOfKeys (other.ignoreCaseOfKeys)
  33. {
  34. }
  35. PropertySet& PropertySet::operator= (const PropertySet& other)
  36. {
  37. properties = other.properties;
  38. fallbackProperties = other.fallbackProperties;
  39. ignoreCaseOfKeys = other.ignoreCaseOfKeys;
  40. propertyChanged();
  41. return *this;
  42. }
  43. PropertySet::~PropertySet()
  44. {
  45. }
  46. void PropertySet::clear()
  47. {
  48. const ScopedLock sl (lock);
  49. if (properties.size() > 0)
  50. {
  51. properties.clear();
  52. propertyChanged();
  53. }
  54. }
  55. String PropertySet::getValue (StringRef keyName, const String& defaultValue) const noexcept
  56. {
  57. const ScopedLock sl (lock);
  58. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  59. if (index >= 0)
  60. return properties.getAllValues() [index];
  61. return fallbackProperties != nullptr ? fallbackProperties->getValue (keyName, defaultValue)
  62. : defaultValue;
  63. }
  64. int PropertySet::getIntValue (StringRef keyName, const int defaultValue) const noexcept
  65. {
  66. const ScopedLock sl (lock);
  67. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  68. if (index >= 0)
  69. return properties.getAllValues() [index].getIntValue();
  70. return fallbackProperties != nullptr ? fallbackProperties->getIntValue (keyName, defaultValue)
  71. : defaultValue;
  72. }
  73. double PropertySet::getDoubleValue (StringRef keyName, const double defaultValue) const noexcept
  74. {
  75. const ScopedLock sl (lock);
  76. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  77. if (index >= 0)
  78. return properties.getAllValues()[index].getDoubleValue();
  79. return fallbackProperties != nullptr ? fallbackProperties->getDoubleValue (keyName, defaultValue)
  80. : defaultValue;
  81. }
  82. bool PropertySet::getBoolValue (StringRef keyName, const bool defaultValue) const noexcept
  83. {
  84. const ScopedLock sl (lock);
  85. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  86. if (index >= 0)
  87. return properties.getAllValues() [index].getIntValue() != 0;
  88. return fallbackProperties != nullptr ? fallbackProperties->getBoolValue (keyName, defaultValue)
  89. : defaultValue;
  90. }
  91. XmlElement* PropertySet::getXmlValue (StringRef keyName) const
  92. {
  93. return XmlDocument::parse (getValue (keyName));
  94. }
  95. void PropertySet::setValue (const String& keyName, const var& v)
  96. {
  97. jassert (keyName.isNotEmpty()); // shouldn't use an empty key name!
  98. if (keyName.isNotEmpty())
  99. {
  100. const String value (v.toString());
  101. const ScopedLock sl (lock);
  102. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  103. if (index < 0 || properties.getAllValues() [index] != value)
  104. {
  105. properties.set (keyName, value);
  106. propertyChanged();
  107. }
  108. }
  109. }
  110. void PropertySet::removeValue (StringRef keyName)
  111. {
  112. if (keyName.isNotEmpty())
  113. {
  114. const ScopedLock sl (lock);
  115. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  116. if (index >= 0)
  117. {
  118. properties.remove (keyName);
  119. propertyChanged();
  120. }
  121. }
  122. }
  123. void PropertySet::setValue (const String& keyName, const XmlElement* const xml)
  124. {
  125. setValue (keyName, xml == nullptr ? var()
  126. : var (xml->createDocument ("", true)));
  127. }
  128. bool PropertySet::containsKey (StringRef keyName) const noexcept
  129. {
  130. const ScopedLock sl (lock);
  131. return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
  132. }
  133. void PropertySet::addAllPropertiesFrom (const PropertySet& source)
  134. {
  135. const ScopedLock sl (source.getLock());
  136. for (int i = 0; i < source.properties.size(); ++i)
  137. setValue (source.properties.getAllKeys() [i],
  138. source.properties.getAllValues() [i]);
  139. }
  140. void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) noexcept
  141. {
  142. const ScopedLock sl (lock);
  143. fallbackProperties = fallbackProperties_;
  144. }
  145. XmlElement* PropertySet::createXml (const String& nodeName) const
  146. {
  147. const ScopedLock sl (lock);
  148. XmlElement* const xml = new XmlElement (nodeName);
  149. for (int i = 0; i < properties.getAllKeys().size(); ++i)
  150. {
  151. XmlElement* const e = xml->createNewChildElement ("VALUE");
  152. e->setAttribute ("name", properties.getAllKeys()[i]);
  153. e->setAttribute ("val", properties.getAllValues()[i]);
  154. }
  155. return xml;
  156. }
  157. void PropertySet::restoreFromXml (const XmlElement& xml)
  158. {
  159. const ScopedLock sl (lock);
  160. clear();
  161. forEachXmlChildElementWithTagName (xml, e, "VALUE")
  162. {
  163. if (e->hasAttribute ("name")
  164. && e->hasAttribute ("val"))
  165. {
  166. properties.set (e->getStringAttribute ("name"),
  167. e->getStringAttribute ("val"));
  168. }
  169. }
  170. if (properties.size() > 0)
  171. propertyChanged();
  172. }
  173. void PropertySet::propertyChanged()
  174. {
  175. }