QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
accountmanager.h
1 #ifndef QTDATASYNC_ACCOUNTMANAGER_H
2 #define QTDATASYNC_ACCOUNTMANAGER_H
3 
4 #include <functional>
5 
6 #include <QtCore/qobject.h>
7 #include <QtCore/quuid.h>
8 #include <QtCore/qshareddata.h>
9 #include <QtCore/qlist.h>
10 #include <QtCore/qiodevice.h>
11 #include <QtCore/qsharedpointer.h>
12 #include <QtCore/qjsonobject.h>
13 
14 #include "QtDataSync/qtdatasync_global.h"
15 #include "QtDataSync/remoteconfig.h"
16 
17 class QRemoteObjectNode;
19 
20 namespace QtDataSync {
21 
22 class DeviceInfoPrivate;
24 class Q_DATASYNC_EXPORT DeviceInfo
25 {
26  Q_GADGET
27 
29  Q_PROPERTY(QUuid deviceId READ deviceId WRITE setDeviceId)
31  Q_PROPERTY(QString name READ name WRITE setName)
33  Q_PROPERTY(QByteArray fingerprint READ fingerprint WRITE setFingerprint)
34 
35 public:
37  DeviceInfo();
39  DeviceInfo(const QUuid &deviceId, const QString &name, const QByteArray &fingerprint);
41  DeviceInfo(const DeviceInfo &other);
43  DeviceInfo(DeviceInfo &&other) noexcept;
44  ~DeviceInfo();
45 
47  DeviceInfo &operator=(const DeviceInfo &other);
49  DeviceInfo &operator=(DeviceInfo &&other) noexcept;
50 
52  QUuid deviceId() const;
54  QString name() const;
56  QByteArray fingerprint() const;
57 
59  void setDeviceId(const QUuid &deviceId);
61  void setName(const QString &name);
63  void setFingerprint(const QByteArray &fingerprint);
64 
66  bool operator==(const DeviceInfo &other) const;
68  bool operator!=(const DeviceInfo &other) const;
69 
70 private:
71  QSharedDataPointer<DeviceInfoPrivate> d;
72 
73  friend Q_DATASYNC_EXPORT QDataStream &operator<<(QDataStream &stream, const DeviceInfo &deviceInfo);
74  friend Q_DATASYNC_EXPORT QDataStream &operator>>(QDataStream &stream, DeviceInfo &deviceInfo);
75 };
76 
77 class LoginRequestPrivate;
79 class Q_DATASYNC_EXPORT LoginRequest
80 {
81  Q_GADGET
82  friend class AccountManager;
83 
85  Q_PROPERTY(QtDataSync::DeviceInfo device READ device CONSTANT)
87  Q_PROPERTY(bool handled READ handled CONSTANT)
88 
89 public:
91  LoginRequest(LoginRequestPrivate *d_ptr = nullptr);
93  LoginRequest(const LoginRequest &other);
95  LoginRequest(LoginRequest &&other) noexcept;
96  ~LoginRequest();
97 
99  LoginRequest &operator=(const LoginRequest &other);
101  LoginRequest &operator=(LoginRequest &&other) noexcept;
102 
104  DeviceInfo device() const;
106  bool handled() const;
107 
109  Q_INVOKABLE void accept();
111  Q_INVOKABLE void reject();
112 
113 private:
114  QSharedPointer<LoginRequestPrivate> d;
115 };
116 
117 class AccountManagerPrivateHolder;
119 class Q_DATASYNC_EXPORT AccountManager : public QObject
120 {
121  Q_OBJECT
122 
124  Q_PROPERTY(QString setupName READ setupName NOTIFY setupNameChanged REVISION 2)
126  Q_PROPERTY(QString deviceName READ deviceName WRITE setDeviceName RESET resetDeviceName NOTIFY deviceNameChanged)
128  Q_PROPERTY(QByteArray deviceFingerprint READ deviceFingerprint NOTIFY deviceFingerprintChanged)
130  Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged)
131 
132 public:
134  explicit AccountManager(QObject *parent = nullptr);
136  explicit AccountManager(const QString &setupName, QObject *parent = nullptr);
138  explicit AccountManager(QRemoteObjectNode *node, QObject *parent = nullptr);
139  ~AccountManager() override;
140 
142  Q_INVOKABLE QRemoteObjectReplica *replica() const;
143 
145  static bool isTrustedImport(const QJsonObject &importData);
147  static bool isTrustedImport(const QByteArray &importData);
148 
150  void exportAccount(bool includeServer, const std::function<void(QJsonObject)> &completedFn, const std::function<void(QString)> &errorFn = {});
152  void exportAccount(bool includeServer, const std::function<void(QByteArray)> &completedFn, const std::function<void(QString)> &errorFn = {});
154  void exportAccountTrusted(bool includeServer, const QString &password, const std::function<void(QJsonObject)> &completedFn, const std::function<void(QString)> &errorFn = {});
156  void exportAccountTrusted(bool includeServer, const QString &password, const std::function<void(QByteArray)> &completedFn, const std::function<void(QString)> &errorFn = {});
158  void importAccount(const QJsonObject &importData, const std::function<void(bool,QString)> &completedFn, bool keepData = false);
160  void importAccount(const QByteArray &importData, const std::function<void(bool,QString)> &completedFn, bool keepData = false);
162  void importAccountTrusted(const QJsonObject &importData, const QString &password, const std::function<void(bool,QString)> &completedFn, bool keepData = false);
164  void importAccountTrusted(const QByteArray &importData, const QString &password, const std::function<void(bool,QString)> &completedFn, bool keepData = false);
165 
167  QString setupName() const;
169  QString deviceName() const;
171  QByteArray deviceFingerprint() const;
173  QString lastError() const;
174 
175 public Q_SLOTS:
177  void listDevices();
179  void removeDevice(const QUuid &deviceInfo);
181  inline void removeDevice(const QtDataSync::DeviceInfo &deviceInfo) {
182  removeDevice(deviceInfo.deviceId());
183  }
185  void resetAccount(bool keepData = true);
187  void changeRemote(const RemoteConfig &config, bool keepData = true);
188 
190  void updateExchangeKey();
191 
193  void setDeviceName(const QString &deviceName);
195  void resetDeviceName();
196 
197 Q_SIGNALS:
199  void accountDevices(const QList<QtDataSync::DeviceInfo> &devices, QPrivateSignal);
201  void loginRequested(QtDataSync::LoginRequest request, QPrivateSignal);
203  void importAccepted(QPrivateSignal);
205  void accountAccessGranted(const QUuid &deviceId, QPrivateSignal);
206 
208  QT_DATASYNC_REVISION_2 void setupNameChanged(const QString &setupName, QPrivateSignal);
210  void deviceNameChanged(const QString &deviceName, QPrivateSignal);
212  void deviceFingerprintChanged(const QByteArray &deviceFingerprint, QPrivateSignal);
214  void lastErrorChanged(const QString &lastError, QPrivateSignal);
215 
216 protected:
218  AccountManager(QObject *parent, void *);
220  void initReplica(const QString &setupName);
222  void initReplica(QRemoteObjectNode *node);
223 
224 private Q_SLOTS:
225  void accountExportReady(const QUuid &id, const QJsonObject &exportData);
226  void accountExportError(const QUuid &id, const QString &errorString);
227  void accountImportResult(bool success, const QString &error);
228  void loginRequestedImpl(const DeviceInfo &deviceInfo);
229 
230 private:
232 };
233 
235 Q_DATASYNC_EXPORT QDataStream &operator<<(QDataStream &stream, const DeviceInfo &deviceInfo);
237 Q_DATASYNC_EXPORT QDataStream &operator>>(QDataStream &stream, DeviceInfo &deviceInfo);
238 
239 //helper class until QTBUG-65557 is fixed (not exported (!), internal only)
241 class JsonObject : public QJsonObject {
242 public:
243  inline JsonObject(const QJsonObject &other = {}) :
244  QJsonObject(other)
245  {}
246 };
247 
249 QDataStream &operator<<(QDataStream &out, const JsonObject &data);
251 QDataStream &operator>>(QDataStream &in, JsonObject &data);
252 
253 }
254 
255 Q_DECLARE_METATYPE(QtDataSync::JsonObject)
256 Q_DECLARE_METATYPE(QtDataSync::DeviceInfo)
257 Q_DECLARE_TYPEINFO(QtDataSync::DeviceInfo, Q_MOVABLE_TYPE);
258 Q_DECLARE_METATYPE(QtDataSync::LoginRequest)
259 
260 #endif // QTDATASYNC_ACCOUNTMANAGER_H
Manages devices that belong to the users account.
STL namespace.
A login request done by another device to this device.
QUuid deviceId
The unique id that device is identified by.
Information about a device that is part of an account or wants to be added.
Q_DATASYNC_EXPORT QDataStream & operator<<(QDataStream &stream, const DeviceInfo &deviceInfo)
Stream operator to stream into a QDataStream.
The primary namespace of the QtDataSync library.
Q_DATASYNC_EXPORT QDataStream & operator>>(QDataStream &stream, DeviceInfo &deviceInfo)
Stream operator to stream out of a QDataStream.
A configuration on how to connect to a remote server.
Definition: remoteconfig.h:16
void removeDevice(const QtDataSync::DeviceInfo &deviceInfo)
Remove the given device from the current account.