]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/BrowserWindow.cpp
Add user agent controls.
[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
26 // KDE Frameworks headers.
27 #include <KActionCollection>
28 #include <KConfigDialog>
29
30 // Qt framework headers.
31 #include <QStatusBar>
32
33 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
34 {
35     // Instantiate the main view pointer.
36     mainViewPointer = new MainView(this);
37
38     // Set the main view as the central widget.
39     setCentralWidget(mainViewPointer);
40
41     // Get a handle for the action collectoin.
42     KActionCollection *actionCollectionPointer = this->actionCollection();
43
44     // Add the standard actions.
45     KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
46     KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
47     KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
48
49     // Update the status bar with the URL when a link is hovered.
50     connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString)));
51
52     // Setup the GUI based on the privacybrowserui.rc file.
53     setupGUI();
54 }
55
56 void BrowserWindow::fileNew()
57 {
58     // Display a new instance of Privacy Browser.
59     (new BrowserWindow)->show();
60 }
61
62 void BrowserWindow::settingsConfigure()
63 {
64     // Check to make sure the dialog box isn't already displayed.
65     if (!KConfigDialog::exists(QStringLiteral("settings")))
66     {
67         // Create the settings widgets.
68         QWidget *privacySettingsWidgetPointer = new QWidget;
69         QWidget *generalSettingsWidgetPointer = new QWidget;
70
71         // Instantiate the settings UI.
72         Ui::PrivacySettings privacySettingsUi;
73         Ui::GeneralSettings generalSettingsUi;
74
75         // Setup the UI to display the settings widgets.
76         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
77         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
78
79         // Get handles for the user agent widgets.
80         QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
81         userAgentLabelPointer = privacySettingsUi.userAgentLabel;
82
83         // Instantiate the user agent helper.
84         userAgentHelperPointer = new UserAgentHelper();
85
86         // Display the initial user agent.
87         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
88
89         // Update the user agent when the combo box changes.
90         connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(QString)), this, SLOT(updateUserAgentLabel(QString)));
91
92         // Instantiate a settings config dialog from the settings.kcfg file.
93         KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
94
95         // Add the settings widgets as config dialog pages.
96         configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
97         configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
98
99         // Delete the config dialog when it is closed.
100         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
101
102         // Make it so.
103         configDialogPointer->show();
104
105         // Expand the config dialog.
106         configDialogPointer->resize(1000, 500);
107
108         // Apply the settings when they are updated.
109         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyApplicationSettings()));
110         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettingsAndReload()));
111     }
112 }
113
114 void BrowserWindow::updateStatusBar(const QString &statusBarMessage)
115 {
116     // Display the status bar message.
117     statusBar()->showMessage(statusBarMessage);
118 }
119
120 void BrowserWindow::updateUserAgentLabel(const QString &userAgentName)
121 {
122     // Update the user agent label.
123     userAgentLabelPointer->setText(userAgentHelperPointer->getUserAgent(userAgentName));
124 }