NetNewsWire/Mac/ShareExtension/ShareViewController.swift

158 lines
4.4 KiB
Swift
Raw Normal View History

2020-08-14 01:03:39 +02:00
//
// ShareViewController.swift
// ShareExtension
//
// Created by Maurice Parker on 8/13/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import Cocoa
2020-08-14 17:25:55 +02:00
import os.log
2024-02-25 04:17:58 +01:00
import UniformTypeIdentifiers
2020-08-14 01:03:39 +02:00
class ShareViewController: NSViewController {
2020-08-14 17:25:55 +02:00
@IBOutlet weak var nameTextField: NSTextField!
@IBOutlet weak var folderPopUpButton: NSPopUpButton!
2020-08-14 01:03:39 +02:00
private var url: URL?
private var extensionContainers: ExtensionContainers?
2020-08-14 17:25:55 +02:00
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "ShareViewController")
2020-08-14 01:03:39 +02:00
override var nibName: NSNib.Name? {
return NSNib.Name("ShareViewController")
}
override func loadView() {
super.loadView()
extensionContainers = ExtensionContainersFile.read()
2020-08-14 17:25:55 +02:00
buildFolderPopupMenu()
var provider: NSItemProvider? = nil
// Try to get any HTML that is maybe passed in
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
for itemProvider in item.attachments! {
2024-02-25 04:17:58 +01:00
if itemProvider.hasItemConformingToTypeIdentifier(UTType.propertyList.identifier) {
2020-08-14 17:25:55 +02:00
provider = itemProvider
}
}
2020-08-14 01:03:39 +02:00
}
2020-08-14 17:25:55 +02:00
if provider != nil {
2024-02-25 04:17:58 +01:00
provider!.loadItem(forTypeIdentifier: UTType.propertyList.identifier, options: nil, completionHandler: { [weak self] (pList, error) in
2020-08-14 17:25:55 +02:00
if error != nil {
return
}
guard let dataGraph = pList as? NSDictionary else {
return
}
guard let results = dataGraph["NSExtensionJavaScriptPreprocessingResultsKey"] as? NSDictionary else {
return
}
if let url = URL(string: results["url"] as! String) {
self?.url = url
}
})
return
}
2020-08-14 01:03:39 +02:00
2020-08-14 17:25:55 +02:00
// Try to get the URL if it is passed in as a URL
2020-08-14 01:03:39 +02:00
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
for itemProvider in item.attachments! {
2024-02-25 04:43:10 +01:00
if itemProvider.hasItemConformingToTypeIdentifier(UTType.url.identifier) {
2020-08-14 01:03:39 +02:00
provider = itemProvider
}
}
}
if provider != nil {
2024-02-25 04:43:10 +01:00
provider!.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil, completionHandler: { [weak self] (urlCoded, error) in
2020-08-14 01:03:39 +02:00
if error != nil {
return
}
2020-08-14 17:25:55 +02:00
if let url = urlCoded as? URL {
self?.url = url
2020-08-14 01:03:39 +02:00
return
}
2020-08-14 17:25:55 +02:00
if let urlData = urlCoded as? Data {
self?.url = URL(dataRepresentation: urlData, relativeTo: nil)
}
2020-08-14 01:03:39 +02:00
})
}
}
@IBAction func send(_ sender: AnyObject?) {
2020-08-14 17:25:55 +02:00
guard let url = url, let selectedContainer = selectedContainer(), let containerID = selectedContainer.containerID else {
2020-08-14 03:09:42 +02:00
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
return
}
2020-08-14 17:25:55 +02:00
let name = nameTextField.stringValue.isEmpty ? nil : nameTextField.stringValue
let request = ExtensionFeedAddRequest(name: name, feedURL: url, destinationContainerID: containerID)
ExtensionFeedAddRequestFile.save(request)
2020-08-14 03:09:42 +02:00
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
2020-08-14 01:03:39 +02:00
}
@IBAction func cancel(_ sender: AnyObject?) {
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
self.extensionContext!.cancelRequest(withError: cancelError)
}
}
2020-08-14 17:25:55 +02:00
private extension ShareViewController {
func buildFolderPopupMenu() {
let menu = NSMenu(title: "Folders")
menu.autoenablesItems = false
guard let extensionContainers = extensionContainers else {
folderPopUpButton.menu = nil
return
}
let defaultContainer = ShareDefaultContainer.defaultContainer(containers: extensionContainers)
var defaultMenuItem: NSMenuItem? = nil
for account in extensionContainers.accounts {
let menuItem = NSMenuItem(title: account.name, action: nil, keyEquivalent: "")
menuItem.representedObject = account
if account.disallowFeedInRootFolder {
menuItem.isEnabled = false
}
menu.addItem(menuItem)
if defaultContainer?.containerID == account.containerID {
defaultMenuItem = menuItem
}
for folder in account.folders {
let menuItem = NSMenuItem(title: folder.name, action: nil, keyEquivalent: "")
menuItem.indentationLevel = 1
menuItem.representedObject = folder
menu.addItem(menuItem)
if defaultContainer?.containerID == folder.containerID {
defaultMenuItem = menuItem
}
}
}
folderPopUpButton.menu = menu
folderPopUpButton.select(defaultMenuItem)
}
func selectedContainer() -> ExtensionContainer? {
return folderPopUpButton.selectedItem?.representedObject as? ExtensionContainer
}
}