Add ability to get symbolic icon for content type

https://bugzilla.gnome.org/show_bug.cgi?id=682101
This commit is contained in:
William Jon McCann 2012-08-28 19:22:01 -04:00
parent a15a071f35
commit 40b4fae42e
5 changed files with 112 additions and 49 deletions

View File

@ -1291,6 +1291,7 @@ g_content_type_is_unknown
g_content_type_get_description
g_content_type_get_mime_type
g_content_type_get_icon
g_content_type_get_symbolic_icon
g_content_type_can_be_executable
g_content_type_from_mime_type
g_content_type_guess

View File

@ -393,34 +393,43 @@ g_content_type_get_mime_type (const char *type)
return g_strdup (type);
}
/**
* g_content_type_get_icon:
* @type: a content type string
*
* Gets the icon for a content type.
*
* Returns: (transfer full): #GIcon corresponding to the content type. Free the returned
* object with g_object_unref()
*/
GIcon *
g_content_type_get_icon (const gchar *type)
static GIcon *
g_content_type_get_icon_internal (const gchar *type,
gboolean symbolic)
{
char *mimetype_icon, *generic_mimetype_icon, *q;
char *xdg_mimetype_icon, *legacy_mimetype_icon;
char *mimetype_icon;
char *generic_mimetype_icon;
char *q;
char *xdg_mimetype_icon;
char *legacy_mimetype_icon;
char *xdg_mimetype_generic_icon;
char *icon_names[5];
int n = 0;
const char *p;
GIcon *themed_icon;
const char *file_template;
const char *generic_suffix;
g_return_val_if_fail (type != NULL, NULL);
if (symbolic)
{
file_template = "%s-symbolic";
generic_suffix = "-x-generic-symbolic";
}
else
{
file_template = "%s";
generic_suffix = "-x-generic";
}
G_LOCK (gio_xdgmime);
xdg_mimetype_icon = g_strdup (xdg_mime_get_icon (type));
xdg_mimetype_generic_icon = g_strdup (xdg_mime_get_generic_icon (type));
xdg_mimetype_icon = g_strdup_printf (file_template, xdg_mime_get_icon (type));
xdg_mimetype_generic_icon = g_strdup_printf (file_template, xdg_mime_get_generic_icon (type));
G_UNLOCK (gio_xdgmime);
mimetype_icon = g_strdup (type);
mimetype_icon = g_strdup_printf (file_template, type);
while ((q = strchr (mimetype_icon, '/')) != NULL)
*q = '-';
@ -432,10 +441,10 @@ g_content_type_get_icon (const gchar *type)
/* Not all icons have migrated to the new icon theme spec, look for old names too */
legacy_mimetype_icon = g_strconcat ("gnome-mime-", mimetype_icon, NULL);
generic_mimetype_icon = g_malloc (p - type + strlen ("-x-generic") + 1);
generic_mimetype_icon = g_malloc (p - type + strlen (generic_suffix) + 1);
memcpy (generic_mimetype_icon, type, p - type);
memcpy (generic_mimetype_icon + (p - type), "-x-generic", strlen ("-x-generic"));
generic_mimetype_icon[(p - type) + strlen ("-x-generic")] = 0;
memcpy (generic_mimetype_icon + (p - type), generic_suffix, strlen (generic_suffix));
generic_mimetype_icon[(p - type) + strlen (generic_suffix)] = 0;
if (xdg_mimetype_icon)
icon_names[n++] = xdg_mimetype_icon;
@ -459,6 +468,38 @@ g_content_type_get_icon (const gchar *type)
return themed_icon;
}
/**
* g_content_type_get_icon:
* @type: a content type string
*
* Gets the icon for a content type.
*
* Returns: (transfer full): #GIcon corresponding to the content type. Free the returned
* object with g_object_unref()
*/
GIcon *
g_content_type_get_icon (const gchar *type)
{
return g_content_type_get_icon_internal (type, FALSE);
}
/**
* g_content_type_get_symbolic_icon:
* @type: a content type string
*
* Gets the symbolic icon for a content type.
*
* Returns: (transfer full): symbolic #GIcon corresponding to the content type.
* Free the returned object with g_object_unref()
*
* Since: 2.34
*/
GIcon *
g_content_type_get_symbolic_icon (const gchar *type)
{
return g_content_type_get_icon_internal (type, TRUE);
}
/**
* g_content_type_can_be_executable:
* @type: a content type string

View File

@ -39,6 +39,7 @@ gboolean g_content_type_is_unknown (const gchar *type);
gchar * g_content_type_get_description (const gchar *type);
gchar * g_content_type_get_mime_type (const gchar *type);
GIcon * g_content_type_get_icon (const gchar *type);
GIcon * g_content_type_get_symbolic_icon (const gchar *type);
gboolean g_content_type_can_be_executable (const gchar *type);
gchar * g_content_type_from_mime_type (const gchar *mime_type);

View File

@ -161,6 +161,7 @@ g_content_type_is_unknown
g_content_type_get_description
g_content_type_get_mime_type
g_content_type_get_icon
g_content_type_get_symbolic_icon
g_content_type_can_be_executable
g_content_type_from_mime_type
g_content_type_guess

View File

@ -1505,6 +1505,40 @@ get_icon_name (const char *path,
return name;
}
static GIcon *
get_icon (const char *path,
const char *content_type,
gboolean is_folder,
gboolean use_symbolic)
{
GIcon *icon = NULL;
const char *icon_name;
gboolean with_fallbacks;
icon_name = get_icon_name (path, use_symbolic, &with_fallbacks);
if (icon_name != NULL)
{
if (with_fallbacks)
icon = g_themed_icon_new_with_default_fallbacks (icon_name);
else
icon = g_themed_icon_new (icon_name);
}
else
{
if (use_symbolic)
icon = g_content_type_get_symbolic_icon (content_type);
else
icon = g_content_type_get_icon (content_type);
if (G_IS_THEMED_ICON (icon) && is_folder)
{
g_themed_icon_append_name (G_THEMED_ICON (icon), use_symbolic ? "folder-symbolic" : "folder");
}
}
return icon;
}
GFileInfo *
_g_local_file_info_get (const char *basename,
const char *path,
@ -1666,46 +1700,31 @@ _g_local_file_info_get (const char *basename,
if (content_type)
{
gboolean use_symbolics = FALSE;
g_file_info_set_content_type (info, content_type);
use_symbolics = _g_file_attribute_matcher_matches_id (attribute_matcher,
G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON);
if (use_symbolics ||
_g_file_attribute_matcher_matches_id (attribute_matcher,
G_FILE_ATTRIBUTE_ID_STANDARD_ICON))
if (_g_file_attribute_matcher_matches_id (attribute_matcher,
G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
|| _g_file_attribute_matcher_matches_id (attribute_matcher,
G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
{
GIcon *icon;
gboolean with_fallbacks = TRUE;
const char *icon_name = get_icon_name (path, use_symbolics, &with_fallbacks);
if (icon_name != NULL)
{
if (with_fallbacks)
icon = g_themed_icon_new_with_default_fallbacks (icon_name);
else
icon = g_themed_icon_new (icon_name);
}
else
{
icon = g_content_type_get_icon (content_type);
if (G_IS_THEMED_ICON (icon))
{
const char *type_icon = NULL;
if (S_ISDIR (statbuf.st_mode))
type_icon = "folder";
if (type_icon)
g_themed_icon_append_name (G_THEMED_ICON (icon), type_icon);
}
}
/* non symbolic icon */
icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), FALSE);
if (icon != NULL)
{
g_file_info_set_icon (info, icon);
g_object_unref (icon);
}
/* symbolic icon */
icon = get_icon (path, content_type, S_ISDIR (statbuf.st_mode), TRUE);
if (icon != NULL)
{
g_file_info_set_symbolic_icon (info, icon);
g_object_unref (icon);
}
}
g_free (content_type);