1 /*
2  * SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #pragma once
9 
10 #include <KIMAP/FetchJob>
11 #include <KIMAP/SearchJob>
12 #include <KJob>
13 
14 #include "messagehelper.h"
15 
16 /**
17  * A job that retrieves a set of messages in reverse-ordered batches.
18  * After each batch fetchNextBatch() needs to be called (for throttling the download speed)
19  */
20 class BatchFetcher : public KJob
21 {
22     Q_OBJECT
23 public:
24     BatchFetcher(MessageHelper::Ptr messageHelper, const KIMAP::ImapSet &set, const KIMAP::FetchJob::FetchScope &scope, int batchSize, KIMAP::Session *session);
25     ~BatchFetcher() override;
26     void start() override;
27     void fetchNextBatch();
28     void setUidBased(bool);
29     void setSearchUids(const KIMAP::ImapInterval &interval);
30     void setGmailExtensionsEnabled(bool enable);
31 
32 Q_SIGNALS:
33     void itemsRetrieved(const Akonadi::Item::List &);
34 
35 private Q_SLOTS:
36     void onMessagesAvailable(const QMap<qint64, KIMAP::Message> &messages);
37     void onHeadersFetchDone(KJob *job);
38     void onUidSearchDone(KJob *job);
39 
40 private:
41     // Batch fetching
42     KIMAP::ImapSet m_currentSet;
43     const KIMAP::FetchJob::FetchScope m_scope;
44     KIMAP::Session *const m_session;
45     const int m_batchSize;
46     bool m_uidBased = false;
47     int m_fetchedItemsInCurrentBatch = 0;
48     const MessageHelper::Ptr m_messageHelper;
49     bool m_fetchInProgress = false;
50     bool m_continuationRequested = false;
51     KIMAP::ImapInterval m_searchUidInterval;
52     bool m_gmailEnabled = false;
53     bool m_searchInChunks = false;
54 };
55 
56