This repository was archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsyncmanager.h
123 lines (100 loc) · 4.42 KB
/
syncmanager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef QTDATASYNC_SYNCMANAGER_H
#define QTDATASYNC_SYNCMANAGER_H
#include <functional>
#include <QtCore/qobject.h>
#include <QtCore/qscopedpointer.h>
#include <QtCore/quuid.h>
#include "QtDataSync/qtdatasync_global.h"
class QRemoteObjectNode;
class QRemoteObjectReplica;
class SyncManagerPrivateReplica;
namespace QtDataSync {
class SyncManagerPrivateHolder;
//! Manages the synchronization process and reports its state
class Q_DATASYNC_EXPORT SyncManager : public QObject
{
Q_OBJECT
//! Holds the name of the setup this manager operates on
Q_PROPERTY(QString setupName READ setupName NOTIFY setupNameChanged REVISION 2)
//! Specifies whether synchronization is currently enabled or disabled
Q_PROPERTY(bool syncEnabled READ isSyncEnabled WRITE setSyncEnabled NOTIFY syncEnabledChanged)
//! Holds the current synchronization state
Q_PROPERTY(SyncState syncState READ syncState NOTIFY syncStateChanged)
//! Holds the progress of the current sync operation
Q_PROPERTY(qreal syncProgress READ syncProgress NOTIFY syncProgressChanged)
//! Holds a description of the last internal error
Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged)
public:
//! The possible states the sync engine can be in
enum SyncState {
Initializing, //!< Initializing internal stuff and connecting to a remote
Downloading, //!< Downloading changes from the remote
Uploading, //!< Uploading changes to the remote
Synchronized, //!< All changes have been synchronized. The engine is idle
Error, //!< An internal error occured. Synchronization is paused until reconnect() is called
Disconnected //!< The remote is not available or sync has been disabled and the engine thus is disconnected
};
Q_ENUM(SyncState)
//! @copydoc AccountManager::AccountManager(QObject*)
explicit SyncManager(QObject *parent = nullptr);
//! @copydoc AccountManager::AccountManager(const QString &, QObject*)
explicit SyncManager(const QString &setupName, QObject *parent = nullptr);
//! @copydoc AccountManager::AccountManager(QRemoteObjectNode *, QObject*)
explicit SyncManager(QRemoteObjectNode *node, QObject *parent = nullptr);
~SyncManager() override;
//! @copybrief AccountManager::replica
Q_INVOKABLE QRemoteObjectReplica *replica() const;
//! @readAcFn{AccountManager::setupName}
QString setupName() const;
//! @readAcFn{syncEnabled}
bool isSyncEnabled() const;
//! @readAcFn{syncState}
SyncState syncState() const;
//! @readAcFn{syncProgress}
qreal syncProgress() const;
//! @readAcFn{lastError}
QString lastError() const;
//! Performs an operation once all changes have been downloaded
void runOnDownloaded(const std::function<void(SyncState)> &resultFn, bool triggerSync = true);
//! Performs an operation once all changes have been synchronized (both directions)
void runOnSynchronized(const std::function<void(SyncState)> &resultFn, bool triggerSync = true);
public Q_SLOTS:
//! @writeAcFn{syncEnabled}
void setSyncEnabled(bool syncEnabled);
//! Triggers a synchronization
void synchronize();
//! Tries to reconnect to the remote
void reconnect();
Q_SIGNALS:
//! @notifyAcFn{AccountManager::setupName}
QT_DATASYNC_REVISION_2 void setupNameChanged(const QString &setupName, QPrivateSignal);
//! @notifyAcFn{syncEnabled}
void syncEnabledChanged(bool syncEnabled, QPrivateSignal);
//! @notifyAcFn{syncState}
void syncStateChanged(QtDataSync::SyncManager::SyncState syncState, QPrivateSignal);
//! @notifyAcFn{syncProgress}
void syncProgressChanged(qreal syncProgress, QPrivateSignal);
//! @notifyAcFn{lastError}
void lastErrorChanged(const QString &lastError, QPrivateSignal);
protected:
//! @private
SyncManager(QObject *parent, void *);
//! @private
void initReplica(const QString &setupName);
//! @private
void initReplica(QRemoteObjectNode *node);
private Q_SLOTS:
void onInit();
void onStateReached(QUuid id, SyncState state);
private:
QScopedPointer<SyncManagerPrivateHolder> d;
void runImp(bool downloadOnly, bool triggerSync, const std::function<void(SyncState)> &resultFn);
};
//! Stream operator to stream into a QDataStream
Q_DATASYNC_EXPORT QDataStream &operator<<(QDataStream &stream, const SyncManager::SyncState &state);
//! Stream operator to stream out of a QDataStream
Q_DATASYNC_EXPORT QDataStream &operator>>(QDataStream &stream, SyncManager::SyncState &state);
}
Q_DECLARE_METATYPE(QtDataSync::SyncManager::SyncState)
Q_DECLARE_TYPEINFO(QtDataSync::SyncManager::SyncState, Q_PRIMITIVE_TYPE);
#endif // QTDATASYNC_SYNCMANAGER_H