123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- //
- // NCService.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 14/03/18.
- // Copyright © 2018 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import Foundation
- import SVGKit
- class NCService: NSObject {
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- @objc static let sharedInstance: NCService = {
- let instance = NCService()
- return instance
- }()
-
- //MARK: -
- //MARK: Start Services API NC
-
- @objc public func startRequestServicesServer() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
-
- self.requestUserProfile()
- self.requestServerStatus()
- }
- //MARK: -
- //MARK: Internal request Service API NC
-
- private func requestUserProfile() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
-
- OCNetworking.sharedManager().getUserProfile(withAccount: appDelegate.activeAccount, completion: { (account, userProfile, message, errorCode) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount {
-
- // Update User (+ userProfile.id) & active account & account network
- guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!, HCProperties: false) else {
- self.appDelegate.messageNotification("Accopunt", description: "Internal error : account not found on DB", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
- return
- }
-
- let user = tableAccount.user
- let url = tableAccount.url
-
- self.appDelegate.settingActiveAccount(tableAccount.account, activeUrl: tableAccount.url, activeUser: tableAccount.user, activeUserID: tableAccount.userID, activePassword: CCUtility.getPassword(tableAccount.account))
-
- // Call func thath required the userdID
- self.appDelegate.activeFavorites.listingFavorites()
- self.appDelegate.activeMedia.reloadDataSource(loadNetworkDatasource: true)
- NCFunctionMain.sharedInstance.synchronizeOffline()
-
- DispatchQueue.global().async {
-
- let avatarUrl = "\(self.appDelegate.activeUrl!)/index.php/avatar/\(self.appDelegate.activeUser!)/128".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
- let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(user, activeUrl: url) + "-" + self.appDelegate.activeUser + ".png"
-
- OCNetworking.sharedManager()?.downloadContents(ofUrl: avatarUrl, completion: { (data, message, errorCode) in
- if errorCode == 0 {
- if let image = UIImage(data: data!) {
- try? FileManager.default.removeItem(atPath: fileNamePath)
- if let data = image.pngData() {
- try? data.write(to: URL(fileURLWithPath: fileNamePath))
- }
- }
- }
- })
-
- DispatchQueue.main.async {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
- }
- }
-
- // Get Capabilities
- self.requestServerCapabilities()
-
- } else if errorCode != 0 {
-
- print("Get user profile failure error")
-
- } else {
-
- if errorCode == kOCErrorServerUnauthorized {
- CCUtility.setPassword(account, password: nil)
- }
-
- print("[LOG] It has been changed user during networking process, error.")
- }
- })
- }
-
- private func requestServerStatus() {
-
- OCNetworking.sharedManager().serverStatusUrl(appDelegate.activeUrl, completion: { (serverProductName, versionMajor, versionMicro, versionMinor, message, errorCode) in
- if errorCode == 0 {
- if serverProductName == "owncloud" {
- self.appDelegate.messageNotification("_warning_", description: "_warning_owncloud_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: Int(k_CCErrorInternalError))
- } else if versionMajor <= k_nextcloud_unsupported {
- self.appDelegate.messageNotification("_warning_", description: "_warning_unsupported_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: Int(k_CCErrorInternalError))
- }
- }
-
- })
- }
-
- private func requestServerCapabilities() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
-
- OCNetworking.sharedManager().getCapabilitiesWithAccount(appDelegate.activeAccount, completion: { (account, capabilities, message, errorCode) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount {
-
- // Update capabilities db
- NCManageDatabase.sharedInstance.addCapabilities(capabilities!, account: account!)
-
- // ------ THEMING -----------------------------------------------------------------------
-
- if (NCBrandOptions.sharedInstance.use_themingBackground && capabilities!.themingBackground != "") {
-
- // Download Theming Background
- DispatchQueue.global().async {
-
- // Download Logo
- let fileNameThemingLogo = CCUtility.getStringUser(self.appDelegate.activeUser, activeUrl: self.appDelegate.activeUrl) + "-themingLogo.png"
- NCUtility.sharedInstance.convertSVGtoPNGWriteToUserData(svgUrlString: capabilities!.themingLogo, fileName: fileNameThemingLogo, width: 40, rewrite: true, closure: { (imageNamePath) in })
-
- let backgroundURL = capabilities!.themingBackground!.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
- let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(self.appDelegate.activeUser, activeUrl: self.appDelegate.activeUrl) + "-themingBackground.png"
-
- OCNetworking.sharedManager()?.downloadContents(ofUrl: backgroundURL, completion: { (data, message, errorCode) in
- if errorCode == 0 {
- if let image = UIImage(data: data!) {
- try? FileManager.default.removeItem(atPath: fileNamePath)
- if let data = image.pngData() {
- try? data.write(to: URL(fileURLWithPath: fileNamePath))
- }
- }
- }
- })
-
- DispatchQueue.main.async {
- self.appDelegate.settingThemingColorBrand()
- }
- }
-
- } else {
-
- self.appDelegate.settingThemingColorBrand()
- }
-
- // ------ SEARCH ------------------------------------------------------------------------
-
- if (NCManageDatabase.sharedInstance.getServerVersion(account: account!) != capabilities!.versionMajor && self.appDelegate.activeMain != nil) {
- self.appDelegate.activeMain.cancelSearchBar()
- }
-
- // ------ GET OTHER SERVICE -------------------------------------------------------------
-
- // Get Notification
- if (capabilities!.isNotificationServerEnabled) {
-
- OCNetworking.sharedManager().getNotificationWithAccount(account!, completion: { (account, listOfNotifications, message, errorCode) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount {
-
- DispatchQueue.global().async {
-
- let sortedListOfNotifications = (listOfNotifications! as NSArray).sortedArray(using: [
- NSSortDescriptor(key: "date", ascending: false)
- ])
-
- var old = ""
- var new = ""
-
- for notification in listOfNotifications! {
- let id = (notification as! OCNotifications).idNotification
- if let icon = (notification as! OCNotifications).icon {
-
- NCUtility.sharedInstance.convertSVGtoPNGWriteToUserData(svgUrlString: icon, fileName: nil, width: 25, rewrite: false, closure: { (imageNamePath) in })
- }
- new = new + String(describing: id)
- }
- for notification in self.appDelegate.listOfNotifications! {
- let id = (notification as! OCNotifications).idNotification
- old = old + String(describing: id)
- }
-
- DispatchQueue.main.async {
-
- if (new != old) {
- self.appDelegate.listOfNotifications = NSMutableArray.init(array: sortedListOfNotifications)
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationReloadData"), object: nil)
- }
-
- // Update Main NavigationBar
- if (self.appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
- self.appDelegate.activeMain.setUINavigationBarDefault()
- }
- }
- }
-
- } else {
-
- // Update Main NavigationBar
- if (self.appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
- self.appDelegate.activeMain.setUINavigationBarDefault()
- }
- }
- })
-
- } else {
-
- // Remove all Notification
- self.appDelegate.listOfNotifications.removeAllObjects()
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationReloadData"), object: nil)
- // Update Main NavigationBar
- if (self.appDelegate.activeMain != nil && self.appDelegate.activeMain.isSelectedMode == false) {
- self.appDelegate.activeMain.setUINavigationBarDefault()
- }
- }
-
- // Get External Sites
- if (capabilities!.isExternalSitesServerEnabled) {
-
- OCNetworking.sharedManager().getExternalSites(withAccount: account!, completion: { (account, listOfExternalSites, message, errorCode) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount {
-
- NCManageDatabase.sharedInstance.deleteExternalSites(account: account!)
- for externalSites in listOfExternalSites! {
- NCManageDatabase.sharedInstance.addExternalSites(externalSites as! OCExternalSites, account: account!)
- }
-
- }
- })
-
- } else {
-
- NCManageDatabase.sharedInstance.deleteExternalSites(account: account!)
- }
-
- // Get Share
- if (capabilities!.isFilesSharingAPIEnabled && self.appDelegate.activeMain != nil) {
-
- self.appDelegate.activeMain.readShare(withAccount: account, openWindow: false, metadata: nil)
- }
-
- // Get Activity
- if (capabilities!.isActivityV2Enabled) {
-
- OCNetworking.sharedManager().getActivityWithAccount(account!, since: 0, limit: 100, link: "", completion: { (account, listOfActivity, message, errorCode) in
- DispatchQueue.global().async {
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- NCManageDatabase.sharedInstance.addActivity(listOfActivity as! [OCActivity], account: account!)
- }
- }
- })
- }
-
- // Get Trash List
- let pathTrash = k_dav + "/trashbin/" + (self.appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)! + "/trash/"
-
- OCNetworking.sharedManager().listingTrash(withAccount: account, path: pathTrash, serverUrl: self.appDelegate.activeUrl, depth: "infinity", completion: { (account, item, message, errorCode) in
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- DispatchQueue.global().async {
- NCManageDatabase.sharedInstance.deleteTrash(filePath: nil, account: account!)
- NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])
- }
- }
- })
-
- // Get Handwerkcloud
- if (capabilities!.isHandwerkcloudEnabled) {
- self.requestHC()
- }
-
- } else if errorCode != 0 {
-
- self.appDelegate.settingThemingColorBrand()
-
- if errorCode == kOCErrorServerUnauthorized {
- CCUtility.setPassword(account, password: nil)
- }
-
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- // Change Theming color
- self.appDelegate.settingThemingColorBrand()
- }
- })
- }
-
- @objc public func middlewarePing() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
- }
-
- //MARK: -
- //MARK: Thirt Part
-
- private func requestHC() {
-
- let professions = CCUtility.getHCBusinessType()
- if professions != nil && professions!.count > 0 {
- OCNetworking.sharedManager()?.putHCUserProfile(withAccount: appDelegate.activeAccount, serverUrl: appDelegate.activeUrl, address: nil, businesssize: nil, businesstype: professions, city: nil, company: nil, country: nil, displayname: nil, email: nil, phone: nil, role_: nil, twitter: nil, website: nil, zip: nil, completion: { (account, message, errorCode) in
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- CCUtility.setHCBusinessType(nil)
- OCNetworking.sharedManager()?.getHCUserProfile(withAccount: self.appDelegate.activeAccount, serverUrl: self.appDelegate.activeUrl, completion: { (account, userProfile, message, errorCode) in
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- _ = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!, HCProperties: true)
- }
- })
- }
- })
- } else {
- OCNetworking.sharedManager()?.getHCUserProfile(withAccount: appDelegate.activeAccount, serverUrl: appDelegate.activeUrl, completion: { (account, userProfile, message, errorCode) in
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- _ = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!, HCProperties: true)
- }
- })
- }
-
- OCNetworking.sharedManager()?.getHCFeatures(withAccount: appDelegate.activeAccount, serverUrl: appDelegate.activeUrl, completion: { (account, features, message, errorCode) in
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- _ = NCManageDatabase.sharedInstance.setAccountHCFeatures(features!)
- }
- })
-
- }
- }
|