From 3f1f4fd24c54bfc7ed272bfc7a3ac6d307ae6cfa Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 14 Sep 2018 13:25:38 -0700 Subject: [PATCH] Write Subscriptions.opml to disk. --- Frameworks/Account/Account.swift | 42 +++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index c8c826ef4..79c28212a 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -63,6 +63,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, private let feedsPath: ODBPath private let feedsTable: ODBTable + private let opmlFilePath: String + private struct SettingsKey { static let unreadCount = "unreadCount" } @@ -115,6 +117,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, self.settingsFile = settingsFile self.dataFolder = dataFolder + self.opmlFilePath = (dataFolder as NSString).appendingPathComponent("Subscriptions.opml") + let databaseFilePath = (dataFolder as NSString).appendingPathComponent("DB.sqlite3") self.database = ArticlesDatabase(databaseFilePath: databaseFilePath, accountID: accountID) @@ -417,6 +421,32 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, flattenedFeeds().forEach { $0.unreadCount = 0 } } + public func opmlDocument() -> String { + let escapedTitle = nameForDisplay.rs_stringByEscapingSpecialXMLCharacters() + let openingText = + """ + + + + + \(escapedTitle) + + + + """ + + let middleText = OPMLString(indentLevel: 0) + + let closingText = + """ + + + """ + + let opml = openingText + middleText + closingText + return opml + } + // MARK: - Debug public func debugDropConditionalGetInfo() { @@ -585,6 +615,8 @@ private extension Account { func saveToDisk() { + dirty = false + let d = diskDictionary() do { try RSPlist.write(d, filePath: settingsFile) @@ -592,7 +624,15 @@ private extension Account { catch let error as NSError { 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) + } } }