use of org.rococoa.ObjCObjectByReference in project cyberduck by iterate-ch.
the class LaunchServicesSchemeHandler method getDefaultHandler.
/**
* See ApplicationServices/ApplicationServices.h#LSCopyDefaultHandlerForURLScheme
*
* @param scheme The protocol identifier
* @return The bundle identifier for the application registered as the default handler for this scheme
*/
@Override
public Application getDefaultHandler(final String scheme) {
final ObjCObjectByReference error = new ObjCObjectByReference();
final NSURL url = LaunchServicesLibrary.library.LSCopyDefaultApplicationURLForURL(NSURL.URLWithString(String.format("%s:/", scheme)), LaunchServicesLibrary.kLSRolesAll, error);
if (url != null) {
final NSBundle bundle = NSBundle.bundleWithPath(url.path());
if (null == bundle) {
log.warn(String.format("Failure loading bundle for path %s", url.path()));
return Application.notfound;
}
final Application application = applicationFinder.getDescription(bundle.bundleIdentifier());
if (applicationFinder.isInstalled(application)) {
return application;
}
}
return Application.notfound;
}
use of org.rococoa.ObjCObjectByReference in project cyberduck by iterate-ch.
the class AbstractPromptBookmarkResolver method create.
@Override
public String create(final Local file) throws AccessDeniedException {
if (this.skip(file)) {
return null;
}
final ObjCObjectByReference error = new ObjCObjectByReference();
// Create new security scoped bookmark
final NSURL url = NSURL.fileURLWithPath(file.getAbsolute());
if (log.isTraceEnabled()) {
log.trace(String.format("Resolved file %s to url %s", file, url));
}
final NSData data = url.bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error(create, null, null, error);
if (null == data) {
log.warn(String.format("Failure getting bookmark data for file %s", file));
final NSError f = error.getValueAs(NSError.class);
if (null == f) {
throw new LocalAccessDeniedException(file.getAbsolute());
}
throw new LocalAccessDeniedException(String.format("%s", f.localizedDescription()));
}
final String encoded = data.base64Encoding();
if (log.isTraceEnabled()) {
log.trace(String.format("Encoded bookmark for %s as %s", file, encoded));
}
return encoded;
}
use of org.rococoa.ObjCObjectByReference in project cyberduck by iterate-ch.
the class AbstractPromptBookmarkResolver method resolve.
@Override
public NSURL resolve(final Local file, final boolean interactive) throws AccessDeniedException {
if (this.skip(file)) {
return null;
}
final NSData bookmark;
if (null == file.getBookmark()) {
if (interactive) {
if (!file.exists()) {
return null;
}
// Prompt user if no bookmark reference is available
log.warn(String.format("Missing security scoped bookmark for file %s", file));
final String reference = this.choose(file);
if (null == reference) {
// Prompt canceled by user
return null;
}
file.setBookmark(reference);
bookmark = NSData.dataWithBase64EncodedString(reference);
} else {
log.warn(String.format("No security scoped bookmark for %s", file.getName()));
return null;
}
} else {
bookmark = NSData.dataWithBase64EncodedString(file.getBookmark());
}
final ObjCObjectByReference error = new ObjCObjectByReference();
final NSURL resolved = NSURL.URLByResolvingBookmarkData(bookmark, resolve, error);
if (null == resolved) {
log.warn(String.format("Error resolving bookmark for %s to URL", file));
final NSError f = error.getValueAs(NSError.class);
if (null == f) {
throw new LocalAccessDeniedException(file.getAbsolute());
}
throw new LocalAccessDeniedException(String.format("%s", f.localizedDescription()));
}
return resolved;
}
use of org.rococoa.ObjCObjectByReference in project cyberduck by iterate-ch.
the class ApplescriptTerminalService method open.
@Override
public void open(final Host host, final Path workdir) throws AccessDeniedException {
final boolean identity = host.getCredentials().isPublicKeyAuthentication();
final Application application;
switch(StringUtils.lowerCase(finder.find(".command").getIdentifier())) {
case "com.googlecode.iterm2":
case "com.apple.terminal":
application = finder.find(".command");
break;
default:
log.warn(String.format("Unsupported application %s assigned", finder.find(".command")));
application = finder.getDescription(preferences.getProperty("terminal.bundle.identifier"));
}
if (!finder.isInstalled(application)) {
throw new LocalAccessDeniedException("Unable to determine default Terminal application");
}
String ssh = MessageFormat.format(preferences.getProperty("terminal.command.ssh"), identity ? String.format("-i \"%s\"", host.getCredentials().getIdentity().getAbsolute()) : StringUtils.EMPTY, host.getCredentials().getUsername(), host.getHostname(), String.valueOf(host.getPort()), this.escape(workdir.getAbsolute()));
if (log.isInfoEnabled()) {
log.info(String.format("Execute SSH command %s", ssh));
}
// Escape
ssh = StringUtils.replace(ssh, "\\", "\\\\");
// Escape all " for do script command
ssh = StringUtils.replace(ssh, "\"", "\\\"");
if (log.isInfoEnabled()) {
log.info("Escaped SSH Command for Applescript:" + ssh);
}
// Applescript
final String applescript;
switch(application.getIdentifier()) {
case "com.googlecode.iterm2":
applescript = MessageFormat.format(preferences.getProperty("terminal.command.iterm2"), ssh);
break;
default:
applescript = MessageFormat.format(preferences.getProperty("terminal.command.default"), ssh);
break;
}
final String command = "tell application \"" + application.getName() + "\"" + "\n" + "activate" + "\n" + applescript + "\n" + "end tell";
if (log.isInfoEnabled()) {
log.info(String.format("Execute AppleScript %s", command));
}
final NSAppleScript as = NSAppleScript.createWithSource(command);
final ObjCObjectByReference error = new ObjCObjectByReference();
if (null == as.executeAndReturnError(error)) {
final NSDictionary d = error.getValueAs(NSDictionary.class);
throw new LocalAccessDeniedException(String.format("Failure running script in %s. %s", application.getName(), d.objectForKey("NSAppleScriptErrorBriefMessage")));
}
}
use of org.rococoa.ObjCObjectByReference in project cyberduck by iterate-ch.
the class FinderLocalAttributes method getNativeAttributes.
private NSDictionary getNativeAttributes() throws AccessDeniedException, NotfoundException {
if ((!local.exists())) {
throw new LocalNotfoundException(local.getAbsolute());
}
final ObjCObjectByReference error = new ObjCObjectByReference();
// If flag is true and path is a symbolic link, the attributes of the linked-to file are returned;
// if the link points to a nonexistent file, this method returns null. If flag is false,
// the attributes of the symbolic link are returned.
final NSDictionary dict = NSFileManager.defaultManager().attributesOfItemAtPath_error(local.getAbsolute(), error);
if (null == dict) {
final NSError f = error.getValueAs(NSError.class);
if (null == f) {
throw new LocalAccessDeniedException(local.getAbsolute());
}
throw new LocalAccessDeniedException(String.format("%s", f.localizedDescription()));
}
return dict;
}
Aggregations