File indexing completed on 2024-05-12 05:11:10

0001 /*
0002     SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadi-mime_export.h"
0010 
0011 #include <Akonadi/Item>
0012 #include <Akonadi/TransactionSequence>
0013 
0014 #include <memory>
0015 
0016 namespace Akonadi
0017 {
0018 class Collection;
0019 class ItemFetchScope;
0020 class Job;
0021 
0022 class FilterActionJob;
0023 
0024 /**
0025  * @short Base class for a filter/action for FilterActionJob.
0026  *
0027  * Abstract class defining an interface for a filter and an action for
0028  * FilterActionJob.  The virtual methods must be implemented in subclasses.
0029  *
0030  * @code
0031  * class ClearErrorAction : public Akonadi::FilterAction
0032  * {
0033  *   public:
0034  *     // reimpl
0035  *     virtual Akonadi::ItemFetchScope fetchScope() const
0036  *     {
0037  *       ItemFetchScope scope;
0038  *       scope.fetchFullPayload( false );
0039  *       scope.fetchAttribute<ErrorAttribute>();
0040  *       return scope;
0041  *     }
0042  *
0043  *     virtual bool itemAccepted( const Akonadi::Item &item ) const
0044  *     {
0045  *       return item.hasAttribute<ErrorAttribute>();
0046  *     }
0047  *
0048  *     virtual Akonadi::Job *itemAction( const Akonadi::Item &item,
0049  *                                       Akonadi::FilterActionJob *parent ) const
0050  *     {
0051  *       Item cp = item;
0052  *       cp.removeAttribute<ErrorAttribute>();
0053  *       return new ItemModifyJob( cp, parent );
0054  *     }
0055  * };
0056  * @endcode
0057  *
0058  * @see FilterActionJob
0059  *
0060  * @author Constantin Berzan <exit3219@gmail.com>
0061  * @since 4.4
0062  */
0063 class AKONADI_MIME_EXPORT FilterAction
0064 {
0065 public:
0066     /**
0067      * Destroys this filter action.
0068      *
0069      * A FilterActionJob will delete its FilterAction automatically.
0070      */
0071     virtual ~FilterAction();
0072 
0073     /**
0074      * Returns an ItemFetchScope to use if the FilterActionJob needs
0075      * to fetch the items from a collection.
0076      *
0077      * @note The items are not fetched unless FilterActionJob is
0078      *       constructed with a Collection parameter.
0079      */
0080     virtual Akonadi::ItemFetchScope fetchScope() const = 0;
0081 
0082     /**
0083      * Returns @c true if the @p item is accepted by the filter and should be
0084      * acted upon by the FilterActionJob.
0085      */
0086     virtual bool itemAccepted(const Akonadi::Item &item) const = 0;
0087 
0088     /**
0089      * Returns a job to act on the @p item.
0090      * The FilterActionJob will finish when all such jobs are finished.
0091      * @param item the item to work on
0092      * @param parent the parent job
0093      */
0094     virtual Akonadi::Job *itemAction(const Akonadi::Item &item, Akonadi::FilterActionJob *parent) const = 0;
0095 };
0096 
0097 class FilterActionJobPrivate;
0098 /**
0099  * @short Job to filter and apply an action on a set of items.
0100  *
0101  * This jobs filters through a set of items, and applies an action to the
0102  * items which are accepted by the filter.  The filter and action
0103  * are provided by a functor class derived from FilterAction.
0104  *
0105  * For example, a MarkAsRead action/filter may be used to mark all messages
0106  * in a folder as read.
0107  *
0108  * @code
0109  * FilterActionJob *mjob = new FilterActionJob( LocalFolders::self()->outbox(),
0110  *                                              new ClearErrorAction, this );
0111  * connect( mjob, SIGNAL( result( KJob* ) ), this, SLOT( massModifyResult( KJob* ) ) );
0112  * @endcode
0113  *
0114  * @see FilterAction
0115  *
0116  * @author Constantin Berzan <exit3219@gmail.com>
0117  * @since 4.4
0118  */
0119 class AKONADI_MIME_EXPORT FilterActionJob : public TransactionSequence
0120 {
0121     Q_OBJECT
0122 
0123 public:
0124     /**
0125      * Creates a filter action job to act on a single item.
0126      *
0127      * @param item The item to act on. The item is not re-fetched.
0128      * @param functor The FilterAction to use.
0129      * @param parent The parent object.
0130      */
0131     FilterActionJob(const Item &item, FilterAction *functor, QObject *parent = nullptr);
0132 
0133     /**
0134      * Creates a filter action job to act on a set of items.
0135      *
0136      * @param items The items to act on. The items are not re-fetched.
0137      * @param functor The FilterAction to use.
0138      * @param parent The parent object.
0139      */
0140     FilterActionJob(const Item::List &items, FilterAction *functor, QObject *parent = nullptr);
0141 
0142     /**
0143      * Creates a filter action job to act on items in a collection.
0144      *
0145      * @param collection The collection to act on.
0146      *                   The items of the collection are fetched using functor->fetchScope().
0147      * @param functor The FilterAction to use.
0148      * @param parent The parent object.
0149      */
0150     FilterActionJob(const Collection &collection, FilterAction *functor, QObject *parent = nullptr);
0151 
0152     /**
0153      * Destroys the filter action job.
0154      */
0155     ~FilterActionJob() override;
0156 
0157 protected:
0158     void doStart() override;
0159 
0160 private:
0161     //@cond PRIVATE
0162     friend class FilterActionJobPrivate;
0163     std::unique_ptr<FilterActionJobPrivate> const d;
0164     //@endcond
0165 };
0166 } // namespace Akonadi