5a5fcecfce60eefd9031fa48a82d8d426bd69844
[nextcloud-desktop.git] /
1 /*
2  * Copyright (C) 2023 by Claudio Cambra <claudio.cambra@nextcloud.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12  * for more details.
13  */
14
15 import FileProvider
16 import Foundation
17 import NCDesktopClientSocketKit
18 import NextcloudKit
19 import OSLog
20
21 extension FileProviderExtension: NSFileProviderServicing {
22     /*
23      This FileProviderExtension extension contains everything needed to communicate with the client.
24      We have two systems for communicating between the extensions and the client.
25
26      Apple's XPC based File Provider APIs let us easily communicate client -> extension.
27      This is what ClientCommunicationService is for.
28
29      We also use sockets, because the File Provider XPC system does not let us easily talk from
30      extension->client.
31      We need this because the extension needs to be able to request account details. We can't
32      reliably do this via XPC because the extensions get torn down by the system, out of the control
33      of the app, and we can receive nil/no services from NSFileProviderManager. Once this is done
34      then XPC works ok.
35     */
36     func supportedServiceSources(
37         for itemIdentifier: NSFileProviderItemIdentifier,
38         completionHandler: @escaping ([NSFileProviderServiceSource]?, Error?) -> Void
39     ) -> Progress {
40         Logger.desktopClientConnection.debug("Serving supported service sources")
41         let clientCommService = ClientCommunicationService(fpExtension: self)
42         let fpuiExtService = FPUIExtensionServiceSource(fpExtension: self)
43         let services: [NSFileProviderServiceSource] = [clientCommService, fpuiExtService]
44         completionHandler(services, nil)
45         let progress = Progress()
46         progress.cancellationHandler = {
47             let error = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError)
48             completionHandler(nil, error)
49         }
50         return progress
51     }
52
53     @objc func sendFileProviderDomainIdentifier() {
54         let command = "FILE_PROVIDER_DOMAIN_IDENTIFIER_REQUEST_REPLY"
55         let argument = domain.identifier.rawValue
56         let message = command + ":" + argument + "\n"
57         socketClient?.sendMessage(message)
58     }
59
60     private func signalEnumeratorAfterAccountSetup() {
61         guard let fpManager = NSFileProviderManager(for: domain) else {
62             Logger.fileProviderExtension.error(
63                 "Could not get file provider manager for domain \(self.domain.displayName, privacy: .public), cannot notify after account setup"
64             )
65             return
66         }
67
68         assert(ncAccount != nil)
69
70         fpManager.signalErrorResolved(NSFileProviderError(.notAuthenticated)) { error in
71             if error != nil {
72                 Logger.fileProviderExtension.error(
73                     "Error resolving not authenticated, received error: \(error!.localizedDescription)"
74                 )
75             }
76         }
77
78         Logger.fileProviderExtension.debug(
79             "Signalling enumerators for user \(self.ncAccount!.username) at server \(self.ncAccount!.serverUrl, privacy: .public)"
80         )
81
82         fpManager.signalEnumerator(for: .workingSet) { error in
83             if error != nil {
84                 Logger.fileProviderExtension.error(
85                     "Error signalling enumerator for working set, received error: \(error!.localizedDescription, privacy: .public)"
86                 )
87             }
88         }
89     }
90
91     @objc func setupDomainAccount(user: String, serverUrl: String, password: String) {
92         let newNcAccount = NextcloudAccount(user: user, serverUrl: serverUrl, password: password)
93         guard newNcAccount != ncAccount else { return }
94         ncAccount = newNcAccount
95         ncKit.setup(
96             user: ncAccount!.username,
97             userId: ncAccount!.username,
98             password: ncAccount!.password,
99             urlBase: ncAccount!.serverUrl,
100             userAgent: "Nextcloud-macOS/FileProviderExt",
101             nextcloudVersion: 25,
102             delegate: nil) // TODO: add delegate methods for self
103
104         Logger.fileProviderExtension.info(
105             "Nextcloud account set up in File Provider extension for user: \(user, privacy: .public) at server: \(serverUrl, privacy: .public)"
106         )
107
108         signalEnumeratorAfterAccountSetup()
109     }
110
111     @objc func removeAccountConfig() {
112         Logger.fileProviderExtension.info(
113             "Received instruction to remove account data for user \(self.ncAccount!.username, privacy: .public) at server \(self.ncAccount!.serverUrl, privacy: .public)"
114         )
115         ncAccount = nil
116     }
117 }