2024-02-21 15:29:03 +03:00
|
|
|
//
|
|
|
|
// FPUIExtensionCommunicationService.swift
|
|
|
|
// FileProviderExt
|
|
|
|
//
|
|
|
|
// Created by Claudio Cambra on 21/2/24.
|
|
|
|
//
|
|
|
|
|
|
|
|
import FileProvider
|
|
|
|
import Foundation
|
2024-02-27 15:29:46 +03:00
|
|
|
import NextcloudKit
|
2024-04-15 12:30:08 +03:00
|
|
|
import NextcloudFileProviderKit
|
2024-02-21 15:29:03 +03:00
|
|
|
import OSLog
|
|
|
|
|
|
|
|
class FPUIExtensionServiceSource: NSObject, NSFileProviderServiceSource, NSXPCListenerDelegate, FPUIExtensionService {
|
|
|
|
let listener = NSXPCListener.anonymous()
|
|
|
|
let serviceName = fpUiExtensionServiceName
|
|
|
|
let fpExtension: FileProviderExtension
|
|
|
|
|
|
|
|
init(fpExtension: FileProviderExtension) {
|
|
|
|
Logger.fpUiExtensionService.debug("Instantiating FPUIExtensionService service")
|
|
|
|
self.fpExtension = fpExtension
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeListenerEndpoint() throws -> NSXPCListenerEndpoint {
|
|
|
|
listener.delegate = self
|
|
|
|
listener.resume()
|
|
|
|
return listener.endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func listener(
|
|
|
|
_ listener: NSXPCListener,
|
|
|
|
shouldAcceptNewConnection newConnection: NSXPCConnection
|
|
|
|
) -> Bool {
|
2024-02-27 16:24:49 +03:00
|
|
|
newConnection.exportedInterface = NSXPCInterface(with: FPUIExtensionService.self)
|
2024-02-21 15:29:03 +03:00
|
|
|
newConnection.exportedObject = self
|
|
|
|
newConnection.resume()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
//MARK: - FPUIExtensionService protocol methods
|
|
|
|
|
2024-02-27 17:47:43 +03:00
|
|
|
func credentials() async -> NSDictionary {
|
|
|
|
return (fpExtension.ncAccount?.dictionary() ?? [:]) as NSDictionary
|
|
|
|
}
|
2024-02-27 18:53:49 +03:00
|
|
|
|
|
|
|
func itemServerPath(identifier: NSFileProviderItemIdentifier) async -> NSString? {
|
|
|
|
let rawIdentifier = identifier.rawValue
|
|
|
|
Logger.shares.info("Fetching shares for item \(rawIdentifier, privacy: .public)")
|
|
|
|
|
|
|
|
guard let baseUrl = fpExtension.ncAccount?.davFilesUrl else {
|
|
|
|
Logger.shares.error("Could not fetch shares as ncAccount on parent extension is nil")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:30:08 +03:00
|
|
|
let dbManager = FilesDatabaseManager.shared
|
2024-02-27 18:53:49 +03:00
|
|
|
guard let item = dbManager.itemMetadataFromFileProviderItemIdentifier(identifier) else {
|
|
|
|
Logger.shares.error("No item \(rawIdentifier, privacy: .public) in db, no shares.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let completePath = item.serverUrl + "/" + item.fileName
|
|
|
|
return completePath.replacingOccurrences(of: baseUrl, with: "") as NSString
|
|
|
|
}
|
2024-02-21 15:29:03 +03:00
|
|
|
}
|