Audio plugin host https://kx.studio/carla
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.

356 lines
11KB

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