File indexing completed on 2024-12-29 04:54:43

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "sieveconditionsize.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "editor/sieveeditorutil.h"
0009 #include "widgets/selectsizewidget.h"
0010 #include <KLocalizedString>
0011 
0012 #include "libksieveui_debug.h"
0013 #include <QComboBox>
0014 #include <QHBoxLayout>
0015 #include <QXmlStreamReader>
0016 
0017 using namespace KSieveUi;
0018 
0019 SieveConditionSize::SieveConditionSize(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0020     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("size"), i18n("Size"), parent)
0021 {
0022 }
0023 
0024 QWidget *SieveConditionSize::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 combo = new QComboBox;
0032     combo->setObjectName(QLatin1StringView("combosize"));
0033     combo->addItem(i18n("under"), QStringLiteral(":under"));
0034     combo->addItem(i18n("over"), QStringLiteral(":over"));
0035     lay->addWidget(combo);
0036     connect(combo, &QComboBox::activated, this, &SieveConditionSize::valueChanged);
0037 
0038     auto sizeWidget = new SelectSizeWidget;
0039     connect(sizeWidget, &SelectSizeWidget::valueChanged, this, &SieveConditionSize::valueChanged);
0040     sizeWidget->setObjectName(QLatin1StringView("sizewidget"));
0041     lay->addWidget(sizeWidget);
0042 
0043     return w;
0044 }
0045 
0046 QString SieveConditionSize::code(QWidget *w) const
0047 {
0048     const QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("combosize"));
0049     const QString comparison = combo->itemData(combo->currentIndex()).toString();
0050     const SelectSizeWidget *sizeWidget = w->findChild<SelectSizeWidget *>(QStringLiteral("sizewidget"));
0051     return QStringLiteral("size %1 %2").arg(comparison, sizeWidget->code()) + AutoCreateScriptUtil::generateConditionComment(comment());
0052 }
0053 
0054 QString SieveConditionSize::help() const
0055 {
0056     return i18n(
0057         "The \"size\" test deals with the size of a message.  It takes either a tagged argument of \":over\" or \":under\", followed by a number representing "
0058         "the size of the message.");
0059 }
0060 
0061 void SieveConditionSize::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
0062 {
0063     QString commentStr;
0064     while (element.readNextStartElement()) {
0065         const QStringView tagName = element.name();
0066         if (tagName == QLatin1StringView("tag")) {
0067             const QString tagValue = element.readElementText();
0068             auto combo = w->findChild<QComboBox *>(QStringLiteral("combosize"));
0069             const int index = combo->findData(AutoCreateScriptUtil::tagValue(tagValue));
0070             if (index != -1) {
0071                 combo->setCurrentIndex(index);
0072             }
0073         } else if (tagName == QLatin1StringView("num")) {
0074             QString numIdentifier;
0075             if (element.attributes().hasAttribute(QLatin1StringView("quantifier"))) {
0076                 numIdentifier = element.attributes().value(QLatin1StringView("quantifier")).toString();
0077             }
0078             const qlonglong tagValue = element.readElementText().toLongLong();
0079             auto sizeWidget = w->findChild<SelectSizeWidget *>(QStringLiteral("sizewidget"));
0080             sizeWidget->setCode(tagValue, numIdentifier, name(), error);
0081         } else if (tagName == QLatin1StringView("crlf")) {
0082             element.skipCurrentElement();
0083             // nothing
0084         } else if (tagName == QLatin1StringView("comment")) {
0085             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0086         } else {
0087             unknownTag(tagName, error);
0088             qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionSize::setParamWidgetValue unknown tagName " << tagName;
0089         }
0090     }
0091     if (!commentStr.isEmpty()) {
0092         setComment(commentStr);
0093     }
0094 }
0095 
0096 QUrl SieveConditionSize::href() const
0097 {
0098     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0099 }
0100 
0101 #include "moc_sieveconditionsize.cpp"