mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 16:26:04 +00:00
117 lines
3.2 KiB
Swift
117 lines
3.2 KiB
Swift
//
|
|
// ExportOPMLWindowController.swift
|
|
// NetNewsWire
|
|
//
|
|
// Created by Maurice Parker on 5/1/19.
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
//
|
|
|
|
import AppKit
|
|
import Account
|
|
import ActivityLog
|
|
import UniformTypeIdentifiers
|
|
|
|
final class ExportOPMLWindowController: NSWindowController {
|
|
|
|
@IBOutlet var accountPopUpButton: NSPopUpButton!
|
|
private weak var hostWindow: NSWindow?
|
|
|
|
convenience init() {
|
|
self.init(windowNibName: NSNib.Name("ExportOPMLSheet"))
|
|
}
|
|
|
|
override func windowDidLoad() {
|
|
accountPopUpButton.removeAllItems()
|
|
|
|
let menu = NSMenu()
|
|
accountPopUpButton.menu = menu
|
|
|
|
let accounts: [Account] = AccountManager.shared.sortedAccounts
|
|
let savedAccountID: String? = AppDefaults.exportOPMLAccountID
|
|
|
|
for oneAccount in accounts {
|
|
addMenuItem(to: menu, for: oneAccount, savedAccountID: savedAccountID)
|
|
}
|
|
}
|
|
|
|
private func addMenuItem(to menu: NSMenu, for account: Account, savedAccountID: String?) {
|
|
let menuItem = NSMenuItem()
|
|
menuItem.title = account.nameForDisplay
|
|
menuItem.representedObject = account
|
|
menu.addItem(menuItem)
|
|
|
|
if account.accountID == savedAccountID {
|
|
accountPopUpButton.select(menuItem)
|
|
}
|
|
}
|
|
|
|
// MARK: API
|
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow) {
|
|
guard let window else {
|
|
return
|
|
}
|
|
|
|
self.hostWindow = hostWindow
|
|
|
|
if AccountManager.shared.accounts.count == 1 {
|
|
let account = AccountManager.shared.accounts.first!
|
|
exportOPML(account: account)
|
|
} else {
|
|
hostWindow.beginSheet(window)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
let account = menuItem.representedObject as! Account
|
|
AppDefaults.exportOPMLAccountID = account.accountID
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.OK)
|
|
exportOPML(account: account)
|
|
|
|
}
|
|
|
|
func exportOPML(account: Account) {
|
|
|
|
let panel = NSSavePanel()
|
|
panel.allowedContentTypes = [UTType.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
|
|
|
|
let accountName = account.nameForDisplay.replacingOccurrences(of: " ", with: "").trimmingCharacters(in: .whitespaces)
|
|
panel.nameFieldStringValue = "Subscriptions-\(accountName).opml"
|
|
|
|
panel.beginSheetModal(for: hostWindow!) { result in
|
|
if result == NSApplication.ModalResponse.OK, let url = panel.url {
|
|
DispatchQueue.main.async {
|
|
let filename = url.lastPathComponent
|
|
do {
|
|
try account.logActivity(kind: .exportOPML, detail: filename) {
|
|
let opmlString = OPMLExporter.OPMLString(with: account, title: filename)
|
|
try opmlString.write(to: url, atomically: true, encoding: String.Encoding.utf8)
|
|
}
|
|
} catch let error as NSError {
|
|
NSApplication.shared.presentError(error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|