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.

437 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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::aaxPath)
  221. {
  222. fileToCheckFor = "Interfaces/AAX_Exports.cpp";
  223. }
  224. else if (key == Ids::araPath)
  225. {
  226. fileToCheckFor = "ARA_API/ARAInterface.h";
  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::androidStudioExePath)
  245. {
  246. #if JUCE_MAC
  247. fileToCheckFor = "Android Studio.app";
  248. #elif JUCE_WINDOWS
  249. fileToCheckFor = "studio64.exe";
  250. #endif
  251. }
  252. else if (key == Ids::jucePath)
  253. {
  254. fileToCheckFor = "CHANGE_LIST.md";
  255. }
  256. else
  257. {
  258. // didn't recognise the key provided!
  259. jassertfalse;
  260. return false;
  261. }
  262. return doesSDKPathContainFile (relativeTo, path, fileToCheckFor);
  263. }
  264. void StoredSettings::checkJUCEPaths()
  265. {
  266. auto moduleFolder = getStoredPath (Ids::defaultJuceModulePath, TargetOS::getThisOS()).get().toString();
  267. auto juceFolder = getStoredPath (Ids::jucePath, TargetOS::getThisOS()).get().toString();
  268. auto validModuleFolder = isGlobalPathValid ({}, Ids::defaultJuceModulePath, moduleFolder);
  269. auto validJuceFolder = isGlobalPathValid ({}, Ids::jucePath, juceFolder);
  270. if (validModuleFolder && ! validJuceFolder)
  271. projectDefaults.getPropertyAsValue (Ids::jucePath, nullptr) = File (moduleFolder).getParentDirectory().getFullPathName();
  272. else if (! validModuleFolder && validJuceFolder)
  273. projectDefaults.getPropertyAsValue (Ids::defaultJuceModulePath, nullptr) = File (juceFolder).getChildFile ("modules").getFullPathName();
  274. }
  275. bool StoredSettings::isJUCEPathIncorrect()
  276. {
  277. return ! isGlobalPathValid ({}, Ids::jucePath, getStoredPath (Ids::jucePath, TargetOS::getThisOS()).get().toString());
  278. }
  279. static String getFallbackPathForOS (const Identifier& key, DependencyPathOS os)
  280. {
  281. if (key == Ids::jucePath)
  282. {
  283. return (os == TargetOS::windows ? "C:\\JUCE" : "~/JUCE");
  284. }
  285. else if (key == Ids::defaultJuceModulePath)
  286. {
  287. return (os == TargetOS::windows ? "C:\\JUCE\\modules" : "~/JUCE/modules");
  288. }
  289. else if (key == Ids::defaultUserModulePath)
  290. {
  291. return (os == TargetOS::windows ? "C:\\modules" : "~/modules");
  292. }
  293. else if (key == Ids::vstLegacyPath)
  294. {
  295. return {};
  296. }
  297. else if (key == Ids::aaxPath)
  298. {
  299. if (os == TargetOS::windows) return "C:\\SDKs\\AAX";
  300. else if (os == TargetOS::osx) return "~/SDKs/AAX";
  301. else return {}; // no AAX on this OS!
  302. }
  303. else if (key == Ids::araPath)
  304. {
  305. if (os == TargetOS::windows) return "C:\\SDKs\\ARA_SDK";
  306. else if (os == TargetOS::osx) return "~/SDKs/ARA_SDK";
  307. else return {};
  308. }
  309. else if (key == Ids::androidSDKPath)
  310. {
  311. if (os == TargetOS::windows) return "${user.home}\\AppData\\Local\\Android\\Sdk";
  312. else if (os == TargetOS::osx) return "${user.home}/Library/Android/sdk";
  313. else if (os == TargetOS::linux) return "${user.home}/Android/Sdk";
  314. jassertfalse;
  315. return {};
  316. }
  317. else if (key == Ids::androidStudioExePath)
  318. {
  319. if (os == TargetOS::windows)
  320. {
  321. #if JUCE_WINDOWS
  322. auto path = WindowsRegistry::getValue ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Android Studio\\Path", {}, {});
  323. if (! path.isEmpty())
  324. return path.unquoted() + "\\bin\\studio64.exe";
  325. #endif
  326. return "C:\\Program Files\\Android\\Android Studio\\bin\\studio64.exe";
  327. }
  328. else if (os == TargetOS::osx)
  329. {
  330. return "/Applications/Android Studio.app";
  331. }
  332. else
  333. {
  334. return {}; // no Android Studio on this OS!
  335. }
  336. }
  337. // unknown key!
  338. jassertfalse;
  339. return {};
  340. }
  341. static Identifier identifierForOS (DependencyPathOS os) noexcept
  342. {
  343. if (os == TargetOS::osx) return Ids::osxFallback;
  344. else if (os == TargetOS::windows) return Ids::windowsFallback;
  345. else if (os == TargetOS::linux) return Ids::linuxFallback;
  346. jassertfalse;
  347. return {};
  348. }
  349. ValueTreePropertyWithDefault StoredSettings::getStoredPath (const Identifier& key, DependencyPathOS os)
  350. {
  351. auto tree = (os == TargetOS::getThisOS() ? projectDefaults
  352. : fallbackPaths.getOrCreateChildWithName (identifierForOS (os), nullptr));
  353. return { tree, key, nullptr, getFallbackPathForOS (key, os) };
  354. }
  355. void StoredSettings::addProjectDefaultsListener (ValueTree::Listener& l) { projectDefaults.addListener (&l); }
  356. void StoredSettings::removeProjectDefaultsListener (ValueTree::Listener& l) { projectDefaults.removeListener (&l); }
  357. void StoredSettings::addFallbackPathsListener (ValueTree::Listener& l) { fallbackPaths.addListener (&l); }
  358. void StoredSettings::removeFallbackPathsListener (ValueTree::Listener& l) { fallbackPaths.removeListener (&l); }