sieveconditionbody.cpp 7.11 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2013-2017 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
*/
#include "sieveconditionbody.h"
#include "widgets/selectbodytypewidget.h"
#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
#include "autocreatescripts/autocreatescriptutil_p.h"
#include "editor/sieveeditorutil.h"

#include <KLocalizedString>
#include <QLineEdit>

#include <QWidget>
#include <QHBoxLayout>
#include "libksieve_debug.h"

using namespace KSieveUi;
33 34
SieveConditionBody::SieveConditionBody(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
    : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("body"), i18n("Body"), parent)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
{
}

QWidget *SieveConditionBody::createParamWidget(QWidget *parent) const
{
    QWidget *w = new QWidget(parent);
    QHBoxLayout *lay = new QHBoxLayout;
    lay->setMargin(0);
    w->setLayout(lay);

    SelectBodyTypeWidget *bodyType = new SelectBodyTypeWidget;
    bodyType->setObjectName(QStringLiteral("bodytype"));
    connect(bodyType, &SelectBodyTypeWidget::valueChanged, this, &SieveConditionBody::valueChanged);
    lay->addWidget(bodyType);

Laurent Montel's avatar
Laurent Montel committed
50
    SelectMatchTypeComboBox *matchType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    lay->addWidget(matchType);
    matchType->setObjectName(QStringLiteral("matchtype"));
    connect(matchType, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionBody::valueChanged);

    QLineEdit *edit = new QLineEdit;
    connect(edit, &QLineEdit::textChanged, this, &SieveConditionBody::valueChanged);
    edit->setClearButtonEnabled(true);
    lay->addWidget(edit);
    edit->setObjectName(QStringLiteral("edit"));

    return w;
}

QString SieveConditionBody::code(QWidget *w) const
{
Laurent Montel's avatar
Laurent Montel committed
66
    const SelectBodyTypeWidget *bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
67 68 69 70 71 72
    const QString bodyValue = bodyType->code();
    const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
    bool isNegative = false;
    const QString matchValue = matchType->code(isNegative);

    QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
73
    const QString editValue = AutoCreateScriptUtil::createAddressList(edit->text().trimmed(), false);
74
    return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("body %1 %2 %3").arg(bodyValue, matchValue, editValue)
Laurent Montel's avatar
Laurent Montel committed
75
           + AutoCreateScriptUtil::generateConditionComment(comment());
76 77
}

Laurent Montel's avatar
Laurent Montel committed
78
QStringList SieveConditionBody::needRequires(QWidget *w) const
79
{
Laurent Montel's avatar
Laurent Montel committed
80 81 82
    const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));

    return QStringList() << QStringLiteral("body") << matchType->needRequires();
83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

bool SieveConditionBody::needCheckIfServerHasCapability() const
{
    return true;
}

QString SieveConditionBody::serverNeedsCapability() const
{
    return QStringLiteral("body");
}

QString SieveConditionBody::help() const
{
Laurent Montel's avatar
Laurent Montel committed
97 98
    return i18n(
        "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 itself, if present, is not considered to be part of the body.)");
99 100 101 102 103 104 105 106 107
}

bool SieveConditionBody::setParamWidgetValue(const QDomElement &element, QWidget *w, bool notCondition, QString &error)
{
    int index = 0;
    int indexStr = 0;
    QStringList tagValueList;
    QStringList strValue;

108
    bool wasListElement = false;
109
    QDomNode node = element.firstChild();
110
    QString commentStr;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    while (!node.isNull()) {
        QDomElement e = node.toElement();
        if (!e.isNull()) {
            const QString tagName = e.tagName();
            if (tagName == QLatin1String("tag")) {
                const QString tagValue = e.text();
                if (index == 0) {
                    tagValueList << AutoCreateScriptUtil::tagValue(tagValue);
                } else if (index == 1) {
                    tagValueList << AutoCreateScriptUtil::tagValueWithCondition(e.text(), notCondition);
                } else {
                    tooManyArgument(tagName, index, 2, error);
                    qCDebug(LIBKSIEVE_LOG) << " SieveConditionBody::setParamWidgetValue too many argument " << index;
                }
                ++index;
            } else if (tagName == QLatin1String("str")) {
                strValue << e.text();
                ++indexStr;
            } else if (tagName == QLatin1String("crlf")) {
                //nothing
            } else if (tagName == QLatin1String("comment")) {
132
                commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, e.text());
Laurent Montel's avatar
Laurent Montel committed
133 134
            } else if (tagName == QLatin1String("list")) {
                strValue << AutoCreateScriptUtil::listValueToStr(e);
135
                wasListElement = true;
Laurent Montel's avatar
Laurent Montel committed
136
                ++indexStr;
137 138 139 140 141 142 143
            } else {
                unknownTag(tagName, error);
                qCDebug(LIBKSIEVE_LOG) << " SieveConditionBody::setParamWidgetValue unknown tagName " << tagName;
            }
        }
        node = node.nextSibling();
    }
144 145 146
    if (!commentStr.isEmpty()) {
        setComment(commentStr);
    }
147 148

    if (strValue.count() == 1) {
Laurent Montel's avatar
Laurent Montel committed
149
        SelectBodyTypeWidget *bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
150 151 152 153
        bodyType->setCode(tagValueList.at(0), QString(), name(), error);
        SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
        matchType->setCode(tagValueList.at(1), name(), error);
        QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
154
        edit->setText(wasListElement ? strValue.at(0) : AutoCreateScriptUtil::quoteStr(strValue.at(0)));
155
    } else if (strValue.count() == 2) {
Laurent Montel's avatar
Laurent Montel committed
156
        SelectBodyTypeWidget *bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
157 158 159 160 161 162 163 164 165 166 167 168 169
        bodyType->setCode(tagValueList.at(0), indexStr == 2 ? strValue.at(0) : QString(), name(), error);
        SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
        matchType->setCode(tagValueList.at(1), name(), error);
        QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
        edit->setText(indexStr == 1 ? AutoCreateScriptUtil::quoteStr(strValue.at(0)) : AutoCreateScriptUtil::quoteStr(strValue.at(1)));
    }
    return true;
}

QUrl SieveConditionBody::href() const
{
    return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
}