File indexing completed on 2024-05-19 04:58:23

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2009-2010 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "configurefilters.h"
0010 
0011 #include <QCheckBox>
0012 #include <QDialogButtonBox>
0013 #include <QPointer>
0014 #include <QPushButton>
0015 
0016 #include <KLocalizedString>
0017 
0018 #include "addeditfilter.h"
0019 #include "filtersettings.h"
0020 
0021 ConfigureFilters::ConfigureFilters(QWidget *parent):
0022     QDialog(parent)
0023 {
0024     QVBoxLayout *mainLayout = new QVBoxLayout;
0025     setLayout(mainLayout);
0026 
0027     ui.setupUi(this);
0028 
0029     mainLayout->addLayout(ui.horizontalLayout);
0030 
0031     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0032     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0033     okButton->setDefault(true);
0034     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0035     connect(buttonBox, &QDialogButtonBox::accepted, this, &ConfigureFilters::accept);
0036     connect(buttonBox, &QDialogButtonBox::rejected, this, &ConfigureFilters::reject);
0037     mainLayout->addWidget(buttonBox);
0038 
0039     resize(500, 300);
0040 
0041     connect(ui.btnAdd, &QPushButton::clicked, this, &ConfigureFilters::slotAddFilter);
0042     connect(ui.btnEdit, &QPushButton::clicked, this, &ConfigureFilters::slotEditFilter);
0043     connect(ui.btnRemove, &QPushButton::clicked, this, &ConfigureFilters::slotRemoveFilter);
0044     connect(ui.cfg_hideRepliesNotRelatedToMe, &QCheckBox::toggled, this,
0045             &ConfigureFilters::slotHideRepliesNotRelatedToMeToggled);
0046     reloadFiltersTable();
0047 }
0048 
0049 ConfigureFilters::~ConfigureFilters()
0050 {
0051 }
0052 
0053 void ConfigureFilters::accept()
0054 {
0055     saveFiltersTable();
0056     QDialog::accept();
0057 }
0058 
0059 void ConfigureFilters::reloadFiltersTable()
0060 {
0061     ui.filters->clearContents();
0062     QList<Filter *> filters = FilterSettings::self()->filters();
0063     //qDebug() << filters.count();
0064     for (Filter *filter: filters) {
0065         addNewFilter(filter);
0066     }
0067     ui.cfg_hideNoneFriendsReplies->setChecked(FilterSettings::hideNoneFriendsReplies());
0068     ui.cfg_hideRepliesNotRelatedToMe->setChecked(FilterSettings::hideRepliesNotRelatedToMe());
0069 }
0070 
0071 void ConfigureFilters::saveFiltersTable()
0072 {
0073     QList<Filter *> list;
0074     int count = ui.filters->rowCount();
0075     for (int i = 0; i < count; ++i) {
0076         Filter::FilterField field = (Filter::FilterField) ui.filters->item(i, 0)->data(32).toInt();
0077         Filter::FilterType type = (Filter::FilterType) ui.filters->item(i, 1)->data(32).toInt();
0078         Filter::FilterAction action = (Filter::FilterAction) ui.filters->item(i, 3)->data(32).toInt();
0079         QString text = ui.filters->item(i, 2)->text();
0080         bool dontHideReplies = ui.filters->item(i, 2)->data(32).toBool();
0081         Filter *f = new Filter(text, field, type, action, dontHideReplies, FilterSettings::self());
0082         list << f;
0083     }
0084     FilterSettings::self()->setFilters(list);
0085     FilterSettings::setHideNoneFriendsReplies(ui.cfg_hideNoneFriendsReplies->isChecked());
0086     FilterSettings::setHideRepliesNotRelatedToMe(ui.cfg_hideRepliesNotRelatedToMe->isChecked());
0087     FilterSettings::self()->writeConfig();
0088 }
0089 
0090 void ConfigureFilters::slotAddFilter()
0091 {
0092     AddEditFilter *f = new AddEditFilter(this);
0093     connect(f, &AddEditFilter::newFilterRegistered, this, &ConfigureFilters::addNewFilter);
0094     f->show();
0095 }
0096 
0097 void ConfigureFilters::slotEditFilter()
0098 {
0099     if (ui.filters->selectedItems().count() > 0) {
0100         int row = ui.filters->currentRow();
0101         Filter::FilterField field = (Filter::FilterField) ui.filters->item(row, 0)->data(32).toInt();
0102         Filter::FilterType type = (Filter::FilterType) ui.filters->item(row, 1)->data(32).toInt();
0103         Filter::FilterAction action = (Filter::FilterAction) ui.filters->item(row, 3)->data(32).toInt();
0104         bool dontHideReplies = ui.filters->item(row, 2)->data(32).toBool();
0105         QString text = ui.filters->item(row, 2)->text();
0106         Filter *f = new Filter(text, field, type, action, dontHideReplies, this);
0107         QPointer<AddEditFilter> dialog = new AddEditFilter(this, f);
0108         connect(dialog, &AddEditFilter::filterUpdated, this, &ConfigureFilters::slotUpdateFilter);
0109         dialog->exec();
0110     }
0111 }
0112 
0113 void ConfigureFilters::slotRemoveFilter()
0114 {
0115     if (ui.filters->selectedItems().count() > 0) {
0116         int row = ui.filters->currentRow();
0117         ui.filters->removeRow(row);
0118     }
0119 }
0120 
0121 void ConfigureFilters::addNewFilter(Filter *filter)
0122 {
0123     int row = ui.filters->rowCount();
0124     ui.filters->insertRow(row);
0125     QTableWidgetItem *item1 = new QTableWidgetItem(FilterSettings::self()->filterFieldName(filter->filterField()));
0126     item1->setData(32, filter->filterField());
0127     ui.filters->setItem(row, 0, item1);
0128     QTableWidgetItem *item2 = new QTableWidgetItem(FilterSettings::self()->filterTypeName(filter->filterType()));
0129     item2->setData(32, filter->filterType());
0130     ui.filters->setItem(row, 1, item2);
0131     QTableWidgetItem *item3 = new QTableWidgetItem(filter->filterText());
0132     item3->setData(32, filter->dontHideReplies());
0133     ui.filters->setItem(row, 2, item3);
0134     QTableWidgetItem *item4 = new QTableWidgetItem(FilterSettings::self()->filterActionName(filter->filterAction()));
0135     item4->setData(32, filter->filterAction());
0136     ui.filters->setItem(row, 3, item4);
0137 }
0138 
0139 void ConfigureFilters::slotUpdateFilter(Filter *filter)
0140 {
0141     int row = ui.filters->currentRow();
0142     ui.filters->item(row, 0)->setText(FilterSettings::self()->filterFieldName(filter->filterField()));
0143     ui.filters->item(row, 0)->setData(32, filter->filterField());
0144 
0145     ui.filters->item(row, 1)->setText(FilterSettings::self()->filterTypeName(filter->filterType()));
0146     ui.filters->item(row, 1)->setData(32, filter->filterType());
0147 
0148     ui.filters->item(row, 2)->setText(filter->filterText());
0149     ui.filters->item(row, 2)->setData(32, filter->dontHideReplies());
0150 
0151     ui.filters->item(row, 3)->setText(FilterSettings::self()->filterActionName(filter->filterAction()));
0152     ui.filters->item(row, 3)->setData(32, filter->filterAction());
0153 }
0154 
0155 void ConfigureFilters::slotHideRepliesNotRelatedToMeToggled(bool enabled)
0156 {
0157     if (enabled) {
0158         ui.cfg_hideNoneFriendsReplies->setChecked(false);
0159         ui.cfg_hideNoneFriendsReplies->setEnabled(false);
0160     } else {
0161         ui.cfg_hideNoneFriendsReplies->setEnabled(true);
0162         ui.cfg_hideNoneFriendsReplies->setChecked(FilterSettings::hideNoneFriendsReplies());
0163     }
0164 }
0165 
0166 #include "moc_configurefilters.cpp"