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.

221 lines
6.7KB

  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. PropertySet::PropertySet (const bool ignoreCaseOfKeyNames)
  19. : properties (ignoreCaseOfKeyNames),
  20. fallbackProperties (nullptr),
  21. ignoreCaseOfKeys (ignoreCaseOfKeyNames)
  22. {
  23. }
  24. PropertySet::PropertySet (const PropertySet& other)
  25. : properties (other.properties),
  26. fallbackProperties (other.fallbackProperties),
  27. ignoreCaseOfKeys (other.ignoreCaseOfKeys)
  28. {
  29. }
  30. PropertySet& PropertySet::operator= (const PropertySet& other)
  31. {
  32. properties = other.properties;
  33. fallbackProperties = other.fallbackProperties;
  34. ignoreCaseOfKeys = other.ignoreCaseOfKeys;
  35. propertyChanged();
  36. return *this;
  37. }
  38. PropertySet::~PropertySet()
  39. {
  40. }
  41. void PropertySet::clear()
  42. {
  43. const ScopedLock sl (lock);
  44. if (properties.size() > 0)
  45. {
  46. properties.clear();
  47. propertyChanged();
  48. }
  49. }
  50. String PropertySet::getValue (const String& keyName,
  51. const String& defaultValue) const noexcept
  52. {
  53. const ScopedLock sl (lock);
  54. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  55. if (index >= 0)
  56. return properties.getAllValues() [index];
  57. return fallbackProperties != nullptr ? fallbackProperties->getValue (keyName, defaultValue)
  58. : defaultValue;
  59. }
  60. int PropertySet::getIntValue (const String& keyName,
  61. const int defaultValue) const noexcept
  62. {
  63. const ScopedLock sl (lock);
  64. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  65. if (index >= 0)
  66. return properties.getAllValues() [index].getIntValue();
  67. return fallbackProperties != nullptr ? fallbackProperties->getIntValue (keyName, defaultValue)
  68. : defaultValue;
  69. }
  70. double PropertySet::getDoubleValue (const String& keyName,
  71. const double defaultValue) const noexcept
  72. {
  73. const ScopedLock sl (lock);
  74. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  75. if (index >= 0)
  76. return properties.getAllValues()[index].getDoubleValue();
  77. return fallbackProperties != nullptr ? fallbackProperties->getDoubleValue (keyName, defaultValue)
  78. : defaultValue;
  79. }
  80. bool PropertySet::getBoolValue (const String& keyName,
  81. const bool defaultValue) const noexcept
  82. {
  83. const ScopedLock sl (lock);
  84. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  85. if (index >= 0)
  86. return properties.getAllValues() [index].getIntValue() != 0;
  87. return fallbackProperties != nullptr ? fallbackProperties->getBoolValue (keyName, defaultValue)
  88. : defaultValue;
  89. }
  90. XmlElement* PropertySet::getXmlValue (const String& keyName) const
  91. {
  92. return XmlDocument::parse (getValue (keyName));
  93. }
  94. void PropertySet::setValue (const String& keyName, const var& v)
  95. {
  96. jassert (keyName.isNotEmpty()); // shouldn't use an empty key name!
  97. if (keyName.isNotEmpty())
  98. {
  99. const String value (v.toString());
  100. const ScopedLock sl (lock);
  101. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  102. if (index < 0 || properties.getAllValues() [index] != value)
  103. {
  104. properties.set (keyName, value);
  105. propertyChanged();
  106. }
  107. }
  108. }
  109. void PropertySet::removeValue (const String& keyName)
  110. {
  111. if (keyName.isNotEmpty())
  112. {
  113. const ScopedLock sl (lock);
  114. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  115. if (index >= 0)
  116. {
  117. properties.remove (keyName);
  118. propertyChanged();
  119. }
  120. }
  121. }
  122. void PropertySet::setValue (const String& keyName, const XmlElement* const xml)
  123. {
  124. setValue (keyName, xml == nullptr ? var::null
  125. : var (xml->createDocument (String::empty, true)));
  126. }
  127. bool PropertySet::containsKey (const String& keyName) const noexcept
  128. {
  129. const ScopedLock sl (lock);
  130. return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
  131. }
  132. void PropertySet::addAllPropertiesFrom (const PropertySet& source)
  133. {
  134. const ScopedLock sl (source.getLock());
  135. for (int i = 0; i < source.properties.size(); ++i)
  136. setValue (source.properties.getAllKeys() [i],
  137. source.properties.getAllValues() [i]);
  138. }
  139. void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) noexcept
  140. {
  141. const ScopedLock sl (lock);
  142. fallbackProperties = fallbackProperties_;
  143. }
  144. XmlElement* PropertySet::createXml (const String& nodeName) const
  145. {
  146. const ScopedLock sl (lock);
  147. XmlElement* const xml = new XmlElement (nodeName);
  148. for (int i = 0; i < properties.getAllKeys().size(); ++i)
  149. {
  150. XmlElement* const e = xml->createNewChildElement ("VALUE");
  151. e->setAttribute ("name", properties.getAllKeys()[i]);
  152. e->setAttribute ("val", properties.getAllValues()[i]);
  153. }
  154. return xml;
  155. }
  156. void PropertySet::restoreFromXml (const XmlElement& xml)
  157. {
  158. const ScopedLock sl (lock);
  159. clear();
  160. forEachXmlChildElementWithTagName (xml, e, "VALUE")
  161. {
  162. if (e->hasAttribute ("name")
  163. && e->hasAttribute ("val"))
  164. {
  165. properties.set (e->getStringAttribute ("name"),
  166. e->getStringAttribute ("val"));
  167. }
  168. }
  169. if (properties.size() > 0)
  170. propertyChanged();
  171. }
  172. void PropertySet::propertyChanged()
  173. {
  174. }