File indexing completed on 2024-06-16 07:47:19

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2010-2012 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 "addeditfilter.h"
0010 
0011 #include <QComboBox>
0012 #include <QDialogButtonBox>
0013 #include <QPushButton>
0014 
0015 #include <KLocalizedString>
0016 
0017 #include "filtersettings.h"
0018 
0019 AddEditFilter::AddEditFilter(QWidget *parent, Filter *filter)
0020     : QDialog(parent), currentFilter(filter)
0021 {
0022     ui.setupUi(this);
0023 
0024     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0025     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0026     okButton->setDefault(true);
0027     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0028     connect(buttonBox, &QDialogButtonBox::accepted, this, &AddEditFilter::accept);
0029     connect(buttonBox, &QDialogButtonBox::rejected, this, &AddEditFilter::reject);
0030     ui.formLayout->addWidget(buttonBox);
0031 
0032     connect(ui.filterAction, (void (QComboBox::*)(int))&QComboBox::currentIndexChanged,
0033             this, &AddEditFilter::slotFilterActionChanged);
0034 
0035     setupFilterFields();
0036     setupFilterTypes();
0037     setupFilterActions();
0038 
0039     if (filter) {
0040         //qDebug() << filter->filterField();
0041         //Editing
0042         ui.filterField->setCurrentIndex(ui.filterField->findData(filter->filterField()));
0043         ui.filterType->setCurrentIndex(ui.filterType->findData(filter->filterType()));
0044         ui.filterAction->setCurrentIndex(ui.filterAction->findData(filter->filterAction()));
0045         ui.filterText->setText(filter->filterText());
0046         ui.dontHideReplies->setChecked(filter->dontHideReplies());
0047         setWindowTitle(i18n("Modify filter rules"));
0048     }
0049     ui.filterText->setFocus();
0050 
0051 }
0052 
0053 AddEditFilter::~AddEditFilter()
0054 {
0055 }
0056 
0057 void AddEditFilter::accept()
0058 {
0059     Filter::FilterField field =
0060         (Filter::FilterField) ui.filterField->itemData(ui.filterField->currentIndex()).toInt();
0061     Filter::FilterType type =
0062         (Filter::FilterType) ui.filterType->itemData(ui.filterType->currentIndex()).toInt();
0063     Filter::FilterAction action =
0064         (Filter::FilterAction) ui.filterAction->itemData(ui.filterAction->currentIndex()).toInt();
0065     QString fText = ui.filterText->text();
0066     bool dontHideReplies = ui.dontHideReplies->isChecked();
0067     if (currentFilter) {
0068         currentFilter->setFilterField(field);
0069         currentFilter->setFilterText(fText);
0070         currentFilter->setFilterType(type);
0071         currentFilter->setFilterAction(action);
0072         currentFilter->setDontHideReplies(dontHideReplies);
0073         Q_EMIT filterUpdated(currentFilter);
0074     } else {
0075         currentFilter = new Filter(fText, field, type, action, dontHideReplies);
0076         Q_EMIT newFilterRegistered(currentFilter);
0077     }
0078     QDialog::accept();
0079 }
0080 
0081 void AddEditFilter::slotFilterActionChanged(int index)
0082 {
0083     ui.dontHideReplies->setVisible((Filter::FilterAction)ui.filterAction->itemData(index).toInt() == Filter::Remove);
0084 }
0085 
0086 void AddEditFilter::setupFilterFields()
0087 {
0088     const QMap<Filter::FilterField, QString> fields = FilterSettings::filterFieldsMap();
0089     for (const Filter::FilterField &field: fields.keys()) {
0090         ui.filterField->addItem(fields.value(field), field);
0091     }
0092 }
0093 
0094 void AddEditFilter::setupFilterTypes()
0095 {
0096     const QMap<Filter::FilterType, QString> types = FilterSettings::filterTypesMap();
0097     for (const Filter::FilterType &type: types.keys()) {
0098         ui.filterType->addItem(types.value(type), type);
0099     }
0100 }
0101 
0102 void AddEditFilter::setupFilterActions()
0103 {
0104     const QMap<Filter::FilterAction, QString> actions = FilterSettings::filterActionsMap();
0105     for (const Filter::FilterAction &action: actions.keys()) {
0106         ui.filterAction->addItem(actions.value(action), action);
0107     }
0108 }
0109 
0110 #include "moc_addeditfilter.cpp"