Add convenience properties to NextcloudItemMetadataTable
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 14 Feb 2023 12:09:07 +0000 (13:09 +0100)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Fri, 12 May 2023 05:21:18 +0000 (13:21 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/NextcloudFilesDatabaseTables.swift

index b324507ae2cbeeb80451512b3fb5799ddd846913..1672998fc5f0a2467b34ada59e0f212a80f58bfe 100644 (file)
@@ -14,6 +14,8 @@
 
 import Foundation
 import RealmSwift
+import FileProvider
+import NextcloudKit
 
 class NextcloudItemMetadataTable: Object {
     enum Status: Int {
@@ -30,6 +32,17 @@ class NextcloudItemMetadataTable: Object {
         case uploadError = 4
     }
 
+    enum SharePermissions: Int {
+        case readShare = 1
+        case updateShare = 2
+        case createShare = 4
+        case deleteShare = 8
+        case shareShare = 16
+
+        case maxFileShare = 19
+        case maxFolderShare = 31
+    }
+
     override func isEqual(_ object: Any?) -> Bool {
         if let object = object as? NextcloudItemMetadataTable {
             return self.fileId == object.fileId &&
@@ -119,6 +132,72 @@ class NextcloudItemMetadataTable: Object {
     @Persisted var urlBase = ""
     @Persisted var user = ""
     @Persisted var userId = ""
+
+    var fileExtension: String {
+        (fileNameView as NSString).pathExtension
+    }
+
+    var fileNoExtension: String {
+        (fileNameView as NSString).deletingPathExtension
+    }
+
+    var isRenameable: Bool {
+        return lock
+    }
+
+    var isPrintable: Bool {
+        if isDocumentViewableOnly {
+            return false
+        }
+        if ["application/pdf", "com.adobe.pdf"].contains(contentType) || contentType.hasPrefix("text/") || classFile == NKCommon.typeClassFile.image.rawValue {
+            return true
+        }
+        return false
+    }
+
+    var isDocumentViewableOnly: Bool {
+        return sharePermissionsCollaborationServices == SharePermissions.readShare.rawValue &&
+            classFile == NKCommon.typeClassFile.document.rawValue
+    }
+
+    var isCopyableInPasteboard: Bool {
+        !isDocumentViewableOnly && !directory
+    }
+
+    var isModifiableWithQuickLook: Bool {
+        if directory || isDocumentViewableOnly {
+            return false
+        }
+        return contentType == "com.adobe.pdf" || contentType == "application/pdf" || classFile == NKCommon.typeClassFile.image.rawValue
+    }
+
+    var isSettableOnOffline: Bool {
+        return session.isEmpty && !isDocumentViewableOnly
+    }
+
+    var canOpenIn: Bool {
+        return session.isEmpty && !isDocumentViewableOnly && !directory
+    }
+
+    var isDownloadUpload: Bool {
+        return status == Status.inDownload.rawValue ||
+            status == Status.downloading.rawValue ||
+            status == Status.inUpload.rawValue ||
+            status == Status.uploading.rawValue
+    }
+
+    var isDownload: Bool {
+        status == Status.inDownload.rawValue || status == Status.downloading.rawValue
+    }
+
+    var isUpload: Bool {
+        status == Status.inUpload.rawValue || status == Status.uploading.rawValue
+    }
+
+    /// Returns false if the user is lokced out of the file. I.e. The file is locked but by somone else
+    func canUnlock(as user: String) -> Bool {
+        return !lock || (lockOwner == user && lockOwnerType == 0)
+    }
 }
 
 class NextcloudDirectoryMetadataTable: Object {