NCService.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. DispatchQueue.global().async {
  44. // Update User (+ userProfile.id) & active account & account network
  45. guard let tableAccount = NCManageDatabase.shared.setAccountUserProfile(userProfile!) else {
  46. NCContentPresenter.shared.messageNotification("Account", description: "Internal error : account not found on DB", delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCBrandGlobal.shared.ErrorInternalError)
  47. return
  48. }
  49. let user = tableAccount.user
  50. let url = tableAccount.urlBase
  51. let stringUser = CCUtility.getStringUser(user, urlBase: url)!
  52. self.appDelegate.settingAccount(tableAccount.account, urlBase: tableAccount.urlBase, user: tableAccount.user, userID: tableAccount.userID, password: CCUtility.getPassword(tableAccount.account))
  53. // Synchronize favorite
  54. NCNetworking.shared.listingFavoritescompletion(selector: NCBrandGlobal.shared.selectorReadFile) { (_, _, _, _) in }
  55. // Synchronize Offline Directory
  56. if let directories = NCManageDatabase.shared.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", tableAccount.account), sorted: "serverUrl", ascending: true) {
  57. for directory: tableDirectory in directories {
  58. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(directory.ocId) else {
  59. continue
  60. }
  61. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: NCBrandGlobal.shared.selectorDownloadFile)
  62. }
  63. }
  64. // Synchronize Offline Files
  65. let files = NCManageDatabase.shared.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", tableAccount.account), sorted: "fileName", ascending: true)
  66. for file: tableLocalFile in files {
  67. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(file.ocId) else {
  68. continue
  69. }
  70. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: NCBrandGlobal.shared.selectorDownloadFile)
  71. }
  72. // Get Avatar
  73. let avatarUrl = "\(self.appDelegate.urlBase!)/index.php/avatar/\(self.appDelegate.user!)/\(NCBrandGlobal.shared.avatarSize)".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
  74. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + stringUser + "-" + self.appDelegate.user + ".png"
  75. NCCommunication.shared.downloadContent(serverUrl: avatarUrl) { (account, data, errorCode, errorMessage) in
  76. if errorCode == 0 {
  77. DispatchQueue.global().async {
  78. if let image = UIImage(data: data!) {
  79. try? FileManager.default.removeItem(atPath: fileNamePath)
  80. if let data = image.pngData() {
  81. try? data.write(to: URL(fileURLWithPath: fileNamePath))
  82. }
  83. }
  84. }
  85. }
  86. }
  87. NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterChangeUserProfile)
  88. self.requestServerCapabilities()
  89. }
  90. } else {
  91. if errorCode == 401 || errorCode == 403 {
  92. NCNetworkingCheckRemoteUser.shared.checkRemoteUser(account: account)
  93. }
  94. }
  95. }
  96. }
  97. private func requestServerStatus() {
  98. NCCommunication.shared.getServerStatus(serverUrl: appDelegate.urlBase) { (serverProductName, serverVersion, versionMajor, versionMinor, versionMicro, extendedSupport, errorCode, errorMessage) in
  99. if errorCode == 0 {
  100. DispatchQueue.global().async {
  101. if extendedSupport == false {
  102. if serverProductName == "owncloud" {
  103. NCContentPresenter.shared.messageNotification("_warning_", description: "_warning_owncloud_", delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCBrandGlobal.shared.ErrorInternalError)
  104. } else if versionMajor <= NCBrandGlobal.shared.nextcloud_unsupported_version {
  105. NCContentPresenter.shared.messageNotification("_warning_", description: "_warning_unsupported_", delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCBrandGlobal.shared.ErrorInternalError)
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. private func requestServerCapabilities() {
  113. if appDelegate.account == nil || appDelegate.account.count == 0 { return }
  114. NCCommunication.shared.getCapabilities() { (account, data, errorCode, errorDescription) in
  115. if errorCode == 0 && data != nil {
  116. DispatchQueue.global().async {
  117. NCManageDatabase.shared.addCapabilitiesJSon(data!, account: account)
  118. // Setup communication
  119. self.appDelegate.settingSetupCommunication(account)
  120. // Theming
  121. NCBrandColor.shared.settingThemingColor(account: account)
  122. // File Sharing
  123. let isFilesSharingEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  124. if isFilesSharingEnabled {
  125. NCCommunication.shared.readShares { (account, shares, errorCode, ErrorDescription) in
  126. if errorCode == 0 {
  127. DispatchQueue.global().async {
  128. NCManageDatabase.shared.deleteTableShare(account: account)
  129. if shares != nil {
  130. NCManageDatabase.shared.addShare(urlBase: self.appDelegate.urlBase, account: account, shares: shares!)
  131. }
  132. self.appDelegate.shares = NCManageDatabase.shared.getTableShares(account: account)
  133. }
  134. } else {
  135. NCContentPresenter.shared.messageNotification("_share_", description: ErrorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  136. }
  137. }
  138. }
  139. let comments = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFilesComments, exists: false)
  140. let activity = NCManageDatabase.shared.getCapabilitiesServerArray(account: account, elements: NCElementsJSON.shared.capabilitiesActivity)
  141. if !isFilesSharingEnabled && !comments && activity == nil {
  142. self.appDelegate.disableSharesView = true
  143. } else {
  144. self.appDelegate.disableSharesView = false
  145. }
  146. // Text direct editor detail
  147. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  148. if serverVersionMajor >= NCBrandGlobal.shared.nextcloudVersion18 {
  149. NCCommunication.shared.NCTextObtainEditorDetails() { (account, editors, creators, errorCode, errorMessage) in
  150. if errorCode == 0 && account == self.appDelegate.account {
  151. DispatchQueue.global().async {
  152. NCManageDatabase.shared.addDirectEditing(account: account, editors: editors, creators: creators)
  153. }
  154. }
  155. }
  156. }
  157. // External file Server
  158. let isExternalSitesServerEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesExternalSitesExists, exists: true)
  159. if (isExternalSitesServerEnabled) {
  160. NCCommunication.shared.getExternalSite() { (account, externalSites, errorCode, errorDescription) in
  161. if errorCode == 0 && account == self.appDelegate.account {
  162. DispatchQueue.global().async {
  163. NCManageDatabase.shared.deleteExternalSites(account: account)
  164. for externalSite in externalSites {
  165. NCManageDatabase.shared.addExternalSites(externalSite, account: account)
  166. }
  167. }
  168. }
  169. }
  170. } else {
  171. NCManageDatabase.shared.deleteExternalSites(account: account)
  172. }
  173. // User Status
  174. let userStatus = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesUserStatusEnabled, exists: false)
  175. if userStatus {
  176. NCCommunication.shared.getUserStatus { (account, clearAt, icon, message, messageId, messageIsPredefined, status, statusIsUserDefined, userId, errorCode, errorDescription) in
  177. if errorCode == 0 && account == self.appDelegate.account && userId == self.appDelegate.userID {
  178. DispatchQueue.global().async {
  179. NCManageDatabase.shared.setAccountUserStatus(userStatusClearAt: clearAt, userStatusIcon: icon, userStatusMessage: message, userStatusMessageId: messageId, userStatusMessageIsPredefined: messageIsPredefined, userStatusStatus: status, userStatusStatusIsUserDefined: statusIsUserDefined, account: account)
  180. }
  181. }
  182. }
  183. }
  184. // Handwerkcloud
  185. let isHandwerkcloudEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesHWCEnabled, exists: false)
  186. if (isHandwerkcloudEnabled) {
  187. self.requestHC()
  188. }
  189. }
  190. } else if errorCode != 0 {
  191. NCBrandColor.shared.settingThemingColor(account: account)
  192. if errorCode == 401 || errorCode == 403 {
  193. NCNetworkingCheckRemoteUser.shared.checkRemoteUser(account: account)
  194. }
  195. } else {
  196. NCBrandColor.shared.settingThemingColor(account: account)
  197. }
  198. }
  199. }
  200. //MARK: - Thirt Part
  201. private func requestHC() {
  202. }
  203. }