2018-10-27 08:25:21 +00:00
|
|
|
//
|
|
|
|
// ImportedHostsViewController.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-10-27 08:25:21 +00:00
|
|
|
//
|
2018-11-03 21:33:30 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-10-27 08:25:21 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
//
|
2018-10-27 09:14:06 +00:00
|
|
|
|
2018-10-27 08:25:21 +00:00
|
|
|
import UIKit
|
2018-10-27 09:14:06 +00:00
|
|
|
import TunnelKit
|
|
|
|
import SwiftyBeaver
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2018-10-27 09:14:06 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
2018-10-27 08:25:21 +00:00
|
|
|
|
|
|
|
class ImportedHostsViewController: UITableViewController {
|
2018-10-27 14:48:38 +00:00
|
|
|
private lazy var pendingConfigurationURLs = TransientStore.shared.service.pendingConfigurationURLs().sortedCaseInsensitive()
|
2018-10-27 10:23:17 +00:00
|
|
|
|
2019-05-23 21:13:45 +00:00
|
|
|
private var parsingResult: OpenVPN.ConfigurationParser.Result?
|
2018-10-27 10:12:01 +00:00
|
|
|
|
2018-10-27 08:25:21 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-06-21 21:15:43 +00:00
|
|
|
title = L10n.App.ImportedHosts.title
|
2018-10-27 08:25:21 +00:00
|
|
|
}
|
2018-10-27 12:40:52 +00:00
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
2018-11-10 09:29:51 +00:00
|
|
|
parsingResult = nil
|
2018-10-27 12:40:52 +00:00
|
|
|
}
|
2018-10-27 08:25:21 +00:00
|
|
|
|
2018-10-27 09:14:06 +00:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
guard !pendingConfigurationURLs.isEmpty else {
|
|
|
|
let alert = Macros.alert(
|
2019-03-27 19:35:24 +00:00
|
|
|
title,
|
2019-06-21 21:15:43 +00:00
|
|
|
L10n.Core.Organizer.Alerts.AddHost.message
|
2018-10-27 09:14:06 +00:00
|
|
|
)
|
2019-06-21 21:15:43 +00:00
|
|
|
alert.addCancelAction(L10n.Core.Global.ok) {
|
2018-10-27 09:14:06 +00:00
|
|
|
self.close()
|
|
|
|
}
|
|
|
|
present(alert, animated: true, completion: nil)
|
|
|
|
return
|
|
|
|
}
|
2018-10-27 12:40:52 +00:00
|
|
|
if let selectedIP = tableView.indexPathForSelectedRow {
|
|
|
|
tableView.deselectRow(at: selectedIP, animated: true)
|
|
|
|
}
|
2018-10-27 09:14:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 08:25:21 +00:00
|
|
|
// MARK: Actions
|
|
|
|
|
2018-10-27 09:14:06 +00:00
|
|
|
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
2018-10-27 12:40:52 +00:00
|
|
|
|
|
|
|
// segue parses configuration file if not yet
|
2018-11-10 09:29:51 +00:00
|
|
|
if parsingResult == nil {
|
2018-10-27 12:40:52 +00:00
|
|
|
guard let cell = sender as? UITableViewCell, let indexPath = tableView.indexPath(for: cell) else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
let url = pendingConfigurationURLs[indexPath.row]
|
2019-03-25 19:33:07 +00:00
|
|
|
return tryParseURL(url, passphrase: nil, cell: cell)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
private func tryParseURL(_ url: URL, passphrase: String?, cell: UITableViewCell) -> Bool {
|
|
|
|
let passphraseBlock: (String) -> Void = { (passphrase) in
|
|
|
|
guard self.tryParseURL(url, passphrase: passphrase, cell: cell) else {
|
|
|
|
return
|
2018-10-27 12:40:52 +00:00
|
|
|
}
|
2019-03-25 19:33:07 +00:00
|
|
|
self.perform(segue: StoryboardSegue.Organizer.importHostSegueIdentifier, sender: cell)
|
|
|
|
}
|
2019-05-23 21:13:45 +00:00
|
|
|
guard let parsingResult = OpenVPN.ConfigurationParser.Result.from(url, withErrorAlertIn: self, passphrase: passphrase, passphraseBlock: passphraseBlock, passphraseCancelBlock: nil) else {
|
2019-03-25 19:33:07 +00:00
|
|
|
deselectSelectedRow()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
self.parsingResult = parsingResult
|
|
|
|
|
|
|
|
// postpone segue until alert dismissal
|
|
|
|
if let warning = parsingResult.warning {
|
2019-05-23 21:13:45 +00:00
|
|
|
OpenVPN.ConfigurationParser.Result.alertImportWarning(url: url, in: self, withWarning: warning) {
|
2019-03-25 19:33:07 +00:00
|
|
|
self.deselectSelectedRow()
|
|
|
|
if $0 {
|
|
|
|
self.perform(segue: StoryboardSegue.Organizer.importHostSegueIdentifier)
|
|
|
|
} else {
|
|
|
|
self.parsingResult = nil
|
2018-10-27 12:40:52 +00:00
|
|
|
}
|
2018-10-27 10:15:19 +00:00
|
|
|
}
|
2019-03-25 19:33:07 +00:00
|
|
|
return false
|
2018-10-27 09:14:06 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
guard let wizard = segue.destination as? WizardHostViewController else {
|
|
|
|
return
|
|
|
|
}
|
2018-11-10 09:29:51 +00:00
|
|
|
wizard.parsingResult = parsingResult
|
2018-10-27 12:43:09 +00:00
|
|
|
|
|
|
|
// retain back button
|
|
|
|
wizard.navigationItem.leftBarButtonItem = nil
|
2018-10-27 09:14:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 08:25:21 +00:00
|
|
|
@IBAction private func close() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
2018-10-27 12:40:52 +00:00
|
|
|
|
|
|
|
private func deselectSelectedRow() {
|
|
|
|
if let selectedIP = tableView.indexPathForSelectedRow {
|
|
|
|
tableView.deselectRow(at: selectedIP, animated: true)
|
|
|
|
}
|
|
|
|
}
|
2018-10-27 08:25:21 +00:00
|
|
|
}
|
2018-10-27 09:14:06 +00:00
|
|
|
|
|
|
|
extension ImportedHostsViewController {
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
return pendingConfigurationURLs.count
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let url = pendingConfigurationURLs[indexPath.row]
|
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
|
|
|
cell.leftText = url.normalizedFilename
|
|
|
|
return cell
|
|
|
|
}
|
2018-10-27 10:28:40 +00:00
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
|
|
|
let url = pendingConfigurationURLs[indexPath.row]
|
|
|
|
try? FileManager.default.removeItem(at: url)
|
|
|
|
pendingConfigurationURLs.remove(at: indexPath.row)
|
|
|
|
tableView.deleteRows(at: [indexPath], with: .top)
|
|
|
|
}
|
2018-10-27 09:14:06 +00:00
|
|
|
}
|