]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/BrowserWindow.cpp
Add on-the-fly settings.
[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 <QStatusBar>
34
35 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
36 {
37     // Instantiate the main view pointer.
38     mainViewPointer = new MainView(this);
39
40     // Set the main view as the central widget.
41     setCentralWidget(mainViewPointer);
42
43     // Get a handle for the action collectoin.
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     searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
61     searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
62     searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
63     searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
64     searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
65     searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
66     searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
67
68     // Create the action groups
69     QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
70     QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
71
72     // Add the actions to the groups.
73     userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
74     userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
75     userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
76     userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
77     userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
78     userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
79     userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
80     userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
81     searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
82     searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
83     searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
84     searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
85     searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
86     searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
87     searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
88
89     // Set some actions to be checkable.
90     userAgentPrivacyBrowserActionPointer->setCheckable(true);
91     userAgentFirefoxLinuxActionPointer->setCheckable(true);
92     userAgentChromiumLinuxActionPointer->setCheckable(true);
93     userAgentFirefoxWindowsActionPointer->setCheckable(true);
94     userAgentChromeWindowsActionPointer->setCheckable(true);
95     userAgentEdgeWindowsActionPointer->setCheckable(true);
96     userAgentSafariMacosActionPointer->setCheckable(true);
97     userAgentCustomActionPointer->setCheckable(true);
98     searchEngineMojeekActionPointer->setCheckable(true);
99     searchEngineMonoclesActionPointer->setCheckable(true);
100     searchEngineMetagerActionPointer->setCheckable(true);
101     searchEngineGoogleActionPointer->setCheckable(true);
102     searchEngineBingActionPointer->setCheckable(true);
103     searchEngineYahooActionPointer->setCheckable(true);
104     searchEngineCustomActionPointer->setCheckable(true);
105
106     // Set the action names.  The custom action text will be set later in the on-the-fly update slots.
107     userAgentPrivacyBrowserActionPointer->setText(i18nc("@action", "Privacy Browser"));
108     userAgentFirefoxLinuxActionPointer->setText(i18nc("@action", "Firefox Linux"));
109     userAgentChromiumLinuxActionPointer->setText(i18nc("@action", "Chromium Linux"));
110     userAgentFirefoxWindowsActionPointer->setText(i18nc("@action", "Firefox Windows"));
111     userAgentChromeWindowsActionPointer->setText(i18nc("@action", "Chrome Windows"));
112     userAgentEdgeWindowsActionPointer->setText(i18nc("@action", "Edge Windows"));
113     userAgentSafariMacosActionPointer->setText(i18nc("@action", "Safari macOS"));
114     searchEngineMojeekActionPointer->setText(i18nc("@action", "Mojeek"));
115     searchEngineMonoclesActionPointer->setText(i18nc("@action", "Monocles"));
116     searchEngineMetagerActionPointer->setText(i18nc("@action", "MetaGer"));
117     searchEngineGoogleActionPointer->setText(i18nc("@action", "Google"));
118     searchEngineBingActionPointer->setText(i18nc("@action", "Bing"));
119     searchEngineYahooActionPointer->setText(i18nc("@action", "Yahoo"));
120
121     // Update the on-the-fly menus.
122     connect(mainViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString)));
123     connect(mainViewPointer, SIGNAL(searchEngineUpdated(QString)), this, SLOT(updateOnTheFlySearchEngine(QString)));
124
125     // Apply the on-the-fly settings when selected.
126     connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
127     connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
128
129     // Initialize the on-the-fly search engine menu, as the slot connection had not been created when MainView was constructed.
130     updateOnTheFlySearchEngine(Settings::searchEngine());
131
132     // Update the status bar with the URL when a link is hovered.
133     connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString)));
134
135     // Setup the GUI based on the privacybrowserui.rc file.
136     setupGUI();
137 }
138
139 void BrowserWindow::fileNew() const
140 {
141     // Display a new instance of Privacy Browser.
142     (new BrowserWindow)->show();
143 }
144
145 void BrowserWindow::settingsConfigure()
146 {
147     // Check to make sure the dialog box isn't already displayed.
148     if (!KConfigDialog::exists(QStringLiteral("settings")))
149     {
150         // Create the settings widgets.
151         QWidget *privacySettingsWidgetPointer = new QWidget;
152         QWidget *generalSettingsWidgetPointer = new QWidget;
153
154         // Instantiate the settings UI.
155         Ui::PrivacySettings privacySettingsUi;
156         Ui::GeneralSettings generalSettingsUi;
157
158         // Setup the UI to display the settings widgets.
159         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
160         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
161
162         // Get handles for the widgets.
163         QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
164         userAgentLabelPointer = privacySettingsUi.userAgentLabel;
165         QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
166         searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
167
168         // Populate the combo box labels.
169         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
170         updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
171
172         // Update the labels when the combo boxs change.
173         connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
174         connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
175
176         // Instantiate a settings config dialog from the settings.kcfg file.
177         KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
178
179         // Add the settings widgets as config dialog pages.
180         configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
181         configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
182
183         // Delete the config dialog when it is closed.
184         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
185
186         // Make it so.
187         configDialogPointer->show();
188
189         // Expand the config dialog.
190         configDialogPointer->resize(1000, 500);
191
192         // Apply the settings when they are updated.
193         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyApplicationSettings()));
194         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettingsAndReload()));
195     }
196 }
197
198 void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const
199 {
200     // Initialize the custom search engine flag.
201     bool customSearchEngine = false;
202
203     if (searchEngine == "Mojeek")  // Mojeek.
204     {
205         searchEngineMojeekActionPointer->setChecked(true);
206     }
207     else if (searchEngine == "Monocles")  // Monocles.
208     {
209         searchEngineMonoclesActionPointer->setChecked(true);
210     }
211     else if (searchEngine == "MetaGer")  // MetaGer.
212     {
213         searchEngineMetagerActionPointer->setChecked(true);
214     }
215     else if (searchEngine == "Google")  // Google.
216     {
217         searchEngineGoogleActionPointer->setChecked(true);
218     }
219     else if (searchEngine == "Bing")  // Bing.
220     {
221         searchEngineBingActionPointer->setChecked(true);
222     }
223     else if (searchEngine == "Yahoo")  // Yahoo.
224     {
225         searchEngineYahooActionPointer->setChecked(true);
226     }
227     else  // Custom search engine.
228     {
229         // Check the user agent.
230         searchEngineCustomActionPointer->setChecked(true);
231
232         // Set the custom search engine flag.
233         customSearchEngine = true;
234     }
235
236
237     // Format the custom search engine.
238     if (customSearchEngine)
239     {
240         // Enable the custom search engine.
241         searchEngineCustomActionPointer->setEnabled(true);
242
243         // Set the custom search engine text.
244         searchEngineCustomActionPointer->setText(searchEngine);
245     }
246     else
247     {
248         // Disable the custom search engine.
249         searchEngineCustomActionPointer->setEnabled(false);
250
251         // Reset the custom search engine text.
252         searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
253     }
254 }
255
256 void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
257 {
258     // Initialize the custom user agent flag.
259     bool customUserAgent = false;
260
261     // Check the indicated on-the-fly user agent.
262     if (userAgent == "Privacy Browser")  // Privacy Browser.
263     {
264         userAgentPrivacyBrowserActionPointer->setChecked(true);
265     }
266     else if (userAgent == "Firefox Linux")  // Firefox Linux.
267     {
268         userAgentFirefoxLinuxActionPointer->setChecked(true);
269     }
270     else if (userAgent == "Chromium Linux")  // Chromium Linux.
271     {
272         userAgentChromiumLinuxActionPointer->setChecked(true);
273     }
274     else if (userAgent == "Firefox Windows")  // Firefox Windows.
275     {
276         userAgentFirefoxWindowsActionPointer->setChecked(true);
277     }
278     else if (userAgent == "Chrome Windows")  // Chrome Windows.
279     {
280         userAgentChromeWindowsActionPointer->setChecked(true);
281     }
282     else if (userAgent == "Edge Windows")  // Edge Windows.
283     {
284         userAgentEdgeWindowsActionPointer->setChecked(true);
285     }
286     else if (userAgent == "Safari macOS")  // Safari macOS.
287     {
288         userAgentSafariMacosActionPointer->setChecked(true);
289     }
290     else  // Custom user agent.
291     {
292         // Check the user agent.
293         userAgentCustomActionPointer->setChecked(true);
294
295         // Set the custom user agent flag.
296         customUserAgent = true;
297     }
298
299
300     // Format the custom user agent.
301     if (customUserAgent)
302     {
303         // Enable the custom user agent.
304         userAgentCustomActionPointer->setEnabled(true);
305
306         // Set the custom user agent text.
307         userAgentCustomActionPointer->setText(userAgent);
308     }
309     else
310     {
311         // Disable the custom user agent.
312         userAgentCustomActionPointer->setEnabled(false);
313
314         // Reset the custom user agent text.
315         userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
316     }
317 }
318
319
320 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
321 {
322     // Update the search engine label.
323     searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
324 }
325
326 void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const
327 {
328     // Display the status bar message.
329     statusBar()->showMessage(statusBarMessage);
330 }
331
332 void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const
333 {
334     // Update the user agent label.
335     userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName));
336 }