NCService.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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] \(message!)")
  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: Delegate : Login
  88. func loginSuccess(_ loginType: Int) {
  89. // go to home sweet home
  90. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "initializeMain"), object: nil)
  91. }
  92. func loginClose() {
  93. appDelegate.activeLogin = nil;
  94. }
  95. func loginWebClose() {
  96. appDelegate.activeLoginWeb = nil;
  97. }
  98. }