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.

357 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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 (filenameSuffix.startsWithChar (L'.')
  79. ? dir.getChildFile (applicationName).withFileExtension (filenameSuffix)
  80. : dir.getChildFile (applicationName + "." + filenameSuffix));
  81. }
  82. //==============================================================================
  83. PropertiesFile::PropertiesFile (const File& f, const Options& o)
  84. : PropertySet (o.ignoreCaseOfKeyNames),
  85. file (f), options (o),
  86. loadedOk (false), needsWriting (false)
  87. {
  88. reload();
  89. }
  90. PropertiesFile::PropertiesFile (const Options& o)
  91. : PropertySet (o.ignoreCaseOfKeyNames),
  92. file (o.getDefaultFile()), options (o),
  93. loadedOk (false), needsWriting (false)
  94. {
  95. reload();
  96. }
  97. bool PropertiesFile::reload()
  98. {
  99. ProcessScopedLock pl (createProcessLock());
  100. if (pl != nullptr && ! pl->isLocked())
  101. return false; // locking failure..
  102. loadedOk = (! file.exists()) || loadAsBinary() || loadAsXml();
  103. return loadedOk;
  104. }
  105. PropertiesFile::~PropertiesFile()
  106. {
  107. saveIfNeeded();
  108. }
  109. InterProcessLock::ScopedLockType* PropertiesFile::createProcessLock() const
  110. {
  111. return options.processLock != nullptr ? new InterProcessLock::ScopedLockType (*options.processLock) : nullptr;
  112. }
  113. bool PropertiesFile::saveIfNeeded()
  114. {
  115. const ScopedLock sl (getLock());
  116. return (! needsWriting) || save();
  117. }
  118. bool PropertiesFile::needsToBeSaved() const
  119. {
  120. const ScopedLock sl (getLock());
  121. return needsWriting;
  122. }
  123. void PropertiesFile::setNeedsToBeSaved (const bool needsToBeSaved_)
  124. {
  125. const ScopedLock sl (getLock());
  126. needsWriting = needsToBeSaved_;
  127. }
  128. bool PropertiesFile::save()
  129. {
  130. const ScopedLock sl (getLock());
  131. stopTimer();
  132. if (options.doNotSave
  133. || file == File()
  134. || file.isDirectory()
  135. || ! file.getParentDirectory().createDirectory())
  136. return false;
  137. if (options.storageFormat == storeAsXML)
  138. return saveAsXml();
  139. return saveAsBinary();
  140. }
  141. bool PropertiesFile::loadAsXml()
  142. {
  143. XmlDocument parser (file);
  144. ScopedPointer<XmlElement> doc (parser.getDocumentElement (true));
  145. if (doc != nullptr && doc->hasTagName (PropertyFileConstants::fileTag))
  146. {
  147. doc = parser.getDocumentElement();
  148. if (doc != nullptr)
  149. {
  150. forEachXmlChildElementWithTagName (*doc, e, PropertyFileConstants::valueTag)
  151. {
  152. const String name (e->getStringAttribute (PropertyFileConstants::nameAttribute));
  153. if (name.isNotEmpty())
  154. {
  155. getAllProperties().set (name,
  156. e->getFirstChildElement() != nullptr
  157. ? e->getFirstChildElement()->createDocument ("", true)
  158. : e->getStringAttribute (PropertyFileConstants::valueAttribute));
  159. }
  160. }
  161. return true;
  162. }
  163. // must be a pretty broken XML file we're trying to parse here,
  164. // or a sign that this object needs an InterProcessLock,
  165. // or just a failure reading the file. This last reason is why
  166. // we don't jassertfalse here.
  167. }
  168. return false;
  169. }
  170. bool PropertiesFile::saveAsXml()
  171. {
  172. XmlElement doc (PropertyFileConstants::fileTag);
  173. const StringPairArray& props = getAllProperties();
  174. for (int i = 0; i < props.size(); ++i)
  175. {
  176. XmlElement* const e = doc.createNewChildElement (PropertyFileConstants::valueTag);
  177. e->setAttribute (PropertyFileConstants::nameAttribute, props.getAllKeys() [i]);
  178. // if the value seems to contain xml, store it as such..
  179. if (XmlElement* const childElement = XmlDocument::parse (props.getAllValues() [i]))
  180. e->addChildElement (childElement);
  181. else
  182. e->setAttribute (PropertyFileConstants::valueAttribute, props.getAllValues() [i]);
  183. }
  184. ProcessScopedLock pl (createProcessLock());
  185. if (pl != nullptr && ! pl->isLocked())
  186. return false; // locking failure..
  187. if (doc.writeToFile (file, String()))
  188. {
  189. needsWriting = false;
  190. return true;
  191. }
  192. return false;
  193. }
  194. bool PropertiesFile::loadAsBinary()
  195. {
  196. FileInputStream fileStream (file);
  197. if (fileStream.openedOk())
  198. {
  199. const int magicNumber = fileStream.readInt();
  200. if (magicNumber == PropertyFileConstants::magicNumberCompressed)
  201. {
  202. SubregionStream subStream (&fileStream, 4, -1, false);
  203. GZIPDecompressorInputStream gzip (subStream);
  204. return loadAsBinary (gzip);
  205. }
  206. if (magicNumber == PropertyFileConstants::magicNumber)
  207. return loadAsBinary (fileStream);
  208. }
  209. return false;
  210. }
  211. bool PropertiesFile::loadAsBinary (InputStream& input)
  212. {
  213. BufferedInputStream in (input, 2048);
  214. int numValues = in.readInt();
  215. while (--numValues >= 0 && ! in.isExhausted())
  216. {
  217. const String key (in.readString());
  218. const String value (in.readString());
  219. jassert (key.isNotEmpty());
  220. if (key.isNotEmpty())
  221. getAllProperties().set (key, value);
  222. }
  223. return true;
  224. }
  225. bool PropertiesFile::saveAsBinary()
  226. {
  227. ProcessScopedLock pl (createProcessLock());
  228. if (pl != nullptr && ! pl->isLocked())
  229. return false; // locking failure..
  230. TemporaryFile tempFile (file);
  231. ScopedPointer<OutputStream> out (tempFile.getFile().createOutputStream());
  232. if (out != nullptr)
  233. {
  234. if (options.storageFormat == storeAsCompressedBinary)
  235. {
  236. out->writeInt (PropertyFileConstants::magicNumberCompressed);
  237. out->flush();
  238. out = new GZIPCompressorOutputStream (out.release(), 9, true);
  239. }
  240. else
  241. {
  242. // have you set up the storage option flags correctly?
  243. jassert (options.storageFormat == storeAsBinary);
  244. out->writeInt (PropertyFileConstants::magicNumber);
  245. }
  246. const StringPairArray& props = getAllProperties();
  247. const int numProperties = props.size();
  248. const StringArray& keys = props.getAllKeys();
  249. const StringArray& values = props.getAllValues();
  250. out->writeInt (numProperties);
  251. for (int i = 0; i < numProperties; ++i)
  252. {
  253. out->writeString (keys[i]);
  254. out->writeString (values[i]);
  255. }
  256. out = nullptr;
  257. if (tempFile.overwriteTargetFileWithTemporary())
  258. {
  259. needsWriting = false;
  260. return true;
  261. }
  262. }
  263. return false;
  264. }
  265. void PropertiesFile::timerCallback()
  266. {
  267. saveIfNeeded();
  268. }
  269. void PropertiesFile::propertyChanged()
  270. {
  271. sendChangeMessage();
  272. needsWriting = true;
  273. if (options.millisecondsBeforeSaving > 0)
  274. startTimer (options.millisecondsBeforeSaving);
  275. else if (options.millisecondsBeforeSaving == 0)
  276. saveIfNeeded();
  277. }