FileProviderData.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 UIKit
  24. import NextcloudKit
  25. class fileProviderData: NSObject {
  26. static let shared: fileProviderData = {
  27. let instance = fileProviderData()
  28. return instance
  29. }()
  30. var domain: NSFileProviderDomain?
  31. var fileProviderManager: NSFileProviderManager = NSFileProviderManager.default
  32. let utilityFileSystem = NCUtilityFileSystem()
  33. var account = ""
  34. var user = ""
  35. var userId = ""
  36. var accountUrlBase = ""
  37. var homeServerUrl = ""
  38. var listFavoriteIdentifierRank: [String: NSNumber] = [:]
  39. var fileProviderSignalDeleteContainerItemIdentifier: [NSFileProviderItemIdentifier: NSFileProviderItemIdentifier] = [:]
  40. var fileProviderSignalUpdateContainerItem: [NSFileProviderItemIdentifier: FileProviderItem] = [:]
  41. var fileProviderSignalDeleteWorkingSetItemIdentifier: [NSFileProviderItemIdentifier: NSFileProviderItemIdentifier] = [:]
  42. var fileProviderSignalUpdateWorkingSetItem: [NSFileProviderItemIdentifier: FileProviderItem] = [:]
  43. enum FileProviderError: Error {
  44. case downloadError
  45. case uploadError
  46. }
  47. enum TypeSignal: String {
  48. case delete
  49. case update
  50. case workingSet
  51. }
  52. // MARK: -
  53. func setupAccount(domain: NSFileProviderDomain?, providerExtension: NSFileProviderExtension) -> tableAccount? {
  54. self.domain = domain
  55. if let domain, let fileProviderManager = NSFileProviderManager(for: domain) {
  56. self.fileProviderManager = fileProviderManager
  57. }
  58. // LOG
  59. NextcloudKit.shared.nkCommonInstance.pathLog = utilityFileSystem.directoryGroup
  60. let levelLog = NCKeychain().logLevel
  61. NextcloudKit.shared.nkCommonInstance.levelLog = levelLog
  62. let version = NSString(format: NCBrandOptions.shared.textCopyrightNextcloudiOS as NSString, NCUtility().getVersionApp()) as String
  63. NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Start File Provider session with level \(levelLog) " + version + " (File Provider Extension)")
  64. // NO DOMAIN -> Set default account
  65. if domain == nil {
  66. guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else { return nil }
  67. account = activeAccount.account
  68. user = activeAccount.user
  69. userId = activeAccount.userId
  70. accountUrlBase = activeAccount.urlBase
  71. homeServerUrl = utilityFileSystem.getHomeServer(urlBase: activeAccount.urlBase, userId: activeAccount.userId)
  72. NCManageDatabase.shared.setCapabilities(account: account)
  73. NextcloudKit.shared.setup(account: activeAccount.account, user: activeAccount.user, userId: activeAccount.userId, password: NCKeychain().getPassword(account: activeAccount.account), urlBase: activeAccount.urlBase, userAgent: userAgent, nextcloudVersion: NCGlobal.shared.capabilityServerVersionMajor, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup, delegate: NCNetworking.shared)
  74. NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate
  75. return tableAccount.init(value: activeAccount)
  76. }
  77. // DOMAIN
  78. let accounts = NCManageDatabase.shared.getAllAccount()
  79. if accounts.isEmpty { return nil }
  80. for activeAccount in accounts {
  81. guard let url = NSURL(string: activeAccount.urlBase) else { continue }
  82. guard let host = url.host else { continue }
  83. let accountDomain = activeAccount.userId + " (" + host + ")"
  84. if accountDomain == domain!.identifier.rawValue {
  85. account = activeAccount.account
  86. user = activeAccount.user
  87. userId = activeAccount.userId
  88. accountUrlBase = activeAccount.urlBase
  89. homeServerUrl = utilityFileSystem.getHomeServer(urlBase: activeAccount.urlBase, userId: activeAccount.userId)
  90. NCManageDatabase.shared.setCapabilities(account: account)
  91. NextcloudKit.shared.setup(account: activeAccount.account, user: activeAccount.user, userId: activeAccount.userId, password: NCKeychain().getPassword(account: activeAccount.account), urlBase: activeAccount.urlBase, userAgent: userAgent, nextcloudVersion: NCGlobal.shared.capabilityServerVersionMajor, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup, delegate: NCNetworking.shared)
  92. NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate
  93. return tableAccount.init(value: activeAccount)
  94. }
  95. }
  96. return nil
  97. }
  98. // MARK: -
  99. @discardableResult
  100. func signalEnumerator(ocId: String, type: TypeSignal) -> FileProviderItem? {
  101. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId),
  102. let parentItemIdentifier = fileProviderUtility().getParentItemIdentifier(metadata: metadata) else { return nil }
  103. let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
  104. if type == .delete {
  105. fileProviderData.shared.fileProviderSignalDeleteContainerItemIdentifier[item.itemIdentifier] = item.itemIdentifier
  106. fileProviderData.shared.fileProviderSignalDeleteWorkingSetItemIdentifier[item.itemIdentifier] = item.itemIdentifier
  107. }
  108. if type == .update {
  109. fileProviderData.shared.fileProviderSignalUpdateContainerItem[item.itemIdentifier] = item
  110. fileProviderData.shared.fileProviderSignalUpdateWorkingSetItem[item.itemIdentifier] = item
  111. }
  112. if type == .workingSet {
  113. fileProviderData.shared.fileProviderSignalUpdateWorkingSetItem[item.itemIdentifier] = item
  114. }
  115. if type == .delete || type == .update {
  116. fileProviderManager.signalEnumerator(for: parentItemIdentifier) { _ in }
  117. }
  118. fileProviderManager.signalEnumerator(for: .workingSet) { _ in }
  119. return item
  120. }
  121. }