NCNetworkingNotificationCenter.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // NCNetworkingNotificationCenter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/04/2020.
  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 Foundation
  24. @objc class NCNetworkingNotificationCenter: NSObject {
  25. @objc public static let shared: NCNetworkingNotificationCenter = {
  26. let instance = NCNetworkingNotificationCenter()
  27. NotificationCenter.default.addObserver(instance, selector: #selector(downloadFileStart(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadFileStart), object: nil)
  28. NotificationCenter.default.addObserver(instance, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  29. NotificationCenter.default.addObserver(instance, selector: #selector(uploadFileStart(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadFileStart), object: nil)
  30. NotificationCenter.default.addObserver(instance, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  31. return instance
  32. }()
  33. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  34. //MARK: - Download
  35. @objc func downloadFileStart(_ notification: NSNotification) {
  36. if let userInfo = notification.userInfo as NSDictionary? {
  37. if let ocId = userInfo["ocId"] as? String, let serverUrl = userInfo["serverUrl"] as? String, let _ = userInfo["task"] as? URLSessionDownloadTask {
  38. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, ocId: ocId, action: Int32(k_action_MOD))
  39. appDelegate.updateApplicationIconBadgeNumber()
  40. }
  41. }
  42. }
  43. @objc func downloadedFile(_ notification: NSNotification) {
  44. if let userInfo = notification.userInfo as NSDictionary? {
  45. 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 {
  46. if metadata.account != appDelegate.activeAccount { return }
  47. if errorCode == 0 {
  48. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: metadata.serverUrl, ocId: metadata.ocId, action: Int32(k_action_MOD))
  49. // Synchronized
  50. if selector == selectorDownloadSynchronize {
  51. appDelegate.updateApplicationIconBadgeNumber()
  52. appDelegate.startLoadAutoDownloadUpload()
  53. return
  54. }
  55. // open View File
  56. if (selector == selectorLoadFileView || selector == selectorLoadFileViewFavorite || selector == selectorLoadFileInternalView) && UIApplication.shared.applicationState == UIApplication.State.active {
  57. var uti = CCUtility.insertTypeFileIconName(metadata.fileNameView, metadata: metadata)
  58. if uti == nil {
  59. uti = ""
  60. } else if uti!.contains("opendocument") && !NCUtility.sharedInstance.isRichDocument(metadata) {
  61. metadata.typeFile = k_metadataTypeFile_unknown
  62. }
  63. #if HC
  64. if metadata.typeFile == k_metadataTypeFile_imagemeter {
  65. if !IMUtility.shared.IMUnzip(metadata: metadata) {
  66. NCContentPresenter.shared.messageNotification("_error_", description: "Bundle imagemeter error. 🤷‍♂️", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: 0)
  67. return
  68. }
  69. let storyboard = UIStoryboard(name: "IMImagemeter", bundle: nil)
  70. let imagemeterViewer = storyboard.instantiateInitialViewController() as! IMImagemeterViewer
  71. imagemeterViewer.metadata = metadata
  72. imagemeterViewer.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  73. imagemeterViewer.imagemeterViewerDelegate = self
  74. self.appDelegate.window.rootViewController?.present(imagemeterViewer, animated: true, completion: nil)
  75. return
  76. }
  77. #else
  78. if metadata.typeFile == k_metadataTypeFile_imagemeter {
  79. NCMainCommon.sharedInstance.openIn(metadata: metadata, selector: selector)
  80. return
  81. }
  82. #endif
  83. if metadata.typeFile == k_metadataTypeFile_compress || metadata.typeFile == k_metadataTypeFile_unknown {
  84. NCMainCommon.sharedInstance.openIn(metadata: metadata, selector: selector)
  85. } else {
  86. if appDelegate.activeMain.view.window != nil {
  87. appDelegate.activeMain.shouldPerformSegue(metadata, selector: selector)
  88. } else if appDelegate.activeFavorites.view.window != nil {
  89. appDelegate.activeFavorites.shouldPerformSegue(metadata, selector: selector)
  90. }
  91. }
  92. }
  93. // Open in...
  94. if (selector == selectorOpenIn || selector == selectorOpenInDetail) && UIApplication.shared.applicationState == UIApplication.State.active {
  95. NCMainCommon.sharedInstance.openIn(metadata: metadata, selector: selector)
  96. }
  97. // Save to Photo Album
  98. if selector == selectorSave {
  99. appDelegate.activeMain.save(toPhotoAlbum: metadata)
  100. }
  101. // Copy File
  102. if selector == selectorLoadCopy {
  103. appDelegate.activeMain.copyFile(toPasteboard: metadata)
  104. }
  105. // Set as available offline
  106. if selector == selectorLoadOffline {
  107. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: true)
  108. }
  109. appDelegate.startLoadAutoDownloadUpload()
  110. } else {
  111. // File do not exists on server, remove in local
  112. if (errorCode == kOCErrorServerPathNotFound || errorCode == -1011) { // - 1011 = kCFURLErrorBadServerResponse
  113. do {
  114. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId))
  115. } catch { }
  116. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  117. NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  118. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: metadata.serverUrl, ocId: metadata.ocId, action: Int32(k_action_DEL))
  119. } else {
  120. NCContentPresenter.shared.messageNotification("_download_file_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  121. }
  122. }
  123. appDelegate.startLoadAutoDownloadUpload()
  124. }
  125. }
  126. }
  127. //MARK: - Upload
  128. @objc func uploadFileStart(_ notification: NSNotification) {
  129. if let userInfo = notification.userInfo as NSDictionary? {
  130. if let ocId = userInfo["ocId"] as? String, let serverUrl = userInfo["serverUrl"] as? String, let _ = userInfo["task"] as? URLSessionUploadTask {
  131. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, ocId: ocId, action: Int32(k_action_MOD))
  132. appDelegate.updateApplicationIconBadgeNumber()
  133. }
  134. }
  135. }
  136. @objc func uploadedFile(_ notification: NSNotification) {
  137. if let userInfo = notification.userInfo as NSDictionary? {
  138. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  139. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: metadata.serverUrl, ocId: metadata.ocId, action: Int32(k_action_MOD))
  140. if metadata.account == appDelegate.activeAccount {
  141. if errorCode == 0 {
  142. appDelegate.startLoadAutoDownloadUpload()
  143. } else {
  144. if errorCode != -999 && errorCode != kOCErrorServerUnauthorized && errorDescription != "" {
  145. NCContentPresenter.shared.messageNotification("_upload_file_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }