From 66b56d0ed26f447ef8e785f4fb9f8f081182bb08 Mon Sep 17 00:00:00 2001 From: Scharel Clemens Date: Mon, 13 May 2019 23:32:37 +0200 Subject: [PATCH] Worked on the c++ model --- qml/pages/NotePage.qml | 57 ++++++++------- qml/pages/NotesPage.qml | 12 +++- src/notesmodel.cpp | 98 +++++++------------------- src/notesmodel.h | 5 +- translations/harbour-nextcloudnotes.ts | 42 +++++------ 5 files changed, 93 insertions(+), 121 deletions(-) diff --git a/qml/pages/NotePage.qml b/qml/pages/NotePage.qml index dd7944c..d290614 100644 --- a/qml/pages/NotePage.qml +++ b/qml/pages/NotePage.qml @@ -6,8 +6,16 @@ import "../js/showdown/dist/showdown.js" as ShowDown Dialog { id: noteDialog - property var note - property int noteID + property int id + property int modified + property string title + property string category + property string content + property bool favorite + property string etag + property bool error + property string errorMessage + property string date property var showdown: ShowDown.showdown property var converter: new showdown.Converter( @@ -30,27 +38,26 @@ Dialog { } onStatusChanged: { if (status === DialogStatus.Opened) { - api.getNoteFromApi(noteID) + api.getNoteFromApi(id) } } Component.onCompleted: { - noteID = note.id parseContent() } function reloadContent() { - api.getNoteFromApi(note.id) - /*note = api.getNote(note.id) - dialogHeader.title = note.title - favoriteButton.selected = note.favorite - categoryField.text = note.category - modifiedDetail.modified = note.modified + api.getNoteFromApi(id) + /*note = api.getNote(id) + dialogHeader.title = title + favoriteButton.selected = favorite + categoryField.text = category + modifiedDetail.modified = modified parseContent()*/ } function parseContent() { - //note = api.getNoteFromApi(note.id, false) - var convertedText = converter.makeHtml(note.content) + //note = api.getNoteFromApi(id, false) + var convertedText = converter.makeHtml(content) var occurence = -1 convertedText = convertedText.replace(/^
  • (

    )?\[ \] (.*)(<.*)$/gmi, function(match, p1, p2, p3, offset) { @@ -93,7 +100,7 @@ Dialog { MenuItem { text: qsTr("Delete") - onClicked: remorse.execute("Deleting", function() { api.deleteNote(note.id) } ) + onClicked: remorse.execute("Deleting", function() { api.deleteNote(id) } ) } MenuItem { text: enabled ? qsTr("Reload") : qsTr("Updating...") @@ -111,7 +118,7 @@ Dialog { DialogHeader { id: dialogHeader - title: note.title + title: title acceptText: qsTr("Edit") cancelText: qsTr("Notes") } @@ -143,7 +150,7 @@ Dialog { onLinkActivated: { //console.log(link) var occurence = -1 - var newContent = note.content + var newContent = content if (/^tasklist:checkbox_(\d+)$/m.test(link)) { newContent = newContent.replace(/- \[ \] (.*)$/gm, function(match, p1, offset, string) { @@ -152,9 +159,9 @@ Dialog { return (appSettings.useCapitalX ? '- [X] ' : '- [x] ') + p1 } else { return match } } ) - note.content = newContent + content = newContent parseContent() - api.updateNote(note.id, { 'content': note.content } ) + api.updateNote(id, { 'content': content } ) } else if (/^tasklist:uncheckbox_(\d+)$/m.test(link)) { newContent = newContent.replace(/- \[[xX]\] (.*)$/gm, @@ -164,9 +171,9 @@ Dialog { return '- [ ] ' + p1 } else { return match } } ) - note.content = newContent + content = newContent parseContent() - api.updateNote(note.id, { 'content': note.content } ) + api.updateNote(id, { 'content': content } ) } else { Qt.openUrlExternally(link) @@ -221,18 +228,18 @@ Dialog { width: parent.width - x IconButton { id: favoriteButton - property bool selected: note.favorite + property bool selected: favorite width: Theme.iconSizeMedium icon.source: (selected ? "image://theme/icon-m-favorite-selected?" : "image://theme/icon-m-favorite?") + (favoriteButton.highlighted ? Theme.secondaryHighlightColor : Theme.secondaryColor) onClicked: { - api.updateNote(note.id, {'favorite': !note.favorite}) + api.updateNote(id, {'favorite': !favorite}) } } TextField { id: categoryField width: parent.width - favoriteButton.width - text: note.category + text: category placeholderText: qsTr("No category") label: qsTr("Category") EnterKey.iconSource: "image://theme/icon-m-enter-accept" @@ -240,8 +247,8 @@ Dialog { categoryField.focus = false } onFocusChanged: { - if (focus === false && text !== note.category) { - api.updateNote(note.id, {'content': note.content, 'category': text}) // This does not seem to work without adding the content + if (focus === false && text !== category) { + api.updateNote(id, {'content': content, 'category': text}) // This does not seem to work without adding the content } } } @@ -250,7 +257,7 @@ Dialog { DetailItem { id: modifiedDetail label: qsTr("Modified") - property int modified: note.modified + property int modified: modified value: new Date(modified * 1000).toLocaleString(Qt.locale(), Locale.ShortFormat) } } diff --git a/qml/pages/NotesPage.qml b/qml/pages/NotesPage.qml index 1fc6d4b..933a115 100644 --- a/qml/pages/NotesPage.qml +++ b/qml/pages/NotesPage.qml @@ -103,7 +103,17 @@ Page { } onClicked: pageStack.push(Qt.resolvedUrl("../pages/NotePage.qml"), - { note: notesList.model.get(index) }) + { id: id, + modified: modified, + title: title, + category: category, + content: content, + favorite: favorite, + etag: etag, + error: error, + errorMessage: errorMessage, + date: date + }) onPressAndHold: menu.open(note) Separator { diff --git a/src/notesmodel.cpp b/src/notesmodel.cpp index 9416298..c70105c 100644 --- a/src/notesmodel.cpp +++ b/src/notesmodel.cpp @@ -57,99 +57,32 @@ void NotesModel::clearSearch() { search(); } -bool NotesModel::applyJSON(QString json, bool replaceIfArray) { +bool NotesModel::applyJSON(const QString &json) { qDebug() << "Applying new JSON input";// << json; - uint notesModified = 0; QJsonParseError error; QJsonDocument jdoc = QJsonDocument::fromJson(json.toUtf8(), &error); if (!jdoc.isNull() && error.error == QJsonParseError::NoError) { if (jdoc.isArray()) { qDebug() << "- It's an array..."; QJsonArray jarr = jdoc.array(); - QList notesToRemove; - for (int i = 0; i < m_notes.size(); i++) - notesToRemove << i; while (!jarr.empty()) { //qDebug() << jarr.count() << "JSON Objects to handle..."; QJsonValue jval = jarr.first(); if (jval.isObject()) { //qDebug() << "It's an object, all fine..."; - QJsonObject jobj = jval.toObject(); - if (!jobj.isEmpty()) { - //qDebug() << "Adding it to the model..."; - Note note = Note::fromjson(jobj); // TODO connect signals - if (!note.error()) { - int oldPosition = indexOf(note.id()); - Note oldNote = get(oldPosition); - if (oldPosition >= 0 && note.etag() != oldNote.etag() && replaceIfArray) { - removeNote(note.id()); - insertNote(note); - } - else if (oldPosition) { //TODO - } - //qDebug() << "New note" << note.title << "adding it to the notes to add..."; - int position = insertPosition(note); - - insertNote(note); - //qDebug() << "Adding note"<< note.title << "on position" << position; - //beginInsertRows(QModelIndex(), position, position); - //m_notes.insert(position, note); - //endInsertRows(); - } - else { - qDebug() << "Note contains an error:" << note.errorMessage(); - } - } - else { - qDebug() << "Unknown JSON object. This message should never occure!"; - } + insertJnote(jval.toObject()); } jarr.pop_front(); } - // TODO the current implementation does not respect the changement of the index - /*for (int i = 0; i < notesToRemove.size(); i++) { - qDebug() << "Removing note" << m_notes[notesToRemove[i]].note.title; - beginRemoveRows(QModelIndex(), notesToRemove[i], notesToRemove[i]); - m_notes.removeAt(notesToRemove[i]); - endRemoveRows(); - }*/ - /*for (int i = 0; i < notesToAdd.size(); i++) { - beginInsertRows(QModelIndex(), notesToAdd[i].param, notesToAdd[i].param); - ModelNote note; - note.note = notesToAdd[i].note; - qDebug() << "Adding note"<< note.note.title << "on position" << notesToAdd[i].param; - m_notes.insert(notesToAdd[i].param, note); - endInsertRows(); - }*/ } else if (jdoc.isObject()) { qDebug() << "It's a single object..."; - QJsonObject jobj = jdoc.object(); - if (!jobj.isEmpty() && !jobj.value(roleNames()[ErrorRole]).toBool(true)) { - Note note = Note::fromjson(jobj); // TODO connect signals - int position = indexOf(note.id()); - if (position >= 0 && replaceIfArray) { - m_notes[position].note = note; - } - else { - position = insertPosition(note); - ModelNote noteToInsert; - noteToInsert.note = note; noteToInsert.param = true; - beginInsertRows(index(position), position, position); - m_notes.insert(position, noteToInsert); - endInsertRows(); - } - notesModified++; - } + insertJnote(jdoc.object()); } else { qDebug() << "Unknown JSON document. This message should never occure!"; + return false; } - if (notesModified > 0) { - sort(); // TODO react to signal connect() - search(m_searchText); - } - return true; } else { @@ -158,7 +91,7 @@ bool NotesModel::applyJSON(QString json, bool replaceIfArray) { return error.error == QJsonParseError::NoError; } -int NotesModel::insertNote(Note ¬e) { +int NotesModel::insertNote(const Note ¬e) { int position = insertPosition(note); ModelNote modelNote; modelNote.note = note; @@ -392,6 +325,27 @@ void NotesModel::sort() { } } +void NotesModel::insertJnote(const QJsonObject &jobj) { + if (!jobj.isEmpty()) { + //qDebug() << "Adding it to the model..."; + Note note = Note::fromjson(jobj); // TODO connect signals + if (!note.error()) { + int oldPosition = indexOf(note.id()); + Note oldNote = get(oldPosition); + if (note.etag() != oldNote.etag()) { + removeNote(note.id()); + } + insertNote(note); + } + else { + qDebug() << "Note contains an error:" << note.errorMessage(); + } + } + else { + qDebug() << "Unknown JSON object. This message should never occure!"; + } +} + int NotesModel::insertPosition(const Note &n) const { int lower = 0; int upper = m_notes.size(); diff --git a/src/notesmodel.h b/src/notesmodel.h index ff0ca67..fa48f76 100644 --- a/src/notesmodel.h +++ b/src/notesmodel.h @@ -32,8 +32,8 @@ public: Q_INVOKABLE void search(QString searchText = QString()); Q_INVOKABLE void clearSearch(); - Q_INVOKABLE bool applyJSON(QString json, bool replaceIfArray = true); - Q_INVOKABLE int insertNote(Note ¬e); + Q_INVOKABLE bool applyJSON(const QString &json); + Q_INVOKABLE int insertNote(const Note ¬e); Q_INVOKABLE bool removeAt(int position); Q_INVOKABLE bool removeNote(int id); Q_INVOKABLE void clear(); @@ -94,6 +94,7 @@ private: void sort(); //void update(); + void insertJnote(const QJsonObject &jobj); int insertPosition(const Note &n) const; bool noteLessThan(const Note &n1, const Note &n2) const; /*static bool noteLessThanByDate(const Note &n1, const Note &n2); diff --git a/translations/harbour-nextcloudnotes.ts b/translations/harbour-nextcloudnotes.ts index 825b365..fe53674 100644 --- a/translations/harbour-nextcloudnotes.ts +++ b/translations/harbour-nextcloudnotes.ts @@ -232,52 +232,52 @@ NotePage - + Delete - + Reload - + Updating... - + Last update - + never - + Edit - + Notes - + No category - + Category - + Modified @@ -328,57 +328,57 @@ - + Modified - + Delete - + Deleting note - + No account yet - + Got to the settings to add an account - + No notes yet - + Pull down to add a note - + No result - + Try another query - + An error occurred - + Open the settings to configure your Nextcloud accounts