GnomeFileTypeDetector.c revision 2362
0N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "jlong.h"
0N/A
0N/A#include <stdlib.h>
0N/A#include <dlfcn.h>
0N/A#include <link.h>
0N/A
0N/A#ifdef __solaris__
0N/A#include <strings.h>
0N/A#endif
0N/A
0N/A#ifdef __linux__
0N/A#include <string.h>
0N/A#endif
0N/A
0N/A/* Definitions for GIO */
0N/A
0N/A#define G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "standard::content-type"
0N/A
0N/Atypedef void* gpointer;
0N/Atypedef struct _GFile GFile;
0N/Atypedef struct _GFileInfo GFileInfo;
0N/Atypedef struct _GCancellable GCancellable;
0N/Atypedef struct _GError GError;
0N/A
0N/Atypedef enum {
0N/A G_FILE_QUERY_INFO_NONE = 0
0N/A} GFileQueryInfoFlags;
0N/A
0N/Atypedef void (*g_type_init_func)(void);
0N/Atypedef void (*g_object_unref_func)(gpointer object);
0N/Atypedef GFile* (*g_file_new_for_path_func)(const char* path);
0N/Atypedef GFileInfo* (*g_file_query_info_func)(GFile *file,
0N/A const char *attributes, GFileQueryInfoFlags flags,
0N/A GCancellable *cancellable, GError **error);
0N/Atypedef char* (*g_file_info_get_content_type_func)(GFileInfo *info);
0N/A
0N/Astatic g_type_init_func g_type_init;
0N/Astatic g_object_unref_func g_object_unref;
0N/Astatic g_file_new_for_path_func g_file_new_for_path;
0N/Astatic g_file_query_info_func g_file_query_info;
0N/Astatic g_file_info_get_content_type_func g_file_info_get_content_type;
0N/A
0N/A
0N/A/* Definitions for GNOME VFS */
0N/A
0N/Atypedef int gboolean;
0N/A
0N/Atypedef gboolean (*gnome_vfs_init_function)(void);
0N/Atypedef const char* (*gnome_vfs_mime_type_from_name_function)
0N/A (const char* filename);
0N/A
0N/Astatic gnome_vfs_init_function gnome_vfs_init;
0N/Astatic gnome_vfs_mime_type_from_name_function gnome_vfs_mime_type_from_name;
0N/A
0N/A
0N/A#include "sun_nio_fs_GnomeFileTypeDetector.h"
0N/A
0N/A
0N/AJNIEXPORT jboolean JNICALL
0N/AJava_sun_nio_fs_GnomeFileTypeDetector_initializeGio
0N/A (JNIEnv* env, jclass this)
0N/A{
0N/A void* gio_handle;
0N/A
0N/A gio_handle = dlopen("libgio-2.0.so", RTLD_LAZY);
0N/A if (gio_handle == NULL) {
0N/A gio_handle = dlopen("libgio-2.0.so.0", RTLD_LAZY);
0N/A if (gio_handle == NULL) {
0N/A return JNI_FALSE;
0N/A }
0N/A }
0N/A
0N/A g_type_init = (g_type_init_func)dlsym(gio_handle, "g_type_init");
0N/A (*g_type_init)();
0N/A
0N/A g_object_unref = (g_object_unref_func)dlsym(gio_handle, "g_object_unref");
0N/A
0N/A g_file_new_for_path =
0N/A (g_file_new_for_path_func)dlsym(gio_handle, "g_file_new_for_path");
0N/A
0N/A g_file_query_info =
0N/A (g_file_query_info_func)dlsym(gio_handle, "g_file_query_info");
0N/A
0N/A g_file_info_get_content_type = (g_file_info_get_content_type_func)
0N/A dlsym(gio_handle, "g_file_info_get_content_type");
0N/A
0N/A
0N/A if (g_type_init == NULL ||
0N/A g_object_unref == NULL ||
0N/A g_file_new_for_path == NULL ||
0N/A g_file_query_info == NULL ||
0N/A g_file_info_get_content_type == NULL)
0N/A {
0N/A dlclose(gio_handle);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A (*g_type_init)();
0N/A return JNI_TRUE;
0N/A}
0N/A
0N/AJNIEXPORT jbyteArray JNICALL
0N/AJava_sun_nio_fs_GnomeFileTypeDetector_probeUsingGio
0N/A (JNIEnv* env, jclass this, jlong pathAddress)
0N/A{
0N/A char* path = (char*)jlong_to_ptr(pathAddress);
0N/A GFile* gfile;
0N/A GFileInfo* gfileinfo;
0N/A jbyteArray result = NULL;
0N/A
0N/A gfile = (*g_file_new_for_path)(path);
0N/A gfileinfo = (*g_file_query_info)(gfile, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
0N/A G_FILE_QUERY_INFO_NONE, NULL, NULL);
0N/A if (gfileinfo != NULL) {
0N/A const char* mime = (*g_file_info_get_content_type)(gfileinfo);
0N/A if (mime != NULL) {
0N/A jsize len = strlen(mime);
0N/A result = (*env)->NewByteArray(env, len);
0N/A if (result != NULL) {
0N/A (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
0N/A }
0N/A }
0N/A (*g_object_unref)(gfileinfo);
0N/A }
0N/A (*g_object_unref)(gfile);
0N/A
0N/A return result;
0N/A}
0N/A
0N/AJNIEXPORT jboolean JNICALL
0N/AJava_sun_nio_fs_GnomeFileTypeDetector_initializeGnomeVfs
0N/A (JNIEnv* env, jclass this)
0N/A{
0N/A void* vfs_handle;
0N/A
0N/A vfs_handle = dlopen("libgnomevfs-2.so", RTLD_LAZY);
0N/A if (vfs_handle == NULL) {
0N/A vfs_handle = dlopen("libgnomevfs-2.so.0", RTLD_LAZY);
0N/A }
0N/A if (vfs_handle == NULL) {
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A gnome_vfs_init = (gnome_vfs_init_function)dlsym(vfs_handle, "gnome_vfs_init");
0N/A gnome_vfs_mime_type_from_name = (gnome_vfs_mime_type_from_name_function)
0N/A dlsym(vfs_handle, "gnome_vfs_mime_type_from_name");
0N/A
0N/A if (gnome_vfs_init == NULL ||
0N/A gnome_vfs_mime_type_from_name == NULL)
0N/A {
0N/A dlclose(vfs_handle);
0N/A return JNI_FALSE;
0N/A }
0N/A
0N/A (*gnome_vfs_init)();
0N/A return JNI_TRUE;
0N/A}
0N/A
0N/AJNIEXPORT jbyteArray JNICALL
0N/AJava_sun_nio_fs_GnomeFileTypeDetector_probeUsingGnomeVfs
0N/A (JNIEnv* env, jclass this, jlong pathAddress)
0N/A{
0N/A char* path = (char*)jlong_to_ptr(pathAddress);
0N/A const char* mime = (*gnome_vfs_mime_type_from_name)(path);
0N/A
0N/A if (mime == NULL) {
0N/A return NULL;
0N/A } else {
0N/A jbyteArray result;
0N/A jsize len = strlen(mime);
0N/A result = (*env)->NewByteArray(env, len);
0N/A if (result != NULL) {
0N/A (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
0N/A }
0N/A return result;
0N/A }
0N/A}
0N/A