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.

247 lines
10KB

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