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.

181 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "../../juce_core/basics/juce_StandardHeader.h"
  24. BEGIN_JUCE_NAMESPACE
  25. #include "juce_ApplicationProperties.h"
  26. #include "../gui/components/windows/juce_AlertWindow.h"
  27. #include "../../juce_core/text/juce_LocalisedStrings.h"
  28. //==============================================================================
  29. juce_ImplementSingleton (ApplicationProperties)
  30. //==============================================================================
  31. ApplicationProperties::ApplicationProperties() throw()
  32. : userProps (0),
  33. commonProps (0),
  34. msBeforeSaving (3000),
  35. options (PropertiesFile::storeAsBinary)
  36. {
  37. }
  38. ApplicationProperties::~ApplicationProperties()
  39. {
  40. closeFiles();
  41. clearSingletonInstance();
  42. }
  43. //==============================================================================
  44. void ApplicationProperties::setStorageParameters (const String& applicationName,
  45. const String& fileNameSuffix,
  46. const String& folderName_,
  47. const int millisecondsBeforeSaving,
  48. const int propertiesFileOptions) throw()
  49. {
  50. appName = applicationName;
  51. fileSuffix = fileNameSuffix;
  52. folderName = folderName_;
  53. msBeforeSaving = millisecondsBeforeSaving;
  54. options = propertiesFileOptions;
  55. }
  56. bool ApplicationProperties::testWriteAccess (const bool testUserSettings,
  57. const bool testCommonSettings,
  58. const bool showWarningDialogOnFailure)
  59. {
  60. const bool userOk = (! testUserSettings) || getUserSettings()->save();
  61. const bool commonOk = (! testCommonSettings)
  62. || (userProps != getCommonSettings() && getCommonSettings()->save());
  63. if (! (userOk && commonOk))
  64. {
  65. if (showWarningDialogOnFailure)
  66. {
  67. String filenames;
  68. if (! userOk)
  69. filenames << "\n" << getUserSettings()->getFile().getFullPathName();
  70. if (! commonOk)
  71. {
  72. PropertiesFile* const realCommonProps
  73. = PropertiesFile::createDefaultAppPropertiesFile (appName, fileSuffix, folderName,
  74. true, msBeforeSaving, options);
  75. if (realCommonProps != 0)
  76. {
  77. filenames << "\n" << realCommonProps->getFile().getFullPathName();
  78. delete realCommonProps;
  79. }
  80. }
  81. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  82. appName + TRANS(" - Unable to save settings"),
  83. TRANS("An error occurred when trying to save the application's settings file...\n\nIn order to save and restore its settings, ")
  84. + appName + TRANS(" needs to be able to write to the following files:\n")
  85. + filenames
  86. + TRANS("\n\nMake sure that these files aren't read-only, and that the disk isn't full."));
  87. }
  88. return false;
  89. }
  90. return true;
  91. }
  92. //==============================================================================
  93. PropertiesFile* ApplicationProperties::getUserSettings() throw()
  94. {
  95. if (userProps == 0)
  96. {
  97. // You need to call setStorageParameters() before trying to get hold of the
  98. // properties!
  99. jassert (appName.isNotEmpty());
  100. if (appName.isNotEmpty())
  101. userProps = PropertiesFile::createDefaultAppPropertiesFile (appName, fileSuffix, folderName,
  102. false, msBeforeSaving, options);
  103. if (userProps == 0)
  104. {
  105. // create an emergency properties object to avoid returning a null pointer..
  106. userProps = new PropertiesFile (File::nonexistent, msBeforeSaving, options);
  107. }
  108. }
  109. return userProps;
  110. }
  111. PropertiesFile* ApplicationProperties::getCommonSettings() throw()
  112. {
  113. if (commonProps == 0)
  114. {
  115. // You need to call setStorageParameters() before trying to get hold of the
  116. // properties!
  117. jassert (appName.isNotEmpty());
  118. if (appName.isNotEmpty())
  119. commonProps = PropertiesFile::createDefaultAppPropertiesFile (appName, fileSuffix, folderName,
  120. true, msBeforeSaving, options);
  121. if (commonProps == 0 || ! commonProps->save())
  122. {
  123. delete commonProps;
  124. commonProps = getUserSettings();
  125. }
  126. }
  127. return commonProps;
  128. }
  129. bool ApplicationProperties::saveIfNeeded()
  130. {
  131. return (userProps == 0 || userProps->saveIfNeeded())
  132. && (commonProps == 0 || commonProps == userProps || commonProps->saveIfNeeded());
  133. }
  134. void ApplicationProperties::closeFiles()
  135. {
  136. delete userProps;
  137. if (commonProps != userProps)
  138. delete commonProps;
  139. userProps = commonProps = 0;
  140. }
  141. END_JUCE_NAMESPACE