File indexing completed on 2024-09-22 04:49:59

0001 /*
0002  * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #pragma once
0009 
0010 #include "mailcommon_export.h"
0011 #include "searchpattern.h"
0012 
0013 #include "mailcommon/itemcontext.h"
0014 
0015 #include <Akonadi/Collection>
0016 #include <Akonadi/Item>
0017 
0018 #include <KMime/KMimeMessage>
0019 #include <KMime/MDN>
0020 
0021 #include <QObject>
0022 
0023 class QWidget;
0024 
0025 namespace MailCommon
0026 {
0027 /**
0028  * @short Abstract base class for mail filter actions.
0029  *
0030  * Abstract base class for mail filter actions. All it can do is
0031  * hold a name (ie. type-string). There are several sub-classes that
0032  * inherit form this and are capable of providing parameter handling
0033  * (import/export as string, a widget to allow editing, etc.)
0034  *
0035  * @author Marc Mutz <mutz@kde.org>, based on work by Stefan Taferner <taferner@kde.org>.
0036  * @see Filter FilterMgr
0037  */
0038 class MAILCOMMON_EXPORT FilterAction : public QObject
0039 {
0040     Q_OBJECT
0041 public:
0042     /**
0043      * Describes the possible return codes of filter processing:
0044      */
0045     enum ReturnCode {
0046         ErrorNeedComplete = 0x1, ///< Could not process because a complete message is needed.
0047         GoOn = 0x2, ///< Go on with applying filter actions.
0048         ErrorButGoOn = 0x4, ///< A non-critical error occurred
0049         ///  (e.g. an invalid address in the 'forward' action),
0050         ///   but processing should continue.
0051         CriticalError = 0x8 ///< A critical error occurred during processing
0052                             ///  (e.g. "disk full").
0053     };
0054 
0055     /**
0056      * Creates a new filter action.
0057      *
0058      * The action is initialized with an identifier @p name and
0059      * an i18n'd @p label.
0060      */
0061     FilterAction(const QString &name, const QString &label, QObject *parent = nullptr);
0062 
0063     /**
0064      * Destroys the filter action.
0065      */
0066     ~FilterAction() override;
0067 
0068     /**
0069      * Returns i18n'd label, ie. the one which is presented in
0070      * the filter dialog.
0071      */
0072     [[nodiscard]] QString label() const;
0073 
0074     /**
0075      * Returns identifier name, ie. the one under which it is
0076      * known in the config.
0077      */
0078     [[nodiscard]] QString name() const;
0079 
0080     [[nodiscard]] virtual QStringList sieveRequires() const;
0081 
0082     [[nodiscard]] virtual QString sieveCode() const;
0083 
0084     /**
0085      * Execute action on given message (inside the item context).
0086      * Returns @p CriticalError if a
0087      * critical error has occurred (eg. disk full), @p ErrorButGoOn if
0088      * there was a non-critical error (e.g. invalid address in
0089      * 'forward' action), @p ErrorNeedComplete if a complete message
0090      * is required, @p GoOn if the message shall be processed by
0091      * further filters and @p Ok otherwise.
0092      */
0093     [[nodiscard]] virtual ReturnCode process(ItemContext &context, bool applyOnOutbound) const = 0;
0094 
0095     /**
0096      * Returns the required part from the item that is needed for the action to
0097      * operate. See @ref MailCommon::SearchRule::RequiredPart */
0098     [[nodiscard]] virtual SearchRule::RequiredPart requiredPart() const = 0;
0099     /**
0100      * Determines whether this action is valid. But this is just a
0101      * quick test. Eg., actions that have a mail address as parameter
0102      * shouldn't try real address validation, but only check if the
0103      * string representation is empty.
0104      */
0105     [[nodiscard]] virtual bool isEmpty() const;
0106 
0107     [[nodiscard]] virtual QString informationAboutNotValidAction() const;
0108 
0109     /**
0110      * Creates a widget for setting the filter action parameter. Also
0111      * sets the value of the widget.
0112      */
0113     [[nodiscard]] virtual QWidget *createParamWidget(QWidget *parent) const;
0114 
0115     /**
0116      * The filter action shall set it's parameter from the widget's
0117      * contents. It is allowed that the value is read by the action
0118      * before this function is called.
0119      */
0120     virtual void applyParamWidgetValue(QWidget *paramWidget);
0121 
0122     /**
0123      * The filter action shall set it's widget's contents from it's
0124      * parameter.
0125      */
0126     virtual void setParamWidgetValue(QWidget *paramWidget) const;
0127 
0128     /**
0129      * The filter action shall clear it's parameter widget's
0130      * contents.
0131      */
0132     virtual void clearParamWidget(QWidget *paramWidget) const;
0133 
0134     /**
0135      * Read extra arguments from given string.
0136      */
0137     virtual void argsFromString(const QString &argsStr) = 0;
0138 
0139     /**
0140      * Read extra arguments from given string.
0141      * Return true if we need to update config file
0142      */
0143     [[nodiscard]] virtual bool argsFromStringInteractive(const QString &argsStr, const QString &filterName);
0144 
0145     [[nodiscard]] virtual QString argsAsStringReal() const;
0146 
0147     /**
0148      * Return extra arguments as string. Must not contain newlines.
0149      */
0150     [[nodiscard]] virtual QString argsAsString() const = 0;
0151 
0152     /**
0153      * Returns a translated string describing this filter for visualization
0154      * purposes, e.g. in the filter log.
0155      */
0156     [[nodiscard]] virtual QString displayString() const = 0;
0157 
0158     /**
0159      * Called from the filter when a folder is removed. Tests if the
0160      * folder @p aFolder is used and changes to @p aNewFolder in this
0161      * case. Returns true if a change was made.
0162      */
0163     [[nodiscard]] virtual bool folderRemoved(const Akonadi::Collection &aFolder, const Akonadi::Collection &aNewFolder);
0164 
0165     /**
0166      * Static function that creates a filter action of this type.
0167      */
0168     static FilterAction *newAction();
0169 
0170     /**
0171      * Automates the sending of MDNs from filter actions.
0172      */
0173     static void sendMDN(const Akonadi::Item &item,
0174                         KMime::MDN::DispositionType d,
0175                         const QList<KMime::MDN::DispositionModifier> &m = QList<KMime::MDN::DispositionModifier>());
0176 
0177 Q_SIGNALS:
0178     /**
0179      * Called to notify that the current FilterAction has had some
0180      * value modification
0181      */
0182     void filterActionModified();
0183 
0184 private:
0185     const QString mName;
0186     const QString mLabel;
0187 };
0188 }