]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/RequestsDialog.cpp
Partial filter list implementation.
[PrivacyBrowserPC.git] / src / dialogs / RequestsDialog.cpp
1 /*
2  * Copyright 2024 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 "RequestsDialog.h"
22 #include "GlobalVariables.h"
23 #include "RequestDetailDialog.h"
24 #include "ui_RequestsDialog.h"
25
26 // Qt toolkit headers.
27 #include <QDebug>
28
29 // KDE Frameworks headers.
30 #include <KColorScheme>
31
32 // Construct the class.
33 RequestsDialog::RequestsDialog(QWidget *parentWidgetPointer, QList<RequestStruct *> *requestsListPointer) : QDialog(parentWidgetPointer)
34 {
35     // Set the window title.
36     setWindowTitle(i18nc("The requests dialog window title", "Requests"));
37
38     // Set the window modality.
39     setWindowModality(Qt::WindowModality::ApplicationModal);
40
41     // Instantiate the requests dialog UI.
42     Ui::RequestsDialog requestsDialogUi;
43
44     // Setup the UI.
45     requestsDialogUi.setupUi(this);
46
47     // Get handles for the views.
48     tableWidgetPointer = requestsDialogUi.tableWidget;
49     QDialogButtonBox *dialogButtonBoxPointer = requestsDialogUi.dialogButtonBox;
50
51     // Create the columns.
52     tableWidgetPointer->setColumnCount(5);
53
54     // Create the table headers.
55     QTableWidgetItem *dispositionHeaderItemPointer = new QTableWidgetItem(i18nc("Request disposition header", "Disposition"));
56     QTableWidgetItem *requestMethodHeaderItemPointer = new QTableWidgetItem(i18nc("Request method header", "Request Method"));
57     QTableWidgetItem *navigationTypeHeaderItemPointer = new QTableWidgetItem(i18nc("Navigation type header", "Navigation Type"));
58     QTableWidgetItem *resourceTypeHeaderItemPointer = new QTableWidgetItem(i18nc("Resource type header", "Resource Type"));
59     QTableWidgetItem *urlHeaderItemPointer = new QTableWidgetItem(i18nc("Request URL header", "URL"));
60
61     // Set the horizontal headers.
62     tableWidgetPointer->setHorizontalHeaderItem(0, dispositionHeaderItemPointer);
63     tableWidgetPointer->setHorizontalHeaderItem(1, requestMethodHeaderItemPointer);
64     tableWidgetPointer->setHorizontalHeaderItem(2, navigationTypeHeaderItemPointer);
65     tableWidgetPointer->setHorizontalHeaderItem(3, resourceTypeHeaderItemPointer);
66     tableWidgetPointer->setHorizontalHeaderItem(4, urlHeaderItemPointer);
67
68     // Create the palette.
69     QPalette negativePalette = QPalette();
70     QPalette positivePalette = QPalette();
71
72     // Adjust the background colors of the palettes.
73     KColorScheme::adjustBackground(negativePalette, KColorScheme::NegativeBackground);
74     KColorScheme::adjustBackground(positivePalette, KColorScheme::PositiveBackground);
75
76     // Prepare the background brushes.
77     QBrush negativeBackgroundBrush = negativePalette.base();
78     QBrush positiveBackgroundBrush = positivePalette.base();
79
80     // Initialize the row counter.
81     int rowCounter = 0;
82
83     for (RequestStruct *requestStructPointer : *requestsListPointer)
84     {
85         // Add a new row.
86         tableWidgetPointer->insertRow(rowCounter);
87
88         // Create the entry items.
89         QTableWidgetItem *dispositionItemPointer = new QTableWidgetItem(globalFilterListHelperPointer->getDispositionString(requestStructPointer->dispositionInt));
90         QTableWidgetItem *requestMethodItemPointer = new QTableWidgetItem(requestStructPointer->requestMethodString);
91         QTableWidgetItem *navigationTypeItemPointer = new QTableWidgetItem(globalFilterListHelperPointer->getNavigationTypeString(requestStructPointer->navigationTypeInt));
92         QTableWidgetItem *resourceTypeItemPointer = new QTableWidgetItem(globalFilterListHelperPointer->getResourceTypeString(requestStructPointer->resourceTypeInt));
93         QTableWidgetItem *urlItemPointer = new QTableWidgetItem(requestStructPointer->urlString);
94
95         // Create a request struct byte array.
96         QByteArray *requestStructByteArrayPointer = new QByteArray();
97
98         // Create a request struct data stream.
99         QDataStream requestStructDataStream(requestStructByteArrayPointer, QIODevice::WriteOnly);
100
101         // Populate the request struct data stream.
102         requestStructDataStream << requestStructPointer->dispositionInt;
103         requestStructDataStream << requestStructPointer->entryStruct.appliedEntry;
104         requestStructDataStream << requestStructPointer->entryStruct.originalEntry;
105         requestStructDataStream << requestStructPointer->filterListTitle;
106         requestStructDataStream << requestStructPointer->navigationTypeInt;
107         requestStructDataStream << requestStructPointer->requestMethodString;
108         requestStructDataStream << requestStructPointer->resourceTypeInt;
109         requestStructDataStream << requestStructPointer->sublistInt;
110         requestStructDataStream << requestStructPointer->urlString;
111
112         // Add the request struct to the disposition item.
113         dispositionItemPointer->setData(Qt::UserRole, *requestStructByteArrayPointer);
114
115         // Colorize the background according to the disposition.
116         if (requestStructPointer->dispositionInt == FilterListHelper::BLOCKED)  // The request was blocked.
117         {
118             // Set the background to be red.
119             dispositionItemPointer->setBackground(negativeBackgroundBrush);
120             requestMethodItemPointer->setBackground(negativeBackgroundBrush);
121             navigationTypeItemPointer->setBackground(negativeBackgroundBrush);
122             resourceTypeItemPointer->setBackground(negativeBackgroundBrush);
123             urlItemPointer->setBackground(negativeBackgroundBrush);
124         }
125         else if (requestStructPointer->dispositionInt == FilterListHelper::ALLOWED)  // The request was allowed.
126         {
127             // Set the background to be green.
128             dispositionItemPointer->setBackground(positiveBackgroundBrush);
129             requestMethodItemPointer->setBackground(positiveBackgroundBrush);
130             navigationTypeItemPointer->setBackground(positiveBackgroundBrush);
131             resourceTypeItemPointer->setBackground(positiveBackgroundBrush);
132             urlItemPointer->setBackground(positiveBackgroundBrush);
133         }
134
135         // Add the entries to the table.
136         tableWidgetPointer->setItem(rowCounter, 0, dispositionItemPointer);
137         tableWidgetPointer->setItem(rowCounter, 1, requestMethodItemPointer);
138         tableWidgetPointer->setItem(rowCounter, 2, navigationTypeItemPointer);
139         tableWidgetPointer->setItem(rowCounter, 3, resourceTypeItemPointer);
140         tableWidgetPointer->setItem(rowCounter, 4, urlItemPointer);
141
142         // Increment the row counter.
143         ++rowCounter;
144     }
145
146     // Select an entire row at a time.
147     tableWidgetPointer->setSelectionBehavior(QAbstractItemView::SelectRows);
148
149     // Get the table header view.
150     QHeaderView *headerViewPointer = tableWidgetPointer->horizontalHeader();
151
152     // Resize the header to fit the contents.
153     headerViewPointer->resizeSections(QHeaderView::ResizeToContents);
154
155     // Connect changes in the sort order.
156     connect(headerViewPointer, SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), this, SLOT(sortTable(int, Qt::SortOrder)));
157
158     // Connect the buttons.
159     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(close()));
160
161     // Open the request detail dialog when
162     connect(tableWidgetPointer, SIGNAL(cellClicked(int, int)), this, SLOT(showRequestDetailDialog(int)));
163 }
164
165 void RequestsDialog::showRequestDetailDialog(int row)
166 {
167     // Get the first table widget item in the row.
168     QTableWidgetItem *rowFirstTableWidgetItemPointer = tableWidgetPointer->item(row, 0);
169
170     // Get the data variant.
171     QVariant dataVariant = rowFirstTableWidgetItemPointer->data(Qt::UserRole);
172
173     // Get the request struct byte array from the data variant.
174     QByteArray requestStructByteArray = dataVariant.toByteArray();
175
176     // Instantiate the request details dialog.
177     RequestDetailDialog *requestDetailDialogPointer = new RequestDetailDialog(this, requestStructByteArray);
178
179     // Show the dialog.
180     requestDetailDialogPointer->show();
181 }
182
183 void RequestsDialog::sortTable(int column, Qt::SortOrder sortOrder) const
184 {
185     // Sort the table.
186     tableWidgetPointer->sortItems(column, sortOrder);
187 }