NCService.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // NCService.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 14/03/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. import SVGKit
  25. import NCCommunication
  26. class NCService: NSObject {
  27. @objc static let shared: NCService = {
  28. let instance = NCService()
  29. return instance
  30. }()
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. //MARK: -
  33. @objc public func startRequestServicesServer() {
  34. if (appDelegate.account == nil || appDelegate.account.count == 0) { return }
  35. self.requestUserProfile()
  36. self.requestServerStatus()
  37. }
  38. //MARK: -
  39. private func requestUserProfile() {
  40. if (appDelegate.account == nil || appDelegate.account.count == 0) { return }
  41. NCCommunication.shared.getUserProfile() { (account, userProfile, errorCode, errorDescription) in
  42. if errorCode == 0 && account == self.appDelegate.account {
  43. // Update User (+ userProfile.id) & active account & account network
  44. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!) else {
  45. NCContentPresenter.shared.messageNotification("Account", description: "Internal error : account not found on DB", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  46. return
  47. }
  48. let user = tableAccount.user
  49. let url = tableAccount.urlBase
  50. let stringUser = CCUtility.getStringUser(user, urlBase: url)!
  51. self.appDelegate.settingAccount(tableAccount.account, urlBase: tableAccount.urlBase, user: tableAccount.user, userID: tableAccount.userID, password: CCUtility.getPassword(tableAccount.account))
  52. // Synchronize favorite ---
  53. var selector = selectorReadFile
  54. if CCUtility.getFavoriteOffline() {
  55. selector = selectorDownloadFile
  56. }
  57. NCNetworking.shared.listingFavoritescompletion(selector: selector) { (_, _, _, _) in }
  58. // Synchronize Offline Directory ---
  59. if let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", tableAccount.account), sorted: "serverUrl", ascending: true) {
  60. for directory: tableDirectory in directories {
  61. guard let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(directory.ocId) else {
  62. continue
  63. }
  64. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: selectorDownloadFile)
  65. }
  66. }
  67. // Synchronize Offline Files ---
  68. let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", tableAccount.account), sorted: "fileName", ascending: true)
  69. for file: tableLocalFile in files {
  70. guard let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(file.ocId) else {
  71. continue
  72. }
  73. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: selectorDownloadFile)
  74. }
  75. let avatarUrl = "\(self.appDelegate.urlBase!)/index.php/avatar/\(self.appDelegate.user!)/\(k_avatar_size)".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
  76. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + stringUser + "-" + self.appDelegate.user + ".png"
  77. NCCommunication.shared.downloadContent(serverUrl: avatarUrl) { (account, data, errorCode, errorMessage) in
  78. if errorCode == 0 {
  79. DispatchQueue.global().async {
  80. if let image = UIImage(data: data!) {
  81. try? FileManager.default.removeItem(atPath: fileNamePath)
  82. if let data = image.pngData() {
  83. try? data.write(to: URL(fileURLWithPath: fileNamePath))
  84. }
  85. }
  86. }
  87. }
  88. }
  89. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_changeUserProfile)
  90. self.requestServerCapabilities()
  91. } else {
  92. if errorCode == 401 || errorCode == 403 {
  93. NCNetworkingCheckRemoteUser.shared.checkRemoteUser(account: account)
  94. }
  95. }
  96. }
  97. }
  98. private func requestServerStatus() {
  99. NCCommunication.shared.getServerStatus(serverUrl: appDelegate.urlBase) { (serverProductName, serverVersion, versionMajor, versionMinor, versionMicro, extendedSupport, errorCode, errorMessage) in
  100. if errorCode == 0 {
  101. if extendedSupport == false {
  102. if serverProductName == "owncloud" {
  103. NCContentPresenter.shared.messageNotification("_warning_", description: "_warning_owncloud_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_dismissAfterSecondLong))
  104. } else if versionMajor <= k_nextcloud_unsupported {
  105. NCContentPresenter.shared.messageNotification("_warning_", description: "_warning_unsupported_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_dismissAfterSecondLong))
  106. }
  107. }
  108. }
  109. }
  110. }
  111. private func requestServerCapabilities() {
  112. if (appDelegate.account == nil || appDelegate.account.count == 0) { return }
  113. NCCommunication.shared.getCapabilities() { (account, data, errorCode, errorDescription) in
  114. if errorCode == 0 && data != nil {
  115. NCManageDatabase.sharedInstance.addCapabilitiesJSon(data!, account: account)
  116. // Setup communication
  117. self.appDelegate.settingSetupCommunication(account)
  118. // Theming
  119. NCBrandColor.sharedInstance.settingThemingColor()
  120. // File Sharing
  121. let isFilesSharingEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  122. if isFilesSharingEnabled {
  123. NCCommunication.shared.readShares { (account, shares, errorCode, ErrorDescription) in
  124. if errorCode == 0 {
  125. NCManageDatabase.sharedInstance.deleteTableShare(account: account)
  126. if shares != nil {
  127. NCManageDatabase.sharedInstance.addShare(urlBase: self.appDelegate.urlBase, account: account, shares: shares!)
  128. }
  129. self.appDelegate.shares = NCManageDatabase.sharedInstance.getTableShares(account: account)
  130. } else {
  131. NCContentPresenter.shared.messageNotification("_share_", description: ErrorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  132. }
  133. }
  134. }
  135. // NCTextObtainEditorDetails
  136. let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  137. if serverVersionMajor >= k_nextcloud_version_18_0 {
  138. NCCommunication.shared.NCTextObtainEditorDetails() { (account, editors, creators, errorCode, errorMessage) in
  139. if errorCode == 0 && account == self.appDelegate.account {
  140. NCManageDatabase.sharedInstance.addDirectEditing(account: account, editors: editors, creators: creators)
  141. }
  142. }
  143. }
  144. let isExternalSitesServerEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesExternalSitesExists, exists: true)
  145. if (isExternalSitesServerEnabled) {
  146. NCCommunication.shared.getExternalSite() { (account, externalSites, errorCode, errorDescription) in
  147. if errorCode == 0 && account == self.appDelegate.account {
  148. NCManageDatabase.sharedInstance.deleteExternalSites(account: account)
  149. for externalSite in externalSites {
  150. NCManageDatabase.sharedInstance.addExternalSites(externalSite, account: account)
  151. }
  152. }
  153. }
  154. } else {
  155. NCManageDatabase.sharedInstance.deleteExternalSites(account: account)
  156. }
  157. // Handwerkcloud
  158. let isHandwerkcloudEnabled = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesHWCEnabled, exists: false)
  159. if (isHandwerkcloudEnabled) {
  160. self.requestHC()
  161. }
  162. } else if errorCode != 0 {
  163. NCBrandColor.sharedInstance.settingThemingColor()
  164. if errorCode == 401 || errorCode == 403 {
  165. NCNetworkingCheckRemoteUser.shared.checkRemoteUser(account: account)
  166. }
  167. } else {
  168. NCBrandColor.sharedInstance.settingThemingColor()
  169. }
  170. }
  171. }
  172. //MARK: - Thirt Part
  173. private func requestHC() {
  174. }
  175. }