2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// AddFeedWindowController.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 8/1/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-02-03 07:51:32 +01:00
|
|
|
import AppKit
|
2017-05-27 19:43:27 +02:00
|
|
|
import RSCore
|
|
|
|
import RSTree
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2017-09-17 21:34:10 +02:00
|
|
|
import Account
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
protocol AddFeedWindowControllerDelegate: class {
|
|
|
|
|
|
|
|
// userEnteredURL will have already been validated and normalized.
|
2017-10-21 22:33:08 +02:00
|
|
|
func addFeedWindowController(_: AddFeedWindowController, userEnteredURL: URL, userEnteredTitle: String?, container: Container)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
func addFeedWindowControllerUserDidCancel(_: AddFeedWindowController)
|
|
|
|
}
|
|
|
|
|
|
|
|
class AddFeedWindowController : NSWindowController {
|
|
|
|
|
|
|
|
@IBOutlet var urlTextField: NSTextField!
|
|
|
|
@IBOutlet var nameTextField: NSTextField!
|
|
|
|
@IBOutlet var addButton: NSButton!
|
|
|
|
@IBOutlet var folderPopupButton: NSPopUpButton!
|
|
|
|
|
|
|
|
private var urlString: String?
|
|
|
|
private var initialName: String?
|
2019-05-31 15:05:26 +02:00
|
|
|
private weak var initialAccount: Account?
|
2018-09-22 21:46:24 +02:00
|
|
|
private var initialFolder: Folder?
|
2019-01-28 06:09:25 +01:00
|
|
|
private weak var delegate: AddFeedWindowControllerDelegate?
|
|
|
|
private var folderTreeController: TreeController!
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
private var userEnteredTitle: String? {
|
2018-02-14 22:14:25 +01:00
|
|
|
var s = nameTextField.stringValue
|
2020-01-17 03:09:18 +01:00
|
|
|
s = s.collapsingWhitespace
|
2018-02-14 22:14:25 +01:00
|
|
|
if s.isEmpty {
|
|
|
|
return nil
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
2018-02-14 22:14:25 +01:00
|
|
|
return s
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var hostWindow: NSWindow!
|
|
|
|
|
2019-05-02 00:33:08 +02:00
|
|
|
convenience init(urlString: String?, name: String?, account: Account?, folder: Folder?, folderTreeController: TreeController, delegate: AddFeedWindowControllerDelegate?) {
|
2018-12-09 21:32:33 +01:00
|
|
|
self.init(windowNibName: NSNib.Name("AddFeedSheet"))
|
2017-05-27 19:43:27 +02:00
|
|
|
self.urlString = urlString
|
|
|
|
self.initialName = name
|
2019-05-02 00:33:08 +02:00
|
|
|
self.initialAccount = account
|
2018-09-22 21:46:24 +02:00
|
|
|
self.initialFolder = folder
|
2017-05-27 19:43:27 +02:00
|
|
|
self.delegate = delegate
|
|
|
|
self.folderTreeController = folderTreeController
|
|
|
|
}
|
|
|
|
|
2019-01-28 06:09:25 +01:00
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow) {
|
|
|
|
hostWindow.beginSheet(window!) { (returnCode: NSApplication.ModalResponse) -> Void in
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidLoad() {
|
|
|
|
if let urlString = urlString {
|
|
|
|
urlTextField.stringValue = urlString
|
|
|
|
}
|
|
|
|
if let initialName = initialName, !initialName.isEmpty {
|
|
|
|
nameTextField.stringValue = initialName
|
|
|
|
}
|
|
|
|
|
2018-09-13 05:23:23 +02:00
|
|
|
folderPopupButton.menu = FolderTreeMenu.createFolderPopupMenu(with: folderTreeController.rootNode)
|
2019-11-17 02:57:05 +01:00
|
|
|
|
2019-05-02 00:33:08 +02:00
|
|
|
if let account = initialAccount {
|
|
|
|
FolderTreeMenu.select(account: account, folder: initialFolder, in: folderPopupButton)
|
2019-11-17 02:57:05 +01:00
|
|
|
} else if let container = AddWebFeedDefaultContainer.defaultContainer {
|
|
|
|
if let folder = container as? Folder, let account = folder.account {
|
|
|
|
FolderTreeMenu.select(account: account, folder: folder, in: folderPopupButton)
|
|
|
|
} else {
|
|
|
|
if let account = container as? Account {
|
|
|
|
FolderTreeMenu.select(account: account, folder: nil, in: folderPopupButton)
|
|
|
|
}
|
2019-05-21 16:36:33 +02:00
|
|
|
}
|
2018-09-22 21:46:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
2018-02-12 03:58:50 +01:00
|
|
|
@IBAction func cancel(_ sender: Any?) {
|
2017-05-27 19:43:27 +02:00
|
|
|
cancelSheet()
|
|
|
|
}
|
|
|
|
|
2018-02-12 03:58:50 +01:00
|
|
|
@IBAction func addFeed(_ sender: Any?) {
|
2017-10-22 00:56:01 +02:00
|
|
|
let urlString = urlTextField.stringValue
|
2020-01-17 03:09:18 +01:00
|
|
|
let normalizedURLString = urlString.normalizedURL
|
2017-10-22 00:56:01 +02:00
|
|
|
|
|
|
|
if normalizedURLString.isEmpty {
|
|
|
|
cancelSheet()
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
guard let url = URL(string: normalizedURLString) else {
|
|
|
|
cancelSheet()
|
|
|
|
return
|
|
|
|
}
|
2019-05-21 16:36:33 +02:00
|
|
|
|
|
|
|
let container = selectedContainer()!
|
2019-11-17 02:57:05 +01:00
|
|
|
AddWebFeedDefaultContainer.saveDefaultContainer(container)
|
2017-10-22 00:56:01 +02:00
|
|
|
|
2019-05-21 16:36:33 +02:00
|
|
|
delegate?.addFeedWindowController(self, userEnteredURL: url, userEnteredTitle: userEnteredTitle, container: container)
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 03:58:50 +01:00
|
|
|
@IBAction func localShowFeedList(_ sender: Any?) {
|
2017-09-17 21:34:10 +02:00
|
|
|
NSApplication.shared.sendAction(NSSelectorFromString("showFeedList:"), to: nil, from: sender)
|
2017-09-17 21:54:08 +02:00
|
|
|
hostWindow.endSheet(window!, returnCode: NSApplication.ModalResponse.continue)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: NSTextFieldDelegate
|
|
|
|
|
2019-01-28 06:09:25 +01:00
|
|
|
@objc func controlTextDidEndEditing(_ obj: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
|
2019-01-28 06:09:25 +01:00
|
|
|
@objc func controlTextDidChange(_ obj: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension AddFeedWindowController {
|
|
|
|
|
2019-01-28 06:09:25 +01:00
|
|
|
private func updateUI() {
|
2020-01-17 03:09:18 +01:00
|
|
|
addButton.isEnabled = urlTextField.stringValue.mayBeURL
|
2019-01-28 06:09:25 +01:00
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2019-01-28 06:09:25 +01:00
|
|
|
func cancelSheet() {
|
2017-05-27 19:43:27 +02:00
|
|
|
delegate?.addFeedWindowControllerUserDidCancel(self)
|
|
|
|
}
|
|
|
|
|
2017-10-22 00:56:01 +02:00
|
|
|
func selectedContainer() -> Container? {
|
|
|
|
return folderPopupButton.selectedItem?.representedObject as? Container
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|