NCService.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. //MARK: -
  139. //MARK: Delegate : Login
  140. func loginSuccess(_ loginType: Int) {
  141. // go to home sweet home
  142. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil)
  143. }
  144. func loginClose() {
  145. appDelegate.activeLogin = nil;
  146. }
  147. func loginWebClose() {
  148. appDelegate.activeLoginWeb = nil;
  149. }
  150. }