QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
userexchangemanager.h
1 #ifndef QTDATASYNC_USEREXCHANGEMANAGER_H
2 #define QTDATASYNC_USEREXCHANGEMANAGER_H
3 
4 #include <QtCore/qobject.h>
5 #include <QtCore/qscopedpointer.h>
6 #include <QtCore/qshareddata.h>
7 
8 #include <QtNetwork/qhostaddress.h>
9 
10 #include "QtDataSync/qtdatasync_global.h"
11 
12 namespace QtDataSync {
13 
14 class AccountManager;
15 
16 class UserInfoPrivate;
18 class Q_DATASYNC_EXPORT UserInfo
19 {
20  Q_GADGET
21  friend class UserInfoPrivate;
22 
24  Q_PROPERTY(QString name READ name CONSTANT)
26  Q_PROPERTY(QHostAddress address READ address CONSTANT)
28  Q_PROPERTY(quint16 port READ port CONSTANT)
29 
30 public:
31  UserInfo();
33  UserInfo(const UserInfo &other);
35  UserInfo(UserInfo &&other) noexcept;
37  UserInfo(UserInfoPrivate *data);
38  ~UserInfo();
39 
41  UserInfo &operator=(const UserInfo &other);
43  UserInfo &operator=(UserInfo &&other) noexcept;
44 
46  QString name() const;
48  QHostAddress address() const;
50  quint16 port() const;
51 
53  bool operator==(const UserInfo &other) const;
55  bool operator!=(const UserInfo &other) const;
56 
57 private:
58  QSharedDataPointer<UserInfoPrivate> d;
59 };
60 
61 class UserExchangeManagerPrivate;
63 class Q_DATASYNC_EXPORT UserExchangeManager : public QObject
64 {
65  Q_OBJECT
66 
68  Q_PROPERTY(QString setupName READ setupName NOTIFY setupNameChanged REVISION 2)
70  Q_PROPERTY(bool active READ isActive NOTIFY activeChanged)
72  Q_PROPERTY(QList<QtDataSync::UserInfo> devices READ devices NOTIFY devicesChanged)
73 
74 public:
76  static const quint16 DataExchangePort;
77 
79  explicit UserExchangeManager(QObject *parent = nullptr);
81  explicit UserExchangeManager(const QString &setupName, QObject *parent = nullptr);
83  explicit UserExchangeManager(AccountManager *manager, QObject *parent = nullptr);
84  ~UserExchangeManager() override;
85 
87  AccountManager *accountManager() const;
88 
90  QString setupName() const;
92  quint16 port() const;
94  bool isActive() const;
96  QList<UserInfo> devices() const;
97 
99  Q_INVOKABLE void exportTo(const QtDataSync::UserInfo &userInfo, bool includeServer);
101  Q_INVOKABLE void exportTrustedTo(const QtDataSync::UserInfo &userInfo, bool includeServer, const QString &password);
103  void importFrom(const QtDataSync::UserInfo &userInfo,
104  const std::function<void(bool,QString)> &completedFn,
105  bool keepData = false);
107  void importTrustedFrom(const QtDataSync::UserInfo &userInfo,
108  const QString &password,
109  const std::function<void(bool,QString)> &completedFn,
110  bool keepData = false);
111 
112 public Q_SLOTS:
114  inline bool startExchange(quint16 port = DataExchangePort) {
115  return startExchange(QHostAddress::Any, port);
116  }
118  bool startExchange(const QHostAddress &listenAddress, quint16 port = DataExchangePort);
120  void stopExchange();
121 
122 Q_SIGNALS:
124  void userDataReceived(const QtDataSync::UserInfo &userInfo, bool trusted, QPrivateSignal);
126  void exchangeError(const QString &errorString, QPrivateSignal);
127 
129  QT_DATASYNC_REVISION_2 void setupNameChanged(const QString &setupName, QPrivateSignal);
131  void activeChanged(bool active, QPrivateSignal);
133  void devicesChanged(QList<QtDataSync::UserInfo> devices, QPrivateSignal);
134 
135 protected:
137  UserExchangeManager(QObject *parent, void*);
139  void initManager(AccountManager *manager);
140 
141 private Q_SLOTS:
142  void timeout();
143  void readDatagram();
144 
145 private:
147 };
148 
150 Q_DATASYNC_EXPORT uint qHash(const QtDataSync::UserInfo &info, uint seed = 0);
152 Q_DATASYNC_EXPORT QDebug operator<<(QDebug stream, const QtDataSync::UserInfo &userInfo);
153 
154 }
155 
156 Q_DECLARE_METATYPE(QtDataSync::UserInfo)
157 Q_DECLARE_TYPEINFO(QtDataSync::UserInfo, Q_MOVABLE_TYPE);
158 
159 #endif // QTDATASYNC_USEREXCHANGEMANAGER_H
Manages devices that belong to the users account.
STL namespace.
A helper class to exchange the account data between devices on the local network. ...
Q_DATASYNC_EXPORT QDataStream & operator<<(QDataStream &stream, const DeviceInfo &deviceInfo)
Stream operator to stream into a QDataStream.
The primary namespace of the QtDataSync library.
uint Q_DATASYNC_EXPORT qHash(const ObjectKey &key, uint seed=0)
Overload of qHash to use ObjectKey with QHash.
Provides information about a detected exchange user.