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



commit 6acaca8831c4c34845b860100effead0f37a8d7c
Author: Jiří Techet <techet gmail com>
Date:   Fri Oct 27 23:45:14 2017 +0200

    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

 gio/gosxcontenttype.c   |   60 ++++++++++++++++++++++++++++++++++++----------
 gio/tests/contenttype.c |   17 ++++++++++++-
 2 files changed, 63 insertions(+), 14 deletions(-)
---
diff --git a/gio/gosxcontenttype.c b/gio/gosxcontenttype.c
index 604a1ed..b646f6e 100644
--- a/gio/gosxcontenttype.c
+++ b/gio/gosxcontenttype.c
@@ -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);
+
+  G_LOCK (gio_xdgmime);
+  xdg_icon = xdg_mime_get_icon (type);
+  G_UNLOCK (gio_xdgmime);
+
+  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;
 
-  icon = g_themed_icon_new_with_default_fallbacks (name);
+  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;
+    }
 
-  return icon;
+  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 *
diff --git a/gio/tests/contenttype.c b/gio/tests/contenttype.c
index a0da5f6..2424b8e 100644
--- a/gio/tests/contenttype.c
+++ b/gio/tests/contenttype.c
@@ -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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]