Update gio/xdgmime with commit 722325f of xdgmime project

This commit is contained in:
Emmanuel Fleury
2021-04-05 15:46:22 +02:00
parent 1176835ce3
commit 3d2ac608de
16 changed files with 269 additions and 475 deletions

View File

@@ -12,7 +12,7 @@
* 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.1 of the License, or (at your option) any later version.
* 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
@@ -20,10 +20,14 @@
* 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, see <http://www.gnu.org/licenses/>.
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "xdgmime.h"
#include "xdgmimeint.h"
@@ -56,8 +60,6 @@ static XdgCallbackList *callback_list = NULL;
static XdgIconList *icon_list = NULL;
static XdgIconList *generic_icon_list = NULL;
static char **xdg_dirs = NULL; /* NULL terminated */
XdgMimeCache **_caches = NULL;
static int n_caches = 0;
@@ -141,8 +143,8 @@ xdg_mime_init_from_directory (const char *directory)
assert (directory != NULL);
file_name = malloc (strlen (directory) + strlen ("/mime.cache") + 1);
strcpy (file_name, directory); strcat (file_name, "/mime.cache");
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);
@@ -161,8 +163,8 @@ xdg_mime_init_from_directory (const char *directory)
}
free (file_name);
file_name = malloc (strlen (directory) + strlen ("/globs2") + 1);
strcpy (file_name, directory); strcat (file_name, "/globs2");
file_name = malloc (strlen (directory) + strlen ("/mime/globs2") + 1);
strcpy (file_name, directory); strcat (file_name, "/mime/globs2");
if (stat (file_name, &st) == 0)
{
_xdg_mime_glob_read_from_file (global_hash, file_name, TRUE);
@@ -171,8 +173,8 @@ xdg_mime_init_from_directory (const char *directory)
else
{
free (file_name);
file_name = malloc (strlen (directory) + strlen ("/globs") + 1);
strcpy (file_name, directory); strcat (file_name, "/globs");
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, FALSE);
@@ -184,8 +186,8 @@ xdg_mime_init_from_directory (const char *directory)
}
}
file_name = malloc (strlen (directory) + strlen ("/magic") + 1);
strcpy (file_name, directory); strcat (file_name, "/magic");
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);
@@ -196,81 +198,69 @@ xdg_mime_init_from_directory (const char *directory)
free (file_name);
}
file_name = malloc (strlen (directory) + strlen ("/aliases") + 1);
strcpy (file_name, directory); strcat (file_name, "/aliases");
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 ("/subclasses") + 1);
strcpy (file_name, directory); strcat (file_name, "/subclasses");
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);
file_name = malloc (strlen (directory) + strlen ("/icons") + 1);
strcpy (file_name, directory); strcat (file_name, "/icons");
file_name = malloc (strlen (directory) + strlen ("/mime/icons") + 1);
strcpy (file_name, directory); strcat (file_name, "/mime/icons");
_xdg_mime_icon_read_from_file (icon_list, file_name);
free (file_name);
file_name = malloc (strlen (directory) + strlen ("/generic-icons") + 1);
strcpy (file_name, directory); strcat (file_name, "/generic-icons");
file_name = malloc (strlen (directory) + strlen ("/mime/generic-icons") + 1);
strcpy (file_name, directory); strcat (file_name, "/mime/generic-icons");
_xdg_mime_icon_read_from_file (generic_icon_list, file_name);
free (file_name);
return FALSE; /* Keep processing */
}
/* Set @xdg_dirs from the environment. It must not have been set already. */
/* Runs a command on all the directories in the search path */
static void
xdg_init_dirs (void)
xdg_run_command_on_dirs (XdgDirectoryFunc func,
void *user_data)
{
const char *xdg_data_home, *home, *xdg_data_dirs;
const char *xdg_data_home;
const char *xdg_data_dirs;
const char *ptr;
size_t n_dirs = 0;
size_t i, current_dir;
assert (xdg_dirs == NULL);
xdg_data_home = getenv ("XDG_DATA_HOME");
home = getenv ("HOME");
xdg_data_dirs = getenv ("XDG_DATA_DIRS");
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/";
/* Work out how many dirs were dealing with. */
if (xdg_data_home != NULL || home != NULL)
n_dirs++;
n_dirs++; /* initial entry in @xdg_data_dirs */
for (i = 0; xdg_data_dirs[i] != '\0'; i++)
if (xdg_data_dirs[i] == ':')
n_dirs++;
xdg_dirs = calloc (n_dirs + 1 /* NULL terminator */, sizeof (char *));
current_dir = 0;
/* $XDG_DATA_HOME */
if (xdg_data_home != NULL)
{
char *mime_subdir;
mime_subdir = malloc (strlen (xdg_data_home) + strlen ("/mime/") + 1);
strcpy (mime_subdir, xdg_data_home);
strcat (mime_subdir, "/mime/");
xdg_dirs[current_dir++] = mime_subdir;
}
else if (home != NULL)
{
char *guessed_xdg_home;
guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/mime/") + 1);
strcpy (guessed_xdg_home, home);
strcat (guessed_xdg_home, "/.local/share/mime/");
xdg_dirs[current_dir++] = guessed_xdg_home;
}
/* $XDG_DATA_DIRS */
ptr = xdg_data_dirs;
while (*ptr != '\000')
@@ -278,83 +268,33 @@ xdg_init_dirs (void)
const char *end_ptr;
char *dir;
int len;
int stop_processing;
end_ptr = ptr;
while (*end_ptr != ':' && *end_ptr != '\000')
end_ptr ++;
end_ptr ++;
if (end_ptr == ptr)
{
ptr++;
continue;
}
{
ptr++;
continue;
}
if (*end_ptr == ':')
len = end_ptr - ptr;
len = end_ptr - ptr;
else
len = end_ptr - ptr + 1;
dir = malloc (len + strlen ("/mime/") + 1);
len = end_ptr - ptr + 1;
dir = malloc (len + 1);
strncpy (dir, ptr, len);
dir[len] = '\0';
strcat (dir, "/mime/");
stop_processing = (func) (dir, user_data);
free (dir);
xdg_dirs[current_dir++] = dir;
if (stop_processing)
return;
ptr = end_ptr;
}
/* NULL terminator */
xdg_dirs[current_dir] = NULL;
need_reread = TRUE;
}
/* Runs a command on all the directories in the search path (@xdg_dirs). */
static void
xdg_run_command_on_dirs (XdgDirectoryFunc func,
void *user_data)
{
size_t i;
if (xdg_dirs == NULL)
xdg_init_dirs ();
for (i = 0; xdg_dirs[i] != NULL; i++)
{
if ((func) (xdg_dirs[i], user_data))
return;
}
}
/* Allows the calling code to override the directories used by xdgmime, without
* having to change environment variables in a running process (which is not
* thread safe). This is intended to be used by tests. The changes will be
* picked up by xdg_mime_init() next time public API is called.
*
* This will set @xdg_dirs. Directories in @dirs must be complete, including
* the conventional `/mime` subdirectory. This is to allow tests to override
* them without the need to create a subdirectory. */
void
xdg_mime_set_dirs (const char * const *dirs)
{
size_t i;
for (i = 0; xdg_dirs != NULL && xdg_dirs[i] != NULL; i++)
free (xdg_dirs[i]);
if (xdg_dirs != NULL)
free (xdg_dirs[i]);
xdg_dirs = NULL;
if (dirs != NULL)
{
for (i = 0; dirs[i] != NULL; i++);
xdg_dirs = calloc (i + 1 /* NULL terminator */, sizeof (char*));
for (i = 0; dirs[i] != NULL; i++)
xdg_dirs[i] = strdup (dirs[i]);
xdg_dirs[i] = NULL;
}
need_reread = TRUE;
}
/* Checks file_path to make sure it has the same mtime as last time it was
@@ -408,8 +348,8 @@ xdg_check_dir (const char *directory,
assert (directory != NULL);
/* Check the mime.cache file */
file_name = malloc (strlen (directory) + strlen ("/mime.cache") + 1);
strcpy (file_name, directory); strcat (file_name, "/mime.cache");
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, &exists);
free (file_name);
if (invalid)
@@ -423,8 +363,8 @@ xdg_check_dir (const char *directory,
}
/* Check the globs file */
file_name = malloc (strlen (directory) + strlen ("/globs") + 1);
strcpy (file_name, directory); strcat (file_name, "/globs");
file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
strcpy (file_name, directory); strcat (file_name, "/mime/globs");
invalid = xdg_check_file (file_name, NULL);
free (file_name);
if (invalid)
@@ -434,8 +374,8 @@ xdg_check_dir (const char *directory,
}
/* Check the magic file */
file_name = malloc (strlen (directory) + strlen ("/magic") + 1);
strcpy (file_name, directory); strcat (file_name, "/magic");
file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
strcpy (file_name, directory); strcat (file_name, "/mime/magic");
invalid = xdg_check_file (file_name, NULL);
free (file_name);
if (invalid)
@@ -549,8 +489,6 @@ xdg_mime_get_mime_type_for_data (const void *data,
return _xdg_binary_or_text_fallback(data, len);
}
#ifdef NOT_USED_IN_GIO
const char *
xdg_mime_get_mime_type_for_file (const char *file_name,
struct stat *statbuf)
@@ -621,13 +559,13 @@ xdg_mime_get_mime_type_for_file (const char *file_name,
mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read, NULL,
mime_types, n);
if (!mime_type)
mime_type = _xdg_binary_or_text_fallback (data, bytes_read);
free (data);
fclose (file);
if (mime_type)
return mime_type;
return _xdg_binary_or_text_fallback(data, bytes_read);
return mime_type;
}
const char *
@@ -646,8 +584,6 @@ xdg_mime_get_mime_type_from_file_name (const char *file_name)
return XDG_MIME_TYPE_UNKNOWN;
}
#endif
int
xdg_mime_get_mime_types_from_file_name (const char *file_name,
const char *mime_types[],
@@ -661,8 +597,6 @@ xdg_mime_get_mime_types_from_file_name (const char *file_name,
return _xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, n_mime_types);
}
#ifdef NOT_USED_IN_GIO
int
xdg_mime_is_valid_mime_type (const char *mime_type)
{
@@ -671,8 +605,6 @@ xdg_mime_is_valid_mime_type (const char *mime_type)
return _xdg_utf8_validate (mime_type);
}
#endif
void
xdg_mime_shutdown (void)
{
@@ -888,19 +820,14 @@ xdg_mime_mime_type_subclass (const char *mime,
char **
xdg_mime_list_mime_parents (const char *mime)
{
const char *umime;
const char **parents;
char **result;
int i, n;
xdg_mime_init ();
if (_caches)
return _xdg_mime_cache_list_mime_parents (mime);
umime = _xdg_mime_unalias_mime_type (mime);
parents = _xdg_mime_parent_list_lookup (parent_list, umime);
parents = xdg_mime_get_mime_parents (mime);
if (!parents)
return NULL;
@@ -914,8 +841,6 @@ xdg_mime_list_mime_parents (const char *mime)
return result;
}
#ifdef NOT_USED_IN_GIO
const char **
xdg_mime_get_mime_parents (const char *mime)
{
@@ -945,7 +870,6 @@ xdg_mime_dump (void)
_xdg_mime_cache_glob_dump ();
}
#endif
/* Registers a function to be called every time the mime database reloads its files
*/
@@ -973,8 +897,6 @@ xdg_mime_register_reload_callback (XdgMimeCallback callback,
return callback_id - 1;
}
#ifdef NOT_USED_IN_GIO
void
xdg_mime_remove_callback (int callback_id)
{
@@ -1000,8 +922,6 @@ xdg_mime_remove_callback (int callback_id)
}
}
#endif
const char *
xdg_mime_get_icon (const char *mime)
{