]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/RequestDetailDialog.cpp
Finish block list implementation.
[PrivacyBrowserPC.git] / src / dialogs / RequestDetailDialog.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 "RequestDetailDialog.h"
22 #include "GlobalVariables.h"
23 #include "ui_RequestDetailDialog.h"
24 #include "structs/RequestStruct.h"
25
26 // KDE Frameworks headers.
27 #include <KActionCollection>
28 #include <KColorScheme>
29
30 // Qt toolkit headers.
31 #include <QShortcut>
32
33 // Construct the class.
34 RequestDetailDialog::RequestDetailDialog(QWidget *parentWidgetPointer, QTableWidget *tableWidgetPointer, const int initialRow) :
35                                          QDialog(parentWidgetPointer), currentRow(initialRow), tableWidgetPointer(tableWidgetPointer)
36 {
37     // Set the window modality.
38     setWindowModality(Qt::WindowModality::ApplicationModal);
39
40     // Instantiate the request detail dialog UI.
41     Ui::RequestDetailDialog requestDetailDialogUi;
42
43     // Setup the UI.
44     requestDetailDialogUi.setupUi(this);
45
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);
65
66     // Make the close button the default.
67     closeButtonPointer->setDefault(true);
68
69     // Get the disposition line edit palettes.
70     normalBackgroundPalette = dispositionLineEditPointer->palette();
71     negativeBackgroundPalette = normalBackgroundPalette;
72     positiveBackgroundPalette = normalBackgroundPalette;
73
74     // Modify the palettes.
75     KColorScheme::adjustBackground(negativeBackgroundPalette, KColorScheme::NegativeBackground);
76     KColorScheme::adjustBackground(positiveBackgroundPalette, KColorScheme::PositiveBackground);
77
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()));
82
83     // Create the keyboard shortcuts.
84     QShortcut *previousShortcutPointer = new QShortcut(Qt::Key_Left, this);
85     QShortcut *nextShortcutPointer = new QShortcut(Qt::Key_Right, this);
86
87     // Connect the keyboard shortcuts to the buttons.
88     connect(previousShortcutPointer, SIGNAL(activated()), previousButtonPointer, SLOT(click()));
89     connect(nextShortcutPointer, SIGNAL(activated()), nextButtonPointer, SLOT(click()));
90
91     // Populate the dialog.
92     populateDialog(currentRow);
93 }
94
95 void RequestDetailDialog::populateDialog(const int row)
96 {
97     // Set the window title.
98     setWindowTitle(i18nc("The request detail dialog window title", "Request %1 Detail", row + 1));
99
100     // Select the row in the table widget (this displays the correct row highlighted in the background of the dialog).
101     tableWidgetPointer->selectRow(row);
102
103     // Get the first table widget item in the row.
104     QTableWidgetItem *rowFirstTableWidgetItemPointer = tableWidgetPointer->item(row, 0);
105
106     // Get the data variant.
107     QVariant dataVariant = rowFirstTableWidgetItemPointer->data(Qt::UserRole);
108
109     // Get the request struct byte array from the data variant.
110     QByteArray requestStructByteArray = dataVariant.toByteArray();
111
112     // Determine if previous should be enabled.
113     bool previousEnabled = (row > 0);
114
115     // Determine if next should be enabled.
116     bool nextEnabled = (row < (tableWidgetPointer->rowCount() - 1));
117
118     // Create a request struct data stream reader.
119     QDataStream requestStructDataStreamReader(requestStructByteArray);
120
121     // Create a new request struct.
122     RequestStruct *requestStructPointer = new RequestStruct();
123
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;
134
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);
145
146     // Set the button status.
147     previousButtonPointer->setEnabled(previousEnabled);
148     nextButtonPointer->setEnabled(nextEnabled);
149
150     // Modify the interface based on the disposition.
151     switch (requestStructPointer->dispositionInt)
152     {
153         case FilterListHelper::DEFAULT:
154         {
155             // Reset the disposition line edit background.
156             dispositionLineEditPointer->setPalette(normalBackgroundPalette);
157
158             // Hide the views.
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();
168
169             break;
170         }
171
172         case FilterListHelper::ALLOWED:
173         {
174             // Colorize the disposition line edit background.
175             dispositionLineEditPointer->setPalette(positiveBackgroundPalette);
176
177             // Show the views.
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();
187
188             break;
189         }
190
191         case FilterListHelper::BLOCKED:
192         {
193             // Colorize the disposition line edit background.
194             dispositionLineEditPointer->setPalette(negativeBackgroundPalette);
195
196             // Show the views.
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();
206
207             break;
208         }
209     }
210 }
211
212 void RequestDetailDialog::previous()
213 {
214     // Update the current row.
215     --currentRow;
216
217     // Populate the dialog.
218     populateDialog(currentRow);
219 }
220
221 void RequestDetailDialog::next()
222 {
223     // Update the current row.
224     ++currentRow;
225
226     // Populate the dialog.
227     populateDialog(currentRow);
228 }