]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/widgets/PrivacyWebEngineView.cpp
Add a background tab action to the context menu. https://redmine.stoutner.com/issues/949
[PrivacyBrowserPC.git] / src / widgets / PrivacyWebEngineView.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 "PrivacyWebEngineView.h"
22 #include "databases/CookiesDatabase.h"
23 #include "windows/BrowserWindow.h"
24
25 // Qt toolkit headers.
26 #include <QContextMenuEvent>
27 #include <QMenu>
28
29 // Construct the class.
30 PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) {}
31
32 void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const
33 {
34     //qDebug() << "Add cookie:  " << cookie.toRawForm();
35
36     // Add the new cookie to the list.
37     cookieListPointer->push_front(cookie);
38
39     // Update the cookie if it is durable and has new data.
40     if (CookiesDatabase::isUpdate(cookie))
41         CookiesDatabase::updateCookie(cookie);
42
43     // Update the cookies action.
44     emit updateCookiesAction(cookieListPointer->size());
45 }
46
47 void PrivacyWebEngineView::contextMenuEvent(QContextMenuEvent *contextMenuEvent) {
48     // Get a handle for the
49     QWebEnginePage *webEnginePagePointer = page();
50
51     // Get a handle for the menu.
52     QMenu *contextMenu = webEnginePagePointer->createStandardContextMenu();
53
54     // Get the list of context menu actions.
55     const QList<QAction *> contextMenuActionsList = contextMenu->actions();
56
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));
60
61     // Display the menu using the location in the context menu event.
62     contextMenu->popup(contextMenuEvent->globalPos());
63 }
64
65 QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType webWindowType) {
66     // Get a handle for the browser window.
67     BrowserWindow *browserWindowPointer = qobject_cast<BrowserWindow*>(window());
68
69     // Create the requsted window type.
70     switch (webWindowType)
71     {
72         case QWebEnginePage::WebBrowserTab:
73         {
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);
77         }
78
79         case QWebEnginePage::WebBrowserWindow:
80         {
81             // Create a new browser window.
82             BrowserWindow *newBrowserWindowPointer = new BrowserWindow();
83
84             // Show the new browser window.
85             newBrowserWindowPointer->show();
86
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();
89         }
90
91         case QWebEnginePage::WebBrowserBackgroundTab:
92         {
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);
96         }
97
98         default:
99         {
100             // Return an null pointer for opening a background tab and opening a web dialog.
101             return nullptr;
102         }
103     }
104 }
105
106 void PrivacyWebEngineView::removeCookieFromList(const QNetworkCookie &cookie) const
107 {
108     //qDebug() << "Remove cookie:  " << cookie.toRawForm();
109
110     // Remove the cookie from the list.
111     cookieListPointer->remove(cookie);
112
113     // Update the cookies action.
114     emit updateCookiesAction(cookieListPointer->size());
115 }