2019-09-13 01:05:29 +02:00
|
|
|
|
//
|
|
|
|
|
// OPMLFile.swift
|
|
|
|
|
// Account
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 9/12/19.
|
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import os.log
|
|
|
|
|
import RSCore
|
|
|
|
|
import RSParser
|
|
|
|
|
|
|
|
|
|
final class OPMLFile: NSObject, NSFilePresenter {
|
|
|
|
|
|
|
|
|
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "account")
|
|
|
|
|
|
|
|
|
|
private let filename: String
|
|
|
|
|
private let account: Account
|
|
|
|
|
private let operationQueue: OperationQueue
|
|
|
|
|
|
|
|
|
|
var presentedItemURL: URL? {
|
2019-09-13 01:41:42 +02:00
|
|
|
|
return URL(fileURLWithPath: filename)
|
2019-09-13 01:05:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var presentedItemOperationQueue: OperationQueue {
|
|
|
|
|
return operationQueue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(filename: String, account: Account) {
|
|
|
|
|
self.filename = filename
|
|
|
|
|
self.account = account
|
|
|
|
|
operationQueue = OperationQueue()
|
|
|
|
|
operationQueue.maxConcurrentOperationCount = 1
|
2019-09-13 01:41:42 +02:00
|
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
|
|
NSFileCoordinator.addFilePresenter(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func presentedItemDidChange() {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.reload()
|
|
|
|
|
}
|
2019-09-13 01:05:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func load() {
|
2019-09-13 01:41:42 +02:00
|
|
|
|
guard let opmlItems = parsedOPMLItems() else { return }
|
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
|
account.loadOPMLItems(opmlItems, parentFolder: nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func save() {
|
|
|
|
|
let opmlDocumentString = opmlDocument()
|
|
|
|
|
do {
|
|
|
|
|
let url = URL(fileURLWithPath: filename)
|
|
|
|
|
try opmlDocumentString.write(to: url, atomically: true, encoding: .utf8)
|
|
|
|
|
} catch let error as NSError {
|
|
|
|
|
os_log(.error, log: log, "Save to disk failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension OPMLFile {
|
|
|
|
|
|
|
|
|
|
func reload() {
|
|
|
|
|
guard let opmlItems = parsedOPMLItems() else { return }
|
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
|
account.topLevelFeeds.removeAll()
|
|
|
|
|
account.loadOPMLItems(opmlItems, parentFolder: nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parsedOPMLItems() -> [RSOPMLItem]? {
|
|
|
|
|
|
2019-09-13 01:05:29 +02:00
|
|
|
|
let opmlFileURL = URL(fileURLWithPath: filename)
|
|
|
|
|
var fileData: Data?
|
|
|
|
|
do {
|
|
|
|
|
fileData = try Data(contentsOf: opmlFileURL)
|
|
|
|
|
} catch {
|
|
|
|
|
// Commented out because it’s not an error on first run.
|
|
|
|
|
// TODO: make it so we know if it’s first run or not.
|
|
|
|
|
//NSApplication.shared.presentError(error)
|
2019-09-13 01:41:42 +02:00
|
|
|
|
return nil
|
2019-09-13 01:05:29 +02:00
|
|
|
|
}
|
|
|
|
|
guard let opmlData = fileData else {
|
2019-09-13 01:41:42 +02:00
|
|
|
|
return nil
|
2019-09-13 01:05:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let parserData = ParserData(url: opmlFileURL.absoluteString, data: opmlData)
|
|
|
|
|
var opmlDocument: RSOPMLDocument?
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
|
|
|
|
|
} catch {
|
|
|
|
|
os_log(.error, log: log, "OPML Import failed: %@.", error.localizedDescription)
|
2019-09-13 01:41:42 +02:00
|
|
|
|
return nil
|
2019-09-13 01:05:29 +02:00
|
|
|
|
}
|
2019-09-13 01:41:42 +02:00
|
|
|
|
|
|
|
|
|
return opmlDocument?.children
|
|
|
|
|
|
2019-09-13 01:05:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func opmlDocument() -> String {
|
|
|
|
|
let escapedTitle = account.nameForDisplay.rs_stringByEscapingSpecialXMLCharacters()
|
|
|
|
|
let openingText =
|
|
|
|
|
"""
|
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<!-- OPML generated by NetNewsWire -->
|
|
|
|
|
<opml version="1.1">
|
|
|
|
|
<head>
|
|
|
|
|
<title>\(escapedTitle)</title>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
let middleText = account.OPMLString(indentLevel: 0)
|
|
|
|
|
|
|
|
|
|
let closingText =
|
|
|
|
|
"""
|
|
|
|
|
</body>
|
|
|
|
|
</opml>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
let opml = openingText + middleText + closingText
|
|
|
|
|
return opml
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|