juce_ApplicationProperties.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. ApplicationProperties::ApplicationProperties()
  18. : commonSettingsAreReadOnly (0)
  19. {
  20. }
  21. ApplicationProperties::~ApplicationProperties()
  22. {
  23. closeFiles();
  24. }
  25. //==============================================================================
  26. void ApplicationProperties::setStorageParameters (const PropertiesFile::Options& newOptions)
  27. {
  28. options = newOptions;
  29. }
  30. //==============================================================================
  31. void ApplicationProperties::openFiles()
  32. {
  33. // You need to call setStorageParameters() before trying to get hold of the properties!
  34. jassert (options.applicationName.isNotEmpty());
  35. if (options.applicationName.isNotEmpty())
  36. {
  37. PropertiesFile::Options o (options);
  38. if (userProps == nullptr)
  39. {
  40. o.commonToAllUsers = false;
  41. userProps = new PropertiesFile (o);
  42. }
  43. if (commonProps == nullptr)
  44. {
  45. o.commonToAllUsers = true;
  46. commonProps = new PropertiesFile (o);
  47. }
  48. userProps->setFallbackPropertySet (commonProps);
  49. }
  50. }
  51. PropertiesFile* ApplicationProperties::getUserSettings()
  52. {
  53. if (userProps == nullptr)
  54. openFiles();
  55. return userProps;
  56. }
  57. PropertiesFile* ApplicationProperties::getCommonSettings (const bool returnUserPropsIfReadOnly)
  58. {
  59. if (commonProps == nullptr)
  60. openFiles();
  61. if (returnUserPropsIfReadOnly)
  62. {
  63. if (commonSettingsAreReadOnly == 0)
  64. commonSettingsAreReadOnly = commonProps->save() ? -1 : 1;
  65. if (commonSettingsAreReadOnly > 0)
  66. return userProps;
  67. }
  68. return commonProps;
  69. }
  70. bool ApplicationProperties::saveIfNeeded()
  71. {
  72. return (userProps == nullptr || userProps->saveIfNeeded())
  73. && (commonProps == nullptr || commonProps->saveIfNeeded());
  74. }
  75. void ApplicationProperties::closeFiles()
  76. {
  77. userProps = nullptr;
  78. commonProps = nullptr;
  79. }