FileProviderData.swift 8.4 KB

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