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 "sieveconditioncurrentdate.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
0009 #include "editor/sieveeditorutil.h"
0010 #include "widgets/selectdatewidget.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 #include "libksieveui_debug.h"
0015 #include <QHBoxLayout>
0016 #include <QXmlStreamReader>
0017 
0018 using namespace KSieveUi;
0019 
0020 SieveConditionCurrentDate::SieveConditionCurrentDate(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0021     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("currentdate"), i18n("Currentdate"), parent)
0022 {
0023 }
0024 
0025 QWidget *SieveConditionCurrentDate::createParamWidget(QWidget *parent) const
0026 {
0027     auto w = new QWidget(parent);
0028     auto lay = new QHBoxLayout;
0029     lay->setContentsMargins({});
0030     w->setLayout(lay);
0031 
0032     auto matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
0033     matchTypeCombo->setObjectName(QLatin1StringView("matchtype"));
0034     lay->addWidget(matchTypeCombo);
0035     connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionCurrentDate::valueChanged);
0036 
0037     auto dateWidget = new SelectDateWidget;
0038     connect(dateWidget, &SelectDateWidget::valueChanged, this, &SieveConditionCurrentDate::valueChanged);
0039     dateWidget->setObjectName(QLatin1StringView("datewidget"));
0040     lay->addWidget(dateWidget);
0041 
0042     return w;
0043 }
0044 
0045 QString SieveConditionCurrentDate::code(QWidget *w) const
0046 {
0047     const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
0048     bool isNegative = false;
0049     const QString matchTypeStr = selectMatchCombobox->code(isNegative);
0050 
0051     const SelectDateWidget *dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
0052     const QString dateWidgetStr = dateWidget->code();
0053 
0054     return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("currentdate %1 %2").arg(matchTypeStr, dateWidgetStr)
0055         + AutoCreateScriptUtil::generateConditionComment(comment());
0056 }
0057 
0058 bool SieveConditionCurrentDate::needCheckIfServerHasCapability() const
0059 {
0060     return true;
0061 }
0062 
0063 QString SieveConditionCurrentDate::serverNeedsCapability() const
0064 {
0065     return QStringLiteral("date");
0066 }
0067 
0068 QStringList SieveConditionCurrentDate::needRequires(QWidget *w) const
0069 {
0070     const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
0071 
0072     return QStringList() << QStringLiteral("date") << selectMatchCombobox->needRequires();
0073 }
0074 
0075 QString SieveConditionCurrentDate::help() const
0076 {
0077     return i18n(
0078         "The currentdate test is similar to the date test, except that it operates on the current date/time rather than a value extracted from the message "
0079         "header.");
0080 }
0081 
0082 void SieveConditionCurrentDate::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
0083 {
0084     int index = 0;
0085     QString type;
0086     QString value;
0087     QString commentStr;
0088     while (element.readNextStartElement()) {
0089         const QStringView tagName = element.name();
0090         if (tagName == QLatin1StringView("str")) {
0091             if (index == 0) {
0092                 type = element.readElementText();
0093             } else if (index == 1) {
0094                 value = element.readElementText();
0095             } else {
0096                 tooManyArguments(tagName, index, 2, error);
0097                 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionCurrentDate::setParamWidgetValue too many argument :" << index;
0098             }
0099             ++index;
0100         } else if (tagName == QLatin1StringView("tag")) {
0101             auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
0102             selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
0103         } else if (tagName == QLatin1StringView("crlf")) {
0104             element.skipCurrentElement();
0105             // nothing
0106         } else if (tagName == QLatin1StringView("comment")) {
0107             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0108         } else {
0109             unknownTag(tagName, error);
0110             qCDebug(LIBKSIEVEUI_LOG) << "SieveConditionCurrentDate::setParamWidgetValue unknown tag " << tagName;
0111         }
0112     }
0113     if (!commentStr.isEmpty()) {
0114         setComment(commentStr);
0115     }
0116 
0117     auto dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
0118     dateWidget->setCode(type, value);
0119 }
0120 
0121 QUrl SieveConditionCurrentDate::href() const
0122 {
0123     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0124 }
0125 
0126 #include "moc_sieveconditioncurrentdate.cpp"