NCAppConfigView.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // NCAppConfigView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 18/09/2019.
  6. // Copyright © 2019 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 NCCommunication
  25. class NCAppConfigView: UIViewController {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. private var serverUrl: String?
  28. private var username: String?
  29. private var password: String?
  30. @IBOutlet weak var logoImage: UIImageView!
  31. @IBOutlet weak var titleLabel: UILabel!
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. self.view.backgroundColor = NCBrandColor.shared.brandElement
  35. titleLabel.textColor = NCBrandColor.shared.brandText
  36. titleLabel.text = NSLocalizedString("_appconfig_view_title_", comment: "")
  37. if let serverConfig = UserDefaults.standard.dictionary(forKey: NCBrandConfiguration.shared.configuration_bundleId) {
  38. serverUrl = serverConfig[NCBrandConfiguration.shared.configuration_serverUrl] as? String
  39. username = serverConfig[NCBrandConfiguration.shared.configuration_username] as? String
  40. password = serverConfig[NCBrandConfiguration.shared.configuration_password] as? String
  41. } else {
  42. serverUrl = UserDefaults.standard.string(forKey: NCBrandConfiguration.shared.configuration_serverUrl)
  43. username = UserDefaults.standard.string(forKey: NCBrandConfiguration.shared.configuration_username)
  44. password = UserDefaults.standard.string(forKey: NCBrandConfiguration.shared.configuration_password)
  45. }
  46. }
  47. override func viewDidAppear(_ animated: Bool) {
  48. super.viewDidAppear(animated)
  49. // Stop timer error network
  50. appDelegate.timerErrorNetworking.invalidate()
  51. guard let serverUrl = self.serverUrl else {
  52. NCContentPresenter.shared.messageNotification("_error_", description: "User Default, serverUrl not found", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  53. return
  54. }
  55. guard let username = self.username else {
  56. NCContentPresenter.shared.messageNotification("_error_", description: "User Default, username not found", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  57. return
  58. }
  59. guard let password = self.password else {
  60. NCContentPresenter.shared.messageNotification("_error_", description: "User Default, password not found", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  61. return
  62. }
  63. NCCommunication.shared.getAppPassword(serverUrl: serverUrl, username: username, password: password, customUserAgent: nil) { (token, errorCode, errorDescription) in
  64. DispatchQueue.main.async {
  65. if errorCode == 0 && token != nil {
  66. let account: String = "\(username) \(serverUrl)"
  67. // NO account found, clear
  68. if NCManageDatabase.shared.getAccounts() == nil { NCUtility.shared.removeAllSettings() }
  69. // Add new account
  70. NCManageDatabase.shared.deleteAccount(account)
  71. NCManageDatabase.shared.addAccount(account, urlBase: serverUrl, user: username, password: token!)
  72. guard let tableAccount = NCManageDatabase.shared.setAccountActive(account) else {
  73. NCContentPresenter.shared.messageNotification("_error_", description: "setAccountActive error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  74. self.dismiss(animated: true, completion: nil)
  75. return
  76. }
  77. self.appDelegate.settingAccount(account, urlBase: serverUrl, user: username, userID: tableAccount.userID, password: token!)
  78. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_initializeMain)
  79. self.dismiss(animated: true) {}
  80. } else {
  81. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  82. }
  83. }
  84. }
  85. }
  86. override func viewDidDisappear(_ animated: Bool) {
  87. super.viewDidDisappear(animated)
  88. // Start timer error network
  89. appDelegate.startTimerErrorNetworking()
  90. }
  91. }