Allow configuring custom command for launching Chromium-based browser

This commit is contained in:
Martchus 2023-03-31 22:39:20 +02:00
parent 2767c9484a
commit 3fd45accde
5 changed files with 71 additions and 12 deletions

View File

@ -12,7 +12,8 @@
<string>General</string>
</property>
<property name="windowIcon">
<iconset theme="preferences-other"/>
<iconset theme="preferences-other">
<normaloff>.</normaloff>.</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
@ -36,11 +37,31 @@
</widget>
</item>
<item>
<widget class="QRadioButton" name="appModeRadioButton">
<property name="text">
<string>Chromium-based browser in &quot;app mode&quot;</string>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
</widget>
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<widget class="QRadioButton" name="appModeRadioButton">
<property name="text">
<string>Chromium-based browser in &quot;app mode&quot;</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="appModeCfgToolButton">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset theme="system-run"/>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>

View File

@ -415,6 +415,7 @@ bool restore()
} else if (auto disabled = settings.value(QStringLiteral("disabled")); disabled.isValid()) {
webView.mode = disabled.toBool() ? WebView::Mode::Browser : WebView::Mode::Builtin;
}
webView.customCommand = settings.value(QStringLiteral("customCommand"), webView.customCommand).toString();
webView.zoomFactor = settings.value(QStringLiteral("zoomFactor"), webView.zoomFactor).toDouble();
webView.geometry = settings.value(QStringLiteral("geometry")).toByteArray();
webView.keepRunning = settings.value(QStringLiteral("keepRunning"), webView.keepRunning).toBool();
@ -534,6 +535,7 @@ bool save()
settings.beginGroup(QStringLiteral("webview"));
const auto &webView = v.webView;
settings.setValue(QStringLiteral("mode"), static_cast<int>(webView.mode));
settings.setValue(QStringLiteral("customCommand"), webView.customCommand);
settings.setValue(QStringLiteral("disabled"), webView.mode == WebView::Mode::Browser);
settings.setValue(QStringLiteral("zoomFactor"), webView.zoomFactor);
settings.setValue(QStringLiteral("geometry"), webView.geometry);

View File

@ -51,6 +51,7 @@
#include <QDesktopServices>
#include <QFileDialog>
#include <QHostAddress>
#include <QInputDialog>
#include <QMessageBox>
#if defined(PLATFORM_LINUX) && !defined(Q_OS_ANDROID)
#include <QStandardPaths>
@ -1382,9 +1383,15 @@ GeneralWebViewOptionPage::~GeneralWebViewOptionPage()
QWidget *GeneralWebViewOptionPage::setupWidget()
{
auto *const widget = GeneralWebViewOptionPageBase::setupWidget();
auto *const cfgToolButton = ui()->appModeCfgToolButton;
#ifdef SYNCTHINGWIDGETS_NO_WEBVIEW
ui()->builtinRadioButton->setEnabled(false);
#endif
const auto minHeight = cfgToolButton->height();
ui()->builtinRadioButton->setMinimumHeight(minHeight);
ui()->browserRadioButton->setMinimumHeight(minHeight);
ui()->appModeRadioButton->setMinimumHeight(minHeight);
QObject::connect(cfgToolButton, &QToolButton::clicked, cfgToolButton, [this] { showCustomCommandPrompt(); });
return widget;
}
@ -1398,6 +1405,7 @@ bool GeneralWebViewOptionPage::apply()
} else if (ui()->appModeRadioButton->isChecked()) {
webView.mode = ::Settings::WebView::Mode::Command;
}
webView.customCommand = m_customCommand;
return true;
}
@ -1415,6 +1423,22 @@ void GeneralWebViewOptionPage::reset()
ui()->appModeRadioButton->setChecked(true);
break;
}
m_customCommand = webView.customCommand;
}
void GeneralWebViewOptionPage::showCustomCommandPrompt()
{
auto dlg = QInputDialog();
dlg.setInputMode(QInputDialog::TextInput);
dlg.setWindowTitle(
QCoreApplication::translate("QtGui::GeneralWebViewOptionPage", "Custom command to launch Syncthing's UI - ") + QStringLiteral(APP_NAME));
dlg.setLabelText(QCoreApplication::translate("QtGui::GeneralWebViewOptionPage",
"<p>Enter a custom command to launch Syncthing's UI. The expression <code>%SYNCTHING_URL%</code> will be replaced with the "
"Syncthing-URL.</p><p>Leave the command empty to use the auto-detection.</p>"));
dlg.setTextValue(m_customCommand);
if (dlg.exec() == QDialog::Accepted) {
m_customCommand = dlg.textValue();
}
}
// BuiltinWebViewOptionPage

View File

@ -162,7 +162,13 @@ QMetaObject::Connection m_enabledChangedConn;
END_DECLARE_OPTION_PAGE
#endif
DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_SETUP(GeneralWebViewOptionPage)
BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(GeneralWebViewOptionPage)
private:
DECLARE_SETUP_WIDGETS
void showCustomCommandPrompt();
QString m_customCommand;
END_DECLARE_OPTION_PAGE
#ifndef SYNCTHINGWIDGETS_NO_WEBVIEW
DECLARE_UI_FILE_BASED_OPTION_PAGE(BuiltinWebViewOptionPage)
#else

View File

@ -208,7 +208,12 @@ static QStringList chromiumBasedBrowserBinaries()
*/
static void openBrowserInAppMode(const QString &url)
{
const auto appList = chromiumBasedBrowserBinaries();
const auto &configuredCustomCommand = Settings::values().webView.customCommand;
const auto customCommand
= Data::SyncthingProcess::splitArguments(QString(configuredCustomCommand).replace(QLatin1String("%SYNCTHING_URL%"), url));
const auto appList = customCommand.isEmpty() ? chromiumBasedBrowserBinaries() : QStringList{ customCommand.first() };
const auto args
= customCommand.isEmpty() ? QStringList{ QStringLiteral("--app=") + url } : QStringList(customCommand.cbegin() + 1, customCommand.cend());
auto *const process = new Data::SyncthingProcess();
QObject::connect(process, &Data::SyncthingProcess::finished, process, &QObject::deleteLater);
QObject::connect(process, &Data::SyncthingProcess::errorOccurred, process, [process] {
@ -216,16 +221,17 @@ static void openBrowserInAppMode(const QString &url)
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.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
process->start(appList
#ifndef LIB_SYNCTHING_CONNECTOR_BOOST_PROCESS
.first()
.first()
#endif
, QStringList{ QStringLiteral("--app=") + url });
,
args);
}
/*!