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

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 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 "item.h"
0011 #include "job.h"
0012 
0013 namespace Akonadi
0014 {
0015 class ItemModifyJobPrivate;
0016 
0017 /**
0018  * @short Job that modifies an existing item in the Akonadi storage.
0019  *
0020  * This job is used to writing back items to the Akonadi storage, after
0021  * the user has changed them in any way.
0022  * For performance reasons either the full item (including the full payload)
0023  * can written back or only the meta data of the item.
0024  *
0025  * Example:
0026  *
0027  * @code
0028  *
0029  * // Fetch item with unique id 125
0030  * Akonadi::ItemFetchJob *fetchJob = new Akonadi::ItemFetchJob( Akonadi::Item( 125 ) );
0031  * connect( fetchJob, SIGNAL(result(KJob*)), SLOT(fetchFinished(KJob*)) );
0032  *
0033  * ...
0034  *
0035  * MyClass::fetchFinished( KJob *job )
0036  * {
0037  *   if ( job->error() )
0038  *     return;
0039  *
0040  *   Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
0041  *
0042  *   Akonadi::Item item = fetchJob->items().at(0);
0043  *
0044  *   // Set a custom flag
0045  *   item.setFlag( "\GotIt" );
0046  *
0047  *   // Store back modified item
0048  *   Akonadi::ItemModifyJob *modifyJob = new Akonadi::ItemModifyJob( item );
0049  *   connect( modifyJob, SIGNAL(result(KJob*)), SLOT(modifyFinished(KJob*)) );
0050  * }
0051  *
0052  * MyClass::modifyFinished( KJob *job )
0053  * {
0054  *   if ( job->error() )
0055  *     qDebug() << "Error occurred";
0056  *   else
0057  *     qDebug() << "Item modified successfully";
0058  * }
0059  *
0060  * @endcode
0061  *
0062  * <h3>Conflict Resolution</h3>
0063 
0064  * When the job is executed, a check is made to ensure that the Item contained
0065  * in the job is not older than the version of the Item already held in the
0066  * Akonadi database. If it is older, a conflict resolution dialog is displayed
0067  * for the user to choose which version of the Item to use, unless
0068  * disableAutomaticConflictHandling() has been called to disable the dialog, or
0069  * disableRevisionCheck() has been called to disable version checking
0070  * altogether.
0071  *
0072  * The item version is checked by comparing the Item::revision() values in the
0073  * job and in the database. To ensure that two successive ItemModifyJobs for
0074  * the same Item work correctly, the revision number of the Item supplied to
0075  * the second ItemModifyJob should be set equal to the Item's revision number
0076  * on completion of the first ItemModifyJob. This can be obtained by, for
0077  * example, calling item().revision() in the job's result slot.
0078  *
0079  * @author Volker Krause <vkrause@kde.org>
0080  */
0081 class AKONADICORE_EXPORT ItemModifyJob : public Job
0082 {
0083     friend class ResourceBase;
0084 
0085     Q_OBJECT
0086 
0087 public:
0088     /**
0089      * Creates a new item modify job.
0090      *
0091      * @param item The modified item object to store.
0092      * @param parent The parent object.
0093      */
0094     explicit ItemModifyJob(const Item &item, QObject *parent = nullptr);
0095 
0096     /**
0097      * Creates a new item modify job for bulk modifications.
0098      *
0099      * Using this is different from running a modification job per item.
0100      * Use this when applying the same change to a set of items, such as a
0101      * mass-change of item flags, not if you just want to store a bunch of
0102      * randomly modified items.
0103      *
0104      * Currently the following modifications are supported:
0105      * - flag changes
0106      *
0107      * @note Since this does not do payload modifications, it implies
0108      *       setIgnorePayload( true ) and disableRevisionCheck().
0109      * @param items The list of items to modify, must not be empty.
0110      * @since 4.6
0111      */
0112     explicit ItemModifyJob(const Item::List &items, QObject *parent = nullptr);
0113 
0114     /**
0115      * Destroys the item modify job.
0116      */
0117     ~ItemModifyJob() override;
0118 
0119     /**
0120      * Sets whether the payload of the modified item shall be
0121      * omitted from transmission to the Akonadi storage.
0122      * The default is @c false, however it can be set for
0123      * performance reasons.
0124      * @param ignore ignores payload if set as @c true
0125      */
0126     void setIgnorePayload(bool ignore);
0127 
0128     /**
0129      * Returns whether the payload of the modified item shall be
0130      * omitted from transmission to the Akonadi storage.
0131      */
0132     [[nodiscard]] bool ignorePayload() const;
0133 
0134     /**
0135      * Sets whether the GID shall be updated either from the gid parameter or
0136      * by extracting it from the payload.
0137      * The default is @c false to avoid unnecessarily update the GID,
0138      * as it should never change once set, and the ItemCreateJob already sets it.
0139      * @param update update the GID if set as @c true
0140      *
0141      * @note If disabled the GID will not be updated, but still be used for identification of the item.
0142      * @since 4.12
0143      */
0144     void setUpdateGid(bool update);
0145 
0146     /**
0147      * Returns whether the GID should be updated.
0148      * @since 4.12
0149      */
0150     [[nodiscard]] bool updateGid() const;
0151 
0152     /**
0153      * Disables the check of the revision number.
0154      *
0155      * @note If disabled, no conflict detection is available.
0156      */
0157     void disableRevisionCheck();
0158 
0159     /**
0160      * Returns the modified and stored item including the changed revision number.
0161      *
0162      * @note Use this method only when using the single item constructor.
0163      */
0164     [[nodiscard]] Item item() const;
0165 
0166     /**
0167      * Returns the modified and stored items including the changed revision number.
0168      *
0169      * @since 4.6
0170      */
0171     [[nodiscard]] Item::List items() const;
0172 
0173     /**
0174      * Disables the automatic handling of conflicts.
0175      *
0176      * By default the item modify job will bring up a dialog to resolve
0177      * a conflict that might happen when modifying an item.
0178      * Calling this method will avoid that and the job returns with an
0179      * error in case of a conflict.
0180      *
0181      * @since 4.6
0182      */
0183     void disableAutomaticConflictHandling();
0184 
0185 protected:
0186     void doStart() override;
0187     bool doHandleResponse(qint64 tag, const Protocol::CommandPtr &response) override;
0188 
0189 private:
0190     /// @cond PRIVATE
0191     Q_DECLARE_PRIVATE(ItemModifyJob)
0192     /// @endcond
0193 };
0194 
0195 }