FileProviderItem.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. init(metadata: tableMetadata, parentItemIdentifier: NSFileProviderItemIdentifier) {
  60. self.parentItemIdentifier = parentItemIdentifier
  61. self.itemIdentifier = fileProviderUtility.sharedInstance.getItemIdentifier(metadata: metadata)
  62. self.contentModificationDate = metadata.date as Date
  63. self.creationDate = metadata.date as Date
  64. self.documentSize = NSNumber(value: metadata.size)
  65. self.filename = metadata.fileNameView
  66. self.isDirectory = metadata.directory
  67. self.typeIdentifier = CCUtility.insertTypeFileIconName(metadata.fileNameView, metadata: metadata)
  68. self.versionIdentifier = metadata.etag.data(using: .utf8)
  69. if (!metadata.directory) {
  70. self.documentSize = NSNumber(value: metadata.size)
  71. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  72. if tableLocalFile == nil {
  73. isMostRecentVersionDownloaded = false
  74. } else {
  75. isMostRecentVersionDownloaded = true
  76. }
  77. } else {
  78. // Favorite directory
  79. let rank = fileProviderData.sharedInstance.listFavoriteIdentifierRank[metadata.ocId]
  80. if (rank == nil) {
  81. favoriteRank = nil
  82. } else {
  83. favoriteRank = fileProviderData.sharedInstance.listFavoriteIdentifierRank[metadata.ocId]
  84. }
  85. }
  86. // Tag
  87. if let tableTag = NCManageDatabase.sharedInstance.getTag(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  88. tagData = tableTag.tagIOS
  89. }
  90. }
  91. }