File indexing completed on 2024-11-10 04:40:32

0001 /*
0002     SPDX-FileCopyrightText: 2006-2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadicore_export.h"
0010 #include "job.h"
0011 
0012 namespace Akonadi
0013 {
0014 class TransactionSequencePrivate;
0015 
0016 /**
0017  * @short Base class for jobs that need to run a sequence of sub-jobs in a transaction.
0018  *
0019  * As soon as the first subjob is added, the transaction is started.
0020  * As soon as the last subjob has successfully finished, the transaction is committed.
0021  * If any subjob fails, the transaction is rolled back.
0022  *
0023  * Alternatively, a TransactionSequence object can be used as a parent object
0024  * for a set of jobs to achieve the same behaviour without subclassing.
0025  *
0026  * Example:
0027  *
0028  * @code
0029  *
0030  * // Delete a couple of items inside a transaction
0031  * Akonadi::TransactionSequence *transaction = new Akonadi::TransactionSequence;
0032  * connect( transaction, SIGNAL(result(KJob*)), SLOT(transactionFinished(KJob*)) );
0033  *
0034  * const Akonadi::Item::List items = ...
0035  *
0036  * for( const Akonadi::Item &item : items ) {
0037  *   new Akonadi::ItemDeleteJob( item, transaction );
0038  * }
0039  *
0040  * ...
0041  *
0042  * MyClass::transactionFinished( KJob *job )
0043  * {
0044  *   if ( job->error() )
0045  *     qDebug() << "Error occurred";
0046  *   else
0047  *     qDebug() << "Items deleted successfully";
0048  * }
0049  *
0050  * @endcode
0051  *
0052  * @author Volker Krause <vkrause@kde.org>
0053  */
0054 class AKONADICORE_EXPORT TransactionSequence : public Job
0055 {
0056     Q_OBJECT
0057 public:
0058     /**
0059      * Creates a new transaction sequence.
0060      *
0061      * @param parent The parent object.
0062      */
0063     explicit TransactionSequence(QObject *parent = nullptr);
0064 
0065     /**
0066      * Destroys the transaction sequence.
0067      */
0068     ~TransactionSequence() override;
0069 
0070     /**
0071      * Commits the transaction as soon as all pending sub-jobs finished successfully.
0072      */
0073     void commit();
0074 
0075     /**
0076      * Rolls back the current transaction as soon as possible.
0077      * You only need to call this method when you want to roll back due to external
0078      * reasons (e.g. user cancellation), the transaction is automatically rolled back
0079      * if one of its subjobs fails.
0080      * @since 4.5
0081      */
0082     void rollback();
0083 
0084     /**
0085      * Sets which job of the sequence might fail without rolling back the
0086      * complete transaction.
0087      * @param job a job to ignore errors from
0088      * @since 4.5
0089      */
0090     void setIgnoreJobFailure(KJob *job);
0091 
0092     /**
0093      * Disable automatic committing.
0094      * Use this when you want to add jobs to this sequence after execution
0095      * has been started, usually that is outside of the constructor or the
0096      * method that creates this transaction sequence.
0097      * @note Calling this method after execution of this job has been started
0098      * has no effect.
0099      * @param enable @c true to enable autocommitting (default), @c false to disable it
0100      * @since 4.5
0101      */
0102     void setAutomaticCommittingEnabled(bool enable);
0103 
0104 protected:
0105     bool addSubjob(KJob *job) override;
0106     void doStart() override;
0107 
0108 protected Q_SLOTS:
0109     void slotResult(KJob *job) override;
0110 
0111 private:
0112     Q_DECLARE_PRIVATE(TransactionSequence)
0113 
0114     /// @cond PRIVATE
0115     Q_PRIVATE_SLOT(d_func(), void commitResult(KJob *))
0116     Q_PRIVATE_SLOT(d_func(), void rollbackResult(KJob *))
0117     /// @endcond
0118 };
0119 
0120 }