Write Subscriptions.opml to disk.

This commit is contained in:
Brent Simmons 2018-09-14 13:25:38 -07:00
parent b8546d8e8b
commit 3f1f4fd24c
1 changed files with 41 additions and 1 deletions

View File

@ -63,6 +63,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
private let feedsPath: ODBPath private let feedsPath: ODBPath
private let feedsTable: ODBTable private let feedsTable: ODBTable
private let opmlFilePath: String
private struct SettingsKey { private struct SettingsKey {
static let unreadCount = "unreadCount" static let unreadCount = "unreadCount"
} }
@ -115,6 +117,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
self.settingsFile = settingsFile self.settingsFile = settingsFile
self.dataFolder = dataFolder self.dataFolder = dataFolder
self.opmlFilePath = (dataFolder as NSString).appendingPathComponent("Subscriptions.opml")
let databaseFilePath = (dataFolder as NSString).appendingPathComponent("DB.sqlite3") let databaseFilePath = (dataFolder as NSString).appendingPathComponent("DB.sqlite3")
self.database = ArticlesDatabase(databaseFilePath: databaseFilePath, accountID: accountID) self.database = ArticlesDatabase(databaseFilePath: databaseFilePath, accountID: accountID)
@ -417,6 +421,32 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
flattenedFeeds().forEach { $0.unreadCount = 0 } flattenedFeeds().forEach { $0.unreadCount = 0 }
} }
public func opmlDocument() -> String {
let escapedTitle = 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 = OPMLString(indentLevel: 0)
let closingText =
"""
</body>
</opml>
"""
let opml = openingText + middleText + closingText
return opml
}
// MARK: - Debug // MARK: - Debug
public func debugDropConditionalGetInfo() { public func debugDropConditionalGetInfo() {
@ -585,6 +615,8 @@ private extension Account {
func saveToDisk() { func saveToDisk() {
dirty = false
let d = diskDictionary() let d = diskDictionary()
do { do {
try RSPlist.write(d, filePath: settingsFile) try RSPlist.write(d, filePath: settingsFile)
@ -592,7 +624,15 @@ private extension Account {
catch let error as NSError { catch let error as NSError {
NSApplication.shared.presentError(error) NSApplication.shared.presentError(error)
} }
dirty = false
let opmlDocumentString = opmlDocument()
do {
let url = URL(fileURLWithPath: opmlFilePath)
try opmlDocumentString.write(to: url, atomically: true, encoding: .utf8)
}
catch let error as NSError {
NSApplication.shared.presentError(error)
}
} }
} }