[gnome-flashback] desktop: add Move to Trash to icon menu



commit 2a9fc0826e4dff5e8f0b55a4134bb1838801bd54
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Fri Nov 22 23:33:06 2019 +0200

    desktop: add Move to Trash to icon menu

 gnome-flashback/libdesktop/gf-home-icon.c  |   7 ++
 gnome-flashback/libdesktop/gf-icon-view.c  |  33 ++++++++
 gnome-flashback/libdesktop/gf-icon-view.h  |   3 +
 gnome-flashback/libdesktop/gf-icon.c       | 126 +++++++++++++++++++++++------
 gnome-flashback/libdesktop/gf-icon.h       |   2 +
 gnome-flashback/libdesktop/gf-trash-icon.c |   7 ++
 6 files changed, 153 insertions(+), 25 deletions(-)
---
diff --git a/gnome-flashback/libdesktop/gf-home-icon.c b/gnome-flashback/libdesktop/gf-home-icon.c
index 41a939a..ae2bb54 100644
--- a/gnome-flashback/libdesktop/gf-home-icon.c
+++ b/gnome-flashback/libdesktop/gf-home-icon.c
@@ -45,6 +45,12 @@ gf_home_icon_get_text (GfIcon *icon)
   return _("Home");
 }
 
+static gboolean
+gf_home_icon_can_delete (GfIcon *icon)
+{
+  return FALSE;
+}
+
 static gboolean
 gf_home_icon_can_rename (GfIcon *icon)
 {
@@ -60,6 +66,7 @@ gf_home_icon_class_init (GfHomeIconClass *self_class)
 
   icon_class->get_icon = gf_home_icon_get_icon;
   icon_class->get_text = gf_home_icon_get_text;
+  icon_class->can_delete = gf_home_icon_can_delete;
   icon_class->can_rename = gf_home_icon_can_rename;
 }
 
diff --git a/gnome-flashback/libdesktop/gf-icon-view.c b/gnome-flashback/libdesktop/gf-icon-view.c
index 2b5f507..a59442d 100644
--- a/gnome-flashback/libdesktop/gf-icon-view.c
+++ b/gnome-flashback/libdesktop/gf-icon-view.c
@@ -513,6 +513,25 @@ empty_trash_cb (GObject      *object,
     }
 }
 
+static void
+trash_files_cb (GObject      *object,
+                GAsyncResult *res,
+                gpointer      user_data)
+{
+  GError *error;
+
+  error = NULL;
+  gf_nautilus_gen_call_trash_files_finish (GF_NAUTILUS_GEN (object),
+                                           res, &error);
+
+  if (error != NULL)
+    {
+      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        g_warning ("Error moving files to trash: %s", error->message);
+      g_error_free (error);
+    }
+}
+
 static void
 rename_file_cb (GObject      *object,
                 GAsyncResult *res,
@@ -2880,6 +2899,20 @@ gf_icon_view_validate_new_name (GfIconView  *self,
   return valid;
 }
 
+void
+gf_icon_view_move_to_trash (GfIconView         *self,
+                            const char * const *uris)
+{
+  if (self->nautilus == NULL)
+    return;
+
+  gf_nautilus_gen_call_trash_files (self->nautilus,
+                                    uris,
+                                    self->cancellable,
+                                    trash_files_cb,
+                                    NULL);
+}
+
 void
 gf_icon_view_rename_file (GfIconView *self,
                           const char *uri,
diff --git a/gnome-flashback/libdesktop/gf-icon-view.h b/gnome-flashback/libdesktop/gf-icon-view.h
index 3f9f003..e0ab375 100644
--- a/gnome-flashback/libdesktop/gf-icon-view.h
+++ b/gnome-flashback/libdesktop/gf-icon-view.h
@@ -49,6 +49,9 @@ gboolean            gf_icon_view_validate_new_name        (GfIconView          *
                                                            const char          *new_name,
                                                            char               **message);
 
+void                gf_icon_view_move_to_trash            (GfIconView          *self,
+                                                           const char * const  *uris);
+
 void                gf_icon_view_rename_file              (GfIconView          *self,
                                                            const char          *uri,
                                                            const char          *new_name);
diff --git a/gnome-flashback/libdesktop/gf-icon.c b/gnome-flashback/libdesktop/gf-icon.c
index fd50d34..83486d4 100644
--- a/gnome-flashback/libdesktop/gf-icon.c
+++ b/gnome-flashback/libdesktop/gf-icon.c
@@ -103,6 +103,44 @@ update_state (GfIcon *self)
   gtk_widget_set_state_flags (GTK_WIDGET (self), state, TRUE);
 }
 
+static char **
+get_selected_uris (GfIcon *self)
+{
+  GfIconPrivate *priv;
+  GList *selected_icons;
+  int n_uris;
+  char **uris;
+  GFile *file;
+  GList *l;
+  int i;
+
+  priv = gf_icon_get_instance_private (self);
+
+  selected_icons = gf_icon_view_get_selected_icons (priv->icon_view);
+  if (selected_icons == NULL)
+    return NULL;
+
+  n_uris = g_list_length (selected_icons);
+  uris = g_new0 (char *, n_uris + 1);
+
+  file = gf_icon_get_file (self);
+  uris[0] = g_file_get_uri (file);
+
+  for (l = selected_icons, i = 1; l != NULL; l = l->next)
+    {
+      GfIcon *icon;
+
+      icon = l->data;
+      if (icon == self)
+        continue;
+
+      file = gf_icon_get_file (icon);
+      uris[i++] = g_file_get_uri (file);
+    }
+
+  return uris;
+}
+
 static void
 rename_validate_cb (GfRenamePopover *popover,
                     const char      *new_name,
@@ -178,6 +216,23 @@ open_cb (GtkMenuItem *item,
   gf_icon_open (self);
 }
 
+static void
+move_to_trash_cb (GtkMenuItem *item,
+                  GfIcon      *self)
+{
+  GfIconPrivate *priv;
+  char **uris;
+
+  priv = gf_icon_get_instance_private (self);
+
+  uris = get_selected_uris (self);
+  if (uris == NULL)
+    return;
+
+  gf_icon_view_move_to_trash (priv->icon_view, (const char * const *) uris);
+  g_strfreev (uris);
+}
+
 static void
 rename_cb (GtkMenuItem *item,
            GfIcon      *self)
@@ -226,37 +281,14 @@ properties_cb (GtkMenuItem *item,
                GfIcon      *self)
 {
   GfIconPrivate *priv;
-  GList *selected_icons;
-  int n_uris;
   char **uris;
-  GFile *file;
-  GList *l;
-  int i;
 
   priv = gf_icon_get_instance_private (self);
 
-  selected_icons = gf_icon_view_get_selected_icons (priv->icon_view);
-  if (selected_icons == NULL)
+  uris = get_selected_uris (self);
+  if (uris == NULL)
     return;
 
-  n_uris = g_list_length (selected_icons);
-  uris = g_new0 (char *, n_uris + 1);
-
-  file = gf_icon_get_file (self);
-  uris[0] = g_file_get_uri (file);
-
-  for (l = selected_icons, i = 1; l != NULL; l = l->next)
-    {
-      GfIcon *icon;
-
-      icon = l->data;
-      if (icon == self)
-        continue;
-
-      file = gf_icon_get_file (icon);
-      uris[i++] = g_file_get_uri (file);
-    }
-
   gf_icon_view_show_item_properties (priv->icon_view,
                                      (const char * const *) uris);
 
@@ -272,6 +304,9 @@ create_popup_menu (GfIcon *self)
   GList *selected_icons;
   int n_selected_icons;
   GtkWidget *item;
+  gboolean show_delete;
+  gboolean disable_delete;
+  GList *l;
 
   priv = gf_icon_get_instance_private (self);
 
@@ -292,6 +327,40 @@ create_popup_menu (GfIcon *self)
                     G_CALLBACK (open_cb),
                     self);
 
+  show_delete = FALSE;
+  disable_delete = FALSE;
+
+  if (n_selected_icons == 1 &&
+      GF_ICON_GET_CLASS (self)->can_delete (GF_ICON (self)))
+    show_delete = TRUE;
+
+  if (n_selected_icons > 1)
+    {
+      for (l = selected_icons; l != NULL; l = l->next)
+        {
+          if (GF_ICON_GET_CLASS (l->data)->can_delete (GF_ICON (l->data)))
+            show_delete = TRUE;
+          else
+            disable_delete = TRUE;
+        }
+    }
+
+  if (show_delete)
+    {
+      item = gtk_separator_menu_item_new ();
+      gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
+      gtk_widget_show (item);
+
+      item = gtk_menu_item_new_with_label (_("Move to Trash"));
+      gtk_widget_set_sensitive (item, !disable_delete);
+      gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
+      gtk_widget_show (item);
+
+      g_signal_connect (item, "activate",
+                        G_CALLBACK (move_to_trash_cb),
+                        self);
+    }
+
   if (GF_ICON_GET_CLASS (self)->can_rename (GF_ICON (self)))
     {
       item = gtk_separator_menu_item_new ();
@@ -884,6 +953,12 @@ gf_icon_get_text (GfIcon *self)
   return name;
 }
 
+static gboolean
+gf_icon_can_delete (GfIcon *self)
+{
+  return TRUE;
+}
+
 static gboolean
 gf_icon_can_rename (GfIcon *self)
 {
@@ -979,6 +1054,7 @@ gf_icon_class_init (GfIconClass *self_class)
 
   self_class->get_icon = gf_icon_get_icon;
   self_class->get_text = gf_icon_get_text;
+  self_class->can_delete = gf_icon_can_delete;
   self_class->can_rename = gf_icon_can_rename;
 
   install_properties (object_class);
diff --git a/gnome-flashback/libdesktop/gf-icon.h b/gnome-flashback/libdesktop/gf-icon.h
index 18d8963..d183ba0 100644
--- a/gnome-flashback/libdesktop/gf-icon.h
+++ b/gnome-flashback/libdesktop/gf-icon.h
@@ -34,6 +34,8 @@ struct _GfIconClass
 
   const char * (* get_text)   (GfIcon   *self);
 
+  gboolean     (* can_delete) (GfIcon   *self);
+
   gboolean     (* can_rename) (GfIcon   *self);
 };
 
diff --git a/gnome-flashback/libdesktop/gf-trash-icon.c b/gnome-flashback/libdesktop/gf-trash-icon.c
index 06819f3..2c17e69 100644
--- a/gnome-flashback/libdesktop/gf-trash-icon.c
+++ b/gnome-flashback/libdesktop/gf-trash-icon.c
@@ -198,6 +198,12 @@ gf_trash_icon_get_icon (GfIcon   *icon,
   return g_file_info_get_icon (info);
 }
 
+static gboolean
+gf_trash_icon_can_delete (GfIcon *icon)
+{
+  return FALSE;
+}
+
 static gboolean
 gf_trash_icon_can_rename (GfIcon *icon)
 {
@@ -217,6 +223,7 @@ gf_trash_icon_class_init (GfTrashIconClass *self_class)
   object_class->dispose = gf_trash_icon_dispose;
 
   icon_class->get_icon = gf_trash_icon_get_icon;
+  icon_class->can_delete = gf_trash_icon_can_delete;
   icon_class->can_rename = gf_trash_icon_can_rename;
 }
 


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