adds mac theme downloading

This commit is contained in:
Stuart Breckenridge
2021-09-19 21:18:23 +08:00
parent 4fab4ffa7c
commit 7994b34551
4 changed files with 70 additions and 1 deletions

View File

@@ -18,6 +18,7 @@
import Foundation
import Articles
import Zip
protocol AppDelegateAppleEvents {
func installAppleEventHandlers()
@@ -44,6 +45,63 @@ extension AppDelegate : AppDelegateAppleEvents {
return
}
// Handle themes
if urlString.hasPrefix("netnewswire://theme") {
guard let comps = URLComponents(string: urlString),
let queryItems = comps.queryItems,
let themeURLString = queryItems.first(where: { $0.name == "url" })?.value else {
return
}
if let themeURL = URL(string: themeURLString) {
let request = URLRequest(url: themeURL)
let task = URLSession.shared.downloadTask(with: request) { location, response, error in
var downloadDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
try? FileManager.default.createDirectory(at: downloadDirectory, withIntermediateDirectories: true, attributes: nil)
let tmpFileName = UUID().uuidString + ".zip"
downloadDirectory.appendPathComponent("\(tmpFileName)")
if location == nil {
return
}
do {
try FileManager.default.moveItem(at: location!, to: downloadDirectory)
var unzippedDir = downloadDirectory
unzippedDir = unzippedDir.deletingLastPathComponent()
unzippedDir.appendPathComponent("newtheme.nnwtheme")
try Zip.unzipFile(downloadDirectory, destination: unzippedDir, overwrite: true, password: nil, progress: nil, fileOutputHandler: nil)
try FileManager.default.removeItem(at: downloadDirectory)
let decoder = PropertyListDecoder()
let plistURL = URL(fileURLWithPath: unzippedDir.appendingPathComponent("Info.plist").path)
let data = try Data(contentsOf: plistURL)
let plist = try decoder.decode(ArticleThemePlist.self, from: data)
// rename
var renamedUnzippedDir = unzippedDir.deletingLastPathComponent()
renamedUnzippedDir.appendPathComponent(plist.name + ".nnwtheme")
if FileManager.default.fileExists(atPath: renamedUnzippedDir.path) {
try FileManager.default.removeItem(at: renamedUnzippedDir)
}
try FileManager.default.moveItem(at: unzippedDir, to: renamedUnzippedDir)
DispatchQueue.main.async {
self.importTheme(filename: renamedUnzippedDir.path)
}
} catch {
print(error)
}
}
task.resume()
}
return
}
// Special case URL with specific scheme handler x-netnewswire-feed: intended to ensure we open
// it regardless of which news reader may be set as the default
let nnwScheme = "x-netnewswire-feed:"