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.

280 lines
10KB

  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. ~RedirectWebBrowserComponent() override {}
  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. ~Header() override
  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. std::unique_ptr<Drawable> juceLogo { Drawable::createFromImageData (BinaryData::jucelogowithtext_svg,
  108. BinaryData::jucelogowithtext_svgSize) };
  109. IconButton avatarButton;
  110. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Header)
  111. };
  112. //==============================================================================
  113. public:
  114. LicenseWebviewContent (LicenseWebview& parentWindowToUse, ModalComponentManager::Callback* callbackToUse)
  115. : parentWindow (parentWindowToUse), modalCallback (callbackToUse), webview (*this)
  116. {
  117. addAndMakeVisible (header);
  118. addAndMakeVisible (webview);
  119. setOpaque (true);
  120. setSize (978, 718);
  121. #if JUCE_WINDOWS // windows needs the webcomponent be visible
  122. parentWindow.enterModalState (true, modalCallback.release(), true);
  123. #endif
  124. }
  125. void goToURL (const String& request)
  126. {
  127. lastURL = request;
  128. webview.goToURL (lastURL);
  129. }
  130. void paint (Graphics& g) override { g.fillAll (Colours::lightblue); }
  131. void resized() override
  132. {
  133. auto r = getLocalBounds();
  134. header.setBounds (r.removeFromTop (78));
  135. webview.setBounds (r);
  136. }
  137. bool pageAboutToLoad (const String& page)
  138. {
  139. URL url (page);
  140. if (page == "about:blank" || page.startsWith ("file://") || page.startsWith ("data:text/html"))
  141. {
  142. if (page != lastErrorPageURI)
  143. lastURL = page;
  144. return true;
  145. }
  146. else if (url.getScheme() == "projucer")
  147. {
  148. HashMap<String, String> params;
  149. auto n = url.getParameterNames().size();
  150. for (int i = 0; i < n; ++i)
  151. params.set (url.getParameterNames()[i], url.getParameterValues()[i]);
  152. String cmd (url.getDomain());
  153. if (n == 0 && cmd.containsChar (L'='))
  154. {
  155. // old-style callback
  156. StringArray domainTokens (StringArray::fromTokens (cmd, "=", ""));
  157. cmd = domainTokens[0];
  158. params.set (cmd, domainTokens[1]);
  159. }
  160. if (pageCallback)
  161. pageCallback (cmd, params);
  162. return false;
  163. }
  164. if (isValidURL (url))
  165. lastURL = page;
  166. return true;
  167. }
  168. void pageFinishedLoading (const String& page)
  169. {
  170. URL url (page);
  171. if ((isValidURL (url)
  172. || page.startsWith ("file://") || page.startsWith ("data:text/html"))
  173. && ! parentWindow.isCurrentlyModal())
  174. parentWindow.enterModalState (true, modalCallback.release(), true);
  175. }
  176. void newWindowAttemptingToLoad (const String& page)
  177. {
  178. URL url (page);
  179. bool isGitHub = url.getDomain().endsWith ("github.com");
  180. if (isValidURL (url) || isGitHub)
  181. {
  182. url.launchInDefaultBrowser();
  183. if (newWindowCallback && ! isGitHub)
  184. newWindowCallback (page);
  185. }
  186. }
  187. bool pageLoadHadNetworkError (const String&)
  188. {
  189. String errorPageSource = String (BinaryData::offlinepage_html, BinaryData::offlinepage_htmlSize)
  190. .replace ("__URL_PLACEHOLDER__", lastURL);
  191. #if JUCE_WINDOWS
  192. auto tmpFile = File::createTempFile (".html");
  193. tmpFile.replaceWithText (errorPageSource, true);
  194. lastErrorPageURI = "file://" + tmpFile.getFullPathName();
  195. #else
  196. lastErrorPageURI = "data:text/html;base64," + Base64::toBase64 (errorPageSource);
  197. #endif
  198. goToURL (lastErrorPageURI);
  199. return false;
  200. }
  201. static bool isValidURL (const URL& url) { return (url.getDomain().endsWith ("roli.com") || url.getDomain().endsWith ("juce.com")); }
  202. //==============================================================================
  203. LicenseWebview& parentWindow;
  204. std::unique_ptr<ModalComponentManager::Callback> modalCallback;
  205. Header header;
  206. RedirectWebBrowserComponent webview;
  207. std::function<void (const String&, const HashMap<String, String>&)> pageCallback;
  208. std::function<void (const String&)> newWindowCallback;
  209. String lastURL, lastErrorPageURI;
  210. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LicenseWebviewContent)
  211. };
  212. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LicenseWebview)
  213. };