From 4464c35865cd6118caa4c30ec353f2433b51b006 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sat, 26 Oct 2024 12:49:25 +0100 Subject: [PATCH] Add isonline parameter to connection from NeoChat --- Quotient/connection.cpp | 8 ++++++++ Quotient/connection.h | 10 ++++++++++ Quotient/connection_p.h | 1 + 3 files changed, 19 insertions(+) diff --git a/Quotient/connection.cpp b/Quotient/connection.cpp index f205fadb8..cc2eee5cf 100644 --- a/Quotient/connection.cpp +++ b/Quotient/connection.cpp @@ -433,16 +433,22 @@ void Connection::sync(int timeout) connect(job, &SyncJob::success, this, [this, job] { onSyncSuccess(job->takeData()); d->syncJob = nullptr; + d->lastSyncSuccessful = true; + emit isOnlineChanged(); emit syncDone(); }); connect(job, &SyncJob::retryScheduled, this, [this, job](int retriesTaken, int nextInMilliseconds) { + d->lastSyncSuccessful = false; + emit isOnlineChanged(); emit networkError(job->errorString(), job->rawDataSample(), retriesTaken, nextInMilliseconds); }); connect(job, &SyncJob::failure, this, [this, job] { // SyncJob persists with retries on transient errors; if it fails, // there's likely something serious enough to stop the loop. + d->lastSyncSuccessful = false; + emit isOnlineChanged(); stopSync(); if (job->error() == BaseJob::Unauthorised) { qCWarning(SYNCJOB) @@ -1099,6 +1105,8 @@ QByteArray Connection::accessToken() const bool Connection::isLoggedIn() const { return !accessToken().isEmpty(); } +bool Connection::isOnline() const { return d->lastSyncSuccessful; } + QOlmAccount* Connection::olmAccount() const { return d->encryptionData ? &d->encryptionData->olmAccount : nullptr; diff --git a/Quotient/connection.h b/Quotient/connection.h index e644eecb6..f515aea63 100644 --- a/Quotient/connection.h +++ b/Quotient/connection.h @@ -120,6 +120,7 @@ class QUOTIENT_API Connection : public QObject { Q_PROPERTY(QString deviceId READ deviceId NOTIFY stateChanged) Q_PROPERTY(QByteArray accessToken READ accessToken NOTIFY stateChanged) Q_PROPERTY(bool isLoggedIn READ isLoggedIn NOTIFY stateChanged STORED false) + Q_PROPERTY(bool isOnline READ isOnline NOTIFY isOnlineChanged) Q_PROPERTY(QString defaultRoomVersion READ defaultRoomVersion NOTIFY capabilitiesLoaded) Q_PROPERTY(QUrl homeserver READ homeserver WRITE setHomeserver NOTIFY homeserverChanged) Q_PROPERTY(QVector loginFlows READ loginFlows NOTIFY loginFlowsChanged) @@ -332,6 +333,11 @@ class QUOTIENT_API Connection : public QObject { QString deviceId() const; QByteArray accessToken() const; bool isLoggedIn() const; + + //! \brief Whether the connection is successfully syncing with the server. + //! + //! \return true, if the last sync was successful, false otherwise. + bool isOnline() const; QOlmAccount* olmAccount() const; Database* database() const; @@ -787,6 +793,10 @@ public Q_SLOTS: //! accessToken - these properties normally only change at //! a successful login and logout and are constant at other times. void stateChanged(); + + //! The online state has changed. + void isOnlineChanged(); + void loginError(QString message, QString details); //! \brief A network request (job) started by callApi() has failed diff --git a/Quotient/connection_p.h b/Quotient/connection_p.h index 216bf305a..126775bc0 100644 --- a/Quotient/connection_p.h +++ b/Quotient/connection_p.h @@ -67,6 +67,7 @@ class Q_DECL_HIDDEN Quotient::Connection::Private { JobHandle loginFlowsJob = nullptr; SyncJob* syncJob = nullptr; + bool lastSyncSuccessful = true; JobHandle logoutJob = nullptr; bool cacheState = true;