#include "Settings.h"
#include "ui_SettingsPrivacy.h"
#include "ui_SettingsGeneral.h"
+#include "UserAgentHelper.h"
// KDE Frameworks headers.
#include <KActionCollection>
setupGUI();
}
-void BrowserWindow::fileNew()
+void BrowserWindow::fileNew() const
{
// Display a new instance of Privacy Browser.
(new BrowserWindow)->show();
QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
userAgentLabelPointer = privacySettingsUi.userAgentLabel;
- // Instantiate the user agent helper.
- userAgentHelperPointer = new UserAgentHelper();
-
// Display the initial user agent.
updateUserAgentLabel(userAgentComboBoxPointer->currentText());
}
}
-void BrowserWindow::updateStatusBar(const QString &statusBarMessage)
+void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const
{
// Display the status bar message.
statusBar()->showMessage(statusBarMessage);
}
-void BrowserWindow::updateUserAgentLabel(const QString &userAgentName)
+void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const
{
// Update the user agent label.
- userAgentLabelPointer->setText(userAgentHelperPointer->getUserAgent(userAgentName));
+ userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName));
}
// Application headers.
#include "MainView.h"
-#include "UserAgentHelper.h"
// Qt framework headers.
#include <QLabel>
private Q_SLOTS:
// Define the private slots.
- void fileNew();
+ void fileNew() const;
void settingsConfigure();
- void updateStatusBar(const QString &statusBarMessage);
- void updateUserAgentLabel(const QString &userAgentName);
+ void updateStatusBar(const QString &statusBarMessage) const;
+ void updateUserAgentLabel(const QString &userAgentName) const;
private:
// Define the private variables.
MainView *mainViewPointer;
QLabel *userAgentLabelPointer;
- UserAgentHelper *userAgentHelperPointer;
};
#endif
#include "MouseEventFilter.h"
#include "Settings.h"
#include "ui_MainView.h"
+#include "UserAgentHelper.h"
// Qt framework headers.
#include <QWebEngineProfile>
webEngineSettingsPointer = webEngineViewPointer->settings();
// Update the webengine view from the URL line edit.
- connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString)));
+ connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString)));
// Update the URL line edit form the webengine view.
connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
// Disable the cache.
webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
- // Instantiate the user agent helper.
- userAgentHelperPointer = new UserAgentHelper();
-
// Don't allow JavaScript to open windows.
webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
}
}
-void MainView::applyApplicationSettings()
+void MainView::applyApplicationSettings() const
{
// TODO.
}
-void MainView::applyDomainSettingsAndReload()
+// This exists as a separate function from `applyDomainSettings()` so it can be listed as a slot and function without the need for a boolean argument.
+void MainView::applyDomainSettingsAndReload() const
{
// Apply the domain setings. `true` reloads the website.
applyDomainSettings(true);
}
-void MainView::applyDomainSettings(bool reloadWebsite)
+void MainView::applyDomainSettings(bool reloadWebsite) const
{
// Set the JavaScript status.
webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
}
// Apply the user agent.
- webEngineProfilePointer->setHttpUserAgent(userAgentHelperPointer->getUserAgent(Settings::userAgent()));
+ webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
// Reload the website if requested.
if (reloadWebsite)
}
}
-void MainView::goHome()
+void MainView::goHome() const
{
- // Load the homepage. TODO. Consider sanitizing the homepage input and adding things like protocols if they are missing.
- webEngineViewPointer->setUrl(Settings::homepage());
+ // Load the homepage.
+ webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage().toString()));
}
-void MainView::loadUrl(const QString &urlFromUser)
+void MainView::loadUrlFromTextBox(QString urlFromUser) const
{
// Remove the focus from the URL line edit.
urlLineEditPointer->clearFocus();
- // Load the URL, adding standard protocol sections if needed. TODO. Replace this with logic that prefers HTTPS.
+ // Check if the URL does not start with a valid protocol.
+ if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://"))
+ {
+ // Add `https://` to the beginning of the URL.
+ urlFromUser = "https://" + urlFromUser;
+ }
+
+ // Load the URL.
webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
}
-void MainView::pageLinkHovered(const QString &linkUrl)
+void MainView::pageLinkHovered(const QString &linkUrl) const
{
// Emit a signal so that the browser window can update the status bar.
emit linkHovered(linkUrl);
}
-void MainView::toggleJavaScript()
+void MainView::toggleJavaScript() const
{
// Toggle JavaScript.
webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
webEngineViewPointer->reload();
}
-void MainView::updateInterface()
+void MainView::updateInterface() const
{
// Update the URL line edit if it does not have focus.
if (!urlLineEditPointer->hasFocus())
#ifndef MAINVIEW_H
#define MAINVIEW_H
-// Application headers.
-#include "UserAgentHelper.h"
-
// Qt framework headers.
#include <QPushButton>
#include <QWebEngineHistory>
signals:
// Define the signals.
- void linkHovered(const QString &linkUrl);
+ void linkHovered(const QString &linkUrl) const;
public Q_SLOTS:
// Define the public slots.
- void applyApplicationSettings();
- void applyDomainSettingsAndReload();
+ void applyApplicationSettings() const;
+ void applyDomainSettingsAndReload() const;
private Q_SLOTS:
// Define the private slots.
- void goHome();
- void loadUrl(const QString &urlFromUser);
- void pageLinkHovered(const QString &linkUrl);
- void toggleJavaScript();
- void updateInterface();
+ void goHome() const;
+ void loadUrlFromTextBox(QString urlFromUser) const;
+ void pageLinkHovered(const QString &linkUrl) const;
+ void toggleJavaScript() const;
+ void updateInterface() const;
private:
// Define the private variables.
QWebEngineProfile *webEngineProfilePointer;
QWebEngineSettings *webEngineSettingsPointer;
QWebEngineView *webEngineViewPointer;
- UserAgentHelper *userAgentHelperPointer;
// Define the private functions.
- void applyDomainSettings(bool reloadWebsite);
+ void applyDomainSettings(bool reloadWebsite) const;
};
#endif
QString UserAgentHelper::getUserAgent(const QString &userAgentName)
{
- if (userAgentName == "Privacy Browser")
+ if (userAgentName == "Privacy Browser") // Privacy Browser.
{
return "PrivacyBrowser/1.0";
}
- else if (userAgentName == "Firefox Linux")
+ else if (userAgentName == "Firefox Linux") // Firefox Linux.
{
return "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0";
}
- else if (userAgentName == "Chromium Linux")
+ else if (userAgentName == "Chromium Linux") // Chromium Linux.
{
return "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
}
- else if (userAgentName == "Firefox Windows")
+ else if (userAgentName == "Firefox Windows") // Firefox Windows.
{
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0";
}
- else if (userAgentName == "Chrome Windows")
+ else if (userAgentName == "Chrome Windows") // Chrome Windows.
{
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36";
}
- else if (userAgentName == "Edge Windows")
+ else if (userAgentName == "Edge Windows") // Edge Windows.
{
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.34";
}
- else if (userAgentName == "Safari macOS")
+ else if (userAgentName == "Safari macOS") // Safari macOS.
{
return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Safari/605.1.15";
}
UserAgentHelper();
// The public functions.
- QString getUserAgent(const QString &userAgentName);
+ static QString getUserAgent(const QString &userAgentName);
};
#endif