* refactoring

This commit is contained in:
Reinhard Pointner 2014-03-26 16:39:21 +00:00
parent 8b2e38a149
commit 91f37a5d20
4 changed files with 52 additions and 61 deletions

View File

@ -1359,6 +1359,21 @@ public class MediaDetection {
}
};
public static Object loadMetaInfo(File file) {
if (useExtendedFileAttributes()) {
try {
MetaAttributes xattr = new MetaAttributes(file);
Object meta = xattr.getObject();
if (meta instanceof Episode || meta instanceof Movie) {
return meta;
}
} catch (Throwable e) {
Logger.getLogger(MediaDetection.class.getClass().getName()).warning("Unable to read xattr: " + e.getMessage());
}
}
return null;
}
public static void storeMetaInfo(File file, Object model, String original, boolean useExtendedFileAttributes, boolean useCreationDate) {
// only for Episode / Movie objects
if ((useExtendedFileAttributes || useCreationDate) && (model instanceof Episode || model instanceof Movie) && file.isFile()) {
@ -1387,7 +1402,9 @@ public class MediaDetection {
// store original name and model as xattr
if (useExtendedFileAttributes) {
try {
xattr.setObject(model);
if (model instanceof Episode || model instanceof Movie) {
xattr.setObject(model);
}
if (xattr.getOriginalName() == null && original != null) {
xattr.setOriginalName(original);
}

View File

@ -98,7 +98,7 @@ class FilesListTransferablePolicy extends FileTransferablePolicy {
}
}
model.addAll(FastFile.foreach(entries));
model.addAll(FastFile.get(entries));
}
@Override

View File

@ -1,7 +1,5 @@
package net.sourceforge.filebot.ui.rename;
import static java.awt.datatransfer.DataFlavor.*;
import static net.sourceforge.filebot.hash.VerificationUtilities.*;
import static net.sourceforge.filebot.ui.transfer.FileTransferable.*;
@ -26,43 +24,37 @@ import net.sourceforge.filebot.vfs.SimpleFileInfo;
import net.sourceforge.filebot.web.Episode;
import net.sourceforge.tuned.FastFile;
class NamesListTransferablePolicy extends FileTransferablePolicy {
private static final DataFlavor episodeArrayFlavor = ArrayTransferable.flavor(Episode.class);
private final List<Object> model;
public NamesListTransferablePolicy(List<Object> model) {
this.model = model;
}
@Override
protected void clear() {
model.clear();
}
@Override
public boolean accept(Transferable tr) throws Exception {
return tr.isDataFlavorSupported(stringFlavor) || hasFileListFlavor(tr);
}
@Override
protected boolean accept(List<File> files) {
return true;
}
@Override
public void handleTransferable(Transferable tr, TransferAction action) throws Exception {
if (action == TransferAction.PUT) {
clear();
}
if (tr.isDataFlavorSupported(episodeArrayFlavor)) {
// episode array transferable
model.addAll(Arrays.asList((Episode[]) tr.getTransferData((episodeArrayFlavor))));
@ -74,29 +66,25 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
load((String) tr.getTransferData(stringFlavor));
}
}
protected void load(String string) {
List<String> values = new ArrayList<String>();
Scanner scanner = new Scanner(string);
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.length() > 0) {
values.add(line);
}
}
model.addAll(values);
}
@Override
protected void load(List<File> files) throws IOException {
List<Object> values = new ArrayList<Object>();
if (containsOnly(files, MediaTypes.getDefaultFilter("application/list"))) {
// list files
loadListFiles(files, values);
@ -108,42 +96,40 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
loadTorrentFiles(files, values);
} else {
// load all files from the given folders recursively up do a depth of 5
values.addAll(FastFile.foreach(flatten(files, 5, false)));
values.addAll(FastFile.get(flatten(files, 5, false)));
}
model.addAll(values);
}
protected void loadListFiles(List<File> files, List<Object> values) throws IOException {
for (File file : files) {
// don't use new Scanner(File) because of BUG 6368019 (http://bugs.sun.com/view_bug.do?bug_id=6368019)
Scanner scanner = new Scanner(createTextReader(file));
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.length() > 0) {
values.add(line);
}
}
scanner.close();
}
}
protected void loadVerificationFiles(List<File> files, List<Object> values) throws IOException {
for (File verificationFile : files) {
HashType type = getHashType(verificationFile);
// check if type is supported
if (type == null)
continue;
// add all file names from verification file
VerificationFileReader parser = new VerificationFileReader(createTextReader(verificationFile), type.getFormat());
try {
while (parser.hasNext()) {
values.add(new SimpleFileInfo(parser.next().getKey().getName(), -1));
@ -153,22 +139,20 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
}
}
}
protected void loadTorrentFiles(List<File> files, List<Object> values) throws IOException {
for (File file : files) {
Torrent torrent = new Torrent(file);
for (Torrent.Entry entry : torrent.getFiles()) {
values.add(new SimpleFileInfo(entry.getName(), entry.getLength()));
}
}
}
@Override
public String getFileFilterDescription() {
return "text files, verification files, torrent files";
}
}

View File

@ -1,74 +1,64 @@
package net.sourceforge.tuned;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class FastFile extends File {
private Long length;
private Boolean isDirectory;
private Boolean isFile;
public FastFile(String path) {
super(path);
}
public FastFile(File parent, String child) {
super(parent, child);
}
@Override
public long length() {
return length != null ? length : (length = super.length());
}
@Override
public boolean isDirectory() {
return isDirectory != null ? isDirectory : (isDirectory = super.isDirectory());
}
@Override
public boolean isFile() {
return isFile != null ? isFile : (isFile = super.isFile());
}
@Override
public File[] listFiles() {
String[] names = list();
if (names == null)
return null;
File[] files = new File[names.length];
for (int i = 0; i < names.length; i++) {
files[i] = new FastFile(this, names[i]);
}
return files;
}
public static List<FastFile> foreach(File... files) {
return foreach(Arrays.asList(files));
public static FastFile get(File file) {
return new FastFile(file.getPath());
}
public static List<FastFile> foreach(final List<File> files) {
public static List<FastFile> get(Collection<File> files) {
List<FastFile> result = new ArrayList<FastFile>(files.size());
for (File file : files) {
result.add(new FastFile(file.getPath()));
}
return result;
}
}