Implemented sync-time capturing and offline detection

This commit is contained in:
Scharel Clemens 2020-01-03 14:00:07 +01:00
parent 4341b2bd01
commit 52132195a5
7 changed files with 81 additions and 6 deletions

View file

@ -1,6 +1,7 @@
import QtQuick 2.0
import Sailfish.Silica 1.0
import Nemo.Configuration 1.0
import Nemo.Notifications 1.0
import harbour.nextcloudnotes.notesapi 1.0
import "pages"
@ -89,6 +90,15 @@ ApplicationWindow
}
}
Notification {
id: offlineNotification
expireTimeout: 0
appName: "Nextcloud " + qsTr("Notes")
summary: qsTr("Offline")
body: qsTr("Synced") + ": " + new Date(notesApi.lastSync).toLocaleString(Qt.locale())
Component.onDestruction: close(Notification.Expired)
}
Timer {
id: autoSyncTimer
interval: appSettings.autoSyncInterval * 1000
@ -120,6 +130,11 @@ ApplicationWindow
sslVerify: !account.doNotVerifySsl
dataFile: appSettings.currentAccount !== "" ? StandardPaths.data + "/" + appSettings.currentAccount + ".json" : ""
Component.onCompleted: getAllNotes()
onNetworkAccessibleChanged: {
console.log("Device is " + (networkAccessible ? "online" : "offline"))
networkAccessible ? offlineNotification.close(Notification.Closed) : offlineNotification.publish()
}
onLastSyncChanged: account.update = lastSync
}
initialPage: Component { NotesPage { } }

View file

@ -6,6 +6,7 @@
NotesApi::NotesApi(QObject *parent) : QObject(parent)
{
m_online = m_manager.networkAccessible() == QNetworkAccessManager::Accessible;
mp_model = new NotesModel(this);
mp_modelProxy = new NotesProxyModel(this);
mp_modelProxy->setSourceModel(mp_model);
@ -225,16 +226,21 @@ void NotesApi::verifyUrl(QUrl url) {
}
void NotesApi::onNetworkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible) {
qDebug() << m_manager.networkAccessible();
emit networkAccessibleChanged(accessible == QNetworkAccessManager::Accessible);
m_online = accessible == QNetworkAccessManager::Accessible;
//qDebug() << m_online;
emit networkAccessibleChanged(m_online);
}
void NotesApi::replyFinished(QNetworkReply *reply) {
if (reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
QJsonDocument json = QJsonDocument::fromJson(data);
if (mp_model)
mp_model->fromJsonDocument(json);
if (mp_model) {
if (mp_model->fromJsonDocument(json)) {
m_lastSync = QDateTime::currentDateTimeUtc();
emit lastSyncChanged(m_lastSync);
}
}
//qDebug() << data;
}
else {

View file

@ -56,7 +56,10 @@ public:
void setDataFile(QString dataFile);
Q_PROPERTY(bool networkAccessible READ networkAccessible NOTIFY networkAccessibleChanged)
bool networkAccessible() const { return m_manager.networkAccessible() == QNetworkAccessManager::Accessible; }
bool networkAccessible() const { return m_online; }
Q_PROPERTY(QDateTime lastSync READ lastSync NOTIFY lastSyncChanged)
QDateTime lastSync() const { return m_lastSync; }
Q_PROPERTY(bool ready READ ready NOTIFY readyChanged)
bool ready() const;
@ -83,6 +86,7 @@ signals:
void pathChanged(QString path);
void dataFileChanged(QString dataFile);
void networkAccessibleChanged(bool accessible);
void lastSyncChanged(QDateTime lastSync);
void readyChanged(bool ready);
void busyChanged(bool busy);
@ -97,6 +101,8 @@ private slots:
void saveToFile(QModelIndex,QModelIndex,QVector<int>);
private:
bool m_online;
QDateTime m_lastSync;
QUrl m_url;
QNetworkAccessManager m_manager;
QNetworkRequest m_request;

View file

@ -81,7 +81,7 @@ bool NotesModel::fromJsonDocument(const QJsonDocument &jdoc) {
}
else if (jdoc.isObject()) {
qDebug() << "- It's a single object...";
return insertNote(jdoc.object()) >= 0;
insertNote(jdoc.object()) >= 0;
}
else if (jdoc.isEmpty()) {
qDebug() << "- Empty JSON document.";

View file

@ -609,4 +609,19 @@ You can also use other markdown syntax inside them.</source>
<translation>Ich benutze diese Option auf eigene Gefahr</translation>
</message>
</context>
<context>
<name>harbour-nextcloudnotes</name>
<message>
<source>Offline</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Notes</source>
<translation type="unfinished">Notizen</translation>
</message>
<message>
<source>Synced</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

View file

@ -609,4 +609,19 @@ You can also use other markdown syntax inside them.</source>
<translation>Ditt användarnamn och lösenord överförs okrypterat över nätverket när du aktiverar det här alternativet.</translation>
</message>
</context>
<context>
<name>harbour-nextcloudnotes</name>
<message>
<source>Offline</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Notes</source>
<translation type="unfinished">Anteckningar</translation>
</message>
<message>
<source>Synced</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

View file

@ -749,4 +749,22 @@ You can also use other markdown syntax inside them.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>harbour-nextcloudnotes</name>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="96"/>
<source>Notes</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="97"/>
<source>Offline</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/harbour-nextcloudnotes.qml" line="98"/>
<source>Synced</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>