File indexing completed on 2024-10-27 04:50:47

0001 /*
0002  * kmail: KDE mail client
0003  * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  *
0007  */
0008 #pragma once
0009 
0010 #include <Akonadi/Collection>
0011 #include <Akonadi/Item>
0012 
0013 #include <MailCommon/SearchPattern>
0014 #include <memory>
0015 namespace MailCommon
0016 {
0017 class MailFilter;
0018 class ItemContext;
0019 }
0020 
0021 class FilterManager : public QObject
0022 {
0023     Q_OBJECT
0024 
0025 public:
0026     /**
0027      * Describes the list of filters.
0028      */
0029     enum FilterSet {
0030         NoSet = 0x0,
0031         Inbound = 0x1,
0032         Outbound = 0x2,
0033         Explicit = 0x4,
0034         BeforeOutbound = 0x8,
0035         AllFolders = 0x16,
0036         All = Inbound | BeforeOutbound | Outbound | Explicit | AllFolders,
0037     };
0038 
0039     enum FilterRequires {
0040         Unknown = 0,
0041         HeaderMessage = 1,
0042         FullMessage = 2,
0043     };
0044 
0045     /**
0046      * Creates a new filter manager.
0047      *
0048      * @param parent The parent object.
0049      */
0050     explicit FilterManager(QObject *parent = nullptr);
0051 
0052     /**
0053      * Destroys the filter manager.
0054      */
0055     ~FilterManager() override;
0056 
0057     /**
0058      * Clears the list of filters and deletes them.
0059      */
0060     void clear();
0061 
0062     /**
0063      * Reloads the filter rules from config file.
0064      */
0065     void readConfig();
0066 
0067     /**
0068      * Checks for existing filters with the @p name and extend the
0069      * "name" to "name (i)" until no match is found for i=1..n
0070      */
0071     [[nodiscard]] QString createUniqueName(const QString &name) const;
0072 
0073     /**
0074      * Process given message item by applying the filter rules one by
0075      * one. You can select which set of filters (incoming or outgoing)
0076      * should be used.
0077      *
0078      *  @param item The message item to process.
0079      *  @param set Select the filter set to use.
0080      *  @param account @c true if an account id is specified else @c false
0081      *  @param accountId The id of the KMAccount that the message was retrieved from
0082      *
0083      *  @return true if the filtering was successful, false in case of any error
0084      */
0085     [[nodiscard]] bool
0086     process(const Akonadi::Item &item, bool needsFullPayload, FilterSet set = Inbound, bool account = false, const QString &accountId = QString());
0087 
0088     [[nodiscard]] bool process(const QList<MailCommon::MailFilter *> &mailFilters,
0089                                const Akonadi::Item &item,
0090                                bool needsFullPayload,
0091                                FilterSet set = Inbound,
0092                                bool account = false,
0093                                const QString &accountId = QString());
0094 
0095     /**
0096      * For ad-hoc filters.
0097      *
0098      * Applies @p filter to message @p item.
0099      * Return codes are as with the above method.
0100      */
0101     [[nodiscard]] bool process(const Akonadi::Item &item, bool needsFullPayload, const MailCommon::MailFilter *filter);
0102 
0103     void filter(const Akonadi::Item &item, FilterManager::FilterSet set, const QString &resourceId);
0104     void filter(const Akonadi::Item &item, const QString &filterId, const QString &resourceId);
0105 
0106     void applySpecificFilters(const Akonadi::Item::List &selectedMessages,
0107                               MailCommon::SearchRule::RequiredPart requiredPart,
0108                               const QStringList &listFilters,
0109                               FilterSet set = Explicit);
0110 
0111     /**
0112      * Applies the filters on the given @p messages.
0113      */
0114     void applyFilters(const Akonadi::Item::List &messages, FilterSet set = Explicit);
0115 
0116     /**
0117      * Returns whether the configured filters need the full mail content.
0118      */
0119     [[nodiscard]] MailCommon::SearchRule::RequiredPart requiredPart(const QString &id) const;
0120 
0121     void mailCollectionRemoved(const Akonadi::Collection &collection);
0122     void agentRemoved(const QString &identifier);
0123 
0124     [[nodiscard]] bool hasAllFoldersFilter() const;
0125 
0126     /**
0127      * Outputs all filter rules to console. Used for debugging.
0128      */
0129     void dump() const;
0130 
0131 protected:
0132     [[nodiscard]] bool processContextItem(MailCommon::ItemContext context);
0133 
0134 Q_SIGNALS:
0135     /**
0136      * This signal is emitted whenever the filter list has been updated.
0137      */
0138     void filterListUpdated();
0139 
0140     /**
0141      * This signal is emitted to notify that @p item has not been moved.
0142      */
0143     void filteringFailed(const Akonadi::Item &item);
0144 
0145     void percent(int progress);
0146     void progressMessage(const QString &message);
0147 
0148 private:
0149     //@cond PRIVATE
0150     class Private;
0151     std::unique_ptr<Private> const d;
0152     //@endcond
0153 };