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 "RequestDetailDialog.h"
22 #include "GlobalVariables.h"
23 #include "ui_RequestDetailDialog.h"
24 #include "structs/RequestStruct.h"
26 // KDE Frameworks headers.
27 #include <KActionCollection>
28 #include <KColorScheme>
30 // Qt toolkit headers.
33 // Construct the class.
34 RequestDetailDialog::RequestDetailDialog(QWidget *parentWidgetPointer, QTableWidget *tableWidgetPointer, const int initialRow) :
35 QDialog(parentWidgetPointer), currentRow(initialRow), tableWidgetPointer(tableWidgetPointer)
37 // Set the window modality.
38 setWindowModality(Qt::WindowModality::ApplicationModal);
40 // Instantiate the request detail dialog UI.
41 Ui::RequestDetailDialog requestDetailDialogUi;
44 requestDetailDialogUi.setupUi(this);
46 // Get handles for the views.
47 dispositionLineEditPointer = requestDetailDialogUi.dispositionLineEdit;
48 urlLineEditPointer = requestDetailDialogUi.urlLineEdit;
49 requestMethodLineEditPointer = requestDetailDialogUi.requestMethodLineEdit;
50 navigationTypeLineEditPointer = requestDetailDialogUi.navigationTypeLineEdit;
51 resourceTypeLineEditPointer = requestDetailDialogUi.resourceTypeLineEdit;
52 horizontalLinePointer = requestDetailDialogUi.horizontalLine;
53 filterListLabelPointer = requestDetailDialogUi.filterListLabel;
54 filterListLineEditPointer = requestDetailDialogUi.filterListLineEdit;
55 sublistLabelPointer = requestDetailDialogUi.sublistLabel;
56 sublistLineEditPointer = requestDetailDialogUi.sublistListLineEdit;
57 appliedEntryListLabelPointer = requestDetailDialogUi.appliedEntryListLabel;
58 appliedEntryListLineEditPointer = requestDetailDialogUi.appliedEntryListLineEdit;
59 originalEntryLabelPointer = requestDetailDialogUi.originalEntryLabel;
60 originalEntryLineEditPointer = requestDetailDialogUi.originalEntryLineEdit;
61 previousButtonPointer = requestDetailDialogUi.previousButton;
62 nextButtonPointer = requestDetailDialogUi.nextButton;
63 QDialogButtonBox *dialogButtonBoxPointer = requestDetailDialogUi.dialogButtonBox;
64 QPushButton *closeButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::StandardButton::Close);
66 // Make the close button the default.
67 closeButtonPointer->setDefault(true);
69 // Get the disposition line edit palettes.
70 normalBackgroundPalette = dispositionLineEditPointer->palette();
71 negativeBackgroundPalette = normalBackgroundPalette;
72 positiveBackgroundPalette = normalBackgroundPalette;
74 // Modify the palettes.
75 KColorScheme::adjustBackground(negativeBackgroundPalette, KColorScheme::NegativeBackground);
76 KColorScheme::adjustBackground(positiveBackgroundPalette, KColorScheme::PositiveBackground);
78 // Connect the buttons.
79 connect(previousButtonPointer, SIGNAL(clicked()), this, SLOT(previous()));
80 connect(nextButtonPointer, SIGNAL(clicked()), this, SLOT(next()));
81 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(close()));
83 // Create the keyboard shortcuts.
84 QShortcut *previousShortcutPointer = new QShortcut(Qt::Key_Left, this);
85 QShortcut *nextShortcutPointer = new QShortcut(Qt::Key_Right, this);
87 // Connect the keyboard shortcuts to the buttons.
88 connect(previousShortcutPointer, SIGNAL(activated()), previousButtonPointer, SLOT(click()));
89 connect(nextShortcutPointer, SIGNAL(activated()), nextButtonPointer, SLOT(click()));
91 // Populate the dialog.
92 populateDialog(currentRow);
95 void RequestDetailDialog::populateDialog(const int row)
97 // Set the window title.
98 setWindowTitle(i18nc("The request detail dialog window title", "Request %1 Detail", row + 1));
100 // Select the row in the table widget (this displays the correct row highlighted in the background of the dialog).
101 tableWidgetPointer->selectRow(row);
103 // Get the first table widget item in the row.
104 QTableWidgetItem *rowFirstTableWidgetItemPointer = tableWidgetPointer->item(row, 0);
106 // Get the data variant.
107 QVariant dataVariant = rowFirstTableWidgetItemPointer->data(Qt::UserRole);
109 // Get the request struct byte array from the data variant.
110 QByteArray requestStructByteArray = dataVariant.toByteArray();
112 // Determine if previous should be enabled.
113 bool previousEnabled = (row > 0);
115 // Determine if next should be enabled.
116 bool nextEnabled = (row < (tableWidgetPointer->rowCount() - 1));
118 // Create a request struct data stream reader.
119 QDataStream requestStructDataStreamReader(requestStructByteArray);
121 // Create a new request struct.
122 RequestStruct *requestStructPointer = new RequestStruct();
124 // Populate the new request struct.
125 requestStructDataStreamReader >> requestStructPointer->dispositionInt;
126 requestStructDataStreamReader >> requestStructPointer->entryStruct.appliedEntryList;
127 requestStructDataStreamReader >> requestStructPointer->entryStruct.originalEntry;
128 requestStructDataStreamReader >> requestStructPointer->filterListTitle;
129 requestStructDataStreamReader >> requestStructPointer->navigationTypeInt;
130 requestStructDataStreamReader >> requestStructPointer->requestMethodString;
131 requestStructDataStreamReader >> requestStructPointer->resourceTypeInt;
132 requestStructDataStreamReader >> requestStructPointer->sublistInt;
133 requestStructDataStreamReader >> requestStructPointer->urlString;
135 // Populate the views.
136 dispositionLineEditPointer->setText(globalFilterListHelperPointer->getDispositionString(requestStructPointer->dispositionInt));
137 urlLineEditPointer->setText(requestStructPointer->urlString);
138 requestMethodLineEditPointer->setText(requestStructPointer->requestMethodString);
139 navigationTypeLineEditPointer->setText(globalFilterListHelperPointer->getNavigationTypeString(requestStructPointer->navigationTypeInt));
140 resourceTypeLineEditPointer->setText(globalFilterListHelperPointer->getResourceTypeString(requestStructPointer->resourceTypeInt));
141 filterListLineEditPointer->setText(requestStructPointer->filterListTitle);
142 sublistLineEditPointer->setText(globalFilterListHelperPointer->getSublistName(requestStructPointer->sublistInt));
143 appliedEntryListLineEditPointer->setText(requestStructPointer->entryStruct.appliedEntryList.join(QLatin1String(" * ")));
144 originalEntryLineEditPointer->setText(requestStructPointer->entryStruct.originalEntry);
146 // Set the button status.
147 previousButtonPointer->setEnabled(previousEnabled);
148 nextButtonPointer->setEnabled(nextEnabled);
150 // Modify the interface based on the disposition.
151 switch (requestStructPointer->dispositionInt)
153 case FilterListHelper::DEFAULT:
155 // Reset the disposition line edit background.
156 dispositionLineEditPointer->setPalette(normalBackgroundPalette);
159 horizontalLinePointer->hide();
160 filterListLabelPointer->hide();
161 filterListLineEditPointer->hide();
162 sublistLabelPointer->hide();
163 sublistLineEditPointer->hide();
164 appliedEntryListLabelPointer->hide();
165 appliedEntryListLineEditPointer->hide();
166 originalEntryLabelPointer->hide();
167 originalEntryLineEditPointer->hide();
172 case FilterListHelper::ALLOWED:
174 // Colorize the disposition line edit background.
175 dispositionLineEditPointer->setPalette(positiveBackgroundPalette);
178 horizontalLinePointer->show();
179 filterListLabelPointer->show();
180 filterListLineEditPointer->show();
181 sublistLabelPointer->show();
182 sublistLineEditPointer->show();
183 appliedEntryListLabelPointer->show();
184 appliedEntryListLineEditPointer->show();
185 originalEntryLabelPointer->show();
186 originalEntryLineEditPointer->show();
191 case FilterListHelper::BLOCKED:
193 // Colorize the disposition line edit background.
194 dispositionLineEditPointer->setPalette(negativeBackgroundPalette);
197 horizontalLinePointer->show();
198 filterListLabelPointer->show();
199 filterListLineEditPointer->show();
200 sublistLabelPointer->show();
201 sublistLineEditPointer->show();
202 appliedEntryListLabelPointer->show();
203 appliedEntryListLineEditPointer->show();
204 originalEntryLabelPointer->show();
205 originalEntryLineEditPointer->show();
212 void RequestDetailDialog::previous()
214 // Update the current row.
217 // Populate the dialog.
218 populateDialog(currentRow);
221 void RequestDetailDialog::next()
223 // Update the current row.
226 // Populate the dialog.
227 populateDialog(currentRow);