2018-01-11 09:51:25 +01:00
|
|
|
//
|
|
|
|
// Account+Scriptability.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
import Data
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-03-05 03:43:29 +01:00
|
|
|
func deleteElement(_ element:ScriptingObject) {
|
|
|
|
if let scriptableFolder = element as? ScriptableFolder {
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
account.deleteFolder(scriptableFolder.folder)
|
|
|
|
}
|
|
|
|
} else if let scriptableFeed = element as? ScriptableFeed {
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
account.deleteFeed(scriptableFeed.feed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ---
|
|
|
|
|
|
|
|
@objc(feeds)
|
|
|
|
var feeds:NSArray {
|
2018-01-28 03:50:48 +01:00
|
|
|
let feeds = account.children.compactMap { $0 as? Feed }
|
2018-01-11 09:51:25 +01:00
|
|
|
return feeds.map { ScriptableFeed($0, container:self) } as NSArray
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:11:52 +01:00
|
|
|
@objc(valueInFeedsWithUniqueID:)
|
|
|
|
func valueInFeeds(withUniqueID id:String) -> ScriptableFeed? {
|
|
|
|
let feeds = account.children.compactMap { $0 as? Feed }
|
|
|
|
guard let feed = feeds.first(where:{$0.feedID == id}) else { return nil }
|
|
|
|
return ScriptableFeed(feed, container:self)
|
|
|
|
}
|
|
|
|
|
2018-03-05 03:43:29 +01:00
|
|
|
@objc(valueInFeedsWithName:)
|
|
|
|
func valueInFeeds(withName name:String) -> ScriptableFeed? {
|
|
|
|
let feeds = account.children.compactMap { $0 as? Feed }
|
|
|
|
guard let feed = feeds.first(where:{$0.name == name}) else { return nil }
|
|
|
|
return ScriptableFeed(feed, container:self)
|
|
|
|
}
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
@objc(folders)
|
|
|
|
var folders:NSArray {
|
2018-01-28 03:50:48 +01:00
|
|
|
let folders = account.children.compactMap { $0 as? Folder }
|
2018-01-11 09:51:25 +01:00
|
|
|
return folders.map { ScriptableFolder($0, container:self) } as NSArray
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:11:52 +01:00
|
|
|
@objc(valueInFoldersWithUniqueID:)
|
|
|
|
func valueInFolders(withUniqueID id:NSNumber) -> ScriptableFolder? {
|
|
|
|
let folderId = id.intValue
|
|
|
|
let folders = account.children.compactMap { $0 as? Folder }
|
|
|
|
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 ---
|
|
|
|
|
|
|
|
@objc(contents)
|
|
|
|
var contents:NSArray {
|
|
|
|
var contentsArray:[AnyObject] = []
|
|
|
|
for child in account.children {
|
|
|
|
if let aFeed = child as? Feed {
|
|
|
|
contentsArray.append(ScriptableFeed(aFeed, container:self))
|
|
|
|
} else if let aFolder = child as? Folder {
|
|
|
|
contentsArray.append(ScriptableFolder(aFolder, container:self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contentsArray as NSArray
|
|
|
|
}
|
2018-01-20 07:49:01 +01:00
|
|
|
|
|
|
|
@objc(opmlRepresentation)
|
|
|
|
var opmlRepresentation:String {
|
|
|
|
return self.account.OPMLString(indentLevel:0)
|
|
|
|
}
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
@objc(accountType)
|
|
|
|
var accountType:OSType {
|
|
|
|
var osType:String = ""
|
|
|
|
switch self.account.type {
|
|
|
|
case .onMyMac:
|
|
|
|
osType = "Locl"
|
|
|
|
case .feedly:
|
|
|
|
osType = "Fdly"
|
|
|
|
case .feedbin:
|
|
|
|
osType = "Fdbn"
|
|
|
|
case .feedWrangler:
|
|
|
|
osType = "FWrg"
|
|
|
|
case .newsBlur:
|
|
|
|
osType = "NBlr"
|
|
|
|
}
|
|
|
|
return osType.FourCharCode()
|
|
|
|
}
|
|
|
|
}
|