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.

218 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. #pragma once
  14. #include "jucer_LicenseState.h"
  15. #include "jucer_LicenseQueryThread.h"
  16. //==============================================================================
  17. class LicenseController : private Timer
  18. {
  19. public:
  20. LicenseController()
  21. {
  22. checkLicense();
  23. }
  24. //==============================================================================
  25. static LicenseState getGPLState()
  26. {
  27. return { LicenseState::Type::gpl, projucerMajorVersion, {}, {} };
  28. }
  29. LicenseState getCurrentState() const noexcept
  30. {
  31. return state;
  32. }
  33. void setState (const LicenseState& newState)
  34. {
  35. if (state != newState)
  36. {
  37. state = newState;
  38. licenseStateToSettings (state, getGlobalProperties());
  39. stateListeners.call ([] (LicenseStateListener& l) { l.licenseStateChanged(); });
  40. }
  41. }
  42. void resetState()
  43. {
  44. setState ({});
  45. }
  46. void signIn (const String& email, const String& password,
  47. std::function<void (const String&)> completionCallback)
  48. {
  49. licenseQueryThread.doSignIn (email, password,
  50. [this, completionCallback] (LicenseQueryThread::ErrorMessageAndType error,
  51. LicenseState newState)
  52. {
  53. completionCallback (error.first);
  54. setState (newState);
  55. });
  56. }
  57. void cancelSignIn()
  58. {
  59. licenseQueryThread.cancelRunningJobs();
  60. }
  61. //==============================================================================
  62. struct LicenseStateListener
  63. {
  64. virtual ~LicenseStateListener() = default;
  65. virtual void licenseStateChanged() = 0;
  66. };
  67. void addListener (LicenseStateListener* listenerToAdd)
  68. {
  69. stateListeners.add (listenerToAdd);
  70. }
  71. void removeListener (LicenseStateListener* listenerToRemove)
  72. {
  73. stateListeners.remove (listenerToRemove);
  74. }
  75. private:
  76. //==============================================================================
  77. static const char* getLicenseStateValue (LicenseState::Type type)
  78. {
  79. switch (type)
  80. {
  81. case LicenseState::Type::gpl: return "GPL";
  82. case LicenseState::Type::personal: return "personal";
  83. case LicenseState::Type::educational: return "edu";
  84. case LicenseState::Type::indie: return "indie";
  85. case LicenseState::Type::pro: return "pro";
  86. case LicenseState::Type::none:
  87. default: break;
  88. }
  89. return nullptr;
  90. }
  91. static LicenseState::Type getLicenseTypeFromValue (const String& d)
  92. {
  93. if (d == getLicenseStateValue (LicenseState::Type::gpl)) return LicenseState::Type::gpl;
  94. if (d == getLicenseStateValue (LicenseState::Type::personal)) return LicenseState::Type::personal;
  95. if (d == getLicenseStateValue (LicenseState::Type::educational)) return LicenseState::Type::educational;
  96. if (d == getLicenseStateValue (LicenseState::Type::indie)) return LicenseState::Type::indie;
  97. if (d == getLicenseStateValue (LicenseState::Type::pro)) return LicenseState::Type::pro;
  98. return LicenseState::Type::none;
  99. }
  100. static LicenseState licenseStateFromSettings (PropertiesFile& props)
  101. {
  102. if (auto licenseXml = props.getXmlValue ("license"))
  103. {
  104. // this is here for backwards compatibility with old-style settings files using XML text elements
  105. if (licenseXml->getChildElementAllSubText ("type", {}).isNotEmpty())
  106. {
  107. auto stateFromOldSettings = [&licenseXml]() -> LicenseState
  108. {
  109. return { getLicenseTypeFromValue (licenseXml->getChildElementAllSubText ("type", {})),
  110. licenseXml->getChildElementAllSubText ("version", "-1").getIntValue(),
  111. licenseXml->getChildElementAllSubText ("username", {}),
  112. licenseXml->getChildElementAllSubText ("authToken", {}) };
  113. }();
  114. licenseStateToSettings (stateFromOldSettings, props);
  115. return stateFromOldSettings;
  116. }
  117. return { getLicenseTypeFromValue (licenseXml->getStringAttribute ("type", {})),
  118. licenseXml->getIntAttribute ("version", -1),
  119. licenseXml->getStringAttribute ("username", {}),
  120. licenseXml->getStringAttribute ("authToken", {}) };
  121. }
  122. return {};
  123. }
  124. static void licenseStateToSettings (const LicenseState& state, PropertiesFile& props)
  125. {
  126. props.removeValue ("license");
  127. if (state.isSignedIn())
  128. {
  129. XmlElement licenseXml ("license");
  130. if (auto* typeString = getLicenseStateValue (state.type))
  131. licenseXml.setAttribute ("type", typeString);
  132. licenseXml.setAttribute ("version", state.version);
  133. licenseXml.setAttribute ("username", state.username);
  134. licenseXml.setAttribute ("authToken", state.authToken);
  135. props.setValue ("license", &licenseXml);
  136. }
  137. props.saveIfNeeded();
  138. }
  139. //==============================================================================
  140. void checkLicense()
  141. {
  142. if (state.authToken.isNotEmpty() && ! state.isGPL())
  143. {
  144. auto completionCallback = [this] (LicenseQueryThread::ErrorMessageAndType error,
  145. LicenseState updatedState)
  146. {
  147. if (error == LicenseQueryThread::ErrorMessageAndType())
  148. {
  149. setState (updatedState);
  150. }
  151. else if ((error.second == LicenseQueryThread::ErrorType::busy
  152. || error.second == LicenseQueryThread::ErrorType::cancelled
  153. || error.second == LicenseQueryThread::ErrorType::connectionError)
  154. && ! hasRetriedLicenseCheck)
  155. {
  156. hasRetriedLicenseCheck = true;
  157. startTimer (10000);
  158. }
  159. };
  160. licenseQueryThread.checkLicenseValidity (state, std::move (completionCallback));
  161. }
  162. }
  163. void timerCallback() override
  164. {
  165. stopTimer();
  166. checkLicense();
  167. }
  168. //==============================================================================
  169. #if JUCER_ENABLE_GPL_MODE
  170. LicenseState state = getGPLState();
  171. #else
  172. LicenseState state = licenseStateFromSettings (getGlobalProperties());
  173. #endif
  174. ListenerList<LicenseStateListener> stateListeners;
  175. LicenseQueryThread licenseQueryThread;
  176. bool hasRetriedLicenseCheck = false;
  177. //==============================================================================
  178. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LicenseController)
  179. };