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 "sieveactionfileinto.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
0009 #include "editor/sieveeditorutil.h"
0010 #include "widgets/moveimapfolderwidget.h"
0011 #include <KLocalizedString>
0012 
0013 #include "libksieveui_debug.h"
0014 #include <QCheckBox>
0015 #include <QHBoxLayout>
0016 #include <QXmlStreamReader>
0017 // Add support for adding flags
0018 using namespace KSieveUi;
0019 SieveActionFileInto::SieveActionFileInto(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0020     : SieveAction(sieveGraphicalModeWidget, QStringLiteral("fileinto"), i18n("File Into"), parent)
0021 {
0022     mHasCopySupport = sieveCapabilities().contains(QLatin1StringView("copy"));
0023     mHasMailBoxSupport = sieveCapabilities().contains(QLatin1StringView("mailbox"));
0024 }
0025 
0026 QString SieveActionFileInto::code(QWidget *w) const
0027 {
0028     QString result = QStringLiteral("fileinto ");
0029     const KSieveUi::AbstractMoveImapFolderWidget *edit = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("fileintolineedit"));
0030     const QString text = edit->text();
0031     if (mHasCopySupport) {
0032         const QCheckBox *copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
0033         if (copy->isChecked()) {
0034             result += QLatin1StringView(":copy ");
0035         }
0036     }
0037     if (mHasMailBoxSupport) {
0038         const QCheckBox *create = w->findChild<QCheckBox *>(QStringLiteral("create"));
0039         if (create->isChecked()) {
0040             result += QLatin1StringView(":create ");
0041         }
0042     }
0043     return result + QStringLiteral("\"%1\";").arg(text);
0044 }
0045 
0046 void SieveActionFileInto::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
0047 {
0048     while (element.readNextStartElement()) {
0049         const QStringView tagName = element.name();
0050         if (tagName == QLatin1StringView("tag")) {
0051             const QString tagValue = element.readElementText();
0052             if (tagValue == QLatin1StringView("copy")) {
0053                 if (mHasCopySupport) {
0054                     auto copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
0055                     copy->setChecked(true);
0056                 } else {
0057                     error += i18n("Action \"fileinto\" has \"copy\" argument but current server does not support it") + QLatin1Char('\n');
0058                     qCDebug(LIBKSIEVEUI_LOG) << "SieveActionFileInto::setParamWidgetValue has not copy support ";
0059                 }
0060             } else if (tagValue == QLatin1StringView("create")) {
0061                 if (mHasMailBoxSupport) {
0062                     auto create = w->findChild<QCheckBox *>(QStringLiteral("create"));
0063                     create->setChecked(true);
0064                 } else {
0065                     serverDoesNotSupportFeatures(QStringLiteral("create"), error);
0066                     qCDebug(LIBKSIEVEUI_LOG) << "SieveActionFileInto::setParamWidgetValue server has not create support ";
0067                 }
0068             } else {
0069                 serverDoesNotSupportFeatures(tagValue, error);
0070                 qCDebug(LIBKSIEVEUI_LOG) << "SieveActionFileInto::setParamWidgetValue server has not flags support ";
0071             }
0072         } else if (tagName == QLatin1StringView("str")) {
0073             const QString tagValue = element.readElementText();
0074             auto edit = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("fileintolineedit"));
0075             edit->setText(AutoCreateScriptUtil::protectSlash(tagValue));
0076         } else if (tagName == QLatin1StringView("crlf")) {
0077             element.skipCurrentElement();
0078             // nothing
0079         } else if (tagName == QLatin1StringView("comment")) {
0080             element.skipCurrentElement();
0081             // implement in the future ?
0082         } else {
0083             unknownTag(tagName, error);
0084             qCDebug(LIBKSIEVEUI_LOG) << " SieveActionFileInto::setParamWidgetValue unknown tagName " << tagName;
0085         }
0086     }
0087 }
0088 
0089 QWidget *SieveActionFileInto::createParamWidget(QWidget *parent) const
0090 {
0091     auto w = new QWidget(parent);
0092     auto lay = new QHBoxLayout(w);
0093     lay->setContentsMargins({});
0094 
0095     if (mHasCopySupport) {
0096         auto copy = new QCheckBox(i18n("Keep a copy"));
0097         copy->setObjectName(QLatin1StringView("copy"));
0098         lay->addWidget(copy);
0099         connect(copy, &QCheckBox::clicked, this, &SieveActionFileInto::valueChanged);
0100     }
0101     if (mHasMailBoxSupport) {
0102         auto create = new QCheckBox(i18n("Create folder"));
0103         create->setObjectName(QLatin1StringView("create"));
0104         connect(create, &QCheckBox::clicked, this, &SieveActionFileInto::valueChanged);
0105         lay->addWidget(create);
0106     }
0107 
0108     KSieveUi::AbstractMoveImapFolderWidget *edit = AutoCreateScriptUtil::createImapFolderWidget();
0109     edit->setSieveImapAccountSettings(sieveImapAccountSettings());
0110     connect(edit, &KSieveUi::AbstractMoveImapFolderWidget::textChanged, this, &SieveActionFileInto::valueChanged);
0111     lay->addWidget(edit);
0112     edit->setObjectName(QLatin1StringView("fileintolineedit"));
0113     return w;
0114 }
0115 
0116 QStringList SieveActionFileInto::needRequires(QWidget *parent) const
0117 {
0118     QStringList lst;
0119     lst << QStringLiteral("fileinto");
0120     if (mHasCopySupport) {
0121         const QCheckBox *copy = parent->findChild<QCheckBox *>(QStringLiteral("copy"));
0122         if (copy->isChecked()) {
0123             lst << QStringLiteral("copy");
0124         }
0125     }
0126     if (mHasMailBoxSupport) {
0127         const QCheckBox *create = parent->findChild<QCheckBox *>(QStringLiteral("create"));
0128         if (create->isChecked()) {
0129             lst << QStringLiteral("mailbox");
0130         }
0131     }
0132     return lst;
0133 }
0134 
0135 bool SieveActionFileInto::needCheckIfServerHasCapability() const
0136 {
0137     return true;
0138 }
0139 
0140 QString SieveActionFileInto::serverNeedsCapability() const
0141 {
0142     return QStringLiteral("fileinto");
0143 }
0144 
0145 QString SieveActionFileInto::help() const
0146 {
0147     QString helpStr = i18n("The \"fileinto\" action delivers the message into the specified mailbox.");
0148     if (mHasMailBoxSupport) {
0149         helpStr += QLatin1Char('\n')
0150             + i18n("If the optional \":create\" argument is specified, it instructs the Sieve interpreter to create the specified mailbox, if needed, before "
0151                    "attempting to deliver the message into the specified mailbox.");
0152     }
0153     if (mHasCopySupport) {
0154         helpStr += QLatin1Char('\n')
0155             + i18n("If the optional \":copy\" keyword is specified, the tagged command does not cancel the implicit \"keep\". Instead, it merely files or "
0156                    "redirects a copy in addition to whatever else is happening to the message.");
0157     }
0158     return helpStr;
0159 }
0160 
0161 QUrl KSieveUi::SieveActionFileInto::href() const
0162 {
0163     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0164 }
0165 
0166 #include "moc_sieveactionfileinto.cpp"