From a389bd93cb6dd068c06ada10905d8fcf3ac65a84 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Mon, 25 Feb 2019 16:22:52 +0530 Subject: Importing: macOS: Support importing of multiple files at a time Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/UI/TunnelImporter.swift | 40 ++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'WireGuard/WireGuard/UI/TunnelImporter.swift') diff --git a/WireGuard/WireGuard/UI/TunnelImporter.swift b/WireGuard/WireGuard/UI/TunnelImporter.swift index 7625761..e51c70d 100644 --- a/WireGuard/WireGuard/UI/TunnelImporter.swift +++ b/WireGuard/WireGuard/UI/TunnelImporter.swift @@ -4,7 +4,45 @@ import Foundation class TunnelImporter { - static func importFromFile(url: URL, into tunnelsManager: TunnelsManager, sourceVC: AnyObject?, errorPresenterType: ErrorPresenterProtocol.Type, completionHandler: (() -> Void)? = nil) { + static func importFromFile(urls: [URL], into tunnelsManager: TunnelsManager, sourceVC: AnyObject?, errorPresenterType: ErrorPresenterProtocol.Type, completionHandler: (() -> Void)? = nil) { + guard !urls.isEmpty else { + 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() + } + } 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) + } + } + dispatchGroup.notify(queue: .main) { + tunnelsManager.addMultiple(tunnelConfigurations: configs.compactMap { $0 }) { numberSuccessful in + if numberSuccessful == configs.count { + completionHandler?() + 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) + } + } + 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 { -- cgit v1.2.3-59-g8ed1b