diff --git a/ChangeLog b/ChangeLog index 9a816158aa04867fdc17b9ece079c64b50f7efac..faf3feafa85b347d5124bdaf6fb588fb685647a1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2005-08-06 Benedikt Meurer <benny@xfce.org> + + * configure.in.in: Check for fnmatch.h and sys/mman.h. Add checks for + working mmap and posix_madvise. + * configure.in.in, thunar-vfs/xdgmime/, thunar-vfs/Makefile.am: Drop + the xdgmime library. + * thunar-vfs/thunar-vfs-mime-cache.{c,h}, + thunar-vfs/thunar-vfs-mime-database.{c,h}, + thunar-vfs/thunar-vfs-mime-info.{c,h}, + thunar-vfs/thunar-vfs-mime-legacy.{c,h}, + thunar-vfs/thunar-vfs-mime-provider.{c,h}, + thunar-vfs/thunar-vfs-mime.{c,h}: Import thread-safe replacement for + the xdgmime library. Works only with very recent shared-mime-info + right now. + * thunar-vfs/Makefile.am: Add new files to the build framework. + * thunar-vfs/thunar-vfs-job.c, thunar-vfs/thunar-vfs-mime.c, + thunar-vfs/thunar-vfs.c, thunar-vfs/thunar-vfs.h, thunar/main.c: Add + ability to shutdown the VFS library. + 2005-08-04 Benedikt Meurer <benny@xfce.org> * configure.in.in: Add "r" in front of the revision for the version diff --git a/configure.in.in b/configure.in.in index be2da65afc35599a4907ac6eaa769299cf50ee65..0a39a5ccca2c4f19a93d58a6d8f1151d49980332 100644 --- a/configure.in.in +++ b/configure.in.in @@ -53,14 +53,17 @@ AC_PROG_LIBTOOL() dnl ********************************** dnl *** Check for standard headers *** dnl ********************************** -AC_CHECK_HEADERS([dirent.h errno.h fcntl.h fstab.h grp.h locale.h \ - math.h memory.h pwd.h stdlib.h string.h sys/cdio.h \ +AC_CHECK_HEADERS([dirent.h errno.h fcntl.h fnmatch.h fstab.h grp.h locale.h \ + math.h memory.h pwd.h stdlib.h string.h \ + sys/cdio.h sys/mman.h \ sys/mount.h sys/stat.h sys/time.h sys/param.h time.h]) dnl ************************************ dnl *** Check for standard functions *** dnl ************************************ -AC_CHECK_FUNCS([lchmod localtime_r readdir_r setgroupent setpassent]) +AC_FUNC_MMAP() +AC_CHECK_FUNCS([lchmod localtime_r posix_madvise readdir_r \ + setgroupent setpassent]) dnl ****************************** dnl *** Check for i18n support *** @@ -158,6 +161,5 @@ tests/Makefile tests/data/Makefile thunar/Makefile thunar-vfs/Makefile -thunar-vfs/xdgmime/Makefile thunarx/Makefile ]) diff --git a/thunar-vfs/Makefile.am b/thunar-vfs/Makefile.am index 30ce78fec266ea1c00943528ff0271b3f60911a2..d25629fd278d8b155947287d694ea996f2451a95 100644 --- a/thunar-vfs/Makefile.am +++ b/thunar-vfs/Makefile.am @@ -1,14 +1,10 @@ # $Id$ -SUBDIRS = \ - xdgmime - INCLUDES = \ -I$(top_srcdir) \ -DEXO_API_SUBJECT_TO_CHANGE \ -DEXO_DISABLE_DEPRECATED \ - -DG_LOG_DOMAIN=\"thunar-vfs\" \ - -DXDG_PREFIX=_thunar_vfs_xdg + -DG_LOG_DOMAIN=\"thunar-vfs\" noinst_LTLIBRARIES = \ libthunar-vfs.la @@ -25,6 +21,8 @@ libthunar_vfs_headers = \ thunar-vfs-job.h \ thunar-vfs-jobs.h \ thunar-vfs-mime.h \ + thunar-vfs-mime-database.h \ + thunar-vfs-mime-info.h \ thunar-vfs-monitor.h \ thunar-vfs-trash.h \ thunar-vfs-user.h \ @@ -43,8 +41,16 @@ libthunar_vfs_la_SOURCES = \ thunar-vfs-listdir-job.c \ thunar-vfs-listdir-job.h \ thunar-vfs-mime.c \ + thunar-vfs-mime-cache.c \ + thunar-vfs-mime-cache.h \ + thunar-vfs-mime-database.c \ + thunar-vfs-mime-info.c \ + thunar-vfs-mime-legacy.c \ + thunar-vfs-mime-legacy.h \ thunar-vfs-mime-parser.h \ thunar-vfs-mime-parser.c \ + thunar-vfs-mime-provider.c \ + thunar-vfs-mime-provider.h \ thunar-vfs-monitor.c \ thunar-vfs-sysdep.c \ thunar-vfs-sysdep.h \ @@ -69,11 +75,7 @@ libthunar_vfs_la_CFLAGS = \ libthunar_vfs_la_LDFLAGS = \ -no-undefined -libthunar_vfs_la_DEPENDENCIES = \ - $(top_builddir)/thunar-vfs/xdgmime/libxdgmime.la - libthunar_vfs_la_LIBADD = \ - $(top_builddir)/thunar-vfs/xdgmime/libxdgmime.la \ $(EXO_LIBS) \ $(GTHREADS_LIBS) \ $(LIBFAM_LIBS) diff --git a/thunar-vfs/thunar-vfs-job.c b/thunar-vfs/thunar-vfs-job.c index 7da0933419de0372d24f538e9c74007ea616462a..20137f1c9f2cbdbddf1aad0aca1a41444d8b5440 100644 --- a/thunar-vfs/thunar-vfs-job.c +++ b/thunar-vfs/thunar-vfs-job.c @@ -389,4 +389,18 @@ _thunar_vfs_job_init (void) +/** + * _thunar_vfs_job_shutdown: + * + * Shuts down the jobs module of the ThunarVFS + * library. + **/ +void +_thunar_vfs_job_shutdown (void) +{ + g_return_if_fail (job_pool != NULL); + + g_thread_pool_free (job_pool, FALSE, TRUE); + job_pool = NULL; +} diff --git a/thunar-vfs/thunar-vfs-mime-cache.c b/thunar-vfs/thunar-vfs-mime-cache.c new file mode 100644 index 0000000000000000000000000000000000000000..a0d7070a2519e4d0321d3e379a19d57575568ad1 --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-cache.c @@ -0,0 +1,501 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Based on code initially written by Matthias Clasen <mclasen@redhat.com> + * for the xdgmime library. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#ifdef HAVE_SYS_MMAN_H +#include <sys/mman.h> +#endif +#ifdef HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif + +#ifdef HAVE_FCNTL_H +#include <fcntl.h> +#endif +#ifdef HAVE_FNMATCH_H +#include <fnmatch.h> +#endif +#ifdef HAVE_MEMORY_H +#include <memory.h> +#endif +#ifdef HAVE_STRING_H +#include <string.h> +#endif +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif + +#include <thunar-vfs/thunar-vfs-mime-cache.h> + +#if GLIB_CHECK_VERSION(2,6,0) +#include <glib/gstdio.h> +#else +#define g_open(path, flags, mode) (open ((path), (flags), (mode))) +#endif + + + +#define CACHE_MAJOR_VERSION (1) +#define CACHE_MINOR_VERSION (0) + +#define CACHE_READ16(cache, offset) (GUINT16_FROM_BE (*((guint16 *) ((cache) + (offset))))) +#define CACHE_READ32(cache, offset) (GUINT32_FROM_BE (*((guint32 *) ((cache) + (offset))))) + + + +static void thunar_vfs_mime_cache_class_init (ThunarVfsMimeCacheClass *klass); +static void thunar_vfs_mime_cache_finalize (ExoObject *object); +static const gchar *thunar_vfs_mime_cache_lookup_data (ThunarVfsMimeProvider *provider, + gconstpointer data, + gsize length, + gint *priority); +static const gchar *thunar_vfs_mime_cache_lookup_literal (ThunarVfsMimeProvider *provider, + const gchar *filename); +static const gchar *thunar_vfs_mime_cache_lookup_suffix (ThunarVfsMimeProvider *provider, + const gchar *suffix, + gboolean ignore_case); +static const gchar *thunar_vfs_mime_cache_lookup_glob (ThunarVfsMimeProvider *provider, + const gchar *filename); +static GList *thunar_vfs_mime_cache_get_stop_characters (ThunarVfsMimeProvider *provider); +static gsize thunar_vfs_mime_cache_get_max_buffer_extents (ThunarVfsMimeProvider *provider); + + + +struct _ThunarVfsMimeCacheClass +{ + ThunarVfsMimeProviderClass __parent__; +}; + +struct _ThunarVfsMimeCache +{ + ThunarVfsMimeProvider __parent__; + + gchar *buffer; + gsize bufsize; +}; + + + +static ExoObjectClass *thunar_vfs_mime_cache_parent_class; + + + +GType +thunar_vfs_mime_cache_get_type (void) +{ + static GType type = G_TYPE_INVALID; + + if (G_UNLIKELY (type == G_TYPE_INVALID)) + { + static const GTypeInfo info = + { + sizeof (ThunarVfsMimeCacheClass), + NULL, + NULL, + (GClassInitFunc) thunar_vfs_mime_cache_class_init, + NULL, + NULL, + sizeof (ThunarVfsMimeCache), + 0, + NULL, + NULL, + }; + + type = g_type_register_static (THUNAR_VFS_TYPE_MIME_PROVIDER, + "ThunarVfsMimeCache", &info, 0); + } + + return type; +} + + + +static void +thunar_vfs_mime_cache_class_init (ThunarVfsMimeCacheClass *klass) +{ + ThunarVfsMimeProviderClass *thunarvfs_mime_provider_class; + ExoObjectClass *exoobject_class; + + thunar_vfs_mime_cache_parent_class = g_type_class_peek_parent (klass); + + exoobject_class = EXO_OBJECT_CLASS (klass); + exoobject_class->finalize = thunar_vfs_mime_cache_finalize; + + thunarvfs_mime_provider_class = THUNAR_VFS_MIME_PROVIDER_CLASS (klass); + thunarvfs_mime_provider_class->lookup_data = thunar_vfs_mime_cache_lookup_data; + thunarvfs_mime_provider_class->lookup_literal = thunar_vfs_mime_cache_lookup_literal; + thunarvfs_mime_provider_class->lookup_suffix = thunar_vfs_mime_cache_lookup_suffix; + thunarvfs_mime_provider_class->lookup_glob = thunar_vfs_mime_cache_lookup_glob; + thunarvfs_mime_provider_class->get_stop_characters = thunar_vfs_mime_cache_get_stop_characters; + thunarvfs_mime_provider_class->get_max_buffer_extents = thunar_vfs_mime_cache_get_max_buffer_extents; +} + + + +static void +thunar_vfs_mime_cache_finalize (ExoObject *object) +{ + ThunarVfsMimeCache *cache = THUNAR_VFS_MIME_CACHE (object); + +#ifdef HAVE_MMAP + if (G_LIKELY (cache->buffer != NULL)) + munmap (cache->buffer, cache->bufsize); +#endif + + (*EXO_OBJECT_CLASS (thunar_vfs_mime_cache_parent_class)->finalize) (object); +} + + + +static gboolean +cache_magic_matchlet_compare_to_data (const gchar *buffer, + guint32 offset, + gconstpointer data, + gsize length) +{ + gboolean valid_matchlet; + guint32 range_start = CACHE_READ32 (buffer, offset); + guint32 range_length = CACHE_READ32 (buffer, offset + 4); + guint32 data_length = CACHE_READ32 (buffer, offset + 12); + guint32 data_offset = CACHE_READ32 (buffer, offset + 16); + guint32 mask_offset = CACHE_READ32 (buffer, offset + 20); + guint32 i, j; + + for (i = range_start; i <= range_start + range_length; i++) + { + valid_matchlet = TRUE; + + if (i + data_length > length) + return FALSE; + + if (mask_offset) + { + for (j = 0; j < data_length; j++) + if ((buffer[data_offset + j] & buffer[mask_offset + j]) != ((((gchar *) data)[j + i]) & buffer[mask_offset + j])) + { + valid_matchlet = FALSE; + break; + } + } + else + { + for (j = 0; j < data_length; j++) + if (buffer[data_offset + j] != ((gchar *) data)[j + i]) + { + valid_matchlet = FALSE; + break; + } + } + + if (valid_matchlet) + return TRUE; + } + + return FALSE; +} + + + +static gboolean +cache_magic_matchlet_compare (const gchar *buffer, + guint32 offset, + gconstpointer data, + gsize length) +{ + guint32 n_children = CACHE_READ32 (buffer, offset + 24); + guint32 child_offset = CACHE_READ32 (buffer, offset + 28); + guint32 i; + + if (cache_magic_matchlet_compare_to_data (buffer, offset, data, length)) + { + if (n_children == 0) + return TRUE; + + for (i = 0; i < n_children; i++) + if (cache_magic_matchlet_compare (buffer, child_offset + 32 * i, data, length)) + return TRUE; + } + + return FALSE; +} + + + +static const gchar* +thunar_vfs_mime_cache_lookup_data (ThunarVfsMimeProvider *provider, + gconstpointer data, + gsize length, + gint *priority) +{ + const gchar *buffer = THUNAR_VFS_MIME_CACHE (provider)->buffer; + guint32 matchlet_offset; + guint32 offset; + guint32 n, m; + + offset = CACHE_READ32 (buffer, 24); + n = CACHE_READ32 (buffer, offset); + offset = CACHE_READ32 (buffer, offset + 8); + + for (; n-- > 0; offset += 16) + { + matchlet_offset = CACHE_READ32 (buffer, offset + 12); + for (m = CACHE_READ32 (buffer, offset + 8); m-- > 0; matchlet_offset += 32) + if (cache_magic_matchlet_compare (buffer, matchlet_offset, data, length)) + { + if (G_LIKELY (priority != NULL)) + *priority = (gint) CACHE_READ32 (buffer, offset); + return buffer + CACHE_READ32 (buffer, offset + 4); + } + } + + return NULL; +} + + + +static const gchar* +thunar_vfs_mime_cache_lookup_literal (ThunarVfsMimeProvider *provider, + const gchar *filename) +{ + const gchar *buffer = THUNAR_VFS_MIME_CACHE (provider)->buffer; + guint32 list_offset = CACHE_READ32 (buffer, 12); + guint32 n_entries = CACHE_READ32 (buffer, list_offset); + guint32 offset; + gint min; + gint mid; + gint max; + gint cmp; + + for (min = 0, max = (gint) n_entries - 1; max >= min; ) + { + mid = (min + max) / 2; + + offset = CACHE_READ32 (buffer, list_offset + 4 + 8 * mid); + cmp = strcmp (buffer + offset, filename); + + if (cmp < 0) + min = mid + 1; + else if (cmp > 0) + max = mid - 1; + else + return buffer + offset + 4; + } + + return NULL; +} + + + +static const gchar* +cache_node_lookup_suffix (const gchar *buffer, + guint32 n_entries, + guint32 offset, + const gchar *suffix, + gboolean ignore_case) +{ + gunichar character; + gunichar match_char; + gint min, max, mid; + + character = g_utf8_get_char (suffix); + if (ignore_case) + character = g_unichar_tolower (character); + + for (min = 0, max = (gint) n_entries - 1; max >= min; ) + { + mid = (min + max) / 2; + + match_char = (gunichar) CACHE_READ32 (buffer, offset + 16 * mid); + + if (match_char < character) + min = mid + 1; + else if (match_char > character) + max = mid - 1; + else + { + suffix = g_utf8_next_char (suffix); + if (*suffix == '\0') + { + return buffer + CACHE_READ32 (buffer, offset + 16 * mid + 4); + } + else + { + return cache_node_lookup_suffix (buffer, CACHE_READ32 (buffer, offset + 16 * mid + 8), + CACHE_READ32 (buffer, offset + 16 * mid + 12), + suffix, ignore_case); + } + } + } + + return NULL; +} + + + +static const gchar* +thunar_vfs_mime_cache_lookup_suffix (ThunarVfsMimeProvider *provider, + const gchar *suffix, + gboolean ignore_case) +{ + const gchar *buffer = THUNAR_VFS_MIME_CACHE (provider)->buffer; + guint32 offset = CACHE_READ32 (buffer, 16); + + g_return_val_if_fail (g_utf8_validate (suffix, -1, NULL), NULL); + + return cache_node_lookup_suffix (buffer, CACHE_READ32 (buffer, offset), + CACHE_READ32 (buffer, offset + 4), + suffix, ignore_case); +} + + + +static const gchar* +thunar_vfs_mime_cache_lookup_glob (ThunarVfsMimeProvider *provider, + const gchar *filename) +{ + const gchar *buffer = THUNAR_VFS_MIME_CACHE (provider)->buffer; + guint32 offset = CACHE_READ32 (buffer, 20); + guint32 n = CACHE_READ32 (buffer, offset); + + for (offset = CACHE_READ32 (buffer, offset + 4); n-- > 0; offset += 8) + if (fnmatch (buffer + offset, filename, 0) == 0) + return buffer + offset + 4; + + return NULL; +} + + + +static GList* +thunar_vfs_mime_cache_get_stop_characters (ThunarVfsMimeProvider *provider) +{ + const gchar *buffer = THUNAR_VFS_MIME_CACHE (provider)->buffer; + gunichar character; + guint32 offset = CACHE_READ32 (buffer, 16); + guint32 n = CACHE_READ32 (buffer, offset); + GList *list = NULL; + + for (offset = CACHE_READ32 (buffer, offset + 4); n-- > 0; offset += 16) + { + /* query the suffix start character */ + character = (gunichar) CACHE_READ32 (buffer, offset); + if (G_UNLIKELY (character >= 128)) + continue; + + /* prepend the character to the list (if not already present) */ + if (G_UNLIKELY (g_list_find (list, GUINT_TO_POINTER (character)) == NULL)) + list = g_list_prepend (list, GUINT_TO_POINTER (character)); + } + + return list; +} + + + +static gsize +thunar_vfs_mime_cache_get_max_buffer_extents (ThunarVfsMimeProvider *provider) +{ + const gchar *buffer = THUNAR_VFS_MIME_CACHE (provider)->buffer; + + /* get the MAX_EXTENTS entry from the MagicList */ + return CACHE_READ32 (buffer, CACHE_READ32 (buffer, 24) + 4); +} + + + +/** + * thunar_vfs_mime_cache_new: + * @directory : the mime base directory. + * + * Creates a new #ThunarVfsMimeCache for @directory. Returns + * %NULL if for some reason, @directory could not be opened + * as a #ThunarVfsMimeCache. + * + * The caller is responsible to call #exo_object_unref() + * on the returned instance. + * + * Return value: a #ThunarVfsMimeCache for @directory or %NULL. + **/ +ThunarVfsMimeProvider* +thunar_vfs_mime_cache_new (const gchar *directory) +{ + ThunarVfsMimeCache *cache = NULL; + +#ifdef HAVE_MMAP + struct stat stat; + gchar *buffer; + gchar *path; + gint fd; + + /* try to open the mime.cache file */ + path = g_build_filename (directory, "mime.cache", NULL); + fd = g_open (path, O_RDONLY, 0); + g_free (path); + + if (G_UNLIKELY (fd < 0)) + return NULL; + + /* stat the file to get proper size info */ + if (fstat (fd, &stat) < 0 || stat.st_size < 4) + goto done; + + /* try to map the file into memory */ + buffer = (gchar *) mmap (NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0); + if (G_UNLIKELY (buffer == MAP_FAILED)) + goto done; + + /* check that we actually support the file version */ + if (CACHE_READ16 (buffer, 0) != CACHE_MAJOR_VERSION || CACHE_READ16 (buffer, 2) != CACHE_MINOR_VERSION) + { + munmap (buffer, stat.st_size); + goto done; + } + + /* allocate a new cache provider */ + cache = exo_object_new (THUNAR_VFS_TYPE_MIME_CACHE); + cache->buffer = buffer; + cache->bufsize = stat.st_size; + + /* tell the system that we'll use this buffer quite often */ +#ifdef HAVE_POSIX_MADVISE + posix_madvise (buffer, stat.st_size, POSIX_MADV_WILLNEED); +#endif + + /* cleanup */ +done: + if (G_LIKELY (fd >= 0)) + close (fd); +#endif /* !HAVE_MMAP */ + + return (ThunarVfsMimeProvider *) cache; +} + + + + diff --git a/thunar-vfs/thunar-vfs-mime-cache.h b/thunar-vfs/thunar-vfs-mime-cache.h new file mode 100644 index 0000000000000000000000000000000000000000..0c68c519b3e42ceb7b0544f31a1b741c341e62bc --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-cache.h @@ -0,0 +1,44 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __THUNAR_VFS_MIME_CACHE_H__ +#define __THUNAR_VFS_MIME_CACHE_H__ + +#include <thunar-vfs/thunar-vfs-mime-provider.h> + +G_BEGIN_DECLS; + +typedef struct _ThunarVfsMimeCacheClass ThunarVfsMimeCacheClass; +typedef struct _ThunarVfsMimeCache ThunarVfsMimeCache; + +#define THUNAR_VFS_TYPE_MIME_CACHE (thunar_vfs_mime_cache_get_type ()) +#define THUNAR_VFS_MIME_CACHE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNAR_VFS_TYPE_MIME_CACHE, ThunarVfsMimeCache)) +#define THUNAR_VFS_MIME_CACHE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNAR_VFS_TYPE_MIME_CACHE, ThunarVfsMimeCacheClass)) +#define THUNAR_VFS_IS_MIME_CACHE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNAR_VFS_TYPE_MIME_CACHE)) +#define THUNAR_VFS_IS_MIME_CACHE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_VFS_TYPE_MIME_CACHE)) +#define THUNAR_VFS_MIME_CACHE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_VFS_TYPE_MIME_CACHE, ThunarVfsMimeCacheClass)) + +GType thunar_vfs_mime_cache_get_type (void) G_GNUC_CONST; + +ThunarVfsMimeProvider *thunar_vfs_mime_cache_new (const gchar *directory) EXO_GNUC_MALLOC; + +G_END_DECLS; + +#endif /* !__THUNAR_VFS_MIME_CACHE_H__ */ diff --git a/thunar-vfs/thunar-vfs-mime-database.c b/thunar-vfs/thunar-vfs-mime-database.c new file mode 100644 index 0000000000000000000000000000000000000000..59ab81240751305c429dff76692db721103381ea --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-database.c @@ -0,0 +1,624 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Based on code initially written by Matthias Clasen <mclasen@redhat.com> + * for the xdgmime library. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#ifdef HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif + +#ifdef HAVE_FCNTL_H +#include <fcntl.h> +#endif +#ifdef HAVE_MEMORY_H +#include <memory.h> +#endif +#ifdef HAVE_STRING_H +#include <string.h> +#endif +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif + +#include <thunar-vfs/thunar-vfs-mime-cache.h> +#include <thunar-vfs/thunar-vfs-mime-database.h> +#include <thunar-vfs/thunar-vfs-mime-legacy.h> +#include <thunar-vfs/thunar-vfs-monitor.h> + +#if GLIB_CHECK_VERSION(2,6,0) +#include <glib/gstdio.h> +#else +#define g_open(path, flags, mode) (open ((path), (flags), (mode))) +#endif + + + +typedef struct _ThunarVfsMimeProviderData ThunarVfsMimeProviderData; + + + +#define THUNAR_VFS_MIME_PROVIDER_DATA(data) ((ThunarVfsMimeProviderData *) (data)) + + + +static void thunar_vfs_mime_database_class_init (ThunarVfsMimeDatabaseClass *klass); +static void thunar_vfs_mime_database_init (ThunarVfsMimeDatabase *database); +static void thunar_vfs_mime_database_finalize (ExoObject *object); +static ThunarVfsMimeInfo *thunar_vfs_mime_database_get_info_unlocked (ThunarVfsMimeDatabase *database, + const gchar *mime_type); +static ThunarVfsMimeInfo *thunar_vfs_mime_database_get_info_for_name_unlocked (ThunarVfsMimeDatabase *database, + const gchar *name); + + + +struct _ThunarVfsMimeDatabaseClass +{ + ExoObjectClass __parent__; +}; + +struct _ThunarVfsMimeDatabase +{ + ExoObject __parent__; + + GMutex *lock; + + ThunarVfsMonitor *monitor; + + GHashTable *infos; + GList *providers; + + gsize max_buffer_extents; + gchar *stopchars; +}; + +struct _ThunarVfsMimeProviderData +{ + ThunarVfsMonitorHandle *handle; + ThunarVfsMimeProvider *provider; + ThunarVfsURI *uri; +}; + + + +static ThunarVfsMimeDatabase *thunar_vfs_mime_database_shared_instance = NULL; + + + +G_DEFINE_TYPE (ThunarVfsMimeDatabase, thunar_vfs_mime_database, EXO_TYPE_OBJECT); + + + +static void +thunar_vfs_mime_database_class_init (ThunarVfsMimeDatabaseClass *klass) +{ + ExoObjectClass *exoobject_class; + + exoobject_class = EXO_OBJECT_CLASS (klass); + exoobject_class->finalize = thunar_vfs_mime_database_finalize; +} + + + +static void +thunar_vfs_mime_database_init (ThunarVfsMimeDatabase *database) +{ + ThunarVfsMimeProviderData *data; + const gchar *s; + GList *stopchars = NULL; + GList *lp; + gchar *directory; + gchar **basedirs; + gchar *p; + gchar c; + gint n; + + /* create the lock for this object */ + database->lock = g_mutex_new (); + + /* acquire a reference on the file alteration monitor */ + database->monitor = thunar_vfs_monitor_get (); + + /* allocate the hash table for the mime infos */ + database->infos = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) exo_object_unref); + + /* lookup all MIME directories */ + basedirs = xfce_resource_dirs (XFCE_RESOURCE_DATA); + for (n = 0; basedirs[n] != NULL; ++n) + { + /* determine the URI for the MIME directory */ + directory = g_build_filename (basedirs[n], "mime", NULL); + + /* allocate the provider data for the directory */ + data = g_new (ThunarVfsMimeProviderData, 1); + data->handle = NULL; + data->uri = thunar_vfs_uri_new_for_path (directory); + + /* try to allocate a provider for the directory */ + data->provider = thunar_vfs_mime_cache_new (directory); + if (G_UNLIKELY (data->provider == NULL)) + data->provider = thunar_vfs_mime_legacy_new (directory); + + /* add the provider to our list */ + database->providers = g_list_append (database->providers, data); + + /* collect the stopchars for this provider */ + if (G_LIKELY (data->provider != NULL)) + stopchars = g_list_concat (stopchars, thunar_vfs_mime_provider_get_stop_characters (data->provider)); + + /* collect the max buffer extents for this provider */ + if (G_LIKELY (data->provider != NULL)) + database->max_buffer_extents = MAX (database->max_buffer_extents, thunar_vfs_mime_provider_get_max_buffer_extents (data->provider)); + + /* cleanup */ + g_free (directory); + } + g_strfreev (basedirs); + + /* clamp the max buffer extents to [1..256] to make + * sure we don't try insane values. + */ + database->max_buffer_extents = CLAMP (database->max_buffer_extents, 1, 256); + + /* collect the stop characters */ + database->stopchars = g_new (gchar, g_list_length (stopchars) + 1); + for (lp = stopchars, p = database->stopchars; lp != NULL; lp = lp->next) + { + /* check if we have that character already */ + c = (gchar) GPOINTER_TO_UINT (lp->data); + for (s = database->stopchars; s < p; ++s) + if (G_UNLIKELY (*s == c)) + break; + + /* insert the stopchar */ + if (G_LIKELY (s == p)) + *p++ = c; + } + g_list_free (stopchars); + *p = '\0'; +} + + + +static void +thunar_vfs_mime_database_finalize (ExoObject *object) +{ + ThunarVfsMimeDatabase *database = THUNAR_VFS_MIME_DATABASE (object); + GList *lp; + + /* free the providers */ + for (lp = database->providers; lp != NULL; lp = lp->next) + { + if (G_LIKELY (THUNAR_VFS_MIME_PROVIDER_DATA (lp->data)->provider != NULL)) + exo_object_unref (THUNAR_VFS_MIME_PROVIDER_DATA (lp->data)->provider); + exo_object_unref (THUNAR_VFS_MIME_PROVIDER_DATA (lp->data)->uri); + g_free (THUNAR_VFS_MIME_PROVIDER_DATA (lp->data)); + } + g_list_free (database->providers); + + /* free all mime infos */ + g_hash_table_destroy (database->infos); + + /* release the reference on the file alteration monitor */ + g_object_unref (G_OBJECT (database->monitor)); + + /* reset the shared instance pointer, as we're the last one to release it */ + g_atomic_pointer_compare_and_exchange ((gpointer) &thunar_vfs_mime_database_shared_instance, + (gpointer) database, (gpointer) NULL); + + /* release the mutex for this object */ + g_mutex_free (database->lock); + + /* free the stopchars */ + g_free (database->stopchars); + + /* call the parent's finalize method */ + (*EXO_OBJECT_CLASS (thunar_vfs_mime_database_parent_class)->finalize) (object); +} + + + +static ThunarVfsMimeInfo* +thunar_vfs_mime_database_get_info_unlocked (ThunarVfsMimeDatabase *database, + const gchar *mime_type) +{ + ThunarVfsMimeInfo *info; + const gchar *s; + const gchar *t; + const gchar *u; + gchar *v; + guint n; + +again: + /* check if we have a cached version of the mime type */ + info = g_hash_table_lookup (database->infos, mime_type); + if (G_UNLIKELY (info == NULL)) + { + /* count the number of slashes in the mime_type */ + for (n = 0, s = NULL, t = mime_type; *t != '\0'; ++t) + if (G_UNLIKELY (*t == '/')) + { + s = t; + ++n; + } + + /* fallback to 'application/octet-stream' if the type is invalid */ + if (G_UNLIKELY (n != 1)) + { + mime_type = "application/octet-stream"; + goto again; + } + + /* allocate the MIME info instance */ + info = exo_object_new (THUNAR_VFS_TYPE_MIME_INFO); + + /* allocate memory to store both the full name, + * as well as the media type alone. + */ + info->name = g_new (gchar, (t - mime_type) + (s - mime_type) + 2); + + /* copy full name (including the terminator) */ + for (u = mime_type, v = info->name; u <= t; ++u, ++v) + *v = *u; + + /* set the subtype portion */ + info->subtype = info->name + (s - mime_type) + 1; + + /* copy the media portion */ + info->media = v; + for (u = mime_type; u < s; ++u, ++v) + *v = *u; + + /* terminate the media portion */ + *v = '\0'; + + /* insert the mime type into the cache */ + g_hash_table_insert (database->infos, info->name, info); + } + + /* take a reference for the caller */ + return exo_object_ref (info); +} + + + +static ThunarVfsMimeInfo* +thunar_vfs_mime_database_get_info_for_name_unlocked (ThunarVfsMimeDatabase *database, + const gchar *name) +{ + ThunarVfsMimeProvider *provider; + const gchar *type = NULL; + const gchar *ptr; + GList *lp; + + /* try the literals first */ + for (lp = database->providers; lp != NULL && type == NULL; lp = lp->next) + { + provider = THUNAR_VFS_MIME_PROVIDER_DATA (lp->data)->provider; + if (G_LIKELY (provider != NULL)) + type = thunar_vfs_mime_provider_lookup_literal (provider, name); + } + + /* next the suffixes */ + if (G_LIKELY (type == NULL)) + { + ptr = strpbrk (name, database->stopchars); + while (ptr != NULL) + { + /* case-sensitive first */ + for (lp = database->providers; lp != NULL && type == NULL; lp = lp->next) + { + provider = THUNAR_VFS_MIME_PROVIDER_DATA (lp->data)->provider; + if (G_LIKELY (provider != NULL)) + type = thunar_vfs_mime_provider_lookup_suffix (provider, ptr, FALSE); + } + + /* case-insensitive next */ + if (G_UNLIKELY (type == NULL)) + { + for (lp = database->providers; lp != NULL && type == NULL; lp = lp->next) + { + provider = THUNAR_VFS_MIME_PROVIDER_DATA (lp->data)->provider; + if (G_LIKELY (provider != NULL)) + type = thunar_vfs_mime_provider_lookup_suffix (provider, ptr, TRUE); + } + } + + /* check if we got a type */ + if (G_LIKELY (type != NULL)) + break; + + ptr = strpbrk (ptr + 1, database->stopchars); + } + } + + /* the glob match comes last */ + if (G_UNLIKELY (type == NULL)) + { + for (lp = database->providers; lp != NULL && type == NULL; lp = lp->next) + { + provider = THUNAR_VFS_MIME_PROVIDER_DATA (lp->data)->provider; + if (G_LIKELY (provider != NULL)) + type = thunar_vfs_mime_provider_lookup_glob (provider, name); + } + } + + return (type != NULL) ? thunar_vfs_mime_database_get_info_unlocked (database, type) : NULL; +} + + + +/** + * thunar_vfs_mime_database_get: + * + * Returns a reference on the shared #ThunarVfsMimeDatabase + * instance. The caller is responsible to call #exo_object_unref() + * on the returned object when no longer needed. + * + * Return value: the shared #ThunarVfsMimeDatabase. + **/ +ThunarVfsMimeDatabase* +thunar_vfs_mime_database_get (void) +{ + if (G_UNLIKELY (thunar_vfs_mime_database_shared_instance == NULL)) + thunar_vfs_mime_database_shared_instance = exo_object_new (THUNAR_VFS_TYPE_MIME_DATABASE); + else + exo_object_ref (EXO_OBJECT (thunar_vfs_mime_database_shared_instance)); + + return thunar_vfs_mime_database_shared_instance; +} + + + +/** + * thunar_vfs_mime_database_get_info: + * @database : a #ThunarVfsMimeDatabase. + * @mime_type : the string representation of the mime type. + * + * Determines the #ThunarVfsMimeInfo which corresponds to @mime_type + * in database. The caller is responsible to call #exo_object_unref() + * on the returned instance. + * + * Return value: the #ThunarVfsMimeInfo corresponding to @mime_type in @database. + **/ +ThunarVfsMimeInfo* +thunar_vfs_mime_database_get_info (ThunarVfsMimeDatabase *database, + const gchar *mime_type) +{ + ThunarVfsMimeInfo *info; + + g_return_val_if_fail (THUNAR_VFS_IS_MIME_DATABASE (database), NULL); + g_return_val_if_fail (mime_type != NULL, NULL); + + g_mutex_lock (database->lock); + info = thunar_vfs_mime_database_get_info_unlocked (database, mime_type); + g_mutex_unlock (database->lock); + + return info; +} + + + +/** + * thunar_vfs_mime_database_get_info_for_data: + * @database : a #ThunarVfsMimeDatabase. + * @data : the data to check. + * @length : the length of @data in bytes. + * + * Determines the #ThunarVfsMimeInfo for @data in @database. The + * caller is responsible to call #exo_object_unref() on the + * returned instance. + * + * Return value: the #ThunarVfsMimeInfo determined for @data. + **/ +ThunarVfsMimeInfo* +thunar_vfs_mime_database_get_info_for_data (ThunarVfsMimeDatabase *database, + gconstpointer data, + gsize length) +{ + ThunarVfsMimeProvider *provider; + ThunarVfsMimeInfo *info = NULL; + const gchar *type_best; + const gchar *type; + GList *lp; + gint prio_best; + gint prio; + + g_return_val_if_fail (THUNAR_VFS_IS_MIME_DATABASE (database), NULL); + + g_mutex_lock (database->lock); + + if (G_LIKELY (data != NULL && length > 0)) + { + /* perform a 'best fit' lookup on all active providers */ + for (type_best = NULL, prio_best = -1, lp = database->providers; lp != NULL; lp = lp->next) + { + provider = THUNAR_VFS_MIME_PROVIDER_DATA (lp->data)->provider; + if (G_LIKELY (provider != NULL)) + { + type = thunar_vfs_mime_provider_lookup_data (provider, data, length, &prio); + if (G_LIKELY (type != NULL && prio > prio_best)) + { + prio_best = prio; + type_best = type; + } + } + } + + if (G_LIKELY (type_best != NULL)) + { + /* lookup the info for the best type (if any) */ + info = thunar_vfs_mime_database_get_info_unlocked (database, type_best); + } + else if (g_utf8_validate (data, length, NULL)) + { + /* we have valid UTF-8 text here! */ + info = thunar_vfs_mime_database_get_info_unlocked (database, "text/plain"); + } + } + + /* fallback to 'application/octet-stream' if we could not determine any type */ + if (G_UNLIKELY (info == NULL)) + info = thunar_vfs_mime_database_get_info_unlocked (database, "application/octet-stream"); + + g_mutex_unlock (database->lock); + + return info; +} + + + +/** + * thunar_vfs_mime_database_get_info_for_name: + * @database : a #ThunarVfsMimeDatabase. + * @name : a filename (must be valid UTF-8!). + * + * Determines the #ThunarVfsMimeInfo for the filename given + * in @name from @database. The caller is responsible to + * call #exo_object_unref() on the returned instance. + * + * The @name must be a valid filename in UTF-8 encoding + * and it may not contained any slashes! + * + * Return value: the #ThunarVfsMimeInfo for @name in @database. + **/ +ThunarVfsMimeInfo* +thunar_vfs_mime_database_get_info_for_name (ThunarVfsMimeDatabase *database, + const gchar *name) +{ + ThunarVfsMimeInfo *info; + + g_return_val_if_fail (THUNAR_VFS_IS_MIME_DATABASE (database), NULL); + g_return_val_if_fail (g_utf8_validate (name, -1, NULL), NULL); + g_return_val_if_fail (strchr (name, '/') == NULL, NULL); + g_return_val_if_fail (*name != '\0', NULL); + + /* try to determine the info from the name */ + g_mutex_lock (database->lock); + info = thunar_vfs_mime_database_get_info_for_name_unlocked (database, name); + g_mutex_unlock (database->lock); + + /* fallback to 'application/octet-stream' */ + if (G_UNLIKELY (info == NULL)) + info = thunar_vfs_mime_database_get_info (database, "application/octet-stream"); + + /* we got it */ + return info; +} + + + +/** + * thunar_vfs_mime_database_get_info_for_file: + * @database : a #ThunarVfsMimeDatabase. + * @path : the path to a file in the local filesystem (in the filesystem encoding). + * @name : the basename of @path in UTF-8 encoding or %NULL. + * + * Determines the #ThunarVfsMimeInfo for @path in @database. The + * caller is responsible to free the returned instance using + * #exo_object_unref(). + * + * The @name parameter is optional. If the caller already knows the + * basename of @path in UTF-8 encoding, it should be specified here + * to speed up the lookup process. + * + * Return value: the #ThunarVfsMimeInfo for @path in @database. + **/ +ThunarVfsMimeInfo* +thunar_vfs_mime_database_get_info_for_file (ThunarVfsMimeDatabase *database, + const gchar *path, + const gchar *name) +{ + ThunarVfsMimeInfo *info; + struct stat stat; + gssize nbytes; + gchar *basename; + gchar *buffer; + gsize buflen; + gint fd; + + g_return_val_if_fail (THUNAR_VFS_IS_MIME_DATABASE (database), NULL); + g_return_val_if_fail (path != NULL && *path != '\0', NULL); + g_return_val_if_fail (name == NULL || g_utf8_validate (name, -1, NULL), NULL); + + /* determine the basename in UTF-8 if not already given by the caller */ + if (G_UNLIKELY (name == NULL)) + { + buffer = g_path_get_basename (path); + name = basename = g_filename_display_name (buffer); + g_free (buffer); + } + else + { + basename = NULL; + } + + /* try to determine the type from the name first */ + g_mutex_lock (database->lock); + info = thunar_vfs_mime_database_get_info_for_name_unlocked (database, name); + g_mutex_unlock (database->lock); + + /* try to determine the type from the file content */ + if (G_UNLIKELY (info == NULL)) + { + fd = g_open (path, O_RDONLY, 0); + if (G_LIKELY (fd >= 0)) + { + /* stat the file and verify that we have a regular file, which is not empty */ + if (G_LIKELY (fstat (fd, &stat) == 0 && S_ISREG (stat.st_mode) && stat.st_size > 0)) + { + /* read the beginning from the file */ + buflen = MIN (stat.st_size, database->max_buffer_extents); + buffer = g_new (gchar, buflen); + nbytes = read (fd, buffer, buflen); + + /* try to determine a type from the buffer contents */ + if (G_LIKELY (nbytes > 0)) + info = thunar_vfs_mime_database_get_info_for_data (database, buffer, nbytes); + + /* cleanup */ + g_free (buffer); + } + + /* cleanup */ + close (fd); + } + + /* fallback to 'application/octet-stream' */ + if (G_UNLIKELY (info == NULL)) + info = thunar_vfs_mime_database_get_info (database, "application/octet-stream"); + } + + /* cleanup */ + if (G_UNLIKELY (basename != NULL)) + g_free (basename); + + /* we got it */ + return info; +} + + diff --git a/thunar-vfs/thunar-vfs-mime-database.h b/thunar-vfs/thunar-vfs-mime-database.h new file mode 100644 index 0000000000000000000000000000000000000000..0621b8648eb513c7becb20a63275e4d3b9e49639 --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-database.h @@ -0,0 +1,55 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __THUNAR_VFS_MIME_DATABASE_H__ +#define __THUNAR_VFS_MIME_DATABASE_H__ + +#include <thunar-vfs/thunar-vfs-mime-info.h> + +G_BEGIN_DECLS; + +typedef struct _ThunarVfsMimeDatabaseClass ThunarVfsMimeDatabaseClass; +typedef struct _ThunarVfsMimeDatabase ThunarVfsMimeDatabase; + +#define THUNAR_VFS_TYPE_MIME_DATABASE (thunar_vfs_mime_database_get_type ()) +#define THUNAR_VFS_MIME_DATABASE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNAR_VFS_TYPE_MIME_DATABASE, ThunarVfsMimeDatabase)) +#define THUNAR_VFS_MIME_DATABASE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNAR_VFS_TYPE_MIME_DATABASE, ThunarVfsMimeDatabaseClass)) +#define THUNAR_VFS_IS_MIME_DATABASE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNAR_VFS_TYPE_MIME_DATABASE)) +#define THUNAR_VFS_IS_MIME_DATABASE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_VFS_TYPE_MIME_DATABASE)) +#define THUNAR_VFS_MIME_DATABASE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_VFS_TYPE_MIME_DATABASE, ThunarVfsMimeDatabaseClass)) + +GType thunar_vfs_mime_database_get_type (void) G_GNUC_CONST; + +ThunarVfsMimeDatabase *thunar_vfs_mime_database_get (void); + +ThunarVfsMimeInfo *thunar_vfs_mime_database_get_info (ThunarVfsMimeDatabase *database, + const gchar *mime_type); +ThunarVfsMimeInfo *thunar_vfs_mime_database_get_info_for_data (ThunarVfsMimeDatabase *database, + gconstpointer data, + gsize length); +ThunarVfsMimeInfo *thunar_vfs_mime_database_get_info_for_name (ThunarVfsMimeDatabase *database, + const gchar *name); +ThunarVfsMimeInfo *thunar_vfs_mime_database_get_info_for_file (ThunarVfsMimeDatabase *database, + const gchar *path, + const gchar *name); + +G_END_DECLS; + +#endif /* !__THUNAR_VFS_MIME_DATABASE_H__ */ diff --git a/thunar-vfs/thunar-vfs-mime-info.c b/thunar-vfs/thunar-vfs-mime-info.c new file mode 100644 index 0000000000000000000000000000000000000000..0902b7847ef7a91f57e034e41ae3e88df444f555 --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-info.c @@ -0,0 +1,362 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#ifdef HAVE_MEMORY_H +#include <memory.h> +#endif +#ifdef HAVE_STRING_H +#include <string.h> +#endif + +#include <thunar-vfs/thunar-vfs-mime-info.h> +#include <thunar-vfs/thunar-vfs-mime-parser.h> + + + +static const struct +{ + const gchar *const type; + const gchar *const icon; +} GNOME_ICONNAMES[] = +{ + { "inode/directory", "gnome-fs-directory" }, + { "inode/blockdevice", "gnome-fs-blockdev" }, + { "inode/chardevice", "gnome-fs-chardev" }, + { "inode/fifo", "gnome-fs-fifo" }, + { "inode/socket", "gnome-fs-socket" }, +}; + + + +static void thunar_vfs_mime_info_register_type (GType *type); +static void thunar_vfs_mime_info_class_init (ThunarVfsMimeInfoClass *klass); +static void thunar_vfs_mime_info_finalize (ExoObject *object); +static void thunar_vfs_mime_info_icon_theme_changed (GtkIconTheme *icon_theme, + ThunarVfsMimeInfo *info); + + + + + +static ExoObjectClass *thunar_vfs_mime_info_parent_class; + + + +GType +thunar_vfs_mime_info_get_type (void) +{ + static GType type = G_TYPE_INVALID; + static GOnce once = G_ONCE_INIT; + + /* thread-safe type registration */ + g_once (&once, (GThreadFunc) thunar_vfs_mime_info_register_type, &type); + + return type; +} + + + +static void +thunar_vfs_mime_info_register_type (GType *type) +{ + static const GTypeInfo info = + { + sizeof (ThunarVfsMimeInfoClass), + NULL, + NULL, + (GClassInitFunc) thunar_vfs_mime_info_class_init, + NULL, + NULL, + sizeof (ThunarVfsMimeInfo), + 128u, + NULL, + NULL, + }; + + *type = g_type_register_static (EXO_TYPE_OBJECT, "ThunarVfsMimeInfo", &info, 0); +} + + + +static void +thunar_vfs_mime_info_class_init (ThunarVfsMimeInfoClass *klass) +{ + ExoObjectClass *exoobject_class; + + thunar_vfs_mime_info_parent_class = g_type_class_peek_parent (klass); + + exoobject_class = EXO_OBJECT_CLASS (klass); + exoobject_class->finalize = thunar_vfs_mime_info_finalize; +} + + + +static void +thunar_vfs_mime_info_finalize (ExoObject *object) +{ + ThunarVfsMimeInfo *info = THUNAR_VFS_MIME_INFO (object); + + g_return_if_fail (THUNAR_VFS_IS_MIME_INFO (info)); + + /* free the comment */ + if (info->comment != NULL && info->comment != info->name) + { +#ifndef G_DISABLE_CHECKS + memset (info->comment, 0xaa, strlen (info->comment) + 1); +#endif + g_free (info->comment); + } + + /* free the name */ +#ifndef G_DISABLE_CHECKS + if (G_LIKELY (info->name != NULL)) + memset (info->name, 0xaa, strlen (info->name) + 1); +#endif + g_free (info->name); + + /* disconnect from the icon theme (if any) */ + if (G_LIKELY (info->icon_theme != NULL)) + { + g_signal_handlers_disconnect_by_func (G_OBJECT (info->icon_theme), thunar_vfs_mime_info_icon_theme_changed, info); + g_object_unref (G_OBJECT (info->icon_theme)); + } + + /* free the icon name if it isn't one of the statics */ + if (G_LIKELY (!info->icon_name_static && info->icon_name != NULL)) + { +#ifndef G_DISABLE_CHECKS + memset (info->icon_name, 0xaa, strlen (info->icon_name) + 1); +#endif + g_free (info->icon_name); + } + + /* invoke the parent's finalize method */ + (*EXO_OBJECT_CLASS (thunar_vfs_mime_info_parent_class)->finalize) (object); +} + + + +static void +thunar_vfs_mime_info_icon_theme_changed (GtkIconTheme *icon_theme, + ThunarVfsMimeInfo *info) +{ + g_return_if_fail (GTK_IS_ICON_THEME (icon_theme)); + g_return_if_fail (THUNAR_VFS_IS_MIME_INFO (info)); + g_return_if_fail (info->icon_theme == icon_theme); + + /* drop the cached icon name, so the next lookup + * call will perform a lookup again. + */ + if (G_LIKELY (!info->icon_name_static)) + { + g_free (info->icon_name); + info->icon_name = NULL; + } +} + + + +/** + * thunar_vfs_mime_info_get_comment: + * @info : a #ThunarVfsMimeInfo. + * + * Determines the description for the given @info. + * + * Note that this method MUST NOT be called from threads other than + * the main thread, because it's not thread-safe! + * + * Return value: the comment associated with the @info or the empty string + * if no comment was provided. + */ +const gchar* +thunar_vfs_mime_info_get_comment (ThunarVfsMimeInfo *info) +{ + gchar *path; + gchar *spec; + + g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); + + if (G_UNLIKELY (info->comment == NULL)) + { + spec = g_strdup_printf ("mime/%s.xml", info->name); + path = xfce_resource_lookup (XFCE_RESOURCE_DATA, spec); + g_free (spec); + + if (G_LIKELY (path != NULL)) + { + info->comment = _thunar_vfs_mime_parser_load_comment_from_file (path, NULL); + g_free (path); + } + + if (G_UNLIKELY (info->comment == NULL)) + info->comment = info->name; + } + + return info->comment; +} + + + +/** + * thunar_vfs_mime_info_get_name: + * @info : a #ThunarVfsMimeInfo. + * + * Returns the full qualified name of the MIME type + * described by the @info object. + * + * Return value: the name of @info. + **/ +const gchar* +thunar_vfs_mime_info_get_name (const ThunarVfsMimeInfo *info) +{ + g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); + return info->name; +} + + + +/** + * thunar_vfs_mime_info_get_media: + * @info : a #ThunarVfsMimeInfo. + * + * Returns the media portion of the MIME type, e.g. if your + * #ThunarVfsMimeInfo instance refers to "text/plain", invoking + * this method will return "text". + * + * Return value: the media portion of the MIME type. + **/ +const gchar* +thunar_vfs_mime_info_get_media (const ThunarVfsMimeInfo *info) +{ + g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); + return info->media; +} + + + +/** + * thunar_vfs_mime_info_get_subtype: + * @info : a #ThunarVfsMimeInfo. + * + * Returns the subtype portion of the MIME type, e.g. if @info + * refers to "application/octect-stream", this method will + * return "octect-stream". + * + * Return value: the subtype portion of @info. + **/ +const gchar* +thunar_vfs_mime_info_get_subtype (const ThunarVfsMimeInfo *info) +{ + g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); + return info->subtype; +} + + + +/** + * thunar_vfs_mime_info_lookup_icon_name: + * @info : a #ThunarVfsMimeInfo. + * @icon_theme : the #GtkIconTheme on which to perform the lookup. + * + * Tries to determine the name of a suitable icon for @info + * in @icon_theme. The returned icon name can then be used + * in calls to #gtk_icon_theme_lookup_icon() or + * #gtk_icon_theme_load_icon(). + * + * Note that this method MUST NOT be called from threads other than + * the main thread, because it's not thread-safe! + * + * Return value: a suitable icon name for @info in @icon_theme. + **/ +const gchar* +thunar_vfs_mime_info_lookup_icon_name (ThunarVfsMimeInfo *info, + GtkIconTheme *icon_theme) +{ + gsize n; + + g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); + g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL); + + /* check if our cached name will suffice */ + if (G_LIKELY (info->icon_theme == icon_theme && info->icon_name != NULL)) + return info->icon_name; + + /* if we have a new icon theme, connect to the new one */ + if (G_UNLIKELY (info->icon_theme != icon_theme)) + { + /* disconnect from the previous one */ + if (G_LIKELY (info->icon_theme != NULL)) + { + g_signal_handlers_disconnect_by_func (G_OBJECT (info->icon_theme), thunar_vfs_mime_info_icon_theme_changed, info); + g_object_unref (G_OBJECT (info->icon_theme)); + } + + /* connect to the new one */ + info->icon_theme = icon_theme; + g_object_ref (G_OBJECT (icon_theme)); + g_signal_connect (G_OBJECT (icon_theme), "changed", G_CALLBACK (thunar_vfs_mime_info_icon_theme_changed), info); + } + + /* free the previously set icon name */ + if (!info->icon_name_static) + g_free (info->icon_name); + + /* start out with the full name (assuming a non-static icon_name) */ + info->icon_name = g_strdup_printf ("gnome-mime-%s-%s", info->media, info->subtype); + info->icon_name_static = FALSE; + if (!gtk_icon_theme_has_icon (icon_theme, info->icon_name)) + { + /* only the media portion */ + info->icon_name[11 + ((info->subtype - 1) - info->name)] = '\0'; + if (!gtk_icon_theme_has_icon (icon_theme, info->icon_name)) + { + /* if we get here, we'll use a static icon name */ + info->icon_name_static = TRUE; + g_free (info->icon_name); + + /* GNOME uses non-standard names for special MIME types */ + for (n = 0; n < G_N_ELEMENTS (GNOME_ICONNAMES); ++n) + if (exo_str_is_equal (info->name, GNOME_ICONNAMES[n].type)) + if (gtk_icon_theme_has_icon (icon_theme, GNOME_ICONNAMES[n].icon)) + { + info->icon_name = (gchar *) GNOME_ICONNAMES[n].icon; + break; + } + + /* fallback is always application/octect-stream */ + if (n == G_N_ELEMENTS (GNOME_ICONNAMES)) + info->icon_name = (gchar *) "gnome-mime-application-octect-stream"; + } + } + + g_assert (info->icon_name != NULL); + g_assert (info->icon_name[0] != '\0'); + + return info->icon_name; +} + + + + + diff --git a/thunar-vfs/thunar-vfs-mime-info.h b/thunar-vfs/thunar-vfs-mime-info.h new file mode 100644 index 0000000000000000000000000000000000000000..a0ce621c082cd168f7454f8f9e1840bcafb908cd --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-info.h @@ -0,0 +1,74 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __THUNAR_VFS_MIME_INFO_H__ +#define __THUNAR_VFS_MIME_INFO_H__ + +#include <exo/exo.h> + +G_BEGIN_DECLS; + +typedef struct _ThunarVfsMimeInfoClass ThunarVfsMimeInfoClass; +typedef struct _ThunarVfsMimeInfo ThunarVfsMimeInfo; + +#define THUNAR_VFS_TYPE_MIME_INFO (thunar_vfs_mime_info_get_type ()) +#define THUNAR_VFS_MIME_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNAR_VFS_TYPE_MIME_INFO, ThunarVfsMimeInfo)) +#define THUNAR_VFS_MIME_INFO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNAR_VFS_TYPE_MIME_INFO, ThunarVfsMimeInfoClass)) +#define THUNAR_VFS_IS_MIME_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNAR_VFS_TYPE_MIME_INFO)) +#define THUNAR_VFS_IS_MIME_INFO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_VFS_TYPE_MIME_INFO)) +#define THUNAR_VFS_MIME_INFO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_VFS_TYPE_MIME_INFO, ThunarVfsMimeInfoClass)) + +struct _ThunarVfsMimeInfoClass +{ + ExoObjectClass __parent__; +}; + +struct _ThunarVfsMimeInfo +{ + ExoObject __parent__; + + gchar *comment; + gchar *name; + + const gchar *media; + const gchar *subtype; + + gchar *icon_name; + gboolean icon_name_static : 1; + GtkIconTheme *icon_theme; +}; + +GType thunar_vfs_mime_info_get_type (void) G_GNUC_CONST; + +#define thunar_vfs_mime_info_ref exo_object_ref +#define thunar_vfs_mime_info_unref exo_object_unref + +const gchar *thunar_vfs_mime_info_get_comment (ThunarVfsMimeInfo *info) G_GNUC_PURE; +const gchar *thunar_vfs_mime_info_get_name (const ThunarVfsMimeInfo *info) G_GNUC_PURE; + +const gchar *thunar_vfs_mime_info_get_media (const ThunarVfsMimeInfo *info) G_GNUC_PURE; +const gchar *thunar_vfs_mime_info_get_subtype (const ThunarVfsMimeInfo *info) G_GNUC_PURE; + +const gchar *thunar_vfs_mime_info_lookup_icon_name (ThunarVfsMimeInfo *info, + GtkIconTheme *icon_theme); + +G_END_DECLS; + +#endif /* !__THUNAR_VFS_MIME_INFO_H__ */ diff --git a/thunar-vfs/thunar-vfs-mime-legacy.c b/thunar-vfs/thunar-vfs-mime-legacy.c new file mode 100644 index 0000000000000000000000000000000000000000..f154652eef8f31be3b50862004d67773b2858090 --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-legacy.c @@ -0,0 +1,201 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Based on code initially written by Matthias Clasen <mclasen@redhat.com> + * for the xdgmime library. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <thunar-vfs/thunar-vfs-mime-legacy.h> + + + +static void thunar_vfs_mime_legacy_class_init (ThunarVfsMimeLegacyClass *klass); +static void thunar_vfs_mime_legacy_finalize (ExoObject *object); +static const gchar *thunar_vfs_mime_legacy_lookup_data (ThunarVfsMimeProvider *provider, + gconstpointer data, + gsize length, + gint *priority); +static const gchar *thunar_vfs_mime_legacy_lookup_literal (ThunarVfsMimeProvider *provider, + const gchar *filename); +static const gchar *thunar_vfs_mime_legacy_lookup_suffix (ThunarVfsMimeProvider *provider, + const gchar *suffix, + gboolean ignore_case); +static const gchar *thunar_vfs_mime_legacy_lookup_glob (ThunarVfsMimeProvider *provider, + const gchar *filename); +static GList *thunar_vfs_mime_legacy_get_stop_characters (ThunarVfsMimeProvider *provider); +static gsize thunar_vfs_mime_legacy_get_max_buffer_extents (ThunarVfsMimeProvider *provider); + + + +struct _ThunarVfsMimeLegacyClass +{ + ThunarVfsMimeProviderClass __parent__; +}; + +struct _ThunarVfsMimeLegacy +{ + ThunarVfsMimeProvider __parent__; +}; + + + +static ExoObjectClass *thunar_vfs_mime_legacy_parent_class; + + + +GType +thunar_vfs_mime_legacy_get_type (void) +{ + static GType type = G_TYPE_INVALID; + + if (G_UNLIKELY (type == G_TYPE_INVALID)) + { + static const GTypeInfo info = + { + sizeof (ThunarVfsMimeLegacyClass), + NULL, + NULL, + (GClassInitFunc) thunar_vfs_mime_legacy_class_init, + NULL, + NULL, + sizeof (ThunarVfsMimeLegacy), + 0, + NULL, + NULL, + }; + + type = g_type_register_static (THUNAR_VFS_TYPE_MIME_PROVIDER, + "ThunarVfsMimeLegacy", &info, 0); + } + + return type; +} + + + +static void +thunar_vfs_mime_legacy_class_init (ThunarVfsMimeLegacyClass *klass) +{ + ThunarVfsMimeProviderClass *thunarvfs_mime_provider_class; + ExoObjectClass *exoobject_class; + + thunar_vfs_mime_legacy_parent_class = g_type_class_peek_parent (klass); + + exoobject_class = EXO_OBJECT_CLASS (klass); + exoobject_class->finalize = thunar_vfs_mime_legacy_finalize; + + thunarvfs_mime_provider_class = THUNAR_VFS_MIME_PROVIDER_CLASS (klass); + thunarvfs_mime_provider_class->lookup_data = thunar_vfs_mime_legacy_lookup_data; + thunarvfs_mime_provider_class->lookup_literal = thunar_vfs_mime_legacy_lookup_literal; + thunarvfs_mime_provider_class->lookup_suffix = thunar_vfs_mime_legacy_lookup_suffix; + thunarvfs_mime_provider_class->lookup_glob = thunar_vfs_mime_legacy_lookup_glob; + thunarvfs_mime_provider_class->get_stop_characters = thunar_vfs_mime_legacy_get_stop_characters; + thunarvfs_mime_provider_class->get_max_buffer_extents = thunar_vfs_mime_legacy_get_max_buffer_extents; +} + + + +static void +thunar_vfs_mime_legacy_finalize (ExoObject *object) +{ + (*EXO_OBJECT_CLASS (thunar_vfs_mime_legacy_parent_class)->finalize) (object); +} + + + +static const gchar* +thunar_vfs_mime_legacy_lookup_data (ThunarVfsMimeProvider *provider, + gconstpointer data, + gsize length, + gint *priority) +{ + return NULL; +} + + + +static const gchar* +thunar_vfs_mime_legacy_lookup_literal (ThunarVfsMimeProvider *provider, + const gchar *filename) +{ + return NULL; +} + + + +static const gchar* +thunar_vfs_mime_legacy_lookup_suffix (ThunarVfsMimeProvider *provider, + const gchar *suffix, + gboolean ignore_case) +{ + return NULL; +} + + + +static const gchar* +thunar_vfs_mime_legacy_lookup_glob (ThunarVfsMimeProvider *provider, + const gchar *filename) +{ + return NULL; +} + + + +static GList* +thunar_vfs_mime_legacy_get_stop_characters (ThunarVfsMimeProvider *provider) +{ + return NULL; +} + + + +static gsize +thunar_vfs_mime_legacy_get_max_buffer_extents (ThunarVfsMimeProvider *provider) +{ + return 0; +} + + + +/** + * thunar_vfs_mime_legacy_new: + * @directory : an XDG mime base directory. + * + * Allocates a new #ThunarVfsMimeLegacy for @directory and + * returns the instance on success, or %NULL on error. + * + * The caller is responsible to free the returned instance + * using #exo_object_unref(). + * + * Return value: the newly allocated #ThunarVfsMimeLegacy + * instance or %NULL on error. + **/ +ThunarVfsMimeProvider* +thunar_vfs_mime_legacy_new (const gchar *directory) +{ + return NULL; +} + + + diff --git a/thunar-vfs/thunar-vfs-mime-legacy.h b/thunar-vfs/thunar-vfs-mime-legacy.h new file mode 100644 index 0000000000000000000000000000000000000000..bd5d4ceb11a2f8dfb7fb4a353df6a578eacbab98 --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-legacy.h @@ -0,0 +1,44 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __THUNAR_VFS_MIME_LEGACY_H__ +#define __THUNAR_VFS_MIME_LEGACY_H__ + +#include <thunar-vfs/thunar-vfs-mime-provider.h> + +G_BEGIN_DECLS; + +typedef struct _ThunarVfsMimeLegacyClass ThunarVfsMimeLegacyClass; +typedef struct _ThunarVfsMimeLegacy ThunarVfsMimeLegacy; + +#define THUNAR_VFS_TYPE_MIME_LEGACY (thunar_vfs_mime_legacy_get_type ()) +#define THUNAR_VFS_MIME_LEGACY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNAR_VFS_TYPE_MIME_LEGACY, ThunarVfsMimeLegacy)) +#define THUNAR_VFS_MIME_LEGACY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNAR_VFS_TYPE_MIME_LEGACY, ThunarVfsMimeLegacyClass)) +#define THUNAR_VFS_IS_MIME_LEGACY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNAR_VFS_TYPE_MIME_LEGACY)) +#define THUNAR_VFS_IS_MIME_LEGACY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_VFS_TYPE_MIME_LEGACY)) +#define THUNAR_VFS_MIME_LEGACY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_VFS_TYPE_MIME_LEGACY, ThunarVfsMimeLegacyClass)) + +GType thunar_vfs_mime_legacy_get_type (void) G_GNUC_CONST; + +ThunarVfsMimeProvider *thunar_vfs_mime_legacy_new (const gchar *directory) EXO_GNUC_MALLOC; + +G_END_DECLS; + +#endif /* !__THUNAR_VFS_MIME_LEGACY_H__ */ diff --git a/thunar-vfs/thunar-vfs-mime-provider.c b/thunar-vfs/thunar-vfs-mime-provider.c new file mode 100644 index 0000000000000000000000000000000000000000..b55213f273d07a4eab7e3a1f041b801e5af6bf11 --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-provider.c @@ -0,0 +1,86 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <thunar-vfs/thunar-vfs-mime-provider.h> + + + +static void thunar_vfs_mime_provider_register_type (GType *type); +static void thunar_vfs_mime_provider_class_init (ThunarVfsMimeProviderClass *klass); + + + +GType +thunar_vfs_mime_provider_get_type (void) +{ + static GType type = G_TYPE_INVALID; + static GOnce once = G_ONCE_INIT; + + /* thread-safe type registration */ + g_once (&once, (GThreadFunc) thunar_vfs_mime_provider_register_type, &type); + + return type; +} + + + +static void +thunar_vfs_mime_provider_register_type (GType *type) +{ + static const GTypeInfo info = + { + sizeof (ThunarVfsMimeProviderClass), + NULL, + NULL, + (GClassInitFunc) thunar_vfs_mime_provider_class_init, + NULL, + NULL, + sizeof (ThunarVfsMimeProvider), + 0, + NULL, + NULL, + }; + + *type = g_type_register_static (EXO_TYPE_OBJECT, "ThunarVfsMimeProvider", &info, G_TYPE_FLAG_ABSTRACT); +} + + + +static void +thunar_vfs_mime_provider_class_init (ThunarVfsMimeProviderClass *klass) +{ + /* We install noops for every virtual method here, + * so derived classes don't need to implement + * each and every method if desired. + */ + klass->lookup_data = (gpointer) exo_noop_null; + klass->lookup_literal = (gpointer) exo_noop_null; + klass->lookup_suffix = (gpointer) exo_noop_null; + klass->lookup_glob = (gpointer) exo_noop_null; + klass->get_stop_characters = (gpointer) exo_noop_null; + klass->get_max_buffer_extents = (gpointer) exo_noop_zero; +} + + + diff --git a/thunar-vfs/thunar-vfs-mime-provider.h b/thunar-vfs/thunar-vfs-mime-provider.h new file mode 100644 index 0000000000000000000000000000000000000000..7e71a2276de680bcd9b0aa7448de61f226564912 --- /dev/null +++ b/thunar-vfs/thunar-vfs-mime-provider.h @@ -0,0 +1,139 @@ +/* $Id$ */ +/*- + * Copyright (c) 2005 Benedikt Meurer <benny@xfce.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __THUNAR_VFS_MIME_PROVIDER_H__ +#define __THUNAR_VFS_MIME_PROVIDER_H__ + +#include <exo/exo.h> + +G_BEGIN_DECLS; + +typedef struct _ThunarVfsMimeProviderClass ThunarVfsMimeProviderClass; +typedef struct _ThunarVfsMimeProvider ThunarVfsMimeProvider; + +#define THUNAR_VFS_TYPE_MIME_PROVIDER (thunar_vfs_mime_provider_get_type ()) +#define THUNAR_VFS_MIME_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNAR_VFS_TYPE_MIME_PROVIDER, ThunarVfsMimeProvider)) +#define THUNAR_VFS_MIME_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNAR_VFS_TYPE_MIME_PROVIDER, ThunarVfsMimeProviderClass)) +#define THUNAR_VFS_IS_MIME_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNAR_VFS_TYPE_MIME_PROVIDER)) +#define THUNAR_VFS_IS_MIME_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_VFS_TYPE_MIME_PROVIDER)) +#define THUNAR_VFS_MIME_PROVIDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_VFS_TYPE_MIME_PROVIDER, ThunarVfsMimeProviderClass)) + +struct _ThunarVfsMimeProviderClass +{ + ExoObjectClass __parent__; + + const gchar *(*lookup_data) (ThunarVfsMimeProvider *provider, + gconstpointer data, + gsize length, + gint *priority); + + const gchar *(*lookup_literal) (ThunarVfsMimeProvider *provider, + const gchar *filename); + const gchar *(*lookup_suffix) (ThunarVfsMimeProvider *provider, + const gchar *suffix, + gboolean ignore_case); + const gchar *(*lookup_glob) (ThunarVfsMimeProvider *provider, + const gchar *filename); + + GList *(*get_stop_characters) (ThunarVfsMimeProvider *provider); + gsize (*get_max_buffer_extents) (ThunarVfsMimeProvider *provider); +}; + +struct _ThunarVfsMimeProvider +{ + ExoObject __parent__; +}; + +GType thunar_vfs_mime_provider_get_type (void) G_GNUC_CONST; + +/** + * thunar_vfs_mime_provider_lookup_data: + * @provider : a #ThunarVfsMimeProvider. + * @data : pointer to the data. + * @length : length of @data in bytes. + * @priority : return location for the priority or %NULL. + * + * The location pointed to by @priority (if not %NULL) will only + * be set to a meaningfull value if this method returns a + * non-%NULL value. + * + * Return value: a pointer to the MIME type or %NULL. + **/ +#define thunar_vfs_mime_provider_lookup_data(provider, data, length, priority) \ + ((*THUNAR_VFS_MIME_PROVIDER_GET_CLASS ((provider))->lookup_data) ((provider), (data), (length), (priority))) + +/** + * thunar_vfs_mime_provider_lookup_literal: + * @provider : a #ThunarVfsMimeProvider. + * @filename : a filename. + * + * Return value: a pointer to the MIME type or %NULL. + **/ +#define thunar_vfs_mime_provider_lookup_literal(provider, filename) \ + ((*THUNAR_VFS_MIME_PROVIDER_GET_CLASS ((provider))->lookup_literal) ((provider), (filename))) + +/** + * thunar_vfs_mime_provider_lookup_suffix: + * @provider : a #ThunarVfsMimeProvider. + * @suffix : a filename suffix. + * @ignore_case : %TRUE to perform case-insensitive comparison. + * + * Return value: a pointer to the MIME type or %NULL. + **/ +#define thunar_vfs_mime_provider_lookup_suffix(provider, suffix, ignore_case) \ + ((*THUNAR_VFS_MIME_PROVIDER_GET_CLASS ((provider))->lookup_suffix) ((provider), (suffix), (ignore_case))) + +/** + * thunar_vfs_mime_provider_lookup_glob: + * @provider : a #ThunarVfsMimeProvider. + * @filename : a filename. + * + * Return value: a pointer to the MIME type or %NULL. + **/ +#define thunar_vfs_mime_provider_lookup_glob(provider, filename) \ + ((*THUNAR_VFS_MIME_PROVIDER_GET_CLASS ((provider))->lookup_glob) ((provider), (filename))) + +/** + * thunar_vfs_mime_provider_get_stop_characters: + * @provider : a #ThunarVfsMimeProvider. + * + * Returns the list of stop characters for all suffix entries in @provider as + * a #GList of #gunichar<!---->s. The caller is responsible to free the list + * using #g_list_free(). + * + * Return value: the list of stop characters for the suffix entries in @provider. + **/ +#define thunar_vfs_mime_provider_get_stop_characters(provider) \ + ((*THUNAR_VFS_MIME_PROVIDER_GET_CLASS ((provider))->get_stop_characters) ((provider))) + +/** + * thunar_vfs_mime_provider_get_max_buffer_extents: + * @provider : a #ThunarVfsMimeProvider. + * + * Returns the max buffer extents required for a data lookup in @provider. + * + * Return value: the max buffer extents for @provider. + **/ +#define thunar_vfs_mime_provider_get_max_buffer_extents(provider) \ + ((*THUNAR_VFS_MIME_PROVIDER_GET_CLASS ((provider))->get_max_buffer_extents) ((provider))) + +G_END_DECLS; + +#endif /* !__THUNAR_VFS_MIME_PROVIDER_H__ */ diff --git a/thunar-vfs/thunar-vfs-mime.c b/thunar-vfs/thunar-vfs-mime.c index 347cd044a429854b26cafaff4e9915ff08072d53..4ffff4f128637a9f3cda073d98d1d72d5463b2d1 100644 --- a/thunar-vfs/thunar-vfs-mime.c +++ b/thunar-vfs/thunar-vfs-mime.c @@ -22,439 +22,44 @@ #include <config.h> #endif -#ifdef HAVE_MEMORY_H -#include <memory.h> -#endif -#ifdef HAVE_STRING_H -#include <string.h> -#endif - #include <thunar-vfs/thunar-vfs-mime.h> -#include <thunar-vfs/thunar-vfs-mime-parser.h> - -#include <xdgmime/xdgmime.h> - - - -static const struct -{ - const gchar *const type; - const gchar *const icon; -} GNOME_ICONNAMES[] = -{ - { "inode/directory", "gnome-fs-directory" }, - { "inode/blockdevice", "gnome-fs-blockdev" }, - { "inode/chardevice", "gnome-fs-chardev" }, - { "inode/fifo", "gnome-fs-fifo" }, - { "inode/socket", "gnome-fs-socket" }, -}; - - - -static void thunar_vfs_mime_info_register_type (GType *type); -static void thunar_vfs_mime_info_class_init (ThunarVfsMimeInfoClass *klass); -static void thunar_vfs_mime_info_finalize (ExoObject *object); -static ThunarVfsMimeInfo *thunar_vfs_mime_info_get_unlocked (const gchar *name); -static void thunar_vfs_mime_info_icon_theme_changed (GtkIconTheme *icon_theme, - ThunarVfsMimeInfo *info); - - - -struct _ThunarVfsMimeInfoClass -{ - ExoObjectClass __parent__; -}; - -struct _ThunarVfsMimeInfo -{ - ExoObject __parent__; - - gchar *comment; - gchar *name; - - const gchar *media; - const gchar *subtype; - - gchar *icon_name; - gboolean icon_name_static : 1; - GtkIconTheme *icon_theme; -}; - - - -static ExoObjectClass *thunar_vfs_mime_info_parent_class; -static GHashTable *mime_infos = NULL; -G_LOCK_DEFINE_STATIC (mime_infos); - - - -GType -thunar_vfs_mime_info_get_type (void) -{ - static GType type = G_TYPE_INVALID; - static GOnce once = G_ONCE_INIT; - - /* thread-safe type registration */ - g_once (&once, (GThreadFunc) thunar_vfs_mime_info_register_type, &type); - - return type; -} - - - -static void -thunar_vfs_mime_info_register_type (GType *type) -{ - static const GTypeInfo info = - { - sizeof (ThunarVfsMimeInfoClass), - NULL, - NULL, - (GClassInitFunc) thunar_vfs_mime_info_class_init, - NULL, - NULL, - sizeof (ThunarVfsMimeInfo), - 128u, - NULL, - NULL, - }; - - *type = g_type_register_static (EXO_TYPE_OBJECT, "ThunarVfsMimeInfo", &info, 0); -} - - - -static void -thunar_vfs_mime_info_class_init (ThunarVfsMimeInfoClass *klass) -{ - ExoObjectClass *exoobject_class; - - thunar_vfs_mime_info_parent_class = g_type_class_peek_parent (klass); - - exoobject_class = EXO_OBJECT_CLASS (klass); - exoobject_class->finalize = thunar_vfs_mime_info_finalize; -} - - - -static void -thunar_vfs_mime_info_finalize (ExoObject *object) -{ - ThunarVfsMimeInfo *info = THUNAR_VFS_MIME_INFO (object); - - g_return_if_fail (THUNAR_VFS_IS_MIME_INFO (info)); - - /* free the comment */ - if (info->comment != NULL && info->comment != info->name) - { -#ifndef G_DISABLE_CHECKS - memset (info->comment, 0xaa, strlen (info->comment) + 1); -#endif - g_free (info->comment); - } - - /* free the name */ -#ifndef G_DISABLE_CHECKS - if (G_LIKELY (info->name != NULL)) - memset (info->name, 0xaa, strlen (info->name) + 1); -#endif - g_free (info->name); - - /* disconnect from the icon theme (if any) */ - if (G_LIKELY (info->icon_theme != NULL)) - { - g_signal_handlers_disconnect_by_func (G_OBJECT (info->icon_theme), thunar_vfs_mime_info_icon_theme_changed, info); - g_object_unref (G_OBJECT (info->icon_theme)); - } - - /* free the icon name if it isn't one of the statics */ - if (G_LIKELY (!info->icon_name_static && info->icon_name != NULL)) - { -#ifndef G_DISABLE_CHECKS - memset (info->icon_name, 0xaa, strlen (info->icon_name) + 1); -#endif - g_free (info->icon_name); - } - - /* invoke the parent's finalize method */ - (*EXO_OBJECT_CLASS (thunar_vfs_mime_info_parent_class)->finalize) (object); -} - - - -static ThunarVfsMimeInfo* -thunar_vfs_mime_info_get_unlocked (const gchar *name) -{ - ThunarVfsMimeInfo *info; - const gchar *s; - const gchar *t; - const gchar *u; - gchar *v; - - /* check if we have a cached version of the mime type */ - info = g_hash_table_lookup (mime_infos, name); - if (info == NULL) - { - for (s = NULL, t = name; *t != '\0'; ++t) - if (G_UNLIKELY (*t == '/')) - s = t; - - /* we could do better here... */ - if (G_UNLIKELY (s == NULL)) - g_error ("Invalid MIME type '%s'", name); - - /* allocate the MIME info instance */ - info = exo_object_new (THUNAR_VFS_TYPE_MIME_INFO); - - /* allocate memory to store both the full name, - * as well as the media type alone. - */ - info->name = g_new (gchar, (t - name) + (s - name) + 2); - - /* copy full name (including the terminator) */ - for (u = name, v = info->name; u <= t; ++u, ++v) - *v = *u; - - /* set the subtype portion */ - info->subtype = info->name + (s - name) + 1; - - /* copy the media portion */ - info->media = v; - for (u = name; u < s; ++u, ++v) - *v = *u; - - /* terminate the media portion */ - *v = '\0'; - - /* insert the mime type into the cache */ - g_hash_table_insert (mime_infos, info->name, info); - } - - /* take a reference for the caller */ - return thunar_vfs_mime_info_ref (info); -} +#include <thunar-vfs/thunar-vfs-mime-database.h> -static void -thunar_vfs_mime_info_icon_theme_changed (GtkIconTheme *icon_theme, - ThunarVfsMimeInfo *info) -{ - g_return_if_fail (GTK_IS_ICON_THEME (icon_theme)); - g_return_if_fail (THUNAR_VFS_IS_MIME_INFO (info)); - g_return_if_fail (info->icon_theme == icon_theme); - - /* drop the cached icon name, so the next lookup - * call will perform a lookup again. - */ - if (G_LIKELY (!info->icon_name_static)) - { - g_free (info->icon_name); - info->icon_name = NULL; - } -} +static ThunarVfsMimeDatabase *mime_database = NULL; /** - * thunar_vfs_mime_info_get_comment: - * @info : a #ThunarVfsMimeInfo. - * - * Determines the description for the given @info. - * - * Note that this method MUST NOT be called from threads other than - * the main thread, because it's not thread-safe! - * - * Return value: the comment associated with the @info or the empty string - * if no comment was provided. - */ -const gchar* -thunar_vfs_mime_info_get_comment (ThunarVfsMimeInfo *info) -{ - gchar *path; - gchar *spec; - - g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); - - if (G_UNLIKELY (info->comment == NULL)) - { - spec = g_strdup_printf ("mime/%s.xml", info->name); - path = xfce_resource_lookup (XFCE_RESOURCE_DATA, spec); - g_free (spec); - - if (G_LIKELY (path != NULL)) - { - info->comment = _thunar_vfs_mime_parser_load_comment_from_file (path, NULL); - g_free (path); - } - - if (G_UNLIKELY (info->comment == NULL)) - info->comment = info->name; - } - - return info->comment; -} - - - -/** - * thunar_vfs_mime_info_get_name: - * @info : a #ThunarVfsMimeInfo. - * - * Returns the full qualified name of the MIME type - * described by the @info object. - * - * Return value: the name of @info. - **/ -const gchar* -thunar_vfs_mime_info_get_name (const ThunarVfsMimeInfo *info) -{ - g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); - return info->name; -} - - - -/** - * thunar_vfs_mime_info_get_media: - * @info : a #ThunarVfsMimeInfo. - * - * Returns the media portion of the MIME type, e.g. if your - * #ThunarVfsMimeInfo instance refers to "text/plain", invoking - * this method will return "text". - * - * Return value: the media portion of the MIME type. - **/ -const gchar* -thunar_vfs_mime_info_get_media (const ThunarVfsMimeInfo *info) -{ - g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); - return info->media; -} - - - -/** - * thunar_vfs_mime_info_get_subtype: - * @info : a #ThunarVfsMimeInfo. - * - * Returns the subtype portion of the MIME type, e.g. if @info - * refers to "application/octect-stream", this method will - * return "octect-stream". - * - * Return value: the subtype portion of @info. - **/ -const gchar* -thunar_vfs_mime_info_get_subtype (const ThunarVfsMimeInfo *info) -{ - g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); - return info->subtype; -} - - - -/** - * thunar_vfs_mime_info_lookup_icon_name: - * @info : a #ThunarVfsMimeInfo. - * @icon_theme : the #GtkIconTheme on which to perform the lookup. - * - * Tries to determine the name of a suitable icon for @info - * in @icon_theme. The returned icon name can then be used - * in calls to #gtk_icon_theme_lookup_icon() or - * #gtk_icon_theme_load_icon(). - * - * Note that this method MUST NOT be called from threads other than - * the main thread, because it's not thread-safe! + * _thunar_vfs_mime_init: * - * Return value: a suitable icon name for @info in @icon_theme. + * Initializes the MIME component of the ThunarVFS + * library. **/ -const gchar* -thunar_vfs_mime_info_lookup_icon_name (ThunarVfsMimeInfo *info, - GtkIconTheme *icon_theme) +void +_thunar_vfs_mime_init (void) { - gsize n; - - g_return_val_if_fail (THUNAR_VFS_IS_MIME_INFO (info), NULL); - g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL); - - /* check if our cached name will suffice */ - if (G_LIKELY (info->icon_theme == icon_theme && info->icon_name != NULL)) - return info->icon_name; - - /* if we have a new icon theme, connect to the new one */ - if (G_UNLIKELY (info->icon_theme != icon_theme)) - { - /* disconnect from the previous one */ - if (G_LIKELY (info->icon_theme != NULL)) - { - g_signal_handlers_disconnect_by_func (G_OBJECT (info->icon_theme), thunar_vfs_mime_info_icon_theme_changed, info); - g_object_unref (G_OBJECT (info->icon_theme)); - } + g_return_if_fail (mime_database == NULL); - /* connect to the new one */ - info->icon_theme = icon_theme; - g_object_ref (G_OBJECT (icon_theme)); - g_signal_connect (G_OBJECT (icon_theme), "changed", G_CALLBACK (thunar_vfs_mime_info_icon_theme_changed), info); - } - - /* free the previously set icon name */ - if (!info->icon_name_static) - g_free (info->icon_name); - - /* start out with the full name (assuming a non-static icon_name) */ - info->icon_name = g_strdup_printf ("gnome-mime-%s-%s", info->media, info->subtype); - info->icon_name_static = FALSE; - if (!gtk_icon_theme_has_icon (icon_theme, info->icon_name)) - { - /* only the media portion */ - info->icon_name[11 + ((info->subtype - 1) - info->name)] = '\0'; - if (!gtk_icon_theme_has_icon (icon_theme, info->icon_name)) - { - /* if we get here, we'll use a static icon name */ - info->icon_name_static = TRUE; - g_free (info->icon_name); - - /* GNOME uses non-standard names for special MIME types */ - for (n = 0; n < G_N_ELEMENTS (GNOME_ICONNAMES); ++n) - if (exo_str_is_equal (info->name, GNOME_ICONNAMES[n].type)) - if (gtk_icon_theme_has_icon (icon_theme, GNOME_ICONNAMES[n].icon)) - { - info->icon_name = (gchar *) GNOME_ICONNAMES[n].icon; - break; - } - - /* fallback is always application/octect-stream */ - if (n == G_N_ELEMENTS (GNOME_ICONNAMES)) - info->icon_name = (gchar *) "gnome-mime-application-octect-stream"; - } - } - - g_assert (info->icon_name != NULL); - g_assert (info->icon_name[0] != '\0'); - - return info->icon_name; + mime_database = thunar_vfs_mime_database_get (); } - - /** - * _thunar_vfs_mime_init: + * _thunar_vfs_mime_shutdown: * - * Initializes the MIME component of the ThunarVFS + * Shuts down the MIME component of the ThunarVFS * library. **/ void -_thunar_vfs_mime_init (void) +_thunar_vfs_mime_shutdown (void) { - g_return_if_fail (mime_infos == NULL); - - /* initialize the XDG mime library */ - xdg_mime_init (); + g_return_if_fail (mime_database != NULL); - /* allocate a string chunk for the mime types */ - mime_infos = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) thunar_vfs_mime_info_unref); + exo_object_unref (EXO_OBJECT (mime_database)); + mime_database = NULL; } @@ -474,17 +79,7 @@ _thunar_vfs_mime_init (void) ThunarVfsMimeInfo* thunar_vfs_mime_info_get (const gchar *mime_type) { - ThunarVfsMimeInfo *info; - - g_return_val_if_fail (mime_type != NULL, NULL); - g_return_val_if_fail (strchr (mime_type, '/') != NULL, NULL); - g_return_val_if_fail (strchr (mime_type, '/') == strrchr (mime_type, '/'), NULL); - - G_LOCK (mime_infos); - info = thunar_vfs_mime_info_get_unlocked (mime_type); - G_UNLOCK (mime_infos); - - return info; + return thunar_vfs_mime_database_get_info (mime_database, mime_type); } @@ -504,18 +99,7 @@ thunar_vfs_mime_info_get (const gchar *mime_type) ThunarVfsMimeInfo* thunar_vfs_mime_info_get_for_file (const gchar *path) { - ThunarVfsMimeInfo *info; - const gchar *name; - - g_return_val_if_fail (g_path_is_absolute (path), NULL); - g_return_val_if_fail (mime_infos != NULL, NULL); - - G_LOCK (mime_infos); - name = xdg_mime_get_mime_type_for_file (path); - info = thunar_vfs_mime_info_get_unlocked (name); - G_UNLOCK (mime_infos); - - return info; + return thunar_vfs_mime_database_get_info_for_file (mime_database, path, NULL); } diff --git a/thunar-vfs/thunar-vfs-mime.h b/thunar-vfs/thunar-vfs-mime.h index 5db95df1c4615f6869de75f0837e984d5f482553..b3d6197ad84700ce7bdccd3def51f629a036727c 100644 --- a/thunar-vfs/thunar-vfs-mime.h +++ b/thunar-vfs/thunar-vfs-mime.h @@ -21,55 +21,13 @@ #ifndef __THUNAR_VFS_MIME_H__ #define __THUNAR_VFS_MIME_H__ -#include <exo/exo.h> +#include <thunar-vfs/thunar-vfs-mime-info.h> G_BEGIN_DECLS; -typedef struct _ThunarVfsMimeInfoClass ThunarVfsMimeInfoClass; -typedef struct _ThunarVfsMimeInfo ThunarVfsMimeInfo; - -#define THUNAR_VFS_TYPE_MIME_INFO (thunar_vfs_mime_info_get_type ()) -#define THUNAR_VFS_MIME_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THUNAR_VFS_TYPE_MIME_INFO, ThunarVfsMimeInfo)) -#define THUNAR_VFS_MIME_INFO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), THUNAR_VFS_TYPE_MIME_INFO, ThunarVfsMimeInfoClass)) -#define THUNAR_VFS_IS_MIME_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THUNAR_VFS_TYPE_MIME_INFO)) -#define THUNAR_VFS_IS_MIME_INFO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_VFS_TYPE_MIME_INFO)) -#define THUNAR_VFS_MIME_INFO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_VFS_TYPE_MIME_INFO, ThunarVfsMimeInfoClass)) - -GType thunar_vfs_mime_info_get_type (void) G_GNUC_CONST; - ThunarVfsMimeInfo *thunar_vfs_mime_info_get (const gchar *mime_type); ThunarVfsMimeInfo *thunar_vfs_mime_info_get_for_file (const gchar *path); -const gchar *thunar_vfs_mime_info_get_comment (ThunarVfsMimeInfo *info) G_GNUC_PURE; -const gchar *thunar_vfs_mime_info_get_name (const ThunarVfsMimeInfo *info) G_GNUC_PURE; - -const gchar *thunar_vfs_mime_info_get_media (const ThunarVfsMimeInfo *info) G_GNUC_PURE; -const gchar *thunar_vfs_mime_info_get_subtype (const ThunarVfsMimeInfo *info) G_GNUC_PURE; - -const gchar *thunar_vfs_mime_info_lookup_icon_name (ThunarVfsMimeInfo *info, - GtkIconTheme *icon_theme); - -/** - * thunar_vfs_mime_info_ref: - * @info : a #ThunarVfsMimeInfo. - * - * Increments the reference count by 1 on @info, and - * returns a pointer to @info. - * - * Return value: a pointer to @info. - **/ -#define thunar_vfs_mime_info_ref exo_object_ref - -/** - * thunar_vfs_mime_info_unref: - * @info : a #ThunarVfsMimeInfo. - * - * Decrements the reference count on @info by - * 1. If the reference count drops to zero, the - * resources allocated to @info will be freed. - **/ -#define thunar_vfs_mime_info_unref exo_object_unref - G_END_DECLS; #endif /* !__THUNAR_VFS_MIME_H__ */ diff --git a/thunar-vfs/thunar-vfs.c b/thunar-vfs/thunar-vfs.c index adaa43948ed969eaa0523f07a202a469312a06c1..ef0e3f5b41e15422c8a8ac43dee8fe9f4a55c345 100644 --- a/thunar-vfs/thunar-vfs.c +++ b/thunar-vfs/thunar-vfs.c @@ -26,6 +26,10 @@ +static gint thunar_vfs_ref_count = 0; + + + /** * thunar_vfs_init: * @@ -37,15 +41,32 @@ thunar_vfs_init (void) extern void _thunar_vfs_job_init (void); extern void _thunar_vfs_mime_init (void); - static gboolean initialized = FALSE; - - if (!initialized) + if (g_atomic_int_exchange_and_add (&thunar_vfs_ref_count, 1) == 0) { - _thunar_vfs_job_init (); _thunar_vfs_mime_init (); + _thunar_vfs_job_init (); + } +} + - initialized = TRUE; + +/** + * thunar_vfs_shutdown: + * + * Shuts down the ThunarVFS library. + **/ +void +thunar_vfs_shutdown (void) +{ + extern void _thunar_vfs_job_shutdown (void); + extern void _thunar_vfs_mime_shutdown (void); + + if (g_atomic_int_dec_and_test (&thunar_vfs_ref_count)) + { + _thunar_vfs_job_shutdown (); + _thunar_vfs_mime_shutdown (); } } + diff --git a/thunar-vfs/thunar-vfs.h b/thunar-vfs/thunar-vfs.h index 1b36aa229b897e158b992cbf1336c30fad961d17..c2453d82a03657b32eb67846e516bcd551097cc8 100644 --- a/thunar-vfs/thunar-vfs.h +++ b/thunar-vfs/thunar-vfs.h @@ -26,6 +26,8 @@ #include <thunar-vfs/thunar-vfs-interactive-job.h> #include <thunar-vfs/thunar-vfs-job.h> #include <thunar-vfs/thunar-vfs-jobs.h> +#include <thunar-vfs/thunar-vfs-mime-database.h> +#include <thunar-vfs/thunar-vfs-mime-info.h> #include <thunar-vfs/thunar-vfs-mime.h> #include <thunar-vfs/thunar-vfs-monitor.h> #include <thunar-vfs/thunar-vfs-trash.h> @@ -36,7 +38,8 @@ G_BEGIN_DECLS; -void thunar_vfs_init (void); +void thunar_vfs_init (void); +void thunar_vfs_shutdown (void); G_END_DECLS; diff --git a/thunar-vfs/xdgmime/ChangeLog b/thunar-vfs/xdgmime/ChangeLog deleted file mode 100644 index 1197652361931716ef7f3dd3c710e3a2b448e8e1..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/ChangeLog +++ /dev/null @@ -1,285 +0,0 @@ -Thu Jun 9 23:55:25 2005 Jonathan Blandford <jrb@redhat.com> - - * src/xdgmime.c (xdg_mime_init_from_directory): patch from - federico to realloc the right size, #3506 - -2005-04-17 Christophe Fergeau <teuf@gnome.org> - - * src/xdgmimeint.c: fix gcc4 signedness warning - -2005-04-17 Christophe Fergeau <teuf@gnome.org> - - * src/xdgmimemagic.c: (_xdg_mime_magic_matchlet_compare_to_data), - (_xdg_mime_magic_matchlet_compare_level), - (_xdg_mime_magic_lookup_data): when magic patterns matches, check if - there aren't subtypes with matching patterns too, and if so, favour - the subtype over the parent type, should fix #2686 - -2005-04-16 Christophe Fergeau <teuf@gnome.org> - - * src/xdgmime.c: (xdg_mime_init_from_directory): fix leak when - mime.cache doesn't exist - -2005-04-16 Christophe Fergeau <teuf@gnome.org> - - * src/test-mime.c: (main): disabled call to xdg_mime_dump for now - * src/xdgmimemagic.c: (_xdg_mime_magic_matchlet_compare_to_data): - fixed off by 1 error when handling offsets, fixes bug #2050 and - partly bug #2359 - -Fri Apr 8 23:37:33 2005 Jonathan Blandford <jrb@redhat.com> - - * src/xdgmimecache.c: Actually add the file. Also, patch from - Matthias Clasen <mclasen@redhat.com> to fix small bugs, #2939 - -Fri Apr 1 14:59:43 2005 Jonathan Blandford <jrb@redhat.com> - - * src/xdgmimecache.c: Patch from Matthias Clasen to mmap the - cached xdg file. - -Mon Mar 28 13:58:32 2005 Jonathan Blandford <jrb@redhat.com> - - * src/xdgmimeglob.c (_xdg_glob_hash_insert_text): patch from - Matthias Clasen to handle globs that don't have '.' chars in - them. As an example 'foo~' should match '*~' - -Mon Mar 21 13:16:12 2005 Jonathan Blandford <jrb@redhat.com> - - * src/xdgmime.c (xdg_mime_shutdown): fix from Axel Liljencrantz - <f97-ali@nada.kth.se> to free parent_list in shutdown. - -2005-01-10 Christophe Fergeau <teuf@gnome.org> - - * src/xdgmimeglob.c: (_xdg_glob_hash_lookup_file_name): make previous - commit actually work... - -2005-01-10 Christophe Fergeau <teuf@gnome.org> - - * src/xdgmimeglob.c: (_xdg_glob_hash_lookup_file_name): don't get - confused by multiple dots in filenames when doing extension matching - -2004-12-13 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmime.h: - - wrap new API in XDG_ENTRY() - -2004-12-13 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimeglob.c: (_xdg_glob_hash_lookup_file_name): - - Do not assume the filename is UTF8. We just need to look - for the dot which is ASCII. - -2004-12-09 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimeint.h: - - Remove spacings I introduced by mistake - -2004-12-09 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimealias.c: (_xdg_mime_alias_read_from_file): - * src/xdgmimeint.c: (_xdg_ucs4_to_lower): - * src/xdgmimeint.h: - * src/xdgmimeparent.c: (_xdg_mime_parent_read_from_file): - - Check in Mariano Suárez-Alvarez <msuarezalvarez@arnet.com.ar> patch - for GNOME bug #160838. - -2004-12-09 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimeglob.c: (_xdg_glob_hash_node_lookup_file_name): - * src/xdgmimeint.c: (_xdg_ucs4_to_lower): - * src/xdgmimeint.h: - - Follow the freedesktop spec about case sensitiveness. Fix #732 - -2004-12-08 Christophe Fergeau <teuf@gnome.org> - - * src/xdgmimeglob.c: (_xdg_mime_glob_read_from_file): backing out - "fix" for bug #1048 since it frees memory that shouldn't be freed. - -2004-12-08 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimemagic.c: (_xdg_mime_magic_read_from_file): - - Check that fread succeeded reading all chars. Fix #1049 - -2004-12-08 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmime.c: - * src/xdgmimealias.c: - * src/xdgmimeglob.c: - * src/xdgmimeint.c: - * src/xdgmimemagic.c: - * src/xdgmimeparent.c: - - Include config.h. Fix #913 - -2004-12-08 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimealias.c: (_xdg_mime_alias_list_lookup): - - Fix a typo - -2004-12-08 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmime.c: (xdg_mime_unalias_mime_type), - (xdg_mime_mime_type_equal), (xdg_mime_mime_type_subclass), - (xdg_mime_get_mime_parents): - * src/xdgmime.h: - - Add apis to get parents and to unalias mime type - -2004-12-08 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimealias.c: (_xdg_mime_alias_list_lookup): - * src/xdgmimeparent.c: (_xdg_mime_parent_list_lookup): - - Protect against stupid bsearch() implementations. (#1961, - Morten Welinder) - -2004-12-08 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimeparent.c: (_xdg_mime_parent_read_from_file): - - Initialize the parent field of the newly allocate list - entry. (#1916, Alex Larsson) - -2004-12-08 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimeglob.c: (_xdg_mime_glob_read_from_file): - - Patch from Matthias Clasen <mclasen@redhat.com> to fix - a mem leak. Bug #1048 - -2004-12-08 Marco Pesenti Gritti <marco@gnome.org> - - * src/xdgmimeglob.h: - - Patch from Michael.Wilson@bull.net to fix compile error on AIX - -Sun Nov 7 02:25:21 2004 Jonathan Blandford <jrb@redhat.com> - - * src/xdgmime.h: Patch from Matthias Clasen <mclasen@redhat.com> - to add alias and inheritence support. - -2004-09-16 Christophe Fergeau <teuf@gnome.org> - - * src/xdgmimeglob.c: (_xdg_glob_hash_free_nodes): - * src/xdgmimemagic.c: (_xdg_mime_magic_free): fix memory leaks, - fixes http://bugzilla.gnome.org/show_bug.cgi?id=152771 and - http://bugzilla.gnome.org/show_bug.cgi?id=152768 - -Mon Jul 19 00:23:00 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmime.c (xdg_mime_register_reload_callback): register a - callback when we reload MIME data. - - * src/xdgmime.c (xdg_mime_remove_callback): Add capability to - remove callback. - -Sun Jul 18 20:56:22 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmime.c (xdg_mime_shutdown): - (xdg_mime_init): reread data when it changes on disk. - -Thu May 27 16:18:14 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmime.h: move xdg_mime_shutdown into the XDG_ENTRY guard. - -Thu May 27 15:02:13 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmimemagic.c (_xdg_mime_magic_read_magic_file): patch from - Hongli Lai <h.lai@chello.nl> to catch magic files that don't end - with a '\n'. - -Fri Apr 30 11:56:01 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmimemagic.c (_xdg_mime_magic_read_a_number): make the - buffer the right size. Reported by Morten Welinder, #136323 - -Sun Mar 21 23:56:46 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmimemagic.c: Patch from Arjan van de Ven - <arjanv@redhat.com> to do s/fgetc/getc_unlocked/g. - -Wed Mar 10 22:28:41 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmimemagic.c (_xdg_mime_magic_read_a_number): fix usage of - isdigit. Reported by Morten Welinder, #136323 - - * src/xdgmimemagic.c (_xdg_mime_magic_read_magic_file): patch from - Christophe Fergeau to reverse the order of the matchlet before - adding it to the list. - - * src/xdgmimeint.h (_xdg_utf8_skip): patch from Alexander Larsson - to make extern. - -Wed Jan 21 09:29:41 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmimemagic.c (_xdg_mime_magic_insert_match): dropped - patches. Fix. - - * src/xdgmimeglob.c (_xdg_glob_hash_free_nodes): dropped patches. - Fix. - -Tue Jan 20 14:55:39 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmime.h (XDG_MIME_TYPE_UNKNOWN): move the definition so - that it catches the XDG_ENTRY mangling. - - * src/xdgmimemagic.c: make some functions static - -Tue Jan 20 14:34:26 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmime.c (xdg_mime_get_max_buffer_extents): add function so - that it's easy to get the max buffer extents. - -Tue Jan 20 12:55:55 2004 Jonathan Blandford <jrb@gnome.org> - - * src/Makefile: Test prefix code - - * src/xdgmime*.h: Fully use the prefix code - - * src/xdgmime.c: finish the syncing from both GTK+ and gnome-vfs. - -Tue Jan 13 16:21:04 2004 Jonathan Blandford <jrb@gnome.org> - - * src/xdgmime.[ch] (XDG_MIME_TYPE_UNKNOWN): make an extern const - char * so that comparisons can work. - - * src/xdgmimeint.c (_xdg_utf8_to_ucs4): patch from Dave Jones - <davej@redhat.com> to make operations more explicit. - -Tue Oct 28 15:09:06 2003 Jonathan Blandford <jrb@redhat.com> - - * README: Add a readme, and clarify the licensing terms of the - software. - -Tue Oct 28 14:47:37 2003 Jonathan Blandford <jrb@redhat.com> - - * src/xdgmime.c (xdg_mime_shutdown): implement shutdown. This - frees all memory and resets to an uninitialized state as best as - possible. - -Mon Oct 27 11:45:58 2003 Jonathan Blandford <jrb@redhat.com> - - * src/xdgmimemagic.c (_xdg_mime_magic_read_a_number): strtol - returns a long, not an int. Thanks to Manish Singh for pointing - this out. - - * src/xdgmimemagic.c (_xdg_mime_magic_parse_magic_line): change - assertion to avoid a warning. - -Tue Oct 21 15:56:55 2003 Jonathan Blandford <jrb@gnome.org> - - * Makefile: add a simple makefile - * src/Makefile: ditto - -Tue Jul 22 15:37:45 2003 Jonathan Blandford <jrb@gnome.org> - - * xdgmime/xdgmime.c (xdg_mime_init): use XDG_DATA_HOME instead of - XDG_CONFIG_HOME. - diff --git a/thunar-vfs/xdgmime/Makefile.am b/thunar-vfs/xdgmime/Makefile.am deleted file mode 100644 index 92c4b8f185784db777977e2bf98cc1322702babb..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/Makefile.am +++ /dev/null @@ -1,40 +0,0 @@ -# $Id$ - -INCLUDES = \ - -DXDG_PREFIX=_thunar_vfs_xdg - -noinst_LTLIBRARIES = \ - libxdgmime.la - -libxdgmime_la_SOURCES = \ - xdgmime.c \ - xdgmime.h \ - xdgmimealias.c \ - xdgmimealias.h \ - xdgmimecache.c \ - xdgmimecache.h \ - xdgmimeglob.c \ - xdgmimeglob.h \ - xdgmimeint.c \ - xdgmimeint.h \ - xdgmimemagic.c \ - xdgmimemagic.h \ - xdgmimeparent.c \ - xdgmimeparent.h - -TESTS = \ - test-mime - -check_PROGRAMS = \ - test-mime - -test_mime_SOURCES = \ - test-mime.c - -test_mime_DEPENDENCIES = \ - $(top_builddir)/thunar-vfs/xdgmime/libxdgmime.la - -test_mime_LDADD = \ - $(top_builddir)/thunar-vfs/xdgmime/libxdgmime.la - -# vi:set ts=8 sw=8 noet ai nocindent syntax=automake: diff --git a/thunar-vfs/xdgmime/test-mime.c b/thunar-vfs/xdgmime/test-mime.c deleted file mode 100644 index b0bcc875fe43159260e6d119276414fbe2639d59..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/test-mime.c +++ /dev/null @@ -1,120 +0,0 @@ -#include "xdgmime.h" -#include "xdgmimeglob.h" -#include <string.h> -#include <stdio.h> - - -static void -test_individual_glob (const char *glob, - XdgGlobType expected_type) -{ - XdgGlobType test_type; - - test_type = _xdg_glob_determine_type (glob); - if (test_type != expected_type) - { - printf ("Test Failed: %s is of type %s, but %s is expected\n", - glob, - ((test_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL": - ((test_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_FULL")), - ((expected_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL": - ((expected_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_COMPLEX"))); - } -} - -static void -test_glob_type (void) -{ - test_individual_glob ("*.gif", XDG_GLOB_SIMPLE); - test_individual_glob ("Foo*.gif", XDG_GLOB_FULL); - test_individual_glob ("*[4].gif", XDG_GLOB_FULL); - test_individual_glob ("Makefile", XDG_GLOB_LITERAL); - test_individual_glob ("sldkfjvlsdf\\\\slkdjf", XDG_GLOB_FULL); - test_individual_glob ("tree.[ch]", XDG_GLOB_FULL); -} - -static void -test_alias (const char *mime_a, - const char *mime_b, - int expected) -{ - int actual; - - actual = xdg_mime_mime_type_equal (mime_a, mime_b); - - if (actual != expected) - { - printf ("Test Failed: %s is %s to %s\n", - mime_a, actual ? "equal" : "not equal", mime_b); - } -} - -static void -test_aliasing (void) -{ - test_alias ("application/wordperfect", "application/vnd.wordperfect", 1); - test_alias ("application/x-gnome-app-info", "application/x-desktop", 1); - test_alias ("application/x-wordperfect", "application/vnd.wordperfect", 1); - test_alias ("application/x-wordperfect", "audio/x-midi", 0); - test_alias ("/", "vnd/vnd", 0); - test_alias ("application/octet-stream", "text/plain", 0); - test_alias ("text/plain", "text/*", 0); -} - -static void -test_subclass (const char *mime_a, - const char *mime_b, - int expected) -{ - int actual; - - actual = xdg_mime_mime_type_subclass (mime_a, mime_b); - - if (actual != expected) - { - printf ("Test Failed: %s is %s of %s\n", - mime_a, actual ? "subclass" : "not subclass", mime_b); - } -} - -static void -test_subclassing (void) -{ - test_subclass ("application/rtf", "text/plain", 1); - test_subclass ("message/news", "text/plain", 1); - test_subclass ("message/news", "message/*", 1); - test_subclass ("message/news", "text/*", 1); - test_subclass ("message/news", "application/octet-stream", 1); - test_subclass ("application/rtf", "application/octet-stream", 1); - test_subclass ("application/x-gnome-app-info", "text/plain", 1); - test_subclass ("image/x-djvu", "image/vnd.djvu", 1); - test_subclass ("image/vnd.djvu", "image/x-djvu", 1); - test_subclass ("image/vnd.djvu", "text/plain", 0); - test_subclass ("image/vnd.djvu", "text/*", 0); - test_subclass ("text/*", "text/plain", 0); -} - -int -main (int argc, char *argv[]) -{ - const char *result; - const char *file_name; - int i; - - test_glob_type (); - test_aliasing (); - test_subclassing (); - - for (i = 1; i < argc; i++) - { - file_name = argv[i]; - result = xdg_mime_get_mime_type_for_file (file_name); - printf ("File \"%s\" has a mime-type of %s\n", file_name, result); - } - -#if 0 - xdg_mime_dump (); -#endif - return 0; -} - diff --git a/thunar-vfs/xdgmime/xdgmime.c b/thunar-vfs/xdgmime/xdgmime.c deleted file mode 100644 index 910c51210e318d6b1e8051ea18de154ebbc30888..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmime.c +++ /dev/null @@ -1,787 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmime.c: XDG Mime Spec mime resolver. Based on version 0.11 of the spec. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2003,2004 Red Hat, Inc. - * Copyright (C) 2003,2004 Jonathan Blandford <jrb@alum.mit.edu> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include "xdgmime.h" -#include "xdgmimeint.h" -#include "xdgmimeglob.h" -#include "xdgmimemagic.h" -#include "xdgmimealias.h" -#include "xdgmimeparent.h" -#include "xdgmimecache.h" -#include <stdio.h> -#include <string.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <sys/time.h> -#include <fcntl.h> -#include <unistd.h> -#include <assert.h> - -typedef struct XdgDirTimeList XdgDirTimeList; -typedef struct XdgCallbackList XdgCallbackList; - -static int need_reread = TRUE; -static time_t last_stat_time = 0; - -static XdgGlobHash *global_hash = NULL; -static XdgMimeMagic *global_magic = NULL; -static XdgAliasList *alias_list = NULL; -static XdgParentList *parent_list = NULL; -static XdgDirTimeList *dir_time_list = NULL; -static XdgCallbackList *callback_list = NULL; -XdgMimeCache **caches = NULL; -int n_caches = 0; - -const char *xdg_mime_type_unknown = "application/octet-stream"; - - -enum -{ - XDG_CHECKED_UNCHECKED, - XDG_CHECKED_VALID, - XDG_CHECKED_INVALID -}; - -struct XdgDirTimeList -{ - time_t mtime; - char *directory_name; - int checked; - XdgDirTimeList *next; -}; - -struct XdgCallbackList -{ - XdgCallbackList *next; - XdgCallbackList *prev; - int callback_id; - XdgMimeCallback callback; - void *data; - XdgMimeDestroy destroy; -}; - -/* Function called by xdg_run_command_on_dirs. If it returns TRUE, further - * directories aren't looked at */ -typedef int (*XdgDirectoryFunc) (const char *directory, - void *user_data); - -static XdgDirTimeList * -xdg_dir_time_list_new (void) -{ - XdgDirTimeList *retval; - - retval = calloc (1, sizeof (XdgDirTimeList)); - retval->checked = XDG_CHECKED_UNCHECKED; - - return retval; -} - -static void -xdg_dir_time_list_free (XdgDirTimeList *list) -{ - XdgDirTimeList *next; - - while (list) - { - next = list->next; - free (list->directory_name); - free (list); - list = next; - } -} - -static int -xdg_mime_init_from_directory (const char *directory) -{ - char *file_name; - struct stat st; - XdgDirTimeList *list; - - assert (directory != NULL); - - file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1); - strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache"); - if (stat (file_name, &st) == 0) - { - XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name); - - if (cache != NULL) - { - list = xdg_dir_time_list_new (); - list->directory_name = file_name; - list->mtime = st.st_mtime; - list->next = dir_time_list; - dir_time_list = list; - - caches = realloc (caches, sizeof (XdgMimeCache *) * (n_caches + 1)); - caches[n_caches] = cache; - n_caches++; - - return FALSE; - } - } - free (file_name); - - file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1); - strcpy (file_name, directory); strcat (file_name, "/mime/globs"); - if (stat (file_name, &st) == 0) - { - _xdg_mime_glob_read_from_file (global_hash, file_name); - - list = xdg_dir_time_list_new (); - list->directory_name = file_name; - list->mtime = st.st_mtime; - list->next = dir_time_list; - dir_time_list = list; - } - else - { - free (file_name); - } - - file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1); - strcpy (file_name, directory); strcat (file_name, "/mime/magic"); - if (stat (file_name, &st) == 0) - { - _xdg_mime_magic_read_from_file (global_magic, file_name); - - list = xdg_dir_time_list_new (); - list->directory_name = file_name; - list->mtime = st.st_mtime; - list->next = dir_time_list; - dir_time_list = list; - } - else - { - free (file_name); - } - - file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1); - strcpy (file_name, directory); strcat (file_name, "/mime/aliases"); - _xdg_mime_alias_read_from_file (alias_list, file_name); - free (file_name); - - file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1); - strcpy (file_name, directory); strcat (file_name, "/mime/subclasses"); - _xdg_mime_parent_read_from_file (parent_list, file_name); - free (file_name); - - return FALSE; /* Keep processing */ -} - -/* Runs a command on all the directories in the search path */ -static void -xdg_run_command_on_dirs (XdgDirectoryFunc func, - void *user_data) -{ - const char *xdg_data_home; - const char *xdg_data_dirs; - const char *ptr; - - xdg_data_home = getenv ("XDG_DATA_HOME"); - if (xdg_data_home) - { - if ((func) (xdg_data_home, user_data)) - return; - } - else - { - const char *home; - - home = getenv ("HOME"); - if (home != NULL) - { - char *guessed_xdg_home; - int stop_processing; - - guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1); - strcpy (guessed_xdg_home, home); - strcat (guessed_xdg_home, "/.local/share/"); - stop_processing = (func) (guessed_xdg_home, user_data); - free (guessed_xdg_home); - - if (stop_processing) - return; - } - } - - xdg_data_dirs = getenv ("XDG_DATA_DIRS"); - if (xdg_data_dirs == NULL) - xdg_data_dirs = "/usr/local/share/:/usr/share/"; - - ptr = xdg_data_dirs; - - while (*ptr != '\000') - { - const char *end_ptr; - char *dir; - int len; - int stop_processing; - - end_ptr = ptr; - while (*end_ptr != ':' && *end_ptr != '\000') - end_ptr ++; - - if (end_ptr == ptr) - { - ptr++; - continue; - } - - if (*end_ptr == ':') - len = end_ptr - ptr; - else - len = end_ptr - ptr + 1; - dir = malloc (len + 1); - strncpy (dir, ptr, len); - dir[len] = '\0'; - stop_processing = (func) (dir, user_data); - free (dir); - - if (stop_processing) - return; - - ptr = end_ptr; - } -} - -/* Checks file_path to make sure it has the same mtime as last time it was - * checked. If it has a different mtime, or if the file doesn't exist, it - * returns FALSE. - * - * FIXME: This doesn't protect against permission changes. - */ -static int -xdg_check_file (const char *file_path) -{ - struct stat st; - - /* If the file exists */ - if (stat (file_path, &st) == 0) - { - XdgDirTimeList *list; - - for (list = dir_time_list; list; list = list->next) - { - if (! strcmp (list->directory_name, file_path) && - st.st_mtime == list->mtime) - { - if (list->checked == XDG_CHECKED_UNCHECKED) - list->checked = XDG_CHECKED_VALID; - else if (list->checked == XDG_CHECKED_VALID) - list->checked = XDG_CHECKED_INVALID; - - return (list->checked != XDG_CHECKED_VALID); - } - } - return TRUE; - } - - return FALSE; -} - -static int -xdg_check_dir (const char *directory, - int *invalid_dir_list) -{ - int invalid; - char *file_name; - - assert (directory != NULL); - - /* Check the globs file */ - file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1); - strcpy (file_name, directory); strcat (file_name, "/mime/globs"); - invalid = xdg_check_file (file_name); - free (file_name); - if (invalid) - { - *invalid_dir_list = TRUE; - return TRUE; - } - - /* Check the magic file */ - file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1); - strcpy (file_name, directory); strcat (file_name, "/mime/magic"); - invalid = xdg_check_file (file_name); - free (file_name); - if (invalid) - { - *invalid_dir_list = TRUE; - return TRUE; - } - - /* Check the mime.cache file */ - file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1); - strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache"); - invalid = xdg_check_file (file_name); - free (file_name); - if (invalid) - { - *invalid_dir_list = TRUE; - return TRUE; - } - - return FALSE; /* Keep processing */ -} - -/* Walks through all the mime files stat()ing them to see if they've changed. - * Returns TRUE if they have. */ -static int -xdg_check_dirs (void) -{ - XdgDirTimeList *list; - int invalid_dir_list = FALSE; - - for (list = dir_time_list; list; list = list->next) - list->checked = XDG_CHECKED_UNCHECKED; - - xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_check_dir, - &invalid_dir_list); - - if (invalid_dir_list) - return TRUE; - - for (list = dir_time_list; list; list = list->next) - { - if (list->checked != XDG_CHECKED_VALID) - return TRUE; - } - - return FALSE; -} - -/* We want to avoid stat()ing on every single mime call, so we only look for - * newer files every 5 seconds. This will return TRUE if we need to reread the - * mime data from disk. - */ -static int -xdg_check_time_and_dirs (void) -{ - struct timeval tv; - time_t current_time; - int retval = FALSE; - - gettimeofday (&tv, NULL); - current_time = tv.tv_sec; - - if (current_time >= last_stat_time + 60) - { - retval = xdg_check_dirs (); - last_stat_time = current_time; - } - - return retval; -} - -/* Called in every public function. It reloads the hash function if need be. - */ -void -xdg_mime_init (void) -{ - if (xdg_check_time_and_dirs ()) - { - xdg_mime_shutdown (); - } - - if (need_reread) - { - global_hash = _xdg_glob_hash_new (); - global_magic = _xdg_mime_magic_new (); - alias_list = _xdg_mime_alias_list_new (); - parent_list = _xdg_mime_parent_list_new (); - - xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory, - NULL); - - need_reread = FALSE; - } -} - -const char * -xdg_mime_get_mime_type_for_data (const void *data, - size_t len) -{ - const char *mime_type; - - xdg_mime_init (); - - if (caches) - return _xdg_mime_cache_get_mime_type_for_data (data, len); - - mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len); - - if (mime_type) - return mime_type; - - return XDG_MIME_TYPE_UNKNOWN; -} - -const char * -xdg_mime_get_mime_type_for_file (const char *file_name) -{ - const char *mime_type; - int fd; - unsigned char *data; - int max_extent; - int bytes_read; - const char *base_name; - - if (file_name == NULL) - return NULL; - if (! _xdg_utf8_validate (file_name)) - return NULL; - - xdg_mime_init (); - - if (caches) - return _xdg_mime_cache_get_mime_type_for_file (file_name); - - base_name = _xdg_get_base_name (file_name); - mime_type = xdg_mime_get_mime_type_from_file_name (base_name); - - if (mime_type != XDG_MIME_TYPE_UNKNOWN) - return mime_type; - - /* FIXME: Need to make sure that max_extent isn't totally broken. This could - * be large and need getting from a stream instead of just reading it all - * in. */ - max_extent = _xdg_mime_magic_get_buffer_extents (global_magic); - data = malloc (max_extent); - if (data == NULL) - return XDG_MIME_TYPE_UNKNOWN; - - fd = open (file_name, O_RDONLY, 0); - if (fd < 0) - { - free (data); - return XDG_MIME_TYPE_UNKNOWN; - } - - bytes_read = read (fd, data, max_extent); - - close (fd); - - if (bytes_read <= 0) - { - free (data); - return XDG_MIME_TYPE_UNKNOWN; - } - - mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read); - - free (data); - - if (mime_type) - return mime_type; - - return XDG_MIME_TYPE_UNKNOWN; -} - -const char * -xdg_mime_get_mime_type_from_file_name (const char *file_name) -{ - const char *mime_type; - - xdg_mime_init (); - - if (caches) - return _xdg_mime_cache_get_mime_type_from_file_name (file_name); - - mime_type = _xdg_glob_hash_lookup_file_name (global_hash, file_name); - if (mime_type) - return mime_type; - else - return XDG_MIME_TYPE_UNKNOWN; -} - -int -xdg_mime_is_valid_mime_type (const char *mime_type) -{ - /* FIXME: We should make this a better test - */ - return _xdg_utf8_validate (mime_type); -} - -void -xdg_mime_shutdown (void) -{ - XdgCallbackList *list; - - /* FIXME: Need to make this (and the whole library) thread safe */ - if (dir_time_list) - { - xdg_dir_time_list_free (dir_time_list); - dir_time_list = NULL; - } - - if (global_hash) - { - _xdg_glob_hash_free (global_hash); - global_hash = NULL; - } - if (global_magic) - { - _xdg_mime_magic_free (global_magic); - global_magic = NULL; - } - - if (alias_list) - { - _xdg_mime_alias_list_free (alias_list); - alias_list = NULL; - } - - if (parent_list) - { - _xdg_mime_parent_list_free (parent_list); - parent_list = NULL; - } - - for (list = callback_list; list; list = list->next) - (list->callback) (list->data); - - need_reread = TRUE; -} - -int -xdg_mime_get_max_buffer_extents (void) -{ - xdg_mime_init (); - - if (caches) - return _xdg_mime_cache_get_max_buffer_extents (); - - return _xdg_mime_magic_get_buffer_extents (global_magic); -} - -const char * -xdg_mime_unalias_mime_type (const char *mime_type) -{ - const char *lookup; - - xdg_mime_init (); - - if (caches) - return _xdg_mime_cache_unalias_mime_type (mime_type); - - if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL) - return lookup; - - return mime_type; -} - -int -xdg_mime_mime_type_equal (const char *mime_a, - const char *mime_b) -{ - const char *unalias_a, *unalias_b; - - xdg_mime_init (); - - unalias_a = xdg_mime_unalias_mime_type (mime_a); - unalias_b = xdg_mime_unalias_mime_type (mime_b); - - if (strcmp (unalias_a, unalias_b) == 0) - return 1; - - return 0; -} - -int -xdg_mime_media_type_equal (const char *mime_a, - const char *mime_b) -{ - char *sep; - - xdg_mime_init (); - - sep = strchr (mime_a, '/'); - - if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0) - return 1; - - return 0; -} - -#if 0 -static int -xdg_mime_is_super_type (const char *mime) -{ - int length; - const char *type; - - length = strlen (mime); - type = &(mime[length - 2]); - - if (strcmp (type, "/*") == 0) - return 1; - - return 0; -} -#endif - -int -xdg_mime_mime_type_subclass (const char *mime, - const char *base) -{ - const char *umime, *ubase; - const char **parents; - - xdg_mime_init (); - - if (caches) - return _xdg_mime_cache_mime_type_subclass (mime, base); - - umime = xdg_mime_unalias_mime_type (mime); - ubase = xdg_mime_unalias_mime_type (base); - - if (strcmp (umime, ubase) == 0) - return 1; - -#if 0 - /* Handle supertypes */ - if (xdg_mime_is_super_type (ubase) && - xdg_mime_media_type_equal (umime, ubase)) - return 1; -#endif - - /* Handle special cases text/plain and application/octet-stream */ - if (strcmp (ubase, "text/plain") == 0 && - strncmp (umime, "text/", 5) == 0) - return 1; - - if (strcmp (ubase, "application/octet-stream") == 0) - return 1; - - parents = _xdg_mime_parent_list_lookup (parent_list, umime); - for (; parents && *parents; parents++) - { - if (xdg_mime_mime_type_subclass (*parents, ubase)) - return 1; - } - - return 0; -} - -char ** -xdg_mime_list_mime_parents (const char *mime) -{ - const char **parents; - char **result; - int i, n; - - if (caches) - return _xdg_mime_cache_list_mime_parents (mime); - - parents = xdg_mime_get_mime_parents (mime); - for (i = 0; parents[i]; i++) ; - - n = (i + 1) * sizeof (char *); - result = (char **) malloc (n); - memcpy (result, parents, n); - - return result; -} - -const char ** -xdg_mime_get_mime_parents (const char *mime) -{ - const char *umime; - - xdg_mime_init (); - - umime = xdg_mime_unalias_mime_type (mime); - - return _xdg_mime_parent_list_lookup (parent_list, umime); -} - -void -xdg_mime_dump (void) -{ - printf ("*** ALIASES ***\n\n"); - _xdg_mime_alias_list_dump (alias_list); - printf ("\n*** PARENTS ***\n\n"); - _xdg_mime_parent_list_dump (parent_list); -} - - -/* Registers a function to be called every time the mime database reloads its files - */ -int -xdg_mime_register_reload_callback (XdgMimeCallback callback, - void *data, - XdgMimeDestroy destroy) -{ - XdgCallbackList *list_el; - static int callback_id = 1; - - /* Make a new list element */ - list_el = calloc (1, sizeof (XdgCallbackList)); - list_el->callback_id = callback_id; - list_el->callback = callback; - list_el->data = data; - list_el->destroy = destroy; - list_el->next = callback_list; - if (list_el->next) - list_el->next->prev = list_el; - - callback_list = list_el; - callback_id ++; - - return callback_id - 1; -} - -void -xdg_mime_remove_callback (int callback_id) -{ - XdgCallbackList *list; - - for (list = callback_list; list; list = list->next) - { - if (list->callback_id == callback_id) - { - if (list->next) - list->next = list->prev; - - if (list->prev) - list->prev->next = list->next; - else - callback_list = list->next; - - /* invoke the destroy handler */ - if (list->destroy != NULL) - (list->destroy) (list->data); - free (list); - return; - } - } -} diff --git a/thunar-vfs/xdgmime/xdgmime.h b/thunar-vfs/xdgmime/xdgmime.h deleted file mode 100644 index f4041ea57dd7ec18da16b8290619a45267945916..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmime.h +++ /dev/null @@ -1,101 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmime.h: XDG Mime Spec mime resolver. Based on version 0.11 of the spec. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2003 Red Hat, Inc. - * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - - -#ifndef __XDG_MIME_H__ -#define __XDG_MIME_H__ - -#include <stdlib.h> - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#ifdef XDG_PREFIX -#define XDG_ENTRY(func) _XDG_ENTRY2(XDG_PREFIX,func) -#define _XDG_ENTRY2(prefix,func) _XDG_ENTRY3(prefix,func) -#define _XDG_ENTRY3(prefix,func) prefix##_##func -#endif - -typedef void (*XdgMimeCallback) (void *user_data); -typedef void (*XdgMimeDestroy) (void *user_data); - - -#ifdef XDG_PREFIX -#define xdg_mime_get_mime_type_for_data XDG_ENTRY(get_mime_type_for_data) -#define xdg_mime_get_mime_type_for_file XDG_ENTRY(get_mime_type_for_file) -#define xdg_mime_get_mime_type_from_file_name XDG_ENTRY(get_mime_type_from_file_name) -#define xdg_mime_is_valid_mime_type XDG_ENTRY(is_valid_mime_type) -#define xdg_mime_mime_type_equal XDG_ENTRY(mime_type_equal) -#define xdg_mime_media_type_equal XDG_ENTRY(media_type_equal) -#define xdg_mime_mime_type_subclass XDG_ENTRY(mime_type_subclass) -#define xdg_mime_get_mime_parents XDG_ENTRY(get_mime_parents) -#define xdg_mime_list_mime_parents XDG_ENTRY(list_mime_parents) -#define xdg_mime_unalias_mime_type XDG_ENTRY(unalias_mime_type) -#define xdg_mime_get_max_buffer_extents XDG_ENTRY(get_max_buffer_extents) -#define xdg_mime_shutdown XDG_ENTRY(shutdown) -#define xdg_mime_register_reload_callback XDG_ENTRY(register_reload_callback) -#define xdg_mime_remove_callback XDG_ENTRY(remove_callback) -#define xdg_mime_type_unknown XDG_ENTRY(type_unknown) -#endif - -extern const char *xdg_mime_type_unknown; -#define XDG_MIME_TYPE_UNKNOWN xdg_mime_type_unknown - -void xdg_mime_init (void); -const char *xdg_mime_get_mime_type_for_data (const void *data, - size_t len); -const char *xdg_mime_get_mime_type_for_file (const char *file_name); -const char *xdg_mime_get_mime_type_from_file_name (const char *file_name); -int xdg_mime_is_valid_mime_type (const char *mime_type); -int xdg_mime_mime_type_equal (const char *mime_a, - const char *mime_b); -int xdg_mime_media_type_equal (const char *mime_a, - const char *mime_b); -int xdg_mime_mime_type_subclass (const char *mime_a, - const char *mime_b); - /* xdg_mime_get_mime_parents() is deprecated since it does - * not work correctly with caches. Use xdg_mime_list_parents() - * instead, but notice that that function expects you to free - * the array it returns. - */ -const char **xdg_mime_get_mime_parents (const char *mime); -char ** xdg_mime_list_mime_parents (const char *mime); -const char *xdg_mime_unalias_mime_type (const char *mime); -int xdg_mime_get_max_buffer_extents (void); -void xdg_mime_shutdown (void); -void xdg_mime_dump (void); -int xdg_mime_register_reload_callback (XdgMimeCallback callback, - void *data, - XdgMimeDestroy destroy); -void xdg_mime_remove_callback (int callback_id); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* __XDG_MIME_H__ */ diff --git a/thunar-vfs/xdgmime/xdgmimealias.c b/thunar-vfs/xdgmime/xdgmimealias.c deleted file mode 100644 index 2dd70f1d6987f6c3ce1fb8822e2005c67e80b23c..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimealias.c +++ /dev/null @@ -1,184 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimealias.c: Private file. Datastructure for storing the aliases. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2004 Red Hat, Inc. - * Copyright (C) 2004 Matthias Clasen <mclasen@redhat.com> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include "xdgmimealias.h" -#include "xdgmimeint.h" -#include <stdlib.h> -#include <stdio.h> -#include <assert.h> -#include <string.h> -#include <fnmatch.h> - -#ifndef FALSE -#define FALSE (0) -#endif - -#ifndef TRUE -#define TRUE (!FALSE) -#endif - -typedef struct XdgAlias XdgAlias; - -struct XdgAlias -{ - char *alias; - char *mime_type; -}; - -struct XdgAliasList -{ - struct XdgAlias *aliases; - int n_aliases; -}; - -XdgAliasList * -_xdg_mime_alias_list_new (void) -{ - XdgAliasList *list; - - list = malloc (sizeof (XdgAliasList)); - - list->aliases = NULL; - list->n_aliases = 0; - - return list; -} - -void -_xdg_mime_alias_list_free (XdgAliasList *list) -{ - int i; - - if (list->aliases) - { - for (i = 0; i < list->n_aliases; i++) - { - free (list->aliases[i].alias); - free (list->aliases[i].mime_type); - } - free (list->aliases); - } - free (list); -} - -static int -alias_entry_cmp (const void *v1, const void *v2) -{ - return strcmp (((XdgAlias *)v1)->alias, ((XdgAlias *)v2)->alias); -} - -const char * -_xdg_mime_alias_list_lookup (XdgAliasList *list, - const char *alias) -{ - XdgAlias *entry; - XdgAlias key; - - if (list->n_aliases > 0) - { - key.alias = (char *)alias; - key.mime_type = 0; - - entry = bsearch (&key, list->aliases, list->n_aliases, - sizeof (XdgAlias), alias_entry_cmp); - if (entry) - return entry->mime_type; - } - - return NULL; -} - -void -_xdg_mime_alias_read_from_file (XdgAliasList *list, - const char *file_name) -{ - FILE *file; - char line[255]; - int alloc; - - file = fopen (file_name, "r"); - - if (file == NULL) - return; - - /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. - * Blah */ - alloc = list->n_aliases + 16; - list->aliases = realloc (list->aliases, alloc * sizeof (XdgAlias)); - while (fgets (line, 255, file) != NULL) - { - char *sep; - if (line[0] == '#') - continue; - - sep = strchr (line, ' '); - if (sep == NULL) - continue; - *(sep++) = '\000'; - sep[strlen (sep) -1] = '\000'; - if (list->n_aliases == alloc) - { - alloc <<= 1; - list->aliases = realloc (list->aliases, - alloc * sizeof (XdgAlias)); - } - list->aliases[list->n_aliases].alias = strdup (line); - list->aliases[list->n_aliases].mime_type = strdup (sep); - list->n_aliases++; - } - list->aliases = realloc (list->aliases, - list->n_aliases * sizeof (XdgAlias)); - - fclose (file); - - if (list->n_aliases > 1) - qsort (list->aliases, list->n_aliases, - sizeof (XdgAlias), alias_entry_cmp); -} - - -void -_xdg_mime_alias_list_dump (XdgAliasList *list) -{ - int i; - - if (list->aliases) - { - for (i = 0; i < list->n_aliases; i++) - { - printf ("%s %s\n", - list->aliases[i].alias, - list->aliases[i].mime_type); - } - } -} - - diff --git a/thunar-vfs/xdgmime/xdgmimealias.h b/thunar-vfs/xdgmime/xdgmimealias.h deleted file mode 100644 index 3df18d66067726c25426b779c31fe1ed355059b0..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimealias.h +++ /dev/null @@ -1,50 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimealias.h: Private file. Datastructure for storing the aliases. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2004 Red Hat, Inc. - * Copyright (C) 200 Matthias Clasen <mclasen@redhat.com> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifndef __XDG_MIME_ALIAS_H__ -#define __XDG_MIME_ALIAS_H__ - -#include "xdgmime.h" - -typedef struct XdgAliasList XdgAliasList; - -#ifdef XDG_PREFIX -#define _xdg_mime_alias_read_from_file XDG_ENTRY(alias_read_from_file) -#define _xdg_mime_alias_list_new XDG_ENTRY(alias_list_new) -#define _xdg_mime_alias_list_free XDG_ENTRY(alias_list_free) -#define _xdg_mime_alias_list_lookup XDG_ENTRY(alias_list_lookup) -#endif - -void _xdg_mime_alias_read_from_file (XdgAliasList *list, - const char *file_name); -XdgAliasList *_xdg_mime_alias_list_new (void); -void _xdg_mime_alias_list_free (XdgAliasList *list); -const char *_xdg_mime_alias_list_lookup (XdgAliasList *list, - const char *alias); -void _xdg_mime_alias_list_dump (XdgAliasList *list); - -#endif /* __XDG_MIME_ALIAS_H__ */ diff --git a/thunar-vfs/xdgmime/xdgmimecache.c b/thunar-vfs/xdgmime/xdgmimecache.c deleted file mode 100644 index bfa02862b559cbd243bffe50b46966432d5f27ee..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimecache.c +++ /dev/null @@ -1,811 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimealias.c: Private file. mmappable caches for mime data - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2005 Matthias Clasen <mclasen@redhat.com> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <fcntl.h> -#include <unistd.h> -#include <fnmatch.h> -#include <assert.h> - -#include <netinet/in.h> /* for ntohl/ntohs */ - -#ifdef HAVE_MMAP -#include <sys/mman.h> -#endif - -#include <sys/stat.h> -#include <sys/types.h> - -#include "xdgmimecache.h" -#include "xdgmimeint.h" - -#ifndef MAX -#define MAX(a,b) ((a) > (b) ? (a) : (b)) -#endif - -#ifndef FALSE -#define FALSE (0) -#endif - -#ifndef TRUE -#define TRUE (!FALSE) -#endif - -#ifndef _O_BINARY -#define _O_BINARY 0 -#endif - -#define MAJOR_VERSION 1 -#define MINOR_VERSION 0 - -extern XdgMimeCache **caches; -extern int n_caches; - -struct _XdgMimeCache -{ - int ref_count; - - size_t size; - char *buffer; -}; - -#define GET_UINT16(cache,offset) (ntohs(*(uint16_t*)((cache) + (offset)))) -#define GET_UINT32(cache,offset) (ntohl(*(uint32_t*)((cache) + (offset)))) - -XdgMimeCache * -_xdg_mime_cache_ref (XdgMimeCache *cache) -{ - cache->ref_count++; - return cache; -} - -void -_xdg_mime_cache_unref (XdgMimeCache *cache) -{ - cache->ref_count--; - - if (cache->ref_count == 0) - { -#ifdef HAVE_MMAP - munmap (cache->buffer, cache->size); -#endif - free (cache); - } -} - -XdgMimeCache * -_xdg_mime_cache_new_from_file (const char *file_name) -{ - XdgMimeCache *cache = NULL; - -#ifdef HAVE_MMAP - int fd = -1; - struct stat st; - char *buffer = NULL; - - /* Open the file and map it into memory */ - fd = open (file_name, O_RDONLY|_O_BINARY, 0); - - if (fd < 0) - return NULL; - - if (fstat (fd, &st) < 0 || st.st_size < 4) - goto done; - - buffer = (char *) mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); - - if (buffer == MAP_FAILED) - goto done; - - /* Verify version */ - if (GET_UINT16 (buffer, 0) != MAJOR_VERSION || - GET_UINT16 (buffer, 2) != MINOR_VERSION) - { - munmap (buffer, st.st_size); - - goto done; - } - - cache = (XdgMimeCache *) malloc (sizeof (XdgMimeCache)); - cache->ref_count = 1; - cache->buffer = buffer; - cache->size = st.st_size; - - done: - if (fd != -1) - close (fd); - -#endif /* HAVE_MMAP */ - - return cache; -} - -static int -cache_magic_matchlet_compare_to_data (XdgMimeCache *cache, - xdg_uint32_t offset, - const void *data, - size_t len) -{ - xdg_uint32_t range_start = GET_UINT32 (cache->buffer, offset); - xdg_uint32_t range_length = GET_UINT32 (cache->buffer, offset + 4); - xdg_uint32_t data_length = GET_UINT32 (cache->buffer, offset + 12); - xdg_uint32_t data_offset = GET_UINT32 (cache->buffer, offset + 16); - xdg_uint32_t mask_offset = GET_UINT32 (cache->buffer, offset + 20); - - int i, j; - - for (i = range_start; i <= range_start + range_length; i++) - { - int valid_matchlet = TRUE; - - if (i + data_length > len) - return FALSE; - - if (mask_offset) - { - for (j = 0; j < data_length; j++) - { - if ((cache->buffer[data_offset + j] & cache->buffer[mask_offset + j]) != - ((((unsigned char *) data)[j + i]) & cache->buffer[mask_offset + j])) - { - valid_matchlet = FALSE; - break; - } - } - } - else - { - for (j = 0; j < data_length; j++) - { - if (cache->buffer[data_offset + j] != ((unsigned char *) data)[j + i]) - { - valid_matchlet = FALSE; - break; - } - } - } - - if (valid_matchlet) - return TRUE; - } - - return FALSE; -} - -static int -cache_magic_matchlet_compare (XdgMimeCache *cache, - xdg_uint32_t offset, - const void *data, - size_t len) -{ - xdg_uint32_t n_children = GET_UINT32 (cache->buffer, offset + 24); - xdg_uint32_t child_offset = GET_UINT32 (cache->buffer, offset + 28); - - int i; - - if (cache_magic_matchlet_compare_to_data (cache, offset, data, len)) - { - if (n_children == 0) - return TRUE; - - for (i = 0; i < n_children; i++) - { - if (cache_magic_matchlet_compare (cache, child_offset + 32 * i, - data, len)) - return TRUE; - } - } - - return FALSE; -} - -static const char * -cache_magic_compare_to_data (XdgMimeCache *cache, - xdg_uint32_t offset, - const void *data, - size_t len, - int *prio) -{ - xdg_uint32_t priority = GET_UINT32 (cache->buffer, offset); - xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, offset + 4); - xdg_uint32_t n_matchlets = GET_UINT32 (cache->buffer, offset + 8); - xdg_uint32_t matchlet_offset = GET_UINT32 (cache->buffer, offset + 12); - - int i; - - for (i = 0; i < n_matchlets; i++) - { - if (cache_magic_matchlet_compare (cache, matchlet_offset + i * 32, - data, len)) - { - *prio = priority; - - return cache->buffer + mimetype_offset; - } - } - - return NULL; -} - -static const char * -cache_magic_lookup_data (XdgMimeCache *cache, - const void *data, - size_t len, - int *prio) -{ - xdg_uint32_t list_offset; - xdg_uint32_t n_entries; - xdg_uint32_t offset; - - int j; - - *prio = 0; - - list_offset = GET_UINT32 (cache->buffer, 24); - n_entries = GET_UINT32 (cache->buffer, list_offset); - offset = GET_UINT32 (cache->buffer, list_offset + 8); - - for (j = 0; j < n_entries; j++) - { - const char *match = cache_magic_compare_to_data (cache, offset + 16 * j, - data, len, prio); - if (match) - return match; - } - - return NULL; -} - -static const char * -cache_alias_lookup (const char *alias) -{ - const char *ptr; - int i, min, max, mid, cmp; - - for (i = 0; i < n_caches; i++) - { - XdgMimeCache *cache = caches[i]; - xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 4 ); - xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); - xdg_uint32_t offset; - - min = 0; - max = n_entries - 1; - while (max >= min) - { - mid = (min + max) / 2; - - offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid); - ptr = cache->buffer + offset; - cmp = strcmp (ptr, alias); - - if (cmp < 0) - min = mid + 1; - else if (cmp > 0) - max = mid - 1; - else - { - offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4); - return cache->buffer + offset; - } - } - } - - return NULL; -} - -static const char * -cache_glob_lookup_literal (const char *file_name) -{ - const char *ptr; - int i, min, max, mid, cmp; - - for (i = 0; i < n_caches; i++) - { - XdgMimeCache *cache = caches[i]; - xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 12); - xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); - xdg_uint32_t offset; - - min = 0; - max = n_entries - 1; - while (max >= min) - { - mid = (min + max) / 2; - - offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid); - ptr = cache->buffer + offset; - cmp = strcmp (ptr, file_name); - - if (cmp < 0) - min = mid + 1; - else if (cmp > 0) - max = mid - 1; - else - { - offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4); - return cache->buffer + offset; - } - } - } - - return NULL; -} - -static const char * -cache_glob_lookup_fnmatch (const char *file_name) -{ - const char *mime_type; - const char *ptr; - - int i, j; - - for (i = 0; i < n_caches; i++) - { - XdgMimeCache *cache = caches[i]; - - xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20); - xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); - - for (j = 0; j < n_entries; j++) - { - xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j); - xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4); - ptr = cache->buffer + offset; - mime_type = cache->buffer + mimetype_offset; - - /* FIXME: Not UTF-8 safe */ - if (fnmatch (ptr, file_name, 0) == 0) - return mime_type; - } - } - - return NULL; -} - -static const char * -cache_glob_node_lookup_suffix (XdgMimeCache *cache, - xdg_uint32_t n_entries, - xdg_uint32_t offset, - const char *suffix, - int ignore_case) -{ - xdg_unichar_t character; - xdg_unichar_t match_char; - xdg_uint32_t mimetype_offset; - xdg_uint32_t n_children; - xdg_uint32_t child_offset; - - int min, max, mid; - - character = _xdg_utf8_to_ucs4 (suffix); - if (ignore_case) - character = _xdg_ucs4_to_lower (character); - - min = 0; - max = n_entries - 1; - while (max >= min) - { - mid = (min + max) / 2; - - match_char = GET_UINT32 (cache->buffer, offset + 16 * mid); - - if (match_char < character) - min = mid + 1; - else if (match_char > character) - max = mid - 1; - else - { - suffix = _xdg_utf8_next_char (suffix); - if (*suffix == '\0') - { - mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * mid + 4); - - return cache->buffer + mimetype_offset; - } - else - { - n_children = GET_UINT32 (cache->buffer, offset + 16 * mid + 8); - child_offset = GET_UINT32 (cache->buffer, offset + 16 * mid + 12); - - return cache_glob_node_lookup_suffix (cache, - n_children, child_offset, - suffix, ignore_case); - } - } - } - - return NULL; -} - -static const char * -cache_glob_lookup_suffix (const char *suffix, - int ignore_case) -{ - const char *mime_type; - - int i; - - for (i = 0; i < n_caches; i++) - { - XdgMimeCache *cache = caches[i]; - - xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16); - xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); - xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4); - - mime_type = cache_glob_node_lookup_suffix (cache, - n_entries, offset, - suffix, ignore_case); - if (mime_type) - return mime_type; - } - - return NULL; -} - -static void -find_stopchars (char *stopchars) -{ - int i, j, k, l; - - k = 0; - for (i = 0; i < n_caches; i++) - { - XdgMimeCache *cache = caches[i]; - - xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16); - xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); - xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4); - - for (j = 0; j < n_entries; j++) - { - xdg_uint32_t match_char = GET_UINT32 (cache->buffer, offset); - - if (match_char < 128) - { - for (l = 0; l < k; l++) - if (stopchars[l] == match_char) - break; - if (l == k) - { - stopchars[k] = (char) match_char; - k++; - } - } - - offset += 16; - } - } - - stopchars[k] = '\0'; -} - -static const char * -cache_glob_lookup_file_name (const char *file_name) -{ - const char *mime_type; - const char *ptr; - char stopchars[128]; - - assert (file_name != NULL); - - /* First, check the literals */ - mime_type = cache_glob_lookup_literal (file_name); - if (mime_type) - return mime_type; - - find_stopchars (stopchars); - - /* Next, check suffixes */ - ptr = strpbrk (file_name, stopchars); - while (ptr) - { - mime_type = cache_glob_lookup_suffix (ptr, FALSE); - if (mime_type != NULL) - return mime_type; - - mime_type = cache_glob_lookup_suffix (ptr, TRUE); - if (mime_type != NULL) - return mime_type; - - ptr = strpbrk (ptr + 1, stopchars); - } - - /* Last, try fnmatch */ - return cache_glob_lookup_fnmatch (file_name); -} - -int -_xdg_mime_cache_get_max_buffer_extents (void) -{ - xdg_uint32_t offset; - xdg_uint32_t max_extent; - int i; - - max_extent = 0; - for (i = 0; i < n_caches; i++) - { - XdgMimeCache *cache = caches[i]; - - offset = GET_UINT32 (cache->buffer, 24); - max_extent = MAX (max_extent, GET_UINT32 (cache->buffer, offset + 4)); - } - - return max_extent; -} - -const char * -_xdg_mime_cache_get_mime_type_for_data (const void *data, - size_t len) -{ - const char *mime_type; - int i, priority; - - priority = 0; - mime_type = NULL; - for (i = 0; i < n_caches; i++) - { - XdgMimeCache *cache = caches[i]; - - int prio; - const char *match; - - match = cache_magic_lookup_data (cache, data, len, &prio); - if (prio > priority) - { - priority = prio; - mime_type = match; - } - } - - if (priority > 0) - return mime_type; - - return XDG_MIME_TYPE_UNKNOWN; -} - -const char * -_xdg_mime_cache_get_mime_type_for_file (const char *file_name) -{ - const char *mime_type; - FILE *file; - unsigned char *data; - int max_extent; - int bytes_read; - struct stat statbuf; - const char *base_name; - - if (file_name == NULL) - return NULL; - - if (! _xdg_utf8_validate (file_name)) - return NULL; - - base_name = _xdg_get_base_name (file_name); - mime_type = _xdg_mime_cache_get_mime_type_from_file_name (base_name); - - if (mime_type != XDG_MIME_TYPE_UNKNOWN) - return mime_type; - - if (stat (file_name, &statbuf) != 0) - return XDG_MIME_TYPE_UNKNOWN; - - if (!S_ISREG (statbuf.st_mode)) - return XDG_MIME_TYPE_UNKNOWN; - - /* FIXME: Need to make sure that max_extent isn't totally broken. This could - * be large and need getting from a stream instead of just reading it all - * in. */ - max_extent = _xdg_mime_cache_get_max_buffer_extents (); - data = malloc (max_extent); - if (data == NULL) - return XDG_MIME_TYPE_UNKNOWN; - - file = fopen (file_name, "r"); - if (file == NULL) - { - free (data); - return XDG_MIME_TYPE_UNKNOWN; - } - - bytes_read = fread (data, 1, max_extent, file); - if (ferror (file)) - { - free (data); - fclose (file); - return XDG_MIME_TYPE_UNKNOWN; - } - - mime_type = _xdg_mime_cache_get_mime_type_for_data (data, bytes_read); - - free (data); - fclose (file); - - return mime_type; -} - -const char * -_xdg_mime_cache_get_mime_type_from_file_name (const char *file_name) -{ - const char *mime_type; - - mime_type = cache_glob_lookup_file_name (file_name); - - if (mime_type) - return mime_type; - else - return XDG_MIME_TYPE_UNKNOWN; -} - -#if 1 -static int -is_super_type (const char *mime) -{ - int length; - const char *type; - - length = strlen (mime); - type = &(mime[length - 2]); - - if (strcmp (type, "/*") == 0) - return 1; - - return 0; -} -#endif - -int -_xdg_mime_cache_mime_type_subclass (const char *mime, - const char *base) -{ - const char *umime, *ubase; - - int i, j, min, max, med, cmp; - - umime = _xdg_mime_cache_unalias_mime_type (mime); - ubase = _xdg_mime_cache_unalias_mime_type (base); - - if (strcmp (umime, ubase) == 0) - return 1; - - /* We really want to handle text/ * in GtkFileFilter, so we just - * turn on the supertype matching - */ -#if 1 - /* Handle supertypes */ - if (is_super_type (ubase) && - xdg_mime_media_type_equal (umime, ubase)) - return 1; -#endif - - /* Handle special cases text/plain and application/octet-stream */ - if (strcmp (ubase, "text/plain") == 0 && - strncmp (umime, "text/", 5) == 0) - return 1; - - if (strcmp (ubase, "application/octet-stream") == 0) - return 1; - - for (i = 0; i < n_caches; i++) - { - XdgMimeCache *cache = caches[i]; - - xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8); - xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); - xdg_uint32_t offset, n_parents, parent_offset; - - min = 0; - max = n_entries - 1; - while (max >= min) - { - med = (min + max)/2; - - offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med); - cmp = strcmp (cache->buffer + offset, umime); - if (cmp < 0) - min = med + 1; - else if (cmp > 0) - max = med - 1; - else - { - offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med + 4); - n_parents = GET_UINT32 (cache->buffer, offset); - - for (j = 0; j < n_parents; j++) - { - parent_offset = GET_UINT32 (cache->buffer, offset + 4 + 4 * j); - if (_xdg_mime_cache_mime_type_subclass (cache->buffer + parent_offset, ubase)) - return 1; - } - - break; - } - } - } - - return 0; -} - -const char * -_xdg_mime_cache_unalias_mime_type (const char *mime) -{ - const char *lookup; - - lookup = cache_alias_lookup (mime); - - if (lookup) - return lookup; - - return mime; -} - -char ** -_xdg_mime_cache_list_mime_parents (const char *mime) -{ - int i, j, p; - char *all_parents[128]; /* we'll stop at 128 */ - char **result; - - p = 0; - for (i = 0; i < n_caches; i++) - { - XdgMimeCache *cache = caches[i]; - - xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8); - xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); - - for (j = 0; j < n_entries; j++) - { - xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * i); - xdg_uint32_t parents_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * i + 4); - - if (strcmp (cache->buffer + mimetype_offset, mime) == 0) - { - xdg_uint32_t n_parents = GET_UINT32 (cache->buffer, parents_offset); - - for (j = 0; j < n_parents; j++) - all_parents[p++] = cache->buffer + parents_offset + 4 + 4 * j; - - break; - } - } - } - all_parents[p++] = 0; - - result = (char **) malloc (p * sizeof (char *)); - memcpy (result, all_parents, p * sizeof (char *)); - - return result; -} - diff --git a/thunar-vfs/xdgmime/xdgmimecache.h b/thunar-vfs/xdgmime/xdgmimecache.h deleted file mode 100644 index 98b289b3e3d37293bf2ea084e03204e86a4a5c04..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimecache.h +++ /dev/null @@ -1,60 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimecache.h: Private file. Datastructure for mmapped caches. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2005 Matthias Clasen <mclasen@redhat.com> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifndef __XDG_MIME_CACHE_H__ -#define __XDG_MIME_CACHE_H__ - -#include "xdgmime.h" - -typedef struct _XdgMimeCache XdgMimeCache; - -#ifdef XDG_PREFIX -#define _xdg_mime_cache_new_from_file XDG_ENTRY(cache_new_from_file) -#define _xdg_mime_cache_ref XDG_ENTRY(cache_ref) -#define _xdg_mime_cache_unref XDG_ENTRY(cache_unref) -#endif - -XdgMimeCache *_xdg_mime_cache_new_from_file (const char *file_name); -XdgMimeCache *_xdg_mime_cache_ref (XdgMimeCache *cache); -void _xdg_mime_cache_unref (XdgMimeCache *cache); - - -const char *_xdg_mime_cache_get_mime_type_for_data (const void *data, - size_t len); -const char *_xdg_mime_cache_get_mime_type_for_file (const char *file_name); -const char *_xdg_mime_cache_get_mime_type_from_file_name (const char *file_name); -int _xdg_mime_cache_is_valid_mime_type (const char *mime_type); -int _xdg_mime_cache_mime_type_equal (const char *mime_a, - const char *mime_b); -int _xdg_mime_cache_media_type_equal (const char *mime_a, - const char *mime_b); -int _xdg_mime_cache_mime_type_subclass (const char *mime_a, - const char *mime_b); -char **_xdg_mime_cache_list_mime_parents (const char *mime); -const char *_xdg_mime_cache_unalias_mime_type (const char *mime); -int _xdg_mime_cache_get_max_buffer_extents (void); - -#endif /* __XDG_MIME_CACHE_H__ */ diff --git a/thunar-vfs/xdgmime/xdgmimeglob.c b/thunar-vfs/xdgmime/xdgmimeglob.c deleted file mode 100644 index d1f3f1940c4117847c4ddf0ee0c6695a975af5f6..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimeglob.c +++ /dev/null @@ -1,485 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimeglob.c: Private file. Datastructure for storing the globs. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2003 Red Hat, Inc. - * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include "xdgmimeglob.h" -#include "xdgmimeint.h" -#include <stdlib.h> -#include <stdio.h> -#include <assert.h> -#include <string.h> -#include <fnmatch.h> - -#ifndef FALSE -#define FALSE (0) -#endif - -#ifndef TRUE -#define TRUE (!FALSE) -#endif - -typedef struct XdgGlobHashNode XdgGlobHashNode; -typedef struct XdgGlobList XdgGlobList; - -struct XdgGlobHashNode -{ - xdg_unichar_t character; - const char *mime_type; - XdgGlobHashNode *next; - XdgGlobHashNode *child; -}; -struct XdgGlobList -{ - const char *data; - const char *mime_type; - XdgGlobList *next; -}; - -struct XdgGlobHash -{ - XdgGlobList *literal_list; - XdgGlobHashNode *simple_node; - XdgGlobList *full_list; -}; - - -/* XdgGlobList - */ -static XdgGlobList * -_xdg_glob_list_new (void) -{ - XdgGlobList *new_element; - - new_element = calloc (1, sizeof (XdgGlobList)); - - return new_element; -} - -/* Frees glob_list and all of it's children */ -static void -_xdg_glob_list_free (XdgGlobList *glob_list) -{ - XdgGlobList *ptr, *next; - - ptr = glob_list; - - while (ptr != NULL) - { - next = ptr->next; - - if (ptr->data) - free ((void *) ptr->data); - if (ptr->mime_type) - free ((void *) ptr->mime_type); - free (ptr); - - ptr = next; - } -} - -static XdgGlobList * -_xdg_glob_list_append (XdgGlobList *glob_list, - void *data, - const char *mime_type) -{ - XdgGlobList *new_element; - XdgGlobList *tmp_element; - - new_element = _xdg_glob_list_new (); - new_element->data = data; - new_element->mime_type = mime_type; - if (glob_list == NULL) - return new_element; - - tmp_element = glob_list; - while (tmp_element->next != NULL) - tmp_element = tmp_element->next; - - tmp_element->next = new_element; - - return glob_list; -} - -#if 0 -static XdgGlobList * -_xdg_glob_list_prepend (XdgGlobList *glob_list, - void *data, - const char *mime_type) -{ - XdgGlobList *new_element; - - new_element = _xdg_glob_list_new (); - new_element->data = data; - new_element->next = glob_list; - new_element->mime_type = mime_type; - - return new_element; -} -#endif - -/* XdgGlobHashNode - */ - -static XdgGlobHashNode * -_xdg_glob_hash_node_new (void) -{ - XdgGlobHashNode *glob_hash_node; - - glob_hash_node = calloc (1, sizeof (XdgGlobHashNode)); - - return glob_hash_node; -} - -static void -_xdg_glob_hash_node_dump (XdgGlobHashNode *glob_hash_node, - int depth) -{ - int i; - for (i = 0; i < depth; i++) - printf (" "); - - printf ("%c", (char)glob_hash_node->character); - if (glob_hash_node->mime_type) - printf (" - %s\n", glob_hash_node->mime_type); - else - printf ("\n"); - if (glob_hash_node->child) - _xdg_glob_hash_node_dump (glob_hash_node->child, depth + 1); - if (glob_hash_node->next) - _xdg_glob_hash_node_dump (glob_hash_node->next, depth); -} - -static XdgGlobHashNode * -_xdg_glob_hash_insert_text (XdgGlobHashNode *glob_hash_node, - const char *text, - const char *mime_type) -{ - XdgGlobHashNode *node; - xdg_unichar_t character; - - character = _xdg_utf8_to_ucs4 (text); - - if ((glob_hash_node == NULL) || - (character < glob_hash_node->character)) - { - node = _xdg_glob_hash_node_new (); - node->character = character; - node->next = glob_hash_node; - glob_hash_node = node; - } - else if (character == glob_hash_node->character) - { - node = glob_hash_node; - } - else - { - XdgGlobHashNode *prev_node; - int found_node = FALSE; - - /* Look for the first character of text in glob_hash_node, and insert it if we - * have to.*/ - prev_node = glob_hash_node; - node = prev_node->next; - - while (node != NULL) - { - if (character < node->character) - { - node = _xdg_glob_hash_node_new (); - node->character = character; - node->next = prev_node->next; - prev_node->next = node; - - found_node = TRUE; - break; - } - else if (character == node->character) - { - found_node = TRUE; - break; - } - prev_node = node; - node = node->next; - } - - if (! found_node) - { - node = _xdg_glob_hash_node_new (); - node->character = character; - node->next = prev_node->next; - prev_node->next = node; - } - } - - text = _xdg_utf8_next_char (text); - if (*text == '\000') - { - free ((void *) node->mime_type); - node->mime_type = mime_type; - } - else - { - node->child = _xdg_glob_hash_insert_text (node->child, text, mime_type); - } - return glob_hash_node; -} - -static const char * -_xdg_glob_hash_node_lookup_file_name (XdgGlobHashNode *glob_hash_node, - const char *file_name, - int ignore_case) -{ - XdgGlobHashNode *node; - xdg_unichar_t character; - - if (glob_hash_node == NULL) - return NULL; - - character = _xdg_utf8_to_ucs4 (file_name); - if (ignore_case) - character = _xdg_ucs4_to_lower(character); - - for (node = glob_hash_node; node && character >= node->character; node = node->next) - { - if (character == node->character) - { - file_name = _xdg_utf8_next_char (file_name); - if (*file_name == '\000') - return node->mime_type; - else - return _xdg_glob_hash_node_lookup_file_name (node->child, - file_name, - ignore_case); - } - } - return NULL; -} - -const char * -_xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash, - const char *file_name) -{ - XdgGlobList *list; - const char *mime_type; - const char *ptr; - char stopchars[128]; - int i; - XdgGlobHashNode *node; - - /* First, check the literals */ - - assert (file_name != NULL); - - for (list = glob_hash->literal_list; list; list = list->next) - if (strcmp ((const char *)list->data, file_name) == 0) - return list->mime_type; - - i = 0; - for (node = glob_hash->simple_node; node; node = node->next) - { - if (node->character < 128) - stopchars[i++] = (char)node->character; - } - stopchars[i] = '\0'; - - ptr = strpbrk (file_name, stopchars); - while (ptr) - { - mime_type = (_xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ptr, FALSE)); - if (mime_type != NULL) - return mime_type; - - mime_type = (_xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, ptr, TRUE)); - if (mime_type != NULL) - return mime_type; - - ptr = strpbrk (ptr + 1, stopchars); - } - - /* FIXME: Not UTF-8 safe */ - for (list = glob_hash->full_list; list; list = list->next) - if (fnmatch ((const char *)list->data, file_name, 0) == 0) - return list->mime_type; - - return NULL; -} - - - -/* XdgGlobHash - */ - -XdgGlobHash * -_xdg_glob_hash_new (void) -{ - XdgGlobHash *glob_hash; - - glob_hash = calloc (1, sizeof (XdgGlobHash)); - - return glob_hash; -} - - -static void -_xdg_glob_hash_free_nodes (XdgGlobHashNode *node) -{ - if (node) - { - if (node->child) - _xdg_glob_hash_free_nodes (node->child); - if (node->next) - _xdg_glob_hash_free_nodes (node->next); - if (node->mime_type) - free ((void *) node->mime_type); - free (node); - } -} - -void -_xdg_glob_hash_free (XdgGlobHash *glob_hash) -{ - _xdg_glob_list_free (glob_hash->literal_list); - _xdg_glob_list_free (glob_hash->full_list); - _xdg_glob_hash_free_nodes (glob_hash->simple_node); - free (glob_hash); -} - -XdgGlobType -_xdg_glob_determine_type (const char *glob) -{ - const char *ptr; - int maybe_in_simple_glob = FALSE; - int first_char = TRUE; - - ptr = glob; - - while (*ptr != '\000') - { - if (*ptr == '*' && first_char) - maybe_in_simple_glob = TRUE; - else if (*ptr == '\\' || *ptr == '[' || *ptr == '?' || *ptr == '*') - return XDG_GLOB_FULL; - - first_char = FALSE; - ptr = _xdg_utf8_next_char (ptr); - } - if (maybe_in_simple_glob) - return XDG_GLOB_SIMPLE; - else - return XDG_GLOB_LITERAL; -} - -/* glob must be valid UTF-8 */ -void -_xdg_glob_hash_append_glob (XdgGlobHash *glob_hash, - const char *glob, - const char *mime_type) -{ - XdgGlobType type; - - assert (glob_hash != NULL); - assert (glob != NULL); - - type = _xdg_glob_determine_type (glob); - - switch (type) - { - case XDG_GLOB_LITERAL: - glob_hash->literal_list = _xdg_glob_list_append (glob_hash->literal_list, strdup (glob), strdup (mime_type)); - break; - case XDG_GLOB_SIMPLE: - glob_hash->simple_node = _xdg_glob_hash_insert_text (glob_hash->simple_node, glob + 1, strdup (mime_type)); - break; - case XDG_GLOB_FULL: - glob_hash->full_list = _xdg_glob_list_append (glob_hash->full_list, strdup (glob), strdup (mime_type)); - break; - } -} - -void -_xdg_glob_hash_dump (XdgGlobHash *glob_hash) -{ - XdgGlobList *list; - printf ("LITERAL STRINGS\n"); - if (glob_hash->literal_list == NULL) - { - printf (" None\n"); - } - else - { - for (list = glob_hash->literal_list; list; list = list->next) - printf (" %s - %s\n", (char *)list->data, list->mime_type); - } - printf ("\nSIMPLE GLOBS\n"); - _xdg_glob_hash_node_dump (glob_hash->simple_node, 4); - - printf ("\nFULL GLOBS\n"); - if (glob_hash->full_list == NULL) - { - printf (" None\n"); - } - else - { - for (list = glob_hash->full_list; list; list = list->next) - printf (" %s - %s\n", (char *)list->data, list->mime_type); - } -} - - -void -_xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash, - const char *file_name) -{ - FILE *glob_file; - char line[255]; - - glob_file = fopen (file_name, "r"); - - if (glob_file == NULL) - return; - - /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. - * Blah */ - while (fgets (line, 255, glob_file) != NULL) - { - char *colon; - if (line[0] == '#') - continue; - - colon = strchr (line, ':'); - if (colon == NULL) - continue; - *(colon++) = '\000'; - colon[strlen (colon) -1] = '\000'; - _xdg_glob_hash_append_glob (glob_hash, colon, line); - } - - fclose (glob_file); -} diff --git a/thunar-vfs/xdgmime/xdgmimeglob.h b/thunar-vfs/xdgmime/xdgmimeglob.h deleted file mode 100644 index 771c4527f3bc65c41ffa313d1f53fa5321bbe11b..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimeglob.h +++ /dev/null @@ -1,65 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimeglob.h: Private file. Datastructure for storing the globs. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2003 Red Hat, Inc. - * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifndef __XDG_MIME_GLOB_H__ -#define __XDG_MIME_GLOB_H__ - -#include "xdgmime.h" - -typedef struct XdgGlobHash XdgGlobHash; - -typedef enum -{ - XDG_GLOB_LITERAL, /* Makefile */ - XDG_GLOB_SIMPLE, /* *.gif */ - XDG_GLOB_FULL /* x*.[ch] */ -} XdgGlobType; - - -#ifdef XDG_PREFIX -#define _xdg_mime_glob_read_from_file XDG_ENTRY(glob_read_from_file) -#define _xdg_glob_hash_new XDG_ENTRY(hash_new) -#define _xdg_glob_hash_free XDG_ENTRY(hash_free) -#define _xdg_glob_hash_lookup_file_name XDG_ENTRY(hash_lookup_file_name) -#define _xdg_glob_hash_append_glob XDG_ENTRY(hash_append_glob) -#define _xdg_glob_determine_type XDG_ENTRY(determine_type) -#define _xdg_glob_hash_dump XDG_ENTRY(hash_dump) -#endif - -void _xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash, - const char *file_name); -XdgGlobHash *_xdg_glob_hash_new (void); -void _xdg_glob_hash_free (XdgGlobHash *glob_hash); -const char *_xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash, - const char *text); -void _xdg_glob_hash_append_glob (XdgGlobHash *glob_hash, - const char *glob, - const char *mime_type); -XdgGlobType _xdg_glob_determine_type (const char *glob); -void _xdg_glob_hash_dump (XdgGlobHash *glob_hash); - -#endif /* __XDG_MIME_GLOB_H__ */ diff --git a/thunar-vfs/xdgmime/xdgmimeint.c b/thunar-vfs/xdgmime/xdgmimeint.c deleted file mode 100644 index 4a0ac4cc397e1d8b6f1c357b9731d4b46445a719..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimeint.c +++ /dev/null @@ -1,154 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimeint.c: Internal defines and functions. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2003 Red Hat, Inc. - * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include "xdgmimeint.h" -#include <ctype.h> -#include <string.h> - -#ifndef FALSE -#define FALSE (0) -#endif - -#ifndef TRUE -#define TRUE (!FALSE) -#endif - -static const char _xdg_utf8_skip_data[256] = { - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, - 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1 -}; - -const char * const _xdg_utf8_skip = _xdg_utf8_skip_data; - - - -/* Returns the number of unprocessed characters. */ -xdg_unichar_t -_xdg_utf8_to_ucs4(const char *source) -{ - xdg_unichar_t ucs32; - if( ! ( *source & 0x80 ) ) - { - ucs32 = *source; - } - else - { - int bytelength = 0; - xdg_unichar_t result; - if ( ! (*source & 0x40) ) - { - ucs32 = *source; - } - else - { - if ( ! (*source & 0x20) ) - { - result = *source++ & 0x1F; - bytelength = 2; - } - else if ( ! (*source & 0x10) ) - { - result = *source++ & 0x0F; - bytelength = 3; - } - else if ( ! (*source & 0x08) ) - { - result = *source++ & 0x07; - bytelength = 4; - } - else if ( ! (*source & 0x04) ) - { - result = *source++ & 0x03; - bytelength = 5; - } - else if ( ! (*source & 0x02) ) - { - result = *source++ & 0x01; - bytelength = 6; - } - else - { - result = *source++; - bytelength = 1; - } - - for ( bytelength --; bytelength > 0; bytelength -- ) - { - result <<= 6; - result |= *source++ & 0x3F; - } - ucs32 = result; - } - } - return ucs32; -} - - -/* hullo. this is great code. don't rewrite it */ - -xdg_unichar_t -_xdg_ucs4_to_lower (xdg_unichar_t source) -{ - /* FIXME: Do a real to_upper sometime */ - /* CaseFolding-3.2.0.txt has a table of rules. */ - if ((source & 0xFF) == source) - return (xdg_unichar_t) tolower ((unsigned char) source); - return source; -} - -int -_xdg_utf8_validate (const char *source) -{ - /* FIXME: actually write */ - return TRUE; -} - -const char * -_xdg_get_base_name (const char *file_name) -{ - const char *base_name; - - if (file_name == NULL) - return NULL; - - base_name = strrchr (file_name, '/'); - - if (base_name == NULL) - return file_name; - else - return base_name + 1; -} diff --git a/thunar-vfs/xdgmime/xdgmimeint.h b/thunar-vfs/xdgmime/xdgmimeint.h deleted file mode 100644 index 288148719e4b0f90cee6a0ea36b8c6f785303a0b..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimeint.h +++ /dev/null @@ -1,73 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimeint.h: Internal defines and functions. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2003 Red Hat, Inc. - * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifndef __XDG_MIME_INT_H__ -#define __XDG_MIME_INT_H__ - -#include "xdgmime.h" - - -#ifndef FALSE -#define FALSE (0) -#endif - -#ifndef TRUE -#define TRUE (!FALSE) -#endif - -/* FIXME: Needs to be configure check */ -typedef unsigned int xdg_unichar_t; -typedef unsigned char xdg_uchar8_t; -typedef unsigned short xdg_uint16_t; -typedef unsigned int xdg_uint32_t; - -#ifdef XDG_PREFIX -#define _xdg_utf8_skip XDG_ENTRY(utf8_skip) -#define _xdg_utf8_to_ucs4 XDG_ENTRY(utf8_to_ucs4) -#define _xdg_ucs4_to_lower XDG_ENTRY(ucs4_to_lower) -#define _xdg_utf8_validate XDG_ENTRY(utf8_validate) -#define _xdg_get_base_name XDG_ENTRY(get_ase_name) -#endif - -#define SWAP_BE16_TO_LE16(val) (xdg_uint16_t)(((xdg_uint16_t)(val) << 8)|((xdg_uint16_t)(val) >> 8)) - -#define SWAP_BE32_TO_LE32(val) (xdg_uint32_t)((((xdg_uint32_t)(val) & 0xFF000000U) >> 24) | \ - (((xdg_uint32_t)(val) & 0x00FF0000U) >> 8) | \ - (((xdg_uint32_t)(val) & 0x0000FF00U) << 8) | \ - (((xdg_uint32_t)(val) & 0x000000FFU) << 24)) -/* UTF-8 utils - */ -extern const char *const _xdg_utf8_skip; -#define _xdg_utf8_next_char(p) (char *)((p) + _xdg_utf8_skip[*(unsigned char *)(p)]) -#define _xdg_utf8_char_size(p) (int) (_xdg_utf8_skip[*(unsigned char *)(p)]) - -xdg_unichar_t _xdg_utf8_to_ucs4 (const char *source); -xdg_unichar_t _xdg_ucs4_to_lower (xdg_unichar_t source); -int _xdg_utf8_validate (const char *source); -const char *_xdg_get_base_name (const char *file_name); - -#endif /* __XDG_MIME_INT_H__ */ diff --git a/thunar-vfs/xdgmime/xdgmimemagic.c b/thunar-vfs/xdgmime/xdgmimemagic.c deleted file mode 100644 index 352886d0f626c0faa3b0cf05be2e37849cf9be8f..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimemagic.c +++ /dev/null @@ -1,784 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimemagic.: Private file. Datastructure for storing magic files. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2003 Red Hat, Inc. - * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include <assert.h> -#include "xdgmimemagic.h" -#include "xdgmimeint.h" -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <ctype.h> -#include <errno.h> -#include <limits.h> - -#ifndef FALSE -#define FALSE (0) -#endif - -#ifndef TRUE -#define TRUE (!FALSE) -#endif - -extern int errno; - -typedef struct XdgMimeMagicMatch XdgMimeMagicMatch; -typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet; - -typedef enum -{ - XDG_MIME_MAGIC_SECTION, - XDG_MIME_MAGIC_MAGIC, - XDG_MIME_MAGIC_ERROR, - XDG_MIME_MAGIC_EOF -} XdgMimeMagicState; - -struct XdgMimeMagicMatch -{ - const char *mime_type; - int priority; - XdgMimeMagicMatchlet *matchlet; - XdgMimeMagicMatch *next; -}; - - -struct XdgMimeMagicMatchlet -{ - int indent; - int offset; - unsigned int value_length; - unsigned char *value; - unsigned char *mask; - unsigned int range_length; - unsigned int word_size; - XdgMimeMagicMatchlet *next; -}; - - -struct XdgMimeMagic -{ - XdgMimeMagicMatch *match_list; - int max_extent; -}; - -static XdgMimeMagicMatch * -_xdg_mime_magic_match_new (void) -{ - return calloc (1, sizeof (XdgMimeMagicMatch)); -} - - -static XdgMimeMagicMatchlet * -_xdg_mime_magic_matchlet_new (void) -{ - XdgMimeMagicMatchlet *matchlet; - - matchlet = malloc (sizeof (XdgMimeMagicMatchlet)); - - matchlet->indent = 0; - matchlet->offset = 0; - matchlet->value_length = 0; - matchlet->value = NULL; - matchlet->mask = NULL; - matchlet->range_length = 1; - matchlet->word_size = 1; - matchlet->next = NULL; - - return matchlet; -} - - -static void -_xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet) -{ - if (mime_magic_matchlet) - { - if (mime_magic_matchlet->next) - _xdg_mime_magic_matchlet_free (mime_magic_matchlet->next); - if (mime_magic_matchlet->value) - free (mime_magic_matchlet->value); - if (mime_magic_matchlet->mask) - free (mime_magic_matchlet->mask); - free (mime_magic_matchlet); - } -} - - -/* Frees mime_magic_match and the remainder of its list - */ -static void -_xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match) -{ - XdgMimeMagicMatch *ptr, *next; - - ptr = mime_magic_match; - while (ptr) - { - next = ptr->next; - - if (ptr->mime_type) - free ((void *) ptr->mime_type); - if (ptr->matchlet) - _xdg_mime_magic_matchlet_free (ptr->matchlet); - free (ptr); - - ptr = next; - } -} - -/* Reads in a hunk of data until a newline character or a '\000' is hit. The - * returned string is null terminated, and doesn't include the newline. - */ -static unsigned char * -_xdg_mime_magic_read_to_newline (FILE *magic_file, - int *end_of_file) -{ - unsigned char *retval; - int c; - int len, pos; - - len = 128; - pos = 0; - retval = malloc (len); - *end_of_file = FALSE; - - while (TRUE) - { - c = getc_unlocked (magic_file); - if (c == EOF) - { - *end_of_file = TRUE; - break; - } - if (c == '\n' || c == '\000') - break; - retval[pos++] = (unsigned char) c; - if (pos % 128 == 127) - { - len = len + 128; - retval = realloc (retval, len); - } - } - - retval[pos] = '\000'; - return retval; -} - -/* Returns the number read from the file, or -1 if no number could be read. - */ -static int -_xdg_mime_magic_read_a_number (FILE *magic_file, - int *end_of_file) -{ - /* LONG_MAX is about 20 characters on my system */ -#define MAX_NUMBER_SIZE 30 - char number_string[MAX_NUMBER_SIZE + 1]; - int pos = 0; - int c; - long retval = -1; - - while (TRUE) - { - c = getc_unlocked (magic_file); - - if (c == EOF) - { - *end_of_file = TRUE; - break; - } - if (! isdigit (c)) - { - ungetc (c, magic_file); - break; - } - number_string[pos] = (char) c; - pos++; - if (pos == MAX_NUMBER_SIZE) - break; - } - if (pos > 0) - { - number_string[pos] = '\000'; - errno = 0; - retval = strtol (number_string, NULL, 10); - - if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0)) - return -1; - } - - return retval; -} - -/* Headers are of the format: - * [<priority>:<mime-type>] - */ -static XdgMimeMagicState -_xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match) -{ - int c; - char *buffer; - char *end_ptr; - int end_of_file = 0; - - assert (magic_file != NULL); - assert (match != NULL); - - c = getc_unlocked (magic_file); - if (c == EOF) - return XDG_MIME_MAGIC_EOF; - if (c != '[') - return XDG_MIME_MAGIC_ERROR; - - match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file); - if (end_of_file) - return XDG_MIME_MAGIC_EOF; - if (match->priority == -1) - return XDG_MIME_MAGIC_ERROR; - - c = getc_unlocked (magic_file); - if (c == EOF) - return XDG_MIME_MAGIC_EOF; - if (c != ':') - return XDG_MIME_MAGIC_ERROR; - - buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file); - if (end_of_file) - return XDG_MIME_MAGIC_EOF; - - end_ptr = buffer; - while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n') - end_ptr++; - if (*end_ptr != ']') - { - free (buffer); - return XDG_MIME_MAGIC_ERROR; - } - *end_ptr = '\000'; - - match->mime_type = strdup (buffer); - free (buffer); - - return XDG_MIME_MAGIC_MAGIC; -} - -static XdgMimeMagicState -_xdg_mime_magic_parse_error (FILE *magic_file) -{ - int c; - - while (1) - { - c = getc_unlocked (magic_file); - if (c == EOF) - return XDG_MIME_MAGIC_EOF; - if (c == '\n') - return XDG_MIME_MAGIC_SECTION; - } -} - -/* Headers are of the format: - * [ indent ] ">" start-offset "=" value - * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n" - */ -static XdgMimeMagicState -_xdg_mime_magic_parse_magic_line (FILE *magic_file, - XdgMimeMagicMatch *match) -{ - XdgMimeMagicMatchlet *matchlet; - int c; - int end_of_file; - int indent = 0; - int bytes_read; - - assert (magic_file != NULL); - - /* Sniff the buffer to make sure it's a valid line */ - c = getc_unlocked (magic_file); - if (c == EOF) - return XDG_MIME_MAGIC_EOF; - else if (c == '[') - { - ungetc (c, magic_file); - return XDG_MIME_MAGIC_SECTION; - } - else if (c == '\n') - return XDG_MIME_MAGIC_MAGIC; - - /* At this point, it must be a digit or a '>' */ - end_of_file = FALSE; - if (isdigit (c)) - { - ungetc (c, magic_file); - indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file); - if (end_of_file) - return XDG_MIME_MAGIC_EOF; - if (indent == -1) - return XDG_MIME_MAGIC_ERROR; - c = getc_unlocked (magic_file); - if (c == EOF) - return XDG_MIME_MAGIC_EOF; - } - - if (c != '>') - return XDG_MIME_MAGIC_ERROR; - - matchlet = _xdg_mime_magic_matchlet_new (); - matchlet->indent = indent; - matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file); - if (end_of_file) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_EOF; - } - if (matchlet->offset == -1) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_ERROR; - } - c = getc_unlocked (magic_file); - if (c == EOF) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_EOF; - } - else if (c != '=') - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_ERROR; - } - - /* Next two bytes determine how long the value is */ - matchlet->value_length = 0; - c = getc_unlocked (magic_file); - if (c == EOF) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_EOF; - } - matchlet->value_length = c & 0xFF; - matchlet->value_length = matchlet->value_length << 8; - - c = getc_unlocked (magic_file); - if (c == EOF) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_EOF; - } - matchlet->value_length = matchlet->value_length + (c & 0xFF); - - matchlet->value = malloc (matchlet->value_length); - - /* OOM */ - if (matchlet->value == NULL) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_ERROR; - } - bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file); - if (bytes_read != matchlet->value_length) - { - _xdg_mime_magic_matchlet_free (matchlet); - if (feof (magic_file)) - return XDG_MIME_MAGIC_EOF; - else - return XDG_MIME_MAGIC_ERROR; - } - - c = getc_unlocked (magic_file); - if (c == '&') - { - matchlet->mask = malloc (matchlet->value_length); - /* OOM */ - if (matchlet->mask == NULL) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_ERROR; - } - bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file); - if (bytes_read != matchlet->value_length) - { - _xdg_mime_magic_matchlet_free (matchlet); - if (feof (magic_file)) - return XDG_MIME_MAGIC_EOF; - else - return XDG_MIME_MAGIC_ERROR; - } - c = getc_unlocked (magic_file); - } - - if (c == '~') - { - matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file); - if (end_of_file) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_EOF; - } - if (matchlet->word_size != 0 && - matchlet->word_size != 1 && - matchlet->word_size != 2 && - matchlet->word_size != 4) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_ERROR; - } - c = getc_unlocked (magic_file); - } - - if (c == '+') - { - matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file); - if (end_of_file) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_EOF; - } - if (matchlet->range_length == -1) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_ERROR; - } - c = getc_unlocked (magic_file); - } - - - if (c == '\n') - { - /* We clean up the matchlet, byte swapping if needed */ - if (matchlet->word_size > 1) - { - int i; - if (matchlet->value_length % matchlet->word_size != 0) - { - _xdg_mime_magic_matchlet_free (matchlet); - return XDG_MIME_MAGIC_ERROR; - } - /* FIXME: need to get this defined in a <config.h> style file */ -#if LITTLE_ENDIAN - for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size) - { - if (matchlet->word_size == 2) - *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i))); - else if (matchlet->word_size == 4) - *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i))); - if (matchlet->mask) - { - if (matchlet->word_size == 2) - *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i))); - else if (matchlet->word_size == 4) - *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i))); - - } - } -#endif - } - - matchlet->next = match->matchlet; - match->matchlet = matchlet; - - - return XDG_MIME_MAGIC_MAGIC; - } - - _xdg_mime_magic_matchlet_free (matchlet); - if (c == EOF) - return XDG_MIME_MAGIC_EOF; - - return XDG_MIME_MAGIC_ERROR; -} - -static int -_xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet, - const void *data, - size_t len) -{ - int i, j; - for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++) - { - int valid_matchlet = TRUE; - - if (i + matchlet->value_length > len) - return FALSE; - - if (matchlet->mask) - { - for (j = 0; j < matchlet->value_length; j++) - { - if ((matchlet->value[j] & matchlet->mask[j]) != - ((((unsigned char *) data)[j + i]) & matchlet->mask[j])) - { - valid_matchlet = FALSE; - break; - } - } - } - else - { - for (j = 0; j < matchlet->value_length; j++) - { - if (matchlet->value[j] != ((unsigned char *) data)[j + i]) - { - valid_matchlet = FALSE; - break; - } - } - } - if (valid_matchlet) - return TRUE; - } - return FALSE; -} - -static int -_xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet, - const void *data, - size_t len, - int indent) -{ - while ((matchlet != NULL) && (matchlet->indent == indent)) - { - if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len)) - { - if ((matchlet->next == NULL) || - (matchlet->next->indent <= indent)) - return TRUE; - - if (_xdg_mime_magic_matchlet_compare_level (matchlet->next, - data, - len, - indent + 1)) - return TRUE; - } - - do - { - matchlet = matchlet->next; - } - while (matchlet && matchlet->indent > indent); - } - - return FALSE; -} - -static int -_xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match, - const void *data, - size_t len) -{ - return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0); -} - -static void -_xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic, - XdgMimeMagicMatch *match) -{ - XdgMimeMagicMatch *list; - - if (mime_magic->match_list == NULL) - { - mime_magic->match_list = match; - return; - } - - if (match->priority > mime_magic->match_list->priority) - { - match->next = mime_magic->match_list; - mime_magic->match_list = match; - return; - } - - list = mime_magic->match_list; - while (list->next != NULL) - { - if (list->next->priority < match->priority) - { - match->next = list->next; - list->next = match; - return; - } - list = list->next; - } - list->next = match; - match->next = NULL; -} - -XdgMimeMagic * -_xdg_mime_magic_new (void) -{ - return calloc (1, sizeof (XdgMimeMagic)); -} - -void -_xdg_mime_magic_free (XdgMimeMagic *mime_magic) -{ - if (mime_magic) { - _xdg_mime_magic_match_free (mime_magic->match_list); - free (mime_magic); - } -} - -int -_xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic) -{ - return mime_magic->max_extent; -} - -const char * -_xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic, - const void *data, - size_t len) -{ - XdgMimeMagicMatch *match; - const char *mime_type; - - mime_type = NULL; - for (match = mime_magic->match_list; match; match = match->next) - { - if (_xdg_mime_magic_match_compare_to_data (match, data, len)) - { - if ((mime_type == NULL) || (xdg_mime_mime_type_subclass (match->mime_type, mime_type))) { - mime_type = match->mime_type; - } - } - } - - return mime_type; -} - -static void -_xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic) -{ - XdgMimeMagicMatch *match; - int max_extent = 0; - - for (match = mime_magic->match_list; match; match = match->next) - { - XdgMimeMagicMatchlet *matchlet; - - for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next) - { - int extent; - - extent = matchlet->value_length + matchlet->offset + matchlet->range_length; - if (max_extent < extent) - max_extent = extent; - } - } - - mime_magic->max_extent = max_extent; -} - -static XdgMimeMagicMatchlet * -_xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets) -{ - XdgMimeMagicMatchlet *new_list; - XdgMimeMagicMatchlet *tmp; - - if ((matchlets == NULL) || (matchlets->next == NULL)) - return matchlets; - - new_list = NULL; - tmp = matchlets; - while (tmp != NULL) - { - XdgMimeMagicMatchlet *matchlet; - - matchlet = tmp; - tmp = tmp->next; - matchlet->next = new_list; - new_list = matchlet; - } - - return new_list; - -} - -static void -_xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic, - FILE *magic_file) -{ - XdgMimeMagicState state; - XdgMimeMagicMatch *match = NULL; /* Quiet compiler */ - - state = XDG_MIME_MAGIC_SECTION; - - while (state != XDG_MIME_MAGIC_EOF) - { - switch (state) - { - case XDG_MIME_MAGIC_SECTION: - match = _xdg_mime_magic_match_new (); - state = _xdg_mime_magic_parse_header (magic_file, match); - if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR) - _xdg_mime_magic_match_free (match); - break; - case XDG_MIME_MAGIC_MAGIC: - state = _xdg_mime_magic_parse_magic_line (magic_file, match); - if (state == XDG_MIME_MAGIC_SECTION || - (state == XDG_MIME_MAGIC_EOF && match->mime_type)) - { - match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet); - _xdg_mime_magic_insert_match (mime_magic, match); - } - else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR) - _xdg_mime_magic_match_free (match); - break; - case XDG_MIME_MAGIC_ERROR: - state = _xdg_mime_magic_parse_error (magic_file); - break; - case XDG_MIME_MAGIC_EOF: - default: - /* Make the compiler happy */ - assert (0); - } - } - _xdg_mime_update_mime_magic_extents (mime_magic); -} - -void -_xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic, - const char *file_name) -{ - FILE *magic_file; - char header[12]; - - magic_file = fopen (file_name, "r"); - - if (magic_file == NULL) - return; - - if (fread (header, 1, 12, magic_file) == 12) - { - if (memcmp ("MIME-Magic\0\n", header, 12) == 0) - _xdg_mime_magic_read_magic_file (mime_magic, magic_file); - } - - fclose (magic_file); -} diff --git a/thunar-vfs/xdgmime/xdgmimemagic.h b/thunar-vfs/xdgmime/xdgmimemagic.h deleted file mode 100644 index dea0a3c040589695362607831a0818f9266ebea7..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimemagic.h +++ /dev/null @@ -1,54 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimemagic.h: Private file. Datastructure for storing the magic files. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2003 Red Hat, Inc. - * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifndef __XDG_MIME_MAGIC_H__ -#define __XDG_MIME_MAGIC_H__ - -#include <unistd.h> -#include "xdgmime.h" -typedef struct XdgMimeMagic XdgMimeMagic; - -#ifdef XDG_PREFIX -#define _xdg_mime_glob_read_from_file XDG_ENTRY(glob_read_from_file) -#define _xdg_mime_magic_new XDG_ENTRY(magic_new) -#define _xdg_mime_magic_read_from_file XDG_ENTRY(magic_read_from_file) -#define _xdg_mime_magic_free XDG_ENTRY(magic_free) -#define _xdg_mime_magic_get_buffer_extents XDG_ENTRY(magic_get_buffer_extents) -#define _xdg_mime_magic_lookup_data XDG_ENTRY(magic_lookup_data) -#endif - - -XdgMimeMagic *_xdg_mime_magic_new (void); -void _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic, - const char *file_name); -void _xdg_mime_magic_free (XdgMimeMagic *mime_magic); -int _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic); -const char *_xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic, - const void *data, - size_t len); - -#endif /* __XDG_MIME_MAGIC_H__ */ diff --git a/thunar-vfs/xdgmime/xdgmimeparent.c b/thunar-vfs/xdgmime/xdgmimeparent.c deleted file mode 100644 index 511bbacbc19c19b59cbcf359499aa492e3bc6cca..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimeparent.c +++ /dev/null @@ -1,219 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimealias.c: Private file. Datastructure for storing the hierarchy. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2004 Red Hat, Inc. - * Copyright (C) 2004 Matthias Clasen <mclasen@redhat.com> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include "xdgmimeparent.h" -#include "xdgmimeint.h" -#include <stdlib.h> -#include <stdio.h> -#include <assert.h> -#include <string.h> -#include <fnmatch.h> - -#ifndef FALSE -#define FALSE (0) -#endif - -#ifndef TRUE -#define TRUE (!FALSE) -#endif - -typedef struct XdgMimeParents XdgMimeParents; - -struct XdgMimeParents -{ - char *mime; - char **parents; - int n_parents; -}; - -struct XdgParentList -{ - struct XdgMimeParents *parents; - int n_mimes; -}; - -XdgParentList * -_xdg_mime_parent_list_new (void) -{ - XdgParentList *list; - - list = malloc (sizeof (XdgParentList)); - - list->parents = NULL; - list->n_mimes = 0; - - return list; -} - -void -_xdg_mime_parent_list_free (XdgParentList *list) -{ - int i; - char **p; - - if (list->parents) - { - for (i = 0; i < list->n_mimes; i++) - { - for (p = list->parents[i].parents; *p; p++) - free (*p); - - free (list->parents[i].parents); - free (list->parents[i].mime); - } - free (list->parents); - } - free (list); -} - -static int -parent_entry_cmp (const void *v1, const void *v2) -{ - return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime); -} - -const char ** -_xdg_mime_parent_list_lookup (XdgParentList *list, - const char *mime) -{ - XdgMimeParents *entry; - XdgMimeParents key; - - if (list->n_mimes > 0) - { - key.mime = (char *)mime; - key.parents = NULL; - - entry = bsearch (&key, list->parents, list->n_mimes, - sizeof (XdgMimeParents), &parent_entry_cmp); - if (entry) - return (const char **)entry->parents; - } - - return NULL; -} - -void -_xdg_mime_parent_read_from_file (XdgParentList *list, - const char *file_name) -{ - FILE *file; - char line[255]; - int i, alloc; - XdgMimeParents *entry; - - file = fopen (file_name, "r"); - - if (file == NULL) - return; - - /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. - * Blah */ - alloc = list->n_mimes + 16; - list->parents = realloc (list->parents, alloc * sizeof (XdgMimeParents)); - while (fgets (line, 255, file) != NULL) - { - char *sep; - if (line[0] == '#') - continue; - - sep = strchr (line, ' '); - if (sep == NULL) - continue; - *(sep++) = '\000'; - sep[strlen (sep) -1] = '\000'; - entry = NULL; - for (i = 0; i < list->n_mimes; i++) - { - if (strcmp (list->parents[i].mime, line) == 0) - { - entry = &(list->parents[i]); - break; - } - } - - if (!entry) - { - if (list->n_mimes == alloc) - { - alloc <<= 1; - list->parents = realloc (list->parents, - alloc * sizeof (XdgMimeParents)); - } - list->parents[list->n_mimes].mime = strdup (line); - list->parents[list->n_mimes].parents = NULL; - entry = &(list->parents[list->n_mimes]); - list->n_mimes++; - } - - if (!entry->parents) - { - entry->n_parents = 1; - entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *)); - } - else - { - entry->n_parents += 1; - entry->parents = realloc (entry->parents, - (entry->n_parents + 2) * sizeof (char *)); - } - entry->parents[entry->n_parents - 1] = strdup (sep); - entry->parents[entry->n_parents] = NULL; - } - - list->parents = realloc (list->parents, - list->n_mimes * sizeof (XdgMimeParents)); - - fclose (file); - - if (list->n_mimes > 1) - qsort (list->parents, list->n_mimes, - sizeof (XdgMimeParents), &parent_entry_cmp); -} - - -void -_xdg_mime_parent_list_dump (XdgParentList *list) -{ - int i; - char **p; - - if (list->parents) - { - for (i = 0; i < list->n_mimes; i++) - { - for (p = list->parents[i].parents; *p; p++) - printf ("%s %s\n", list->parents[i].mime, *p); - } - } -} - - diff --git a/thunar-vfs/xdgmime/xdgmimeparent.h b/thunar-vfs/xdgmime/xdgmimeparent.h deleted file mode 100644 index da29452cb8ee5998126328ea13dc47f9ccd8b3eb..0000000000000000000000000000000000000000 --- a/thunar-vfs/xdgmime/xdgmimeparent.h +++ /dev/null @@ -1,50 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* xdgmimeparent.h: Private file. Datastructure for storing the hierarchy. - * - * More info can be found at http://www.freedesktop.org/standards/ - * - * Copyright (C) 2004 Red Hat, Inc. - * Copyright (C) 200 Matthias Clasen <mclasen@redhat.com> - * - * Licensed under the Academic Free License version 2.0 - * Or under the following terms: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifndef __XDG_MIME_PARENT_H__ -#define __XDG_MIME_PARENT_H__ - -#include "xdgmime.h" - -typedef struct XdgParentList XdgParentList; - -#ifdef XDG_PREFIX -#define _xdg_mime_parent_read_from_file XDG_ENTRY(parent_read_from_file) -#define _xdg_mime_parent_list_new XDG_ENTRY(parent_list_new) -#define _xdg_mime_parent_list_free XDG_ENTRY(parent_list_free) -#define _xdg_mime_parent_list_lookup XDG_ENTRY(parent_list_lookup) -#endif - -void _xdg_mime_parent_read_from_file (XdgParentList *list, - const char *file_name); -XdgParentList *_xdg_mime_parent_list_new (void); -void _xdg_mime_parent_list_free (XdgParentList *list); -const char **_xdg_mime_parent_list_lookup (XdgParentList *list, - const char *mime); -void _xdg_mime_parent_list_dump (XdgParentList *list); - -#endif /* __XDG_MIME_PARENT_H__ */ diff --git a/thunar/main.c b/thunar/main.c index 1b0034fcc53508f0d11ed767e16d4db55f488c5a..353b562dbfcaed38ab673973f33f9b594f9c7608 100644 --- a/thunar/main.c +++ b/thunar/main.c @@ -103,5 +103,8 @@ main (int argc, char **argv) /* release the application reference */ g_object_unref (G_OBJECT (application)); + /* shutdown the VFS library */ + thunar_vfs_shutdown (); + return EXIT_SUCCESS; }