refactor(browser): improve code readability

This commit is contained in:
Oleg Shparber 2020-05-10 11:04:12 -04:00
parent e477c4a0c9
commit 6c2e13828d
2 changed files with 20 additions and 16 deletions

View File

@ -36,8 +36,8 @@ UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent)
void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
QUrl requestUrl = info.requestUrl();
QUrl firstPartyUrl = info.firstPartyUrl();
const QUrl requestUrl = info.requestUrl();
const QUrl firstPartyUrl = info.firstPartyUrl();
if (!firstPartyUrl.isValid()) {
return;
@ -55,16 +55,18 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
return;
}
Core::Settings::ExternalLinkPolicy linkPolicy = Core::Application::instance()->settings()->externalLinkPolicy;
// TODO: [C++20] using enum Core::Settings::ExternalLinkPolicy;
typedef Core::Settings::ExternalLinkPolicy ExternalLinkPolicy;
ExternalLinkPolicy linkPolicy = Core::Application::instance()->settings()->externalLinkPolicy;
switch (info.resourceType()) {
case QWebEngineUrlRequestInfo::ResourceTypeMainFrame:
if (linkPolicy != Core::Settings::ExternalLinkPolicy::Open
&& linkPolicy != Core::Settings::ExternalLinkPolicy::Ask) {
if (linkPolicy != ExternalLinkPolicy::Open && linkPolicy != ExternalLinkPolicy::Ask) {
blockRequest(info);
}
break;
case QWebEngineUrlRequestInfo::ResourceTypeSubFrame:
if (linkPolicy != Core::Settings::ExternalLinkPolicy::Open) {
if (linkPolicy != ExternalLinkPolicy::Open) {
blockRequest(info);
}
break;

View File

@ -49,10 +49,14 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio
return true;
}
switch (Core::Application::instance()->settings()->externalLinkPolicy) {
case Core::Settings::ExternalLinkPolicy::Open:
auto appSettings = Core::Application::instance()->settings();
// TODO: [C++20] using enum Core::Settings::ExternalLinkPolicy;
typedef Core::Settings::ExternalLinkPolicy ExternalLinkPolicy;
switch (appSettings->externalLinkPolicy) {
case ExternalLinkPolicy::Open:
break;
case Core::Settings::ExternalLinkPolicy::Ask: {
case ExternalLinkPolicy::Ask: {
QMessageBox mb;
mb.setIcon(QMessageBox::Question);
mb.setText(tr("How do you want to open the external link?<br>URL: <b>%1</b>")
@ -74,9 +78,8 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio
if (mb.clickedButton() == openInZealButton) {
if (checkBox->isChecked()) {
Core::Application::instance()->settings()->externalLinkPolicy
= Core::Settings::ExternalLinkPolicy::Open;
Core::Application::instance()->settings()->save();
appSettings->externalLinkPolicy = ExternalLinkPolicy::Open;
appSettings->save();
}
return true;
@ -84,9 +87,8 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio
if (mb.clickedButton() == openInBrowserButton) {
if (checkBox->isChecked()) {
Core::Application::instance()->settings()->externalLinkPolicy
= Core::Settings::ExternalLinkPolicy::OpenInSystemBrowser;
Core::Application::instance()->settings()->save();
appSettings->externalLinkPolicy = ExternalLinkPolicy::OpenInSystemBrowser;
appSettings->save();
}
QDesktopServices::openUrl(url);
@ -95,7 +97,7 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio
break;
}
case Core::Settings::ExternalLinkPolicy::OpenInSystemBrowser:
case ExternalLinkPolicy::OpenInSystemBrowser:
QDesktopServices::openUrl(url);
return false;
}