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.

315 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. #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 = nullptr;
  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. listeners.call (&StateChangedCallback::licenseStateChanged, getState());
  124. }
  125. #if ! JUCER_ENABLE_GPL_MODE
  126. if (thread == nullptr)
  127. thread = new LicenseThread (*this, false);
  128. #endif
  129. }
  130. void LicenseController::logout()
  131. {
  132. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  133. #if ! JUCER_ENABLE_GPL_MODE
  134. thread = nullptr;
  135. updateState ({});
  136. #if ! JUCE_LINUX
  137. WebBrowserComponent::clearCookies();
  138. #endif
  139. thread = new LicenseThread (*this, false);
  140. #endif
  141. }
  142. void LicenseController::chooseNewLicense()
  143. {
  144. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  145. #if ! JUCER_ENABLE_GPL_MODE
  146. thread = nullptr;
  147. thread = new LicenseThread (*this, true);
  148. #endif
  149. }
  150. void LicenseController::setApplicationUsageDataState (LicenseState::ApplicationUsageData newState)
  151. {
  152. if (state.applicationUsageDataState != newState)
  153. {
  154. state.applicationUsageDataState = newState;
  155. updateState (state);
  156. }
  157. }
  158. //==============================================================================
  159. #if ! JUCER_ENABLE_GPL_MODE
  160. void LicenseController::closeWebview (int result)
  161. {
  162. if (licenseWebview != nullptr)
  163. licenseWebview->exitModalState (result);
  164. }
  165. void LicenseController::modalStateFinished (int result)
  166. {
  167. licenseWebview = nullptr;
  168. if (result == -1 && (state.type == LicenseState::Type::notLoggedIn
  169. || state.type == LicenseState::Type::noLicenseChosenYet))
  170. JUCEApplication::getInstance()->systemRequestedQuit();
  171. }
  172. void LicenseController::ensureLicenseWebviewIsOpenWithPage (const String& param)
  173. {
  174. if (licenseWebview != nullptr)
  175. {
  176. licenseWebview->goToURL (param);
  177. licenseWebview->toFront (true);
  178. }
  179. else
  180. {
  181. #if ! JUCE_LINUX
  182. WebBrowserComponent::clearCookies();
  183. #endif
  184. licenseWebview = new LicenseWebview (new ModalCompletionCallback (*this), param);
  185. }
  186. }
  187. void LicenseController::queryWebview (const String& startURL, const String& valueToQuery,
  188. HashMap<String, String>& result)
  189. {
  190. ensureLicenseWebviewIsOpenWithPage (startURL);
  191. licenseWebview->setPageCallback ([this,valueToQuery,&result] (const String& cmd, const HashMap<String, String>& params)
  192. {
  193. if (valueToQuery.isEmpty() || cmd == valueToQuery)
  194. {
  195. result.clear();
  196. for (HashMap<String, String>::Iterator it = params.begin(); it != params.end(); ++it)
  197. result.set (it.getKey(), it.getValue());
  198. if (thread != nullptr && ! thread->threadShouldExit())
  199. thread->finished.signal();
  200. }
  201. });
  202. licenseWebview->setNewWindowCallback ([this, &result] (const String& url)
  203. {
  204. if (url.endsWith ("get-juce/indie") || url.endsWith ("get-juce/pro"))
  205. {
  206. result.clear();
  207. result.set ("page-redirect", url);
  208. if (thread != nullptr && ! thread->threadShouldExit())
  209. thread->finished.signal();
  210. }
  211. });
  212. }
  213. #endif
  214. void LicenseController::updateState (const LicenseState& newState)
  215. {
  216. auto& props = ProjucerApplication::getApp().settings->getGlobalProperties();
  217. state = newState;
  218. licenseStateToSettings (state, props);
  219. listeners.call (&StateChangedCallback::licenseStateChanged, getState());
  220. }
  221. LicenseState LicenseController::licenseStateFromSettings (PropertiesFile& props)
  222. {
  223. ScopedPointer<XmlElement> licenseXml = props.getXmlValue ("license");
  224. if (licenseXml != nullptr)
  225. {
  226. LicenseState result;
  227. result.type = getLicenseTypeFromValue (licenseXml->getChildElementAllSubText ("type", {}));
  228. result.applicationUsageDataState = getApplicationUsageDataTypeFromValue (licenseXml->getChildElementAllSubText ("applicationUsageData", {}));
  229. result.username = licenseXml->getChildElementAllSubText ("username", {});
  230. result.email = licenseXml->getChildElementAllSubText ("email", {});
  231. result.authToken = licenseXml->getChildElementAllSubText ("authToken", {});
  232. MemoryOutputStream imageData;
  233. Base64::convertFromBase64 (imageData, licenseXml->getChildElementAllSubText ("avatar", {}));
  234. result.avatar = ImageFileFormat::loadFrom (imageData.getData(), imageData.getDataSize());
  235. return result;
  236. }
  237. return {};
  238. }
  239. void LicenseController::licenseStateToSettings (const LicenseState& state, PropertiesFile& props)
  240. {
  241. props.removeValue ("license");
  242. if (state.type != LicenseState::Type::notLoggedIn && state.username.isNotEmpty())
  243. {
  244. XmlElement licenseXml ("license");
  245. if (auto* typeString = getLicenseStateValue (state.type))
  246. licenseXml.createNewChildElement ("type")->addTextElement (typeString);
  247. licenseXml.createNewChildElement ("applicationUsageData")->addTextElement (getApplicationUsageDataStateValue (state.applicationUsageDataState));
  248. licenseXml.createNewChildElement ("username")->addTextElement (state.username);
  249. licenseXml.createNewChildElement ("email") ->addTextElement (state.email);
  250. // TODO: encrypt authToken
  251. licenseXml.createNewChildElement ("authToken")->addTextElement (state.authToken);
  252. MemoryOutputStream imageData;
  253. if (state.avatar.isValid() && PNGImageFormat().writeImageToStream (state.avatar, imageData))
  254. licenseXml.createNewChildElement ("avatar")->addTextElement (Base64::toBase64 (imageData.getData(), imageData.getDataSize()));
  255. props.setValue ("license", &licenseXml);
  256. }
  257. props.saveIfNeeded();
  258. }