File indexing completed on 2024-11-17 04:45:09

0001 /*
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003     SPDX-FileContributor: Kevin Ottens <kevin@kdab.com>
0004     SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include <KIMAP/FetchJob>
0012 
0013 #include "resourcetask.h"
0014 #include <QElapsedTimer>
0015 class BatchFetcher;
0016 namespace Akonadi
0017 {
0018 class Session;
0019 }
0020 
0021 class RetrieveItemsTask : public ResourceTask
0022 {
0023     Q_OBJECT
0024 
0025 public:
0026     explicit RetrieveItemsTask(const ResourceStateInterface::Ptr &resource, QObject *parent = nullptr);
0027     ~RetrieveItemsTask() override;
0028     void setFetchMissingItemBodies(bool enabled);
0029 
0030 public Q_SLOTS:
0031     void onFetchItemsWithoutBodiesDone(const QList<qint64> &items);
0032     void onReadyForNextBatch(int size);
0033 
0034 private Q_SLOTS:
0035     void fetchItemsWithoutBodiesDone(KJob *job);
0036     void onPreExpungeSelectDone(KJob *job);
0037     void onExpungeDone(KJob *job);
0038     void onFinalSelectDone(KJob *job);
0039     void onStatusDone(KJob *job);
0040     void onItemsRetrieved(const Akonadi::Item::List &addedItems);
0041     void onRetrievalDone(KJob *job);
0042     void onFlagsFetchDone(KJob *job);
0043 
0044 protected:
0045     void doStart(KIMAP::Session *session) override;
0046 
0047     virtual BatchFetcher *createBatchFetcher(MessageHelper::Ptr messageHelper,
0048                                              const KIMAP::ImapSet &set,
0049                                              const KIMAP::FetchJob::FetchScope &scope,
0050                                              int batchSize,
0051                                              KIMAP::Session *session);
0052 
0053 private:
0054     void prepareRetrieval();
0055     void startRetrievalTasks();
0056     void triggerPreExpungeSelect(const QString &mailBox);
0057     void triggerExpunge(const QString &mailBox);
0058     void triggerFinalSelect(const QString &mailBox);
0059     void retrieveItems(const KIMAP::ImapSet &set, const KIMAP::FetchJob::FetchScope &scope, bool incremental = false, bool uidBased = false);
0060     void listFlagsForImapSet(const KIMAP::ImapSet &set);
0061     void taskComplete();
0062 
0063     KIMAP::Session *m_session = nullptr;
0064     QList<qint64> m_messageUidsMissingBody;
0065     int m_fetchedMissingBodies = -1;
0066     bool m_fetchMissingBodies = false;
0067     bool m_incremental = true;
0068     qint64 m_localHighestModSeq = -1;
0069     BatchFetcher *m_batchFetcher = nullptr;
0070     Akonadi::Collection m_modifiedCollection;
0071     bool m_uidBasedFetch = true;
0072     bool m_flagsChanged = false;
0073     QElapsedTimer m_time;
0074 
0075     // Results of SELECT
0076     QString m_mailBox;
0077     int m_messageCount = -1;
0078     int m_uidValidity = -1;
0079     qint64 m_nextUid = -1;
0080     qint64 m_highestModSeq = -1;
0081     QList<QByteArray> m_flags;
0082 };