FileProviderData.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 accountUrlBase = ""
  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 setupAccount(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. let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: tableAccount.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  63. let webDav = NCUtility.shared.getWebDAV(account: tableAccount.account)
  64. account = tableAccount.account
  65. accountUser = tableAccount.user
  66. accountUserID = tableAccount.userID
  67. accountPassword = CCUtility.getPassword(tableAccount.account)
  68. accountUrlBase = tableAccount.urlBase
  69. homeServerUrl = NCUtility.shared.getHomeServer(urlBase: tableAccount.urlBase, account: tableAccount.account)
  70. NCCommunicationCommon.shared.setup(account: account, user: accountUser, userId: accountUserID, password: accountPassword, urlBase: accountUrlBase, userAgent: CCUtility.getUserAgent(), webDav: webDav, dav: nil, nextcloudVersion: serverVersionMajor, delegate: NCNetworking.shared)
  71. NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate
  72. // Background session
  73. NCCommunicationBackground.shared.delegate = NCNetworking.shared
  74. NCCommunicationBackground.shared.setupExtensionSession(capabilitiesGroup: NCBrandOptions.sharedInstance.capabilitiesGroups, allowsCellularAccess: true)
  75. return true
  76. }
  77. let tableAccounts = NCManageDatabase.sharedInstance.getAllAccount()
  78. if tableAccounts.count == 0 { return false }
  79. for tableAccount in tableAccounts {
  80. guard let url = NSURL(string: tableAccount.urlBase) else { continue }
  81. guard let host = url.host else { continue }
  82. let accountDomain = tableAccount.userID + " (" + host + ")"
  83. if accountDomain == domain {
  84. let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: tableAccount.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  85. let webDav = NCUtility.shared.getWebDAV(account: tableAccount.account)
  86. account = tableAccount.account
  87. accountUser = tableAccount.user
  88. accountUserID = tableAccount.userID
  89. guard let password = CCUtility.getPassword(tableAccount.account) else { return false }
  90. accountPassword = password
  91. accountUrlBase = tableAccount.urlBase
  92. homeServerUrl = NCUtility.shared.getHomeServer(urlBase: tableAccount.urlBase, account: tableAccount.account)
  93. NCCommunicationCommon.shared.setup(account: account, user: accountUser, userId: accountUserID, password: accountPassword, urlBase: accountUrlBase, userAgent: CCUtility.getUserAgent(), webDav: webDav, dav: nil, nextcloudVersion: serverVersionMajor, delegate: NCNetworking.shared)
  94. NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate
  95. // Background session
  96. NCCommunicationBackground.shared.delegate = NCNetworking.shared
  97. NCCommunicationBackground.shared.setupExtensionSession(capabilitiesGroup: NCBrandOptions.sharedInstance.capabilitiesGroups, allowsCellularAccess: true)
  98. foundAccount = true
  99. }
  100. }
  101. return foundAccount
  102. }
  103. func setupAccount(itemIdentifier: NSFileProviderItemIdentifier, providerExtension: NSFileProviderExtension) -> Bool {
  104. var foundAccount: Bool = false
  105. guard let accountFromItemIdentifier = fileProviderUtility.sharedInstance.getAccountFromItemIdentifier(itemIdentifier) else { return false }
  106. let tableAccounts = NCManageDatabase.sharedInstance.getAllAccount()
  107. if tableAccounts.count == 0 { return false }
  108. for tableAccount in tableAccounts {
  109. if accountFromItemIdentifier == tableAccount.account {
  110. let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: tableAccount.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  111. let webDav = NCUtility.shared.getWebDAV(account: tableAccount.account)
  112. account = tableAccount.account
  113. accountUser = tableAccount.user
  114. accountUserID = tableAccount.userID
  115. accountPassword = CCUtility.getPassword(tableAccount.account)
  116. accountUrlBase = tableAccount.urlBase
  117. homeServerUrl = NCUtility.shared.getHomeServer(urlBase: tableAccount.urlBase, account: tableAccount.account)
  118. NCCommunicationCommon.shared.setup(account: account, user: accountUser, userId: accountUserID, password: accountPassword, urlBase: accountUrlBase, userAgent: CCUtility.getUserAgent(), webDav: webDav, dav: nil, nextcloudVersion: serverVersionMajor, delegate: NCNetworking.shared)
  119. NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate
  120. // Background session
  121. NCCommunicationBackground.shared.delegate = NCNetworking.shared
  122. NCCommunicationBackground.shared.setupExtensionSession(capabilitiesGroup: NCBrandOptions.sharedInstance.capabilitiesGroups, allowsCellularAccess: true)
  123. foundAccount = true
  124. }
  125. }
  126. return foundAccount
  127. }
  128. // MARK: -
  129. // Convinent method to signal the enumeration for containers.
  130. //
  131. func signalEnumerator(for containerItemIdentifiers: [NSFileProviderItemIdentifier]) {
  132. currentAnchor += 1
  133. for containerItemIdentifier in containerItemIdentifiers {
  134. NSFileProviderManager.default.signalEnumerator(for: containerItemIdentifier) { error in
  135. if let error = error {
  136. print("SignalEnumerator for \(containerItemIdentifier) returned error: \(error)")
  137. }
  138. }
  139. }
  140. }
  141. /*
  142. func updateFavoriteForWorkingSet() {
  143. var updateWorkingSet = false
  144. let oldListFavoriteIdentifierRank = listFavoriteIdentifierRank
  145. listFavoriteIdentifierRank = NCManageDatabase.sharedInstance.getTableMetadatasDirectoryFavoriteIdentifierRank(account: account)
  146. // (ADD)
  147. for (identifier, _) in listFavoriteIdentifierRank {
  148. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", identifier)) else { continue }
  149. guard let parentItemIdentifier = fileProviderUtility.sharedInstance.getParentItemIdentifier(metadata: metadata, homeServerUrl: homeServerUrl) else { continue }
  150. let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
  151. fileProviderSignalUpdateWorkingSetItem[item.itemIdentifier] = item
  152. updateWorkingSet = true
  153. }
  154. // (REMOVE)
  155. for (identifier, _) in oldListFavoriteIdentifierRank {
  156. if !listFavoriteIdentifierRank.keys.contains(identifier) {
  157. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", identifier)) else { continue }
  158. let itemIdentifier = fileProviderUtility.sharedInstance.getItemIdentifier(metadata: metadata)
  159. fileProviderSignalDeleteWorkingSetItemIdentifier[itemIdentifier] = itemIdentifier
  160. updateWorkingSet = true
  161. }
  162. }
  163. if updateWorkingSet {
  164. signalEnumerator(for: [.workingSet])
  165. }
  166. }
  167. */
  168. }