2019-05-28 19:08:15 +02:00
|
|
|
|
//
|
|
|
|
|
// AccountsAddFeedbinWindowController.swift
|
|
|
|
|
// NetNewsWire
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 5/2/19.
|
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
|
import Account
|
2024-04-02 04:31:57 +02:00
|
|
|
|
import Web
|
2020-04-10 04:07:56 +02:00
|
|
|
|
import Secrets
|
2024-04-06 22:06:24 +02:00
|
|
|
|
import ReaderAPI
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
2019-06-19 18:25:37 +02:00
|
|
|
|
class AccountsReaderAPIWindowController: NSWindowController {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
2019-06-20 14:22:51 +02:00
|
|
|
|
@IBOutlet weak var titleImageView: NSImageView!
|
|
|
|
|
@IBOutlet weak var titleLabel: NSTextField!
|
|
|
|
|
|
2020-10-25 02:42:34 +01:00
|
|
|
|
@IBOutlet weak var gridView: NSGridView!
|
2019-05-28 19:08:15 +02:00
|
|
|
|
@IBOutlet weak var progressIndicator: NSProgressIndicator!
|
|
|
|
|
@IBOutlet weak var usernameTextField: NSTextField!
|
|
|
|
|
@IBOutlet weak var apiURLTextField: NSTextField!
|
|
|
|
|
@IBOutlet weak var passwordTextField: NSSecureTextField!
|
2020-11-07 02:31:52 +01:00
|
|
|
|
@IBOutlet weak var createAccountButton: NSButton!
|
2019-05-28 19:08:15 +02:00
|
|
|
|
@IBOutlet weak var errorMessageLabel: NSTextField!
|
|
|
|
|
@IBOutlet weak var actionButton: NSButton!
|
2020-11-06 12:07:28 +01:00
|
|
|
|
@IBOutlet weak var noAccountTextField: NSTextField!
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
|
|
var account: Account?
|
2019-06-20 14:22:51 +02:00
|
|
|
|
var accountType: AccountType?
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
|
|
private weak var hostWindow: NSWindow?
|
|
|
|
|
|
|
|
|
|
convenience init() {
|
2019-06-19 18:25:37 +02:00
|
|
|
|
self.init(windowNibName: NSNib.Name("AccountsReaderAPI"))
|
2019-05-28 19:08:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func windowDidLoad() {
|
2019-06-20 14:22:51 +02:00
|
|
|
|
if let accountType = accountType {
|
|
|
|
|
switch accountType {
|
|
|
|
|
case .freshRSS:
|
|
|
|
|
titleImageView.image = AppAssets.accountFreshRSS
|
2020-11-06 12:07:28 +01:00
|
|
|
|
titleLabel.stringValue = NSLocalizedString("Sign in to your FreshRSS account.", comment: "FreshRSS")
|
2021-06-01 01:01:01 +02:00
|
|
|
|
noAccountTextField.stringValue = NSLocalizedString("Don’t have a FreshRSS instance?", comment: "No FreshRSS")
|
2020-11-07 02:31:52 +01:00
|
|
|
|
createAccountButton.title = NSLocalizedString("Find out more", comment: "No FreshRSS Button")
|
2021-04-25 07:31:15 +02:00
|
|
|
|
apiURLTextField.placeholderString = NSLocalizedString("fresh.rss.net/api/greader.php", comment: "FreshRSS API Helper")
|
2020-10-24 21:18:01 +02:00
|
|
|
|
case .inoreader:
|
|
|
|
|
titleImageView.image = AppAssets.accountInoreader
|
2020-11-06 12:07:28 +01:00
|
|
|
|
titleLabel.stringValue = NSLocalizedString("Sign in to your InoReader account.", comment: "InoReader")
|
2020-10-25 02:42:34 +01:00
|
|
|
|
gridView.row(at: 2).isHidden = true
|
2021-06-01 01:01:01 +02:00
|
|
|
|
noAccountTextField.stringValue = NSLocalizedString("Don’t have an InoReader account?", comment: "No InoReader")
|
2020-10-24 21:18:01 +02:00
|
|
|
|
case .bazQux:
|
|
|
|
|
titleImageView.image = AppAssets.accountBazQux
|
2020-11-06 12:07:28 +01:00
|
|
|
|
titleLabel.stringValue = NSLocalizedString("Sign in to your BazQux account.", comment: "BazQux")
|
2020-10-25 02:42:34 +01:00
|
|
|
|
gridView.row(at: 2).isHidden = true
|
2021-06-01 01:01:01 +02:00
|
|
|
|
noAccountTextField.stringValue = NSLocalizedString("Don’t have a BazQux account?", comment: "No BazQux")
|
2020-10-24 21:18:01 +02:00
|
|
|
|
case .theOldReader:
|
|
|
|
|
titleImageView.image = AppAssets.accountTheOldReader
|
2020-11-06 12:07:28 +01:00
|
|
|
|
titleLabel.stringValue = NSLocalizedString("Sign in to your The Old Reader account.", comment: "The Old Reader")
|
2020-10-25 02:42:34 +01:00
|
|
|
|
gridView.row(at: 2).isHidden = true
|
2021-06-01 01:01:01 +02:00
|
|
|
|
noAccountTextField.stringValue = NSLocalizedString("Don’t have a The Old Reader account?", comment: "No OldReader")
|
2019-06-20 14:22:51 +02:00
|
|
|
|
default:
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-15 17:03:47 +02:00
|
|
|
|
if let account = account, let credentials = try? account.retrieveCredentials(type: .readerBasic) {
|
|
|
|
|
usernameTextField.stringValue = credentials.username
|
|
|
|
|
apiURLTextField.stringValue = account.endpointURL?.absoluteString ?? ""
|
2019-05-28 19:08:15 +02:00
|
|
|
|
actionButton.title = NSLocalizedString("Update", comment: "Update")
|
|
|
|
|
} else {
|
|
|
|
|
actionButton.title = NSLocalizedString("Create", comment: "Create")
|
|
|
|
|
}
|
2020-11-07 02:42:20 +01:00
|
|
|
|
|
2021-06-17 06:38:53 +02:00
|
|
|
|
enableAutofill()
|
2020-11-07 02:42:20 +01:00
|
|
|
|
usernameTextField.becomeFirstResponder()
|
2019-05-28 19:08:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
|
2024-05-06 03:02:46 +02:00
|
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow) {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
self.hostWindow = hostWindow
|
2024-05-06 03:02:46 +02:00
|
|
|
|
Task { @MainActor in
|
|
|
|
|
await hostWindow.beginSheet(window!)
|
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
|
|
@IBAction func cancel(_ sender: Any) {
|
|
|
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.cancel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBAction func action(_ sender: Any) {
|
|
|
|
|
self.errorMessageLabel.stringValue = ""
|
2024-04-04 06:15:13 +02:00
|
|
|
|
|
2020-10-25 02:42:34 +01:00
|
|
|
|
guard !usernameTextField.stringValue.isEmpty && !passwordTextField.stringValue.isEmpty else {
|
2019-05-28 19:08:15 +02:00
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Username, password & API URL are required.", comment: "Credentials Error")
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-04-04 06:15:13 +02:00
|
|
|
|
|
2020-10-25 02:42:34 +01:00
|
|
|
|
guard let accountType = accountType, !(accountType == .freshRSS && apiURLTextField.stringValue.isEmpty) else {
|
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Username, password & API URL are required.", comment: "Credentials Error")
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-04-04 06:15:13 +02:00
|
|
|
|
|
2020-10-29 20:05:55 +01:00
|
|
|
|
guard account != nil || !AccountManager.shared.duplicateServiceAccount(type: accountType, username: usernameTextField.stringValue) else {
|
2020-10-26 01:41:16 +01:00
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("There is already an account of this type with that username created.", comment: "Duplicate Error")
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-04-04 06:15:13 +02:00
|
|
|
|
|
2020-10-25 02:42:34 +01:00
|
|
|
|
let apiURL: URL
|
|
|
|
|
switch accountType {
|
|
|
|
|
case .freshRSS:
|
|
|
|
|
guard let inputURL = URL(string: apiURLTextField.stringValue) else {
|
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid API URL.", comment: "Invalid API URL")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
apiURL = inputURL
|
|
|
|
|
case .inoreader:
|
|
|
|
|
apiURL = URL(string: ReaderAPIVariant.inoreader.host)!
|
|
|
|
|
case .bazQux:
|
|
|
|
|
apiURL = URL(string: ReaderAPIVariant.bazQux.host)!
|
|
|
|
|
case .theOldReader:
|
|
|
|
|
apiURL = URL(string: ReaderAPIVariant.theOldReader.host)!
|
|
|
|
|
default:
|
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Unrecognized account type.", comment: "Bad account type")
|
2019-05-28 19:08:15 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-04-04 06:15:13 +02:00
|
|
|
|
|
2020-10-25 02:42:34 +01:00
|
|
|
|
actionButton.isEnabled = false
|
|
|
|
|
progressIndicator.isHidden = false
|
|
|
|
|
progressIndicator.startAnimation(self)
|
2024-04-04 06:15:13 +02:00
|
|
|
|
|
2019-09-15 17:03:47 +02:00
|
|
|
|
let credentials = Credentials(type: .readerBasic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue)
|
2024-03-11 06:22:41 +01:00
|
|
|
|
|
2024-04-04 06:15:13 +02:00
|
|
|
|
Task { @MainActor in
|
|
|
|
|
|
|
|
|
|
var validationDidThrow = false
|
|
|
|
|
var validatedCredentials: Credentials?
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
validatedCredentials = try await Account.validateCredentials(type: accountType, credentials: credentials, endpoint: apiURL, secretsProvider: Secrets())
|
|
|
|
|
} catch {
|
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
|
|
|
|
|
validationDidThrow = true
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 19:08:15 +02:00
|
|
|
|
self.actionButton.isEnabled = true
|
|
|
|
|
self.progressIndicator.isHidden = true
|
|
|
|
|
self.progressIndicator.stopAnimation(self)
|
2024-04-04 06:15:13 +02:00
|
|
|
|
|
|
|
|
|
if validationDidThrow {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let validatedCredentials else {
|
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.account == nil {
|
|
|
|
|
self.account = AccountManager.shared.createAccount(type: self.accountType!)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
self.account?.endpointURL = apiURL
|
|
|
|
|
|
|
|
|
|
try self.account?.removeCredentials(type: .readerBasic)
|
|
|
|
|
try self.account?.removeCredentials(type: .readerAPIKey)
|
|
|
|
|
try self.account?.storeCredentials(credentials)
|
|
|
|
|
try self.account?.storeCredentials(validatedCredentials)
|
|
|
|
|
|
|
|
|
|
self.refreshAll()
|
|
|
|
|
|
|
|
|
|
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
|
|
|
|
|
} catch {
|
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
|
2019-05-28 19:08:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-06 12:07:28 +01:00
|
|
|
|
|
2024-04-04 06:15:13 +02:00
|
|
|
|
private func refreshAll() {
|
|
|
|
|
|
|
|
|
|
Task { @MainActor in
|
|
|
|
|
do {
|
|
|
|
|
try await self.account?.refreshAll()
|
|
|
|
|
} catch {
|
|
|
|
|
NSApplication.shared.presentError(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 12:07:28 +01:00
|
|
|
|
@IBAction func createAccountWithProvider(_ sender: Any) {
|
|
|
|
|
switch accountType {
|
|
|
|
|
case .freshRSS:
|
|
|
|
|
NSWorkspace.shared.open(URL(string: "https://freshrss.org")!)
|
|
|
|
|
case .inoreader:
|
|
|
|
|
NSWorkspace.shared.open(URL(string: "https://www.inoreader.com")!)
|
|
|
|
|
case .bazQux:
|
|
|
|
|
NSWorkspace.shared.open(URL(string: "https://bazqux.com")!)
|
|
|
|
|
case .theOldReader:
|
|
|
|
|
NSWorkspace.shared.open(URL(string: "https://theoldreader.com")!)
|
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 06:38:53 +02:00
|
|
|
|
// MARK: Autofill
|
2024-02-28 05:06:57 +01:00
|
|
|
|
|
2021-06-17 06:38:53 +02:00
|
|
|
|
func enableAutofill() {
|
2024-02-28 05:06:57 +01:00
|
|
|
|
|
|
|
|
|
usernameTextField.contentType = .username
|
|
|
|
|
passwordTextField.contentType = .password
|
2021-06-17 06:38:53 +02:00
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
}
|