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.

402 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../Application/jucer_Headers.h"
  20. #include "jucer_StoredSettings.h"
  21. #include "../Application/jucer_Application.h"
  22. //==============================================================================
  23. StoredSettings& getAppSettings()
  24. {
  25. return *ProjucerApplication::getApp().settings;
  26. }
  27. PropertiesFile& getGlobalProperties()
  28. {
  29. return getAppSettings().getGlobalProperties();
  30. }
  31. //==============================================================================
  32. StoredSettings::StoredSettings()
  33. : appearance (true),
  34. projectDefaults ("PROJECT_DEFAULT_SETTINGS"),
  35. fallbackPaths ("FALLBACK_PATHS")
  36. {
  37. updateOldProjectSettingsFiles();
  38. reload();
  39. projectDefaults.addListener (this);
  40. fallbackPaths.addListener (this);
  41. }
  42. StoredSettings::~StoredSettings()
  43. {
  44. projectDefaults.removeListener (this);
  45. fallbackPaths.removeListener (this);
  46. flush();
  47. }
  48. PropertiesFile& StoredSettings::getGlobalProperties()
  49. {
  50. return *propertyFiles.getUnchecked (0);
  51. }
  52. static PropertiesFile* createPropsFile (const String& filename, bool isProjectSettings)
  53. {
  54. return new PropertiesFile (ProjucerApplication::getApp()
  55. .getPropertyFileOptionsFor (filename, isProjectSettings));
  56. }
  57. PropertiesFile& StoredSettings::getProjectProperties (const String& projectUID)
  58. {
  59. const auto filename = String ("Projucer_Project_" + projectUID);
  60. for (auto i = propertyFiles.size(); --i >= 0;)
  61. {
  62. auto* const props = propertyFiles.getUnchecked(i);
  63. if (props->getFile().getFileNameWithoutExtension() == filename)
  64. return *props;
  65. }
  66. auto* p = createPropsFile (filename, true);
  67. propertyFiles.add (p);
  68. return *p;
  69. }
  70. void StoredSettings::updateGlobalPreferences()
  71. {
  72. // update 'invisible' global settings
  73. updateRecentFiles();
  74. updateLastWizardFolder();
  75. updateKeyMappings();
  76. }
  77. void StoredSettings::updateRecentFiles()
  78. {
  79. getGlobalProperties().setValue ("recentFiles", recentFiles.toString());
  80. }
  81. void StoredSettings::updateLastWizardFolder()
  82. {
  83. getGlobalProperties().setValue ("lastWizardFolder", lastWizardFolder.getFullPathName());
  84. }
  85. void StoredSettings::updateKeyMappings()
  86. {
  87. getGlobalProperties().removeValue ("keyMappings");
  88. if (auto* commandManager = ProjucerApplication::getApp().commandManager.get())
  89. {
  90. const ScopedPointer<XmlElement> keys (commandManager->getKeyMappings()->createXml (true));
  91. if (keys != nullptr)
  92. getGlobalProperties().setValue ("keyMappings", keys);
  93. }
  94. }
  95. void StoredSettings::flush()
  96. {
  97. updateGlobalPreferences();
  98. saveSwatchColours();
  99. for (auto i = propertyFiles.size(); --i >= 0;)
  100. propertyFiles.getUnchecked(i)->saveIfNeeded();
  101. }
  102. void StoredSettings::reload()
  103. {
  104. propertyFiles.clear();
  105. propertyFiles.add (createPropsFile ("Projucer", false));
  106. ScopedPointer<XmlElement> projectDefaultsXml (propertyFiles.getFirst()->getXmlValue ("PROJECT_DEFAULT_SETTINGS"));
  107. if (projectDefaultsXml != nullptr)
  108. projectDefaults = ValueTree::fromXml (*projectDefaultsXml);
  109. ScopedPointer<XmlElement> fallbackPathsXml (propertyFiles.getFirst()->getXmlValue ("FALLBACK_PATHS"));
  110. if (fallbackPathsXml != nullptr)
  111. fallbackPaths = ValueTree::fromXml (*fallbackPathsXml);
  112. // recent files...
  113. recentFiles.restoreFromString (getGlobalProperties().getValue ("recentFiles"));
  114. recentFiles.removeNonExistentFiles();
  115. lastWizardFolder = getGlobalProperties().getValue ("lastWizardFolder");
  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. void StoredSettings::updateOldProjectSettingsFiles()
  135. {
  136. // Global properties file hasn't been created yet so create a dummy file
  137. auto projucerSettingsDirectory = ProjucerApplication::getApp().getPropertyFileOptionsFor ("Dummy", false)
  138. .getDefaultFile().getParentDirectory();
  139. auto newProjectSettingsDir = projucerSettingsDirectory.getChildFile ("ProjectSettings");
  140. newProjectSettingsDir.createDirectory();
  141. DirectoryIterator iter (projucerSettingsDirectory, false, "*.settings");
  142. while (iter.next())
  143. {
  144. auto f = iter.getFile();
  145. auto oldFileName = f.getFileName();
  146. if (oldFileName.contains ("Introjucer"))
  147. {
  148. auto newFileName = oldFileName.replace ("Introjucer", "Projucer");
  149. if (oldFileName.contains ("_Project"))
  150. f.moveFileTo (f.getSiblingFile (newProjectSettingsDir.getFileName()).getChildFile (newFileName));
  151. else
  152. f.moveFileTo (f.getSiblingFile (newFileName));
  153. }
  154. }
  155. }
  156. //==============================================================================
  157. void StoredSettings::loadSwatchColours()
  158. {
  159. swatchColours.clear();
  160. #define COL(col) Colours::col,
  161. const Colour colours[] =
  162. {
  163. #include "../Utility/Helpers/jucer_Colours.h"
  164. Colours::transparentBlack
  165. };
  166. #undef COL
  167. const auto numSwatchColours = 24;
  168. auto& props = getGlobalProperties();
  169. for (auto i = 0; i < numSwatchColours; ++i)
  170. swatchColours.add (Colour::fromString (props.getValue ("swatchColour" + String (i),
  171. colours [2 + i].toString())));
  172. }
  173. void StoredSettings::saveSwatchColours()
  174. {
  175. auto& props = getGlobalProperties();
  176. for (auto i = 0; i < swatchColours.size(); ++i)
  177. props.setValue ("swatchColour" + String (i), swatchColours.getReference(i).toString());
  178. }
  179. StoredSettings::ColourSelectorWithSwatches::ColourSelectorWithSwatches() {}
  180. StoredSettings::ColourSelectorWithSwatches::~ColourSelectorWithSwatches() {}
  181. int StoredSettings::ColourSelectorWithSwatches::getNumSwatches() const
  182. {
  183. return getAppSettings().swatchColours.size();
  184. }
  185. Colour StoredSettings::ColourSelectorWithSwatches::getSwatchColour (int index) const
  186. {
  187. return getAppSettings().swatchColours [index];
  188. }
  189. void StoredSettings::ColourSelectorWithSwatches::setSwatchColour (int index, const Colour& newColour)
  190. {
  191. getAppSettings().swatchColours.set (index, newColour);
  192. }
  193. //==============================================================================
  194. Value StoredSettings::getStoredPath (const Identifier& key)
  195. {
  196. auto v = projectDefaults.getPropertyAsValue (key, nullptr);
  197. if (v.toString().isEmpty())
  198. v = getFallbackPathForOS (key, TargetOS::getThisOS()).toString();
  199. return v;
  200. }
  201. Value StoredSettings::getFallbackPathForOS (const Identifier& key, DependencyPathOS os)
  202. {
  203. auto id = Identifier();
  204. if (os == TargetOS::osx) id = Ids::osxFallback;
  205. else if (os == TargetOS::windows) id = Ids::windowsFallback;
  206. else if (os == TargetOS::linux) id = Ids::linuxFallback;
  207. if (id == Identifier())
  208. jassertfalse;
  209. auto v = fallbackPaths.getOrCreateChildWithName (id, nullptr)
  210. .getPropertyAsValue (key, nullptr);
  211. if (v.toString().isEmpty())
  212. {
  213. if (key == Ids::defaultJuceModulePath)
  214. {
  215. v = (os == TargetOS::windows ? "C:\\JUCE\\modules"
  216. : "~/JUCE/modules");
  217. }
  218. else if (key == Ids::defaultUserModulePath)
  219. {
  220. v = (os == TargetOS::windows ? "C:\\modules"
  221. : "~/modules");
  222. }
  223. else if (key == Ids::vst3Path)
  224. {
  225. v = (os == TargetOS::windows ? "C:\\SDKs\\VST_SDK\\VST3_SDK"
  226. : "~/SDKs/VST_SDK/VST3_SDK");
  227. }
  228. else if (key == Ids::rtasPath)
  229. {
  230. if (os == TargetOS::windows) v = "C:\\SDKs\\PT_90_SDK";
  231. else if (os == TargetOS::osx) v = "~/SDKs/PT_90_SDK";
  232. else jassertfalse; // no RTAS on this OS!
  233. }
  234. else if (key == Ids::aaxPath)
  235. {
  236. if (os == TargetOS::windows) v = "C:\\SDKs\\AAX";
  237. else if (os == TargetOS::osx) v = "~/SDKs/AAX";
  238. else jassertfalse; // no AAX on this OS!
  239. }
  240. else if (key == Ids::androidSDKPath)
  241. {
  242. v = "${user.home}/Library/Android/sdk";
  243. }
  244. else if (key == Ids::androidNDKPath)
  245. {
  246. v = "${user.home}/Library/Android/sdk/ndk-bundle";
  247. }
  248. else if (key == Ids::clionExePath)
  249. {
  250. if (os == TargetOS::windows)
  251. {
  252. #if JUCE_WINDOWS
  253. auto regValue = WindowsRegistry::getValue ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Applications\\clion64.exe\\shell\\open\\command\\", {}, {});
  254. auto openCmd = StringArray::fromTokens (regValue, true);
  255. if (! openCmd.isEmpty())
  256. return Value (openCmd[0].unquoted());
  257. #endif
  258. v = "C:\\Program Files\\JetBrains\\CLion YYYY.MM.DD\\bin\\clion64.exe";
  259. }
  260. else if (os == TargetOS::osx)
  261. {
  262. v = "/Applications/CLion.app";
  263. }
  264. else
  265. {
  266. v = "${user.home}/clion/bin/clion.sh";
  267. }
  268. }
  269. }
  270. return v;
  271. }
  272. static bool doesSDKPathContainFile (const File& relativeTo, const String& path, const String& fileToCheckFor)
  273. {
  274. auto actualPath = path.replace ("${user.home}", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  275. return relativeTo.getChildFile (actualPath + "/" + fileToCheckFor).exists();
  276. }
  277. bool StoredSettings::isGlobalPathValid (const File& relativeTo, const Identifier& key, const String& path)
  278. {
  279. String fileToCheckFor;
  280. if (key == Ids::vst3Path)
  281. {
  282. fileToCheckFor = "base/source/baseiids.cpp";
  283. }
  284. else if (key == Ids::rtasPath)
  285. {
  286. fileToCheckFor = "AlturaPorts/TDMPlugIns/PlugInLibrary/EffectClasses/CEffectProcessMIDI.cpp";
  287. }
  288. else if (key == Ids::aaxPath)
  289. {
  290. fileToCheckFor = "Interfaces/AAX_Exports.cpp";
  291. }
  292. else if (key == Ids::androidSDKPath)
  293. {
  294. #if JUCE_WINDOWS
  295. fileToCheckFor = "platform-tools/adb.exe";
  296. #else
  297. fileToCheckFor = "platform-tools/adb";
  298. #endif
  299. }
  300. else if (key == Ids::androidNDKPath)
  301. {
  302. #if JUCE_WINDOWS
  303. fileToCheckFor = "ndk-depends.cmd";
  304. #else
  305. fileToCheckFor = "ndk-depends";
  306. #endif
  307. }
  308. else if (key == Ids::defaultJuceModulePath)
  309. {
  310. fileToCheckFor = "juce_core";
  311. }
  312. else if (key == Ids::defaultUserModulePath)
  313. {
  314. fileToCheckFor = {};
  315. }
  316. else if (key == Ids::clionExePath)
  317. {
  318. #if JUCE_MAC
  319. fileToCheckFor = path.trim().endsWith (".app") ? "Contents/MacOS/clion" : "../clion";
  320. #elif JUCE_WIDOWS
  321. fileToCheckFor = "../clion64.exe";
  322. #else
  323. fileToCheckFor = "../clion.sh";
  324. #endif
  325. }
  326. else
  327. {
  328. // didn't recognise the key provided!
  329. jassertfalse;
  330. return false;
  331. }
  332. return doesSDKPathContainFile (relativeTo, path, fileToCheckFor);
  333. }