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
|
2024-03-22 01:21:50 +01:00
|
|
|
import Tree
|
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
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
protocol AddFeedWindowControllerDelegate: AnyObject {
|
|
|
|
|
|
|
|
// userEnteredURL will have already been validated and normalized.
|
2024-04-03 05:46:28 +02:00
|
|
|
@MainActor func addFeedWindowController(_: AddFeedWindowController, userEnteredURL: URL, userEnteredTitle: String?, container: Container)
|
|
|
|
@MainActor func addFeedWindowControllerUserDidCancel(_: AddFeedWindowController)
|
2024-02-26 08:12:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class AddFeedWindowController : NSWindowController {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
@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?) {
|
2024-02-26 08:12:21 +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) {
|
2024-05-06 03:02:46 +02:00
|
|
|
hostWindow.beginSheet(window!, completionHandler: nil)
|
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)
|
2024-02-26 08:12:21 +01:00
|
|
|
} else if let container = AddFeedDefaultContainer.defaultContainer {
|
2019-11-17 02:57:05 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-07-06 17:06:12 +02:00
|
|
|
guard let url = URL(unicodeString: normalizedURLString) else {
|
2017-10-22 00:56:01 +02:00
|
|
|
cancelSheet()
|
|
|
|
return
|
|
|
|
}
|
2019-05-21 16:36:33 +02:00
|
|
|
|
2020-11-05 17:34:26 +01:00
|
|
|
guard let container = selectedContainer() else { return }
|
2024-02-26 08:12:21 +01:00
|
|
|
AddFeedDefaultContainer.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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
private extension AddFeedWindowController {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2019-01-28 06:09:25 +01:00
|
|
|
private func updateUI() {
|
2020-11-05 17:34:26 +01:00
|
|
|
addButton.isEnabled = urlTextField.stringValue.mayBeURL && selectedContainer() != nil
|
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? {
|
2020-11-05 17:34:26 +01:00
|
|
|
guard folderPopupButton.selectedItem?.isEnabled ?? false else { return nil }
|
2017-10-22 00:56:01 +02:00
|
|
|
return folderPopupButton.selectedItem?.representedObject as? Container
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|