FileProviderItem.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 FileProvider
  24. class FileProviderItem: NSObject, NSFileProviderItem {
  25. // Providing Required Properties
  26. var itemIdentifier: NSFileProviderItemIdentifier // The item's persistent identifier
  27. var filename: String = "" // The item's filename
  28. var typeIdentifier: String = "" // The item's uniform type identifiers
  29. var capabilities: NSFileProviderItemCapabilities { // The item's capabilities
  30. if (self.isDirectory) {
  31. return [ .allowsAddingSubItems, .allowsContentEnumerating, .allowsReading, .allowsDeleting, .allowsRenaming ]
  32. } else {
  33. if isUpload {
  34. return [ ]
  35. } else {
  36. return [ .allowsWriting, .allowsReading, .allowsDeleting, .allowsRenaming, .allowsReparenting ]
  37. }
  38. }
  39. }
  40. // Managing Content
  41. var childItemCount: NSNumber? // The number of items contained by this item
  42. var documentSize: NSNumber? // The document's size, in bytes
  43. // Specifying Content Location
  44. var parentItemIdentifier: NSFileProviderItemIdentifier // The persistent identifier of the item's parent folder
  45. var isTrashed: Bool = false // A Boolean value that indicates whether an item is in the trash
  46. // Tracking Usage
  47. var contentModificationDate: Date? // The date the item was last modified
  48. var creationDate: Date? // The date the item was created
  49. var lastUsedDate: Date? = Date() // The date the item was last used, default to the moment when the item is created
  50. // Tracking Versions
  51. var versionIdentifier: Data? // A data value used to determine when the item changes
  52. var isMostRecentVersionDownloaded: Bool = true // A Boolean value that indicates whether the item is the most recent version downloaded from the server
  53. // Monitoring File Transfers
  54. var isUploading: Bool = false // A Boolean value that indicates whether the item is currently uploading to your remote server
  55. var isUploaded: Bool = true // A Boolean value that indicates whether the item has been uploaded to your remote server
  56. var uploadingError: Error? // An error that occurred while uploading to your remote server
  57. var isDownloading: Bool = false // A Boolean value that indicates whether the item is currently downloading from your remote server
  58. var isDownloaded: Bool = true // A Boolean value that indicates whether the item has been downloaded from your remote server
  59. var downloadingError: Error? // An error that occurred while downloading the item
  60. var tagData: Data? // Tag
  61. var favoriteRank: NSNumber? // Favorite
  62. var isDirectory = false
  63. var isDownload = false
  64. var isUpload = false
  65. init(metadata: tableMetadata, parentItemIdentifier: NSFileProviderItemIdentifier) {
  66. self.parentItemIdentifier = parentItemIdentifier
  67. self.itemIdentifier = fileProviderUtility.sharedInstance.getItemIdentifier(metadata: metadata)
  68. self.contentModificationDate = metadata.date as Date
  69. self.creationDate = metadata.date as Date
  70. self.documentSize = NSNumber(value: metadata.size)
  71. self.filename = metadata.fileNameView
  72. self.isDirectory = metadata.directory
  73. self.typeIdentifier = CCUtility.insertTypeFileIconName(metadata.fileNameView, metadata: metadata)
  74. self.versionIdentifier = metadata.etag.data(using: .utf8)
  75. if (!metadata.directory) {
  76. self.documentSize = NSNumber(value: metadata.size)
  77. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  78. if tableLocalFile == nil {
  79. // self.isDownloaded = false
  80. } else {
  81. // self.isDownloaded = true
  82. }
  83. // Download
  84. if (metadata.session == k_download_session_extension && metadata.status != k_metadataStatusDownloadError) {
  85. self.isDownloading = true
  86. } else {
  87. self.isDownloading = false
  88. }
  89. // Upload
  90. if (metadata.session == k_upload_session_extension && metadata.status != k_metadataStatusUploadError) {
  91. isUpload = true
  92. self.isUploading = true
  93. self.isUploaded = false
  94. }
  95. // Error ?
  96. if metadata.sessionError != "" {
  97. uploadingError = NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo:[:])
  98. }
  99. } else {
  100. // Favorite directory
  101. let rank = fileProviderData.sharedInstance.listFavoriteIdentifierRank[metadata.ocId]
  102. if (rank == nil) {
  103. favoriteRank = nil
  104. } else {
  105. favoriteRank = fileProviderData.sharedInstance.listFavoriteIdentifierRank[metadata.ocId]
  106. }
  107. }
  108. // Tag
  109. if let tableTag = NCManageDatabase.sharedInstance.getTag(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  110. tagData = tableTag.tagIOS
  111. }
  112. }
  113. }