* allow move-rename in CLI

This commit is contained in:
Reinhard Pointner 2011-10-10 16:21:54 +00:00
parent cd8b8aa620
commit 4ca962f297
5 changed files with 43 additions and 41 deletions

View File

@ -172,19 +172,20 @@ public class ArgumentProcessor {
}
// map old files to new paths by applying formatting and validating filenames
Map<File, String> renameMap = new LinkedHashMap<File, String>();
Map<File, File> renameMap = new LinkedHashMap<File, File>();
for (Match<File, Episode> match : matches) {
File file = match.getValue();
Episode episode = match.getCandidate();
String newName = (format != null) ? format.format(new MediaBindingBean(episode, file)) : EpisodeFormat.SeasonEpisode.format(episode);
File newFile = new File(newName + "." + getExtension(file));
if (isInvalidFileName(newName)) {
if (isInvalidFilePath(newFile)) {
CLILogger.config("Stripping invalid characters from new name: " + newName);
newName = validateFileName(newName);
newFile = validateFilePath(newFile);
}
renameMap.put(file, newName + "." + getExtension(file));
renameMap.put(file, newFile);
}
// rename episodes
@ -209,20 +210,21 @@ public class ArgumentProcessor {
}
// map old files to new paths by applying formatting and validating filenames
Map<File, String> renameMap = new LinkedHashMap<File, String>();
Map<File, File> renameMap = new LinkedHashMap<File, File>();
for (int i = 0; i < movieFiles.length; i++) {
if (movieDescriptors[i] != null) {
Movie movie = movieDescriptors[i];
File file = movieFiles[i];
String newName = (format != null) ? format.format(new MediaBindingBean(movie, file)) : movie.toString();
File newFile = new File(newName + "." + getExtension(file));
if (isInvalidFileName(newName)) {
if (isInvalidFilePath(newFile)) {
CLILogger.config("Stripping invalid characters from new path: " + newName);
newName = validateFileName(newName);
newFile = validateFilePath(newFile);
}
renameMap.put(file, newName + "." + getExtension(file));
renameMap.put(file, newFile);
} else {
CLILogger.warning("No matching movie: " + movieFiles[i]);
}
@ -237,8 +239,9 @@ public class ArgumentProcessor {
String movieName = getName(movieFiles[i]);
if (subtitleName.equalsIgnoreCase(movieName)) {
String movieDestinationName = renameMap.get(movieFiles[i]);
renameMap.put(subtitleFile, getNameWithoutExtension(movieDestinationName) + "." + getExtension(subtitleFile));
File movieDestination = renameMap.get(movieFiles[i]);
File subtitleDestination = new File(movieDestination.getParentFile(), getName(movieDestination) + "." + getExtension(subtitleFile));
renameMap.put(subtitleFile, subtitleDestination);
// movie match found, we're done
break;
@ -375,12 +378,12 @@ public class ArgumentProcessor {
}
private Set<File> renameAll(Map<File, String> renameMap) throws Exception {
private Set<File> renameAll(Map<File, File> renameMap) throws Exception {
// rename files
final List<Entry<File, File>> renameLog = new ArrayList<Entry<File, File>>();
try {
for (Entry<File, String> it : renameMap.entrySet()) {
for (Entry<File, File> it : renameMap.entrySet()) {
try {
// rename file, throw exception on failure
File destination = renameFile(it.getKey(), it.getValue());

View File

@ -50,11 +50,11 @@ public final class HistorySpooler {
}
public synchronized void append(Iterable<Entry<File, String>> elements) {
public synchronized void append(Iterable<Entry<File, File>> elements) {
List<Element> sequence = new ArrayList<Element>();
for (Entry<File, String> element : elements) {
sequence.add(new Element(element.getKey().getName(), element.getValue(), element.getKey().getParentFile()));
for (Entry<File, File> element : elements) {
sequence.add(new Element(element.getKey().getName(), element.getValue().getPath(), element.getKey().getParentFile()));
}
// append to session history

View File

@ -16,6 +16,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import javax.swing.AbstractAction;
@ -39,10 +40,10 @@ class RenameAction extends AbstractAction {
public void actionPerformed(ActionEvent evt) {
List<Entry<File, String>> renameLog = new ArrayList<Entry<File, String>>();
List<Entry<File, File>> renameLog = new ArrayList<Entry<File, File>>();
try {
for (Entry<File, String> mapping : validate(model.getRenameMap(), getWindow(evt.getSource()))) {
for (Entry<File, File> mapping : validate(model.getRenameMap(), getWindow(evt.getSource()))) {
// rename file, throw exception on failure
renameFile(mapping.getKey(), mapping.getValue());
@ -59,12 +60,12 @@ class RenameAction extends AbstractAction {
UILogger.warning(e.getMessage());
// revert rename operations in reverse order
for (ListIterator<Entry<File, String>> iterator = renameLog.listIterator(renameLog.size()); iterator.hasPrevious();) {
Entry<File, String> mapping = iterator.previous();
for (ListIterator<Entry<File, File>> iterator = renameLog.listIterator(renameLog.size()); iterator.hasPrevious();) {
Entry<File, File> mapping = iterator.previous();
// revert rename
File original = mapping.getKey();
File current = new File(original.getParentFile(), mapping.getValue());
File current = new File(original.getParentFile(), mapping.getValue().getPath());
if (current.renameTo(original)) {
// remove reverted rename operation from log
@ -100,19 +101,22 @@ class RenameAction extends AbstractAction {
}
private Iterable<Entry<File, String>> validate(Map<File, String> renameMap, Window parent) {
final List<Entry<File, String>> source = new ArrayList<Entry<File, String>>(renameMap.entrySet());
private Iterable<Entry<File, File>> validate(Map<File, String> renameMap, Window parent) {
final List<Entry<File, File>> source = new ArrayList<Entry<File, File>>(renameMap.size());
for (Entry<File, String> entry : renameMap.entrySet()) {
source.add(new SimpleEntry<File, File>(entry.getKey(), new File(entry.getValue())));
}
List<String> destinationFileNameView = new AbstractList<String>() {
List<File> destinationFileNameView = new AbstractList<File>() {
@Override
public String get(int index) {
public File get(int index) {
return source.get(index).getValue();
}
@Override
public String set(int index, String name) {
public File set(int index, File name) {
return source.get(index).setValue(name);
}
@ -131,5 +135,4 @@ class RenameAction extends AbstractAction {
// return empty list if validation was cancelled
return emptyList();
}
}

View File

@ -39,15 +39,15 @@ class ValidateDialog extends JDialog {
private final JList list;
private String[] model;
private File[] model;
private boolean cancelled = true;
public ValidateDialog(Window owner, Collection<String> source) {
public ValidateDialog(Window owner, Collection<File> source) {
super(owner, "Invalid Names", ModalityType.DOCUMENT_MODAL);
model = source.toArray(new String[0]);
model = source.toArray(new File[0]);
list = new JList(model);
list.setEnabled(false);
@ -99,7 +99,7 @@ class ValidateDialog extends JDialog {
}
public List<String> getModel() {
public List<File> getModel() {
return unmodifiableList(Arrays.asList(model));
}
@ -124,7 +124,7 @@ class ValidateDialog extends JDialog {
// validate names
for (int i = 0; i < model.length; i++) {
// remove illegal characters
model[i] = validateFilePath(new File(model[i])).getPath();
model[i] = validateFilePath(model[i]);
}
// update view
@ -155,14 +155,12 @@ class ValidateDialog extends JDialog {
};
public static boolean validate(Component parent, List<String> source) {
IndexView<String> invalidFilePaths = new IndexView<String>(source);
public static boolean validate(Component parent, List<File> source) {
IndexView<File> invalidFilePaths = new IndexView<File>(source);
for (int i = 0; i < source.size(); i++) {
String path = source.get(i);
// invalid file names are also invalid file paths
if (isInvalidFilePath(new File(path))) {
if (isInvalidFilePath(source.get(i))) {
invalidFilePaths.addIndex(i);
}
}
@ -182,7 +180,7 @@ class ValidateDialog extends JDialog {
return false;
}
List<String> validatedFilePaths = dialog.getModel();
List<File> validatedFilePaths = dialog.getModel();
// validate source list via index view
for (int i = 0; i < invalidFilePaths.size(); i++) {

View File

@ -30,13 +30,11 @@ import com.ibm.icu.text.CharsetMatch;
public final class FileUtilities {
public static File renameFile(File source, String newPath) throws IOException {
File destination = new File(newPath);
public static File renameFile(File source, File destination) throws IOException {
// resolve destination
if (!destination.isAbsolute()) {
// same folder, different name
destination = new File(source.getParentFile(), newPath);
destination = new File(source.getParentFile(), destination.getPath());
}
// make sure we that we can create the destination folder structure