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.

168 lines
6.0KB

  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. commonSettingsAreReadOnly (0)
  37. {
  38. }
  39. ApplicationProperties::~ApplicationProperties()
  40. {
  41. closeFiles();
  42. clearSingletonInstance();
  43. }
  44. //==============================================================================
  45. void ApplicationProperties::setStorageParameters (const String& applicationName,
  46. const String& fileNameSuffix,
  47. const String& folderName_,
  48. const int millisecondsBeforeSaving,
  49. const int propertiesFileOptions) throw()
  50. {
  51. appName = applicationName;
  52. fileSuffix = fileNameSuffix;
  53. folderName = folderName_;
  54. msBeforeSaving = millisecondsBeforeSaving;
  55. options = propertiesFileOptions;
  56. }
  57. bool ApplicationProperties::testWriteAccess (const bool testUserSettings,
  58. const bool testCommonSettings,
  59. const bool showWarningDialogOnFailure)
  60. {
  61. const bool userOk = (! testUserSettings) || getUserSettings()->save();
  62. const bool commonOk = (! testCommonSettings) || getCommonSettings (false)->save();
  63. if (! (userOk && commonOk))
  64. {
  65. if (showWarningDialogOnFailure)
  66. {
  67. String filenames;
  68. if (userProps != 0 && ! userOk)
  69. filenames << '\n' << userProps->getFile().getFullPathName();
  70. if (commonProps != 0 && ! commonOk)
  71. filenames << '\n' << commonProps->getFile().getFullPathName();
  72. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  73. appName + TRANS(" - Unable to save settings"),
  74. TRANS("An error occurred when trying to save the application's settings file...\n\nIn order to save and restore its settings, ")
  75. + appName + TRANS(" needs to be able to write to the following files:\n")
  76. + filenames
  77. + TRANS("\n\nMake sure that these files aren't read-only, and that the disk isn't full."));
  78. }
  79. return false;
  80. }
  81. return true;
  82. }
  83. //==============================================================================
  84. void ApplicationProperties::openFiles() throw()
  85. {
  86. // You need to call setStorageParameters() before trying to get hold of the
  87. // properties!
  88. jassert (appName.isNotEmpty());
  89. if (appName.isNotEmpty())
  90. {
  91. if (userProps == 0)
  92. userProps = PropertiesFile::createDefaultAppPropertiesFile (appName, fileSuffix, folderName,
  93. false, msBeforeSaving, options);
  94. if (commonProps == 0)
  95. commonProps = PropertiesFile::createDefaultAppPropertiesFile (appName, fileSuffix, folderName,
  96. true, msBeforeSaving, options);
  97. userProps->setFallbackPropertySet (commonProps);
  98. }
  99. }
  100. PropertiesFile* ApplicationProperties::getUserSettings() throw()
  101. {
  102. if (userProps == 0)
  103. openFiles();
  104. return userProps;
  105. }
  106. PropertiesFile* ApplicationProperties::getCommonSettings (const bool returnUserPropsIfReadOnly) throw()
  107. {
  108. if (commonProps == 0)
  109. openFiles();
  110. if (returnUserPropsIfReadOnly)
  111. {
  112. if (commonSettingsAreReadOnly == 0)
  113. commonSettingsAreReadOnly = commonProps->save() ? -1 : 1;
  114. if (commonSettingsAreReadOnly > 0)
  115. return userProps;
  116. }
  117. return commonProps;
  118. }
  119. bool ApplicationProperties::saveIfNeeded()
  120. {
  121. return (userProps == 0 || userProps->saveIfNeeded())
  122. && (commonProps == 0 || commonProps->saveIfNeeded());
  123. }
  124. void ApplicationProperties::closeFiles()
  125. {
  126. deleteAndZero (userProps);
  127. deleteAndZero (commonProps);
  128. }
  129. END_JUCE_NAMESPACE