Clearing NotesModel when changing the account

This commit is contained in:
Scharel Clemens 2020-04-11 15:40:47 +02:00
parent 4ea61c565f
commit 10b9423f35
8 changed files with 79 additions and 135 deletions

View file

@ -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"))

View file

@ -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) {

View file

@ -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();

View file

@ -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<Note::NoteField> 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<Note::NoteField> noteFields = Note::noteFields();
@ -97,6 +100,8 @@ void NotesApi::getNote(const int id, Note::NoteField exclude) {
}
void NotesApi::createNote(const Note &note) {
// 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 &note) {
}
void NotesApi::updateNote(const Note &note) {
// 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 &note) {
}
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();

View file

@ -5,7 +5,7 @@
#include <QtMath>
#include <QDebug>
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<int> 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<int> NotesModel::ids() const {
@ -113,6 +59,7 @@ QVector<int> NotesModel::ids() const {
}
int NotesModel::insertNote(const Note &note) {
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 &note) {
}
bool NotesModel::removeNote(const Note &note) {
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 &note) {
}
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<int, QByteArray> NotesModel::roleNames() const {
return QHash<int, QByteArray> {
{NotesModel::IdRole, "id"},

View file

@ -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 &note);
bool removeNote(const Note &note);
bool removeNote(int id);
Q_INVOKABLE void clear();
protected:
//void addNote(const QJsonValue &note);
QVector<int> ids() const;
//int indexOf(const Note &note) const;
//int indexOf(int id) const;
//bool replaceNote(const Note &note);
signals:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ());
private:
QVector<Note> m_notes;
//QJsonArray m_notes;
};
#endif // NOTESMODEL_H

View file

@ -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.<NUMBER>.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);
}

View file

@ -311,37 +311,37 @@
<context>
<name>NotesApi</name>
<message>
<location filename="../src/notesapi.cpp" line="335"/>
<location filename="../src/notesapi.cpp" line="343"/>
<source>No network connection available</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="338"/>
<location filename="../src/notesapi.cpp" line="346"/>
<source>Failed to communicate with the Nextcloud server</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="341"/>
<location filename="../src/notesapi.cpp" line="349"/>
<source>An error happened while reading from the local storage</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="344"/>
<location filename="../src/notesapi.cpp" line="352"/>
<source>An error happened while writing to the local storage</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="347"/>
<location filename="../src/notesapi.cpp" line="355"/>
<source>An error occured while establishing an encrypted connection</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="350"/>
<location filename="../src/notesapi.cpp" line="358"/>
<source>Could not authenticate to the Nextcloud instance</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="353"/>
<location filename="../src/notesapi.cpp" line="361"/>
<source>Unknown error</source>
<translation type="unfinished"></translation>
</message>
@ -349,97 +349,97 @@
<context>
<name>NotesPage</name>
<message>
<location filename="../qml/pages/NotesPage.qml" line="54"/>
<location filename="../qml/pages/NotesPage.qml" line="30"/>
<source>Settings</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="58"/>
<location filename="../qml/pages/NotesPage.qml" line="34"/>
<source>Add note</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="63"/>
<location filename="../qml/pages/NotesPage.qml" line="39"/>
<source>Reload</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="63"/>
<location filename="../qml/pages/NotesPage.qml" line="39"/>
<source>Updating...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="69"/>
<location filename="../qml/pages/NotesPage.qml" line="45"/>
<source>Last update</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="72"/>
<location filename="../qml/pages/NotesPage.qml" line="48"/>
<source>never</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="82"/>
<location filename="../qml/pages/NotesPage.qml" line="58"/>
<source>Nextcloud Notes</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="223"/>
<location filename="../qml/pages/NotesPage.qml" line="199"/>
<source>Modified</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="226"/>
<location filename="../qml/pages/NotesPage.qml" line="202"/>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="228"/>
<location filename="../qml/pages/NotesPage.qml" line="204"/>
<source>Deleting note</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="258"/>
<location filename="../qml/pages/NotesPage.qml" line="234"/>
<source>Loading notes...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="264"/>
<location filename="../qml/pages/NotesPage.qml" line="240"/>
<source>No account yet</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="265"/>
<location filename="../qml/pages/NotesPage.qml" line="241"/>
<source>Got to the settings to add an account</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="271"/>
<location filename="../qml/pages/NotesPage.qml" line="247"/>
<source>No notes yet</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="272"/>
<location filename="../qml/pages/NotesPage.qml" line="248"/>
<source>Pull down to add a note</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="278"/>
<location filename="../qml/pages/NotesPage.qml" line="254"/>
<source>No result</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="279"/>
<location filename="../qml/pages/NotesPage.qml" line="255"/>
<source>Try another query</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="285"/>
<location filename="../qml/pages/NotesPage.qml" line="261"/>
<source>An error occurred</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="296"/>
<location filename="../qml/pages/NotesPage.qml" line="272"/>
<source>Open the settings to configure your Nextcloud accounts</source>
<translation type="unfinished"></translation>
</message>
@ -840,22 +840,22 @@ You can also use other markdown syntax inside them.</source>
<context>
<name>harbour-nextcloudnotes</name>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="110"/>
<location filename="../qml/harbour-nextcloudnotes.qml" line="117"/>
<source>Notes</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="111"/>
<location filename="../qml/harbour-nextcloudnotes.qml" line="118"/>
<source>Offline</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="112"/>
<location filename="../qml/harbour-nextcloudnotes.qml" line="119"/>
<source>Synced</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="119"/>
<location filename="../qml/harbour-nextcloudnotes.qml" line="126"/>
<source>Error</source>
<translation type="unfinished"></translation>
</message>