From 842ea9f6991cd2314bfa776e14ae0579a2d10edc Mon Sep 17 00:00:00 2001 From: Scharel Clemens Date: Sun, 3 May 2020 13:00:36 +0200 Subject: [PATCH] Prepare sync of offline changed notes --- qml/pages/LoginPage.qml | 1 + src/notesmodel.cpp | 13 ++++++++++--- src/notesmodel.h | 1 + src/notesstore.cpp | 37 ++++++++++++++++++++++++------------- src/notesstore.h | 1 + 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/qml/pages/LoginPage.qml b/qml/pages/LoginPage.qml index 4a5c1df..3d4fc79 100644 --- a/qml/pages/LoginPage.qml +++ b/qml/pages/LoginPage.qml @@ -204,6 +204,7 @@ Page { TextSwitch { id: forceLegacyButton + visible: debug text: qsTr("Enforce legacy login") onCheckedChanged: { checked != checked diff --git a/src/notesmodel.cpp b/src/notesmodel.cpp index c9cad9f..ce6be79 100644 --- a/src/notesmodel.cpp +++ b/src/notesmodel.cpp @@ -177,6 +177,13 @@ bool NotesModel::deleteNote(const int id) { return success; } +bool NotesModel::syncNotes() { + if (mp_notesApi && mp_notesStore) { + // TODO + } + return false; +} + void NotesModel::insert(const int id, const QJsonObject& note) { qDebug() << "Inserting note: " << id; if (m_notes.contains(id)) { @@ -215,11 +222,11 @@ void NotesModel::remove(const int id) { qDebug() << "Removing note: " << id; if (m_notes.contains(id)) { beginRemoveRows(QModelIndex(), indexOfNoteById(id), indexOfNoteById(id)); - if (m_notes.remove(id) == 0) { - qDebug() << "Note not found"; + if (m_notes.remove(id) > 0) { + emit noteDeleted(id); } else { - //emit noteRemoved(id); + qDebug() << "Note not found"; } endRemoveRows(); } diff --git a/src/notesmodel.h b/src/notesmodel.h index 3353e38..33adb2a 100644 --- a/src/notesmodel.h +++ b/src/notesmodel.h @@ -80,6 +80,7 @@ public slots: Q_INVOKABLE bool createNote(const QJsonObject& note); Q_INVOKABLE bool updateNote(const int id, const QJsonObject& note); Q_INVOKABLE bool deleteNote(const int id); + Q_INVOKABLE bool syncNotes(); void insert(const int id, const QJsonObject& note); void update(const int id, const QJsonObject& note); diff --git a/src/notesstore.cpp b/src/notesstore.cpp index f9a6653..e61c69e 100644 --- a/src/notesstore.cpp +++ b/src/notesstore.cpp @@ -76,6 +76,25 @@ 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(); @@ -145,24 +164,16 @@ bool NotesStore::removeNoteFile(const int id) { bool NotesStore::getAllNotes(const QStringList& exclude) { qDebug() << "Getting all notes"; - //QJsonArray notes; - 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) { - getNote(id, exclude); - //notes.append(QJsonValue(getNote(id, exclude))); - } + const QList ids = noteFileIdList(); + if (!ids.empty()) { + for (int i = 0; i < ids.size(); ++i) { + getNote(ids.at(i), exclude); } return true; } else { - qDebug() << errorMessage(DirNotFoundError); - emit noteError(DirCannotReadError); + return false; } - return false; } bool NotesStore::getNote(const int id, const QStringList& exclude) { diff --git a/src/notesstore.h b/src/notesstore.h index edc5290..669c722 100644 --- a/src/notesstore.h +++ b/src/notesstore.h @@ -36,6 +36,7 @@ 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);