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.

346 lines
12KB

  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. namespace PropertyFileConstants
  21. {
  22. static const int magicNumber = (int) ByteOrder::littleEndianInt ("PROP");
  23. static const int magicNumberCompressed = (int) ByteOrder::littleEndianInt ("CPRP");
  24. static const char* const fileTag = "PROPERTIES";
  25. static const char* const valueTag = "VALUE";
  26. static const char* const nameAttribute = "name";
  27. static const char* const valueAttribute = "val";
  28. }
  29. //==============================================================================
  30. PropertiesFile::Options::Options()
  31. : commonToAllUsers (false),
  32. ignoreCaseOfKeyNames (false),
  33. millisecondsBeforeSaving (3000),
  34. storageFormat (PropertiesFile::storeAsXML),
  35. processLock (nullptr)
  36. {
  37. }
  38. File PropertiesFile::Options::getDefaultFile() const
  39. {
  40. // mustn't have illegal characters in this name..
  41. jassert (applicationName == File::createLegalFileName (applicationName));
  42. #if JUCE_MAC || JUCE_IOS
  43. File dir (commonToAllUsers ? "/Library/"
  44. : "~/Library/");
  45. if (osxLibrarySubFolder != "Preferences" && osxLibrarySubFolder != "Application Support")
  46. {
  47. /* The PropertiesFile class always used to put its settings files in "Library/Preferences", but Apple
  48. have changed their advice, and now stipulate that settings should go in "Library/Application Support".
  49. Because older apps would be broken by a silent change in this class's behaviour, you must now
  50. explicitly set the osxLibrarySubFolder value to indicate which path you want to use.
  51. In newer apps, you should always set this to "Application Support".
  52. If your app needs to load settings files that were created by older versions of juce and
  53. you want to maintain backwards-compatibility, then you can set this to "Preferences".
  54. But.. for better Apple-compliance, the recommended approach would be to write some code that
  55. finds your old settings files in ~/Library/Preferences, moves them to ~/Library/Application Support,
  56. and then uses the new path.
  57. */
  58. jassertfalse;
  59. dir = dir.getChildFile ("Application Support");
  60. }
  61. else
  62. {
  63. dir = dir.getChildFile (osxLibrarySubFolder);
  64. }
  65. if (folderName.isNotEmpty())
  66. dir = dir.getChildFile (folderName);
  67. #elif JUCE_LINUX || JUCE_ANDROID
  68. const File dir ((commonToAllUsers ? "/var/" : "~/")
  69. + (folderName.isNotEmpty() ? folderName
  70. : ("." + applicationName)));
  71. #elif JUCE_WINDOWS
  72. File dir (File::getSpecialLocation (commonToAllUsers ? File::commonApplicationDataDirectory
  73. : File::userApplicationDataDirectory));
  74. if (dir == File::nonexistent)
  75. return File::nonexistent;
  76. dir = dir.getChildFile (folderName.isNotEmpty() ? folderName
  77. : applicationName);
  78. #endif
  79. return dir.getChildFile (applicationName)
  80. .withFileExtension (filenameSuffix);
  81. }
  82. //==============================================================================
  83. PropertiesFile::PropertiesFile (const File& file_, const Options& options_)
  84. : PropertySet (options_.ignoreCaseOfKeyNames),
  85. file (file_), options (options_),
  86. loadedOk (false), needsWriting (false)
  87. {
  88. initialise();
  89. }
  90. PropertiesFile::PropertiesFile (const Options& options_)
  91. : PropertySet (options_.ignoreCaseOfKeyNames),
  92. file (options_.getDefaultFile()), options (options_),
  93. loadedOk (false), needsWriting (false)
  94. {
  95. initialise();
  96. }
  97. void PropertiesFile::initialise()
  98. {
  99. // You need to correctly specify just one storage format for the file
  100. ProcessScopedLock pl (createProcessLock());
  101. if (pl != nullptr && ! pl->isLocked())
  102. return; // locking failure..
  103. ScopedPointer<InputStream> fileStream (file.createInputStream());
  104. if (fileStream != nullptr)
  105. {
  106. int magicNumber = fileStream->readInt();
  107. if (magicNumber == PropertyFileConstants::magicNumberCompressed)
  108. {
  109. fileStream = new GZIPDecompressorInputStream (new SubregionStream (fileStream.release(), 4, -1, true), true);
  110. magicNumber = PropertyFileConstants::magicNumber;
  111. }
  112. if (magicNumber == PropertyFileConstants::magicNumber)
  113. {
  114. loadedOk = true;
  115. BufferedInputStream in (fileStream.release(), 2048, true);
  116. int numValues = in.readInt();
  117. while (--numValues >= 0 && ! in.isExhausted())
  118. {
  119. const String key (in.readString());
  120. const String value (in.readString());
  121. jassert (key.isNotEmpty());
  122. if (key.isNotEmpty())
  123. getAllProperties().set (key, value);
  124. }
  125. }
  126. else
  127. {
  128. // Not a binary props file - let's see if it's XML..
  129. fileStream = nullptr;
  130. XmlDocument parser (file);
  131. ScopedPointer<XmlElement> doc (parser.getDocumentElement (true));
  132. if (doc != nullptr && doc->hasTagName (PropertyFileConstants::fileTag))
  133. {
  134. doc = parser.getDocumentElement();
  135. if (doc != nullptr)
  136. {
  137. loadedOk = true;
  138. forEachXmlChildElementWithTagName (*doc, e, PropertyFileConstants::valueTag)
  139. {
  140. const String name (e->getStringAttribute (PropertyFileConstants::nameAttribute));
  141. if (name.isNotEmpty())
  142. {
  143. getAllProperties().set (name,
  144. e->getFirstChildElement() != nullptr
  145. ? e->getFirstChildElement()->createDocument (String::empty, true)
  146. : e->getStringAttribute (PropertyFileConstants::valueAttribute));
  147. }
  148. }
  149. }
  150. else
  151. {
  152. // must be a pretty broken XML file we're trying to parse here,
  153. // or a sign that this object needs an InterProcessLock,
  154. // or just a failure reading the file. This last reason is why
  155. // we don't jassertfalse here.
  156. }
  157. }
  158. }
  159. }
  160. else
  161. {
  162. loadedOk = ! file.exists();
  163. }
  164. }
  165. PropertiesFile::~PropertiesFile()
  166. {
  167. if (! saveIfNeeded())
  168. jassertfalse;
  169. }
  170. InterProcessLock::ScopedLockType* PropertiesFile::createProcessLock() const
  171. {
  172. return options.processLock != nullptr ? new InterProcessLock::ScopedLockType (*options.processLock) : nullptr;
  173. }
  174. bool PropertiesFile::saveIfNeeded()
  175. {
  176. const ScopedLock sl (getLock());
  177. return (! needsWriting) || save();
  178. }
  179. bool PropertiesFile::needsToBeSaved() const
  180. {
  181. const ScopedLock sl (getLock());
  182. return needsWriting;
  183. }
  184. void PropertiesFile::setNeedsToBeSaved (const bool needsToBeSaved_)
  185. {
  186. const ScopedLock sl (getLock());
  187. needsWriting = needsToBeSaved_;
  188. }
  189. bool PropertiesFile::save()
  190. {
  191. const ScopedLock sl (getLock());
  192. stopTimer();
  193. if (file == File::nonexistent
  194. || file.isDirectory()
  195. || ! file.getParentDirectory().createDirectory())
  196. return false;
  197. if (options.storageFormat == storeAsXML)
  198. {
  199. XmlElement doc (PropertyFileConstants::fileTag);
  200. for (int i = 0; i < getAllProperties().size(); ++i)
  201. {
  202. XmlElement* const e = doc.createNewChildElement (PropertyFileConstants::valueTag);
  203. e->setAttribute (PropertyFileConstants::nameAttribute, getAllProperties().getAllKeys() [i]);
  204. // if the value seems to contain xml, store it as such..
  205. XmlElement* const childElement = XmlDocument::parse (getAllProperties().getAllValues() [i]);
  206. if (childElement != nullptr)
  207. e->addChildElement (childElement);
  208. else
  209. e->setAttribute (PropertyFileConstants::valueAttribute,
  210. getAllProperties().getAllValues() [i]);
  211. }
  212. ProcessScopedLock pl (createProcessLock());
  213. if (pl != nullptr && ! pl->isLocked())
  214. return false; // locking failure..
  215. if (doc.writeToFile (file, String::empty))
  216. {
  217. needsWriting = false;
  218. return true;
  219. }
  220. }
  221. else
  222. {
  223. ProcessScopedLock pl (createProcessLock());
  224. if (pl != nullptr && ! pl->isLocked())
  225. return false; // locking failure..
  226. TemporaryFile tempFile (file);
  227. ScopedPointer <OutputStream> out (tempFile.getFile().createOutputStream());
  228. if (out != nullptr)
  229. {
  230. if (options.storageFormat == storeAsCompressedBinary)
  231. {
  232. out->writeInt (PropertyFileConstants::magicNumberCompressed);
  233. out->flush();
  234. out = new GZIPCompressorOutputStream (out.release(), 9, true);
  235. }
  236. else
  237. {
  238. // have you set up the storage option flags correctly?
  239. jassert (options.storageFormat == storeAsBinary);
  240. out->writeInt (PropertyFileConstants::magicNumber);
  241. }
  242. const int numProperties = getAllProperties().size();
  243. out->writeInt (numProperties);
  244. for (int i = 0; i < numProperties; ++i)
  245. {
  246. out->writeString (getAllProperties().getAllKeys() [i]);
  247. out->writeString (getAllProperties().getAllValues() [i]);
  248. }
  249. out = nullptr;
  250. if (tempFile.overwriteTargetFileWithTemporary())
  251. {
  252. needsWriting = false;
  253. return true;
  254. }
  255. }
  256. }
  257. return false;
  258. }
  259. void PropertiesFile::timerCallback()
  260. {
  261. saveIfNeeded();
  262. }
  263. void PropertiesFile::propertyChanged()
  264. {
  265. sendChangeMessage();
  266. needsWriting = true;
  267. if (options.millisecondsBeforeSaving > 0)
  268. startTimer (options.millisecondsBeforeSaving);
  269. else if (options.millisecondsBeforeSaving == 0)
  270. saveIfNeeded();
  271. }
  272. END_JUCE_NAMESPACE