FileProviderDomain.swift 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // FileProviderDomain.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/06/2019.
  6. // Copyright © 2019 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. class FileProviderDomain: NSObject {
  25. func registerDomains() {
  26. NSFileProviderManager.getDomainsWithCompletionHandler { fileProviderDomain, error in
  27. var domains: [String] = []
  28. let pathRelativeToDocumentStorage = NSFileProviderManager.default.documentStorageURL.absoluteString
  29. let tableAccounts = NCManageDatabase.shared.getAllTableAccount()
  30. for domain in fileProviderDomain {
  31. domains.append(domain.identifier.rawValue)
  32. }
  33. // Delete
  34. for domain in domains {
  35. var domainFound = false
  36. for tableAccount in tableAccounts {
  37. guard let urlBase = NSURL(string: tableAccount.urlBase) else { continue }
  38. guard let host = urlBase.host else { continue }
  39. let accountDomain = tableAccount.userId + " (" + host + ")"
  40. if domain == accountDomain {
  41. domainFound = true
  42. break
  43. }
  44. }
  45. if !domainFound {
  46. let fileProviderDomain = NSFileProviderDomain(identifier: NSFileProviderDomainIdentifier(rawValue: domain), displayName: domain, pathRelativeToDocumentStorage: pathRelativeToDocumentStorage)
  47. NSFileProviderManager.remove(fileProviderDomain, completionHandler: { error in
  48. if let error {
  49. print("Error domain: \(fileProviderDomain) error: \(String(describing: error))")
  50. }
  51. })
  52. }
  53. }
  54. // Add
  55. for tableAccount in tableAccounts {
  56. var domainFound = false
  57. guard let urlBase = NSURL(string: tableAccount.urlBase) else { continue }
  58. guard let host = urlBase.host else { continue }
  59. let accountDomain = tableAccount.userId + " (" + host + ")"
  60. for domain in domains {
  61. if domain == accountDomain {
  62. domainFound = true
  63. break
  64. }
  65. }
  66. if !domainFound {
  67. let fileProviderDomain = NSFileProviderDomain(identifier: NSFileProviderDomainIdentifier(rawValue: accountDomain), displayName: accountDomain, pathRelativeToDocumentStorage: pathRelativeToDocumentStorage)
  68. NSFileProviderManager.add(fileProviderDomain, completionHandler: { error in
  69. if let error {
  70. print("Error domain: \(fileProviderDomain) error: \(String(describing: error))")
  71. }
  72. })
  73. }
  74. }
  75. }
  76. }
  77. }