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.

478 lines
15KB

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