mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
Refactored and cleaned up synchronizer
This commit is contained in:
parent
35e2fb8c6e
commit
0e4a6f0fe6
@ -19,6 +19,9 @@ void Database::initialize(const QString &path) {
|
|||||||
qDebug() << "Database: connection ok";
|
qDebug() << "Database: connection ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// execQuery("DELETE FROM folders");
|
||||||
|
// execQuery("DELETE FROM settings");
|
||||||
|
|
||||||
upgrade();
|
upgrade();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,78 @@ void Synchronizer::start() {
|
|||||||
|
|
||||||
qDebug() << "Starting synchronizer...";
|
qDebug() << "Starting synchronizer...";
|
||||||
|
|
||||||
state_ = UploadingChanges;
|
switchState(UploadingChanges);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Synchronizer::setSessionId(const QString &v) {
|
||||||
|
api_.setSessionId(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
QUrlQuery Synchronizer::valuesToUrlQuery(const QHash<QString, Change::Value>& values) const {
|
||||||
|
QUrlQuery query;
|
||||||
|
for (QHash<QString, Change::Value>::const_iterator it = values.begin(); it != values.end(); ++it) {
|
||||||
|
query.addQueryItem(it.key(), it.value().toString());
|
||||||
|
}
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Synchronizer::checkNextState() {
|
||||||
|
switch (state_) {
|
||||||
|
|
||||||
|
case UploadingChanges:
|
||||||
|
|
||||||
|
if (uploadsRemaining_ < 0) qCritical() << "Mismatch on upload operations done" << uploadsRemaining_;
|
||||||
|
|
||||||
|
if (uploadsRemaining_ <= 0) {
|
||||||
|
uploadsRemaining_ = 0;
|
||||||
|
switchState(DownloadingChanges);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DownloadingChanges:
|
||||||
|
|
||||||
|
if (downloadsRemaining_ < 0) qCritical() << "Mismatch on download operations done" << downloadsRemaining_;
|
||||||
|
|
||||||
|
if (downloadsRemaining_ <= 0) {
|
||||||
|
downloadsRemaining_ = 0;
|
||||||
|
switchState(Idle);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Idle:
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
qCritical() << "Synchronizer has invalid state" << state_;
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Synchronizer::switchState(Synchronizer::SynchronizationState state) {
|
||||||
|
if (state_ == state) {
|
||||||
|
qCritical() << "Trying to switch synchronizer to its current state" << state;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
state_ = state;
|
||||||
|
|
||||||
|
qDebug() << "Switching synchronizer state to" << state;
|
||||||
|
|
||||||
|
if (state == Idle) {
|
||||||
|
|
||||||
|
// =============================================================================================
|
||||||
|
// IDLE STATE
|
||||||
|
// =============================================================================================
|
||||||
|
|
||||||
|
} else if (state == UploadingChanges) {
|
||||||
|
|
||||||
|
// =============================================================================================
|
||||||
|
// UPLOADING STATE
|
||||||
|
// =============================================================================================
|
||||||
|
|
||||||
QVector<Change> changes = Change::all();
|
QVector<Change> changes = Change::all();
|
||||||
changes = Change::mergedChanges(changes);
|
changes = Change::mergedChanges(changes);
|
||||||
@ -63,31 +134,23 @@ void Synchronizer::start() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!uploadsRemaining_) {
|
checkNextState();
|
||||||
downloadChanges();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Synchronizer::setSessionId(const QString &v) {
|
} else if (state_ == DownloadingChanges) {
|
||||||
api_.setSessionId(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
QUrlQuery Synchronizer::valuesToUrlQuery(const QHash<QString, Change::Value>& values) const {
|
// =============================================================================================
|
||||||
QUrlQuery query;
|
// DOWNLOADING STATE
|
||||||
for (QHash<QString, Change::Value>::const_iterator it = values.begin(); it != values.end(); ++it) {
|
// =============================================================================================
|
||||||
query.addQueryItem(it.key(), it.value().toString());
|
|
||||||
}
|
|
||||||
return query;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Synchronizer::downloadChanges() {
|
|
||||||
Settings settings;
|
Settings settings;
|
||||||
QString lastRevId = settings.value("lastRevId", "0").toString();
|
QString lastRevId = settings.value("lastRevId", "0").toString();
|
||||||
|
|
||||||
state_ = DownloadingChanges;
|
|
||||||
QUrlQuery query;
|
QUrlQuery query;
|
||||||
query.addQueryItem("last_id", lastRevId);
|
query.addQueryItem("last_id", lastRevId);
|
||||||
api_.get("synchronizer", query, QUrlQuery(), "download:getSynchronizer");
|
api_.get("synchronizer", query, QUrlQuery(), "download:getSynchronizer");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Synchronizer::api_requestDone(const QJsonObject& response, const QString& tag) {
|
void Synchronizer::api_requestDone(const QJsonObject& response, const QString& tag) {
|
||||||
@ -104,6 +167,10 @@ void Synchronizer::api_requestDone(const QJsonObject& response, const QString& t
|
|||||||
|
|
||||||
// TODO: check for error
|
// TODO: check for error
|
||||||
|
|
||||||
|
// =============================================================================================
|
||||||
|
// HANDLE UPLOAD RESPONSE
|
||||||
|
// =============================================================================================
|
||||||
|
|
||||||
if (category == "upload") {
|
if (category == "upload") {
|
||||||
uploadsRemaining_--;
|
uploadsRemaining_--;
|
||||||
|
|
||||||
@ -125,10 +192,12 @@ void Synchronizer::api_requestDone(const QJsonObject& response, const QString& t
|
|||||||
qWarning() << "Mismatch on operations done:" << uploadsRemaining_;
|
qWarning() << "Mismatch on operations done:" << uploadsRemaining_;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uploadsRemaining_ <= 0) {
|
checkNextState();
|
||||||
uploadsRemaining_ = 0;
|
|
||||||
downloadChanges();
|
// =============================================================================================
|
||||||
}
|
// HANDLE DOWNLOAD RESPONSE
|
||||||
|
// =============================================================================================
|
||||||
|
|
||||||
} else if (category == "download") {
|
} else if (category == "download") {
|
||||||
if (action == "getSynchronizer") {
|
if (action == "getSynchronizer") {
|
||||||
downloadsRemaining_ = 0;
|
downloadsRemaining_ = 0;
|
||||||
@ -146,13 +215,19 @@ void Synchronizer::api_requestDone(const QJsonObject& response, const QString& t
|
|||||||
api_.get(path + "/" + itemId, QUrlQuery(), QUrlQuery(), "download:createFolder:" + itemId + ":" + revId);
|
api_.get(path + "/" + itemId, QUrlQuery(), QUrlQuery(), "download:createFolder:" + itemId + ":" + revId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (operationType == "update") {
|
||||||
|
//QUrlQuery data;
|
||||||
|
|
||||||
|
api_.patch(path + "/" + itemId, QUrlQuery(), QUrlQuery(), "download:createFolder:" + itemId + ":" + revId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: update, delete
|
||||||
|
|
||||||
downloadsRemaining_++;
|
downloadsRemaining_++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!downloadsRemaining_) {
|
checkNextState();
|
||||||
qDebug() << "All download operations complete";
|
|
||||||
state_ = Idle;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
downloadsRemaining_--;
|
downloadsRemaining_--;
|
||||||
|
|
||||||
@ -163,18 +238,10 @@ void Synchronizer::api_requestDone(const QJsonObject& response, const QString& t
|
|||||||
folder.loadJsonObject(response);
|
folder.loadJsonObject(response);
|
||||||
folder.save(false);
|
folder.save(false);
|
||||||
|
|
||||||
settings.setValue("lastRevId", arg2);
|
//settings.setValue("lastRevId", arg2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (downloadsRemaining_ < 0) {
|
checkNextState();
|
||||||
qCritical() << "Mismatch on download operations done" << downloadsRemaining_;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (downloadsRemaining_ <= 0) {
|
|
||||||
qDebug() << "All download operations complete";
|
|
||||||
downloadsRemaining_ = 0;
|
|
||||||
state_ = Idle;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qCritical() << "Invalid category" << category;
|
qCritical() << "Invalid category" << category;
|
||||||
|
@ -28,7 +28,8 @@ private:
|
|||||||
SynchronizationState state_;
|
SynchronizationState state_;
|
||||||
int uploadsRemaining_;
|
int uploadsRemaining_;
|
||||||
int downloadsRemaining_;
|
int downloadsRemaining_;
|
||||||
void downloadChanges();
|
void checkNextState();
|
||||||
|
void switchState(SynchronizationState state);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user