2017-11-08 06:14:58 +01:00
|
|
|
//
|
2018-09-20 22:29:01 +02:00
|
|
|
// PasteboardFeed.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-11-08 06:14:58 +01:00
|
|
|
//
|
2018-09-20 22:29:01 +02:00
|
|
|
// Created by Brent Simmons on 9/20/18.
|
|
|
|
// Copyright © 2018 Ranchero Software. All rights reserved.
|
2017-11-08 06:14:58 +01:00
|
|
|
//
|
|
|
|
|
2018-02-03 07:51:32 +01:00
|
|
|
import AppKit
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2018-07-28 21:16:14 +02:00
|
|
|
import Account
|
2018-02-12 07:10:28 +01:00
|
|
|
import RSCore
|
|
|
|
|
2018-09-20 22:29:01 +02:00
|
|
|
typealias PasteboardFeedDictionary = [String: String]
|
|
|
|
|
|
|
|
struct PasteboardFeed: Hashable {
|
|
|
|
|
|
|
|
private struct Key {
|
|
|
|
static let url = "URL"
|
|
|
|
static let homePageURL = "homePageURL"
|
|
|
|
static let name = "name"
|
|
|
|
|
|
|
|
// Internal
|
|
|
|
static let accountID = "accountID"
|
|
|
|
static let feedID = "feedID"
|
|
|
|
static let editedName = "editedName"
|
|
|
|
}
|
|
|
|
|
|
|
|
let url: String
|
|
|
|
let feedID: String?
|
|
|
|
let homePageURL: String?
|
|
|
|
let name: String?
|
|
|
|
let editedName: String?
|
|
|
|
let accountID: String?
|
2018-09-22 20:54:02 +02:00
|
|
|
let isLocalFeed: Bool
|
2018-09-20 22:29:01 +02:00
|
|
|
|
|
|
|
init(url: String, feedID: String?, homePageURL: String?, name: String?, editedName: String?, accountID: String?) {
|
2018-09-22 06:37:20 +02:00
|
|
|
self.url = url.rs_normalizedURL()
|
2018-09-20 22:29:01 +02:00
|
|
|
self.feedID = feedID
|
2018-09-22 06:37:20 +02:00
|
|
|
self.homePageURL = homePageURL?.rs_normalizedURL()
|
2018-09-20 22:29:01 +02:00
|
|
|
self.name = name
|
|
|
|
self.editedName = editedName
|
|
|
|
self.accountID = accountID
|
2018-09-22 20:54:02 +02:00
|
|
|
self.isLocalFeed = accountID != nil
|
2018-09-20 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Reading
|
|
|
|
|
|
|
|
init?(dictionary: PasteboardFeedDictionary) {
|
|
|
|
guard let url = dictionary[Key.url] else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let homePageURL = dictionary[Key.homePageURL]
|
|
|
|
let name = dictionary[Key.name]
|
|
|
|
let accountID = dictionary[Key.accountID]
|
|
|
|
let feedID = dictionary[Key.feedID]
|
|
|
|
let editedName = dictionary[Key.editedName]
|
|
|
|
|
|
|
|
self.init(url: url, feedID: feedID, homePageURL: homePageURL, name: name, editedName: editedName, accountID: accountID)
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(pasteboardItem: NSPasteboardItem) {
|
|
|
|
var pasteboardType: NSPasteboard.PasteboardType?
|
|
|
|
if pasteboardItem.types.contains(FeedPasteboardWriter.feedUTIInternalType) {
|
|
|
|
pasteboardType = FeedPasteboardWriter.feedUTIInternalType
|
|
|
|
}
|
|
|
|
else if pasteboardItem.types.contains(FeedPasteboardWriter.feedUTIType) {
|
|
|
|
pasteboardType = FeedPasteboardWriter.feedUTIType
|
|
|
|
}
|
2018-09-22 06:37:20 +02:00
|
|
|
if let foundType = pasteboardType {
|
|
|
|
if let feedDictionary = pasteboardItem.propertyList(forType: foundType) as? PasteboardFeedDictionary {
|
|
|
|
self.init(dictionary: feedDictionary)
|
|
|
|
return
|
|
|
|
}
|
2018-09-20 22:29:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-22 06:37:20 +02:00
|
|
|
// Check for URL or a string that may be a URL.
|
|
|
|
if pasteboardItem.types.contains(.URL) {
|
|
|
|
pasteboardType = .URL
|
|
|
|
}
|
|
|
|
else if pasteboardItem.types.contains(.string) {
|
|
|
|
pasteboardType = .string
|
|
|
|
}
|
|
|
|
if let foundType = pasteboardType {
|
|
|
|
if let possibleURLString = pasteboardItem.string(forType: foundType) {
|
|
|
|
if possibleURLString.rs_stringMayBeURL() {
|
|
|
|
self.init(url: possibleURLString, feedID: nil, homePageURL: nil, name: nil, editedName: nil, accountID: nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2018-09-22 06:37:20 +02:00
|
|
|
return nil
|
2018-09-20 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func pasteboardFeeds(with pasteboard: NSPasteboard) -> Set<PasteboardFeed>? {
|
|
|
|
guard let items = pasteboard.pasteboardItems else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let feeds = items.compactMap { PasteboardFeed(pasteboardItem: $0) }
|
|
|
|
return feeds.isEmpty ? nil : Set(feeds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Writing
|
|
|
|
|
|
|
|
func exportDictionary() -> PasteboardFeedDictionary {
|
|
|
|
var d = PasteboardFeedDictionary()
|
|
|
|
d[Key.url] = url
|
|
|
|
d[Key.homePageURL] = homePageURL ?? ""
|
|
|
|
if let nameForDisplay = editedName ?? name {
|
|
|
|
d[Key.name] = nameForDisplay
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func internalDictionary() -> PasteboardFeedDictionary {
|
|
|
|
var d = PasteboardFeedDictionary()
|
|
|
|
d[PasteboardFeed.Key.feedID] = feedID
|
|
|
|
d[PasteboardFeed.Key.url] = url
|
|
|
|
if let homePageURL = homePageURL {
|
|
|
|
d[PasteboardFeed.Key.homePageURL] = homePageURL
|
|
|
|
}
|
|
|
|
if let name = name {
|
|
|
|
d[PasteboardFeed.Key.name] = name
|
|
|
|
}
|
|
|
|
if let editedName = editedName {
|
|
|
|
d[PasteboardFeed.Key.editedName] = editedName
|
|
|
|
}
|
|
|
|
if let accountID = accountID {
|
|
|
|
d[PasteboardFeed.Key.accountID] = accountID
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 07:10:28 +01:00
|
|
|
extension Feed: PasteboardWriterOwner {
|
|
|
|
|
|
|
|
public var pasteboardWriter: NSPasteboardWriting {
|
|
|
|
return FeedPasteboardWriter(feed: self)
|
|
|
|
}
|
|
|
|
}
|
2017-11-08 06:14:58 +01:00
|
|
|
|
|
|
|
@objc final class FeedPasteboardWriter: NSObject, NSPasteboardWriting {
|
|
|
|
|
|
|
|
private let feed: Feed
|
|
|
|
static let feedUTI = "com.ranchero.feed"
|
|
|
|
static let feedUTIType = NSPasteboard.PasteboardType(rawValue: feedUTI)
|
2018-08-29 07:18:24 +02:00
|
|
|
static let feedUTIInternal = "com.ranchero.NetNewsWire-Evergreen.internal.feed"
|
2017-11-08 06:14:58 +01:00
|
|
|
static let feedUTIInternalType = NSPasteboard.PasteboardType(rawValue: feedUTIInternal)
|
|
|
|
|
2018-09-20 06:49:13 +02:00
|
|
|
|
2017-11-08 06:14:58 +01:00
|
|
|
init(feed: Feed) {
|
|
|
|
self.feed = feed
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSPasteboardWriting
|
|
|
|
|
|
|
|
func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
|
|
|
|
|
2018-02-04 22:45:26 +01:00
|
|
|
return [FeedPasteboardWriter.feedUTIType, .URL, .string, FeedPasteboardWriter.feedUTIInternalType]
|
2017-11-08 06:14:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? {
|
|
|
|
|
|
|
|
let plist: Any?
|
|
|
|
|
|
|
|
switch type {
|
|
|
|
case .string:
|
|
|
|
plist = feed.nameForDisplay
|
|
|
|
case .URL:
|
|
|
|
plist = feed.url
|
|
|
|
case FeedPasteboardWriter.feedUTIType:
|
2019-02-06 06:00:10 +01:00
|
|
|
plist = exportDictionary
|
2017-11-08 06:14:58 +01:00
|
|
|
case FeedPasteboardWriter.feedUTIInternalType:
|
2019-02-06 06:00:10 +01:00
|
|
|
plist = internalDictionary
|
2017-11-08 06:14:58 +01:00
|
|
|
default:
|
|
|
|
plist = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return plist
|
|
|
|
}
|
2018-09-20 06:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension FeedPasteboardWriter {
|
2017-11-08 06:14:58 +01:00
|
|
|
|
2019-02-06 06:00:10 +01:00
|
|
|
var pasteboardFeed: PasteboardFeed {
|
|
|
|
return PasteboardFeed(url: feed.url, feedID: feed.feedID, homePageURL: feed.homePageURL, name: feed.name, editedName: feed.editedName, accountID: feed.account?.accountID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var exportDictionary: PasteboardFeedDictionary {
|
2018-09-20 22:29:01 +02:00
|
|
|
return pasteboardFeed.exportDictionary()
|
2017-11-08 06:14:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 06:00:10 +01:00
|
|
|
var internalDictionary: PasteboardFeedDictionary {
|
2018-09-20 22:29:01 +02:00
|
|
|
return pasteboardFeed.internalDictionary()
|
2018-09-20 06:49:13 +02:00
|
|
|
}
|
|
|
|
}
|