passepartout-apple/Passepartout-iOS/Global/ConfigurationParserResult+A...

139 lines
5.5 KiB
Swift
Raw Normal View History

//
// ConfigurationParserResult+Alerts.swift
// Passepartout-iOS
//
// Created by Davide De Rosa on 10/27/18.
2019-03-09 10:44:44 +00:00
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
//
2018-11-03 21:33:30 +00:00
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
import UIKit
import TunnelKit
import SwiftyBeaver
2019-03-18 10:52:19 +00:00
import Passepartout_Core
private let log = SwiftyBeaver.self
extension ConfigurationParser.Result {
static func from(_ url: URL, withErrorAlertIn viewController: UIViewController, passphrase: String?,
passphraseBlock: @escaping (String) -> Void, passphraseCancelBlock: (() -> Void)?) -> ConfigurationParser.Result? {
let result: ConfigurationParser.Result
let fm = FileManager.default
log.debug("Parsing configuration URL: \(url)")
do {
2019-03-25 18:15:48 +00:00
result = try ConfigurationParser.parsed(fromURL: url, passphrase: passphrase)
} catch let e as ConfigurationError {
switch e {
case .encryptionPassphrase, .unableToDecrypt(_):
let alert = Macros.alert(url.normalizedFilename, L10n.ParsedFile.Alerts.EncryptionPassphrase.message)
2019-03-26 10:23:49 +00:00
alert.addTextField { (field) in
field.isSecureTextEntry = true
}
alert.addDefaultAction(L10n.Global.ok) {
guard let passphrase = alert.textFields?.first?.text else {
return
}
passphraseBlock(passphrase)
}
alert.addCancelAction(L10n.Global.cancel) {
passphraseCancelBlock?()
}
viewController.present(alert, animated: true, completion: nil)
default:
let message = localizedMessage(forError: e)
alertImportError(url: url, in: viewController, withMessage: message)
try? fm.removeItem(at: url)
}
return nil
} catch let e {
let message = localizedMessage(forError: e)
alertImportError(url: url, in: viewController, withMessage: message)
try? fm.removeItem(at: url)
return nil
}
2018-11-10 09:29:51 +00:00
return result
}
private static func alertImportError(url: URL, in vc: UIViewController, withMessage message: String) {
let alert = Macros.alert(url.normalizedFilename, message)
// alert.addDefaultAction(L10n.ParsedFile.Alerts.Buttons.report) {
// var attach = IssueReporter.Attachments(debugLog: false, configurationURL: url)
// attach.description = message
// IssueReporter.shared.present(in: vc, withAttachments: attach)
// }
alert.addCancelAction(L10n.Global.ok)
vc.present(alert, animated: true, completion: nil)
}
static func alertImportWarning(url: URL, in vc: UIViewController, withWarning warning: ConfigurationError, completionHandler: @escaping (Bool) -> Void) {
let message = details(forWarning: warning)
let alert = Macros.alert(url.normalizedFilename, L10n.ParsedFile.Alerts.PotentiallyUnsupported.message(message))
alert.addDefaultAction(L10n.Global.ok) {
completionHandler(true)
}
alert.addCancelAction(L10n.Global.cancel) {
completionHandler(false)
}
vc.present(alert, animated: true, completion: nil)
}
private static func localizedMessage(forError error: Error) -> String {
if let appError = error as? ConfigurationError {
switch appError {
case .malformed(let option):
log.error("Could not parse configuration URL: malformed option, \(option)")
return L10n.ParsedFile.Alerts.Malformed.message(option)
case .missingConfiguration(let option):
log.error("Could not parse configuration URL: missing configuration, \(option)")
return L10n.ParsedFile.Alerts.Missing.message(option)
case .unsupportedConfiguration(let option):
log.error("Could not parse configuration URL: unsupported configuration, \(option)")
return L10n.ParsedFile.Alerts.Unsupported.message(option)
default:
break
}
}
log.error("Could not parse configuration URL: \(error)")
return L10n.ParsedFile.Alerts.Parsing.message(error.localizedDescription)
}
private static func details(forWarning warning: ConfigurationError) -> String {
switch warning {
case .malformed(let option):
return option
case .missingConfiguration(let option):
return option
case .unsupportedConfiguration(let option):
return option
default:
return "" // XXX: should never get here
}
}
}