From 250cd1933ca500d47fe0bd9a9ecd4d50bbd0d154 Mon Sep 17 00:00:00 2001 From: varjolintu Date: Tue, 22 Jun 2021 20:24:30 +0300 Subject: [PATCH] Handle tilde with custom paths --- src/browser/BrowserSettings.cpp | 25 +++++++++++++++++++++++++ src/browser/BrowserSettings.h | 2 ++ src/browser/BrowserSettingsWidget.cpp | 23 ++++++++++++----------- src/browser/BrowserSettingsWidget.ui | 2 +- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/browser/BrowserSettings.cpp b/src/browser/BrowserSettings.cpp index 331a25785..4595d5c0e 100644 --- a/src/browser/BrowserSettings.cpp +++ b/src/browser/BrowserSettings.cpp @@ -20,6 +20,7 @@ #include "BrowserSettings.h" #include "core/Config.h" #include "core/PasswordHealth.h" +#include #include @@ -516,3 +517,27 @@ void BrowserSettings::updateBinaryPaths() { m_nativeMessageInstaller.updateBinaryPaths(); } + +QString BrowserSettings::replaceHomePath(QString location) +{ +#ifndef Q_OS_WIN + auto homePath = QDir::homePath(); + if (location.startsWith(homePath)) { + location.replace(homePath, "~"); + } +#endif + + return location; +} + +QString BrowserSettings::replaceTildeHomePath(QString location) +{ +#ifndef Q_OS_WIN + auto homePath = QDir::homePath(); + if (location.startsWith("~")) { + location.replace("~", homePath); + } +#endif + + return location; +} diff --git a/src/browser/BrowserSettings.h b/src/browser/BrowserSettings.h index 3203e389d..63a4846ae 100644 --- a/src/browser/BrowserSettings.h +++ b/src/browser/BrowserSettings.h @@ -120,6 +120,8 @@ public: PasswordGenerator::GeneratorFlags passwordGeneratorFlags(); QJsonObject generatePassword(); void updateBinaryPaths(); + QString replaceHomePath(QString location); + QString replaceTildeHomePath(QString location); private: static BrowserSettings* m_instance; diff --git a/src/browser/BrowserSettingsWidget.cpp b/src/browser/BrowserSettingsWidget.cpp index a9fd53231..7aa3e3cd0 100644 --- a/src/browser/BrowserSettingsWidget.cpp +++ b/src/browser/BrowserSettingsWidget.cpp @@ -123,7 +123,7 @@ void BrowserSettingsWidget::loadSettings() m_ui->supportKphFields->setChecked(settings->supportKphFields()); m_ui->noMigrationPrompt->setChecked(settings->noMigrationPrompt()); m_ui->useCustomProxy->setChecked(settings->useCustomProxy()); - m_ui->customProxyLocation->setText(settings->customProxyLocation()); + m_ui->customProxyLocation->setText(settings->replaceHomePath(settings->customProxyLocation())); m_ui->updateBinaryPath->setChecked(settings->updateBinaryPath()); m_ui->allowExpiredCredentials->setChecked(settings->allowExpiredCredentials()); m_ui->chromeSupport->setChecked(settings->browserSupport(BrowserShared::CHROME)); @@ -175,7 +175,7 @@ void BrowserSettingsWidget::loadSettings() if (typeIndex >= 0) { m_ui->browserTypeComboBox->setCurrentIndex(typeIndex); } - m_ui->customBrowserLocation->setText(settings->customBrowserLocation()); + m_ui->customBrowserLocation->setText(settings->replaceHomePath(settings->customBrowserLocation())); #ifdef QT_DEBUG m_ui->customExtensionId->setText(settings->customExtensionId()); @@ -186,7 +186,8 @@ void BrowserSettingsWidget::loadSettings() void BrowserSettingsWidget::validateCustomProxyLocation() { - auto path = m_ui->customProxyLocation->text(); + auto path = browserSettings()->customProxyLocation(); + if (m_ui->useCustomProxy->isChecked() && !QFile::exists(path)) { m_ui->warningWidget->showMessage(tr("Error: The custom proxy location cannot be found!" "
Browser integration WILL NOT WORK without the proxy application."), @@ -207,7 +208,7 @@ void BrowserSettingsWidget::saveSettings() settings->setMatchUrlScheme(m_ui->matchUrlScheme->isChecked()); settings->setUseCustomProxy(m_ui->useCustomProxy->isChecked()); - settings->setCustomProxyLocation(m_ui->customProxyLocation->text()); + settings->setCustomProxyLocation(browserSettings()->replaceTildeHomePath(m_ui->customProxyLocation->text())); settings->setUpdateBinaryPath(m_ui->updateBinaryPath->isChecked()); settings->setAllowExpiredCredentials(m_ui->allowExpiredCredentials->isChecked()); @@ -232,16 +233,12 @@ void BrowserSettingsWidget::saveSettings() settings->setBrowserSupport(BrowserShared::TOR_BROWSER, m_ui->torBrowserSupport->isChecked()); // Custom browser settings - bool customBrowserEnabled = m_ui->customBrowserSupport->isChecked(); + auto customBrowserEnabled = m_ui->customBrowserSupport->isChecked(); settings->setCustomBrowserType(m_ui->browserTypeComboBox->currentData().toInt()); - settings->setCustomBrowserLocation(m_ui->customBrowserLocation->text()); + settings->setCustomBrowserLocation( + customBrowserEnabled ? browserSettings()->replaceTildeHomePath(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 } @@ -256,6 +253,8 @@ void BrowserSettingsWidget::showProxyLocationFileDialog() tr("Select custom proxy location"), QFileInfo(QCoreApplication::applicationDirPath()).filePath(), fileTypeFilter); + + proxyLocation = browserSettings()->replaceHomePath(proxyLocation); m_ui->customProxyLocation->setText(proxyLocation); validateCustomProxyLocation(); } @@ -265,6 +264,8 @@ void BrowserSettingsWidget::showCustomBrowserLocationFileDialog() auto location = QFileDialog::getExistingDirectory(this, tr("Select native messaging host folder location"), QFileInfo(QCoreApplication::applicationDirPath()).filePath()); + + location = browserSettings()->replaceHomePath(location); if (!location.isEmpty()) { m_ui->customBrowserLocation->setText(location); } diff --git a/src/browser/BrowserSettingsWidget.ui b/src/browser/BrowserSettingsWidget.ui index 497ffc163..25508a7b9 100644 --- a/src/browser/BrowserSettingsWidget.ui +++ b/src/browser/BrowserSettingsWidget.ui @@ -515,7 +515,7 @@ Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - ~/.custom/config/Mozilla/native-messaging-hosts/ + ~/.config/Mozilla/native-messaging-hosts/