2 * Copyright 2024 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 "RequestsDialog.h"
22 #include "GlobalVariables.h"
23 #include "RequestDetailDialog.h"
24 #include "ui_RequestsDialog.h"
26 // Qt toolkit headers.
29 // KDE Frameworks headers.
30 #include <KColorScheme>
32 // Construct the class.
33 RequestsDialog::RequestsDialog(QWidget *parentWidgetPointer, QList<RequestStruct *> *requestsListPointer) : QDialog(parentWidgetPointer)
35 // Set the window title.
36 setWindowTitle(i18nc("The requests dialog window title", "Requests"));
38 // Set the window modality.
39 setWindowModality(Qt::WindowModality::ApplicationModal);
41 // Instantiate the requests dialog UI.
42 Ui::RequestsDialog requestsDialogUi;
45 requestsDialogUi.setupUi(this);
47 // Get handles for the views.
48 tableWidgetPointer = requestsDialogUi.tableWidget;
49 QDialogButtonBox *dialogButtonBoxPointer = requestsDialogUi.dialogButtonBox;
51 // Create the columns.
52 tableWidgetPointer->setColumnCount(5);
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"));
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);
68 // Create the palette.
69 QPalette negativePalette = QPalette();
70 QPalette positivePalette = QPalette();
72 // Adjust the background colors of the palettes.
73 KColorScheme::adjustBackground(negativePalette, KColorScheme::NegativeBackground);
74 KColorScheme::adjustBackground(positivePalette, KColorScheme::PositiveBackground);
76 // Prepare the background brushes.
77 QBrush negativeBackgroundBrush = negativePalette.base();
78 QBrush positiveBackgroundBrush = positivePalette.base();
80 // Initialize the row counter.
83 for (RequestStruct *requestStructPointer : *requestsListPointer)
86 tableWidgetPointer->insertRow(rowCounter);
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);
95 // Create a request struct byte array.
96 QByteArray *requestStructByteArrayPointer = new QByteArray();
98 // Create a request struct data stream.
99 QDataStream requestStructDataStream(requestStructByteArrayPointer, QIODevice::WriteOnly);
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;
112 // Add the request struct to the disposition item.
113 dispositionItemPointer->setData(Qt::UserRole, *requestStructByteArrayPointer);
115 // Colorize the background according to the disposition.
116 if (requestStructPointer->dispositionInt == FilterListHelper::BLOCKED) // The request was blocked.
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);
125 else if (requestStructPointer->dispositionInt == FilterListHelper::ALLOWED) // The request was allowed.
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);
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);
142 // Increment the row counter.
146 // Select an entire row at a time.
147 tableWidgetPointer->setSelectionBehavior(QAbstractItemView::SelectRows);
149 // Get the table header view.
150 QHeaderView *headerViewPointer = tableWidgetPointer->horizontalHeader();
152 // Resize the header to fit the contents.
153 headerViewPointer->resizeSections(QHeaderView::ResizeToContents);
155 // Connect changes in the sort order.
156 connect(headerViewPointer, SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), this, SLOT(sortTable(int, Qt::SortOrder)));
158 // Connect the buttons.
159 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(close()));
161 // Open the request detail dialog when
162 connect(tableWidgetPointer, SIGNAL(cellClicked(int, int)), this, SLOT(showRequestDetailDialog(int)));
165 void RequestsDialog::showRequestDetailDialog(int row)
167 // Get the first table widget item in the row.
168 QTableWidgetItem *rowFirstTableWidgetItemPointer = tableWidgetPointer->item(row, 0);
170 // Get the data variant.
171 QVariant dataVariant = rowFirstTableWidgetItemPointer->data(Qt::UserRole);
173 // Get the request struct byte array from the data variant.
174 QByteArray requestStructByteArray = dataVariant.toByteArray();
176 // Instantiate the request details dialog.
177 RequestDetailDialog *requestDetailDialogPointer = new RequestDetailDialog(this, requestStructByteArray);
180 requestDetailDialogPointer->show();
183 void RequestsDialog::sortTable(int column, Qt::SortOrder sortOrder) const
186 tableWidgetPointer->sortItems(column, sortOrder);