2019-05-01 16:04:56 -05:00
|
|
|
//
|
|
|
|
|
// ExportOPMLWindowController.swift
|
|
|
|
|
// NetNewsWire
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 5/1/19.
|
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
|
import Account
|
2026-05-29 16:16:40 -07:00
|
|
|
import ActivityLog
|
2025-04-25 08:46:34 -07:00
|
|
|
import UniformTypeIdentifiers
|
2019-05-01 16:04:56 -05:00
|
|
|
|
2025-09-16 10:21:29 -07:00
|
|
|
final class ExportOPMLWindowController: NSWindowController {
|
2019-05-01 16:04:56 -05:00
|
|
|
|
2025-11-18 22:22:38 -08:00
|
|
|
@IBOutlet var accountPopUpButton: NSPopUpButton!
|
2019-05-01 16:04:56 -05:00
|
|
|
private weak var hostWindow: NSWindow?
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
convenience init() {
|
|
|
|
|
self.init(windowNibName: NSNib.Name("ExportOPMLSheet"))
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
override func windowDidLoad() {
|
|
|
|
|
accountPopUpButton.removeAllItems()
|
2019-05-21 15:15:26 -05:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
let menu = NSMenu()
|
2019-05-21 15:15:26 -05:00
|
|
|
accountPopUpButton.menu = menu
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2026-03-07 14:00:04 -08:00
|
|
|
let accounts: [Account] = AccountManager.shared.sortedAccounts
|
|
|
|
|
let savedAccountID: String? = AppDefaults.shared.exportOPMLAccountID
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2026-03-07 14:00:04 -08:00
|
|
|
for oneAccount in accounts {
|
2026-03-19 10:14:37 -07:00
|
|
|
addMenuItem(to: menu, for: oneAccount, savedAccountID: savedAccountID)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2026-03-19 10:14:37 -07:00
|
|
|
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)
|
2019-05-01 16:04:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: API
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow) {
|
2025-09-16 11:02:41 -07:00
|
|
|
guard let window else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
self.hostWindow = hostWindow
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-21 15:15:26 -05:00
|
|
|
if AccountManager.shared.accounts.count == 1 {
|
|
|
|
|
let account = AccountManager.shared.accounts.first!
|
2019-05-14 19:40:38 -05:00
|
|
|
exportOPML(account: account)
|
|
|
|
|
} else {
|
2025-09-16 11:02:41 -07:00
|
|
|
hostWindow.beginSheet(window)
|
2019-05-14 19:40:38 -05:00
|
|
|
}
|
2019-05-01 16:04:56 -05:00
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
// MARK: Actions
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
@IBAction func cancel(_ sender: Any) {
|
|
|
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.cancel)
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
@IBAction func exportOPML(_ sender: Any) {
|
|
|
|
|
|
|
|
|
|
guard let menuItem = accountPopUpButton.selectedItem else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-05-14 19:40:38 -05:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
let account = menuItem.representedObject as! Account
|
2020-07-02 11:17:38 +08:00
|
|
|
AppDefaults.shared.exportOPMLAccountID = account.accountID
|
2019-05-01 16:04:56 -05:00
|
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.OK)
|
2019-05-14 19:40:38 -05:00
|
|
|
exportOPML(account: account)
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-14 19:40:38 -05:00
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-14 19:40:38 -05:00
|
|
|
func exportOPML(account: Account) {
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
let panel = NSSavePanel()
|
2025-04-25 08:46:34 -07:00
|
|
|
panel.allowedContentTypes = [UTType.opml]
|
2019-05-01 16:04:56 -05: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
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-21 15:15:26 -05:00
|
|
|
let accountName = account.nameForDisplay.replacingOccurrences(of: " ", with: "").trimmingCharacters(in: .whitespaces)
|
2019-05-26 22:17:19 -07:00
|
|
|
panel.nameFieldStringValue = "Subscriptions-\(accountName).opml"
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
panel.beginSheetModal(for: hostWindow!) { result in
|
|
|
|
|
if result == NSApplication.ModalResponse.OK, let url = panel.url {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
let filename = url.lastPathComponent
|
|
|
|
|
do {
|
2026-05-29 16:16:40 -07:00
|
|
|
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)
|
|
|
|
|
}
|
2026-01-02 11:57:55 -08:00
|
|
|
} catch let error as NSError {
|
2019-05-01 16:04:56 -05:00
|
|
|
NSApplication.shared.presentError(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
2019-05-01 16:04:56 -05:00
|
|
|
}
|