Add api to get the generic icon name for a mime type
authorWilliam Jon McCann <jmccann@redhat.com>
Mon, 10 Sep 2012 19:16:58 +0000 (15:16 -0400)
committerWilliam Jon McCann <jmccann@redhat.com>
Thu, 13 Sep 2012 16:50:02 +0000 (12:50 -0400)
https://bugzilla.gnome.org/show_bug.cgi?id=683744

docs/reference/gio/gio-sections.txt
gio/gcontenttype.c
gio/gcontenttype.h
gio/gio.symbols

index ee72a68..04637d0 100644 (file)
@@ -1292,6 +1292,7 @@ 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_get_generic_icon_name
 g_content_type_can_be_executable
 g_content_type_from_mime_type
 g_content_type_guess
index 3ada6f0..2de6957 100644 (file)
@@ -399,63 +399,50 @@ g_content_type_get_icon_internal (const gchar *type,
                                   gboolean     symbolic)
 {
   char *mimetype_icon;
-  char *generic_mimetype_icon;
+  char *generic_mimetype_icon = NULL;
   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_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);
+  xdg_mimetype_generic_icon = g_content_type_get_generic_icon_name (type);
 
   mimetype_icon = g_strdup_printf (file_template, type);
+  if (xdg_mimetype_generic_icon)
+    generic_mimetype_icon = g_strdup_printf (file_template, xdg_mimetype_generic_icon);
 
   while ((q = strchr (mimetype_icon, '/')) != NULL)
     *q = '-';
 
-  p = strchr (type, '/');
-  if (p == NULL)
-    p = type + strlen (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 (generic_suffix) + 1);
-  memcpy (generic_mimetype_icon, type, p - type);
-  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;
 
   icon_names[n++] = mimetype_icon;
   icon_names[n++] = legacy_mimetype_icon;
 
-  if (xdg_mimetype_generic_icon)
-    icon_names[n++] = xdg_mimetype_generic_icon;
-
-  icon_names[n++] = generic_mimetype_icon;
+  if (generic_mimetype_icon)
+    icon_names[n++] = generic_mimetype_icon;
 
   themed_icon = g_themed_icon_new_from_names (icon_names, n);
 
@@ -501,6 +488,52 @@ g_content_type_get_symbolic_icon (const gchar *type)
 }
 
 /**
+ * g_content_type_get_generic_icon_name:
+ * @type: a content type string
+ *
+ * Gets the generic icon name for a content type.
+ *
+ * See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
+ * specification for more on the generic icon name.
+ *
+ * Returns: (allow-none): the registered generic icon name for the given @type,
+ *     or %NULL if unknown. Free with g_free()
+ *
+ * Since: 2.34
+ */
+gchar *
+g_content_type_get_generic_icon_name (const gchar *type)
+{
+  const gchar *xdg_icon_name;
+  gchar *icon_name;
+
+  G_LOCK (gio_xdgmime);
+  xdg_icon_name = xdg_mime_get_generic_icon (type);
+  G_UNLOCK (gio_xdgmime);
+
+  if (!xdg_icon_name)
+    {
+      const char *p;
+      const char *suffix = "-x-generic";
+
+      p = strchr (type, '/');
+      if (p == NULL)
+        p = type + strlen (type);
+
+      icon_name = g_malloc (p - type + strlen (suffix) + 1);
+      memcpy (icon_name, type, p - type);
+      memcpy (icon_name + (p - type), suffix, strlen (suffix));
+      icon_name[(p - type) + strlen (suffix)] = 0;
+    }
+  else
+    {
+      icon_name = g_strdup (xdg_icon_name);
+    }
+
+  return icon_name;
+}
+
+/**
  * g_content_type_can_be_executable:
  * @type: a content type string
  *
index 5e3d3ce..fb4b861 100644 (file)
@@ -40,6 +40,9 @@ 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);
+GLIB_AVAILABLE_IN_2_34
+gchar *  g_content_type_get_generic_icon_name (const gchar  *type);
+
 gboolean g_content_type_can_be_executable (const gchar  *type);
 
 gchar *  g_content_type_from_mime_type    (const gchar  *mime_type);
index 9b90a36..536f0d1 100644 (file)
@@ -162,6 +162,7 @@ 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_get_generic_icon_name
 g_content_type_can_be_executable
 g_content_type_from_mime_type
 g_content_type_guess