diff options
Diffstat (limited to 'iOSClient/Networking/NCService.swift')
-rw-r--r-- | iOSClient/Networking/NCService.swift | 154 |
1 files changed, 100 insertions, 54 deletions
diff --git a/iOSClient/Networking/NCService.swift b/iOSClient/Networking/NCService.swift index 6fa223943..f888fdc53 100644 --- a/iOSClient/Networking/NCService.swift +++ b/iOSClient/Networking/NCService.swift @@ -23,7 +23,8 @@ import UIKit import SVGKit -import NCCommunication +import NextcloudKit +import SwiftyJSON class NCService: NSObject { @objc static let shared: NCService = { @@ -43,7 +44,7 @@ class NCService: NSObject { addInternalTypeIdentifier() requestServerStatus() - requestUserProfile() + requestUserProfile() } // MARK: - @@ -51,71 +52,91 @@ class NCService: NSObject { func addInternalTypeIdentifier() { // txt - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "text/plain", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorText, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "markdown") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "text/plain", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorText, iconName: NKCommon.typeIconFile.document.rawValue, name: "markdown") // html - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "text/html", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorText, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "markdown") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "text/html", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorText, iconName: NKCommon.typeIconFile.document.rawValue, name: "markdown") // markdown - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "net.daringfireball.markdown", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorText, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "markdown") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "text/x-markdown", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorText, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "markdown") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "net.daringfireball.markdown", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorText, iconName: NKCommon.typeIconFile.document.rawValue, name: "markdown") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "text/x-markdown", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorText, iconName: NKCommon.typeIconFile.document.rawValue, name: "markdown") // document: text - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.oasis-open.opendocument.text", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorCollabora, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "document") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.openxmlformats.wordprocessingml.document", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorOnlyoffice, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "document") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.microsoft.word.doc", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "document") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.apple.iwork.pages.pages", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "pages") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.oasis-open.opendocument.text", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorCollabora, iconName: NKCommon.typeIconFile.document.rawValue, name: "document") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.openxmlformats.wordprocessingml.document", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorOnlyoffice, iconName: NKCommon.typeIconFile.document.rawValue, name: "document") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.microsoft.word.doc", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NKCommon.typeIconFile.document.rawValue, name: "document") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.apple.iwork.pages.pages", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NKCommon.typeIconFile.document.rawValue, name: "pages") // document: sheet - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.oasis-open.opendocument.spreadsheet", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorCollabora, iconName: NCCommunicationCommon.typeIconFile.xls.rawValue, name: "sheet") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.openxmlformats.spreadsheetml.sheet", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorOnlyoffice, iconName: NCCommunicationCommon.typeIconFile.xls.rawValue, name: "sheet") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.microsoft.excel.xls", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NCCommunicationCommon.typeIconFile.xls.rawValue, name: "sheet") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.apple.iwork.numbers.numbers", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NCCommunicationCommon.typeIconFile.xls.rawValue, name: "numbers") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.oasis-open.opendocument.spreadsheet", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorCollabora, iconName: NKCommon.typeIconFile.xls.rawValue, name: "sheet") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.openxmlformats.spreadsheetml.sheet", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorOnlyoffice, iconName: NKCommon.typeIconFile.xls.rawValue, name: "sheet") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.microsoft.excel.xls", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NKCommon.typeIconFile.xls.rawValue, name: "sheet") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.apple.iwork.numbers.numbers", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NKCommon.typeIconFile.xls.rawValue, name: "numbers") // document: presentation - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.oasis-open.opendocument.presentation", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorCollabora, iconName: NCCommunicationCommon.typeIconFile.ppt.rawValue, name: "presentation") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.openxmlformats.presentationml.presentation", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorOnlyoffice, iconName: NCCommunicationCommon.typeIconFile.ppt.rawValue, name: "presentation") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.microsoft.powerpoint.ppt", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NCCommunicationCommon.typeIconFile.ppt.rawValue, name: "presentation") - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.apple.iwork.keynote.key", classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NCCommunicationCommon.typeIconFile.ppt.rawValue, name: "keynote") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.oasis-open.opendocument.presentation", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorCollabora, iconName: NKCommon.typeIconFile.ppt.rawValue, name: "presentation") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "org.openxmlformats.presentationml.presentation", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorOnlyoffice, iconName: NKCommon.typeIconFile.ppt.rawValue, name: "presentation") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.microsoft.powerpoint.ppt", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NKCommon.typeIconFile.ppt.rawValue, name: "presentation") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: "com.apple.iwork.keynote.key", classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorQuickLook, iconName: NKCommon.typeIconFile.ppt.rawValue, name: "keynote") } + // MARK: - + private func requestServerStatus() { - NCCommunication.shared.getServerStatus(serverUrl: appDelegate.urlBase, queue: NCCommunicationCommon.shared.backgroundQueue) { serverProductName, _, versionMajor, _, _, extendedSupport, errorCode, _ in - guard errorCode == 0, extendedSupport == false else { + let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue) + + NextcloudKit.shared.getServerStatus(serverUrl: appDelegate.urlBase, options: options) { serverProductName, _, versionMajor, _, _, extendedSupport, data, error in + guard error == .success, extendedSupport == false else { return } if serverProductName == "owncloud" { - NCContentPresenter.shared.messageNotification("_warning_", description: "_warning_owncloud_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorInternalError, priority: .max) + let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_warning_owncloud_") + NCContentPresenter.shared.showWarning(error: error, priority: .max) } else if versionMajor <= NCGlobal.shared.nextcloud_unsupported_version { - NCContentPresenter.shared.messageNotification("_warning_", description: "_warning_unsupported_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorInternalError, priority: .max) + let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_warning_unsupported_") + NCContentPresenter.shared.showWarning(error: error, priority: .max) } } } + // MARK: - + private func requestUserProfile() { guard !appDelegate.account.isEmpty else { return } - NCCommunication.shared.getUserProfile(queue: NCCommunicationCommon.shared.backgroundQueue) { account, userProfile, errorCode, errorDescription in - guard errorCode == 0, account == self.appDelegate.account else { - NCBrandColor.shared.settingThemingColor(account: account) - if errorCode == 401 || errorCode == 403 { - NCNetworkingCheckRemoteUser.shared.checkRemoteUser(account: account, errorCode: errorCode, errorDescription: errorDescription) + let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue) + + NextcloudKit.shared.getUserProfile(options: options) { account, userProfile, data, error in + guard error == .success, account == self.appDelegate.account else { + + // Ops the server has Unauthorized + NKCommon.shared.writeLog("[ERROR] The server has response with Unauthorized \(error.errorCode)") + + DispatchQueue.main.async { + if (UIApplication.shared.applicationState == .active) && + (NCNetworking.shared.networkReachability != NKCommon.typeReachability.notReachable) && + (error.errorCode == NCGlobal.shared.errorNCUnauthorized || error.errorCode == NCGlobal.shared.errorUnauthorized || error.errorCode == NCGlobal.shared.errorForbidden) { + + NCBrandColor.shared.settingThemingColor(account: account) + NCNetworkingCheckRemoteUser().checkRemoteUser(account: account, error: error) + } } return } // Update User (+ userProfile.id) & active account & account network guard let tableAccount = NCManageDatabase.shared.setAccountUserProfile(userProfile!) else { - NCContentPresenter.shared.messageNotification("Account", description: "Internal error : account not found on DB", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError, priority: .max) + let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "Internal error : account not found on DB") + NCContentPresenter.shared.showError(error: error, priority: .max) return } self.appDelegate.settingAccount(tableAccount.account, urlBase: tableAccount.urlBase, user: tableAccount.user, userId: tableAccount.userId, password: CCUtility.getPassword(tableAccount.account)) // Synchronize favorite - NCNetworking.shared.listingFavoritescompletion(selector: NCGlobal.shared.selectorReadFile) { _, _, _, _ in } + NCNetworking.shared.listingFavoritescompletion(selector: NCGlobal.shared.selectorReadFile) { _, _, _ in } // Synchronize Offline self.synchronizeOffline(account: tableAccount.account) @@ -124,10 +145,11 @@ class NCService: NSObject { let fileName = tableAccount.userBaseUrl + "-" + self.appDelegate.user + ".png" let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName let etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag + let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue) - NCCommunication.shared.downloadAvatar(user: tableAccount.userId, fileNameLocalPath: fileNameLocalPath, sizeImage: NCGlobal.shared.avatarSize, avatarSizeRounded: NCGlobal.shared.avatarSizeRounded, etag: etag, queue: NCCommunicationCommon.shared.backgroundQueue) { _, _, _, etag, errorCode, _ in - guard let etag = etag, errorCode == 0 else { - if errorCode == NCGlobal.shared.errorNotModified { + NextcloudKit.shared.downloadAvatar(user: tableAccount.userId, fileNameLocalPath: fileNameLocalPath, sizeImage: NCGlobal.shared.avatarSize, avatarSizeRounded: NCGlobal.shared.avatarSizeRounded, etag: etag, options: options) { _, _, _, etag, error in + guard let etag = etag, error == .success else { + if error.errorCode == NCGlobal.shared.errorNotModified { NCManageDatabase.shared.setAvatarLoaded(fileName: fileName) } return @@ -137,18 +159,20 @@ class NCService: NSObject { } self.requestServerCapabilities() + self.requestDashboardWidget() } } + // MARK: - + private func requestServerCapabilities() { guard !appDelegate.account.isEmpty else { return } - NCCommunication.shared.getCapabilities(queue: NCCommunicationCommon.shared.backgroundQueue) { account, data, errorCode, errorDescription in - guard errorCode == 0, let data = data else { + let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue) + + NextcloudKit.shared.getCapabilities(options: options) { account, data, error in + guard error == .success, let data = data else { NCBrandColor.shared.settingThemingColor(account: account) - if errorCode == 401 || errorCode == 403 { - NCNetworkingCheckRemoteUser.shared.checkRemoteUser(account: account, errorCode: errorCode, errorDescription: errorDescription) - } return } @@ -157,7 +181,7 @@ class NCService: NSObject { // Setup communication if serverVersionMajor > 0 { - NCCommunicationCommon.shared.setup(nextcloudVersion: serverVersionMajor) + NKCommon.shared.setup(nextcloudVersion: serverVersionMajor) } // Theming @@ -171,15 +195,13 @@ class NCService: NSObject { // File Sharing let isFilesSharingEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false) if isFilesSharingEnabled { - NCCommunication.shared.readShares(parameters: NCCShareParameter(), queue: NCCommunicationCommon.shared.backgroundQueue) { account, shares, errorCode, errorDescription in - if errorCode == 0 { + NextcloudKit.shared.readShares(parameters: NKShareParameter(), options: options) { account, shares, data, error in + if error == .success { NCManageDatabase.shared.deleteTableShare(account: account) - if shares != nil { - NCManageDatabase.shared.addShare(urlBase: self.appDelegate.urlBase, account: account, shares: shares!) + if let shares = shares, !shares.isEmpty { + NCManageDatabase.shared.addShare(urlBase: self.appDelegate.urlBase, account: account, shares: shares) } self.appDelegate.shares = NCManageDatabase.shared.getTableShares(account: account) - } else { - NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode) } } } @@ -195,8 +217,9 @@ class NCService: NSObject { // Text direct editor detail if serverVersionMajor >= NCGlobal.shared.nextcloudVersion18 { - NCCommunication.shared.NCTextObtainEditorDetails(queue: NCCommunicationCommon.shared.backgroundQueue) { account, editors, creators, errorCode, _ in - if errorCode == 0 && account == self.appDelegate.account { + let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue) + NextcloudKit.shared.NCTextObtainEditorDetails(options: options) { account, editors, creators, data, error in + if error == .success && account == self.appDelegate.account { NCManageDatabase.shared.addDirectEditing(account: account, editors: editors, creators: creators) } } @@ -205,8 +228,8 @@ class NCService: NSObject { // External file Server let isExternalSitesServerEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesExternalSitesExists, exists: true) if isExternalSitesServerEnabled { - NCCommunication.shared.getExternalSite(queue: NCCommunicationCommon.shared.backgroundQueue) { account, externalSites, errorCode, _ in - if errorCode == 0 && account == self.appDelegate.account { + NextcloudKit.shared.getExternalSite(options: options) { account, externalSites, data, error in + if error == .success && account == self.appDelegate.account { NCManageDatabase.shared.deleteExternalSites(account: account) for externalSite in externalSites { NCManageDatabase.shared.addExternalSites(externalSite, account: account) @@ -220,8 +243,8 @@ class NCService: NSObject { // User Status let userStatus = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesUserStatusEnabled, exists: false) if userStatus { - NCCommunication.shared.getUserStatus(queue: NCCommunicationCommon.shared.backgroundQueue) { account, clearAt, icon, message, messageId, messageIsPredefined, status, statusIsUserDefined, userId, errorCode, _ in - if errorCode == 0 && account == self.appDelegate.account && userId == self.appDelegate.userId { + NextcloudKit.shared.getUserStatus(options: options) { account, clearAt, icon, message, messageId, messageIsPredefined, status, statusIsUserDefined, userId, data, error in + if error == .success && account == self.appDelegate.account && userId == self.appDelegate.userId { NCManageDatabase.shared.setAccountUserStatus(userStatusClearAt: clearAt, userStatusIcon: icon, userStatusMessage: message, userStatusMessageId: messageId, userStatusMessageIsPredefined: messageIsPredefined, userStatusStatus: status, userStatusStatusIsUserDefined: statusIsUserDefined, account: account) } } @@ -230,18 +253,43 @@ class NCService: NSObject { // Added UTI for Collabora if let richdocumentsMimetypes = NCManageDatabase.shared.getCapabilitiesServerArray(account: account, elements: NCElementsJSON.shared.capabilitiesRichdocumentsMimetypes) { for mimeType in richdocumentsMimetypes { - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: mimeType, classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorCollabora, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "document") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: mimeType, classFile: NKCommon.typeClassFile.document.rawValue, editor: NCGlobal.shared.editorCollabora, iconName: NKCommon.typeIconFile.document.rawValue, name: "document") } } // Added UTI for ONLYOFFICE & Text if let directEditingCreators = NCManageDatabase.shared.getDirectEditingCreators(account: account) { for directEditing in directEditingCreators { - NCCommunicationCommon.shared.addInternalTypeIdentifier(typeIdentifier: directEditing.mimetype, classFile: NCCommunicationCommon.typeClassFile.document.rawValue, editor: directEditing.editor, iconName: NCCommunicationCommon.typeIconFile.document.rawValue, name: "document") + NKCommon.shared.addInternalTypeIdentifier(typeIdentifier: directEditing.mimetype, classFile: NKCommon.typeClassFile.document.rawValue, editor: directEditing.editor, iconName: NKCommon.typeIconFile.document.rawValue, name: "document") } } } } + + // MARK: - + + private func requestDashboardWidget() { + + let options = NKRequestOptions(queue: NKCommon.shared.backgroundQueue) + + NextcloudKit.shared.getDashboardWidget(options: options) { account, dashboardWidgets, data, error in + Task { + if error == .success, let dashboardWidgets = dashboardWidgets { + NCManageDatabase.shared.addDashboardWidget(account: account, dashboardWidgets: dashboardWidgets) + for widget in dashboardWidgets { + if let url = URL(string: widget.iconUrl), let fileName = widget.iconClass { + let (_, data, error) = await NCNetworking.shared.getPreview(url: url) + if error == .success { + NCUtility.shared.convertDataToImage(data: data, size: CGSize(width: 256, height: 256), fileNameToWrite: fileName) + } + } + } + } + } + } + } + + // MARK: - @objc func synchronizeOffline(account: String) { @@ -264,6 +312,4 @@ class NCService: NSObject { NCOperationQueue.shared.synchronizationMetadata(metadata, selector: NCGlobal.shared.selectorDownloadFile) } } - - // MARK: - Thirt Part } |