123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- import Foundation
- import SVGKit
- class NCService: NSObject {
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- @objc static let sharedInstance: NCService = {
- let instance = NCService()
- return instance
- }()
-
-
-
-
- @objc public func startRequestServicesServer() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
-
- self.requestUserProfile()
- self.requestServerStatus()
- }
-
-
-
- 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 {
-
-
- 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))
-
-
- 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)
- }
- }
-
-
- self.requestServerCapabilities()
-
- } else {
-
- if errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden {
- OCNetworking.sharedManager()?.checkRemoteWipe(account)
- }
-
- print("[LOG] It has been changed user during networking process, error.")
- }
- })
- }
-
- private func requestServerStatus() {
-
- OCNetworking.sharedManager().serverStatusUrl(appDelegate.activeUrl, completion: { (serverProductName, versionMajor, versionMicro, versionMinor, extendedSupport, message, errorCode) in
- if errorCode == 0 {
- if extendedSupport == false {
- 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 {
-
-
- NCManageDatabase.sharedInstance.addCapabilities(capabilities!, account: account!)
-
-
-
- if (NCBrandOptions.sharedInstance.use_themingBackground && capabilities!.themingBackground != "") {
-
-
- DispatchQueue.global().async {
-
-
- 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()
- }
-
-
-
- if (NCManageDatabase.sharedInstance.getServerVersion(account: account!) != capabilities!.versionMajor && self.appDelegate.activeMain != nil) {
- self.appDelegate.activeMain.cancelSearchBar()
- }
-
-
-
-
- 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)
- }
-
-
- if (self.appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
- self.appDelegate.activeMain.setUINavigationBarDefault()
- }
- }
- }
-
- } else {
-
-
- if (self.appDelegate.activeMain.isSelectedMode == false && self.appDelegate.activeMain != nil) {
- self.appDelegate.activeMain.setUINavigationBarDefault()
- }
- }
- })
-
- } else {
-
-
- self.appDelegate.listOfNotifications.removeAllObjects()
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationReloadData"), object: nil)
-
- if (self.appDelegate.activeMain != nil && self.appDelegate.activeMain.isSelectedMode == false) {
- self.appDelegate.activeMain.setUINavigationBarDefault()
- }
- }
-
-
- 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!)
- }
-
-
- if (capabilities!.isFilesSharingAPIEnabled && self.appDelegate.activeMain != nil) {
-
- OCNetworking.sharedManager()?.readShare(withAccount: account, completion: { (account, items, message, errorCode) in
- if errorCode == 0 && account == self.appDelegate.activeAccount{
- let itemsOCSharedDto = items as! [OCSharedDto]
- NCManageDatabase.sharedInstance.deleteTableShare(account: account!)
- self.appDelegate.shares = NCManageDatabase.sharedInstance.addShare(account: account!, activeUrl: self.appDelegate.activeUrl, items: itemsOCSharedDto)
- self.appDelegate.activeMain?.tableView?.reloadData()
- self.appDelegate.activeFavorites?.tableView?.reloadData()
- } else {
- self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- }
- })
- }
-
-
- 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!)
- }
- }
- })
- }
-
-
- if (capabilities!.isHandwerkcloudEnabled) {
- self.requestHC()
- }
-
- } else if errorCode != 0 {
-
- self.appDelegate.settingThemingColorBrand()
-
- if errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden {
- OCNetworking.sharedManager()?.checkRemoteWipe(account)
- }
-
- } else {
- print("[LOG] It has been changed user during networking process, error.")
-
- self.appDelegate.settingThemingColorBrand()
- }
- })
- }
-
- @objc public func middlewarePing() {
-
- if (appDelegate.activeAccount == nil || appDelegate.activeAccount.count == 0 || appDelegate.maintenanceMode == true) {
- return
- }
- }
-
-
-
-
- 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!)
- }
- })
-
- }
- }
|