File indexing completed on 2025-02-16 04:55:57
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "sieveactionsetvariable.h" 0007 #include "autocreatescripts/autocreatescriptutil_p.h" 0008 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h" 0009 #include "editor/sieveeditorutil.h" 0010 #include "widgets/selectvariablemodifiercombobox.h" 0011 0012 #include <KLineEditEventHandler> 0013 #include <KLocalizedString> 0014 #include <QLineEdit> 0015 0016 #include "libksieveui_debug.h" 0017 #include <QCheckBox> 0018 #include <QGridLayout> 0019 #include <QLabel> 0020 #include <QXmlStreamReader> 0021 0022 using namespace KSieveUi; 0023 SieveActionSetVariable::SieveActionSetVariable(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent) 0024 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("set"), i18n("Variable"), parent) 0025 { 0026 mHasRegexCapability = sieveCapabilities().contains(QLatin1StringView("regex")); 0027 } 0028 0029 QWidget *SieveActionSetVariable::createParamWidget(QWidget *parent) const 0030 { 0031 auto w = new QWidget(parent); 0032 auto grid = new QGridLayout; 0033 grid->setContentsMargins({}); 0034 w->setLayout(grid); 0035 0036 auto modifier = new SelectVariableModifierComboBox; 0037 modifier->setObjectName(QLatin1StringView("modifier")); 0038 connect(modifier, &SelectVariableModifierComboBox::valueChanged, this, &SieveActionSetVariable::valueChanged); 0039 grid->addWidget(modifier, 0, 0); 0040 0041 if (mHasRegexCapability) { 0042 auto protectAgainstUseRegexp = new QCheckBox(i18n("Protect special character")); 0043 connect(protectAgainstUseRegexp, &QCheckBox::clicked, this, &SieveActionSetVariable::valueChanged); 0044 protectAgainstUseRegexp->setObjectName(QLatin1StringView("regexprotect")); 0045 grid->addWidget(protectAgainstUseRegexp, 0, 1); 0046 } 0047 0048 auto lab = new QLabel(i18n("Value:")); 0049 grid->addWidget(lab, 1, 0); 0050 0051 auto value = new QLineEdit; 0052 KLineEditEventHandler::catchReturnKey(value); 0053 value->setObjectName(QLatin1StringView("value")); 0054 connect(value, &QLineEdit::textChanged, this, &SieveActionSetVariable::valueChanged); 0055 grid->addWidget(value, 1, 1); 0056 0057 lab = new QLabel(i18n("In variable:")); 0058 grid->addWidget(lab, 2, 0); 0059 0060 auto variable = new QLineEdit; 0061 KLineEditEventHandler::catchReturnKey(variable); 0062 variable->setObjectName(QLatin1StringView("variable")); 0063 connect(variable, &QLineEdit::textChanged, this, &SieveActionSetVariable::valueChanged); 0064 grid->addWidget(variable, 2, 1); 0065 0066 return w; 0067 } 0068 0069 QUrl SieveActionSetVariable::href() const 0070 { 0071 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name())); 0072 } 0073 0074 void SieveActionSetVariable::setLocalVariable(QWidget *w, const SieveGlobalVariableActionWidget::VariableElement &var) 0075 { 0076 auto value = w->findChild<QLineEdit *>(QStringLiteral("value")); 0077 value->setText(var.variableValue); 0078 auto variable = w->findChild<QLineEdit *>(QStringLiteral("variable")); 0079 variable->setText(AutoCreateScriptUtil::protectSlash(var.variableName)); 0080 } 0081 0082 void SieveActionSetVariable::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error) 0083 { 0084 while (element.readNextStartElement()) { 0085 const QStringView tagName = element.name(); 0086 if (tagName == QLatin1StringView("str")) { 0087 const QString tagValue = element.readElementText(); 0088 auto value = w->findChild<QLineEdit *>(QStringLiteral("value")); 0089 value->setText(tagValue); 0090 if (element.readNextStartElement()) { 0091 const QStringView variableTagName = element.name(); 0092 if (variableTagName == QLatin1StringView("str")) { 0093 auto variable = w->findChild<QLineEdit *>(QStringLiteral("variable")); 0094 variable->setText(AutoCreateScriptUtil::protectSlash(element.readElementText())); 0095 } 0096 } else { 0097 return; 0098 } 0099 } else if (tagName == QLatin1StringView("tag")) { 0100 const QString tagValue = element.readElementText(); 0101 if (tagValue == QLatin1StringView("quoteregex")) { 0102 if (mHasRegexCapability) { 0103 auto protectAgainstUseRegexp = w->findChild<QCheckBox *>(QStringLiteral("regexprotect")); 0104 protectAgainstUseRegexp->setChecked(true); 0105 } else { 0106 error += i18n("Script needs regex support, but server does not have it.") + QLatin1Char('\n'); 0107 } 0108 } else { 0109 auto modifier = w->findChild<SelectVariableModifierComboBox *>(QStringLiteral("modifier")); 0110 modifier->setCode(AutoCreateScriptUtil::tagValue(tagValue), name(), error); 0111 } 0112 } else if (tagName == QLatin1StringView("crlf")) { 0113 element.skipCurrentElement(); 0114 // nothing 0115 } else if (tagName == QLatin1StringView("comment")) { 0116 element.skipCurrentElement(); 0117 // implement in the future ? 0118 } else { 0119 unknownTag(tagName, error); 0120 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionSetVariable::setParamWidgetValue unknown tagName " << tagName; 0121 } 0122 } 0123 } 0124 0125 QString SieveActionSetVariable::code(QWidget *w) const 0126 { 0127 QString result = QStringLiteral("set "); 0128 const SelectVariableModifierComboBox *modifier = w->findChild<SelectVariableModifierComboBox *>(QStringLiteral("modifier")); 0129 const QString modifierStr = modifier->code(); 0130 if (!modifierStr.isEmpty()) { 0131 result += modifierStr + QLatin1Char(' '); 0132 } 0133 0134 if (mHasRegexCapability) { 0135 const QCheckBox *protectAgainstUseRegexp = w->findChild<QCheckBox *>(QStringLiteral("regexprotect")); 0136 if (protectAgainstUseRegexp->isChecked()) { 0137 result += QLatin1StringView(":quoteregex "); 0138 } 0139 } 0140 0141 const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value")); 0142 const QString valueStr = value->text(); 0143 result += QStringLiteral("\"%1\" ").arg(valueStr); 0144 0145 const QLineEdit *variable = w->findChild<QLineEdit *>(QStringLiteral("variable")); 0146 const QString variableStr = variable->text(); 0147 result += QStringLiteral("\"%1\";").arg(variableStr); 0148 0149 return result; 0150 } 0151 0152 QStringList SieveActionSetVariable::needRequires(QWidget *) const 0153 { 0154 return QStringList() << QStringLiteral("variables"); 0155 } 0156 0157 bool SieveActionSetVariable::needCheckIfServerHasCapability() const 0158 { 0159 return true; 0160 } 0161 0162 QString SieveActionSetVariable::serverNeedsCapability() const 0163 { 0164 return QStringLiteral("variables"); 0165 } 0166 0167 QString SieveActionSetVariable::help() const 0168 { 0169 QString helpStr = i18n("The \"set\" action stores the specified value in the variable identified by name."); 0170 if (mHasRegexCapability) { 0171 helpStr += QLatin1Char('\n') 0172 + i18n("This modifier adds the necessary quoting to ensure that the expanded text will only match a literal occurrence if used as a parameter " 0173 "to :regex. Every character with special meaning (. , *, ? , etc.) is prefixed with \\ in the expansion"); 0174 } 0175 return helpStr; 0176 } 0177 0178 #include "moc_sieveactionsetvariable.cpp"