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 "sieveconditionbody.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
0009 #include "editor/sieveeditorutil.h"
0010 #include "widgets/selectbodytypewidget.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 #include "libksieveui_debug.h"
0015 #include <QHBoxLayout>
0016 #include <QWidget>
0017 
0018 using namespace KSieveUi;
0019 SieveConditionBody::SieveConditionBody(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0020     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("body"), i18n("Body"), parent)
0021 {
0022 }
0023 
0024 QWidget *SieveConditionBody::createParamWidget(QWidget *parent) const
0025 {
0026     auto w = new QWidget(parent);
0027     auto lay = new QHBoxLayout;
0028     lay->setContentsMargins({});
0029     w->setLayout(lay);
0030 
0031     auto bodyType = new SelectBodyTypeWidget;
0032     bodyType->setObjectName(QLatin1StringView("bodytype"));
0033     connect(bodyType, &SelectBodyTypeWidget::valueChanged, this, &SieveConditionBody::valueChanged);
0034     lay->addWidget(bodyType);
0035 
0036     auto matchType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
0037     lay->addWidget(matchType);
0038     matchType->setObjectName(QLatin1StringView("matchtype"));
0039     connect(matchType, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionBody::valueChanged);
0040 
0041     AbstractRegexpEditorLineEdit *edit = AutoCreateScriptUtil::createRegexpEditorLineEdit();
0042     connect(edit, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionBody::valueChanged);
0043     connect(matchType, &SelectMatchTypeComboBox::switchToRegexp, edit, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
0044     edit->setClearButtonEnabled(true);
0045     lay->addWidget(edit);
0046     edit->setObjectName(QLatin1StringView("edit"));
0047 
0048     return w;
0049 }
0050 
0051 QString SieveConditionBody::code(QWidget *w) const
0052 {
0053     const SelectBodyTypeWidget *bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
0054     const QString bodyValue = bodyType->code();
0055     const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
0056     bool isNegative = false;
0057     const QString matchValue = matchType->code(isNegative);
0058 
0059     const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
0060     const QString editValue = AutoCreateScriptUtil::createAddressList(edit->code().trimmed(), false);
0061     return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("body %1 %2 %3").arg(bodyValue, matchValue, editValue)
0062         + AutoCreateScriptUtil::generateConditionComment(comment());
0063 }
0064 
0065 QStringList SieveConditionBody::needRequires(QWidget *w) const
0066 {
0067     const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
0068 
0069     return QStringList() << QStringLiteral("body") << matchType->needRequires();
0070 }
0071 
0072 bool SieveConditionBody::needCheckIfServerHasCapability() const
0073 {
0074     return true;
0075 }
0076 
0077 QString SieveConditionBody::serverNeedsCapability() const
0078 {
0079     return QStringLiteral("body");
0080 }
0081 
0082 QString SieveConditionBody::help() const
0083 {
0084     return i18n(
0085         "The body test matches content in the body of an email message, that is, anything following the first empty line after the header.  (The empty line "
0086         "itself, if present, is not considered to be part of the body.)");
0087 }
0088 
0089 void SieveConditionBody::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
0090 {
0091     int index = 0;
0092     int indexStr = 0;
0093     QStringList tagValueList;
0094     QStringList strValue;
0095 
0096     bool wasListElement = false;
0097     QString commentStr;
0098     while (element.readNextStartElement()) {
0099         const QStringView tagName = element.name();
0100         if (tagName == QLatin1StringView("tag")) {
0101             const QString tagValue = element.readElementText();
0102             if (index == 0) {
0103                 tagValueList << AutoCreateScriptUtil::tagValue(tagValue);
0104             } else if (index == 1) {
0105                 tagValueList << AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition);
0106             } else {
0107                 tooManyArguments(tagName, index, 2, error);
0108                 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionBody::setParamWidgetValue too many argument " << index;
0109             }
0110             ++index;
0111         } else if (tagName == QLatin1StringView("str")) {
0112             strValue << element.readElementText();
0113             ++indexStr;
0114         } else if (tagName == QLatin1StringView("crlf")) {
0115             element.skipCurrentElement();
0116             // nothing
0117         } else if (tagName == QLatin1StringView("comment")) {
0118             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0119         } else if (tagName == QLatin1StringView("list")) {
0120             strValue << AutoCreateScriptUtil::listValueToStr(element);
0121             wasListElement = true;
0122             ++indexStr;
0123         } else {
0124             unknownTag(tagName, error);
0125             qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionBody::setParamWidgetValue unknown tagName " << tagName;
0126         }
0127     }
0128     if (!commentStr.isEmpty()) {
0129         setComment(commentStr);
0130     }
0131     QString errorStr;
0132     if (strValue.count() == 1) {
0133         auto bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
0134         bodyType->setCode(tagValueList.at(0), QString(), name(), errorStr);
0135         if (errorStr.isEmpty()) {
0136             auto matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
0137             matchType->setCode(tagValueList.at(1), name(), error);
0138         } else {
0139             auto matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
0140             if (tagValueList.count() == 1) {
0141                 matchType->setCode(tagValueList.at(0), name(), error);
0142             } else if (tagValueList.count() == 2) {
0143                 matchType->setCode(tagValueList.at(0), name(), error);
0144                 bodyType->setCode(tagValueList.at(1), QString(), name(), errorStr);
0145             }
0146         }
0147         auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
0148         edit->setCode(wasListElement ? strValue.at(0) : AutoCreateScriptUtil::quoteStr(strValue.at(0)));
0149     } else if (strValue.count() == 2) {
0150         auto bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
0151         bodyType->setCode(tagValueList.at(0), indexStr == 2 ? strValue.at(0) : QString(), name(), errorStr);
0152         auto matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
0153         if (!errorStr.isEmpty()) {
0154             matchType->setCode(tagValueList.at(0), name(), error);
0155             bodyType->setCode(tagValueList.at(1), indexStr == 2 ? strValue.at(0) : QString(), name(), error);
0156         } else {
0157             matchType->setCode(tagValueList.at(1), name(), error);
0158         }
0159         auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
0160         edit->setCode(indexStr == 1 ? AutoCreateScriptUtil::quoteStr(strValue.at(0)) : AutoCreateScriptUtil::quoteStr(strValue.at(1)));
0161     }
0162 }
0163 
0164 QUrl SieveConditionBody::href() const
0165 {
0166     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0167 }
0168 
0169 #include "moc_sieveconditionbody.cpp"