QtDataSync
4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
|
Manages the synchronization process and reports its state. More...
#include <syncmanager.h>
Public Types | |
enum | SyncState { Initializing, Downloading, Uploading, Synchronized, Error, Disconnected } |
The possible states the sync engine can be in. More... | |
Public Slots | |
void | setSyncEnabled (bool syncEnabled) |
WRITE accessor for syncEnabled. | |
void | synchronize () |
Triggers a synchronization. More... | |
void | reconnect () |
Tries to reconnect to the remote. More... | |
Signals | |
QT_DATASYNC_REVISION_2 void | setupNameChanged (const QString &setupName, QPrivateSignal) |
NOTIFY accessor for AccountManager::setupName. | |
void | syncEnabledChanged (bool syncEnabled, QPrivateSignal) |
NOTIFY accessor for syncEnabled. | |
void | syncStateChanged (QtDataSync::SyncManager::SyncState syncState, QPrivateSignal) |
NOTIFY accessor for syncState. | |
void | syncProgressChanged (qreal syncProgress, QPrivateSignal) |
NOTIFY accessor for syncProgress. | |
void | lastErrorChanged (const QString &lastError, QPrivateSignal) |
NOTIFY accessor for lastError. | |
Public Member Functions | |
SyncManager (QObject *parent=nullptr) | |
Default constructor, uses the default setup. More... | |
SyncManager (const QString &setupName, QObject *parent=nullptr) | |
Constructor with an explicit setup. More... | |
SyncManager (QRemoteObjectNode *node, QObject *parent=nullptr) | |
Constructor with a remote object node to connect to. More... | |
Q_INVOKABLE QRemoteObjectReplica * | replica () const |
The internal replica used to connect to the engine. More... | |
QString | setupName () const |
READ accessor for AccountManager::setupName. | |
bool | isSyncEnabled () const |
READ accessor for syncEnabled. | |
SyncState | syncState () const |
READ accessor for syncState. | |
qreal | syncProgress () const |
READ accessor for syncProgress. | |
QString | lastError () const |
READ accessor for lastError. | |
void | runOnDownloaded (const std::function< void(SyncState)> &resultFn, bool triggerSync=true) |
Performs an operation once all changes have been downloaded. More... | |
void | runOnSynchronized (const std::function< void(SyncState)> &resultFn, bool triggerSync=true) |
Performs an operation once all changes have been synchronized (both directions) More... | |
Public Member Functions inherited from QObject | |
virtual const QMetaObject * | metaObject () const const |
virtual void * | qt_metacast (const char *) |
virtual int | qt_metacall (QMetaObject::Call, int, void **) |
QObject (QObject *parent) | |
virtual bool | event (QEvent *e) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
QString | objectName () const const |
void | setObjectName (const QString &name) |
bool | isWidgetType () const const |
bool | isWindowType () const const |
bool | signalsBlocked () const const |
bool | blockSignals (bool block) |
QThread * | thread () const const |
void | moveToThread (QThread *targetThread) |
int | startTimer (int interval, Qt::TimerType timerType) |
int | startTimer (std::chrono::milliseconds time, Qt::TimerType timerType) |
void | killTimer (int id) |
T | findChild (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegExp ®Exp, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const |
const QObjectList & | children () const const |
void | setParent (QObject *parent) |
void | installEventFilter (QObject *filterObj) |
void | removeEventFilter (QObject *obj) |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const |
bool | disconnect (const char *signal, const QObject *receiver, const char *method) const const |
bool | disconnect (const QObject *receiver, const char *method) const const |
void | dumpObjectTree () |
void | dumpObjectInfo () |
void | dumpObjectTree () const const |
void | dumpObjectInfo () const const |
bool | setProperty (const char *name, const QVariant &value) |
QVariant | property (const char *name) const const |
QList< QByteArray > | dynamicPropertyNames () const const |
void | destroyed (QObject *obj) |
void | objectNameChanged (const QString &objectName) |
QObject * | parent () const const |
bool | inherits (const char *className) const const |
void | deleteLater () |
Properties | |
QString | setupName |
Holds the name of the setup this manager operates on. More... | |
bool | syncEnabled |
Specifies whether synchronization is currently enabled or disabled. More... | |
SyncState | syncState |
Holds the current synchronization state. More... | |
qreal | syncProgress |
Holds the progress of the current sync operation. More... | |
QString | lastError |
Holds a description of the last internal error. More... | |
Properties inherited from QObject | |
objectName | |
Additional Inherited Members | |
Static Public Member Functions inherited from QObject | |
QString | tr (const char *sourceText, const char *disambiguation, int n) |
QString | trUtf8 (const char *sourceText, const char *disambiguation, int n) |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, Functor functor) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type) |
bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method) |
bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method) |
bool | disconnect (const QMetaObject::Connection &connection) |
bool | disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method) |
Protected Member Functions inherited from QObject | |
QObject * | sender () const const |
int | senderSignalIndex () const const |
int | receivers (const char *signal) const const |
bool | isSignalConnected (const QMetaMethod &signal) const const |
virtual void | timerEvent (QTimerEvent *event) |
virtual void | childEvent (QChildEvent *event) |
virtual void | customEvent (QEvent *event) |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
Manages the synchronization process and reports its state.
The manager connects to the engine via a QRemoteObjectNode. Internally the class holds a replica of a source that is part of the datasync engine which is created by the setup. Typically, this node is managed by the setup too, but you can specify your own node in the constructor. In order to increase security and performance a special remote object connection is used that utilizes events to deliver messages within the same process only. See Threaded remote objects for details.
Definition at line 20 of file syncmanager.h.
The possible states the sync engine can be in.
Enumerator | |
---|---|
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. |
Definition at line 37 of file syncmanager.h.
|
explicit |
Default constructor, uses the default setup.
parent | The parent object |
SetupDoesNotExistException | Thrown if the default setup was not created yet |
|
explicit |
Constructor with an explicit setup.
setupName | The name of the setup to connect to |
parent | The parent object |
SetupDoesNotExistException | Thrown if the given setup was not created yet |
|
explicit |
Constructor with a remote object node to connect to.
node | The remote object node used to connect to the source objects |
parent | The parent object |
|
slot |
Tries to reconnect to the remote.
Completely reconnects to the remote. If currently connected, the remote is first disconnected, then reconnected. Can also be used to leave the SyncManager::Error state and go back to the normal operational mode. If no remote is defined or the synchronization is disabled, reconnecting will not have any effect.
QtDataSync::SyncManager::replica | ( | ) | const |
The internal replica used to connect to the engine.
Returns a reference to the internally used replica which connects to the engine. The replica is only valid as long as the SyncManager exists. You can use the replica reference to get information about it's state etc.
QtDataSync::SyncManager::runOnDownloaded | ( | const std::function< void(SyncState)> & | resultFn, |
bool | triggerSync = true |
||
) |
Performs an operation once all changes have been downloaded.
resultFn | The function to be called as soon as a target state is reached |
triggerSync | Trigger a synchronisation in case the engine is currently synchronized |
Semantics of the resultFn
:
This method behaves just like runOnSynchronized(), but with the one difference that the SyncManager::Uploading state will also trigger the callback (in both phases). This method can be used to only wait for all changes to be downloaded from the remote, instead of waiting for the uploads as well.
QtDataSync::SyncManager::runOnSynchronized | ( | const std::function< void(SyncState)> & | resultFn, |
bool | triggerSync = true |
||
) |
Performs an operation once all changes have been synchronized (both directions)
resultFn | The function to be called as soon as a target state is reached |
triggerSync | Trigger a synchronisation in case the engine is currently synchronized |
Semantics of the resultFn
:
This method will pass a callback to the engine that is called as soon as a "stable" state is reached. The logic is as follows:
If the engine is in the SyncManager::Error or SyncManager::Disconnected state, the callback is immediatly called. If it is SyncManager::Synchronized, and triggerSync
is false
, it will be called too. If it is true
a new synchronization is triggered and the method is not called yet. For all other states, nothing happens yet.
If the method was not directly called, the engine will instead call it as soon as one of the SyncManager::Synchronized, SyncManager::Error or SyncManager::Disconnected states is reached.
The state that triggerd the function to be called is the one that is passed to the callback.
|
slot |
Triggers a synchronization.
This method will only work if the engine is currently in the SyncManager::Synchronized state. In all other cases it does nothing. To leave the error state, use reconnect() instead.
|
read |
Holds a description of the last internal error.
Default: empty
When the engine is in the SyncManager::Error state, this property contains a localized error string to be displayed to the user. As long as it stays in the error state this property keeps the string. As soon as the engine enters another state, the error gets cleared. To leave the error state, one must reconnect the engine explicitly by calling the reconnect() method.
Accessors | |
---|---|
READ | lastError() |
NOTIFY | lastErrorChanged() |
Definition at line 33 of file syncmanager.h.
|
read |
Holds the name of the setup this manager operates on.
Default: QtDataSync::DefaultSetup
The setup name is the name that was passed to the Setup::create method to create the datasync instance this manager is refering to.
Accessors | |
---|---|
READ | setupName() |
NOTIFY | setupNameChanged() |
REVISION | 2 |
Definition at line 25 of file syncmanager.h.
|
readwrite |
Specifies whether synchronization is currently enabled or disabled.
Default: true
Changing this property will asynchronously trigger the specific operation in the engine, i.e. either disconnect it to disable sync, or start initializing in order to synchronize again. Whether the engine can actually connect to a remote can be monitored via SyncManager::syncState.
Accessors | |
---|---|
READ | isSyncEnabled() |
WRITE | setSyncEnabled() |
NOTIFY | syncEnabledChanged() |
Definition at line 27 of file syncmanager.h.
|
read |
Holds the progress of the current sync operation.
Default: -1.0
When in the SyncManager::Downloading or SyncManager::Uploading state, this property will provide a progress of that operation. The possible progress values range from 0.0
to 1.0
. If there is currently no active progress (i.e. because the engine is in a state different from those two), the progress is set to -1.0
, meaning that there is no progress to show.
Accessors | |
---|---|
READ | syncProgress() |
NOTIFY | syncProgressChanged() |
Definition at line 31 of file syncmanager.h.
|
read |
Holds the current synchronization state.
Default: SyncManager::Initializing
The sync state is simply forwareded form the engine. When in Downloading or Uploading state, the SyncManager::syncProgress property can be used to monitor the progress of each of those operations. When in the Error state, SyncManager::lastError holds the error string, and the engine disconnects from the remote. It will stay in the error state until it is explicitly cleared by calling reconnect(). When disconnected, the engine automatically tries to reconnect to the remote periodically, unless no remote is defined or synchronization has been disabled. Explicit tries to connect can by made by reconnect().
Accessors | |
---|---|
READ | syncState() |
NOTIFY | syncStateChanged() |
Definition at line 29 of file syncmanager.h.