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.

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