FileProviderItem.swift 6.2 KB

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