File indexing completed on 2024-06-16 04:50:11

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadicore_export.h"
0010 #include "monitor.h"
0011 
0012 class QSettings;
0013 
0014 namespace Akonadi
0015 {
0016 class ChangeRecorderPrivate;
0017 
0018 /**
0019  * @short Records and replays change notification.
0020  *
0021  * This class is responsible for recording change notifications while
0022  * an agent is not online and replaying the notifications when the agent
0023  * is online again. Therefore the agent doesn't have to care about
0024  * online/offline mode in its synchronization algorithm.
0025  *
0026  * Unlike Akonadi::Monitor this class only emits one change signal at a
0027  * time. To receive the next one you need to explicitly call replayNext().
0028  * If a signal is emitted that has no receivers, it's automatically skipped,
0029  * which means you only need to connect to signals you are actually interested
0030  * in.
0031  *
0032  * @author Volker Krause <vkrause@kde.org>
0033  */
0034 class AKONADICORE_EXPORT ChangeRecorder : public Monitor
0035 {
0036     Q_OBJECT
0037 public:
0038     /**
0039      * Creates a new change recorder.
0040      */
0041     explicit ChangeRecorder(QObject *parent = nullptr);
0042 
0043     /**
0044      * Destroys the change recorder.
0045      * All not yet processed changes are written back to the config file.
0046      */
0047     ~ChangeRecorder() override;
0048 
0049     /**
0050      * Sets the QSettings object used for persistent recorded changes.
0051      */
0052     void setConfig(QSettings *settings);
0053 
0054     /**
0055      * Returns whether there are recorded changes.
0056      */
0057     [[nodiscard]] bool isEmpty() const;
0058 
0059     /**
0060      * Removes the previously emitted change from the records.
0061      */
0062     void changeProcessed();
0063 
0064     /**
0065      * Enables change recording. If change recording is disabled, this class
0066      * behaves exactly like Akonadi::Monitor.
0067      * Change recording is enabled by default.
0068      * @param enable @c false to disable change recording. @c true by default
0069      */
0070     void setChangeRecordingEnabled(bool enable);
0071 
0072     /**
0073      * Debugging: dump current list of notifications, as saved on disk.
0074      */
0075     [[nodiscard]] QString dumpNotificationListToString() const;
0076 
0077 public Q_SLOTS:
0078     /**
0079      * Replay the next change notification and erase the previous one from the record.
0080      */
0081     void replayNext();
0082 
0083 Q_SIGNALS:
0084     /**
0085      * Emitted when new changes are recorded.
0086      */
0087     void changesAdded();
0088 
0089     /**
0090      * Emitted when replayNext() was called, but there was no valid change to replay.
0091      * This can happen when all pending changes have been filtered out, for example.
0092      * You only need to connect to this signal if you rely on one signal being emitted
0093      * as a result of calling replayNext().
0094      */
0095     void nothingToReplay();
0096 
0097 protected:
0098     /// @cond PRIVATE
0099     explicit ChangeRecorder(ChangeRecorderPrivate *d, QObject *parent = nullptr);
0100     /// @endcond
0101 
0102 private:
0103     /// @cond PRIVATE
0104     Q_DECLARE_PRIVATE(ChangeRecorder)
0105     /// @endcond
0106 };
0107 
0108 }