syncthingtray/syncthingwidgets/webview/webviewdialog.cpp

296 lines
9.5 KiB
C++
Raw Normal View History

2016-08-25 00:45:32 +02:00
#include "./webviewdialog.h"
#include "../settings/settings.h"
// use meta-data of syncthingtray application here
// note: Using the meta-data from the library would make more sense but this breaks
// unity builds.
#include "resources/../../tray/resources/config.h"
#include <syncthingconnector/syncthingprocess.h>
#include <QCoreApplication>
#include <QDesktopServices>
#include <QMessageBox>
#include <QUrl>
#ifdef Q_OS_WINDOWS
#include <QFile>
#include <QSettings> // for reading registry
#endif
#ifndef SYNCTHINGWIDGETS_NO_WEBVIEW
2016-08-25 00:45:32 +02:00
#include "./webpage.h"
#include "./webviewinterceptor.h"
2016-08-25 00:45:32 +02:00
#include <qtutilities/misc/compat.h>
2016-08-25 00:45:32 +02:00
#include <qtutilities/misc/dialogutils.h>
#include <QCloseEvent>
2017-05-01 03:34:43 +02:00
#include <QIcon>
2016-11-01 17:16:27 +01:00
#include <QKeyEvent>
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
#include <QWebEngineProfile>
#include <QtWebEngineWidgetsVersion>
#endif
2016-08-25 00:45:32 +02:00
#include <initializer_list>
2019-06-10 22:48:26 +02:00
using namespace QtUtilities;
2016-08-25 00:45:32 +02:00
namespace QtGui {
2017-05-01 03:34:43 +02:00
WebViewDialog::WebViewDialog(QWidget *parent)
: QMainWindow(parent)
, m_view(new SYNCTHINGWIDGETS_WEB_VIEW(this))
2016-08-25 00:45:32 +02:00
{
setWindowTitle(tr("Syncthing"));
setWindowIcon(QIcon(QStringLiteral(":/icons/hicolor/scalable/app/syncthingtray.svg")));
setCentralWidget(m_view);
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
m_profile = new QWebEngineProfile(objectName(), this);
#if (QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(5, 13, 0))
2019-07-07 12:14:23 +02:00
m_profile->setUrlRequestInterceptor(new WebViewInterceptor(m_connectionSettings, m_profile));
#else
m_profile->setRequestInterceptor(new WebViewInterceptor(m_connectionSettings, m_profile));
#endif
m_view->setPage(new WebPage(m_profile, this, m_view));
#else
2016-09-03 20:14:52 +02:00
m_view->setPage(new WebPage(this, m_view));
#endif
connect(m_view, &SYNCTHINGWIDGETS_WEB_VIEW::titleChanged, this, &WebViewDialog::setWindowTitle);
2016-08-25 00:45:32 +02:00
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
m_view->installEventFilter(this);
2017-05-01 03:34:43 +02:00
if (m_view->focusProxy()) {
m_view->focusProxy()->installEventFilter(this);
}
#endif
2017-05-01 03:34:43 +02:00
if (Settings::values().webView.geometry.isEmpty()) {
2016-08-25 00:45:32 +02:00
resize(1200, 800);
centerWidget(this);
} else {
2016-11-02 20:03:38 +01:00
restoreGeometry(Settings::values().webView.geometry);
2016-08-25 00:45:32 +02:00
}
}
QtGui::WebViewDialog::~WebViewDialog()
{
2016-11-02 20:03:38 +01:00
Settings::values().webView.geometry = saveGeometry();
2016-08-25 00:45:32 +02:00
}
void QtGui::WebViewDialog::applySettings(const Data::SyncthingConnectionSettings &connectionSettings, bool aboutToShow)
2016-08-25 00:45:32 +02:00
{
// delete the web view if currently hidden and the configuration to keep it running in the background isn't enabled
const auto &settings(Settings::values());
if (!aboutToShow && !settings.webView.keepRunning && isHidden()) {
deleteLater();
return;
}
// apply settings to the view
m_connectionSettings = connectionSettings;
if (!WebPage::isSamePage(m_view->url(), connectionSettings.syncthingUrl)) { // prevent reload if the URL remains the same
m_view->setUrl(connectionSettings.syncthingUrl);
}
m_view->setZoomFactor(settings.webView.zoomFactor);
2016-08-25 00:45:32 +02:00
}
#if defined(SYNCTHINGWIDGETS_USE_WEBKIT)
2016-11-16 20:40:54 +01:00
bool WebViewDialog::isModalVisible() const
{
2017-05-01 03:34:43 +02:00
if (m_view->page()->mainFrame()) {
2016-11-16 20:40:54 +01:00
return m_view->page()->mainFrame()->evaluateJavaScript(QStringLiteral("$('.modal-dialog').is(':visible')")).toBool();
}
return false;
}
#endif
void WebViewDialog::closeUnlessModalVisible()
{
#if defined(SYNCTHINGWIDGETS_USE_WEBKIT)
2017-05-01 03:34:43 +02:00
if (!isModalVisible()) {
2016-11-16 20:40:54 +01:00
close();
}
#elif defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
2017-05-01 03:34:43 +02:00
m_view->page()->runJavaScript(QStringLiteral("$('.modal-dialog').is(':visible')"), [this](const QVariant &modalVisible) {
if (!modalVisible.toBool()) {
2016-11-16 20:40:54 +01:00
close();
}
});
#else
close();
#endif
}
2016-08-25 00:45:32 +02:00
void QtGui::WebViewDialog::closeEvent(QCloseEvent *event)
{
2017-05-01 03:34:43 +02:00
if (!Settings::values().webView.keepRunning) {
2016-08-25 00:45:32 +02:00
deleteLater();
}
event->accept();
}
2016-11-01 17:16:27 +01:00
void WebViewDialog::keyPressEvent(QKeyEvent *event)
{
2017-05-01 03:34:43 +02:00
switch (event->key()) {
2016-11-01 17:16:27 +01:00
case Qt::Key_F5:
m_view->reload();
event->accept();
break;
2016-11-16 20:40:54 +01:00
case Qt::Key_Escape:
closeUnlessModalVisible();
event->accept();
break;
2016-11-01 17:16:27 +01:00
default:
QMainWindow::keyPressEvent(event);
}
}
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
bool WebViewDialog::eventFilter(QObject *watched, QEvent *event)
{
2017-05-01 03:34:43 +02:00
switch (event->type()) {
case QEvent::ChildAdded:
2017-05-01 03:34:43 +02:00
if (m_view->focusProxy()) {
m_view->focusProxy()->installEventFilter(this);
}
break;
case QEvent::KeyPress:
keyPressEvent(static_cast<QKeyEvent *>(event));
break;
case QEvent::PaletteChange:
if (auto *const page = qobject_cast<WebPage *>(m_view->page())) {
page->styleScrollBars(true);
}
break;
2017-05-01 03:34:43 +02:00
default:;
}
return QMainWindow::eventFilter(watched, event);
}
#endif
} // namespace QtGui
2016-08-25 00:45:32 +02:00
#endif // SYNCTHINGWIDGETS_NO_WEBVIEW
namespace QtGui {
/*!
* \brief Returns a list of binaries of Chromium-based browsers to consider.
* \remarks
* The binary names will be passed to SyncthingProcess which will try to locate them in path. On Windows,
* the absolute binary path might be pre-determined by this function checking the "Application Registry",
* though.
* \sa documentation of "Application Registry": https://learn.microsoft.com/en-us/windows/win32/shell/app-registration
*/
static QStringList chromiumBasedBrowserBinaries()
{
static const auto envOverride = qEnvironmentVariable(PROJECT_VARNAME_UPPER "_CHROMIUM_BASED_BROWSER");
if (!envOverride.isEmpty()) {
2023-03-31 22:46:48 +02:00
return { envOverride };
}
static const auto relevantBinaries = std::initializer_list<QString>{
#ifdef Q_OS_WINDOWS
2023-03-31 22:46:48 +02:00
QStringLiteral("msedge.exe"),
QStringLiteral("chromium.exe"),
QStringLiteral("chrome.exe"),
#else
2023-03-31 22:46:48 +02:00
QStringLiteral("chromium"),
QStringLiteral("chrome"),
QStringLiteral("msedge"),
#endif
};
#ifdef Q_OS_WINDOWS
2023-03-31 22:46:48 +02:00
const auto appPath
= QSettings(QStringLiteral("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths"), QSettings::NativeFormat);
for (const auto &binaryName : relevantBinaries) {
const auto binaryPath = appPath.value(binaryName + QStringLiteral("/Default")).toString();
if (!binaryPath.isEmpty() && QFile::exists(binaryPath)) {
2023-03-31 22:46:48 +02:00
return { binaryPath };
}
}
#endif
return relevantBinaries;
}
/*!
* \brief Opens the specified \a url as "app" in a Chromium-based web browser.
* \todo Check for other Chromium-based browsers and use the Windows registry to find apps under Windows.
*/
static void openBrowserInAppMode(const QString &url)
{
const auto &configuredCustomCommand = Settings::values().webView.customCommand;
2023-06-20 11:09:46 +02:00
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
const
#endif
auto customCommand
= Data::SyncthingProcess::splitArguments(QString(configuredCustomCommand).replace(QLatin1String("%SYNCTHING_URL%"), url));
const auto appList = customCommand.isEmpty() ? chromiumBasedBrowserBinaries() : QStringList{ customCommand.first() };
2023-06-20 11:09:46 +02:00
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
customCommand.removeFirst();
#endif
const auto args = customCommand.isEmpty() ? QStringList{ QStringLiteral("--app=") + url } :
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QStringList(customCommand.cbegin() + 1, customCommand.cend())
#else
customCommand
#endif
;
auto *const process = new Data::SyncthingProcess();
QObject::connect(process, &Data::SyncthingProcess::finished, process, &QObject::deleteLater);
QObject::connect(process, &Data::SyncthingProcess::errorOccurred, process, [process] {
auto messageBox = QMessageBox();
messageBox.setWindowTitle(QStringLiteral("Syncthing"));
messageBox.setWindowIcon(QIcon(QStringLiteral(":/icons/hicolor/scalable/app/syncthingtray.svg")));
messageBox.setIcon(QMessageBox::Critical);
messageBox.setText(
QCoreApplication::translate("QtGui", "Unable to open Syncthing UI via \"%1\": %2").arg(process->program(), process->errorString()));
messageBox.exec();
});
process->setProcessChannelMode(QProcess::ForwardedChannels);
process->start(appList
#ifndef LIB_SYNCTHING_CONNECTOR_BOOST_PROCESS
.first()
#endif
,
args);
}
/*!
* \brief Opens the Syncthing UI using the configured web view mode.
* \param url The URL of the Syncthing UI.
* \param settings The connection settings to be used (instead of the \a url) in case a WebViewDialog is used. Allowed to be nullptr.
* \param dlg An existing WebViewDialog that may be reused in case a WebViewDialog is used.
* \param parent The parent to use when creating a new WebViewDialog. Allowed to be nullptr (as usual with parents).
* \returns Returns the used WebViewDialog or nullptr if another method was used.
*/
WebViewDialog *showWebUI(const QString &url, const Data::SyncthingConnectionSettings *settings, WebViewDialog *dlg, QWidget *parent)
{
switch (Settings::values().webView.mode) {
#ifndef SYNCTHINGWIDGETS_NO_WEBVIEW
case Settings::WebView::Mode::Builtin:
if (!dlg) {
dlg = new WebViewDialog(parent);
}
if (settings) {
dlg->applySettings(*settings, true);
}
return dlg;
#else
Q_UNUSED(settings)
Q_UNUSED(dlg)
Q_UNUSED(parent)
#endif
case Settings::WebView::Mode::Command:
openBrowserInAppMode(url);
break;
default:
QDesktopServices::openUrl(url);
}
return nullptr;
}
} // namespace QtGui