]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/windows/BrowserWindow.cpp
Add User Agent to Domain Settings.
[PrivacyBrowserPC.git] / src / windows / BrowserWindow.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
6  * Privacy Browser PC is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser PC is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 // Application headers.
21 #include "BrowserWindow.h"
22 #include "Settings.h"
23 #include "ui_SettingsPrivacy.h"
24 #include "ui_SettingsGeneral.h"
25 #include "helpers/SearchEngineHelper.h"
26 #include "helpers/UserAgentHelper.h"
27
28 // KDE Frameworks headers.
29 #include <KActionCollection>
30
31 // Qt toolkit headers.
32 #include <QInputDialog>
33 #include <QStatusBar>
34
35 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
36 {
37     // Instantiate the main view pointer.
38     browserViewPointer = new BrowserView(this);
39
40     // Set the main view as the central widget.
41     setCentralWidget(browserViewPointer);
42
43     // Get a handle for the action collection.
44     KActionCollection *actionCollectionPointer = this->actionCollection();
45
46     // Add the standard actions.
47     KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
48     KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
49     KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
50
51     // Add the custom actions.
52     userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
53     userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
54     userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
55     userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
56     userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
57     userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
58     userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
59     userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
60     zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
61     searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
62     searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
63     searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
64     searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
65     searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
66     searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
67     searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
68
69     // Create the action groups
70     QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
71     QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
72
73     // Add the actions to the groups.
74     userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
75     userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
76     userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
77     userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
78     userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
79     userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
80     userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
81     userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
82     searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
83     searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
84     searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
85     searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
86     searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
87     searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
88     searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
89
90     // Set some actions to be checkable.
91     userAgentPrivacyBrowserActionPointer->setCheckable(true);
92     userAgentFirefoxLinuxActionPointer->setCheckable(true);
93     userAgentChromiumLinuxActionPointer->setCheckable(true);
94     userAgentFirefoxWindowsActionPointer->setCheckable(true);
95     userAgentChromeWindowsActionPointer->setCheckable(true);
96     userAgentEdgeWindowsActionPointer->setCheckable(true);
97     userAgentSafariMacosActionPointer->setCheckable(true);
98     userAgentCustomActionPointer->setCheckable(true);
99     searchEngineMojeekActionPointer->setCheckable(true);
100     searchEngineMonoclesActionPointer->setCheckable(true);
101     searchEngineMetagerActionPointer->setCheckable(true);
102     searchEngineGoogleActionPointer->setCheckable(true);
103     searchEngineBingActionPointer->setCheckable(true);
104     searchEngineYahooActionPointer->setCheckable(true);
105     searchEngineCustomActionPointer->setCheckable(true);
106
107     // Set the non-mutable action text.
108     userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
109     userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
110     userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
111     userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
112     userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
113     userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
114     userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
115     searchEngineMojeekActionPointer->setText(i18nc("@action", "Mojeek"));
116     searchEngineMonoclesActionPointer->setText(i18nc("@action", "Monocles"));
117     searchEngineMetagerActionPointer->setText(i18nc("@action", "MetaGer"));
118     searchEngineGoogleActionPointer->setText(i18nc("@action", "Google"));
119     searchEngineBingActionPointer->setText(i18nc("@action", "Bing"));
120     searchEngineYahooActionPointer->setText(i18nc("@action", "Yahoo"));
121
122     // Update the on-the-fly menus.
123     connect(browserViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString)));
124     connect(browserViewPointer, SIGNAL(zoomFactorUpdated(double)), this, SLOT(updateOnTheFlyZoomFactor(double)));
125     connect(browserViewPointer, SIGNAL(searchEngineUpdated(QString)), this, SLOT(updateOnTheFlySearchEngine(QString)));
126
127     // Apply the on-the-fly settings when selected.
128     connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
129     connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
130
131     // Display dialogs.
132     connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
133
134     // Update the status bar with the URL when a link is hovered.
135     connect(browserViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString)));
136
137     // Setup the GUI based on the browser_ui.rc file.
138     setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
139
140     // Load the initial webstie.
141     browserViewPointer->loadInitialWebsite();
142 }
143
144 void BrowserWindow::fileNew() const
145 {
146     // Display a new instance of Privacy Browser.
147     (new BrowserWindow)->show();
148 }
149
150 void BrowserWindow::getZoomFactorFromUser()
151 {
152     // Create an OK flag.
153     bool okClicked;
154
155     // Display a dialog to get the new zoom factor from the user.  Format the double to display two decimals and have a 0.25 step.
156     double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
157                                                    i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
158                                                    currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
159
160     // Update the zoom factor if the user clicked OK.
161     if (okClicked)
162     {
163         // Update the current zoom factor.
164         currentZoomFactor = newZoomFactor;
165
166         // Set the new zoom factor.
167         browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
168
169         // Update the on-the-fly action text.
170         updateOnTheFlyZoomFactor(newZoomFactor);
171     }
172 }
173
174 void BrowserWindow::settingsConfigure()
175 {
176     // Check to make sure the dialog box isn't already displayed.
177     if (KConfigDialog::exists(QStringLiteral("settings")))
178     {
179         // Show the existing config dialog if it is hidden.
180         configDialogPointer->show();
181
182         // Raise the existing config dialog if it is below other windows.
183         configDialogPointer->raise();
184
185         // Restore the existing config dialog if it has been minimized.
186         if (configDialogPointer->isMinimized()) {
187             configDialogPointer->showNormal();
188         }
189
190         // Activate the existing config dialog, which brings its virtual desktop into focus.
191         configDialogPointer->activateWindow();
192     }
193     else
194     {
195         // Create the settings widgets.
196         QWidget *privacySettingsWidgetPointer = new QWidget;
197         QWidget *generalSettingsWidgetPointer = new QWidget;
198
199         // Instantiate the settings UI.
200         Ui::PrivacySettings privacySettingsUi;
201         Ui::GeneralSettings generalSettingsUi;
202
203         // Setup the UI to display the settings widgets.
204         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
205         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
206
207         // Get handles for the widgets.
208         QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
209         userAgentLabelPointer = privacySettingsUi.userAgentLabel;
210         QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
211         searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
212
213         // Populate the combo box labels.
214         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
215         updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
216
217         // Update the labels when the combo boxes change.
218         connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
219         connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
220
221         // Instantiate a settings config dialog from the settings.kcfg file.
222         configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
223
224         // Add the settings widgets as config dialog pages.
225         configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
226         configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
227
228         // Delete the config dialog when it is closed.
229         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
230
231         // Make it so.
232         configDialogPointer->show();
233
234         // TODO.  KConfigDialog does not respect expanding size policies.
235         configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
236         privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
237         generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
238         configDialogPointer->adjustSize();
239
240         // Expand the config dialog.
241         configDialogPointer->resize(1000, 500);
242
243         // Apply the settings when they are updated.
244         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
245         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
246     }
247 }
248
249 void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const
250 {
251     // Initialize the custom search engine flag.
252     bool customSearchEngine = false;
253
254     if (searchEngine == "Mojeek")  // Mojeek.
255     {
256         searchEngineMojeekActionPointer->setChecked(true);
257     }
258     else if (searchEngine == "Monocles")  // Monocles.
259     {
260         searchEngineMonoclesActionPointer->setChecked(true);
261     }
262     else if (searchEngine == "MetaGer")  // MetaGer.
263     {
264         searchEngineMetagerActionPointer->setChecked(true);
265     }
266     else if (searchEngine == "Google")  // Google.
267     {
268         searchEngineGoogleActionPointer->setChecked(true);
269     }
270     else if (searchEngine == "Bing")  // Bing.
271     {
272         searchEngineBingActionPointer->setChecked(true);
273     }
274     else if (searchEngine == "Yahoo")  // Yahoo.
275     {
276         searchEngineYahooActionPointer->setChecked(true);
277     }
278     else  // Custom search engine.
279     {
280         // Check the user agent.
281         searchEngineCustomActionPointer->setChecked(true);
282
283         // Set the custom search engine flag.
284         customSearchEngine = true;
285     }
286
287
288     // Format the custom search engine.
289     if (customSearchEngine)
290     {
291         // Enable the custom search engine.
292         searchEngineCustomActionPointer->setEnabled(true);
293
294         // Set the custom search engine text.
295         searchEngineCustomActionPointer->setText(searchEngine);
296     }
297     else
298     {
299         // Disable the custom search engine.
300         searchEngineCustomActionPointer->setEnabled(false);
301
302         // Reset the custom search engine text.
303         searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
304     }
305 }
306
307 void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
308 {
309     // Initialize the custom user agent flag.
310     bool customUserAgent = false;
311
312     // Check the indicated on-the-fly user agent.
313     if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) userAgentPrivacyBrowserActionPointer->setChecked(true);  // Privacy Browser.
314     else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) userAgentFirefoxLinuxActionPointer->setChecked(true);  // Firefox Linux.
315     else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) userAgentChromiumLinuxActionPointer->setChecked(true);  // Chromium Linux.
316     else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) userAgentFirefoxWindowsActionPointer->setChecked(true);  // Firefox Windows.
317     else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) userAgentChromeWindowsActionPointer->setChecked(true);  // Chrome Windows.
318     else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) userAgentEdgeWindowsActionPointer->setChecked(true);  // Edge Windows.
319     else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) userAgentSafariMacosActionPointer->setChecked(true);  // Safara macOS.
320     else  // Custom user agent.
321     {
322         // Check the user agent.
323         userAgentCustomActionPointer->setChecked(true);
324
325         // Set the custom user agent flag.
326         customUserAgent = true;
327     }
328
329
330     // Format the custom user agent.
331     if (customUserAgent)
332     {
333         // Enable the custom user agent.
334         userAgentCustomActionPointer->setEnabled(true);
335
336         // Set the custom user agent text.
337         userAgentCustomActionPointer->setText(userAgent);
338     }
339     else
340     {
341         // Disable the custom user agent.
342         userAgentCustomActionPointer->setEnabled(false);
343
344         // Reset the custom user agent text.
345         userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
346     }
347 }
348
349 void BrowserWindow::updateOnTheFlyZoomFactor(const double &zoomFactor)
350 {
351     // Set the current zoom factor.
352     currentZoomFactor = Settings::zoomFactor();
353
354     // Update the zoom factor action text, formatting the double with 2 decimal places.
355     zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
356 }
357
358 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
359 {
360     // Update the search engine label.
361     searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
362 }
363
364 void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const
365 {
366     // Display the status bar message.
367     statusBar()->showMessage(statusBarMessage);
368 }
369
370 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
371 {
372     // Update the user agent label.
373     userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));
374 }