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