Akonadi

itemsync.h
1/*
2 SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "akonadicore_export.h"
10#include "item.h"
11#include "job.h"
12
13#include <QDateTime>
14
15namespace Akonadi
16{
17class Collection;
18class ItemSyncPrivate;
19
20/**
21 * @short Syncs between items known to a client (usually a resource) and the Akonadi storage.
22 *
23 * Remote Id must only be set by the resource storing the item, other clients
24 * should leave it empty, since the resource responsible for the target collection
25 * will be notified about the addition and then create a suitable remote Id.
26 *
27 * There are two different forms of ItemSync usage:
28 * - Full-Sync: meaning the client provides all valid items, i.e. any item not
29 * part of the list but currently stored in Akonadi will be removed
30 * - Incremental-Sync: meaning the client provides two lists, one for items which
31 * are new or modified and one for items which should be removed. Any item not
32 * part of either list but currently stored in Akonadi will not be changed.
33 *
34 * @note This is provided for convenience to implement "save all" like behavior,
35 * however it is strongly recommended to use single item jobs whenever
36 * possible, e.g. ItemCreateJob, ItemModifyJob and ItemDeleteJob
37 *
38 * @author Tobias Koenig <tokoe@kde.org>
39 */
40class AKONADICORE_EXPORT ItemSync : public Job
41{
42 Q_OBJECT
43
44public:
45 enum MergeMode {
46 RIDMerge,
47 GIDMerge,
48 };
49
50 /**
51 * Creates a new item synchronizer.
52 *
53 * @param collection The collection we are syncing.
54 * @param timestamp Optional timestamp of itemsync start. Will be used to detect local changes that happen
55 while the ItemSync is running.
56 * @param parent The parent object.
57 */
58 explicit ItemSync(const Collection &collection, const QDateTime &timestamp = {}, QObject *parent = nullptr);
59
60 /**
61 * Destroys the item synchronizer.
62 */
63 ~ItemSync() override;
64
65 /**
66 * Sets the full item list for the collection.
67 *
68 * Usually the result of a full item listing.
69 *
70 * @warning If the client using this is a resource, all items must have
71 * a valid remote identifier.
72 *
73 * @param items A list of items.
74 */
75 void setFullSyncItems(const Item::List &items);
76
77 /**
78 * Set the amount of items which you are going to return in total
79 * by using the setFullSyncItems()/setIncrementalSyncItems() methods.
80 *
81 * @warning By default the item sync will automatically end once
82 * sufficient items have been provided.
83 * To disable this use setDisableAutomaticDeliveryDone
84 *
85 * @see setDisableAutomaticDeliveryDone
86 * @param amount The amount of items in total.
87 */
88 void setTotalItems(int amount);
89
90 /**
91 Enable item streaming. Item streaming means that the items delivered by setXItems() calls
92 are delivered in chunks and you manually indicate when all items have been delivered
93 by calling deliveryDone().
94 @param enable @c true to enable item streaming
95 */
96 void setStreamingEnabled(bool enable);
97
98 /**
99 Notify ItemSync that all remote items have been delivered.
100 Only call this in streaming mode.
101 */
102 void deliveryDone();
103
104 /**
105 * Sets the item lists for incrementally syncing the collection.
106 *
107 * Usually the result of an incremental remote item listing.
108 *
109 * @warning If the client using this is a resource, all items must have
110 * a valid remote identifier.
111 *
112 * @param changedItems A list of items added or changed by the client.
113 * @param removedItems A list of items deleted by the client.
114 */
115 void setIncrementalSyncItems(const Item::List &changedItems, const Item::List &removedItems);
116
117 /**
118 * Aborts the sync process and rolls back all not yet committed transactions.
119 * Use this if an external error occurred during the sync process (such as the
120 * user canceling it).
121 * @since 4.5
122 */
123 void rollback();
124
125 /**
126 * Transaction mode used by ItemSync.
127 * @since 4.6
128 */
130 SingleTransaction, ///< Use a single transaction for the entire sync process (default), provides maximum consistency ("all or nothing") and best
131 ///< performance
132 MultipleTransactions, ///< Use one transaction per chunk of delivered items, good compromise between the other two when using streaming
133 NoTransaction ///< Use no transaction at all, provides highest responsiveness (might therefore feel faster even when actually taking slightly longer),
134 ///< no consistency guaranteed (can fail anywhere in the sync process)
135 };
136
137 /**
138 * Set the transaction mode to use for this sync.
139 * @note You must call this method before starting the sync, changes afterwards lead to undefined results.
140 * @param mode the transaction mode to use
141 * @since 4.6
142 */
143 void setTransactionMode(TransactionMode mode);
144
145 /**
146 * Minimum number of items required to start processing in streaming mode.
147 * When MultipleTransactions is used, one transaction per batch will be created.
148 *
149 * @see setBatchSize()
150 * @since 4.14
151 */
152 [[nodiscard]] int batchSize() const;
153
154 /**
155 * Set the batch size.
156 *
157 * The default is 10.
158 *
159 * @note You must call this method before starting the sync, changes afterwards lead to undefined results.
160 * @see batchSize()
161 * @since 4.14
162 */
163 void setBatchSize(int);
164
165 /**
166 * Disables the automatic completion of the item sync,
167 * based on the number of delivered items.
168 *
169 * This ensures that the item sync only finishes once deliveryDone()
170 * is called, while still making it possible to use the progress
171 * reporting of the ItemSync.
172 *
173 * @note You must call this method before starting the sync, changes afterwards lead to undefined results.
174 * @see setTotalItems
175 * @since 4.14
176 */
177 void setDisableAutomaticDeliveryDone(bool disable);
178
179 /**
180 * Returns current merge mode
181 *
182 * @see setMergeMode()
183 * @since 5.1
184 */
185 [[nodiscard]] MergeMode mergeMode() const;
186
187 /**
188 * Set what merge method should be used for next ItemSync run
189 *
190 * By default ItemSync uses RIDMerge method.
191 *
192 * See ItemCreateJob for details on Item merging.
193 *
194 * @note You must call this method before starting the sync, changes afterwards lead to undefined results.
195 * @see mergeMode
196 * @since 4.14.11
197 */
198 void setMergeMode(MergeMode mergeMode);
199
200Q_SIGNALS:
201 /**
202 * Signals the resource that new items can be delivered.
203 * @param remainingBatchSize the number of items required to complete the batch (typically the same as batchSize())
204 *
205 * @since 4.14
206 */
207 void readyForNextBatch(int remainingBatchSize);
208
209 /**
210 * @internal
211 * Emitted whenever a transaction is committed. This is for testing only.
212 *
213 * @since 4.14
214 */
216
217protected:
218 void doStart() override;
219 void slotResult(KJob *job) override;
220
221private:
222 /// @cond PRIVATE
223 Q_DECLARE_PRIVATE(ItemSync)
224
225 Q_PRIVATE_SLOT(d_func(), void slotLocalListDone(KJob *))
226 Q_PRIVATE_SLOT(d_func(), void slotTransactionResult(KJob *))
227 Q_PRIVATE_SLOT(d_func(), void slotItemsReceived(const Akonadi::Item::List &))
228 /// @endcond
229};
230
231}
Represents a collection of PIM items.
Definition collection.h:62
Syncs between items known to a client (usually a resource) and the Akonadi storage.
Definition itemsync.h:41
void readyForNextBatch(int remainingBatchSize)
Signals the resource that new items can be delivered.
TransactionMode
Transaction mode used by ItemSync.
Definition itemsync.h:129
@ MultipleTransactions
Use one transaction per chunk of delivered items, good compromise between the other two when using st...
Definition itemsync.h:132
void transactionCommitted()
Base class for all actions in the Akonadi storage.
Definition job.h:81
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 11:58:37 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.