FileProviderItem.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // FileProviderItem.swift
  3. // Files
  4. //
  5. // Created by Marino Faggiana on 26/03/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import FileProvider
  25. import NextcloudKit
  26. import UniformTypeIdentifiers
  27. class FileProviderItem: NSObject, NSFileProviderItem {
  28. var metadata: tableMetadata
  29. /// Providing Required Properties
  30. var itemIdentifier: NSFileProviderItemIdentifier {
  31. return fileProviderUtility().getItemIdentifier(metadata: metadata)
  32. }
  33. var filename: String {
  34. return metadata.fileNameView
  35. }
  36. var typeIdentifier: String {
  37. let results = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: metadata.fileNameView, mimeType: "", directory: metadata.directory, account: metadata.account)
  38. return results.typeIdentifier
  39. }
  40. var capabilities: NSFileProviderItemCapabilities {
  41. if metadata.directory {
  42. return [ .allowsAddingSubItems, .allowsContentEnumerating, .allowsReading, .allowsDeleting, .allowsRenaming ]
  43. } else if metadata.lock {
  44. return [ .allowsReading ]
  45. }
  46. return [ .allowsWriting, .allowsReading, .allowsDeleting, .allowsRenaming, .allowsReparenting ]
  47. }
  48. /// Managing Content
  49. var childItemCount: NSNumber? {
  50. return metadata.directory ? nil : nil
  51. }
  52. var documentSize: NSNumber? {
  53. return metadata.directory ? nil : NSNumber(value: metadata.size)
  54. }
  55. /// Specifying Content Location
  56. var parentItemIdentifier: NSFileProviderItemIdentifier
  57. var isTrashed: Bool {
  58. return false
  59. }
  60. var symlinkTargetPath: String? {
  61. return nil
  62. }
  63. /// Tracking Usage
  64. var contentModificationDate: Date? {
  65. return metadata.date as Date
  66. }
  67. var creationDate: Date? {
  68. return metadata.creationDate as Date
  69. }
  70. var lastUsedDate: Date? {
  71. return metadata.date as Date
  72. }
  73. /// Tracking Versions
  74. var versionIdentifier: Data? {
  75. return metadata.etag.data(using: .utf8)
  76. }
  77. var isMostRecentVersionDownloaded: Bool {
  78. if NCManageDatabase.shared.getTableLocalFile(ocId: metadata.ocId) == nil {
  79. return false
  80. } else {
  81. return true
  82. }
  83. }
  84. /// Monitoring File Transfers
  85. var isUploading: Bool {
  86. if metadata.status == NCGlobal.shared.metadataStatusWaitUpload || metadata.status == NCGlobal.shared.metadataStatusUploading {
  87. return true
  88. } else {
  89. return false
  90. }
  91. }
  92. var isUploaded: Bool {
  93. if metadata.status == NCGlobal.shared.metadataStatusWaitUpload || metadata.status == NCGlobal.shared.metadataStatusUploading || metadata.status == NCGlobal.shared.metadataStatusUploadError {
  94. return false
  95. } else {
  96. return true
  97. }
  98. }
  99. var uploadingError: Error? {
  100. if metadata.status == NCGlobal.shared.metadataStatusUploadError {
  101. return fileProviderData.FileProviderError.uploadError
  102. } else {
  103. return nil
  104. }
  105. }
  106. var isDownloading: Bool {
  107. if metadata.status == NCGlobal.shared.metadataStatusWaitDownload || metadata.status == NCGlobal.shared.metadataStatusDownloading {
  108. return true
  109. } else {
  110. return false
  111. }
  112. }
  113. var isDownloaded: Bool {
  114. if NCUtilityFileSystem().fileProviderStorageExists(metadata) {
  115. return true
  116. } else {
  117. return false
  118. }
  119. }
  120. var downloadingError: Error? {
  121. if metadata.status == NCGlobal.shared.metadataStatusDownloadError {
  122. return fileProviderData.FileProviderError.downloadError
  123. } else {
  124. return nil
  125. }
  126. }
  127. /// Sharing
  128. /// Managing Metadata
  129. var tagData: Data? {
  130. if let tableTag = NCManageDatabase.shared.getTag(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  131. return tableTag.tagIOS
  132. } else {
  133. return nil
  134. }
  135. }
  136. var favoriteRank: NSNumber? {
  137. if let rank = fileProviderData.shared.listFavoriteIdentifierRank[metadata.ocId] {
  138. return rank
  139. } else {
  140. return nil
  141. }
  142. }
  143. init(metadata: tableMetadata, parentItemIdentifier: NSFileProviderItemIdentifier) {
  144. self.metadata = tableMetadata(value: metadata)
  145. self.parentItemIdentifier = parentItemIdentifier
  146. }
  147. }