2017-05-22 13:00:45 -07:00
|
|
|
|
//
|
|
|
|
|
// AppDelegate.swift
|
2018-08-28 22:18:24 -07:00
|
|
|
|
// NetNewsWire
|
2017-05-22 13:00:45 -07:00
|
|
|
|
//
|
2017-05-27 10:43:27 -07:00
|
|
|
|
// Created by Brent Simmons on 7/11/15.
|
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
2017-05-22 13:00:45 -07:00
|
|
|
|
//
|
|
|
|
|
|
2018-02-02 22:51:32 -08:00
|
|
|
|
import AppKit
|
2019-10-02 19:42:16 -05:00
|
|
|
|
import UserNotifications
|
2018-07-23 18:29:08 -07:00
|
|
|
|
import Articles
|
2017-05-27 10:43:27 -07:00
|
|
|
|
import RSTree
|
2017-05-27 13:37:50 -07:00
|
|
|
|
import RSWeb
|
2017-09-17 17:12:42 -07:00
|
|
|
|
import Account
|
2017-11-13 18:33:23 -08:00
|
|
|
|
import RSCore
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2017-11-14 21:31:17 -08:00
|
|
|
|
var appDelegate: AppDelegate!
|
2017-05-22 13:00:45 -07:00
|
|
|
|
|
|
|
|
|
@NSApplicationMain
|
2019-10-02 19:42:16 -05:00
|
|
|
|
class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, UNUserNotificationCenterDelegate, UnreadCountProvider {
|
2017-05-22 13:00:45 -07:00
|
|
|
|
|
2019-10-02 19:42:16 -05:00
|
|
|
|
var userNotificationManager: UserNotificationManager!
|
2017-11-24 21:39:59 -08:00
|
|
|
|
var faviconDownloader: FaviconDownloader!
|
2017-11-25 20:12:53 -08:00
|
|
|
|
var imageDownloader: ImageDownloader!
|
2017-11-26 13:16:32 -08:00
|
|
|
|
var authorAvatarDownloader: AuthorAvatarDownloader!
|
2017-11-26 19:57:45 -08:00
|
|
|
|
var feedIconDownloader: FeedIconDownloader!
|
2017-11-24 21:39:59 -08:00
|
|
|
|
var appName: String!
|
2019-05-15 17:21:58 -05:00
|
|
|
|
|
|
|
|
|
var refreshTimer: AccountRefreshTimer?
|
|
|
|
|
var syncTimer: ArticleStatusSyncTimer?
|
|
|
|
|
|
2019-01-27 18:00:09 -08:00
|
|
|
|
var shuttingDown = false {
|
|
|
|
|
didSet {
|
|
|
|
|
if shuttingDown {
|
2019-04-23 11:20:44 -05:00
|
|
|
|
refreshTimer?.shuttingDown = shuttingDown
|
|
|
|
|
refreshTimer?.invalidate()
|
2019-05-15 17:21:58 -05:00
|
|
|
|
syncTimer?.shuttingDown = shuttingDown
|
|
|
|
|
syncTimer?.invalidate()
|
2019-01-27 18:00:09 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-27 11:32:59 -08:00
|
|
|
|
@IBOutlet var debugMenuItem: NSMenuItem!
|
2018-01-27 15:11:02 -08:00
|
|
|
|
@IBOutlet var sortByOldestArticleOnTopMenuItem: NSMenuItem!
|
|
|
|
|
@IBOutlet var sortByNewestArticleOnTopMenuItem: NSMenuItem!
|
2019-09-08 17:09:26 -05:00
|
|
|
|
@IBOutlet var groupArticlesByFeedMenuItem: NSMenuItem!
|
2018-12-09 12:12:55 -08:00
|
|
|
|
@IBOutlet var checkForUpdatesMenuItem: NSMenuItem!
|
2019-09-19 10:38:17 -05:00
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
var unreadCount = 0 {
|
|
|
|
|
didSet {
|
2017-10-05 18:12:58 -07:00
|
|
|
|
if unreadCount != oldValue {
|
2018-12-27 21:19:19 -08:00
|
|
|
|
CoalescingQueue.standard.add(self, #selector(updateDockBadge))
|
2017-11-19 13:57:42 -08:00
|
|
|
|
postUnreadCountDidChangeNotification()
|
2017-10-05 18:12:58 -07:00
|
|
|
|
}
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-24 21:39:59 -08:00
|
|
|
|
private var preferencesWindowController: NSWindowController?
|
2018-01-27 12:39:07 -08:00
|
|
|
|
private var mainWindowController: MainWindowController?
|
2017-11-24 21:39:59 -08:00
|
|
|
|
private var addFeedController: AddFeedController?
|
|
|
|
|
private var addFolderWindowController: AddFolderWindowController?
|
2019-05-01 16:04:56 -05:00
|
|
|
|
private var importOPMLController: ImportOPMLWindowController?
|
|
|
|
|
private var exportOPMLController: ExportOPMLWindowController?
|
2017-11-24 21:39:59 -08:00
|
|
|
|
private var keyboardShortcutsWindowController: WebViewWindowController?
|
|
|
|
|
private var inspectorWindowController: InspectorWindowController?
|
2018-12-29 12:31:39 -08:00
|
|
|
|
private var crashReportWindowController: CrashReportWindowController? // For testing only
|
2017-11-24 21:39:59 -08:00
|
|
|
|
private let log = Log()
|
2018-09-06 20:16:12 -07:00
|
|
|
|
private let appNewsURLString = "https://nnw.ranchero.com/feed.json"
|
2019-09-06 09:13:09 -04:00
|
|
|
|
private let appMovementMonitor = RSAppMovementMonitor()
|
2017-11-24 21:39:59 -08:00
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
override init() {
|
|
|
|
|
NSWindow.allowsAutomaticWindowTabbing = false
|
|
|
|
|
super.init()
|
2017-10-18 18:37:45 -07:00
|
|
|
|
|
2019-09-12 10:59:26 -05:00
|
|
|
|
AccountManager.shared = AccountManager(accountsFolder: RSDataSubfolder(nil, "Accounts")!)
|
|
|
|
|
|
2017-10-18 18:37:45 -07:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2019-02-17 18:46:28 -08:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(inspectableObjectsDidChange(_:)), name: .InspectableObjectsDidChange, object: nil)
|
2018-01-21 12:46:22 -08:00
|
|
|
|
|
2017-11-14 21:31:17 -08:00
|
|
|
|
appDelegate = self
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 13:13:40 -08:00
|
|
|
|
// MARK: - API
|
|
|
|
|
|
2017-11-14 21:31:17 -08:00
|
|
|
|
func logMessage(_ message: String, type: LogItem.ItemType) {
|
|
|
|
|
|
2017-11-25 16:15:17 -08:00
|
|
|
|
#if DEBUG
|
2018-09-10 22:06:50 -07:00
|
|
|
|
if type == .debug {
|
2017-11-25 16:15:17 -08:00
|
|
|
|
print("logMessage: \(message) - \(type)")
|
2018-09-10 22:06:50 -07:00
|
|
|
|
}
|
2017-11-25 16:15:17 -08:00
|
|
|
|
#endif
|
|
|
|
|
|
2017-11-14 21:31:17 -08:00
|
|
|
|
let logItem = LogItem(type: type, message: message)
|
|
|
|
|
log.add(logItem)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func logDebugMessage(_ message: String) {
|
|
|
|
|
|
|
|
|
|
logMessage(message, type: .debug)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 13:13:40 -08:00
|
|
|
|
func showAddFolderSheetOnWindow(_ window: NSWindow) {
|
|
|
|
|
|
|
|
|
|
addFolderWindowController = AddFolderWindowController()
|
|
|
|
|
addFolderWindowController!.runSheetOnWindow(window)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:33:08 -05:00
|
|
|
|
func showAddFeedSheetOnWindow(_ window: NSWindow, urlString: String?, name: String?, account: Account?, folder: Folder?) {
|
2018-02-03 21:30:30 -08:00
|
|
|
|
|
|
|
|
|
addFeedController = AddFeedController(hostWindow: window)
|
2019-05-01 17:33:08 -05:00
|
|
|
|
addFeedController?.showAddFeedSheet(urlString, name, account, folder)
|
2018-02-03 21:30:30 -08:00
|
|
|
|
}
|
2018-09-13 10:04:20 -05:00
|
|
|
|
|
2017-09-18 22:00:35 -07:00
|
|
|
|
// MARK: - NSApplicationDelegate
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2018-11-26 19:22:05 -08:00
|
|
|
|
func applicationWillFinishLaunching(_ notification: Notification) {
|
|
|
|
|
installAppleEventHandlers()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
func applicationDidFinishLaunching(_ note: Notification) {
|
|
|
|
|
|
2018-12-09 12:12:55 -08:00
|
|
|
|
#if MAC_APP_STORE
|
|
|
|
|
checkForUpdatesMenuItem.isHidden = true
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-15 12:05:00 -07:00
|
|
|
|
appName = (Bundle.main.infoDictionary!["CFBundleExecutable"]! as! String)
|
2017-11-24 21:39:59 -08:00
|
|
|
|
|
2018-12-27 21:34:21 -08:00
|
|
|
|
AppDefaults.registerDefaults()
|
|
|
|
|
let isFirstRun = AppDefaults.isFirstRun
|
2017-11-25 16:10:19 -08:00
|
|
|
|
if isFirstRun {
|
|
|
|
|
logDebugMessage("Is first run.")
|
|
|
|
|
}
|
2019-05-01 05:53:18 -05:00
|
|
|
|
let localAccount = AccountManager.shared.defaultAccount
|
2017-09-26 21:43:40 -07:00
|
|
|
|
DefaultFeedsImporter.importIfNeeded(isFirstRun, account: localAccount)
|
2019-10-02 19:42:16 -05:00
|
|
|
|
|
2017-11-25 11:14:06 -08:00
|
|
|
|
let tempDirectory = NSTemporaryDirectory()
|
2018-12-11 21:49:04 -08:00
|
|
|
|
let bundleIdentifier = (Bundle.main.infoDictionary!["CFBundleIdentifier"]! as! String)
|
|
|
|
|
let cacheFolder = (tempDirectory as NSString).appendingPathComponent(bundleIdentifier)
|
2017-11-25 20:12:53 -08:00
|
|
|
|
|
2019-09-16 16:26:40 -05:00
|
|
|
|
// If the image disk cache hasn't been flushed for 3 days and the network is available, delete it
|
|
|
|
|
if let flushDate = AppDefaults.lastImageCacheFlushDate, flushDate.addingTimeInterval(3600*24*3) < Date() {
|
|
|
|
|
if let reachability = try? Reachability(hostname: "apple.com") {
|
|
|
|
|
if reachability.connection != .unavailable {
|
|
|
|
|
try? FileManager.default.removeItem(atPath: cacheFolder)
|
|
|
|
|
AppDefaults.lastImageCacheFlushDate = Date()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 13:47:26 -08:00
|
|
|
|
let faviconsFolder = (cacheFolder as NSString).appendingPathComponent("Favicons")
|
2017-11-25 11:14:06 -08:00
|
|
|
|
let faviconsFolderURL = URL(fileURLWithPath: faviconsFolder)
|
|
|
|
|
try! FileManager.default.createDirectory(at: faviconsFolderURL, withIntermediateDirectories: true, attributes: nil)
|
2017-11-24 21:39:59 -08:00
|
|
|
|
faviconDownloader = FaviconDownloader(folder: faviconsFolder)
|
|
|
|
|
|
2017-11-25 20:12:53 -08:00
|
|
|
|
let imagesFolder = (cacheFolder as NSString).appendingPathComponent("Images")
|
|
|
|
|
let imagesFolderURL = URL(fileURLWithPath: imagesFolder)
|
|
|
|
|
try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
|
imageDownloader = ImageDownloader(folder: imagesFolder)
|
|
|
|
|
|
2017-11-26 13:16:32 -08:00
|
|
|
|
authorAvatarDownloader = AuthorAvatarDownloader(imageDownloader: imageDownloader)
|
2019-08-27 13:29:34 -05:00
|
|
|
|
feedIconDownloader = FeedIconDownloader(imageDownloader: imageDownloader, folder: cacheFolder)
|
2017-12-16 11:16:32 -08:00
|
|
|
|
|
2018-01-27 15:24:33 -08:00
|
|
|
|
updateSortMenuItems()
|
2019-09-08 17:09:26 -05:00
|
|
|
|
updateGroupByFeedMenuItem()
|
2018-02-11 01:20:30 -08:00
|
|
|
|
createAndShowMainWindow()
|
2019-08-23 18:30:28 -07:00
|
|
|
|
if isFirstRun {
|
|
|
|
|
mainWindowController?.window?.center()
|
|
|
|
|
}
|
2017-10-18 18:37:45 -07:00
|
|
|
|
|
2017-11-26 13:16:32 -08:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(feedSettingDidChange(_:)), name: .FeedSettingDidChange, object: nil)
|
2018-01-27 15:24:33 -08:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
|
2017-11-26 13:16:32 -08:00
|
|
|
|
|
2017-10-18 18:37:45 -07:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.unreadCount = AccountManager.shared.unreadCount
|
|
|
|
|
}
|
2017-12-25 10:23:12 -08:00
|
|
|
|
|
2018-01-21 13:27:17 -08:00
|
|
|
|
if InspectorWindowController.shouldOpenAtStartup {
|
2018-01-21 13:30:26 -08:00
|
|
|
|
self.toggleInspectorWindow(self)
|
2018-01-21 13:27:17 -08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 17:21:58 -05:00
|
|
|
|
refreshTimer = AccountRefreshTimer()
|
|
|
|
|
syncTimer = ArticleStatusSyncTimer()
|
2019-04-24 14:46:01 -05:00
|
|
|
|
|
2019-10-02 19:42:16 -05:00
|
|
|
|
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .sound, .alert]) { (granted, error) in
|
|
|
|
|
if granted {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
NSApplication.shared.registerForRemoteNotifications()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UNUserNotificationCenter.current().delegate = self
|
|
|
|
|
userNotificationManager = UserNotificationManager()
|
|
|
|
|
|
2017-12-25 10:23:12 -08:00
|
|
|
|
#if RELEASE
|
2018-01-27 11:32:59 -08:00
|
|
|
|
debugMenuItem.menu?.removeItem(debugMenuItem)
|
2017-12-25 10:23:12 -08:00
|
|
|
|
DispatchQueue.main.async {
|
2019-04-24 14:46:01 -05:00
|
|
|
|
self.refreshTimer!.timedRefresh(nil)
|
2019-05-15 17:21:58 -05:00
|
|
|
|
self.syncTimer!.timedRefresh(nil)
|
2017-12-25 10:23:12 -08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2018-12-20 22:21:42 -08:00
|
|
|
|
|
2019-01-27 18:00:09 -08:00
|
|
|
|
#if DEBUG
|
2019-04-23 11:20:44 -05:00
|
|
|
|
refreshTimer!.update()
|
2019-05-15 17:21:58 -05:00
|
|
|
|
syncTimer!.update()
|
2019-01-27 18:00:09 -08:00
|
|
|
|
#endif
|
|
|
|
|
|
2019-01-11 23:18:48 -08:00
|
|
|
|
#if !MAC_APP_STORE
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
CrashReporter.check(appName: "NetNewsWire")
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
2019-10-03 15:49:27 -05:00
|
|
|
|
|
|
|
|
|
func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([NSUserActivityRestoring]) -> Void) -> Bool {
|
|
|
|
|
guard let mainWindowController = mainWindowController else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
mainWindowController.handle(userActivity)
|
|
|
|
|
return true
|
|
|
|
|
}
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
|
|
|
|
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
2019-01-27 20:25:09 -08:00
|
|
|
|
// https://github.com/brentsimmons/NetNewsWire/issues/522
|
|
|
|
|
// I couldn’t reproduce the crashing bug, but it appears to happen on creating a main window
|
|
|
|
|
// and its views and view controllers. The check below is so that the app does nothing
|
|
|
|
|
// if the window doesn’t already exist — because it absolutely *should* exist already.
|
|
|
|
|
// And if the window exists, then maybe the views and view controllers are also already loaded?
|
|
|
|
|
// We’ll try this, and then see if we get more crash logs like this or not.
|
|
|
|
|
guard let mainWindowController = mainWindowController, mainWindowController.isWindowLoaded else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
mainWindowController.showWindow(self)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-27 18:00:09 -08:00
|
|
|
|
func applicationDidBecomeActive(_ notification: Notification) {
|
|
|
|
|
// It’s possible there’s a refresh timer set to go off in the past.
|
|
|
|
|
// In that case, refresh now and update the timer.
|
2019-04-23 11:20:44 -05:00
|
|
|
|
refreshTimer?.fireOldTimer()
|
2019-05-15 17:21:58 -05:00
|
|
|
|
syncTimer?.fireOldTimer()
|
2019-01-27 18:00:09 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
func applicationDidResignActive(_ notification: Notification) {
|
|
|
|
|
|
2018-12-02 10:51:32 -08:00
|
|
|
|
TimelineStringFormatter.emptyCaches()
|
2018-01-21 13:27:17 -08:00
|
|
|
|
|
|
|
|
|
saveState()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func applicationWillTerminate(_ notification: Notification) {
|
2019-01-27 18:00:09 -08:00
|
|
|
|
shuttingDown = true
|
2018-01-21 13:27:17 -08:00
|
|
|
|
saveState()
|
2019-05-21 17:20:49 -05:00
|
|
|
|
|
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
|
AccountManager.shared.syncArticleStatusAll() {
|
|
|
|
|
group.leave()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let timeout = DispatchTime.now() + .seconds(1)
|
|
|
|
|
_ = group.wait(timeout: timeout)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Notifications
|
|
|
|
|
|
2017-10-18 18:37:45 -07:00
|
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2017-10-18 18:37:45 -07:00
|
|
|
|
if note.object is AccountManager {
|
|
|
|
|
unreadCount = AccountManager.shared.unreadCount
|
|
|
|
|
}
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 13:16:32 -08:00
|
|
|
|
@objc func feedSettingDidChange(_ note: Notification) {
|
|
|
|
|
|
2019-03-17 13:54:30 -07:00
|
|
|
|
guard let feed = note.object as? Feed, let key = note.userInfo?[Feed.FeedSettingUserInfoKey] as? String else {
|
2017-11-26 13:16:32 -08:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-03-17 13:54:30 -07:00
|
|
|
|
if key == Feed.FeedSettingKey.homePageURL || key == Feed.FeedSettingKey.faviconURL {
|
|
|
|
|
let _ = faviconDownloader.favicon(for: feed)
|
|
|
|
|
}
|
2017-11-26 13:16:32 -08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-17 18:46:28 -08:00
|
|
|
|
@objc func inspectableObjectsDidChange(_ note: Notification) {
|
2018-01-21 12:46:22 -08:00
|
|
|
|
|
|
|
|
|
guard let inspectorWindowController = inspectorWindowController, inspectorWindowController.isOpen else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
inspectorWindowController.objects = objectsForInspector()
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-27 15:24:33 -08:00
|
|
|
|
@objc func userDefaultsDidChange(_ note: Notification) {
|
|
|
|
|
updateSortMenuItems()
|
2019-09-08 17:09:26 -05:00
|
|
|
|
updateGroupByFeedMenuItem()
|
2019-04-23 11:20:44 -05:00
|
|
|
|
refreshTimer?.update()
|
2019-09-03 22:17:31 -07:00
|
|
|
|
updateDockBadge()
|
2018-01-27 15:24:33 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
// MARK: Main Window
|
|
|
|
|
|
|
|
|
|
func windowControllerWithName(_ storyboardName: String) -> NSWindowController {
|
|
|
|
|
|
2018-12-09 12:32:33 -08:00
|
|
|
|
let storyboard = NSStoryboard(name: NSStoryboard.Name(storyboardName), bundle: nil)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
return storyboard.instantiateInitialController()! as! NSWindowController
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createAndShowMainWindow() {
|
|
|
|
|
|
|
|
|
|
if mainWindowController == nil {
|
2017-11-14 13:18:25 -08:00
|
|
|
|
mainWindowController = createReaderWindow()
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mainWindowController!.showWindow(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: NSUserInterfaceValidations
|
|
|
|
|
|
|
|
|
|
func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
|
2019-01-27 18:00:09 -08:00
|
|
|
|
if shuttingDown {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2018-02-03 10:56:12 -08:00
|
|
|
|
let isDisplayingSheet = mainWindowController?.isDisplayingSheet ?? false
|
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
if item.action == #selector(refreshAll(_:)) {
|
2019-05-19 14:23:54 -05:00
|
|
|
|
return !AccountManager.shared.refreshInProgress && !AccountManager.shared.activeAccounts.isEmpty
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
if item.action == #selector(addAppNews(_:)) {
|
2019-05-19 14:23:54 -05:00
|
|
|
|
return !isDisplayingSheet && !AccountManager.shared.anyAccountHasFeedWithURL(appNewsURLString) && !AccountManager.shared.activeAccounts.isEmpty
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
2018-01-27 15:13:45 -08:00
|
|
|
|
if item.action == #selector(sortByNewestArticleOnTop(_:)) || item.action == #selector(sortByOldestArticleOnTop(_:)) {
|
|
|
|
|
return mainWindowController?.isOpen ?? false
|
|
|
|
|
}
|
2018-02-03 10:56:12 -08:00
|
|
|
|
if item.action == #selector(showAddFeedWindow(_:)) || item.action == #selector(showAddFolderWindow(_:)) {
|
2019-05-19 10:21:42 -05:00
|
|
|
|
return !isDisplayingSheet && !AccountManager.shared.activeAccounts.isEmpty
|
2018-02-03 10:56:12 -08:00
|
|
|
|
}
|
2019-09-19 11:17:58 -05:00
|
|
|
|
#if !MAC_APP_STORE
|
2019-09-16 21:06:35 -05:00
|
|
|
|
if item.action == #selector(toggleWebInspectorEnabled(_:)) {
|
|
|
|
|
(item as! NSMenuItem).state = AppDefaults.webInspectorEnabled ? .on : .off
|
|
|
|
|
}
|
2019-09-19 10:38:17 -05:00
|
|
|
|
#endif
|
2017-05-27 10:43:27 -07:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 19:42:16 -05:00
|
|
|
|
// MARK: UNUserNotificationCenterDelegate
|
|
|
|
|
|
|
|
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
|
|
|
|
completionHandler([.alert, .badge, .sound])
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-03 11:39:48 -05:00
|
|
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
|
|
|
|
mainWindowController?.handle(response)
|
|
|
|
|
completionHandler()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
// MARK: Add Feed
|
|
|
|
|
|
2019-05-01 17:33:08 -05:00
|
|
|
|
func addFeed(_ urlString: String?, name: String? = nil, account: Account? = nil, folder: Folder? = nil) {
|
2017-05-22 13:00:45 -07:00
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
createAndShowMainWindow()
|
2018-02-03 10:56:12 -08:00
|
|
|
|
if mainWindowController!.isDisplayingSheet {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2019-05-01 17:33:08 -05:00
|
|
|
|
showAddFeedSheetOnWindow(mainWindowController!.window!, urlString: urlString, name: name, account: account, folder: folder)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-27 21:19:19 -08:00
|
|
|
|
// MARK: - Dock Badge
|
|
|
|
|
|
|
|
|
|
@objc func updateDockBadge() {
|
2019-03-08 11:35:37 -08:00
|
|
|
|
let label = unreadCount > 0 && !AppDefaults.hideDockUnreadCount ? "\(unreadCount)" : ""
|
2018-12-27 21:19:19 -08:00
|
|
|
|
NSApplication.shared.dockTile.badgeLabel = label
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-23 12:17:14 -07:00
|
|
|
|
// MARK: - Actions
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func showPreferences(_ sender: Any?) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
|
|
|
|
if preferencesWindowController == nil {
|
|
|
|
|
preferencesWindowController = windowControllerWithName("Preferences")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
preferencesWindowController!.showWindow(self)
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func showMainWindow(_ sender: Any?) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
|
|
|
|
createAndShowMainWindow()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func refreshAll(_ sender: Any?) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2019-05-26 11:54:32 -05:00
|
|
|
|
AccountManager.shared.refreshAll(errorHandler: ErrorHandler.present)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func showAddFeedWindow(_ sender: Any?) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
|
|
|
|
addFeed(nil)
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func showAddFolderWindow(_ sender: Any?) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
|
|
|
|
createAndShowMainWindow()
|
2017-11-15 13:13:40 -08:00
|
|
|
|
showAddFolderSheetOnWindow(mainWindowController!.window!)
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-13 18:33:23 -08:00
|
|
|
|
@IBAction func showKeyboardShortcutsWindow(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
if keyboardShortcutsWindowController == nil {
|
|
|
|
|
keyboardShortcutsWindowController = WebViewWindowController(title: NSLocalizedString("Keyboard Shortcuts", comment: "window title"))
|
|
|
|
|
let htmlFile = Bundle(for: type(of: self)).path(forResource: "KeyboardShortcuts", ofType: "html")!
|
|
|
|
|
keyboardShortcutsWindowController?.displayContents(of: htmlFile)
|
2017-12-20 17:23:46 -08:00
|
|
|
|
|
2017-12-22 11:13:20 -08:00
|
|
|
|
if let window = keyboardShortcutsWindowController?.window {
|
|
|
|
|
let point = NSPoint(x: 128, y: 64)
|
2019-03-04 14:39:30 -06:00
|
|
|
|
let size = NSSize(width: 620, height: 1100)
|
2017-12-22 11:13:20 -08:00
|
|
|
|
let minSize = NSSize(width: 400, height: 400)
|
|
|
|
|
window.setPointAndSizeAdjustingForScreen(point: point, size: size, minimumSize: minSize)
|
2017-12-20 17:23:46 -08:00
|
|
|
|
}
|
2017-11-13 18:33:23 -08:00
|
|
|
|
}
|
2017-12-20 17:23:46 -08:00
|
|
|
|
|
2017-11-13 18:33:23 -08:00
|
|
|
|
keyboardShortcutsWindowController!.showWindow(self)
|
|
|
|
|
}
|
2017-11-15 22:33:35 -08:00
|
|
|
|
|
|
|
|
|
@IBAction func toggleInspectorWindow(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
if inspectorWindowController == nil {
|
2018-01-20 19:06:07 -08:00
|
|
|
|
inspectorWindowController = (windowControllerWithName("Inspector") as! InspectorWindowController)
|
2017-11-15 22:33:35 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inspectorWindowController!.isOpen {
|
|
|
|
|
inspectorWindowController!.window!.performClose(self)
|
|
|
|
|
}
|
|
|
|
|
else {
|
2018-01-21 12:46:22 -08:00
|
|
|
|
inspectorWindowController!.objects = objectsForInspector()
|
2017-11-15 22:33:35 -08:00
|
|
|
|
inspectorWindowController!.showWindow(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func importOPMLFromFile(_ sender: Any?) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
|
createAndShowMainWindow()
|
|
|
|
|
if mainWindowController!.isDisplayingSheet {
|
|
|
|
|
return
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
2019-05-01 16:04:56 -05:00
|
|
|
|
|
|
|
|
|
importOPMLController = ImportOPMLWindowController()
|
|
|
|
|
importOPMLController?.runSheetOnWindow(mainWindowController!.window!)
|
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
2017-09-23 12:17:14 -07:00
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func exportOPML(_ sender: Any?) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
|
createAndShowMainWindow()
|
|
|
|
|
if mainWindowController!.isDisplayingSheet {
|
|
|
|
|
return
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
2019-05-01 16:04:56 -05:00
|
|
|
|
|
|
|
|
|
exportOPMLController = ExportOPMLWindowController()
|
|
|
|
|
exportOPMLController?.runSheetOnWindow(mainWindowController!.window!)
|
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
2017-09-23 12:17:14 -07:00
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func addAppNews(_ sender: Any?) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
|
2017-09-23 12:17:14 -07:00
|
|
|
|
if AccountManager.shared.anyAccountHasFeedWithURL(appNewsURLString) {
|
2017-05-27 10:43:27 -07:00
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-22 12:46:24 -07:00
|
|
|
|
addFeed(appNewsURLString, name: "NetNewsWire News")
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
2017-05-27 13:37:50 -07:00
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func openWebsite(_ sender: Any?) {
|
2017-05-27 13:37:50 -07:00
|
|
|
|
|
2018-08-28 22:18:24 -07:00
|
|
|
|
Browser.open("https://ranchero.com/netnewswire/", inBackground: false)
|
2017-05-27 13:37:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 17:38:00 -05:00
|
|
|
|
@IBAction func openHowToSupport(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
Browser.open("https://github.com/brentsimmons/NetNewsWire/blob/master/Technotes/HowToSupportNetNewsWire.markdown", inBackground: false)
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func openRepository(_ sender: Any?) {
|
2017-05-27 13:37:50 -07:00
|
|
|
|
|
2018-08-28 22:18:24 -07:00
|
|
|
|
Browser.open("https://github.com/brentsimmons/NetNewsWire", inBackground: false)
|
2017-05-27 13:37:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func openBugTracker(_ sender: Any?) {
|
2017-05-27 13:37:50 -07:00
|
|
|
|
|
2018-08-28 22:18:24 -07:00
|
|
|
|
Browser.open("https://github.com/brentsimmons/NetNewsWire/issues", inBackground: false)
|
2017-05-27 13:37:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-25 10:21:25 -07:00
|
|
|
|
@IBAction func openSlackGroup(_ sender: Any?) {
|
2019-09-29 22:46:40 -07:00
|
|
|
|
Browser.open("https://ranchero.com/netnewswire/slack", inBackground: false)
|
2019-05-25 10:21:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 21:23:48 -08:00
|
|
|
|
@IBAction func openTechnotes(_ sender: Any?) {
|
|
|
|
|
|
2018-08-28 22:18:24 -07:00
|
|
|
|
Browser.open("https://github.com/brentsimmons/NetNewsWire/tree/master/Technotes", inBackground: false)
|
2017-12-20 21:23:48 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 18:58:50 -08:00
|
|
|
|
@IBAction func showHelp(_ sender: Any?) {
|
2017-05-27 13:37:50 -07:00
|
|
|
|
|
2019-08-10 14:26:51 -07:00
|
|
|
|
Browser.open("https://ranchero.com/netnewswire/help/mac/5.0/en/", inBackground: false)
|
2017-05-27 13:37:50 -07:00
|
|
|
|
}
|
2017-11-16 18:23:07 -08:00
|
|
|
|
|
2018-02-12 13:10:13 -08:00
|
|
|
|
@IBAction func donateToAppCampForGirls(_ sender: Any?) {
|
|
|
|
|
Browser.open("https://appcamp4girls.com/contribute/", inBackground: false)
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 13:59:14 -08:00
|
|
|
|
@IBAction func showPrivacyPolicy(_ sender: Any?) {
|
|
|
|
|
Browser.open("https://ranchero.com/netnewswire/privacypolicy", inBackground: false)
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 22:39:13 -08:00
|
|
|
|
@IBAction func debugDropConditionalGetInfo(_ sender: Any?) {
|
|
|
|
|
#if DEBUG
|
2019-05-02 06:01:30 -05:00
|
|
|
|
AccountManager.shared.activeAccounts.forEach{ $0.debugDropConditionalGetInfo() }
|
2017-11-19 22:39:13 -08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2018-01-27 12:39:07 -08:00
|
|
|
|
|
2018-12-29 12:31:39 -08:00
|
|
|
|
@IBAction func debugTestCrashReporterWindow(_ sender: Any?) {
|
|
|
|
|
#if DEBUG
|
|
|
|
|
crashReportWindowController = CrashReportWindowController(crashLogText: "This is a test crash log.")
|
|
|
|
|
crashReportWindowController!.testing = true
|
|
|
|
|
crashReportWindowController!.showWindow(self)
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 22:06:24 -08:00
|
|
|
|
@IBAction func debugTestCrashReportSending(_ sender: Any?) {
|
|
|
|
|
#if DEBUG
|
|
|
|
|
CrashReporter.sendCrashLogText("This is a test. Hi, Brent.")
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-27 12:39:07 -08:00
|
|
|
|
@IBAction func gotoToday(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
createAndShowMainWindow()
|
|
|
|
|
mainWindowController!.gotoToday(sender)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBAction func gotoAllUnread(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
createAndShowMainWindow()
|
|
|
|
|
mainWindowController!.gotoAllUnread(sender)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBAction func gotoStarred(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
createAndShowMainWindow()
|
|
|
|
|
mainWindowController!.gotoStarred(sender)
|
|
|
|
|
}
|
2018-01-27 15:11:02 -08:00
|
|
|
|
|
|
|
|
|
@IBAction func sortByOldestArticleOnTop(_ sender: Any?) {
|
|
|
|
|
|
2018-12-27 21:34:21 -08:00
|
|
|
|
AppDefaults.timelineSortDirection = .orderedAscending
|
2018-01-27 15:11:02 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBAction func sortByNewestArticleOnTop(_ sender: Any?) {
|
|
|
|
|
|
2018-12-27 21:34:21 -08:00
|
|
|
|
AppDefaults.timelineSortDirection = .orderedDescending
|
2018-01-27 15:11:02 -08:00
|
|
|
|
}
|
2019-09-08 17:09:26 -05:00
|
|
|
|
|
|
|
|
|
@IBAction func groupByFeedToggled(_ sender: NSMenuItem) {
|
|
|
|
|
AppDefaults.timelineGroupByFeed.toggle()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 10:43:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-18 22:29:43 -08:00
|
|
|
|
// MARK: - Debug Menu
|
|
|
|
|
extension AppDelegate {
|
|
|
|
|
|
|
|
|
|
@IBAction func debugSearch(_ sender: Any?) {
|
2019-05-01 05:53:18 -05:00
|
|
|
|
AccountManager.shared.defaultAccount.debugRunSearch()
|
2019-02-18 22:29:43 -08:00
|
|
|
|
}
|
2019-09-16 21:06:35 -05:00
|
|
|
|
|
|
|
|
|
@IBAction func toggleWebInspectorEnabled(_ sender: Any?) {
|
2019-09-19 11:17:58 -05:00
|
|
|
|
#if !MAC_APP_STORE
|
2019-09-19 10:38:17 -05:00
|
|
|
|
let newValue = !AppDefaults.webInspectorEnabled
|
|
|
|
|
AppDefaults.webInspectorEnabled = newValue
|
2019-09-17 13:58:45 -05:00
|
|
|
|
|
2019-09-19 10:38:17 -05:00
|
|
|
|
// An attached inspector can display incorrectly on certain setups (like mine); default to displaying in a separate window,
|
|
|
|
|
// and reset the default to a separate window when the preference is toggled off and on again in case the inspector is
|
|
|
|
|
// accidentally reattached.
|
|
|
|
|
|
|
|
|
|
AppDefaults.webInspectorStartsAttached = false
|
|
|
|
|
NotificationCenter.default.post(name: .WebInspectorEnabledDidChange, object: newValue)
|
|
|
|
|
#endif
|
2019-09-16 21:06:35 -05:00
|
|
|
|
}
|
2019-02-18 22:29:43 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 13:18:25 -08:00
|
|
|
|
private extension AppDelegate {
|
2017-05-22 13:00:45 -07:00
|
|
|
|
|
2018-01-27 12:39:07 -08:00
|
|
|
|
func createReaderWindow() -> MainWindowController {
|
2017-11-14 13:18:25 -08:00
|
|
|
|
|
2018-01-27 12:39:07 -08:00
|
|
|
|
return windowControllerWithName("MainWindow") as! MainWindowController
|
2017-11-14 13:18:25 -08:00
|
|
|
|
}
|
2018-01-21 12:46:22 -08:00
|
|
|
|
|
|
|
|
|
func objectsForInspector() -> [Any]? {
|
|
|
|
|
|
|
|
|
|
guard let window = NSApplication.shared.mainWindow, let windowController = window.windowController as? MainWindowController else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return windowController.selectedObjectsInSidebar()
|
|
|
|
|
}
|
2018-01-21 13:27:17 -08:00
|
|
|
|
|
|
|
|
|
func saveState() {
|
|
|
|
|
|
2018-02-12 13:31:43 -08:00
|
|
|
|
inspectorWindowController?.saveState()
|
2019-02-25 22:25:29 -08:00
|
|
|
|
mainWindowController?.saveState()
|
2018-01-21 13:27:17 -08:00
|
|
|
|
}
|
2018-01-27 15:24:33 -08:00
|
|
|
|
|
|
|
|
|
func updateSortMenuItems() {
|
|
|
|
|
|
2018-12-27 21:34:21 -08:00
|
|
|
|
let sortByNewestOnTop = AppDefaults.timelineSortDirection == .orderedDescending
|
2018-01-27 15:24:33 -08:00
|
|
|
|
sortByNewestArticleOnTopMenuItem.state = sortByNewestOnTop ? .on : .off
|
|
|
|
|
sortByOldestArticleOnTopMenuItem.state = sortByNewestOnTop ? .off : .on
|
|
|
|
|
}
|
2019-09-08 17:09:26 -05:00
|
|
|
|
|
|
|
|
|
func updateGroupByFeedMenuItem() {
|
|
|
|
|
let groupByFeedEnabled = AppDefaults.timelineGroupByFeed
|
|
|
|
|
groupArticlesByFeedMenuItem.state = groupByFeedEnabled ? .on : .off
|
|
|
|
|
}
|
2017-11-14 13:18:25 -08:00
|
|
|
|
}
|
2018-02-08 00:11:52 -08:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
the ScriptingAppDelegate protocol exposes a narrow set of accessors with
|
|
|
|
|
internal visibility which are very similar to some private vars.
|
|
|
|
|
|
|
|
|
|
These would be unnecessary if the similar accessors were marked internal rather than private,
|
|
|
|
|
but for now, we'll keep the stratification of visibility
|
|
|
|
|
*/
|
|
|
|
|
extension AppDelegate : ScriptingAppDelegate {
|
|
|
|
|
|
|
|
|
|
internal var scriptingMainWindowController: ScriptingMainWindowController? {
|
|
|
|
|
return mainWindowController
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal var scriptingCurrentArticle: Article? {
|
|
|
|
|
return self.scriptingMainWindowController?.scriptingCurrentArticle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal var scriptingSelectedArticles: [Article] {
|
|
|
|
|
return self.scriptingMainWindowController?.scriptingSelectedArticles ?? []
|
|
|
|
|
}
|
|
|
|
|
}
|