File indexing completed on 2024-11-24 04:44:19

0001 /*
0002     SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <KIMAP/FetchJob>
0010 #include <KJob>
0011 #include <KMime/Message>
0012 
0013 struct Merger {
0014     virtual ~Merger() = default;
0015 
0016     virtual KMime::Message::Ptr merge(const KMime::Message::Ptr &newMessage, const QList<KMime::Message::Ptr> &conflictingMessages) const = 0;
0017 };
0018 
0019 /**
0020  * This job appends a message, marks the old one as deleted, and returns the uid of the appended message.
0021  */
0022 class UpdateMessageJob : public KJob
0023 {
0024     Q_OBJECT
0025 public:
0026     UpdateMessageJob(const KMime::Message::Ptr &msg,
0027                      KIMAP::Session *session,
0028                      const QByteArray &kolabUid,
0029                      const QSharedPointer<Merger> &merger,
0030                      const QString &mailbox,
0031                      qint64 uidNext = -1,
0032                      qint64 oldUid = -1,
0033                      QObject *parent = nullptr);
0034 
0035     qint64 newUid() const;
0036 
0037     void start() override;
0038 
0039 private:
0040     void fetchHeaders();
0041     void searchForLatestVersion();
0042     void appendMessage();
0043 
0044 private Q_SLOTS:
0045     void onMessagesAvailable(const QMap<qint64, KIMAP::Message> &messages);
0046     void onHeadersFetchDone(KJob *job);
0047     void onSelectDone(KJob *job);
0048     void onSearchDone(KJob *job);
0049     void onConflictingMessagesReceived(const QMap<qint64, KIMAP::Message> &message);
0050     void onConflictingMessageFetchDone(KJob *job);
0051     void onReplaceDone(KJob *job);
0052 
0053 private:
0054     KIMAP::Session *mSession = nullptr;
0055     KMime::Message::Ptr mMessage;
0056     const QString mMailbox;
0057     qint64 mUidNext;
0058     qint64 mOldUid;
0059     KIMAP::ImapSet mOldUids;
0060     qint64 mNewUid = -1;
0061     const QByteArray mMessageId;
0062     const QByteArray mKolabUid;
0063     QList<qint64> mFoundUids;
0064     QList<KIMAP::MessagePtr> mMessagesToMerge;
0065     QSharedPointer<Merger> mMerger;
0066 };