2017-05-27 19:43:27 +02:00
|
|
|
|
//
|
|
|
|
|
// AddFeedController.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
|
// NetNewsWire
|
2017-05-27 19:43:27 +02:00
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 8/28/16.
|
2017-05-29 22:17:58 +02:00
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
2017-05-27 19:43:27 +02:00
|
|
|
|
//
|
|
|
|
|
|
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-12-03 02:47:08 +01:00
|
|
|
|
import RSParser
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
// Run add-feed sheet.
|
|
|
|
|
// If it returns with URL and optional name,
|
|
|
|
|
// run FeedFinder plus modal progress window.
|
|
|
|
|
// If FeedFinder returns feed,
|
|
|
|
|
// add feed.
|
|
|
|
|
// Else,
|
|
|
|
|
// display error sheet.
|
|
|
|
|
|
2019-05-09 00:41:19 +02:00
|
|
|
|
class AddFeedController: AddFeedWindowControllerDelegate {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2017-10-22 00:56:01 +02:00
|
|
|
|
private let hostWindow: NSWindow
|
|
|
|
|
private var addFeedWindowController: AddFeedWindowController?
|
|
|
|
|
private var foundFeedURLString: String?
|
|
|
|
|
private var titleFromFeed: String?
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
init(hostWindow: NSWindow) {
|
|
|
|
|
self.hostWindow = hostWindow
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 04:25:45 +02:00
|
|
|
|
func showAddFeedSheet(_ type: AddFeedWindowControllerType, _ urlString: String? = nil, _ name: String? = nil, _ account: Account? = nil, _ folder: Folder? = nil) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
let folderTreeControllerDelegate = FolderTreeControllerDelegate()
|
2019-05-01 21:56:26 +02:00
|
|
|
|
let folderTreeController = TreeController(delegate: folderTreeControllerDelegate)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2020-04-22 04:25:45 +02:00
|
|
|
|
switch type {
|
|
|
|
|
case .webFeed:
|
|
|
|
|
addFeedWindowController = AddWebFeedWindowController(urlString: urlString ?? urlStringFromPasteboard,
|
|
|
|
|
name: name,
|
|
|
|
|
account: account,
|
|
|
|
|
folder: folder,
|
|
|
|
|
folderTreeController: folderTreeController,
|
|
|
|
|
delegate: self)
|
2020-05-10 18:44:30 +02:00
|
|
|
|
case .redditFeed:
|
|
|
|
|
addFeedWindowController = AddRedditFeedWindowController(folderTreeController: folderTreeController,
|
|
|
|
|
delegate: self)
|
2020-04-22 04:25:45 +02:00
|
|
|
|
case .twitterFeed:
|
|
|
|
|
addFeedWindowController = AddTwitterFeedWindowController(folderTreeController: folderTreeController,
|
|
|
|
|
delegate: self)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
addFeedWindowController!.runSheetOnWindow(hostWindow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: AddFeedWindowControllerDelegate
|
|
|
|
|
|
2020-04-22 04:25:45 +02:00
|
|
|
|
func addFeedWindowController(_: AddFeedWindowController, userEnteredURL url: URL, userEnteredTitle title: String?, container: Container) {
|
2017-09-17 21:54:08 +02:00
|
|
|
|
closeAddFeedSheet(NSApplication.ModalResponse.OK)
|
2017-10-22 00:56:01 +02:00
|
|
|
|
|
|
|
|
|
guard let accountAndFolderSpecifier = accountAndFolderFromContainer(container) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let account = accountAndFolderSpecifier.account
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
if account.hasWebFeed(withURL: url.absoluteString) {
|
2017-10-22 00:56:01 +02:00
|
|
|
|
showAlreadySubscribedError(url.absoluteString)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
|
account.createWebFeed(url: url.absoluteString, name: title, container: container) { result in
|
2019-05-09 00:41:19 +02:00
|
|
|
|
|
2019-06-10 15:21:03 +02:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.endShowingProgress()
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 00:41:19 +02:00
|
|
|
|
switch result {
|
2019-05-10 17:14:24 +02:00
|
|
|
|
case .success(let feed):
|
2019-11-15 03:11:41 +01:00
|
|
|
|
NotificationCenter.default.post(name: .UserDidAddFeed, object: self, userInfo: [UserInfoKey.webFeed: feed])
|
2019-05-10 17:14:24 +02:00
|
|
|
|
case .failure(let error):
|
|
|
|
|
switch error {
|
|
|
|
|
case AccountError.createErrorAlreadySubscribed:
|
2019-05-28 16:45:02 +02:00
|
|
|
|
self.showAlreadySubscribedError(url.absoluteString)
|
2019-05-10 17:14:24 +02:00
|
|
|
|
case AccountError.createErrorNotFound:
|
2019-05-28 16:45:02 +02:00
|
|
|
|
self.showNoFeedsErrorMessage()
|
2019-05-10 17:14:24 +02:00
|
|
|
|
default:
|
|
|
|
|
NSApplication.shared.presentError(error)
|
2019-05-09 00:41:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beginShowingProgress()
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 04:25:45 +02:00
|
|
|
|
func addFeedWindowControllerUserDidCancel(_: AddFeedWindowController) {
|
2017-09-17 21:54:08 +02:00
|
|
|
|
closeAddFeedSheet(NSApplication.ModalResponse.cancel)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension AddFeedController {
|
|
|
|
|
|
|
|
|
|
var urlStringFromPasteboard: String? {
|
2020-01-10 21:00:22 +01:00
|
|
|
|
if let urlString = NSPasteboard.urlString(from: NSPasteboard.general) {
|
2020-01-17 03:09:18 +01:00
|
|
|
|
return urlString.normalizedURL
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
2018-02-14 22:14:25 +01:00
|
|
|
|
return nil
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
2018-02-14 22:14:25 +01:00
|
|
|
|
|
2017-10-22 00:56:01 +02:00
|
|
|
|
struct AccountAndFolderSpecifier {
|
|
|
|
|
let account: Account
|
|
|
|
|
let folder: Folder?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func accountAndFolderFromContainer(_ container: Container) -> AccountAndFolderSpecifier? {
|
|
|
|
|
if let account = container as? Account {
|
|
|
|
|
return AccountAndFolderSpecifier(account: account, folder: nil)
|
|
|
|
|
}
|
2017-10-22 01:37:40 +02:00
|
|
|
|
if let folder = container as? Folder, let account = folder.account {
|
|
|
|
|
return AccountAndFolderSpecifier(account: account, folder: folder)
|
2017-10-22 00:56:01 +02:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
|
func closeAddFeedSheet(_ returnCode: NSApplication.ModalResponse) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
if let sheetWindow = addFeedWindowController?.window {
|
|
|
|
|
hostWindow.endSheet(sheetWindow, returnCode: returnCode)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Errors
|
|
|
|
|
|
2017-10-22 00:56:01 +02:00
|
|
|
|
func showAlreadySubscribedError(_ urlString: String) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
let alert = NSAlert()
|
|
|
|
|
alert.alertStyle = .informational
|
|
|
|
|
alert.messageText = NSLocalizedString("Already subscribed", comment: "Feed finder")
|
|
|
|
|
alert.informativeText = NSLocalizedString("Can’t add this feed because you’ve already subscribed to it.", comment: "Feed finder")
|
|
|
|
|
alert.beginSheetModal(for: hostWindow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func showInitialDownloadError(_ error: Error) {
|
|
|
|
|
let alert = NSAlert()
|
|
|
|
|
alert.alertStyle = .informational
|
|
|
|
|
alert.messageText = NSLocalizedString("Download Error", comment: "Feed finder")
|
|
|
|
|
|
|
|
|
|
let formatString = NSLocalizedString("Can’t add this feed because of a download error: “%@”", comment: "Feed finder")
|
|
|
|
|
let errorText = NSString.localizedStringWithFormat(formatString as NSString, error.localizedDescription)
|
|
|
|
|
alert.informativeText = errorText as String
|
|
|
|
|
alert.beginSheetModal(for: hostWindow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func showNoFeedsErrorMessage() {
|
|
|
|
|
let alert = NSAlert()
|
|
|
|
|
alert.alertStyle = .informational
|
|
|
|
|
alert.messageText = NSLocalizedString("Feed not found", comment: "Feed finder")
|
|
|
|
|
alert.informativeText = NSLocalizedString("Can’t add a feed because no feed was found.", comment: "Feed finder")
|
|
|
|
|
alert.beginSheetModal(for: hostWindow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Progress
|
|
|
|
|
|
|
|
|
|
func beginShowingProgress() {
|
|
|
|
|
runIndeterminateProgressWithMessage(NSLocalizedString("Finding feed…", comment:"Feed finder"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func endShowingProgress() {
|
|
|
|
|
stopIndeterminateProgress()
|
|
|
|
|
hostWindow.makeKeyAndOrderFront(self)
|
|
|
|
|
}
|
2020-04-22 04:25:45 +02:00
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|