2022-03-29 16:00:59 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
|
|
|
|
*
|
|
|
|
* 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 FileProvider
|
2023-01-12 23:49:28 +03:00
|
|
|
import NextcloudKit
|
2024-01-22 09:40:47 +03:00
|
|
|
import UniformTypeIdentifiers
|
2022-03-29 16:00:59 +03:00
|
|
|
|
|
|
|
class FileProviderItem: NSObject, NSFileProviderItem {
|
2023-01-12 23:49:28 +03:00
|
|
|
enum FileProviderItemTransferError: Error {
|
|
|
|
case downloadError
|
|
|
|
case uploadError
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
2023-01-12 23:49:28 +03:00
|
|
|
|
|
|
|
let metadata: NextcloudItemMetadataTable
|
|
|
|
let parentItemIdentifier: NSFileProviderItemIdentifier
|
2023-02-02 22:13:35 +03:00
|
|
|
let ncKit: NextcloudKit
|
2024-01-22 09:40:47 +03:00
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
var itemIdentifier: NSFileProviderItemIdentifier {
|
2024-01-22 09:40:47 +03:00
|
|
|
NSFileProviderItemIdentifier(metadata.ocId)
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
2024-01-22 09:40:47 +03:00
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
var capabilities: NSFileProviderItemCapabilities {
|
2023-01-12 23:49:28 +03:00
|
|
|
guard !metadata.directory else {
|
2024-01-22 09:40:47 +03:00
|
|
|
return [
|
|
|
|
.allowsAddingSubItems,
|
|
|
|
.allowsContentEnumerating,
|
|
|
|
.allowsReading,
|
|
|
|
.allowsDeleting,
|
|
|
|
.allowsRenaming,
|
|
|
|
]
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
|
|
|
guard !metadata.lock else {
|
2024-01-22 09:40:47 +03:00
|
|
|
return [.allowsReading]
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
2024-01-22 09:40:47 +03:00
|
|
|
return [
|
|
|
|
.allowsWriting,
|
|
|
|
.allowsReading,
|
|
|
|
.allowsDeleting,
|
|
|
|
.allowsRenaming,
|
|
|
|
.allowsReparenting,
|
|
|
|
]
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
2024-01-22 09:40:47 +03:00
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
var itemVersion: NSFileProviderItemVersion {
|
2024-01-22 09:40:47 +03:00
|
|
|
NSFileProviderItemVersion(
|
|
|
|
contentVersion: metadata.etag.data(using: .utf8)!,
|
|
|
|
metadataVersion: metadata.etag.data(using: .utf8)!)
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
2024-01-22 09:40:47 +03:00
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
var filename: String {
|
2024-01-22 09:40:47 +03:00
|
|
|
metadata.fileNameView
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
2024-01-22 09:40:47 +03:00
|
|
|
|
2022-03-29 16:00:59 +03:00
|
|
|
var contentType: UTType {
|
2024-01-22 09:40:47 +03:00
|
|
|
if itemIdentifier == .rootContainer || metadata.directory {
|
2023-01-12 23:49:28 +03:00
|
|
|
return .folder
|
|
|
|
}
|
|
|
|
|
2024-01-22 09:40:47 +03:00
|
|
|
let internalType = ncKit.nkCommonInstance.getInternalType(
|
|
|
|
fileName: metadata.fileNameView,
|
|
|
|
mimeType: "",
|
|
|
|
directory: metadata.directory)
|
2023-01-12 23:49:28 +03:00
|
|
|
return UTType(filenameExtension: internalType.ext) ?? .content
|
|
|
|
}
|
|
|
|
|
|
|
|
var documentSize: NSNumber? {
|
2024-01-22 09:40:47 +03:00
|
|
|
NSNumber(value: metadata.size)
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var creationDate: Date? {
|
2024-01-22 09:40:47 +03:00
|
|
|
metadata.creationDate as Date
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var lastUsedDate: Date? {
|
2024-01-22 09:40:47 +03:00
|
|
|
metadata.date as Date
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
|
|
|
|
2023-04-03 13:22:38 +03:00
|
|
|
var contentModificationDate: Date? {
|
2024-01-22 09:40:47 +03:00
|
|
|
metadata.date as Date
|
2023-04-03 13:22:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-12 23:49:28 +03:00
|
|
|
var isDownloaded: Bool {
|
2024-01-22 09:40:47 +03:00
|
|
|
metadata.directory
|
|
|
|
|| NextcloudFilesDatabaseManager.shared.localFileMetadataFromOcId(metadata.ocId) != nil
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var isDownloading: Bool {
|
2024-01-22 09:40:47 +03:00
|
|
|
metadata.status == NextcloudItemMetadataTable.Status.downloading.rawValue
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var downloadingError: Error? {
|
|
|
|
if metadata.status == NextcloudItemMetadataTable.Status.downloadError.rawValue {
|
|
|
|
return FileProviderItemTransferError.downloadError
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var isUploaded: Bool {
|
2024-01-22 09:40:47 +03:00
|
|
|
NextcloudFilesDatabaseManager.shared.localFileMetadataFromOcId(metadata.ocId) != nil
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var isUploading: Bool {
|
2024-01-22 09:40:47 +03:00
|
|
|
metadata.status == NextcloudItemMetadataTable.Status.uploading.rawValue
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var uploadingError: Error? {
|
|
|
|
if metadata.status == NextcloudItemMetadataTable.Status.uploadError.rawValue {
|
2024-01-22 09:40:47 +03:00
|
|
|
FileProviderItemTransferError.uploadError
|
2023-01-12 23:49:28 +03:00
|
|
|
} else {
|
2024-01-22 09:40:47 +03:00
|
|
|
nil
|
2023-01-12 23:49:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 10:56:29 +03:00
|
|
|
var childItemCount: NSNumber? {
|
|
|
|
if metadata.directory {
|
2024-01-22 09:40:47 +03:00
|
|
|
NSNumber(
|
|
|
|
integerLiteral: NextcloudFilesDatabaseManager.shared.childItemsForDirectory(
|
|
|
|
metadata
|
|
|
|
).count)
|
2023-04-04 10:56:29 +03:00
|
|
|
} else {
|
2024-01-22 09:40:47 +03:00
|
|
|
nil
|
2023-04-04 10:56:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 09:40:47 +03:00
|
|
|
required init(
|
2024-02-05 12:31:55 +03:00
|
|
|
metadata: NextcloudItemMetadataTable,
|
|
|
|
parentItemIdentifier: NSFileProviderItemIdentifier,
|
2024-01-22 09:40:47 +03:00
|
|
|
ncKit: NextcloudKit
|
|
|
|
) {
|
2023-03-11 00:25:28 +03:00
|
|
|
self.metadata = metadata
|
2023-01-12 23:49:28 +03:00
|
|
|
self.parentItemIdentifier = parentItemIdentifier
|
2023-02-02 22:13:35 +03:00
|
|
|
self.ncKit = ncKit
|
2023-01-12 23:49:28 +03:00
|
|
|
super.init()
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
|
|
|
}
|