From 385cdc4cca4d99017b464f3c25d28cfb4a75b2c2 Mon Sep 17 00:00:00 2001 From: Scharel Clemens Date: Sat, 4 Jan 2020 16:08:40 +0100 Subject: [PATCH] Write notes to local files for upcoming offline mode --- src/notesapi.cpp | 10 ++++++---- src/notesmodel.cpp | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/notesapi.cpp b/src/notesapi.cpp index a3f5add..de2b0b1 100644 --- a/src/notesapi.cpp +++ b/src/notesapi.cpp @@ -136,10 +136,8 @@ void NotesApi::setPath(QString path) { void NotesApi::setDataFile(QString dataFile) { if (dataFile != m_jsonFile.fileName()) { m_jsonFile.close(); - if (!dataFile.isEmpty()) { + if (!dataFile.isEmpty()) m_jsonFile.setFileName(dataFile); - m_jsonFile.open(QIODevice::ReadWrite | QIODevice::Text); // | QIODevice::Unbuffered - } emit dataFileChanged(m_jsonFile.fileName()); //qDebug() << m_jsonFile.fileName(); } @@ -259,5 +257,9 @@ void NotesApi::sslError(QNetworkReply *reply, const QList &errors) { } void NotesApi::saveToFile(QModelIndex, QModelIndex, QVector) { - qDebug() << "Should write the data now to a file" << m_jsonFile.fileName(); + if (m_jsonFile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) { + qDebug() << "Writing data to file" << m_jsonFile.fileName(); + m_jsonFile.write(mp_model->toJsonDocument().toJson()); + m_jsonFile.close(); + } } diff --git a/src/notesmodel.cpp b/src/notesmodel.cpp index 7816f2a..609aaf2 100644 --- a/src/notesmodel.cpp +++ b/src/notesmodel.cpp @@ -141,6 +141,7 @@ bool NotesModel::removeNote(const Note ¬e) { beginRemoveRows(QModelIndex(), position, position); m_notes.removeAt(position); endRemoveRows(); + emit dataChanged(index(position), index(position)); return true; } return false;