X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=src%2Fdialogs%2FFilterListsDialog.cpp;fp=src%2Fdialogs%2FFilterListsDialog.cpp;h=dbba88e25863c6026c03acae73b7df7189fa0933;hb=a44e607fb5398c80c5de2629017865ae749e8fbf;hp=0000000000000000000000000000000000000000;hpb=c5706a6ff3fbc42418e60b79fbe3f5c19396f7d2;p=PrivacyBrowserPC.git diff --git a/src/dialogs/FilterListsDialog.cpp b/src/dialogs/FilterListsDialog.cpp new file mode 100644 index 0000000..dbba88e --- /dev/null +++ b/src/dialogs/FilterListsDialog.cpp @@ -0,0 +1,219 @@ +/* + * Copyright 2024 Soren Stoutner . + * + * This file is part of Privacy Browser PC . + * + * Privacy Browser PC is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser PC is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser PC. If not, see . + */ + +// Application headers. +#include "FilterListsDialog.h" +#include "GlobalVariables.h" +#include "ui_FilterListsDialog.h" + +// Qt toolkit headers. +#include +#include + +// Construct the class. +FilterListsDialog::FilterListsDialog(QWidget *parentWidgetPointer) : QDialog(parentWidgetPointer) +{ + // Set the window title. + setWindowTitle(i18nc("The filter lists dialog window title", "Filter Lists")); + + // Set the window modality. + setWindowModality(Qt::WindowModality::ApplicationModal); + + // Instantiate the filter lists dialog UI. + Ui::FilterListsDialog filterListsDialogUi; + + // Setup the UI. + filterListsDialogUi.setupUi(this); + + // Get handles for the views. + filterListComboBoxPointer = filterListsDialogUi.filterListComboBox; + filterListTextEditPointer = filterListsDialogUi.filterListTextEdit; + sublistComboBoxPointer = filterListsDialogUi.sublistComboBox; + sublistTableWidgetPointer = filterListsDialogUi.sublistTableWidget; + QDialogButtonBox *dialogButtonBoxPointer = filterListsDialogUi.dialogButtonBox; + + // Connect the combo boxes. + connect(filterListComboBoxPointer, SIGNAL(currentIndexChanged(int)), this, SLOT(populateFilterListTextEdit(int))); + connect(sublistComboBoxPointer, SIGNAL(currentIndexChanged(int)), this, SLOT(populateTableWidget(int))); + + // Connect the buttons. + connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(close())); + + // Populate the filter list combo box. + populateFilterListComboBox(globalFilterListHelperPointer->ultraListStructPointer); + populateFilterListComboBox(globalFilterListHelperPointer->ultraPrivacyStructPointer); + populateFilterListComboBox(globalFilterListHelperPointer->easyListStructPointer); + populateFilterListComboBox(globalFilterListHelperPointer->easyPrivacyStructPointer); + populateFilterListComboBox(globalFilterListHelperPointer->fanboyAnnoyanceStructPointer); +} + +void FilterListsDialog::populateFilterListComboBox(const FilterListStruct *filterListStructPointer) const +{ + // Populate the filter list combo box. + filterListComboBoxPointer->addItem(i18nc("Filter list combo box items", "%1 - %2", filterListStructPointer->title, filterListStructPointer->version)); +} + +void FilterListsDialog::populateFilterListTextEdit(int filterListComboBoxInt) const +{ + // Create the filter list struct pointer. + FilterListStruct *filterListStructPointer; + + // Get the indicated filter list map. + switch (filterListComboBoxInt) + { + case 0: // UltraList. + filterListStructPointer = globalFilterListHelperPointer->ultraListStructPointer; + break; + + case 1: // UltraPrivacy. + filterListStructPointer = globalFilterListHelperPointer->ultraPrivacyStructPointer; + break; + + case 2: // EasyList. + filterListStructPointer = globalFilterListHelperPointer->easyListStructPointer; + break; + + case 3: // EasyPrivacy. + filterListStructPointer = globalFilterListHelperPointer->easyPrivacyStructPointer; + break; + + case 4: // Fanboy's Annoyance List. + filterListStructPointer = globalFilterListHelperPointer->fanboyAnnoyanceStructPointer; + break; + } + + // Get the filter list file. + QFile filterListFile(filterListStructPointer->filePath); + + // Open the filter list file. + filterListFile.open(QIODevice::ReadOnly); + + // Create a filter list text stream. + QTextStream filterListTextStream(&filterListFile); + + // Populate the text edit. + filterListTextEditPointer->setText(filterListTextStream.readAll()); + + // Clear the sublist combo box. + sublistComboBoxPointer->clear(); + + // Calculate the size of the main filter list. + int mainBlockListSize = distance(filterListStructPointer->mainBlockList.begin(), filterListStructPointer->mainBlockList.end()); + + // Get the translated filter list names. + QString mainBlockListName = globalFilterListHelperPointer->getSublistName(FilterListHelper::MAIN_BLOCKLIST); + + // Populate the sublist combo box. + sublistComboBoxPointer->addItem(i18nc("The main block list", "%1 - %2", mainBlockListName, mainBlockListSize)); +} + +void FilterListsDialog::populateTableWidget(int sublistComboBoxIndex) const +{ + // Wipe the current table. + sublistTableWidgetPointer->clear(); + sublistTableWidgetPointer->setColumnCount(0); + sublistTableWidgetPointer->setRowCount(0); + + // Create the filter list struct. + FilterListStruct *filterListStructPointer; + + // Populate the filer list array. + switch (filterListComboBoxPointer->currentIndex()) + { + case 0: // UltraList. + filterListStructPointer = globalFilterListHelperPointer->ultraListStructPointer; + break; + + case 1: // UltraPrivacy. + filterListStructPointer = globalFilterListHelperPointer->ultraPrivacyStructPointer; + break; + + case 2: // EasyList. + filterListStructPointer = globalFilterListHelperPointer->easyListStructPointer; + break; + + case 3: // EasyPrivacy. + filterListStructPointer = globalFilterListHelperPointer->easyPrivacyStructPointer; + break; + + case 4: // Fanboy's Annoyance List. + filterListStructPointer = globalFilterListHelperPointer->fanboyAnnoyanceStructPointer; + break; + } + + // Create a filter list forward list. + std::forward_list filterListForwardList; + + // Populate the filter list forward list. + switch (sublistComboBoxIndex) + { + case 0: // Main block list. + filterListForwardList = filterListStructPointer->mainBlockList; + break; + } + + // Create the columns. + sublistTableWidgetPointer->setColumnCount(2); + + // Create the table headers. + QTableWidgetItem *appliedEntryHeaderItemPointer = new QTableWidgetItem(i18nc("Sublist applied entry header", "Applied Entry")); + QTableWidgetItem *originalEntryHeaderItemPointer = new QTableWidgetItem(i18nc("Sublist original entry header", "Original Entry")); + + // Set the horizontal headers. + sublistTableWidgetPointer->setHorizontalHeaderItem(0, appliedEntryHeaderItemPointer); + sublistTableWidgetPointer->setHorizontalHeaderItem(1, originalEntryHeaderItemPointer); + + // Initialize the row counter. + int rowCounter = 0; + + // Populate the table. + for (auto filterListEntry = filterListForwardList.begin(); filterListEntry != filterListForwardList.end(); ++filterListEntry) { + // Get the entry struct. + EntryStruct *entryStructPointer = *filterListEntry; + + // Add a new row. + sublistTableWidgetPointer->insertRow(rowCounter); + + // Create the entry items. + QTableWidgetItem *appliedEntryItemPointer = new QTableWidgetItem(entryStructPointer->appliedEntry); + QTableWidgetItem *originalEntryItemPointer = new QTableWidgetItem(entryStructPointer->originalEntry); + + // Add the entries to the table. + sublistTableWidgetPointer->setItem(rowCounter, 0, appliedEntryItemPointer); + sublistTableWidgetPointer->setItem(rowCounter, 1, originalEntryItemPointer); + + // Increment the row counter. + ++rowCounter; + } + + // Get the table header view. + QHeaderView *headerViewPointer = sublistTableWidgetPointer->horizontalHeader(); + + // Resize the header to fit the contents. + headerViewPointer->resizeSections(QHeaderView::ResizeToContents); + + // Connect changes in the sort order. + connect(headerViewPointer, SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), this, SLOT(sortTable(int, Qt::SortOrder))); +} + +void FilterListsDialog::sortTable(int column, Qt::SortOrder sortOrder) const +{ + // Sort the table. + sublistTableWidgetPointer->sortItems(column, sortOrder); +}