[gnome-builder/wip/gtk4-port] libide/vcs: simplify IdeVcsBranch interface



commit acc650e8116b0b83df31e6e8a192406930e6b786
Author: Christian Hergert <chergert redhat com>
Date:   Tue May 31 13:03:08 2022 -0700

    libide/vcs: simplify IdeVcsBranch interface
    
    Just use required properties instead of virtual method getters. This makes
    things easier to bind from a GtkBuilder perspective.

 src/libide/vcs/ide-vcs-branch.c          |  37 ++++++-----
 src/libide/vcs/ide-vcs-branch.h          |   7 +--
 src/plugins/git/gbp-git-branch.c         | 105 ++++++++++++++++++++++---------
 src/plugins/git/gbp-git-branch.h         |   2 +-
 src/plugins/git/gbp-git-vcs.c            |   6 +-
 src/plugins/vcsui/gbp-vcsui-tree-addin.c |   2 +-
 6 files changed, 104 insertions(+), 55 deletions(-)
---
diff --git a/src/libide/vcs/ide-vcs-branch.c b/src/libide/vcs/ide-vcs-branch.c
index 02f7c34d5..0248f7718 100644
--- a/src/libide/vcs/ide-vcs-branch.c
+++ b/src/libide/vcs/ide-vcs-branch.c
@@ -1,6 +1,6 @@
 /* ide-vcs-branch.c
  *
- * Copyright 2019 Christian Hergert <chergert redhat com>
+ * Copyright 2019-2022 Christian Hergert <chergert redhat com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -29,10 +29,17 @@ G_DEFINE_INTERFACE (IdeVcsBranch, ide_vcs_branch, G_TYPE_OBJECT)
 static void
 ide_vcs_branch_default_init (IdeVcsBranchInterface *iface)
 {
+  g_object_interface_install_property (iface,
+                                       g_param_spec_string ("id", NULL, NULL, NULL,
+                                                            (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
+  g_object_interface_install_property (iface,
+                                       g_param_spec_string ("name", NULL, NULL, NULL,
+                                                            (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
+
 }
 
 /**
- * ide_vcs_branch_get_name:
+ * ide_vcs_branch_dup_name:
  * @self: an #IdeVcsBranch
  *
  * Gets the name of the branch, which is used in various UI elements
@@ -40,32 +47,28 @@ ide_vcs_branch_default_init (IdeVcsBranchInterface *iface)
  *
  * Returns: (transfer full): a string containing the branch name
  */
-gchar *
-ide_vcs_branch_get_name (IdeVcsBranch *self)
+char *
+ide_vcs_branch_dup_name (IdeVcsBranch *self)
 {
+  char *name = NULL;
   g_return_val_if_fail (IDE_IS_VCS_BRANCH (self), NULL);
-
-  if (IDE_VCS_BRANCH_GET_IFACE (self)->get_name)
-    return IDE_VCS_BRANCH_GET_IFACE (self)->get_name (self);
-
-  return NULL;
+  g_object_get (self, "name", &name, NULL);
+  return name;
 }
 
 /**
- * ide_vcs_branch_get_id:
+ * ide_vcs_branch_dup_id:
  * @self: an #IdeVcsBranch
  *
  * Gets the identifier of the branch.
  *
  * Returns: (transfer full): a string containing the branch identifier
  */
-gchar *
-ide_vcs_branch_get_id (IdeVcsBranch *self)
+char *
+ide_vcs_branch_dup_id (IdeVcsBranch *self)
 {
+  char *id = NULL;
   g_return_val_if_fail (IDE_IS_VCS_BRANCH (self), NULL);
-
-  if (IDE_VCS_BRANCH_GET_IFACE (self)->get_id)
-    return IDE_VCS_BRANCH_GET_IFACE (self)->get_id (self);
-
-  return NULL;
+  g_object_get (self, "id", &id, NULL);
+  return id;
 }
diff --git a/src/libide/vcs/ide-vcs-branch.h b/src/libide/vcs/ide-vcs-branch.h
index 18ad7d373..c83b13b72 100644
--- a/src/libide/vcs/ide-vcs-branch.h
+++ b/src/libide/vcs/ide-vcs-branch.h
@@ -32,14 +32,11 @@ G_DECLARE_INTERFACE (IdeVcsBranch, ide_vcs_branch, IDE, VCS_BRANCH, GObject)
 struct _IdeVcsBranchInterface
 {
   GTypeInterface parent;
-
-  gchar *(*get_name) (IdeVcsBranch *self);
-  gchar *(*get_id)   (IdeVcsBranch *self);
 };
 
 IDE_AVAILABLE_IN_ALL
-gchar *ide_vcs_branch_get_name (IdeVcsBranch *self);
+char *ide_vcs_branch_dup_name (IdeVcsBranch *self);
 IDE_AVAILABLE_IN_ALL
-gchar *ide_vcs_branch_get_id   (IdeVcsBranch *self);
+char *ide_vcs_branch_dup_id   (IdeVcsBranch *self);
 
 G_END_DECLS
diff --git a/src/plugins/git/gbp-git-branch.c b/src/plugins/git/gbp-git-branch.c
index 1e62d17aa..bb7358b2d 100644
--- a/src/plugins/git/gbp-git-branch.c
+++ b/src/plugins/git/gbp-git-branch.c
@@ -30,44 +30,84 @@
 struct _GbpGitBranch
 {
   GObject parent_instance;
-  gchar *id;
+  char *id;
 };
 
-static gchar *
-gbp_git_branch_get_id (IdeVcsBranch *branch)
-{
-  return g_strdup (GBP_GIT_BRANCH (branch)->id);
-}
+G_DEFINE_FINAL_TYPE_WITH_CODE (GbpGitBranch, gbp_git_branch, G_TYPE_OBJECT,
+                               G_IMPLEMENT_INTERFACE (IDE_TYPE_VCS_BRANCH, NULL))
+
+enum {
+  PROP_0,
+  PROP_ID,
+  PROP_NAME,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
 
-static gchar *
-gbp_git_branch_get_name (IdeVcsBranch *branch)
+static const char *
+gbp_git_branch_get_name (GbpGitBranch *self)
 {
-  const gchar *id = GBP_GIT_BRANCH (branch)->id;
+  const char *id;
 
-  if (id && g_str_has_prefix (id, "refs/heads/"))
+  g_assert (GBP_IS_GIT_BRANCH (self));
+
+  if ((id = self->id) && g_str_has_prefix (id, "refs/heads/"))
     id += strlen ("refs/heads/");
 
-  return g_strdup (id);
+  return id;
 }
 
 static void
-vcs_branch_iface_init (IdeVcsBranchInterface *iface)
+gbp_git_branch_dispose (GObject *object)
 {
-  iface->get_name = gbp_git_branch_get_name;
-  iface->get_id = gbp_git_branch_get_id;
+  GbpGitBranch *self = (GbpGitBranch *)object;
+
+  ide_clear_string (&self->id);
+
+  G_OBJECT_CLASS (gbp_git_branch_parent_class)->dispose (object);
 }
 
-G_DEFINE_FINAL_TYPE_WITH_CODE (GbpGitBranch, gbp_git_branch, G_TYPE_OBJECT,
-                         G_IMPLEMENT_INTERFACE (IDE_TYPE_VCS_BRANCH, vcs_branch_iface_init))
+static void
+gbp_git_branch_get_property (GObject    *object,
+                             guint       prop_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+  GbpGitBranch *self = GBP_GIT_BRANCH (object);
+
+  switch (prop_id)
+    {
+    case PROP_ID:
+      g_value_set_string (value, self->id);
+      break;
+
+    case PROP_NAME:
+      g_value_set_string (value, gbp_git_branch_get_name (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
 
 static void
-gbp_git_branch_finalize (GObject *object)
+gbp_git_branch_set_property (GObject      *object,
+                             guint         prop_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
 {
-  GbpGitBranch *self = (GbpGitBranch *)object;
+  GbpGitBranch *self = GBP_GIT_BRANCH (object);
 
-  g_clear_pointer (&self->id, g_free);
+  switch (prop_id)
+    {
+    case PROP_ID:
+      self->id = g_value_dup_string (value);
+      break;
 
-  G_OBJECT_CLASS (gbp_git_branch_parent_class)->finalize (object);
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
 }
 
 static void
@@ -75,7 +115,19 @@ gbp_git_branch_class_init (GbpGitBranchClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  object_class->finalize = gbp_git_branch_finalize;
+  object_class->dispose = gbp_git_branch_dispose;
+  object_class->get_property = gbp_git_branch_get_property;
+  object_class->set_property = gbp_git_branch_set_property;
+
+  properties [PROP_ID] =
+    g_param_spec_string ("id", NULL, NULL, NULL,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_NAME] =
+    g_param_spec_string ("name", NULL, NULL, NULL,
+                         (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
 }
 
 static void
@@ -84,12 +136,9 @@ gbp_git_branch_init (GbpGitBranch *self)
 }
 
 GbpGitBranch *
-gbp_git_branch_new (const gchar *id)
+gbp_git_branch_new (const char *id)
 {
-  GbpGitBranch *self;
-
-  self = g_object_new (GBP_TYPE_GIT_BRANCH, NULL);
-  self->id = g_strdup (id);
-
-  return g_steal_pointer (&self);
+  return g_object_new (GBP_TYPE_GIT_BRANCH,
+                       "id", id,
+                       NULL);
 }
diff --git a/src/plugins/git/gbp-git-branch.h b/src/plugins/git/gbp-git-branch.h
index b7518d611..7f6ce2a36 100644
--- a/src/plugins/git/gbp-git-branch.h
+++ b/src/plugins/git/gbp-git-branch.h
@@ -28,6 +28,6 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GbpGitBranch, gbp_git_branch, GBP, GIT_BRANCH, GObject)
 
-GbpGitBranch *gbp_git_branch_new (const gchar *id);
+GbpGitBranch *gbp_git_branch_new (const char *id);
 
 G_END_DECLS
diff --git a/src/plugins/git/gbp-git-vcs.c b/src/plugins/git/gbp-git-vcs.c
index 2188efff7..81d0162e2 100644
--- a/src/plugins/git/gbp-git-vcs.c
+++ b/src/plugins/git/gbp-git-vcs.c
@@ -160,7 +160,7 @@ gbp_git_vcs_switch_branch_async (IdeVcs              *vcs,
   task = ide_task_new (self, cancellable, callback, user_data);
   ide_task_set_source_tag (task, gbp_git_vcs_switch_branch_async);
 
-  branch_id = ide_vcs_branch_get_id (branch);
+  branch_id = ide_vcs_branch_dup_id (branch);
 
   ipc_git_repository_call_switch_branch (self->repository,
                                          branch_id,
@@ -233,8 +233,8 @@ gbp_git_vcs_push_branch_async (IdeVcs              *vcs,
   task = ide_task_new (self, cancellable, callback, user_data);
   ide_task_set_source_tag (task, gbp_git_vcs_push_branch_async);
 
-  branch_id = ide_vcs_branch_get_id (branch);
-  name = ide_vcs_branch_get_name (branch);
+  branch_id = ide_vcs_branch_dup_id (branch);
+  name = ide_vcs_branch_dup_name (branch);
 
   ref_specs = g_ptr_array_new_with_free_func (g_free);
   g_ptr_array_add (ref_specs, g_strdup_printf ("%s:%s", branch_id, branch_id));
diff --git a/src/plugins/vcsui/gbp-vcsui-tree-addin.c b/src/plugins/vcsui/gbp-vcsui-tree-addin.c
index 2250cdfea..b1e9170a6 100644
--- a/src/plugins/vcsui/gbp-vcsui-tree-addin.c
+++ b/src/plugins/vcsui/gbp-vcsui-tree-addin.c
@@ -293,7 +293,7 @@ gbp_vcsui_tree_addin_list_branches_cb (GObject      *object,
       for (guint i = 0; i < branches->len; i++)
         {
           IdeVcsBranch *branch = g_ptr_array_index (branches, i);
-          g_autofree gchar *name = ide_vcs_branch_get_name (branch);
+          g_autofree gchar *name = ide_vcs_branch_dup_name (branch);
           g_autoptr(IdeTreeNode) child = NULL;
 
           child = g_object_new (IDE_TYPE_TREE_NODE,


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