]> dgit.raspbian.org Git - nextcloud-desktop.git/blob
dd3bef8d8738016263e2b37332dfe664c45b912e
[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 Foundation
16 import FileProvider
17 import OSLog
18 import NCDesktopClientSocketKit
19 import NextcloudKit
20
21 extension FileProviderExtension {
22     func sendFileProviderDomainIdentifier() {
23         let command = "FILE_PROVIDER_DOMAIN_IDENTIFIER_REQUEST_REPLY"
24         let argument = domain.identifier.rawValue
25         let message = command + ":" + argument + "\n"
26         socketClient?.sendMessage(message)
27     }
28
29     private func signalEnumeratorAfterAccountSetup() {
30         guard let fpManager = NSFileProviderManager(for: domain) else {
31             Logger.fileProviderExtension.error("Could not get file provider manager for domain \(self.domain.displayName, privacy: .public), cannot notify after account setup")
32             return
33         }
34
35         assert(ncAccount != nil)
36
37         fpManager.signalErrorResolved(NSFileProviderError(.notAuthenticated)) { error in
38             if error != nil {
39                 Logger.fileProviderExtension.error("Error resolving not authenticated, received error: \(error!.localizedDescription)")
40             }
41         }
42
43         Logger.fileProviderExtension.debug("Signalling enumerators for user \(self.ncAccount!.username) at server \(self.ncAccount!.serverUrl, privacy: .public)")
44
45         fpManager.signalEnumerator(for: .workingSet) { error in
46             if error != nil {
47                 Logger.fileProviderExtension.error("Error signalling enumerator for working set, received error: \(error!.localizedDescription, privacy: .public)")
48             }
49         }
50     }
51
52     func setupDomainAccount(user: String, serverUrl: String, password: String) {
53         ncAccount = NextcloudAccount(user: user, serverUrl: serverUrl, password: password)
54         ncKit.setup(user: ncAccount!.username,
55                     userId: ncAccount!.username,
56                     password: ncAccount!.password,
57                     urlBase: ncAccount!.serverUrl,
58                     userAgent: "Nextcloud-macOS/FileProviderExt",
59                     nextcloudVersion: 25,
60                     delegate: nil) // TODO: add delegate methods for self
61
62         Logger.fileProviderExtension.info("Nextcloud account set up in File Provider extension for user: \(user, privacy: .public) at server: \(serverUrl, privacy: .public)")
63
64         signalEnumeratorAfterAccountSetup()
65     }
66
67     func removeAccountConfig() {
68         Logger.fileProviderExtension.info("Received instruction to remove account data for user \(self.ncAccount!.username, privacy: .public) at server \(self.ncAccount!.serverUrl, privacy: .public)")
69         ncAccount = nil
70     }
71 }