itemfetchjob.h 8 KB
Newer Older
Cornelius Schumacher's avatar
Cornelius Schumacher committed
1
/*
2
    SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
Cornelius Schumacher's avatar
Cornelius Schumacher committed
3

4
    SPDX-License-Identifier: LGPL-2.0-or-later
Cornelius Schumacher's avatar
Cornelius Schumacher committed
5
6
*/

7
#pragma once
Cornelius Schumacher's avatar
Cornelius Schumacher committed
8

Daniel Vrátil's avatar
Daniel Vrátil committed
9
10
11
#include "akonadicore_export.h"
#include "item.h"
#include "job.h"
Cornelius Schumacher's avatar
Cornelius Schumacher committed
12

Laurent Montel's avatar
Laurent Montel committed
13
14
namespace Akonadi
{
15
class Collection;
16
class ItemFetchJobPrivate;
17
class ItemFetchScope;
18

Cornelius Schumacher's avatar
Cornelius Schumacher committed
19
/**
Tobias Koenig's avatar
Tobias Koenig committed
20
21
22
23
24
25
 * @short Job that fetches items from the Akonadi storage.
 *
 * This class is used to fetch items from the Akonadi storage.
 * Which parts of the items (e.g. headers only, attachments or all)
 * can be specified by the ItemFetchScope.
 *
David Jarvie's avatar
David Jarvie committed
26
27
28
29
30
 * Note that ItemFetchJob does not refresh the Akonadi storage from the
 * backend; this is unnecessary due to the fact that backend updates
 * automatically trigger an update to the Akonadi database whenever they occur
 * (unless the resource is offline).
 *
31
32
33
34
35
 * Note that items can not be created in the root collection (Collection::root())
 * and therefore can not be fetched from there either. That is - an item fetch in
 * the root collection will yield an empty list.
 *
 *
Tobias Koenig's avatar
Tobias Koenig committed
36
37
38
39
 * Example:
 *
 * @code
 *
40
41
42
43
 * // Fetch all items with full payload from a collection
 *
 * const Collection collection = getCollection();
 *
44
45
 * Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(collection);
 * connect(job, SIGNAL(result(KJob*)), SLOT(jobFinished(KJob*)));
Tobias Koenig's avatar
Tobias Koenig committed
46
47
 * job->fetchScope().fetchFullPayload();
 *
48
49
 * ...
 *
50
 * MyClass::jobFinished(KJob *job)
51
 * {
52
 *   if (job->error()) {
53
54
55
56
 *     qDebug() << "Error occurred";
 *     return;
 *   }
 *
57
 *   Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>(job);
58
59
 *
 *   const Akonadi::Item::List items = fetchJob->items();
Daniel Vrátil's avatar
Daniel Vrátil committed
60
 *   for (const Akonadi::Item &item : items) {
Cornelius Schumacher's avatar
Cornelius Schumacher committed
61
 *     qDebug() << "Item ID:" << item.id();
Tobias Koenig's avatar
Tobias Koenig committed
62
63
64
65
66
67
68
 *   }
 * }
 *
 * @endcode
 *
 * @author Volker Krause <vkrause@kde.org>
 */
Daniel Vrátil's avatar
Daniel Vrátil committed
69
class AKONADICORE_EXPORT ItemFetchJob : public Job
Cornelius Schumacher's avatar
Cornelius Schumacher committed
70
71
{
    Q_OBJECT
72
    Q_FLAGS(DeliveryOptions)
Guy Maurel's avatar
Guy Maurel committed
73
public:
Cornelius Schumacher's avatar
Cornelius Schumacher committed
74
    /**
75
     * Creates a new item fetch job that retrieves all items inside the given collection.
Tobias Koenig's avatar
Tobias Koenig committed
76
77
78
79
     *
     * @param collection The parent collection to fetch all items from.
     * @param parent The parent object.
     */
Laurent Montel's avatar
Laurent Montel committed
80
    explicit ItemFetchJob(const Collection &collection, QObject *parent = nullptr);
81
82

    /**
83
     * Creates a new item fetch job that retrieves the specified item.
David Jarvie's avatar
David Jarvie committed
84
85
86
     * If the item has a uid set, this is used to identify the item on the Akonadi
     * server. If only a remote identifier is available, that is used.
     * However, as remote identifiers are not necessarily globally unique, you
87
88
89
90
91
92
93
     * need to specify the collection to search in in that case, using
     * setCollection().
     *
     * @internal
     * For internal use only when using remote identifiers, the resource search
     * context can be set globally by ResourceSelectJob.
     * @endinternal
Tobias Koenig's avatar
Tobias Koenig committed
94
95
96
97
     *
     * @param item The item to fetch.
     * @param parent The parent object.
     */
Laurent Montel's avatar
Laurent Montel committed
98
    explicit ItemFetchJob(const Item &item, QObject *parent = nullptr);
Cornelius Schumacher's avatar
Cornelius Schumacher committed
99

100
101
    /**
     * Creates a new item fetch job that retrieves the specified items.
David Jarvie's avatar
David Jarvie committed
102
103
104
     * If the items have a uid set, this is used to identify the item on the Akonadi
     * server. If only a remote identifier is available, that is used.
     * However, as remote identifiers are not necessarily globally unique, you
105
106
107
108
109
110
111
     * need to specify the collection to search in in that case, using
     * setCollection().
     *
     * @internal
     * For internal use only when using remote identifiers, the resource search
     * context can be set globally by ResourceSelectJob.
     * @endinternal
112
113
114
     *
     * @param items The items to fetch.
     * @param parent The parent object.
115
     * @since 4.4
116
     */
Laurent Montel's avatar
Laurent Montel committed
117
    explicit ItemFetchJob(const Item::List &items, QObject *parent = nullptr);
118

119
    /**
Laurent Montel's avatar
Laurent Montel committed
120
     * Convenience ctor equivalent to ItemFetchJob(const Item::List &items, QObject *parent = nullptr)
121
122
     * @since 4.8
     */
Laurent Montel's avatar
Laurent Montel committed
123
    explicit ItemFetchJob(const QList<Item::Id> &items, QObject *parent = nullptr);
124

125
    /**
Laurent Montel's avatar
Laurent Montel committed
126
     * Convenience ctor equivalent to ItemFetchJob(const Item::List &items, QObject *parent = nullptr)
127
128
     * @since 5.4
     */
Laurent Montel's avatar
Laurent Montel committed
129
    explicit ItemFetchJob(const QVector<Item::Id> &items, QObject *parent = nullptr);
130

131
132
133
134
135
136
137
138
    /**
     * Creates a new item fetch job that retrieves all items tagged with specified @p tag.
     *
     * @param tag The tag to fetch all items from.
     * @param parent The parent object.
     *
     * @since 4.14
     */
Laurent Montel's avatar
Laurent Montel committed
139
    explicit ItemFetchJob(const Tag &tag, QObject *parent = nullptr);
140

Cornelius Schumacher's avatar
Cornelius Schumacher committed
141
    /**
Tobias Koenig's avatar
Tobias Koenig committed
142
143
     * Destroys the item fetch job.
     */
Laurent Montel's avatar
Laurent Montel committed
144
    ~ItemFetchJob() override;
Cornelius Schumacher's avatar
Cornelius Schumacher committed
145
146

    /**
147
     * Returns the fetched items.
Tobias Koenig's avatar
Tobias Koenig committed
148
     *
149
150
     * This returns an empty list when not using the ItemGetter DeliveryOption.
     *
151
     * @note The items are invalid before the result(KJob*)
Tobias Koenig's avatar
Tobias Koenig committed
152
153
     *       signal has been emitted or if an error occurred.
     */
Laurent Montel's avatar
Laurent Montel committed
154
    Q_REQUIRED_RESULT Item::List items() const;
Cornelius Schumacher's avatar
Cornelius Schumacher committed
155

156
157
158
159
160
161
    /**
     * Save memory by clearing the fetched items.
     * @since 4.12
     */
    void clearItems();

Volker Krause's avatar
Volker Krause committed
162
163
164
165
166
167
168
169
170
171
    /**
     * Sets the item fetch scope.
     *
     * The ItemFetchScope controls how much of an item's data is fetched
     * from the server, e.g. whether to fetch the full item payload or
     * only meta data.
     *
     * @param fetchScope The new scope for item fetch operations.
     *
     * @see fetchScope()
Tobias Koenig's avatar
Tobias Koenig committed
172
     * @since 4.4
Volker Krause's avatar
Volker Krause committed
173
     */
Guy Maurel's avatar
Guy Maurel committed
174
    void setFetchScope(const ItemFetchScope &fetchScope);
175

176
    /**
Tobias Koenig's avatar
Tobias Koenig committed
177
178
179
180
181
182
183
184
185
186
187
     * Returns the item fetch scope.
     *
     * Since this returns a reference it can be used to conveniently modify the
     * current scope in-place, i.e. by calling a method on the returned reference
     * without storing it in a local variable. See the ItemFetchScope documentation
     * for an example.
     *
     * @return a reference to the current item fetch scope
     *
     * @see setFetchScope() for replacing the current item fetch scope
     */
188
    ItemFetchScope &fetchScope();
189

190
    /**
191
     * Specifies the collection the item is in.
192
193
     * This is only required when retrieving an item based on its remote id
     * which might not be unique globally.
194
     *
195
196
197
     * @internal
     * @see ResourceSelectJob (for internal use only)
     * @endinternal
198
     */
Guy Maurel's avatar
Guy Maurel committed
199
    void setCollection(const Collection &collection);
200

201
    enum DeliveryOption {
Laurent Montel's avatar
Laurent Montel committed
202
        ItemGetter = 0x1, ///< items available through items()
Guy Maurel's avatar
Guy Maurel committed
203
        EmitItemsIndividually = 0x2, ///< emitted via signal upon reception
Laurent Montel's avatar
Laurent Montel committed
204
        EmitItemsInBatches = 0x4, ///< emitted via signal in bulk (collected and emitted delayed via timer)
Guy Maurel's avatar
Guy Maurel committed
205
        Default = ItemGetter | EmitItemsInBatches
206
    };
Laurent Montel's avatar
Laurent Montel committed
207
    Q_DECLARE_FLAGS(DeliveryOptions, DeliveryOption)
208
209
210
211
212

    /**
     * Sets the mechanisms by which the items should be fetched
     * @since 4.13
     */
Guy Maurel's avatar
Guy Maurel committed
213
    void setDeliveryOption(DeliveryOptions options);
214
215
216
217
218
219
220

    /**
     * Returns the delivery options
     * @since 4.13
     */
    DeliveryOptions deliveryOptions() const;

221
222
223
    /**
     * Returns the total number of retrieved items.
     * This works also without the ItemGetter DeliveryOption.
224
     * @since 4.14
225
226
227
     */
    int count() const;

228
229
230
231
    /**
     * Sets the limit of fetched items.
     *
     * @param limit the maximum number of items to retrieve.
232
     * The default value for @p limit is -1, indicating no limit.
233
234
235
236
237
238
239
     * @param start specifies the offset of the first item to retrieve.
     * @param order specifies whether items will be fetched
     * starting with the highest or lowest ID of the item.
     */

    void setLimit(int limit, int start, Qt::SortOrder order = Qt::DescendingOrder);

Guy Maurel's avatar
Guy Maurel committed
240
Q_SIGNALS:
241
    /**
Tobias Koenig's avatar
Tobias Koenig committed
242
243
     * This signal is emitted whenever new items have been fetched completely.
     *
David Jarvie's avatar
David Jarvie committed
244
     * @note This is an optimization; instead of waiting for the end of the job
Tobias Koenig's avatar
Tobias Koenig committed
245
246
     *       and calling items(), you can connect to this signal and get the items
     *       incrementally.
Tobias Koenig's avatar
Tobias Koenig committed
247
248
249
     *
     * @param items The fetched items.
     */
Guy Maurel's avatar
Guy Maurel committed
250
    void itemsReceived(const Akonadi::Item::List &items);
251

Guy Maurel's avatar
Guy Maurel committed
252
protected:
253
254
    void doStart() override;
    bool doHandleResponse(qint64 tag, const Protocol::CommandPtr &response) override;
Cornelius Schumacher's avatar
Cornelius Schumacher committed
255

Guy Maurel's avatar
Guy Maurel committed
256
257
private:
    Q_DECLARE_PRIVATE(ItemFetchJob)
Cornelius Schumacher's avatar
Cornelius Schumacher committed
258
259
260
261
};

}

Laurent Montel's avatar
Laurent Montel committed
262
Q_DECLARE_OPERATORS_FOR_FLAGS(Akonadi::ItemFetchJob::DeliveryOptions)
263