AdvancedViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import UIKit
  2. import DcCore
  3. import Intents
  4. internal final class AdvancedViewController: UITableViewController, ProgressAlertHandler {
  5. private struct SectionConfigs {
  6. let headerTitle: String?
  7. let footerTitle: String?
  8. let cells: [UITableViewCell]
  9. }
  10. private enum CellTags: Int {
  11. case autocryptPreferences
  12. case sendAutocryptMessage
  13. case manageKeys
  14. case experimentalFeatures
  15. case videoChat
  16. case viewLog
  17. }
  18. private var dcContext: DcContext
  19. internal let dcAccounts: DcAccounts
  20. private let externalPathDescr = "File Sharing/Delta Chat"
  21. // MARK: - ProgressAlertHandler
  22. weak var progressAlert: UIAlertController?
  23. var progressObserver: NSObjectProtocol?
  24. // MARK: - cells
  25. private lazy var autocryptSwitch: UISwitch = {
  26. let switchControl = UISwitch()
  27. switchControl.isOn = dcContext.e2eeEnabled
  28. switchControl.addTarget(self, action: #selector(handleAutocryptPreferencesToggle(_:)), for: .valueChanged)
  29. return switchControl
  30. }()
  31. private lazy var autocryptPreferencesCell: UITableViewCell = {
  32. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  33. cell.tag = CellTags.autocryptPreferences.rawValue
  34. cell.textLabel?.text = String.localized("autocrypt_prefer_e2ee")
  35. cell.accessoryView = autocryptSwitch
  36. cell.selectionStyle = .none
  37. return cell
  38. }()
  39. private lazy var sendAutocryptMessageCell: ActionCell = {
  40. let cell = ActionCell()
  41. cell.tag = CellTags.sendAutocryptMessage.rawValue
  42. cell.actionTitle = String.localized("autocrypt_send_asm_title")
  43. return cell
  44. }()
  45. private lazy var manageKeysCell: ActionCell = {
  46. let cell = ActionCell()
  47. cell.tag = CellTags.manageKeys.rawValue
  48. cell.actionTitle = String.localized("pref_manage_keys")
  49. return cell
  50. }()
  51. lazy var sentboxWatchCell: SwitchCell = {
  52. return SwitchCell(
  53. textLabel: String.localized("pref_watch_sent_folder"),
  54. on: dcContext.getConfigBool("sentbox_watch"),
  55. action: { cell in
  56. self.dcAccounts.stopIo()
  57. self.dcContext.setConfigBool("sentbox_watch", cell.isOn)
  58. self.dcAccounts.startIo()
  59. })
  60. }()
  61. lazy var sendCopyToSelfCell: SwitchCell = {
  62. return SwitchCell(
  63. textLabel: String.localized("pref_send_copy_to_self"),
  64. on: dcContext.getConfigBool("bcc_self"),
  65. action: { cell in
  66. self.dcContext.setConfigBool("bcc_self", cell.isOn)
  67. })
  68. }()
  69. lazy var mvboxMoveCell: SwitchCell = {
  70. return SwitchCell(
  71. textLabel: String.localized("pref_auto_folder_moves"),
  72. on: dcContext.getConfigBool("mvbox_move"),
  73. action: { cell in
  74. self.dcAccounts.stopIo()
  75. self.dcContext.setConfigBool("mvbox_move", cell.isOn)
  76. self.dcAccounts.startIo()
  77. })
  78. }()
  79. lazy var onlyFetchMvboxCell: SwitchCell = {
  80. return SwitchCell(
  81. textLabel: String.localized("pref_only_fetch_mvbox_title"),
  82. on: dcContext.getConfigBool("only_fetch_mvbox"),
  83. action: { cell in
  84. if cell.isOn {
  85. let alert = UIAlertController(title: String.localized("pref_only_fetch_mvbox_title"),
  86. message: String.localized("pref_imap_folder_warn_disable_defaults"),
  87. preferredStyle: .safeActionSheet)
  88. alert.addAction(UIAlertAction(title: String.localized("perm_continue"), style: .destructive, handler: { _ in
  89. self.dcAccounts.stopIo()
  90. self.dcContext.setConfigBool("only_fetch_mvbox", true)
  91. self.dcAccounts.startIo()
  92. }))
  93. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in
  94. cell.uiSwitch.setOn(false, animated: true)
  95. }))
  96. self.navigationController?.present(alert, animated: true, completion: nil)
  97. } else {
  98. self.dcAccounts.stopIo()
  99. self.dcContext.setConfigBool("only_fetch_mvbox", false)
  100. self.dcAccounts.startIo()
  101. }
  102. })
  103. }()
  104. private lazy var experimentalFeaturesCell: ActionCell = {
  105. let cell = ActionCell()
  106. cell.tag = CellTags.experimentalFeatures.rawValue
  107. cell.actionTitle = String.localized("pref_experimental_features")
  108. return cell
  109. }()
  110. private lazy var videoChatInstanceCell: UITableViewCell = {
  111. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  112. cell.tag = CellTags.videoChat.rawValue
  113. cell.textLabel?.text = String.localized("videochat_instance")
  114. cell.accessoryType = .disclosureIndicator
  115. return cell
  116. }()
  117. private lazy var viewLogCell: UITableViewCell = {
  118. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  119. cell.tag = CellTags.viewLog.rawValue
  120. cell.textLabel?.text = String.localized("pref_view_log")
  121. cell.accessoryType = .disclosureIndicator
  122. return cell
  123. }()
  124. private lazy var sections: [SectionConfigs] = {
  125. let autocryptSection = SectionConfigs(
  126. headerTitle: String.localized("autocrypt"),
  127. footerTitle: String.localized("autocrypt_explain"),
  128. cells: [autocryptPreferencesCell, manageKeysCell, sendAutocryptMessageCell]
  129. )
  130. let folderSection = SectionConfigs(
  131. headerTitle: String.localized("pref_imap_folder_handling"),
  132. footerTitle: String.localized("pref_only_fetch_mvbox_explain"),
  133. cells: [sentboxWatchCell, sendCopyToSelfCell, mvboxMoveCell, onlyFetchMvboxCell])
  134. let miscSection = SectionConfigs(
  135. headerTitle: nil,
  136. footerTitle: nil,
  137. cells: [experimentalFeaturesCell, videoChatInstanceCell])
  138. let viewLogSection = SectionConfigs(
  139. headerTitle: nil,
  140. footerTitle: nil,
  141. cells: [viewLogCell])
  142. return [autocryptSection, folderSection, miscSection, viewLogSection]
  143. }()
  144. init(dcAccounts: DcAccounts) {
  145. self.dcContext = dcAccounts.getSelected()
  146. self.dcAccounts = dcAccounts
  147. super.init(style: .grouped)
  148. hidesBottomBarWhenPushed = true
  149. }
  150. required init?(coder _: NSCoder) {
  151. fatalError("init(coder:) has not been implemented")
  152. }
  153. // MARK: - lifecycle
  154. override func viewDidLoad() {
  155. super.viewDidLoad()
  156. title = String.localized("menu_advanced")
  157. tableView.rowHeight = UITableView.automaticDimension
  158. }
  159. override func viewWillAppear(_ animated: Bool) {
  160. super.viewWillAppear(animated)
  161. updateCells()
  162. }
  163. override func viewDidAppear(_ animated: Bool) {
  164. super.viewDidAppear(animated)
  165. addProgressAlertListener(dcAccounts: dcAccounts, progressName: dcNotificationImexProgress) { [weak self] in
  166. guard let self = self else { return }
  167. self.progressAlert?.dismiss(animated: true)
  168. }
  169. }
  170. override func viewDidDisappear(_ animated: Bool) {
  171. super.viewDidDisappear(animated)
  172. let nc = NotificationCenter.default
  173. if let backupProgressObserver = self.progressObserver {
  174. nc.removeObserver(backupProgressObserver)
  175. }
  176. }
  177. // MARK: - UITableViewDelegate + UITableViewDatasource
  178. override func numberOfSections(in tableView: UITableView) -> Int {
  179. return sections.count
  180. }
  181. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  182. return sections[section].cells.count
  183. }
  184. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  185. return sections[indexPath.section].cells[indexPath.row]
  186. }
  187. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  188. guard let cell = tableView.cellForRow(at: indexPath), let cellTag = CellTags(rawValue: cell.tag) else {
  189. safe_fatalError()
  190. return
  191. }
  192. tableView.deselectRow(at: indexPath, animated: false)
  193. switch cellTag {
  194. case .autocryptPreferences: break
  195. case .sendAutocryptMessage: sendAutocryptSetupMessage()
  196. case .manageKeys: showManageKeysDialog()
  197. case .experimentalFeatures: showExperimentalDialog()
  198. case .videoChat: showVideoChatInstance()
  199. case .viewLog: showLogViewController()
  200. }
  201. }
  202. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  203. return sections[section].headerTitle
  204. }
  205. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  206. return sections[section].footerTitle
  207. }
  208. // MARK: - actions
  209. @objc private func handleAutocryptPreferencesToggle(_ sender: UISwitch) {
  210. dcContext.e2eeEnabled = sender.isOn
  211. }
  212. private func sendAutocryptSetupMessage() {
  213. let askAlert = UIAlertController(title: String.localized("autocrypt_send_asm_explain_before"), message: nil, preferredStyle: .safeActionSheet)
  214. askAlert.addAction(UIAlertAction(title: String.localized("autocrypt_send_asm_title"), style: .default, handler: { _ in
  215. let sc = self.dcContext.initiateKeyTransfer()
  216. guard var sc = sc else {
  217. return
  218. }
  219. if sc.count == 44 {
  220. // format setup code to the typical 3 x 3 numbers
  221. sc = sc.substring(0, 4) + " - " + sc.substring(5, 9) + " - " + sc.substring(10, 14) + " -\n\n" +
  222. sc.substring(15, 19) + " - " + sc.substring(20, 24) + " - " + sc.substring(25, 29) + " -\n\n" +
  223. sc.substring(30, 34) + " - " + sc.substring(35, 39) + " - " + sc.substring(40, 44)
  224. }
  225. let text = String.localizedStringWithFormat(String.localized("autocrypt_send_asm_explain_after"), sc)
  226. let showAlert = UIAlertController(title: text, message: nil, preferredStyle: .alert)
  227. showAlert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
  228. self.present(showAlert, animated: true, completion: nil)
  229. }))
  230. askAlert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  231. present(askAlert, animated: true, completion: nil)
  232. }
  233. private func showLogViewController() {
  234. let controller = LogViewController(dcContext: dcContext)
  235. navigationController?.pushViewController(controller, animated: true)
  236. }
  237. private func showExperimentalDialog() {
  238. let alert = UIAlertController(title: String.localized("pref_experimental_features"), message: nil, preferredStyle: .safeActionSheet)
  239. let broadcastLists = UserDefaults.standard.bool(forKey: "broadcast_lists")
  240. alert.addAction(UIAlertAction(title: (broadcastLists ? "✔︎ " : "") + String.localized("broadcast_lists"),
  241. style: .default, handler: { [weak self] _ in
  242. guard let self = self else { return }
  243. UserDefaults.standard.set(!broadcastLists, forKey: "broadcast_lists")
  244. if !broadcastLists {
  245. let alert = UIAlertController(title: "Thanks for trying out the experimental feature 🧪 \"Broadcast Lists\"!",
  246. message: "You can now create new \"Broadcast Lists\" from the \"New Chat\" dialog\n\n"
  247. + "In case you are using more than one device, broadcast lists are currently not synced between them\n\n"
  248. + "If you want to quit the experimental feature, you can disable it at \"Settings / Advanced\".",
  249. preferredStyle: .alert)
  250. alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
  251. self.navigationController?.present(alert, animated: true, completion: nil)
  252. }
  253. }))
  254. let locationStreaming = UserDefaults.standard.bool(forKey: "location_streaming")
  255. let title = (locationStreaming ? "✔︎ " : "") + String.localized("pref_on_demand_location_streaming")
  256. alert.addAction(UIAlertAction(title: title, style: .default, handler: { [weak self] _ in
  257. guard let self = self else { return }
  258. UserDefaults.standard.set(!locationStreaming, forKey: "location_streaming")
  259. if !locationStreaming {
  260. let alert = UIAlertController(title: "Thanks for trying out the experimental feature 🧪 \"Location streaming\"",
  261. message: "You will find a corresponding option in the attach menu (the paper clip) of each chat now.\n\n"
  262. + "If you want to quit the experimental feature, you can disable it at \"Settings / Advanced\".",
  263. preferredStyle: .alert)
  264. alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
  265. self.navigationController?.present(alert, animated: true, completion: nil)
  266. } else if self.dcContext.isSendingLocationsToChat(chatId: 0) {
  267. guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
  268. return
  269. }
  270. appDelegate.locationManager.disableLocationStreamingInAllChats()
  271. }
  272. }))
  273. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  274. present(alert, animated: true, completion: nil)
  275. }
  276. private func showManageKeysDialog() {
  277. let alert = UIAlertController(title: String.localized("pref_manage_keys"), message: nil, preferredStyle: .safeActionSheet)
  278. alert.addAction(UIAlertAction(title: String.localized("pref_managekeys_export_secret_keys"), style: .default, handler: { _ in
  279. let msg = String.localizedStringWithFormat(String.localized("pref_managekeys_export_explain"), self.externalPathDescr)
  280. let alert = UIAlertController(title: String.localized("pref_managekeys_export_secret_keys"), message: msg, preferredStyle: .alert)
  281. alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
  282. self.startImex(what: DC_IMEX_EXPORT_SELF_KEYS)
  283. }))
  284. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  285. self.present(alert, animated: true, completion: nil)
  286. }))
  287. alert.addAction(UIAlertAction(title: String.localized("pref_managekeys_import_secret_keys"), style: .default, handler: { _ in
  288. let msg = String.localizedStringWithFormat(String.localized("pref_managekeys_import_explain"), self.externalPathDescr)
  289. let alert = UIAlertController(title: String.localized("pref_managekeys_import_secret_keys"), message: msg, preferredStyle: .alert)
  290. alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
  291. self.startImex(what: DC_IMEX_IMPORT_SELF_KEYS)
  292. }))
  293. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  294. self.present(alert, animated: true, completion: nil)
  295. }))
  296. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  297. present(alert, animated: true, completion: nil)
  298. }
  299. private func presentError(message: String) {
  300. let error = UIAlertController(title: nil, message: message, preferredStyle: .alert)
  301. error.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel))
  302. present(error, animated: true)
  303. }
  304. private func startImex(what: Int32, passphrase: String? = nil) {
  305. let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
  306. if !documents.isEmpty {
  307. showProgressAlert(title: String.localized(what==DC_IMEX_IMPORT_SELF_KEYS ? "pref_managekeys_import_secret_keys" : "pref_managekeys_export_secret_keys"), dcContext: dcContext)
  308. DispatchQueue.main.async {
  309. self.dcAccounts.stopIo()
  310. self.dcContext.imex(what: what, directory: documents[0], passphrase: passphrase)
  311. }
  312. } else {
  313. logger.error("document directory not found")
  314. }
  315. }
  316. // MARK: - updates
  317. private func updateCells() {
  318. videoChatInstanceCell.detailTextLabel?.text = VideoChatInstanceViewController.getValString(val: dcContext.getConfig("webrtc_instance") ?? "")
  319. }
  320. // MARK: - coordinator
  321. private func showVideoChatInstance() {
  322. let videoInstanceController = VideoChatInstanceViewController(dcContext: dcContext)
  323. navigationController?.pushViewController(videoInstanceController, animated: true)
  324. }
  325. }