sieveconditionhasflag.cpp 7.21 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 33 34
*/
#include "sieveconditionhasflag.h"
#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
#include "autocreatescripts/autocreatescriptutil_p.h"
#include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
#include "editor/sieveeditorutil.h"

#include <KLocalizedString>
#include <QLineEdit>

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

using namespace KSieveUi;
35 36
SieveConditionHasFlag::SieveConditionHasFlag(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
    : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("hasflag"), i18n("Has Flag"), parent)
37
{
Laurent Montel's avatar
Laurent Montel committed
38
    hasVariableSupport = sieveCapabilities().contains(QStringLiteral("variables"));
39 40 41 42 43 44 45 46
}

QWidget *SieveConditionHasFlag::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 *selecttype = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    selecttype->setObjectName(QStringLiteral("matchtype"));
    connect(selecttype, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionHasFlag::valueChanged);
    lay->addWidget(selecttype);

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

    int row = 0;
    if (hasVariableSupport) {
        QLabel *lab = new QLabel(i18n("Variable name\n (if empty it uses internal variable):"));
        grid->addWidget(lab, row, 0);

        QLineEdit *variableName = new QLineEdit;
        variableName->setObjectName(QStringLiteral("variablename"));
        connect(variableName, &QLineEdit::textChanged, this, &SieveConditionHasFlag::valueChanged);
        grid->addWidget(variableName, row, 1);
        ++row;
    }
    QLabel *lab = new QLabel(i18n("Value:"));
    grid->addWidget(lab, row, 0);

    QLineEdit *value = new QLineEdit;
    connect(value, &QLineEdit::textChanged, this, &SieveConditionHasFlag::valueChanged);
    value->setObjectName(QStringLiteral("value"));
    grid->addWidget(value, row, 1);

    return w;
}

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

    QString result = AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("hasflag %1").arg(matchString);

    if (hasVariableSupport) {
        const QLineEdit *variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
        const QString variableNameStr = variableName->text();
        if (!variableNameStr.isEmpty()) {
            result += QLatin1String(" \"") + variableNameStr + QLatin1Char('"');
        }

        const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
        const QString valueStr = value->text();
        result += QLatin1String(" \"") + valueStr + QLatin1Char('"');
    }
97
    return result + AutoCreateScriptUtil::generateConditionComment(comment());
98 99
}

Laurent Montel's avatar
Laurent Montel committed
100
QStringList SieveConditionHasFlag::needRequires(QWidget *w) const
101 102
{
    QStringList lst;
Laurent Montel's avatar
Laurent Montel committed
103
    if (sieveCapabilities().contains(QStringLiteral("imap4flags"))) {
104 105 106 107 108 109 110
        lst << QStringLiteral("imap4flags");
    } else {
        lst << QStringLiteral("imapflags");
    }
    if (hasVariableSupport) {
        lst << QStringLiteral("variables");
    }
Laurent Montel's avatar
Laurent Montel committed
111 112
    const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
    lst << matchTypeCombo->needRequires();
113 114 115 116 117 118 119 120 121 122
    return lst;
}

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

QString SieveConditionHasFlag::serverNeedsCapability() const
{
123 124 125 126
    return QStringLiteral("variables");
#if 0
    if (sieveCapabilities().contains(QStringLiteral("variables"))) {
    }
Laurent Montel's avatar
Laurent Montel committed
127
    if (sieveCapabilities().contains(QStringLiteral("imap4flags"))) {
128 129 130 131
        return QStringLiteral("imap4flags");
    } else {
        return QStringLiteral("imapflags");
    }
132
#endif
133 134 135 136 137 138 139 140 141 142 143
}

QString SieveConditionHasFlag::help() const
{
    return i18n("The hasflag test evaluates to true if any of the variables matches any flag name.");
}

bool SieveConditionHasFlag::setParamWidgetValue(const QDomElement &element, QWidget *w, bool notCondition, QString &error)
{
    QStringList strList;
    QDomNode node = element.firstChild();
144
    QString commentStr;
145 146 147 148 149 150 151 152 153 154 155 156
    while (!node.isNull()) {
        QDomElement e = node.toElement();
        if (!e.isNull()) {
            const QString tagName = e.tagName();
            if (tagName == QLatin1String("tag")) {
                SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
                matchTypeCombo->setCode(AutoCreateScriptUtil::tagValueWithCondition(e.text(), notCondition), name(), error);
            } else if (tagName == QLatin1String("str")) {
                strList << e.text();
            } else if (tagName == QLatin1String("crlf")) {
                //nothing
            } else if (tagName == QLatin1String("comment")) {
157
                commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, e.text());
158 159 160 161 162 163 164
            } else {
                unknownTag(tagName, error);
                qCDebug(LIBKSIEVE_LOG) << " SieveConditionExists::setParamWidgetValue unknown tagName " << tagName;
            }
        }
        node = node.nextSibling();
    }
165 166 167 168
    if (!commentStr.isEmpty()) {
        setComment(commentStr);
    }

169
    switch (strList.count()) {
Laurent Montel's avatar
Laurent Montel committed
170 171
    case 1:
    {
172 173 174 175
        QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
        value->setText(strList.at(0));
        break;
    }
Laurent Montel's avatar
Laurent Montel committed
176
    case 2:
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        if (hasVariableSupport) {
            QLineEdit *variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
            variableName->setText(strList.at(0));
            QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
            value->setText(strList.at(1));
        } else {
            qCDebug(LIBKSIEVE_LOG) << " SieveConditionHasFlag has not variable support";
        }
        break;
    default:
        qCDebug(LIBKSIEVE_LOG) << " SieveConditionHasFlag::setParamWidgetValue str list count not correct :" << strList.count();
        break;
    }
    return true;
}

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