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.

256 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /** Wrapper on a file that stores a list of key/value data pairs.
  22. Useful for storing application settings, etc. See the PropertySet class for
  23. the interfaces that read and write values.
  24. Not designed for very large amounts of data, as it keeps all the values in
  25. memory and writes them out to disk lazily when they are changed.
  26. Because this class derives from ChangeBroadcaster, ChangeListeners can be registered
  27. with it, and these will be signalled when a value changes.
  28. @see PropertySet
  29. @tags{DataStructures}
  30. */
  31. class JUCE_API PropertiesFile : public PropertySet,
  32. public ChangeBroadcaster,
  33. private Timer
  34. {
  35. public:
  36. //==============================================================================
  37. enum StorageFormat
  38. {
  39. storeAsBinary,
  40. storeAsCompressedBinary,
  41. storeAsXML
  42. };
  43. //==============================================================================
  44. /** Structure describing properties file options */
  45. struct JUCE_API Options
  46. {
  47. /** Creates an empty Options structure.
  48. You'll need to fill-in the data members appropriately before using this structure.
  49. */
  50. Options();
  51. /** The name of your application - this is used to help generate the path and filename
  52. at which the properties file will be stored. */
  53. String applicationName;
  54. /** The suffix to use for your properties file.
  55. It doesn't really matter what this is - you may want to use ".settings" or
  56. ".properties" or something. If the suffix includes the prefixing dot (for example
  57. ".settings") then the suffix of applicationName will be replaced with your suffix
  58. ("MyApp.exe" -> "MyApp.settings"). If your filenameSuffix does NOT include the dot,
  59. then the suffix will be appended to the applicationName ("MyApp.exe" ->
  60. "MyApp.exe.settings").
  61. */
  62. String filenameSuffix;
  63. /** The name of a subfolder in which you'd like your properties file to live.
  64. See the getDefaultFile() method for more details about how this is used.
  65. */
  66. String folderName;
  67. /** If you're using properties files on a Mac, you must set this value - failure to
  68. do so will cause a runtime assertion.
  69. The PropertiesFile class always used to put its settings files in "Library/Preferences", but Apple
  70. have changed their advice, and now stipulate that settings should go in "Library/Application Support".
  71. Because older apps would be broken by a silent change in this class's behaviour, you must now
  72. explicitly set the osxLibrarySubFolder value to indicate which path you want to use.
  73. In newer apps, you should always set this to "Application Support" or
  74. "Application Support/YourSubFolderName".
  75. If your app needs to load settings files that were created by older versions of juce and
  76. you want to maintain backwards-compatibility, then you can set this to "Preferences".
  77. But.. for better Apple-compliance, the recommended approach would be to write some code that
  78. finds your old settings files in ~/Library/Preferences, moves them to ~/Library/Application Support,
  79. and then uses the new path.
  80. */
  81. String osxLibrarySubFolder;
  82. /** If true, the file will be created in a location that's shared between users.
  83. The default constructor initialises this value to false.
  84. */
  85. bool commonToAllUsers;
  86. /** If true, this means that property names are matched in a case-insensitive manner.
  87. See the PropertySet constructor for more info.
  88. The default constructor initialises this value to false.
  89. */
  90. bool ignoreCaseOfKeyNames;
  91. /** If set to true, this prevents the file from being written to disk. */
  92. bool doNotSave;
  93. /** If this is zero or greater, then after a value is changed, the object will wait
  94. for this amount of time and then save the file. If this zero, the file will be
  95. written to disk immediately on being changed (which might be slow, as it'll re-write
  96. synchronously each time a value-change method is called). If it is less than zero,
  97. the file won't be saved until save() or saveIfNeeded() are explicitly called.
  98. The default constructor sets this to a reasonable value of a few seconds, so you
  99. only need to change it if you need a special case.
  100. */
  101. int millisecondsBeforeSaving;
  102. /** Specifies whether the file should be written as XML, binary, etc.
  103. The default constructor sets this to storeAsXML, so you only need to set it explicitly
  104. if you want to use a different format.
  105. */
  106. StorageFormat storageFormat;
  107. /** An optional InterprocessLock object that will be used to prevent multiple threads or
  108. processes from writing to the file at the same time. The PropertiesFile will keep a
  109. pointer to this object but will not take ownership of it - the caller is responsible for
  110. making sure that the lock doesn't get deleted before the PropertiesFile has been deleted.
  111. The default constructor initialises this value to nullptr, so you don't need to touch it
  112. unless you want to use a lock.
  113. */
  114. InterProcessLock* processLock;
  115. /** This can be called to suggest a file that should be used, based on the values
  116. in this structure.
  117. So on a Mac, this will return a file called:
  118. ~/Library/[osxLibrarySubFolder]/[folderName]/[applicationName].[filenameSuffix]
  119. On Windows it'll return something like:
  120. C:\\Documents and Settings\\username\\Application Data\\[folderName]\\[applicationName].[filenameSuffix]
  121. On Linux it'll return
  122. ~/[folderName]/[applicationName].[filenameSuffix]
  123. If the folderName variable is empty, it'll use the app name for this (or omit the
  124. folder name on the Mac).
  125. The paths will also vary depending on whether commonToAllUsers is true.
  126. */
  127. File getDefaultFile() const;
  128. };
  129. //==============================================================================
  130. /** Creates a PropertiesFile object.
  131. The file used will be chosen by calling PropertiesFile::Options::getDefaultFile()
  132. for the options provided. To set the file explicitly, use the other constructor.
  133. */
  134. explicit PropertiesFile (const Options& options);
  135. /** Creates a PropertiesFile object.
  136. Unlike the other constructor, this one allows you to explicitly set the file that you
  137. want to be used, rather than using the default one.
  138. */
  139. PropertiesFile (const File& file,
  140. const Options& options);
  141. /** Destructor.
  142. When deleted, the file will first call saveIfNeeded() to flush any changes to disk.
  143. */
  144. ~PropertiesFile() override;
  145. //==============================================================================
  146. /** Returns true if this file was created from a valid (or non-existent) file.
  147. If the file failed to load correctly because it was corrupt or had insufficient
  148. access, this will be false.
  149. */
  150. bool isValidFile() const noexcept { return loadedOk; }
  151. //==============================================================================
  152. /** This will flush all the values to disk if they've changed since the last
  153. time they were saved.
  154. Returns false if it fails to write to the file for some reason (maybe because
  155. it's read-only or the directory doesn't exist or something).
  156. @see save
  157. */
  158. bool saveIfNeeded();
  159. /** This will force a write-to-disk of the current values, regardless of whether
  160. anything has changed since the last save.
  161. Returns false if it fails to write to the file for some reason (maybe because
  162. it's read-only or the directory doesn't exist or something).
  163. @see saveIfNeeded
  164. */
  165. bool save();
  166. /** Returns true if the properties have been altered since the last time they were saved.
  167. The file is flagged as needing to be saved when you change a value, but you can
  168. explicitly set this flag with setNeedsToBeSaved().
  169. */
  170. bool needsToBeSaved() const;
  171. /** Explicitly sets the flag to indicate whether the file needs saving or not.
  172. @see needsToBeSaved
  173. */
  174. void setNeedsToBeSaved (bool needsToBeSaved);
  175. /** Attempts to reload the settings from the file. */
  176. bool reload();
  177. //==============================================================================
  178. /** Returns the file that's being used. */
  179. const File& getFile() const noexcept { return file; }
  180. protected:
  181. /** @internal */
  182. void propertyChanged() override;
  183. private:
  184. //==============================================================================
  185. File file;
  186. Options options;
  187. bool loadedOk = false, needsWriting = false;
  188. using ProcessScopedLock = const std::unique_ptr<InterProcessLock::ScopedLockType>;
  189. InterProcessLock::ScopedLockType* createProcessLock() const;
  190. void timerCallback() override;
  191. bool saveAsXml();
  192. bool saveAsBinary();
  193. bool loadAsXml();
  194. bool loadAsBinary();
  195. bool loadAsBinary (InputStream&);
  196. bool writeToStream (OutputStream&);
  197. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertiesFile)
  198. };
  199. } // namespace juce