2018-01-11 09:51:25 +01:00
|
|
|
//
|
|
|
|
// Account+Scriptability.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2018-01-11 09:51:25 +01:00
|
|
|
//
|
|
|
|
// Created by Olof Hellman on 1/9/18.
|
|
|
|
// Copyright © 2018 Olof Hellman. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-02-03 07:51:32 +01:00
|
|
|
import AppKit
|
2018-01-11 09:51:25 +01:00
|
|
|
import Account
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2018-03-05 03:43:29 +01:00
|
|
|
import RSCore
|
2018-01-11 09:51:25 +01:00
|
|
|
|
|
|
|
@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)
|
|
|
|
}
|
2019-07-30 04:56:22 +02:00
|
|
|
|
|
|
|
@objc(scriptingIsActive)
|
|
|
|
var scriptingIsActive: Bool {
|
|
|
|
get {
|
|
|
|
return account.isActive
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
account.isActive = newValue
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 21:44:24 +02:00
|
|
|
|
|
|
|
@objc(scriptingName)
|
|
|
|
var scriptingName: NSString {
|
|
|
|
get {
|
|
|
|
return account.nameForDisplay as NSString
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
account.name = newValue as String
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
// 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
|
2018-04-26 06:40:50 +02:00
|
|
|
@objc(uniqueId)
|
2018-01-11 09:51:25 +01:00
|
|
|
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 {
|
2019-05-30 03:53:00 +02:00
|
|
|
account.removeFolder(scriptableFolder.folder) { result in
|
2019-05-07 00:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:46:43 +01:00
|
|
|
} else if let scriptableFeed = element as? ScriptableWebFeed {
|
2019-05-07 00:34:41 +02:00
|
|
|
BatchUpdate.shared.perform {
|
2019-05-30 00:56:26 +02:00
|
|
|
var container: Container? = nil
|
|
|
|
if let scriptableFolder = scriptableFeed.container as? ScriptableFolder {
|
|
|
|
container = scriptableFolder.folder
|
2019-07-17 22:41:21 +02:00
|
|
|
} else {
|
|
|
|
container = account
|
2019-05-30 00:56:26 +02:00
|
|
|
}
|
2019-11-15 03:11:41 +01:00
|
|
|
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-03-05 03:43:29 +01:00
|
|
|
|
2018-02-20 09:26:46 +01:00
|
|
|
@objc(isLocationRequiredToCreateForKey:)
|
|
|
|
func isLocationRequiredToCreate(forKey key:String) -> Bool {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-11 09:51:25 +01:00
|
|
|
|
|
|
|
// MARK: --- Scriptable elements ---
|
|
|
|
|
2019-11-15 22:46:43 +01:00
|
|
|
@objc(webFeeds)
|
|
|
|
var webFeeds:NSArray {
|
|
|
|
return account.topLevelWebFeeds.map { ScriptableWebFeed($0, container:self) } as NSArray
|
2018-01-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 22:46:43 +01:00
|
|
|
@objc(valueInWebFeedsWithUniqueID:)
|
|
|
|
func valueInWebFeeds(withUniqueID id:String) -> ScriptableWebFeed? {
|
2020-06-08 03:13:51 +02:00
|
|
|
guard let feed = account.existingWebFeed(withWebFeedID: id) else { return nil }
|
2019-11-15 22:46:43 +01:00
|
|
|
return ScriptableWebFeed(feed, container:self)
|
2018-02-08 09:11:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 22:46:43 +01:00
|
|
|
@objc(valueInWebFeedsWithName:)
|
|
|
|
func valueInWebFeeds(withName name:String) -> ScriptableWebFeed? {
|
2020-06-08 03:13:51 +02:00
|
|
|
let feeds = Array(account.flattenedWebFeeds())
|
2018-03-05 03:43:29 +01:00
|
|
|
guard let feed = feeds.first(where:{$0.name == name}) else { return nil }
|
2019-11-15 22:46:43 +01:00
|
|
|
return ScriptableWebFeed(feed, container:self)
|
2018-03-05 03:43:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
@objc(folders)
|
|
|
|
var folders:NSArray {
|
2018-09-17 02:54:42 +02:00
|
|
|
let foldersSet = account.folders ?? Set<Folder>()
|
|
|
|
let folders = Array(foldersSet)
|
|
|
|
return folders.map { ScriptableFolder($0, container:self) } as NSArray
|
2018-01-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:11:52 +01:00
|
|
|
@objc(valueInFoldersWithUniqueID:)
|
|
|
|
func valueInFolders(withUniqueID id:NSNumber) -> ScriptableFolder? {
|
|
|
|
let folderId = id.intValue
|
2018-09-17 02:54:42 +02:00
|
|
|
let foldersSet = account.folders ?? Set<Folder>()
|
|
|
|
let folders = Array(foldersSet)
|
2018-02-08 09:11:52 +01:00
|
|
|
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
|
|
|
}
|
2018-02-08 09:11:52 +01:00
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
// MARK: --- Scriptable properties ---
|
|
|
|
|
2019-11-15 22:46:43 +01:00
|
|
|
@objc(allWebFeeds)
|
|
|
|
var allWebFeeds: NSArray {
|
|
|
|
var webFeeds = [ScriptableWebFeed]()
|
|
|
|
for webFeed in account.topLevelWebFeeds {
|
|
|
|
webFeeds.append(ScriptableWebFeed(webFeed, container: self))
|
2018-09-17 02:54:42 +02:00
|
|
|
}
|
|
|
|
if let folders = account.folders {
|
|
|
|
for folder in folders {
|
2019-08-02 07:51:03 +02:00
|
|
|
let scriptableFolder = ScriptableFolder(folder, container: self)
|
2019-11-15 22:46:43 +01:00
|
|
|
for webFeed in folder.topLevelWebFeeds {
|
|
|
|
webFeeds.append(ScriptableWebFeed(webFeed, container: scriptableFolder))
|
2019-08-02 07:51:03 +02:00
|
|
|
}
|
2018-09-17 02:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:46:43 +01:00
|
|
|
return webFeeds as NSArray
|
2018-01-11 09:51:25 +01:00
|
|
|
}
|
2018-01-20 07:49:01 +01:00
|
|
|
|
|
|
|
@objc(opmlRepresentation)
|
|
|
|
var opmlRepresentation:String {
|
2020-01-14 05:20:20 +01:00
|
|
|
return self.account.OPMLString(indentLevel:0)
|
2018-01-20 07:49:01 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
@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"
|
2018-01-11 09:51:25 +01:00
|
|
|
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"
|
2018-01-11 09:51:25 +01:00
|
|
|
}
|
2020-01-13 01:38:04 +01:00
|
|
|
return osType.fourCharCode
|
2018-01-11 09:51:25 +01:00
|
|
|
}
|
|
|
|
}
|