FileProviderData.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // FileProviderData.swift
  3. // Files
  4. //
  5. // Created by Marino Faggiana on 27/05/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 NCCommunication
  24. class fileProviderData: NSObject {
  25. @objc static let sharedInstance: fileProviderData = {
  26. let instance = fileProviderData()
  27. return instance
  28. }()
  29. var account = ""
  30. var accountUser = ""
  31. var accountUserID = ""
  32. var accountPassword = ""
  33. var accountUrl = ""
  34. var homeServerUrl = ""
  35. // Max item for page
  36. let itemForPage = 20
  37. // List of etag for serverUrl
  38. var listServerUrlEtag = [String:String]()
  39. // Anchor
  40. var currentAnchor: UInt64 = 0
  41. // Rank favorite
  42. var listFavoriteIdentifierRank = [String:NSNumber]()
  43. // Item for signalEnumerator
  44. var fileProviderSignalDeleteContainerItemIdentifier = [NSFileProviderItemIdentifier:NSFileProviderItemIdentifier]()
  45. var fileProviderSignalUpdateContainerItem = [NSFileProviderItemIdentifier:FileProviderItem]()
  46. var fileProviderSignalDeleteWorkingSetItemIdentifier = [NSFileProviderItemIdentifier:NSFileProviderItemIdentifier]()
  47. var fileProviderSignalUpdateWorkingSetItem = [NSFileProviderItemIdentifier:FileProviderItem]()
  48. // UserDefaults
  49. var ncUserDefaults = UserDefaults(suiteName: NCBrandOptions.sharedInstance.capabilitiesGroups)
  50. // Error
  51. enum FileProviderError: Error {
  52. case downloadError
  53. case uploadError
  54. }
  55. // MARK: -
  56. func setupActiveAccount(domain: String?, providerExtension: NSFileProviderExtension) -> Bool {
  57. var foundAccount: Bool = false
  58. if CCUtility.getDisableFilesApp() || NCBrandOptions.sharedInstance.disable_openin_file {
  59. return false
  60. }
  61. // NO DOMAIN -> Set default account
  62. if domain == nil {
  63. guard let tableAccount = NCManageDatabase.sharedInstance.getAccountActive() else { return false }
  64. account = tableAccount.account
  65. accountUser = tableAccount.user
  66. accountUserID = tableAccount.userID
  67. accountPassword = CCUtility.getPassword(tableAccount.account)
  68. accountUrl = tableAccount.url
  69. homeServerUrl = CCUtility.getHomeServerUrlActiveUrl(tableAccount.url)
  70. NCCommunicationCommon.sharedInstance.setup(username: accountUserID, password: accountPassword, userAgent: CCUtility.getUserAgent(), capabilitiesGroup: NCBrandOptions.sharedInstance.capabilitiesGroups, delegate: NCNetworking.sharedInstance)
  71. NCNetworking.sharedInstance.setup(account: tableAccount.account, delegate: providerExtension as? NCNetworkingDelegate)
  72. return true
  73. }
  74. let tableAccounts = NCManageDatabase.sharedInstance.getAllAccount()
  75. if tableAccounts.count == 0 { return false }
  76. for tableAccount in tableAccounts {
  77. guard let url = NSURL(string: tableAccount.url) else { continue }
  78. guard let host = url.host else { continue }
  79. let accountDomain = tableAccount.userID + " (" + host + ")"
  80. if accountDomain == domain {
  81. account = tableAccount.account
  82. accountUser = tableAccount.user
  83. accountUserID = tableAccount.userID
  84. guard let password = CCUtility.getPassword(tableAccount.account) else { return false }
  85. accountPassword = password
  86. accountUrl = tableAccount.url
  87. homeServerUrl = CCUtility.getHomeServerUrlActiveUrl(tableAccount.url)
  88. NCCommunicationCommon.sharedInstance.setup(username: accountUserID, password: accountPassword, userAgent: CCUtility.getUserAgent(), capabilitiesGroup: NCBrandOptions.sharedInstance.capabilitiesGroups, delegate: NCNetworking.sharedInstance)
  89. NCNetworking.sharedInstance.setup(account: tableAccount.account, delegate: providerExtension as? NCNetworkingDelegate)
  90. foundAccount = true
  91. }
  92. }
  93. return foundAccount
  94. }
  95. func setupActiveAccount(itemIdentifier: NSFileProviderItemIdentifier, providerExtension: NSFileProviderExtension) -> Bool {
  96. var foundAccount: Bool = false
  97. guard let accountFromItemIdentifier = fileProviderUtility.sharedInstance.getAccountFromItemIdentifier(itemIdentifier) else { return false }
  98. let tableAccounts = NCManageDatabase.sharedInstance.getAllAccount()
  99. if tableAccounts.count == 0 { return false }
  100. for tableAccount in tableAccounts {
  101. if accountFromItemIdentifier == tableAccount.account {
  102. account = tableAccount.account
  103. accountUser = tableAccount.user
  104. accountUserID = tableAccount.userID
  105. accountPassword = CCUtility.getPassword(tableAccount.account)
  106. accountUrl = tableAccount.url
  107. homeServerUrl = CCUtility.getHomeServerUrlActiveUrl(tableAccount.url)
  108. NCCommunicationCommon.sharedInstance.setup(username: accountUserID, password: accountPassword, userAgent: CCUtility.getUserAgent(), capabilitiesGroup: NCBrandOptions.sharedInstance.capabilitiesGroups, delegate: NCNetworking.sharedInstance)
  109. NCNetworking.sharedInstance.setup(account: tableAccount.account, delegate: providerExtension as? NCNetworkingDelegate)
  110. foundAccount = true
  111. }
  112. }
  113. return foundAccount
  114. }
  115. // MARK: -
  116. func updateFavoriteForWorkingSet() {
  117. var updateWorkingSet = false
  118. let oldListFavoriteIdentifierRank = listFavoriteIdentifierRank
  119. listFavoriteIdentifierRank = NCManageDatabase.sharedInstance.getTableMetadatasDirectoryFavoriteIdentifierRank(account: account)
  120. // (ADD)
  121. for (identifier, _) in listFavoriteIdentifierRank {
  122. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", identifier)) else { continue }
  123. guard let parentItemIdentifier = fileProviderUtility.sharedInstance.getParentItemIdentifier(metadata: metadata, homeServerUrl: homeServerUrl) else { continue }
  124. let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
  125. fileProviderSignalUpdateWorkingSetItem[item.itemIdentifier] = item
  126. updateWorkingSet = true
  127. }
  128. // (REMOVE)
  129. for (identifier, _) in oldListFavoriteIdentifierRank {
  130. if !listFavoriteIdentifierRank.keys.contains(identifier) {
  131. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", identifier)) else { continue }
  132. let itemIdentifier = fileProviderUtility.sharedInstance.getItemIdentifier(metadata: metadata)
  133. fileProviderSignalDeleteWorkingSetItemIdentifier[itemIdentifier] = itemIdentifier
  134. updateWorkingSet = true
  135. }
  136. }
  137. if updateWorkingSet {
  138. signalEnumerator(for: [.workingSet])
  139. }
  140. }
  141. // MARK: -
  142. // Convinent method to signal the enumeration for containers.
  143. //
  144. func signalEnumerator(for containerItemIdentifiers: [NSFileProviderItemIdentifier]) {
  145. currentAnchor += 1
  146. for containerItemIdentifier in containerItemIdentifiers {
  147. NSFileProviderManager.default.signalEnumerator(for: containerItemIdentifier) { error in
  148. if let error = error {
  149. print("SignalEnumerator for \(containerItemIdentifier) returned error: \(error)")
  150. }
  151. }
  152. }
  153. }
  154. }