QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
eventcursor.h
1 #ifndef QTDATASYNC_EVENTCURSOR_H
2 #define QTDATASYNC_EVENTCURSOR_H
3 
4 #include <functional>
5 
6 #include <QtCore/qobject.h>
7 #include <QtCore/qscopedpointer.h>
8 #include <QtCore/qdatetime.h>
9 #include <QtCore/qbytearray.h>
10 
11 #include "QtDataSync/qtdatasync_global.h"
12 #include "QtDataSync/objectkey.h"
13 #include "QtDataSync/exception.h"
14 
15 namespace QtDataSync {
16 
17 class EventCursorPrivate;
19 class Q_DATASYNC_EXPORT EventCursor : public QObject
20 {
21  Q_OBJECT
22 
24  Q_PROPERTY(bool valid READ isValid CONSTANT)
26  Q_PROPERTY(QString setupName READ setupName CONSTANT)
27 
29  Q_PROPERTY(quint64 index READ index NOTIFY indexChanged USER true)
31  Q_PROPERTY(QtDataSync::ObjectKey key READ key NOTIFY keyChanged)
33  Q_PROPERTY(bool wasRemoved READ wasRemoved NOTIFY wasRemovedChanged)
35  Q_PROPERTY(QDateTime timestamp READ timestamp NOTIFY timestampChanged)
36 
38  Q_PROPERTY(bool skipObsolete READ skipObsolete WRITE setSkipObsolete NOTIFY skipObsoleteChanged)
39 
40 public:
41  ~EventCursor() override;
42 
44  static bool isEventLogActive(const QString &setupName = DefaultSetup);
45 
47  static EventCursor *first(QObject *parent = nullptr);
49  static EventCursor *first(const QString &setupName, QObject *parent = nullptr);
51  static EventCursor *last(QObject *parent = nullptr);
53  static EventCursor *last(const QString &setupName, QObject *parent = nullptr);
55  static EventCursor *create(quint64 index, QObject *parent = nullptr);
57  static EventCursor *create(quint64 index, const QString &setupName, QObject *parent = nullptr);
59  static EventCursor *load(const QByteArray &data, QObject *parent = nullptr);
61  static EventCursor *load(const QByteArray &data, const QString &setupName, QObject *parent = nullptr);
63  Q_INVOKABLE QByteArray save() const;
64 
66  bool isValid() const;
68  QString setupName() const;
69 
71  quint64 index() const;
73  QtDataSync::ObjectKey key() const;
75  bool wasRemoved() const;
77  QDateTime timestamp() const;
78 
80  bool skipObsolete() const;
81 
83  Q_INVOKABLE bool hasNext() const;
85  Q_INVOKABLE bool next();
86 
88  Q_INVOKABLE void autoScanLog();
90  void autoScanLog(std::function<bool(const EventCursor *)> function, bool scanCurrent = true);
92  void autoScanLog(QObject *scope, std::function<bool(const EventCursor *)> function, bool scanCurrent = true);
94  template <typename TClass>
95  void autoScanLog(TClass *scope, bool(TClass::* function)(const EventCursor *), bool scanCurrent = true);
96 
97 public Q_SLOTS:
99  void setSkipObsolete(bool skipObsolete);
100 
102  void clearEventLog(quint64 offset = 0);
103 
104 Q_SIGNALS:
106  void eventLogChanged(QPrivateSignal);
107 
109  void indexChanged(quint64 index, QPrivateSignal);
111  void keyChanged(const QtDataSync::ObjectKey &key, QPrivateSignal);
113  void wasRemovedChanged(bool wasRemoved, QPrivateSignal);
115  void timestampChanged(const QDateTime &timestamp, QPrivateSignal);
117  void skipObsoleteChanged(bool skipObsolete, QPrivateSignal);
118 
119 private:
120  friend class QtDataSync::EventCursorPrivate;
121  QScopedPointer<EventCursorPrivate> d;
122 
123  explicit EventCursor(const QString &setupName, QObject *parent = nullptr);
124 };
125 
127 class Q_DATASYNC_EXPORT EventCursorException : public Exception
128 {
129 public:
131  EventCursorException(const Defaults &defaults,
132  quint64 index,
133  QString context,
134  const QString &message);
135 
137  quint64 index() const;
139  QString context() const;
140 
141  QByteArray className() const noexcept override;
142  QString qWhat() const override;
143  void raise() const override;
144  QException *clone() const override;
145 
146 protected:
148  EventCursorException(const EventCursorException * const other);
149 
151  quint64 _index;
153  QString _context;
154 };
155 
156 // ------------- GENERIC IMPLEMENTATION -------------
157 
158 
159 template<typename TClass>
160 void EventCursor::autoScanLog(TClass *scope, bool (TClass::*function)(const EventCursor *), bool scanCurrent)
161 {
162  static_assert(std::is_base_of<QObject, TClass>::value, "TClass must extend QObject");
163  autoScanLog(scope, [scope, function](const EventCursor *cursor) -> bool {
164  return (scope->*function)(cursor);
165  }, scanCurrent);
166 }
167 
168 }
169 
170 #endif // QTDATASYNC_EVENTCURSOR_H
Q_INVOKABLE void autoScanLog()
Automatically scans though all change events.
The base class for all exceptions of QtDataSync.
Definition: exception.h:13
STL namespace.
Defines a unique key to identify a dataset globally.
Definition: objectkey.h:11
The primary namespace of the QtDataSync library.
Q_DATASYNC_EXPORT const QString DefaultSetup
The default setup name.
A cursor style class to read from the global change event log.
Definition: eventcursor.h:19
A helper class to get defaults per datasync instance (threadsafe)
Definition: defaults.h:61
Exception thrown from the event cursor if something goes wrong.
Definition: eventcursor.h:127