]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/BrowserWindow.cpp
Add on-the-fly zoom factor.
[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     // Set the current zoom factor.
136     currentZoomFactor = Settings::zoomFactor();
137
138     // Initialize the on-the-fly menus, as the slot connections had not been created when MainView was constructed.
139     updateOnTheFlyZoomFactor(currentZoomFactor);
140     updateOnTheFlySearchEngine(Settings::searchEngine());
141
142     // Update the status bar with the URL when a link is hovered.
143     connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString)));
144
145     // Setup the GUI based on the privacybrowserui.rc file.
146     setupGUI();
147 }
148
149 void BrowserWindow::fileNew() const
150 {
151     // Display a new instance of Privacy Browser.
152     (new BrowserWindow)->show();
153 }
154
155 void BrowserWindow::getZoomFactorFromUser()
156 {
157     // Create an OK flag.
158     bool okClicked;
159
160     // 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.
161     double newZoomFactor = QInputDialog::getDouble(this, i18nc("The tile of the on-the-fly zoom factor dialog", "On-The-Fly Zoom Factor"),
162                                                    i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
163                                                    currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
164
165     if (okClicked)
166     {
167         // Update the current zoom factor.
168         currentZoomFactor = newZoomFactor;
169
170         // Set the new zoom factor.
171         mainViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
172
173         // Update the on-the-fly action text.
174         updateOnTheFlyZoomFactor(newZoomFactor);
175     }
176 }
177
178 void BrowserWindow::settingsConfigure()
179 {
180     // Check to make sure the dialog box isn't already displayed.
181     if (!KConfigDialog::exists(QStringLiteral("settings")))
182     {
183         // Create the settings widgets.
184         QWidget *privacySettingsWidgetPointer = new QWidget;
185         QWidget *generalSettingsWidgetPointer = new QWidget;
186
187         // Instantiate the settings UI.
188         Ui::PrivacySettings privacySettingsUi;
189         Ui::GeneralSettings generalSettingsUi;
190
191         // Setup the UI to display the settings widgets.
192         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
193         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
194
195         // Get handles for the widgets.
196         QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
197         userAgentLabelPointer = privacySettingsUi.userAgentLabel;
198         QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
199         searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
200
201         // Populate the combo box labels.
202         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
203         updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
204
205         // Update the labels when the combo boxs change.
206         connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
207         connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
208
209         // Instantiate a settings config dialog from the settings.kcfg file.
210         KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
211
212         // Add the settings widgets as config dialog pages.
213         configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
214         configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
215
216         // Delete the config dialog when it is closed.
217         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
218
219         // Make it so.
220         configDialogPointer->show();
221
222         // Expand the config dialog.
223         configDialogPointer->resize(1000, 500);
224
225         // Apply the settings when they are updated.
226         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyApplicationSettings()));
227         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettingsAndReload()));
228     }
229 }
230
231 void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const
232 {
233     // Initialize the custom search engine flag.
234     bool customSearchEngine = false;
235
236     if (searchEngine == "Mojeek")  // Mojeek.
237     {
238         searchEngineMojeekActionPointer->setChecked(true);
239     }
240     else if (searchEngine == "Monocles")  // Monocles.
241     {
242         searchEngineMonoclesActionPointer->setChecked(true);
243     }
244     else if (searchEngine == "MetaGer")  // MetaGer.
245     {
246         searchEngineMetagerActionPointer->setChecked(true);
247     }
248     else if (searchEngine == "Google")  // Google.
249     {
250         searchEngineGoogleActionPointer->setChecked(true);
251     }
252     else if (searchEngine == "Bing")  // Bing.
253     {
254         searchEngineBingActionPointer->setChecked(true);
255     }
256     else if (searchEngine == "Yahoo")  // Yahoo.
257     {
258         searchEngineYahooActionPointer->setChecked(true);
259     }
260     else  // Custom search engine.
261     {
262         // Check the user agent.
263         searchEngineCustomActionPointer->setChecked(true);
264
265         // Set the custom search engine flag.
266         customSearchEngine = true;
267     }
268
269
270     // Format the custom search engine.
271     if (customSearchEngine)
272     {
273         // Enable the custom search engine.
274         searchEngineCustomActionPointer->setEnabled(true);
275
276         // Set the custom search engine text.
277         searchEngineCustomActionPointer->setText(searchEngine);
278     }
279     else
280     {
281         // Disable the custom search engine.
282         searchEngineCustomActionPointer->setEnabled(false);
283
284         // Reset the custom search engine text.
285         searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
286     }
287 }
288
289 void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
290 {
291     // Initialize the custom user agent flag.
292     bool customUserAgent = false;
293
294     // Check the indicated on-the-fly user agent.
295     if (userAgent == "Privacy Browser")  // Privacy Browser.
296     {
297         userAgentPrivacyBrowserActionPointer->setChecked(true);
298     }
299     else if (userAgent == "Firefox Linux")  // Firefox Linux.
300     {
301         userAgentFirefoxLinuxActionPointer->setChecked(true);
302     }
303     else if (userAgent == "Chromium Linux")  // Chromium Linux.
304     {
305         userAgentChromiumLinuxActionPointer->setChecked(true);
306     }
307     else if (userAgent == "Firefox Windows")  // Firefox Windows.
308     {
309         userAgentFirefoxWindowsActionPointer->setChecked(true);
310     }
311     else if (userAgent == "Chrome Windows")  // Chrome Windows.
312     {
313         userAgentChromeWindowsActionPointer->setChecked(true);
314     }
315     else if (userAgent == "Edge Windows")  // Edge Windows.
316     {
317         userAgentEdgeWindowsActionPointer->setChecked(true);
318     }
319     else if (userAgent == "Safari macOS")  // Safari macOS.
320     {
321         userAgentSafariMacosActionPointer->setChecked(true);
322     }
323     else  // Custom user agent.
324     {
325         // Check the user agent.
326         userAgentCustomActionPointer->setChecked(true);
327
328         // Set the custom user agent flag.
329         customUserAgent = true;
330     }
331
332
333     // Format the custom user agent.
334     if (customUserAgent)
335     {
336         // Enable the custom user agent.
337         userAgentCustomActionPointer->setEnabled(true);
338
339         // Set the custom user agent text.
340         userAgentCustomActionPointer->setText(userAgent);
341     }
342     else
343     {
344         // Disable the custom user agent.
345         userAgentCustomActionPointer->setEnabled(false);
346
347         // Reset the custom user agent text.
348         userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
349     }
350 }
351
352 void BrowserWindow::updateOnTheFlyZoomFactor(const double &zoomFactor) const
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 &userAgentName) const
371 {
372     // Update the user agent label.
373     userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName));
374 }