]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
macOS: Edit view: Validate and save
authorRoopesh Chander <roop@roopc.net>
Tue, 8 Jan 2019 13:28:40 +0000 (18:58 +0530)
committerRoopesh Chander <roop@roopc.net>
Mon, 14 Jan 2019 09:22:34 +0000 (14:52 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard/Base.lproj/Localizable.strings
WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift

index 897d13dca8962370228c2e730aca839c8bc4e1ab..e2a7295051fa448eb65698760732974748e571d6 100644 (file)
 "macEditDiscard" = "Discard";
 "macEditSave" = "Save";
 
+"macAlertNameIsEmpty" = "Name is required";
+"macAlertDuplicateName (%@)" = "Another tunnel already exists with the name '%@'.";
+
 "macAlertInvalidLine (%@)" = "Invalid line: '%@'.";
 
 "macAlertNoInterface" = "Configuration must have an 'Interface' section.";
index 47f488c33faf784ab7824bdb65ebcbc297112d6e..42cc6815962fb87238957925760c13e5415acb23 100644 (file)
@@ -116,7 +116,33 @@ class TunnelEditViewController: NSViewController {
     }
 
     @objc func saveButtonClicked() {
-        print("saveButtonClicked")
+        let name = nameRow.value
+        guard !name.isEmpty else {
+            ErrorPresenter.showErrorAlert(title: tr("macAlertNameIsEmpty"), message: "", from: self)
+            return
+        }
+        if let tunnel = tunnel {
+            // We're modifying an existing tunnel
+            if name != tunnel.name && tunnelsManager.tunnel(named: name) != nil {
+                ErrorPresenter.showErrorAlert(title: tr(format: "macAlertDuplicateName (%@)", name), message: "", from: self)
+                return
+            }
+            do {
+                let tunnelConfiguration = try TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value, ignoreUnrecognizedKeys: false)
+                let onDemandSetting = ActivateOnDemandSetting.defaultSetting
+                tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, activateOnDemandSetting: onDemandSetting) { [weak self] error in
+                    if let error = error {
+                        ErrorPresenter.showErrorAlert(error: error, from: self)
+                        return
+                    }
+                    self?.dismiss(self)
+                }
+            } catch let error as WireGuardAppError {
+                ErrorPresenter.showErrorAlert(error: error, from: self)
+            } catch {
+                fatalError()
+            }
+        }
     }
 
     @objc func discardButtonClicked() {