Connected signals and slots throught the NotesModel, so this can later handle the synchronisation

This commit is contained in:
Scharel Clemens 2020-04-20 22:48:56 +02:00
parent cd55e83ae9
commit 84d720c7ca
10 changed files with 308 additions and 176 deletions

View file

@ -18,6 +18,7 @@ DEFINES += APP_VERSION=\\\"$$VERSION\\\"
HEADERS += src/note.h \ HEADERS += src/note.h \
src/notesapi.h \ src/notesapi.h \
src/notesinterface.h \
src/notesmodel.h \ src/notesmodel.h \
src/notesstore.h src/notesstore.h

View file

@ -29,11 +29,13 @@ int main(int argc, char *argv[])
NotesStore* notesStore = new NotesStore; NotesStore* notesStore = new NotesStore;
NotesApi* notesApi = new NotesApi; NotesApi* notesApi = new NotesApi;
notesModel->setNotesApi(notesApi);
notesModel->setNotesStore(notesStore);
//QObject::connect(notesApi, SIGNAL(allNotesReceived(QList<int>)), notesModel, SLOT()); //QObject::connect(notesApi, SIGNAL(allNotesReceived(QList<int>)), notesModel, SLOT());
QObject::connect(notesApi, SIGNAL(noteCreated(int,QJsonObject)), notesModel, SLOT(insertNoteFromApi(int,QJsonObject))); //QObject::connect(notesApi, SIGNAL(noteCreated(int,QJsonObject)), notesModel, SLOT(insertNoteFromApi(int,QJsonObject)));
QObject::connect(notesApi, SIGNAL(noteUpdated(int,QJsonObject)), notesModel, SLOT(updateNoteFromApi(int,QJsonObject))); //QObject::connect(notesApi, SIGNAL(noteUpdated(int,QJsonObject)), notesModel, SLOT(updateNoteFromApi(int,QJsonObject)));
QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesModel, SLOT(removeNoteFromApi(int))); //QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesModel, SLOT(removeNoteFromApi(int)));
//QObject::connect(notesApi, SIGNAL(noteUpdated(int,QJsonObject)), notesStore, SLOT(updateNote(int,QJsonObject))); //QObject::connect(notesApi, SIGNAL(noteUpdated(int,QJsonObject)), notesStore, SLOT(updateNote(int,QJsonObject)));
//QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesStore, SLOT(deleteNote(int))); //QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesStore, SLOT(deleteNote(int)));

View file

@ -7,7 +7,7 @@
#include <QDir> #include <QDir>
NotesApi::NotesApi(const QString statusEndpoint, const QString loginEndpoint, const QString ocsEndpoint, const QString notesEndpoint, QObject *parent) NotesApi::NotesApi(const QString statusEndpoint, const QString loginEndpoint, const QString ocsEndpoint, const QString notesEndpoint, QObject *parent)
: QObject(parent), m_statusEndpoint(statusEndpoint), m_loginEndpoint(loginEndpoint), m_ocsEndpoint(ocsEndpoint), m_notesEndpoint(notesEndpoint) : m_statusEndpoint(statusEndpoint), m_loginEndpoint(loginEndpoint), m_ocsEndpoint(ocsEndpoint), m_notesEndpoint(notesEndpoint)
{ {
// TODO verify connections (also in destructor) // TODO verify connections (also in destructor)
m_loginPollTimer.setInterval(POLL_INTERVALL); m_loginPollTimer.setInterval(POLL_INTERVALL);
@ -42,6 +42,17 @@ NotesApi::~NotesApi() {
disconnect(&m_manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(sslError(QNetworkReply*,QList<QSslError>))); disconnect(&m_manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(sslError(QNetworkReply*,QList<QSslError>)));
} }
QString NotesApi::account() const {
return m_account;
}
void NotesApi::setAccount(const QString &account) {
if (account != m_account) {
m_account = account;
emit accountChanged(account);
}
}
bool NotesApi::getAllNotes(const QStringList& exclude) { bool NotesApi::getAllNotes(const QStringList& exclude) {
qDebug() << "Getting all notes"; qDebug() << "Getting all notes";
QUrl url = apiEndpointUrl(m_notesEndpoint); QUrl url = apiEndpointUrl(m_notesEndpoint);
@ -597,7 +608,7 @@ void NotesApi::updateApiNotes(const QJsonArray &json) {
} }
} }
} }
emit allNotesReceived(ids); emit allNotesChanged(ids);
} }
void NotesApi::updateApiNote(const QJsonObject &json) { void NotesApi::updateApiNote(const QJsonObject &json) {

View file

@ -10,6 +10,8 @@
#include <QTimer> #include <QTimer>
#include <QDebug> #include <QDebug>
#include "notesinterface.h"
#define STATUS_ENDPOINT "/status.php" #define STATUS_ENDPOINT "/status.php"
#define LOGIN_ENDPOINT "/index.php/login/v2" #define LOGIN_ENDPOINT "/index.php/login/v2"
#define NOTES_ENDPOINT "/index.php/apps/notes/api/v0.2/notes" #define NOTES_ENDPOINT "/index.php/apps/notes/api/v0.2/notes"
@ -17,7 +19,7 @@
#define EXCLUDE_QUERY "exclude=" #define EXCLUDE_QUERY "exclude="
#define POLL_INTERVALL 5000 #define POLL_INTERVALL 5000
class NotesApi : public QObject class NotesApi : public QObject, public NotesInterface
{ {
Q_OBJECT Q_OBJECT
@ -139,6 +141,9 @@ public:
Q_ENUM(ErrorCodes) Q_ENUM(ErrorCodes)
Q_INVOKABLE const QString errorMessage(int error) const; Q_INVOKABLE const QString errorMessage(int error) const;
QString account() const;
void setAccount(const QString& account);
public slots: public slots:
Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList()); Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList());
Q_INVOKABLE bool getNote(const int id, const QStringList& exclude = QStringList()); Q_INVOKABLE bool getNote(const int id, const QStringList& exclude = QStringList());
@ -175,13 +180,12 @@ signals:
void loginStatusChanged(LoginStatus status); void loginStatusChanged(LoginStatus status);
void loginUrlChanged(QUrl url); void loginUrlChanged(QUrl url);
void allNotesReceived(const QList<int>& ids); void accountChanged(const QString& account);
void allNotesChanged(const QList<int>& ids);
void noteCreated(const int id, const QJsonObject& note); void noteCreated(const int id, const QJsonObject& note);
void noteUpdated(const int id, const QJsonObject& note); void noteUpdated(const int id, const QJsonObject& note);
void noteDeleted(const int id); void noteDeleted(const int id);
void noteError(const ErrorCodes error); void noteError(ErrorCodes error);
public slots:
private slots: private slots:
void verifyUrl(QUrl url); void verifyUrl(QUrl url);
@ -193,6 +197,7 @@ private slots:
private: private:
QUrl m_url; QUrl m_url;
QString m_account;
QNetworkAccessManager m_manager; QNetworkAccessManager m_manager;
QNetworkRequest m_request; QNetworkRequest m_request;
QNetworkRequest m_authenticatedRequest; QNetworkRequest m_authenticatedRequest;

36
src/notesinterface.h Normal file
View file

@ -0,0 +1,36 @@
#ifndef NOTESINTERFACE_H
#define NOTESINTERFACE_H
//#include <QObject>
class NotesInterface
{
//Q_CLASSINFO("author", "Scharel Clemens")
//Q_CLASSINFO("url", "https://github.com/scharel/harbour-nextcloudnotes")
Q_PROPERTY(QString account READ account WRITE setAccount NOTIFY accountChanged)
public:
explicit NotesInterface() {
}
virtual QString account() const = 0;
virtual void setAccount(const QString& account) = 0;
public slots:
Q_INVOKABLE virtual bool getAllNotes(const QStringList& exclude = QStringList()) = 0;
Q_INVOKABLE virtual bool getNote(const int id, const QStringList& exclude = QStringList()) = 0;
Q_INVOKABLE virtual bool createNote(const QJsonObject& note) = 0;
Q_INVOKABLE virtual bool updateNote(const int id, const QJsonObject& note) = 0;
Q_INVOKABLE virtual bool deleteNote(const int id) = 0;
signals:
virtual void accountChanged(const QString& account) = 0;
virtual void allNotesChanged(const QList<int>& ids) = 0;
virtual void noteCreated(const int id, const QJsonObject& note) = 0;
virtual void noteUpdated(const int id, const QJsonObject& note) = 0;
virtual void noteDeleted(const int id) = 0;
};
#endif // NOTESINTERFACE_H

View file

@ -68,49 +68,131 @@ const QHash<int, QByteArray> NotesModel::m_roleNames = QHash<int, QByteArray> (
{NotesModel::NoneRole, "none"} } ); {NotesModel::NoneRole, "none"} } );
NotesModel::NotesModel(QObject *parent) : QAbstractListModel(parent) { NotesModel::NotesModel(QObject *parent) : QAbstractListModel(parent) {
mp_notesApi = nullptr;
mp_notesStore = nullptr;
} }
NotesModel::~NotesModel() { NotesModel::~NotesModel() {
setNotesApi(nullptr);
setNotesStore(nullptr);
clear(); clear();
} }
const QJsonArray NotesModel::getAllNotes(const QStringList &exclude) { void NotesModel::setNotesApi(NotesApi *notesApi) {
qDebug() << "Getting all Notes"; if (mp_notesApi) {
QJsonArray array; // disconnect stuff
QMapIterator<int, QJsonObject> i(m_notes); disconnect(mp_notesApi, SIGNAL(accountChanged(QString)), this, SIGNAL(accountChanged(QString)));
while (i.hasNext()) { disconnect(mp_notesApi, SIGNAL(noteCreated(int,QJsonObject)), this, SLOT(insert(int,QJsonObject)));
i.next(); disconnect(mp_notesApi, SIGNAL(noteUpdated(int,QJsonObject)), this, SLOT(update(int,QJsonObject)));
array << QJsonValue(i.value()); disconnect(mp_notesApi, SIGNAL(noteDeleted(int)), this, SLOT(remove(int)));
}
mp_notesApi = notesApi;
if (mp_notesApi) {
// connect stuff
connect(mp_notesApi, SIGNAL(accountChanged(QString)), this, SIGNAL(accountChanged(QString)));
connect(mp_notesApi, SIGNAL(noteCreated(int,QJsonObject)), this, SLOT(insert(int,QJsonObject)));
connect(mp_notesApi, SIGNAL(noteUpdated(int,QJsonObject)), this, SLOT(update(int,QJsonObject)));
connect(mp_notesApi, SIGNAL(noteDeleted(int)), this, SLOT(remove(int)));
} }
return array;
} }
const QJsonObject NotesModel::getNote(const int id, const QStringList &exclude) { void NotesModel::setNotesStore(NotesStore *notesStore) {
qDebug() << "Getting note: " << id; if (mp_notesStore) {
return m_notes.value(id); // disconnect stuff
disconnect(mp_notesStore, SIGNAL(accountChanged(QString)), this, SIGNAL(accountChanged(QString)));
disconnect(mp_notesStore, SIGNAL(noteCreated(int,QJsonObject)), this, SLOT(insert(int,QJsonObject)));
disconnect(mp_notesStore, SIGNAL(noteUpdated(int,QJsonObject)), this, SLOT(update(int,QJsonObject)));
disconnect(mp_notesStore, SIGNAL(noteDeleted(int)), this, SLOT(remove(int)));
}
mp_notesStore = notesStore;
if (mp_notesStore) {
// connect stuff
connect(mp_notesStore, SIGNAL(accountChanged(QString)), this, SIGNAL(accountChanged(QString)));
connect(mp_notesStore, SIGNAL(noteCreated(int,QJsonObject)), this, SLOT(insert(int,QJsonObject)));
connect(mp_notesStore, SIGNAL(noteUpdated(int,QJsonObject)), this, SLOT(update(int,QJsonObject)));
connect(mp_notesStore, SIGNAL(noteDeleted(int)), this, SLOT(remove(int)));
}
} }
void NotesModel::insertNote(const int id, const QJsonObject& note) { QString NotesModel::account() const {
QString account;
if (mp_notesStore)
account = mp_notesStore->account();
else if (mp_notesApi)
account = mp_notesApi->account();
return account;
}
void NotesModel::setAccount(const QString &account) {
if (mp_notesApi)
mp_notesApi->setAccount(account);
if (mp_notesStore)
mp_notesStore->setAccount(account);
}
bool NotesModel::getAllNotes(const QStringList &exclude) {
bool success = true;
if (mp_notesApi)
success &= mp_notesApi->getAllNotes(exclude);
if (mp_notesStore)
success &= mp_notesStore->getAllNotes(exclude);
return success;
}
bool NotesModel::getNote(const int id, const QStringList &exclude) {
bool success = true;
if (mp_notesApi)
success &= mp_notesApi->getNote(id, exclude);
if (mp_notesStore)
success &= mp_notesStore->getNote(id, exclude);
return success;
}
bool NotesModel::createNote(const QJsonObject &note) {
bool success = true;
if (mp_notesApi)
success &= mp_notesApi->createNote(note);
return success;
}
bool NotesModel::updateNote(const int id, const QJsonObject &note) {
bool success = true;
if (mp_notesApi)
success &= mp_notesApi->updateNote(id, note);
if (mp_notesStore)
success &= mp_notesStore->updateNote(id, note);
return success;
}
bool NotesModel::deleteNote(const int id) {
bool success = true;
if (mp_notesApi)
success &= mp_notesApi->deleteNote(id);
if (mp_notesStore)
success &= mp_notesStore->deleteNote(id);
return success;
}
void NotesModel::insert(const int id, const QJsonObject& note) {
qDebug() << "Inserting note: " << id; qDebug() << "Inserting note: " << id;
if (m_notes.contains(id)) { if (m_notes.contains(id)) {
qDebug() << "Note already present"; qDebug() << "Note already present";
updateNote(id, note); update(id, note);
} }
else { else {
beginInsertRows(QModelIndex(), indexOfNoteById(id), indexOfNoteById(id)); beginInsertRows(QModelIndex(), indexOfNoteById(id), indexOfNoteById(id));
m_notes.insert(id, note); m_notes.insert(id, note);
endInsertRows(); endInsertRows();
emit noteInserted(id, note); //emit noteInserted(id, note);
qDebug() << "Note inserted"; qDebug() << "Note inserted";
} }
} }
void NotesModel::updateNote(const int id, const QJsonObject &note) { void NotesModel::update(const int id, const QJsonObject &note) {
qDebug() << "Updating note: " << id; qDebug() << "Updating note: " << id;
if (!m_notes.contains(id)) { if (!m_notes.contains(id)) {
qDebug() << "Note is new"; qDebug() << "Note is new";
insertNote(id, note); insert(id, note);
} }
else { else {
if (m_notes.value(id) == note) { if (m_notes.value(id) == note) {
@ -125,7 +207,7 @@ void NotesModel::updateNote(const int id, const QJsonObject &note) {
} }
} }
void NotesModel::removeNote(const int id) { void NotesModel::remove(const int id) {
qDebug() << "Removing note: " << id; qDebug() << "Removing note: " << id;
if (m_notes.contains(id)) { if (m_notes.contains(id)) {
beginRemoveRows(QModelIndex(), indexOfNoteById(id), indexOfNoteById(id)); beginRemoveRows(QModelIndex(), indexOfNoteById(id), indexOfNoteById(id));
@ -133,36 +215,12 @@ void NotesModel::removeNote(const int id) {
qDebug() << "Note not found"; qDebug() << "Note not found";
} }
else { else {
emit noteRemoved(id); //emit noteRemoved(id);
} }
endRemoveRows(); endRemoveRows();
} }
} }
void NotesModel::insertNoteFromApi(const int id, const QJsonObject &note) {
insertNote(id, note);
}
void NotesModel::updateNoteFromApi(const int id, const QJsonObject &note) {
updateNote(id, note);
}
void NotesModel::removeNoteFromApi(const int id) {
removeNote(id);
}
void NotesModel::insertNoteFromStore(const int id, const QJsonObject &note) {
insertNote(id, note);
}
void NotesModel::updateNoteFromStore(const int id, const QJsonObject &note) {
updateNote(id, note);
}
void NotesModel::removeNoteFromStore(const int id) {
removeNote(id);
}
void NotesModel::clear() { void NotesModel::clear() {
qDebug() << "Clearing model"; qDebug() << "Clearing model";
beginResetModel(); beginResetModel();

View file

@ -6,6 +6,9 @@
#include <QJsonArray> #include <QJsonArray>
#include <QDateTime> #include <QDateTime>
#include "note.h" #include "note.h"
#include "notesinterface.h"
#include "notesapi.h"
#include "notesstore.h"
class NotesProxyModel : public QSortFilterProxyModel { class NotesProxyModel : public QSortFilterProxyModel {
Q_OBJECT Q_OBJECT
@ -34,7 +37,7 @@ private:
bool m_favoritesOnTop; bool m_favoritesOnTop;
}; };
class NotesModel : public QAbstractListModel { class NotesModel : public QAbstractListModel, public NotesInterface {
Q_OBJECT Q_OBJECT
public: public:
@ -63,31 +66,41 @@ public:
QMap<int, QVariant> itemData(const QModelIndex &index) const; QMap<int, QVariant> itemData(const QModelIndex &index) const;
virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles); virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
void setNotesApi(NotesApi* notesApi);
void setNotesStore(NotesStore *notesStore);
QString account() const;
void setAccount(const QString& account);
public slots: public slots:
Q_INVOKABLE const QJsonArray getAllNotes(const QStringList& exclude = QStringList()); Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList());
Q_INVOKABLE const QJsonObject getNote(const int id, const QStringList& exclude = QStringList()); Q_INVOKABLE bool getNote(const int id, const QStringList& exclude = QStringList());
Q_INVOKABLE void insertNote(const int id, const QJsonObject& note); Q_INVOKABLE bool createNote(const QJsonObject& note);
Q_INVOKABLE void updateNote(const int id, const QJsonObject& note); Q_INVOKABLE bool updateNote(const int id, const QJsonObject& note);
Q_INVOKABLE void removeNote(const int id); Q_INVOKABLE bool deleteNote(const int id);
Q_INVOKABLE void insertNoteFromApi(const int id, const QJsonObject& note);
Q_INVOKABLE void updateNoteFromApi(const int id, const QJsonObject& note); void insert(const int id, const QJsonObject& note);
Q_INVOKABLE void removeNoteFromApi(const int id); void update(const int id, const QJsonObject& note);
Q_INVOKABLE void insertNoteFromStore(const int id, const QJsonObject& note); void remove(const int id);
Q_INVOKABLE void updateNoteFromStore(const int id, const QJsonObject& note);
Q_INVOKABLE void removeNoteFromStore(const int id);
Q_INVOKABLE void clear(); Q_INVOKABLE void clear();
Q_INVOKABLE int indexOfNoteById(int id) const; Q_INVOKABLE int indexOfNoteById(int id) const;
signals: signals:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ()); void accountChanged(const QString& account);
void noteInserted(const int id, const QJsonObject& note); void allNotesChanged(const QList<int>& ids);
void noteCreated(const int id, const QJsonObject& note);
void noteUpdated(const int id, const QJsonObject& note); void noteUpdated(const int id, const QJsonObject& note);
void noteRemoved(const int id); void noteDeleted(const int id);
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ());
private: private:
QMap<int, QJsonObject> m_notes; QMap<int, QJsonObject> m_notes;
const static QHash<int, QByteArray> m_roleNames; const static QHash<int, QByteArray> m_roleNames;
NotesApi* mp_notesApi;
NotesStore* mp_notesStore;
}; };
#endif // NOTESMODEL_H #endif // NOTESMODEL_H

View file

@ -76,98 +76,6 @@ const QString NotesStore::errorMessage(ErrorCodes error) const {
return message; return message;
} }
const QJsonArray NotesStore::getAllNotes(const QStringList& exclude) {
qDebug() << "Getting all notes";
QJsonArray notes;
if (m_dir.exists()) {
QFileInfoList files = m_dir.entryInfoList();
for (int i = 0; i < files.size(); ++i) {
bool ok;
int id = files[i].baseName().toInt(&ok);
if (ok) {
notes.append(QJsonValue(getNote(id, exclude)));
}
}
}
else {
qDebug() << errorMessage(DirNotFoundError);
emit noteError(DirCannotReadError);
}
return notes;
}
const QJsonObject NotesStore::getNote(const int id, const QStringList& exclude) {
qDebug() << "Getting note: " << id;
QJsonObject note;
if (id >= 0) {
note = readNoteFile(id, exclude);
emit noteUpdated(id, note);
}
else {
qDebug() << "Skipping, invalid ID";
}
return note;
}
/*
bool NotesStore::createNote(const int id, const QJsonObject& note) {
qDebug() << "Creating note: " << id;
if (id < 0) {
// TODO probably crate files with an '.json.<NUMBER>.new' extension
qDebug() << "Creating notes without the server API is not supported yet!";
}
else if (!noteFileExists(id)) {
if (writeNoteFile(id, note)) {
emit noteUpdated(id, note);
return true;
}
}
else {
qDebug() << "Note already exists";
}
return false;
}
*/
bool NotesStore::updateNote(const int id, const QJsonObject& note) {
qDebug() << "Updating note: " << id;
if (id >= 0) {
QJsonObject tmpNote = readNoteFile(id);
if (note != tmpNote) {
if (note.value("modified").toInt() >= tmpNote.value("modified").toInt() || note.value("modified").toInt() == 0) {
QStringList fields = note.keys();
for (int i = 0; i < fields.size(); ++i) {
tmpNote[fields[i]] = note[fields[i]];
}
if (tmpNote.value("modified").toInt() == 0) {
tmpNote["modified"] = QJsonValue::fromVariant(QDateTime::currentDateTime().toTime_t());
}
if (writeNoteFile(id, tmpNote)) {
emit noteUpdated(id, tmpNote);
return true;
}
}
else {
qDebug() << "Skipping, note is older" << QDateTime::fromTime_t(note.value("modified").toInt()) << QDateTime::fromTime_t(tmpNote.value("modified").toInt());
}
}
else {
qDebug() << "Skipping, note is equal";
}
}
else {
qDebug() << "Skipping, invalid ID";
}
return false;
}
bool NotesStore::deleteNote(const int id) {
qDebug() << "Deleting note: " << id;
if (removeNoteFile(id)) {
emit noteDeleted(id);
return true;
}
return false;
}
bool NotesStore::noteFileExists(const int id) const { bool NotesStore::noteFileExists(const int id) const {
QFileInfo fileinfo(m_dir, QString("%1.%2").arg(id).arg(m_suffix)); QFileInfo fileinfo(m_dir, QString("%1.%2").arg(id).arg(m_suffix));
return fileinfo.exists(); return fileinfo.exists();
@ -234,3 +142,98 @@ bool NotesStore::removeNoteFile(const int id) {
} }
return success; return success;
} }
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)));
}
}
return true;
}
else {
qDebug() << errorMessage(DirNotFoundError);
emit noteError(DirCannotReadError);
}
return false;
}
bool NotesStore::getNote(const int id, const QStringList& exclude) {
qDebug() << "Getting note: " << id;
QJsonObject note;
if (id >= 0) {
note = readNoteFile(id, exclude);
emit noteUpdated(id, note);
return true;
}
else {
qDebug() << "Skipping, invalid ID";
}
return false;
}
bool NotesStore::createNote(const QJsonObject& note) {
/* qDebug() << "Creating note: " << id;
if (id < 0) {
// TODO probably crate files with an '.json.<NUMBER>.new' extension
qDebug() << "Creating notes without the server API is not supported yet!";
}
else if (!noteFileExists(id)) {
if (writeNoteFile(id, note)) {
emit noteUpdated(id, note);
return true;
}
}
else {
qDebug() << "Note already exists";
}*/
return false;
}
bool NotesStore::updateNote(const int id, const QJsonObject& note) {
qDebug() << "Updating note: " << id;
if (id >= 0) {
QJsonObject tmpNote = readNoteFile(id);
if (note != tmpNote) {
if (note.value("modified").toInt() >= tmpNote.value("modified").toInt() || note.value("modified").toInt() == 0) {
QStringList fields = note.keys();
for (int i = 0; i < fields.size(); ++i) {
tmpNote[fields[i]] = note[fields[i]];
}
if (tmpNote.value("modified").toInt() == 0) {
tmpNote["modified"] = QJsonValue::fromVariant(QDateTime::currentDateTime().toTime_t());
}
if (writeNoteFile(id, tmpNote)) {
emit noteUpdated(id, tmpNote);
return true;
}
}
else {
qDebug() << "Skipping, note is older" << QDateTime::fromTime_t(note.value("modified").toInt()) << QDateTime::fromTime_t(tmpNote.value("modified").toInt());
}
}
else {
qDebug() << "Skipping, note is equal";
}
}
else {
qDebug() << "Skipping, invalid ID";
}
return false;
}
bool NotesStore::deleteNote(const int id) {
qDebug() << "Deleting note: " << id;
if (removeNoteFile(id)) {
emit noteDeleted(id);
return true;
}
return false;
}

View file

@ -7,7 +7,9 @@
#include <QDir> #include <QDir>
#include <QStandardPaths> #include <QStandardPaths>
class NotesStore : public QObject #include "notesinterface.h"
class NotesStore : public QObject, public NotesInterface
{ {
Q_OBJECT Q_OBJECT
@ -34,16 +36,22 @@ public:
Q_ENUM(ErrorCodes) Q_ENUM(ErrorCodes)
Q_INVOKABLE const QString errorMessage(ErrorCodes error) const; Q_INVOKABLE const QString errorMessage(ErrorCodes error) const;
bool noteFileExists(const int id) const;
QJsonObject readNoteFile(const int id, const QStringList& exclude = QStringList());
bool writeNoteFile(const int id, const QJsonObject& note);
bool removeNoteFile(const int id);
public slots: public slots:
Q_INVOKABLE const QJsonArray getAllNotes(const QStringList& exclude = QStringList()); Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList());
Q_INVOKABLE const QJsonObject getNote(const int id, const QStringList& exclude = QStringList()); Q_INVOKABLE bool getNote(const int id, const QStringList& exclude = QStringList());
//Q_INVOKABLE bool createNote(const int id, const QJsonObject& note); Q_INVOKABLE bool createNote(const QJsonObject& note);
Q_INVOKABLE bool updateNote(const int id, const QJsonObject& note); Q_INVOKABLE bool updateNote(const int id, const QJsonObject& note);
Q_INVOKABLE bool deleteNote(const int id); Q_INVOKABLE bool deleteNote(const int id);
signals: signals:
void accountChanged(const QString& account); void accountChanged(const QString& account);
//void noteCreated(const int id, const QJsonObject& note); void allNotesChanged(const QList<int>& ids);
void noteCreated(const int id, const QJsonObject& note);
void noteUpdated(const int id, const QJsonObject& note); void noteUpdated(const int id, const QJsonObject& note);
void noteDeleted(const int id); void noteDeleted(const int id);
void noteError(const ErrorCodes error); void noteError(const ErrorCodes error);
@ -51,11 +59,6 @@ signals:
private: private:
QDir m_dir; QDir m_dir;
const static QString m_suffix; const static QString m_suffix;
bool noteFileExists(const int id) const;
QJsonObject readNoteFile(const int id, const QStringList& exclude = QStringList());
bool writeNoteFile(const int id, const QJsonObject& note);
bool removeNoteFile(const int id);
}; };
#endif // NOTESSTORE_H #endif // NOTESSTORE_H

View file

@ -311,32 +311,32 @@
<context> <context>
<name>NotesApi</name> <name>NotesApi</name>
<message> <message>
<location filename="../src/notesapi.cpp" line="313"/> <location filename="../src/notesapi.cpp" line="324"/>
<source>No error</source> <source>No error</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="316"/> <location filename="../src/notesapi.cpp" line="327"/>
<source>No network connection available</source> <source>No network connection available</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="319"/> <location filename="../src/notesapi.cpp" line="330"/>
<source>Failed to communicate with the Nextcloud server</source> <source>Failed to communicate with the Nextcloud server</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="322"/> <location filename="../src/notesapi.cpp" line="333"/>
<source>An error occured while establishing an encrypted connection</source> <source>An error occured while establishing an encrypted connection</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="325"/> <location filename="../src/notesapi.cpp" line="336"/>
<source>Could not authenticate to the Nextcloud instance</source> <source>Could not authenticate to the Nextcloud instance</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../src/notesapi.cpp" line="328"/> <location filename="../src/notesapi.cpp" line="339"/>
<source>Unknown error</source> <source>Unknown error</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>