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 "sieveconditionihave.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 <QHBoxLayout>
0016 #include <QWidget>
0017 #include <QXmlStreamReader>
0018 
0019 using namespace KSieveUi;
0020 SieveConditionIhave::SieveConditionIhave(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0021     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("ihave"), i18n("IHave"), parent)
0022 {
0023 }
0024 
0025 QWidget *SieveConditionIhave::createParamWidget(QWidget *parent) const
0026 {
0027     auto w = new QWidget(parent);
0028     auto lay = new QHBoxLayout;
0029     lay->setContentsMargins({});
0030     w->setLayout(lay);
0031 
0032     auto edit = new QLineEdit;
0033     KLineEditEventHandler::catchReturnKey(edit);
0034     connect(edit, &QLineEdit::textChanged, this, &SieveConditionIhave::valueChanged);
0035     edit->setPlaceholderText(i18n("Use \",\" to separate capabilities"));
0036     edit->setClearButtonEnabled(true);
0037     lay->addWidget(edit);
0038     edit->setObjectName(QLatin1StringView("edit"));
0039 
0040     return w;
0041 }
0042 
0043 QString SieveConditionIhave::code(QWidget *w) const
0044 {
0045     const QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
0046     const QString editValue = edit->text();
0047     return QStringLiteral("ihave %1").arg(AutoCreateScriptUtil::createList(editValue, QLatin1Char(',')))
0048         + AutoCreateScriptUtil::generateConditionComment(comment());
0049 }
0050 
0051 QStringList SieveConditionIhave::needRequires(QWidget *) const
0052 {
0053     return QStringList() << QStringLiteral("ihave");
0054 }
0055 
0056 bool SieveConditionIhave::needCheckIfServerHasCapability() const
0057 {
0058     return true;
0059 }
0060 
0061 QString SieveConditionIhave::serverNeedsCapability() const
0062 {
0063     return QStringLiteral("ihave");
0064 }
0065 
0066 QString SieveConditionIhave::help() const
0067 {
0068     return i18n("The \"ihave\" test provides a means for Sieve scripts to test for the existence of a given extension prior to actually using it.");
0069 }
0070 
0071 void SieveConditionIhave::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool, QString &error)
0072 {
0073     QString commentStr;
0074     while (element.readNextStartElement()) {
0075         const QStringView tagName = element.name();
0076         if (tagName == QLatin1StringView("str")) {
0077             const QString tagValue = element.readElementText();
0078             auto edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
0079             edit->setText(tagValue);
0080         } else if (tagName == QLatin1StringView("crlf")) {
0081             element.skipCurrentElement();
0082             // nothing
0083         } else if (tagName == QLatin1StringView("comment")) {
0084             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0085         } else {
0086             unknownTag(tagName, error);
0087             qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionIhave::setParamWidgetValue unknown tagName " << tagName;
0088         }
0089     }
0090     if (!commentStr.isEmpty()) {
0091         setComment(commentStr);
0092     }
0093 }
0094 
0095 QUrl SieveConditionIhave::href() const
0096 {
0097     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0098 }
0099 
0100 #include "moc_sieveconditionihave.cpp"