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.

226 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. PropertySet::PropertySet (const bool ignoreCaseOfKeyNames)
  21. : properties (ignoreCaseOfKeyNames),
  22. fallbackProperties (nullptr),
  23. ignoreCaseOfKeys (ignoreCaseOfKeyNames)
  24. {
  25. }
  26. PropertySet::PropertySet (const PropertySet& other)
  27. : properties (other.properties),
  28. fallbackProperties (other.fallbackProperties),
  29. ignoreCaseOfKeys (other.ignoreCaseOfKeys)
  30. {
  31. }
  32. PropertySet& PropertySet::operator= (const PropertySet& other)
  33. {
  34. properties = other.properties;
  35. fallbackProperties = other.fallbackProperties;
  36. ignoreCaseOfKeys = other.ignoreCaseOfKeys;
  37. propertyChanged();
  38. return *this;
  39. }
  40. PropertySet::~PropertySet()
  41. {
  42. }
  43. void PropertySet::clear()
  44. {
  45. const ScopedLock sl (lock);
  46. if (properties.size() > 0)
  47. {
  48. properties.clear();
  49. propertyChanged();
  50. }
  51. }
  52. String PropertySet::getValue (const String& keyName,
  53. const String& defaultValue) const noexcept
  54. {
  55. const ScopedLock sl (lock);
  56. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  57. if (index >= 0)
  58. return properties.getAllValues() [index];
  59. return fallbackProperties != nullptr ? fallbackProperties->getValue (keyName, defaultValue)
  60. : defaultValue;
  61. }
  62. int PropertySet::getIntValue (const String& keyName,
  63. const int defaultValue) const noexcept
  64. {
  65. const ScopedLock sl (lock);
  66. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  67. if (index >= 0)
  68. return properties.getAllValues() [index].getIntValue();
  69. return fallbackProperties != nullptr ? fallbackProperties->getIntValue (keyName, defaultValue)
  70. : defaultValue;
  71. }
  72. double PropertySet::getDoubleValue (const String& keyName,
  73. 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 (const String& keyName,
  83. const bool defaultValue) const noexcept
  84. {
  85. const ScopedLock sl (lock);
  86. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  87. if (index >= 0)
  88. return properties.getAllValues() [index].getIntValue() != 0;
  89. return fallbackProperties != nullptr ? fallbackProperties->getBoolValue (keyName, defaultValue)
  90. : defaultValue;
  91. }
  92. XmlElement* PropertySet::getXmlValue (const String& keyName) const
  93. {
  94. return XmlDocument::parse (getValue (keyName));
  95. }
  96. void PropertySet::setValue (const String& keyName, const var& v)
  97. {
  98. jassert (keyName.isNotEmpty()); // shouldn't use an empty key name!
  99. if (keyName.isNotEmpty())
  100. {
  101. const String value (v.toString());
  102. const ScopedLock sl (lock);
  103. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  104. if (index < 0 || properties.getAllValues() [index] != value)
  105. {
  106. properties.set (keyName, value);
  107. propertyChanged();
  108. }
  109. }
  110. }
  111. void PropertySet::removeValue (const String& keyName)
  112. {
  113. if (keyName.isNotEmpty())
  114. {
  115. const ScopedLock sl (lock);
  116. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  117. if (index >= 0)
  118. {
  119. properties.remove (keyName);
  120. propertyChanged();
  121. }
  122. }
  123. }
  124. void PropertySet::setValue (const String& keyName, const XmlElement* const xml)
  125. {
  126. setValue (keyName, xml == nullptr ? var::null
  127. : var (xml->createDocument (String::empty, true)));
  128. }
  129. bool PropertySet::containsKey (const String& keyName) const noexcept
  130. {
  131. const ScopedLock sl (lock);
  132. return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
  133. }
  134. void PropertySet::addAllPropertiesFrom (const PropertySet& source)
  135. {
  136. const ScopedLock sl (source.getLock());
  137. for (int i = 0; i < source.properties.size(); ++i)
  138. setValue (source.properties.getAllKeys() [i],
  139. source.properties.getAllValues() [i]);
  140. }
  141. void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) noexcept
  142. {
  143. const ScopedLock sl (lock);
  144. fallbackProperties = fallbackProperties_;
  145. }
  146. XmlElement* PropertySet::createXml (const String& nodeName) const
  147. {
  148. const ScopedLock sl (lock);
  149. XmlElement* const xml = new XmlElement (nodeName);
  150. for (int i = 0; i < properties.getAllKeys().size(); ++i)
  151. {
  152. XmlElement* const e = xml->createNewChildElement ("VALUE");
  153. e->setAttribute ("name", properties.getAllKeys()[i]);
  154. e->setAttribute ("val", properties.getAllValues()[i]);
  155. }
  156. return xml;
  157. }
  158. void PropertySet::restoreFromXml (const XmlElement& xml)
  159. {
  160. const ScopedLock sl (lock);
  161. clear();
  162. forEachXmlChildElementWithTagName (xml, e, "VALUE")
  163. {
  164. if (e->hasAttribute ("name")
  165. && e->hasAttribute ("val"))
  166. {
  167. properties.set (e->getStringAttribute ("name"),
  168. e->getStringAttribute ("val"));
  169. }
  170. }
  171. if (properties.size() > 0)
  172. propertyChanged();
  173. }
  174. void PropertySet::propertyChanged()
  175. {
  176. }
  177. END_JUCE_NAMESPACE