diff --git a/qml/harbour-nextcloudnotes.qml b/qml/harbour-nextcloudnotes.qml index cf974f8..dc6176a 100644 --- a/qml/harbour-nextcloudnotes.qml +++ b/qml/harbour-nextcloudnotes.qml @@ -95,7 +95,14 @@ ApplicationWindow expireTimeout: 0 appName: "Nextcloud " + qsTr("Notes") summary: qsTr("Offline") - body: qsTr("Synced") + ": " + new Date(notesApi.lastSync).toLocaleString(Qt.locale()) + body: qsTr("Synced") + ": " + new Date(account.update).toLocaleString(Qt.locale()) + Component.onDestruction: close(Notification.Expired) + } + + Notification { + id: errorNotification + appName: offlineNotification.appName + summary: qsTr("Error") Component.onDestruction: close(Notification.Expired) } @@ -134,6 +141,14 @@ ApplicationWindow console.log("Device is " + (networkAccessible ? "online" : "offline")) networkAccessible ? offlineNotification.close(Notification.Closed) : offlineNotification.publish() } + onError: { + console.log("Error (" + error + "): " + errorMessage(error)) + errorNotification.close() + if (error && networkAccessible) { + errorNotification.body = errorMessage(error) + errorNotification.publish() + } + } onLastSyncChanged: account.update = lastSync } diff --git a/src/notesapi.cpp b/src/notesapi.cpp index de2b0b1..ea08632 100644 --- a/src/notesapi.cpp +++ b/src/notesapi.cpp @@ -45,13 +45,6 @@ void NotesApi::setSslVerify(bool verify) { } } -void NotesApi::requireAuthentication(QNetworkReply *reply, QAuthenticator *authenticator) { - if (reply && authenticator) { - authenticator->setUser(username()); - authenticator->setPassword(password()); - } -} - void NotesApi::setUrl(QUrl url) { if (url != m_url) { QUrl oldUrl = m_url; @@ -223,6 +216,15 @@ void NotesApi::verifyUrl(QUrl url) { emit urlValidChanged(url.isValid()); } +void NotesApi::requireAuthentication(QNetworkReply *reply, QAuthenticator *authenticator) { + if (reply && authenticator) { + authenticator->setUser(username()); + authenticator->setPassword(password()); + } + else + emit error(AuthenticationError); +} + void NotesApi::onNetworkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible) { m_online = accessible == QNetworkAccessManager::Accessible; //qDebug() << m_online; @@ -230,7 +232,9 @@ void NotesApi::onNetworkAccessibleChanged(QNetworkAccessManager::NetworkAccessib } void NotesApi::replyFinished(QNetworkReply *reply) { + qDebug() << reply->error() << reply->errorString(); if (reply->error() == QNetworkReply::NoError) { + emit error(NoError); QByteArray data = reply->readAll(); QJsonDocument json = QJsonDocument::fromJson(data); if (mp_model) { @@ -241,9 +245,10 @@ void NotesApi::replyFinished(QNetworkReply *reply) { } //qDebug() << data; } - else { - qDebug() << reply->error() << reply->errorString(); - } + else if (reply->error() == QNetworkReply::AuthenticationRequiredError) + emit error(AuthenticationError); + else + emit error(CommunicationError); m_replies.removeAll(reply); reply->deleteLater(); emit busyChanged(busy()); @@ -254,12 +259,47 @@ void NotesApi::sslError(QNetworkReply *reply, const QList &errors) { for (int i = 0; i < errors.size(); ++i) { qDebug() << errors[i].errorString(); } + emit error(SslHandshakeError); } void NotesApi::saveToFile(QModelIndex, QModelIndex, QVector) { if (m_jsonFile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) { qDebug() << "Writing data to file" << m_jsonFile.fileName(); - m_jsonFile.write(mp_model->toJsonDocument().toJson()); + QByteArray data = mp_model->toJsonDocument().toJson(); + if (m_jsonFile.write(data) < data.size()) + emit error(LocalFileWriteError); m_jsonFile.close(); } + else + emit error(LocalFileWriteError); +} + +const QString NotesApi::errorMessage(int error) const { + QString message; + switch (error) { + case NoError: + break; + case NoConnectionError: + message = tr("No network connection available"); + break; + case CommunicationError: + message = tr("Failed to communicate with the Nextcloud server"); + break; + case LocalFileReadError: + message = tr("An error happened while reading from the local storage"); + break; + case LocalFileWriteError: + message = tr("An error happened while writing to the local storage"); + break; + case SslHandshakeError: + message = tr("An error occured while establishing an encrypted connection"); + break; + case AuthenticationError: + message = tr("Could not authenticate to the Nextcloud instance"); + break; + default: + message = tr("Unknown error"); + break; + } + return message; } diff --git a/src/notesapi.h b/src/notesapi.h index 49445d5..9e144e5 100644 --- a/src/notesapi.h +++ b/src/notesapi.h @@ -74,6 +74,17 @@ public: Q_INVOKABLE void deleteNote(double noteId); Q_INVOKABLE NotesProxyModel* model() const { return mp_modelProxy; } + enum ErrorCodes { + NoError, + NoConnectionError, + CommunicationError, + LocalFileReadError, + LocalFileWriteError, + SslHandshakeError, + AuthenticationError + }; + Q_INVOKABLE const QString errorMessage(int error) const; + signals: void sslVerifyChanged(bool verify); void urlChanged(QUrl url); @@ -89,6 +100,7 @@ signals: void lastSyncChanged(QDateTime lastSync); void readyChanged(bool ready); void busyChanged(bool busy); + void error(int error); public slots: diff --git a/src/notesmodel.cpp b/src/notesmodel.cpp index 609aaf2..131f396 100644 --- a/src/notesmodel.cpp +++ b/src/notesmodel.cpp @@ -81,7 +81,7 @@ bool NotesModel::fromJsonDocument(const QJsonDocument &jdoc) { } else if (jdoc.isObject()) { qDebug() << "- It's a single object..."; - insertNote(jdoc.object()) >= 0; + insertNote(jdoc.object()); } else if (jdoc.isEmpty()) { qDebug() << "- Empty JSON document."; diff --git a/translations/harbour-nextcloudnotes-de.ts b/translations/harbour-nextcloudnotes-de.ts index 8c925e7..693ef16 100644 --- a/translations/harbour-nextcloudnotes-de.ts +++ b/translations/harbour-nextcloudnotes-de.ts @@ -213,6 +213,37 @@ GeƤndert + + NotesApi + + No network connection available + + + + Failed to communicate with the Nextcloud server + + + + An error happened while reading from the local storage + + + + An error happened while writing to the local storage + + + + An error occured while establishing an encrypted connection + + + + Could not authenticate to the Nextcloud instance + + + + Unknown error + + + NotesPage @@ -623,5 +654,9 @@ You can also use other markdown syntax inside them. Synced + + Error + + diff --git a/translations/harbour-nextcloudnotes-sv.ts b/translations/harbour-nextcloudnotes-sv.ts index d17e432..f9ffd9e 100644 --- a/translations/harbour-nextcloudnotes-sv.ts +++ b/translations/harbour-nextcloudnotes-sv.ts @@ -213,6 +213,37 @@ Ingen kategori + + NotesApi + + No network connection available + + + + Failed to communicate with the Nextcloud server + + + + An error happened while reading from the local storage + + + + An error happened while writing to the local storage + + + + An error occured while establishing an encrypted connection + + + + Could not authenticate to the Nextcloud instance + + + + Unknown error + + + NotesPage @@ -623,5 +654,9 @@ You can also use other markdown syntax inside them. Synced + + Error + + diff --git a/translations/harbour-nextcloudnotes.ts b/translations/harbour-nextcloudnotes.ts index 45f6a05..f71bafe 100644 --- a/translations/harbour-nextcloudnotes.ts +++ b/translations/harbour-nextcloudnotes.ts @@ -259,6 +259,44 @@ + + NotesApi + + + No network connection available + + + + + Failed to communicate with the Nextcloud server + + + + + An error happened while reading from the local storage + + + + + An error happened while writing to the local storage + + + + + An error occured while establishing an encrypted connection + + + + + Could not authenticate to the Nextcloud instance + + + + + Unknown error + + + NotesPage @@ -766,5 +804,10 @@ You can also use other markdown syntax inside them. Synced + + + Error + +