File indexing completed on 2024-09-22 04:50:04

0001 /*
0002   SPDX-FileCopyrightText: 2011 Tobias Koenig <tokoe@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "mailcommon_export.h"
0010 #include "mailfilter.h"
0011 
0012 #include <Akonadi/Item>
0013 #include <Akonadi/ServerManager>
0014 
0015 #include <QObject>
0016 
0017 namespace MailCommon
0018 {
0019 class FilterActionDict;
0020 
0021 /**
0022  * @short A wrapper class that allows easy access to the mail filters
0023  *
0024  * This class communicates with the mailfilter agent via DBus.
0025  */
0026 class MAILCOMMON_EXPORT FilterManager : public QObject
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     ~FilterManager() override;
0032     /**
0033      * Describes the list of filters.
0034      */
0035     enum FilterSet {
0036         NoSet = 0x0,
0037         Inbound = 0x1,
0038         Outbound = 0x2,
0039         Explicit = 0x4,
0040         BeforeOutbound = 0x8,
0041         AllFolders = 0x16, ///< Apply the filter on all folders, not just inbox
0042         All = Inbound | BeforeOutbound | Outbound | Explicit | AllFolders
0043     };
0044 
0045     /**
0046      * Returns the global filter manager object.
0047      */
0048     static FilterManager *instance();
0049 
0050     /**
0051      * Returns whether the filter manager is in a usable state.
0052      */
0053     [[nodiscard]] bool isValid() const;
0054 
0055     /**
0056      * Checks for existing filters with the @p name and extend the
0057      * "name" to "name (i)" until no match is found for i=1..n
0058      */
0059     [[nodiscard]] QString createUniqueFilterName(const QString &name) const;
0060 
0061     /**
0062      * Returns the global filter action dictionary.
0063      */
0064     static FilterActionDict *filterActionDict();
0065 
0066     /**
0067      * Shows the filter log dialog.
0068      *
0069      * This is used to debug problems with filters.
0070      */
0071     void showFilterLogDialog(qlonglong windowId);
0072 
0073     /// Apply filters interface
0074 
0075     /**
0076      * Applies filter with the given @p identifier on the message @p item.
0077      * @return @c true on success, @c false otherwise.
0078      */
0079     void filter(const Akonadi::Item &item, const QString &identifier, const QString &resourceId) const;
0080 
0081     /**
0082      * Process given message item by applying the filter rules one by
0083      * one. You can select which set of filters (incoming or outgoing)
0084      * should be used.
0085      *
0086      * @param item The message item to process.
0087      * @param set Select the filter set to use.
0088      * @param account @c true if an account id is specified else @c false
0089      * @param accountId The id of the resource that the message was retrieved from
0090      */
0091     void filter(const Akonadi::Item &item, FilterSet set = Inbound, bool account = false, const QString &resourceId = QString()) const;
0092 
0093     /**
0094      * Process all messages in given collection by applying the filters rules one
0095      * by one. You can select which set of filters (incoming or outgoing)
0096      * should be used.
0097      */
0098     void filter(const Akonadi::Collection &collection, FilterSet set = Explicit) const;
0099 
0100     /**
0101      * Apply specified filters on all messages in given collection
0102      */
0103     void filter(const Akonadi::Collection &collection, const QStringList &listFilters) const;
0104 
0105     /**
0106      * Process all messages in given collections by applying the filters rules one
0107      * by one. You can select which set of filters (incoming or outgoing)
0108      * should be used.
0109      */
0110     void filter(const Akonadi::Collection::List &collections, FilterSet set = Explicit) const;
0111 
0112     /**
0113      * Apply specified filters on all messages in given collection
0114      */
0115     void filter(const Akonadi::Collection::List &collections, const QStringList &listFilters, FilterSet set = Explicit) const;
0116 
0117     /**
0118      * Process given @p messages by applying the filter rules one by
0119      * one. You can select which set of filters (incoming or outgoing)
0120      * should be used.
0121      *
0122      * @param item The message item to process.
0123      * @param set Select the filter set to use.
0124      */
0125     void filter(const Akonadi::Item::List &messages, FilterSet set = Explicit) const;
0126 
0127     void filter(const Akonadi::Item::List &messages, SearchRule::RequiredPart requiredPart, const QStringList &listFilters) const;
0128 
0129     /// Manage filters interface
0130 
0131     /**
0132      * Appends the list of @p filters to the current list of filters and
0133      * write everything back into the configuration. The filter manager
0134      * takes ownership of the filters in the list.
0135      */
0136     void appendFilters(const QList<MailCommon::MailFilter *> &filters, bool replaceIfNameExists = false);
0137 
0138     /**
0139      * Removes the given @p filter from the list.
0140      * The filter object is not deleted.
0141      */
0142     void removeFilter(MailCommon::MailFilter *filter);
0143 
0144     /**
0145      * Replace the list of filters of the filter manager with the given list of @p filters.
0146      * The manager takes ownership of the filters.
0147      */
0148     void setFilters(const QList<MailCommon::MailFilter *> &filters);
0149 
0150     /**
0151      * Returns the filter list of the manager.
0152      */
0153     [[nodiscard]] QList<MailCommon::MailFilter *> filters() const;
0154 
0155     /**
0156      * Should be called at the beginning of an filter list update.
0157      */
0158     void beginUpdate();
0159 
0160     /**
0161      * Should be called at the end of an filter list update.
0162      */
0163     void endUpdate();
0164 
0165     [[nodiscard]] QMap<QUrl, QString> tagList() const;
0166 
0167     [[nodiscard]] bool initialized() const;
0168 
0169     void cleanup();
0170 private Q_SLOTS:
0171     MAILCOMMON_NO_EXPORT void slotServerStateChanged(Akonadi::ServerManager::State);
0172     MAILCOMMON_NO_EXPORT void slotFinishedTagListing(KJob *);
0173     MAILCOMMON_NO_EXPORT void slotReadConfig();
0174     MAILCOMMON_NO_EXPORT void updateTagList();
0175 
0176     MAILCOMMON_NO_EXPORT void slotTagAdded(const Akonadi::Tag &);
0177     MAILCOMMON_NO_EXPORT void slotTagChanged(const Akonadi::Tag &);
0178     MAILCOMMON_NO_EXPORT void slotTagRemoved(const Akonadi::Tag &);
0179 
0180 Q_SIGNALS:
0181     /**
0182      * This signal is emitted whenever the filter list has been updated.
0183      */
0184     void filtersChanged();
0185 
0186     void tagListingFinished();
0187 
0188     void loadingFiltersDone();
0189 
0190 private:
0191     MAILCOMMON_NO_EXPORT FilterManager();
0192 
0193     class FilterManagerPrivate;
0194     FilterManagerPrivate *const d;
0195 };
0196 }