public String getLocal() { final Local local = roots.iterator().next().local; if(roots.size() == 1) { return local.getAbbreviatedPath(); } else { return local.getParent().getAbbreviatedPath(); } }
/** * @param source Connection to source server of transfer. May be null. * @param destination Connection to target server of transfer * @param files Files transfered * @param callback Prompt */ public void post(final Session<?> source, final Session<?> destination, final Map<TransferItem, TransferStatus> files, final ConnectionCallback callback) throws BackgroundException { for(Iterator<Map.Entry<Local, Object>> iter = locks.entrySet().iterator(); iter.hasNext(); ) { final Map.Entry<Local, Object> entry = iter.next(); switch(this.getType()) { case download: final Local directory = entry.getKey().getParent(); directory.release(entry.getValue()); break; case upload: entry.getKey().release(entry.getValue()); break; } iter.remove(); } }
public TransferItem getParent() { return new TransferItem(remote.getParent(), null == local ? null : local.getParent()); }
@Override public Local getVolume() { for(Local parent = this.getParent(); !parent.isRoot(); parent = parent.getParent()) { final Local directory = parent.getParent(); if(null == directory) { return super.getVolume(); } if("/Volumes".equals(directory.getAbsolute())) { return parent; } } return super.getVolume(); }
/** * @param source Connection to source server of transfer. May be null. * @param destination Connection to target server of transfer * @param files Files pending transfer * @param callback Prompt */ public void pre(final Session<?> source, final Session<?> destination, final Map<TransferItem, TransferStatus> files, final ConnectionCallback callback) throws BackgroundException { for(TransferItem item : roots) { switch(this.getType()) { case download: final Local directory = item.local.getParent(); locks.put(directory, directory.lock(true)); break; case upload: locks.put(item.local, item.local.lock(true)); break; } } }
private Local create(final String folder, final String name) { final Local file = LocalFactory.get(new File(PreferencesFactory.get().getProperty("tmp.dir"), folder).getAbsolutePath(), name); this.delete(file.getParent()); return this.delete(file); } }
@Override public void touch(final Local file) throws AccessDeniedException { final Local parent = file.getParent(); if(!parent.exists()) { new DefaultLocalDirectoryFeature().mkdir(parent); if(log.isDebugEnabled()) { log.debug(String.format("Created folder %s", parent)); } } if(file.exists()) { if(log.isDebugEnabled()) { log.debug(String.format("Skip creating file %s", file)); } return; } try { Files.createFile(Paths.get(file.getAbsolute())); } catch(IOException e) { throw new LocalAccessDeniedException(MessageFormat.format( LocaleFactory.localizedString("Cannot create {0}", "Error"), file.getAbsolute()), e); } if(log.isDebugEnabled()) { log.debug(String.format("Created file %s", file)); } } }
public Local getSymlinkTarget() throws NotfoundException { try { try { Paths.get(path).toRealPath(); } catch(NoSuchFileException ignore) { // Proceed if target is not found } catch(FileSystemException e) { // Too many levels of symbolic links log.warn(String.format("Failure resolving symlink target for %s. %s", path, e.getMessage())); throw new LocalNotfoundException(MessageFormat.format("Failure to read attributes of {0}", this.getName()), e); } // For a link that actually points to something (either a file or a directory), // the absolute path is the path through the link, whereas the canonical path // is the path the link references. final Path target = Files.readSymbolicLink(Paths.get(path)); if(target.isAbsolute()) { return LocalFactory.get(target.toString()); } else { return LocalFactory.get(this.getParent(), target.toString()); } } catch(InvalidPathException | IOException e) { throw new LocalNotfoundException(MessageFormat.format("Failure to read attributes of {0}", this.getName()), e); } }
private Properties load() { final Properties properties = new Properties(); try { new DefaultLocalDirectoryFeature().mkdir(file.getParent()); } catch(AccessDeniedException e) { log.warn(String.format("Failure saving credentials to %s. %s", file.getAbsolute(), e.getDetail())); } if(file.exists()) { try { try (InputStream in = file.getInputStream()) { properties.load(in); } } catch(AccessDeniedException e) { log.warn(String.format("Failure reading credentials from %s. %s", file.getAbsolute(), e.getDetail())); } catch(IOException e) { log.warn(String.format("Failure reading credentials from %s. %s", file.getAbsolute(), e.getMessage())); } } return properties; }
protected NSBundle bundle(final NSBundle main, Local executable) { if(!executable.isSymbolicLink()) { return main; } while(executable.isSymbolicLink()) { try { executable = executable.getSymlinkTarget(); } catch(NotfoundException e) { return main; } } Local folder = executable.getParent(); NSBundle b; do { b = NSBundle.bundleWithPath(folder.getAbsolute()); if(null == b) { log.error(String.format("Loading bundle %s failed", folder)); break; } if(StringUtils.equals(String.valueOf(Path.DELIMITER), b.bundlePath())) { break; } folder = folder.getParent(); } while(b.executablePath() == null); return b; } }
@Override public void apply(final Path file, final Local local, final TransferStatus status, final ProgressListener listener) throws BackgroundException { if(status.isExists()) { Local rename; do { String proposal = MessageFormat.format(PreferencesFactory.get().getProperty("queue.download.file.rename.format"), FilenameUtils.getBaseName(file.getName()), UserDateFormatterFactory.get().getMediumFormat(System.currentTimeMillis(), false).replace(local.getDelimiter(), '-').replace(':', '-'), StringUtils.isNotBlank(file.getExtension()) ? String.format(".%s", file.getExtension()) : StringUtils.EMPTY); rename = LocalFactory.get(local.getParent().getAbsolute(), proposal); } while(rename.exists()); if(log.isInfoEnabled()) { log.info(String.format("Rename existing file %s to %s", local, rename)); } local.rename(rename); if(log.isDebugEnabled()) { log.debug(String.format("Clear exist flag for file %s", local)); } status.setExists(false); } super.apply(file, local, status, listener); } }
/** * This function works only with executables stored in the * <code>Contents/Library/LoginItems</code> directory of the bundle. */ @Override public boolean register(final Application application) { final Local helper = new FinderLocal(new BundleApplicationResourcesFinder().find().getParent(), String.format("Library/LoginItems/%s.app", application.getName())); if(!finder.register(helper)) { log.warn(String.format("Failed to register %s (%s) with launch services", helper, finder.getDescription(application.getIdentifier()))); } if(!ServiceManagementLibrary.SMLoginItemSetEnabled(application.getIdentifier(), true)) { log.warn(String.format("Failed to register %s as login item", application)); return false; } return true; }
@Override public boolean reveal(final Local file) { synchronized(NSWorkspace.class) { // If a second path argument is specified, a new file viewer is opened. If you specify an // empty string (@"") for this parameter, the file is selected in the main viewer. return NSWorkspace.sharedWorkspace().selectFile(new NFDNormalizer().normalize(file.getAbsolute()).toString(), new NFDNormalizer().normalize(file.getParent().getAbsolute()).toString()); } } }
public String normalize(final String name) { if(StringUtils.equals(name, ".")) { return finder.find().getAbsolute(); } if(StringUtils.equals(name, "..")) { return finder.find().getParent().getAbsolute(); } if(!this.isAbsolute(name)) { return String.format("%s%s%s", finder.find().getAbsolute(), PreferencesFactory.get().getProperty("local.delimiter"), name); } return name; } }
/** * Move file to trash on main interface thread using <code>NSWorkspace.RecycleOperation</code>. */ @Override public void trash(final Local file) throws LocalAccessDeniedException { synchronized(NSWorkspace.class) { if(log.isDebugEnabled()) { log.debug(String.format("Move %s to Trash", file)); } final NSWorkspace workspace = NSWorkspace.sharedWorkspace(); // Asynchronous operation. 0 if the operation is performed synchronously and succeeds, and a positive // integer if the operation is performed asynchronously and succeeds if(!workspace.performFileOperation( NSWorkspace.RecycleOperation, new NFDNormalizer().normalize(file.getParent().getAbsolute()).toString(), StringUtils.EMPTY, NSArray.arrayWithObject(new NFDNormalizer().normalize(file.getName()).toString()))) { throw new LocalAccessDeniedException(String.format("Failed to move %s to Trash", file.getName())); } } } }
status.rename(LocalFactory.get(local.getParent(), proposal));
@Action public void downloadAsPanelDidEnd_returnCode_contextInfo(final NSSavePanel sheet, final int returncode, final ID contextInfo) { sheet.orderOut(contextInfo); if(returncode == SheetCallback.DEFAULT_OPTION) { if(sheet.filename() != null) { final Local target = LocalFactory.get(sheet.filename()); new DownloadDirectoryFinder().save(pool.getHost(), target.getParent()); final List<TransferItem> downloads = Collections.singletonList(new TransferItem(this.getSelectedPath(), target)); this.transfer(new DownloadTransfer(pool.getHost(), downloads), Collections.emptyList()); } } }
while((next = iterator.nextObject()) != null) { final Local local = LocalFactory.get(next.toString()); final Local localParent = local.getParent();
@Override public void run() { final NSOpenPanel panel = NSOpenPanel.openPanel(); panel.setCanChooseDirectories(file.isDirectory()); panel.setCanChooseFiles(file.isFile()); panel.setAllowsMultipleSelection(false); panel.setMessage(MessageFormat.format(LocaleFactory.localizedString("Select {0}", "Credentials"), file.getAbbreviatedPath())); panel.setPrompt(LocaleFactory.localizedString("Choose")); final NSInteger modal = panel.runModal(file.getParent().getAbsolute(), file.getName()); if(modal.intValue() == SheetCallback.DEFAULT_OPTION) { final NSArray filenames = panel.filenames(); final NSEnumerator enumerator = filenames.objectEnumerator(); NSObject next; while((next = enumerator.nextObject()) != null) { selected.set(new FinderLocal(next.toString())); } } panel.orderOut(null); } };
@Action public void syncPanelDidEnd_returnCode_contextInfo(final NSOpenPanel sheet, final int returncode, final ID contextInfo) { sheet.orderOut(contextInfo); if(returncode == SheetCallback.DEFAULT_OPTION) { if(sheet.filename() != null) { final Local target = LocalFactory.get(sheet.filename()); new UploadDirectoryFinder().save(pool.getHost(), target.getParent()); final Path selected; if(this.getSelectionCount() == 1 && this.getSelectedPath().isDirectory()) { selected = this.getSelectedPath(); } else { selected = this.workdir(); } this.transfer(new SyncTransfer(pool.getHost(), new TransferItem(selected, target))); } } }