From 68167c0aba46c4bd4c859b176824873ffd14a3e7 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Sat, 12 Feb 2022 16:09:35 -0700 Subject: [PATCH] Add search engine options. --- src/BrowserWindow.cpp | 21 ++++++-- src/BrowserWindow.h | 2 + src/CMakeLists.txt | 4 +- src/MainView.cpp | 34 +++++++----- src/Settings.kcfg | 10 ++-- src/SettingsGeneral.ui | 76 ++++++++++++++++++++++++++- src/SettingsPrivacy.ui | 8 +-- src/helpers/CMakeLists.txt | 23 ++++++++ src/helpers/SearchEngineHelper.cpp | 57 ++++++++++++++++++++ src/helpers/SearchEngineHelper.h | 35 ++++++++++++ src/{ => helpers}/UserAgentHelper.cpp | 0 src/{ => helpers}/UserAgentHelper.h | 0 12 files changed, 243 insertions(+), 27 deletions(-) create mode 100644 src/helpers/CMakeLists.txt create mode 100644 src/helpers/SearchEngineHelper.cpp create mode 100644 src/helpers/SearchEngineHelper.h rename src/{ => helpers}/UserAgentHelper.cpp (100%) rename src/{ => helpers}/UserAgentHelper.h (100%) diff --git a/src/BrowserWindow.cpp b/src/BrowserWindow.cpp index e2f17bd..29fe464 100644 --- a/src/BrowserWindow.cpp +++ b/src/BrowserWindow.cpp @@ -22,7 +22,8 @@ #include "Settings.h" #include "ui_SettingsPrivacy.h" #include "ui_SettingsGeneral.h" -#include "UserAgentHelper.h" +#include "helpers/SearchEngineHelper.h" +#include "helpers/UserAgentHelper.h" // KDE Frameworks headers. #include @@ -77,15 +78,19 @@ void BrowserWindow::settingsConfigure() privacySettingsUi.setupUi(privacySettingsWidgetPointer); generalSettingsUi.setupUi(generalSettingsWidgetPointer); - // Get handles for the user agent widgets. + // Get handles for the widgets. QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent; userAgentLabelPointer = privacySettingsUi.userAgentLabel; + QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine; + searchEngineLabelPointer = generalSettingsUi.searchEngineLabel; - // Display the initial user agent. + // Populate the combo box labels. updateUserAgentLabel(userAgentComboBoxPointer->currentText()); + updateSearchEngineLabel(searchEngineComboBoxPointer->currentText()); - // Update the user agent when the combo box changes. - connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(QString)), this, SLOT(updateUserAgentLabel(QString))); + // Update the labels when the combo boxs change. + connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString))); + connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString))); // Instantiate a settings config dialog from the settings.kcfg file. KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self()); @@ -109,6 +114,12 @@ void BrowserWindow::settingsConfigure() } } +void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const +{ + // Update the search engine label. + searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString)); +} + void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const { // Display the status bar message. diff --git a/src/BrowserWindow.h b/src/BrowserWindow.h index 7539099..8d65827 100644 --- a/src/BrowserWindow.h +++ b/src/BrowserWindow.h @@ -42,12 +42,14 @@ private Q_SLOTS: // Define the private slots. void fileNew() const; void settingsConfigure(); + void updateSearchEngineLabel(const QString &searchEngineString) const; void updateStatusBar(const QString &statusBarMessage) const; void updateUserAgentLabel(const QString &userAgentName) const; private: // Define the private variables. MainView *mainViewPointer; + QLabel *searchEngineLabelPointer; QLabel *userAgentLabelPointer; }; #endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8f184ad..79526bf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,7 +25,6 @@ target_sources(privacy-browser PRIVATE main.cpp MainView.cpp MouseEventFilter.cpp - UserAgentHelper.cpp ) # Add the Qt logging category. This will create the `debug.h` header file. @@ -61,6 +60,9 @@ target_link_libraries(privacy-browser KF5::XmlGui ) +# Add the subdirectories. +add_subdirectory(helpers) + # Install Privacy Browser using the default KDE arguments. install(TARGETS privacy-browser ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/src/MainView.cpp b/src/MainView.cpp index 5acb447..9ddf1fb 100644 --- a/src/MainView.cpp +++ b/src/MainView.cpp @@ -23,7 +23,8 @@ #include "MouseEventFilter.h" #include "Settings.h" #include "ui_MainView.h" -#include "UserAgentHelper.h" +#include "helpers/SearchEngineHelper.h" +#include "helpers/UserAgentHelper.h" // Qt framework headers. #include @@ -56,8 +57,8 @@ MainView::MainView(QWidget *parent) : QWidget(parent) // Update the URL line edit form the webengine view. connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface())); - connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface())); - connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), 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. connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back())); @@ -73,7 +74,7 @@ MainView::MainView(QWidget *parent) : QWidget(parent) qApp->installEventFilter(mouseEventFilterPointer); // Listen for hovered link URLs. - connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString))); + connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString))); // Disable the cache. webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache); @@ -149,7 +150,7 @@ void MainView::applyDomainSettings(bool reloadWebsite) const void MainView::goHome() const { // Load the homepage. - webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage().toString())); + webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage())); } void MainView::loadUrlFromTextBox(QString urlFromUser) const @@ -157,15 +158,24 @@ void MainView::loadUrlFromTextBox(QString urlFromUser) const // Remove the focus from the URL line edit. urlLineEditPointer->clearFocus(); - // Check if the URL does not start with a valid protocol. - if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://")) + // Decide if the text is more likely to be a URL or a search. + if (urlFromUser.contains(".")) // The text is likely a URL. { - // Add `https://` to the beginning of the URL. - urlFromUser = "https://" + urlFromUser; + // 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)); + } + else // The text is likely a search. + { + // Load the search. + webEngineViewPointer->setUrl(QUrl::fromUserInput(SearchEngineHelper::getSearchUrl(Settings::searchEngine()) + urlFromUser)); } - - // Load the URL. - webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser)); } void MainView::pageLinkHovered(const QString &linkUrl) const diff --git a/src/Settings.kcfg b/src/Settings.kcfg index fe9b22d..25b12a8 100644 --- a/src/Settings.kcfg +++ b/src/Settings.kcfg @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with Privacy Browser PC. If not, see . --> - + - PrivacyBrowser/1.0 + Privacy Browser - + https://www.mojeek.com/ + + Mojeek + + 1.00 diff --git a/src/SettingsGeneral.ui b/src/SettingsGeneral.ui index de42696..41b5200 100644 --- a/src/SettingsGeneral.ui +++ b/src/SettingsGeneral.ui @@ -41,8 +41,80 @@ - + + + + Search engine + + + + + + + + + 0 + 500 + + + + + The default is Mojeek. + + + + true + + + + + Mojeek + + + + + + Monocles + + + + + + MetaGer + + + + + + Google + + + + + + Bing + + + + + + Yahoo + + + + + + + + + Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse + + + + + + Zoom factor @@ -50,7 +122,7 @@ - + Set the zoom factor between 0.25 and 5.00. The default is 1.00. diff --git a/src/SettingsPrivacy.ui b/src/SettingsPrivacy.ui index 264ac2d..f800220 100644 --- a/src/SettingsPrivacy.ui +++ b/src/SettingsPrivacy.ui @@ -37,11 +37,11 @@ - + - UserAgent + User agent @@ -50,13 +50,13 @@ - 500 0 + 500 - The default is Privacy Browser + The default is Privacy Browser. diff --git a/src/helpers/CMakeLists.txt b/src/helpers/CMakeLists.txt new file mode 100644 index 0000000..042c5c3 --- /dev/null +++ b/src/helpers/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright © 2022 Soren Stoutner . +# +# This file is part of Privacy Browser PC . +# +# Privacy Browser PC is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Privacy Browser PC is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Privacy Browser PC. If not, see . + + +# List the sources to include in the executable. +target_sources(privacy-browser PRIVATE + SearchEngineHelper.cpp + UserAgentHelper.cpp +) diff --git a/src/helpers/SearchEngineHelper.cpp b/src/helpers/SearchEngineHelper.cpp new file mode 100644 index 0000000..8291926 --- /dev/null +++ b/src/helpers/SearchEngineHelper.cpp @@ -0,0 +1,57 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of Privacy Browser PC . + * + * Privacy Browser PC is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser PC is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser PC. If not, see . + */ + +// Application headers. +#include "SearchEngineHelper.h" + +// The default constructor. +SearchEngineHelper::SearchEngineHelper() {}; + +QString SearchEngineHelper::getSearchUrl(const QString &searchEngineName) +{ + if (searchEngineName == "Mojeek") // Mojeek. + { + return "https://www.mojeek.com/search?q="; + } + else if (searchEngineName == "Monocles") // Monocles. + { + return "https://monocles.de/search?q="; + } + else if (searchEngineName == "MetaGer") // MetaGer. + { + return "https://metager.org/meta/meta.ger3?eingabe="; + } + else if (searchEngineName == "Google") // Google. + { + return "https://www.google.com/search?q="; + } + else if (searchEngineName == "Bing") // Bing. + { + return "https://www.bing.com/search?q="; + } + else if (searchEngineName == "Yahoo") // Yahoo. + { + return "https://search.yahoo.com/search?p="; + } + else + { + // Return the custom user agent. + return searchEngineName; + } +} diff --git a/src/helpers/SearchEngineHelper.h b/src/helpers/SearchEngineHelper.h new file mode 100644 index 0000000..7eb2b2c --- /dev/null +++ b/src/helpers/SearchEngineHelper.h @@ -0,0 +1,35 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of Privacy Browser PC . + * + * Privacy Browser PC is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser PC is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser PC. If not, see . + */ + +#ifndef SEARCHENGINEHELPER_H +#define SEARCHENGINEHELPER_H + +// Qt framework headers. +#include + +class SearchEngineHelper +{ +public: + // The default constructor. + SearchEngineHelper(); + + // The public functions. + static QString getSearchUrl(const QString &searchEngineName); +}; +#endif diff --git a/src/UserAgentHelper.cpp b/src/helpers/UserAgentHelper.cpp similarity index 100% rename from src/UserAgentHelper.cpp rename to src/helpers/UserAgentHelper.cpp diff --git a/src/UserAgentHelper.h b/src/helpers/UserAgentHelper.h similarity index 100% rename from src/UserAgentHelper.h rename to src/helpers/UserAgentHelper.h -- 2.45.2