Send API-Key when using QWebEngine

so when showing Syncthing via QWebEngine, the password
must not be provided.
This commit is contained in:
Martchus 2017-12-30 02:26:55 +01:00
parent 53dafd1627
commit eeca6715de
7 changed files with 79 additions and 0 deletions

View File

@ -27,6 +27,8 @@ set(WIDGETS_SRC_FILES
settings/settingsdialog.cpp
webview/webpage.cpp
webview/webviewdialog.cpp
webview/webviewinterceptor.h
webview/webviewinterceptor.cpp
misc/textviewdialog.cpp
misc/errorviewdialog.cpp
misc/statusinfo.cpp

View File

@ -30,8 +30,13 @@ using namespace Data;
namespace QtGui {
#ifdef SYNCTHINGWIDGETS_USE_WEBENGINE
WebPage::WebPage(QWebEngineProfile *profile, WebViewDialog *dlg, SYNCTHINGWIDGETS_WEB_VIEW *view)
: SYNCTHINGWIDGETS_WEB_PAGE(profile, view)
#else
WebPage::WebPage(WebViewDialog *dlg, SYNCTHINGWIDGETS_WEB_VIEW *view)
: SYNCTHINGWIDGETS_WEB_PAGE(view)
#endif
, m_dlg(dlg)
, m_view(view)
{

View File

@ -19,7 +19,11 @@ class WebViewDialog;
class SYNCTHINGWIDGETS_EXPORT WebPage : public SYNCTHINGWIDGETS_WEB_PAGE {
Q_OBJECT
public:
#ifdef SYNCTHINGWIDGETS_USE_WEBENGINE
WebPage(QWebEngineProfile *profile = nullptr, WebViewDialog *dlg = nullptr, SYNCTHINGWIDGETS_WEB_VIEW *view = nullptr);
#else
WebPage(WebViewDialog *dlg = nullptr, SYNCTHINGWIDGETS_WEB_VIEW *view = nullptr);
#endif
static bool isSamePage(const QUrl &url1, const QUrl &url2);

View File

@ -1,6 +1,7 @@
#ifndef SYNCTHINGWIDGETS_NO_WEBVIEW
#include "./webviewdialog.h"
#include "./webpage.h"
#include "./webviewinterceptor.h"
#include "../settings/settings.h"
@ -9,6 +10,9 @@
#include <QCloseEvent>
#include <QIcon>
#include <QKeyEvent>
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
#include <QWebEngineProfile>
#endif
using namespace Dialogs;
@ -22,7 +26,13 @@ WebViewDialog::WebViewDialog(QWidget *parent)
setWindowIcon(QIcon(QStringLiteral(":/icons/hicolor/scalable/app/syncthingtray.svg")));
setCentralWidget(m_view);
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
m_profile = new QWebEngineProfile(objectName(), this);
m_profile->setRequestInterceptor(new WebViewInterceptor(m_settings, m_profile));
m_view->setPage(new WebPage(m_profile, this, m_view));
#else
m_view->setPage(new WebPage(this, m_view));
#endif
connect(m_view, &SYNCTHINGWIDGETS_WEB_VIEW::titleChanged, this, &WebViewDialog::setWindowTitle);
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
@ -122,6 +132,7 @@ bool WebViewDialog::eventFilter(QObject *watched, QEvent *event)
return QMainWindow::eventFilter(watched, event);
}
#endif
} // namespace QtGui
#endif // SYNCTHINGWIDGETS_NO_WEBVIEW

View File

@ -9,6 +9,7 @@
#include <QMainWindow>
QT_FORWARD_DECLARE_CLASS(WEB_VIEW_PROVIDER)
QT_FORWARD_DECLARE_CLASS(QWebEngineProfile)
namespace Settings {
struct ConnectionSettings;
@ -40,12 +41,16 @@ protected:
private:
SYNCTHINGWIDGETS_WEB_VIEW *m_view;
Data::SyncthingConnectionSettings m_settings;
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
QWebEngineProfile *m_profile;
#endif
};
inline const Data::SyncthingConnectionSettings &WebViewDialog::settings() const
{
return m_settings;
}
} // namespace QtGui
#endif // SYNCTHINGWIDGETS_NO_WEBVIEW

View File

@ -0,0 +1,18 @@
#ifdef SYNCTHINGWIDGETS_USE_WEBENGINE
#include "./webviewinterceptor.h"
#include "../../connector/syncthingconnectionsettings.h"
#include <QWebEngineProfile>
#include <QWebEngineUrlRequestInterceptor>
namespace QtGui {
void WebViewInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
info.setHttpHeader(QByteArray("X-API-Key"), m_settings.apiKey);
}
} // namespace QtGui
#endif // SYNCTHINGWIDGETS_USE_WEBENGINE

View File

@ -0,0 +1,34 @@
#ifndef WEBVIEW_INTERCEPTOR_H
#define WEBVIEW_INTERCEPTOR_H
#ifdef SYNCTHINGWIDGETS_USE_WEBENGINE
#include <QWebEngineUrlRequestInterceptor>
namespace Data {
struct SyncthingConnectionSettings;
}
namespace QtGui {
class WebViewInterceptor : public QWebEngineUrlRequestInterceptor {
Q_OBJECT
public:
explicit WebViewInterceptor(const Data::SyncthingConnectionSettings &settings, QObject *parent = nullptr);
virtual void interceptRequest(QWebEngineUrlRequestInfo &info);
private:
const Data::SyncthingConnectionSettings &m_settings;
};
inline WebViewInterceptor::WebViewInterceptor(const Data::SyncthingConnectionSettings &settings, QObject *parent)
: QWebEngineUrlRequestInterceptor(parent)
, m_settings(settings)
{
}
} // namespace QtGui
#endif // SYNCTHINGWIDGETS_USE_WEBENGINE
#endif // WEBVIEW_INTERCEPTOR_H