NCService.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. @objc static let sharedInstance: NCService = {
  27. let instance = NCService()
  28. return instance
  29. }()
  30. //MARK: -
  31. //MARK: Start Services API NC
  32. @objc func startRequestServicesServer() {
  33. if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
  34. return
  35. }
  36. self.requestUserProfile()
  37. self.requestServerCapabilities()
  38. self.requestActivityServer()
  39. }
  40. //MARK: -
  41. //MARK: Request Service API NC
  42. @objc func requestServerCapabilities() {
  43. if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
  44. return
  45. }
  46. guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
  47. return
  48. }
  49. metadataNet.action = actionGetCapabilities
  50. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  51. }
  52. @objc func requestUserProfile() {
  53. if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
  54. return
  55. }
  56. guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
  57. return
  58. }
  59. metadataNet.action = actionGetUserProfile
  60. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  61. }
  62. @objc func requestActivityServer() {
  63. if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
  64. return
  65. }
  66. guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
  67. return
  68. }
  69. metadataNet.action = actionGetActivityServer
  70. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  71. }
  72. @objc func middlewarePing() {
  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 = actionMiddlewarePing
  80. metadataNet.serverUrl = NCBrandOptions.sharedInstance.middlewarePingUrl
  81. //appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  82. }
  83. //MARK: -
  84. //MARK: Delegate Service API NC
  85. func getCapabilitiesOfServerSuccessFailure(_ metadataNet: CCMetadataNet!, capabilities: OCCapabilities?, message: String?, errorCode: Int) {
  86. // Check Active Account
  87. if (metadataNet.account != appDelegate.activeAccount) {
  88. return
  89. }
  90. if (errorCode == 0) {
  91. // Update capabilities db
  92. NCManageDatabase.sharedInstance.addCapabilities(capabilities!)
  93. // ------ THEMING -----------------------------------------------------------------------
  94. if (NCBrandOptions.sharedInstance.use_themingBackground && capabilities!.themingBackground != "") {
  95. // Download Theming Background & Change Theming color
  96. DispatchQueue.global().async {
  97. let address = capabilities!.themingBackground!.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
  98. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(self.appDelegate.activeUser, activeUrl: self.appDelegate.activeUrl) + "-themingBackground.png"
  99. guard let imageData = try? Data(contentsOf: URL(string: address)!) else {
  100. DispatchQueue.main.async {
  101. self.appDelegate.settingThemingColorBrand()
  102. }
  103. return
  104. }
  105. DispatchQueue.main.async {
  106. guard let image = UIImage(data: imageData) else {
  107. try? FileManager.default.removeItem(atPath: fileNamePath)
  108. self.appDelegate.settingThemingColorBrand()
  109. return
  110. }
  111. if let data = UIImagePNGRepresentation(image) {
  112. try? data.write(to: URL(fileURLWithPath: fileNamePath))
  113. }
  114. self.appDelegate.settingThemingColorBrand()
  115. }
  116. }
  117. } else {
  118. self.appDelegate.settingThemingColorBrand()
  119. }
  120. // ------ SEARCH ------------------------------------------------------------------------
  121. if (NCManageDatabase.sharedInstance.getServerVersion() != capabilities!.versionMajor && appDelegate.activeMain != nil) {
  122. appDelegate.activeMain.cancelSearchBar()
  123. }
  124. // ------ GET OTHER SERVICE -------------------------------------------------------------
  125. // Read Notification
  126. if (capabilities!.isNotificationServerEnabled) {
  127. metadataNet.action = actionGetNotificationServer
  128. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  129. } else {
  130. // Remove all Notification
  131. self.appDelegate.listOfNotifications.removeAllObjects()
  132. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationReloadData"), object: nil)
  133. // Update Main NavigationBar
  134. if (appDelegate.activeMain != nil && self.appDelegate.activeMain.isSelectedMode == false) {
  135. self.appDelegate.activeMain.setUINavigationBarDefault()
  136. }
  137. }
  138. // Read External Sites
  139. if (capabilities!.isExternalSitesServerEnabled) {
  140. metadataNet.action = actionGetExternalSitesServer
  141. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  142. } else {
  143. NCManageDatabase.sharedInstance.deleteExternalSites()
  144. }
  145. // Read Share
  146. if (capabilities!.isFilesSharingAPIEnabled && appDelegate.activeMain != nil) {
  147. appDelegate.sharesID.removeAllObjects()
  148. metadataNet.action = actionReadShareServer
  149. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: appDelegate.activeMain, metadataNet: metadataNet)
  150. }
  151. } else {
  152. // Change Theming color
  153. appDelegate.settingThemingColorBrand()
  154. var error = ""
  155. if let message = message {
  156. error = "Get Capabilities failure error \(errorCode) \(message)"
  157. } else {
  158. error = "Get Capabilities failure error \(errorCode)"
  159. }
  160. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Capabilities of Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  161. }
  162. }
  163. @objc func getUserProfileSuccessFailure(_ metadataNet: CCMetadataNet!, userProfile: OCUserProfile?, message: String?, errorCode: Int) {
  164. // Check Active Account
  165. if (metadataNet.account != appDelegate.activeAccount) {
  166. return
  167. }
  168. if (errorCode == 0) {
  169. // Update User (+ userProfile.id) & active account & account network
  170. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!) else {
  171. appDelegate.messageNotification("Accopunt", description: "Internal error : account not found on DB", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  172. return
  173. }
  174. let user = tableAccount.user
  175. let url = tableAccount.url
  176. CCNetworking.shared().settingAccount()
  177. appDelegate.settingActiveAccount(tableAccount.account, activeUrl: tableAccount.url, activeUser: tableAccount.user, activeUserID: tableAccount.userID, activePassword: tableAccount.password)
  178. // Call func thath required the userdID
  179. appDelegate.activeFavorites.listingFavorites()
  180. appDelegate.activePhotos.searchPhotoVideo()
  181. DispatchQueue.global(qos: .default).async {
  182. let address = "\(self.appDelegate.activeUrl!)/index.php/avatar/\(self.appDelegate.activeUser!)/128".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
  183. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(user, activeUrl: url) + "-avatar.png"
  184. guard let imageData = try? Data(contentsOf: URL(string: address)!) else {
  185. DispatchQueue.main.async {
  186. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  187. }
  188. return
  189. }
  190. DispatchQueue.main.async {
  191. guard let image = UIImage(data: imageData) else {
  192. try? FileManager.default.removeItem(atPath: fileNamePath)
  193. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  194. return
  195. }
  196. if let data = UIImagePNGRepresentation(image) {
  197. try? data.write(to: URL(fileURLWithPath: fileNamePath))
  198. }
  199. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  200. }
  201. }
  202. } else {
  203. var error = ""
  204. if let message = message {
  205. error = "Get user profile failure error \(errorCode) \(message)"
  206. } else {
  207. error = "Get user profile failure error \(errorCode)"
  208. }
  209. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get user profile Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  210. }
  211. }
  212. @objc func getExternalSitesServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfExternalSites: [Any]?, message: String?, errorCode: Int) {
  213. // Check Active Account
  214. if (metadataNet.account != appDelegate.activeAccount) {
  215. return
  216. }
  217. if (errorCode == 0) {
  218. NCManageDatabase.sharedInstance.deleteExternalSites()
  219. for externalSites in listOfExternalSites! {
  220. NCManageDatabase.sharedInstance.addExternalSites(externalSites as! OCExternalSites)
  221. }
  222. } else {
  223. var error = ""
  224. if let message = message {
  225. error = "Get external site failure error \(errorCode) \(message)"
  226. } else {
  227. error = "Get external site failure error \(errorCode)"
  228. }
  229. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get external site Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  230. }
  231. }
  232. @objc func getActivityServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfActivity: [Any]?, message: String?, errorCode: Int) {
  233. // Check Active Account
  234. if (metadataNet.account != appDelegate.activeAccount) {
  235. return
  236. }
  237. if (errorCode == 0) {
  238. NCManageDatabase.sharedInstance.addActivityServer(listOfActivity as! [OCActivity])
  239. if (appDelegate.activeActivity != nil) {
  240. appDelegate.activeActivity.reloadDatasource()
  241. }
  242. } else {
  243. var error = ""
  244. if let message = message {
  245. error = "Get Activity Server failure error \(errorCode) \(message)"
  246. } else {
  247. error = "Get Activity Server failure error \(errorCode)"
  248. }
  249. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Activity Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  250. }
  251. }
  252. @objc func getNotificationServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfNotifications: [Any]?, message: String?, errorCode: Int) {
  253. // Check Active Account
  254. if (metadataNet.account != appDelegate.activeAccount) {
  255. return
  256. }
  257. if (errorCode == 0) {
  258. DispatchQueue.global(qos: .default).async {
  259. let sortedListOfNotifications = (listOfNotifications! as NSArray).sortedArray(using: [
  260. NSSortDescriptor(key: "date", ascending: false)
  261. ])
  262. var old = ""
  263. var new = ""
  264. for notification in listOfNotifications! {
  265. let id = (notification as AnyObject).idNotification!
  266. new = new + String(describing: id)
  267. }
  268. for notification in self.appDelegate.listOfNotifications! {
  269. let id = (notification as AnyObject).idNotification!
  270. old = old + String(describing: id)
  271. }
  272. DispatchQueue.main.async {
  273. if (new != old) {
  274. self.appDelegate.listOfNotifications = NSMutableArray.init(array: sortedListOfNotifications)
  275. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationReloadData"), object: nil)
  276. // Update Main NavigationBar
  277. if (self.appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
  278. self.appDelegate.activeMain.setUINavigationBarDefault()
  279. }
  280. }
  281. }
  282. }
  283. } else {
  284. var error = ""
  285. if let message = message {
  286. error = "Get Notification Server failure error \(errorCode) \(message)"
  287. } else {
  288. error = "Get Notification Server failure error \(errorCode)"
  289. }
  290. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Notification Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  291. // Update Main NavigationBar
  292. if (appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
  293. appDelegate.activeMain.setUINavigationBarDefault()
  294. }
  295. }
  296. }
  297. }