From eaea85ed4a2f8e442b713b50d89d5df35322c746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 26 Mar 2009 12:10:05 +0100 Subject: [PATCH] xdgmime: Use GIOs g_content_type_guess() if possible --- configure.ac | 6 +++++ gst/xdgmime/Makefile.am | 16 ++++++++---- gst/xdgmime/gstxdgmime.c | 56 +++++++++++++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/configure.ac b/configure.ac index 77eacfb225..6442780872 100644 --- a/configure.ac +++ b/configure.ac @@ -316,6 +316,12 @@ dnl disable experimental plug-ins #if test "x$BUILD_EXPERIMENTAL" != "xyes"; then #fi +# For xdgmime, to use g_content_type_guess() +PKG_CHECK_MODULES(GIO, gio-2.0 >= 2.16, HAVE_GIO=yes, HAVE_GIO=no) +AM_CONDITIONAL(HAVE_GIO, test "x$HAVE_GIO" = "xyes") +AC_SUBST(GIO_CFLAGS) +AC_SUBST(GIO_LIBS) + dnl disable gst plugins we might not be able to build on this dnl platform: librfb (ugly but minimally invasive) dnl FIXME: maybe move to sys, or make work with winsock2 diff --git a/gst/xdgmime/Makefile.am b/gst/xdgmime/Makefile.am index 0c1c75427a..960747e7be 100644 --- a/gst/xdgmime/Makefile.am +++ b/gst/xdgmime/Makefile.am @@ -1,7 +1,6 @@ plugin_LTLIBRARIES = libgstxdgmime.la -libgstxdgmime_la_SOURCES = gstxdgmime.c \ - xdgmime/xdgmimealias.c \ +xdgmime_sources = xdgmime/xdgmimealias.c \ xdgmime/xdgmime.c \ xdgmime/xdgmimecache.c \ xdgmime/xdgmimeglob.c \ @@ -9,8 +8,15 @@ libgstxdgmime_la_SOURCES = gstxdgmime.c \ xdgmime/xdgmimeint.c \ xdgmime/xdgmimemagic.c \ xdgmime/xdgmimeparent.c -libgstxdgmime_la_CFLAGS = $(GST_CFLAGS) -DXDG_PREFIX=gst_xdg_mime -libgstxdgmime_la_LIBADD = $(GST_LIBS) $(XDG_LIBS) + +if HAVE_GIO +libgstxdgmime_la_SOURCES = gstxdgmime.c +else +libgstxdgmime_la_SOURCES = gstxdgmime.c $(xdgmime_sources) +endif + +libgstxdgmime_la_CFLAGS = $(GIO_CFLAGS) $(GST_CFLAGS) -DXDG_PREFIX=gst_xdg_mime +libgstxdgmime_la_LIBADD = $(GIO_LIBS) $(GST_LIBS) $(XDG_LIBS) libgstxdgmime_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) libgstxdgmime_la_LIBTOOLFLAGS = --tag=disable-static @@ -23,4 +29,4 @@ noinst_HEADERS = xdgmime/xdgmimealias.h \ xdgmime/xdgmimemagic.h \ xdgmime/xdgmimeparent.h -EXTRA_DIST = +EXTRA_DIST = $(xdgmime_sources) diff --git a/gst/xdgmime/gstxdgmime.c b/gst/xdgmime/gstxdgmime.c index 74253bfaeb..cd20764a67 100644 --- a/gst/xdgmime/gstxdgmime.c +++ b/gst/xdgmime/gstxdgmime.c @@ -26,15 +26,18 @@ GST_DEBUG_CATEGORY (xdgmime_debug); #define GST_CAT_DEFAULT xdgmime_debug +#if GLIB_CHECK_VERSION (2, 16, 0) +#include +#else G_LOCK_DEFINE_STATIC (xdg_lock); +#endif static void xdgmime_typefind (GstTypeFind * find, gpointer user_data) { - const gchar *mimetype; + gchar *mimetype; gsize length = 16384; guint64 tf_length; - gint prio; guint8 *data; if ((tf_length = gst_type_find_get_length (find)) > 0) @@ -43,18 +46,45 @@ xdgmime_typefind (GstTypeFind * find, gpointer user_data) if ((data = gst_type_find_peek (find, 0, length)) == NULL) return; - /* FIXME: xdg-mime is not thread-safe as it stores the cache globally - * and updates it from every call if changes were done without - * any locking - */ - G_LOCK (xdg_lock); - mimetype = xdg_mime_get_mime_type_for_data (data, length, &prio); - G_UNLOCK (xdg_lock); +#if GLIB_CHECK_VERSION (2, 16, 0) + { + gchar *tmp; - if (mimetype == NULL || g_str_equal (mimetype, XDG_MIME_TYPE_UNKNOWN)) - return; + tmp = g_content_type_guess (NULL, data, length, NULL); + if (tmp == NULL || g_content_type_is_unknown (tmp)) { + g_free (tmp); + return; + } - GST_DEBUG ("Got mimetype '%s' with prio %d", mimetype, prio); + mimetype = g_content_type_get_mime_type (tmp); + g_free (tmp); + + if (mimetype == NULL) + return; + + GST_DEBUG ("Got mimetype '%s'", mimetype); + } +#else + { + const gchar *tmp; + gint prio; + + /* FIXME: xdg-mime is not thread-safe as it stores the cache globally + * and updates it from every call if changes were done without + * any locking + */ + G_LOCK (xdg_lock); + tmp = xdg_mime_get_mime_type_for_data (data, length, &prio); + G_UNLOCK (xdg_lock); + + if (tmp == NULL || g_str_equal (tmp, XDG_MIME_TYPE_UNKNOWN)) + return; + + GST_DEBUG ("Got mimetype '%s' with prio %d", tmp, prio); + + mimetype = g_strdup (tmp); + } +#endif /* Ignore audio/video types: * - our own typefinders in -base are likely to be better at this @@ -67,6 +97,7 @@ xdgmime_typefind (GstTypeFind * find, gpointer user_data) if (g_str_has_prefix (mimetype, "audio/") || g_str_has_prefix (mimetype, "video/")) { GST_LOG ("Ignoring audio/video mime type"); + g_free (mimetype); return; } @@ -75,6 +106,7 @@ xdgmime_typefind (GstTypeFind * find, gpointer user_data) * uncertain results of our typefinders, but not more than that. */ GST_LOG ("Suggesting '%s' with probability POSSIBLE", mimetype); gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE, mimetype, NULL); + g_free (mimetype); } static gboolean