diff --git a/QtClient/JoplinQtClient/database.cpp b/QtClient/JoplinQtClient/database.cpp index f63c008a3..01529c2cd 100755 --- a/QtClient/JoplinQtClient/database.cpp +++ b/QtClient/JoplinQtClient/database.cpp @@ -19,6 +19,9 @@ void Database::initialize(const QString &path) { qDebug() << "Database: connection ok"; } +// execQuery("DELETE FROM folders"); +// execQuery("DELETE FROM settings"); + upgrade(); } diff --git a/QtClient/JoplinQtClient/synchronizer.cpp b/QtClient/JoplinQtClient/synchronizer.cpp index 0914fafbb..cbecd162e 100755 --- a/QtClient/JoplinQtClient/synchronizer.cpp +++ b/QtClient/JoplinQtClient/synchronizer.cpp @@ -21,51 +21,7 @@ void Synchronizer::start() { qDebug() << "Starting synchronizer..."; - state_ = UploadingChanges; - - QVector changes = Change::all(); - changes = Change::mergedChanges(changes); - - uploadsRemaining_ = changes.size(); - - foreach (Change change, changes) { - jop::Table itemType = (jop::Table)change.value("item_type").toInt(); - QString itemId = change.value("item_id").toString(); - Change::Type type = (Change::Type)change.value("type").toInt(); - - qDebug() << itemId << itemType << type; - - if (itemType == jop::FoldersTable) { - - if (type == Change::Create) { - - Folder folder; - folder.load(itemId); - QUrlQuery data = valuesToUrlQuery(folder.values()); - api_.put("folders/" + folder.id().toString(), QUrlQuery(), data, "upload:putFolder:" + folder.id().toString()); - - } else if (type == Change::Update) { - - Folder folder; - folder.load(itemId); - QStringList mergedFields = change.mergedFields(); - QUrlQuery data; - foreach (QString field, mergedFields) { - data.addQueryItem(field, folder.value(field).toString()); - } - api_.patch("folders/" + folder.id().toString(), QUrlQuery(), data, "upload:patchFolder:" + folder.id().toString()); - - } else if (type == Change::Delete) { - - api_.del("folders/" + itemId, QUrlQuery(), QUrlQuery(), "upload:deleteFolder:" + itemId); - - } - } - } - - if (!uploadsRemaining_) { - downloadChanges(); - } + switchState(UploadingChanges); } void Synchronizer::setSessionId(const QString &v) { @@ -80,14 +36,121 @@ QUrlQuery Synchronizer::valuesToUrlQuery(const QHash& va return query; } -void Synchronizer::downloadChanges() { - Settings settings; - QString lastRevId = settings.value("lastRevId", "0").toString(); +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 changes = Change::all(); + changes = Change::mergedChanges(changes); + + uploadsRemaining_ = changes.size(); + + foreach (Change change, changes) { + jop::Table itemType = (jop::Table)change.value("item_type").toInt(); + QString itemId = change.value("item_id").toString(); + Change::Type type = (Change::Type)change.value("type").toInt(); + + qDebug() << itemId << itemType << type; + + if (itemType == jop::FoldersTable) { + + if (type == Change::Create) { + + Folder folder; + folder.load(itemId); + QUrlQuery data = valuesToUrlQuery(folder.values()); + api_.put("folders/" + folder.id().toString(), QUrlQuery(), data, "upload:putFolder:" + folder.id().toString()); + + } else if (type == Change::Update) { + + Folder folder; + folder.load(itemId); + QStringList mergedFields = change.mergedFields(); + QUrlQuery data; + foreach (QString field, mergedFields) { + data.addQueryItem(field, folder.value(field).toString()); + } + api_.patch("folders/" + folder.id().toString(), QUrlQuery(), data, "upload:patchFolder:" + folder.id().toString()); + + } else if (type == Change::Delete) { + + api_.del("folders/" + itemId, QUrlQuery(), QUrlQuery(), "upload:deleteFolder:" + itemId); + + } + } + } + + checkNextState(); + + } else if (state_ == DownloadingChanges) { + + // ============================================================================================= + // DOWNLOADING STATE + // ============================================================================================= + + Settings settings; + QString lastRevId = settings.value("lastRevId", "0").toString(); + + QUrlQuery query; + query.addQueryItem("last_id", lastRevId); + api_.get("synchronizer", query, QUrlQuery(), "download:getSynchronizer"); + + } - 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; diff --git a/QtClient/JoplinQtClient/synchronizer.h b/QtClient/JoplinQtClient/synchronizer.h index 8dec6cae6..d5b0de511 100755 --- a/QtClient/JoplinQtClient/synchronizer.h +++ b/QtClient/JoplinQtClient/synchronizer.h @@ -28,7 +28,8 @@ private: SynchronizationState state_; int uploadsRemaining_; int downloadsRemaining_; - void downloadChanges(); + void checkNextState(); + void switchState(SynchronizationState state); public slots: