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.

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