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