[gnome-flashback/gnome-3-36] desktop: monitor files for changes



commit 44ecd559df70c746e18d3cb65e63309d32389034
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Tue Apr 28 12:18:07 2020 +0300

    desktop: monitor files for changes
    
    https://launchpad.net/bugs/1875317

 gnome-flashback/libdesktop/gf-icon.c       | 103 +++++++++++++++++++++++++++--
 gnome-flashback/libdesktop/gf-icon.h       |  12 ++--
 gnome-flashback/libdesktop/gf-trash-icon.c |  48 +++++++++-----
 3 files changed, 136 insertions(+), 27 deletions(-)
---
diff --git a/gnome-flashback/libdesktop/gf-icon.c b/gnome-flashback/libdesktop/gf-icon.c
index a4c1adb..6305043 100644
--- a/gnome-flashback/libdesktop/gf-icon.c
+++ b/gnome-flashback/libdesktop/gf-icon.c
@@ -41,6 +41,8 @@ typedef struct
   GFile           *file;
   GFileInfo       *info;
 
+  GFileMonitor    *monitor;
+
   GfIconSize       icon_size;
   guint            extra_text_width;
 
@@ -1004,6 +1006,69 @@ set_icon_size (GfIcon *self,
   update_icon (self);
 }
 
+static void
+file_changed_cb (GFileMonitor      *monitor,
+                 GFile             *file,
+                 GFile             *other_file,
+                 GFileMonitorEvent  event_type,
+                 GfIcon            *self)
+{
+  switch (event_type)
+    {
+      case G_FILE_MONITOR_EVENT_CHANGED:
+        break;
+
+      case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
+        icon_refresh (self);
+        break;
+
+      case G_FILE_MONITOR_EVENT_DELETED:
+        break;
+
+      case G_FILE_MONITOR_EVENT_CREATED:
+        break;
+
+      case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
+        gf_icon_update (self);
+        break;
+
+      case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
+        break;
+
+      case G_FILE_MONITOR_EVENT_UNMOUNTED:
+        break;
+
+      case G_FILE_MONITOR_EVENT_MOVED:
+        break;
+
+      case G_FILE_MONITOR_EVENT_RENAMED:
+        break;
+
+      case G_FILE_MONITOR_EVENT_MOVED_IN:
+        break;
+
+      case G_FILE_MONITOR_EVENT_MOVED_OUT:
+        break;
+
+      default:
+        break;
+    }
+}
+
+static void
+set_file (GfIcon *self,
+          GFile  *file)
+{
+  GfIconPrivate *priv;
+
+  priv = gf_icon_get_instance_private (self);
+
+  g_clear_object (&priv->file);
+  priv->file = g_object_ref (file);
+
+  GF_ICON_GET_CLASS (self)->create_file_monitor (self);
+}
+
 static void
 gf_icon_constructed (GObject *object)
 {
@@ -1031,6 +1096,8 @@ gf_icon_dispose (GObject *object)
   g_clear_object (&priv->file);
   g_clear_object (&priv->info);
 
+  g_clear_object (&priv->monitor);
+
   g_clear_object (&priv->app_info);
 
   g_clear_object (&priv->thumbnail);
@@ -1107,7 +1174,7 @@ gf_icon_set_property (GObject      *object,
 
       case PROP_FILE:
         g_assert (priv->file == NULL);
-        priv->file = g_value_dup_object (value);
+        set_file (self, g_value_get_object (value));
         break;
 
       case PROP_INFO:
@@ -1149,6 +1216,35 @@ gf_icon_get_preferred_width (GtkWidget *widget,
   *natural_width += priv->extra_text_width;
 }
 
+static void
+gf_icon_create_file_monitor (GfIcon *self)
+{
+  GfIconPrivate *priv;
+  GError *error;
+
+  priv = gf_icon_get_instance_private (self);
+
+  g_clear_object (&priv->monitor);
+
+  error = NULL;
+  priv->monitor = g_file_monitor_file (priv->file,
+                                       G_FILE_MONITOR_NONE,
+                                       NULL,
+                                       &error);
+
+  if (error != NULL)
+    {
+      g_warning ("%s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  g_signal_connect (priv->monitor,
+                    "changed",
+                    G_CALLBACK (file_changed_cb),
+                    self);
+}
+
 static GIcon *
 gf_icon_get_icon (GfIcon   *self,
                   gboolean *is_thumbnail)
@@ -1294,6 +1390,7 @@ gf_icon_class_init (GfIconClass *self_class)
 
   widget_class->get_preferred_width = gf_icon_get_preferred_width;
 
+  self_class->create_file_monitor = gf_icon_create_file_monitor;
   self_class->get_icon = gf_icon_get_icon;
   self_class->get_text = gf_icon_get_text;
   self_class->can_delete = gf_icon_can_delete;
@@ -1411,9 +1508,7 @@ gf_icon_set_file (GfIcon *self,
 
   g_clear_pointer (&priv->popover, gtk_widget_destroy);
 
-  g_clear_object (&priv->file);
-  priv->file = g_object_ref (file);
-
+  set_file (self, file);
   gf_icon_update (self);
 }
 
diff --git a/gnome-flashback/libdesktop/gf-icon.h b/gnome-flashback/libdesktop/gf-icon.h
index 399978c..8de6a5e 100644
--- a/gnome-flashback/libdesktop/gf-icon.h
+++ b/gnome-flashback/libdesktop/gf-icon.h
@@ -29,14 +29,16 @@ struct _GfIconClass
 {
   GtkButtonClass parent_class;
 
-  GIcon      * (* get_icon)   (GfIcon   *self,
-                              gboolean *is_thumbnail);
+  void         (* create_file_monitor) (GfIcon   *self);
 
-  const char * (* get_text)   (GfIcon   *self);
+  GIcon      * (* get_icon)            (GfIcon   *self,
+                                        gboolean *is_thumbnail);
 
-  gboolean     (* can_delete) (GfIcon   *self);
+  const char * (* get_text)            (GfIcon   *self);
 
-  gboolean     (* can_rename) (GfIcon   *self);
+  gboolean     (* can_delete)          (GfIcon   *self);
+
+  gboolean     (* can_rename)          (GfIcon   *self);
 };
 
 GtkWidget  *gf_icon_new               (GfIconView *icon_view,
diff --git a/gnome-flashback/libdesktop/gf-trash-icon.c b/gnome-flashback/libdesktop/gf-trash-icon.c
index 2c17e69..5b1c642 100644
--- a/gnome-flashback/libdesktop/gf-trash-icon.c
+++ b/gnome-flashback/libdesktop/gf-trash-icon.c
@@ -145,29 +145,11 @@ 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);
-
   check_if_empty (self);
 }
 
@@ -186,6 +168,35 @@ gf_trash_icon_dispose (GObject *object)
   G_OBJECT_CLASS (gf_trash_icon_parent_class)->dispose (object);
 }
 
+static void
+gf_trash_icon_create_file_monitor (GfIcon *icon)
+{
+  GfTrashIcon *self;
+  GError *error;
+
+  self = GF_TRASH_ICON (icon);
+
+  g_clear_object (&self->monitor);
+
+  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 GIcon *
 gf_trash_icon_get_icon (GfIcon   *icon,
                         gboolean *is_thumbnail)
@@ -222,6 +233,7 @@ gf_trash_icon_class_init (GfTrashIconClass *self_class)
   object_class->constructed = gf_trash_icon_constructed;
   object_class->dispose = gf_trash_icon_dispose;
 
+  icon_class->create_file_monitor = gf_trash_icon_create_file_monitor;
   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]