From 13cf81578275e5211bc9fade72c5e548b88ab473 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Wed, 21 Feb 2024 18:49:58 +0800 Subject: [PATCH] Set the right filename and icon on share view controller view Signed-off-by: Claudio Cambra --- .../Extensions/Logger+Extensions.swift | 1 + .../ShareViewController.swift | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Extensions/Logger+Extensions.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Extensions/Logger+Extensions.swift index 88e9b15a9..8ec218668 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Extensions/Logger+Extensions.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/Extensions/Logger+Extensions.swift @@ -11,5 +11,6 @@ extension Logger { private static var subsystem = Bundle.main.bundleIdentifier! static let actionViewController = Logger(subsystem: subsystem, category: "actionViewController") + static let shareViewController = Logger(subsystem: subsystem, category: "shareViewController") } diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareViewController.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareViewController.swift index 4ffc8f731..068349e26 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareViewController.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareViewController.swift @@ -7,6 +7,8 @@ import AppKit import FileProvider +import OSLog +import QuickLookThumbnailing class ShareViewController: NSViewController { let itemIdentifiers: [NSFileProviderItemIdentifier] @@ -27,6 +29,16 @@ class ShareViewController: NSViewController { init(_ itemIdentifiers: [NSFileProviderItemIdentifier]) { self.itemIdentifiers = itemIdentifiers super.init(nibName: nil, bundle: nil) + + guard let firstItem = itemIdentifiers.first else { + Logger.shareViewController.error("called without items") + closeAction(self) + return + } + + Task { + await processItemIdentifier(firstItem) + } } required init?(coder: NSCoder) { @@ -36,4 +48,38 @@ class ShareViewController: NSViewController { @IBAction func closeAction(_ sender: Any) { actionViewController.extensionContext.completeRequest() } + + func processItemIdentifier(_ itemIdentifier: NSFileProviderItemIdentifier) async { + guard let manager = NSFileProviderManager(for: actionViewController.domain) else { + fatalError("NSFileProviderManager isn't expected to fail") + } + + do { + let itemUrl = try await manager.getUserVisibleURL(for: itemIdentifier) + fileNameLabel.stringValue = itemUrl.lastPathComponent + + let request = QLThumbnailGenerator.Request( + fileAt: itemUrl, + size: CGSize(width: 128, height: 128), + scale: 1.0, + representationTypes: .icon + ) + + let generator = QLThumbnailGenerator.shared + let fileThumbnail = await withCheckedContinuation { continuation in + generator.generateRepresentations(for: request) { thumbnail, type, error in + if thumbnail == nil || error != nil { + Logger.shareViewController.error("Could not get thumbnail: \(error)") + } + continuation.resume(returning: thumbnail) + } + } + fileNameIcon.image = fileThumbnail?.nsImage + } catch let error { + let errorString = "Error processing item: \(error)" + Logger.shareViewController.error("\(errorString)") + fileNameLabel.stringValue = "Unknown item" + descriptionLabel.stringValue = errorString + } + } } -- 2.30.2