[libgit2-glib] Correctly wrap up git_transfer_progress



commit d89beece9c3683efc7d4548da18eab5ad4acad7c
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Sun Jun 30 12:23:30 2013 +0200

    Correctly wrap up git_transfer_progress

 examples/clone.c                      |    4 +-
 libgit2-glib/ggit-clone-options.c     |   26 ++++++++++++++++++++-
 libgit2-glib/ggit-transfer-progress.c |   38 ++++++++++++++++++--------------
 libgit2-glib/ggit-transfer-progress.h |    3 ++
 libgit2-glib/ggit-types.h             |    4 +-
 5 files changed, 52 insertions(+), 23 deletions(-)
---
diff --git a/examples/clone.c b/examples/clone.c
index d03d59b..5bab90b 100644
--- a/examples/clone.c
+++ b/examples/clone.c
@@ -3,8 +3,8 @@
 #include "ggit.h"
 
 static int
-fetch_progress (const GgitTransferProgress *stats,
-                gpointer                    useless)
+fetch_progress (GgitTransferProgress *stats,
+                gpointer              useless)
 {
        gint network_percent = (100 * ggit_transfer_progress_get_received_objects (stats)) / 
ggit_transfer_progress_get_total_objects (stats);
        gint index_percent = (100 * ggit_transfer_progress_get_indexed_objects (stats)) / 
ggit_transfer_progress_get_total_objects (stats);
diff --git a/libgit2-glib/ggit-clone-options.c b/libgit2-glib/ggit-clone-options.c
index ee5037f..8b5c07a 100644
--- a/libgit2-glib/ggit-clone-options.c
+++ b/libgit2-glib/ggit-clone-options.c
@@ -21,6 +21,7 @@
 #include <git2.h>
 
 #include "ggit-clone-options.h"
+#include "ggit-transfer-progress.h"
 #include "ggit-native.h"
 
 struct _GgitCloneOptions
@@ -28,6 +29,8 @@ struct _GgitCloneOptions
        git_clone_options clone_options;
        GgitCredAcquireCallback cred_callback;
        gpointer cred_user_data;
+       GgitTransferProgressCallback fetch_progress_callback;
+       gpointer fetch_progress_user_data;
 };
 
 G_DEFINE_BOXED_TYPE (GgitCloneOptions, ggit_clone_options,
@@ -188,6 +191,22 @@ ggit_clone_options_set_is_bare (GgitCloneOptions *options,
        options->clone_options.bare = bare;
 }
 
+static int
+wrap_fetch_progress (const git_transfer_progress *stats,
+                     gpointer                     data)
+{
+       GgitCloneOptions *options = (GgitCloneOptions *)data;
+       GgitTransferProgress *gstats;
+       gint ret;
+
+       gstats = _ggit_transfer_progress_wrap (stats);
+
+       ret = options->fetch_progress_callback (gstats, options->fetch_progress_user_data);
+       ggit_transfer_progress_free (gstats);
+
+       return ret;
+}
+
 /**
  * ggit_clone_options_set_fetch_progress_callback:
  * @options: a #GgitCloneOptions.
@@ -204,8 +223,11 @@ ggit_clone_options_set_fetch_progress_callback (GgitCloneOptions             *op
 {
        g_return_if_fail (options != NULL);
 
-       options->clone_options.fetch_progress_cb = (git_transfer_progress_callback)callback;
-       options->clone_options.fetch_progress_payload = user_data;
+       options->fetch_progress_callback = callback;
+       options->fetch_progress_user_data = user_data;
+
+       options->clone_options.fetch_progress_cb = wrap_fetch_progress;
+       options->clone_options.fetch_progress_payload = options;
 }
 
 /**
diff --git a/libgit2-glib/ggit-transfer-progress.c b/libgit2-glib/ggit-transfer-progress.c
index 42fe76b..571fe43 100644
--- a/libgit2-glib/ggit-transfer-progress.c
+++ b/libgit2-glib/ggit-transfer-progress.c
@@ -19,19 +19,31 @@
  */
 
 #include "ggit-transfer-progress.h"
+#include <git2.h>
+#include <string.h>
 
 struct _GgitTransferProgress
 {
-       guint total_objects;
-       guint indexed_objects;
-       guint received_objects;
-       gsize received_bytes;
+       git_transfer_progress progress;
 };
 
 G_DEFINE_BOXED_TYPE (GgitTransferProgress, ggit_transfer_progress,
                      ggit_transfer_progress_copy,
                      ggit_transfer_progress_free)
 
+GgitTransferProgress *
+_ggit_transfer_progress_wrap (const git_transfer_progress *progress)
+{
+       GgitTransferProgress *gprogress;
+
+       g_return_val_if_fail (progress != NULL, NULL);
+
+       gprogress = g_slice_new (GgitTransferProgress);
+       memcpy (&gprogress->progress, progress, sizeof (git_transfer_progress));
+
+       return gprogress;
+}
+
 /**
  * ggit_transfer_progress_copy:
  * @progress: a #GgitTransferProgress.
@@ -43,17 +55,9 @@ G_DEFINE_BOXED_TYPE (GgitTransferProgress, ggit_transfer_progress,
 GgitTransferProgress *
 ggit_transfer_progress_copy (GgitTransferProgress *progress)
 {
-       GgitTransferProgress *copy;
-
        g_return_val_if_fail (progress != NULL, NULL);
 
-       copy = g_slice_new (GgitTransferProgress);
-       copy->total_objects = progress->total_objects;
-       copy->indexed_objects = progress->indexed_objects;
-       copy->received_objects = progress->received_objects;
-       copy->received_bytes = progress->received_bytes;
-
-       return copy;
+       return _ggit_transfer_progress_wrap (&progress->progress);
 }
 
 /**
@@ -83,7 +87,7 @@ ggit_transfer_progress_get_total_objects (GgitTransferProgress *progress)
 {
        g_return_val_if_fail (progress != NULL, 0);
 
-       return progress->total_objects;
+       return progress->progress.total_objects;
 }
 
 /**
@@ -99,7 +103,7 @@ ggit_transfer_progress_get_indexed_objects (GgitTransferProgress *progress)
 {
        g_return_val_if_fail (progress != NULL, 0);
 
-       return progress->indexed_objects;
+       return progress->progress.indexed_objects;
 }
 
 /**
@@ -115,7 +119,7 @@ ggit_transfer_progress_get_received_objects (GgitTransferProgress *progress)
 {
        g_return_val_if_fail (progress != NULL, 0);
 
-       return progress->received_objects;
+       return progress->progress.received_objects;
 }
 
 /**
@@ -131,7 +135,7 @@ ggit_transfer_progress_get_received_bytes (GgitTransferProgress *progress)
 {
        g_return_val_if_fail (progress != NULL, 0);
 
-       return progress->received_bytes;
+       return progress->progress.received_bytes;
 }
 
 /* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-transfer-progress.h b/libgit2-glib/ggit-transfer-progress.h
index 6b62e01..639f3ce 100644
--- a/libgit2-glib/ggit-transfer-progress.h
+++ b/libgit2-glib/ggit-transfer-progress.h
@@ -22,6 +22,7 @@
 #define __GGIT_TRANSFER_PROGRESS_H__
 
 #include <glib-object.h>
+#include <git2.h>
 
 #include "ggit-types.h"
 
@@ -32,6 +33,8 @@ G_BEGIN_DECLS
 
 GType                   ggit_transfer_progress_get_type             (void) G_GNUC_CONST;
 
+GgitTransferProgress  *_ggit_transfer_progress_wrap                 (const git_transfer_progress *progress);
+
 GgitTransferProgress   *ggit_transfer_progress_copy                 (GgitTransferProgress     *progress);
 void                    ggit_transfer_progress_free                 (GgitTransferProgress     *progress);
 
diff --git a/libgit2-glib/ggit-types.h b/libgit2-glib/ggit-types.h
index 9fc98a3..9a68c21 100644
--- a/libgit2-glib/ggit-types.h
+++ b/libgit2-glib/ggit-types.h
@@ -953,8 +953,8 @@ typedef gint (* GgitSubmoduleCallback) (GgitSubmodule *submodule,
  *
  * Returns: a value less than zero to cancel the transfer.
  */
-typedef gint (* GgitTransferProgressCallback) (const GgitTransferProgress *stats,
-                                               gpointer                    user_data);
+typedef gint (* GgitTransferProgressCallback) (GgitTransferProgress *stats,
+                                               gpointer              user_data);
 
 /**
  * GgitTreeWalkCallback:


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