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.

345 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. static const char* getApplicationUsageDataStateValue (LicenseState::ApplicationUsageData type)
  61. {
  62. switch (type)
  63. {
  64. case LicenseState::ApplicationUsageData::enabled: return "enabled";
  65. case LicenseState::ApplicationUsageData::disabled: return "disabled";
  66. case LicenseState::ApplicationUsageData::notChosenYet:
  67. default: return "notChosen";
  68. }
  69. }
  70. static LicenseState::ApplicationUsageData getApplicationUsageDataTypeFromValue (const String& value)
  71. {
  72. if (value == getApplicationUsageDataStateValue (LicenseState::ApplicationUsageData::enabled)) return LicenseState::ApplicationUsageData::enabled;
  73. if (value == getApplicationUsageDataStateValue (LicenseState::ApplicationUsageData::disabled)) return LicenseState::ApplicationUsageData::disabled;
  74. return LicenseState::ApplicationUsageData::notChosenYet;
  75. }
  76. #if ! JUCER_ENABLE_GPL_MODE
  77. struct LicenseController::ModalCompletionCallback : ModalComponentManager::Callback
  78. {
  79. ModalCompletionCallback (LicenseController& controller) : owner (controller) {}
  80. void modalStateFinished (int returnValue) override { owner.modalStateFinished (returnValue); }
  81. LicenseController& owner;
  82. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModalCompletionCallback)
  83. };
  84. #endif
  85. //==============================================================================
  86. LicenseController::LicenseController()
  87. : state (licenseStateFromSettings (ProjucerApplication::getApp().settings->getGlobalProperties()))
  88. {
  89. #if JUCER_ENABLE_GPL_MODE
  90. state.type = LicenseState::Type::GPL;
  91. state.username = "GPL mode";
  92. #endif
  93. }
  94. LicenseController::~LicenseController()
  95. {
  96. #if ! JUCER_ENABLE_GPL_MODE
  97. thread.reset();
  98. closeWebview (-1);
  99. #endif
  100. }
  101. LicenseState LicenseController::getState() const noexcept
  102. {
  103. LicenseState projucerState = state;
  104. // if the user has never logged in before and the user is running from command line
  105. // then we have no way to ask the user to log in, so fallback to GPL mode
  106. if (guiNotInitialisedYet
  107. && (state.type == LicenseState::Type::notLoggedIn
  108. || state.type == LicenseState::Type::noLicenseChosenYet))
  109. {
  110. projucerState.type = LicenseState::Type::GPL;
  111. projucerState.username = "GPL mode";
  112. }
  113. return projucerState;
  114. }
  115. void LicenseController::startWebviewIfNeeded()
  116. {
  117. if (guiNotInitialisedYet)
  118. {
  119. guiNotInitialisedYet = false;
  120. auto stateParam = getState();
  121. listeners.call ([&] (StateChangedCallback& l) { l.licenseStateChanged (stateParam); });
  122. }
  123. #if ! JUCER_ENABLE_GPL_MODE
  124. if (thread == nullptr)
  125. thread.reset (new LicenseThread (*this, false));
  126. #endif
  127. }
  128. void LicenseController::logout()
  129. {
  130. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  131. #if ! JUCER_ENABLE_GPL_MODE
  132. thread.reset();
  133. updateState ({});
  134. #if ! JUCE_LINUX
  135. WebBrowserComponent::clearCookies();
  136. #endif
  137. thread.reset (new LicenseThread (*this, false));
  138. #endif
  139. }
  140. void LicenseController::chooseNewLicense()
  141. {
  142. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  143. #if ! JUCER_ENABLE_GPL_MODE
  144. thread.reset();
  145. thread.reset (new LicenseThread (*this, true));
  146. #endif
  147. }
  148. void LicenseController::setApplicationUsageDataState (LicenseState::ApplicationUsageData newState)
  149. {
  150. if (state.applicationUsageDataState != newState)
  151. {
  152. state.applicationUsageDataState = newState;
  153. ProjucerApplication::getApp().setAnalyticsEnabled (newState == LicenseState::ApplicationUsageData::enabled);
  154. updateState (state);
  155. }
  156. }
  157. //==============================================================================
  158. #if ! JUCER_ENABLE_GPL_MODE
  159. void LicenseController::closeWebview (int result)
  160. {
  161. if (licenseWebview != nullptr)
  162. licenseWebview->exitModalState (result);
  163. }
  164. void LicenseController::modalStateFinished (int result)
  165. {
  166. licenseWebview = nullptr;
  167. if (result == -1 && (state.type == LicenseState::Type::notLoggedIn
  168. || state.type == LicenseState::Type::noLicenseChosenYet))
  169. JUCEApplication::getInstance()->systemRequestedQuit();
  170. }
  171. void LicenseController::ensureLicenseWebviewIsOpenWithPage (const String& param)
  172. {
  173. if (licenseWebview != nullptr)
  174. {
  175. licenseWebview->goToURL (param);
  176. licenseWebview->toFront (true);
  177. }
  178. else
  179. {
  180. #if ! JUCE_LINUX
  181. WebBrowserComponent::clearCookies();
  182. #endif
  183. licenseWebview = new LicenseWebview (new ModalCompletionCallback (*this), param);
  184. }
  185. }
  186. void LicenseController::queryWebview (const String& startURL, const String& valueToQuery,
  187. HashMap<String, String>& result)
  188. {
  189. ensureLicenseWebviewIsOpenWithPage (startURL);
  190. licenseWebview->setPageCallback ([this,valueToQuery,&result] (const String& cmd, const HashMap<String, String>& params)
  191. {
  192. if (valueToQuery.isEmpty() || cmd == valueToQuery)
  193. {
  194. result.clear();
  195. for (HashMap<String, String>::Iterator it = params.begin(); it != params.end(); ++it)
  196. result.set (it.getKey(), it.getValue());
  197. if (thread != nullptr && ! thread->threadShouldExit())
  198. thread->finished.signal();
  199. }
  200. });
  201. licenseWebview->setNewWindowCallback ([this, &result] (const String& url)
  202. {
  203. if (url.endsWith ("get-juce/indie") || url.endsWith ("get-juce/pro"))
  204. {
  205. result.clear();
  206. result.set ("page-redirect", url);
  207. if (thread != nullptr && ! thread->threadShouldExit())
  208. thread->finished.signal();
  209. }
  210. });
  211. }
  212. #endif
  213. void LicenseController::updateState (const LicenseState& newState)
  214. {
  215. auto& props = ProjucerApplication::getApp().settings->getGlobalProperties();
  216. auto oldLicenseType = state.type;
  217. state = newState;
  218. licenseStateToSettings (state, props);
  219. auto stateParam = getState();
  220. listeners.call ([&] (StateChangedCallback& l) { l.licenseStateChanged (stateParam); });
  221. if (oldLicenseType != state.type)
  222. {
  223. StringPairArray data;
  224. data.set ("label", state.licenseTypeToString (state.type));
  225. Analytics::getInstance()->logEvent ("License Type", data, ProjucerAnalyticsEvent::userEvent);
  226. }
  227. }
  228. LicenseState LicenseController::licenseStateFromOldSettings (XmlElement* licenseXml)
  229. {
  230. LicenseState result;
  231. result.type = getLicenseTypeFromValue (licenseXml->getChildElementAllSubText ("type", {}));
  232. result.applicationUsageDataState = getApplicationUsageDataTypeFromValue (licenseXml->getChildElementAllSubText ("applicationUsageData", {}));
  233. result.username = licenseXml->getChildElementAllSubText ("username", {});
  234. result.email = licenseXml->getChildElementAllSubText ("email", {});
  235. result.authToken = licenseXml->getChildElementAllSubText ("authToken", {});
  236. MemoryOutputStream imageData;
  237. Base64::convertFromBase64 (imageData, licenseXml->getChildElementAllSubText ("avatar", {}));
  238. result.avatar = ImageFileFormat::loadFrom (imageData.getData(), imageData.getDataSize());
  239. return result;
  240. }
  241. LicenseState LicenseController::licenseStateFromSettings (PropertiesFile& props)
  242. {
  243. if (auto licenseXml = props.getXmlValue ("license"))
  244. {
  245. // this is here for backwards compatibility with old-style settings files using XML text elements
  246. if (licenseXml->getChildElementAllSubText ("type", {}) != String())
  247. {
  248. auto stateFromOldSettings = licenseStateFromOldSettings (licenseXml.get());
  249. licenseStateToSettings (stateFromOldSettings, props);
  250. return stateFromOldSettings;
  251. }
  252. LicenseState result;
  253. result.type = getLicenseTypeFromValue (licenseXml->getStringAttribute ("type", {}));
  254. result.applicationUsageDataState = getApplicationUsageDataTypeFromValue (licenseXml->getStringAttribute ("applicationUsageData", {}));
  255. result.username = licenseXml->getStringAttribute ("username", {});
  256. result.email = licenseXml->getStringAttribute ("email", {});
  257. result.authToken = licenseXml->getStringAttribute ("authToken", {});
  258. MemoryOutputStream imageData;
  259. Base64::convertFromBase64 (imageData, licenseXml->getStringAttribute ("avatar", {}));
  260. result.avatar = ImageFileFormat::loadFrom (imageData.getData(), imageData.getDataSize());
  261. return result;
  262. }
  263. return {};
  264. }
  265. void LicenseController::licenseStateToSettings (const LicenseState& state, PropertiesFile& props)
  266. {
  267. props.removeValue ("license");
  268. if (state.type != LicenseState::Type::notLoggedIn && state.username.isNotEmpty())
  269. {
  270. XmlElement licenseXml ("license");
  271. if (auto* typeString = getLicenseStateValue (state.type))
  272. licenseXml.setAttribute ("type", typeString);
  273. licenseXml.setAttribute ("applicationUsageData", getApplicationUsageDataStateValue (state.applicationUsageDataState));
  274. licenseXml.setAttribute ("username", state.username);
  275. licenseXml.setAttribute ("email", state.email);
  276. licenseXml.setAttribute ("authToken", state.authToken);
  277. MemoryOutputStream imageData;
  278. if (state.avatar.isValid() && PNGImageFormat().writeImageToStream (state.avatar, imageData))
  279. licenseXml.setAttribute ("avatar", Base64::toBase64 (imageData.getData(), imageData.getDataSize()));
  280. props.setValue ("license", &licenseXml);
  281. }
  282. props.saveIfNeeded();
  283. }