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 "sieveactionreplace.h" 0007 #include "autocreatescripts/autocreatescriptutil_p.h" 0008 #include "editor/sieveeditorutil.h" 0009 #include "widgets/multilineedit.h" 0010 0011 #include <KLineEditEventHandler> 0012 #include <KLocalizedString> 0013 #include <QLineEdit> 0014 0015 #include "libksieveui_debug.h" 0016 #include <QGridLayout> 0017 #include <QLabel> 0018 #include <QXmlStreamReader> 0019 0020 #include <KSieveUi/AbstractSelectEmailLineEdit> 0021 0022 using namespace KSieveUi; 0023 SieveActionReplace::SieveActionReplace(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent) 0024 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("replace"), i18n("Replace"), parent) 0025 { 0026 } 0027 0028 QWidget *SieveActionReplace::createParamWidget(QWidget *parent) const 0029 { 0030 auto w = new QWidget(parent); 0031 auto grid = new QGridLayout; 0032 grid->setContentsMargins({}); 0033 w->setLayout(grid); 0034 0035 auto lab = new QLabel(i18n("Subject:")); 0036 grid->addWidget(lab, 0, 0); 0037 0038 auto subject = new QLineEdit; 0039 KLineEditEventHandler::catchReturnKey(subject); 0040 subject->setObjectName(QLatin1StringView("subject")); 0041 connect(subject, &QLineEdit::textChanged, this, &SieveActionReplace::valueChanged); 0042 grid->addWidget(subject, 0, 1); 0043 0044 lab = new QLabel(i18n("from:")); 0045 grid->addWidget(lab, 1, 0); 0046 0047 KSieveUi::AbstractSelectEmailLineEdit *headers = AutoCreateScriptUtil::createSelectEmailsWidget(); 0048 headers->setObjectName(QLatin1StringView("from")); 0049 connect(headers, &AbstractSelectEmailLineEdit::valueChanged, this, &SieveActionReplace::valueChanged); 0050 grid->addWidget(headers, 1, 1); 0051 0052 lab = new QLabel(i18n("text:")); 0053 grid->addWidget(lab, 2, 0); 0054 0055 auto text = new MultiLineEdit; 0056 text->setObjectName(QLatin1StringView("text")); 0057 connect(text, &MultiLineEdit::textChanged, this, &SieveActionReplace::valueChanged); 0058 grid->addWidget(text, 2, 1); 0059 0060 return w; 0061 } 0062 0063 void SieveActionReplace::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error) 0064 { 0065 while (element.readNextStartElement()) { 0066 const QStringView tagName = element.name(); 0067 if (tagName == QLatin1StringView("str")) { 0068 auto edit = w->findChild<MultiLineEdit *>(QStringLiteral("text")); 0069 edit->setPlainText(element.readElementText()); 0070 } else if (tagName == QLatin1StringView("tag")) { 0071 const QString tagValue = element.readElementText(); 0072 if (tagValue == QLatin1StringView("subject")) { 0073 const QString strValue = AutoCreateScriptUtil::strValue(element); 0074 if (!strValue.isEmpty()) { 0075 auto subject = w->findChild<QLineEdit *>(QStringLiteral("subject")); 0076 subject->setText(strValue); 0077 } 0078 } else if (tagValue == QLatin1StringView("from")) { 0079 const QString strValue = AutoCreateScriptUtil::strValue(element); 0080 if (!strValue.isEmpty()) { 0081 auto headers = w->findChild<KSieveUi::AbstractSelectEmailLineEdit *>(QStringLiteral("from")); 0082 headers->setText(strValue); 0083 } 0084 } else { 0085 unknownTagValue(tagValue, error); 0086 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionReplace::setParamWidgetValue unknown tagValue " << tagValue; 0087 } 0088 } else if (tagName == QLatin1StringView("crlf")) { 0089 element.skipCurrentElement(); 0090 // nothing 0091 } else if (tagName == QLatin1StringView("comment")) { 0092 element.skipCurrentElement(); 0093 // implement in the future ? 0094 } else { 0095 unknownTag(tagName, error); 0096 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionReplace::setParamWidgetValue unknown tagName " << tagName; 0097 } 0098 } 0099 } 0100 0101 QString SieveActionReplace::code(QWidget *w) const 0102 { 0103 QString result = QStringLiteral("replace "); 0104 const QLineEdit *subject = w->findChild<QLineEdit *>(QStringLiteral("subject")); 0105 const QString subjectStr = subject->text(); 0106 if (!subjectStr.isEmpty()) { 0107 result += QStringLiteral(":subject \"%1\" ").arg(subjectStr); 0108 } 0109 0110 const KSieveUi::AbstractSelectEmailLineEdit *headers = w->findChild<KSieveUi::AbstractSelectEmailLineEdit *>(QStringLiteral("from")); 0111 const QString headersStr = headers->text(); 0112 if (!headersStr.isEmpty()) { 0113 result += QStringLiteral(":from \"%1\" ").arg(headersStr); 0114 } 0115 0116 const MultiLineEdit *edit = w->findChild<MultiLineEdit *>(QStringLiteral("text")); 0117 const QString text = edit->toPlainText(); 0118 if (!text.isEmpty()) { 0119 result += QStringLiteral("text:%1").arg(AutoCreateScriptUtil::createMultiLine(text)); 0120 } 0121 0122 return result; 0123 } 0124 0125 QStringList SieveActionReplace::needRequires(QWidget *) const 0126 { 0127 return QStringList() << QStringLiteral("replace"); 0128 } 0129 0130 bool SieveActionReplace::needCheckIfServerHasCapability() const 0131 { 0132 return true; 0133 } 0134 0135 QString SieveActionReplace::serverNeedsCapability() const 0136 { 0137 return QStringLiteral("replace"); 0138 } 0139 0140 QString SieveActionReplace::help() const 0141 { 0142 return i18n("The \"replace\" command is defined to allow a MIME part to be replaced with the text supplied in the command."); 0143 } 0144 0145 QUrl SieveActionReplace::href() const 0146 { 0147 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name())); 0148 } 0149 0150 #include "moc_sieveactionreplace.cpp"