NCService.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. //MARK: -
  31. //MARK: middlewarePing
  32. @objc func middlewarePing() {
  33. if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
  34. return;
  35. }
  36. guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
  37. return
  38. }
  39. metadataNet.action = actionMiddlewarePing
  40. metadataNet.serverUrl = NCBrandOptions.sharedInstance.middlewarePingUrl
  41. //appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  42. }
  43. //MARK: -
  44. //MARK: requestServerCapabilities
  45. func getCapabilitiesOfServerSuccessFailure(_ metadataNet: CCMetadataNet!, capabilities: OCCapabilities?, message: String?, errorCode: Int) {
  46. // Check Active Account
  47. if (metadataNet.account != appDelegate.activeAccount) {
  48. return;
  49. }
  50. if (errorCode == 0) {
  51. // Update capabilities db
  52. NCManageDatabase.sharedInstance.addCapabilities(capabilities!)
  53. // ------ THEMING -----------------------------------------------------------------------
  54. // Download Theming Background & Change Theming color
  55. DispatchQueue.global().async {
  56. if (NCBrandOptions.sharedInstance.use_themingBackground) {
  57. }
  58. }
  59. // ------ SEARCH ------------------------------------------------------------------------
  60. if (NCManageDatabase.sharedInstance.getServerVersion() != capabilities!.versionMajor && appDelegate.activeMain != nil) {
  61. appDelegate.activeMain.cancelSearchBar()
  62. }
  63. // ------ GET OTHER SERVICE -------------------------------------------------------------
  64. } else {
  65. // Unauthorized
  66. if (errorCode == kOCErrorServerUnauthorized) {
  67. appDelegate.openLoginView(self, loginType: loginModifyPasswordUser)
  68. }
  69. let error = "Get Capabilities failure error \(errorCode) \(message!)"
  70. print("[LOG] \(error)")
  71. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Capabilities of Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  72. // Change Theming color
  73. appDelegate.settingThemingColorBrand()
  74. }
  75. }
  76. @objc func requestServerCapabilities() {
  77. if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
  78. return;
  79. }
  80. guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
  81. return
  82. }
  83. metadataNet.action = actionGetCapabilities;
  84. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  85. }
  86. //MARK: -
  87. //MARK: requestServerCapabilities
  88. @objc func getUserProfileSuccessFailure(_ metadataNet: CCMetadataNet!, userProfile: OCUserProfile?, message: String?, errorCode: Int) {
  89. // Check Active Account
  90. if (metadataNet.account != appDelegate.activeAccount) {
  91. return;
  92. }
  93. if (errorCode == 0) {
  94. // Update User (+ userProfile.id) & active account & account network
  95. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!) else {
  96. appDelegate.messageNotification("Accopunt", description: "Internal error : account not found on DB", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  97. return
  98. }
  99. CCNetworking.shared().settingAccount()
  100. appDelegate.settingActiveAccount(tableAccount.account, activeUrl: tableAccount.url, activeUser: tableAccount.user, activeUserID: tableAccount.userID, activePassword: tableAccount.password)
  101. // Call func thath required the userdID
  102. appDelegate.activePhotos.readPhotoVideo()
  103. appDelegate.activeFavorites.readListingFavorites()
  104. DispatchQueue.global(qos: .default).async {
  105. guard let imageData = try? Data(contentsOf: URL(string: "\(self.appDelegate.activeUrl)/index.php/avatar/\(self.appDelegate.activeUser)/128")!) else {
  106. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  107. return
  108. }
  109. guard let avatar = UIImage(data: imageData) else {
  110. try? FileManager.default.removeItem(atPath: "\(self.appDelegate.directoryUser)/avatar.png")
  111. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  112. return
  113. }
  114. if let data = UIImagePNGRepresentation(avatar) {
  115. try? data.write(to: URL(string:"\(self.appDelegate.directoryUser)/avatar.png")!)
  116. }
  117. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
  118. }
  119. } else {
  120. let error = "Get user profile failure error \(errorCode) \(message!)"
  121. print("[LOG] \(error)")
  122. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get user profile Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  123. }
  124. }
  125. //MARK: -
  126. //MARK: Delegate : Login
  127. func loginSuccess(_ loginType: Int) {
  128. // go to home sweet home
  129. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil)
  130. }
  131. func loginClose() {
  132. appDelegate.activeLogin = nil;
  133. }
  134. func loginWebClose() {
  135. appDelegate.activeLoginWeb = nil;
  136. }
  137. }