SafeServerViewController.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import UIKit
  21. import ThreemaFramework
  22. class SafeServerViewController: IDCreationPageViewController {
  23. @IBOutlet weak var titleLabel: UILabel!
  24. @IBOutlet weak var descriptionLabel: UILabel!
  25. @IBOutlet weak var serverSwitchLabel: UILabel!
  26. @IBOutlet weak var serverSwitch: UISwitch!
  27. @IBOutlet weak var serverStackView: UIStackView!
  28. @IBOutlet weak var serverField: SetupTextField!
  29. @IBOutlet weak var serverLabel: UILabel!
  30. @IBOutlet weak var serverUsernameField: SetupTextField!
  31. @IBOutlet weak var serverPasswordField: SetupTextField!
  32. @IBOutlet weak var cancelButton: SetupButton!
  33. @IBOutlet weak var okButton: SetupButton!
  34. var keyboardResize: KeyboardResizeCenterY?
  35. var isServerForRestore: Bool = false
  36. private var cancelSegueID : String?
  37. private var okSegueID: String?
  38. var customServer: String?
  39. var server: String?
  40. var serverUsername: String?
  41. var serverPassword: String?
  42. var maxBackupBytes: Int?
  43. var retentionDays: Int?
  44. override func viewDidLoad() {
  45. super.viewDidLoad()
  46. self.cancelSegueID = self.isServerForRestore ? "CancelRestoreSafeServer" : "CancelSafeServer"
  47. self.okSegueID = self.isServerForRestore ? "OkRestoreSafeServer" : "OkSafeServer"
  48. self.hideKeyboardWhenTappedAround()
  49. self.keyboardResize = KeyboardResizeCenterY(parent: self.view, resize: self.mainContentView)
  50. if self.isServerForRestore {
  51. self.titleLabel.isHidden = true
  52. self.descriptionLabel.text = NSLocalizedString("safe_configure_choose_server", comment: "")
  53. } else {
  54. self.titleLabel.text = NSLocalizedString("safe_configure_choose_server", comment: "")
  55. self.descriptionLabel.text = NSLocalizedString("safe_configure_server_explain", comment: "")
  56. }
  57. self.serverSwitchLabel.text = NSLocalizedString("safe_use_default_server", comment: "")
  58. self.serverField.delegate = self
  59. self.serverField.placeholder = "https://server.example.com"
  60. self.serverLabel.text = BundleUtil.localizedString(forKey: "safe_server_authentication")
  61. self.serverUsernameField.delegate = self
  62. self.serverUsernameField.placeholder = BundleUtil.localizedString(forKey: "username")
  63. self.serverPasswordField.delegate = self
  64. self.serverPasswordField.placeholder = BundleUtil.localizedString(forKey: "Password")
  65. self.cancelButton.setTitle(NSLocalizedString("cancel", comment: ""), for: .normal)
  66. self.okButton.setTitle(NSLocalizedString("ok", comment: ""), for: .normal)
  67. let isDefault = self.server == nil
  68. self.serverSwitch.isOn = isDefault
  69. self.serverStackView.isHidden = isDefault
  70. self.serverField.text = self.customServer
  71. self.serverUsernameField.text = self.serverUsername
  72. self.serverPasswordField.text = self.serverPassword
  73. self.serverSwitch.onTintColor = Colors.mainThemeDark()
  74. }
  75. //MARK: - Controlling the Keyboard
  76. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  77. return true
  78. }
  79. func validateServer() -> Bool {
  80. if serverSwitch.isOn {
  81. self.customServer = nil
  82. self.server = nil
  83. self.serverUsername = nil
  84. self.serverPassword = nil
  85. self.maxBackupBytes = nil
  86. self.retentionDays = nil
  87. } else {
  88. let safeConfigManager = SafeConfigManager()
  89. let safeStore = SafeStore(safeConfigManager: safeConfigManager, serverApiConnector: ServerAPIConnector())
  90. if let customServer = self.serverField.text,
  91. let customServerUrl = safeStore.composeSafeServerAuth(server: customServer, user: self.serverUsernameField.text, password: self.serverPasswordField.text) {
  92. let safeManager = SafeManager(safeConfigManager: safeConfigManager, safeStore: safeStore, safeApiService: SafeApiService())
  93. let result = safeManager.testServer(serverUrl: customServerUrl)
  94. if let errorMessage = result.errorMessage {
  95. let alert = IntroQuestionViewHelper(parent: self, onAnswer: nil)
  96. alert.showAlert(errorMessage, title: BundleUtil.localizedString(forKey: "safe_test_server"))
  97. return false
  98. } else {
  99. self.customServer = customServer
  100. self.server = customServerUrl.absoluteString
  101. self.serverUsername = self.serverUsernameField.text
  102. self.serverPassword = self.serverPasswordField.text
  103. self.maxBackupBytes = result.maxBackupBytes
  104. self.retentionDays = result.retentionDays
  105. }
  106. }
  107. }
  108. return true
  109. }
  110. }
  111. extension SafeServerViewController: SetupTextFieldDelegate {
  112. func editingChangedTextField(_ sender: SetupTextField, forEvent event: UIEvent) { }
  113. func primaryActionTriggered(_ sender: SetupTextField, forEvent event: UIEvent) {
  114. if sender == self.serverField {
  115. self.serverUsernameField.becomeFirstResponder()
  116. } else if sender == self.serverUsernameField {
  117. self.serverPasswordField.becomeFirstResponder()
  118. } else if sender == self.serverPasswordField {
  119. sender.resignFirstResponder()
  120. if self.validateServer() {
  121. self.performSegue(withIdentifier: self.okSegueID!, sender: self)
  122. }
  123. }
  124. }
  125. }
  126. extension SafeServerViewController {
  127. @IBAction func changedServerSwitch(_ sender: UISwitch) {
  128. self.serverStackView.isHidden = sender.isOn
  129. }
  130. @IBAction func touchDownButton(_ sender: UIButton) {
  131. if sender == self.cancelButton {
  132. performSegue(withIdentifier: self.cancelSegueID!, sender: sender)
  133. } else if sender == self.okButton {
  134. if self.validateServer() {
  135. performSegue(withIdentifier: self.okSegueID!, sender: sender)
  136. }
  137. }
  138. }
  139. }