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.

315 lines
9.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #include "../jucer_Headers.h"
  18. #include "jucer_StoredSettings.h"
  19. #include "../Application/jucer_Application.h"
  20. #include "../Application/jucer_GlobalPreferences.h"
  21. //==============================================================================
  22. StoredSettings& getAppSettings()
  23. {
  24. return *ProjucerApplication::getApp().settings;
  25. }
  26. PropertiesFile& getGlobalProperties()
  27. {
  28. return getAppSettings().getGlobalProperties();
  29. }
  30. //==============================================================================
  31. StoredSettings::StoredSettings()
  32. : appearance (true), projectDefaults ("PROJECT_DEFAULT_SETTINGS")
  33. {
  34. reload();
  35. projectDefaults.addListener (this);
  36. }
  37. StoredSettings::~StoredSettings()
  38. {
  39. projectDefaults.removeListener (this);
  40. flush();
  41. }
  42. PropertiesFile& StoredSettings::getGlobalProperties()
  43. {
  44. return *propertyFiles.getUnchecked (0);
  45. }
  46. static PropertiesFile* createPropsFile (const String& filename)
  47. {
  48. return new PropertiesFile (ProjucerApplication::getApp()
  49. .getPropertyFileOptionsFor (filename));
  50. }
  51. PropertiesFile& StoredSettings::getProjectProperties (const String& projectUID)
  52. {
  53. const String filename ("Introjucer_Project_" + projectUID);
  54. for (int i = propertyFiles.size(); --i >= 0;)
  55. {
  56. PropertiesFile* const props = propertyFiles.getUnchecked(i);
  57. if (props->getFile().getFileNameWithoutExtension() == filename)
  58. return *props;
  59. }
  60. PropertiesFile* p = createPropsFile (filename);
  61. propertyFiles.add (p);
  62. return *p;
  63. }
  64. void StoredSettings::updateGlobalPreferences()
  65. {
  66. // update global settings editable from the global preferences window
  67. updateAppearanceSettings();
  68. // update 'invisible' global settings
  69. updateRecentFiles();
  70. updateKeyMappings();
  71. }
  72. void StoredSettings::updateAppearanceSettings()
  73. {
  74. const ScopedPointer<XmlElement> xml (appearance.settings.createXml());
  75. getGlobalProperties().setValue ("editorColours", xml);
  76. }
  77. void StoredSettings::updateRecentFiles()
  78. {
  79. getGlobalProperties().setValue ("recentFiles", recentFiles.toString());
  80. }
  81. void StoredSettings::updateKeyMappings()
  82. {
  83. getGlobalProperties().removeValue ("keyMappings");
  84. if (ApplicationCommandManager* commandManager = ProjucerApplication::getApp().commandManager)
  85. {
  86. const ScopedPointer<XmlElement> keys (commandManager->getKeyMappings()->createXml (true));
  87. if (keys != nullptr)
  88. getGlobalProperties().setValue ("keyMappings", keys);
  89. }
  90. }
  91. void StoredSettings::flush()
  92. {
  93. updateGlobalPreferences();
  94. saveSwatchColours();
  95. for (int i = propertyFiles.size(); --i >= 0;)
  96. propertyFiles.getUnchecked(i)->saveIfNeeded();
  97. }
  98. void StoredSettings::reload()
  99. {
  100. propertyFiles.clear();
  101. propertyFiles.add (createPropsFile ("Introjucer"));
  102. ScopedPointer<XmlElement> projectDefaultsXml (propertyFiles.getFirst()->getXmlValue ("PROJECT_DEFAULT_SETTINGS"));
  103. if (projectDefaultsXml != nullptr)
  104. projectDefaults = ValueTree::fromXml (*projectDefaultsXml);
  105. // recent files...
  106. recentFiles.restoreFromString (getGlobalProperties().getValue ("recentFiles"));
  107. recentFiles.removeNonExistentFiles();
  108. ScopedPointer<XmlElement> xml (getGlobalProperties().getXmlValue ("editorColours"));
  109. if (xml == nullptr)
  110. {
  111. xml = XmlDocument::parse (BinaryData::colourscheme_dark_xml);
  112. jassert (xml != nullptr);
  113. }
  114. appearance.readFromXML (*xml);
  115. appearance.updateColourScheme();
  116. loadSwatchColours();
  117. }
  118. Array<File> StoredSettings::getLastProjects()
  119. {
  120. StringArray s;
  121. s.addTokens (getGlobalProperties().getValue ("lastProjects"), "|", "");
  122. Array<File> f;
  123. for (int i = 0; i < s.size(); ++i)
  124. f.add (File (s[i]));
  125. return f;
  126. }
  127. void StoredSettings::setLastProjects (const Array<File>& files)
  128. {
  129. StringArray s;
  130. for (int i = 0; i < files.size(); ++i)
  131. s.add (files.getReference(i).getFullPathName());
  132. getGlobalProperties().setValue ("lastProjects", s.joinIntoString ("|"));
  133. }
  134. //==============================================================================
  135. void StoredSettings::loadSwatchColours()
  136. {
  137. swatchColours.clear();
  138. #define COL(col) Colours::col,
  139. const Colour colours[] =
  140. {
  141. #include "jucer_Colours.h"
  142. Colours::transparentBlack
  143. };
  144. #undef COL
  145. const int numSwatchColours = 24;
  146. PropertiesFile& props = getGlobalProperties();
  147. for (int i = 0; i < numSwatchColours; ++i)
  148. swatchColours.add (Colour::fromString (props.getValue ("swatchColour" + String (i),
  149. colours [2 + i].toString())));
  150. }
  151. void StoredSettings::saveSwatchColours()
  152. {
  153. PropertiesFile& props = getGlobalProperties();
  154. for (int i = 0; i < swatchColours.size(); ++i)
  155. props.setValue ("swatchColour" + String (i), swatchColours.getReference(i).toString());
  156. }
  157. int StoredSettings::ColourSelectorWithSwatches::getNumSwatches() const
  158. {
  159. return getAppSettings().swatchColours.size();
  160. }
  161. Colour StoredSettings::ColourSelectorWithSwatches::getSwatchColour (int index) const
  162. {
  163. return getAppSettings().swatchColours [index];
  164. }
  165. void StoredSettings::ColourSelectorWithSwatches::setSwatchColour (int index, const Colour& newColour) const
  166. {
  167. getAppSettings().swatchColours.set (index, newColour);
  168. }
  169. //==============================================================================
  170. static bool doesSDKPathContainFile (const File& relativeTo, const String& path, const String& fileToCheckFor)
  171. {
  172. String actualPath = path.replace ("${user.home}", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  173. return relativeTo.getChildFile (actualPath + "/" + fileToCheckFor).existsAsFile();
  174. }
  175. Value StoredSettings::getGlobalPath (const Identifier& key, DependencyPathOS os)
  176. {
  177. Value v (projectDefaults.getPropertyAsValue (key, nullptr));
  178. if (v.toString().isEmpty())
  179. v = getFallbackPath (key, os);
  180. return v;
  181. }
  182. String StoredSettings::getFallbackPath (const Identifier& key, DependencyPathOS os)
  183. {
  184. if (key == Ids::vst3Path)
  185. return os == TargetOS::windows ? "c:\\SDKs\\VST3 SDK"
  186. : "~/SDKs/VST3 SDK";
  187. if (key == Ids::rtasPath)
  188. {
  189. if (os == TargetOS::windows) return "c:\\SDKs\\PT_90_SDK";
  190. if (os == TargetOS::osx) return "~/SDKs/PT_90_SDK";
  191. // no RTAS on this OS!
  192. jassertfalse;
  193. return String();
  194. }
  195. if (key == Ids::aaxPath)
  196. {
  197. if (os == TargetOS::windows) return "c:\\SDKs\\AAX";
  198. if (os == TargetOS::osx) return "~/SDKs/AAX" ;
  199. // no AAX on this OS!
  200. jassertfalse;
  201. return String();
  202. }
  203. if (key == Ids::androidSDKPath)
  204. return "${user.home}/Library/Android/sdk";
  205. if (key == Ids::androidNDKPath)
  206. return "${user.home}/Library/Android/sdk/ndk-bundle";
  207. // didn't recognise the key provided!
  208. jassertfalse;
  209. return String();
  210. }
  211. bool StoredSettings::isGlobalPathValid (const File& relativeTo, const Identifier& key, const String& path)
  212. {
  213. String fileToCheckFor;
  214. if (key == Ids::vst3Path)
  215. {
  216. fileToCheckFor = "base/source/baseiids.cpp";
  217. }
  218. else if (key == Ids::rtasPath)
  219. {
  220. fileToCheckFor = "AlturaPorts/TDMPlugIns/PlugInLibrary/EffectClasses/CEffectProcessMIDI.cpp";
  221. }
  222. else if (key == Ids::aaxPath)
  223. {
  224. fileToCheckFor = "Interfaces/AAX_Exports.cpp";
  225. }
  226. else if (key == Ids::androidSDKPath)
  227. {
  228. #if JUCE_WINDOWS
  229. fileToCheckFor = "platform-tools/adb.exe";
  230. #else
  231. fileToCheckFor = "platform-tools/adb";
  232. #endif
  233. }
  234. else if (key == Ids::androidNDKPath)
  235. {
  236. #if JUCE_WINDOWS
  237. fileToCheckFor = "ndk-depends.cmd";
  238. #else
  239. fileToCheckFor = "ndk-depends";
  240. #endif
  241. }
  242. else
  243. {
  244. // didn't recognise the key provided!
  245. jassertfalse;
  246. return false;
  247. }
  248. return doesSDKPathContainFile (relativeTo, path, fileToCheckFor);
  249. }