]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/BrowserWindow.cpp
7e908a34dff6a876eba5e340c8f82c69ba2191c0
[PrivacyBrowserPC.git] / src / 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 #include <KConfigDialog>
31
32 // Qt framework headers.
33 #include <QInputDialog>
34 #include <QStatusBar>
35
36 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
37 {
38     // Instantiate the main view pointer.
39     mainViewPointer = new MainView(this);
40
41     // Set the main view as the central widget.
42     setCentralWidget(mainViewPointer);
43
44     // Get a handle for the action collectoin.
45     KActionCollection *actionCollectionPointer = this->actionCollection();
46
47     // Add the standard actions.
48     KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
49     KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
50     KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
51
52     // Add the custom actions.
53     userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
54     userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
55     userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
56     userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
57     userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
58     userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
59     userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
60     userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
61     zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
62     searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
63     searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
64     searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
65     searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
66     searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
67     searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
68     searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
69
70     // Create the action groups
71     QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
72     QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
73
74     // Add the actions to the groups.
75     userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
76     userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
77     userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
78     userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
79     userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
80     userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
81     userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
82     userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
83     searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
84     searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
85     searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
86     searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
87     searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
88     searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
89     searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
90
91     // Set some actions to be checkable.
92     userAgentPrivacyBrowserActionPointer->setCheckable(true);
93     userAgentFirefoxLinuxActionPointer->setCheckable(true);
94     userAgentChromiumLinuxActionPointer->setCheckable(true);
95     userAgentFirefoxWindowsActionPointer->setCheckable(true);
96     userAgentChromeWindowsActionPointer->setCheckable(true);
97     userAgentEdgeWindowsActionPointer->setCheckable(true);
98     userAgentSafariMacosActionPointer->setCheckable(true);
99     userAgentCustomActionPointer->setCheckable(true);
100     searchEngineMojeekActionPointer->setCheckable(true);
101     searchEngineMonoclesActionPointer->setCheckable(true);
102     searchEngineMetagerActionPointer->setCheckable(true);
103     searchEngineGoogleActionPointer->setCheckable(true);
104     searchEngineBingActionPointer->setCheckable(true);
105     searchEngineYahooActionPointer->setCheckable(true);
106     searchEngineCustomActionPointer->setCheckable(true);
107
108     // Set the non-mutable action names.
109     userAgentPrivacyBrowserActionPointer->setText(i18nc("@action", "Privacy Browser"));
110     userAgentFirefoxLinuxActionPointer->setText(i18nc("@action", "Firefox Linux"));
111     userAgentChromiumLinuxActionPointer->setText(i18nc("@action", "Chromium Linux"));
112     userAgentFirefoxWindowsActionPointer->setText(i18nc("@action", "Firefox Windows"));
113     userAgentChromeWindowsActionPointer->setText(i18nc("@action", "Chrome Windows"));
114     userAgentEdgeWindowsActionPointer->setText(i18nc("@action", "Edge Windows"));
115     userAgentSafariMacosActionPointer->setText(i18nc("@action", "Safari macOS"));
116     searchEngineMojeekActionPointer->setText(i18nc("@action", "Mojeek"));
117     searchEngineMonoclesActionPointer->setText(i18nc("@action", "Monocles"));
118     searchEngineMetagerActionPointer->setText(i18nc("@action", "MetaGer"));
119     searchEngineGoogleActionPointer->setText(i18nc("@action", "Google"));
120     searchEngineBingActionPointer->setText(i18nc("@action", "Bing"));
121     searchEngineYahooActionPointer->setText(i18nc("@action", "Yahoo"));
122
123     // Update the on-the-fly menus.
124     connect(mainViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString)));
125     connect(mainViewPointer, SIGNAL(zoomFactorUpdated(double)), this, SLOT(updateOnTheFlyZoomFactor(double)));
126     connect(mainViewPointer, SIGNAL(searchEngineUpdated(QString)), this, SLOT(updateOnTheFlySearchEngine(QString)));
127
128     // Apply the on-the-fly settings when selected.
129     connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
130     connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
131
132     // Display dialogs.
133     connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
134
135     // Update the status bar with the URL when a link is hovered.
136     connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString)));
137
138     // Setup the GUI based on the privacybrowserui.rc file.
139     setupGUI();
140
141     // Load the initial webstie.
142     mainViewPointer->loadInitialWebsite();
143 }
144
145 void BrowserWindow::fileNew() const
146 {
147     // Display a new instance of Privacy Browser.
148     (new BrowserWindow)->show();
149 }
150
151 void BrowserWindow::getZoomFactorFromUser()
152 {
153     // Create an OK flag.
154     bool okClicked;
155
156     // 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.
157     double newZoomFactor = QInputDialog::getDouble(this, i18nc("The tile of the on-the-fly zoom factor dialog", "On-The-Fly Zoom Factor"),
158                                                    i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
159                                                    currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
160
161     if (okClicked)
162     {
163         // Update the current zoom factor.
164         currentZoomFactor = newZoomFactor;
165
166         // Set the new zoom factor.
167         mainViewPointer->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         // Create the settings widgets.
180         QWidget *privacySettingsWidgetPointer = new QWidget;
181         QWidget *generalSettingsWidgetPointer = new QWidget;
182
183         // Instantiate the settings UI.
184         Ui::PrivacySettings privacySettingsUi;
185         Ui::GeneralSettings generalSettingsUi;
186
187         // Setup the UI to display the settings widgets.
188         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
189         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
190
191         // Get handles for the widgets.
192         QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
193         userAgentLabelPointer = privacySettingsUi.userAgentLabel;
194         QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
195         searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
196
197         // Populate the combo box labels.
198         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
199         updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
200
201         // Update the labels when the combo boxs change.
202         connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
203         connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
204
205         // Instantiate a settings config dialog from the settings.kcfg file.
206         KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
207
208         // Add the settings widgets as config dialog pages.
209         configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
210         configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
211
212         // Delete the config dialog when it is closed.
213         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
214
215         // Make it so.
216         configDialogPointer->show();
217
218         // Expand the config dialog.
219         configDialogPointer->resize(1000, 500);
220
221         // Apply the settings when they are updated.
222         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyApplicationSettings()));
223         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettingsAndReload()));
224     }
225 }
226
227 void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const
228 {
229     // Initialize the custom search engine flag.
230     bool customSearchEngine = false;
231
232     if (searchEngine == "Mojeek")  // Mojeek.
233     {
234         searchEngineMojeekActionPointer->setChecked(true);
235     }
236     else if (searchEngine == "Monocles")  // Monocles.
237     {
238         searchEngineMonoclesActionPointer->setChecked(true);
239     }
240     else if (searchEngine == "MetaGer")  // MetaGer.
241     {
242         searchEngineMetagerActionPointer->setChecked(true);
243     }
244     else if (searchEngine == "Google")  // Google.
245     {
246         searchEngineGoogleActionPointer->setChecked(true);
247     }
248     else if (searchEngine == "Bing")  // Bing.
249     {
250         searchEngineBingActionPointer->setChecked(true);
251     }
252     else if (searchEngine == "Yahoo")  // Yahoo.
253     {
254         searchEngineYahooActionPointer->setChecked(true);
255     }
256     else  // Custom search engine.
257     {
258         // Check the user agent.
259         searchEngineCustomActionPointer->setChecked(true);
260
261         // Set the custom search engine flag.
262         customSearchEngine = true;
263     }
264
265
266     // Format the custom search engine.
267     if (customSearchEngine)
268     {
269         // Enable the custom search engine.
270         searchEngineCustomActionPointer->setEnabled(true);
271
272         // Set the custom search engine text.
273         searchEngineCustomActionPointer->setText(searchEngine);
274     }
275     else
276     {
277         // Disable the custom search engine.
278         searchEngineCustomActionPointer->setEnabled(false);
279
280         // Reset the custom search engine text.
281         searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
282     }
283 }
284
285 void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
286 {
287     // Initialize the custom user agent flag.
288     bool customUserAgent = false;
289
290     // Check the indicated on-the-fly user agent.
291     if (userAgent == "Privacy Browser")  // Privacy Browser.
292     {
293         userAgentPrivacyBrowserActionPointer->setChecked(true);
294     }
295     else if (userAgent == "Firefox Linux")  // Firefox Linux.
296     {
297         userAgentFirefoxLinuxActionPointer->setChecked(true);
298     }
299     else if (userAgent == "Chromium Linux")  // Chromium Linux.
300     {
301         userAgentChromiumLinuxActionPointer->setChecked(true);
302     }
303     else if (userAgent == "Firefox Windows")  // Firefox Windows.
304     {
305         userAgentFirefoxWindowsActionPointer->setChecked(true);
306     }
307     else if (userAgent == "Chrome Windows")  // Chrome Windows.
308     {
309         userAgentChromeWindowsActionPointer->setChecked(true);
310     }
311     else if (userAgent == "Edge Windows")  // Edge Windows.
312     {
313         userAgentEdgeWindowsActionPointer->setChecked(true);
314     }
315     else if (userAgent == "Safari macOS")  // Safari macOS.
316     {
317         userAgentSafariMacosActionPointer->setChecked(true);
318     }
319     else  // Custom user agent.
320     {
321         // Check the user agent.
322         userAgentCustomActionPointer->setChecked(true);
323
324         // Set the custom user agent flag.
325         customUserAgent = true;
326     }
327
328
329     // Format the custom user agent.
330     if (customUserAgent)
331     {
332         // Enable the custom user agent.
333         userAgentCustomActionPointer->setEnabled(true);
334
335         // Set the custom user agent text.
336         userAgentCustomActionPointer->setText(userAgent);
337     }
338     else
339     {
340         // Disable the custom user agent.
341         userAgentCustomActionPointer->setEnabled(false);
342
343         // Reset the custom user agent text.
344         userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
345     }
346 }
347
348 void BrowserWindow::updateOnTheFlyZoomFactor(const double &zoomFactor)
349 {
350     // Set the current zoom factor.
351     currentZoomFactor = Settings::zoomFactor();
352
353     // Update the zoom factor action text, formatting the double with 2 decimal places.
354     zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
355 }
356
357 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
358 {
359     // Update the search engine label.
360     searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
361 }
362
363 void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const
364 {
365     // Display the status bar message.
366     statusBar()->showMessage(statusBarMessage);
367 }
368
369 void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const
370 {
371     // Update the user agent label.
372     userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName));
373 }