Updated signals and slots of the modules

This commit is contained in:
Scharel Clemens 2020-04-16 21:25:36 +02:00
parent 312be40fcf
commit 433a049a14
12 changed files with 107 additions and 80 deletions

View file

@ -58,7 +58,6 @@ ApplicationWindow
account.path = "/apps/harbour-nextcloudnotes/accounts/" + currentAccount
notesStore.account = currentAccount
notesStore.getAllNotes()
notesApi.account = currentAccount
notesApi.getAllNotes()
}

View file

@ -12,6 +12,9 @@
# * date Author's Name <author's email> version-release
# - Summary of changes
* Thu Apr 13 2020 Scharel Clemens <harbour-nextcloudnotes@scharel.rocks> 0.8-1
- Updated Signals and Slots between the modules
* Mon Apr 13 2020 Scharel Clemens <harbour-nextcloudnotes@scharel.rocks> 0.8-0
- New methods for data handling improved

View file

@ -14,7 +14,7 @@ Name: harbour-nextcloudnotes
%{?qtc_builddir:%define _builddir %qtc_builddir}
Summary: Nextcloud Notes
Version: 0.8
Release: 0
Release: 1
Group: Applications/Editors
License: MIT
URL: https://github.com/scharel/harbour-nextcloudnotes

View file

@ -1,7 +1,7 @@
Name: harbour-nextcloudnotes
Summary: Nextcloud Notes
Version: 0.8
Release: 0
Release: 1
# The contents of the Group field should be one of the groups listed here:
# https://github.com/mer-tools/spectacle/blob/master/data/GROUPS
Group: Applications/Editors

View file

@ -30,15 +30,17 @@ int main(int argc, char *argv[])
NotesStore* notesStore = new NotesStore;
NotesApi* notesApi = new NotesApi;
QObject::connect(notesApi, SIGNAL(noteUpdated(int, QJsonObject)), notesStore, SLOT(updateNote(int, QJsonObject)));
//QObject::connect(notesStore, SIGNAL(noteUpdated(int, QJsonObject)), notesApi, SLOT(updateNote(int, QJsonObject)));
QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesStore, SLOT(deleteNote(int)));
//QObject::connect(notesStore, SIGNAL(noteDeleted(int)), notesApi, SLOT(deleteNote(int)));
//QObject::connect(notesApi, SIGNAL(allNotesReceived(QList<int>)), notesModel, SLOT());
QObject::connect(notesApi, SIGNAL(noteCreated(int,QJsonObject)), notesModel, SLOT(insertNote(int,QJsonObject)));
QObject::connect(notesApi, SIGNAL(noteUpdated(int,QJsonObject)), notesModel, SLOT(updateNote(int,QJsonObject)));
QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesModel, SLOT(removeNote(int)));
QObject::connect(notesStore, SIGNAL(noteUpdated(int, QJsonObject)), notesModel, SLOT(insertNote(int, QJsonObject)));
QObject::connect(notesApi, SIGNAL(noteCreated(int,QJsonObject)), notesStore, SLOT(insertNote(int,QJsonObject)));
QObject::connect(notesApi, SIGNAL(noteUpdated(int,QJsonObject)), notesStore, SLOT(updateNote(int,QJsonObject)));
QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesStore, SLOT(removeNote(int)));
QObject::connect(notesStore, SIGNAL(noteUpdated(int,QJsonObject)), notesModel, SLOT(updateNote(int,QJsonObject)));
QObject::connect(notesStore, SIGNAL(noteDeleted(int)), notesModel, SLOT(removeNote(int)));
//QObject::connect(notesApi, SIGNAL(noteUpdated(Note)), notesModel, SLOT(insertNote(Note)));
//QObject::connect(notesApi, SIGNAL(noteDeleted(int)), notesModel, SLOT(removeNote(int)));
QQuickView* view = SailfishApp::createView();
#ifdef QT_DEBUG
@ -55,15 +57,6 @@ int main(int argc, char *argv[])
view->show();
int retval = app->exec();
//QObject::disconnect(notesApi, SIGNAL(noteDeleted(int)), notesModel, SLOT(removeNote(int)));
//QObject::disconnect(notesApi, SIGNAL(noteUpdated(Note)), notesModel, SLOT(insertNote(Note)));
QObject::disconnect(notesStore, SIGNAL(noteDeleted(int)), notesModel, SLOT(removeNote(int)));
QObject::disconnect(notesStore, SIGNAL(noteUpdated(int, QJsonObject)), notesModel, SLOT(insertNote(int, QJsonObject)));
//QObject::disconnect(notesStore, SIGNAL(noteDeleted(int)), notesApi, 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(int, QJsonObject)), notesStore, SLOT(updateNote(int, QJsonObject)));
notesApi->deleteLater();
notesStore->deleteLater();

View file

@ -7,7 +7,7 @@
#include <QDir>
NotesApi::NotesApi(const QString statusEndpoint, const QString loginEndpoint, const QString ocsEndpoint, const QString notesEndpoint, QObject *parent)
: m_statusEndpoint(statusEndpoint), m_loginEndpoint(loginEndpoint), m_ocsEndpoint(ocsEndpoint), m_notesEndpoint(notesEndpoint)
: QObject(parent), m_statusEndpoint(statusEndpoint), m_loginEndpoint(loginEndpoint), m_ocsEndpoint(ocsEndpoint), m_notesEndpoint(notesEndpoint)
{
// TODO verify connections (also in destructor)
m_loginPollTimer.setInterval(POLL_INTERVALL);
@ -42,7 +42,7 @@ NotesApi::~NotesApi() {
disconnect(&m_manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(sslError(QNetworkReply*,QList<QSslError>)));
}
void NotesApi::getAllNotes(const QStringList& exclude) {
bool NotesApi::getAllNotes(const QStringList& exclude) {
qDebug() << "Getting all notes";
QUrl url = apiEndpointUrl(m_notesEndpoint);
@ -54,10 +54,12 @@ void NotesApi::getAllNotes(const QStringList& exclude) {
m_authenticatedRequest.setUrl(url);
m_getAllNotesReplies << m_manager.get(m_authenticatedRequest);
emit busyChanged(true);
return true;
}
return false;
}
void NotesApi::getNote(const int id, const QStringList& exclude) {
bool NotesApi::getNote(const int id, const QStringList& exclude) {
qDebug() << "Getting note: " << id;
QUrl url = apiEndpointUrl(m_notesEndpoint + QString("/%1").arg(id));
if (!exclude.isEmpty())
@ -68,10 +70,12 @@ void NotesApi::getNote(const int id, const QStringList& exclude) {
m_authenticatedRequest.setUrl(url);
m_getNoteReplies << m_manager.get(m_authenticatedRequest);
emit busyChanged(true);
return true;
}
return false;
}
void NotesApi::createNote(const QJsonObject& note) {
bool NotesApi::createNote(const QJsonObject& note) {
qDebug() << "Creating note";
QUrl url = apiEndpointUrl(m_notesEndpoint);
if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
@ -79,10 +83,12 @@ void NotesApi::createNote(const QJsonObject& note) {
m_authenticatedRequest.setUrl(url);
m_createNoteReplies << m_manager.post(m_authenticatedRequest, QJsonDocument(note).toJson());
emit busyChanged(true);
return true;
}
return false;
}
void NotesApi::updateNote(const int id, const QJsonObject& note) {
bool NotesApi::updateNote(const int id, const QJsonObject& note) {
qDebug() << "Updating note: " << id;
QUrl url = apiEndpointUrl(m_notesEndpoint + QString("/%1").arg(id));
if (id >= 0 && url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
@ -90,10 +96,12 @@ void NotesApi::updateNote(const int id, const QJsonObject& note) {
m_authenticatedRequest.setUrl(url);
m_updateNoteReplies << m_manager.put(m_authenticatedRequest, QJsonDocument(note).toJson());
emit busyChanged(true);
return true;
}
return false;
}
void NotesApi::deleteNote(const int id) {
bool 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()) {
@ -101,7 +109,9 @@ void NotesApi::deleteNote(const int id) {
m_authenticatedRequest.setUrl(url);
m_deleteNoteReplies << m_manager.deleteResource(m_authenticatedRequest);
emit busyChanged(true);
return true;
}
return false;
}
bool NotesApi::busy() const {
@ -356,8 +366,9 @@ void NotesApi::replyFinished(QNetworkReply *reply) {
if (m_getAllNotesReplies.contains(reply)) {
qDebug() << "Get all notes reply";
if (reply->error() == QNetworkReply::NoError && json.isArray())
if (reply->error() == QNetworkReply::NoError && json.isArray()) {
updateApiNotes(json.array());
}
m_getAllNotesReplies.removeOne(reply);
}
else if (m_getNoteReplies.contains(reply)) {
@ -369,7 +380,7 @@ void NotesApi::replyFinished(QNetworkReply *reply) {
else if (m_createNoteReplies.contains(reply)) {
qDebug() << "Create note reply";
if (reply->error() == QNetworkReply::NoError && json.isObject())
updateApiNote(json.object());
createApiNote(json.object());
m_createNoteReplies.removeOne(reply);
}
else if (m_updateNoteReplies.contains(reply)) {
@ -576,10 +587,17 @@ void NotesApi::setLoginStatus(LoginStatus status, bool *changed) {
}
void NotesApi::updateApiNotes(const QJsonArray &json) {
QList<int> ids;
for (int i = 0; i < json.size(); ++i) {
if (json[i].isObject())
updateApiNote(json[i].toObject());
if (json[i].isObject()) {
QJsonObject object = json[i].toObject();
if (!object.isEmpty()) {
updateApiNote(json[i].toObject());
ids << object.value("id").toInt(-1);
}
}
}
emit allNotesReceived(ids);
}
void NotesApi::updateApiNote(const QJsonObject &json) {
@ -587,3 +605,9 @@ void NotesApi::updateApiNote(const QJsonObject &json) {
if (id >= 0)
emit noteUpdated(id, json);
}
void NotesApi::createApiNote(const QJsonObject &json) {
int id = json["id"].toInt(-1);
if (id >= 0)
emit noteCreated(id, json);
}

View file

@ -21,9 +21,8 @@ class NotesApi : public QObject
{
Q_OBJECT
Q_PROPERTY(bool verifySsl READ verifySsl() WRITE setVerifySsl NOTIFY verifySslChanged)
Q_PROPERTY(bool verifySsl READ verifySsl WRITE setVerifySsl NOTIFY verifySslChanged)
Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
Q_PROPERTY(bool urlValid READ urlValid NOTIFY urlValidChanged)
Q_PROPERTY(QString server READ server WRITE setServer NOTIFY serverChanged)
Q_PROPERTY(QString scheme READ scheme WRITE setScheme NOTIFY schemeChanged)
Q_PROPERTY(QString host READ host WRITE setHost NOTIFY hostChanged)
@ -31,6 +30,8 @@ class NotesApi : public QObject
Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged)
Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
Q_PROPERTY(bool urlValid READ urlValid NOTIFY urlValidChanged)
Q_PROPERTY(bool networkAccessible READ networkAccessible NOTIFY networkAccessibleChanged)
Q_PROPERTY(QDateTime lastSync READ lastSync NOTIFY lastSyncChanged)
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
@ -139,15 +140,11 @@ public:
Q_INVOKABLE const QString errorMessage(int error) const;
public slots:
Q_INVOKABLE void getAllNotes(const QStringList& exclude = QStringList());
//void getAllNotes(Note::NoteField exclude);
Q_INVOKABLE void getNote(const int id, const QStringList& exclude = QStringList());
//void getNote(const int id, Note::NoteField exclude);
Q_INVOKABLE void createNote(const QJsonObject& note);
//void createNote(const Note& note);
Q_INVOKABLE void updateNote(const int id, const QJsonObject& note);
//void updateNote(const Note& note);
Q_INVOKABLE void deleteNote(const int id);
Q_INVOKABLE bool getAllNotes(const QStringList& exclude = QStringList());
Q_INVOKABLE bool getNote(const int id, const QStringList& exclude = QStringList());
Q_INVOKABLE bool createNote(const QJsonObject& note);
Q_INVOKABLE bool updateNote(const int id, const QJsonObject& note);
Q_INVOKABLE bool deleteNote(const int id);
signals:
void verifySslChanged(bool verify);
@ -178,10 +175,11 @@ signals:
void loginStatusChanged(LoginStatus status);
void loginUrlChanged(QUrl url);
void allNotesReceived(const QList<int>& ids);
void noteCreated(const int id, const QJsonObject& note);
void noteUpdated(const int id, const QJsonObject& note);
void noteDeleted(const int id);
void noteError(ErrorCodes error);
void noteError(const ErrorCodes error);
public slots:
@ -242,6 +240,7 @@ private:
QVector<QNetworkReply*> m_deleteNoteReplies;
void updateApiNotes(const QJsonArray& json);
void updateApiNote(const QJsonObject& json);
void createApiNote(const QJsonObject& json);
QDateTime m_lastSync;
};

View file

@ -91,22 +91,28 @@ const QJsonObject NotesModel::getNote(const int id, const QStringList &exclude)
return m_notes.value(id);
}
void NotesModel::createNote(const int id, const QJsonObject &note) {
qDebug() << "New note, adding it";
if (!m_notes.contains(id)) {
beginInsertRows(QModelIndex(), indexOfNoteById(id), indexOfNoteById(id));
m_notes.insert(id, note);
emit noteUpdated(id, note);
endInsertRows();
void NotesModel::insertNote(const int id, const QJsonObject& note) {
qDebug() << "Inserting note: " << id;
if (m_notes.contains(id)) {
qDebug() << "Note already present";
updateNote(id, note);
}
else {
qDebug() << "Note already present";
beginInsertRows(QModelIndex(), indexOfNoteById(id), indexOfNoteById(id));
m_notes.insert(id, note);
endInsertRows();
emit noteInserted(id, note);
qDebug() << "Note inserted";
}
}
void NotesModel::updateNote(const int id, const QJsonObject& note) {
void NotesModel::updateNote(const int id, const QJsonObject &note) {
qDebug() << "Updating note: " << id;
if (m_notes.contains(id)) {
if (!m_notes.contains(id)) {
qDebug() << "Note is new";
insertNote(id, note);
}
else {
if (m_notes.value(id) == note) {
qDebug() << "Note unchanged";
}
@ -117,12 +123,9 @@ void NotesModel::updateNote(const int id, const QJsonObject& note) {
qDebug() << "Note changed";
}
}
else {
qDebug() << "Note not found";
}
}
void NotesModel::deleteNote(const int id) {
void NotesModel::removeNote(const int id) {
qDebug() << "Removing note: " << id;
if (m_notes.contains(id)) {
beginRemoveRows(QModelIndex(), indexOfNoteById(id), indexOfNoteById(id));
@ -130,7 +133,7 @@ void NotesModel::deleteNote(const int id) {
qDebug() << "Note not found";
}
else {
emit noteDeleted(id);
emit noteRemoved(id);
}
endRemoveRows();
}

View file

@ -66,18 +66,18 @@ public:
public slots:
Q_INVOKABLE const QJsonArray getAllNotes(const QStringList& exclude = QStringList());
Q_INVOKABLE const QJsonObject getNote(const int id, const QStringList& exclude = QStringList());
Q_INVOKABLE void createNote(const int id, const QJsonObject& note);
Q_INVOKABLE void insertNote(const int id, const QJsonObject& note);
Q_INVOKABLE void updateNote(const int id, const QJsonObject& note);
Q_INVOKABLE void deleteNote(const int id);
Q_INVOKABLE void removeNote(const int id);
Q_INVOKABLE void clear();
Q_INVOKABLE int indexOfNoteById(int id) const;
signals:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ());
void noteCreated(const int id, const QJsonObject& note);
void noteInserted(const int id, const QJsonObject& note);
void noteUpdated(const int id, const QJsonObject& note);
void noteDeleted(const int id);
void noteRemoved(const int id);
private:
QMap<int, QJsonObject> m_notes;

View file

@ -6,7 +6,7 @@
const QString NotesStore::m_suffix = "json";
NotesStore::NotesStore(QString directory, QObject *parent)
NotesStore::NotesStore(QString directory, QObject *parent) : QObject(parent)
{
m_dir.setCurrent(directory);
m_dir.setPath("");
@ -101,15 +101,15 @@ const QJsonObject NotesStore::getNote(const int id, const QStringList& exclude)
QJsonObject note;
if (id >= 0) {
note = readNoteFile(id, exclude);
emit noteUpdated(id, note);
}
else {
qDebug() << "Skipping, invalid ID";
}
return note;
}
bool NotesStore::createNote(const QJsonObject& note) {
int id = note.value("id").toInt(-1);
/*
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
@ -126,8 +126,8 @@ bool NotesStore::createNote(const QJsonObject& note) {
}
return false;
}
const QJsonObject NotesStore::updateNote(const int id, const QJsonObject& note) {
*/
bool NotesStore::updateNote(const int id, const QJsonObject& note) {
qDebug() << "Updating note: " << id;
if (id >= 0) {
QJsonObject tmpNote = readNoteFile(id);
@ -142,6 +142,7 @@ const QJsonObject NotesStore::updateNote(const int id, const QJsonObject& note)
}
if (writeNoteFile(id, tmpNote)) {
emit noteUpdated(id, tmpNote);
return true;
}
}
else {
@ -155,13 +156,16 @@ const QJsonObject NotesStore::updateNote(const int id, const QJsonObject& note)
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 {

View file

@ -17,6 +17,8 @@ public:
QObject *parent = nullptr);
virtual ~NotesStore();
Q_PROPERTY(QString account READ account WRITE setAccount NOTIFY accountChanged)
QString account() const;
void setAccount(const QString& account);
@ -35,13 +37,13 @@ public:
public slots:
Q_INVOKABLE const QJsonArray getAllNotes(const QStringList& exclude = QStringList());
Q_INVOKABLE const QJsonObject getNote(const int id, const QStringList& exclude = QStringList());
Q_INVOKABLE bool createNote(const QJsonObject& note);
Q_INVOKABLE const QJsonObject updateNote(const int id, const QJsonObject& note);
//Q_INVOKABLE bool createNote(const int id, const QJsonObject& note);
Q_INVOKABLE bool updateNote(const int id, const QJsonObject& note);
Q_INVOKABLE bool deleteNote(const int id);
signals:
void accountChanged(const QString& account);
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 noteDeleted(const int id);
void noteError(const ErrorCodes error);

View file

@ -311,32 +311,32 @@
<context>
<name>NotesApi</name>
<message>
<location filename="../src/notesapi.cpp" line="303"/>
<location filename="../src/notesapi.cpp" line="313"/>
<source>No error</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="306"/>
<location filename="../src/notesapi.cpp" line="316"/>
<source>No network connection available</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="309"/>
<location filename="../src/notesapi.cpp" line="319"/>
<source>Failed to communicate with the Nextcloud server</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="312"/>
<location filename="../src/notesapi.cpp" line="322"/>
<source>An error occured while establishing an encrypted connection</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="315"/>
<location filename="../src/notesapi.cpp" line="325"/>
<source>Could not authenticate to the Nextcloud instance</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/notesapi.cpp" line="318"/>
<location filename="../src/notesapi.cpp" line="328"/>
<source>Unknown error</source>
<translation type="unfinished"></translation>
</message>
@ -878,27 +878,27 @@ You can also use other markdown syntax inside them.</source>
<context>
<name>harbour-nextcloudnotes</name>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="125"/>
<location filename="../qml/harbour-nextcloudnotes.qml" line="124"/>
<source>Notes</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="126"/>
<location filename="../qml/harbour-nextcloudnotes.qml" line="125"/>
<source>Offline</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="127"/>
<location filename="../qml/harbour-nextcloudnotes.qml" line="126"/>
<source>Synced</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="141"/>
<location filename="../qml/harbour-nextcloudnotes.qml" line="140"/>
<source>API error</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="134"/>
<location filename="../qml/harbour-nextcloudnotes.qml" line="133"/>
<source>File error</source>
<translation type="unfinished"></translation>
</message>