Forráskód Böngészése

Merge pull request #249 from deltachat/send-asm

send autocrypt setup message
björn petersen 5 éve
szülő
commit
15c127700e

+ 53 - 3
deltachat-ios/Controller/SettingsController.swift

@@ -5,6 +5,8 @@ import UIKit
 internal final class SettingsViewController: QuickTableViewController {
     weak var coordinator: SettingsCoordinator?
 
+    private var dcContext: DcContext
+
     let documentInteractionController = UIDocumentInteractionController()
     var backupProgressObserver: Any?
     var configureProgressObserver: Any?
@@ -22,6 +24,15 @@ internal final class SettingsViewController: QuickTableViewController {
     static let MvToMvbox: Int = 6
     private typealias SVC = SettingsViewController
 
+    init(dcContext: DcContext) {
+        self.dcContext = dcContext
+        super.init(nibName: nil, bundle: nil)
+    }
+
+    required init?(coder _: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
     override func viewDidLoad() {
         super.viewDidLoad()
         title = String.localized("menu_settings")
@@ -106,9 +117,6 @@ internal final class SettingsViewController: QuickTableViewController {
             Section(
                 title: String.localized("pref_communication"),
                 rows: [
-                    SwitchRow(text: String.localized("autocrypt_prefer_e2ee"),
-                              switchValue: DcConfig.e2eeEnabled,
-                              action: editCell(key: SVC.e2eeEnabled)),
                     SwitchRow(text: String.localized("pref_read_receipts"),
                               switchValue: DcConfig.mdnsEnabled,
                               action: editCell(key: SVC.readReceipts)),
@@ -135,6 +143,17 @@ internal final class SettingsViewController: QuickTableViewController {
                 footer: String.localized("pref_auto_folder_moves_explain")
             ),
 
+            Section(
+                title: String.localized("autocrypt"),
+                rows: [
+                    SwitchRow(text: String.localized("autocrypt_prefer_e2ee"),
+                              switchValue: DcConfig.e2eeEnabled,
+                              action: editCell(key: SVC.e2eeEnabled)),
+                    TapActionRow(text: String.localized("autocrypt_send_asm_title"), action: { [weak self] in self?.sendAsm($0) }),
+                ],
+                footer: String.localized("autocrypt_explain")
+            ),
+
             Section(
                 title: String.localized("pref_backup"),
                 rows: [
@@ -200,6 +219,37 @@ internal final class SettingsViewController: QuickTableViewController {
         present(alert, animated: true, completion: nil)
     }
 
+    private func sendAsm(_: Row) {
+        let askAlert = UIAlertController(title: String.localized("autocrypt_send_asm_explain_before"), message: nil, preferredStyle: .actionSheet)
+        askAlert.addAction(UIAlertAction(title: String.localized("autocrypt_send_asm_title"), style: .default, handler: { _ in
+            let waitAlert = UIAlertController(title: String.localized("one_moment"), message: nil, preferredStyle: .alert)
+            waitAlert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: { _ in self.dcContext.stopOngoingProcess() }))
+            self.present(waitAlert, animated: true, completion: nil)
+            DispatchQueue.global(qos: .background).async {
+                let sc = self.dcContext.intiateKeyTransfer()
+                DispatchQueue.main.async {
+                    waitAlert.dismiss(animated: true, completion: nil)
+                    guard var sc = sc else {
+                        return
+                    }
+                    if sc.count == 44 {
+                        // format setup code to the typical 3 x 3 numbers
+                        sc = sc.substring(0, 4) + "  -  " + sc.substring(5, 9) + "  -  " + sc.substring(10, 14) + "  -\n\n" +
+                            sc.substring(15, 19) + "  -  " + sc.substring(20, 24) + "  -  " + sc.substring(25, 29) + "  -\n\n" +
+                            sc.substring(30, 34) + "  -  " + sc.substring(35, 39) + "  -  " + sc.substring(40, 44)
+                    }
+
+                    let text = String.localizedStringWithFormat(String.localized("autocrypt_send_asm_explain_after"), sc)
+                    let showAlert = UIAlertController(title: text, message: nil, preferredStyle: .alert)
+                    showAlert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
+                    self.present(showAlert, animated: true, completion: nil)
+                }
+            }
+        }))
+        askAlert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
+        present(askAlert, animated: true, completion: nil)
+    }
+
     private func configure(_: Row) {
         hudHandler.showHud(String.localized("configuring_account"))
         dc_configure(mailboxPointer)

+ 1 - 1
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -62,7 +62,7 @@ class AppCoordinator: NSObject, Coordinator {
     }()
 
     private lazy var settingsController: UIViewController = {
-        let controller = SettingsViewController()
+        let controller = SettingsViewController(dcContext: dcContext)
         let nav = DcNavigationController(rootViewController: controller)
         let settingsImage = UIImage(named: "settings")
         nav.tabBarItem = UITabBarItem(title: String.localized("menu_settings"), image: settingsImage, tag: settingsTab)

+ 8 - 0
deltachat-ios/DC/Wrapper.swift

@@ -61,6 +61,14 @@ class DcContext {
         return "ErrGetMsgInfo"
     }
 
+    func intiateKeyTransfer() -> String? {
+        if let cString = dc_initiate_key_transfer(self.contextPointer) {
+            let swiftString = String(cString: cString)
+            free(cString)
+            return swiftString
+        }
+        return nil
+    }
 }
 
 class DcConfig {

+ 7 - 0
deltachat-ios/Helper/Extensions.swift

@@ -1,6 +1,13 @@
 import UIKit
 
 extension String {
+
+    func substring(_ from: Int, _ to: Int) -> String {
+        let idx1 = index(startIndex, offsetBy: from)
+        let idx2 = index(startIndex, offsetBy: to)
+        return String(self[idx1..<idx2])
+    }
+
     func containsCharacters() -> Bool {
         return !trimmingCharacters(in: [" "]).isEmpty
     }