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 "sieveconditionmailboxexists.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "editor/sieveeditorutil.h"
0009 #include <KLineEditEventHandler>
0010 #include <KLocalizedString>
0011 #include <QLineEdit>
0012 
0013 #include "libksieveui_debug.h"
0014 #include <QHBoxLayout>
0015 #include <QWidget>
0016 #include <QXmlStreamReader>
0017 
0018 using namespace KSieveUi;
0019 SieveConditionMailboxExists::SieveConditionMailboxExists(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0020     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("mailboxexists"), i18n("Mailbox exists"), parent)
0021 {
0022 }
0023 
0024 QWidget *SieveConditionMailboxExists::createParamWidget(QWidget *parent) const
0025 {
0026     auto w = new QWidget(parent);
0027     auto lay = new QHBoxLayout;
0028     lay->setContentsMargins({});
0029     w->setLayout(lay);
0030 
0031     auto edit = new QLineEdit;
0032     KLineEditEventHandler::catchReturnKey(edit);
0033     connect(edit, &QLineEdit::textChanged, this, &SieveConditionMailboxExists::valueChanged);
0034     edit->setClearButtonEnabled(true);
0035     lay->addWidget(edit);
0036     edit->setObjectName(QLatin1StringView("edit"));
0037 
0038     return w;
0039 }
0040 
0041 QString SieveConditionMailboxExists::code(QWidget *w) const
0042 {
0043     const QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
0044     const QString editValue = edit->text();
0045     return QStringLiteral("mailboxexists \"%1\"").arg(editValue) + AutoCreateScriptUtil::generateConditionComment(comment());
0046 }
0047 
0048 QStringList SieveConditionMailboxExists::needRequires(QWidget *) const
0049 {
0050     return QStringList() << QStringLiteral("mailbox");
0051 }
0052 
0053 bool SieveConditionMailboxExists::needCheckIfServerHasCapability() const
0054 {
0055     return true;
0056 }
0057 
0058 QString SieveConditionMailboxExists::serverNeedsCapability() const
0059 {
0060     return QStringLiteral("mailbox");
0061 }
0062 
0063 QString SieveConditionMailboxExists::help() const
0064 {
0065     return i18n(
0066         "The \"mailboxexists\" test is true if all mailboxes listed in the \"mailbox-names\" argument exist in the mailstore, and each allows the user in "
0067         "whose context the Sieve script runs to \"deliver\" messages into it.");
0068 }
0069 
0070 void SieveConditionMailboxExists::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
0071 {
0072     QString commentStr;
0073     while (element.readNextStartElement()) {
0074         const QStringView tagName = element.name();
0075         if (tagName == QLatin1StringView("str")) {
0076             const QString tagValue = element.readElementText();
0077             auto edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
0078             edit->setText(AutoCreateScriptUtil::quoteStr(tagValue));
0079         } else if (tagName == QLatin1StringView("crlf")) {
0080             element.skipCurrentElement();
0081             // nothing
0082         } else if (tagName == QLatin1StringView("comment")) {
0083             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0084         } else {
0085             unknownTag(tagName, error);
0086             qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionMailboxExists::setParamWidgetValue unknown tagName " << tagName;
0087         }
0088     }
0089     if (!commentStr.isEmpty()) {
0090         setComment(commentStr);
0091     }
0092 }
0093 
0094 QUrl SieveConditionMailboxExists::href() const
0095 {
0096     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0097 }
0098 
0099 #include "moc_sieveconditionmailboxexists.cpp"