From 054afd4ca285b2d03dde99fd8c0c89ee62c1ceed Mon Sep 17 00:00:00 2001 From: Scharel Clemens Date: Mon, 4 May 2020 20:15:17 +0200 Subject: [PATCH] Implemented two functions that should ease the syncronisation --- qml/pages/NotePage.qml | 10 +++--- src/notesapi.cpp | 20 +++++++++-- src/notesapi.h | 4 +++ src/notesinterface.h | 4 +++ src/notesmodel.cpp | 8 +++++ src/notesmodel.h | 3 ++ src/notesstore.cpp | 44 ++++++++++++----------- src/notesstore.h | 4 ++- translations/harbour-nextcloudnotes.ts | 48 +++++++++++++------------- 9 files changed, 92 insertions(+), 53 deletions(-) diff --git a/qml/pages/NotePage.qml b/qml/pages/NotePage.qml index d2617a2..617f737 100644 --- a/qml/pages/NotePage.qml +++ b/qml/pages/NotePage.qml @@ -156,9 +156,9 @@ Dialog { return (appSettings.useCapitalX ? '- [X] ' : '- [x] ') + p1 } else { return match } } ) - content = newContent + note["content"] = newContent parseContent() - notesApi.updateNote(id, { 'content': content } ) + notesApi.updateNote(id, { 'content': note["content"] } ) } else if (/^tasklist:uncheckbox_(\d+)$/m.test(link)) { newContent = newContent.replace(/- \[[xX]\] (.*)$/gm, @@ -168,9 +168,9 @@ Dialog { return '- [ ] ' + p1 } else { return match } } ) - content = newContent + note["content"] = newContent parseContent() - notesApi.updateNote(id, { 'content': content } ) + notesApi.updateNote(id, { 'content': note["content"] } ) } else { Qt.openUrlExternally(link) @@ -244,7 +244,7 @@ Dialog { } onFocusChanged: { if (focus === false && text !== note["category"]) { - notesApi.updateNote(id, {'content': content, 'category': text, 'modified': new Date().valueOf() / 1000}) // This does not seem to work without adding the content + notesApi.updateNote(id, {'content': note["content"], 'category': text, 'modified': new Date().valueOf() / 1000}) // This does not seem to work without adding the content } } } diff --git a/src/notesapi.cpp b/src/notesapi.cpp index 67ca5ca..3e3236d 100644 --- a/src/notesapi.cpp +++ b/src/notesapi.cpp @@ -53,6 +53,14 @@ void NotesApi::setAccount(const QString &account) { } } +const QList NotesApi::noteIds() { + return m_syncedNotes.keys(); +} + +int NotesApi::noteModified(const int id) { + return m_syncedNotes.value(id, -1); +} + bool NotesApi::getAllNotes(const QStringList& exclude) { qDebug() << "Getting all notes"; QUrl url = apiEndpointUrl(m_notesEndpoint); @@ -410,8 +418,10 @@ void NotesApi::replyFinished(QNetworkReply *reply) { bool ok; QString idString = reply->url().path().split('/', QString::SkipEmptyParts).last(); int id = idString.toInt(&ok); - if (reply->error() == QNetworkReply::NoError && id >= 0 && ok) + if (reply->error() == QNetworkReply::NoError && id >= 0 && ok) { + m_syncedNotes.remove(id); emit noteDeleted(id); + } m_deleteNoteReplies.removeOne(reply); } else if (m_loginReplies.contains(reply)) { @@ -613,12 +623,16 @@ void NotesApi::updateApiNotes(const QJsonArray &json) { void NotesApi::updateApiNote(const QJsonObject &json) { int id = json["id"].toInt(-1); - if (id >= 0) + if (id >= 0) { + m_syncedNotes.insert(id, json.value("modified").toInt(-1)); emit noteUpdated(id, json); + } } void NotesApi::createApiNote(const QJsonObject &json) { int id = json["id"].toInt(-1); - if (id >= 0) + if (id >= 0) { + m_syncedNotes.insert(id, json.value("modified").toInt(-1)); emit noteCreated(id, json); + } } diff --git a/src/notesapi.h b/src/notesapi.h index 97a9bc3..34d330e 100644 --- a/src/notesapi.h +++ b/src/notesapi.h @@ -144,6 +144,9 @@ public: QString account() const; void setAccount(const QString& account); + const QList noteIds(); + int noteModified(const int id); + public slots: Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList()); Q_INVOKABLE bool getNote(const int id, const QStringList& exclude = QStringList()); @@ -198,6 +201,7 @@ private slots: private: QUrl m_url; QString m_account; + QMap m_syncedNotes; QNetworkAccessManager m_manager; QNetworkRequest m_request; QNetworkRequest m_authenticatedRequest; diff --git a/src/notesinterface.h b/src/notesinterface.h index 2e2e0b5..6f276bf 100644 --- a/src/notesinterface.h +++ b/src/notesinterface.h @@ -2,6 +2,7 @@ #define NOTESINTERFACE_H //#include +#include class NotesInterface { @@ -17,6 +18,9 @@ public: virtual QString account() const = 0; virtual void setAccount(const QString& account) = 0; + virtual const QList noteIds() = 0; + virtual int noteModified(const int id) = 0; + public slots: Q_INVOKABLE virtual bool getAllNotes(const QStringList& exclude = QStringList()) = 0; Q_INVOKABLE virtual bool getNote(const int id, const QStringList& exclude = QStringList()) = 0; diff --git a/src/notesmodel.cpp b/src/notesmodel.cpp index 691b2e1..5b654a8 100644 --- a/src/notesmodel.cpp +++ b/src/notesmodel.cpp @@ -130,6 +130,14 @@ void NotesModel::setAccount(const QString &account) { mp_notesStore->setAccount(account); } +const QList NotesModel::noteIds() { + return m_notes.keys(); +} + +int NotesModel::noteModified(const int id) { + return m_notes.value(id).value("modified").toInt(-1); +} + const QVariantMap NotesModel::getNoteById(const int id) const { return m_notes[id].toVariantMap(); } diff --git a/src/notesmodel.h b/src/notesmodel.h index cae242f..6f25d07 100644 --- a/src/notesmodel.h +++ b/src/notesmodel.h @@ -72,6 +72,9 @@ public: QString account() const; void setAccount(const QString& account); + const QList noteIds(); + int noteModified(const int id); + Q_INVOKABLE const QVariantMap getNoteById(const int id) const; public slots: diff --git a/src/notesstore.cpp b/src/notesstore.cpp index 7ddf9c6..e126809 100644 --- a/src/notesstore.cpp +++ b/src/notesstore.cpp @@ -45,6 +45,29 @@ void NotesStore::setAccount(const QString& account) { } } +const QList NotesStore::noteIds() { + QList ids; + if (m_dir.exists() && !account().isEmpty()) { + QFileInfoList files = m_dir.entryInfoList(); + for (int i = 0; i < files.size(); ++i) { + bool ok; + int id = files[i].baseName().toInt(&ok); + if (ok) { + ids << id; + } + } + } + else { + qDebug() << errorMessage(DirNotFoundError); + emit noteError(DirCannotReadError); + } + return ids; +} + +int NotesStore::noteModified(const int id) { + return readNoteFile(id, { "content" }).value("modified").toInt(-1); +} + const QString NotesStore::errorMessage(ErrorCodes error) const { QString message; switch (error) { @@ -76,25 +99,6 @@ const QString NotesStore::errorMessage(ErrorCodes error) const { return message; } -const QList NotesStore::noteFileIdList() { - QList ids; - if (m_dir.exists() && !account().isEmpty()) { - QFileInfoList files = m_dir.entryInfoList(); - for (int i = 0; i < files.size(); ++i) { - bool ok; - int id = files[i].baseName().toInt(&ok); - if (ok) { - ids << id; - } - } - } - else { - qDebug() << errorMessage(DirNotFoundError); - emit noteError(DirCannotReadError); - } - return ids; -} - bool NotesStore::noteFileExists(const int id) const { QFileInfo fileinfo(m_dir, QString("%1.%2").arg(id).arg(m_suffix)); return fileinfo.exists(); @@ -164,7 +168,7 @@ bool NotesStore::removeNoteFile(const int id) { bool NotesStore::getAllNotes(const QStringList& exclude) { qDebug() << "Getting all notes"; - const QList ids = noteFileIdList(); + const QList ids = noteIds(); if (!ids.empty()) { for (int i = 0; i < ids.size(); ++i) { getNote(ids.at(i), exclude); diff --git a/src/notesstore.h b/src/notesstore.h index 669c722..bd9e21c 100644 --- a/src/notesstore.h +++ b/src/notesstore.h @@ -24,6 +24,9 @@ public: QString account() const; void setAccount(const QString& account); + const QList noteIds(); + int noteModified(const int id); + enum ErrorCodes { NoError, FileNotFoundError, @@ -36,7 +39,6 @@ public: Q_ENUM(ErrorCodes) Q_INVOKABLE const QString errorMessage(ErrorCodes error) const; - const QList noteFileIdList(); bool noteFileExists(const int id) const; QJsonObject readNoteFile(const int id, const QStringList& exclude = QStringList()); bool writeNoteFile(const int id, const QJsonObject& note); diff --git a/translations/harbour-nextcloudnotes.ts b/translations/harbour-nextcloudnotes.ts index c282d52..1c1688b 100644 --- a/translations/harbour-nextcloudnotes.ts +++ b/translations/harbour-nextcloudnotes.ts @@ -263,52 +263,52 @@ NotePage - + Delete - + Reload - + Updating... - + Last update - + never - + Edit - + Notes - + No category - + Category - + Modified @@ -316,32 +316,32 @@ NotesApi - + No error - + No network connection available - + Failed to communicate with the Nextcloud server - + An error occured while establishing an encrypted connection - + Could not authenticate to the Nextcloud instance - + Unknown error @@ -447,42 +447,42 @@ NotesStore - + No error - + File not found - + Cannot read from the file - + Cannot write to the file - + Directory not found - + Cannot read from directory - + Cannot create or write to directory - + Unknown error