2024-02-27 15:27:04 +03:00
|
|
|
//
|
|
|
|
// ShareTableViewDataSource.swift
|
|
|
|
// FileProviderUIExt
|
|
|
|
//
|
|
|
|
// Created by Claudio Cambra on 27/2/24.
|
|
|
|
//
|
|
|
|
|
2024-02-27 15:28:09 +03:00
|
|
|
import AppKit
|
|
|
|
import FileProvider
|
|
|
|
import NextcloudKit
|
2024-04-15 13:42:50 +03:00
|
|
|
import NextcloudFileProviderKit
|
2024-03-19 21:26:35 +03:00
|
|
|
import NextcloudCapabilitiesKit
|
2024-02-27 15:28:55 +03:00
|
|
|
import OSLog
|
|
|
|
|
2024-02-27 18:56:08 +03:00
|
|
|
class ShareTableViewDataSource: NSObject, NSTableViewDataSource, NSTableViewDelegate {
|
|
|
|
private let shareItemViewIdentifier = NSUserInterfaceItemIdentifier("ShareTableItemView")
|
2024-02-27 18:56:18 +03:00
|
|
|
private let shareItemViewNib = NSNib(nibNamed: "ShareTableItemView", bundle: nil)
|
2024-03-19 15:54:41 +03:00
|
|
|
private let reattemptInterval: TimeInterval = 3.0
|
2024-02-27 18:56:18 +03:00
|
|
|
|
2024-02-28 16:16:34 +03:00
|
|
|
var uiDelegate: ShareViewDataSourceUIDelegate?
|
2024-02-27 15:28:09 +03:00
|
|
|
var sharesTableView: NSTableView? {
|
|
|
|
didSet {
|
2024-02-27 18:56:18 +03:00
|
|
|
sharesTableView?.register(shareItemViewNib, forIdentifier: shareItemViewIdentifier)
|
2024-02-27 19:21:01 +03:00
|
|
|
sharesTableView?.rowHeight = 42.0 // Height of view in ShareTableItemView XIB
|
2024-02-27 15:28:09 +03:00
|
|
|
sharesTableView?.dataSource = self
|
2024-02-27 19:19:37 +03:00
|
|
|
sharesTableView?.delegate = self
|
2024-02-27 15:28:09 +03:00
|
|
|
sharesTableView?.reloadData()
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 21:26:35 +03:00
|
|
|
var capabilities: Capabilities?
|
2024-03-19 13:57:52 +03:00
|
|
|
var itemMetadata: NKFile?
|
2024-02-28 16:16:34 +03:00
|
|
|
|
2024-03-04 16:42:06 +03:00
|
|
|
private(set) var kit: NextcloudKit?
|
2024-03-05 14:15:05 +03:00
|
|
|
private(set) var itemURL: URL?
|
|
|
|
private(set) var itemServerRelativePath: String?
|
2024-04-06 20:09:13 +03:00
|
|
|
private(set) var shares: [NKShare] = [] {
|
2024-03-04 19:05:10 +03:00
|
|
|
didSet { Task { @MainActor in sharesTableView?.reloadData() } }
|
2024-02-27 15:28:09 +03:00
|
|
|
}
|
2024-04-15 12:30:08 +03:00
|
|
|
private var account: Account? {
|
2024-02-27 18:56:55 +03:00
|
|
|
didSet {
|
|
|
|
guard let account = account else { return }
|
|
|
|
kit = NextcloudKit()
|
|
|
|
kit?.setup(
|
|
|
|
user: account.username,
|
|
|
|
userId: account.username,
|
|
|
|
password: account.password,
|
|
|
|
urlBase: account.serverUrl
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-02-27 15:28:27 +03:00
|
|
|
|
2024-02-27 18:56:55 +03:00
|
|
|
func loadItem(url: URL) {
|
2024-03-05 14:24:16 +03:00
|
|
|
itemServerRelativePath = nil
|
2024-02-27 15:28:55 +03:00
|
|
|
itemURL = url
|
|
|
|
Task {
|
|
|
|
await reload()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:54:41 +03:00
|
|
|
func reattempt() {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
Timer.scheduledTimer(withTimeInterval: self.reattemptInterval, repeats: false) { _ in
|
|
|
|
Task { await self.reload() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:51:37 +03:00
|
|
|
func reload() async {
|
2024-07-30 13:47:38 +03:00
|
|
|
guard let itemURL else {
|
|
|
|
presentError("No item URL, cannot reload data!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let kit else {
|
|
|
|
presentError("NextcloudKit instance is unavailable, cannot reload data!")
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 18:56:55 +03:00
|
|
|
guard let itemIdentifier = await withCheckedContinuation({
|
|
|
|
(continuation: CheckedContinuation<NSFileProviderItemIdentifier?, Never>) -> Void in
|
|
|
|
NSFileProviderManager.getIdentifierForUserVisibleFile(
|
|
|
|
at: itemURL
|
|
|
|
) { identifier, domainIdentifier, error in
|
|
|
|
defer { continuation.resume(returning: identifier) }
|
|
|
|
guard error == nil else {
|
2024-03-19 15:19:16 +03:00
|
|
|
self.presentError("No item with identifier: \(error.debugDescription)")
|
2024-02-27 18:56:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}) else {
|
2024-03-19 15:19:16 +03:00
|
|
|
presentError("Could not get identifier for item, no shares can be acquired.")
|
2024-02-27 18:56:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 15:28:55 +03:00
|
|
|
do {
|
2024-07-30 13:16:06 +03:00
|
|
|
let connection = try await serviceConnection(url: itemURL, interruptionHandler: {
|
|
|
|
Logger.sharesDataSource.error("Service connection interrupted")
|
|
|
|
})
|
2024-02-27 18:56:55 +03:00
|
|
|
guard let serverPath = await connection.itemServerPath(identifier: itemIdentifier),
|
|
|
|
let credentials = await connection.credentials() as? Dictionary<String, String>,
|
2024-04-15 12:30:08 +03:00
|
|
|
let convertedAccount = Account(dictionary: credentials),
|
2024-03-19 15:54:41 +03:00
|
|
|
!convertedAccount.password.isEmpty
|
|
|
|
else {
|
|
|
|
presentError("Failed to get details from File Provider Extension. Retrying.")
|
|
|
|
reattempt()
|
2024-02-27 18:56:55 +03:00
|
|
|
return
|
|
|
|
}
|
2024-03-19 13:57:52 +03:00
|
|
|
let serverPathString = serverPath as String
|
|
|
|
itemServerRelativePath = serverPathString
|
2024-02-27 18:56:55 +03:00
|
|
|
account = convertedAccount
|
2024-03-04 19:05:25 +03:00
|
|
|
await sharesTableView?.deselectAll(self)
|
2024-03-19 21:26:35 +03:00
|
|
|
capabilities = await fetchCapabilities()
|
2024-04-17 21:07:48 +03:00
|
|
|
guard capabilities != nil else { return }
|
2024-03-19 21:26:35 +03:00
|
|
|
guard capabilities?.filesSharing?.apiEnabled == true else {
|
2024-03-19 15:19:16 +03:00
|
|
|
presentError("Server does not support shares.")
|
2024-03-18 13:06:56 +03:00
|
|
|
return
|
|
|
|
}
|
2024-07-30 13:47:38 +03:00
|
|
|
itemMetadata = await fetchItemMetadata(itemRelativePath: serverPathString, kit: kit)
|
2024-03-19 14:38:56 +03:00
|
|
|
guard itemMetadata?.permissions.contains("R") == true else {
|
2024-03-19 15:19:16 +03:00
|
|
|
presentError("This file cannot be shared.")
|
2024-03-19 13:57:52 +03:00
|
|
|
return
|
|
|
|
}
|
2024-02-27 18:56:55 +03:00
|
|
|
shares = await fetch(
|
2024-03-19 13:57:52 +03:00
|
|
|
itemIdentifier: itemIdentifier, itemRelativePath: serverPathString
|
2024-02-27 18:56:55 +03:00
|
|
|
)
|
2024-02-27 15:28:55 +03:00
|
|
|
} catch let error {
|
2024-03-19 15:54:41 +03:00
|
|
|
presentError("Could not reload data: \(error), will try again.")
|
|
|
|
reattempt()
|
2024-02-27 15:28:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:56:55 +03:00
|
|
|
private func fetch(
|
|
|
|
itemIdentifier: NSFileProviderItemIdentifier, itemRelativePath: String
|
|
|
|
) async -> [NKShare] {
|
2024-02-28 16:57:08 +03:00
|
|
|
Task { @MainActor in uiDelegate?.fetchStarted() }
|
|
|
|
defer { Task { @MainActor in uiDelegate?.fetchFinished() } }
|
|
|
|
|
2024-02-27 18:56:55 +03:00
|
|
|
let rawIdentifier = itemIdentifier.rawValue
|
|
|
|
Logger.sharesDataSource.info("Fetching shares for item \(rawIdentifier, privacy: .public)")
|
|
|
|
|
|
|
|
guard let kit = kit else {
|
2024-03-19 15:19:16 +03:00
|
|
|
self.presentError("NextcloudKit instance is unavailable, cannot fetch shares!")
|
2024-02-27 18:56:55 +03:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
let parameter = NKShareParameter(path: itemRelativePath)
|
|
|
|
|
|
|
|
return await withCheckedContinuation { continuation in
|
|
|
|
kit.readShares(parameters: parameter) { account, shares, data, error in
|
|
|
|
let shareCount = shares?.count ?? 0
|
|
|
|
Logger.sharesDataSource.info("Received \(shareCount, privacy: .public) shares")
|
|
|
|
defer { continuation.resume(returning: shares ?? []) }
|
|
|
|
guard error == .success else {
|
2024-03-19 15:19:16 +03:00
|
|
|
self.presentError("Error fetching shares: \(error.errorDescription)")
|
2024-02-27 18:56:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 21:26:35 +03:00
|
|
|
private func fetchCapabilities() async -> Capabilities? {
|
2024-03-18 13:06:26 +03:00
|
|
|
return await withCheckedContinuation { continuation in
|
|
|
|
kit?.getCapabilities { account, capabilitiesJson, error in
|
|
|
|
guard error == .success, let capabilitiesJson = capabilitiesJson else {
|
2024-03-19 21:26:35 +03:00
|
|
|
self.presentError("Error getting server caps: \(error.errorDescription)")
|
|
|
|
continuation.resume(returning: nil)
|
2024-03-18 13:06:26 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
Logger.sharesDataSource.info("Successfully retrieved server share capabilities")
|
2024-03-19 21:26:35 +03:00
|
|
|
continuation.resume(returning: Capabilities(data: capabilitiesJson))
|
2024-03-18 13:06:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:19:16 +03:00
|
|
|
private func presentError(_ errorString: String) {
|
|
|
|
Logger.sharesDataSource.error("\(errorString, privacy: .public)")
|
|
|
|
Task { @MainActor in self.uiDelegate?.showError(errorString) }
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:55:26 +03:00
|
|
|
// MARK: - NSTableViewDataSource protocol methods
|
|
|
|
|
|
|
|
@objc func numberOfRows(in tableView: NSTableView) -> Int {
|
|
|
|
shares.count
|
|
|
|
}
|
2024-02-27 18:56:08 +03:00
|
|
|
|
|
|
|
// MARK: - NSTableViewDelegate protocol methods
|
|
|
|
|
|
|
|
@objc func tableView(
|
|
|
|
_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int
|
|
|
|
) -> NSView? {
|
2024-02-27 19:20:43 +03:00
|
|
|
let share = shares[row]
|
|
|
|
guard let view = tableView.makeView(
|
|
|
|
withIdentifier: shareItemViewIdentifier, owner: self
|
|
|
|
) as? ShareTableItemView else {
|
2024-03-19 15:19:16 +03:00
|
|
|
Logger.sharesDataSource.error("Acquired item view from table is not a share item view!")
|
2024-02-27 19:20:43 +03:00
|
|
|
return nil
|
|
|
|
}
|
2024-02-28 11:01:01 +03:00
|
|
|
view.share = share
|
2024-02-27 18:56:08 +03:00
|
|
|
return view
|
|
|
|
}
|
2024-02-28 16:16:34 +03:00
|
|
|
|
|
|
|
@objc func tableViewSelectionDidChange(_ notification: Notification) {
|
|
|
|
guard let selectedRow = sharesTableView?.selectedRow, selectedRow >= 0 else {
|
2024-03-19 15:08:14 +03:00
|
|
|
Task { @MainActor in uiDelegate?.hideOptions(self) }
|
2024-02-28 16:16:34 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
let share = shares[selectedRow]
|
2024-02-28 16:57:23 +03:00
|
|
|
Task { @MainActor in uiDelegate?.showOptions(share: share) }
|
2024-02-28 16:16:34 +03:00
|
|
|
}
|
2024-02-27 15:28:09 +03:00
|
|
|
}
|