Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Release
Browse files Browse the repository at this point in the history
  • Loading branch information
hrbrmstr committed May 25, 2020
1 parent 1deb333 commit 41e68c4
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 174 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# 1.7.0

- Added checks for network availability
- Added command-line switcher
- Supports file uploads to and exports from RStudio Server
- Added more web resource URLs
- Added Preference to always check/force that RStudio owns .R and .Rmd files

# 1.4.0

Expand Down
Binary file not shown.
18 changes: 4 additions & 14 deletions RSwitch/Swift/Downloaders/DownloadFromRStudioServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,40 @@ import Foundation
import Cocoa

func download_from_studio_server(fromRS : String, toFS : String) {

NSLog("download from rstudio server")

let rsURL = URL(string: fromRS)!
let fsURL = URL(string: toFS)!

URLSession.shared.configuration.timeoutIntervalForRequest = 300.0

//URLSession.shared.downloadTask(with: <#T##URLRequest#>)


let task = URLSession.shared.downloadTask(with: rsURL) {
localURL, urlResponse, error in

if (error != nil) {

NSLog("dler \(String(describing: error))")
NSLog("Error exporting from RStudio Server; \(String(describing: error))")

} else {

if let localURL = localURL {

NSLog("We've got the data");

if (FileManager.default.fileExists(atPath: fsURL.path)) {

NSLog("Deleting old file")
do {
try FileManager.default.removeItem(at: fsURL)
} catch {
NSLog("error deleting old file")
NSLog("Error removing file during RStudio Server export; \(error)")
}

}

do {
NSLog("Trying to move the data from \(localURL) to \(fsURL)");
try FileManager.default.moveItem(at: localURL, to: fsURL)
NSWorkspace.shared.openFile(
fsURL.deletingLastPathComponent().absoluteString, withApplication: "Finder"
)
NSWorkspace.shared.openFile(fsURL.deletingLastPathComponent().absoluteString, withApplication: "Finder")
NSWorkspace.shared.activateFileViewerSelecting([fsURL])
} catch {
NSLog("Move Error \(error)")
NSLog("Error moving RStudio Server export file; \(error)")
}

}
Expand Down
3 changes: 0 additions & 3 deletions RSwitch/Swift/Notify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,10 @@ extension AppDelegate : NSUserNotificationCenterDelegate {
}

func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
print("should present")
return(true)
}

func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {
print("did activate")
print(notification)
}

}
34 changes: 6 additions & 28 deletions RSwitch/Swift/RStudioServerSessionWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ class RstudioServerSessionWebViewController: NSViewController, NSWindowDelegate
extension RstudioServerSessionWebViewController: WKUIDelegate {

func webView(_ webView: WKWebView, runOpenPanelWith parameters: WKOpenPanelParameters, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping ([URL]?) -> Void) {

NSLog("PANELING!")
print("PANELING!")

let openPanel = NSOpenPanel()

Expand All @@ -79,52 +76,33 @@ extension RstudioServerSessionWebViewController: WKUIDelegate {
let u = URL(string: navigationAction.request.url!.absoluteString)!

if ((u.urlComponents?.path.starts(with: "/export/")) != nil) {

NSLog("Navigation action thing: " + navigationAction.request.url!.absoluteString)


let exportWindowController = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "exportPopupPanel") as! ExportWebViewController

let exportWV = (exportWindowController.contentViewController as! exportPopupViewController)

exportWV.view.window?.title = navigationAction.request.url!.absoluteString

exportWV.setupWebView(configuration: configuration)
//exportWindowController.showWindow(self)

NSLog("Before exportWV.loadWebView")

// exportWV.loadWebView(urlIn: navigationAction.request.url!.absoluteString)

exportWV.loadWebView(urlIn: "")

NSLog("After exportWV.loadWebView")

let urlPath = navigationAction.request.url!.absoluteString

NSLog("loadWebView: \(urlPath)")


// Check for "/export/"
// If export, then get bring up a Save Panel and then download the file to that location

if let url = URL(string: urlPath) {

NSLog("URL path: \(url.path)")


if (url.path.starts(with: "/export")) {

NSLog(" Name: " + url.queryParameters["name"]!)


let savePanel = NSSavePanel()

savePanel.canCreateDirectories = true
savePanel.nameFieldStringValue = url.queryParameters["name"]!
savePanel.beginSheetModal(for:self.view.window!) { (response) in
if (response == NSApplication.ModalResponse.OK) {

if (response == NSApplication.ModalResponse.OK) {
download_from_studio_server(fromRS: url.absoluteString, toFS: savePanel.url!.absoluteString)

} else {

NSLog("Don't do anything!")
}
savePanel.close()
}
Expand Down
44 changes: 13 additions & 31 deletions RSwitch/Swift/Utils/Associations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,27 @@ import ApplicationServices

class FileAssociationUtils {

public static func getHandlers() {

let workspace = NSWorkspace.shared;

let setResR : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "R" as CFString, nil)?.takeRetainedValue() as String? ?? "";
NSLog("UTI of .R extension: " + setResR);

let handlerR : String = LSCopyDefaultRoleHandlerForContentType(setResR as CFString, LSRolesMask.all)?.takeRetainedValue() as String? ?? "";
NSLog("Bundle ID of handler for .R files is: [" + handlerR + "]");

let rAppUrl : URL = (workspace.urlForApplication(withBundleIdentifier: handlerR))!.appendingPathComponent("Contents/Info.plist");
NSLog("The Info.plist for the app that handles .R files is: " + rAppUrl.absoluteString);

let rAppDict : NSDictionary = NSDictionary(contentsOf: rAppUrl)!;
NSLog("The name of the app that handles .R files is: " + (rAppDict.object(forKey: "CFBundleName") as! String));

let setResRmd : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "Rmd" as CFString, nil)?.takeRetainedValue() as String? ?? "";
NSLog("UTI of .Rmd extension: " + setResRmd);

let handlerRmd : String = LSCopyDefaultRoleHandlerForContentType(setResRmd as CFString, LSRolesMask.all)?.takeRetainedValue() as String? ?? "";
NSLog("Bundle ID of handler for .Rmd files is: [" + handlerRmd + "]");

let rmdAppUrl : URL = (workspace.urlForApplication(withBundleIdentifier: handlerRmd))!.appendingPathComponent("Contents/Info.plist");
NSLog("The Info.plist for the app that handles .Rmd files is: " + rmdAppUrl.absoluteString);

let rmdAppDict : NSDictionary = NSDictionary(contentsOf: rmdAppUrl)!;
NSLog("The name of the app that handles .Rmd files is: " + (rmdAppDict.object(forKey: "CFBundleName") as! String));

}
// public static func getHandlers() {
//
// let workspace = NSWorkspace.shared;
//
// let setResR : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "R" as CFString, nil)?.takeRetainedValue() as String? ?? "";
// let handlerR : String = LSCopyDefaultRoleHandlerForContentType(setResR as CFString, LSRolesMask.all)?.takeRetainedValue() as String? ?? "";
// let rAppUrl : URL = (workspace.urlForApplication(withBundleIdentifier: handlerR))!.appendingPathComponent("Contents/Info.plist");
// let rAppDict : NSDictionary = NSDictionary(contentsOf: rAppUrl)!;
// let setResRmd : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "Rmd" as CFString, nil)?.takeRetainedValue() as String? ?? "";
// let handlerRmd : String = LSCopyDefaultRoleHandlerForContentType(setResRmd as CFString, LSRolesMask.all)?.takeRetainedValue() as String? ?? "";
// let rmdAppUrl : URL = (workspace.urlForApplication(withBundleIdentifier: handlerRmd))!.appendingPathComponent("Contents/Info.plist");
// let rmdAppDict : NSDictionary = NSDictionary(contentsOf: rmdAppUrl)!;
// }

public static func setHandlers() {

let setResR : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "R" as CFString, nil)?.takeRetainedValue() as String? ?? "";
//NSLog("UTI of .R extension: " + setResR);

LSSetDefaultRoleHandlerForContentType(setResR as CFString, LSRolesMask.all, "org.rstudio.RStudio" as CFString);

let setResRmd : String = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "Rmd" as CFString, nil)?.takeRetainedValue() as String? ?? "";
//NSLog("UTI of .Rmd extension: " + setResRmd);

LSSetDefaultRoleHandlerForContentType(setResRmd as CFString, LSRolesMask.all, "org.rstudio.RStudio" as CFString);

Expand Down
1 change: 0 additions & 1 deletion RSwitch/Swift/Utils/urls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class QueryParameters {
let queryItems: [URLQueryItem]
init(url: URL?) {
queryItems = URLComponents(string: url?.absoluteString ?? "")?.queryItems ?? []
print(queryItems)
}
subscript(name: String) -> String? {
return queryItems.first(where: { $0.name == name })?.value
Expand Down
66 changes: 25 additions & 41 deletions RSwitch/Swift/exportPopupViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,29 @@ class exportPopupViewController: NSViewController {

func loadWebView(urlIn: String) {

urlPath = urlIn

NSLog("loadWebView: \(urlPath)")

// Check for "/export/"
// If export, then get bring up a Save Panel and then download the file to that location

if let url = URL(string: urlPath) {

NSLog("URL path: \(url.path)")

if (url.path.starts(with: "/export")) {

NSLog(" Name: " + url.queryParameters["name"]!)

let savePanel = NSSavePanel()

savePanel.canCreateDirectories = true
savePanel.nameFieldStringValue = url.queryParameters["name"]!
savePanel.beginSheetModal(for:self.view.window!) { (response) in
if (response == NSApplication.ModalResponse.OK) {

download_from_studio_server(fromRS: url.absoluteString, toFS: savePanel.url!.absoluteString)

} else {

NSLog("Don't do anything!")
}
savePanel.close()
}


}

}
// urlPath = urlIn
//
// // Check for "/export/"
// // If export, then get bring up a Save Panel and then download the file to that location
//
// if let url = URL(string: urlPath) {
//
// if (url.path.starts(with: "/export")) {
//
// let savePanel = NSSavePanel()
//
// savePanel.canCreateDirectories = true
// savePanel.nameFieldStringValue = url.queryParameters["name"]!
// savePanel.beginSheetModal(for:self.view.window!) { (response) in
// if (response == NSApplication.ModalResponse.OK) {
// download_from_studio_server(fromRS: url.absoluteString, toFS: savePanel.url!.absoluteString)
// }
// savePanel.close()
// }
//
// }
//
// }

}

Expand All @@ -88,13 +76,9 @@ extension exportPopupViewController: WKUIDelegate {

extension exportPopupViewController: WKNavigationDelegate {

open func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Export DID START \(String(describing: webView.url))")
}
open func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { }

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("Export DID FINISH \(String(describing: webView.url))")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { }

}

34 changes: 2 additions & 32 deletions RSwitch/Swift/plotPopupViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,13 @@ class plotPopupViewController: NSViewController {

urlPath = urlIn

NSLog(urlPath)

// Check for "/export/"
// If export, then get bring up a Save Panel and then download the file to that location

if let url = URL(string: urlPath) {

let urlRequest = URLRequest(url: url)

NSLog("URL path: " + url.path)

if (url.path.starts(with: "/export")) {

NSLog(" Name: " + url.queryParameters["name"]!)

DispatchQueue.main.async {
self.webView.removeFromSuperview()
}

} else {

webView.load(urlRequest)
Expand All @@ -75,34 +63,16 @@ extension plotPopupViewController: WKUIDelegate {


func webView(_ webView: WKWebView, runOpenPanelWith parameters: WKOpenPanelParameters, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping ([URL]?) -> Void) {

NSLog("savePanel!")

let savePanel = NSSavePanel()

savePanel.canCreateDirectories = true
savePanel.beginSheetModal(for:self.view.window!) { (response) in
if (response == NSApplication.ModalResponse.OK) {
completionHandler([savePanel.url!])
} else {
completionHandler(nil)
}
savePanel.close()
}

}

}

extension plotPopupViewController: WKNavigationDelegate {

open func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("DID START")
}
open func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("DID FINISH")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { }

}

Loading

0 comments on commit 41e68c4

Please sign in to comment.