Implemented two functions that should ease the syncronisation

This commit is contained in:
Scharel Clemens 2020-05-04 20:15:17 +02:00
parent fe31598042
commit 054afd4ca2
9 changed files with 92 additions and 53 deletions

View file

@ -156,9 +156,9 @@ Dialog {
return (appSettings.useCapitalX ? '- [X] ' : '- [x] ') + p1 }
else { return match }
} )
content = newContent
note["content"] = newContent
parseContent()
notesApi.updateNote(id, { 'content': content } )
notesApi.updateNote(id, { 'content': note["content"] } )
}
else if (/^tasklist:uncheckbox_(\d+)$/m.test(link)) {
newContent = newContent.replace(/- \[[xX]\] (.*)$/gm,
@ -168,9 +168,9 @@ Dialog {
return '- [ ] ' + p1 }
else { return match }
} )
content = newContent
note["content"] = newContent
parseContent()
notesApi.updateNote(id, { 'content': content } )
notesApi.updateNote(id, { 'content': note["content"] } )
}
else {
Qt.openUrlExternally(link)
@ -244,7 +244,7 @@ Dialog {
}
onFocusChanged: {
if (focus === false && text !== note["category"]) {
notesApi.updateNote(id, {'content': content, 'category': text, 'modified': new Date().valueOf() / 1000}) // This does not seem to work without adding the content
notesApi.updateNote(id, {'content': note["content"], 'category': text, 'modified': new Date().valueOf() / 1000}) // This does not seem to work without adding the content
}
}
}

View file

@ -53,6 +53,14 @@ void NotesApi::setAccount(const QString &account) {
}
}
const QList<int> NotesApi::noteIds() {
return m_syncedNotes.keys();
}
int NotesApi::noteModified(const int id) {
return m_syncedNotes.value(id, -1);
}
bool NotesApi::getAllNotes(const QStringList& exclude) {
qDebug() << "Getting all notes";
QUrl url = apiEndpointUrl(m_notesEndpoint);
@ -410,8 +418,10 @@ void NotesApi::replyFinished(QNetworkReply *reply) {
bool ok;
QString idString = reply->url().path().split('/', QString::SkipEmptyParts).last();
int id = idString.toInt(&ok);
if (reply->error() == QNetworkReply::NoError && id >= 0 && ok)
if (reply->error() == QNetworkReply::NoError && id >= 0 && ok) {
m_syncedNotes.remove(id);
emit noteDeleted(id);
}
m_deleteNoteReplies.removeOne(reply);
}
else if (m_loginReplies.contains(reply)) {
@ -613,12 +623,16 @@ void NotesApi::updateApiNotes(const QJsonArray &json) {
void NotesApi::updateApiNote(const QJsonObject &json) {
int id = json["id"].toInt(-1);
if (id >= 0)
if (id >= 0) {
m_syncedNotes.insert(id, json.value("modified").toInt(-1));
emit noteUpdated(id, json);
}
}
void NotesApi::createApiNote(const QJsonObject &json) {
int id = json["id"].toInt(-1);
if (id >= 0)
if (id >= 0) {
m_syncedNotes.insert(id, json.value("modified").toInt(-1));
emit noteCreated(id, json);
}
}

View file

@ -144,6 +144,9 @@ public:
QString account() const;
void setAccount(const QString& account);
const QList<int> noteIds();
int noteModified(const int id);
public slots:
Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList());
Q_INVOKABLE bool getNote(const int id, const QStringList& exclude = QStringList());
@ -198,6 +201,7 @@ private slots:
private:
QUrl m_url;
QString m_account;
QMap<int, int> m_syncedNotes;
QNetworkAccessManager m_manager;
QNetworkRequest m_request;
QNetworkRequest m_authenticatedRequest;

View file

@ -2,6 +2,7 @@
#define NOTESINTERFACE_H
//#include <QObject>
#include <QList>
class NotesInterface
{
@ -17,6 +18,9 @@ public:
virtual QString account() const = 0;
virtual void setAccount(const QString& account) = 0;
virtual const QList<int> noteIds() = 0;
virtual int noteModified(const int id) = 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;

View file

@ -130,6 +130,14 @@ void NotesModel::setAccount(const QString &account) {
mp_notesStore->setAccount(account);
}
const QList<int> NotesModel::noteIds() {
return m_notes.keys();
}
int NotesModel::noteModified(const int id) {
return m_notes.value(id).value("modified").toInt(-1);
}
const QVariantMap NotesModel::getNoteById(const int id) const {
return m_notes[id].toVariantMap();
}

View file

@ -72,6 +72,9 @@ public:
QString account() const;
void setAccount(const QString& account);
const QList<int> noteIds();
int noteModified(const int id);
Q_INVOKABLE const QVariantMap getNoteById(const int id) const;
public slots:

View file

@ -45,6 +45,29 @@ void NotesStore::setAccount(const QString& account) {
}
}
const QList<int> NotesStore::noteIds() {
QList<int> ids;
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) {
ids << id;
}
}
}
else {
qDebug() << errorMessage(DirNotFoundError);
emit noteError(DirCannotReadError);
}
return ids;
}
int NotesStore::noteModified(const int id) {
return readNoteFile(id, { "content" }).value("modified").toInt(-1);
}
const QString NotesStore::errorMessage(ErrorCodes error) const {
QString message;
switch (error) {
@ -76,25 +99,6 @@ const QString NotesStore::errorMessage(ErrorCodes error) const {
return message;
}
const QList<int> NotesStore::noteFileIdList() {
QList<int> ids;
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) {
ids << id;
}
}
}
else {
qDebug() << errorMessage(DirNotFoundError);
emit noteError(DirCannotReadError);
}
return ids;
}
bool NotesStore::noteFileExists(const int id) const {
QFileInfo fileinfo(m_dir, QString("%1.%2").arg(id).arg(m_suffix));
return fileinfo.exists();
@ -164,7 +168,7 @@ bool NotesStore::removeNoteFile(const int id) {
bool NotesStore::getAllNotes(const QStringList& exclude) {
qDebug() << "Getting all notes";
const QList<int> ids = noteFileIdList();
const QList<int> ids = noteIds();
if (!ids.empty()) {
for (int i = 0; i < ids.size(); ++i) {
getNote(ids.at(i), exclude);

View file

@ -24,6 +24,9 @@ public:
QString account() const;
void setAccount(const QString& account);
const QList<int> noteIds();
int noteModified(const int id);
enum ErrorCodes {
NoError,
FileNotFoundError,
@ -36,7 +39,6 @@ public:
Q_ENUM(ErrorCodes)
Q_INVOKABLE const QString errorMessage(ErrorCodes error) const;
const QList<int> noteFileIdList();
bool noteFileExists(const int id) const;
QJsonObject readNoteFile(const int id, const QStringList& exclude = QStringList());
bool writeNoteFile(const int id, const QJsonObject& note);

View file

@ -263,52 +263,52 @@
<context>
<name>NotePage</name>
<message>
<location filename="../qml/pages/NotePage.qml" line="96"/>
<location filename="../qml/pages/NotePage.qml" line="101"/>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="100"/>
<location filename="../qml/pages/NotePage.qml" line="105"/>
<source>Reload</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="100"/>
<location filename="../qml/pages/NotePage.qml" line="105"/>
<source>Updating...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="111"/>
<location filename="../qml/pages/NotePage.qml" line="116"/>
<source>Last update</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="114"/>
<location filename="../qml/pages/NotePage.qml" line="119"/>
<source>never</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="120"/>
<location filename="../qml/pages/NotePage.qml" line="125"/>
<source>Edit</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="121"/>
<location filename="../qml/pages/NotePage.qml" line="126"/>
<source>Notes</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="234"/>
<location filename="../qml/pages/NotePage.qml" line="239"/>
<source>No category</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="235"/>
<location filename="../qml/pages/NotePage.qml" line="240"/>
<source>Category</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotePage.qml" line="250"/>
<location filename="../qml/pages/NotePage.qml" line="255"/>
<source>Modified</source>
<translation type="unfinished"></translation>
</message>
@ -316,32 +316,32 @@
<context>
<name>NotesApi</name>
<message>
<location filename="../src/notesapi.cpp" line="324"/>
<location filename="../src/notesapi.cpp" line="332"/>
<source>No error</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="327"/>
<location filename="../src/notesapi.cpp" line="335"/>
<source>No network connection available</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="330"/>
<location filename="../src/notesapi.cpp" line="338"/>
<source>Failed to communicate with the Nextcloud server</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="333"/>
<location filename="../src/notesapi.cpp" line="341"/>
<source>An error occured while establishing an encrypted connection</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="336"/>
<location filename="../src/notesapi.cpp" line="344"/>
<source>Could not authenticate to the Nextcloud instance</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="339"/>
<location filename="../src/notesapi.cpp" line="347"/>
<source>Unknown error</source>
<translation type="unfinished"></translation>
</message>
@ -447,42 +447,42 @@
<context>
<name>NotesStore</name>
<message>
<location filename="../src/notesstore.cpp" line="52"/>
<location filename="../src/notesstore.cpp" line="75"/>
<source>No error</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesstore.cpp" line="55"/>
<location filename="../src/notesstore.cpp" line="78"/>
<source>File not found</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesstore.cpp" line="58"/>
<location filename="../src/notesstore.cpp" line="81"/>
<source>Cannot read from the file</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesstore.cpp" line="61"/>
<location filename="../src/notesstore.cpp" line="84"/>
<source>Cannot write to the file</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesstore.cpp" line="64"/>
<location filename="../src/notesstore.cpp" line="87"/>
<source>Directory not found</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesstore.cpp" line="67"/>
<location filename="../src/notesstore.cpp" line="90"/>
<source>Cannot read from directory</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesstore.cpp" line="70"/>
<location filename="../src/notesstore.cpp" line="93"/>
<source>Cannot create or write to directory</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesstore.cpp" line="73"/>
<location filename="../src/notesstore.cpp" line="96"/>
<source>Unknown error</source>
<translation type="unfinished"></translation>
</message>