aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/TunnelImporter.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-03-04 13:50:06 +0530
committerRoopesh Chander <roop@roopc.net>2019-03-04 14:13:49 +0530
commit202e7a489096caba89303f7fdae32a57975214cf (patch)
treee5bcc852a08a96f64eeac7dd39924800074c2782 /WireGuard/WireGuard/UI/TunnelImporter.swift
parentwireguard-go-bridge: use go modules (diff)
downloadwireguard-apple-202e7a489096caba89303f7fdae32a57975214cf.tar.xz
wireguard-apple-202e7a489096caba89303f7fdae32a57975214cf.zip
Importing: Simplify TunnelImporter
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/UI/TunnelImporter.swift')
-rw-r--r--WireGuard/WireGuard/UI/TunnelImporter.swift121
1 files changed, 56 insertions, 65 deletions
diff --git a/WireGuard/WireGuard/UI/TunnelImporter.swift b/WireGuard/WireGuard/UI/TunnelImporter.swift
index e51c70d..4fabd07 100644
--- a/WireGuard/WireGuard/UI/TunnelImporter.swift
+++ b/WireGuard/WireGuard/UI/TunnelImporter.swift
@@ -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)
}
}
}
-
}