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"
25 // KDE Framework headers.
28 // Qt framework headers.
29 #include <QWebEngineHistory>
30 #include <QWebEngineView>
32 MainView::MainView(QWidget *parent) : QWidget(parent)
34 // Instantiate the mainview UI.
35 Ui::MainView mainViewUi;
38 mainViewUi.setupUi(this);
40 // Get handles for the views.
41 backButtonPointer = mainViewUi.backButton;
42 forwardButtonPointer = mainViewUi.forwardButton;
43 urlLineEditPointer = mainViewUi.urlLineEdit;
44 webEngineViewPointer = mainViewUi.webEngineView;
46 // Get handles for the webpage and history.
47 QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
48 webEngineHistoryPointer = webEnginePagePointer->history();
50 // Update the webengine view from the URL line edit.
51 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString)));
53 // Update the URL line edit form the webengine view.
54 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
55 connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface()));
56 connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface()));
58 // Setup the forward and back buttons.
59 connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
60 connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
62 // Listen for hovered link URLs.
63 connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString)));
65 // Set the zoom factor.
66 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
68 // Set the focus on the WebEngine view.
69 webEngineViewPointer->setFocus();
71 // Load the homepage. TODO. Consider sanitizing the homepage input and adding things like protocols if they are missing.
72 webEngineViewPointer->setUrl(Settings::homepage());
75 void MainView::loadUrl(const QString &urlFromUser)
77 // Remove the focus from the URL line edit.
78 urlLineEditPointer->clearFocus();
80 // Load the URL, adding standard protocol sections if needed. TODO. Replace this with logic that prefers HTTPS.
81 webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
84 void MainView::pageLinkHovered(const QString &linkUrl)
86 // Emit a signal so that the browser window can update the status bar.
87 emit linkHovered(linkUrl);
90 void MainView::updateInterface()
92 // Update the URL line edit if it does not have focus.
93 if (!urlLineEditPointer->hasFocus())
95 // Update the URL line edit.
96 urlLineEditPointer->setUrl(webEngineViewPointer->url().toString());
99 backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
100 forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
102 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. Hopefully it will be fixed in Qt6. <https://bugreports.qt.io/browse/QTBUG-51992>
103 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());