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.

248 lines
10KB

  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. #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 members 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" or
  67. "Application Support/YourSubFolderName".
  68. If your app needs to load settings files that were created by older versions of juce and
  69. you want to maintain backwards-compatibility, then you can set this to "Preferences".
  70. But.. for better Apple-compliance, the recommended approach would be to write some code that
  71. finds your old settings files in ~/Library/Preferences, moves them to ~/Library/Application Support,
  72. and then uses the new path.
  73. */
  74. String osxLibrarySubFolder;
  75. /** If true, the file will be created in a location that's shared between users.
  76. The default constructor initialises this value to false.
  77. */
  78. bool commonToAllUsers;
  79. /** If true, this means that property names are matched in a case-insensitive manner.
  80. See the PropertySet constructor for more info.
  81. The default constructor initialises this value to false.
  82. */
  83. bool ignoreCaseOfKeyNames;
  84. /** If set to true, this prevents the file from being written to disk. */
  85. bool doNotSave;
  86. /** If this is zero or greater, then after a value is changed, the object will wait
  87. for this amount of time and then save the file. If this zero, the file will be
  88. written to disk immediately on being changed (which might be slow, as it'll re-write
  89. synchronously each time a value-change method is called). If it is less than zero,
  90. the file won't be saved until save() or saveIfNeeded() are explicitly called.
  91. The default constructor sets this to a reasonable value of a few seconds, so you
  92. only need to change it if you need a special case.
  93. */
  94. int millisecondsBeforeSaving;
  95. /** Specifies whether the file should be written as XML, binary, etc.
  96. The default constructor sets this to storeAsXML, so you only need to set it explicitly
  97. if you want to use a different format.
  98. */
  99. StorageFormat storageFormat;
  100. /** An optional InterprocessLock object that will be used to prevent multiple threads or
  101. processes from writing to the file at the same time. The PropertiesFile will keep a
  102. pointer to this object but will not take ownership of it - the caller is responsible for
  103. making sure that the lock doesn't get deleted before the PropertiesFile has been deleted.
  104. The default constructor initialises this value to nullptr, so you don't need to touch it
  105. unless you want to use a lock.
  106. */
  107. InterProcessLock* processLock;
  108. /** This can be called to suggest a file that should be used, based on the values
  109. in this structure.
  110. So on a Mac, this will return a file called:
  111. ~/Library/[osxLibrarySubFolder]/[folderName]/[applicationName].[filenameSuffix]
  112. On Windows it'll return something like:
  113. C:\\Documents and Settings\\username\\Application Data\\[folderName]\\[applicationName].[filenameSuffix]
  114. On Linux it'll return
  115. ~/[folderName]/[applicationName].[filenameSuffix]
  116. If the folderName variable is empty, it'll use the app name for this (or omit the
  117. folder name on the Mac).
  118. The paths will also vary depending on whether commonToAllUsers is true.
  119. */
  120. File getDefaultFile() const;
  121. };
  122. //==============================================================================
  123. /** Creates a PropertiesFile object.
  124. The file used will be chosen by calling PropertiesFile::Options::getDefaultFile()
  125. for the options provided. To set the file explicitly, use the other constructor.
  126. */
  127. explicit PropertiesFile (const Options& options);
  128. /** Creates a PropertiesFile object.
  129. Unlike the other constructor, this one allows you to explicitly set the file that you
  130. want to be used, rather than using the default one.
  131. */
  132. PropertiesFile (const File& file,
  133. const Options& options);
  134. /** Destructor.
  135. When deleted, the file will first call saveIfNeeded() to flush any changes to disk.
  136. */
  137. ~PropertiesFile();
  138. //==============================================================================
  139. /** Returns true if this file was created from a valid (or non-existent) file.
  140. If the file failed to load correctly because it was corrupt or had insufficient
  141. access, this will be false.
  142. */
  143. bool isValidFile() const noexcept { return loadedOk; }
  144. //==============================================================================
  145. /** This will flush all the values to disk if they've changed since the last
  146. time they were saved.
  147. Returns false if it fails to write to the file for some reason (maybe because
  148. it's read-only or the directory doesn't exist or something).
  149. @see save
  150. */
  151. bool saveIfNeeded();
  152. /** This will force a write-to-disk of the current values, regardless of whether
  153. anything has changed since the last save.
  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 saveIfNeeded
  157. */
  158. bool save();
  159. /** Returns true if the properties have been altered since the last time they were saved.
  160. The file is flagged as needing to be saved when you change a value, but you can
  161. explicitly set this flag with setNeedsToBeSaved().
  162. */
  163. bool needsToBeSaved() const;
  164. /** Explicitly sets the flag to indicate whether the file needs saving or not.
  165. @see needsToBeSaved
  166. */
  167. void setNeedsToBeSaved (bool needsToBeSaved);
  168. /** Attempts to reload the settings from the file. */
  169. bool reload();
  170. //==============================================================================
  171. /** Returns the file that's being used. */
  172. const File& getFile() const noexcept { return file; }
  173. protected:
  174. /** @internal */
  175. void propertyChanged() override;
  176. private:
  177. //==============================================================================
  178. File file;
  179. Options options;
  180. bool loadedOk, needsWriting;
  181. typedef const ScopedPointer<InterProcessLock::ScopedLockType> ProcessScopedLock;
  182. InterProcessLock::ScopedLockType* createProcessLock() const;
  183. void timerCallback() override;
  184. bool saveAsXml();
  185. bool saveAsBinary();
  186. bool loadAsXml();
  187. bool loadAsBinary();
  188. bool loadAsBinary (InputStream&);
  189. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertiesFile)
  190. };
  191. #endif // JUCE_PROPERTIESFILE_H_INCLUDED