+++ 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;
import static java.util.Collections.*;
import java.lang.reflect.Method;
import com.sun.jna.FunctionMapper;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
interface GIOLibrary extends Library {
GIOLibrary INSTANCE = (GIOLibrary) Native.loadLibrary("gio-2.0",
GIOLibrary.class);
GIOLibrary INSTANCE = (GIOLibrary) Native.loadLibrary("gio-2.0", GIOLibrary.class);
void g_type_init();
Pointer g_vfs_get_default();
Pointer g_vfs_get_file_for_uri(Pointer gvfs, String uri);
Pointer g_file_get_path(Pointer gfile);
void g_free(Pointer gpointer);
void g_object_unref(Pointer gobject);
}

View File

@ -1,15 +1,19 @@
package net.sourceforge.filebot.gio;
import java.io.File;
import java.net.URI;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
public class GVFS {
private static Pointer gvfs;
private synchronized static Pointer getDefaultVFS() {
if (gvfs == null) {
GIOLibrary.INSTANCE.g_type_init();
@ -18,9 +22,9 @@ public class GVFS {
return gvfs;
}
public static File getPathForURI(URI uri) {
Pointer gfile = GIOLibrary.INSTANCE.g_vfs_get_file_for_uri(
getDefaultVFS(), uri.toString());
Pointer gfile = GIOLibrary.INSTANCE.g_vfs_get_file_for_uri(getDefaultVFS(), uri.toString());
Pointer chars = GIOLibrary.INSTANCE.g_file_get_path(gfile);
try {
@ -34,6 +38,7 @@ public class GVFS {
}
}
public static boolean isSupported() {
return Platform.isLinux() || Platform.isFreeBSD();
}

View File

@ -1,5 +1,7 @@
package net.sourceforge.filebot.ui.transfer;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
@ -18,10 +20,12 @@ import java.util.logging.Logger;
import net.sourceforge.filebot.gio.GVFS;
public class FileTransferable implements Transferable {
public static final DataFlavor uriListFlavor = createUriListFlavor();
private static DataFlavor createUriListFlavor() {
try {
return new DataFlavor("text/uri-list;class=java.nio.CharBuffer");
@ -33,17 +37,19 @@ public class FileTransferable implements Transferable {
private final File[] files;
public FileTransferable(File... files) {
this.files = files;
}
public FileTransferable(Collection<File> files) {
this.files = files.toArray(new File[0]);
}
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException {
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (flavor.isFlavorJavaFileListType())
return Arrays.asList(files);
else if (flavor.equals(uriListFlavor))
@ -52,6 +58,7 @@ public class FileTransferable implements Transferable {
throw new UnsupportedFlavorException(flavor);
}
/**
* @return line separated list of file URIs
*/
@ -67,33 +74,34 @@ public class FileTransferable implements Transferable {
return sb.toString();
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.javaFileListFlavor, uriListFlavor };
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return isFileListFlavor(flavor);
}
public static boolean isFileListFlavor(DataFlavor flavor) {
return flavor.isFlavorJavaFileListType()
|| flavor.equals(uriListFlavor);
return flavor.isFlavorJavaFileListType() || flavor.equals(uriListFlavor);
}
public static boolean hasFileListFlavor(Transferable tr) {
return tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
|| tr.isDataFlavorSupported(FileTransferable.uriListFlavor);
return tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || tr.isDataFlavorSupported(FileTransferable.uriListFlavor);
}
@SuppressWarnings("unchecked")
public static List<File> getFilesFromTransferable(Transferable tr)
throws IOException, UnsupportedFlavorException {
public static List<File> getFilesFromTransferable(Transferable tr) throws IOException, UnsupportedFlavorException {
if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) {
// file URI list flavor (Linux)
Readable transferData = (Readable) tr
.getTransferData(FileTransferable.uriListFlavor);
Readable transferData = (Readable) tr.getTransferData(FileTransferable.uriListFlavor);
Scanner scanner = new Scanner(transferData);
List<File> files = new ArrayList<File>();
@ -124,18 +132,15 @@ public class FileTransferable implements Transferable {
}
files.add(file);
} catch (Exception e) {
// URISyntaxException, IllegalArgumentException,
// FileNotFoundException
Logger.getLogger(FileTransferable.class.getName()).log(
Level.WARNING, "Invalid file uri: " + line);
} catch (Throwable e) {
// URISyntaxException, IllegalArgumentException, FileNotFoundException, LinkageError, etc
Logger.getLogger(FileTransferable.class.getName()).log(Level.WARNING, "Invalid file URI: " + line, e);
}
return files;
}
} else if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
// file list flavor
return (List<File>) tr
.getTransferData(DataFlavor.javaFileListFlavor);
return (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
}
// cannot get files from transferable