From ca94b452f12bdba96a17eba9f93c95dd991de654 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Wed, 20 Mar 2024 02:26:35 +0800 Subject: [PATCH] Add package dependency NextcloudCapabilitiesKit to NextcloudIntegration Signed-off-by: Claudio Cambra --- .../FileProviderUIExt/ShareCapabilities.swift | 114 ------------------ .../FileProviderUIExt/ShareOptionsView.swift | 18 +-- .../ShareTableViewDataSource.swift | 15 +-- .../project.pbxproj | 21 +++- 4 files changed, 34 insertions(+), 134 deletions(-) delete mode 100644 shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareCapabilities.swift diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareCapabilities.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareCapabilities.swift deleted file mode 100644 index 747802c27..000000000 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareCapabilities.swift +++ /dev/null @@ -1,114 +0,0 @@ -// -// ShareCapabilities.swift -// FileProviderUIExt -// -// Created by Claudio Cambra on 13/3/24. -// - -import Foundation -import OSLog - -struct ShareCapabilities { - struct EmailCapabilities { - private(set) var passwordEnabled = false - private(set) var passwordEnforced = false - - init(dict: [String: Any]) { - Logger.shareCapabilities.debug("Parsing email capabilities: \(dict, privacy: .public)") - - if let passwordCapabilities = dict["password"] as? [String : Any] { - passwordEnabled = passwordCapabilities["enabled"] as? Bool ?? false - passwordEnforced = passwordCapabilities["enforced"] as? Bool ?? false - } - } - } - - struct PublicLinkCapabilities { - private(set) var enabled = false - private(set) var allowUpload = false - private(set) var supportsUploadOnly = false - private(set) var askOptionalPassword = false - private(set) var enforcePassword = false - private(set) var enforceExpireDate = false - private(set) var expireDateDays = 1 - private(set) var internalEnforceExpireDate = false - private(set) var internalExpireDateDays = 1 - private(set) var remoteEnforceExpireDate = false - private(set) var remoteExpireDateDays = 1 - private(set) var multipleAllowed = false - - init(dict: [String: Any]) { - Logger.shareCapabilities.debug("Parsing link capabilities: \(dict, privacy: .public)") - - enabled = dict["enabled"] as? Bool ?? false - allowUpload = dict["upload"] as? Bool ?? false - supportsUploadOnly = dict["supports_upload_only"] as? Bool ?? false - multipleAllowed = dict["multiple"] as? Bool ?? false - - if let passwordCaps = dict["password"] as? [String : Any] { - askOptionalPassword = passwordCaps["askForOptionalPassword"] as? Bool ?? false - enforcePassword = passwordCaps["enforced"] as? Bool ?? false - } - - if let expireDateCapabilities = dict["expire_date"] as? [String: Any] { - expireDateDays = expireDateCapabilities["days"] as? Int ?? 1 - enforceExpireDate = expireDateCapabilities["enforced"] as? Bool ?? false - } - - if let internalExpDateCaps = dict["expire_date_internal"] as? [String: Any] { - internalExpireDateDays = internalExpDateCaps["days"] as? Int ?? 1 - internalEnforceExpireDate = internalExpDateCaps["enforced"] as? Bool ?? false - } - - if let remoteExpDateCaps = dict["expire_date_remote"] as? [String: Any] { - remoteExpireDateDays = remoteExpDateCaps["days"] as? Int ?? 1 - remoteEnforceExpireDate = remoteExpDateCaps["enforced"] as? Bool ?? false - } - } - } - - private(set) var apiEnabled = false - private(set) var resharing = false - private(set) var defaultPermissions = 0 - private(set) var email = EmailCapabilities(dict: [:]) - private(set) var publicLink = PublicLinkCapabilities(dict: [:]) - - - init() { - Logger.shareCapabilities.warning("Providing defaulted share capabilities!") - } - - init(json: Data) { - guard let anyJson = try? JSONSerialization.jsonObject(with: json, options: []) else { - let jsonString = String(data: json, encoding: .utf8) ?? "UNKNOWN" - Logger.shareCapabilities.error( - "Received capabilities is not valid JSON! \(jsonString, privacy: .public)" - ) - return - } - - guard let jsonDict = anyJson as? [String : Any], - let ocsData = jsonDict["ocs"] as? [String : Any], - let receivedData = ocsData["data"] as? [String : Any], - let capabilities = receivedData["capabilities"] as? [String : Any], - let sharingCapabilities = capabilities["files_sharing"] as? [String : Any] - else { - let jsonString = anyJson as? [String : Any] ?? ["UNKNOWN" : "UNKNOWN"] - Logger.shareCapabilities.error( - "Could not parse share capabilities! \(jsonString, privacy: .public)" - ) - return - } - apiEnabled = sharingCapabilities["api_enabled"] as? Bool ?? false - - if let emailCapabilities = sharingCapabilities["sharebymail"] as? [String : Any] { - email = EmailCapabilities(dict: emailCapabilities) - } - - if let publicLinkCapabilities = sharingCapabilities["public"] as? [String : Any] { - publicLink = PublicLinkCapabilities(dict: publicLinkCapabilities) - } - - Logger.shareCapabilities.debug("Parses share capabilities.") - } -} diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareOptionsView.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareOptionsView.swift index d8231de28..1a9d51fa8 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareOptionsView.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareOptionsView.swift @@ -128,7 +128,7 @@ class ShareOptionsView: NSView { let type = pickedShareType() shareRecipientTextField.isHidden = type == .publicLink - if let caps = dataSource?.shareCapabilities { + if let caps = dataSource?.capabilities?.filesSharing { uploadEditPermissionCheckbox.state = caps.defaultPermissions & NKShare.PermissionValues.updateShare.rawValue != 0 ? .on : .off @@ -136,20 +136,20 @@ class ShareOptionsView: NSView { switch type { case .publicLink: passwordProtectCheckbox.isHidden = false - passwordProtectCheckbox.state = caps.publicLink.enforcePassword ? .on : .off - passwordProtectCheckbox.isEnabled = !caps.publicLink.enforcePassword - expirationDateCheckbox.state = caps.publicLink.enforceExpireDate ? .on : .off - expirationDateCheckbox.isEnabled = !caps.publicLink.enforceExpireDate + passwordProtectCheckbox.state = caps.publicLink?.passwordEnforced == true ? .on : .off + passwordProtectCheckbox.isEnabled = caps.publicLink?.passwordEnforced == false + expirationDateCheckbox.state = caps.publicLink?.expireDateEnforced == true ? .on : .off + expirationDateCheckbox.isEnabled = caps.publicLink?.expireDateEnforced == false expirationDatePicker.dateValue = Date( timeIntervalSinceNow: - TimeInterval(caps.publicLink.expireDateDays * 24 * 60 * 60) + TimeInterval((caps.publicLink?.expireDateDays ?? 1) * 24 * 60 * 60) ) - if caps.publicLink.enforceExpireDate { + if caps.publicLink?.expireDateEnforced == true { expirationDatePicker.maxDate = expirationDatePicker.dateValue } case .email: - passwordProtectCheckbox.isHidden = !caps.email.passwordEnabled - passwordProtectCheckbox.state = caps.email.passwordEnforced ? .on : .off + passwordProtectCheckbox.isHidden = caps.email?.passwordEnabled == false + passwordProtectCheckbox.state = caps.email?.passwordEnforced == true ? .on : .off default: passwordProtectCheckbox.isHidden = true passwordProtectCheckbox.state = .off diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareTableViewDataSource.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareTableViewDataSource.swift index d61d38b73..cf8087bc4 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareTableViewDataSource.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderUIExt/ShareTableViewDataSource.swift @@ -8,6 +8,7 @@ import AppKit import FileProvider import NextcloudKit +import NextcloudCapabilitiesKit import OSLog class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDelegate { @@ -25,7 +26,7 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele sharesTableView?.reloadData() } } - var shareCapabilities = ShareCapabilities() + var capabilities: Capabilities? var itemMetadata: NKFile? private(set) var kit: NextcloudKit? @@ -96,8 +97,8 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele itemServerRelativePath = serverPathString account = convertedAccount await sharesTableView?.deselectAll(self) - shareCapabilities = await fetchCapabilities() - guard shareCapabilities.apiEnabled else { + capabilities = await fetchCapabilities() + guard capabilities?.filesSharing?.apiEnabled == true else { presentError("Server does not support shares.") return } @@ -163,16 +164,16 @@ class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDele } } - private func fetchCapabilities() async -> ShareCapabilities { + private func fetchCapabilities() async -> Capabilities? { return await withCheckedContinuation { continuation in kit?.getCapabilities { account, capabilitiesJson, error in guard error == .success, let capabilitiesJson = capabilitiesJson else { - self.presentError("Error getting server capabilities: \(error.errorDescription)") - continuation.resume(returning: ShareCapabilities()) + self.presentError("Error getting server caps: \(error.errorDescription)") + continuation.resume(returning: nil) return } Logger.sharesDataSource.info("Successfully retrieved server share capabilities") - continuation.resume(returning: ShareCapabilities(json: capabilitiesJson)) + continuation.resume(returning: Capabilities(data: capabilitiesJson)) } } } diff --git a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj index 98460d511..421fc64e9 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj +++ b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj @@ -16,12 +16,12 @@ 5318AD9529BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9429BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift */; }; 5318AD9729BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9629BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift */; }; 5318AD9929BF58D000CBB71C /* NKError+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9829BF58D000CBB71C /* NKError+Extensions.swift */; }; - 534F812F2BA1B3D80068D145 /* ShareCapabilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 534F812E2BA1B3D80068D145 /* ShareCapabilities.swift */; }; 5350E4E92B0C534A00F276CB /* ClientCommunicationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5350E4E82B0C534A00F276CB /* ClientCommunicationService.swift */; }; 5352B36629DC14970011CE03 /* NextcloudFilesDatabaseManager+Directories.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5352B36529DC14970011CE03 /* NextcloudFilesDatabaseManager+Directories.swift */; }; 5352B36829DC17D60011CE03 /* NextcloudFilesDatabaseManager+LocalFiles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5352B36729DC17D60011CE03 /* NextcloudFilesDatabaseManager+LocalFiles.swift */; }; 5352B36C29DC44B50011CE03 /* FileProviderExtension+Thumbnailing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5352B36B29DC44B50011CE03 /* FileProviderExtension+Thumbnailing.swift */; }; 5352E85B29B7BFE6002CE85C /* Progress+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5352E85A29B7BFE6002CE85C /* Progress+Extensions.swift */; }; + 5358F2B92BAA0F5300E3C729 /* NextcloudCapabilitiesKit in Frameworks */ = {isa = PBXBuildFile; productRef = 5358F2B82BAA0F5300E3C729 /* NextcloudCapabilitiesKit */; }; 535AE30E29C0A2CC0042A9BA /* Logger+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 535AE30D29C0A2CC0042A9BA /* Logger+Extensions.swift */; }; 536EFBF7295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */; }; 536EFC36295E3C1100F4CB13 /* NextcloudAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536EFC35295E3C1100F4CB13 /* NextcloudAccount.swift */; }; @@ -165,7 +165,6 @@ 5318AD9429BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudLocalFileMetadataTable.swift; sourceTree = ""; }; 5318AD9629BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderMaterialisedEnumerationObserver.swift; sourceTree = ""; }; 5318AD9829BF58D000CBB71C /* NKError+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NKError+Extensions.swift"; sourceTree = ""; }; - 534F812E2BA1B3D80068D145 /* ShareCapabilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareCapabilities.swift; sourceTree = ""; }; 5350E4E72B0C514400F276CB /* ClientCommunicationProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ClientCommunicationProtocol.h; sourceTree = ""; }; 5350E4E82B0C534A00F276CB /* ClientCommunicationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientCommunicationService.swift; sourceTree = ""; }; 5350E4EA2B0C9CE100F276CB /* FileProviderExt-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FileProviderExt-Bridging-Header.h"; sourceTree = ""; }; @@ -250,6 +249,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 5358F2B92BAA0F5300E3C729 /* NextcloudCapabilitiesKit in Frameworks */, 53FE14542B8E1219006C4193 /* NextcloudKit in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -365,7 +365,6 @@ children = ( 5376307B2B85E2E00026BFAB /* Extensions */, 53B979802B84C81F002DA742 /* DocumentActionViewController.swift */, - 534F812E2BA1B3D80068D145 /* ShareCapabilities.swift */, 5374FD432B95EE1400C78D54 /* ShareController.swift */, 53FE14662B8F78B6006C4193 /* ShareOptionsView.swift */, 53FE14582B8E3F6C006C4193 /* ShareTableItemView.swift */, @@ -523,6 +522,7 @@ name = FileProviderUIExt; packageProductDependencies = ( 53FE14532B8E1219006C4193 /* NextcloudKit */, + 5358F2B82BAA0F5300E3C729 /* NextcloudCapabilitiesKit */, ); productName = FileProviderUIExt; productReference = 53B9797E2B84C81F002DA742 /* FileProviderUIExt.appex */; @@ -623,6 +623,7 @@ packageReferences = ( 5307A6E42965C6FA001E0C6A /* XCRemoteSwiftPackageReference "NextcloudKit" */, 5307A6E92965DB57001E0C6A /* XCRemoteSwiftPackageReference "realm-swift" */, + 5358F2B72BAA045E00E3C729 /* XCRemoteSwiftPackageReference "NextcloudCapabilitiesKit" */, ); productRefGroup = C2B573B21B1CD91E00303B36 /* Products */; projectDirPath = ""; @@ -750,7 +751,6 @@ 5374FD442B95EE1400C78D54 /* ShareController.swift in Sources */, 53FE145B2B8F1305006C4193 /* NKShare+Extensions.swift in Sources */, 53FE14592B8E3F6C006C4193 /* ShareTableItemView.swift in Sources */, - 534F812F2BA1B3D80068D145 /* ShareCapabilities.swift in Sources */, 53FE14552B8E28E9006C4193 /* NextcloudAccount.swift in Sources */, 5376307D2B85E2ED0026BFAB /* Logger+Extensions.swift in Sources */, 53FE14502B8E0658006C4193 /* ShareTableViewDataSource.swift in Sources */, @@ -1531,6 +1531,14 @@ version = 10.33.0; }; }; + 5358F2B72BAA045E00E3C729 /* XCRemoteSwiftPackageReference "NextcloudCapabilitiesKit" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/claucambra/NextcloudCapabilitiesKit.git"; + requirement = { + branch = main; + kind = branch; + }; + }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ @@ -1549,6 +1557,11 @@ package = 5307A6E92965DB57001E0C6A /* XCRemoteSwiftPackageReference "realm-swift" */; productName = RealmSwift; }; + 5358F2B82BAA0F5300E3C729 /* NextcloudCapabilitiesKit */ = { + isa = XCSwiftPackageProductDependency; + package = 5358F2B72BAA045E00E3C729 /* XCRemoteSwiftPackageReference "NextcloudCapabilitiesKit" */; + productName = NextcloudCapabilitiesKit; + }; 53FE14512B8E1213006C4193 /* NextcloudKit */ = { isa = XCSwiftPackageProductDependency; package = 5307A6E42965C6FA001E0C6A /* XCRemoteSwiftPackageReference "NextcloudKit" */;