The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

238 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_PROPERTIESFILE_JUCEHEADER__
  19. #define __JUCE_PROPERTIESFILE_JUCEHEADER__
  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. */
  30. class JUCE_API PropertiesFile : public PropertySet,
  31. public ChangeBroadcaster,
  32. private Timer
  33. {
  34. public:
  35. //==============================================================================
  36. enum StorageFormat
  37. {
  38. storeAsBinary,
  39. storeAsCompressedBinary,
  40. storeAsXML
  41. };
  42. //==============================================================================
  43. struct JUCE_API Options
  44. {
  45. /** Creates an empty Options structure.
  46. You'll need to fill-in the data memebers appropriately before using this structure.
  47. */
  48. Options();
  49. /** The name of your application - this is used to help generate the path and filename
  50. at which the properties file will be stored. */
  51. String applicationName;
  52. /** The suffix to use for your properties file.
  53. It doesn't really matter what this is - you may want to use ".settings" or
  54. ".properties" or something.
  55. */
  56. String filenameSuffix;
  57. /** The name of a subfolder in which you'd like your properties file to live.
  58. See the getDefaultFile() method for more details about how this is used.
  59. */
  60. String folderName;
  61. /** If you're using properties files on a Mac, you must set this value - failure to
  62. do so will cause a runtime assertion.
  63. The PropertiesFile class always used to put its settings files in "Library/Preferences", but Apple
  64. have changed their advice, and now stipulate that settings should go in "Library/Application Support".
  65. Because older apps would be broken by a silent change in this class's behaviour, you must now
  66. explicitly set the osxLibrarySubFolder value to indicate which path you want to use.
  67. In newer apps, you should always set this to "Application Support".
  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 this is zero or greater, then after a value is changed, the object will wait
  85. for this amount of time and then save the file. If this zero, the file will be
  86. written to disk immediately on being changed (which might be slow, as it'll re-write
  87. synchronously each time a value-change method is called). If it is less than zero,
  88. the file won't be saved until save() or saveIfNeeded() are explicitly called.
  89. The default constructor sets this to a reasonable value of a few seconds, so you
  90. only need to change it if you need a special case.
  91. */
  92. int millisecondsBeforeSaving;
  93. /** Specifies whether the file should be written as XML, binary, etc.
  94. The default constructor sets this to storeAsXML, so you only need to set it explicitly
  95. if you want to use a different format.
  96. */
  97. StorageFormat storageFormat;
  98. /** An optional InterprocessLock object that will be used to prevent multiple threads or
  99. processes from writing to the file at the same time. The PropertiesFile will keep a
  100. pointer to this object but will not take ownership of it - the caller is responsible for
  101. making sure that the lock doesn't get deleted before the PropertiesFile has been deleted.
  102. The default constructor initialises this value to nullptr, so you don't need to touch it
  103. unless you want to use a lock.
  104. */
  105. InterProcessLock* processLock;
  106. /** This can be called to suggest a file that should be used, based on the values
  107. in this structure.
  108. So on a Mac, this will return a file called:
  109. ~/Library/[osxLibrarySubFolder]/[folderName]/[applicationName].[filenameSuffix]
  110. On Windows it'll return something like:
  111. C:\\Documents and Settings\\username\\Application Data\\[folderName]\\[applicationName].[filenameSuffix]
  112. On Linux it'll return
  113. ~/.[folderName]/[applicationName].[filenameSuffix]
  114. If the folderName variable is empty, it'll use the app name for this (or omit the
  115. folder name on the Mac).
  116. The paths will also vary depending on whether commonToAllUsers is true.
  117. */
  118. File getDefaultFile() const;
  119. };
  120. //==============================================================================
  121. /** Creates a PropertiesFile object.
  122. The file used will be chosen by calling PropertiesFile::Options::getDefaultFile()
  123. for the options provided. To set the file explicitly, use the other constructor.
  124. */
  125. explicit PropertiesFile (const Options& options);
  126. /** Creates a PropertiesFile object.
  127. Unlike the other constructor, this one allows you to explicitly set the file that you
  128. want to be used, rather than using the default one.
  129. */
  130. PropertiesFile (const File& file,
  131. const Options& options);
  132. /** Destructor.
  133. When deleted, the file will first call saveIfNeeded() to flush any changes to disk.
  134. */
  135. ~PropertiesFile();
  136. //==============================================================================
  137. /** Returns true if this file was created from a valid (or non-existent) file.
  138. If the file failed to load correctly because it was corrupt or had insufficient
  139. access, this will be false.
  140. */
  141. bool isValidFile() const noexcept { return loadedOk; }
  142. //==============================================================================
  143. /** This will flush all the values to disk if they've changed since the last
  144. time they were saved.
  145. Returns false if it fails to write to the file for some reason (maybe because
  146. it's read-only or the directory doesn't exist or something).
  147. @see save
  148. */
  149. bool saveIfNeeded();
  150. /** This will force a write-to-disk of the current values, regardless of whether
  151. anything has changed since the last save.
  152. Returns false if it fails to write to the file for some reason (maybe because
  153. it's read-only or the directory doesn't exist or something).
  154. @see saveIfNeeded
  155. */
  156. bool save();
  157. /** Returns true if the properties have been altered since the last time they were saved.
  158. The file is flagged as needing to be saved when you change a value, but you can
  159. explicitly set this flag with setNeedsToBeSaved().
  160. */
  161. bool needsToBeSaved() const;
  162. /** Explicitly sets the flag to indicate whether the file needs saving or not.
  163. @see needsToBeSaved
  164. */
  165. void setNeedsToBeSaved (bool needsToBeSaved);
  166. //==============================================================================
  167. /** Returns the file that's being used. */
  168. File getFile() const { return file; }
  169. protected:
  170. /** @internal */
  171. virtual void propertyChanged();
  172. private:
  173. //==============================================================================
  174. File file;
  175. Options options;
  176. bool loadedOk, needsWriting;
  177. typedef const ScopedPointer<InterProcessLock::ScopedLockType> ProcessScopedLock;
  178. InterProcessLock::ScopedLockType* createProcessLock() const;
  179. void timerCallback();
  180. void initialise();
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PropertiesFile);
  182. };
  183. #endif // __JUCE_PROPERTIESFILE_JUCEHEADER__