1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 16:28:51 -05:00

Refactor NativeRenameAction

This commit is contained in:
Reinhard Pointner 2016-08-15 02:55:05 +08:00
parent 9f263f7de8
commit 76619dbfdf
2 changed files with 26 additions and 28 deletions

View File

@ -4,8 +4,9 @@ import static java.util.Collections.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import com.sun.jna.Platform; import com.sun.jna.Platform;
@ -26,21 +27,20 @@ public enum NativeRenameAction implements RenameAction {
} }
public void rename(Map<File, File> map) { public void rename(Map<File, File> map) {
String[] src = new String[map.size()]; List<File> src = new ArrayList<File>(map.size());
String[] dst = new String[map.size()]; List<File> dst = new ArrayList<File>(map.size());
// resolve paths map.forEach((from, to) -> {
int i = 0; // resolve relative paths
for (Entry<File, File> it : map.entrySet()) { src.add(from);
src[i] = it.getKey().getAbsolutePath(); dst.add(resolve(from, to));
dst[i] = resolve(it.getKey(), it.getValue()).getAbsolutePath(); });
i++;
}
callNative_Shell32(this, src, dst); // call Windows MOVE / COPY dialog
SHFileOperation(this, getPathArray(src), getPathArray(dst));
} }
private static void callNative_Shell32(NativeRenameAction action, String[] src, String[] dst) { private static void SHFileOperation(NativeRenameAction action, String[] src, String[] dst) {
// configure parameter structure // configure parameter structure
SHFILEOPSTRUCT op = new SHFILEOPSTRUCT(); SHFILEOPSTRUCT op = new SHFILEOPSTRUCT();
op.wFunc = (action == MOVE) ? ShellAPI.FO_MOVE : ShellAPI.FO_COPY; op.wFunc = (action == MOVE) ? ShellAPI.FO_MOVE : ShellAPI.FO_COPY;
@ -56,9 +56,13 @@ public enum NativeRenameAction implements RenameAction {
} }
} }
public static boolean isSupported() { private static String[] getPathArray(List<File> files) {
return files.stream().map(File::getAbsolutePath).toArray(String[]::new);
}
public static boolean isSupported(StandardRenameAction action) {
try { try {
return Platform.isWindows(); return Platform.isWindows() && (action == StandardRenameAction.MOVE || action == StandardRenameAction.COPY);
} catch (Throwable e) { } catch (Throwable e) {
return false; return false;
} }

View File

@ -88,7 +88,7 @@ class RenameAction extends AbstractAction {
Map<File, File> renameLog = new LinkedHashMap<File, File>(); Map<File, File> renameLog = new LinkedHashMap<File, File>();
try { try {
if (useNativeShell() && isNativeActionSupported(action)) { if (useNativeShell() && NativeRenameAction.isSupported(action)) {
// call on EDT // call on EDT
RenameWorker worker = new NativeRenameWorker(renameMap, renameLog, NativeRenameAction.valueOf(action.name())); RenameWorker worker = new NativeRenameWorker(renameMap, renameLog, NativeRenameAction.valueOf(action.name()));
worker.call(null, null, null); worker.call(null, null, null);
@ -191,14 +191,6 @@ class RenameAction extends AbstractAction {
} }
} }
public boolean isNativeActionSupported(StandardRenameAction action) {
try {
return NativeRenameAction.isSupported() && NativeRenameAction.valueOf(action.name()) != null;
} catch (IllegalArgumentException e) {
return false;
}
}
private Map<File, File> checkRenamePlan(List<Entry<File, File>> renamePlan, Window parent) throws IOException { private Map<File, File> checkRenamePlan(List<Entry<File, File>> renamePlan, Window parent) throws IOException {
// ask for user permissions to output paths // ask for user permissions to output paths
if (isMacSandbox()) { if (isMacSandbox()) {
@ -348,13 +340,15 @@ class RenameAction extends AbstractAction {
// prepare delta, ignore files already named as desired // prepare delta, ignore files already named as desired
Map<File, File> renamePlan = new LinkedHashMap<File, File>(); Map<File, File> renamePlan = new LinkedHashMap<File, File>();
for (Entry<File, File> mapping : renameMap.entrySet()) {
File source = mapping.getKey(); renameMap.forEach((source, destination) -> {
File destination = resolve(mapping.getKey(), mapping.getValue()); // resolve relative paths
if (!source.equals(destination)) { destination = resolve(source, destination);
if (!equalsCaseSensitive(source, destination)) {
renamePlan.put(source, destination); renamePlan.put(source, destination);
} }
} });
// call native shell move/copy // call native shell move/copy
try { try {