PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/webenginewidgets/notifications/main.cpp

https://gitlab.com/f3822/qtwebengine
C++ | 100 lines | 39 code | 11 blank | 50 comment | 4 complexity | 3be4298963e462f0061a0d91f28c2189 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2019 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the examples of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** BSD License Usage
  18. ** Alternatively, you may use this file under the terms of the BSD license
  19. ** as follows:
  20. **
  21. ** "Redistribution and use in source and binary forms, with or without
  22. ** modification, are permitted provided that the following conditions are
  23. ** met:
  24. ** * Redistributions of source code must retain the above copyright
  25. ** notice, this list of conditions and the following disclaimer.
  26. ** * Redistributions in binary form must reproduce the above copyright
  27. ** notice, this list of conditions and the following disclaimer in
  28. ** the documentation and/or other materials provided with the
  29. ** distribution.
  30. ** * Neither the name of The Qt Company Ltd nor the names of its
  31. ** contributors may be used to endorse or promote products derived
  32. ** from this software without specific prior written permission.
  33. **
  34. **
  35. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  46. **
  47. ** $QT_END_LICENSE$
  48. **
  49. ****************************************************************************/
  50. #include "notificationpopup.h"
  51. #include <QApplication>
  52. #include <QDesktopServices>
  53. #include <QWebEnginePage>
  54. #include <QWebEngineProfile>
  55. #include <QWebEngineView>
  56. class WebEnginePage : public QWebEnginePage
  57. {
  58. public:
  59. WebEnginePage(QWidget *parent) : QWebEnginePage(parent) { }
  60. bool acceptNavigationRequest(const QUrl &url, NavigationType, bool) override
  61. {
  62. if (url.scheme() != "https")
  63. return true;
  64. QDesktopServices::openUrl(url);
  65. return false;
  66. }
  67. };
  68. int main(int argc, char *argv[])
  69. {
  70. QCoreApplication::setOrganizationName("QtExamples");
  71. QApplication app(argc, argv);
  72. QWebEngineView view;
  73. // set custom page to open all page's links for https scheme in system browser
  74. view.setPage(new WebEnginePage(&view));
  75. QObject::connect(view.page(), &QWebEnginePage::featurePermissionRequested,
  76. [&] (const QUrl &origin, QWebEnginePage::Feature feature) {
  77. if (feature != QWebEnginePage::Notifications)
  78. return;
  79. view.page()->setFeaturePermission(origin, feature, QWebEnginePage::PermissionGrantedByUser);
  80. });
  81. auto profile = view.page()->profile();
  82. auto popup = new NotificationPopup(&view);
  83. profile->setNotificationPresenter([&] (std::unique_ptr<QWebEngineNotification> notification)
  84. { popup->present(notification); });
  85. view.resize(640, 480);
  86. view.show();
  87. view.setUrl(QStringLiteral("qrc:/index.html"));
  88. return app.exec();
  89. }