2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// AccountViewController.swift
|
2021-01-01 16:53:28 +00:00
|
|
|
// Passepartout
|
2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 7/29/18.
|
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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 Cocoa
|
2021-10-27 16:47:08 +00:00
|
|
|
import TunnelKitCore
|
|
|
|
import PassepartoutConstants
|
2021-01-01 10:13:41 +00:00
|
|
|
import PassepartoutCore
|
|
|
|
|
2021-06-26 07:49:48 +00:00
|
|
|
protocol AccountViewControllerDelegate: AnyObject {
|
2021-01-01 10:13:41 +00:00
|
|
|
func accountController(_ accountController: AccountViewController, shouldUpdateCredentials credentials: Credentials, forProfile profile: ConnectionProfile) -> Bool
|
|
|
|
|
|
|
|
func accountController(_ accountController: AccountViewController, didUpdateCredentials credentials: Credentials, forProfile profile: ConnectionProfile)
|
|
|
|
|
|
|
|
func accountControllerDidCancel(_ accountController: AccountViewController)
|
|
|
|
}
|
|
|
|
|
|
|
|
class AccountViewController: NSViewController {
|
|
|
|
@IBOutlet private weak var labelUsernameCaption: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var textUsername: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var labelPasswordCaption: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var textPassword: NSSecureTextField!
|
|
|
|
|
2021-04-10 13:49:51 +00:00
|
|
|
@IBOutlet private weak var labelGuidance: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var buttonGuidance: NSButton!
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
@IBOutlet private weak var buttonOK: NSButton!
|
|
|
|
|
|
|
|
@IBOutlet private weak var buttonCancel: NSButton!
|
|
|
|
|
|
|
|
private let service = TransientStore.shared.service
|
|
|
|
|
|
|
|
var profile: ConnectionProfile!
|
|
|
|
|
|
|
|
weak var delegate: AccountViewControllerDelegate?
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
assert(profile != nil, "Profile not set")
|
|
|
|
|
2021-08-07 11:57:22 +00:00
|
|
|
labelUsernameCaption.stringValue = L10n.Account.Cells.Username.caption.asCaption
|
2021-01-01 10:13:41 +00:00
|
|
|
if let providerProfile = profile as? ProviderConnectionProfile {
|
|
|
|
textUsername.placeholderString = providerProfile.infrastructure.defaults.username
|
|
|
|
} else {
|
2021-08-07 11:57:22 +00:00
|
|
|
textUsername.placeholderString = L10n.Account.Cells.Username.placeholder
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2021-08-07 11:57:22 +00:00
|
|
|
labelPasswordCaption.stringValue = L10n.Account.Cells.Password.caption.asCaption
|
|
|
|
textPassword.placeholderString = L10n.Account.Cells.Password.placeholder
|
|
|
|
buttonGuidance.title = L10n.Account.Cells.OpenGuide.caption
|
|
|
|
buttonOK.title = L10n.Global.ok
|
|
|
|
buttonCancel.title = L10n.Global.cancel
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
let credentials = service.credentials(for: profile)
|
|
|
|
textUsername.stringValue = credentials?.username ?? ""
|
|
|
|
textPassword.stringValue = credentials?.password ?? ""
|
2021-04-10 13:49:51 +00:00
|
|
|
|
|
|
|
if let guidanceString = guidanceString, !guidanceString.isEmpty {
|
|
|
|
labelGuidance.stringValue = guidanceString
|
|
|
|
buttonGuidance.isHidden = (guidanceURL == nil)
|
|
|
|
} else {
|
|
|
|
labelGuidance.isHidden = true
|
|
|
|
buttonGuidance.isHidden = true
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2021-04-10 13:49:51 +00:00
|
|
|
|
2021-04-10 21:24:51 +00:00
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction private func openGuidanceURL(_ sender: Any?) {
|
2021-04-10 13:49:51 +00:00
|
|
|
guard let url = guidanceURL else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
NSWorkspace.shared.open(url)
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
@IBAction private func confirm(_ sender: Any?) {
|
|
|
|
let username = textUsername.stringValue
|
|
|
|
let password = textPassword.stringValue
|
|
|
|
let credentials = Credentials(username, password)
|
|
|
|
if let delegate = delegate {
|
|
|
|
guard delegate.accountController(self, shouldUpdateCredentials: credentials, forProfile: profile) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
try service.setCredentials(credentials, for: profile)
|
|
|
|
} catch {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
delegate?.accountController(self, didUpdateCredentials: credentials, forProfile: profile)
|
2021-04-10 07:15:09 +00:00
|
|
|
presentingViewController?.dismiss(self)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction private func delegateAndDismiss(_ sender: Any?) {
|
|
|
|
delegate?.accountControllerDidCancel(self)
|
2021-04-10 07:15:09 +00:00
|
|
|
presentingViewController?.dismiss(self)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func cancelOperation(_ sender: Any?) {
|
|
|
|
delegateAndDismiss(sender)
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 13:49:51 +00:00
|
|
|
|
|
|
|
extension AccountViewController {
|
|
|
|
private var guidanceString: String? {
|
2021-04-10 21:24:51 +00:00
|
|
|
return metadata?.guidanceString
|
2021-04-10 13:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var guidanceURL: URL? {
|
2021-04-10 21:24:51 +00:00
|
|
|
return metadata?.guidanceURL
|
|
|
|
}
|
|
|
|
|
|
|
|
private var referralURL: URL? {
|
|
|
|
return metadata?.referralURL
|
|
|
|
}
|
|
|
|
|
|
|
|
private var metadata: Infrastructure.Metadata? {
|
|
|
|
guard let providerProfile = profile as? ProviderConnectionProfile else {
|
2021-04-10 13:49:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-10 21:24:51 +00:00
|
|
|
return InfrastructureFactory.shared.metadata(forName: providerProfile.name)
|
2021-04-10 13:49:51 +00:00
|
|
|
}
|
|
|
|
}
|