gio: Show icons based on file's mime type on OS X

The patch basically just grabs the implementation of g_content_type_get_icon_internal()
from gcontenttype.c - the only difference is that it first converts UTI to MIME using
g_content_type_get_mime_type() and at the end frees this temporary MIME type.

https://bugzilla.gnome.org/show_bug.cgi?id=788936
This commit is contained in:
Jiří Techet 2017-10-27 23:45:14 +02:00 committed by Philip Withnall
parent fc031daa33
commit 6acaca8831
2 changed files with 63 additions and 14 deletions

View File

@ -192,25 +192,59 @@ g_content_type_get_description (const gchar *type)
}
static GIcon *
g_content_type_get_icon_internal (const gchar *type,
g_content_type_get_icon_internal (const gchar *uti,
gboolean symbolic)
{
GIcon *icon = NULL;
gchar *name;
char *mimetype_icon;
char *type;
char *generic_mimetype_icon = NULL;
char *q;
char *icon_names[6];
int n = 0;
GIcon *themed_icon;
const char *xdg_icon;
int i;
g_return_val_if_fail (type != NULL, NULL);
g_return_val_if_fail (uti != NULL, NULL);
/* TODO: Show mimetype icons. */
if (g_content_type_can_be_executable (type))
name = "gtk-execute";
else if (g_content_type_is_a (type, "public.directory"))
name = symbolic ? "inode-directory-symbolic" : "inode-directory";
else
name = "gtk-file";
type = g_content_type_get_mime_type (uti);
icon = g_themed_icon_new_with_default_fallbacks (name);
G_LOCK (gio_xdgmime);
xdg_icon = xdg_mime_get_icon (type);
G_UNLOCK (gio_xdgmime);
return icon;
if (xdg_icon)
icon_names[n++] = g_strdup (xdg_icon);
mimetype_icon = g_strdup (type);
while ((q = strchr (mimetype_icon, '/')) != NULL)
*q = '-';
icon_names[n++] = mimetype_icon;
generic_mimetype_icon = g_content_type_get_generic_icon_name (type);
if (generic_mimetype_icon)
icon_names[n++] = generic_mimetype_icon;
if (symbolic)
{
for (i = 0; i < n; i++)
{
icon_names[n + i] = icon_names[i];
icon_names[i] = g_strconcat (icon_names[i], "-symbolic", NULL);
}
n += n;
}
themed_icon = g_themed_icon_new_from_names (icon_names, n);
for (i = 0; i < n; i++)
g_free (icon_names[i]);
g_free(type);
return themed_icon;
}
GIcon *

View File

@ -233,8 +233,12 @@ test_icon (void)
const gchar *const *names;
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
#ifdef __APPLE__
g_assert (g_strv_contains (names, "text-*"));
#else
g_assert (g_strv_contains (names, "text-plain"));
g_assert (g_strv_contains (names, "text-x-generic"));
#endif
}
g_object_unref (icon);
g_free (type);
@ -248,7 +252,9 @@ test_icon (void)
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
g_assert (g_strv_contains (names, "application-rtf"));
#ifndef __APPLE__
g_assert (g_strv_contains (names, "x-office-document"));
#endif
}
g_object_unref (icon);
g_free (type);
@ -269,10 +275,15 @@ test_symbolic_icon (void)
const gchar *const *names;
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
#ifdef __APPLE__
g_assert (g_strv_contains (names, "text-*-symbolic"));
g_assert (g_strv_contains (names, "text-*"));
#else
g_assert (g_strv_contains (names, "text-plain-symbolic"));
g_assert (g_strv_contains (names, "text-x-generic-symbolic"));
g_assert (g_strv_contains (names, "text-plain"));
g_assert (g_strv_contains (names, "text-x-generic"));
#endif
}
g_object_unref (icon);
g_free (type);
@ -286,9 +297,11 @@ test_symbolic_icon (void)
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
g_assert (g_strv_contains (names, "application-rtf-symbolic"));
g_assert (g_strv_contains (names, "x-office-document-symbolic"));
g_assert (g_strv_contains (names, "application-rtf"));
#ifndef __APPLE__
g_assert (g_strv_contains (names, "x-office-document-symbolic"));
g_assert (g_strv_contains (names, "x-office-document"));
#endif
}
g_object_unref (icon);
g_free (type);
@ -334,8 +347,10 @@ test_type_is_a_special_case (void)
/* Everything but the inode type is application/octet-stream */
res = g_content_type_is_a ("inode/directory", "application/octet-stream");
g_assert_false (res);
#ifndef __APPLE__
res = g_content_type_is_a ("anything", "application/octet-stream");
g_assert_true (res);
#endif
}
static void