2019-05-01 23:04:56 +02:00
|
|
|
//
|
|
|
|
// ImportOPMLWindowController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 5/1/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
import Account
|
|
|
|
|
|
|
|
class ImportOPMLWindowController: NSWindowController {
|
|
|
|
|
|
|
|
@IBOutlet weak var accountPopUpButton: NSPopUpButton!
|
|
|
|
private weak var hostWindow: NSWindow?
|
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
self.init(windowNibName: NSNib.Name("ImportOPMLSheet"))
|
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidLoad() {
|
|
|
|
accountPopUpButton.removeAllItems()
|
2019-05-21 21:56:41 +02:00
|
|
|
|
2019-05-01 23:04:56 +02:00
|
|
|
let menu = NSMenu()
|
2019-05-21 21:56:41 +02:00
|
|
|
accountPopUpButton.menu = menu
|
|
|
|
|
2019-05-02 13:01:30 +02:00
|
|
|
for oneAccount in AccountManager.shared.sortedActiveAccounts {
|
2019-05-21 21:56:41 +02:00
|
|
|
|
2019-06-20 00:50:32 +02:00
|
|
|
if !oneAccount.isOPMLImportSupported {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:04:56 +02:00
|
|
|
let oneMenuItem = NSMenuItem()
|
|
|
|
oneMenuItem.title = oneAccount.nameForDisplay
|
|
|
|
oneMenuItem.representedObject = oneAccount
|
|
|
|
menu.addItem(oneMenuItem)
|
2019-05-21 21:56:41 +02:00
|
|
|
|
|
|
|
if oneAccount.accountID == AppDefaults.importOPMLAccountID {
|
|
|
|
accountPopUpButton.select(oneMenuItem)
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:04:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow) {
|
2019-05-15 02:40:38 +02:00
|
|
|
|
2019-05-01 23:04:56 +02:00
|
|
|
self.hostWindow = hostWindow
|
2019-05-15 02:40:38 +02:00
|
|
|
|
|
|
|
if AccountManager.shared.activeAccounts.count == 1 {
|
|
|
|
let account = AccountManager.shared.activeAccounts.first!
|
|
|
|
importOPML(account: account)
|
|
|
|
} else {
|
|
|
|
hostWindow.beginSheet(window!)
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction func cancel(_ sender: Any) {
|
|
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.cancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func importOPML(_ sender: Any) {
|
|
|
|
|
|
|
|
guard let menuItem = accountPopUpButton.selectedItem else {
|
|
|
|
return
|
|
|
|
}
|
2019-05-15 02:40:38 +02:00
|
|
|
|
2019-05-01 23:04:56 +02:00
|
|
|
let account = menuItem.representedObject as! Account
|
2019-05-21 21:56:41 +02:00
|
|
|
AppDefaults.importOPMLAccountID = account.accountID
|
2019-05-01 23:04:56 +02:00
|
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.OK)
|
2019-05-15 02:40:38 +02:00
|
|
|
importOPML(account: account)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func importOPML(account: Account) {
|
2019-05-01 23:04:56 +02:00
|
|
|
|
|
|
|
let panel = NSOpenPanel()
|
|
|
|
panel.canDownloadUbiquitousContents = true
|
|
|
|
panel.canResolveUbiquitousConflicts = true
|
|
|
|
panel.canChooseFiles = true
|
|
|
|
panel.allowsMultipleSelection = false
|
|
|
|
panel.canChooseDirectories = false
|
|
|
|
panel.resolvesAliases = true
|
|
|
|
panel.allowedFileTypes = ["opml", "xml"]
|
|
|
|
panel.allowsOtherFileTypes = false
|
|
|
|
|
2019-05-11 19:26:23 +02:00
|
|
|
panel.beginSheetModal(for: hostWindow!) { modalResult in
|
|
|
|
if modalResult == NSApplication.ModalResponse.OK, let url = panel.url {
|
|
|
|
account.importOPML(url) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
2019-05-01 23:04:56 +02:00
|
|
|
NSApplication.shared.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|