2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
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.
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.
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/>.
20 // Application headers.
21 #include "BrowserWindow.h"
23 #include "ui_SettingsPrivacy.h"
24 #include "ui_SettingsGeneral.h"
25 #include "helpers/SearchEngineHelper.h"
26 #include "helpers/UserAgentHelper.h"
28 // KDE Frameworks headers.
29 #include <KActionCollection>
30 #include <KConfigDialog>
32 // Qt framework headers.
35 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
37 // Instantiate the main view pointer.
38 mainViewPointer = new MainView(this);
40 // Set the main view as the central widget.
41 setCentralWidget(mainViewPointer);
43 // Get a handle for the action collectoin.
44 KActionCollection *actionCollectionPointer = this->actionCollection();
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);
51 // Update the status bar with the URL when a link is hovered.
52 connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString)));
54 // Setup the GUI based on the privacybrowserui.rc file.
58 void BrowserWindow::fileNew() const
60 // Display a new instance of Privacy Browser.
61 (new BrowserWindow)->show();
64 void BrowserWindow::settingsConfigure()
66 // Check to make sure the dialog box isn't already displayed.
67 if (!KConfigDialog::exists(QStringLiteral("settings")))
69 // Create the settings widgets.
70 QWidget *privacySettingsWidgetPointer = new QWidget;
71 QWidget *generalSettingsWidgetPointer = new QWidget;
73 // Instantiate the settings UI.
74 Ui::PrivacySettings privacySettingsUi;
75 Ui::GeneralSettings generalSettingsUi;
77 // Setup the UI to display the settings widgets.
78 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
79 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
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;
87 // Populate the combo box labels.
88 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
89 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
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)));
95 // Instantiate a settings config dialog from the settings.kcfg file.
96 KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
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"));
102 // Delete the config dialog when it is closed.
103 configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
106 configDialogPointer->show();
108 // Expand the config dialog.
109 configDialogPointer->resize(1000, 500);
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()));
117 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
119 // Update the search engine label.
120 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
123 void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const
125 // Display the status bar message.
126 statusBar()->showMessage(statusBarMessage);
129 void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const
131 // Update the user agent label.
132 userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName));