NCService.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // NCService.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 14/03/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 Foundation
  24. class NCService: NSObject, OCNetworkingDelegate, CCLoginDelegate, CCLoginDelegateWeb {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. @objc static let sharedInstance: NCService = {
  27. let instance = NCService()
  28. return instance
  29. }()
  30. @objc func middlewarePing() {
  31. if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
  32. return;
  33. }
  34. guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
  35. return
  36. }
  37. metadataNet.action = actionMiddlewarePing
  38. metadataNet.serverUrl = NCBrandOptions.sharedInstance.middlewarePingUrl
  39. //appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  40. }
  41. func getCapabilitiesOfServerSuccessFailure(_ metadataNet: CCMetadataNet!, capabilities: OCCapabilities?, message: String?, errorCode: Int) {
  42. // Check Active Account
  43. if (metadataNet.account != appDelegate.activeAccount) {
  44. return;
  45. }
  46. if (errorCode == 0) {
  47. // Update capabilities db
  48. NCManageDatabase.sharedInstance.addCapabilities(capabilities!)
  49. // ------ THEMING -----------------------------------------------------------------------
  50. // Download Theming Background & Change Theming color
  51. DispatchQueue.global().async {
  52. if (NCBrandOptions.sharedInstance.use_themingBackground) {
  53. }
  54. }
  55. // ------ SEARCH ------------------------------------------------------------------------
  56. if (NCManageDatabase.sharedInstance.getServerVersion() != capabilities!.versionMajor && appDelegate.activeMain != nil) {
  57. appDelegate.activeMain.cancelSearchBar()
  58. }
  59. // ------ GET OTHER SERVICE -------------------------------------------------------------
  60. } else {
  61. // Unauthorized
  62. if (errorCode == kOCErrorServerUnauthorized) {
  63. appDelegate.openLoginView(self, loginType: loginModifyPasswordUser)
  64. }
  65. let error = "Get Capabilities failure error \(errorCode) \(message!)"
  66. print("[LOG] \(error)")
  67. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Capabilities of Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  68. // Change Theming color
  69. appDelegate.settingThemingColorBrand()
  70. }
  71. }
  72. @objc func requestServerCapabilities() {
  73. if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
  74. return;
  75. }
  76. guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
  77. return
  78. }
  79. metadataNet.action = actionGetCapabilities;
  80. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  81. }
  82. @objc func getUserProfileSuccessFailure(_ metadataNet: CCMetadataNet!, userProfile: OCUserProfile?, message: String?, errorCode: Int) {
  83. // Check Active Account
  84. if (metadataNet.account != appDelegate.activeAccount) {
  85. return;
  86. }
  87. if (errorCode == 0) {
  88. // Update User (+ userProfile.id) & active account & account network
  89. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!) else {
  90. appDelegate.messageNotification("Accopunt", description: "Internal error : account not found on DB", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  91. return
  92. }
  93. CCNetworking.shared().settingAccount()
  94. appDelegate.settingActiveAccount(tableAccount.account, activeUrl: tableAccount.url, activeUser: tableAccount.user, activeUserID: tableAccount.userID, activePassword: tableAccount.password)
  95. // Call func thath required the userdID
  96. appDelegate.activePhotos.readPhotoVideo()
  97. appDelegate.activeFavorites.readListingFavorites()
  98. DispatchQueue.global(qos: .default).async {
  99. let address = "\(self.appDelegate.activeUrl!)/index.php/avatar/\(self.appDelegate.activeUser!)/128"
  100. guard let imageData = try? Data(contentsOf: URL(string: address)!) else {
  101. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  102. return
  103. }
  104. guard let avatar = UIImage(data: imageData) else {
  105. let fileName = "\(self.appDelegate.directoryUser!)/avatar.png"
  106. try? FileManager.default.removeItem(atPath: fileName)
  107. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  108. return
  109. }
  110. if let data = UIImagePNGRepresentation(avatar) {
  111. let fileName = "\(self.appDelegate.directoryUser!)/avatar.png"
  112. try? data.write(to: URL(fileURLWithPath: fileName))
  113. }
  114. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  115. }
  116. } else {
  117. let error = "Get user profile failure error \(errorCode) \(message!)"
  118. print("[LOG] \(error)")
  119. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get user profile Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  120. }
  121. }
  122. @objc func getExternalSitesServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfExternalSites: [Any]?, message: String?, errorCode: Int) {
  123. // Check Active Account
  124. if (metadataNet.account != appDelegate.activeAccount) {
  125. return;
  126. }
  127. if (errorCode == 0) {
  128. NCManageDatabase.sharedInstance.deleteExternalSites()
  129. for externalSites in listOfExternalSites! {
  130. NCManageDatabase.sharedInstance.addExternalSites(externalSites as! OCExternalSites)
  131. }
  132. } else {
  133. let error = "Get external site failure error \(errorCode) \(message!)"
  134. print("[LOG] \(error)")
  135. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get external site Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  136. }
  137. }
  138. @objc func getActivityServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfActivity: [Any]?, message: String?, errorCode: Int) {
  139. // Check Active Account
  140. if (metadataNet.account != appDelegate.activeAccount) {
  141. return;
  142. }
  143. if (errorCode == 0) {
  144. NCManageDatabase.sharedInstance.addActivityServer(listOfActivity as! [OCActivity])
  145. if (appDelegate.activeActivity != nil) {
  146. appDelegate.activeActivity.reloadDatasource()
  147. }
  148. } else {
  149. let error = "Get Activity Server failure error \(errorCode) \(message!)"
  150. print("[LOG] \(error)")
  151. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Activity Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  152. }
  153. }
  154. @objc func getNotificationServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfNotifications: [Any]?, message: String?, errorCode: Int) {
  155. // Check Active Account
  156. if (metadataNet.account != appDelegate.activeAccount) {
  157. return;
  158. }
  159. if (errorCode == 0) {
  160. DispatchQueue.global(qos: .default).async {
  161. let sortedListOfNotifications = (listOfNotifications! as NSArray).sortedArray(using: [
  162. NSSortDescriptor(key: "date", ascending: false)
  163. ])
  164. var old = ""
  165. var new = ""
  166. for notification in listOfNotifications! {
  167. let id = (notification as AnyObject).idNotification!
  168. new = new + String(describing: id)
  169. }
  170. for notification in self.appDelegate.listOfNotifications! {
  171. let id = (notification as AnyObject).idNotification!
  172. old = old + String(describing: id)
  173. }
  174. DispatchQueue.main.async {
  175. if (new != old) {
  176. self.appDelegate.listOfNotifications = NSMutableArray.init(array: sortedListOfNotifications)
  177. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationReloadData"), object: nil)
  178. // Update Main NavigationBar
  179. if (self.appDelegate.activeMain.isSelectedMode == false) {
  180. self.appDelegate.activeMain.setUINavigationBarDefault()
  181. }
  182. }
  183. }
  184. }
  185. } else {
  186. let error = "Get Notification Server failure error \(errorCode) \(message!)"
  187. print("[LOG] \(error)")
  188. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Notification Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  189. // Update Main NavigationBar
  190. if (appDelegate.activeMain.isSelectedMode == false) {
  191. appDelegate.activeMain.setUINavigationBarDefault()
  192. }
  193. }
  194. }
  195. //MARK: -
  196. //MARK: Delegate : Login
  197. func loginSuccess(_ loginType: Int) {
  198. // go to home sweet home
  199. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil)
  200. }
  201. func loginClose() {
  202. appDelegate.activeLogin = nil;
  203. }
  204. func loginWebClose() {
  205. appDelegate.activeLoginWeb = nil;
  206. }
  207. }