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.

303 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - 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 this technical preview, this file is not subject to commercial licensing.
  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 "../Application/jucer_Application.h"
  15. #include "../ProjectSaving/jucer_ProjectExporter.h"
  16. #include "../Project/UI/jucer_HeaderComponent.h"
  17. #include "jucer_LicenseController.h"
  18. #if ! JUCER_ENABLE_GPL_MODE
  19. #include "jucer_LicenseWebview.h"
  20. #include "jucer_LicenseThread.h"
  21. #endif
  22. //==============================================================================
  23. const char* LicenseState::licenseTypeToString (LicenseState::Type type)
  24. {
  25. switch (type)
  26. {
  27. case Type::notLoggedIn: return "<notLoggedIn>";
  28. case Type::noLicenseChosenYet: return "<noLicenseChosenYet>";
  29. case Type::GPL: return "JUCE GPL";
  30. case Type::personal: return "JUCE Personal";
  31. case Type::edu: return "JUCE Education";
  32. case Type::indie: return "JUCE Indie";
  33. case Type::pro: return "JUCE Pro";
  34. default: return "<unknown>";
  35. }
  36. }
  37. static const char* getLicenseStateValue (LicenseState::Type type)
  38. {
  39. switch (type)
  40. {
  41. case LicenseState::Type::GPL: return "GPL";
  42. case LicenseState::Type::personal: return "personal";
  43. case LicenseState::Type::edu: return "edu";
  44. case LicenseState::Type::indie: return "indie";
  45. case LicenseState::Type::pro: return "pro";
  46. case LicenseState::Type::notLoggedIn:
  47. case LicenseState::Type::noLicenseChosenYet:
  48. default: return nullptr;
  49. }
  50. }
  51. static LicenseState::Type getLicenseTypeFromValue (const String& d)
  52. {
  53. if (d == getLicenseStateValue (LicenseState::Type::GPL)) return LicenseState::Type::GPL;
  54. if (d == getLicenseStateValue (LicenseState::Type::personal)) return LicenseState::Type::personal;
  55. if (d == getLicenseStateValue (LicenseState::Type::edu)) return LicenseState::Type::edu;
  56. if (d == getLicenseStateValue (LicenseState::Type::indie)) return LicenseState::Type::indie;
  57. if (d == getLicenseStateValue (LicenseState::Type::pro)) return LicenseState::Type::pro;
  58. return LicenseState::Type::noLicenseChosenYet;
  59. }
  60. #if ! JUCER_ENABLE_GPL_MODE
  61. struct LicenseController::ModalCompletionCallback : ModalComponentManager::Callback
  62. {
  63. ModalCompletionCallback (LicenseController& controller) : owner (controller) {}
  64. void modalStateFinished (int returnValue) override { owner.modalStateFinished (returnValue); }
  65. LicenseController& owner;
  66. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModalCompletionCallback)
  67. };
  68. #endif
  69. //==============================================================================
  70. LicenseController::LicenseController()
  71. : state (licenseStateFromSettings (ProjucerApplication::getApp().settings->getGlobalProperties()))
  72. {
  73. #if JUCER_ENABLE_GPL_MODE
  74. state.type = LicenseState::Type::GPL;
  75. state.username = "GPL mode";
  76. #endif
  77. }
  78. LicenseController::~LicenseController()
  79. {
  80. #if ! JUCER_ENABLE_GPL_MODE
  81. thread.reset();
  82. closeWebview (-1);
  83. #endif
  84. }
  85. LicenseState LicenseController::getState() const noexcept
  86. {
  87. LicenseState projucerState = state;
  88. // if the user has never logged in before and the user is running from command line
  89. // then we have no way to ask the user to log in, so fallback to GPL mode
  90. if (guiNotInitialisedYet
  91. && (state.type == LicenseState::Type::notLoggedIn
  92. || state.type == LicenseState::Type::noLicenseChosenYet))
  93. {
  94. projucerState.type = LicenseState::Type::GPL;
  95. projucerState.username = "GPL mode";
  96. }
  97. return projucerState;
  98. }
  99. void LicenseController::startWebviewIfNeeded()
  100. {
  101. if (guiNotInitialisedYet)
  102. {
  103. guiNotInitialisedYet = false;
  104. auto stateParam = getState();
  105. listeners.call ([&] (StateChangedCallback& l) { l.licenseStateChanged (stateParam); });
  106. }
  107. #if ! JUCER_ENABLE_GPL_MODE
  108. if (thread == nullptr)
  109. thread.reset (new LicenseThread (*this, false));
  110. #endif
  111. }
  112. void LicenseController::logout()
  113. {
  114. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  115. #if ! JUCER_ENABLE_GPL_MODE
  116. thread.reset();
  117. updateState ({});
  118. #if ! JUCE_LINUX
  119. WebBrowserComponent::clearCookies();
  120. #endif
  121. thread.reset (new LicenseThread (*this, false));
  122. #endif
  123. }
  124. void LicenseController::chooseNewLicense()
  125. {
  126. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  127. #if ! JUCER_ENABLE_GPL_MODE
  128. thread.reset();
  129. thread.reset (new LicenseThread (*this, true));
  130. #endif
  131. }
  132. //==============================================================================
  133. #if ! JUCER_ENABLE_GPL_MODE
  134. void LicenseController::closeWebview (int result)
  135. {
  136. if (licenseWebview != nullptr)
  137. licenseWebview->exitModalState (result);
  138. }
  139. void LicenseController::modalStateFinished (int result)
  140. {
  141. licenseWebview = nullptr;
  142. if (result == -1 && (state.type == LicenseState::Type::notLoggedIn
  143. || state.type == LicenseState::Type::noLicenseChosenYet))
  144. JUCEApplication::getInstance()->systemRequestedQuit();
  145. }
  146. void LicenseController::ensureLicenseWebviewIsOpenWithPage (const String& param)
  147. {
  148. if (licenseWebview != nullptr)
  149. {
  150. licenseWebview->goToURL (param);
  151. licenseWebview->toFront (true);
  152. }
  153. else
  154. {
  155. #if ! JUCE_LINUX
  156. WebBrowserComponent::clearCookies();
  157. #endif
  158. licenseWebview = new LicenseWebview (new ModalCompletionCallback (*this), param);
  159. }
  160. }
  161. void LicenseController::queryWebview (const String& startURL, const String& valueToQuery,
  162. HashMap<String, String>& result)
  163. {
  164. ensureLicenseWebviewIsOpenWithPage (startURL);
  165. licenseWebview->setPageCallback ([this,valueToQuery,&result] (const String& cmd, const HashMap<String, String>& params)
  166. {
  167. if (valueToQuery.isEmpty() || cmd == valueToQuery)
  168. {
  169. result.clear();
  170. for (HashMap<String, String>::Iterator it = params.begin(); it != params.end(); ++it)
  171. result.set (it.getKey(), it.getValue());
  172. if (thread != nullptr && ! thread->threadShouldExit())
  173. thread->finished.signal();
  174. }
  175. });
  176. licenseWebview->setNewWindowCallback ([this, &result] (const String& url)
  177. {
  178. if (url.endsWith ("get-juce/indie") || url.endsWith ("get-juce/pro"))
  179. {
  180. result.clear();
  181. result.set ("page-redirect", url);
  182. if (thread != nullptr && ! thread->threadShouldExit())
  183. thread->finished.signal();
  184. }
  185. });
  186. }
  187. #endif
  188. void LicenseController::updateState (const LicenseState& newState)
  189. {
  190. auto& props = ProjucerApplication::getApp().settings->getGlobalProperties();
  191. state = newState;
  192. licenseStateToSettings (state, props);
  193. auto stateParam = getState();
  194. listeners.call ([&] (StateChangedCallback& l) { l.licenseStateChanged (stateParam); });
  195. }
  196. LicenseState LicenseController::licenseStateFromOldSettings (XmlElement* licenseXml)
  197. {
  198. LicenseState result;
  199. result.type = getLicenseTypeFromValue (licenseXml->getChildElementAllSubText ("type", {}));
  200. result.username = licenseXml->getChildElementAllSubText ("username", {});
  201. result.email = licenseXml->getChildElementAllSubText ("email", {});
  202. result.authToken = licenseXml->getChildElementAllSubText ("authToken", {});
  203. MemoryOutputStream imageData;
  204. Base64::convertFromBase64 (imageData, licenseXml->getChildElementAllSubText ("avatar", {}));
  205. result.avatar = ImageFileFormat::loadFrom (imageData.getData(), imageData.getDataSize());
  206. return result;
  207. }
  208. LicenseState LicenseController::licenseStateFromSettings (PropertiesFile& props)
  209. {
  210. if (auto licenseXml = props.getXmlValue ("license"))
  211. {
  212. // this is here for backwards compatibility with old-style settings files using XML text elements
  213. if (licenseXml->getChildElementAllSubText ("type", {}) != String())
  214. {
  215. auto stateFromOldSettings = licenseStateFromOldSettings (licenseXml.get());
  216. licenseStateToSettings (stateFromOldSettings, props);
  217. return stateFromOldSettings;
  218. }
  219. LicenseState result;
  220. result.type = getLicenseTypeFromValue (licenseXml->getStringAttribute ("type", {}));
  221. result.username = licenseXml->getStringAttribute ("username", {});
  222. result.email = licenseXml->getStringAttribute ("email", {});
  223. result.authToken = licenseXml->getStringAttribute ("authToken", {});
  224. MemoryOutputStream imageData;
  225. Base64::convertFromBase64 (imageData, licenseXml->getStringAttribute ("avatar", {}));
  226. result.avatar = ImageFileFormat::loadFrom (imageData.getData(), imageData.getDataSize());
  227. return result;
  228. }
  229. return {};
  230. }
  231. void LicenseController::licenseStateToSettings (const LicenseState& state, PropertiesFile& props)
  232. {
  233. props.removeValue ("license");
  234. if (state.type != LicenseState::Type::notLoggedIn && state.username.isNotEmpty())
  235. {
  236. XmlElement licenseXml ("license");
  237. if (auto* typeString = getLicenseStateValue (state.type))
  238. licenseXml.setAttribute ("type", typeString);
  239. licenseXml.setAttribute ("username", state.username);
  240. licenseXml.setAttribute ("email", state.email);
  241. licenseXml.setAttribute ("authToken", state.authToken);
  242. MemoryOutputStream imageData;
  243. if (state.avatar.isValid() && PNGImageFormat().writeImageToStream (state.avatar, imageData))
  244. licenseXml.setAttribute ("avatar", Base64::toBase64 (imageData.getData(), imageData.getDataSize()));
  245. props.setValue ("license", &licenseXml);
  246. }
  247. props.saveIfNeeded();
  248. }