File indexing completed on 2024-12-29 04:54:41
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "sieveconditionaddress.h" 0007 #include "autocreatescripts/autocreatescriptutil_p.h" 0008 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h" 0009 #include "editor/sieveeditorutil.h" 0010 #include "widgets/selectaddresspartcombobox.h" 0011 #include "widgets/selectheadertypecombobox.h" 0012 0013 #include <KLocalizedString> 0014 0015 #include "libksieveui_debug.h" 0016 #include <QHBoxLayout> 0017 #include <QLabel> 0018 #include <QXmlStreamReader> 0019 0020 using namespace KSieveUi; 0021 0022 SieveConditionAddress::SieveConditionAddress(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent) 0023 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("address"), i18n("Address"), parent) 0024 { 0025 } 0026 0027 QWidget *SieveConditionAddress::createParamWidget(QWidget *parent) const 0028 { 0029 auto w = new QWidget(parent); 0030 auto lay = new QHBoxLayout; 0031 lay->setContentsMargins({}); 0032 w->setLayout(lay); 0033 0034 auto selectAddressPart = new SelectAddressPartComboBox(mSieveGraphicalModeWidget); 0035 connect(selectAddressPart, &SelectAddressPartComboBox::valueChanged, this, &SieveConditionAddress::valueChanged); 0036 selectAddressPart->setObjectName(QLatin1StringView("addresspartcombobox")); 0037 lay->addWidget(selectAddressPart); 0038 0039 auto grid = new QGridLayout; 0040 grid->setContentsMargins({}); 0041 lay->addLayout(grid); 0042 auto selectMatchCombobox = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget); 0043 connect(selectMatchCombobox, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionAddress::valueChanged); 0044 selectMatchCombobox->setObjectName(QLatin1StringView("matchtypecombobox")); 0045 grid->addWidget(selectMatchCombobox, 0, 0); 0046 0047 auto selectHeaderType = new SelectHeaderTypeComboBox(true); 0048 connect(selectHeaderType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionAddress::valueChanged); 0049 selectHeaderType->setObjectName(QLatin1StringView("headertypecombobox")); 0050 grid->addWidget(selectHeaderType, 0, 1); 0051 0052 auto lab = new QLabel(i18n("address:")); 0053 grid->addWidget(lab, 1, 0); 0054 0055 AbstractRegexpEditorLineEdit *edit = AutoCreateScriptUtil::createRegexpEditorLineEdit(); 0056 connect(edit, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionAddress::valueChanged); 0057 connect(selectMatchCombobox, &SelectMatchTypeComboBox::switchToRegexp, edit, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit); 0058 edit->setClearButtonEnabled(true); 0059 edit->setPlaceholderText(i18n("Use ; to separate emails")); 0060 grid->addWidget(edit, 1, 1); 0061 edit->setObjectName(QLatin1StringView("editaddress")); 0062 return w; 0063 } 0064 0065 QString SieveConditionAddress::code(QWidget *w) const 0066 { 0067 const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox")); 0068 bool isNegative = false; 0069 const QString matchTypeStr = selectMatchCombobox->code(isNegative); 0070 0071 const SelectAddressPartComboBox *selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox")); 0072 const QString selectAddressPartStr = selectAddressPart->code(); 0073 0074 const SelectHeaderTypeComboBox *selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox")); 0075 const QString selectHeaderTypeStr = selectHeaderType->code(); 0076 0077 const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress")); 0078 const QString addressStr = AutoCreateScriptUtil::createAddressList(edit->code().trimmed(), false); 0079 return AutoCreateScriptUtil::negativeString(isNegative) 0080 + QStringLiteral("address %1 %2 %3 %4").arg(selectAddressPartStr, matchTypeStr, selectHeaderTypeStr, addressStr) 0081 + AutoCreateScriptUtil::generateConditionComment(comment()); 0082 } 0083 0084 QStringList SieveConditionAddress::needRequires(QWidget *w) const 0085 { 0086 const SelectAddressPartComboBox *selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox")); 0087 const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox")); 0088 0089 return selectAddressPart->extraRequire() + selectMatchCombobox->needRequires(); 0090 } 0091 0092 QString SieveConditionAddress::help() const 0093 { 0094 return i18n( 0095 "The \"address\" test matches Internet addresses in structured headers that contain addresses. It returns true if any header contains any key in the " 0096 "specified part of the address, as modified by the comparator and the match keyword."); 0097 } 0098 0099 void SieveConditionAddress::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error) 0100 { 0101 int index = 0; 0102 int indexStr = 0; 0103 QStringList lstTagValue; 0104 QString commentStr; 0105 while (element.readNextStartElement()) { 0106 const QStringView tagName = element.name(); 0107 if (tagName == QLatin1StringView("tag")) { 0108 lstTagValue << element.readElementText(); 0109 ++index; 0110 } else if (tagName == QLatin1StringView("str")) { 0111 if (indexStr == 0) { 0112 auto selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox")); 0113 selectHeaderType->setCode(element.readElementText()); 0114 } else if (indexStr == 1) { 0115 auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress")); 0116 edit->setCode(AutoCreateScriptUtil::quoteStr(element.readElementText())); 0117 } else { 0118 tooManyArguments(tagName, indexStr, 2, error); 0119 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionAddress::setParamWidgetValue too many argument :" << index; 0120 } 0121 ++indexStr; 0122 } else if (tagName == QLatin1StringView("list")) { 0123 if (indexStr == 0) { 0124 auto selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox")); 0125 selectHeaderType->setCode(AutoCreateScriptUtil::listValueToStr(element)); 0126 } else if (indexStr == 1) { 0127 auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress")); 0128 edit->setCode(AutoCreateScriptUtil::listValueToStr(element)); 0129 } else { 0130 tooManyArguments(tagName, indexStr, 2, error); 0131 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionAddress::setParamWidgetValue too many argument :" << index; 0132 } 0133 ++indexStr; 0134 } else if (tagName == QLatin1StringView("crlf")) { 0135 element.skipCurrentElement(); 0136 // nothing 0137 } else if (tagName == QLatin1StringView("comment")) { 0138 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText()); 0139 } else { 0140 unknownTag(tagName, error); 0141 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionAddress::setParamWidgetValue unknown tagName " << tagName; 0142 } 0143 } 0144 if (!commentStr.isEmpty()) { 0145 setComment(commentStr); 0146 } 0147 if (lstTagValue.count() == 1) { 0148 QString specificError; 0149 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox")); 0150 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(lstTagValue.at(0), notCondition), name(), specificError); 0151 if (!specificError.isEmpty()) { // Test if default match type == is 0152 auto selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox")); 0153 selectAddressPart->setCode(AutoCreateScriptUtil::tagValue(lstTagValue.at(0)), name(), error); 0154 } 0155 } else if (lstTagValue.count() == 2) { 0156 QString errorStr; 0157 auto selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox")); 0158 selectAddressPart->setCode(AutoCreateScriptUtil::tagValue(lstTagValue.at(0)), name(), errorStr); 0159 if (errorStr.isEmpty()) { 0160 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox")); 0161 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(lstTagValue.at(1), notCondition), name(), error); 0162 } else { // Problem with order 0163 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox")); 0164 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(lstTagValue.at(0), notCondition), name(), error); 0165 selectAddressPart->setCode(AutoCreateScriptUtil::tagValue(lstTagValue.at(1)), name(), error); 0166 } 0167 } else if (lstTagValue.count() > 2) { 0168 tooManyArguments(QStringLiteral("tag"), lstTagValue.count(), 2, error); 0169 qCDebug(LIBKSIEVEUI_LOG) << "SieveConditionAddress::setParamWidgetValue too many argument :" << lstTagValue.count(); 0170 } 0171 } 0172 0173 QUrl SieveConditionAddress::href() const 0174 { 0175 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name())); 0176 } 0177 0178 #include "moc_sieveconditionaddress.cpp"