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.

441 lines
14KB

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