From 43c82ccb09f05c3a14fd6109183e442facc58e97 Mon Sep 17 00:00:00 2001 From: varjolintu Date: Sun, 17 May 2020 22:44:52 +0300 Subject: [PATCH] Custom browser feature for Linux/macOS * Also move "search in all databases" to the general tab --- src/browser/BrowserSettings.cpp | 30 +++ src/browser/BrowserSettings.h | 7 +- src/browser/BrowserSettingsWidget.cpp | 49 ++++ src/browser/BrowserSettingsWidget.h | 1 + src/browser/BrowserSettingsWidget.ui | 349 +++++++++++++++++++------ src/browser/BrowserShared.h | 1 + src/browser/NativeMessageInstaller.cpp | 14 +- src/core/Config.cpp | 3 + src/core/Config.h | 3 + 9 files changed, 378 insertions(+), 79 deletions(-) diff --git a/src/browser/BrowserSettings.cpp b/src/browser/BrowserSettings.cpp index 18477e599..93b5c138b 100644 --- a/src/browser/BrowserSettings.cpp +++ b/src/browser/BrowserSettings.cpp @@ -182,6 +182,36 @@ void BrowserSettings::setCustomProxyLocation(const QString& location) config()->set(Config::Browser_CustomProxyLocation, location); } +bool BrowserSettings::customBrowserSupport() +{ + return config()->get(Config::Browser_UseCustomBrowser).toBool(); +} + +void BrowserSettings::setCustomBrowserSupport(bool enabled) +{ + config()->set(Config::Browser_UseCustomBrowser, enabled); +} + +int BrowserSettings::customBrowserType() +{ + return config()->get(Config::Browser_CustomBrowserType).toInt(); +} + +void BrowserSettings::setCustomBrowserType(int type) +{ + config()->set(Config::Browser_CustomBrowserType, type); +} + +QString BrowserSettings::customBrowserLocation() +{ + return config()->get(Config::Browser_CustomBrowserLocation).toString(); +} + +void BrowserSettings::setCustomBrowserLocation(const QString& location) +{ + config()->set(Config::Browser_CustomBrowserLocation, location); +} + QString BrowserSettings::proxyLocation() { return m_nativeMessageInstaller.getProxyPath(); diff --git a/src/browser/BrowserSettings.h b/src/browser/BrowserSettings.h index e3bfe6ab4..4e457a929 100644 --- a/src/browser/BrowserSettings.h +++ b/src/browser/BrowserSettings.h @@ -75,7 +75,12 @@ public: bool browserSupport(BrowserShared::SupportedBrowsers browser); void setBrowserSupport(BrowserShared::SupportedBrowsers browser, bool enabled); - + bool customBrowserSupport(); + void setCustomBrowserSupport(bool enabled); + int customBrowserType(); + void setCustomBrowserType(int type); + QString customBrowserLocation(); + void setCustomBrowserLocation(const QString& location); bool passwordUseNumbers(); void setPasswordUseNumbers(bool useNumbers); bool passwordUseLowercase(); diff --git a/src/browser/BrowserSettingsWidget.cpp b/src/browser/BrowserSettingsWidget.cpp index 7eaf87c2d..d0bdad1f1 100644 --- a/src/browser/BrowserSettingsWidget.cpp +++ b/src/browser/BrowserSettingsWidget.cpp @@ -57,8 +57,20 @@ BrowserSettingsWidget::BrowserSettingsWidget(QWidget* parent) m_ui->tabWidget->setEnabled(m_ui->enableBrowserSupport->isChecked()); connect(m_ui->enableBrowserSupport, SIGNAL(toggled(bool)), m_ui->tabWidget, SLOT(setEnabled(bool))); + // Custom Browser option +#ifdef Q_OS_WIN + // TODO: Custom browser is disabled on Windows + m_ui->customBrowserSupport->setVisible(false); + m_ui->customBrowserGroupBox->setVisible(false); +#else + connect(m_ui->customBrowserLocationBrowseButton, SIGNAL(clicked()), SLOT(showCustomBrowserLocationFileDialog())); + connect(m_ui->customBrowserSupport, SIGNAL(toggled(bool)), m_ui->customBrowserGroupBox, SLOT(setEnabled(bool))); +#endif + + // Custom Proxy option m_ui->customProxyLocation->setEnabled(m_ui->useCustomProxy->isChecked()); m_ui->customProxyLocationBrowseButton->setEnabled(m_ui->useCustomProxy->isChecked()); + connect(m_ui->useCustomProxy, SIGNAL(toggled(bool)), m_ui->customProxyLocation, SLOT(setEnabled(bool))); connect(m_ui->useCustomProxy, SIGNAL(toggled(bool)), m_ui->customProxyLocationBrowseButton, SLOT(setEnabled(bool))); connect(m_ui->useCustomProxy, SIGNAL(toggled(bool)), SLOT(validateCustomProxyLocation())); @@ -150,6 +162,9 @@ void BrowserSettingsWidget::loadSettings() m_ui->browsersGroupBox->setEnabled(false); m_ui->updateBinaryPath->setChecked(false); m_ui->updateBinaryPath->setVisible(false); + // No custom browser for snaps + m_ui->customBrowserSupport->setVisible(false); + m_ui->customBrowserGroupBox->setVisible(false); // Show notice to user m_ui->browserGlobalWarningWidget->showMessage(tr("Please see special instructions for browser extension use below"), MessageWidget::Warning); @@ -157,6 +172,18 @@ void BrowserSettingsWidget::loadSettings() m_ui->browserGlobalWarningWidget->setAutoHideTimeout(-1); #endif + const auto customBrowserSet = settings->customBrowserSupport(); + m_ui->customBrowserSupport->setChecked(customBrowserSet); + m_ui->customBrowserGroupBox->setEnabled(customBrowserSet); + m_ui->browserTypeComboBox->clear(); + m_ui->browserTypeComboBox->addItem(tr("Firefox"), BrowserShared::SupportedBrowsers::FIREFOX); + m_ui->browserTypeComboBox->addItem(tr("Chromium"), BrowserShared::SupportedBrowsers::CHROMIUM); + auto typeIndex = m_ui->browserTypeComboBox->findData(settings->customBrowserType()); + if (typeIndex >= 0) { + m_ui->browserTypeComboBox->setCurrentIndex(typeIndex); + } + m_ui->customBrowserLocation->setText(settings->customBrowserLocation()); + #ifdef QT_DEBUG m_ui->customExtensionId->setText(settings->customExtensionId()); #endif @@ -211,6 +238,18 @@ void BrowserSettingsWidget::saveSettings() settings->setBrowserSupport(BrowserShared::BRAVE, m_ui->braveSupport->isChecked()); settings->setBrowserSupport(BrowserShared::VIVALDI, m_ui->vivaldiSupport->isChecked()); settings->setBrowserSupport(BrowserShared::TOR_BROWSER, m_ui->torBrowserSupport->isChecked()); + + // Custom browser settings + bool customBrowserEnabled = m_ui->customBrowserSupport->isChecked(); + settings->setCustomBrowserType(m_ui->browserTypeComboBox->currentData().toInt()); + settings->setCustomBrowserLocation(m_ui->customBrowserLocation->text()); + settings->setCustomBrowserSupport(customBrowserEnabled); + settings->setBrowserSupport(BrowserShared::CUSTOM, customBrowserEnabled); + + // If we disabled the custom browser support make sure to clear variables + if (!customBrowserEnabled) { + settings->setCustomBrowserLocation(""); + } #endif } @@ -228,3 +267,13 @@ void BrowserSettingsWidget::showProxyLocationFileDialog() m_ui->customProxyLocation->setText(proxyLocation); validateCustomProxyLocation(); } + +void BrowserSettingsWidget::showCustomBrowserLocationFileDialog() +{ + auto location = QFileDialog::getExistingDirectory(this, + tr("Select native messaging host folder location"), + QFileInfo(QCoreApplication::applicationDirPath()).filePath()); + if (!location.isEmpty()) { + m_ui->customBrowserLocation->setText(location); + } +} diff --git a/src/browser/BrowserSettingsWidget.h b/src/browser/BrowserSettingsWidget.h index 8f9dea62f..3f5ea5763 100644 --- a/src/browser/BrowserSettingsWidget.h +++ b/src/browser/BrowserSettingsWidget.h @@ -42,6 +42,7 @@ public slots: private slots: void showProxyLocationFileDialog(); void validateCustomProxyLocation(); + void showCustomBrowserLocationFileDialog(); private: QScopedPointer m_ui; diff --git a/src/browser/BrowserSettingsWidget.ui b/src/browser/BrowserSettingsWidget.ui index 467aa6167..40064b541 100644 --- a/src/browser/BrowserSettingsWidget.ui +++ b/src/browser/BrowserSettingsWidget.ui @@ -6,8 +6,8 @@ 0 0 - 523 - 456 + 584 + 467 @@ -84,23 +84,20 @@ 40 - - - - Qt::Horizontal - - - - 179 - 20 - - - - - - + + - Google Chrome + Vivaldi + + + false + + + + + + + &Edge false @@ -117,26 +114,6 @@ - - - - Chromium - - - false - - - - - - - Vivaldi - - - false - - - @@ -157,16 +134,39 @@ - - + + - &Edge + Google Chrome false + + + + Chromium + + + false + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + @@ -236,6 +236,16 @@ + + + + All databases connected to the extension will return matching credentials. + + + Search in all opened databases for matching credentials + + + @@ -301,16 +311,6 @@ - - - - All databases connected to the extension will return matching credentials. - - - Search in all opened databases for matching credentials - - - @@ -347,14 +347,42 @@ Use a custom proxy location if you installed a proxy manually. - Use a custom proxy location + Use a custom proxy location: + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 15 + 20 + + + + + + + 0 + 0 + + + + + 450 + 0 + + Custom proxy location field @@ -376,34 +404,203 @@ + + + + Qt::Horizontal + + + + 40 + 20 + + + + - - - 20 + + + Use a custom browser configuration location: - - - - Custom extension ID: - - - - - - - Custom extension ID - - - 999 - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - - - - + + false + + + + + + + + + + + + + + + + 0 + 0 + + + + margin-right: 5px + + + Browser type: + + + + + + + + 0 + 0 + + + + Qt::StrongFocus + + + Toolbar button style + + + QComboBox::AdjustToContents + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + 0 + 0 + + + + margin-right: 5px + + + Config Location: + + + + + + + + 0 + 0 + + + + + 350 + 0 + + + + Custom browser location field + + + 999 + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + ~/.custom/config/Mozilla/native-messaging-hosts/ + + + + + + + Browse for custom browser path + + + Browse... + + + + + + + + + + + + + 6 + + + + + Custom extension ID: + + + + + + + + 0 + 0 + + + + + 250 + 0 + + + + Custom extension ID + + + 999 + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + diff --git a/src/browser/BrowserShared.h b/src/browser/BrowserShared.h index 02bee9c44..e9542a74c 100644 --- a/src/browser/BrowserShared.h +++ b/src/browser/BrowserShared.h @@ -33,6 +33,7 @@ namespace BrowserShared TOR_BROWSER, BRAVE, EDGE, + CUSTOM, MAX_SUPPORTED }; diff --git a/src/browser/NativeMessageInstaller.cpp b/src/browser/NativeMessageInstaller.cpp index d9c6428bb..8b038f619 100644 --- a/src/browser/NativeMessageInstaller.cpp +++ b/src/browser/NativeMessageInstaller.cpp @@ -114,7 +114,7 @@ void NativeMessageInstaller::setBrowserEnabled(SupportedBrowsers browser, bool e } } else { // Remove the script file - QString fileName = getNativeMessagePath(browser); + const QString fileName = getNativeMessagePath(browser); QFile::remove(fileName); #ifdef Q_OS_WIN // Remove the registry entry @@ -159,6 +159,8 @@ QString NativeMessageInstaller::getTargetPath(SupportedBrowsers browser) const return TARGET_DIR_BRAVE; case SupportedBrowsers::EDGE: return TARGET_DIR_EDGE; + case SupportedBrowsers::CUSTOM: + return browserSettings()->customBrowserLocation(); default: return {}; } @@ -188,6 +190,8 @@ QString NativeMessageInstaller::getBrowserName(SupportedBrowsers browser) const return QStringLiteral("brave"); case SupportedBrowsers::EDGE: return QStringLiteral("edge"); + case SupportedBrowsers::CUSTOM: + return QStringLiteral("custom"); default: return {}; } @@ -223,6 +227,10 @@ QString NativeMessageInstaller::getNativeMessagePath(SupportedBrowsers browser) #else basePath = QDir::homePath(); #endif + if (browser == SupportedBrowsers::CUSTOM) { + return QString("%1/%2.json").arg(getTargetPath(browser), HOST_NAME); + } + return QStringLiteral("%1%2/%3.json").arg(basePath, getTargetPath(browser), HOST_NAME); } @@ -267,7 +275,9 @@ QJsonObject NativeMessageInstaller::constructFile(SupportedBrowsers browser) script["type"] = QStringLiteral("stdio"); QJsonArray arr; - if (browser == SupportedBrowsers::FIREFOX || browser == SupportedBrowsers::TOR_BROWSER) { + if (browser == SupportedBrowsers::FIREFOX || browser == SupportedBrowsers::TOR_BROWSER + || (browser == SupportedBrowsers::CUSTOM + && browserSettings()->customBrowserType() == SupportedBrowsers::FIREFOX)) { for (const QString& extension : ALLOWED_EXTENSIONS) { arr.append(extension); } diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 0a4e88b76..b413d460e 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -152,6 +152,9 @@ static const QHash configStrings = { {Config::Browser_SearchInAllDatabases, {QS("Browser/SearchInAllDatabases"), Roaming, false}}, {Config::Browser_SupportKphFields, {QS("Browser/SupportKphFields"), Roaming, true}}, {Config::Browser_NoMigrationPrompt, {QS("Browser/NoMigrationPrompt"), Roaming, false}}, + {Config::Browser_UseCustomBrowser, {QS("Browser/UseCustomBrowser"), Local, false}}, + {Config::Browser_CustomBrowserType, {QS("Browser/CustomBrowserType"), Local, -1}}, + {Config::Browser_CustomBrowserLocation, {QS("Browser/CustomBrowserLocation"), Local, {}}}, #ifdef QT_DEBUG {Config::Browser_CustomExtensionId, {QS("Browser/CustomExtensionId"), Local, {}}}, #endif diff --git a/src/core/Config.h b/src/core/Config.h index 382864c0b..8d4609a21 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -133,6 +133,9 @@ public: Browser_SearchInAllDatabases, Browser_SupportKphFields, Browser_NoMigrationPrompt, + Browser_UseCustomBrowser, + Browser_CustomBrowserType, + Browser_CustomBrowserLocation, #ifdef QT_DEBUG Browser_CustomExtensionId, #endif