From 10b9423f356156b3741e2bfb3cf307e221572a57 Mon Sep 17 00:00:00 2001 From: Scharel Clemens Date: Sat, 11 Apr 2020 15:40:47 +0200 Subject: [PATCH] Clearing NotesModel when changing the account --- qml/harbour-nextcloudnotes.qml | 23 ++++---- qml/pages/NotesPage.qml | 24 --------- src/harbour-nextcloudnotes.cpp | 8 +-- src/notesapi.cpp | 8 +++ src/notesmodel.cpp | 72 ++++++-------------------- src/notesmodel.h | 10 +--- src/notesstore.cpp | 9 +++- translations/harbour-nextcloudnotes.ts | 60 ++++++++++----------- 8 files changed, 79 insertions(+), 135 deletions(-) diff --git a/qml/harbour-nextcloudnotes.qml b/qml/harbour-nextcloudnotes.qml index 5000683..901ea1e 100644 --- a/qml/harbour-nextcloudnotes.qml +++ b/qml/harbour-nextcloudnotes.qml @@ -33,6 +33,7 @@ ApplicationWindow onPasswordChanged: notesApi.password = password onDoNotVerifySslChanged: notesApi.verifySsl = !doNotVerifySsl onPathChanged: { + notesModel.sourceModel.clear() notesStore.account = appSettings.currentAccount notesApi.account = appSettings.currentAccount } @@ -53,6 +54,16 @@ ApplicationWindow property bool useMonoFont: value("useMonoFont", false, Boolean) property bool useCapitalX: value("useCapitalX", false, Boolean) + onSortByChanged: { + if (sortBy == "none") + notesModel.invalidate() + else + notesModel.sortRole = notesModel.roleFromName(sortBy) + } + onFavoritesOnTopChanged: { + notesModel.favoritesOnTop = favoritesOnTop + } + function addAccount() { var uuid = uuidv4() var tmpIDs = accounts.value @@ -143,14 +154,7 @@ ApplicationWindow onAccountChanged: { //console.log(notesStore.account) - if (notesStore.account !== "") - notesStore.getAllNotes() - } - onNoteUpdated: { - //console.log("Note updated", note.id) - } - onNoteDeleted: { - //console.log("Note deleted", note.id) + notesStore.getAllNotes() } } @@ -159,8 +163,7 @@ ApplicationWindow onAccountChanged: { //console.log(notesStore.account) - if (notesApi.account !== "") - notesApi.getAllNotes() + notesApi.getAllNotes() } onNetworkAccessibleChanged: { console.log("Device is " + (accessible ? "online" : "offline")) diff --git a/qml/pages/NotesPage.qml b/qml/pages/NotesPage.qml index 63746f7..2efe3c9 100644 --- a/qml/pages/NotesPage.qml +++ b/qml/pages/NotesPage.qml @@ -7,30 +7,6 @@ Page { property string searchString - Connections { - target: appSettings - onSortByChanged: { - if (appSettings.sortBy == "none") - notesModel.invalidate() - else - notesModel.sortRole = notesModel.roleFromName(appSettings.sortBy) - } - onFavoritesOnTopChanged: { - notesModel.favoritesOnTop = appSettings.favoritesOnTop - } - onCurrentAccountChanged: { - notesList.model = 0 - notesList.model = notesApi.model() - if (appSettings.currentAccount.length > 0) - notesApi.getAllNotes() - - } - } - Component.onCompleted: { - notesModel.favoritesOnTop = appSettings.favoritesOnTop - notesModel.sortRole = notesModel.roleFromName(appSettings.sortBy) - } - onStatusChanged: { if (status === PageStatus.Active) { if (accounts.value.length <= 0) { diff --git a/src/harbour-nextcloudnotes.cpp b/src/harbour-nextcloudnotes.cpp index eef617c..cbcf397 100644 --- a/src/harbour-nextcloudnotes.cpp +++ b/src/harbour-nextcloudnotes.cpp @@ -30,9 +30,9 @@ int main(int argc, char *argv[]) NotesStore* notesStore = new NotesStore; NotesApi* notesApi = new NotesApi; - QObject::connect(notesApi, SIGNAL(noteUpdated(Note)), notesStore, SLOT(updateNote(Note))); + //QObject::connect(notesApi, SIGNAL(noteUpdated(Note)), notesStore, SLOT(updateNote(Note))); //QObject::connect(notesStore, SIGNAL(noteUpdated(Note)), notesApi, SLOT(updateNote(Note))); - QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesStore, SLOT(deleteNote(int))); + //QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesStore, SLOT(deleteNote(int))); //QObject::connect(notesStore, SIGNAL(noteDeleted(int)), notesApi, SLOT(deleteNote(int))); QObject::connect(notesStore, SIGNAL(noteUpdated(Note)), notesModel, SLOT(insertNote(Note))); @@ -60,9 +60,9 @@ int main(int argc, char *argv[]) QObject::disconnect(notesStore, SIGNAL(noteUpdated(Note)), notesModel, SLOT(insertNote(Note))); //QObject::disconnect(notesStore, SIGNAL(noteDeleted(int)), notesApi, SLOT(deleteNote(int))); - QObject::disconnect(notesApi, SIGNAL(noteDeleted(int)), notesStore, SLOT(deleteNote(int))); + //QObject::disconnect(notesApi, SIGNAL(noteDeleted(int)), notesStore, SLOT(deleteNote(int))); //QObject::disconnect(notesStore, SIGNAL(noteUpdated(Note)), notesApi, SLOT(updateNote(Note))); - QObject::disconnect(notesApi, SIGNAL(noteUpdated(Note)), notesStore, SLOT(updateNote(Note))); + //QObject::disconnect(notesApi, SIGNAL(noteUpdated(Note)), notesStore, SLOT(updateNote(Note))); notesApi->deleteLater(); notesStore->deleteLater(); diff --git a/src/notesapi.cpp b/src/notesapi.cpp index 4fb766b..bd7c086 100644 --- a/src/notesapi.cpp +++ b/src/notesapi.cpp @@ -45,6 +45,7 @@ NotesApi::~NotesApi() { } void NotesApi::setAccount(const QString &account) { + qDebug() << "Setting account: " << account; if (account != m_account) { m_account = account; // TODO reset the class @@ -53,6 +54,7 @@ void NotesApi::setAccount(const QString &account) { } void NotesApi::getAllNotes(Note::NoteField exclude) { + qDebug() << "Getting all notes"; QUrl url = apiEndpointUrl(m_notesEndpoint); QStringList excludeFields; QList noteFields = Note::noteFields(); @@ -75,6 +77,7 @@ void NotesApi::getAllNotes(Note::NoteField exclude) { } void NotesApi::getNote(const int id, Note::NoteField exclude) { + qDebug() << "Getting note: " << id; QUrl url = apiEndpointUrl(m_notesEndpoint + QString("/%1").arg(id)); QStringList excludeFields; QList noteFields = Note::noteFields(); @@ -97,6 +100,8 @@ void NotesApi::getNote(const int id, Note::NoteField exclude) { } void NotesApi::createNote(const Note ¬e) { + // TODO verify modified + qDebug() << "Creating note: " << note.id(); QUrl url = apiEndpointUrl(m_notesEndpoint); if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { qDebug() << "POST" << url.toDisplayString(); @@ -107,6 +112,8 @@ void NotesApi::createNote(const Note ¬e) { } void NotesApi::updateNote(const Note ¬e) { + // TODO verify modified + qDebug() << "Updating note: " << note.id(); QUrl url = apiEndpointUrl(m_notesEndpoint + QString("/%1").arg(note.id())); if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { qDebug() << "PUT" << url.toDisplayString(); @@ -117,6 +124,7 @@ void NotesApi::updateNote(const Note ¬e) { } void NotesApi::deleteNote(const int id) { + qDebug() << "Deleting note: " << id; QUrl url = apiEndpointUrl(m_notesEndpoint + QString("/%1").arg(id)); if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) { qDebug() << "DELETE" << url.toDisplayString(); diff --git a/src/notesmodel.cpp b/src/notesmodel.cpp index 78452d3..cfb70e8 100644 --- a/src/notesmodel.cpp +++ b/src/notesmodel.cpp @@ -5,7 +5,7 @@ #include #include -NotesProxyModel::NotesProxyModel(QObject *parent) { +NotesProxyModel::NotesProxyModel(QObject *parent) : QSortFilterProxyModel(parent) { m_favoritesOnTop = true; //connect(this, SIGNAL(favoritesOnTopChanged(bool)), this, SLOT(resort())); } @@ -42,66 +42,12 @@ void NotesProxyModel::sort() { QSortFilterProxyModel::sort(0); } -NotesModel::NotesModel(QObject *parent) { +NotesModel::NotesModel(QObject *parent) : QAbstractListModel(parent) { } NotesModel::~NotesModel() { - m_notes.clear(); -} - -bool NotesModel::fromJsonDocument(const QJsonDocument &jdoc) { - qDebug() << "Applying new JSON input"; // << json; - if (!jdoc.isNull() && !jdoc.isEmpty()) { - if (jdoc.isArray()) { - //qDebug() << "- It's an array..."; - QVector notesIdsToRemove; - QJsonArray jarr = jdoc.array(); - if (!jarr.empty()) - notesIdsToRemove = ids(); - while (!jarr.empty()) { - QJsonValue jval = jarr.first(); - if (jval.isObject()) { - QJsonObject jobj = jval.toObject(); - if (!jobj.isEmpty()) { - insertNote(jobj); - notesIdsToRemove.removeAll(Note::id(jobj)); - } - } - else { - qDebug() << "-- JSON array element is not an object!"; - } - jarr.pop_front(); - } - while (!notesIdsToRemove.empty()) { - removeNote(notesIdsToRemove.first()); - notesIdsToRemove.pop_front(); - } - return true; - } - else if (jdoc.isObject()) { - //qDebug() << "- It's a single object..."; - insertNote(jdoc.object()); - } - else if (jdoc.isEmpty()) { - qDebug() << "- Empty JSON document."; - } - else { - qDebug() << "- Unknown JSON document. This message should never occure!"; - } - } - else { - qDebug() << "JSON document is NULL!"; - } - return false; -} - -QJsonDocument NotesModel::toJsonDocument() const { - QJsonArray jarr; - for (int i = 0; i < m_notes.size(); ++i) { - jarr << m_notes[i].toJsonValue(); - } - return QJsonDocument(jarr); + clear(); } QVector NotesModel::ids() const { @@ -113,6 +59,7 @@ QVector NotesModel::ids() const { } int NotesModel::insertNote(const Note ¬e) { + qDebug() << "Inserting note: " << note.id(); int position = m_notes.indexOf(note); if (position >= 0) { if (m_notes.at(position).equal(note)) { @@ -138,6 +85,7 @@ int NotesModel::insertNote(const Note ¬e) { } bool NotesModel::removeNote(const Note ¬e) { + qDebug() << "Removing note: " << note.id(); int position = m_notes.indexOf(note); if (position >= 0 && position < m_notes.size()) { beginRemoveRows(QModelIndex(), position, position); @@ -150,9 +98,19 @@ bool NotesModel::removeNote(const Note ¬e) { } bool NotesModel::removeNote(int id) { + qDebug() << "Removing note: " << id; return removeNote(Note(QJsonObject{ {"id", id} } )); } +void NotesModel::clear() { + qDebug() << "Clearing model"; + int lastNoteIndex = m_notes.size() - 1; + beginRemoveRows(QModelIndex(), 0, lastNoteIndex); + m_notes.clear(); + endRemoveRows(); + emit dataChanged(index(0), index(lastNoteIndex)); +} + QHash NotesModel::roleNames() const { return QHash { {NotesModel::IdRole, "id"}, diff --git a/src/notesmodel.h b/src/notesmodel.h index d459089..27d0517 100644 --- a/src/notesmodel.h +++ b/src/notesmodel.h @@ -38,9 +38,6 @@ public: explicit NotesModel(QObject *parent = 0); virtual ~NotesModel(); - bool fromJsonDocument(const QJsonDocument &jdoc); - QJsonDocument toJsonDocument() const; - enum NoteRoles { IdRole = Qt::UserRole, ModifiedRole = Qt::UserRole + 1, @@ -67,21 +64,16 @@ public slots: int insertNote(const Note ¬e); bool removeNote(const Note ¬e); bool removeNote(int id); - + Q_INVOKABLE void clear(); protected: - //void addNote(const QJsonValue ¬e); QVector ids() const; - //int indexOf(const Note ¬e) const; - //int indexOf(int id) const; - //bool replaceNote(const Note ¬e); signals: void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles = QVector ()); private: QVector m_notes; - //QJsonArray m_notes; }; #endif // NOTESMODEL_H diff --git a/src/notesstore.cpp b/src/notesstore.cpp index 59f413d..1e7f2fb 100644 --- a/src/notesstore.cpp +++ b/src/notesstore.cpp @@ -23,7 +23,7 @@ QString NotesStore::account() const { } void NotesStore::setAccount(const QString& account) { - //qDebug() << account << m_dir.path(); + qDebug() << "Setting account: " << account; if (account != m_dir.path()) { if (m_dir != QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation))) { m_dir.cdUp(); @@ -40,6 +40,7 @@ void NotesStore::setAccount(const QString& account) { } void NotesStore::getAllNotes(Note::NoteField exclude) { + qDebug() << "Getting all notes"; QFileInfoList files = m_dir.entryInfoList(); for (int i = 0; i < files.size(); ++i) { bool ok; @@ -51,6 +52,7 @@ void NotesStore::getAllNotes(Note::NoteField exclude) { } void NotesStore::getNote(const int id, Note::NoteField exclude) { + qDebug() << "Getting note: " << id; if (id >= 0) { Note note = readNoteFile(id, exclude); if (note.isValid()) @@ -59,6 +61,8 @@ void NotesStore::getNote(const int id, Note::NoteField exclude) { } void NotesStore::createNote(const Note& note) { + // TODO verify modified + qDebug() << "Creating note: " << note.id(); if (!note.isValid()) { // TODO probably crate files with an '.json..new' extension qDebug() << "Creating notes without the server API is not supported yet!"; @@ -71,6 +75,8 @@ void NotesStore::createNote(const Note& note) { } void NotesStore::updateNote(const Note& note) { + // TODO verify modified + qDebug() << "Updating note: " << note.id(); if (note.isValid()) { Note file = readNoteFile(note.id()); if (!file.equal(note)) { @@ -82,6 +88,7 @@ void NotesStore::updateNote(const Note& note) { } void NotesStore::deleteNote(const int id) { + qDebug() << "Deleting note: " << id; if (removeNoteFile(id)) { emit noteDeleted(id); } diff --git a/translations/harbour-nextcloudnotes.ts b/translations/harbour-nextcloudnotes.ts index f6276e3..e2b3fd4 100644 --- a/translations/harbour-nextcloudnotes.ts +++ b/translations/harbour-nextcloudnotes.ts @@ -311,37 +311,37 @@ NotesApi - + No network connection available - + Failed to communicate with the Nextcloud server - + An error happened while reading from the local storage - + An error happened while writing to the local storage - + An error occured while establishing an encrypted connection - + Could not authenticate to the Nextcloud instance - + Unknown error @@ -349,97 +349,97 @@ NotesPage - + Settings - + Add note - + Reload - + Updating... - + Last update - + never - + Nextcloud Notes - + Modified - + Delete - + Deleting note - + Loading notes... - + 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 @@ -840,22 +840,22 @@ You can also use other markdown syntax inside them. harbour-nextcloudnotes - + Notes - + Offline - + Synced - + Error