diff --git a/src/gui/accountstate.cpp b/src/gui/accountstate.cpp index eacfec7181f9d..c3e8ef3b08bbb 100644 --- a/src/gui/accountstate.cpp +++ b/src/gui/accountstate.cpp @@ -21,6 +21,7 @@ #include "logger.h" #include "configfile.h" #include "ocsnavigationappsjob.h" +#include "ocsuserstatusconnector.h" #include "pushnotifications.h" #include @@ -58,6 +59,8 @@ AccountState::AccountState(AccountPtr account) this, &AccountState::slotCredentialsAsked); connect(account.data(), &Account::pushNotificationsReady, this, &AccountState::slotPushNotificationsReady); + connect(account.data(), &Account::serverUserStatusChanged, this, + &AccountState::slotServerUserStatusChanged); connect(this, &AccountState::isConnectedChanged, [=]{ // Get the Apps available on the server if we're now connected. @@ -558,6 +561,11 @@ void AccountState::slotPushNotificationsReady() } } +void AccountState::slotServerUserStatusChanged() +{ + setDesktopNotificationsAllowed(_account->userStatusConnector()->userStatus().state() != UserStatus::OnlineStatus::DoNotDisturb); +} + void AccountState::slotNavigationAppsFetched(const QJsonDocument &reply, int statusCode) { if(_account){ diff --git a/src/gui/accountstate.h b/src/gui/accountstate.h index 0a489c14975b6..2e47fcf6e13aa 100644 --- a/src/gui/accountstate.h +++ b/src/gui/accountstate.h @@ -217,6 +217,7 @@ private Q_SLOTS: void slotCheckConnection(); void slotPushNotificationsReady(); + void slotServerUserStatusChanged(); private: AccountPtr _account; diff --git a/src/gui/tray/notificationhandler.cpp b/src/gui/tray/notificationhandler.cpp index d21f0d7c62a9d..3be035a101136 100644 --- a/src/gui/tray/notificationhandler.cpp +++ b/src/gui/tray/notificationhandler.cpp @@ -47,8 +47,6 @@ void ServerNotificationHandler::slotFetchNotifications() this, &ServerNotificationHandler::slotNotificationsReceived); QObject::connect(_notificationJob.data(), &JsonApiJob::etagResponseHeaderReceived, this, &ServerNotificationHandler::slotEtagResponseHeaderReceived); - QObject::connect(_notificationJob.data(), &JsonApiJob::allowDesktopNotificationsChanged, - this, &ServerNotificationHandler::slotAllowDesktopNotificationsChanged); _notificationJob->setProperty(propertyAccountStateC, QVariant::fromValue(_accountState)); _notificationJob->addRawHeader("If-None-Match", _accountState->notificationsEtagResponseHeader()); _notificationJob->start(); @@ -63,14 +61,6 @@ void ServerNotificationHandler::slotEtagResponseHeaderReceived(const QByteArray } } -void ServerNotificationHandler::slotAllowDesktopNotificationsChanged(bool isAllowed) -{ - auto *account = qvariant_cast(sender()->property(propertyAccountStateC)); - if (account != nullptr) { - account->setDesktopNotificationsAllowed(isAllowed); - } -} - void ServerNotificationHandler::slotNotificationsReceived(const QJsonDocument &json, int statusCode) { if (statusCode != successStatusCode && statusCode != notModifiedStatusCode) { diff --git a/src/gui/tray/notificationhandler.h b/src/gui/tray/notificationhandler.h index fc039137c5ff2..4100df7d156fe 100644 --- a/src/gui/tray/notificationhandler.h +++ b/src/gui/tray/notificationhandler.h @@ -25,7 +25,6 @@ public slots: private slots: void slotNotificationsReceived(const QJsonDocument &json, int statusCode); void slotEtagResponseHeaderReceived(const QByteArray &value, int statusCode); - void slotAllowDesktopNotificationsChanged(bool isAllowed); private: QPointer _notificationJob; diff --git a/src/gui/userstatusselectormodel.cpp b/src/gui/userstatusselectormodel.cpp index fea9fb61c18ff..da0b44c1523c6 100644 --- a/src/gui/userstatusselectormodel.cpp +++ b/src/gui/userstatusselectormodel.cpp @@ -182,6 +182,7 @@ void UserStatusSelectorModel::setOnlineStatus(UserStatus::OnlineStatus status) } _userStatus.setState(status); + _userStatusConnector->setUserStatus(_userStatus); emit onlineStatusChanged(); } diff --git a/src/libsync/account.cpp b/src/libsync/account.cpp index bdc08c9ba5f65..eab683dd63830 100644 --- a/src/libsync/account.cpp +++ b/src/libsync/account.cpp @@ -635,9 +635,12 @@ void Account::setupUserStatusConnector() connect(_userStatusConnector.get(), &UserStatusConnector::userStatusFetched, this, [this](const UserStatus &) { emit userStatusChanged(); }); + connect(_userStatusConnector.get(), &UserStatusConnector::serverUserStatusChanged, this, &Account::serverUserStatusChanged); connect(_userStatusConnector.get(), &UserStatusConnector::messageCleared, this, [this] { emit userStatusChanged(); }); + + _userStatusConnector->fetchUserStatus(); } QString Account::serverVersion() const diff --git a/src/libsync/account.h b/src/libsync/account.h index d211d77d014a3..876fa94b9bc0a 100644 --- a/src/libsync/account.h +++ b/src/libsync/account.h @@ -322,6 +322,8 @@ public slots: void userStatusChanged(); + void serverUserStatusChanged(); + void capabilitiesChanged(); void lockFileSuccess(); diff --git a/src/libsync/networkjobs.cpp b/src/libsync/networkjobs.cpp index 82b5e5f4bf764..335dd1eb548c2 100644 --- a/src/libsync/networkjobs.cpp +++ b/src/libsync/networkjobs.cpp @@ -915,11 +915,6 @@ bool JsonApiJob::finished() if(reply()->rawHeaderList().contains("ETag")) emit etagResponseHeaderReceived(reply()->rawHeader("ETag"), statusCode); - const auto desktopNotificationsAllowed = reply()->rawHeader(QByteArray("X-Nextcloud-User-Status")); - if(!desktopNotificationsAllowed.isEmpty()) { - emit allowDesktopNotificationsChanged(desktopNotificationsAllowed == "online"); - } - QJsonParseError error; auto json = QJsonDocument::fromJson(jsonStr.toUtf8(), &error); // empty or invalid response and status code is != 304 because jsonStr is expected to be empty diff --git a/src/libsync/networkjobs.h b/src/libsync/networkjobs.h index b2c0ad87662d9..c169e056ec7de 100644 --- a/src/libsync/networkjobs.h +++ b/src/libsync/networkjobs.h @@ -449,12 +449,6 @@ public slots: */ void etagResponseHeaderReceived(const QByteArray &value, int statusCode); - /** - * @brief desktopNotificationStatusReceived - signal to report if notifications are allowed - * @param status - set desktop notifications allowed status - */ - void allowDesktopNotificationsChanged(bool isAllowed); - private: QByteArray _body; QUrlQuery _additionalParams; diff --git a/src/libsync/ocsuserstatusconnector.cpp b/src/libsync/ocsuserstatusconnector.cpp index 19cb34b3dbca0..cbe65c320a2e3 100644 --- a/src/libsync/ocsuserstatusconnector.cpp +++ b/src/libsync/ocsuserstatusconnector.cpp @@ -256,8 +256,14 @@ void OcsUserStatusConnector::onUserStatusFetched(const QJsonDocument &json, int return; } + const auto oldOnlineState = _userStatus.state(); _userStatus = jsonToUserStatus(json); + emit userStatusFetched(_userStatus); + + if (oldOnlineState != _userStatus.state()) { + emit serverUserStatusChanged(); + } } void OcsUserStatusConnector::startFetchPredefinedStatuses() @@ -396,7 +402,9 @@ void OcsUserStatusConnector::setUserStatus(const UserStatus &userStatus) return; } - setUserStatusOnlineStatus(userStatus.state()); + if (userStatus.state() != _userStatus.state()) { + setUserStatusOnlineStatus(userStatus.state()); + } setUserStatusMessage(userStatus); } @@ -408,6 +416,15 @@ void OcsUserStatusConnector::onUserStatusOnlineStatusSet(const QJsonDocument &js emit error(Error::CouldNotSetUserStatus); return; } + + const auto oldOnlineState = _userStatus.state(); + _userStatus.setState(jsonToUserStatus(json).state()); + + emit userStatusSet(); + + if (oldOnlineState != _userStatus.state()) { + emit serverUserStatusChanged(); + } } void OcsUserStatusConnector::onUserStatusMessageSet(const QJsonDocument &json, int statusCode) @@ -449,7 +466,10 @@ void OcsUserStatusConnector::onMessageCleared(const QJsonDocument &json, int sta return; } + const auto onlineState = _userStatus.state(); + _userStatus = {}; + _userStatus.setState(onlineState); emit messageCleared(); } } diff --git a/src/libsync/userstatusconnector.h b/src/libsync/userstatusconnector.h index d5593fe9e50cf..08de548eb1038 100644 --- a/src/libsync/userstatusconnector.h +++ b/src/libsync/userstatusconnector.h @@ -129,6 +129,7 @@ class OWNCLOUDSYNC_EXPORT UserStatusConnector : public QObject void userStatusFetched(const UserStatus &userStatus); void predefinedStatusesFetched(const std::vector &statuses); void userStatusSet(); + void serverUserStatusChanged(); void messageCleared(); void error(Error error); };