[gnome-builder/wip/chergert/git-oop: 32/34] emit changed signal when monitor notifies



commit c6e93bc9e9a81baa32d5c7022f89a9248d46b877
Author: Christian Hergert <chergert redhat com>
Date:   Thu Mar 21 19:03:47 2019 -0700

    emit changed signal when monitor notifies

 src/plugins/git/gbp-git.c   | 67 +++++++++++++++++++++++++++++++++++++++++----
 src/plugins/git/meson.build |  1 +
 2 files changed, 62 insertions(+), 6 deletions(-)
---
diff --git a/src/plugins/git/gbp-git.c b/src/plugins/git/gbp-git.c
index 43a150b75..679ef112d 100644
--- a/src/plugins/git/gbp-git.c
+++ b/src/plugins/git/gbp-git.c
@@ -25,28 +25,39 @@
 #include <libgit2-glib/ggit.h>
 
 #include "gbp-git.h"
+#include "gbp-git-index-monitor.h"
 #include "line-cache.h"
 
 struct _GbpGit
 {
-  GObject         parent_instance;
+  GObject             parent_instance;
 
   /* Mutex for field access */
-  GMutex          mutex;
+  GMutex              mutex;
 
   /* Working directory as set by peer */
-  GFile          *workdir;
+  GFile              *workdir;
 
   /* Repository we've opened from initialize call */
-  GgitRepository *repository;
+  GgitRepository     *repository;
+
+  /* A monitor that we can use to inform the peer of changes */
+  GbpGitIndexMonitor *monitor;
 
   /* Cached blob so that we can avoid re-looking up file data */
-  gchar          *last_blob_path;
-  GgitBlob       *last_blob;
+  gchar              *last_blob_path;
+  GgitBlob           *last_blob;
 };
 
 G_DEFINE_TYPE (GbpGit, gbp_git, G_TYPE_OBJECT)
 
+enum {
+  CHANGED,
+  N_SIGNALS
+};
+
+static guint signals [N_SIGNALS];
+
 static void
 gbp_git_finalize (GObject *object)
 {
@@ -67,6 +78,18 @@ gbp_git_class_init (GbpGitClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
   object_class->finalize = gbp_git_finalize;
+
+  signals [CHANGED] =
+    g_signal_new ("changed",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  0,
+                  NULL, NULL,
+                  g_cclosure_marshal_VOID__VOID,
+                  G_TYPE_NONE, 0);
+  g_signal_set_va_marshaller (signals [CHANGED],
+                              G_TYPE_FROM_CLASS (klass),
+                              g_cclosure_marshal_VOID__VOIDv);
 }
 
 static void
@@ -81,16 +104,48 @@ gbp_git_new (void)
   return g_object_new (GBP_TYPE_GIT, NULL);
 }
 
+static void
+gbp_git_monitor_changed_cb (GbpGit             *self,
+                            GbpGitIndexMonitor *monitor)
+{
+  g_assert (GBP_IS_GIT (self));
+  g_assert (GBP_IS_GIT_INDEX_MONITOR (monitor));
+
+  g_signal_emit (self, signals [CHANGED], 0);
+}
+
 void
 gbp_git_set_workdir (GbpGit *self,
                      GFile  *workdir)
 {
+  g_autoptr(GMutexLocker) locker = NULL;
+
   g_return_if_fail (GBP_IS_GIT (self));
   g_return_if_fail (G_IS_FILE (workdir));
 
+  locker = g_mutex_locker_new (&self->mutex);
+
   if (g_set_object (&self->workdir, workdir))
     {
       g_clear_object (&self->repository);
+      g_clear_object (&self->monitor);
+
+      if (workdir != NULL)
+        {
+          if ((self->repository = ggit_repository_open (workdir, NULL)))
+            {
+              g_autoptr(GFile) location = NULL;
+
+              location = ggit_repository_get_location (self->repository);
+              self->monitor = gbp_git_index_monitor_new (location);
+
+              g_signal_connect_object (self->monitor,
+                                       "changed",
+                                       G_CALLBACK (gbp_git_monitor_changed_cb),
+                                       self,
+                                       G_CONNECT_SWAPPED);
+            }
+        }
     }
 }
 
diff --git a/src/plugins/git/meson.build b/src/plugins/git/meson.build
index ec56fcd74..179616672 100644
--- a/src/plugins/git/meson.build
+++ b/src/plugins/git/meson.build
@@ -22,6 +22,7 @@ plugins_sources += files([
 
 gnome_builder_git_sources = [
   'gbp-git.c',
+  'gbp-git-index-monitor.c',
   'gbp-git-remote-callbacks.c',
   'gnome-builder-git.c',
   'line-cache.c',


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