File indexing completed on 2024-11-24 04:43:06

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "confirmbeforedeletingcreaterulewidget.h"
0008 #include "confirmbeforedeletingrule.h"
0009 #include <KLocalizedString>
0010 #include <QComboBox>
0011 #include <QHBoxLayout>
0012 #include <QLabel>
0013 #include <QLineEdit>
0014 
0015 ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingCreateRuleWidget(QWidget *parent)
0016     : QWidget(parent)
0017     , mPatternLineEdit(new QLineEdit(this))
0018     , mRuleTypeComboBox(new QComboBox(this))
0019 {
0020     auto mainLayout = new QHBoxLayout(this);
0021     mainLayout->setContentsMargins({});
0022     mainLayout->setObjectName(QLatin1StringView("mainLayout"));
0023 
0024     auto label = new QLabel(i18n("Type:"), this);
0025     label->setObjectName(QLatin1StringView("typeLabel"));
0026     mainLayout->addWidget(label);
0027 
0028     mRuleTypeComboBox->setObjectName(QLatin1StringView("mRuleTypeComboBox"));
0029     mainLayout->addWidget(mRuleTypeComboBox);
0030 
0031     label = new QLabel(i18n("Contains:"), this);
0032     label->setObjectName(QLatin1StringView("containsLabel"));
0033     mainLayout->addWidget(label);
0034 
0035     mPatternLineEdit->setObjectName(QLatin1StringView("mPatternLineEdit"));
0036     mPatternLineEdit->setClearButtonEnabled(true);
0037     mainLayout->addWidget(mPatternLineEdit);
0038     fillComboBox();
0039     connect(mPatternLineEdit, &QLineEdit::textChanged, this, [this](const QString &str) {
0040         Q_EMIT updateOkButton(!str.trimmed().isEmpty());
0041     });
0042     connect(mRuleTypeComboBox, &QComboBox::currentIndexChanged, this, &ConfirmBeforeDeletingCreateRuleWidget::slotRuleTypeChanged);
0043 }
0044 
0045 ConfirmBeforeDeletingCreateRuleWidget::~ConfirmBeforeDeletingCreateRuleWidget() = default;
0046 
0047 ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo ConfirmBeforeDeletingCreateRuleWidget::info() const
0048 {
0049     const ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo info(mPatternLineEdit->text(), mRuleTypeComboBox->currentData().toString());
0050     return info;
0051 }
0052 
0053 void ConfirmBeforeDeletingCreateRuleWidget::slotRuleTypeChanged(int index)
0054 {
0055     const QString str = mRuleTypeComboBox->itemData(index).toString();
0056     const bool isAStatus = (str == QLatin1StringView("unread") || str == QLatin1StringView("important"));
0057     mPatternLineEdit->setEnabled(!isAStatus);
0058     if (isAStatus) {
0059         mPatternLineEdit->clear();
0060         Q_EMIT updateOkButton(true);
0061     } else {
0062         Q_EMIT updateOkButton(!mPatternLineEdit->text().trimmed().isEmpty());
0063     }
0064 }
0065 
0066 void ConfirmBeforeDeletingCreateRuleWidget::setInfo(const ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo &info)
0067 {
0068     mPatternLineEdit->setText(info.pattern);
0069     mRuleTypeComboBox->setCurrentIndex(mRuleTypeComboBox->findData(info.ruleType));
0070 }
0071 
0072 void ConfirmBeforeDeletingCreateRuleWidget::fillComboBox()
0073 {
0074     mRuleTypeComboBox->addItem(i18n("Body"), ConfirmBeforeDeletingRule::ruleTypeToString(ConfirmBeforeDeletingRule::Body));
0075     mRuleTypeComboBox->addItem(i18n("Subject"), ConfirmBeforeDeletingRule::ruleTypeToString(ConfirmBeforeDeletingRule::Subject));
0076     mRuleTypeComboBox->addItem(i18n("To"), ConfirmBeforeDeletingRule::ruleTypeToString(ConfirmBeforeDeletingRule::To));
0077     mRuleTypeComboBox->addItem(i18n("Cc"), ConfirmBeforeDeletingRule::ruleTypeToString(ConfirmBeforeDeletingRule::Cc));
0078     mRuleTypeComboBox->addItem(i18n("Bcc"), ConfirmBeforeDeletingRule::ruleTypeToString(ConfirmBeforeDeletingRule::Bcc));
0079     mRuleTypeComboBox->addItem(i18n("Unread"), ConfirmBeforeDeletingRule::ruleTypeToString(ConfirmBeforeDeletingRule::Unread));
0080     mRuleTypeComboBox->addItem(i18n("Important"), ConfirmBeforeDeletingRule::ruleTypeToString(ConfirmBeforeDeletingRule::Important));
0081 }
0082 
0083 bool ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo::operator==(const ConfirmBeforeDeletingInfo &other) const
0084 {
0085     return other.pattern != pattern && other.ruleType != ruleType;
0086 }
0087 
0088 bool ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo::operator!=(const ConfirmBeforeDeletingInfo &other) const
0089 {
0090     return !operator==(other);
0091 }
0092 
0093 #include "moc_confirmbeforedeletingcreaterulewidget.cpp"