]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/BrowserWindow.cpp
29fe4644feb31db29f80f92ea8bdd798e82d7862
[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     // Update the status bar with the URL when a link is hovered.
52     connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString)));
53
54     // Setup the GUI based on the privacybrowserui.rc file.
55     setupGUI();
56 }
57
58 void BrowserWindow::fileNew() const
59 {
60     // Display a new instance of Privacy Browser.
61     (new BrowserWindow)->show();
62 }
63
64 void BrowserWindow::settingsConfigure()
65 {
66     // Check to make sure the dialog box isn't already displayed.
67     if (!KConfigDialog::exists(QStringLiteral("settings")))
68     {
69         // Create the settings widgets.
70         QWidget *privacySettingsWidgetPointer = new QWidget;
71         QWidget *generalSettingsWidgetPointer = new QWidget;
72
73         // Instantiate the settings UI.
74         Ui::PrivacySettings privacySettingsUi;
75         Ui::GeneralSettings generalSettingsUi;
76
77         // Setup the UI to display the settings widgets.
78         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
79         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
80
81         // Get handles for the widgets.
82         QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
83         userAgentLabelPointer = privacySettingsUi.userAgentLabel;
84         QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
85         searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
86
87         // Populate the combo box labels.
88         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
89         updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
90
91         // Update the labels when the combo boxs change.
92         connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
93         connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
94
95         // Instantiate a settings config dialog from the settings.kcfg file.
96         KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
97
98         // Add the settings widgets as config dialog pages.
99         configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
100         configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
101
102         // Delete the config dialog when it is closed.
103         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
104
105         // Make it so.
106         configDialogPointer->show();
107
108         // Expand the config dialog.
109         configDialogPointer->resize(1000, 500);
110
111         // Apply the settings when they are updated.
112         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyApplicationSettings()));
113         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettingsAndReload()));
114     }
115 }
116
117 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
118 {
119     // Update the search engine label.
120     searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
121 }
122
123 void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const
124 {
125     // Display the status bar message.
126     statusBar()->showMessage(statusBarMessage);
127 }
128
129 void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const
130 {
131     // Update the user agent label.
132     userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName));
133 }