1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 08:48:51 -05:00

+++ clean-up GVFS support

This commit is contained in:
Reinhard Pointner 2012-11-24 00:11:07 +00:00
parent 879deaa643
commit 8ab2bd5b5a
3 changed files with 73 additions and 62 deletions

View File

@ -1,31 +1,32 @@
package net.sourceforge.filebot.gio; package net.sourceforge.filebot.gio;
import static java.util.Collections.*;
import java.lang.reflect.Method;
import com.sun.jna.FunctionMapper;
import com.sun.jna.Library; import com.sun.jna.Library;
import com.sun.jna.Native; import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Pointer; import com.sun.jna.Pointer;
import com.sun.jna.WString;
interface GIOLibrary extends Library { interface GIOLibrary extends Library {
GIOLibrary INSTANCE = (GIOLibrary) Native.loadLibrary("gio-2.0", GIOLibrary INSTANCE = (GIOLibrary) Native.loadLibrary("gio-2.0", GIOLibrary.class);
GIOLibrary.class);
void g_type_init(); void g_type_init();
Pointer g_vfs_get_default(); Pointer g_vfs_get_default();
Pointer g_vfs_get_file_for_uri(Pointer gvfs, String uri); Pointer g_vfs_get_file_for_uri(Pointer gvfs, String uri);
Pointer g_file_get_path(Pointer gfile); Pointer g_file_get_path(Pointer gfile);
void g_free(Pointer gpointer); void g_free(Pointer gpointer);
void g_object_unref(Pointer gobject); void g_object_unref(Pointer gobject);
} }

View File

@ -1,15 +1,19 @@
package net.sourceforge.filebot.gio; package net.sourceforge.filebot.gio;
import java.io.File; import java.io.File;
import java.net.URI; import java.net.URI;
import com.sun.jna.Platform; import com.sun.jna.Platform;
import com.sun.jna.Pointer; import com.sun.jna.Pointer;
public class GVFS { public class GVFS {
private static Pointer gvfs; private static Pointer gvfs;
private synchronized static Pointer getDefaultVFS() { private synchronized static Pointer getDefaultVFS() {
if (gvfs == null) { if (gvfs == null) {
GIOLibrary.INSTANCE.g_type_init(); GIOLibrary.INSTANCE.g_type_init();
@ -17,12 +21,12 @@ public class GVFS {
} }
return gvfs; return gvfs;
} }
public static File getPathForURI(URI uri) { public static File getPathForURI(URI uri) {
Pointer gfile = GIOLibrary.INSTANCE.g_vfs_get_file_for_uri( Pointer gfile = GIOLibrary.INSTANCE.g_vfs_get_file_for_uri(getDefaultVFS(), uri.toString());
getDefaultVFS(), uri.toString());
Pointer chars = GIOLibrary.INSTANCE.g_file_get_path(gfile); Pointer chars = GIOLibrary.INSTANCE.g_file_get_path(gfile);
try { try {
if (chars != null) if (chars != null)
return new File(chars.getString(0)); return new File(chars.getString(0));
@ -33,9 +37,10 @@ public class GVFS {
GIOLibrary.INSTANCE.g_free(chars); GIOLibrary.INSTANCE.g_free(chars);
} }
} }
public static boolean isSupported() { public static boolean isSupported() {
return Platform.isLinux() || Platform.isFreeBSD(); return Platform.isLinux() || Platform.isFreeBSD();
} }
} }

View File

@ -1,5 +1,7 @@
package net.sourceforge.filebot.ui.transfer; package net.sourceforge.filebot.ui.transfer;
import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.datatransfer.UnsupportedFlavorException;
@ -18,10 +20,12 @@ import java.util.logging.Logger;
import net.sourceforge.filebot.gio.GVFS; import net.sourceforge.filebot.gio.GVFS;
public class FileTransferable implements Transferable { public class FileTransferable implements Transferable {
public static final DataFlavor uriListFlavor = createUriListFlavor(); public static final DataFlavor uriListFlavor = createUriListFlavor();
private static DataFlavor createUriListFlavor() { private static DataFlavor createUriListFlavor() {
try { try {
return new DataFlavor("text/uri-list;class=java.nio.CharBuffer"); return new DataFlavor("text/uri-list;class=java.nio.CharBuffer");
@ -30,20 +34,22 @@ public class FileTransferable implements Transferable {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private final File[] files; private final File[] files;
public FileTransferable(File... files) { public FileTransferable(File... files) {
this.files = files; this.files = files;
} }
public FileTransferable(Collection<File> files) { public FileTransferable(Collection<File> files) {
this.files = files.toArray(new File[0]); this.files = files.toArray(new File[0]);
} }
@Override @Override
public Object getTransferData(DataFlavor flavor) public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
throws UnsupportedFlavorException {
if (flavor.isFlavorJavaFileListType()) if (flavor.isFlavorJavaFileListType())
return Arrays.asList(files); return Arrays.asList(files);
else if (flavor.equals(uriListFlavor)) else if (flavor.equals(uriListFlavor))
@ -51,64 +57,66 @@ public class FileTransferable implements Transferable {
else else
throw new UnsupportedFlavorException(flavor); throw new UnsupportedFlavorException(flavor);
} }
/** /**
* @return line separated list of file URIs * @return line separated list of file URIs
*/ */
private String getUriList() { private String getUriList() {
StringBuilder sb = new StringBuilder(80 * files.length); StringBuilder sb = new StringBuilder(80 * files.length);
for (File file : files) { for (File file : files) {
// use URI encoded path // use URI encoded path
sb.append("file://").append(file.toURI().getRawPath()); sb.append("file://").append(file.toURI().getRawPath());
sb.append("\r\n"); sb.append("\r\n");
} }
return sb.toString(); return sb.toString();
} }
@Override @Override
public DataFlavor[] getTransferDataFlavors() { public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.javaFileListFlavor, uriListFlavor }; return new DataFlavor[] { DataFlavor.javaFileListFlavor, uriListFlavor };
} }
@Override @Override
public boolean isDataFlavorSupported(DataFlavor flavor) { public boolean isDataFlavorSupported(DataFlavor flavor) {
return isFileListFlavor(flavor); return isFileListFlavor(flavor);
} }
public static boolean isFileListFlavor(DataFlavor flavor) { public static boolean isFileListFlavor(DataFlavor flavor) {
return flavor.isFlavorJavaFileListType() return flavor.isFlavorJavaFileListType() || flavor.equals(uriListFlavor);
|| flavor.equals(uriListFlavor);
} }
public static boolean hasFileListFlavor(Transferable tr) { public static boolean hasFileListFlavor(Transferable tr) {
return tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor) return tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || tr.isDataFlavorSupported(FileTransferable.uriListFlavor);
|| tr.isDataFlavorSupported(FileTransferable.uriListFlavor);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static List<File> getFilesFromTransferable(Transferable tr) public static List<File> getFilesFromTransferable(Transferable tr) throws IOException, UnsupportedFlavorException {
throws IOException, UnsupportedFlavorException {
if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) { if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) {
// file URI list flavor (Linux) // file URI list flavor (Linux)
Readable transferData = (Readable) tr Readable transferData = (Readable) tr.getTransferData(FileTransferable.uriListFlavor);
.getTransferData(FileTransferable.uriListFlavor);
Scanner scanner = new Scanner(transferData); Scanner scanner = new Scanner(transferData);
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
String line = scanner.nextLine(); String line = scanner.nextLine();
if (line.startsWith("#")) { if (line.startsWith("#")) {
// the line is a comment (as per RFC 2483) // the line is a comment (as per RFC 2483)
continue; continue;
} }
try { try {
URI uri = new URI(line); URI uri = new URI(line);
File file = null; File file = null;
try { try {
// file URIs // file URIs
file = new File(uri); file = new File(uri);
@ -118,26 +126,23 @@ public class FileTransferable implements Transferable {
file = GVFS.getPathForURI(uri); file = GVFS.getPathForURI(uri);
} }
} }
if (!file.exists()) { if (!file.exists()) {
throw new FileNotFoundException(file.toString()); throw new FileNotFoundException(file.toString());
} }
files.add(file); files.add(file);
} catch (Exception e) { } catch (Throwable e) {
// URISyntaxException, IllegalArgumentException, // URISyntaxException, IllegalArgumentException, FileNotFoundException, LinkageError, etc
// FileNotFoundException Logger.getLogger(FileTransferable.class.getName()).log(Level.WARNING, "Invalid file URI: " + line, e);
Logger.getLogger(FileTransferable.class.getName()).log(
Level.WARNING, "Invalid file uri: " + line);
} }
return files; return files;
} }
} else if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { } else if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
// file list flavor // file list flavor
return (List<File>) tr return (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
.getTransferData(DataFlavor.javaFileListFlavor);
} }
// cannot get files from transferable // cannot get files from transferable
throw new UnsupportedFlavorException(null); throw new UnsupportedFlavorException(null);
} }