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
|
|
|
|
|
|
2019-09-13 22:46:22 +02:00
|
|
|
|
final class OPMLFile {
|
2019-09-13 01:05:29 +02:00
|
|
|
|
|
2019-09-13 21:56:24 +02:00
|
|
|
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "opmlFile")
|
2019-09-13 01:05:29 +02:00
|
|
|
|
|
2019-09-13 17:48:04 +02:00
|
|
|
|
private let fileURL: URL
|
2019-09-13 01:05:29 +02:00
|
|
|
|
private let account: Account
|
2019-09-13 23:12:19 +02:00
|
|
|
|
private lazy var managedFile = ManagedResourceFile(fileURL: fileURL, load: loadCallback, save: saveCallback)
|
2019-09-13 01:05:29 +02:00
|
|
|
|
|
|
|
|
|
init(filename: String, account: Account) {
|
2019-09-13 17:48:04 +02:00
|
|
|
|
self.fileURL = URL(fileURLWithPath: filename)
|
2019-09-13 01:05:29 +02:00
|
|
|
|
self.account = account
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 17:15:11 +02:00
|
|
|
|
func markAsDirty() {
|
2019-09-13 22:46:22 +02:00
|
|
|
|
managedFile.markAsDirty()
|
2019-09-13 17:15:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 01:05:29 +02:00
|
|
|
|
func load() {
|
2019-09-13 22:46:22 +02:00
|
|
|
|
managedFile.load()
|
2019-09-13 01:41:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 18:09:40 +02:00
|
|
|
|
func saveIfNecessary() {
|
|
|
|
|
managedFile.saveIfNecessary()
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 17:15:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension OPMLFile {
|
2019-09-13 22:46:22 +02:00
|
|
|
|
|
|
|
|
|
func loadCallback() {
|
|
|
|
|
guard let opmlItems = parsedOPMLItems() else { return }
|
|
|
|
|
BatchUpdate.shared.perform {
|
2019-09-13 23:12:19 +02:00
|
|
|
|
account.topLevelFeeds.removeAll()
|
2019-09-13 22:46:22 +02:00
|
|
|
|
account.loadOPMLItems(opmlItems, parentFolder: nil)
|
2019-09-13 17:15:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-13 22:46:22 +02:00
|
|
|
|
|
|
|
|
|
func saveCallback() {
|
|
|
|
|
guard !account.isDeleted else { return }
|
|
|
|
|
|
2019-09-13 01:41:42 +02:00
|
|
|
|
let opmlDocumentString = opmlDocument()
|
2019-09-13 17:48:04 +02:00
|
|
|
|
|
|
|
|
|
let errorPointer: NSErrorPointer = nil
|
2019-09-13 22:46:22 +02:00
|
|
|
|
let fileCoordinator = NSFileCoordinator(filePresenter: managedFile)
|
2019-09-13 17:48:04 +02:00
|
|
|
|
|
2019-09-13 23:58:10 +02:00
|
|
|
|
fileCoordinator.coordinate(writingItemAt: fileURL, options: [], error: errorPointer, byAccessor: { writeURL in
|
2019-09-13 17:48:04 +02:00
|
|
|
|
do {
|
|
|
|
|
try opmlDocumentString.write(to: writeURL, atomically: true, encoding: .utf8)
|
|
|
|
|
} catch let error as NSError {
|
|
|
|
|
os_log(.error, log: log, "OPML save to disk failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if let error = errorPointer?.pointee {
|
|
|
|
|
os_log(.error, log: log, "OPML save to disk coordination failed: %@.", error.localizedDescription)
|
2019-09-13 01:41:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parsedOPMLItems() -> [RSOPMLItem]? {
|
2019-09-13 17:48:04 +02:00
|
|
|
|
|
|
|
|
|
var fileData: Data? = nil
|
|
|
|
|
let errorPointer: NSErrorPointer = nil
|
2019-09-13 22:46:22 +02:00
|
|
|
|
let fileCoordinator = NSFileCoordinator(filePresenter: managedFile)
|
2019-09-13 01:41:42 +02:00
|
|
|
|
|
2019-09-13 17:48:04 +02:00
|
|
|
|
fileCoordinator.coordinate(readingItemAt: fileURL, options: [], error: errorPointer, byAccessor: { readURL in
|
|
|
|
|
do {
|
|
|
|
|
fileData = try Data(contentsOf: readURL)
|
|
|
|
|
} 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)
|
|
|
|
|
os_log(.error, log: log, "OPML read from disk failed: %@.", error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if let error = errorPointer?.pointee {
|
|
|
|
|
os_log(.error, log: log, "OPML read from disk coordination failed: %@.", error.localizedDescription)
|
2019-09-13 01:05:29 +02:00
|
|
|
|
}
|
2019-09-13 17:48:04 +02:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 17:48:04 +02:00
|
|
|
|
let parserData = ParserData(url: fileURL.absoluteString, data: opmlData)
|
2019-09-13 01:05:29 +02:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|