Use specific functions for creating/converting domain identifiers and account Ids, prevent mismatches

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-03-11 14:05:51 +01:00
parent 712dde0a2f
commit 30b453f85e
No known key found for this signature in database
GPG key ID: C839200C384636B0

View file

@ -23,6 +23,37 @@
#include "gui/accountstate.h" #include "gui/accountstate.h"
#include "libsync/account.h" #include "libsync/account.h"
namespace {
QString domainIdentifierForAccount(const OCC::Account * const account)
{
Q_ASSERT(account);
return account->userIdAtHostWithPort();
}
QString domainIdentifierForAccount(const OCC::AccountPtr account)
{
return domainIdentifierForAccount(account.get());
}
QString domainDisplayNameForAccount(const OCC::Account * const account)
{
Q_ASSERT(account);
return account->displayName();
}
QString domainDisplayNameForAccount(const OCC::AccountPtr account)
{
return domainDisplayNameForAccount(account.get());
}
QString accountIdFromDomain(NSFileProviderDomain * const domain)
{
return QString::fromNSString(domain.identifier);
}
}
namespace OCC { namespace OCC {
Q_LOGGING_CATEGORY(lcMacFileProviderDomainManager, "nextcloud.gui.macfileproviderdomainmanager", QtInfoMsg) Q_LOGGING_CATEGORY(lcMacFileProviderDomainManager, "nextcloud.gui.macfileproviderdomainmanager", QtInfoMsg)
@ -59,12 +90,12 @@ class FileProviderDomainManager::Private {
} }
for (NSFileProviderDomain * const domain in domains) { for (NSFileProviderDomain * const domain in domains) {
const auto accountId = QString::fromNSString(domain.identifier); const auto accountId = accountIdFromDomain(domain);
if (const auto accountState = AccountManager::instance()->accountFromUserId(accountId); if (const auto accountState = AccountManager::instance()->accountFromUserId(accountId);
accountState && accountState &&
accountState->account() && accountState->account() &&
accountState->account()->displayName() == QString::fromNSString(domain.displayName)) { domainDisplayNameForAccount(accountState->account()) == QString::fromNSString(domain.displayName)) {
qCDebug(lcMacFileProviderDomainManager) << "Found existing file provider domain for account:" qCDebug(lcMacFileProviderDomainManager) << "Found existing file provider domain for account:"
<< accountState->account()->displayName(); << accountState->account()->displayName();
@ -92,18 +123,18 @@ class FileProviderDomainManager::Private {
void addFileProviderDomain(const AccountState * const accountState) void addFileProviderDomain(const AccountState * const accountState)
{ {
const auto accountDisplayName = accountState->account()->displayName(); const auto domainDisplayName = domainDisplayNameForAccount(account);
const auto accountId = accountState->account()->userIdAtHostWithPort(); const auto domainId = domainIdentifierForAccount(account);
qCDebug(lcMacFileProviderDomainManager) << "Adding new file provider domain for account with id: " << accountId; qCDebug(lcMacFileProviderDomainManager) << "Adding new file provider domain with id: " << domainId;
if(_registeredDomains.contains(accountId) && _registeredDomains.value(accountId) != nil) { if(_registeredDomains.contains(domainId) && _registeredDomains.value(domainId) != nil) {
qCDebug(lcMacFileProviderDomainManager) << "File provider domain for account with id already exists: " << accountId; qCDebug(lcMacFileProviderDomainManager) << "File provider domain with id already exists: " << domainId;
return; return;
} }
NSFileProviderDomain * const fileProviderDomain = [[NSFileProviderDomain alloc] initWithIdentifier:accountId.toNSString() NSFileProviderDomain * const fileProviderDomain = [[NSFileProviderDomain alloc] initWithIdentifier:domainId.toNSString()
displayName:accountDisplayName.toNSString()]; displayName:domainDisplayName.toNSString()];
[fileProviderDomain retain]; [fileProviderDomain retain];
[NSFileProviderManager addDomain:fileProviderDomain completionHandler:^(NSError * const error) { [NSFileProviderManager addDomain:fileProviderDomain completionHandler:^(NSError * const error) {
@ -114,20 +145,21 @@ class FileProviderDomainManager::Private {
} }
}]; }];
_registeredDomains.insert(accountId, fileProviderDomain); _registeredDomains.insert(domainId, fileProviderDomain);
} }
void removeFileProviderDomain(const AccountState * const accountState) void removeFileProviderDomain(const AccountState * const accountState)
{ {
const auto accountId = accountState->account()->userIdAtHostWithPort();
qCDebug(lcMacFileProviderDomainManager) << "Removing file provider domain for account with id: " << accountId;
if(!_registeredDomains.contains(accountId)) { const auto domainId = domainIdentifierForAccount(account);
qCDebug(lcMacFileProviderDomainManager) << "File provider domain not found for id: " << accountId; qCDebug(lcMacFileProviderDomainManager) << "Removing file provider domain with id: " << domainId;
if(!_registeredDomains.contains(domainId)) {
qCDebug(lcMacFileProviderDomainManager) << "File provider domain not found for id: " << domainId;
return; return;
} }
NSFileProviderDomain * const fileProviderDomain = _registeredDomains[accountId]; NSFileProviderDomain * const fileProviderDomain = _registeredDomains[domainId];
[NSFileProviderManager removeDomain:fileProviderDomain completionHandler:^(NSError *error) { [NSFileProviderManager removeDomain:fileProviderDomain completionHandler:^(NSError *error) {
if(error) { if(error) {
@ -137,7 +169,7 @@ class FileProviderDomainManager::Private {
} }
}]; }];
NSFileProviderDomain * const domain = _registeredDomains.take(accountId); NSFileProviderDomain * const domain = _registeredDomains.take(domainId);
[domain release]; [domain release];
} }
@ -153,8 +185,11 @@ class FileProviderDomainManager::Private {
return; return;
} }
for (NSFileProviderDomain * const domain : _registeredDomains.values()) { const auto registeredDomainPtrs = _registeredDomains.values();
[domain release]; for (NSFileProviderDomain * const domain : registeredDomainPtrs) {
if (domain != nil) {
[domain release];
}
} }
_registeredDomains.clear(); _registeredDomains.clear();
}]; }];
@ -195,15 +230,16 @@ class FileProviderDomainManager::Private {
void signalEnumeratorChanged(const Account * const account) void signalEnumeratorChanged(const Account * const account)
{ {
const auto accountId = account->userIdAtHostWithPort(); const auto domainId = domainIdentifierForAccount(account);
qCDebug(lcMacFileProviderDomainManager) << "Signalling enumerator changed in file provider domain for account with id: " << accountId;
if(!_registeredDomains.contains(accountId)) { qCDebug(lcMacFileProviderDomainManager) << "Signalling enumerator changed in file provider domain for account with id: " << domainId;
qCDebug(lcMacFileProviderDomainManager) << "File provider domain not found for id: " << accountId;
if(!_registeredDomains.contains(domainId)) {
qCDebug(lcMacFileProviderDomainManager) << "File provider domain not found for id: " << domainId;
return; return;
} }
NSFileProviderDomain * const fileProviderDomain = _registeredDomains[accountId]; NSFileProviderDomain * const fileProviderDomain = _registeredDomains[domainId];
Q_ASSERT(fileProviderDomain != nil); Q_ASSERT(fileProviderDomain != nil);
NSFileProviderManager * const fpManager = [NSFileProviderManager managerForDomain:fileProviderDomain]; NSFileProviderManager * const fpManager = [NSFileProviderManager managerForDomain:fileProviderDomain];