SettingsController.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. import UIKit
  2. import DcCore
  3. import DBDebugToolkit
  4. import Intents
  5. internal final class SettingsViewController: UITableViewController, ProgressAlertHandler {
  6. private struct SectionConfigs {
  7. let headerTitle: String?
  8. let footerTitle: String?
  9. let cells: [UITableViewCell]
  10. }
  11. private enum CellTags: Int {
  12. case profile = 0
  13. case showEmails = 2
  14. case blockedContacts = 3
  15. case notifications = 4
  16. case receiptConfirmation = 5
  17. case autocryptPreferences = 6
  18. case sendAutocryptMessage = 7
  19. case exportBackup = 8
  20. case advanced = 9
  21. case help = 10
  22. case autodel = 11
  23. case mediaQuality = 12
  24. case switchAccount = 13
  25. case videoChat = 14
  26. case connectivity = 15
  27. }
  28. private var dcContext: DcContext
  29. private let dcAccounts: DcAccounts
  30. private let externalPathDescr = "File Sharing/Delta Chat"
  31. let documentInteractionController = UIDocumentInteractionController()
  32. private var connectivityChangedObserver: NSObjectProtocol?
  33. // MARK: - ProgressAlertHandler
  34. weak var progressAlert: UIAlertController?
  35. var progressObserver: NSObjectProtocol?
  36. // MARK: - cells
  37. private lazy var profileCell: ContactCell = {
  38. let cell = ContactCell(style: .default, reuseIdentifier: nil)
  39. let cellViewModel = ProfileViewModel(context: dcContext)
  40. cell.updateCell(cellViewModel: cellViewModel)
  41. cell.tag = CellTags.profile.rawValue
  42. cell.accessoryType = .disclosureIndicator
  43. return cell
  44. }()
  45. private lazy var showEmailsCell: UITableViewCell = {
  46. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  47. cell.tag = CellTags.showEmails.rawValue
  48. cell.textLabel?.text = String.localized("pref_show_emails")
  49. cell.accessoryType = .disclosureIndicator
  50. cell.detailTextLabel?.text = SettingsClassicViewController.getValString(val: dcContext.showEmails)
  51. return cell
  52. }()
  53. private lazy var blockedContactsCell: UITableViewCell = {
  54. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  55. cell.tag = CellTags.blockedContacts.rawValue
  56. cell.textLabel?.text = String.localized("pref_blocked_contacts")
  57. cell.accessoryType = .disclosureIndicator
  58. return cell
  59. }()
  60. func autodelSummary() -> String {
  61. let delDeviceAfter = dcContext.getConfigInt("delete_device_after")
  62. let delServerAfter = dcContext.getConfigInt("delete_server_after")
  63. if delDeviceAfter==0 && delServerAfter==0 {
  64. return String.localized("never")
  65. } else {
  66. return String.localized("on")
  67. }
  68. }
  69. private lazy var autodelCell: UITableViewCell = {
  70. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  71. cell.tag = CellTags.autodel.rawValue
  72. cell.textLabel?.text = String.localized("delete_old_messages")
  73. cell.accessoryType = .disclosureIndicator
  74. cell.detailTextLabel?.text = autodelSummary()
  75. return cell
  76. }()
  77. private lazy var mediaQualityCell: UITableViewCell = {
  78. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  79. cell.tag = CellTags.mediaQuality.rawValue
  80. cell.textLabel?.text = String.localized("pref_outgoing_media_quality")
  81. cell.accessoryType = .disclosureIndicator
  82. cell.detailTextLabel?.text = MediaQualityController.getValString(val: dcContext.getConfigInt("media_quality"))
  83. return cell
  84. }()
  85. private lazy var videoChatInstanceCell: UITableViewCell = {
  86. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  87. cell.tag = CellTags.videoChat.rawValue
  88. cell.textLabel?.text = String.localized("videochat_instance")
  89. cell.accessoryType = .disclosureIndicator
  90. cell.detailTextLabel?.text = dcContext.getConfig("webrtc_instance")
  91. return cell
  92. }()
  93. private lazy var notificationSwitch: UISwitch = {
  94. let switchControl = UISwitch()
  95. switchControl.isOn = !UserDefaults.standard.bool(forKey: "notifications_disabled")
  96. switchControl.addTarget(self, action: #selector(handleNotificationToggle(_:)), for: .valueChanged)
  97. return switchControl
  98. }()
  99. private lazy var notificationCell: UITableViewCell = {
  100. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  101. cell.tag = CellTags.notifications.rawValue
  102. cell.textLabel?.text = String.localized("pref_notifications")
  103. cell.accessoryView = notificationSwitch
  104. cell.selectionStyle = .none
  105. return cell
  106. }()
  107. private lazy var receiptConfirmationSwitch: UISwitch = {
  108. let switchControl = UISwitch()
  109. switchControl.isOn = dcContext.mdnsEnabled
  110. switchControl.addTarget(self, action: #selector(handleReceiptConfirmationToggle(_:)), for: .valueChanged)
  111. return switchControl
  112. }()
  113. private lazy var receiptConfirmationCell: UITableViewCell = {
  114. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  115. cell.tag = CellTags.receiptConfirmation.rawValue
  116. cell.textLabel?.text = String.localized("pref_read_receipts")
  117. cell.accessoryView = receiptConfirmationSwitch
  118. cell.selectionStyle = .none
  119. return cell
  120. }()
  121. private lazy var autocryptSwitch: UISwitch = {
  122. let switchControl = UISwitch()
  123. switchControl.isOn = dcContext.e2eeEnabled
  124. switchControl.addTarget(self, action: #selector(handleAutocryptPreferencesToggle(_:)), for: .valueChanged)
  125. return switchControl
  126. }()
  127. private lazy var autocryptPreferencesCell: UITableViewCell = {
  128. let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  129. cell.tag = CellTags.autocryptPreferences.rawValue
  130. cell.textLabel?.text = String.localized("autocrypt_prefer_e2ee")
  131. cell.accessoryView = autocryptSwitch
  132. cell.selectionStyle = .none
  133. return cell
  134. }()
  135. private lazy var sendAutocryptMessageCell: ActionCell = {
  136. let cell = ActionCell()
  137. cell.tag = CellTags.sendAutocryptMessage.rawValue
  138. cell.actionTitle = String.localized("autocrypt_send_asm_title")
  139. return cell
  140. }()
  141. private lazy var exportBackupCell: ActionCell = {
  142. let cell = ActionCell()
  143. cell.tag = CellTags.exportBackup.rawValue
  144. cell.actionTitle = String.localized("export_backup_desktop")
  145. return cell
  146. }()
  147. private lazy var advancedCell: ActionCell = {
  148. let cell = ActionCell()
  149. cell.tag = CellTags.advanced.rawValue
  150. cell.actionTitle = String.localized("menu_advanced")
  151. return cell
  152. }()
  153. private lazy var switchAccountCell: ActionCell = {
  154. let cell = ActionCell()
  155. cell.tag = CellTags.switchAccount.rawValue
  156. cell.actionTitle = String.localized("switch_account")
  157. cell.selectionStyle = .default
  158. return cell
  159. }()
  160. private lazy var helpCell: ActionCell = {
  161. let cell = ActionCell()
  162. cell.tag = CellTags.help.rawValue
  163. cell.actionTitle = String.localized("menu_help")
  164. return cell
  165. }()
  166. private lazy var connectivityCell: UITableViewCell = {
  167. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  168. cell.tag = CellTags.connectivity.rawValue
  169. cell.textLabel?.text = String.localized("connectivity")
  170. cell.accessoryType = .disclosureIndicator
  171. return cell
  172. }()
  173. private lazy var sections: [SectionConfigs] = {
  174. var appNameAndVersion = "Delta Chat"
  175. if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  176. appNameAndVersion += " v" + appVersion
  177. }
  178. let profileSection = SectionConfigs(
  179. headerTitle: String.localized("pref_profile_info_headline"),
  180. footerTitle: nil,
  181. cells: [profileCell, switchAccountCell]
  182. )
  183. let preferencesSection = SectionConfigs(
  184. headerTitle: String.localized("pref_chats_and_media"),
  185. footerTitle: String.localized("pref_read_receipts_explain"),
  186. cells: [showEmailsCell, blockedContactsCell, autodelCell, mediaQualityCell, videoChatInstanceCell, notificationCell, receiptConfirmationCell]
  187. )
  188. let autocryptSection = SectionConfigs(
  189. headerTitle: String.localized("autocrypt"),
  190. footerTitle: String.localized("autocrypt_explain"),
  191. cells: [autocryptPreferencesCell, sendAutocryptMessageCell]
  192. )
  193. let backupSection = SectionConfigs(
  194. headerTitle: nil,
  195. footerTitle: String.localized("pref_backup_explain"),
  196. cells: [advancedCell, exportBackupCell])
  197. let helpSection = SectionConfigs(
  198. headerTitle: nil,
  199. footerTitle: appNameAndVersion,
  200. cells: [connectivityCell, helpCell]
  201. )
  202. return [profileSection, preferencesSection, autocryptSection, backupSection, helpSection]
  203. }()
  204. init(dcAccounts: DcAccounts) {
  205. self.dcContext = dcAccounts.getSelected()
  206. self.dcAccounts = dcAccounts
  207. super.init(style: .grouped)
  208. }
  209. required init?(coder _: NSCoder) {
  210. fatalError("init(coder:) has not been implemented")
  211. }
  212. // MARK: - lifecycle
  213. override func viewDidLoad() {
  214. super.viewDidLoad()
  215. title = String.localized("menu_settings")
  216. documentInteractionController.delegate = self as? UIDocumentInteractionControllerDelegate
  217. tableView.rowHeight = UITableView.automaticDimension
  218. }
  219. override func viewWillAppear(_ animated: Bool) {
  220. super.viewWillAppear(animated)
  221. updateCells()
  222. }
  223. override func viewDidAppear(_ animated: Bool) {
  224. super.viewDidAppear(animated)
  225. addProgressAlertListener(dcAccounts: dcAccounts, progressName: dcNotificationImexProgress) { [weak self] in
  226. guard let self = self else { return }
  227. self.progressAlert?.dismiss(animated: true, completion: nil)
  228. }
  229. connectivityChangedObserver = NotificationCenter.default.addObserver(forName: dcNotificationConnectivityChanged,
  230. object: nil,
  231. queue: nil) { [weak self] _ in
  232. guard let self = self else { return }
  233. self.connectivityCell.detailTextLabel?.text = DcUtils.getConnectivityString(dcContext: self.dcContext,
  234. connectedString: String.localized("connectivity_connected"))
  235. }
  236. }
  237. override func viewDidDisappear(_ animated: Bool) {
  238. super.viewDidDisappear(animated)
  239. let nc = NotificationCenter.default
  240. if let backupProgressObserver = self.progressObserver {
  241. nc.removeObserver(backupProgressObserver)
  242. }
  243. if let connectivityChangedObserver = self.connectivityChangedObserver {
  244. NotificationCenter.default.removeObserver(connectivityChangedObserver)
  245. }
  246. }
  247. // MARK: - UITableViewDelegate + UITableViewDatasource
  248. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  249. if indexPath.section == 0 && indexPath.row == 0 {
  250. return ContactCell.cellHeight
  251. } else {
  252. return UITableView.automaticDimension
  253. }
  254. }
  255. override func numberOfSections(in tableView: UITableView) -> Int {
  256. return sections.count
  257. }
  258. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  259. return sections[section].cells.count
  260. }
  261. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  262. return sections[indexPath.section].cells[indexPath.row]
  263. }
  264. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  265. guard let cell = tableView.cellForRow(at: indexPath), let cellTag = CellTags(rawValue: cell.tag) else {
  266. safe_fatalError()
  267. return
  268. }
  269. tableView.deselectRow(at: indexPath, animated: false)
  270. switch cellTag {
  271. case .profile: showEditSettingsController()
  272. case .showEmails: showClassicMail()
  273. case .blockedContacts: showBlockedContacts()
  274. case .autodel: showAutodelOptions()
  275. case .mediaQuality: showMediaQuality()
  276. case .videoChat: showVideoChatInstance()
  277. case .notifications: break
  278. case .receiptConfirmation: break
  279. case .autocryptPreferences: break
  280. case .sendAutocryptMessage: sendAutocryptSetupMessage()
  281. case .exportBackup: createBackup()
  282. case .advanced: showAdvancedDialog()
  283. case .switchAccount: showSwitchAccountMenu()
  284. case .help: showHelp()
  285. case .connectivity: showConnectivity()
  286. }
  287. }
  288. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  289. return sections[section].headerTitle
  290. }
  291. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  292. return sections[section].footerTitle
  293. }
  294. // MARK: - actions
  295. private func createBackup() {
  296. let alert = UIAlertController(title: String.localized("pref_backup_export_explain"), message: nil, preferredStyle: .safeActionSheet)
  297. alert.addAction(UIAlertAction(title: String.localized("pref_backup_export_start_button"), style: .default, handler: { _ in
  298. self.dismiss(animated: true, completion: nil)
  299. self.startImex(what: DC_IMEX_EXPORT_BACKUP)
  300. }))
  301. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  302. present(alert, animated: true, completion: nil)
  303. }
  304. @objc private func handleNotificationToggle(_ sender: UISwitch) {
  305. UserDefaults.standard.set(!sender.isOn, forKey: "notifications_disabled")
  306. if sender.isOn {
  307. if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  308. appDelegate.registerForNotifications()
  309. }
  310. } else {
  311. NotificationManager.removeAllNotifications()
  312. }
  313. UserDefaults.standard.synchronize()
  314. NotificationManager.updateApplicationIconBadge(dcContext: dcContext, reset: !sender.isOn)
  315. }
  316. @objc private func handleReceiptConfirmationToggle(_ sender: UISwitch) {
  317. dcContext.mdnsEnabled = sender.isOn
  318. }
  319. @objc private func handleAutocryptPreferencesToggle(_ sender: UISwitch) {
  320. dcContext.e2eeEnabled = sender.isOn
  321. }
  322. private func sendAutocryptSetupMessage() {
  323. let askAlert = UIAlertController(title: String.localized("autocrypt_send_asm_explain_before"), message: nil, preferredStyle: .safeActionSheet)
  324. askAlert.addAction(UIAlertAction(title: String.localized("autocrypt_send_asm_title"), style: .default, handler: { _ in
  325. let waitAlert = UIAlertController(title: String.localized("one_moment"), message: nil, preferredStyle: .alert)
  326. waitAlert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: { _ in self.dcContext.stopOngoingProcess() }))
  327. self.present(waitAlert, animated: true, completion: nil)
  328. DispatchQueue.global(qos: .background).async {
  329. let sc = self.dcContext.initiateKeyTransfer()
  330. DispatchQueue.main.async {
  331. waitAlert.dismiss(animated: true, completion: nil)
  332. guard var sc = sc else {
  333. return
  334. }
  335. if sc.count == 44 {
  336. // format setup code to the typical 3 x 3 numbers
  337. sc = sc.substring(0, 4) + " - " + sc.substring(5, 9) + " - " + sc.substring(10, 14) + " -\n\n" +
  338. sc.substring(15, 19) + " - " + sc.substring(20, 24) + " - " + sc.substring(25, 29) + " -\n\n" +
  339. sc.substring(30, 34) + " - " + sc.substring(35, 39) + " - " + sc.substring(40, 44)
  340. }
  341. let text = String.localizedStringWithFormat(String.localized("autocrypt_send_asm_explain_after"), sc)
  342. let showAlert = UIAlertController(title: text, message: nil, preferredStyle: .alert)
  343. showAlert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
  344. self.present(showAlert, animated: true, completion: nil)
  345. }
  346. }
  347. }))
  348. askAlert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  349. present(askAlert, animated: true, completion: nil)
  350. }
  351. private func showAdvancedDialog() {
  352. let alert = UIAlertController(title: String.localized("menu_advanced"), message: nil, preferredStyle: .safeActionSheet)
  353. alert.addAction(UIAlertAction(title: String.localized("pref_managekeys_export_secret_keys"), style: .default, handler: { _ in
  354. let msg = String.localizedStringWithFormat(String.localized("pref_managekeys_export_explain"), self.externalPathDescr)
  355. let alert = UIAlertController(title: nil, message: msg, preferredStyle: .alert)
  356. alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
  357. self.startImex(what: DC_IMEX_EXPORT_SELF_KEYS)
  358. }))
  359. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  360. self.present(alert, animated: true, completion: nil)
  361. }))
  362. alert.addAction(UIAlertAction(title: String.localized("pref_managekeys_import_secret_keys"), style: .default, handler: { _ in
  363. let msg = String.localizedStringWithFormat(String.localized("pref_managekeys_import_explain"), self.externalPathDescr)
  364. let alert = UIAlertController(title: nil, message: msg, preferredStyle: .alert)
  365. alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
  366. self.startImex(what: DC_IMEX_IMPORT_SELF_KEYS)
  367. }))
  368. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  369. self.present(alert, animated: true, completion: nil)
  370. }))
  371. let locationStreaming = UserDefaults.standard.bool(forKey: "location_streaming")
  372. let title = locationStreaming ?
  373. "Disable on-demand location streaming" : String.localized("pref_on_demand_location_streaming")
  374. alert.addAction(UIAlertAction(title: title, style: .default, handler: { [weak self] _ in
  375. guard let self = self else { return }
  376. UserDefaults.standard.set(!locationStreaming, forKey: "location_streaming")
  377. if !locationStreaming {
  378. let alert = UIAlertController(title: "Thanks for trying out the experimental feature 🧪 \"Location streaming\"",
  379. message: "You will find a corresponding option in the attach menu (the paper clip) of each chat now.\n\n"
  380. + "If you want to quit the experimental feature, you can disable it at \"Settings / Advanced\".",
  381. preferredStyle: .alert)
  382. alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
  383. self.navigationController?.present(alert, animated: true, completion: nil)
  384. } else if self.dcContext.isSendingLocationsToChat(chatId: 0) {
  385. guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
  386. return
  387. }
  388. appDelegate.locationManager.disableLocationStreamingInAllChats()
  389. }
  390. }))
  391. let logAction = UIAlertAction(title: String.localized("pref_view_log"), style: .default, handler: { [weak self] _ in
  392. guard let self = self else { return }
  393. SettingsViewController.showDebugToolkit(dcContext: self.dcContext)
  394. })
  395. alert.addAction(logAction)
  396. alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  397. present(alert, animated: true, completion: nil)
  398. }
  399. private func presentError(message: String) {
  400. let error = UIAlertController(title: nil, message: message, preferredStyle: .alert)
  401. error.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel))
  402. present(error, animated: true)
  403. }
  404. private func showSwitchAccountMenu() {
  405. let accountIds = dcAccounts.getAll()
  406. let selectedAccountId = dcAccounts.getSelected().id
  407. guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
  408. // switch account
  409. let menu = UIAlertController(title: String.localized("switch_account"), message: nil, preferredStyle: .safeActionSheet)
  410. for accountId in accountIds {
  411. let account = dcAccounts.get(id: accountId)
  412. var title = account.displaynameAndAddr
  413. title = (selectedAccountId==accountId ? "✔︎ " : "") + title
  414. menu.addAction(UIAlertAction(title: title, style: .default, handler: { [weak self] _ in
  415. guard let self = self else { return }
  416. _ = self.dcAccounts.select(id: accountId)
  417. appDelegate.reloadDcContext()
  418. }))
  419. }
  420. // add account
  421. menu.addAction(UIAlertAction(title: String.localized("add_account"), style: .default, handler: { [weak self] _ in
  422. guard let self = self else { return }
  423. _ = self.dcAccounts.add()
  424. appDelegate.reloadDcContext()
  425. }))
  426. // delete account
  427. menu.addAction(UIAlertAction(title: String.localized("delete_account"), style: .default, handler: { [weak self] _ in
  428. let confirm1 = UIAlertController(title: String.localized("delete_account_ask"), message: nil, preferredStyle: .safeActionSheet)
  429. confirm1.addAction(UIAlertAction(title: String.localized("delete_account"), style: .destructive, handler: { [weak self] _ in
  430. guard let self = self else { return }
  431. let account = self.dcAccounts.get(id: selectedAccountId)
  432. let confirm2 = UIAlertController(title: account.displaynameAndAddr,
  433. message: String.localized("forget_login_confirmation_desktop"), preferredStyle: .alert)
  434. confirm2.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: { [weak self] _ in
  435. guard let self = self else { return }
  436. appDelegate.locationManager.disableLocationStreamingInAllChats()
  437. _ = self.dcAccounts.remove(id: selectedAccountId)
  438. INInteraction.delete(with: "\(selectedAccountId)", completion: nil)
  439. if self.dcAccounts.getAll().isEmpty {
  440. _ = self.dcAccounts.add()
  441. }
  442. appDelegate.reloadDcContext()
  443. }))
  444. confirm2.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))
  445. self.present(confirm2, animated: true, completion: nil)
  446. }))
  447. confirm1.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))
  448. self?.present(confirm1, animated: true, completion: nil)
  449. }))
  450. menu.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
  451. present(menu, animated: true, completion: nil)
  452. }
  453. private func startImex(what: Int32) {
  454. let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
  455. if !documents.isEmpty {
  456. showProgressAlert(title: String.localized("imex_progress_title_desktop"), dcContext: dcContext)
  457. DispatchQueue.main.async {
  458. self.dcAccounts.stopIo()
  459. self.dcContext.imex(what: what, directory: documents[0])
  460. }
  461. } else {
  462. logger.error("document directory not found")
  463. }
  464. }
  465. // MARK: - updates
  466. private func updateCells() {
  467. profileCell.updateCell(cellViewModel: ProfileViewModel(context: dcContext))
  468. showEmailsCell.detailTextLabel?.text = SettingsClassicViewController.getValString(val: dcContext.showEmails)
  469. mediaQualityCell.detailTextLabel?.text = MediaQualityController.getValString(val: dcContext.getConfigInt("media_quality"))
  470. videoChatInstanceCell.detailTextLabel?.text = dcContext.getConfig("webrtc_instance")
  471. autodelCell.detailTextLabel?.text = autodelSummary()
  472. connectivityCell.detailTextLabel?.text = DcUtils.getConnectivityString(dcContext: dcContext,
  473. connectedString: String.localized("connectivity_connected"))
  474. }
  475. // MARK: - coordinator
  476. private func showEditSettingsController() {
  477. let editController = EditSettingsController(dcAccounts: dcAccounts)
  478. navigationController?.pushViewController(editController, animated: true)
  479. }
  480. private func showClassicMail() {
  481. let settingsClassicViewController = SettingsClassicViewController(dcContext: dcContext)
  482. navigationController?.pushViewController(settingsClassicViewController, animated: true)
  483. }
  484. private func showMediaQuality() {
  485. let mediaQualityController = MediaQualityController(dcContext: dcContext)
  486. navigationController?.pushViewController(mediaQualityController, animated: true)
  487. }
  488. private func showVideoChatInstance() {
  489. let videoInstanceController = SettingsVideoChatViewController(dcContext: dcContext)
  490. navigationController?.pushViewController(videoInstanceController, animated: true)
  491. }
  492. private func showBlockedContacts() {
  493. let blockedContactsController = BlockedContactsViewController(dcContext: dcContext)
  494. navigationController?.pushViewController(blockedContactsController, animated: true)
  495. }
  496. private func showAutodelOptions() {
  497. let settingsAutodelOverviewController = SettingsAutodelOverviewController(dcContext: dcContext)
  498. navigationController?.pushViewController(settingsAutodelOverviewController, animated: true)
  499. }
  500. private func showHelp() {
  501. navigationController?.pushViewController(HelpViewController(), animated: true)
  502. }
  503. private func showConnectivity() {
  504. navigationController?.pushViewController(ConnectivityViewController(dcContext: dcContext), animated: true)
  505. }
  506. public static func showDebugToolkit(dcContext: DcContext) {
  507. var info = ""
  508. let systemVersion = UIDevice.current.systemVersion
  509. info += "iosVersion=\(systemVersion)\n"
  510. let notifyEnabled = !UserDefaults.standard.bool(forKey: "notifications_disabled")
  511. info += "notify-enabled=\(notifyEnabled)\n"
  512. if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  513. info += "notify-token=\(appDelegate.notifyToken ?? "<unset>")\n"
  514. }
  515. for name in ["notify-remote-launch", "notify-remote-receive", "notify-local-wakeup"] {
  516. let cnt = UserDefaults.standard.integer(forKey: name + "-count")
  517. let startDbl = UserDefaults.standard.double(forKey: name + "-start")
  518. let startStr = startDbl==0.0 ? "" : " since " + DateUtils.getExtendedRelativeTimeSpanString(timeStamp: startDbl)
  519. let timestampDbl = UserDefaults.standard.double(forKey: name + "-last")
  520. let timestampStr = timestampDbl==0.0 ? "" : ", last " + DateUtils.getExtendedRelativeTimeSpanString(timeStamp: timestampDbl)
  521. info += "\(name)=\(cnt)x\(startStr)\(timestampStr)\n"
  522. }
  523. var val = "?"
  524. switch UIApplication.shared.backgroundRefreshStatus {
  525. case .restricted: val = "restricted"
  526. case .available: val = "available"
  527. case .denied: val = "denied"
  528. }
  529. info += "backgroundRefreshStatus=\(val)\n"
  530. #if DEBUG
  531. info += "DEBUG=1\n"
  532. #else
  533. info += "DEBUG=0\n"
  534. #endif
  535. info += "\n" + dcContext.getInfo()
  536. DBDebugToolkit.add(DBCustomVariable(name: "", value: info))
  537. DBDebugToolkit.showMenu()
  538. }
  539. }