NetNewsWire/Evergreen/AppDelegate.swift

429 lines
12 KiB
Swift
Raw Normal View History

2017-05-22 22:00:45 +02:00
//
// AppDelegate.swift
// Evergreen
//
2017-05-27 19:43:27 +02:00
// Created by Brent Simmons on 7/11/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
2017-05-22 22:00:45 +02:00
//
import Cocoa
2017-05-27 19:43:27 +02:00
import DB5
import Data
2017-05-27 19:43:27 +02:00
import RSTextDrawing
import RSTree
2017-05-27 22:37:50 +02:00
import RSWeb
2017-09-18 02:12:42 +02:00
import Account
import RSCore
2017-05-27 19:43:27 +02:00
var appDelegate: AppDelegate!
2017-05-22 22:00:45 +02:00
@NSApplicationMain
2017-11-19 22:57:42 +01:00
class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, UnreadCountProvider {
2017-05-22 22:00:45 +02:00
var currentTheme: VSTheme!
var faviconDownloader: FaviconDownloader!
var appName: String!
var pseudoFeeds = [PseudoFeed]()
2017-05-27 19:43:27 +02:00
var unreadCount = 0 {
didSet {
if unreadCount != oldValue {
dockBadge.update()
2017-11-19 22:57:42 +01:00
postUnreadCountDidChangeNotification()
}
2017-05-27 19:43:27 +02:00
}
}
private let windowControllers = NSMutableArray()
private var preferencesWindowController: NSWindowController?
private var mainWindowController: NSWindowController?
private var readerWindows = [NSWindowController]()
private var feedListWindowController: NSWindowController?
private var dinosaursWindowController: DinosaursWindowController?
private var addFeedController: AddFeedController?
private var addFolderWindowController: AddFolderWindowController?
private var keyboardShortcutsWindowController: WebViewWindowController?
private var inspectorWindowController: InspectorWindowController?
private var logWindowController: LogWindowController?
private var panicButtonWindowController: PanicButtonWindowController?
private let log = Log()
private let themeLoader = VSThemeLoader()
private let appNewsURLString = "https://ranchero.com/evergreen/feed.json"
private let dockBadge = DockBadge()
2017-05-27 19:43:27 +02:00
override init() {
NSWindow.allowsAutomaticWindowTabbing = false
super.init()
dockBadge.appDelegate = self
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
appDelegate = self
}
// MARK: - API
func logMessage(_ message: String, type: LogItem.ItemType) {
let logItem = LogItem(type: type, message: message)
log.add(logItem)
}
func logDebugMessage(_ message: String) {
logMessage(message, type: .debug)
2017-05-27 19:43:27 +02:00
}
func showAddFolderSheetOnWindow(_ window: NSWindow) {
addFolderWindowController = AddFolderWindowController()
addFolderWindowController!.runSheetOnWindow(window)
}
func markOlderArticlesAsRead(with window: NSWindow) {
2017-11-17 03:23:07 +01:00
panicButtonWindowController = PanicButtonWindowController()
panicButtonWindowController!.runSheetOnWindow(window)
}
2017-11-20 01:28:26 +01:00
func markEverywhereAsRead(with window: NSWindow) {
let alert = NSAlert()
alert.messageText = NSLocalizedString("Mark All Articles as Read Everywhere?", comment: "Mark Everywhere alert messageText")
alert.informativeText = NSLocalizedString("This will mark every single article as read. All of them. The unread count will be zero.\n\nNote: this operation cannot be undone.", comment: "Mark Everywhere informativeText.")
alert.addButton(withTitle: NSLocalizedString("Mark All Articles as Read", comment: "Mark Everywhere alert button."))
alert.addButton(withTitle: NSLocalizedString("Dont Mark as Read", comment: "Mark Everywhere alert button."))
alert.beginSheetModal(for: window) { (returnCode) in
if returnCode == .alertFirstButtonReturn {
self.markEverywhereAsRead()
}
}
}
func markEverywhereAsRead() {
AccountManager.shared.accounts.forEach { $0.markEverywhereAsRead() }
}
// MARK: - NSApplicationDelegate
2017-05-27 19:43:27 +02:00
func applicationDidFinishLaunching(_ note: Notification) {
appName = Bundle.main.infoDictionary!["CFBundleExecutable"]! as! String
2017-09-23 21:17:14 +02:00
let isFirstRun = AppDefaults.shared.isFirstRun
logDebugMessage(isFirstRun ? "Is first run." : "Is not first run.")
2017-09-24 21:24:44 +02:00
let localAccount = AccountManager.shared.localAccount
2017-09-27 06:43:40 +02:00
DefaultFeedsImporter.importIfNeeded(isFirstRun, account: localAccount)
2017-05-27 19:43:27 +02:00
currentTheme = themeLoader.defaultTheme
let faviconsFolder = RSDataSubfolder(nil, "Favicons")!
faviconDownloader = FaviconDownloader(folder: faviconsFolder)
let todayFeed = SmartFeed(delegate: TodayFeedDelegate())
let unreadFeed = UnreadFeed()
let starredFeed = SmartFeed(delegate: StarredFeedDelegate())
pseudoFeeds = [todayFeed, unreadFeed, starredFeed]
2017-09-24 21:24:44 +02:00
2017-05-27 19:43:27 +02:00
createAndShowMainWindow()
#if RELEASE
DispatchQueue.main.async {
self.refreshAll(self)
}
#endif
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(AppDelegate.getURL(_:_:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
DispatchQueue.main.async {
self.unreadCount = AccountManager.shared.unreadCount
}
2017-05-27 19:43:27 +02:00
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if (!flag) {
createAndShowMainWindow()
}
return false
}
func applicationDidResignActive(_ notification: Notification) {
RSSingleLineRenderer.emptyCache()
RSMultiLineRenderer.emptyCache()
TimelineCellData.emptyCache()
timelineEmptyCaches()
}
// MARK: GetURL Apple Event
2017-09-18 02:12:42 +02:00
@objc func getURL(_ event: NSAppleEventDescriptor, _ withReplyEvent: NSAppleEventDescriptor) {
2017-05-22 22:00:45 +02:00
2017-05-27 19:43:27 +02:00
guard let urlString = event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue else {
return
}
2017-05-22 22:00:45 +02:00
2017-05-27 19:43:27 +02:00
let normalizedURLString = urlString.rs_normalizedURL()
if !normalizedURLString.rs_stringMayBeURL() {
return
}
DispatchQueue.main.async {
self.addFeed(normalizedURLString)
}
2017-05-22 22:00:45 +02:00
}
2017-05-27 19:43:27 +02:00
// MARK: Notifications
@objc func unreadCountDidChange(_ note: Notification) {
2017-05-27 19:43:27 +02:00
if note.object is AccountManager {
unreadCount = AccountManager.shared.unreadCount
}
2017-05-27 19:43:27 +02:00
}
// MARK: Main Window
func windowControllerWithName(_ storyboardName: String) -> NSWindowController {
2017-09-18 02:12:42 +02:00
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: storyboardName), bundle: nil)
2017-05-27 19:43:27 +02:00
return storyboard.instantiateInitialController()! as! NSWindowController
}
func createAndShowMainWindow() {
if mainWindowController == nil {
mainWindowController = createReaderWindow()
2017-05-27 19:43:27 +02:00
}
mainWindowController!.showWindow(self)
}
// MARK: NSUserInterfaceValidations
func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
if item.action == #selector(refreshAll(_:)) {
2017-09-23 21:17:14 +02:00
return !AccountManager.shared.refreshInProgress
2017-05-27 19:43:27 +02:00
}
if item.action == #selector(addAppNews(_:)) {
2017-09-23 21:17:14 +02:00
return !AccountManager.shared.anyAccountHasFeedWithURL(appNewsURLString)
2017-05-27 19:43:27 +02:00
}
return true
}
// MARK: Add Feed
func addFeed(_ urlString: String?, _ name: String? = nil) {
2017-05-22 22:00:45 +02:00
2017-05-27 19:43:27 +02:00
createAndShowMainWindow()
addFeedController = AddFeedController(hostWindow: mainWindowController!.window!)
addFeedController?.showAddFeedSheet(urlString, name)
}
2017-09-23 21:17:14 +02:00
// MARK: - Actions
2017-05-27 19:43:27 +02:00
@IBAction func newReaderWindow(_ sender: Any?) {
let readerWindow = createReaderWindow()
readerWindows += [readerWindow]
readerWindow.showWindow(self)
}
2017-05-27 19:43:27 +02:00
@IBAction func showPreferences(_ sender: AnyObject) {
if preferencesWindowController == nil {
preferencesWindowController = windowControllerWithName("Preferences")
}
preferencesWindowController!.showWindow(self)
}
@IBAction func showMainWindow(_ sender: AnyObject) {
createAndShowMainWindow()
}
@IBAction func refreshAll(_ sender: AnyObject) {
2017-09-24 21:24:44 +02:00
AccountManager.shared.refreshAll()
2017-05-27 19:43:27 +02:00
}
@IBAction func showAddFeedWindow(_ sender: AnyObject) {
addFeed(nil)
}
@IBAction func showAddFolderWindow(_ sender: AnyObject) {
createAndShowMainWindow()
showAddFolderSheetOnWindow(mainWindowController!.window!)
2017-05-27 19:43:27 +02:00
}
@IBAction func showFeedList(_ sender: AnyObject) {
if feedListWindowController == nil {
feedListWindowController = windowControllerWithName("FeedList")
}
feedListWindowController!.showWindow(self)
}
@IBAction func showDinosaursWindow(_ sender: Any?) {
if dinosaursWindowController == nil {
dinosaursWindowController = DinosaursWindowController()
}
dinosaursWindowController!.showWindow(self)
}
@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)
}
keyboardShortcutsWindowController!.showWindow(self)
}
@IBAction func toggleInspectorWindow(_ sender: Any?) {
if inspectorWindowController == nil {
inspectorWindowController = InspectorWindowController()
}
if inspectorWindowController!.isOpen {
inspectorWindowController!.window!.performClose(self)
}
else {
inspectorWindowController!.showWindow(self)
}
}
@IBAction func showLogWindow(_ sender: Any?) {
if logWindowController == nil {
logWindowController = LogWindowController(title: "Errors", log: log)
}
logWindowController!.showWindow(self)
}
2017-05-27 19:43:27 +02:00
@IBAction func importOPMLFromFile(_ sender: AnyObject) {
let panel = NSOpenPanel()
panel.canDownloadUbiquitousContents = true
panel.canResolveUbiquitousConflicts = true
panel.canChooseFiles = true
panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
panel.resolvesAliases = true
panel.allowedFileTypes = ["opml"]
panel.allowsOtherFileTypes = false
let result = panel.runModal()
2017-09-23 21:17:14 +02:00
if result == NSApplication.ModalResponse.OK, let url = panel.url {
DispatchQueue.main.async {
do {
try OPMLImporter.parseAndImport(fileURL: url, account: AccountManager.shared.localAccount)
}
catch let error as NSError {
NSApplication.shared.presentError(error)
}
2017-05-27 19:43:27 +02:00
}
}
}
2017-09-23 21:17:14 +02:00
2017-05-27 20:33:31 +02:00
@IBAction func importOPMLFromURL(_ sender: AnyObject) {
}
2017-05-27 19:43:27 +02:00
@IBAction func exportOPML(_ sender: AnyObject) {
let panel = NSSavePanel()
panel.allowedFileTypes = ["opml"]
panel.allowsOtherFileTypes = false
panel.prompt = NSLocalizedString("Export OPML", comment: "Export OPML")
panel.title = NSLocalizedString("Export OPML", comment: "Export OPML")
panel.nameFieldLabel = NSLocalizedString("Export to:", comment: "Export OPML")
panel.message = NSLocalizedString("Choose a location for the exported OPML file.", comment: "Export OPML")
panel.isExtensionHidden = false
panel.nameFieldStringValue = "MySubscriptions.opml"
let result = panel.runModal()
2017-10-07 21:00:47 +02:00
if result == NSApplication.ModalResponse.OK, let url = panel.url {
2017-09-23 21:17:14 +02:00
DispatchQueue.main.async {
2017-09-24 21:24:44 +02:00
let opmlString = AccountManager.shared.localAccount.OPMLString(indentLevel: 0)
2017-09-23 21:17:14 +02:00
do {
try opmlString.write(to: url, atomically: true, encoding: String.Encoding.utf8)
}
catch let error as NSError {
NSApplication.shared.presentError(error)
2017-05-27 19:43:27 +02:00
}
}
}
}
2017-09-23 21:17:14 +02:00
2017-05-27 19:43:27 +02:00
@IBAction func addAppNews(_ sender: AnyObject) {
2017-09-23 21:17:14 +02:00
if AccountManager.shared.anyAccountHasFeedWithURL(appNewsURLString) {
2017-05-27 19:43:27 +02:00
return
}
addFeed(appNewsURLString, "Evergreen News")
}
2017-05-27 22:37:50 +02:00
@IBAction func openWebsite(_ sender: AnyObject) {
Browser.open("//ranchero.com/evergreen/", inBackground: false)
2017-05-27 22:37:50 +02:00
}
@IBAction func openRepository(_ sender: AnyObject) {
Browser.open("https://github.com/brentsimmons/Evergreen", inBackground: false)
2017-05-27 22:37:50 +02:00
}
@IBAction func openBugTracker(_ sender: AnyObject) {
Browser.open("https://github.com/brentsimmons/Evergreen/issues", inBackground: false)
2017-05-27 22:37:50 +02:00
}
@IBAction func showHelp(_ sender: AnyObject) {
Browser.open("https://ranchero.com/evergreen/help/1.0/", inBackground: false)
2017-05-27 22:37:50 +02:00
}
2017-11-17 03:23:07 +01:00
@IBAction func markOlderArticlesAsRead(_ sender: Any?) {
2017-11-17 03:23:07 +01:00
createAndShowMainWindow()
markOlderArticlesAsRead(with: mainWindowController!.window!)
2017-11-17 03:23:07 +01:00
}
2017-11-20 01:28:26 +01:00
@IBAction func markEverywhereAsRead(_ sender: Any?) {
createAndShowMainWindow()
markEverywhereAsRead(with: mainWindowController!.window!)
}
2017-11-20 07:39:13 +01:00
@IBAction func debugDropConditionalGetInfo(_ sender: Any?) {
#if DEBUG
print("debug")
#endif
}
2017-05-27 19:43:27 +02:00
}
private extension AppDelegate {
2017-05-22 22:00:45 +02:00
func createReaderWindow() -> NSWindowController {
return windowControllerWithName("MainWindow")
}
}