File indexing completed on 2024-12-29 04:54:42
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "sieveconditionhasflag.h" 0007 #include "autocreatescripts/autocreatescriptutil_p.h" 0008 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h" 0009 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h" 0010 #include "editor/sieveeditorutil.h" 0011 0012 #include <KLineEditEventHandler> 0013 #include <KLocalizedString> 0014 #include <QLineEdit> 0015 0016 #include "libksieveui_debug.h" 0017 #include <QHBoxLayout> 0018 #include <QLabel> 0019 #include <QWidget> 0020 #include <QXmlStreamReader> 0021 0022 using namespace KSieveUi; 0023 SieveConditionHasFlag::SieveConditionHasFlag(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent) 0024 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("hasflag"), i18n("Has Flag"), parent) 0025 { 0026 hasVariableSupport = sieveCapabilities().contains(QLatin1StringView("variables")); 0027 } 0028 0029 QWidget *SieveConditionHasFlag::createParamWidget(QWidget *parent) const 0030 { 0031 auto w = new QWidget(parent); 0032 auto lay = new QHBoxLayout; 0033 lay->setContentsMargins({}); 0034 w->setLayout(lay); 0035 auto selecttype = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget); 0036 selecttype->setObjectName(QLatin1StringView("matchtype")); 0037 connect(selecttype, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionHasFlag::valueChanged); 0038 lay->addWidget(selecttype); 0039 0040 auto grid = new QGridLayout; 0041 grid->setContentsMargins({}); 0042 lay->addLayout(grid); 0043 0044 int row = 0; 0045 if (hasVariableSupport) { 0046 auto lab = new QLabel(i18n("Variable name\n (if empty it uses internal variable):")); 0047 grid->addWidget(lab, row, 0); 0048 0049 auto variableName = new QLineEdit; 0050 KLineEditEventHandler::catchReturnKey(variableName); 0051 variableName->setObjectName(QLatin1StringView("variablename")); 0052 connect(variableName, &QLineEdit::textChanged, this, &SieveConditionHasFlag::valueChanged); 0053 grid->addWidget(variableName, row, 1); 0054 ++row; 0055 } 0056 auto lab = new QLabel(i18n("Value:")); 0057 grid->addWidget(lab, row, 0); 0058 0059 AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit(); 0060 connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionHasFlag::valueChanged); 0061 connect(selecttype, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit); 0062 value->setObjectName(QLatin1StringView("value")); 0063 grid->addWidget(value, row, 1); 0064 0065 return w; 0066 } 0067 0068 QString SieveConditionHasFlag::code(QWidget *w) const 0069 { 0070 const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype")); 0071 bool isNegative = false; 0072 const QString matchString = matchTypeCombo->code(isNegative); 0073 0074 QString result = AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("hasflag %1").arg(matchString); 0075 0076 if (hasVariableSupport) { 0077 const QLineEdit *variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename")); 0078 const QString variableNameStr = variableName->text(); 0079 if (!variableNameStr.isEmpty()) { 0080 result += QLatin1StringView(" \"") + variableNameStr + QLatin1Char('"'); 0081 } 0082 0083 const AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value")); 0084 const QString valueStr = value->code(); 0085 result += QLatin1StringView(" \"") + valueStr + QLatin1Char('"'); 0086 } 0087 return result + AutoCreateScriptUtil::generateConditionComment(comment()); 0088 } 0089 0090 QStringList SieveConditionHasFlag::needRequires(QWidget *w) const 0091 { 0092 QStringList lst; 0093 if (sieveCapabilities().contains(QLatin1StringView("imap4flags"))) { 0094 lst << QStringLiteral("imap4flags"); 0095 } else { 0096 lst << QStringLiteral("imapflags"); 0097 } 0098 if (hasVariableSupport) { 0099 lst << QStringLiteral("variables"); 0100 } 0101 const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype")); 0102 lst << matchTypeCombo->needRequires(); 0103 return lst; 0104 } 0105 0106 bool SieveConditionHasFlag::needCheckIfServerHasCapability() const 0107 { 0108 return true; 0109 } 0110 0111 QString SieveConditionHasFlag::serverNeedsCapability() const 0112 { 0113 return QStringLiteral("variables"); 0114 #if 0 0115 if (sieveCapabilities().contains(QLatin1StringView("variables"))) { 0116 } 0117 if (sieveCapabilities().contains(QLatin1StringView("imap4flags"))) { 0118 return QStringLiteral("imap4flags"); 0119 } else { 0120 return QStringLiteral("imapflags"); 0121 } 0122 #endif 0123 } 0124 0125 QString SieveConditionHasFlag::help() const 0126 { 0127 return i18n("The hasflag test evaluates to true if any of the variables matches any flag name."); 0128 } 0129 0130 void SieveConditionHasFlag::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error) 0131 { 0132 QStringList strList; 0133 QString commentStr; 0134 while (element.readNextStartElement()) { 0135 const QStringView tagName = element.name(); 0136 if (tagName == QLatin1StringView("tag")) { 0137 auto matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype")); 0138 matchTypeCombo->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error); 0139 } else if (tagName == QLatin1StringView("str")) { 0140 strList << element.readElementText(); 0141 } else if (tagName == QLatin1StringView("crlf")) { 0142 element.skipCurrentElement(); 0143 // nothing 0144 } else if (tagName == QLatin1StringView("comment")) { 0145 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText()); 0146 } else { 0147 unknownTag(tagName, error); 0148 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionExists::setParamWidgetValue unknown tagName " << tagName; 0149 } 0150 } 0151 if (!commentStr.isEmpty()) { 0152 setComment(commentStr); 0153 } 0154 0155 switch (strList.count()) { 0156 case 1: { 0157 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value")); 0158 value->setCode(strList.at(0)); 0159 break; 0160 } 0161 case 2: 0162 if (hasVariableSupport) { 0163 auto variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename")); 0164 variableName->setText(strList.at(0)); 0165 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value")); 0166 value->setCode(strList.at(1)); 0167 } else { 0168 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionHasFlag has not variable support"; 0169 } 0170 break; 0171 default: 0172 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionHasFlag::setParamWidgetValue str list count not correct :" << strList.count(); 0173 break; 0174 } 0175 } 0176 0177 QUrl SieveConditionHasFlag::href() const 0178 { 0179 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name())); 0180 } 0181 0182 #include "moc_sieveconditionhasflag.cpp"