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.

319 lines
9.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_StoredSettings.h"
  19. #include "../Application/jucer_Application.h"
  20. #include "../Application/jucer_GlobalPreferences.h"
  21. //==============================================================================
  22. StoredSettings& getAppSettings()
  23. {
  24. return *IntrojucerApp::getApp().settings;
  25. }
  26. PropertiesFile& getGlobalProperties()
  27. {
  28. return getAppSettings().getGlobalProperties();
  29. }
  30. //==============================================================================
  31. StoredSettings::StoredSettings()
  32. : appearance (true), projectDefaults ("PROJECT_DEFAULT_SETTINGS")
  33. {
  34. reload();
  35. projectDefaults.addListener (this);
  36. }
  37. StoredSettings::~StoredSettings()
  38. {
  39. projectDefaults.removeListener (this);
  40. flush();
  41. }
  42. PropertiesFile& StoredSettings::getGlobalProperties()
  43. {
  44. return *propertyFiles.getUnchecked (0);
  45. }
  46. static PropertiesFile* createPropsFile (const String& filename)
  47. {
  48. return new PropertiesFile (IntrojucerApp::getApp()
  49. .getPropertyFileOptionsFor (filename));
  50. }
  51. PropertiesFile& StoredSettings::getProjectProperties (const String& projectUID)
  52. {
  53. const String filename ("Introjucer_Project_" + projectUID);
  54. for (int i = propertyFiles.size(); --i >= 0;)
  55. {
  56. PropertiesFile* const props = propertyFiles.getUnchecked(i);
  57. if (props->getFile().getFileNameWithoutExtension() == filename)
  58. return *props;
  59. }
  60. PropertiesFile* p = createPropsFile (filename);
  61. propertyFiles.add (p);
  62. return *p;
  63. }
  64. void StoredSettings::updateGlobalPreferences()
  65. {
  66. // update global settings editable from the global preferences window
  67. updateAppearanceSettings();
  68. // update 'invisible' global settings
  69. updateRecentFiles();
  70. updateKeyMappings();
  71. }
  72. void StoredSettings::updateAppearanceSettings()
  73. {
  74. const ScopedPointer<XmlElement> xml (appearance.settings.createXml());
  75. getGlobalProperties().setValue ("editorColours", xml);
  76. }
  77. void StoredSettings::updateRecentFiles()
  78. {
  79. getGlobalProperties().setValue ("recentFiles", recentFiles.toString());
  80. }
  81. void StoredSettings::updateKeyMappings()
  82. {
  83. getGlobalProperties().removeValue ("keyMappings");
  84. if (ApplicationCommandManager* commandManager = IntrojucerApp::getApp().commandManager)
  85. {
  86. const ScopedPointer<XmlElement> keys (commandManager->getKeyMappings()->createXml (true));
  87. if (keys != nullptr)
  88. getGlobalProperties().setValue ("keyMappings", keys);
  89. }
  90. }
  91. void StoredSettings::flush()
  92. {
  93. updateGlobalPreferences();
  94. saveSwatchColours();
  95. for (int i = propertyFiles.size(); --i >= 0;)
  96. propertyFiles.getUnchecked(i)->saveIfNeeded();
  97. }
  98. void StoredSettings::reload()
  99. {
  100. propertyFiles.clear();
  101. propertyFiles.add (createPropsFile ("Introjucer"));
  102. ScopedPointer<XmlElement> projectDefaultsXml (propertyFiles.getFirst()->getXmlValue ("PROJECT_DEFAULT_SETTINGS"));
  103. if (projectDefaultsXml != nullptr)
  104. projectDefaults = ValueTree::fromXml (*projectDefaultsXml);
  105. // recent files...
  106. recentFiles.restoreFromString (getGlobalProperties().getValue ("recentFiles"));
  107. recentFiles.removeNonExistentFiles();
  108. ScopedPointer<XmlElement> xml (getGlobalProperties().getXmlValue ("editorColours"));
  109. if (xml == nullptr)
  110. {
  111. xml = XmlDocument::parse (BinaryData::colourscheme_dark_xml);
  112. jassert (xml != nullptr);
  113. }
  114. appearance.readFromXML (*xml);
  115. appearance.updateColourScheme();
  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. //==============================================================================
  135. void StoredSettings::loadSwatchColours()
  136. {
  137. swatchColours.clear();
  138. #define COL(col) Colours::col,
  139. const Colour colours[] =
  140. {
  141. #include "jucer_Colours.h"
  142. Colours::transparentBlack
  143. };
  144. #undef COL
  145. const int numSwatchColours = 24;
  146. PropertiesFile& props = getGlobalProperties();
  147. for (int i = 0; i < numSwatchColours; ++i)
  148. swatchColours.add (Colour::fromString (props.getValue ("swatchColour" + String (i),
  149. colours [2 + i].toString())));
  150. }
  151. void StoredSettings::saveSwatchColours()
  152. {
  153. PropertiesFile& props = getGlobalProperties();
  154. for (int i = 0; i < swatchColours.size(); ++i)
  155. props.setValue ("swatchColour" + String (i), swatchColours.getReference(i).toString());
  156. }
  157. int StoredSettings::ColourSelectorWithSwatches::getNumSwatches() const
  158. {
  159. return getAppSettings().swatchColours.size();
  160. }
  161. Colour StoredSettings::ColourSelectorWithSwatches::getSwatchColour (int index) const
  162. {
  163. return getAppSettings().swatchColours [index];
  164. }
  165. void StoredSettings::ColourSelectorWithSwatches::setSwatchColour (int index, const Colour& newColour) const
  166. {
  167. getAppSettings().swatchColours.set (index, newColour);
  168. }
  169. //==============================================================================
  170. static bool doesSDKPathContainFile (const String& path, const String& fileToCheckFor)
  171. {
  172. return File::getCurrentWorkingDirectory().getChildFile( path + "/" + fileToCheckFor).existsAsFile();
  173. }
  174. Value StoredSettings::getGlobalPath (const Identifier& key, DependencyPathOS os)
  175. {
  176. Value v (projectDefaults.getPropertyAsValue (key, nullptr));
  177. if (v.toString().isEmpty())
  178. v = getFallbackPath (key, os);
  179. return v;
  180. }
  181. String StoredSettings::getFallbackPath (const Identifier& key, DependencyPathOS os)
  182. {
  183. if (key == Ids::vst2Path || key == Ids::vst3Path)
  184. return os == TargetOS::windows ? "c:\\SDKs\\VST3 SDK"
  185. : "~/SDKs/VST3 SDK";
  186. if (key == Ids::rtasPath)
  187. {
  188. if (os == TargetOS::windows) return "c:\\SDKs\\PT_80_SDK";
  189. if (os == TargetOS::osx) return "~/SDKs/PT_80_SDK";
  190. // no RTAS on this OS!
  191. jassertfalse;
  192. return String();
  193. }
  194. if (key == Ids::aaxPath)
  195. {
  196. if (os == TargetOS::windows) return "c:\\SDKs\\AAX";
  197. if (os == TargetOS::osx) return "~/SDKs/AAX" ;
  198. // no AAX on this OS!
  199. jassertfalse;
  200. return String();
  201. }
  202. if (key == Ids::androidSDKPath)
  203. return os == TargetOS::windows ? "c:\\SDKs\\android-sdk"
  204. : "~/Library/Android/sdk";
  205. if (key == Ids::androidNDKPath)
  206. return os == TargetOS::windows ? "c:\\SDKs\\android-ndk"
  207. : "~/Library/Android/ndk";
  208. // didn't recognise the key provided!
  209. jassertfalse;
  210. return String();
  211. }
  212. bool StoredSettings::isGlobalPathValid (const Identifier& key, const String& path)
  213. {
  214. String fileToCheckFor;
  215. if (key == Ids::vst2Path)
  216. {
  217. fileToCheckFor = "public.sdk/source/vst2.x/audioeffectx.h";
  218. }
  219. else if (key == Ids::vst3Path)
  220. {
  221. fileToCheckFor = "base/source/baseiids.cpp";
  222. }
  223. else if (key == Ids::rtasPath)
  224. {
  225. fileToCheckFor = "AlturaPorts/TDMPlugIns/PlugInLibrary/EffectClasses/CEffectProcessMIDI.cpp";
  226. }
  227. else if (key == Ids::aaxPath)
  228. {
  229. fileToCheckFor = "Interfaces/AAX_Exports.cpp";
  230. }
  231. else if (key == Ids::androidSDKPath)
  232. {
  233. #if JUCE_WINDOWS
  234. fileToCheckFor = "platform-tools/adb.exe";
  235. #else
  236. fileToCheckFor = "platform-tools/adb";
  237. #endif
  238. }
  239. else if (key == Ids::androidNDKPath)
  240. {
  241. #if JUCE_WINDOWS
  242. fileToCheckFor = "ndk-depends.exe";
  243. #else
  244. fileToCheckFor = "ndk-depends";
  245. #endif
  246. }
  247. else
  248. {
  249. // didn't recognise the key provided!
  250. jassertfalse;
  251. return false;
  252. }
  253. return doesSDKPathContainFile (path, fileToCheckFor);
  254. }