sieveconditionheader.cpp 7.03 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2013-2017 Laurent Montel <montel@kde.org>
3

Laurent Montel's avatar
Laurent Montel committed
4 5 6 7
   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.
8

Laurent Montel's avatar
Laurent Montel committed
9 10 11 12
   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.
13

Laurent Montel's avatar
Laurent Montel committed
14 15 16 17
   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
*/
#include "sieveconditionheader.h"
#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
#include "autocreatescripts/autocreatescriptutil_p.h"
#include "widgets/selectheadertypecombobox.h"
23
#include "editor/sieveeditorutil.h"
24 25 26 27 28 29

#include <KLocalizedString>
#include <QLineEdit>

#include <QHBoxLayout>
#include <QLabel>
Laurent Montel's avatar
Laurent Montel committed
30
#include <QXmlStreamReader>
31 32 33 34
#include "libksieve_debug.h"

using namespace KSieveUi;

35 36
SieveConditionHeader::SieveConditionHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
    : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("header"), i18n("Header"), parent)
37 38 39 40 41 42 43 44 45 46
{
}

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

Laurent Montel's avatar
Laurent Montel committed
47
    SelectMatchTypeComboBox *matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    matchTypeCombo->setObjectName(QStringLiteral("matchtypecombobox"));
    connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
    lay->addWidget(matchTypeCombo);

    QGridLayout *grid = new QGridLayout;
    lay->addLayout(grid);

    SelectHeaderTypeComboBox *headerType = new SelectHeaderTypeComboBox;
    headerType->setObjectName(QStringLiteral("headertype"));
    connect(headerType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
    grid->addWidget(headerType, 0, 0, 1, 2);

    QLabel *lab = new QLabel(i18n("With value:"));
    grid->addWidget(lab, 1, 0);

63 64 65
    AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
    connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionHeader::valueChanged);
    connect(matchTypeCombo, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
66 67 68 69 70 71 72 73 74 75 76 77 78 79
    value->setObjectName(QStringLiteral("value"));
    grid->addWidget(value, 1, 1);
    return w;
}

QString SieveConditionHeader::code(QWidget *w) const
{
    const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
    bool isNegative = false;
    const QString matchString = matchTypeCombo->code(isNegative);

    const SelectHeaderTypeComboBox *headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
    const QString headerStr = headerType->code();

80 81
    const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
    QString valueStr = edit->code();
82 83

    valueStr = AutoCreateScriptUtil::fixListValue(valueStr);
84
    return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("header %1 %2 %3").arg(matchString, headerStr, valueStr)
Laurent Montel's avatar
Laurent Montel committed
85
           + AutoCreateScriptUtil::generateConditionComment(comment());
86 87 88 89 90 91 92
}

QString SieveConditionHeader::help() const
{
    return i18n("The \"header\" test evaluates to true if the value of any of the named headers, ignoring leading and trailing whitespace, matches any key.");
}

Laurent Montel's avatar
Laurent Montel committed
93
bool SieveConditionHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
94
{
Laurent Montel's avatar
Porting  
Laurent Montel committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    int index = 0;
    QString commentStr;
    while (element.readNextStartElement()) {
        const QStringRef tagName = element.name();

        if (tagName == QLatin1String("tag")) {
            const QString tagValue = element.readElementText();
            if (tagValue == QLatin1String("comparator")) {
                qCWarning(LIBKSIEVE_LOG) << " comparator support not implemented yet!";
            } else {
                SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
                selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition), name(), error);
            }
        } else if (tagName == QLatin1String("str")) {
            if (index == 0) {
                SelectHeaderTypeComboBox *headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
                headerType->setCode(element.readElementText());
            } else if (index == 1) {
                AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
                QString st = AutoCreateScriptUtil::quoteStr(element.readElementText(), true);
                value->setCode(st);
            } else {
                tooManyArgument(tagName, index, 2, error);
                qCDebug(LIBKSIEVE_LOG) << " SieveConditionHeader::setParamWidgetValue too many argument " << index;
            }
            ++index;
        } else if (tagName == QLatin1String("list")) {
            //Header list
            if (index == 0) {
                SelectHeaderTypeComboBox *headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
                headerType->setCode(AutoCreateScriptUtil::listValueToStr(element));
            } else if (index == 1) {
                AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
                value->setCode(AutoCreateScriptUtil::listValueToStr(element));
            } else {
                tooManyArgument(tagName, index, 2, error);
                qCDebug(LIBKSIEVE_LOG) << " SieveConditionHeader::setParamWidgetValue too many argument " << index;
            }
            ++index;
        } else if (tagName == QLatin1String("crlf")) {
Laurent Montel's avatar
Laurent Montel committed
135
            element.skipCurrentElement();
Laurent Montel's avatar
Porting  
Laurent Montel committed
136 137 138 139 140 141 142 143 144 145 146 147
            //nothing
        } else if (tagName == QLatin1String("comment")) {
            commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
        } else {
            unknownTag(tagName, error);
            qCDebug(LIBKSIEVE_LOG) << " SieveConditionHeader::setParamWidgetValue unknown tagName " << tagName;
        }
    }
    if (!commentStr.isEmpty()) {
        setComment(commentStr);
    }

148 149
    return true;
}
Laurent Montel's avatar
Laurent Montel committed
150 151 152 153 154 155

QStringList KSieveUi::SieveConditionHeader::needRequires(QWidget *w) const
{
    const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
    return matchTypeCombo->needRequires();
}