From b2d76abf88aa3cd3cf43e144c1ccbb0a9b850498 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Sat, 11 Mar 2023 01:25:05 +0100 Subject: [PATCH] Use push notifications to notify file provider of changes Signed-off-by: Claudio Cambra --- src/gui/macOS/fileproviderdomainmanager.h | 5 ++ .../macOS/fileproviderdomainmanager_mac.mm | 63 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/gui/macOS/fileproviderdomainmanager.h b/src/gui/macOS/fileproviderdomainmanager.h index 7e4fff181..7f2ddd460 100644 --- a/src/gui/macOS/fileproviderdomainmanager.h +++ b/src/gui/macOS/fileproviderdomainmanager.h @@ -18,6 +18,7 @@ namespace OCC { +class Account; class AccountState; namespace Mac { @@ -36,6 +37,10 @@ private slots: void addFileProviderDomainForAccount(OCC::AccountState *accountState); void removeFileProviderDomainForAccount(OCC::AccountState *accountState); + void trySetupPushNotificationsForAccount(Account *account); + void setupPushNotificationsForAccount(OCC::Account *account); + void signalEnumeratorChanged(OCC::Account *account); + private: explicit FileProviderDomainManager(QObject *parent = nullptr); static FileProviderDomainManager *_instance; diff --git a/src/gui/macOS/fileproviderdomainmanager_mac.mm b/src/gui/macOS/fileproviderdomainmanager_mac.mm index 6993c0186..f87f466af 100644 --- a/src/gui/macOS/fileproviderdomainmanager_mac.mm +++ b/src/gui/macOS/fileproviderdomainmanager_mac.mm @@ -13,6 +13,7 @@ */ #include "fileproviderdomainmanager.h" +#include "pushnotifications.h" #import @@ -218,12 +219,74 @@ void FileProviderDomainManager::setupFileProviderDomains() void FileProviderDomainManager::addFileProviderDomainForAccount(AccountState *accountState) { + Q_ASSERT(accountState); + const auto account = accountState->account(); + Q_ASSERT(account); + d->addFileProviderDomain(accountState); + + const auto accountCapabilities = account->capabilities().isValid(); + if (!accountCapabilities) { + connect(account.get(), &Account::capabilitiesChanged, this, [this, account] { + trySetupPushNotificationsForAccount(account.get()); + }); + return; + } + + trySetupPushNotificationsForAccount(account.get()); +} + +void FileProviderDomainManager::trySetupPushNotificationsForAccount(Account *account) +{ + Q_ASSERT(account); + + const auto pushNotifications = account->pushNotifications(); + const auto pushNotificationsCapability = account->capabilities().availablePushNotifications() & PushNotificationType::Files; + + if (pushNotificationsCapability && pushNotifications && pushNotifications->isReady()) { + qCDebug(lcMacFileProviderDomainManager) << "Push notifications already ready, connecting them to enumerator signalling." + << account->displayName(); + setupPushNotificationsForAccount(account); + } else if (pushNotificationsCapability) { + qCDebug(lcMacFileProviderDomainManager) << "Push notifications not yet ready, will connect to signalling when ready." + << account->displayName(); + connect(account, &Account::pushNotificationsReady, this, &FileProviderDomainManager::setupPushNotificationsForAccount); + } +} + +void FileProviderDomainManager::setupPushNotificationsForAccount(Account *account) +{ + Q_ASSERT(account); + + qCDebug(lcMacFileProviderDomainManager) << "Setting up push notifications for file provider domain for account:" + << account->displayName(); + + connect(account->pushNotifications(), &PushNotifications::filesChanged, this, &FileProviderDomainManager::signalEnumeratorChanged); + disconnect(account, &Account::pushNotificationsReady, this, &FileProviderDomainManager::setupPushNotificationsForAccount); +} + +void FileProviderDomainManager::signalEnumeratorChanged(Account *account) +{ + Q_ASSERT(account); + d->signalEnumeratorChanged(account); } void FileProviderDomainManager::removeFileProviderDomainForAccount(AccountState* accountState) { + Q_ASSERT(accountState); + const auto account = accountState->account(); + Q_ASSERT(account); + d->removeFileProviderDomain(accountState); + + const auto pushNotifications = account->pushNotifications(); + const auto pushNotificationsCapability = account->capabilities().availablePushNotifications() & PushNotificationType::Files; + + if (pushNotificationsCapability && pushNotifications && pushNotifications->isReady()) { + disconnect(pushNotifications, &PushNotifications::filesChanged, this, &FileProviderDomainManager::signalEnumeratorChanged); + } else if (pushNotificationsCapability) { + disconnect(account.get(), &Account::pushNotificationsReady, this, &FileProviderDomainManager::setupPushNotificationsForAccount); + } } } // namespace Mac -- 2.30.2