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 "FilterEntryDialog.h"
22 #include "GlobalVariables.h"
23 #include "ui_FilterEntryDialog.h"
25 // KDE Framework headers.
26 #include <KColorScheme>
28 // Qt toolkit headers.
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)
35 // Set the window modality.
36 setWindowModality(Qt::WindowModality::ApplicationModal);
38 // Instantiate the filter entry dialog UI.
39 Ui::FilterEntryDialog filterEntryDialogUi;
42 filterEntryDialogUi.setupUi(this);
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);
73 // Populate the views.
74 filterListLineEditPointer->setText(filterListTitle);
75 sublistLineEditPointer->setText(sublistTitle);
77 // Disable changing the checkbox checked status.
78 hasRequestOptionsCheckBoxPointer->setAttribute(Qt::WA_TransparentForMouseEvents);
80 // Make the close button the default.
81 closeButtonPointer->setDefault(true);
83 // Get the disposition line edit palettes.
84 normalBackgroundPalette = appliedEntryListLineEditPointer->palette();
85 negativeBackgroundPalette = normalBackgroundPalette;
86 positiveBackgroundPalette = normalBackgroundPalette;
88 // Modify the palettes.
89 KColorScheme::adjustBackground(negativeBackgroundPalette, KColorScheme::NegativeBackground);
90 KColorScheme::adjustBackground(positiveBackgroundPalette, KColorScheme::PositiveBackground);
92 // Set the sublist background palette. TODO Add logic for allow lists.
93 sublistLineEditPointer->setPalette(negativeBackgroundPalette);
95 // Set the applied entry background palette to be the same as the sublist.
96 appliedEntryListLineEditPointer->setPalette(sublistLineEditPointer->palette());
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()));
103 // Create the keyboard shortcuts.
104 QShortcut *previousShortcutPointer = new QShortcut(Qt::Key_Left, this);
105 QShortcut *nextShortcutPointer = new QShortcut(Qt::Key_Right, this);
107 // Connect the keyboard shortcuts to the buttons.
108 connect(previousShortcutPointer, SIGNAL(activated()), previousButtonPointer, SLOT(click()));
109 connect(nextShortcutPointer, SIGNAL(activated()), nextButtonPointer, SLOT(click()));
111 // Populate the dialog.
112 populateDialog(currentRow);
115 void FilterEntryDialog::populateDialog(const int row)
117 // Set the window title.
118 setWindowTitle(i18nc("The filter entry dialog window title", "Filter Entry %1 Detail", row + 1));
120 // Select the row in the table widget (this displays the correct row highlighted in the background of the dialog).
121 tableWidgetPointer->selectRow(row);
123 // Determine if previous should be enabled.
124 bool previousEnabled = (row > 0);
126 // Determine if next should be enabled.
127 bool nextEnabled = (row < (tableWidgetPointer->rowCount() - 1));
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());
151 // Populate the check boxes.
152 hasRequestOptionsCheckBoxPointer->setChecked(tableWidgetPointer->item(row, 6)->text() == i18n("Yes"));
154 // Set the initial and final match background palettes.
155 setInitialAndFinalMatchBackgroundPalette(initialMatchLineEditPointer);
156 setInitialAndFinalMatchBackgroundPalette(finalMatchLineEditPointer);
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);
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());
186 // Set the domain list line edit to have the same palette as the domain line edit.
187 domainListLineEditPointer->setPalette(domainLineEditPointer->palette());
189 // Set the button status.
190 previousButtonPointer->setEnabled(previousEnabled);
191 nextButtonPointer->setEnabled(nextEnabled);
194 void FilterEntryDialog::previous()
196 // Update the current row.
199 // Populate the dialog.
200 populateDialog(currentRow);
203 void FilterEntryDialog::next()
205 // Update the current row.
208 // Populate the dialog.
209 populateDialog(currentRow);
212 void FilterEntryDialog::setFilterOptionBackgroundPalette(QLineEdit *lineEditPointer)
214 // Set the background palette according to the text.
215 if (lineEditPointer->text() == globalFilterListHelperPointer->getRequestOptionDispositionString(FilterOptionEnum::Disposition::Null)) // Not text is displayed.
217 // Set the normal background palette.
218 lineEditPointer->setPalette(normalBackgroundPalette);
220 else if (lineEditPointer->text() == globalFilterListHelperPointer->getRequestOptionDispositionString(FilterOptionEnum::Disposition::Apply)) // `Apply` is displayed.
222 // Set the negative (red) background palette.
223 lineEditPointer->setPalette(negativeBackgroundPalette);
225 else // `Override` is displayed.
227 // Set the positive (green) background palette.
228 lineEditPointer->setPalette(positiveBackgroundPalette);
232 void FilterEntryDialog::setInitialAndFinalMatchBackgroundPalette(QLineEdit *lineEditPointer)
234 // Set the background palette according to the text.
235 if (lineEditPointer->text() == i18n("Yes")) // `Yes` is displayed.
237 // Set the negative (red) background palette.
238 lineEditPointer->setPalette(negativeBackgroundPalette);
240 else // No text is displayed.
242 // Set the normal background palette.
243 lineEditPointer->setPalette(normalBackgroundPalette);