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
|
|
|
|
import RSWeb
|
|
|
|
|
|
|
|
class AccountsGoogleReaderCompatibleWindowController: NSWindowController {
|
|
|
|
|
|
|
|
@IBOutlet weak var progressIndicator: NSProgressIndicator!
|
|
|
|
@IBOutlet weak var usernameTextField: NSTextField!
|
|
|
|
@IBOutlet weak var apiURLTextField: NSTextField!
|
|
|
|
@IBOutlet weak var passwordTextField: NSSecureTextField!
|
|
|
|
@IBOutlet weak var errorMessageLabel: NSTextField!
|
|
|
|
@IBOutlet weak var actionButton: NSButton!
|
|
|
|
|
|
|
|
var account: Account?
|
|
|
|
|
|
|
|
private weak var hostWindow: NSWindow?
|
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
self.init(windowNibName: NSNib.Name("AccountsGoogleReaderCompatible"))
|
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidLoad() {
|
|
|
|
if let account = account, let credentials = try? account.retrieveBasicCredentials() {
|
|
|
|
if case .basic(let username, let password) = credentials {
|
|
|
|
usernameTextField.stringValue = username
|
|
|
|
passwordTextField.stringValue = password
|
|
|
|
}
|
|
|
|
actionButton.title = NSLocalizedString("Update", comment: "Update")
|
|
|
|
} else {
|
|
|
|
actionButton.title = NSLocalizedString("Create", comment: "Create")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow, completionHandler handler: ((NSApplication.ModalResponse) -> Void)? = nil) {
|
|
|
|
self.hostWindow = hostWindow
|
|
|
|
hostWindow.beginSheet(window!, completionHandler: handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction func cancel(_ sender: Any) {
|
|
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.cancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func action(_ sender: Any) {
|
|
|
|
|
|
|
|
self.errorMessageLabel.stringValue = ""
|
|
|
|
|
|
|
|
guard !usernameTextField.stringValue.isEmpty && !passwordTextField.stringValue.isEmpty && !apiURLTextField.stringValue.isEmpty else {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Username, password & API URL are required.", comment: "Credentials Error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
actionButton.isEnabled = false
|
|
|
|
progressIndicator.isHidden = false
|
|
|
|
progressIndicator.startAnimation(self)
|
|
|
|
|
|
|
|
guard let apiURL = URL(string: apiURLTextField.stringValue) else {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Invalie API URL.", comment: "Credentials Error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-29 21:16:09 +02:00
|
|
|
let credentials = Credentials.googleBasicLogin(username: usernameTextField.stringValue, password: passwordTextField.stringValue)
|
|
|
|
Account.validateCredentials(type: .googleReaderCompatible, credentials: credentials, endpoint: apiURL) { [weak self] result in
|
2019-05-28 19:08:15 +02:00
|
|
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
self.actionButton.isEnabled = true
|
|
|
|
self.progressIndicator.isHidden = true
|
|
|
|
self.progressIndicator.stopAnimation(self)
|
|
|
|
|
|
|
|
switch result {
|
2019-05-29 16:54:52 +02:00
|
|
|
case .success(let validatedCredentials):
|
|
|
|
guard let validatedCredentials = validatedCredentials else {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
|
|
|
|
return
|
|
|
|
}
|
2019-05-28 19:08:15 +02:00
|
|
|
|
2019-05-29 16:54:52 +02:00
|
|
|
|
|
|
|
var newAccount = false
|
|
|
|
if self.account == nil {
|
|
|
|
self.account = AccountManager.shared.createAccount(type: .googleReaderCompatible)
|
|
|
|
newAccount = true
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2019-05-29 21:16:09 +02:00
|
|
|
self.account?.endpointURL = apiURL
|
|
|
|
|
2019-05-30 13:48:34 +02:00
|
|
|
try self.account?.removeGoogleAuthCredentials()
|
2019-05-29 16:54:52 +02:00
|
|
|
try self.account?.storeCredentials(validatedCredentials)
|
2019-05-29 21:16:09 +02:00
|
|
|
|
2019-05-29 16:54:52 +02:00
|
|
|
if newAccount {
|
|
|
|
self.account?.refreshAll() { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
NSApplication.shared.presentError(error)
|
2019-05-28 19:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 16:54:52 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-29 16:54:52 +02:00
|
|
|
case .failure:
|
2019-05-28 19:08:15 +02:00
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|