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.

313 lines
9.0KB

  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 = XmlDocument::parse (BinaryData::colourscheme_dark_xml);
  109. appearance.readFromXML (*xml);
  110. appearance.updateColourScheme();
  111. loadSwatchColours();
  112. }
  113. Array<File> StoredSettings::getLastProjects()
  114. {
  115. StringArray s;
  116. s.addTokens (getGlobalProperties().getValue ("lastProjects"), "|", "");
  117. Array<File> f;
  118. for (int i = 0; i < s.size(); ++i)
  119. f.add (File (s[i]));
  120. return f;
  121. }
  122. void StoredSettings::setLastProjects (const Array<File>& files)
  123. {
  124. StringArray s;
  125. for (int i = 0; i < files.size(); ++i)
  126. s.add (files.getReference(i).getFullPathName());
  127. getGlobalProperties().setValue ("lastProjects", s.joinIntoString ("|"));
  128. }
  129. //==============================================================================
  130. void StoredSettings::loadSwatchColours()
  131. {
  132. swatchColours.clear();
  133. #define COL(col) Colours::col,
  134. const Colour colours[] =
  135. {
  136. #include "jucer_Colours.h"
  137. Colours::transparentBlack
  138. };
  139. #undef COL
  140. const int numSwatchColours = 24;
  141. PropertiesFile& props = getGlobalProperties();
  142. for (int i = 0; i < numSwatchColours; ++i)
  143. swatchColours.add (Colour::fromString (props.getValue ("swatchColour" + String (i),
  144. colours [2 + i].toString())));
  145. }
  146. void StoredSettings::saveSwatchColours()
  147. {
  148. PropertiesFile& props = getGlobalProperties();
  149. for (int i = 0; i < swatchColours.size(); ++i)
  150. props.setValue ("swatchColour" + String (i), swatchColours.getReference(i).toString());
  151. }
  152. int StoredSettings::ColourSelectorWithSwatches::getNumSwatches() const
  153. {
  154. return getAppSettings().swatchColours.size();
  155. }
  156. Colour StoredSettings::ColourSelectorWithSwatches::getSwatchColour (int index) const
  157. {
  158. return getAppSettings().swatchColours [index];
  159. }
  160. void StoredSettings::ColourSelectorWithSwatches::setSwatchColour (int index, const Colour& newColour) const
  161. {
  162. getAppSettings().swatchColours.set (index, newColour);
  163. }
  164. //==============================================================================
  165. static bool doesSDKPathContainFile (const File& relativeTo, const String& path, const String& fileToCheckFor)
  166. {
  167. String actualPath = path.replace ("${user.home}", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  168. return relativeTo.getChildFile (actualPath + "/" + fileToCheckFor).existsAsFile();
  169. }
  170. Value StoredSettings::getGlobalPath (const Identifier& key, DependencyPathOS os)
  171. {
  172. Value v (projectDefaults.getPropertyAsValue (key, nullptr));
  173. if (v.toString().isEmpty())
  174. {
  175. auto defaultPath = getFallbackPath (key, os);
  176. if (os == TargetOS::getThisOS())
  177. v = defaultPath;
  178. }
  179. return v;
  180. }
  181. String StoredSettings::getFallbackPath (const Identifier& key, DependencyPathOS os)
  182. {
  183. if (key == Ids::vst3Path)
  184. return os == TargetOS::windows ? "c:\\SDKs\\VST_SDK\\VST3_SDK"
  185. : "~/SDKs/VST_SDK/VST3_SDK";
  186. if (key == Ids::rtasPath)
  187. {
  188. if (os == TargetOS::windows) return "c:\\SDKs\\PT_90_SDK";
  189. if (os == TargetOS::osx) return "~/SDKs/PT_90_SDK";
  190. // no RTAS on this OS!
  191. jassertfalse;
  192. return {};
  193. }
  194. if (key == Ids::aaxPath)
  195. {
  196. if (os == TargetOS::windows) return "c:\\SDKs\\AAX";
  197. if (os == TargetOS::osx) return "~/SDKs/AAX" ;
  198. // no AAX on this OS!
  199. jassertfalse;
  200. return {};
  201. }
  202. if (key == Ids::androidSDKPath)
  203. return "${user.home}/Library/Android/sdk";
  204. if (key == Ids::androidNDKPath)
  205. return "${user.home}/Library/Android/sdk/ndk-bundle";
  206. // didn't recognise the key provided!
  207. jassertfalse;
  208. return {};
  209. }
  210. bool StoredSettings::isGlobalPathValid (const File& relativeTo, const Identifier& key, const String& path)
  211. {
  212. String fileToCheckFor;
  213. if (key == Ids::vst3Path)
  214. {
  215. fileToCheckFor = "base/source/baseiids.cpp";
  216. }
  217. else if (key == Ids::rtasPath)
  218. {
  219. fileToCheckFor = "AlturaPorts/TDMPlugIns/PlugInLibrary/EffectClasses/CEffectProcessMIDI.cpp";
  220. }
  221. else if (key == Ids::aaxPath)
  222. {
  223. fileToCheckFor = "Interfaces/AAX_Exports.cpp";
  224. }
  225. else if (key == Ids::androidSDKPath)
  226. {
  227. #if JUCE_WINDOWS
  228. fileToCheckFor = "platform-tools/adb.exe";
  229. #else
  230. fileToCheckFor = "platform-tools/adb";
  231. #endif
  232. }
  233. else if (key == Ids::androidNDKPath)
  234. {
  235. #if JUCE_WINDOWS
  236. fileToCheckFor = "ndk-depends.cmd";
  237. #else
  238. fileToCheckFor = "ndk-depends";
  239. #endif
  240. }
  241. else
  242. {
  243. // didn't recognise the key provided!
  244. jassertfalse;
  245. return false;
  246. }
  247. return doesSDKPathContainFile (relativeTo, path, fileToCheckFor);
  248. }