From 15382807fadafdc6e51b5ec301c05e72800a301c Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Wed, 29 May 2024 00:02:44 +0800 Subject: [PATCH] Implement opening of a file with a given ocId in File Provider files Signed-off-by: Claudio Cambra --- src/gui/CMakeLists.txt | 1 + src/gui/macOS/fileprovidereditlocallyjob.h | 1 + .../macOS/fileprovidereditlocallyjob_mac.mm | 92 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/gui/macOS/fileprovidereditlocallyjob_mac.mm diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 8cf5eb9816..f26bfaa8e4 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -292,6 +292,7 @@ IF( APPLE ) macOS/fileproviderdomainsyncstatus_mac.mm macOS/fileprovidereditlocallyjob.h macOS/fileprovidereditlocallyjob.cpp + macOS/fileprovidereditlocallyjob_mac.mm macOS/fileprovideritemmetadata.h macOS/fileprovideritemmetadata.cpp macOS/fileprovideritemmetadata_mac.mm diff --git a/src/gui/macOS/fileprovidereditlocallyjob.h b/src/gui/macOS/fileprovidereditlocallyjob.h index 763debe108..1f3829999a 100644 --- a/src/gui/macOS/fileprovidereditlocallyjob.h +++ b/src/gui/macOS/fileprovidereditlocallyjob.h @@ -39,6 +39,7 @@ public slots: signals: void error(const QString &message, const QString &informativeText); + void ocIdAcquired(const QString &ocId); void notAvailable(); void finished(); diff --git a/src/gui/macOS/fileprovidereditlocallyjob_mac.mm b/src/gui/macOS/fileprovidereditlocallyjob_mac.mm new file mode 100644 index 0000000000..f7ebd1d281 --- /dev/null +++ b/src/gui/macOS/fileprovidereditlocallyjob_mac.mm @@ -0,0 +1,92 @@ +/* + * Copyright (C) by Claudio Cambra + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "fileprovidereditlocallyjob.h" + +#include + +#include "account.h" +#include "accountstate.h" +#include "editlocallymanager.h" +#include "systray.h" + +#include "macOS/fileprovider.h" +#include "macOS/fileproviderdomainmanager.h" + +#import +#import + +namespace OCC::Mac { + +Q_LOGGING_CATEGORY(lcFileProviderEditLocallyMacJob, + "nextcloud.gui.fileprovidereditlocallymac", + QtInfoMsg) + +void FileProviderEditLocallyJob::openFileProviderFile(const QString &ocId) +{ + qCDebug(lcFileProviderEditLocallyMacJob) << "Opening file provider file with OC ID" << ocId; + + const auto nsOcId = ocId.toNSString(); + const auto userId = _accountState->account()->userIdAtHostWithPort(); + const auto ncDomainManager = FileProvider::instance()->domainManager(); + const auto voidDomain = ncDomainManager->domainForAccount(_accountState.data()); + + NSFileProviderDomain *const domain = (NSFileProviderDomain *)voidDomain; + if (domain == nil) { + qCWarning(lcFileProviderEditLocallyMacJob) << "Could not get domain for account:" + << userId; + emit notAvailable(); + } + + NSFileProviderManager *const manager = [NSFileProviderManager managerForDomain:domain]; + if (manager == nil) { + qCWarning(lcFileProviderEditLocallyMacJob) << "Could not get file provider manager" + "for domain of account:" << userId;; + emit notAvailable(); + } + + dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); + __block NSError *receivedError; + __block NSURL *itemLocalUrl; + [manager getUserVisibleURLForItemIdentifier:nsOcId + completionHandler:^(NSURL *const url, NSError *const error) { + [url retain]; + [error retain]; + itemLocalUrl = url; + receivedError = error; + dispatch_semaphore_signal(semaphore); + }]; + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); + + Systray::instance()->destroyEditFileLocallyLoadingDialog(); + + if (receivedError != nil) { + const auto errorMessage = QString::fromNSString(receivedError.localizedDescription); + qCWarning(lcFileProviderEditLocallyMacJob) << "Error getting user visible URL for item" + << ocId << ":" << errorMessage; + emit notAvailable(); + } else if (itemLocalUrl != nil) { + const auto itemLocalPath = QString::fromNSString(itemLocalUrl.path); + qCDebug(lcFileProviderEditLocallyMacJob) << "Got user visible URL for item" + << ocId << ":" << itemLocalPath; + [NSWorkspace.sharedWorkspace openURL:itemLocalUrl]; + emit finished(); + } else { + qCWarning(lcFileProviderEditLocallyMacJob) << "Got nil user visible URL for item" + << ocId; + emit notAvailable(); + } +} + +} // namespace OCC::Mac