123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- import Foundation
- class NCService: NSObject, OCNetworkingDelegate {
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- @objc static let sharedInstance: NCService = {
- let instance = NCService()
- return instance
- }()
-
-
-
-
- @objc func startRequestServicesServer() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
-
- self.requestUserProfile()
- self.requestServerCapabilities()
- self.requestActivityServer()
- }
-
-
-
- @objc func requestServerCapabilities() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
-
- guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
- return
- }
-
- metadataNet.action = actionGetCapabilities
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
- }
-
- @objc func requestUserProfile() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
-
- guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
- return
- }
-
- metadataNet.action = actionGetUserProfile
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
- }
-
- @objc func requestActivityServer() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
-
- guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
- return
- }
-
- metadataNet.action = actionGetActivityServer
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
- }
-
- @objc func middlewarePing() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
-
- guard let metadataNet = CCMetadataNet.init(account: appDelegate.activeAccount) else {
- return
- }
-
- metadataNet.action = actionMiddlewarePing
- metadataNet.serverUrl = NCBrandOptions.sharedInstance.middlewarePingUrl
-
-
- }
-
-
-
-
- func getCapabilitiesOfServerSuccessFailure(_ metadataNet: CCMetadataNet!, capabilities: OCCapabilities?, message: String?, errorCode: Int) {
-
-
- if (metadataNet.account != appDelegate.activeAccount) {
- return
- }
-
- if (errorCode == 0) {
-
-
- NCManageDatabase.sharedInstance.addCapabilities(capabilities!)
-
-
-
- if (NCBrandOptions.sharedInstance.use_themingBackground && capabilities!.themingBackground != "") {
-
-
- DispatchQueue.global().async {
-
- let address = capabilities!.themingBackground!.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
- let fileName = "\(self.appDelegate.directoryUser!)/themingBackground.png"
- guard let imageData = try? Data(contentsOf: URL(string: address)!) else {
- DispatchQueue.main.async {
- self.appDelegate.settingThemingColorBrand()
- }
- return
- }
-
- DispatchQueue.main.async {
-
- guard let image = UIImage(data: imageData) else {
- try? FileManager.default.removeItem(atPath: fileName)
- self.appDelegate.settingThemingColorBrand()
- return
- }
-
- if let data = UIImagePNGRepresentation(image) {
- try? data.write(to: URL(fileURLWithPath: fileName))
- }
-
- self.appDelegate.settingThemingColorBrand()
- }
- }
-
- } else {
-
- self.appDelegate.settingThemingColorBrand()
- }
-
-
-
- if (NCManageDatabase.sharedInstance.getServerVersion() != capabilities!.versionMajor && appDelegate.activeMain != nil) {
- appDelegate.activeMain.cancelSearchBar()
- }
-
-
-
- if (capabilities!.isNotificationServerEnabled) {
-
- metadataNet.action = actionGetNotificationServer
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
-
- } else {
-
-
- self.appDelegate.listOfNotifications.removeAllObjects()
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationReloadData"), object: nil)
-
- if (appDelegate.activeMain != nil && self.appDelegate.activeMain.isSelectedMode == false) {
- self.appDelegate.activeMain.setUINavigationBarDefault()
- }
- }
-
-
- if (capabilities!.isExternalSitesServerEnabled) {
-
- metadataNet.action = actionGetExternalSitesServer
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
-
- } else {
-
- NCManageDatabase.sharedInstance.deleteExternalSites()
- }
-
-
- if (capabilities!.isFilesSharingAPIEnabled && appDelegate.activeMain != nil) {
-
- appDelegate.sharesID.removeAllObjects()
- metadataNet.action = actionReadShareServer
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: appDelegate.activeMain, metadataNet: metadataNet)
- }
-
- } else {
-
-
- appDelegate.settingThemingColorBrand()
-
- var error = ""
- if let message = message {
- error = "Get Capabilities failure error \(errorCode) \(message)"
- } else {
- error = "Get Capabilities failure error \(errorCode)"
- }
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Capabilities of Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
- }
- }
-
- @objc func getUserProfileSuccessFailure(_ metadataNet: CCMetadataNet!, userProfile: OCUserProfile?, message: String?, errorCode: Int) {
-
-
- if (metadataNet.account != appDelegate.activeAccount) {
- return
- }
-
- if (errorCode == 0) {
-
-
- guard let tableAccount = NCManageDatabase.sharedInstance.setAccountUserProfile(userProfile!) else {
- 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
- }
-
- CCNetworking.shared().settingAccount()
- appDelegate.settingActiveAccount(tableAccount.account, activeUrl: tableAccount.url, activeUser: tableAccount.user, activeUserID: tableAccount.userID, activePassword: tableAccount.password)
-
-
- appDelegate.activeFavorites.listingFavorites()
- appDelegate.activePhotos.searchPhotoVideo()
-
- DispatchQueue.global(qos: .default).async {
-
- let address = "\(self.appDelegate.activeUrl!)/index.php/avatar/\(self.appDelegate.activeUser!)/128".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
- let fileName = "\(self.appDelegate.directoryUser!)/avatar.png"
- guard let imageData = try? Data(contentsOf: URL(string: address)!) else {
- DispatchQueue.main.async {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
- }
- return
- }
-
- DispatchQueue.main.async {
-
- guard let image = UIImage(data: imageData) else {
- try? FileManager.default.removeItem(atPath: fileName)
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
- return
- }
-
- if let data = UIImagePNGRepresentation(image) {
- try? data.write(to: URL(fileURLWithPath: fileName))
- }
-
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeUserProfile"), object: nil)
- }
- }
-
- } else {
-
- var error = ""
- if let message = message {
- error = "Get user profile failure error \(errorCode) \(message)"
- } else {
- error = "Get user profile failure error \(errorCode)"
- }
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get user profile Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
- }
- }
-
- @objc func getExternalSitesServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfExternalSites: [Any]?, message: String?, errorCode: Int) {
-
-
- if (metadataNet.account != appDelegate.activeAccount) {
- return
- }
-
- if (errorCode == 0) {
-
- NCManageDatabase.sharedInstance.deleteExternalSites()
- for externalSites in listOfExternalSites! {
- NCManageDatabase.sharedInstance.addExternalSites(externalSites as! OCExternalSites)
- }
-
- } else {
-
- var error = ""
- if let message = message {
- error = "Get external site failure error \(errorCode) \(message)"
- } else {
- error = "Get external site failure error \(errorCode)"
- }
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get external site Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
- }
- }
-
- @objc func getActivityServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfActivity: [Any]?, message: String?, errorCode: Int) {
-
-
- if (metadataNet.account != appDelegate.activeAccount) {
- return
- }
-
- if (errorCode == 0) {
-
- NCManageDatabase.sharedInstance.addActivityServer(listOfActivity as! [OCActivity])
- if (appDelegate.activeActivity != nil) {
- appDelegate.activeActivity.reloadDatasource()
- }
-
- } else {
-
- var error = ""
- if let message = message {
- error = "Get Activity Server failure error \(errorCode) \(message)"
- } else {
- error = "Get Activity Server failure error \(errorCode)"
- }
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Activity Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
- }
- }
-
- @objc func getNotificationServerSuccessFailure(_ metadataNet: CCMetadataNet!, listOfNotifications: [Any]?, message: String?, errorCode: Int) {
-
-
- if (metadataNet.account != appDelegate.activeAccount) {
- return
- }
-
- if (errorCode == 0) {
-
- DispatchQueue.global(qos: .default).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 AnyObject).idNotification!
- new = new + String(describing: id)
- }
- for notification in self.appDelegate.listOfNotifications! {
- let id = (notification as AnyObject).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)
-
-
- if (self.appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
- self.appDelegate.activeMain.setUINavigationBarDefault()
- }
- }
- }
- }
-
- } else {
-
- var error = ""
- if let message = message {
- error = "Get Notification Server failure error \(errorCode) \(message)"
- } else {
- error = "Get Notification Server failure error \(errorCode)"
- }
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionCapabilities, selector: "Get Notification Server", note: error, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
-
-
- if (appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
- appDelegate.activeMain.setUINavigationBarDefault()
- }
- }
- }
- }
|