1 /*
2     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3     SPDX-FileContributor: Kevin Ottens <kevin@kdab.com>
4     SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
5 
6     SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #pragma once
10 
11 #include <KIMAP/FetchJob>
12 
13 #include "resourcetask.h"
14 #include <QElapsedTimer>
15 class BatchFetcher;
16 namespace Akonadi
17 {
18 class Session;
19 }
20 
21 class RetrieveItemsTask : public ResourceTask
22 {
23     Q_OBJECT
24 
25 public:
26     explicit RetrieveItemsTask(const ResourceStateInterface::Ptr &resource, QObject *parent = nullptr);
27     ~RetrieveItemsTask() override;
28     void setFetchMissingItemBodies(bool enabled);
29 
30 public Q_SLOTS:
31     void onFetchItemsWithoutBodiesDone(const QVector<qint64> &items);
32     void onReadyForNextBatch(int size);
33 
34 private Q_SLOTS:
35     void fetchItemsWithoutBodiesDone(KJob *job);
36     void onPreExpungeSelectDone(KJob *job);
37     void onExpungeDone(KJob *job);
38     void onFinalSelectDone(KJob *job);
39     void onStatusDone(KJob *job);
40     void onItemsRetrieved(const Akonadi::Item::List &addedItems);
41     void onRetrievalDone(KJob *job);
42     void onFlagsFetchDone(KJob *job);
43 
44 protected:
45     void doStart(KIMAP::Session *session) override;
46 
47     virtual BatchFetcher *createBatchFetcher(MessageHelper::Ptr messageHelper,
48                                              const KIMAP::ImapSet &set,
49                                              const KIMAP::FetchJob::FetchScope &scope,
50                                              int batchSize,
51                                              KIMAP::Session *session);
52 
53 private:
54     void prepareRetrieval();
55     void startRetrievalTasks();
56     void triggerPreExpungeSelect(const QString &mailBox);
57     void triggerExpunge(const QString &mailBox);
58     void triggerFinalSelect(const QString &mailBox);
59     void retrieveItems(const KIMAP::ImapSet &set, const KIMAP::FetchJob::FetchScope &scope, bool incremental = false, bool uidBased = false);
60     void listFlagsForImapSet(const KIMAP::ImapSet &set);
61     void taskComplete();
62 
63     KIMAP::Session *m_session = nullptr;
64     QVector<qint64> m_messageUidsMissingBody;
65     int m_fetchedMissingBodies = -1;
66     bool m_fetchMissingBodies = false;
67     bool m_incremental = true;
68     qint64 m_localHighestModSeq = -1;
69     BatchFetcher *m_batchFetcher = nullptr;
70     Akonadi::Collection m_modifiedCollection;
71     bool m_uidBasedFetch = true;
72     bool m_flagsChanged = false;
73     QElapsedTimer m_time;
74 
75     // Results of SELECT
76     QString m_mailBox;
77     int m_messageCount = -1;
78     int m_uidValidity = -1;
79     qint64 m_nextUid = -1;
80     qint64 m_highestModSeq = -1;
81     QList<QByteArray> m_flags;
82 };
83 
84