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.

464 lines
15KB

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