2019-05-01 23:04:56 +02:00
|
|
|
//
|
|
|
|
// ExportOPMLWindowController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 5/1/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
import Account
|
2024-02-25 04:17:58 +01:00
|
|
|
import UniformTypeIdentifiers
|
2019-05-01 23:04:56 +02:00
|
|
|
|
|
|
|
class ExportOPMLWindowController: NSWindowController {
|
|
|
|
|
|
|
|
@IBOutlet weak var accountPopUpButton: NSPopUpButton!
|
|
|
|
private weak var hostWindow: NSWindow?
|
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
self.init(windowNibName: NSNib.Name("ExportOPMLSheet"))
|
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidLoad() {
|
|
|
|
accountPopUpButton.removeAllItems()
|
2019-05-21 22:15:26 +02:00
|
|
|
|
2019-05-01 23:04:56 +02:00
|
|
|
let menu = NSMenu()
|
2019-05-21 22:15:26 +02:00
|
|
|
accountPopUpButton.menu = menu
|
|
|
|
|
|
|
|
for oneAccount in AccountManager.shared.sortedAccounts {
|
|
|
|
|
2019-05-01 23:04:56 +02:00
|
|
|
let oneMenuItem = NSMenuItem()
|
|
|
|
oneMenuItem.title = oneAccount.nameForDisplay
|
|
|
|
oneMenuItem.representedObject = oneAccount
|
|
|
|
menu.addItem(oneMenuItem)
|
2019-05-21 22:15:26 +02:00
|
|
|
|
2020-07-02 05:17:38 +02:00
|
|
|
if oneAccount.accountID == AppDefaults.shared.exportOPMLAccountID {
|
2019-05-21 22:15:26 +02:00
|
|
|
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
|
|
|
|
2019-05-21 22:15:26 +02:00
|
|
|
if AccountManager.shared.accounts.count == 1 {
|
|
|
|
let account = AccountManager.shared.accounts.first!
|
2019-05-15 02:40:38 +02:00
|
|
|
exportOPML(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 exportOPML(_ 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
|
2020-07-02 05:17:38 +02:00
|
|
|
AppDefaults.shared.exportOPMLAccountID = 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
|
|
|
exportOPML(account: account)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func exportOPML(account: Account) {
|
2019-05-01 23:04:56 +02:00
|
|
|
|
|
|
|
let panel = NSSavePanel()
|
2024-02-25 04:17:58 +01:00
|
|
|
panel.allowedContentTypes = [UTType.opml]
|
2019-05-01 23:04:56 +02:00
|
|
|
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
|
2019-05-21 22:15:26 +02:00
|
|
|
|
|
|
|
let accountName = account.nameForDisplay.replacingOccurrences(of: " ", with: "").trimmingCharacters(in: .whitespaces)
|
2019-05-27 07:17:19 +02:00
|
|
|
panel.nameFieldStringValue = "Subscriptions-\(accountName).opml"
|
2019-05-01 23:04:56 +02:00
|
|
|
|
|
|
|
panel.beginSheetModal(for: hostWindow!) { result in
|
|
|
|
if result == NSApplication.ModalResponse.OK, let url = panel.url {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
let filename = url.lastPathComponent
|
|
|
|
let opmlString = OPMLExporter.OPMLString(with: account, title: filename)
|
|
|
|
do {
|
|
|
|
try opmlString.write(to: url, atomically: true, encoding: String.Encoding.utf8)
|
|
|
|
}
|
|
|
|
catch let error as NSError {
|
|
|
|
NSApplication.shared.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-15 02:40:38 +02:00
|
|
|
|
2019-05-01 23:04:56 +02:00
|
|
|
}
|