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 "PrivacyWebEngineView.h"
22 #include "databases/CookiesDatabase.h"
23 #include "windows/BrowserWindow.h"
25 // Qt toolkit headers.
26 #include <QContextMenuEvent>
29 // Construct the class.
30 PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) {}
32 void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const
34 //qDebug() << "Add cookie: " << cookie.toRawForm();
36 // Add the new cookie to the list.
37 cookieListPointer->push_front(cookie);
39 // Update the cookie if it is durable and has new data.
40 if (CookiesDatabase::isUpdate(cookie))
41 CookiesDatabase::updateCookie(cookie);
43 // Update the cookies action.
44 emit updateCookiesAction(cookieListPointer->size());
47 void PrivacyWebEngineView::contextMenuEvent(QContextMenuEvent *contextMenuEvent) {
48 // Get a handle for the
49 QWebEnginePage *webEnginePagePointer = page();
51 // Get a handle for the menu.
52 QMenu *contextMenu = webEnginePagePointer->createStandardContextMenu();
54 // Get the list of context menu actions.
55 const QList<QAction *> contextMenuActionsList = contextMenu->actions();
57 // Add the open link in new background tab action if the context menu already contains the open link in new window action.
58 if (contextMenuActionsList.contains(webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewWindow)))
59 contextMenu->insertAction(webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewWindow), webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewBackgroundTab));
61 // Display the menu using the location in the context menu event.
62 contextMenu->popup(contextMenuEvent->globalPos());
65 QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType webWindowType) {
66 // Get a handle for the browser window.
67 BrowserWindow *browserWindowPointer = qobject_cast<BrowserWindow*>(window());
69 // Create the requsted window type.
70 switch (webWindowType)
72 case QWebEnginePage::WebBrowserTab:
74 // Create the new tab and return the privacy WebEngine view pointer. `true` removes the focus from the blank URL line edit.
75 // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
76 return browserWindowPointer->tabWidgetPointer->addTab(true);
79 case QWebEnginePage::WebBrowserWindow:
81 // Create a new browser window.
82 BrowserWindow *newBrowserWindowPointer = new BrowserWindow();
84 // Show the new browser window.
85 newBrowserWindowPointer->show();
87 // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
88 return newBrowserWindowPointer->tabWidgetPointer->loadBlankInitialWebsite();
91 case QWebEnginePage::WebBrowserBackgroundTab:
93 // Create the new tab and return the privacy WebEngine view pointer. `false` does not clear the URL line edit. `true` creates a background tab.
94 // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
95 return browserWindowPointer->tabWidgetPointer->addTab(false, true);
100 // Return an null pointer for opening a background tab and opening a web dialog.
106 void PrivacyWebEngineView::removeCookieFromList(const QNetworkCookie &cookie) const
108 //qDebug() << "Remove cookie: " << cookie.toRawForm();
110 // Remove the cookie from the list.
111 cookieListPointer->remove(cookie);
113 // Update the cookies action.
114 emit updateCookiesAction(cookieListPointer->size());