NetNewsWire/Mac/Scriptability/Account+Scriptability.swift

177 lines
4.9 KiB
Swift
Raw Normal View History

//
// Account+Scriptability.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
//
// Created by Olof Hellman on 1/9/18.
// Copyright © 2018 Olof Hellman. All rights reserved.
//
import AppKit
import Account
import Articles
import RSCore
@objc(ScriptableAccount)
class ScriptableAccount: NSObject, UniqueIdScriptingObject, ScriptingObjectContainer {
let account:Account
init (_ account:Account) {
self.account = account
}
@objc(objectSpecifier)
override var objectSpecifier: NSScriptObjectSpecifier? {
let myContainer = NSApplication.shared
let scriptObjectSpecifier = myContainer.makeFormUniqueIDScriptObjectSpecifier(forObject:self)
return (scriptObjectSpecifier)
}
@objc(scriptingIsActive)
var scriptingIsActive: Bool {
get {
return account.isActive
}
set {
account.isActive = newValue
}
}
@objc(scriptingName)
var scriptingName: NSString {
get {
return account.nameForDisplay as NSString
}
set {
account.name = newValue as String
}
}
// MARK: --- ScriptingObject protocol ---
var scriptingKey: String {
return "accounts"
}
// MARK: --- UniqueIdScriptingObject protocol ---
// I am not sure if account should prefer to be specified by name or by ID
// but in either case it seems like the accountID would be used as the keydata, so I chose ID
@objc(uniqueId)
var scriptingUniqueId:Any {
return account.accountID
}
// MARK: --- ScriptingObjectContainer protocol ---
var scriptingClassDescription: NSScriptClassDescription {
return self.classDescription as! NSScriptClassDescription
}
2018-02-20 09:26:46 +01:00
2019-05-07 00:34:41 +02:00
func deleteElement(_ element:ScriptingObject) {
if let scriptableFolder = element as? ScriptableFolder {
BatchUpdate.shared.perform {
account.removeFolder(scriptableFolder.folder) { result in
2019-05-07 00:34:41 +02:00
}
}
} else if let scriptableFeed = element as? ScriptableWebFeed {
2019-05-07 00:34:41 +02:00
BatchUpdate.shared.perform {
var container: Container? = nil
if let scriptableFolder = scriptableFeed.container as? ScriptableFolder {
container = scriptableFolder.folder
} else {
container = account
}
account.removeWebFeed(scriptableFeed.webFeed, from: container!) { result in
2019-05-09 14:25:45 +02:00
}
2019-05-07 00:34:41 +02:00
}
}
}
2018-02-20 09:26:46 +01:00
@objc(isLocationRequiredToCreateForKey:)
func isLocationRequiredToCreate(forKey key:String) -> Bool {
return false;
}
// MARK: --- Scriptable elements ---
@objc(webFeeds)
var webFeeds:NSArray {
return account.topLevelWebFeeds.map { ScriptableWebFeed($0, container:self) } as NSArray
}
@objc(valueInWebFeedsWithUniqueID:)
func valueInWebFeeds(withUniqueID id:String) -> ScriptableWebFeed? {
guard let feed = account.existingWebFeed(withWebFeedID: id) else { return nil }
return ScriptableWebFeed(feed, container:self)
}
@objc(valueInWebFeedsWithName:)
func valueInWebFeeds(withName name:String) -> ScriptableWebFeed? {
let feeds = Array(account.flattenedWebFeeds())
guard let feed = feeds.first(where:{$0.name == name}) else { return nil }
return ScriptableWebFeed(feed, container:self)
}
@objc(folders)
var folders:NSArray {
let foldersSet = account.folders ?? Set<Folder>()
let folders = Array(foldersSet)
return folders.map { ScriptableFolder($0, container:self) } as NSArray
}
@objc(valueInFoldersWithUniqueID:)
func valueInFolders(withUniqueID id:NSNumber) -> ScriptableFolder? {
let folderId = id.intValue
let foldersSet = account.folders ?? Set<Folder>()
let folders = Array(foldersSet)
guard let folder = folders.first(where:{$0.folderID == folderId}) else { return nil }
return ScriptableFolder(folder, container:self)
2018-02-20 09:26:46 +01:00
}
// MARK: --- Scriptable properties ---
@objc(allWebFeeds)
var allWebFeeds: NSArray {
var webFeeds = [ScriptableWebFeed]()
for webFeed in account.topLevelWebFeeds {
webFeeds.append(ScriptableWebFeed(webFeed, container: self))
}
if let folders = account.folders {
for folder in folders {
let scriptableFolder = ScriptableFolder(folder, container: self)
for webFeed in folder.topLevelWebFeeds {
webFeeds.append(ScriptableWebFeed(webFeed, container: scriptableFolder))
}
}
}
return webFeeds as NSArray
}
@objc(opmlRepresentation)
var opmlRepresentation:String {
return self.account.OPMLString(indentLevel:0)
}
@objc(accountType)
var accountType:OSType {
var osType:String = ""
switch self.account.type {
case .onMyMac:
osType = "Locl"
2020-03-18 21:48:44 +01:00
case .cloudKit:
osType = "Clkt"
case .feedly:
osType = "Fdly"
case .feedbin:
osType = "Fdbn"
case .feedWrangler:
osType = "FWrg"
case .newsBlur:
osType = "NBlr"
2019-06-20 14:22:51 +02:00
case .freshRSS:
osType = "Frsh"
}
2020-01-13 01:38:04 +01:00
return osType.fourCharCode
}
}