[gnome-panel] menu: add gp_menu_utils_get_icon_for_file



commit a998e046f781318e92ee28215e10da332a698816
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sat Feb 3 20:34:52 2018 +0200

    menu: add gp_menu_utils_get_icon_for_file
    
    This function is different from panel_util_get_icon_for_uri(), but
    hopefully it will provide similar results. Known differences:
    
    - no longer returns special icon for `x-nautilus-search` uri and
    at least in last two major versions nautilus crashes when used with
    this uri - https://gitlab.gnome.org/GNOME/nautilus/issues/234.
    
    - no longer returns special icon for `burn` uri, seems that returned
    icon `nautilus-cd-burner` does not exist.

 modules/menu/gp-menu-utils.c |   90 ++++++++++++++++++++++++++++++++++++++++++
 modules/menu/gp-menu-utils.h |    4 +-
 2 files changed, 93 insertions(+), 1 deletions(-)
---
diff --git a/modules/menu/gp-menu-utils.c b/modules/menu/gp-menu-utils.c
index 9963f53..4577d2a 100644
--- a/modules/menu/gp-menu-utils.c
+++ b/modules/menu/gp-menu-utils.c
@@ -23,6 +23,69 @@
 
 #include "gp-menu-utils.h"
 
+static GFile *
+get_file_root (GFile *file)
+{
+  GFile *parent;
+
+  g_object_ref (file);
+  while ((parent = g_file_get_parent (file)) != NULL)
+    {
+      g_object_unref (file);
+      file = parent;
+    }
+
+  return file;
+}
+
+static GIcon *
+get_icon_if_mount (GFile *file)
+{
+  GMount *mount;
+  GIcon *icon;
+
+  mount = g_file_find_enclosing_mount (file, NULL, NULL);
+  if (mount == NULL)
+    return NULL;
+
+  icon = g_mount_get_icon (mount);
+  g_object_unref (mount);
+
+  return icon;
+}
+
+static GIcon *
+get_icon_if_trash (GFile *file)
+{
+  gchar *uri;
+  gboolean is_trash;
+  GFile *root;
+  GFileQueryInfoFlags flags;
+  GFileInfo *info;
+  GIcon *icon;
+
+  uri = g_file_get_uri (file);
+  is_trash = g_str_has_prefix (uri, "trash:");
+  g_free (uri);
+
+  if (!is_trash)
+    return NULL;
+
+  root = get_file_root (file);
+  flags = G_FILE_QUERY_INFO_NONE;
+
+  info = g_file_query_info (root, "standard::icon", flags, NULL, NULL);
+  g_object_unref (root);
+
+  if (info == NULL)
+    return NULL;
+
+  icon = g_object_ref (g_file_info_get_icon (info));
+  g_object_unref (info);
+
+  return icon;
+}
+
 static void
 child_setup (gpointer user_data)
 {
@@ -93,3 +156,30 @@ gp_menu_utils_launch_app_info (GDesktopAppInfo *app_info)
 
   g_clear_error (&error);
 }
+
+GIcon *
+gp_menu_utils_get_icon_for_file (GFile *file)
+{
+  GIcon *icon;
+  GFileQueryInfoFlags flags;
+  GFileInfo *info;
+
+  icon = get_icon_if_mount (file);
+  if (icon != NULL)
+    return icon;
+
+  icon = get_icon_if_trash (file);
+  if (icon != NULL)
+    return icon;
+
+  flags = G_FILE_QUERY_INFO_NONE;
+  info = g_file_query_info (file, "standard::icon", flags, NULL, NULL);
+
+  if (info == NULL)
+    return NULL;
+
+  icon = g_object_ref (g_file_info_get_icon (info));
+  g_object_unref (info);
+
+  return icon;
+}
diff --git a/modules/menu/gp-menu-utils.h b/modules/menu/gp-menu-utils.h
index 3fcd6a0..37d9d6f 100644
--- a/modules/menu/gp-menu-utils.h
+++ b/modules/menu/gp-menu-utils.h
@@ -22,7 +22,9 @@
 
 G_BEGIN_DECLS
 
-void gp_menu_utils_launch_app_info (GDesktopAppInfo *app_info);
+void   gp_menu_utils_launch_app_info   (GDesktopAppInfo *app_info);
+
+GIcon *gp_menu_utils_get_icon_for_file (GFile           *file);
 
 G_END_DECLS
 


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