diff --git a/src/gui/tray/syncstatussummary.cpp b/src/gui/tray/syncstatussummary.cpp index 5a2074dd3..5c142cb90 100644 --- a/src/gui/tray/syncstatussummary.cpp +++ b/src/gui/tray/syncstatussummary.cpp @@ -13,6 +13,7 @@ */ #include "syncstatussummary.h" +#include "accountfwd.h" #include "folderman.h" #include "navigationpanehelper.h" #include "networkjobs.h" @@ -51,41 +52,24 @@ SyncStatusSummary::SyncStatusSummary(QObject *parent) connect(folderMan, &FolderMan::folderSyncStateChange, this, &SyncStatusSummary::onFolderSyncStateChanged); } +bool SyncStatusSummary::reloadNeeded(AccountState *accountState) const +{ + if (_accountState.data() == accountState) { + return false; + } + return true; +} + void SyncStatusSummary::load() { const auto currentUser = UserModel::instance()->currentUser(); if (!currentUser) { return; } - auto accountState = currentUser->accountState(); - - if (_accountState.data() == accountState.data()) { - return; - } - - _accountState = accountState; + setAccountState(currentUser->accountState()); clearFolderErrors(); connectToFoldersProgress(FolderMan::instance()->map()); - auto syncStateFallbackNeeded = true; - for (const auto &folder : FolderMan::instance()->map()) { - if (_accountState.data() != folder->accountState()) { - continue; - } - onFolderSyncStateChanged(folder); - syncStateFallbackNeeded = false; - } - - if (syncStateFallbackNeeded) { - setSyncing(false); - setSyncStatusDetailString(""); - if (_accountState && !_accountState->isConnected()) { - setSyncStatusString(tr("Offline")); - setSyncIcon(Theme::instance()->folderOffline()); - } else { - setSyncStatusString(tr("All synced!")); - setSyncIcon(Theme::instance()->syncStatusOk()); - } - } + initSyncState(); } double SyncStatusSummary::syncProgress() const @@ -315,4 +299,48 @@ void SyncStatusSummary::connectToFoldersProgress(const Folder::Map &folderMap) } } } + +void SyncStatusSummary::onIsConnectedChanged() +{ + setSyncStateToConnectedState(); +} + +void SyncStatusSummary::setSyncStateToConnectedState() +{ + setSyncing(false); + setSyncStatusDetailString(""); + if (_accountState && !_accountState->isConnected()) { + setSyncStatusString(tr("Offline")); + setSyncIcon(Theme::instance()->folderOffline()); + } else { + setSyncStatusString(tr("All synced!")); + setSyncIcon(Theme::instance()->syncStatusOk()); + } +} + +void SyncStatusSummary::setAccountState(AccountStatePtr accountState) +{ + if (!reloadNeeded(accountState.data())) { + return; + } + if (_accountState) { + disconnect( + _accountState.data(), &AccountState::isConnectedChanged, this, &SyncStatusSummary::onIsConnectedChanged); + } + _accountState = accountState; + connect(_accountState.data(), &AccountState::isConnectedChanged, this, &SyncStatusSummary::onIsConnectedChanged); +} + +void SyncStatusSummary::initSyncState() +{ + auto syncStateFallbackNeeded = true; + for (const auto &folder : FolderMan::instance()->map()) { + onFolderSyncStateChanged(folder); + syncStateFallbackNeeded = false; + } + + if (syncStateFallbackNeeded) { + setSyncStateToConnectedState(); + } +} } diff --git a/src/gui/tray/syncstatussummary.h b/src/gui/tray/syncstatussummary.h index f4abec198..b4d1bb91b 100644 --- a/src/gui/tray/syncstatussummary.h +++ b/src/gui/tray/syncstatussummary.h @@ -14,6 +14,8 @@ #pragma once +#include "account.h" +#include "accountfwd.h" #include "accountstate.h" #include "folderman.h" @@ -59,6 +61,7 @@ private: void onFolderListChanged(const OCC::Folder::Map &folderMap); void onFolderProgressInfo(const ProgressInfo &progress); void onFolderSyncStateChanged(const Folder *folder); + void onIsConnectedChanged(); void setSyncStateForFolder(const Folder *folder); void markFolderAsError(const Folder *folder); @@ -66,12 +69,16 @@ private: bool folderErrors() const; bool folderError(const Folder *folder) const; void clearFolderErrors(); + void setSyncStateToConnectedState(); + bool reloadNeeded(AccountState *accountState) const; + void initSyncState(); void setSyncProgress(double value); void setSyncing(bool value); void setSyncStatusString(const QString &value); void setSyncStatusDetailString(const QString &value); void setSyncIcon(const QUrl &value); + void setAccountState(AccountStatePtr accountState); AccountStatePtr _accountState; std::set _foldersWithErrors;