NCNetworkingNotificationCenter.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // NCNetworkingNotificationCenter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/04/2020.
  6. // Copyright © 2020 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 Foundation
  24. @objc class NCNetworkingNotificationCenter: NSObject, UIDocumentInteractionControllerDelegate {
  25. @objc public static let shared: NCNetworkingNotificationCenter = {
  26. let instance = NCNetworkingNotificationCenter()
  27. NotificationCenter.default.addObserver(instance, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  28. NotificationCenter.default.addObserver(instance, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  29. return instance
  30. }()
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. var viewerQuickLook: NCViewerQuickLook?
  33. var docController: UIDocumentInteractionController?
  34. //MARK: - Download
  35. @objc func downloadedFile(_ notification: NSNotification) {
  36. if let userInfo = notification.userInfo as NSDictionary? {
  37. if let metadata = userInfo["metadata"] as? tableMetadata, let selector = userInfo["selector"] as? String, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  38. if metadata.account != appDelegate.account { return }
  39. if errorCode == 0 {
  40. let fileURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  41. switch selector {
  42. case selectorLoadFileQuickLook:
  43. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  44. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  45. viewerQuickLook = NCViewerQuickLook.init()
  46. viewerQuickLook?.quickLook(url: URL(fileURLWithPath: fileNamePath), viewController: appDelegate.activeMain)
  47. case selectorLoadFileView:
  48. if UIApplication.shared.applicationState == UIApplication.State.active {
  49. if metadata.contentType.contains("opendocument") && !NCUtility.shared.isRichDocument(metadata) {
  50. openIn(fileURL: fileURL, selector: selector)
  51. } else if metadata.typeFile == k_metadataTypeFile_compress || metadata.typeFile == k_metadataTypeFile_unknown {
  52. openIn(fileURL: fileURL, selector: selector)
  53. } else if metadata.typeFile == k_metadataTypeFile_imagemeter {
  54. openIn(fileURL: fileURL, selector: selector)
  55. } else {
  56. if self.appDelegate.activeViewController is CCMain {
  57. (self.appDelegate.activeViewController as! CCMain).shouldPerformSegue(metadata, selector: "")
  58. } else if self.appDelegate.activeViewController is NCFavorite {
  59. (self.appDelegate.activeViewController as! NCFavorite).segue(metadata: metadata)
  60. } else if self.appDelegate.activeViewController is NCOffline {
  61. (self.appDelegate.activeViewController as! NCOffline).segue(metadata: metadata)
  62. }
  63. }
  64. }
  65. case selectorOpenIn, selectorOpenInDetail:
  66. if UIApplication.shared.applicationState == UIApplication.State.active {
  67. openIn(fileURL: fileURL, selector: selector)
  68. }
  69. case selectorSaveAlbum:
  70. appDelegate.activeMain.save(toPhotoAlbum: metadata)
  71. case selectorLoadCopy:
  72. var items = UIPasteboard.general.items
  73. do {
  74. let etagPasteboard = try NSKeyedArchiver.archivedData(withRootObject: metadata.ocId, requiringSecureCoding: false)
  75. items.append([k_metadataKeyedUnarchiver:etagPasteboard])
  76. } catch {
  77. print("error")
  78. }
  79. UIPasteboard.general.setItems(items, options: [:])
  80. case selectorLoadOffline:
  81. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: true)
  82. default:
  83. break
  84. }
  85. } else {
  86. // File do not exists on server, remove in local
  87. if (errorCode == 404 || errorCode == -1011) { // - 1011 = kCFURLErrorBadServerResponse
  88. do {
  89. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId))
  90. } catch { }
  91. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  92. NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  93. } else {
  94. NCContentPresenter.shared.messageNotification("_download_file_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  95. }
  96. }
  97. }
  98. }
  99. }
  100. func openIn(fileURL: URL, selector: String?) {
  101. docController = UIDocumentInteractionController(url: fileURL)
  102. docController?.delegate = self
  103. if selector == selectorOpenInDetail {
  104. guard let barButtonItem = appDelegate.activeDetail.navigationItem.rightBarButtonItem else { return }
  105. guard let buttonItemView = barButtonItem.value(forKey: "view") as? UIView else { return }
  106. docController?.presentOptionsMenu(from: buttonItemView.frame, in: buttonItemView, animated: true)
  107. } else {
  108. guard let splitViewController = appDelegate.window?.rootViewController as? UISplitViewController, let view = splitViewController.viewControllers.first?.view, let frame = splitViewController.viewControllers.first?.view.frame else {
  109. return }
  110. docController?.presentOptionsMenu(from: frame, in: view, animated: true)
  111. }
  112. }
  113. @objc func openShare(ViewController: UIViewController, metadata: tableMetadata, indexPage: Int) {
  114. let shareNavigationController = UIStoryboard(name: "NCShare", bundle: nil).instantiateInitialViewController() as! UINavigationController
  115. let shareViewController = shareNavigationController.topViewController as! NCSharePaging
  116. shareViewController.metadata = metadata
  117. shareViewController.indexPage = indexPage
  118. shareNavigationController.modalPresentationStyle = .formSheet
  119. ViewController.present(shareNavigationController, animated: true, completion: nil)
  120. }
  121. @objc func downloadOpen(metadata: tableMetadata, selector: String) {
  122. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  123. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_downloadedFile, userInfo: ["metadata": metadata, "selector": selector, "errorCode": 0, "errorDescription": "" ])
  124. } else {
  125. NCNetworking.shared.download(metadata: metadata, selector: selector) { (_) in }
  126. }
  127. }
  128. //MARK: - Upload
  129. @objc func uploadedFile(_ notification: NSNotification) {
  130. if let userInfo = notification.userInfo as NSDictionary? {
  131. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  132. if metadata.account == appDelegate.account {
  133. if errorCode != 0 {
  134. if errorCode != -999 && errorCode != 401 && errorDescription != "" {
  135. NCContentPresenter.shared.messageNotification("_upload_file_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }