Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit a2befbe

Browse files
committed
added change remote operation
1 parent 781026b commit a2befbe

18 files changed

+267
-190
lines changed

doc/accountmanager.dox

+16-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,22 @@ ids.
379379
If `keepData` is false, all local data is deleted. This is basically a full reset. If it is true
380380
(the default) then all data is kept.
381381

382-
@sa AccountManager::importAccount, AccountManager::importAccountTrusted
382+
@sa AccountManager::importAccount, AccountManager::importAccountTrusted,
383+
AccountManager::changeRemote
384+
*/
385+
386+
/*!
387+
@fn QtDataSync::AccountManager::changeRemote
388+
389+
@param config The remote config to be used to connect to the new remote
390+
@param keepData Specify whether the stored data should be preserved
391+
392+
Disconnects you from the current remote, and then tries to connect to the new remote defined by
393+
the new config. This operation implicitly performs an account reset. See resetAccount() for
394+
more details.
395+
396+
@sa AccountManager::importAccount, AccountManager::importAccountTrusted,
397+
AccountManager::resetAccount
383398
*/
384399

385400
/*!

src/datasync/accountmanager.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ bool AccountManager::isTrustedImport(const QByteArray &importData)
123123

124124
void AccountManager::resetAccount(bool keepData)
125125
{
126-
return d->replica->resetAccount(keepData);
126+
d->replica->resetAccount(keepData);
127+
}
128+
129+
void AccountManager::changeRemote(const RemoteConfig &config, bool keepData)
130+
{
131+
d->replica->changeRemote(config, keepData);
127132
}
128133

129134
void AccountManager::exportAccount(bool includeServer, const function<void(QJsonObject)> &completedFn, const function<void(QString)> &errorFn)

src/datasync/accountmanager.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#include <QtCore/qsharedpointer.h>
1212
#include <QtCore/qjsonobject.h>
1313

14-
#include "qtdatasync_global.h"
14+
#include "QtDataSync/qtdatasync_global.h"
15+
#include "QtDataSync/remoteconfig.h"
1516

1617
class QRemoteObjectNode;
1718
class QRemoteObjectReplica;
@@ -160,6 +161,8 @@ public Q_SLOTS:
160161
}
161162
//! Removes this device from the current account and then creates a new one
162163
void resetAccount(bool keepData = true);
164+
//! Resets the account and connects to the new remote to create a new one
165+
void changeRemote(const RemoteConfig &config, bool keepData = true);
163166

164167
//! Generate a new secret exchange key used to encrypt data
165168
void updateExchangeKey();

src/datasync/accountmanager_p.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ void AccountManagerPrivate::resetAccount(bool keepData)
7272
_engine->resetAccount(keepData);
7373
}
7474

75+
void AccountManagerPrivate::changeRemote(const RemoteConfig &config, bool keepData)
76+
{
77+
_engine->remoteConnector()->changeRemote(config);
78+
_engine->resetAccount(keepData, false);
79+
}
80+
7581
void AccountManagerPrivate::exportAccount(const QUuid &id, bool includeServer)
7682
{
7783
try {

src/datasync/accountmanager_p.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public Q_SLOTS:
2828
void removeDevice(const QUuid &deviceId) override;
2929
void updateExchangeKey() override;
3030
void resetAccount(bool keepData) override;
31+
void changeRemote(const RemoteConfig &config, bool keepData) override;
3132
void exportAccount(const QUuid &id, bool includeServer) override;
3233
void exportAccountTrusted(const QUuid &id, bool includeServer, const QString &password) override;
3334
void importAccount(const JsonObject &importData, bool keepData) override;

src/datasync/accountmanager_p.rep

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class AccountManagerPrivate {
1010
SLOT(void removeDevice(const QUuid &deviceId));
1111
SLOT(void updateExchangeKey());
1212
SLOT(void resetAccount(bool keepData));
13+
SLOT(void changeRemote(const QtDataSync::RemoteConfig &config, bool keepData));
1314
SLOT(void exportAccount(const QUuid &id, bool includeServer));
1415
SLOT(void exportAccountTrusted(const QUuid &id, bool includeServer, const QString &password));
1516
SLOT(void importAccount(const QtDataSync::JsonObject &importData, bool keepData));

src/datasync/datasync.pro

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ HEADERS += \
3939
changeemitter_p.h \
4040
signal_private_connect_p.h \
4141
migrationhelper.h \
42-
migrationhelper_p.h
42+
migrationhelper_p.h \
43+
remoteconfig.h \
44+
remoteconfig_p.h
4345

4446
SOURCES += \
4547
localstore.cpp \
@@ -68,7 +70,8 @@ SOURCES += \
6870
userexchangemanager.cpp \
6971
emitteradapter.cpp \
7072
changeemitter.cpp \
71-
migrationhelper.cpp
73+
migrationhelper.cpp \
74+
remoteconfig.cpp
7275

7376
STATECHARTS += \
7477
connectorstatemachine.scxml

src/datasync/localstore.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ void LocalStore::beginWriteTransaction(const ObjectKey &key, bool exclusive)
718718

719719
void LocalStore::exec(QSqlQuery &query, const ObjectKey &key) const
720720
{
721-
if(!query.exec()) { //TODO do same for prepare
721+
if(!query.exec()) {
722722
throw LocalStoreException(_defaults,
723723
key,
724724
query.executedQuery().simplified(),

src/datasync/remoteconfig.cpp

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include "remoteconfig.h"
2+
#include "remoteconfig_p.h"
3+
#include "message_p.h"
4+
using namespace QtDataSync;
5+
6+
RemoteConfig::RemoteConfig(const QUrl &url, const QString &accessKey, const HeaderHash &headers, int keepaliveTimeout) :
7+
d(new RemoteConfigPrivate(url, accessKey, headers, keepaliveTimeout))
8+
{}
9+
10+
RemoteConfig::RemoteConfig(const RemoteConfig &other) :
11+
d(other.d)
12+
{}
13+
14+
RemoteConfig::~RemoteConfig() {}
15+
16+
RemoteConfig &RemoteConfig::operator=(const RemoteConfig &other)
17+
{
18+
d = other.d;
19+
return (*this);
20+
}
21+
22+
QUrl RemoteConfig::url() const
23+
{
24+
return d->url;
25+
}
26+
27+
QString RemoteConfig::accessKey() const
28+
{
29+
return d->accessKey;
30+
}
31+
32+
RemoteConfig::HeaderHash RemoteConfig::headers() const
33+
{
34+
return d->headers;
35+
}
36+
37+
int RemoteConfig::keepaliveTimeout() const
38+
{
39+
return d->keepaliveTimeout;
40+
}
41+
42+
void RemoteConfig::setUrl(QUrl url)
43+
{
44+
d->url = url;
45+
}
46+
47+
void RemoteConfig::setAccessKey(QString accessKey)
48+
{
49+
d->accessKey = accessKey;
50+
}
51+
52+
void RemoteConfig::setHeaders(RemoteConfig::HeaderHash headers)
53+
{
54+
d->headers = headers;
55+
}
56+
57+
void RemoteConfig::setKeepaliveTimeout(int keepaliveTimeout)
58+
{
59+
d->keepaliveTimeout = keepaliveTimeout;
60+
}
61+
62+
63+
64+
QDataStream &QtDataSync::operator<<(QDataStream &stream, const RemoteConfig &deviceInfo)
65+
{
66+
stream << deviceInfo.d->url
67+
<< static_cast<Utf8String>(deviceInfo.d->accessKey)
68+
<< deviceInfo.d->headers
69+
<< deviceInfo.d->keepaliveTimeout;
70+
return stream;
71+
}
72+
73+
QDataStream &QtDataSync::operator>>(QDataStream &stream, RemoteConfig &deviceInfo)
74+
{
75+
Utf8String accessKey;
76+
stream >> deviceInfo.d->url
77+
>> accessKey
78+
>> deviceInfo.d->headers
79+
>> deviceInfo.d->keepaliveTimeout;
80+
deviceInfo.d->accessKey = accessKey;
81+
return stream;
82+
}
83+
84+
85+
86+
RemoteConfigPrivate::RemoteConfigPrivate(const QUrl &url, const QString &accessKey, const RemoteConfig::HeaderHash &headers, int keepaliveTimeout) :
87+
QSharedData(),
88+
url(url),
89+
accessKey(accessKey),
90+
headers(headers),
91+
keepaliveTimeout(keepaliveTimeout)
92+
{}
93+
94+
RemoteConfigPrivate::RemoteConfigPrivate(const RemoteConfigPrivate &other) :
95+
QSharedData(other),
96+
url(other.url),
97+
accessKey(other.accessKey),
98+
headers(other.headers),
99+
keepaliveTimeout(other.keepaliveTimeout)
100+
{}

src/datasync/remoteconfig.h

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#ifndef QTDATASYNC_REMOTECONFIG_H
2+
#define QTDATASYNC_REMOTECONFIG_H
3+
4+
#include <QtCore/qobject.h>
5+
#include <QtCore/qurl.h>
6+
#include <QtCore/qhash.h>
7+
#include <QtCore/qshareddata.h>
8+
9+
#include "QtDataSync/qtdatasync_global.h"
10+
11+
namespace QtDataSync {
12+
13+
class RemoteConfigPrivate;
14+
//! A configuration on how to connect to a remote server
15+
class Q_DATASYNC_EXPORT RemoteConfig
16+
{
17+
Q_GADGET
18+
19+
//! The websocket url of the server to connect to
20+
Q_PROPERTY(QUrl url READ url WRITE setUrl)
21+
//! A access secret needed in order to connect to the server
22+
Q_PROPERTY(QString accessKey READ accessKey WRITE setAccessKey)
23+
//! A collection of additional HTTP headers to be sent with the request
24+
Q_PROPERTY(HeaderHash headers READ headers WRITE setHeaders)
25+
//! The keep alive timeout to be used to send pings to the server
26+
Q_PROPERTY(int keepaliveTimeout READ keepaliveTimeout WRITE setKeepaliveTimeout)
27+
28+
public:
29+
//! Typedef for a hash of additional HTTP headers
30+
typedef QHash<QByteArray, QByteArray> HeaderHash;
31+
32+
//! Default constructor, with optional parameters
33+
RemoteConfig(const QUrl &url = {},
34+
const QString &accessKey = {},
35+
const HeaderHash &headers = {},
36+
int keepaliveTimeout = 1); //1 minute between ping messages (nginx timeout is 75 seconds be default)
37+
//! Copy constructor
38+
RemoteConfig(const RemoteConfig &other);
39+
~RemoteConfig();
40+
41+
//! Assignment operator
42+
RemoteConfig &operator=(const RemoteConfig &other);
43+
44+
//! @readAcFn{RemoteConfig::url}
45+
QUrl url() const;
46+
//! @readAcFn{RemoteConfig::accessKey}
47+
QString accessKey() const;
48+
//! @readAcFn{RemoteConfig::headers}
49+
HeaderHash headers() const;
50+
//! @readAcFn{RemoteConfig::keepaliveTimeout}
51+
int keepaliveTimeout() const;
52+
53+
//! @writeAcFn{RemoteConfig::url}
54+
void setUrl(QUrl url);
55+
//! @writeAcFn{RemoteConfig::accessKey}
56+
void setAccessKey(QString accessKey);
57+
//! @writeAcFn{RemoteConfig::headers}
58+
void setHeaders(HeaderHash headers);
59+
//! @writeAcFn{RemoteConfig::keepaliveTimeout}
60+
void setKeepaliveTimeout(int keepaliveTimeout);
61+
62+
private:
63+
QSharedDataPointer<RemoteConfigPrivate> d;
64+
65+
friend Q_DATASYNC_EXPORT QDataStream &operator<<(QDataStream &stream, const RemoteConfig &deviceInfo);
66+
friend Q_DATASYNC_EXPORT QDataStream &operator>>(QDataStream &stream, RemoteConfig &deviceInfo);
67+
};
68+
69+
//! Stream operator to stream into a QDataStream
70+
Q_DATASYNC_EXPORT QDataStream &operator<<(QDataStream &stream, const RemoteConfig &deviceInfo);
71+
//! Stream operator to stream out of a QDataStream
72+
Q_DATASYNC_EXPORT QDataStream &operator>>(QDataStream &stream, RemoteConfig &deviceInfo);
73+
74+
}
75+
76+
Q_DECLARE_METATYPE(QtDataSync::RemoteConfig)
77+
Q_DECLARE_TYPEINFO(QtDataSync::RemoteConfig, Q_MOVABLE_TYPE);
78+
79+
80+
#endif // QTDATASYNC_REMOTECONFIG_H

src/datasync/remoteconfig_p.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef QTDATASYNC_REMOTECONFIG_P_H
2+
#define QTDATASYNC_REMOTECONFIG_P_H
3+
4+
#include "qtdatasync_global.h"
5+
#include "remoteconfig.h"
6+
7+
namespace QtDataSync {
8+
9+
//no export needed
10+
class RemoteConfigPrivate : public QSharedData
11+
{
12+
public:
13+
RemoteConfigPrivate(const QUrl &url,
14+
const QString &accessKey,
15+
const RemoteConfig::HeaderHash &headers,
16+
int keepaliveTimeout);
17+
RemoteConfigPrivate(const RemoteConfigPrivate &other);
18+
19+
QUrl url;
20+
QString accessKey;
21+
RemoteConfig::HeaderHash headers;
22+
int keepaliveTimeout;
23+
};
24+
25+
}
26+
27+
#endif // QTDATASYNC_REMOTECONFIG_P_H

src/datasync/remoteconnector.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ void RemoteConnector::resetAccount(bool clearConfig)
250250
}
251251
}
252252

253+
void RemoteConnector::changeRemote(const RemoteConfig &config)
254+
{
255+
storeConfig(config);
256+
//after storing, continue with "normal" reset. This MUST be done by the engine, thus not in this function
257+
logDebug() << "Prepared new remote configuration for next reconnect";
258+
}
259+
253260
void RemoteConnector::prepareImport(const ExportData &data, const CryptoPP::SecByteBlock &key)
254261
{
255262
//assume data was already "validated"
@@ -827,6 +834,8 @@ RemoteConfig RemoteConnector::loadConfig() const
827834

828835
void RemoteConnector::storeConfig(const RemoteConfig &config)
829836
{
837+
//clean the old config
838+
settings()->remove(keyRemoteConfig);
830839
//store remote config as well -> via current values, taken from defaults
831840
settings()->setValue(keyRemoteUrl, config.url());
832841
settings()->setValue(keyRemoteAccessKey, config.accessKey());

src/datasync/remoteconnector_p.h

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public Q_SLOTS:
103103
void listDevices();
104104
void removeDevice(const QUuid &deviceId);
105105
void resetAccount(bool removeConfig);
106+
void changeRemote(const RemoteConfig &config);
106107
void prepareImport(const ExportData &data, const CryptoPP::SecByteBlock &key);
107108
void loginReply(const QUuid &deviceId, bool accept);
108109

0 commit comments

Comments
 (0)