QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
setup.h
1 #ifndef QTDATASYNC_SETUP_H
2 #define QTDATASYNC_SETUP_H
3 
4 #include <functional>
5 #include <ratio>
6 
7 #include <QtCore/qobject.h>
8 #include <QtCore/qlogging.h>
9 #include <QtCore/qurl.h>
10 class QLockFile;
11 
12 #include <QtNetwork/qsslconfiguration.h>
13 
14 #include "QtDataSync/qtdatasync_global.h"
15 #include "QtDataSync/exception.h"
16 #include "QtDataSync/remoteconfig.h"
17 
18 class QJsonSerializer;
19 
20 namespace QtDataSync {
21 
23 template<typename TRatio>
24 Q_DECL_CONSTEXPR inline int ratioBytes(intmax_t value);
26 Q_DECL_CONSTEXPR inline int KB(intmax_t value);
28 Q_DECL_CONSTEXPR inline int MB(intmax_t value);
30 Q_DECL_CONSTEXPR inline int GB(intmax_t value);
31 
32 #if __cplusplus >= 201402L
33 namespace literals {
35 
37 Q_DECL_CONSTEXPR inline int operator "" _kb(unsigned long long x) {
38  return KB(static_cast<intmax_t>(x));
39 }
40 
42 Q_DECL_CONSTEXPR inline int operator "" _mb(unsigned long long x) {
43  return MB(static_cast<intmax_t>(x));
44 }
45 
47 Q_DECL_CONSTEXPR inline int operator "" _gb(unsigned long long x) {
48  return GB(static_cast<intmax_t>(x));
49 }
50 
51 }
52 #endif
53 
54 class ConflictResolver;
55 class KeyStore;
56 
57 class SetupPrivate;
59 class Q_DATASYNC_EXPORT Setup
60 {
61  Q_GADGET
62  Q_DISABLE_COPY(Setup)
63 
64 
65  Q_PROPERTY(QString localDir READ localDir WRITE setLocalDir RESET resetLocalDir)
67  Q_PROPERTY(QUrl remoteObjectHost READ remoteObjectHost WRITE setRemoteObjectHost RESET resetRemoteObjectHost)
69  Q_PROPERTY(QJsonSerializer* serializer READ serializer WRITE setSerializer RESET resetSerializer)
71  Q_PROPERTY(QtDataSync::ConflictResolver* conflictResolver READ conflictResolver WRITE setConflictResolver RESET resetConflictResolver)
73  Q_PROPERTY(FatalErrorHandler fatalErrorHandler READ fatalErrorHandler WRITE setFatalErrorHandler RESET resetFatalErrorHandler)
75  Q_PROPERTY(int cacheSize READ cacheSize WRITE setCacheSize RESET resetCacheSize)
77  Q_PROPERTY(bool persistDeletedVersion READ persistDeletedVersion WRITE setPersistDeletedVersion RESET resetPersistDeletedVersion)
79  Q_PROPERTY(SyncPolicy syncPolicy READ syncPolicy WRITE setSyncPolicy RESET resetSyncPolicy)
81  Q_PROPERTY(QSslConfiguration sslConfiguration READ sslConfiguration WRITE setSslConfiguration RESET resetSslConfiguration)
83  Q_PROPERTY(QtDataSync::RemoteConfig remoteConfiguration READ remoteConfiguration WRITE setRemoteConfiguration RESET resetRemoteConfiguration)
85  Q_PROPERTY(QString keyStoreProvider READ keyStoreProvider WRITE setKeyStoreProvider RESET resetKeyStoreProvider)
87  Q_PROPERTY(SignatureScheme signatureScheme READ signatureScheme WRITE setSignatureScheme RESET resetSignatureScheme)
89  Q_PROPERTY(QVariant signatureKeyParam READ signatureKeyParam WRITE setSignatureKeyParam RESET resetSignatureKeyParam)
91  Q_PROPERTY(EncryptionScheme encryptionScheme READ encryptionScheme WRITE setEncryptionScheme RESET resetEncryptionScheme)
93  Q_PROPERTY(QVariant encryptionKeyParam READ encryptionKeyParam WRITE setEncryptionKeyParam RESET resetEncryptionKeyParam)
95  Q_PROPERTY(CipherScheme cipherScheme READ cipherScheme WRITE setCipherScheme RESET resetCipherScheme)
97  Q_PROPERTY(qint32 cipherKeySize READ cipherKeySize WRITE setCipherKeySize RESET resetCipherKeySize) //MAJOR make uint
99  Q_PROPERTY(EventMode eventLoggingMode READ eventLoggingMode WRITE setEventLoggingMode RESET resetEventLoggingMode REVISION 2)
100 
101 public:
103  using FatalErrorHandler = std::function<void (QString, QString, const QMessageLogContext &)>;
104 
106  enum SyncPolicy {
108  PreferDeleted
109  };
110  Q_ENUM(SyncPolicy)
111 
112 
116  ECNR_ECP_SHA3_512
117  };
118  Q_ENUM(SignatureScheme)
119 
120 
123  ECIES_ECP_SHA3_512
124  };
125  Q_ENUM(EncryptionScheme)
126 
127 
136  };
137  Q_ENUM(CipherScheme)
138 
139 
141  secp112r1,
142  secp128r1,
143  secp160r1,
144  secp192r1,
145  secp224r1,
146  secp256r1,
147  secp384r1,
148  secp521r1,
149 
150  brainpoolP160r1,
151  brainpoolP192r1,
152  brainpoolP224r1,
153  brainpoolP256r1,
154  brainpoolP320r1,
155  brainpoolP384r1,
156  brainpoolP512r1,
157 
158  secp112r2,
159  secp128r2,
160  secp160r2,
161 
162  secp160k1,
163  secp192k1,
164  secp224k1,
165  secp256k1
166  };
167  Q_ENUM(EllipticCurve)
168 
169 
170  enum class EventMode {
171  Unchanged,
172  Enabled,
173  Disabled
174  };
175  Q_ENUM(EventMode)
176 
177 
178  static bool exists(const QString &name = DefaultSetup);
180  static void setCleanupTimeout(unsigned long timeout);
182  static void removeSetup(const QString &name, bool waitForFinished = false);
183 
185  static QStringList keystoreProviders();
187  static QStringList availableKeystores();
189  static bool keystoreAvailable(const QString &provider);
191  static QString defaultKeystoreProvider();
193  static KeyStore *loadKeystore(QObject *parent = nullptr, const QString &setupName = DefaultSetup);
195  static KeyStore *loadKeystore(const QString &provider, QObject *parent = nullptr, const QString &setupName = DefaultSetup);
196 
197  Setup();
199  Setup(Setup &&other) noexcept;
201  Setup &operator=(Setup &&other) noexcept;
202  ~Setup();
203 
205  QString localDir() const;
207  QUrl remoteObjectHost() const;
209  QJsonSerializer *serializer() const;
211  ConflictResolver* conflictResolver() const;
213  FatalErrorHandler fatalErrorHandler() const;
215  int cacheSize() const;
217  bool persistDeletedVersion() const;
219  SyncPolicy syncPolicy() const;
221  QSslConfiguration sslConfiguration() const;
223  RemoteConfig remoteConfiguration() const;
225  QString keyStoreProvider() const;
227  SignatureScheme signatureScheme() const;
229  QVariant signatureKeyParam() const;
231  EncryptionScheme encryptionScheme() const;
233  QVariant encryptionKeyParam() const;
235  CipherScheme cipherScheme() const;
237  qint32 cipherKeySize() const;
239  EventMode eventLoggingMode() const;
240 
242  Setup &setLocalDir(QString localDir);
244  Setup &setRemoteObjectHost(QUrl remoteObjectHost);
246  Setup &setSerializer(QJsonSerializer *serializer);
248  Setup &setConflictResolver(ConflictResolver* conflictResolver);
250  Setup &setFatalErrorHandler(const FatalErrorHandler &fatalErrorHandler);
252  Setup &setCacheSize(int cacheSize);
254  Setup &setPersistDeletedVersion(bool persistDeletedVersion);
256  Setup &setSyncPolicy(SyncPolicy syncPolicy);
258  Setup &setSslConfiguration(QSslConfiguration sslConfiguration);
260  Setup &setRemoteConfiguration(RemoteConfig remoteConfiguration);
262  Setup &setKeyStoreProvider(QString keyStoreProvider);
264  Setup &setSignatureScheme(SignatureScheme signatureScheme);
266  Setup &setSignatureKeyParam(QVariant signatureKeyParam);
268  Setup &setEncryptionScheme(EncryptionScheme encryptionScheme);
270  Setup &setEncryptionKeyParam(QVariant encryptionKeyParam);
272  Setup &setCipherScheme(CipherScheme cipherScheme);
274  Setup &setCipherKeySize(qint32 cipherKeySize);
276  Setup &setEventLoggingMode(EventMode eventLoggingMode);
277 
279  Setup &resetLocalDir();
281  Setup &resetRemoteObjectHost();
283  Setup &resetSerializer();
285  Setup &resetConflictResolver();
287  Setup &resetFatalErrorHandler();
289  Setup &resetCacheSize();
291  Setup &resetPersistDeletedVersion();
293  Setup &resetSyncPolicy();
295  Setup &resetSslConfiguration();
297  Setup &resetRemoteConfiguration();
299  Setup &resetKeyStoreProvider();
301  Setup &resetSignatureScheme();
303  Setup &resetSignatureKeyParam();
305  Setup &resetEncryptionScheme();
307  Setup &resetEncryptionKeyParam();
309  Setup &resetCipherScheme();
311  Setup &resetCipherKeySize();
313  Setup &resetEventLoggingMode();
314 
316  Setup &setAccount(const QJsonObject &importData, bool keepData = false, bool allowFailure = false);
318  Setup &setAccount(const QByteArray &importData, bool keepData = false, bool allowFailure = false);
320  Setup &setAccountTrusted(const QJsonObject &importData, const QString &password, bool keepData = false, bool allowFailure = false);
322  Setup &setAccountTrusted(const QByteArray &importData, const QString &password, bool keepData = false, bool allowFailure = false);
323 
325  void create(const QString &name = DefaultSetup);
327  bool createPassive(const QString &name = DefaultSetup, int timeout = 30000);
328 
329 private:
331 };
332 
334 class Q_DATASYNC_EXPORT SetupException : public Exception
335 {
336 protected:
338  SetupException(const QString &setupName, const QString &message);
340  SetupException(const SetupException * const other);
341 };
342 
344 class Q_DATASYNC_EXPORT SetupExistsException : public SetupException
345 {
346 public:
348  SetupExistsException(const QString &setupName);
349 
350  QByteArray className() const noexcept override;
351  void raise() const final;
352  QException *clone() const final;
353 
354 protected:
356  SetupExistsException(const SetupExistsException * const other);
357 };
358 
360 class Q_DATASYNC_EXPORT SetupLockedException : public SetupException
361 {
362 public:
364  SetupLockedException(QLockFile *lockfile, const QString &setupName);
365 
367  qint64 pid() const;
369  QString hostname() const;
371  QString appname() const;
372 
373  QByteArray className() const noexcept override;
374  QString qWhat() const override;
375  void raise() const final;
376  QException *clone() const final;
377 
378 protected:
380  SetupLockedException(const SetupLockedException *cloneFrom);
381 
383  qint64 _pid = -1;
385  QString _hostname;
387  QString _appname;
388 };
389 
390 // ------------- Generic Implementation -------------
391 
392 template<typename TRatio>
393 Q_DECL_CONSTEXPR inline int ratioBytes(intmax_t value)
394 {
395  return static_cast<int>(qMin(TRatio().num * value, static_cast<intmax_t>(INT_MAX)));
396 }
397 
398 Q_DECL_CONSTEXPR inline int KB(intmax_t value)
399 {
400  return ratioBytes<std::kilo>(value);
401 }
402 
403 Q_DECL_CONSTEXPR inline int MB(intmax_t value)
404 {
405  return ratioBytes<std::mega>(value);
406 }
407 
408 Q_DECL_CONSTEXPR inline int GB(intmax_t value)
409 {
410  return ratioBytes<std::giga>(value);
411 }
412 
413 }
414 
415 #endif // QTDATASYNC_SETUP_H
Twofish operating in EAX authenticated encryption mode.
Definition: setup.h:131
The class to setup and create datasync instances.
Definition: setup.h:59
IDEA operating in EAX authenticated encryption mode.
Definition: setup.h:135
Keep the changed entry.
Definition: setup.h:107
Twofish operating in GCM authenticated encryption mode.
Definition: setup.h:132
The base class for all exceptions of QtDataSync.
Definition: exception.h:13
STL namespace.
std::function< void(QString, QString, const QMessageLogContext &)> FatalErrorHandler
Typedef of an error handler function. See Setup::fatalErrorHandler.
Definition: setup.h:103
ECDSA on prime curves with Sha3 hash of 512 bits.
Definition: setup.h:115
AES operating in EAX authenticated encryption mode.
Definition: setup.h:129
AES operating in GCM authenticated encryption mode.
Definition: setup.h:130
Exception thrown if a setups storage directory is locked by another instance.
Definition: setup.h:360
An interface for a generic keystore to securely store secret cryptographic keys.
Definition: keystore.h:37
The primary namespace of the QtDataSync library.
Q_DECL_CONSTEXPR int MB(intmax_t value)
Interprets value as megabytes and returns it converted to bytes.
Definition: setup.h:403
Q_DATASYNC_EXPORT const QString DefaultSetup
The default setup name.
SignatureScheme
The signature schemes supported for Setup::signatureScheme.
Definition: setup.h:113
EventMode
Possible values for the event logging mode change.
Definition: setup.h:170
Serpent operating in EAX authenticated encryption mode.
Definition: setup.h:133
Exception throw if Setup::create fails.
Definition: setup.h:334
Interface to implement a custom conflict handler for sync conflicts.
Serpent operating in GCM authenticated encryption mode.
Definition: setup.h:134
SyncPolicy
Defines the possible policies on how to treat merge conflicts between change and delete.
Definition: setup.h:106
Exception thrown if a setup with the same name already exsits.
Definition: setup.h:344
Q_DECL_CONSTEXPR int GB(intmax_t value)
Interprets value as gigabytes and returns it converted to bytes.
Definition: setup.h:408
RSA in PSS mode with Sha3 hash of 512 bits.
Definition: setup.h:114
EncryptionScheme
The encryption schemes supported for Setup::encryptionScheme.
Definition: setup.h:121
A configuration on how to connect to a remote server.
Definition: remoteconfig.h:16
CipherScheme
The symmetric cipher schemes supported for Setup::cipherScheme.
Definition: setup.h:128
EllipticCurve
Elliptic curves supported as key parameter for Setup::signatureKeyParam and Setup::encryptionKeyParam...
Definition: setup.h:140
Q_DECL_CONSTEXPR int KB(intmax_t value)
Interprets value as kilobytes and returns it converted to bytes.
Definition: setup.h:398
RSA in OAEP mode with Sha3 hash of 512 bits.
Definition: setup.h:122