Finished NotesStore class
This commit is contained in:
parent
60e1eb0bdd
commit
1d4022bdbd
6 changed files with 145 additions and 99 deletions
|
@ -2,8 +2,8 @@ import QtQuick 2.2
|
|||
import Sailfish.Silica 1.0
|
||||
import Nemo.Configuration 1.0
|
||||
import Nemo.Notifications 1.0
|
||||
import harbour.nextcloudnotes.note 1.0
|
||||
import harbour.nextcloudnotes.notesstore 1.0
|
||||
//import harbour.nextcloudnotes.note 1.0
|
||||
//import harbour.nextcloudnotes.notesstore 1.0
|
||||
import harbour.nextcloudnotes.notesapi 1.0
|
||||
import harbour.nextcloudnotes.notesmodel 1.0
|
||||
import "pages"
|
||||
|
@ -142,7 +142,7 @@ ApplicationWindow
|
|||
}
|
||||
}
|
||||
|
||||
NotesStore {
|
||||
/*NotesStore {
|
||||
id: notesStore
|
||||
|
||||
Component.onCompleted: getAllNotes()
|
||||
|
@ -160,6 +160,22 @@ ApplicationWindow
|
|||
onNoteDeleted: {
|
||||
//console.log("Note deleted", deletedNoteId)
|
||||
}
|
||||
}*/
|
||||
|
||||
Connections {
|
||||
target: notesStore
|
||||
|
||||
onAccountChanged: {
|
||||
console.log(notesStore.account)
|
||||
if (notesStore.account !== "")
|
||||
notesStore.getAllNotes()
|
||||
}
|
||||
onNoteUpdated: {
|
||||
console.log("Note updated", note["id"])
|
||||
}
|
||||
onNoteDeleted: {
|
||||
console.log("Note deleted", note["id"])
|
||||
}
|
||||
}
|
||||
|
||||
NotesApi {
|
||||
|
|
|
@ -21,10 +21,13 @@ int main(int argc, char *argv[])
|
|||
qRegisterMetaType<Note>();
|
||||
qmlRegisterType<Note>("harbour.nextcloudnotes.note", 1, 0, "Note");
|
||||
qmlRegisterType<NotesApi>("harbour.nextcloudnotes.notesapi", 1, 0, "NotesApi");
|
||||
qmlRegisterType<NotesStore>("harbour.nextcloudnotes.notesstore", 1, 0, "NotesStore");
|
||||
//qmlRegisterType<NotesStore>("harbour.nextcloudnotes.notesstore", 1, 0, "NotesStore");
|
||||
qmlRegisterType<NotesProxyModel>("harbour.nextcloudnotes.notesmodel", 1, 0, "NotesModel");
|
||||
|
||||
NotesStore* notesStore = new NotesStore;
|
||||
|
||||
QQuickView* view = SailfishApp::createView();
|
||||
view->rootContext()->setContextProperty("notesStore", notesStore);
|
||||
view->setSource(SailfishApp::pathTo("qml/harbour-nextcloudnotes.qml"));
|
||||
|
||||
#ifdef QT_DEBUG
|
||||
|
|
|
@ -16,6 +16,18 @@
|
|||
#define OCS_ENDPOINT "/ocs/v1.php/cloud"
|
||||
#define POLL_INTERVALL 5000
|
||||
|
||||
/*
|
||||
m_noteFieldNames[Id] = "id";
|
||||
m_noteFieldNames[Modified] = "modified";
|
||||
m_noteFieldNames[Title] = "title";
|
||||
m_noteFieldNames[Category] = "category";
|
||||
m_noteFieldNames[Content] = "content";
|
||||
m_noteFieldNames[Favorite] = "favorite";
|
||||
m_noteFieldNames[Etag] = "etag";
|
||||
m_noteFieldNames[Error] = "error";
|
||||
m_noteFieldNames[ErrorMessage] = "errorMessage";
|
||||
*/
|
||||
|
||||
class NotesApi : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
#define NOTESINTERFACE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVector>
|
||||
|
||||
#include "note.h"
|
||||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
|
||||
class NotesInterface : public QObject
|
||||
{
|
||||
|
@ -15,29 +14,49 @@ class NotesInterface : public QObject
|
|||
|
||||
public:
|
||||
explicit NotesInterface(QObject *parent = nullptr) : QObject(parent) {
|
||||
m_noteFieldNames = QMap<NoteField, QString>( { {Id, "id"}, {Modified, "modified"}, {Title, "title"}, {Category, "category"}, {Content, "content"}, {Favorite, "favorite"}, {Etag, "etag"}, {Error, "error"}, {ErrorMessage, "errorMessage"} } );
|
||||
}
|
||||
|
||||
virtual QString account() const = 0;
|
||||
virtual void setAccount(const QString& account) = 0;
|
||||
|
||||
Q_INVOKABLE virtual void getAllNotes() = 0;
|
||||
Q_INVOKABLE virtual void getNote(const int id) = 0;
|
||||
Q_INVOKABLE virtual void getNote(const Note& note) = 0;
|
||||
Q_INVOKABLE virtual void createNote(const Note& note) = 0;
|
||||
Q_INVOKABLE virtual void updateNote(const Note& note) = 0;
|
||||
enum NoteField {
|
||||
None = 0x0000,
|
||||
Id = 0x0001,
|
||||
Modified = 0x0002,
|
||||
Title = 0x0004,
|
||||
Category = 0x0008,
|
||||
Content = 0x0010,
|
||||
Favorite = 0x0020,
|
||||
Etag = 0x0040,
|
||||
Error = 0x0080,
|
||||
ErrorMessage = 0x0100
|
||||
};
|
||||
Q_DECLARE_FLAGS(NoteFields, NoteField)
|
||||
Q_FLAG(NoteFields)
|
||||
Q_INVOKABLE QList<NoteField> noteFields() const {
|
||||
return m_noteFieldNames.keys();
|
||||
}
|
||||
Q_INVOKABLE QString noteFieldName(NoteField field) {
|
||||
return m_noteFieldNames[field];
|
||||
}
|
||||
|
||||
Q_INVOKABLE virtual void getAllNotes(NoteField exclude = None) = 0;
|
||||
Q_INVOKABLE virtual void getNote(const int id, NoteField exclude = None) = 0;
|
||||
Q_INVOKABLE virtual void createNote(const QJsonObject& note) = 0;
|
||||
Q_INVOKABLE virtual void updateNote(const QJsonObject& note) = 0;
|
||||
Q_INVOKABLE virtual void deleteNote(const int id) = 0;
|
||||
Q_INVOKABLE virtual void deleteNote(const Note& note) = 0;
|
||||
Q_INVOKABLE virtual Note* noteData(const int id) = 0;
|
||||
Q_INVOKABLE virtual Note* noteData(const Note& note) = 0;
|
||||
|
||||
signals:
|
||||
void accountChanged(const QString& account);
|
||||
void noteCreated(Note* createdNote);
|
||||
void noteUpdated(Note* updatedNote);
|
||||
void noteDeleted(int deletedNoteId);
|
||||
void accountChanged(const QString account);
|
||||
void noteUpdated(const QJsonObject note);
|
||||
void noteDeleted(const int deletedNoteId);
|
||||
|
||||
public slots:
|
||||
|
||||
protected:
|
||||
QMap<NoteField, QString> m_noteFieldNames;
|
||||
|
||||
};
|
||||
|
||||
#endif // NOTESINTERFACE_H
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "notesstore.h"
|
||||
|
||||
#include "note.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
NotesStore::NotesStore(QString directory, QObject *parent) : NotesInterface(parent)
|
||||
|
@ -8,21 +10,16 @@ NotesStore::NotesStore(QString directory, QObject *parent) : NotesInterface(pare
|
|||
m_dir.setPath("");
|
||||
m_dir.setFilter(QDir::Files);
|
||||
m_dir.setNameFilters( { "*.json" } );
|
||||
m_note = nullptr;
|
||||
}
|
||||
|
||||
NotesStore::~NotesStore() {
|
||||
if (m_note)
|
||||
m_note->deleteLater();
|
||||
m_note = nullptr;
|
||||
}
|
||||
|
||||
QString NotesStore::account() const {
|
||||
QString dir;
|
||||
if (m_dir != QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation))) {
|
||||
dir = m_dir.dirName();
|
||||
return m_dir.path();
|
||||
}
|
||||
return dir;
|
||||
return QString();
|
||||
}
|
||||
|
||||
void NotesStore::setAccount(const QString& account) {
|
||||
|
@ -42,106 +39,104 @@ void NotesStore::setAccount(const QString& account) {
|
|||
}
|
||||
}
|
||||
|
||||
void NotesStore::getAllNotes() {
|
||||
void NotesStore::getAllNotes(NoteField exclude) {
|
||||
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);
|
||||
getNote(id, exclude);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NotesStore::getNote(const int id) {
|
||||
QFileInfo file(m_dir, QString("%1.json").arg(id));
|
||||
if (file.exists()) {
|
||||
emit noteUpdated(noteData(id));
|
||||
void NotesStore::getNote(const int id, NoteField exclude) {
|
||||
if (id >= 0) {
|
||||
QJsonObject file = readNoteFile(id, exclude);
|
||||
if (!file.empty())
|
||||
emit noteUpdated(file);
|
||||
}
|
||||
}
|
||||
|
||||
void NotesStore::getNote(const Note& note) {
|
||||
getNote(note.id());
|
||||
}
|
||||
|
||||
void NotesStore::createNote(const Note& note) {
|
||||
if (note.id() < 0) {
|
||||
void NotesStore::createNote(const QJsonObject& note) {
|
||||
if (Note::id(note) < 0) {
|
||||
// TODO probably crate files with an '.json.<NUMBER>.new' extension
|
||||
qDebug() << "Creating notes without the server API is not supported yet!";
|
||||
}
|
||||
else {
|
||||
updateNote(note);
|
||||
else if (!noteFileExists(Note::id(note))) {
|
||||
if (writeNoteFile(note)) {
|
||||
emit noteUpdated(note);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NotesStore::updateNote(const Note& note) {
|
||||
if (note.id() >= 0) {
|
||||
QFileInfo fileinfo(m_dir, QString("%1.json").arg(note.id()));
|
||||
QFile file(fileinfo.filePath());
|
||||
if (file.exists()) {
|
||||
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QJsonDocument fileJson = QJsonDocument::fromBinaryData(file.readAll());
|
||||
file.close();
|
||||
if (!note.equal(fileJson.object())) {
|
||||
if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
|
||||
QByteArray data = note.toJsonDocument().toJson();
|
||||
if (file.write(data) == data.size()) {
|
||||
emit noteUpdated(noteData(note));
|
||||
void NotesStore::updateNote(const QJsonObject& note) {
|
||||
if (Note::id(note) >= 0) {
|
||||
QJsonObject file = readNoteFile(Note::id(note));
|
||||
if (!Note(file).equal(note)) {
|
||||
if (writeNoteFile(note)) {
|
||||
emit noteUpdated(note);
|
||||
}
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
|
||||
QByteArray data = note.toJsonDocument().toJson();
|
||||
if (file.write(data) == data.size()) {
|
||||
emit noteCreated(noteData(note));
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
createNote(note);
|
||||
}
|
||||
}
|
||||
|
||||
void NotesStore::deleteNote(const int id) {
|
||||
QFileInfo fileinfo(m_dir, QString("%1.json").arg(id));
|
||||
if (fileinfo.exists()) {
|
||||
QFile file(fileinfo.filePath());
|
||||
if (file.remove()) {
|
||||
if (removeNoteFile(id)) {
|
||||
emit noteDeleted(id);
|
||||
if (m_note)
|
||||
m_note->deleteLater();
|
||||
m_note = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NotesStore::deleteNote(const Note& note) {
|
||||
deleteNote(note.id());
|
||||
bool NotesStore::noteFileExists(const int id) const {
|
||||
QFileInfo fileinfo(m_dir, QString("%1.json").arg(id));
|
||||
return fileinfo.exists();
|
||||
}
|
||||
|
||||
Note* NotesStore::noteData(const int id) {
|
||||
if (m_note)
|
||||
m_note->deleteLater();
|
||||
m_note = nullptr;
|
||||
QJsonObject NotesStore::readNoteFile(const int id, NoteField exclude) const {
|
||||
QJsonObject json;
|
||||
QFileInfo fileinfo(m_dir, QString("%1.json").arg(id));
|
||||
QFile file(fileinfo.filePath());
|
||||
if (file.exists()) {
|
||||
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QByteArray data = file.readAll();
|
||||
QJsonDocument json = QJsonDocument::fromJson(data);
|
||||
m_note = new Note(json.object());
|
||||
json = QJsonDocument::fromJson(data).object();
|
||||
file.close();
|
||||
QFlags<NoteField> flags(exclude);
|
||||
QMapIterator<NoteField, QString> fields(m_noteFieldNames);
|
||||
while (fields.hasNext()) {
|
||||
fields.next();
|
||||
if (flags.testFlag(fields.key())) {
|
||||
json.remove(fields.value());
|
||||
}
|
||||
}
|
||||
return m_note;
|
||||
}
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
Note* NotesStore::noteData(const Note& note) {
|
||||
return noteData(note.id());
|
||||
bool NotesStore::writeNoteFile(const QJsonObject ¬e) const {
|
||||
bool success = false;
|
||||
QJsonDocument json(note);
|
||||
QFileInfo fileinfo(m_dir, QString("%1.json").arg(Note::id(note)));
|
||||
QFile file(fileinfo.filePath());
|
||||
if (file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
|
||||
QByteArray data = json.toJson();
|
||||
if (file.write(data) == data.size()) {
|
||||
success = true;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool NotesStore::removeNoteFile(const int id) const {
|
||||
bool success = false;
|
||||
QFileInfo fileinfo(m_dir, QString("%1.json").arg(id));
|
||||
QFile file(fileinfo.filePath());
|
||||
if (file.exists()) {
|
||||
if (file.remove()) {
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
|
|
@ -19,22 +19,23 @@ public:
|
|||
QString account() const;
|
||||
void setAccount(const QString& account);
|
||||
|
||||
Q_INVOKABLE void getAllNotes(NoteField exclude = None);
|
||||
Q_INVOKABLE void getNote(const int id, NoteField exclude = None);
|
||||
Q_INVOKABLE void createNote(const QJsonObject& note);
|
||||
Q_INVOKABLE void updateNote(const QJsonObject& note);
|
||||
Q_INVOKABLE void deleteNote(const int id);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
Q_INVOKABLE void getAllNotes();
|
||||
Q_INVOKABLE void getNote(const int id);
|
||||
Q_INVOKABLE void getNote(const Note& note);
|
||||
Q_INVOKABLE void createNote(const Note& note);
|
||||
Q_INVOKABLE void updateNote(const Note& note);
|
||||
Q_INVOKABLE void deleteNote(const int id);
|
||||
Q_INVOKABLE void deleteNote(const Note& note);
|
||||
Q_INVOKABLE Note* noteData(const int id);
|
||||
Q_INVOKABLE Note* noteData(const Note& note);
|
||||
|
||||
private:
|
||||
QDir m_dir;
|
||||
Note* m_note;
|
||||
|
||||
bool noteFileExists(const int id) const;
|
||||
QJsonObject readNoteFile(const int id, NoteField exclude = None) const;
|
||||
bool writeNoteFile(const QJsonObject& note) const;
|
||||
bool removeNoteFile(const int id) const;
|
||||
};
|
||||
|
||||
#endif // NOTESSTORE_H
|
||||
|
|
Loading…
Reference in a new issue