QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
syncmanager.h
1 #ifndef QTDATASYNC_SYNCMANAGER_H
2 #define QTDATASYNC_SYNCMANAGER_H
3 
4 #include <functional>
5 
6 #include <QtCore/qobject.h>
7 #include <QtCore/qscopedpointer.h>
8 #include <QtCore/quuid.h>
9 
10 #include "QtDataSync/qtdatasync_global.h"
11 
12 class QRemoteObjectNode;
14 class SyncManagerPrivateReplica;
15 
16 namespace QtDataSync {
17 
18 class SyncManagerPrivateHolder;
20 class Q_DATASYNC_EXPORT SyncManager : public QObject
21 {
22  Q_OBJECT
23 
25  Q_PROPERTY(QString setupName READ setupName NOTIFY setupNameChanged REVISION 2)
27  Q_PROPERTY(bool syncEnabled READ isSyncEnabled WRITE setSyncEnabled NOTIFY syncEnabledChanged)
29  Q_PROPERTY(SyncState syncState READ syncState NOTIFY syncStateChanged)
31  Q_PROPERTY(qreal syncProgress READ syncProgress NOTIFY syncProgressChanged)
33  Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged)
34 
35 public:
37  enum SyncState {
43  Disconnected
44  };
45  Q_ENUM(SyncState)
46 
47 
48  explicit SyncManager(QObject *parent = nullptr);
50  explicit SyncManager(const QString &setupName, QObject *parent = nullptr);
52  explicit SyncManager(QRemoteObjectNode *node, QObject *parent = nullptr);
53  ~SyncManager() override;
54 
56  Q_INVOKABLE QRemoteObjectReplica *replica() const;
57 
59  QString setupName() const;
61  bool isSyncEnabled() const;
63  SyncState syncState() const;
65  qreal syncProgress() const;
67  QString lastError() const;
68 
70  void runOnDownloaded(const std::function<void(SyncState)> &resultFn, bool triggerSync = true);
72  void runOnSynchronized(const std::function<void(SyncState)> &resultFn, bool triggerSync = true);
73 
74 public Q_SLOTS:
76  void setSyncEnabled(bool syncEnabled);
77 
79  void synchronize();
81  void reconnect();
82 
83 Q_SIGNALS:
85  QT_DATASYNC_REVISION_2 void setupNameChanged(const QString &setupName, QPrivateSignal);
87  void syncEnabledChanged(bool syncEnabled, QPrivateSignal);
89  void syncStateChanged(QtDataSync::SyncManager::SyncState syncState, QPrivateSignal);
91  void syncProgressChanged(qreal syncProgress, QPrivateSignal);
93  void lastErrorChanged(const QString &lastError, QPrivateSignal);
94 
95 protected:
97  SyncManager(QObject *parent, void *);
99  void initReplica(const QString &setupName);
101  void initReplica(QRemoteObjectNode *node);
102 
103 private Q_SLOTS:
104  void onInit();
105  void onStateReached(QUuid id, SyncState state);
106 
107 private:
109 
110  void runImp(bool downloadOnly, bool triggerSync, const std::function<void(SyncState)> &resultFn);
111 };
112 
114 Q_DATASYNC_EXPORT QDataStream &operator<<(QDataStream &stream, const SyncManager::SyncState &state);
116 Q_DATASYNC_EXPORT QDataStream &operator>>(QDataStream &stream, SyncManager::SyncState &state);
117 
118 }
119 
120 Q_DECLARE_METATYPE(QtDataSync::SyncManager::SyncState)
121 Q_DECLARE_TYPEINFO(QtDataSync::SyncManager::SyncState, Q_PRIMITIVE_TYPE);
122 
123 #endif // QTDATASYNC_SYNCMANAGER_H
Uploading changes to the remote.
Definition: syncmanager.h:40
Downloading changes from the remote.
Definition: syncmanager.h:39
An internal error occured. Synchronization is paused until reconnect() is called. ...
Definition: syncmanager.h:42
Q_DATASYNC_EXPORT QDataStream & operator<<(QDataStream &stream, const DeviceInfo &deviceInfo)
Stream operator to stream into a QDataStream.
Initializing internal stuff and connecting to a remote.
Definition: syncmanager.h:38
The primary namespace of the QtDataSync library.
SyncState
The possible states the sync engine can be in.
Definition: syncmanager.h:37
All changes have been synchronized. The engine is idle.
Definition: syncmanager.h:41
Q_DATASYNC_EXPORT QDataStream & operator>>(QDataStream &stream, DeviceInfo &deviceInfo)
Stream operator to stream out of a QDataStream.
Manages the synchronization process and reports its state.
Definition: syncmanager.h:20