passepartout-apple/Passepartout/App/Views/AddHostViewModel.swift

116 lines
3.5 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// AddHostViewModel.swift
// Passepartout
//
// Created by Davide De Rosa on 2/14/22.
2023-03-17 15:56:19 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +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
2022-06-23 21:31:01 +00:00
import PassepartoutLibrary
2022-04-12 13:09:14 +00:00
import TunnelKitOpenVPN
import TunnelKitWireGuard
extension AddHostView {
struct ViewModel: Equatable {
private var isNamePreset = false
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var profileName = ""
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private(set) var requiresPassphrase = false
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var encryptionPassphrase = ""
var processedProfile: Profile = .placeholder
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private(set) var errorMessage: String?
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var isAskingOverwrite = false
2023-03-17 20:55:47 +00:00
mutating func presetName(withURL url: URL) {
guard !isNamePreset else {
return
}
isNamePreset = true
profileName = url.filename
}
2022-04-12 13:09:14 +00:00
@MainActor
2022-04-12 13:09:14 +00:00
mutating func processURL(
_ url: URL,
with profileManager: ProfileManager,
replacingExisting: Bool,
deletingURLOnSuccess: Bool
) {
profileName = profileName.stripped
guard !profileName.isEmpty else {
return
}
if !replacingExisting {
guard !profileManager.isExistingProfile(withName: profileName) else {
isAskingOverwrite = true
return
}
}
errorMessage = nil
do {
let profile = try profileManager.profile(
withHeader: .init(name: profileName),
fromURL: url,
passphrase: encryptionPassphrase
)
processedProfile = profile
if deletingURLOnSuccess {
try? FileManager.default.removeItem(at: url)
}
} catch {
2023-07-02 10:51:50 +00:00
if case Passepartout.ProfileError.decryptionFailure = error {
2022-04-12 13:09:14 +00:00
requiresPassphrase = true
2023-07-02 10:51:50 +00:00
} else {
2022-04-12 13:09:14 +00:00
requiresPassphrase = false
}
2023-07-02 10:51:50 +00:00
setErrorMessage(for: error)
2022-04-12 13:09:14 +00:00
}
}
2023-03-17 20:55:47 +00:00
@MainActor
2022-04-12 13:09:14 +00:00
mutating func addProcessedProfile(to profileManager: ProfileManager) -> Bool {
guard !processedProfile.isPlaceholder else {
assertionFailure("Saving profile without processing first?")
return false
}
errorMessage = nil
profileManager.saveProfile(processedProfile, isActive: nil)
return true
}
2023-03-17 20:55:47 +00:00
2023-07-02 10:51:50 +00:00
private mutating func setErrorMessage(for error: Error) {
setErrorMessage(AppError(error).localizedDescription)
}
private mutating func setErrorMessage(_ message: String) {
errorMessage = message.withTrailingDot
2022-04-12 13:09:14 +00:00
}
}
}