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 "sieveconditionenvironment.h" 0007 #include "autocreatescripts/autocreatescriptutil_p.h" 0008 #include "editor/sieveeditorutil.h" 0009 0010 #include <KLineEditEventHandler> 0011 #include <KLocalizedString> 0012 #include <QLineEdit> 0013 0014 #include "libksieveui_debug.h" 0015 #include <QCompleter> 0016 #include <QGridLayout> 0017 #include <QLabel> 0018 #include <QWidget> 0019 #include <QXmlStreamReader> 0020 0021 using namespace KSieveUi; 0022 SieveConditionEnvironment::SieveConditionEnvironment(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent) 0023 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("environment"), i18n("Environment"), parent) 0024 { 0025 } 0026 0027 QWidget *SieveConditionEnvironment::createParamWidget(QWidget *parent) const 0028 { 0029 auto w = new QWidget(parent); 0030 auto grid = new QGridLayout; 0031 grid->setContentsMargins({}); 0032 w->setLayout(grid); 0033 auto lab = new QLabel(i18n("Item:")); 0034 grid->addWidget(lab, 0, 0); 0035 0036 auto item = new QLineEdit; 0037 KLineEditEventHandler::catchReturnKey(item); 0038 QStringList itemList; 0039 itemList << QStringLiteral("domain") << QStringLiteral("host") << QStringLiteral("location") << QStringLiteral("name") << QStringLiteral("phase") 0040 << QStringLiteral("remote-host") << QStringLiteral("remote-ip") << QStringLiteral("version"); 0041 auto completer = new QCompleter(itemList, w); 0042 completer->setCaseSensitivity(Qt::CaseInsensitive); 0043 item->setCompleter(completer); 0044 connect(item, &QLineEdit::textChanged, this, &SieveConditionEnvironment::valueChanged); 0045 0046 item->setObjectName(QLatin1StringView("item")); 0047 grid->addWidget(item, 0, 1); 0048 0049 lab = new QLabel(i18n("Value:")); 0050 grid->addWidget(lab, 1, 0); 0051 0052 auto value = new QLineEdit; 0053 KLineEditEventHandler::catchReturnKey(value); 0054 connect(value, &QLineEdit::textChanged, this, &SieveConditionEnvironment::valueChanged); 0055 value->setObjectName(QLatin1StringView("value")); 0056 grid->addWidget(value, 1, 1); 0057 0058 return w; 0059 } 0060 0061 QString SieveConditionEnvironment::code(QWidget *w) const 0062 { 0063 const QLineEdit *item = w->findChild<QLineEdit *>(QStringLiteral("item")); 0064 const QString itemStr = item->text(); 0065 0066 const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value")); 0067 const QString valueStr = value->text(); 0068 0069 return QStringLiteral("environment \"%1\" \"%2\"").arg(itemStr, valueStr) + AutoCreateScriptUtil::generateConditionComment(comment()); 0070 } 0071 0072 QStringList SieveConditionEnvironment::needRequires(QWidget *) const 0073 { 0074 return QStringList() << QStringLiteral("environment"); 0075 } 0076 0077 bool SieveConditionEnvironment::needCheckIfServerHasCapability() const 0078 { 0079 return true; 0080 } 0081 0082 QString SieveConditionEnvironment::serverNeedsCapability() const 0083 { 0084 return QStringLiteral("environment"); 0085 } 0086 0087 QString SieveConditionEnvironment::help() const 0088 { 0089 return i18n( 0090 "The environment test retrieves the item of environment information specified by the name string and matches it to the values specified in the " 0091 "key-list argument."); 0092 } 0093 0094 void SieveConditionEnvironment::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error) 0095 { 0096 int index = 0; 0097 QString commentStr; 0098 while (element.readNextStartElement()) { 0099 const QStringView tagName = element.name(); 0100 if (tagName == QLatin1StringView("str")) { 0101 if (index == 0) { 0102 auto item = w->findChild<QLineEdit *>(QStringLiteral("item")); 0103 item->setText(AutoCreateScriptUtil::quoteStr(element.readElementText())); 0104 } else if (index == 1) { 0105 auto value = w->findChild<QLineEdit *>(QStringLiteral("value")); 0106 value->setText(AutoCreateScriptUtil::quoteStr(element.readElementText())); 0107 } else { 0108 tooManyArguments(tagName, index, 2, error); 0109 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionEnvironment::setParamWidgetValue to many argument " << index; 0110 } 0111 ++index; 0112 } else if (tagName == QLatin1StringView("crlf")) { 0113 element.skipCurrentElement(); 0114 // nothing 0115 } else if (tagName == QLatin1StringView("comment")) { 0116 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText()); 0117 } else { 0118 unknownTag(tagName, error); 0119 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionSetVariable::setParamWidgetValue unknown tagName " << tagName; 0120 } 0121 } 0122 if (!commentStr.isEmpty()) { 0123 setComment(commentStr); 0124 } 0125 } 0126 0127 QUrl KSieveUi::SieveConditionEnvironment::href() const 0128 { 0129 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name())); 0130 } 0131 0132 #include "moc_sieveconditionenvironment.cpp"