2003-07-16 21:07:38 +00:00
|
|
|
/* -*- 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/
|
|
|
|
*
|
2004-11-08 19:36:12 +00:00
|
|
|
* Copyright (C) 2003,2004 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2003,2004 Jonathan Blandford <jrb@alum.mit.edu>
|
2003-07-16 21:07:38 +00:00
|
|
|
*
|
2003-10-24 20:03:37 +00:00
|
|
|
* Licensed under the Academic Free License version 2.0
|
|
|
|
* Or under the following terms:
|
2003-07-16 21:07:38 +00:00
|
|
|
*
|
2003-10-24 20:03:37 +00:00
|
|
|
* 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.
|
2003-07-16 21:07:38 +00:00
|
|
|
*
|
2003-10-24 20:03:37 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2003-07-16 21:07:38 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2003-10-24 20:03:37 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2003-07-16 21:07:38 +00:00
|
|
|
*
|
2003-10-24 20:03:37 +00:00
|
|
|
* 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.
|
2003-07-16 21:07:38 +00:00
|
|
|
*/
|
|
|
|
|
2004-12-09 17:26:52 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2004-03-12 18:25:01 +00:00
|
|
|
#include <config.h>
|
2004-12-09 17:26:52 +00:00
|
|
|
#endif
|
|
|
|
|
2003-07-16 21:07:38 +00:00
|
|
|
#include "xdgmime.h"
|
|
|
|
#include "xdgmimeint.h"
|
|
|
|
#include "xdgmimeglob.h"
|
|
|
|
#include "xdgmimemagic.h"
|
2004-11-08 19:36:12 +00:00
|
|
|
#include "xdgmimealias.h"
|
|
|
|
#include "xdgmimeparent.h"
|
2005-04-04 21:57:39 +00:00
|
|
|
#include "xdgmimecache.h"
|
2003-07-16 21:07:38 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2004-11-08 19:36:12 +00:00
|
|
|
#include <sys/time.h>
|
2003-07-16 21:07:38 +00:00
|
|
|
#include <unistd.h>
|
2004-11-08 19:36:12 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
typedef struct XdgDirTimeList XdgDirTimeList;
|
|
|
|
typedef struct XdgCallbackList XdgCallbackList;
|
|
|
|
|
|
|
|
static int need_reread = TRUE;
|
|
|
|
static time_t last_stat_time = 0;
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-01-20 18:07:07 +00:00
|
|
|
static XdgGlobHash *global_hash = NULL;
|
|
|
|
static XdgMimeMagic *global_magic = NULL;
|
2004-11-08 19:36:12 +00:00
|
|
|
static XdgAliasList *alias_list = NULL;
|
|
|
|
static XdgParentList *parent_list = NULL;
|
|
|
|
static XdgDirTimeList *dir_time_list = NULL;
|
|
|
|
static XdgCallbackList *callback_list = NULL;
|
2005-08-07 13:32:27 +00:00
|
|
|
|
|
|
|
XdgMimeCache **_caches = NULL;
|
|
|
|
static int n_caches = 0;
|
2005-04-04 21:57:39 +00:00
|
|
|
|
2006-04-04 03:49:32 +00:00
|
|
|
const char xdg_mime_type_unknown[] = "application/octet-stream";
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-07-16 21:07:38 +00:00
|
|
|
static void
|
2004-11-08 19:36:12 +00:00
|
|
|
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)
|
2003-07-16 21:07:38 +00:00
|
|
|
{
|
|
|
|
char *file_name;
|
2004-11-08 19:36:12 +00:00
|
|
|
struct stat st;
|
|
|
|
XdgDirTimeList *list;
|
|
|
|
|
|
|
|
assert (directory != NULL);
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2005-04-04 21:57:39 +00:00
|
|
|
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;
|
|
|
|
|
2005-08-07 13:32:27 +00:00
|
|
|
_caches = realloc (_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
|
|
|
|
_caches[n_caches] = cache;
|
|
|
|
_caches[n_caches + 1] = NULL;
|
2005-04-04 21:57:39 +00:00
|
|
|
n_caches++;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2005-04-29 18:24:38 +00:00
|
|
|
free (file_name);
|
2005-04-04 21:57:39 +00:00
|
|
|
|
2003-07-16 21:07:38 +00:00
|
|
|
file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
|
2004-11-08 19:36:12 +00:00
|
|
|
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);
|
|
|
|
}
|
2003-07-16 21:07:38 +00:00
|
|
|
|
|
|
|
file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
|
2004-11-08 19:36:12 +00:00
|
|
|
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);
|
2003-07-16 21:07:38 +00:00
|
|
|
free (file_name);
|
2004-11-08 19:36:12 +00:00
|
|
|
|
|
|
|
return FALSE; /* Keep processing */
|
2003-07-16 21:07:38 +00:00
|
|
|
}
|
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
/* Runs a command on all the directories in the search path */
|
2003-07-16 21:07:38 +00:00
|
|
|
static void
|
2004-11-08 19:36:12 +00:00
|
|
|
xdg_run_command_on_dirs (XdgDirectoryFunc func,
|
|
|
|
void *user_data)
|
2003-07-16 21:07:38 +00:00
|
|
|
{
|
2004-11-08 19:36:12 +00:00
|
|
|
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
|
2003-07-16 21:07:38 +00:00
|
|
|
{
|
2004-11-08 19:36:12 +00:00
|
|
|
const char *home;
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
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 ++;
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
if (end_ptr == ptr)
|
2003-07-16 21:07:38 +00:00
|
|
|
{
|
2004-11-08 19:36:12 +00:00
|
|
|
ptr++;
|
|
|
|
continue;
|
2003-07-16 21:07:38 +00:00
|
|
|
}
|
2004-11-08 19:36:12 +00:00
|
|
|
|
|
|
|
if (*end_ptr == ':')
|
|
|
|
len = end_ptr - ptr;
|
2003-07-16 21:07:38 +00:00
|
|
|
else
|
2004-11-08 19:36:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
/* 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)
|
2003-07-16 21:07:38 +00:00
|
|
|
{
|
2004-11-08 19:36:12 +00:00
|
|
|
if (list->checked == XDG_CHECKED_UNCHECKED)
|
|
|
|
list->checked = XDG_CHECKED_VALID;
|
|
|
|
else if (list->checked == XDG_CHECKED_VALID)
|
|
|
|
list->checked = XDG_CHECKED_INVALID;
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
return (list->checked != XDG_CHECKED_VALID);
|
2003-07-16 21:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
2004-11-08 19:36:12 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
static int
|
|
|
|
xdg_check_dir (const char *directory,
|
|
|
|
int *invalid_dir_list)
|
|
|
|
{
|
|
|
|
int invalid;
|
|
|
|
char *file_name;
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
assert (directory != NULL);
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2005-04-04 21:57:39 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
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 + 5)
|
|
|
|
{
|
|
|
|
retval = xdg_check_dirs ();
|
|
|
|
last_stat_time = current_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called in every public function. It reloads the hash function if need be.
|
|
|
|
*/
|
|
|
|
static 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;
|
2003-07-16 21:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
xdg_mime_get_mime_type_for_data (const void *data,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
const char *mime_type;
|
|
|
|
|
|
|
|
xdg_mime_init ();
|
|
|
|
|
2005-08-07 13:32:27 +00:00
|
|
|
if (_caches)
|
2005-04-04 21:57:39 +00:00
|
|
|
return _xdg_mime_cache_get_mime_type_for_data (data, len);
|
|
|
|
|
2005-12-01 19:04:41 +00:00
|
|
|
mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, NULL, 0);
|
2003-07-16 21:07:38 +00:00
|
|
|
|
|
|
|
if (mime_type)
|
|
|
|
return mime_type;
|
|
|
|
|
|
|
|
return XDG_MIME_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2005-09-01 14:42:02 +00:00
|
|
|
xdg_mime_get_mime_type_for_file (const char *file_name,
|
|
|
|
struct stat *statbuf)
|
2003-07-16 21:07:38 +00:00
|
|
|
{
|
|
|
|
const char *mime_type;
|
2005-12-01 19:04:41 +00:00
|
|
|
/* currently, only a few globs occur twice, and none
|
|
|
|
* more often, so 5 seems plenty.
|
|
|
|
*/
|
|
|
|
const char *mime_types[5];
|
2003-07-16 21:07:38 +00:00
|
|
|
FILE *file;
|
|
|
|
unsigned char *data;
|
|
|
|
int max_extent;
|
|
|
|
int bytes_read;
|
2005-09-01 14:42:02 +00:00
|
|
|
struct stat buf;
|
2003-07-16 21:07:38 +00:00
|
|
|
const char *base_name;
|
2005-12-01 19:04:41 +00:00
|
|
|
int n;
|
2003-07-16 21:07:38 +00:00
|
|
|
|
|
|
|
if (file_name == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (! _xdg_utf8_validate (file_name))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
xdg_mime_init ();
|
|
|
|
|
2005-08-07 13:32:27 +00:00
|
|
|
if (_caches)
|
2005-12-01 19:04:41 +00:00
|
|
|
return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
|
2005-04-04 21:57:39 +00:00
|
|
|
|
2003-07-16 21:07:38 +00:00
|
|
|
base_name = _xdg_get_base_name (file_name);
|
2005-12-01 19:04:41 +00:00
|
|
|
n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 5);
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2005-12-01 19:04:41 +00:00
|
|
|
if (n == 1)
|
|
|
|
return mime_types[0];
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2005-09-01 14:42:02 +00:00
|
|
|
if (!statbuf)
|
|
|
|
{
|
|
|
|
if (stat (file_name, &buf) != 0)
|
|
|
|
return XDG_MIME_TYPE_UNKNOWN;
|
|
|
|
|
|
|
|
statbuf = &buf;
|
|
|
|
}
|
2003-07-16 21:07:38 +00:00
|
|
|
|
2005-09-01 14:42:02 +00:00
|
|
|
if (!S_ISREG (statbuf->st_mode))
|
2003-07-16 21:07:38 +00:00
|
|
|
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_magic_get_buffer_extents (global_magic);
|
|
|
|
data = malloc (max_extent);
|
|
|
|
if (data == NULL)
|
|
|
|
return XDG_MIME_TYPE_UNKNOWN;
|
|
|
|
|
2005-10-18 02:54:55 +00:00
|
|
|
file = fopen (file_name, "r");
|
2003-07-16 21:07:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-12-01 19:04:41 +00:00
|
|
|
mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read,
|
|
|
|
mime_types, n);
|
2003-07-16 21:07:38 +00:00
|
|
|
|
|
|
|
free (data);
|
|
|
|
fclose (file);
|
|
|
|
|
|
|
|
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 ();
|
|
|
|
|
2005-08-07 13:32:27 +00:00
|
|
|
if (_caches)
|
2005-04-04 21:57:39 +00:00
|
|
|
return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
|
|
|
|
|
2005-12-01 19:04:41 +00:00
|
|
|
if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, &mime_type, 1))
|
2003-07-16 21:07:38 +00:00
|
|
|
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);
|
|
|
|
}
|
2004-01-20 18:07:07 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
xdg_mime_shutdown (void)
|
|
|
|
{
|
2004-11-08 19:36:12 +00:00
|
|
|
XdgCallbackList *list;
|
|
|
|
|
2004-01-20 18:07:07 +00:00
|
|
|
/* FIXME: Need to make this (and the whole library) thread safe */
|
2004-11-08 19:36:12 +00:00
|
|
|
if (dir_time_list)
|
|
|
|
{
|
|
|
|
xdg_dir_time_list_free (dir_time_list);
|
|
|
|
dir_time_list = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global_hash)
|
2004-01-20 18:07:07 +00:00
|
|
|
{
|
|
|
|
_xdg_glob_hash_free (global_hash);
|
|
|
|
global_hash = NULL;
|
2004-11-08 19:36:12 +00:00
|
|
|
}
|
|
|
|
if (global_magic)
|
|
|
|
{
|
2004-01-20 18:07:07 +00:00
|
|
|
_xdg_mime_magic_free (global_magic);
|
|
|
|
global_magic = NULL;
|
2004-11-08 19:36:12 +00:00
|
|
|
}
|
2004-01-20 18:07:07 +00:00
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
if (alias_list)
|
|
|
|
{
|
|
|
|
_xdg_mime_alias_list_free (alias_list);
|
|
|
|
alias_list = NULL;
|
2004-01-20 18:07:07 +00:00
|
|
|
}
|
2005-04-29 18:24:38 +00:00
|
|
|
|
|
|
|
if (parent_list)
|
|
|
|
{
|
|
|
|
_xdg_mime_parent_list_free (parent_list);
|
|
|
|
parent_list = NULL;
|
|
|
|
}
|
2004-11-08 19:36:12 +00:00
|
|
|
|
2006-07-20 14:40:57 +00:00
|
|
|
if (_caches)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n_caches; i++)
|
|
|
|
_xdg_mime_cache_unref (_caches[i]);
|
|
|
|
free (_caches);
|
|
|
|
_caches = NULL;
|
|
|
|
n_caches = 0;
|
|
|
|
}
|
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
for (list = callback_list; list; list = list->next)
|
|
|
|
(list->callback) (list->data);
|
|
|
|
|
|
|
|
need_reread = TRUE;
|
2004-01-20 18:07:07 +00:00
|
|
|
}
|
2004-03-11 03:48:26 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
xdg_mime_get_max_buffer_extents (void)
|
|
|
|
{
|
|
|
|
xdg_mime_init ();
|
|
|
|
|
2005-08-07 13:32:27 +00:00
|
|
|
if (_caches)
|
2005-04-04 21:57:39 +00:00
|
|
|
return _xdg_mime_cache_get_max_buffer_extents ();
|
|
|
|
|
2004-03-11 03:48:26 +00:00
|
|
|
return _xdg_mime_magic_get_buffer_extents (global_magic);
|
|
|
|
}
|
2004-11-08 19:36:12 +00:00
|
|
|
|
2004-12-09 17:26:52 +00:00
|
|
|
const char *
|
2006-07-20 04:14:59 +00:00
|
|
|
_xdg_mime_unalias_mime_type (const char *mime_type)
|
2004-11-08 19:36:12 +00:00
|
|
|
{
|
|
|
|
const char *lookup;
|
|
|
|
|
2005-08-07 13:32:27 +00:00
|
|
|
if (_caches)
|
2005-04-04 21:57:39 +00:00
|
|
|
return _xdg_mime_cache_unalias_mime_type (mime_type);
|
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
|
|
|
|
return lookup;
|
|
|
|
|
|
|
|
return mime_type;
|
|
|
|
}
|
|
|
|
|
2006-07-20 04:14:59 +00:00
|
|
|
const char *
|
|
|
|
xdg_mime_unalias_mime_type (const char *mime_type)
|
|
|
|
{
|
|
|
|
xdg_mime_init ();
|
|
|
|
|
|
|
|
return _xdg_mime_unalias_mime_type (mime_type);
|
|
|
|
}
|
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
int
|
2006-07-20 04:14:59 +00:00
|
|
|
_xdg_mime_mime_type_equal (const char *mime_a,
|
|
|
|
const char *mime_b)
|
2004-11-08 19:36:12 +00:00
|
|
|
{
|
|
|
|
const char *unalias_a, *unalias_b;
|
|
|
|
|
2006-07-20 04:14:59 +00:00
|
|
|
unalias_a = _xdg_mime_unalias_mime_type (mime_a);
|
|
|
|
unalias_b = _xdg_mime_unalias_mime_type (mime_b);
|
2004-11-08 19:36:12 +00:00
|
|
|
|
|
|
|
if (strcmp (unalias_a, unalias_b) == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 04:14:59 +00:00
|
|
|
int
|
|
|
|
xdg_mime_mime_type_equal (const char *mime_a,
|
|
|
|
const char *mime_b)
|
|
|
|
{
|
|
|
|
xdg_mime_init ();
|
|
|
|
|
|
|
|
return _xdg_mime_mime_type_equal (mime_a, mime_b);
|
|
|
|
}
|
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-11-08 21:25:27 +00:00
|
|
|
#if 1
|
2004-11-08 19:36:12 +00:00
|
|
|
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
|
2006-07-20 04:14:59 +00:00
|
|
|
_xdg_mime_mime_type_subclass (const char *mime,
|
|
|
|
const char *base)
|
2004-11-08 19:36:12 +00:00
|
|
|
{
|
|
|
|
const char *umime, *ubase;
|
|
|
|
const char **parents;
|
|
|
|
|
2005-08-07 13:32:27 +00:00
|
|
|
if (_caches)
|
2005-04-04 21:57:39 +00:00
|
|
|
return _xdg_mime_cache_mime_type_subclass (mime, base);
|
|
|
|
|
2006-07-20 04:14:59 +00:00
|
|
|
umime = _xdg_mime_unalias_mime_type (mime);
|
|
|
|
ubase = _xdg_mime_unalias_mime_type (base);
|
2004-11-08 19:36:12 +00:00
|
|
|
|
|
|
|
if (strcmp (umime, ubase) == 0)
|
|
|
|
return 1;
|
|
|
|
|
2005-04-29 18:24:38 +00:00
|
|
|
#if 1
|
2004-11-08 19:36:12 +00:00
|
|
|
/* 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++)
|
|
|
|
{
|
2006-07-20 04:14:59 +00:00
|
|
|
if (_xdg_mime_mime_type_subclass (*parents, ubase))
|
2004-11-08 19:36:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 04:14:59 +00:00
|
|
|
int
|
|
|
|
xdg_mime_mime_type_subclass (const char *mime,
|
|
|
|
const char *base)
|
|
|
|
{
|
|
|
|
xdg_mime_init ();
|
|
|
|
|
|
|
|
return _xdg_mime_mime_type_subclass (mime, base);
|
|
|
|
}
|
|
|
|
|
2005-04-04 21:57:39 +00:00
|
|
|
char **
|
|
|
|
xdg_mime_list_mime_parents (const char *mime)
|
|
|
|
{
|
|
|
|
const char **parents;
|
|
|
|
char **result;
|
|
|
|
int i, n;
|
|
|
|
|
2005-08-07 13:32:27 +00:00
|
|
|
if (_caches)
|
2005-04-04 21:57:39 +00:00
|
|
|
return _xdg_mime_cache_list_mime_parents (mime);
|
|
|
|
|
|
|
|
parents = xdg_mime_get_mime_parents (mime);
|
2005-11-04 15:56:13 +00:00
|
|
|
|
|
|
|
if (!parents)
|
|
|
|
return NULL;
|
|
|
|
|
2005-04-04 21:57:39 +00:00
|
|
|
for (i = 0; parents[i]; i++) ;
|
|
|
|
|
|
|
|
n = (i + 1) * sizeof (char *);
|
|
|
|
result = (char **) malloc (n);
|
|
|
|
memcpy (result, parents, n);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-12-09 17:26:52 +00:00
|
|
|
const char **
|
|
|
|
xdg_mime_get_mime_parents (const char *mime)
|
|
|
|
{
|
|
|
|
const char *umime;
|
|
|
|
|
|
|
|
xdg_mime_init ();
|
|
|
|
|
2006-07-20 04:14:59 +00:00
|
|
|
umime = _xdg_mime_unalias_mime_type (mime);
|
2004-12-09 17:26:52 +00:00
|
|
|
|
|
|
|
return _xdg_mime_parent_list_lookup (parent_list, umime);
|
|
|
|
}
|
|
|
|
|
2004-11-08 19:36:12 +00:00
|
|
|
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 */
|
|
|
|
(list->destroy) (list->data);
|
|
|
|
free (list);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|