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.

482 lines
15KB

  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. checkJUCEPaths();
  40. projectDefaults.addListener (this);
  41. fallbackPaths.addListener (this);
  42. }
  43. StoredSettings::~StoredSettings()
  44. {
  45. projectDefaults.removeListener (this);
  46. fallbackPaths.removeListener (this);
  47. flush();
  48. }
  49. PropertiesFile& StoredSettings::getGlobalProperties()
  50. {
  51. return *propertyFiles.getUnchecked (0);
  52. }
  53. static PropertiesFile* createPropsFile (const String& filename, bool isProjectSettings)
  54. {
  55. return new PropertiesFile (ProjucerApplication::getApp()
  56. .getPropertyFileOptionsFor (filename, isProjectSettings));
  57. }
  58. PropertiesFile& StoredSettings::getProjectProperties (const String& projectUID)
  59. {
  60. const auto filename = String ("Projucer_Project_" + projectUID);
  61. for (auto i = propertyFiles.size(); --i >= 0;)
  62. {
  63. auto* const props = propertyFiles.getUnchecked(i);
  64. if (props->getFile().getFileNameWithoutExtension() == filename)
  65. return *props;
  66. }
  67. auto* p = createPropsFile (filename, true);
  68. propertyFiles.add (p);
  69. return *p;
  70. }
  71. void StoredSettings::updateGlobalPreferences()
  72. {
  73. // update 'invisible' global settings
  74. updateRecentFiles();
  75. updateLastWizardFolder();
  76. updateKeyMappings();
  77. }
  78. void StoredSettings::updateRecentFiles()
  79. {
  80. getGlobalProperties().setValue ("recentFiles", recentFiles.toString());
  81. }
  82. void StoredSettings::updateLastWizardFolder()
  83. {
  84. getGlobalProperties().setValue ("lastWizardFolder", lastWizardFolder.getFullPathName());
  85. }
  86. void StoredSettings::updateKeyMappings()
  87. {
  88. getGlobalProperties().removeValue ("keyMappings");
  89. if (auto* commandManager = ProjucerApplication::getApp().commandManager.get())
  90. {
  91. const std::unique_ptr<XmlElement> keys (commandManager->getKeyMappings()->createXml (true));
  92. if (keys != nullptr)
  93. getGlobalProperties().setValue ("keyMappings", keys.get());
  94. }
  95. }
  96. void StoredSettings::flush()
  97. {
  98. updateGlobalPreferences();
  99. saveSwatchColours();
  100. for (auto i = propertyFiles.size(); --i >= 0;)
  101. propertyFiles.getUnchecked(i)->saveIfNeeded();
  102. }
  103. void StoredSettings::reload()
  104. {
  105. propertyFiles.clear();
  106. propertyFiles.add (createPropsFile ("Projucer", false));
  107. std::unique_ptr<XmlElement> projectDefaultsXml (propertyFiles.getFirst()->getXmlValue ("PROJECT_DEFAULT_SETTINGS"));
  108. if (projectDefaultsXml != nullptr)
  109. projectDefaults = ValueTree::fromXml (*projectDefaultsXml);
  110. std::unique_ptr<XmlElement> fallbackPathsXml (propertyFiles.getFirst()->getXmlValue ("FALLBACK_PATHS"));
  111. if (fallbackPathsXml != nullptr)
  112. fallbackPaths = ValueTree::fromXml (*fallbackPathsXml);
  113. // recent files...
  114. recentFiles.restoreFromString (getGlobalProperties().getValue ("recentFiles"));
  115. recentFiles.removeNonExistentFiles();
  116. lastWizardFolder = getGlobalProperties().getValue ("lastWizardFolder");
  117. loadSwatchColours();
  118. }
  119. Array<File> StoredSettings::getLastProjects()
  120. {
  121. StringArray s;
  122. s.addTokens (getGlobalProperties().getValue ("lastProjects"), "|", "");
  123. Array<File> f;
  124. for (int i = 0; i < s.size(); ++i)
  125. f.add (File (s[i]));
  126. return f;
  127. }
  128. void StoredSettings::setLastProjects (const Array<File>& files)
  129. {
  130. StringArray s;
  131. for (int i = 0; i < files.size(); ++i)
  132. s.add (files.getReference(i).getFullPathName());
  133. getGlobalProperties().setValue ("lastProjects", s.joinIntoString ("|"));
  134. }
  135. void StoredSettings::updateOldProjectSettingsFiles()
  136. {
  137. // Global properties file hasn't been created yet so create a dummy file
  138. auto projucerSettingsDirectory = ProjucerApplication::getApp().getPropertyFileOptionsFor ("Dummy", false)
  139. .getDefaultFile().getParentDirectory();
  140. auto newProjectSettingsDir = projucerSettingsDirectory.getChildFile ("ProjectSettings");
  141. newProjectSettingsDir.createDirectory();
  142. DirectoryIterator iter (projucerSettingsDirectory, false, "*.settings");
  143. while (iter.next())
  144. {
  145. auto f = iter.getFile();
  146. auto oldFileName = f.getFileName();
  147. if (oldFileName.contains ("Introjucer"))
  148. {
  149. auto newFileName = oldFileName.replace ("Introjucer", "Projucer");
  150. if (oldFileName.contains ("_Project"))
  151. {
  152. f.moveFileTo (f.getSiblingFile (newProjectSettingsDir.getFileName()).getChildFile (newFileName));
  153. }
  154. else
  155. {
  156. auto newFile = f.getSiblingFile (newFileName);
  157. // don't overwrite newer settings file
  158. if (! newFile.existsAsFile())
  159. f.moveFileTo (f.getSiblingFile (newFileName));
  160. }
  161. }
  162. }
  163. }
  164. void StoredSettings::checkJUCEPaths()
  165. {
  166. auto moduleFolder = projectDefaults.getProperty (Ids::defaultJuceModulePath).toString();
  167. auto juceFolder = projectDefaults.getProperty (Ids::jucePath).toString();
  168. auto validModuleFolder = moduleFolder.isNotEmpty() && isGlobalPathValid ({}, Ids::defaultJuceModulePath, moduleFolder);
  169. auto validJuceFolder = juceFolder.isNotEmpty() && isGlobalPathValid ({}, Ids::jucePath, juceFolder);
  170. if (validModuleFolder && ! validJuceFolder)
  171. projectDefaults.getPropertyAsValue (Ids::jucePath, nullptr) = File (moduleFolder).getParentDirectory().getFullPathName();
  172. else if (! validModuleFolder && validJuceFolder)
  173. projectDefaults.getPropertyAsValue (Ids::defaultJuceModulePath, nullptr) = File (juceFolder).getChildFile ("modules").getFullPathName();
  174. }
  175. bool StoredSettings::shouldAskUserToSetJUCEPath() noexcept
  176. {
  177. if (! isGlobalPathValid ({}, Ids::jucePath, projectDefaults.getProperty (Ids::jucePath).toString())
  178. && getGlobalProperties().getValue ("dontAskAboutJUCEPath", {}).isEmpty())
  179. return true;
  180. return false;
  181. }
  182. void StoredSettings::setDontAskAboutJUCEPathAgain() noexcept
  183. {
  184. getGlobalProperties().setValue ("dontAskAboutJUCEPath", 1);
  185. }
  186. //==============================================================================
  187. void StoredSettings::loadSwatchColours()
  188. {
  189. swatchColours.clear();
  190. #define COL(col) Colours::col,
  191. const Colour colours[] =
  192. {
  193. #include "../Utility/Helpers/jucer_Colours.h"
  194. Colours::transparentBlack
  195. };
  196. #undef COL
  197. const auto numSwatchColours = 24;
  198. auto& props = getGlobalProperties();
  199. for (auto i = 0; i < numSwatchColours; ++i)
  200. swatchColours.add (Colour::fromString (props.getValue ("swatchColour" + String (i),
  201. colours [2 + i].toString())));
  202. }
  203. void StoredSettings::saveSwatchColours()
  204. {
  205. auto& props = getGlobalProperties();
  206. for (auto i = 0; i < swatchColours.size(); ++i)
  207. props.setValue ("swatchColour" + String (i), swatchColours.getReference(i).toString());
  208. }
  209. StoredSettings::ColourSelectorWithSwatches::ColourSelectorWithSwatches() {}
  210. StoredSettings::ColourSelectorWithSwatches::~ColourSelectorWithSwatches() {}
  211. int StoredSettings::ColourSelectorWithSwatches::getNumSwatches() const
  212. {
  213. return getAppSettings().swatchColours.size();
  214. }
  215. Colour StoredSettings::ColourSelectorWithSwatches::getSwatchColour (int index) const
  216. {
  217. return getAppSettings().swatchColours [index];
  218. }
  219. void StoredSettings::ColourSelectorWithSwatches::setSwatchColour (int index, const Colour& newColour)
  220. {
  221. getAppSettings().swatchColours.set (index, newColour);
  222. }
  223. //==============================================================================
  224. Value StoredSettings::getStoredPath (const Identifier& key)
  225. {
  226. auto v = projectDefaults.getPropertyAsValue (key, nullptr);
  227. if (v.toString().isEmpty())
  228. v = getFallbackPathForOS (key, TargetOS::getThisOS()).toString();
  229. return v;
  230. }
  231. Value StoredSettings::getFallbackPathForOS (const Identifier& key, DependencyPathOS os)
  232. {
  233. auto id = identifierForOS (os);
  234. auto osFallback = fallbackPaths.getOrCreateChildWithName (id, nullptr);
  235. auto v = osFallback.getPropertyAsValue (key, nullptr);
  236. if (v.toString().isEmpty())
  237. {
  238. if (key == Ids::jucePath)
  239. {
  240. v = (os == TargetOS::windows ? "C:\\JUCE"
  241. : "~/JUCE");
  242. }
  243. else if (key == Ids::defaultJuceModulePath)
  244. {
  245. v = (os == TargetOS::windows ? "C:\\JUCE\\modules"
  246. : "~/JUCE/modules");
  247. }
  248. else if (key == Ids::defaultUserModulePath)
  249. {
  250. v = (os == TargetOS::windows ? "C:\\modules"
  251. : "~/modules");
  252. }
  253. else if (key == Ids::vst3Path)
  254. {
  255. v = "";
  256. }
  257. else if (key == Ids::rtasPath)
  258. {
  259. if (os == TargetOS::windows) v = "C:\\SDKs\\PT_90_SDK";
  260. else if (os == TargetOS::osx) v = "~/SDKs/PT_90_SDK";
  261. else jassertfalse; // no RTAS on this OS!
  262. }
  263. else if (key == Ids::aaxPath)
  264. {
  265. if (os == TargetOS::windows) v = "C:\\SDKs\\AAX";
  266. else if (os == TargetOS::osx) v = "~/SDKs/AAX";
  267. else jassertfalse; // no AAX on this OS!
  268. }
  269. else if (key == Ids::androidSDKPath)
  270. {
  271. v = "${user.home}/Library/Android/sdk";
  272. }
  273. else if (key == Ids::androidNDKPath)
  274. {
  275. v = "${user.home}/Library/Android/sdk/ndk-bundle";
  276. }
  277. else if (key == Ids::clionExePath)
  278. {
  279. if (os == TargetOS::windows)
  280. {
  281. #if JUCE_WINDOWS
  282. auto regValue = WindowsRegistry::getValue ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Applications\\clion64.exe\\shell\\open\\command\\", {}, {});
  283. auto openCmd = StringArray::fromTokens (regValue, true);
  284. if (! openCmd.isEmpty())
  285. return Value (openCmd[0].unquoted());
  286. #endif
  287. v = "C:\\Program Files\\JetBrains\\CLion YYYY.MM.DD\\bin\\clion64.exe";
  288. }
  289. else if (os == TargetOS::osx)
  290. {
  291. v = "/Applications/CLion.app";
  292. }
  293. else
  294. {
  295. v = "${user.home}/clion/bin/clion.sh";
  296. }
  297. }
  298. else if (key == Ids::androidStudioExePath)
  299. {
  300. if (os == TargetOS::windows)
  301. {
  302. #if JUCE_WINDOWS
  303. auto path = WindowsRegistry::getValue ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Android Studio\\Path", {}, {});
  304. if (! path.isEmpty())
  305. return Value (path.unquoted() + "\\bin\\studio64.exe");
  306. #endif
  307. v = "C:\\Program Files\\Android\\Android Studio\\bin\\studio64.exe";
  308. }
  309. else if (os == TargetOS::osx)
  310. {
  311. v = "/Applications/Android Studio.app";
  312. }
  313. else
  314. {
  315. jassertfalse; // no Android Studio on this OS!
  316. }
  317. }
  318. }
  319. return v;
  320. }
  321. Identifier StoredSettings::identifierForOS (DependencyPathOS os) noexcept
  322. {
  323. if (os == TargetOS::osx) return Ids::osxFallback;
  324. else if (os == TargetOS::windows) return Ids::windowsFallback;
  325. else if (os == TargetOS::linux) return Ids::linuxFallback;
  326. jassertfalse;
  327. return {};
  328. }
  329. static bool doesSDKPathContainFile (const File& relativeTo, const String& path, const String& fileToCheckFor) noexcept
  330. {
  331. auto actualPath = path.replace ("${user.home}", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  332. return relativeTo.getChildFile (actualPath + "/" + fileToCheckFor).exists();
  333. }
  334. bool StoredSettings::isGlobalPathValid (const File& relativeTo, const Identifier& key, const String& path) const noexcept
  335. {
  336. String fileToCheckFor;
  337. if (key == Ids::vst3Path)
  338. {
  339. fileToCheckFor = "base/source/baseiids.cpp";
  340. }
  341. else if (key == Ids::rtasPath)
  342. {
  343. fileToCheckFor = "AlturaPorts/TDMPlugIns/PlugInLibrary/EffectClasses/CEffectProcessMIDI.cpp";
  344. }
  345. else if (key == Ids::aaxPath)
  346. {
  347. fileToCheckFor = "Interfaces/AAX_Exports.cpp";
  348. }
  349. else if (key == Ids::androidSDKPath)
  350. {
  351. #if JUCE_WINDOWS
  352. fileToCheckFor = "platform-tools/adb.exe";
  353. #else
  354. fileToCheckFor = "platform-tools/adb";
  355. #endif
  356. }
  357. else if (key == Ids::androidNDKPath)
  358. {
  359. #if JUCE_WINDOWS
  360. fileToCheckFor = "ndk-depends.cmd";
  361. #else
  362. fileToCheckFor = "ndk-depends";
  363. #endif
  364. }
  365. else if (key == Ids::defaultJuceModulePath)
  366. {
  367. fileToCheckFor = "juce_core";
  368. }
  369. else if (key == Ids::defaultUserModulePath)
  370. {
  371. fileToCheckFor = {};
  372. }
  373. else if (key == Ids::clionExePath)
  374. {
  375. #if JUCE_MAC
  376. fileToCheckFor = path.trim().endsWith (".app") ? "Contents/MacOS/clion" : "../clion";
  377. #elif JUCE_WINDOWS
  378. fileToCheckFor = "../clion64.exe";
  379. #else
  380. fileToCheckFor = "../clion.sh";
  381. #endif
  382. }
  383. else if (key == Ids::androidStudioExePath)
  384. {
  385. #if JUCE_MAC
  386. fileToCheckFor = "Android Studio.app";
  387. #elif JUCE_WINDOWS
  388. fileToCheckFor = "studio64.exe";
  389. #endif
  390. }
  391. else if (key == Ids::jucePath)
  392. {
  393. fileToCheckFor = "ChangeList.txt";
  394. }
  395. else
  396. {
  397. // didn't recognise the key provided!
  398. jassertfalse;
  399. return false;
  400. }
  401. return doesSDKPathContainFile (relativeTo, path, fileToCheckFor);
  402. }