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.

160 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. #include "../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_ApplicationProperties.h"
  21. #include "../gui/components/windows/juce_AlertWindow.h"
  22. #include "../text/juce_LocalisedStrings.h"
  23. //==============================================================================
  24. juce_ImplementSingleton (ApplicationProperties)
  25. //==============================================================================
  26. ApplicationProperties::ApplicationProperties() throw()
  27. : msBeforeSaving (3000),
  28. options (PropertiesFile::storeAsBinary),
  29. commonSettingsAreReadOnly (0)
  30. {
  31. }
  32. ApplicationProperties::~ApplicationProperties()
  33. {
  34. closeFiles();
  35. clearSingletonInstance();
  36. }
  37. //==============================================================================
  38. void ApplicationProperties::setStorageParameters (const String& applicationName,
  39. const String& fileNameSuffix,
  40. const String& folderName_,
  41. const int millisecondsBeforeSaving,
  42. const int propertiesFileOptions) throw()
  43. {
  44. appName = applicationName;
  45. fileSuffix = fileNameSuffix;
  46. folderName = folderName_;
  47. msBeforeSaving = millisecondsBeforeSaving;
  48. options = propertiesFileOptions;
  49. }
  50. bool ApplicationProperties::testWriteAccess (const bool testUserSettings,
  51. const bool testCommonSettings,
  52. const bool showWarningDialogOnFailure)
  53. {
  54. const bool userOk = (! testUserSettings) || getUserSettings()->save();
  55. const bool commonOk = (! testCommonSettings) || getCommonSettings (false)->save();
  56. if (! (userOk && commonOk))
  57. {
  58. if (showWarningDialogOnFailure)
  59. {
  60. String filenames;
  61. if (userProps != 0 && ! userOk)
  62. filenames << '\n' << userProps->getFile().getFullPathName();
  63. if (commonProps != 0 && ! commonOk)
  64. filenames << '\n' << commonProps->getFile().getFullPathName();
  65. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  66. appName + TRANS(" - Unable to save settings"),
  67. TRANS("An error occurred when trying to save the application's settings file...\n\nIn order to save and restore its settings, ")
  68. + appName + TRANS(" needs to be able to write to the following files:\n")
  69. + filenames
  70. + TRANS("\n\nMake sure that these files aren't read-only, and that the disk isn't full."));
  71. }
  72. return false;
  73. }
  74. return true;
  75. }
  76. //==============================================================================
  77. void ApplicationProperties::openFiles() throw()
  78. {
  79. // You need to call setStorageParameters() before trying to get hold of the
  80. // properties!
  81. jassert (appName.isNotEmpty());
  82. if (appName.isNotEmpty())
  83. {
  84. if (userProps == 0)
  85. userProps = PropertiesFile::createDefaultAppPropertiesFile (appName, fileSuffix, folderName,
  86. false, msBeforeSaving, options);
  87. if (commonProps == 0)
  88. commonProps = PropertiesFile::createDefaultAppPropertiesFile (appName, fileSuffix, folderName,
  89. true, msBeforeSaving, options);
  90. userProps->setFallbackPropertySet (commonProps);
  91. }
  92. }
  93. PropertiesFile* ApplicationProperties::getUserSettings() throw()
  94. {
  95. if (userProps == 0)
  96. openFiles();
  97. return userProps;
  98. }
  99. PropertiesFile* ApplicationProperties::getCommonSettings (const bool returnUserPropsIfReadOnly) throw()
  100. {
  101. if (commonProps == 0)
  102. openFiles();
  103. if (returnUserPropsIfReadOnly)
  104. {
  105. if (commonSettingsAreReadOnly == 0)
  106. commonSettingsAreReadOnly = commonProps->save() ? -1 : 1;
  107. if (commonSettingsAreReadOnly > 0)
  108. return userProps;
  109. }
  110. return commonProps;
  111. }
  112. bool ApplicationProperties::saveIfNeeded()
  113. {
  114. return (userProps == 0 || userProps->saveIfNeeded())
  115. && (commonProps == 0 || commonProps->saveIfNeeded());
  116. }
  117. void ApplicationProperties::closeFiles()
  118. {
  119. userProps = 0;
  120. commonProps = 0;
  121. }
  122. END_JUCE_NAMESPACE