]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/FilterEntryDialog.cpp
703ca0417daa2f41300aa4894c73cba365c81061
[PrivacyBrowserPC.git] / src / dialogs / FilterEntryDialog.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 "FilterEntryDialog.h"
22 #include "GlobalVariables.h"
23 #include "ui_FilterEntryDialog.h"
24
25 // KDE Framework headers.
26 #include <KColorScheme>
27
28 // Qt toolkit headers.
29 #include <QShortcut>
30
31 // Construct the class.
32 FilterEntryDialog::FilterEntryDialog(QWidget *parentWidgetPointer, QTableWidget *tableWidgetPointer, const int initialRow, const QString &filterListTitle, const QString &sublistTitle) :
33                                      QDialog(parentWidgetPointer), currentRow(initialRow), tableWidgetPointer(tableWidgetPointer)
34 {
35     // Set the window modality.
36     setWindowModality(Qt::WindowModality::ApplicationModal);
37
38     // Instantiate the filter entry dialog UI.
39     Ui::FilterEntryDialog filterEntryDialogUi;
40
41     // Setup the UI.
42     filterEntryDialogUi.setupUi(this);
43
44     // Get handles for the views.
45     QLineEdit *filterListLineEditPointer = filterEntryDialogUi.filterListLineEdit;
46     QLineEdit *sublistLineEditPointer = filterEntryDialogUi.sublistListLineEdit;
47     appliedEntryListLineEditPointer = filterEntryDialogUi.appliedEntryListLineEdit;
48     initialMatchLineEditPointer = filterEntryDialogUi.initialMatchLineEdit;
49     finalMatchLineEditPointer = filterEntryDialogUi.finalMatchLineEdit;
50     domainLineEditPointer = filterEntryDialogUi.domainLineEdit;
51     domainListLineEditPointer = filterEntryDialogUi.domainListLineEdit;
52     thirdPartyLineEditPointer = filterEntryDialogUi.thirdPartyLineEdit;
53     hasRequestOptionsCheckBoxPointer = filterEntryDialogUi.hasRequestOptionsCheckBox;
54     fontLineEditPointer = filterEntryDialogUi.fontLineEdit;
55     imageLineEditPointer = filterEntryDialogUi.imageLineEdit;
56     mainFrameLineEditPointer = filterEntryDialogUi.mainFrameLineEdit;
57     mediaLineEditPointer = filterEntryDialogUi.mediaLineEdit;
58     objectLineEditPointer = filterEntryDialogUi.objectLineEdit;
59     otherLineEditPointer = filterEntryDialogUi.otherLineEdit;
60     pingLineEditPointer = filterEntryDialogUi.pingLineEdit;
61     scriptLineEditPointer = filterEntryDialogUi.scriptLineEdit;
62     styleSheetLineEditPointer = filterEntryDialogUi.styleSheetLineEdit;
63     subFrameLineEditPointer = filterEntryDialogUi.subFrameLineEdit;
64     xmlHttpRequestLineEditPointer = filterEntryDialogUi.xmlHttpRequestLineEdit;
65     appliedFilterOptionsLineEditPointer = filterEntryDialogUi.appliedFilterOptionsLineEdit;
66     originalFilterOptionsLineEditPointer = filterEntryDialogUi.originalFilterOptionsLineEdit;
67     originalEntryLineEditPointer = filterEntryDialogUi.originalEntryLineEdit;
68     previousButtonPointer = filterEntryDialogUi.previousButton;
69     nextButtonPointer = filterEntryDialogUi.nextButton;
70     QDialogButtonBox *dialogButtonBoxPointer = filterEntryDialogUi.dialogButtonBox;
71     QPushButton *closeButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::StandardButton::Close);
72
73     // Populate the views.
74     filterListLineEditPointer->setText(filterListTitle);
75     sublistLineEditPointer->setText(sublistTitle);
76
77     // Disable changing the checkbox checked status.
78     hasRequestOptionsCheckBoxPointer->setAttribute(Qt::WA_TransparentForMouseEvents);
79
80     // Make the close button the default.
81     closeButtonPointer->setDefault(true);
82
83     // Get the disposition line edit palettes.
84     normalBackgroundPalette = appliedEntryListLineEditPointer->palette();
85     negativeBackgroundPalette = normalBackgroundPalette;
86     positiveBackgroundPalette = normalBackgroundPalette;
87
88     // Modify the palettes.
89     KColorScheme::adjustBackground(negativeBackgroundPalette, KColorScheme::NegativeBackground);
90     KColorScheme::adjustBackground(positiveBackgroundPalette, KColorScheme::PositiveBackground);
91
92     // Set the sublist background palette.  TODO  Add logic for allow lists.
93     sublistLineEditPointer->setPalette(negativeBackgroundPalette);
94
95     // Set the applied entry background palette to be the same as the sublist.
96     appliedEntryListLineEditPointer->setPalette(sublistLineEditPointer->palette());
97
98     // Connect the buttons.
99     connect(previousButtonPointer, SIGNAL(clicked()), this, SLOT(previous()));
100     connect(nextButtonPointer, SIGNAL(clicked()), this, SLOT(next()));
101     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(close()));
102
103     // Create the keyboard shortcuts.
104     QShortcut *previousShortcutPointer = new QShortcut(Qt::Key_Left, this);
105     QShortcut *nextShortcutPointer = new QShortcut(Qt::Key_Right, this);
106
107     // Connect the keyboard shortcuts to the buttons.
108     connect(previousShortcutPointer, SIGNAL(activated()), previousButtonPointer, SLOT(click()));
109     connect(nextShortcutPointer, SIGNAL(activated()), nextButtonPointer, SLOT(click()));
110
111     // Populate the dialog.
112     populateDialog(currentRow);
113 }
114
115 void FilterEntryDialog::populateDialog(const int row)
116 {
117     // Set the window title.
118     setWindowTitle(i18nc("The filter entry dialog window title", "Filter Entry %1 Detail", row + 1));
119
120     // Select the row in the table widget (this displays the correct row highlighted in the background of the dialog).
121     tableWidgetPointer->selectRow(row);
122
123     // Determine if previous should be enabled.
124     bool previousEnabled = (row > 0);
125
126     // Determine if next should be enabled.
127     bool nextEnabled = (row < (tableWidgetPointer->rowCount() - 1));
128
129     // Populate the line edits.
130     appliedEntryListLineEditPointer->setText(tableWidgetPointer->item(row, 0)->text());
131     initialMatchLineEditPointer->setText(tableWidgetPointer->item(row, 1)->text());
132     finalMatchLineEditPointer->setText(tableWidgetPointer->item(row, 2)->text());
133     domainLineEditPointer->setText(tableWidgetPointer->item(row, 3)->text());
134     domainListLineEditPointer->setText(tableWidgetPointer->item(row, 4)->text());
135     thirdPartyLineEditPointer->setText(tableWidgetPointer->item(row, 5)->text());
136     fontLineEditPointer->setText(tableWidgetPointer->item(row, 7)->text());
137     imageLineEditPointer->setText(tableWidgetPointer->item(row, 8)->text());
138     mainFrameLineEditPointer->setText(tableWidgetPointer->item(row, 9)->text());
139     mediaLineEditPointer->setText(tableWidgetPointer->item(row, 10)->text());
140     objectLineEditPointer->setText(tableWidgetPointer->item(row, 11)->text());
141     otherLineEditPointer->setText(tableWidgetPointer->item(row, 12)->text());
142     pingLineEditPointer->setText(tableWidgetPointer->item(row, 13)->text());
143     scriptLineEditPointer->setText(tableWidgetPointer->item(row, 14)->text());
144     styleSheetLineEditPointer->setText(tableWidgetPointer->item(row, 15)->text());
145     subFrameLineEditPointer->setText(tableWidgetPointer->item(row, 16)->text());
146     xmlHttpRequestLineEditPointer->setText(tableWidgetPointer->item(row, 17)->text());
147     appliedFilterOptionsLineEditPointer->setText(tableWidgetPointer->item(row, 18)->text());
148     originalFilterOptionsLineEditPointer->setText(tableWidgetPointer->item(row, 19)->text());
149     originalEntryLineEditPointer->setText(tableWidgetPointer->item(row, 20)->text());
150
151     // Populate the check boxes.
152     hasRequestOptionsCheckBoxPointer->setChecked(tableWidgetPointer->item(row, 6)->text() == i18n("Yes"));
153
154     // Set the initial and final match background palettes.
155     setInitialAndFinalMatchBackgroundPalette(initialMatchLineEditPointer);
156     setInitialAndFinalMatchBackgroundPalette(finalMatchLineEditPointer);
157
158     // Set the request option background palettes and status.
159     setFilterOptionBackgroundPalette(domainLineEditPointer);
160     setFilterOptionBackgroundPalette(thirdPartyLineEditPointer);
161     setFilterOptionBackgroundPalette(fontLineEditPointer);
162     setFilterOptionBackgroundPalette(imageLineEditPointer);
163     setFilterOptionBackgroundPalette(mainFrameLineEditPointer);
164     setFilterOptionBackgroundPalette(mediaLineEditPointer);
165     setFilterOptionBackgroundPalette(objectLineEditPointer);
166     setFilterOptionBackgroundPalette(otherLineEditPointer);
167     setFilterOptionBackgroundPalette(pingLineEditPointer);
168     setFilterOptionBackgroundPalette(scriptLineEditPointer);
169     setFilterOptionBackgroundPalette(styleSheetLineEditPointer);
170     setFilterOptionBackgroundPalette(subFrameLineEditPointer);
171     setFilterOptionBackgroundPalette(xmlHttpRequestLineEditPointer);
172
173     // Set the request option status.
174     fontLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
175     imageLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
176     mainFrameLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
177     mediaLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
178     objectLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
179     otherLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
180     pingLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
181     scriptLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
182     styleSheetLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
183     subFrameLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
184     xmlHttpRequestLineEditPointer->setEnabled(hasRequestOptionsCheckBoxPointer->isChecked());
185
186     // Set the domain list line edit to have the same palette as the domain line edit.
187     domainListLineEditPointer->setPalette(domainLineEditPointer->palette());
188
189     // Set the button status.
190     previousButtonPointer->setEnabled(previousEnabled);
191     nextButtonPointer->setEnabled(nextEnabled);
192 }
193
194 void FilterEntryDialog::previous()
195 {
196     // Update the current row.
197     --currentRow;
198
199     // Populate the dialog.
200     populateDialog(currentRow);
201 }
202
203 void FilterEntryDialog::next()
204 {
205     // Update the current row.
206     ++currentRow;
207
208     // Populate the dialog.
209     populateDialog(currentRow);
210 }
211
212 void FilterEntryDialog::setFilterOptionBackgroundPalette(QLineEdit *lineEditPointer)
213 {
214     // Set the background palette according to the text.
215     if (lineEditPointer->text() == globalFilterListHelperPointer->getRequestOptionDispositionString(FilterOptionEnum::Disposition::Null))  // Not text is displayed.
216     {
217         // Set the normal background palette.
218         lineEditPointer->setPalette(normalBackgroundPalette);
219     }
220     else if (lineEditPointer->text() == globalFilterListHelperPointer->getRequestOptionDispositionString(FilterOptionEnum::Disposition::Apply))  // `Apply` is displayed.
221     {
222         // Set the negative (red) background palette.
223         lineEditPointer->setPalette(negativeBackgroundPalette);
224     }
225     else  // `Override` is displayed.
226     {
227         // Set the positive (green) background palette.
228         lineEditPointer->setPalette(positiveBackgroundPalette);
229     }
230 }
231
232 void FilterEntryDialog::setInitialAndFinalMatchBackgroundPalette(QLineEdit *lineEditPointer)
233 {
234     // Set the background palette according to the text.
235     if (lineEditPointer->text() == i18n("Yes"))  // `Yes` is displayed.
236     {
237         // Set the negative (red) background palette.
238         lineEditPointer->setPalette(negativeBackgroundPalette);
239     }
240     else  // No text is displayed.
241     {
242         // Set the normal background palette.
243         lineEditPointer->setPalette(normalBackgroundPalette);
244     }
245 }