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.

463 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  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::aaxPath)
  216. {
  217. fileToCheckFor = "Interfaces/AAX_Exports.cpp";
  218. }
  219. else if (key == Ids::araPath)
  220. {
  221. fileToCheckFor = "ARA_API/ARAInterface.h";
  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::defaultJuceModulePath)
  232. {
  233. fileToCheckFor = "juce_core";
  234. }
  235. else if (key == Ids::defaultUserModulePath)
  236. {
  237. fileToCheckFor = {};
  238. }
  239. else if (key == Ids::clionExePath)
  240. {
  241. #if JUCE_MAC
  242. fileToCheckFor = path.trim().endsWith (".app") ? "Contents/MacOS/clion" : "../clion";
  243. #elif JUCE_WINDOWS
  244. fileToCheckFor = "../clion64.exe";
  245. #else
  246. fileToCheckFor = "../clion.sh";
  247. #endif
  248. }
  249. else if (key == Ids::androidStudioExePath)
  250. {
  251. #if JUCE_MAC
  252. fileToCheckFor = "Android Studio.app";
  253. #elif JUCE_WINDOWS
  254. fileToCheckFor = "studio64.exe";
  255. #endif
  256. }
  257. else if (key == Ids::jucePath)
  258. {
  259. fileToCheckFor = "ChangeList.txt";
  260. }
  261. else
  262. {
  263. // didn't recognise the key provided!
  264. jassertfalse;
  265. return false;
  266. }
  267. return doesSDKPathContainFile (relativeTo, path, fileToCheckFor);
  268. }
  269. void StoredSettings::checkJUCEPaths()
  270. {
  271. auto moduleFolder = getStoredPath (Ids::defaultJuceModulePath, TargetOS::getThisOS()).get().toString();
  272. auto juceFolder = getStoredPath (Ids::jucePath, TargetOS::getThisOS()).get().toString();
  273. auto validModuleFolder = isGlobalPathValid ({}, Ids::defaultJuceModulePath, moduleFolder);
  274. auto validJuceFolder = isGlobalPathValid ({}, Ids::jucePath, juceFolder);
  275. if (validModuleFolder && ! validJuceFolder)
  276. projectDefaults.getPropertyAsValue (Ids::jucePath, nullptr) = File (moduleFolder).getParentDirectory().getFullPathName();
  277. else if (! validModuleFolder && validJuceFolder)
  278. projectDefaults.getPropertyAsValue (Ids::defaultJuceModulePath, nullptr) = File (juceFolder).getChildFile ("modules").getFullPathName();
  279. }
  280. bool StoredSettings::isJUCEPathIncorrect()
  281. {
  282. return ! isGlobalPathValid ({}, Ids::jucePath, getStoredPath (Ids::jucePath, TargetOS::getThisOS()).get().toString());
  283. }
  284. static String getFallbackPathForOS (const Identifier& key, DependencyPathOS os)
  285. {
  286. if (key == Ids::jucePath)
  287. {
  288. return (os == TargetOS::windows ? "C:\\JUCE" : "~/JUCE");
  289. }
  290. else if (key == Ids::defaultJuceModulePath)
  291. {
  292. return (os == TargetOS::windows ? "C:\\JUCE\\modules" : "~/JUCE/modules");
  293. }
  294. else if (key == Ids::defaultUserModulePath)
  295. {
  296. return (os == TargetOS::windows ? "C:\\modules" : "~/modules");
  297. }
  298. else if (key == Ids::vstLegacyPath)
  299. {
  300. return {};
  301. }
  302. else if (key == Ids::aaxPath)
  303. {
  304. if (os == TargetOS::windows) return "C:\\SDKs\\AAX";
  305. else if (os == TargetOS::osx) return "~/SDKs/AAX";
  306. else return {}; // no AAX on this OS!
  307. }
  308. else if (key == Ids::araPath)
  309. {
  310. if (os == TargetOS::windows) return "C:\\SDKs\\ARA_SDK";
  311. else if (os == TargetOS::osx) return "~/SDKs/ARA_SDK";
  312. else return {};
  313. }
  314. else if (key == Ids::androidSDKPath)
  315. {
  316. if (os == TargetOS::windows) return "${user.home}\\AppData\\Local\\Android\\Sdk";
  317. else if (os == TargetOS::osx) return "${user.home}/Library/Android/sdk";
  318. else if (os == TargetOS::linux) return "${user.home}/Android/Sdk";
  319. jassertfalse;
  320. return {};
  321. }
  322. else if (key == Ids::clionExePath)
  323. {
  324. if (os == TargetOS::windows)
  325. {
  326. #if JUCE_WINDOWS
  327. auto regValue = WindowsRegistry::getValue ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Applications\\clion64.exe\\shell\\open\\command\\", {}, {});
  328. auto openCmd = StringArray::fromTokens (regValue, true);
  329. if (! openCmd.isEmpty())
  330. return openCmd[0].unquoted();
  331. #endif
  332. return "C:\\Program Files\\JetBrains\\CLion YYYY.MM.DD\\bin\\clion64.exe";
  333. }
  334. else if (os == TargetOS::osx)
  335. {
  336. return "/Applications/CLion.app";
  337. }
  338. else
  339. {
  340. return "${user.home}/clion/bin/clion.sh";
  341. }
  342. }
  343. else if (key == Ids::androidStudioExePath)
  344. {
  345. if (os == TargetOS::windows)
  346. {
  347. #if JUCE_WINDOWS
  348. auto path = WindowsRegistry::getValue ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Android Studio\\Path", {}, {});
  349. if (! path.isEmpty())
  350. return path.unquoted() + "\\bin\\studio64.exe";
  351. #endif
  352. return "C:\\Program Files\\Android\\Android Studio\\bin\\studio64.exe";
  353. }
  354. else if (os == TargetOS::osx)
  355. {
  356. return "/Applications/Android Studio.app";
  357. }
  358. else
  359. {
  360. return {}; // no Android Studio on this OS!
  361. }
  362. }
  363. // unknown key!
  364. jassertfalse;
  365. return {};
  366. }
  367. static Identifier identifierForOS (DependencyPathOS os) noexcept
  368. {
  369. if (os == TargetOS::osx) return Ids::osxFallback;
  370. else if (os == TargetOS::windows) return Ids::windowsFallback;
  371. else if (os == TargetOS::linux) return Ids::linuxFallback;
  372. jassertfalse;
  373. return {};
  374. }
  375. ValueTreePropertyWithDefault StoredSettings::getStoredPath (const Identifier& key, DependencyPathOS os)
  376. {
  377. auto tree = (os == TargetOS::getThisOS() ? projectDefaults
  378. : fallbackPaths.getOrCreateChildWithName (identifierForOS (os), nullptr));
  379. return { tree, key, nullptr, getFallbackPathForOS (key, os) };
  380. }
  381. void StoredSettings::addProjectDefaultsListener (ValueTree::Listener& l) { projectDefaults.addListener (&l); }
  382. void StoredSettings::removeProjectDefaultsListener (ValueTree::Listener& l) { projectDefaults.removeListener (&l); }
  383. void StoredSettings::addFallbackPathsListener (ValueTree::Listener& l) { fallbackPaths.addListener (&l); }
  384. void StoredSettings::removeFallbackPathsListener (ValueTree::Listener& l) { fallbackPaths.removeListener (&l); }