From 5daa7af456c7daa05b1a055932b5156ed1f88b00 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Fri, 25 Mar 2022 21:52:15 -0700 Subject: [PATCH] Fix the loading of websites when the user agent changes. https://redmine.stoutner.com/issues/821 --- src/main.cpp | 3 +- src/ui.rc/browser_ui.rc | 16 +++- src/views/BrowserView.cpp | 69 ++++++++++------ src/views/BrowserView.h | 15 +++- src/windows/BrowserWindow.cpp | 144 +++++++++++++++++++++++++++++----- src/windows/BrowserWindow.h | 17 +++- 6 files changed, 215 insertions(+), 49 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ad23101..6aa5c51 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,7 +54,8 @@ int main(int argc, char *argv[]) aboutData.setCopyrightStatement(i18n("Copyright © 2016-2017,2021-2022 Soren Stoutner ")); aboutData.setDesktopFileName(QStringLiteral("com.stoutner.privacybrowser")); aboutData.setHomepage(QStringLiteral("https://www.stoutner.com/privacy-browser-pc/")); - aboutData.setLicenseTextFile(":/licenses/GPLv3+.txt"); + //aboutData.setLicense(KAboutLicense::GPL_V3, KAboutLicense::OrLaterVersions); + aboutData.setLicenseTextFile(QStringLiteral(":/licenses/GPLv3+.txt")); aboutData.setOrganizationDomain("stoutner.com"); aboutData.setShortDescription(i18n("A web browser that respects your privacy.")); diff --git a/src/ui.rc/browser_ui.rc b/src/ui.rc/browser_ui.rc index 3b086cf..ec7729e 100644 --- a/src/ui.rc/browser_ui.rc +++ b/src/ui.rc/browser_ui.rc @@ -19,6 +19,7 @@ along with Privacy Browser PC. If not, see . --> + + On-The-Fly Settings User Agent @@ -55,7 +57,17 @@ - - Main Toolbar + + + + + Navigation Toolbar + + + + + + + URL Toolbar diff --git a/src/views/BrowserView.cpp b/src/views/BrowserView.cpp index c6a984c..07c1130 100644 --- a/src/views/BrowserView.cpp +++ b/src/views/BrowserView.cpp @@ -57,27 +57,27 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent) webEngineProfilePointer = webEnginePagePointer->profile(); webEngineSettingsPointer = webEngineViewPointer->settings(); - // Update the webengine view from the URL line edit. - connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString))); + // Update the webengine view from the URL line edit. TODO. Remove. + connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString))); // Update the URL line edit from the webengine view. connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface())); connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(updateInterface())); connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(updateInterface())); - // Setup the URL bar buttons. + // Setup the URL bar buttons. TODO. Remove. connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back())); connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward())); connect(refreshButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(reload())); - connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome())); + connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(home())); connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript())); connect(domainSettingsButtonPointer, SIGNAL(clicked()), this, SLOT(openDomainSettings())); - // Get the URL line edit palettes. + // Get the URL line edit palettes. TODO. Remove. noDomainSettingsPalette = urlLineEditPointer->palette(); domainSettingsPalette = urlLineEditPointer->palette(); - // Modify the domain settings palette. + // Modify the domain settings palette. TODO. Remove. domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9")); // Instantiate the mouse event pointer. @@ -177,7 +177,8 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); // Apply the domain settings palette to the URL line edit. - urlLineEditPointer->setPalette(domainSettingsPalette); + urlLineEditPointer->setPalette(domainSettingsPalette); // TODO. Remove. + emit updateDomainSettingsIndicator(true); } else // The hostname does not have domain settings. { @@ -191,7 +192,8 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); // Apply the no domain settings palette to the URL line edit. - urlLineEditPointer->setPalette(noDomainSettingsPalette); + urlLineEditPointer->setPalette(noDomainSettingsPalette); // TODO. Remove. + emit updateDomainSettingsIndicator(false); } // Update the JavaScript button. @@ -248,10 +250,22 @@ void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor) const webEngineViewPointer->setZoomFactor(zoomFactor); } -void BrowserView::goHome() const +void BrowserView::back() const +{ + // Go back. + webEngineViewPointer->back(); +} + +void BrowserView::forward() const +{ + // Go forward. + webEngineViewPointer->forward(); +} + +void BrowserView::home() const { // Load the homepage. - webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage())); + webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage())); } void BrowserView::loadInitialWebsite() @@ -266,37 +280,37 @@ void BrowserView::loadInitialWebsite() if (argumentsStringList.size() > 1) { // Load the URL from the arguments list. - webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1))); + webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1))); } else { // Load the homepage. - goHome(); + home(); } } -void BrowserView::loadUrlFromTextBox(QString urlFromUser) const +void BrowserView::loadUrlFromLineEdit(QString url) const { - // Remove the focus from the URL line edit. + // Remove the focus from the URL line edit. TODO. Remove. urlLineEditPointer->clearFocus(); // Decide if the text is more likely to be a URL or a search. - if (urlFromUser.contains(".")) // The text is likely a URL. + if (url.contains(".")) // The text is likely a URL. { // Check if the URL does not start with a valid protocol. - if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://")) + if (!url.startsWith("http") && !url.startsWith("file://")) { // Add `https://` to the beginning of the URL. - urlFromUser = "https://" + urlFromUser; + url = "https://" + url; } // Load the URL. - webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser)); + webEngineViewPointer->load(QUrl::fromUserInput(url)); } else // The text is likely a search. { // Load the search. - webEngineViewPointer->setUrl(QUrl::fromUserInput(searchEngineUrl + urlFromUser)); + webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url)); } } @@ -324,6 +338,12 @@ void BrowserView::pageLinkHovered(const QString &linkUrl) const emit linkHovered(linkUrl); } +void BrowserView::refresh() const +{ + // Reload the website. + webEngineViewPointer->reload(); +} + void BrowserView::toggleJavaScript() const { // Toggle JavaScript. @@ -345,16 +365,21 @@ void BrowserView::toggleJavaScript() const void BrowserView::updateInterface() const { - // Update the URL line edit if it does not have focus. + // Update the URL line edit if it does not have focus. TODO. Remove block. if (!urlLineEditPointer->hasFocus()) { // Update the URL line edit. urlLineEditPointer->setText(webEngineViewPointer->url().toString()); } + // Update the URL line edit. + emit updateUrlLineEdit(webEngineViewPointer->url().toString()); + // Update the status of the forward and back buttons. - backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack()); - forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward()); + backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack()); // TODO Remove. + emit updateBackAction(webEngineHistoryPointer->canGoBack()); + forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward()); // TODO Remove. + emit updateForwardAction(webEngineHistoryPointer->canGoForward()); // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. Hopefully it will be fixed in Qt6. webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); diff --git a/src/views/BrowserView.h b/src/views/BrowserView.h index 2685216..27c4439 100644 --- a/src/views/BrowserView.h +++ b/src/views/BrowserView.h @@ -47,6 +47,10 @@ signals: void linkHovered(const QString &linkUrl) const; void userAgentUpdated(const QString &userAgent) const; void searchEngineUpdated(const QString &searchEngine) const; + void updateBackAction(const bool &isEnabled) const; + void updateDomainSettingsIndicator(const bool status) const; + void updateForwardAction(const bool &isEnabled) const; + void updateUrlLineEdit(const QString &newUrl) const; void zoomFactorUpdated(const double &zoomFactor) const; public Q_SLOTS: @@ -56,11 +60,14 @@ public Q_SLOTS: void applyDomainSettingsWithoutReloading(const QString &hostname) const; void applyOnTheFlySearchEngine(QAction *searchEngineActionPointer); void applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const; + void back() const; + void forward() const; + void home() const; + void loadUrlFromLineEdit(QString url) const; + void refresh() const; private Q_SLOTS: // The private slots. - void goHome() const; - void loadUrlFromTextBox(QString urlFromUser) const; void pageLinkHovered(const QString &linkUrl) const; void toggleJavaScript() const; void openDomainSettings() const; @@ -69,10 +76,10 @@ private Q_SLOTS: private: // The private variables. QPushButton *backButtonPointer; - QPalette domainSettingsPalette; + QPalette domainSettingsPalette; // TODO. Remove. QPushButton *forwardButtonPointer; QPushButton *javaScriptButtonPointer; - QPalette noDomainSettingsPalette; + QPalette noDomainSettingsPalette; // TODO. Remove. QString searchEngineUrl; KLineEdit *urlLineEditPointer; QWebEngineHistory *webEngineHistoryPointer; diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index 3d6eb61..5a84d40 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -27,6 +27,7 @@ // KDE Frameworks headers. #include +#include // Qt toolkit headers. #include @@ -65,6 +66,10 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing")); searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo")); searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom")); + backActionPointer = actionCollectionPointer->addAction(QStringLiteral("back")); + forwardActionPointer = actionCollectionPointer->addAction(QStringLiteral("forward")); + refreshActionPointer = actionCollectionPointer->addAction(QStringLiteral("refresh")); + homeActionPointer = actionCollectionPointer->addAction(QStringLiteral("home")); // Create the action groups QActionGroup *userAgentActionGroupPointer = new QActionGroup(this); @@ -112,12 +117,22 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED); userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED); userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED); - searchEngineMojeekActionPointer->setText(i18nc("@action", "Mojeek")); - searchEngineMonoclesActionPointer->setText(i18nc("@action", "Monocles")); - searchEngineMetagerActionPointer->setText(i18nc("@action", "MetaGer")); - searchEngineGoogleActionPointer->setText(i18nc("@action", "Google")); - searchEngineBingActionPointer->setText(i18nc("@action", "Bing")); - searchEngineYahooActionPointer->setText(i18nc("@action", "Yahoo")); + searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek")); + searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles")); + searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer")); + searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google")); + searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing")); + searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo")); + backActionPointer->setText(i18nc("Back button", "Back")); + forwardActionPointer->setText(i18nc("Forward button", "Forward")); + refreshActionPointer->setText(i18nc("Refresh button", "Refresh")); + homeActionPointer->setText(i18nc("Home button", "Home")); + + // Set the action icons. + backActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left"))); + forwardActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right"))); + refreshActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh"))); + homeActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("home"))); // Update the on-the-fly menus. connect(browserViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString))); @@ -131,22 +146,78 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Display dialogs. connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser())); - // Update the status bar with the URL when a link is hovered. - connect(browserViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString))); + // Connect the URL toolbar actions. + connect(backActionPointer, SIGNAL(triggered()), this, SLOT(back())); + connect(forwardActionPointer, SIGNAL(triggered()), this, SLOT(forward())); + connect(refreshActionPointer, SIGNAL(triggered()), this, SLOT(refresh())); + connect(homeActionPointer, SIGNAL(triggered()), this, SLOT(home())); + + // Update the URL toolbar actions. + connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool))); + connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool))); // Setup the GUI based on the browser_ui.rc file. setupGUI(StandardWindowOption::Default, ("browser_ui.rc")); - // Load the initial webstie. + // Get a handle for the URL toolbar. + KToolBar *urlToolBarPointer = toolBar(QStringLiteral("url_toolbar")); + + // Create a URL line edit. + urlLineEditPointer = new KLineEdit(); + + // Add the URL line edit to the URL toolbar. + urlToolBarPointer->addWidget(urlLineEditPointer); + + // Load a new URL from the URL line edit. + connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString))); + + // Update the URL line edit on page loads. + connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QString)), this, SLOT(updateUrlLineEdit(QString))); + + // Get a handle for the status bar. + QStatusBar *statusBarPointer = statusBar(); + + // Update the status bar with the URL when a link is hovered. + connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString))); + + // Get the URL line edit palettes. + noDomainSettingsPalette = urlLineEditPointer->palette(); + domainSettingsPalette = urlLineEditPointer->palette(); + + // Modify the domain settings palette. + domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9")); + + // Update the applied palette. + connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool)), this, SLOT(updateDomainSettingsIndicator(bool))); + + // Load the initial website. browserViewPointer->loadInitialWebsite(); } +void BrowserWindow::back() const +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Go back. + browserViewPointer->back(); +} + void BrowserWindow::fileNew() const { // Display a new instance of Privacy Browser. (new BrowserWindow)->show(); } +void BrowserWindow::forward() const +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Go forward. + browserViewPointer->forward(); +} + void BrowserWindow::getZoomFactorFromUser() { // Create an OK flag. @@ -171,6 +242,33 @@ void BrowserWindow::getZoomFactorFromUser() } } +void BrowserWindow::home() const +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Go home. + browserViewPointer->home(); +} + +void BrowserWindow::loadUrlFromLineEdit(const QString &url) const +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Load the URL. + browserViewPointer->loadUrlFromLineEdit(url); +} + +void BrowserWindow::refresh() const +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Refresh the web page. + browserViewPointer->refresh(); +} + void BrowserWindow::settingsConfigure() { // Check to make sure the dialog box isn't already displayed. @@ -231,11 +329,11 @@ void BrowserWindow::settingsConfigure() // Make it so. configDialogPointer->show(); - // TODO. KConfigDialog does not respect expanding size policies. - configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - configDialogPointer->adjustSize(); + // TODO. KConfigDialog does not respect expanding size policies. + //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + //configDialogPointer->adjustSize(); // Expand the config dialog. configDialogPointer->resize(1000, 500); @@ -246,6 +344,13 @@ void BrowserWindow::settingsConfigure() } } +void BrowserWindow::updateDomainSettingsIndicator(const bool status) const +{ + // Set the domain palette according to the status. + if (status) urlLineEditPointer->setPalette(domainSettingsPalette); + else urlLineEditPointer->setPalette(noDomainSettingsPalette); +} + void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const { // Initialize the custom search engine flag. @@ -284,7 +389,6 @@ void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) cons customSearchEngine = true; } - // Format the custom search engine. if (customSearchEngine) { @@ -361,10 +465,14 @@ void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) c searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString)); } -void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const +void BrowserWindow::updateUrlLineEdit(const QString &newUrl) const { - // Display the status bar message. - statusBar()->showMessage(statusBarMessage); + // Update the URL line edit if it does not have focus. + if (!urlLineEditPointer->hasFocus()) + { + // Update the URL line edit. + urlLineEditPointer->setText(newUrl); + } } void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const diff --git a/src/windows/BrowserWindow.h b/src/windows/BrowserWindow.h index f4dc071..f86c1b2 100644 --- a/src/windows/BrowserWindow.h +++ b/src/windows/BrowserWindow.h @@ -41,20 +41,32 @@ public: private Q_SLOTS: // The private slots. + void back() const; void fileNew() const; + void forward() const; void getZoomFactorFromUser(); + void home() const; + void loadUrlFromLineEdit(const QString &url) const; + void refresh() const; void settingsConfigure(); + void updateDomainSettingsIndicator(const bool status) const; void updateOnTheFlySearchEngine(const QString &searchEngine) const; void updateOnTheFlyUserAgent(const QString &userAgent) const; void updateOnTheFlyZoomFactor(const double &zoomFactor); void updateSearchEngineLabel(const QString &searchEngineString) const; - void updateStatusBar(const QString &statusBarMessage) const; + void updateUrlLineEdit(const QString &newUrl) const; void updateUserAgentLabel(const QString &userAgentDatabaseName) const; private: // The private variables. + QAction *backActionPointer; BrowserView *browserViewPointer; + KConfigDialog *configDialogPointer; + QPalette domainSettingsPalette; double currentZoomFactor; + QAction *forwardActionPointer; + QAction *homeActionPointer; + QPalette noDomainSettingsPalette; QLabel *searchEngineLabelPointer; QAction *searchEngineMojeekActionPointer; QAction *searchEngineMonoclesActionPointer; @@ -63,6 +75,7 @@ private: QAction *searchEngineBingActionPointer; QAction *searchEngineYahooActionPointer; QAction *searchEngineCustomActionPointer; + QAction *refreshActionPointer; QLabel *userAgentLabelPointer; QAction *userAgentPrivacyBrowserActionPointer; QAction *userAgentFirefoxLinuxActionPointer; @@ -72,7 +85,7 @@ private: QAction *userAgentEdgeWindowsActionPointer; QAction *userAgentSafariMacosActionPointer; QAction *userAgentCustomActionPointer; + KLineEdit *urlLineEditPointer; QAction *zoomFactorActionPointer; - KConfigDialog *configDialogPointer; }; #endif -- 2.43.0