]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Importing: Simplify TunnelImporter
authorRoopesh Chander <roop@roopc.net>
Mon, 4 Mar 2019 08:20:06 +0000 (13:50 +0530)
committerRoopesh Chander <roop@roopc.net>
Mon, 4 Mar 2019 08:43:49 +0000 (14:13 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard/UI/TunnelImporter.swift

index e51c70d69a74669519d0b1f0745c9e9027673adf..4fabd07b5a355df46b542cddfa3d46032684c0a4 100644 (file)
@@ -9,86 +9,77 @@ class TunnelImporter {
             completionHandler?()
             return
         }
-        if urls.count > 1 {
-            let dispatchGroup = DispatchGroup()
-            var configs = [TunnelConfiguration?]()
-            for url in urls {
-                if url.pathExtension.lowercased() == "zip" {
-                    dispatchGroup.enter()
-                    ZipImporter.importConfigFiles(from: url) { result in
-                        if let configsInZip = result.value {
-                            configs.append(contentsOf: configsInZip)
-                        }
-                        dispatchGroup.leave()
+        let dispatchGroup = DispatchGroup()
+        var configs = [TunnelConfiguration?]()
+        var lastFileImportErrorText: (title: String, message: String)?
+        for url in urls {
+            if url.pathExtension.lowercased() == "zip" {
+                dispatchGroup.enter()
+                ZipImporter.importConfigFiles(from: url) { result in
+                    if let error = result.error {
+                        lastFileImportErrorText = error.alertText
                     }
-                } else {
-                    let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
-                    let fileContents = try? String(contentsOf: url)
-                    let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents ?? "", called: fileBaseName)
-                    configs.append(tunnelConfiguration)
+                    if let configsInZip = result.value {
+                        configs.append(contentsOf: configsInZip)
+                    }
+                    dispatchGroup.leave()
                 }
-            }
-            dispatchGroup.notify(queue: .main) {
-                tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in
-                    if numberSuccessful == configs.count {
-                        completionHandler?()
+            } else { /* if it is not a zip, we assume it is a conf */
+                let fileName = url.lastPathComponent
+                let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
+                dispatchGroup.enter()
+                DispatchQueue.global(qos: .userInitiated).async {
+                    let fileContents: String
+                    do {
+                        fileContents = try String(contentsOf: url)
+                    } catch let error {
+                        if let cocoaError = error as? CocoaError, cocoaError.isFileError {
+                            lastFileImportErrorText = (title: tr("alertCantOpenInputConfFileTitle"), message: error.localizedDescription)
+                        } else {
+                            lastFileImportErrorText = (title: tr("alertCantOpenInputConfFileTitle"), message: tr(format: "alertCantOpenInputConfFileMessage (%@)", fileName))
+                        }
+                        DispatchQueue.main.async {
+                            configs.append(nil)
+                            dispatchGroup.leave()
+                        }
                         return
                     }
-                    let title = tr(format: "alertImportedFromMultipleFilesTitle (%d)", numberSuccessful)
-                    let message = tr(format: "alertImportedFromMultipleFilesMessage (%1$d of %2$d)", numberSuccessful, configs.count)
-                    errorPresenterType.showErrorAlert(title: title, message: message, from: sourceVC, onPresented: completionHandler)
+                    let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents, called: fileBaseName)
+                    if tunnelConfiguration == nil {
+                        lastFileImportErrorText = (title: tr("alertBadConfigImportTitle"), message: tr(format: "alertBadConfigImportMessage (%@)", fileName))
+                    }
+                    DispatchQueue.main.async {
+                        configs.append(tunnelConfiguration)
+                        dispatchGroup.leave()
+                    }
                 }
             }
-            return
         }
-        assert(urls.count == 1)
-        let url = urls.first!
-        if url.pathExtension.lowercased() == "zip" {
-            ZipImporter.importConfigFiles(from: url) { result in
-                if let error = result.error {
-                    errorPresenterType.showErrorAlert(error: error, from: sourceVC)
+        dispatchGroup.notify(queue: .main) {
+            tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in
+                if !configs.isEmpty && numberSuccessful == configs.count {
+                    completionHandler?()
                     return
                 }
-                let configs = result.value!
-                tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in
-                    if numberSuccessful == configs.count {
-                        completionHandler?()
-                        return
-                    }
-                    let title = tr(format: "alertImportedFromZipTitle (%d)", numberSuccessful)
-                    let message = tr(format: "alertImportedFromZipMessage (%1$d of %2$d)", numberSuccessful, configs.count)
-                    errorPresenterType.showErrorAlert(title: title, message: message, from: sourceVC, onPresented: completionHandler)
-                }
-            }
-        } else /* if (url.pathExtension == "conf") -- we assume everything else is a conf */ {
-            let fileName = url.lastPathComponent
-            let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
-            let fileContents: String
-            do {
-                fileContents = try String(contentsOf: url)
-            } catch let error {
+                let title: String
                 let message: String
-                if let cocoaError = error as? CocoaError, cocoaError.isFileError {
-                    message = error.localizedDescription
-                } else {
-                    message = tr(format: "alertCantOpenInputConfFileMessage (%@)", fileName)
-                }
-                errorPresenterType.showErrorAlert(title: tr("alertCantOpenInputConfFileTitle"), message: message, from: sourceVC, onPresented: completionHandler)
-                return
-            }
-            if let tunnelConfiguration = try? TunnelConfiguration(fromWgQuickConfig: fileContents, called: fileBaseName) {
-                tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { result in
-                    if let error = result.error {
-                        errorPresenterType.showErrorAlert(error: error, from: sourceVC, onPresented: completionHandler)
+                if urls.count == 1 {
+                    if urls.first!.pathExtension.lowercased() == "zip" && !configs.isEmpty {
+                        title = tr(format: "alertImportedFromZipTitle (%d)", numberSuccessful)
+                        message = tr(format: "alertImportedFromZipMessage (%1$d of %2$d)", numberSuccessful, configs.count)
+                    } else if let lastFileImportErrorText = lastFileImportErrorText {
+                        title = lastFileImportErrorText.title
+                        message = lastFileImportErrorText.message
                     } else {
                         completionHandler?()
+                        return
                     }
+                } else {
+                    title = tr(format: "alertImportedFromMultipleFilesTitle (%d)", numberSuccessful)
+                    message = tr(format: "alertImportedFromMultipleFilesMessage (%1$d of %2$d)", numberSuccessful, configs.count)
                 }
-            } else {
-                errorPresenterType.showErrorAlert(title: tr("alertBadConfigImportTitle"), message: tr(format: "alertBadConfigImportMessage (%@)", fileName),
-                                              from: sourceVC, onPresented: completionHandler)
+                errorPresenterType.showErrorAlert(title: title, message: message, from: sourceVC, onPresented: completionHandler)
             }
         }
     }
-
 }