Skip to content

Commit 3a9fd23

Browse files
author
varjolintu
committed
Simplify the code
1 parent 484fc95 commit 3a9fd23

5 files changed

Lines changed: 50 additions & 46 deletions

File tree

src/browser/BrowserOptionDialog.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ void BrowserOptionDialog::loadSettings()
157157
m_ui->browserGlobalWarningWidget->setAutoHideTimeout(-1);
158158
#endif
159159

160-
m_ui->customBrowserSupport->setChecked(settings->useCustomBrowser());
161-
m_ui->browserTypeComboBox->setEnabled(settings->useCustomBrowser());
160+
const auto customBrowserSet = settings->customBrowserSupport();
161+
m_ui->customBrowserSupport->setChecked(customBrowserSet);
162+
m_ui->browserTypeComboBox->setEnabled(customBrowserSet);
162163
m_ui->browserTypeComboBox->clear();
163164
m_ui->browserTypeComboBox->addItem(tr("Firefox"), Qt::ToolButtonIconOnly);
164165
m_ui->browserTypeComboBox->addItem(tr("Chromium"), Qt::ToolButtonTextOnly);
@@ -167,9 +168,9 @@ void BrowserOptionDialog::loadSettings()
167168
m_ui->browserTypeComboBox->setCurrentIndex(browserTypeIndex);
168169
}
169170

170-
m_ui->customBrowserLocation->setEnabled(settings->useCustomBrowser());
171+
m_ui->customBrowserLocation->setEnabled(customBrowserSet);
171172
m_ui->customBrowserLocation->setText(settings->customBrowserLocation());
172-
m_ui->customBrowserLocationBrowseButton->setEnabled(settings->useCustomBrowser());
173+
m_ui->customBrowserLocationBrowseButton->setEnabled(customBrowserSet);
173174

174175
// Check for native messaging host location errors
175176
QString path;
@@ -219,9 +220,9 @@ void BrowserOptionDialog::saveSettings()
219220
settings->setTorBrowserSupport(m_ui->torBrowserSupport->isChecked());
220221
#endif
221222

222-
settings->setUseCustomBrowser(m_ui->customBrowserSupport->isChecked());
223223
settings->setCustomBrowserType(m_ui->browserTypeComboBox->currentIndex());
224224
settings->setCustomBrowserLocation(m_ui->customBrowserLocation->text());
225+
settings->setCustomBrowserSupport(m_ui->customBrowserSupport->isChecked());
225226
}
226227

227228
void BrowserOptionDialog::showProxyLocationFileDialog()

src/browser/BrowserSettings.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,20 @@ void BrowserSettings::setAllowExpiredCredentials(bool enabled)
214214
config()->set("Browser/AllowExpiredCredentials", enabled);
215215
}
216216

217-
bool BrowserSettings::useCustomBrowser() {
218-
return config()->get("Browser/UseCustomBrowser", false).toBool();
217+
bool BrowserSettings::customBrowserSupport() {
218+
auto type = config()->get("Browser/CustomBrowserType", 1).toInt() == 0
219+
? HostInstaller::SupportedBrowsers::FIREFOX : HostInstaller::SupportedBrowsers::CHROMIUM;
220+
m_hostInstaller.setCustomBrowserSettings(customBrowserLocation(), type);
221+
return m_hostInstaller.checkIfInstalled(HostInstaller::SupportedBrowsers::CUSTOM);
219222
}
220223

221-
void BrowserSettings::setUseCustomBrowser(bool enabled) {
222-
config()->set("Browser/UseCustomBrowser", enabled);
224+
void BrowserSettings::setCustomBrowserSupport(bool enabled) {
225+
auto type = config()->get("Browser/CustomBrowserType", 1).toInt() == 0
226+
? HostInstaller::SupportedBrowsers::FIREFOX : HostInstaller::SupportedBrowsers::CHROMIUM;
227+
m_hostInstaller.setCustomBrowserSettings(customBrowserLocation(), type);
228+
229+
m_hostInstaller.installBrowser(
230+
HostInstaller::SupportedBrowsers::CUSTOM, enabled, supportBrowserProxy(), customProxyLocation());
223231
}
224232

225233
bool BrowserSettings::customBrowserType()
@@ -239,19 +247,7 @@ QString BrowserSettings::customBrowserLocation()
239247

240248
void BrowserSettings::setCustomBrowserLocation(const QString& location)
241249
{
242-
if (!config()->get("Browser/UseCustomBrowser", false).toBool()) {
243-
return;
244-
}
245-
246250
config()->set("Browser/CustomBrowserLocation", location);
247-
auto type = config()->get("Browser/CustomBrowserType", 1).toInt() == 0
248-
? HostInstaller::SupportedBrowsers::FIREFOX : HostInstaller::SupportedBrowsers::CHROMIUM;
249-
m_hostInstaller.installBrowser(type,
250-
(location.isEmpty() ? false : true),
251-
supportBrowserProxy(),
252-
customProxyLocation(),
253-
true,
254-
customBrowserLocation());
255251
}
256252

257253
bool BrowserSettings::chromeSupport()

src/browser/BrowserSettings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class BrowserSettings
6868
void setUpdateBinaryPath(bool enabled);
6969
bool allowExpiredCredentials();
7070
void setAllowExpiredCredentials(bool enabled);
71-
bool useCustomBrowser();
72-
void setUseCustomBrowser(bool enabled);
71+
bool customBrowserSupport();
72+
void setCustomBrowserSupport(bool enabled);
7373
bool customBrowserType();
7474
void setCustomBrowserType(int type);
7575
QString customBrowserLocation();

src/browser/HostInstaller.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ bool HostInstaller::checkIfProxyExists(const bool& proxy, const QString& locatio
103103
void HostInstaller::installBrowser(SupportedBrowsers browser,
104104
const bool& enabled,
105105
const bool& proxy,
106-
const QString& location,
107-
const bool& customBrowser,
108-
const QString& customBrowserPath)
106+
const QString& location)
109107
{
110108
if (enabled) {
111109
#ifdef Q_OS_WIN
@@ -115,15 +113,15 @@ void HostInstaller::installBrowser(SupportedBrowsers browser,
115113
#endif
116114
// Always create the script file
117115
QJsonObject script = constructFile(browser, proxy, location);
118-
if (!saveFile(browser, script, customBrowser, customBrowserPath)) {
116+
if (!saveFile(browser, script)) {
119117
QMessageBox::critical(nullptr,
120118
tr("KeePassXC: Cannot save file!"),
121119
tr("Cannot save the native messaging script file."),
122120
QMessageBox::Ok);
123121
}
124122
} else {
125123
// Remove the script file
126-
const QString fileName = customBrowser ? customBrowserPath : getPath(browser);
124+
const QString fileName = getPath(browser);
127125
QFile::remove(fileName);
128126
#ifdef Q_OS_WIN
129127
// Remove the registry entry
@@ -141,14 +139,19 @@ void HostInstaller::installBrowser(SupportedBrowsers browser,
141139
*/
142140
void HostInstaller::updateBinaryPaths(const bool& proxy, const QString& location)
143141
{
144-
// Where 6 is the number of entries in the SupportedBrowsers enum declared in HostInstaller.h
145-
for (int i = 0; i < 6; ++i) {
142+
for (int i = 0; i <= SupportedBrowsers::CUSTOM; ++i) {
146143
if (checkIfInstalled(static_cast<SupportedBrowsers>(i))) {
147144
installBrowser(static_cast<SupportedBrowsers>(i), true, proxy, location);
148145
}
149146
}
150147
}
151148

149+
void HostInstaller::setCustomBrowserSettings(const QString& path, SupportedBrowsers browser)
150+
{
151+
m_customBrowserPath = path;
152+
m_customBrowserType = browser;
153+
}
154+
152155
/**
153156
* Returns the target path for each browser. Windows uses a registry path instead of a file path
154157
*
@@ -170,6 +173,8 @@ QString HostInstaller::getTargetPath(SupportedBrowsers browser) const
170173
return TARGET_DIR_TOR_BROWSER;
171174
case SupportedBrowsers::BRAVE:
172175
return TARGET_DIR_BRAVE;
176+
case SupportedBrowsers::CUSTOM:
177+
return m_customBrowserPath;
173178
default:
174179
return QString();
175180
}
@@ -197,6 +202,8 @@ QString HostInstaller::getBrowserName(SupportedBrowsers browser) const
197202
return "tor-browser";
198203
case SupportedBrowsers::BRAVE:
199204
return "brave";
205+
case SupportedBrowsers::CUSTOM:
206+
return "custom";
200207
default:
201208
return QString();
202209
}
@@ -225,6 +232,11 @@ QString HostInstaller::getPath(SupportedBrowsers browser) const
225232
return winPath;
226233
#else
227234
const QString path = getTargetPath(browser);
235+
236+
if (browser == SupportedBrowsers::CUSTOM) {
237+
return QString("%1/%2.json").arg(m_customBrowserPath, HOST_NAME);
238+
}
239+
228240
return QString("%1%2/%3.json").arg(QDir::homePath(), path, HOST_NAME);
229241
#endif
230242
}
@@ -302,7 +314,8 @@ QJsonObject HostInstaller::constructFile(SupportedBrowsers browser, const bool&
302314
script["type"] = QString("stdio");
303315

304316
QJsonArray arr;
305-
if (browser == SupportedBrowsers::FIREFOX || browser == SupportedBrowsers::TOR_BROWSER) {
317+
if (browser == SupportedBrowsers::FIREFOX || browser == SupportedBrowsers::TOR_BROWSER ||
318+
(browser == SupportedBrowsers::CUSTOM && m_customBrowserType == SupportedBrowsers::FIREFOX)) {
306319
for (const QString& extension : ALLOWED_EXTENSIONS) {
307320
arr.append(extension);
308321
}
@@ -335,16 +348,11 @@ bool HostInstaller::registryEntryFound(const QSettings& settings)
335348
* @param script JSON native messaging script object
336349
* @return bool Write succeeds
337350
*/
338-
bool HostInstaller::saveFile(SupportedBrowsers browser,
339-
const QJsonObject& script,
340-
const bool& customBrowser,
341-
const QString& customBrowserPath)
351+
bool HostInstaller::saveFile(SupportedBrowsers browser, const QJsonObject& script)
342352
{
343-
const QString path = customBrowser ?
344-
QString("%1/%2.json").arg(customBrowserPath, HOST_NAME) :
345-
getPath(browser);
353+
const QString path = getPath(browser);
346354
const QString installDir = getInstallDir(browser);
347-
QDir dir(installDir);
355+
const QDir dir(installDir);
348356
if (!dir.exists()) {
349357
QDir().mkpath(installDir);
350358
}

src/browser/HostInstaller.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ class HostInstaller : public QObject
4646
void installBrowser(SupportedBrowsers browser,
4747
const bool& enabled,
4848
const bool& proxy = false,
49-
const QString& location = "",
50-
const bool& customBrowser = false,
51-
const QString& customBrowserPath = "");
49+
const QString& location = "");
5250
void updateBinaryPaths(const bool& proxy, const QString& location = "");
51+
void setCustomBrowserSettings(const QString& path, SupportedBrowsers browser);
5352

5453
private:
5554
QString getTargetPath(SupportedBrowsers browser) const;
@@ -59,10 +58,7 @@ class HostInstaller : public QObject
5958
QString getProxyPath(const bool& proxy, const QString& location) const;
6059
QJsonObject constructFile(SupportedBrowsers browser, const bool& proxy, const QString& location);
6160
bool registryEntryFound(const QSettings& settings);
62-
bool saveFile(SupportedBrowsers browser,
63-
const QJsonObject& script,
64-
const bool& customBrowser = false,
65-
const QString& customBrowserPath = "");
61+
bool saveFile(SupportedBrowsers browser, const QJsonObject& script);
6662

6763
private:
6864
const QString HOST_NAME;
@@ -74,6 +70,9 @@ class HostInstaller : public QObject
7470
const QString TARGET_DIR_VIVALDI;
7571
const QString TARGET_DIR_TOR_BROWSER;
7672
const QString TARGET_DIR_BRAVE;
73+
74+
QString m_customBrowserPath;
75+
SupportedBrowsers m_customBrowserType;
7776
};
7877

7978
#endif // HOSTINSTALLER_H

0 commit comments

Comments
 (0)