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.

241 lines
7.1KB

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