1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Refactored and cleaned up synchronizer

This commit is contained in:
Laurent Cozic 2017-01-09 12:13:58 +01:00
parent 35e2fb8c6e
commit 0e4a6f0fe6
3 changed files with 142 additions and 71 deletions

View File

@ -19,6 +19,9 @@ void Database::initialize(const QString &path) {
qDebug() << "Database: connection ok";
}
// execQuery("DELETE FROM folders");
// execQuery("DELETE FROM settings");
upgrade();
}

View File

@ -21,7 +21,78 @@ void Synchronizer::start() {
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();
changes = Change::mergedChanges(changes);
@ -63,31 +134,23 @@ void Synchronizer::start() {
}
}
if (!uploadsRemaining_) {
downloadChanges();
}
}
checkNextState();
void Synchronizer::setSessionId(const QString &v) {
api_.setSessionId(v);
}
} else if (state_ == DownloadingChanges) {
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;
}
// =============================================================================================
// DOWNLOADING STATE
// =============================================================================================
void Synchronizer::downloadChanges() {
Settings settings;
QString lastRevId = settings.value("lastRevId", "0").toString();
state_ = DownloadingChanges;
QUrlQuery query;
query.addQueryItem("last_id", lastRevId);
api_.get("synchronizer", query, QUrlQuery(), "download:getSynchronizer");
}
}
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
// =============================================================================================
// HANDLE UPLOAD RESPONSE
// =============================================================================================
if (category == "upload") {
uploadsRemaining_--;
@ -125,10 +192,12 @@ void Synchronizer::api_requestDone(const QJsonObject& response, const QString& t
qWarning() << "Mismatch on operations done:" << uploadsRemaining_;
}
if (uploadsRemaining_ <= 0) {
uploadsRemaining_ = 0;
downloadChanges();
}
checkNextState();
// =============================================================================================
// HANDLE DOWNLOAD RESPONSE
// =============================================================================================
} else if (category == "download") {
if (action == "getSynchronizer") {
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);
}
if (operationType == "update") {
//QUrlQuery data;
api_.patch(path + "/" + itemId, QUrlQuery(), QUrlQuery(), "download:createFolder:" + itemId + ":" + revId);
}
// TODO: update, delete
downloadsRemaining_++;
}
if (!downloadsRemaining_) {
qDebug() << "All download operations complete";
state_ = Idle;
}
checkNextState();
} else {
downloadsRemaining_--;
@ -163,18 +238,10 @@ void Synchronizer::api_requestDone(const QJsonObject& response, const QString& t
folder.loadJsonObject(response);
folder.save(false);
settings.setValue("lastRevId", arg2);
//settings.setValue("lastRevId", arg2);
}
if (downloadsRemaining_ < 0) {
qCritical() << "Mismatch on download operations done" << downloadsRemaining_;
}
if (downloadsRemaining_ <= 0) {
qDebug() << "All download operations complete";
downloadsRemaining_ = 0;
state_ = Idle;
}
checkNextState();
}
} else {
qCritical() << "Invalid category" << category;

View File

@ -28,7 +28,8 @@ private:
SynchronizationState state_;
int uploadsRemaining_;
int downloadsRemaining_;
void downloadChanges();
void checkNextState();
void switchState(SynchronizationState state);
public slots: