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.

285 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. #pragma once
  20. //==============================================================================
  21. class LicenseWebview : public DialogWindow
  22. {
  23. public:
  24. LicenseWebview (ModalComponentManager::Callback* callbackToUse, const String& request)
  25. : DialogWindow ("Log-in to Projucer", Colour (0xfff1f1f1), true, true)
  26. {
  27. LicenseWebviewContent* content;
  28. setUsingNativeTitleBar (true);
  29. setContentOwned (content = new LicenseWebviewContent (*this, callbackToUse), true);
  30. centreWithSize (getWidth(), getHeight());
  31. content->goToURL (request);
  32. }
  33. void goToURL (const String& request) { reinterpret_cast<LicenseWebviewContent*> (getContentComponent())->goToURL (request); }
  34. void setPageCallback (const std::function<void (const String&, const HashMap<String, String>&)>& cb)
  35. {
  36. reinterpret_cast<LicenseWebviewContent*> (getContentComponent())->pageCallback = cb;
  37. }
  38. void setNewWindowCallback (const std::function<void (const String&)>& cb)
  39. {
  40. reinterpret_cast<LicenseWebviewContent*> (getContentComponent())->newWindowCallback = cb;
  41. }
  42. void closeButtonPressed() override { exitModalState (-1); }
  43. private:
  44. class LicenseWebviewContent : public Component
  45. {
  46. //==============================================================================
  47. struct RedirectWebBrowserComponent : public WebBrowserComponent
  48. {
  49. RedirectWebBrowserComponent (LicenseWebviewContent& controller) : WebBrowserComponent (false), owner (controller) {}
  50. virtual ~RedirectWebBrowserComponent() {}
  51. bool pageAboutToLoad (const String& url) override { return owner.pageAboutToLoad (url); }
  52. void pageFinishedLoading (const String& url) override { owner.pageFinishedLoading (url); }
  53. void newWindowAttemptingToLoad (const String& url) override { owner.newWindowAttemptingToLoad (url); }
  54. bool pageLoadHadNetworkError (const String& err) override { return owner.pageLoadHadNetworkError (err); }
  55. LicenseWebviewContent& owner;
  56. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RedirectWebBrowserComponent)
  57. };
  58. //==============================================================================
  59. struct Header : public Component,
  60. private LicenseController::StateChangedCallback
  61. {
  62. Header() : avatarButton ("User Settings", &getIcons().user)
  63. {
  64. setOpaque (true);
  65. addChildComponent (avatarButton);
  66. avatarButton.onClick = [this] { showAvatarWindow(); };
  67. if (auto* licenseController = ProjucerApplication::getApp().licenseController.get())
  68. {
  69. licenseController->addLicenseStatusChangedCallback (this);
  70. licenseStateChanged (licenseController->getState());
  71. }
  72. }
  73. virtual ~Header()
  74. {
  75. if (auto* licenseController = ProjucerApplication::getApp().licenseController.get())
  76. licenseController->removeLicenseStatusChangedCallback (this);
  77. }
  78. void resized() override
  79. {
  80. auto r = getLocalBounds().reduced (30, 20);
  81. avatarButton.setBounds (r.removeFromRight (r.getHeight()));
  82. }
  83. void paint (Graphics& g) override
  84. {
  85. auto r = getLocalBounds().reduced (30, 20);
  86. g.fillAll (Colour (backgroundColour));
  87. if (juceLogo != nullptr)
  88. juceLogo->drawWithin (g, r.toFloat(), RectanglePlacement::xLeft + RectanglePlacement::yMid, 1.0);
  89. }
  90. void licenseStateChanged (const LicenseState& state) override
  91. {
  92. avatarButton.iconImage = state.avatar;
  93. avatarButton.setVisible (state.type != LicenseState::Type::notLoggedIn && state.type != LicenseState::Type::GPL);
  94. avatarButton.repaint();
  95. }
  96. void showAvatarWindow()
  97. {
  98. if (auto* licenseController = ProjucerApplication::getApp().licenseController.get())
  99. {
  100. auto type = licenseController->getState().type;
  101. auto* content = new UserSettingsPopup (true);
  102. content->setSize (200, (type == LicenseState::Type::noLicenseChosenYet ? 100 : 150));
  103. CallOutBox::launchAsynchronously (content, avatarButton.getScreenBounds(), nullptr);
  104. }
  105. }
  106. const uint32 backgroundColour = 0xff414141;
  107. ScopedPointer<Drawable> juceLogo
  108. = Drawable::createFromImageData (BinaryData::jucelogowithtext_svg,
  109. BinaryData::jucelogowithtext_svgSize);
  110. IconButton avatarButton;
  111. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Header)
  112. };
  113. //==============================================================================
  114. public:
  115. LicenseWebviewContent (LicenseWebview& parentWindowToUse, ModalComponentManager::Callback* callbackToUse)
  116. : parentWindow (parentWindowToUse), modalCallback (callbackToUse), webview (*this)
  117. {
  118. addAndMakeVisible (header);
  119. addAndMakeVisible (webview);
  120. setOpaque (true);
  121. setSize (978, 718);
  122. #if JUCE_WINDOWS // windows needs the webcomponent be visible
  123. parentWindow.enterModalState (true, modalCallback.release(), true);
  124. #endif
  125. }
  126. void goToURL (const String& request)
  127. {
  128. lastURL = request;
  129. webview.goToURL (lastURL);
  130. }
  131. void paint (Graphics& g) override { g.fillAll (Colours::lightblue); }
  132. void resized() override
  133. {
  134. auto r = getLocalBounds();
  135. header.setBounds (r.removeFromTop (78));
  136. webview.setBounds (r);
  137. }
  138. bool pageAboutToLoad (const String& page)
  139. {
  140. URL url (page);
  141. if (page == "about:blank" || page.startsWith ("file://") || page.startsWith ("data:text/html"))
  142. {
  143. if (page != lastErrorPageURI)
  144. lastURL = page;
  145. return true;
  146. }
  147. else if (url.getScheme() == "projucer")
  148. {
  149. HashMap<String, String> params;
  150. auto n = url.getParameterNames().size();
  151. for (int i = 0; i < n; ++i)
  152. params.set (url.getParameterNames()[i], url.getParameterValues()[i]);
  153. String cmd (url.getDomain());
  154. if (n == 0 && cmd.containsChar (L'='))
  155. {
  156. // old-style callback
  157. StringArray domainTokens (StringArray::fromTokens (cmd, "=", ""));
  158. cmd = domainTokens[0];
  159. params.set (cmd, domainTokens[1]);
  160. }
  161. if (pageCallback)
  162. pageCallback (cmd, params);
  163. return false;
  164. }
  165. bool isValid = (url.getDomain().endsWith ("roli.com") || url.getDomain().endsWith ("juce.com"));
  166. if (isValid)
  167. lastURL = page;
  168. return true;
  169. }
  170. void pageFinishedLoading (const String& page)
  171. {
  172. URL url (page);
  173. if ((isValidURL (url)
  174. || page.startsWith ("file://") || page.startsWith ("data:text/html"))
  175. && ! parentWindow.isCurrentlyModal())
  176. parentWindow.enterModalState (true, modalCallback.release(), true);
  177. }
  178. void newWindowAttemptingToLoad (const String& page)
  179. {
  180. URL url (page);
  181. bool isGitHub = url.getDomain().endsWith ("github.com");
  182. if (url.getDomain().endsWith ("roli.com")
  183. || url.getDomain().endsWith ("juce.com")
  184. || isGitHub)
  185. {
  186. url.launchInDefaultBrowser();
  187. if (newWindowCallback && ! isGitHub)
  188. newWindowCallback (page);
  189. }
  190. }
  191. bool pageLoadHadNetworkError (const String&)
  192. {
  193. String errorPageSource = String (BinaryData::offlinepage_html, BinaryData::offlinepage_htmlSize)
  194. .replace ("__URL_PLACEHOLDER__", lastURL);
  195. #if JUCE_WINDOWS
  196. auto tmpFile = File::createTempFile (".html");
  197. tmpFile.replaceWithText (errorPageSource, true);
  198. lastErrorPageURI = "file://" + tmpFile.getFullPathName();
  199. #else
  200. lastErrorPageURI = "data:text/html;base64," + Base64::toBase64 (errorPageSource);
  201. #endif
  202. goToURL (lastErrorPageURI);
  203. return false;
  204. }
  205. static bool isValidURL (const URL& url) { return (url.getDomain().endsWith ("roli.com") || url.getDomain().endsWith ("juce.com")); }
  206. //==============================================================================
  207. LicenseWebview& parentWindow;
  208. ScopedPointer<ModalComponentManager::Callback> modalCallback;
  209. Header header;
  210. RedirectWebBrowserComponent webview;
  211. std::function<void (const String&, const HashMap<String, String>&)> pageCallback;
  212. std::function<void (const String&)> newWindowCallback;
  213. String lastURL, lastErrorPageURI;
  214. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LicenseWebviewContent)
  215. };
  216. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LicenseWebview)
  217. };