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 "sieveconditiondate.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 <KLineEditEventHandler> 0013 #include <KLocalizedString> 0014 #include <QLineEdit> 0015 0016 #include "libksieveui_debug.h" 0017 #include <QHBoxLayout> 0018 #include <QLabel> 0019 0020 using namespace KSieveUi; 0021 0022 SieveConditionDate::SieveConditionDate(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent) 0023 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("date"), i18n("Date"), parent) 0024 { 0025 } 0026 0027 QWidget *SieveConditionDate::createParamWidget(QWidget *parent) const 0028 { 0029 auto w = new QWidget(parent); 0030 auto lay = new QHBoxLayout; 0031 lay->setContentsMargins({}); 0032 w->setLayout(lay); 0033 0034 auto matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget); 0035 matchTypeCombo->setObjectName(QLatin1StringView("matchtype")); 0036 connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionDate::valueChanged); 0037 lay->addWidget(matchTypeCombo); 0038 0039 auto grid = new QGridLayout; 0040 grid->setContentsMargins({}); 0041 lay->addLayout(grid); 0042 auto lab = new QLabel(i18n("header:")); 0043 grid->addWidget(lab, 0, 0); 0044 0045 auto header = new QLineEdit; 0046 KLineEditEventHandler::catchReturnKey(header); 0047 connect(header, &QLineEdit::textChanged, this, &SieveConditionDate::valueChanged); 0048 header->setObjectName(QLatin1StringView("header")); 0049 grid->addWidget(header, 0, 1); 0050 0051 auto dateWidget = new SelectDateWidget; 0052 connect(dateWidget, &SelectDateWidget::valueChanged, this, &SieveConditionDate::valueChanged); 0053 dateWidget->setObjectName(QLatin1StringView("datewidget")); 0054 grid->addWidget(dateWidget, 1, 0, 1, 2); 0055 0056 return w; 0057 } 0058 0059 QString SieveConditionDate::code(QWidget *w) const 0060 { 0061 const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype")); 0062 bool isNegative = false; 0063 const QString matchTypeStr = selectMatchCombobox->code(isNegative); 0064 0065 const QLineEdit *header = w->findChild<QLineEdit *>(QStringLiteral("header")); 0066 const QString headerStr = header->text(); 0067 0068 const SelectDateWidget *dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget")); 0069 const QString dateWidgetStr = dateWidget->code(); 0070 0071 return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("date %1 \"%2\" %3").arg(matchTypeStr, headerStr, dateWidgetStr) 0072 + AutoCreateScriptUtil::generateConditionComment(comment()); 0073 } 0074 0075 bool SieveConditionDate::needCheckIfServerHasCapability() const 0076 { 0077 return true; 0078 } 0079 0080 QString SieveConditionDate::serverNeedsCapability() const 0081 { 0082 return QStringLiteral("date"); 0083 } 0084 0085 QStringList SieveConditionDate::needRequires(QWidget *w) const 0086 { 0087 const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype")); 0088 return QStringList() << QStringLiteral("date") << selectMatchCombobox->needRequires(); 0089 } 0090 0091 QString SieveConditionDate::help() const 0092 { 0093 return i18n("The date test matches date/time information derived from headers containing date-time values."); 0094 } 0095 0096 void SieveConditionDate::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error) 0097 { 0098 int index = 0; 0099 QString type; 0100 QString value; 0101 QString headerStr; 0102 QString commentStr; 0103 while (element.readNextStartElement()) { 0104 const QStringView tagName = element.name(); 0105 if (tagName == QLatin1StringView("str")) { 0106 if (index == 0) { 0107 headerStr = element.readElementText(); 0108 } else if (index == 1) { 0109 type = element.readElementText(); 0110 } else if (index == 2) { 0111 value = element.readElementText(); 0112 } else { 0113 tooManyArguments(tagName, index, 3, error); 0114 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionDate::setParamWidgetValue too many argument :" << index; 0115 } 0116 ++index; 0117 } else if (tagName == QLatin1StringView("tag")) { 0118 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype")); 0119 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error); 0120 } else if (tagName == QLatin1StringView("crlf")) { 0121 element.skipCurrentElement(); 0122 // nothing 0123 } else if (tagName == QLatin1StringView("comment")) { 0124 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText()); 0125 } else { 0126 unknownTag(tagName, error); 0127 qCDebug(LIBKSIEVEUI_LOG) << "SieveConditionDate::setParamWidgetValue unknown tag " << tagName; 0128 } 0129 } 0130 if (!commentStr.isEmpty()) { 0131 setComment(commentStr); 0132 } 0133 0134 auto dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget")); 0135 dateWidget->setCode(type, value); 0136 auto header = w->findChild<QLineEdit *>(QStringLiteral("header")); 0137 header->setText(headerStr); 0138 } 0139 0140 QUrl SieveConditionDate::href() const 0141 { 0142 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name())); 0143 } 0144 0145 #include "moc_sieveconditiondate.cpp"