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.

308 lines
11KB

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