FileProviderEnumeratorFile.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // FileProviderEnumeratorFile.swift
  3. // PickerFileProvider
  4. //
  5. // Created by Marino Faggiana on 30/04/18.
  6. // Copyright © 2018 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 FileProvider
  24. class FileProviderEnumeratorFile: NSObject, NSFileProviderEnumerator {
  25. var enumeratedItemIdentifier: NSFileProviderItemIdentifier
  26. init(enumeratedItemIdentifier: NSFileProviderItemIdentifier) {
  27. self.enumeratedItemIdentifier = enumeratedItemIdentifier
  28. super.init()
  29. }
  30. func invalidate() {
  31. // TODO: perform invalidation of server connection if necessary
  32. }
  33. func enumerateItems(for observer: NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage) {
  34. var items: [NSFileProviderItemProtocol] = []
  35. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, enumeratedItemIdentifier.rawValue)) else {
  36. observer.finishEnumerating(upTo: nil)
  37. return
  38. }
  39. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  40. observer.finishEnumerating(upTo: nil)
  41. return
  42. }
  43. let item = FileProviderItem(metadata: metadata, serverUrl: serverUrl)
  44. items.append(item)
  45. observer.didEnumerate(items)
  46. observer.finishEnumerating(upTo: nil)
  47. /* TODO:
  48. - inspect the page to determine whether this is an initial or a follow-up request
  49. If this is an enumerator for a directory, the root container or all directories:
  50. - perform a server request to fetch directory contents
  51. If this is an enumerator for the active set:
  52. - perform a server request to update your local database
  53. - fetch the active set from your local database
  54. - inform the observer about the items returned by the server (possibly multiple times)
  55. - inform the observer that you are finished with this page
  56. */
  57. }
  58. func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) {
  59. /* TODO:
  60. - query the server for updates since the passed-in sync anchor
  61. If this is an enumerator for the active set:
  62. - note the changes in your local database
  63. - inform the observer about item deletions and updates (modifications + insertions)
  64. - inform the observer when you have finished enumerating up to a subsequent sync anchor
  65. */
  66. }
  67. }