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.

352 lines
13KB

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