From 2dcb1748dae8dc6f2f0c68a41c09f5af128effb9 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Thu, 14 Nov 2024 21:35:51 +0100 Subject: [PATCH] Add selection dialog for media capture request Available from Qt 6.7 onwards. Signed-off-by: Juraj Oravec --- src/lib/CMakeLists.txt | 2 ++ src/lib/other/comboboxdialog.cpp | 61 ++++++++++++++++++++++++++++++++ src/lib/other/comboboxdialog.h | 47 ++++++++++++++++++++++++ src/lib/webengine/webpage.cpp | 48 +++++++++++++++++++++++++ src/lib/webengine/webpage.h | 8 +++++ 5 files changed, 166 insertions(+) create mode 100644 src/lib/other/comboboxdialog.cpp create mode 100644 src/lib/other/comboboxdialog.h diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index b484e940d..bbdcbd879 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -145,6 +145,7 @@ set(SRCS ${SRCS} other/browsinglibrary.cpp other/clearprivatedata.cpp other/checkboxdialog.cpp + other/comboboxdialog.cpp other/iconchooser.cpp other/licenseviewer.cpp other/qzsettings.cpp @@ -379,6 +380,7 @@ set(SRCS ${SRCS} other/browsinglibrary.h other/clearprivatedata.h other/checkboxdialog.h + other/comboboxdialog.h other/iconchooser.h other/licenseviewer.h other/qzsettings.h diff --git a/src/lib/other/comboboxdialog.cpp b/src/lib/other/comboboxdialog.cpp new file mode 100644 index 000000000..c85d10a0e --- /dev/null +++ b/src/lib/other/comboboxdialog.cpp @@ -0,0 +1,61 @@ +/* ============================================================ + * Falkon - Qt web browser + * SPDX-FileCopyrightText: 2024 Juraj Oravec + * SPDX-License-Identifier: GPL-3.0-or-later + * ============================================================ */ + +#include "comboboxdialog.h" + +#include +#include +#include +#include +#include + +ComboBoxDialog::ComboBoxDialog(const QDialogButtonBox::StandardButtons buttons, QWidget* parent) + : QDialog(parent) +{ + m_gridLayout = new QGridLayout(); + m_label = new QLabel(); + m_description = new QLabel(); + m_comboBox = new QComboBox(); + + m_hLayout = new QHBoxLayout(); + m_buttonBox = new QDialogButtonBox(buttons); + m_hSpacerLeft = new QSpacerItem(40, 20, QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Minimum); + m_hSpacerRight = new QSpacerItem(40, 20, QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Minimum); + + m_hLayout->addItem(m_hSpacerLeft); + m_hLayout->addWidget(m_buttonBox); + m_hLayout->addItem(m_hSpacerRight); + + m_gridLayout->addWidget(m_description, 0, 0, 1, 2); + m_gridLayout->addWidget(m_label, 1, 0); + m_gridLayout->addWidget(m_comboBox, 1, 1); + m_gridLayout->addLayout(m_hLayout, 2, 0, 1, 2); + + setLayout(m_gridLayout); + + connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); +} + +void ComboBoxDialog::setLabel(const QString& label) +{ + m_label->setText(label); +} + +void ComboBoxDialog::setModel(QAbstractItemModel* model) +{ + m_comboBox->setModel(model); +} + +int ComboBoxDialog::currentIndex() +{ + return m_comboBox->currentIndex(); +} + +void ComboBoxDialog::setDescription(const QString& description) +{ + m_description->setText(description); +} diff --git a/src/lib/other/comboboxdialog.h b/src/lib/other/comboboxdialog.h new file mode 100644 index 000000000..bdf84ad7e --- /dev/null +++ b/src/lib/other/comboboxdialog.h @@ -0,0 +1,47 @@ +/* ============================================================ + * Falkon - Qt web browser + * SPDX-FileCopyrightText: 2024 Juraj Oravec + * SPDX-License-Identifier: GPL-3.0-or-later + * ============================================================ */ + +#ifndef COMBOBOXDIALOG_H +#define COMBOBOXDIALOG_H + +#include +#include +#include + +#include "qzcommon.h" + +class QComboBox; +class QGridLayout; +class QHBoxLayout; +class QLabel; +class QSpacerItem; + +class FALKON_EXPORT ComboBoxDialog : public QDialog +{ + Q_OBJECT +public: + explicit ComboBoxDialog(const QDialogButtonBox::StandardButtons buttons, QWidget* parent = nullptr); + + void setModel(QAbstractItemModel *model); + void setLabel(const QString &label); + void setDescription(const QString &description); + + int currentIndex(); + +private: + QComboBox *m_comboBox; + QLabel *m_label; + QLabel *m_description; + + QGridLayout *m_gridLayout; + QHBoxLayout *m_hLayout; + QDialogButtonBox *m_buttonBox; + QSpacerItem *m_hSpacerLeft; + QSpacerItem *m_hSpacerRight; +}; + +#endif // COMBOBOXDIALOG_H + diff --git a/src/lib/webengine/webpage.cpp b/src/lib/webengine/webpage.cpp index a9ddaf706..e94232dc6 100644 --- a/src/lib/webengine/webpage.cpp +++ b/src/lib/webengine/webpage.cpp @@ -44,6 +44,7 @@ #include "passwordmanager.h" #include "scripts.h" #include "ocssupport.h" +#include "comboboxdialog.h" #include @@ -60,9 +61,15 @@ #include #include #include +#include #include +#if QTWEBENGINECORE_VERSION >= QT_VERSION_CHECK(6, 7, 0) +#include +#include +#endif + QString WebPage::s_lastUploadLocation = QDir::homePath(); QUrl WebPage::s_lastUnsupportedUrl; QElapsedTimer WebPage::s_lastUnsupportedUrlTime; @@ -118,6 +125,9 @@ WebPage::WebPage(QObject* parent) // TODO: It should prompt user selection.select(selection.certificates().at(0)); }); +#if QTWEBENGINECORE_VERSION >= QT_VERSION_CHECK(6, 7, 0) + connect(this, &QWebEnginePage::desktopMediaRequested, this, &WebPage::handleDesktopMediaRequested); +#endif } WebPage::~WebPage() @@ -765,3 +775,41 @@ QWebEnginePage* WebPage::createWindow(QWebEnginePage::WebWindowType type) return nullptr; } + +#if QTWEBENGINECORE_VERSION >= QT_VERSION_CHECK(6, 7, 0) +void WebPage::handleDesktopMediaRequested(const QWebEngineDesktopMediaRequest& request) +{ + if (!mApp->webSettings()->testAttribute(QWebEngineSettings::ScreenCaptureEnabled)) { + request.cancel(); + return; + } + + QConcatenateTablesProxyModel proxyModel; + proxyModel.addSourceModel(request.screensModel()); + proxyModel.addSourceModel(request.windowsModel()); + + ComboBoxDialog dialog(QDialogButtonBox::StandardButton::Ok | QDialogButtonBox::StandardButton::Cancel); + dialog.setLabel(tr("Source:")); + dialog.setDescription(tr("Select cource for %1 to capture.").arg(url().host())); + dialog.setModel(&proxyModel); + + dialog.exec(); + + if (dialog.result() != QDialog::Rejected) { + auto index = proxyModel.mapToSource(proxyModel.index(dialog.currentIndex(), 0)); + + if (index.model() == request.screensModel()) { + request.selectScreen(index); + } + else if (index.model() == request.windowsModel()) { + request.selectWindow(index); + } + else { + request.cancel(); + } + } + else { + request.cancel(); + } +} +#endif diff --git a/src/lib/webengine/webpage.h b/src/lib/webengine/webpage.h index 233029dba..eb495778f 100644 --- a/src/lib/webengine/webpage.h +++ b/src/lib/webengine/webpage.h @@ -35,6 +35,10 @@ class WebView; class WebHitTestResult; class DelayedFileWatcher; +#if QTWEBENGINECORE_VERSION >= QT_VERSION_CHECK(6, 7, 0) +class QWebEngineDesktopMediaRequest; +#endif + class FALKON_EXPORT WebPage : public QWebEnginePage { Q_OBJECT @@ -96,6 +100,10 @@ private Q_SLOTS: void renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode); void onCertificateError(QWebEngineCertificateError error); +#if QTWEBENGINECORE_VERSION >= QT_VERSION_CHECK(6, 7, 0) + void handleDesktopMediaRequested(const QWebEngineDesktopMediaRequest &request); +#endif + private: bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override;