From 7e1ebed8c730858f84436d46594d03eb43fe8c6f Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 21 Mar 2023 13:28:48 +0100 Subject: [PATCH] Split client interfacing code into FileProviderExtension extension Signed-off-by: Claudio Cambra --- ...ileProviderExtension+ClientInterface.swift | 71 +++++++++++++++++++ .../FileProviderExtension.swift | 50 ------------- .../project.pbxproj | 4 ++ 3 files changed, 75 insertions(+), 50 deletions(-) create mode 100644 shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift new file mode 100644 index 000000000..7cc860a71 --- /dev/null +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2023 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. + */ + +import Foundation +import FileProvider +import OSLog +import NCDesktopClientSocketKit +import NextcloudKit + +extension FileProviderExtension { + func sendFileProviderDomainIdentifier() { + let command = "FILE_PROVIDER_DOMAIN_IDENTIFIER_REQUEST_REPLY" + let argument = domain.identifier.rawValue + let message = command + ":" + argument + "\n" + socketClient?.sendMessage(message) + } + + private func signalEnumeratorAfterAccountSetup() { + guard let fpManager = NSFileProviderManager(for: domain) else { + Logger.fileProviderExtension.error("Could not get file provider manager for domain \(self.domain.displayName, privacy: OSLogPrivacy.auto(mask: .hash)), cannot notify after account setup") + return + } + + assert(ncAccount != nil) + + fpManager.signalErrorResolved(NSFileProviderError(.notAuthenticated)) { error in + if error != nil { + Logger.fileProviderExtension.error("Error resolving not authenticated, received error: \(error!.localizedDescription)") + } + } + + Logger.fileProviderExtension.debug("Signalling enumerators for user \(self.ncAccount!.username) at server \(self.ncAccount!.serverUrl, privacy: OSLogPrivacy.auto(mask: .hash))") + + fpManager.signalEnumerator(for: .workingSet) { error in + if error != nil { + Logger.fileProviderExtension.error("Error signalling enumerator for working set, received error: \(error!.localizedDescription, privacy: .public)") + } + } + } + + func setupDomainAccount(user: String, serverUrl: String, password: String) { + ncAccount = NextcloudAccount(user: user, serverUrl: serverUrl, password: password) + ncKit.setup(user: ncAccount!.username, + userId: ncAccount!.username, + password: ncAccount!.password, + urlBase: ncAccount!.serverUrl, + userAgent: "Nextcloud-macOS/FileProviderExt", + nextcloudVersion: 25, + delegate: nil) // TODO: add delegate methods for self + + Logger.fileProviderExtension.info("Nextcloud account set up in File Provider extension for user: \(user, privacy: OSLogPrivacy.auto(mask: .hash)) at server: \(serverUrl, privacy: OSLogPrivacy.auto(mask: .hash))") + + signalEnumeratorAfterAccountSetup() + } + + func removeAccountConfig() { + Logger.fileProviderExtension.info("Received instruction to remove account data for user \(self.ncAccount!.username, privacy: OSLogPrivacy.auto(mask: .hash)) at server \(self.ncAccount!.serverUrl, privacy: OSLogPrivacy.auto(mask: .hash))") + ncAccount = nil + } +} diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension.swift index d4357b7b4..4cf00fa5b 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension.swift @@ -630,54 +630,4 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKComm fpManager.signalEnumerator(for: .workingSet, completionHandler: completionHandler) } - - // MARK: Nextcloud desktop client communication - func sendFileProviderDomainIdentifier() { - let command = "FILE_PROVIDER_DOMAIN_IDENTIFIER_REQUEST_REPLY" - let argument = domain.identifier.rawValue - let message = command + ":" + argument + "\n" - socketClient?.sendMessage(message) - } - - private func signalEnumeratorAfterAccountSetup() { - guard let fpManager = NSFileProviderManager(for: domain) else { - Logger.fileProviderExtension.error("Could not get file provider manager for domain \(self.domain.displayName, privacy: OSLogPrivacy.auto(mask: .hash)), cannot notify after account setup") - return - } - - assert(ncAccount != nil) - - fpManager.signalErrorResolved(NSFileProviderError(.notAuthenticated)) { error in - if error != nil { - Logger.fileProviderExtension.error("Error resolving not authenticated, received error: \(error!.localizedDescription)") - } - } - - Logger.fileProviderExtension.debug("Signalling enumerators for user \(self.ncAccount!.username) at server \(self.ncAccount!.serverUrl, privacy: OSLogPrivacy.auto(mask: .hash))") - fpManager.signalEnumerator(for: .workingSet) { error in - if error != nil { - Logger.fileProviderExtension.error("Error signalling enumerator for working set, received error: \(error!.localizedDescription, privacy: .public)") - } - } - } - - func setupDomainAccount(user: String, serverUrl: String, password: String) { - ncAccount = NextcloudAccount(user: user, serverUrl: serverUrl, password: password) - ncKit.setup(user: ncAccount!.username, - userId: ncAccount!.username, - password: ncAccount!.password, - urlBase: ncAccount!.serverUrl, - userAgent: "Nextcloud-macOS/FileProviderExt", - nextcloudVersion: 25, - delegate: nil) // TODO: add delegate methods for self - - Logger.fileProviderExtension.info("Nextcloud account set up in File Provider extension for user: \(user, privacy: OSLogPrivacy.auto(mask: .hash)) at server: \(serverUrl, privacy: OSLogPrivacy.auto(mask: .hash))") - - signalEnumeratorAfterAccountSetup() - } - - func removeAccountConfig() { - Logger.fileProviderExtension.info("Received instruction to remove account data for user \(self.ncAccount!.username, privacy: OSLogPrivacy.auto(mask: .hash)) at server \(self.ncAccount!.serverUrl, privacy: OSLogPrivacy.auto(mask: .hash))") - ncAccount = nil - } } diff --git a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj index 3fac888d4..590657cca 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj +++ b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj @@ -38,6 +38,7 @@ 53D056312970594F00988392 /* LocalFilesUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53D056302970594F00988392 /* LocalFilesUtils.swift */; }; 53ED472029C5E64200795DB1 /* FileProviderEnumerator+SyncEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53ED471F29C5E64200795DB1 /* FileProviderEnumerator+SyncEngine.swift */; }; 53ED472829C88E7000795DB1 /* NextcloudItemMetadataTable+NKFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53ED472729C88E7000795DB1 /* NextcloudItemMetadataTable+NKFile.swift */; }; + 53ED473029C9CE0B00795DB1 /* FileProviderExtension+ClientInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53ED472F29C9CE0B00795DB1 /* FileProviderExtension+ClientInterface.swift */; }; C2B573BA1B1CD91E00303B36 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = C2B573B91B1CD91E00303B36 /* main.m */; }; C2B573D21B1CD94B00303B36 /* main.m in Resources */ = {isa = PBXBuildFile; fileRef = C2B573B91B1CD91E00303B36 /* main.m */; }; C2B573DE1B1CD9CE00303B36 /* FinderSync.m in Sources */ = {isa = PBXBuildFile; fileRef = C2B573DD1B1CD9CE00303B36 /* FinderSync.m */; }; @@ -163,6 +164,7 @@ 53D056302970594F00988392 /* LocalFilesUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalFilesUtils.swift; sourceTree = ""; }; 53ED471F29C5E64200795DB1 /* FileProviderEnumerator+SyncEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileProviderEnumerator+SyncEngine.swift"; sourceTree = ""; }; 53ED472729C88E7000795DB1 /* NextcloudItemMetadataTable+NKFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NextcloudItemMetadataTable+NKFile.swift"; sourceTree = ""; }; + 53ED472F29C9CE0B00795DB1 /* FileProviderExtension+ClientInterface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileProviderExtension+ClientInterface.swift"; sourceTree = ""; }; C2B573B11B1CD91E00303B36 /* desktopclient.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = desktopclient.app; sourceTree = BUILT_PRODUCTS_DIR; }; C2B573B51B1CD91E00303B36 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; C2B573B91B1CD91E00303B36 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; @@ -254,6 +256,7 @@ 538E397027F4765000FA63D5 /* FileProviderEnumerator.swift */, 53ED471F29C5E64200795DB1 /* FileProviderEnumerator+SyncEngine.swift */, 538E396C27F4765000FA63D5 /* FileProviderExtension.swift */, + 53ED472F29C9CE0B00795DB1 /* FileProviderExtension+ClientInterface.swift */, 538E396E27F4765000FA63D5 /* FileProviderItem.swift */, 5318AD9629BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift */, 536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */, @@ -565,6 +568,7 @@ files = ( 5352E85B29B7BFE6002CE85C /* Progress+Extensions.swift in Sources */, 536EFC36295E3C1100F4CB13 /* NextcloudAccount.swift in Sources */, + 53ED473029C9CE0B00795DB1 /* FileProviderExtension+ClientInterface.swift in Sources */, 538E396D27F4765000FA63D5 /* FileProviderExtension.swift in Sources */, 536EFBF7295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift in Sources */, 53ED472029C5E64200795DB1 /* FileProviderEnumerator+SyncEngine.swift in Sources */, -- 2.30.2