[libgit2-glib] Make GgitDiffOptions an object



commit 8b2a3e4138e380e30a1a2f64dd5245e8b1c6ebe8
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Mon Jun 30 19:56:52 2014 +0200

    Make GgitDiffOptions an object

 libgit2-glib/ggit-diff-options.c |  464 ++++++++++++++++++++++++++++++++------
 libgit2-glib/ggit-diff-options.h |   61 ++++-
 2 files changed, 445 insertions(+), 80 deletions(-)
---
diff --git a/libgit2-glib/ggit-diff-options.c b/libgit2-glib/ggit-diff-options.c
index a3b1ac0..52bbc65 100644
--- a/libgit2-glib/ggit-diff-options.c
+++ b/libgit2-glib/ggit-diff-options.c
@@ -22,14 +22,216 @@
 
 #include "ggit-diff-options.h"
 #include "ggit-utils.h"
+#include "ggit-enum-types.h"
 
-struct _GgitDiffOptions
+#define GGIT_DIFF_OPTIONS_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GGIT_TYPE_DIFF_OPTIONS, 
GgitDiffOptionsPrivate))
+
+struct _GgitDiffOptionsPrivate
 {
        git_diff_options diff_options;
+
+       gchar *old_prefix;
+       gchar *new_prefix;
+
+       gchar **pathspec;
+};
+
+G_DEFINE_TYPE (GgitDiffOptions, ggit_diff_options, G_TYPE_OBJECT)
+
+enum
+{
+       PROP_0,
+       PROP_FLAGS,
+       PROP_N_CONTEXT_LINES,
+       PROP_N_INTERHUNK_LINES,
+       PROP_OLD_PREFIX,
+       PROP_NEW_PREFIX,
+       PROP_PATHSPEC
 };
 
-G_DEFINE_BOXED_TYPE (GgitDiffOptions, ggit_diff_options,
-                     ggit_diff_options_copy, ggit_diff_options_free)
+static void
+ggit_diff_options_finalize (GObject *object)
+{
+       GgitDiffOptions *options;
+
+       options = GGIT_DIFF_OPTIONS (object);
+
+       g_free (options->priv->old_prefix);
+       g_free (options->priv->new_prefix);
+       g_strfreev (options->priv->pathspec);
+
+       G_OBJECT_CLASS (ggit_diff_options_parent_class)->finalize (object);
+}
+
+static void
+ggit_diff_options_set_property (GObject      *object,
+                                guint         prop_id,
+                                const GValue *value,
+                                GParamSpec   *pspec)
+{
+       GgitDiffOptions *self = GGIT_DIFF_OPTIONS (object);
+
+       switch (prop_id)
+       {
+       case PROP_FLAGS:
+               self->priv->diff_options.flags = g_value_get_flags (value);
+               break;
+       case PROP_N_CONTEXT_LINES:
+               self->priv->diff_options.context_lines = g_value_get_int (value);
+               break;
+       case PROP_N_INTERHUNK_LINES:
+               self->priv->diff_options.interhunk_lines = g_value_get_int (value);
+               break;
+       case PROP_OLD_PREFIX:
+               ggit_diff_options_set_old_prefix (self,
+                                                 g_value_get_string (value));
+               break;
+       case PROP_NEW_PREFIX:
+               ggit_diff_options_set_new_prefix (self,
+                                                 g_value_get_string (value));
+               break;
+       case PROP_PATHSPEC:
+               ggit_diff_options_set_pathspec (self,
+                                               g_value_get_boxed (value));
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+ggit_diff_options_get_property (GObject    *object,
+                                guint       prop_id,
+                                GValue     *value,
+                                GParamSpec *pspec)
+{
+       GgitDiffOptions *self = GGIT_DIFF_OPTIONS (object);
+
+       switch (prop_id)
+       {
+       case PROP_FLAGS:
+               g_value_set_flags (value, (GgitDiffOption)self->priv->diff_options.flags);
+               break;
+       case PROP_N_CONTEXT_LINES:
+               g_value_set_int (value, self->priv->diff_options.context_lines);
+               break;
+       case PROP_N_INTERHUNK_LINES:
+               g_value_set_int (value, self->priv->diff_options.interhunk_lines);
+               break;
+       case PROP_OLD_PREFIX:
+               g_value_set_string (value, self->priv->old_prefix);
+               break;
+       case PROP_NEW_PREFIX:
+               g_value_set_string (value, self->priv->new_prefix);
+               break;
+       case PROP_PATHSPEC:
+               g_value_set_boxed (value, self->priv->pathspec);
+               break;
+       default:
+               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+               break;
+       }
+}
+
+static void
+ggit_diff_options_class_init (GgitDiffOptionsClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       git_diff_options defopts = GIT_DIFF_OPTIONS_INIT;
+
+       object_class->finalize = ggit_diff_options_finalize;
+
+       object_class->get_property = ggit_diff_options_get_property;
+       object_class->set_property = ggit_diff_options_set_property;
+
+       g_type_class_add_private (object_class, sizeof (GgitDiffOptionsPrivate));
+
+       g_object_class_install_property (object_class,
+                                        PROP_FLAGS,
+                                        g_param_spec_flags ("flags",
+                                                            "Flags",
+                                                            "Flags",
+                                                            GGIT_TYPE_DIFF_OPTION,
+                                                            defopts.flags,
+                                                            G_PARAM_READWRITE |
+                                                            G_PARAM_CONSTRUCT |
+                                                            G_PARAM_STATIC_STRINGS));
+
+       g_object_class_install_property (object_class,
+                                        PROP_N_CONTEXT_LINES,
+                                        g_param_spec_int ("n-context-lines",
+                                                          "N Context Lines",
+                                                          "N context lines",
+                                                          G_MININT,
+                                                          G_MAXINT,
+                                                          defopts.context_lines,
+                                                          G_PARAM_READWRITE |
+                                                          G_PARAM_CONSTRUCT |
+                                                          G_PARAM_STATIC_STRINGS));
+
+       g_object_class_install_property (object_class,
+                                        PROP_N_INTERHUNK_LINES,
+                                        g_param_spec_int ("n-interhunk-lines",
+                                                          "N Interhunk Lines",
+                                                          "N interhunk lines",
+                                                          G_MININT,
+                                                          G_MAXINT,
+                                                          defopts.interhunk_lines,
+                                                          G_PARAM_READWRITE |
+                                                          G_PARAM_CONSTRUCT |
+                                                          G_PARAM_STATIC_STRINGS));
+
+       g_object_class_install_property (object_class,
+                                        PROP_OLD_PREFIX,
+                                        g_param_spec_string ("old-prefix",
+                                                             "Old Prefix",
+                                                             "Old prefix",
+                                                             defopts.old_prefix,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT |
+                                                             G_PARAM_STATIC_STRINGS));
+
+       g_object_class_install_property (object_class,
+                                        PROP_NEW_PREFIX,
+                                        g_param_spec_string ("new-prefix",
+                                                             "New Prefix",
+                                                             "New prefix",
+                                                             NULL,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT |
+                                                             G_PARAM_STATIC_STRINGS));
+
+       g_object_class_install_property (object_class,
+                                        PROP_PATHSPEC,
+                                        g_param_spec_boxed ("pathspec",
+                                                            "Pathspec",
+                                                            "Pathspec",
+                                                            G_TYPE_STRV,
+                                                            G_PARAM_READWRITE |
+                                                            G_PARAM_CONSTRUCT |
+                                                            G_PARAM_STATIC_STRINGS));
+}
+
+static void
+ggit_diff_options_init (GgitDiffOptions *self)
+{
+       self->priv = GGIT_DIFF_OPTIONS_GET_PRIVATE (self);
+}
+
+/**
+ * ggit_diff_options_new:
+ *
+ * Create a new diff options object.
+ *
+ * Returns: a #GgitDiffOptions.
+ *
+ **/
+GgitDiffOptions *
+ggit_diff_options_new (void)
+{
+       return g_object_new (GGIT_TYPE_DIFF_OPTIONS, NULL);
+}
 
 const git_diff_options *
 _ggit_diff_options_get_diff_options (GgitDiffOptions *diff_options)
@@ -42,100 +244,228 @@ _ggit_diff_options_get_diff_options (GgitDiffOptions *diff_options)
                return NULL;
        }
 
-       return (const git_diff_options *)&diff_options->diff_options;
+       return (const git_diff_options *)&diff_options->priv->diff_options;
 }
 
 /**
- * ggit_diff_options_copy:
- * @diff_options: a #GgitDiffOptions.
+ * ggit_diff_options_get_flags:
+ * @options: a #GgitDiffOptions.
  *
- * Copies @diff_options into a newly allocated #GgitDiffOptions.
+ * Get the diff flags.
  *
- * Returns: (transfer full): a newly allocated #GgitDiffOptions.
- */
-GgitDiffOptions *
-ggit_diff_options_copy (GgitDiffOptions *diff_options)
+ * Returns: a #GgitDiffOption.
+ *
+ **/
+GgitDiffOption
+ggit_diff_options_get_flags (GgitDiffOptions *options)
+{
+       g_return_val_if_fail (GGIT_IS_DIFF_OPTIONS (options), 0);
+
+       return (GgitDiffOption)options->priv->diff_options.flags;
+}
+
+/**
+ * ggit_diff_options_set_flags:
+ * @options: a #GgitDiffOptions.
+ * @flags: a #GgitDiffOption.
+ *
+ * Set the diff flags.
+ *
+ **/
+void
+ggit_diff_options_set_flags (GgitDiffOptions *options,
+                             GgitDiffOption   flags)
+{
+       g_return_if_fail (GGIT_IS_DIFF_OPTIONS (options));
+
+       options->priv->diff_options.flags = flags;
+       g_object_notify (G_OBJECT (options), "flags");
+}
+
+/**
+ * ggit_diff_options_get_n_context_lines:
+ * @options: a #GgitDiffOptions.
+ *
+ * Get the number of context lines to include in the diff.
+ *
+ * Returns: the number of context lines.
+ *
+ **/
+gint
+ggit_diff_options_get_n_context_lines (GgitDiffOptions *options)
 {
-       GgitDiffOptions *new_diff_options;
-       git_diff_options *gdiff_options;
-       git_diff_options *gnew_diff_options;
+       g_return_val_if_fail (GGIT_IS_DIFF_OPTIONS (options), 0);
 
-       g_return_val_if_fail (diff_options != NULL, NULL);
+       return (GgitDiffOption)options->priv->diff_options.context_lines;
+}
 
-       new_diff_options = g_slice_new (GgitDiffOptions);
+/**
+ * ggit_diff_options_set_n_context_lines:
+ * @options: a #GgitDiffOptions.
+ * @n: the number of lines.
+ *
+ * Set the number of context lines to include in the diff.
+ *
+ **/
+void
+ggit_diff_options_set_n_context_lines (GgitDiffOptions *options,
+                                       gint             n)
+{
+       g_return_if_fail (GGIT_IS_DIFF_OPTIONS (options));
 
-       gdiff_options = &diff_options->diff_options;
-       gnew_diff_options = &new_diff_options->diff_options;
+       options->priv->diff_options.context_lines = n;
+       g_object_notify (G_OBJECT (options), "n-context-lines");
+}
 
-       gnew_diff_options->flags = gdiff_options->flags;
-       gnew_diff_options->context_lines = gdiff_options->context_lines;
-       gnew_diff_options->interhunk_lines = gdiff_options->interhunk_lines;
-       gnew_diff_options->old_prefix = g_strdup (gdiff_options->old_prefix);
-       gnew_diff_options->new_prefix = g_strdup (gdiff_options->new_prefix);
-       git_strarray_copy (&gnew_diff_options->pathspec,
-                          &gdiff_options->pathspec);
+/**
+ * ggit_diff_options_get_n_interhunk_lines:
+ * @options: a #GgitDiffOptions.
+ *
+ * Get the number of interhunk lines to include in the diff.
+ *
+ * Returns: the number of lines.
+ *
+ **/
+gint
+ggit_diff_options_get_n_interhunk_lines (GgitDiffOptions *options)
+{
+       g_return_val_if_fail (GGIT_IS_DIFF_OPTIONS (options), 0);
 
-       return new_diff_options;
+       return (GgitDiffOption)options->priv->diff_options.interhunk_lines;
 }
 
 /**
- * ggit_diff_options_free:
- * @diff_options: a #GgitDiffOptions.
+ * ggit_diff_options_set_n_interhunk_lines:
+ * @options: a #GgitDiffOptions.
+ * @n: the number of lines.
  *
- * Frees @diff_options.
- */
+ * Set the number of interhunk lines to include in the diff.
+ *
+ **/
 void
-ggit_diff_options_free (GgitDiffOptions *diff_options)
+ggit_diff_options_set_n_interhunk_lines (GgitDiffOptions *options,
+                                         gint             n)
 {
-       git_diff_options *gdiff_options;
+       g_return_if_fail (GGIT_IS_DIFF_OPTIONS (options));
 
-       g_return_if_fail (diff_options != NULL);
+       options->priv->diff_options.interhunk_lines = n;
+       g_object_notify (G_OBJECT (options), "n-interhunk-lines");
+}
 
-       gdiff_options = &diff_options->diff_options;
-       g_free ((gchar *)gdiff_options->old_prefix);
-       g_free ((gchar *)gdiff_options->new_prefix);
-       git_strarray_free (&gdiff_options->pathspec);
+/**
+ * ggit_diff_options_get_old_prefix:
+ * @options: a #GgitDiffOptions.
+ *
+ * Get the diff old-prefix string.
+ *
+ * Returns: the old-prefix string.
+ *
+ **/
+const gchar *
+ggit_diff_options_get_old_prefix (GgitDiffOptions *options)
+{
+       g_return_val_if_fail (GGIT_IS_DIFF_OPTIONS (options), NULL);
 
-       g_slice_free (GgitDiffOptions, diff_options);
+       return options->priv->old_prefix;
 }
 
 /**
- * ggit_diff_options_new:
- * @flags: how the diff should be performed, default is #GGIT_DIFF_NORMAL.
- * @n_context_lines: the number of context lines, default it 3.
- * @n_interhunk_lines: the number of interhunk lines, default is 3.
- * @old_prefix: (allow-none): the old prefix, defaults to "a".
- * @new_prefix: (allow-none): the new prefix, defaults to "b".
- * @pathspec: (allow-none) (array zero-terminated=1): which paths to show, defaults to showing all paths.
+ * ggit_diff_options_set_old_prefix:
+ * @options: a #GgitDiffOptions.
+ * @prefix: the prefix.
  *
- * Creates a new #GgitDiffOptions for use in creating a #GgitDiff.
+ * Get the diff old-prefix string.
  *
- * Returns: a newly allocated #GgitDiffOptions.
- */
-GgitDiffOptions *
-ggit_diff_options_new (GgitDiffOption  flags,
-                       gint            n_context_lines,
-                       gint            n_interhunk_lines,
-                       const gchar    *old_prefix,
-                       const gchar    *new_prefix,
-                       const gchar   **pathspec)
+ **/
+void
+ggit_diff_options_set_old_prefix (GgitDiffOptions *options,
+                                  const gchar     *prefix)
+{
+       g_return_if_fail (GGIT_IS_DIFF_OPTIONS (options));
+
+       g_free (options->priv->old_prefix);
+       options->priv->old_prefix = g_strdup (prefix);
+
+       options->priv->diff_options.old_prefix = options->priv->old_prefix;
+       g_object_notify (G_OBJECT (options), "old-prefix");
+}
+
+/**
+ * ggit_diff_options_get_new_prefix:
+ * @options: a #GgitDiffOptions.
+ *
+ * Get the diff new-prefix string.
+ *
+ * Returns: the new-prefix string.
+ *
+ **/
+const gchar *
+ggit_diff_options_get_new_prefix (GgitDiffOptions *options)
+{
+       g_return_val_if_fail (GGIT_IS_DIFF_OPTIONS (options), NULL);
+
+       return options->priv->new_prefix;
+}
+
+/**
+ * ggit_diff_options_set_new_prefix:
+ * @options: a #GgitDiffOptions.
+ * @prefix: the prefix.
+ *
+ * Set the diff new-prefix string.
+ *
+ **/
+void
+ggit_diff_options_set_new_prefix (GgitDiffOptions *options,
+                                  const gchar     *prefix)
 {
-       GgitDiffOptions *diff_options;
-       git_diff_options gdiff_options = GIT_DIFF_OPTIONS_INIT;
+       g_return_if_fail (GGIT_IS_DIFF_OPTIONS (options));
+
+       g_free (options->priv->new_prefix);
+       options->priv->new_prefix = g_strdup (prefix);
 
-       diff_options = g_slice_new (GgitDiffOptions);
+       options->priv->diff_options.new_prefix = options->priv->new_prefix;
+       g_object_notify (G_OBJECT (options), "new-prefix");
+}
+
+/**
+ * ggit_diff_options_get_pathspec:
+ * @options: a #GgitDiffOptions.
+ *
+ * Get the pathspec.
+ *
+ * Returns: (transfer none) (array zero-terminated=1): the pathspec.
+ *
+ **/
+const gchar **
+ggit_diff_options_get_pathspec (GgitDiffOptions *options)
+{
+       g_return_val_if_fail (GGIT_IS_DIFF_OPTIONS (options), NULL);
+
+       return (const gchar **)options->priv->pathspec;
+}
+
+/**
+ * ggit_diff_options_set_pathspec:
+ * @options: a #GgitDiffOptions.
+ * @pathspec: (array zero-terminated=1): the pathspec.
+ *
+ * Set the pathspec.
+ *
+ **/
+void
+ggit_diff_options_set_pathspec (GgitDiffOptions  *options,
+                                const gchar     **pathspec)
+{
+       g_return_if_fail (GGIT_IS_DIFF_OPTIONS (options));
 
-       gdiff_options.flags = flags;
-       gdiff_options.context_lines = n_context_lines;
-       gdiff_options.interhunk_lines = n_interhunk_lines;
-       gdiff_options.old_prefix = old_prefix == NULL ? NULL : g_strdup (old_prefix);
-       gdiff_options.new_prefix = new_prefix == NULL ? NULL : g_strdup (new_prefix);
+       g_strfreev (options->priv->pathspec);
+       options->priv->pathspec = g_strdupv ((gchar **)pathspec);
 
-       ggit_utils_get_git_strarray_from_str_array (pathspec,
-                                                   &gdiff_options.pathspec);
+       options->priv->diff_options.pathspec.strings = options->priv->pathspec;
+       options->priv->diff_options.pathspec.count = g_strv_length (options->priv->pathspec);
 
-       diff_options->diff_options = gdiff_options;
-       return diff_options;
+       g_object_notify (G_OBJECT (options), "pathspec");
 }
 
 /* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-diff-options.h b/libgit2-glib/ggit-diff-options.h
index a64b3a3..42053be 100644
--- a/libgit2-glib/ggit-diff-options.h
+++ b/libgit2-glib/ggit-diff-options.h
@@ -18,7 +18,6 @@
  * along with libgit2-glib. If not, see <http://www.gnu.org/licenses/>.
  */
 
-
 #ifndef __GGIT_DIFF_OPTIONS_H__
 #define __GGIT_DIFF_OPTIONS_H__
 
@@ -29,22 +28,58 @@
 
 G_BEGIN_DECLS
 
-#define GGIT_TYPE_DIFF_OPTIONS       (ggit_diff_options_get_type ())
-#define GGIT_DIFF_OPTIONS(obj)       ((GgitDiffOptions *)obj)
+#define GGIT_TYPE_DIFF_OPTIONS                 (ggit_diff_options_get_type ())
+#define GGIT_DIFF_OPTIONS(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GGIT_TYPE_DIFF_OPTIONS, 
GgitDiffOptions))
+#define GGIT_DIFF_OPTIONS_CONST(obj)           (G_TYPE_CHECK_INSTANCE_CAST ((obj), GGIT_TYPE_DIFF_OPTIONS, 
GgitDiffOptions const))
+#define GGIT_DIFF_OPTIONS_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GGIT_TYPE_DIFF_OPTIONS, 
GgitDiffOptionsClass))
+#define GGIT_IS_DIFF_OPTIONS(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GGIT_TYPE_DIFF_OPTIONS))
+#define GGIT_IS_DIFF_OPTIONS_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), GGIT_TYPE_DIFF_OPTIONS))
+#define GGIT_DIFF_OPTIONS_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), GGIT_TYPE_DIFF_OPTIONS, 
GgitDiffOptionsClass))
+
+typedef struct _GgitDiffOptionsClass   GgitDiffOptionsClass;
+typedef struct _GgitDiffOptionsPrivate GgitDiffOptionsPrivate;
+
+struct _GgitDiffOptions
+{
+       GObject parent;
+
+       GgitDiffOptionsPrivate *priv;
+};
+
+struct _GgitDiffOptionsClass
+{
+       GObjectClass parent_class;
+};
+
+const git_diff_options *
+                 _ggit_diff_options_get_diff_options     (GgitDiffOptions  *options);
+
+GType            ggit_diff_options_get_type              (void) G_GNUC_CONST;
+GgitDiffOptions *ggit_diff_options_new                   (void);
+
+GgitDiffOption   ggit_diff_options_get_flags             (GgitDiffOptions  *options);
+void             ggit_diff_options_set_flags             (GgitDiffOptions  *options,
+                                                          GgitDiffOption    flags);
+
+gint             ggit_diff_options_get_n_context_lines   (GgitDiffOptions  *options);
+void             ggit_diff_options_set_n_context_lines   (GgitDiffOptions  *options,
+                                                          gint              n);
 
-GType                   ggit_diff_options_get_type          (void) G_GNUC_CONST;
+gint             ggit_diff_options_get_n_interhunk_lines (GgitDiffOptions  *options);
+void             ggit_diff_options_set_n_interhunk_lines (GgitDiffOptions  *options,
+                                                          gint              n);
 
-const git_diff_options *_ggit_diff_options_get_diff_options (GgitDiffOptions        *diff_options);
+const gchar     *ggit_diff_options_get_old_prefix        (GgitDiffOptions  *options);
+void             ggit_diff_options_set_old_prefix        (GgitDiffOptions  *options,
+                                                          const gchar      *prefix);
 
-GgitDiffOptions        *ggit_diff_options_copy              (GgitDiffOptions        *diff_options);
-void                    ggit_diff_options_free              (GgitDiffOptions        *diff_options);
+const gchar     *ggit_diff_options_get_new_prefix        (GgitDiffOptions  *options);
+void             ggit_diff_options_set_new_prefix        (GgitDiffOptions  *options,
+                                                          const gchar      *prefix);
 
-GgitDiffOptions        *ggit_diff_options_new               (GgitDiffOption          flags,
-                                                             gint                    n_context_lines,
-                                                             gint                    n_interhunk_lines,
-                                                             const gchar            *old_prefix,
-                                                             const gchar            *new_prefix,
-                                                             const gchar           **pathspec);
+const gchar    **ggit_diff_options_get_pathspec          (GgitDiffOptions  *options);
+void             ggit_diff_options_set_pathspec          (GgitDiffOptions  *options,
+                                                          const gchar     **pathspec);
 
 G_END_DECLS
 


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