[gnome-flashback] desktop: monitor trash for changes



commit dc8aa2f428a1dadd62d47ee6ccbec0135f85bb03
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Fri Nov 15 18:19:29 2019 +0200

    desktop: monitor trash for changes

 gnome-flashback/libdesktop/gf-icon.c       | 38 +++++++++-----
 gnome-flashback/libdesktop/gf-icon.h       |  2 +
 gnome-flashback/libdesktop/gf-trash-icon.c | 84 +++++++++++++++++++++++++++++-
 3 files changed, 111 insertions(+), 13 deletions(-)
---
diff --git a/gnome-flashback/libdesktop/gf-icon.c b/gnome-flashback/libdesktop/gf-icon.c
index 57007d0..1472297 100644
--- a/gnome-flashback/libdesktop/gf-icon.c
+++ b/gnome-flashback/libdesktop/gf-icon.c
@@ -683,24 +683,13 @@ gf_icon_set_file (GfIcon *self,
                   GFile  *file)
 {
   GfIconPrivate *priv;
-  char *attributes;
 
   priv = gf_icon_get_instance_private (self);
 
   g_clear_object (&priv->file);
   priv->file = g_object_ref (file);
 
-  attributes = gf_icon_view_get_file_attributes (priv->icon_view);
-
-  g_file_query_info_async (file,
-                           attributes,
-                           G_FILE_QUERY_INFO_NONE,
-                           G_PRIORITY_LOW,
-                           priv->cancellable,
-                           query_info_cb,
-                           self);
-
-  g_free (attributes);
+  gf_icon_update (self);
 }
 
 GFile *
@@ -810,3 +799,28 @@ gf_icon_get_selected (GfIcon *self)
 
   return priv->selected;
 }
+
+void
+gf_icon_update (GfIcon *self)
+{
+  GfIconPrivate *priv;
+  char *attributes;
+
+  priv = gf_icon_get_instance_private (self);
+  attributes = gf_icon_view_get_file_attributes (priv->icon_view);
+
+  g_cancellable_cancel (priv->cancellable);
+  g_clear_object (&priv->cancellable);
+
+  priv->cancellable = g_cancellable_new ();
+
+  g_file_query_info_async (priv->file,
+                           attributes,
+                           G_FILE_QUERY_INFO_NONE,
+                           G_PRIORITY_LOW,
+                           priv->cancellable,
+                           query_info_cb,
+                           self);
+
+  g_free (attributes);
+}
diff --git a/gnome-flashback/libdesktop/gf-icon.h b/gnome-flashback/libdesktop/gf-icon.h
index 66cb232..5c446c2 100644
--- a/gnome-flashback/libdesktop/gf-icon.h
+++ b/gnome-flashback/libdesktop/gf-icon.h
@@ -56,6 +56,8 @@ void        gf_icon_set_selected      (GfIcon     *self,
 
 gboolean    gf_icon_get_selected      (GfIcon     *self);
 
+void        gf_icon_update            (GfIcon     *self);
+
 G_END_DECLS
 
 #endif
diff --git a/gnome-flashback/libdesktop/gf-trash-icon.c b/gnome-flashback/libdesktop/gf-trash-icon.c
index edbeeba..1900a4a 100644
--- a/gnome-flashback/libdesktop/gf-trash-icon.c
+++ b/gnome-flashback/libdesktop/gf-trash-icon.c
@@ -20,19 +20,101 @@
 
 struct _GfTrashIcon
 {
-  GfIcon parent;
+  GfIcon        parent;
+
+  GCancellable *cancellable;
+
+  GFileMonitor *monitor;
 };
 
 G_DEFINE_TYPE (GfTrashIcon, gf_trash_icon, GF_TYPE_ICON)
 
+static void
+trash_changed_cb (GFileMonitor      *monitor,
+                  GFile             *file,
+                  GFile             *other_file,
+                  GFileMonitorEvent  event_type,
+                  GfTrashIcon       *self)
+{
+  switch (event_type)
+    {
+      case G_FILE_MONITOR_EVENT_DELETED:
+      case G_FILE_MONITOR_EVENT_CREATED:
+      case G_FILE_MONITOR_EVENT_MOVED_IN:
+      case G_FILE_MONITOR_EVENT_MOVED_OUT:
+        gf_icon_update (GF_ICON (self));
+        break;
+
+      case G_FILE_MONITOR_EVENT_CHANGED:
+      case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
+      case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
+      case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
+      case G_FILE_MONITOR_EVENT_UNMOUNTED:
+      case G_FILE_MONITOR_EVENT_MOVED:
+      case G_FILE_MONITOR_EVENT_RENAMED:
+      default:
+        break;
+    }
+}
+
+static void
+gf_trash_icon_constructed (GObject *object)
+{
+  GfTrashIcon *self;
+  GError *error;
+
+  self = GF_TRASH_ICON (object);
+
+  G_OBJECT_CLASS (gf_trash_icon_parent_class)->constructed (object);
+
+  error = NULL;
+  self->monitor = g_file_monitor_directory (gf_icon_get_file (GF_ICON (self)),
+                                            G_FILE_MONITOR_WATCH_MOVES,
+                                            self->cancellable,
+                                            &error);
+
+  if (error != NULL)
+    {
+      g_warning ("%s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  g_signal_connect (self->monitor, "changed",
+                    G_CALLBACK (trash_changed_cb),
+                    self);
+}
+
+static void
+gf_trash_icon_dispose (GObject *object)
+{
+  GfTrashIcon *self;
+
+  self = GF_TRASH_ICON (object);
+
+  g_cancellable_cancel (self->cancellable);
+  g_clear_object (&self->cancellable);
+
+  g_clear_object (&self->monitor);
+
+  G_OBJECT_CLASS (gf_trash_icon_parent_class)->dispose (object);
+}
+
 static void
 gf_trash_icon_class_init (GfTrashIconClass *self_class)
 {
+  GObjectClass *object_class;
+
+  object_class = G_OBJECT_CLASS (self_class);
+
+  object_class->constructed = gf_trash_icon_constructed;
+  object_class->dispose = gf_trash_icon_dispose;
 }
 
 static void
 gf_trash_icon_init (GfTrashIcon *self)
 {
+  self->cancellable = g_cancellable_new ();
 }
 
 GtkWidget *


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