[gnome-builder] git: add IdeGitRemoteCallbacks



commit a50a5b12ab51574b9987de4ceddb1ad6ab42895b
Author: Christian Hergert <christian hergert me>
Date:   Wed Apr 8 00:01:04 2015 -0700

    git: add IdeGitRemoteCallbacks
    
    This class provides the hooks necessary to load various keys and track
    transfer progress (such as the percentage of completion in transfer).

 libide/Makefile.am                    |    2 +
 libide/git/ide-git-remote-callbacks.c |  230 +++++++++++++++++++++++++++++++++
 libide/git/ide-git-remote-callbacks.h |   48 +++++++
 libide/ide.h                          |    1 +
 4 files changed, 281 insertions(+), 0 deletions(-)
---
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 3fc64aa..4acb49d 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -36,6 +36,8 @@ libide_1_0_la_public_sources = \
        libide/directory/ide-directory-vcs.h \
        libide/editorconfig/ide-editorconfig-file-settings.c \
        libide/editorconfig/ide-editorconfig-file-settings.h \
+       libide/git/ide-git-remote-callbacks.c \
+       libide/git/ide-git-remote-callbacks.h \
        libide/git/ide-git-search-provider.c \
        libide/git/ide-git-search-provider.h \
        libide/git/ide-git-search-result.c \
diff --git a/libide/git/ide-git-remote-callbacks.c b/libide/git/ide-git-remote-callbacks.c
new file mode 100644
index 0000000..7e34c9c
--- /dev/null
+++ b/libide/git/ide-git-remote-callbacks.c
@@ -0,0 +1,230 @@
+/* ide-git-remote-callbacks.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n.h>
+
+#include "ide-animation.h"
+#include "ide-debug.h"
+#include "ide-git-remote-callbacks.h"
+#include "ide-macros.h"
+#include "ide-progress.h"
+
+#define ANIMATION_DURATION_MSEC 250
+
+struct _IdeGitRemoteCallbacks
+{
+  GgitRemoteCallbacks  parent_instance;
+
+  IdeAnimation        *animation;
+  IdeProgress         *progress;
+  gdouble              fraction;
+};
+
+struct _IdeGitRemoteCallbacksClass
+{
+  GgitRemoteCallbacksClass parent_class;
+};
+
+G_DEFINE_TYPE (IdeGitRemoteCallbacks, ide_git_remote_callbacks, GGIT_TYPE_REMOTE_CALLBACKS)
+
+enum {
+  PROP_0,
+  PROP_FRACTION,
+  LAST_PROP
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+
+GgitRemoteCallbacks *
+ide_git_remote_callbacks_new (void)
+{
+  return g_object_new (IDE_TYPE_GIT_REMOTE_CALLBACKS, NULL);
+}
+
+/**
+ * ide_git_remote_callbacks_get_progress:
+ *
+ * Gets the #IdeProgress for the operation.
+ *
+ * Returns: (transfer none): An #IdeProgress.
+ */
+IdeProgress *
+ide_git_remote_callbacks_get_progress (IdeGitRemoteCallbacks *self)
+{
+  g_return_val_if_fail (IDE_IS_GIT_REMOTE_CALLBACKS (self), NULL);
+
+  return self->progress;
+}
+
+/**
+ * ide_git_remote_callbacks_get_fraction:
+ *
+ * Gets the fraction of the current operation. This should typically be bound using
+ * g_object_bind_property() to GtkProgressBar:fraction or similar progress widget.
+ *
+ * Returns: The operation completion percentage, as a fraction between 0 and 1.
+ */
+gdouble
+ide_git_remote_callbacks_get_fraction (IdeGitRemoteCallbacks *self)
+{
+  g_return_val_if_fail (IDE_IS_GIT_REMOTE_CALLBACKS (self), 0.0);
+
+  return self->fraction;
+}
+
+static gboolean
+ide_git_remote_callbacks__notify_fraction_cb (gpointer data)
+{
+  g_autoptr(IdeGitRemoteCallbacks) self = data;
+  IdeAnimation *animation;
+
+  g_assert (IDE_IS_GIT_REMOTE_CALLBACKS (self));
+
+  if ((animation = self->animation))
+    {
+      ide_clear_weak_pointer (&self->animation);
+      ide_animation_stop (animation);
+    }
+
+  animation = ide_object_animate (self->progress,
+                                  IDE_ANIMATION_EASE_IN_OUT_QUAD,
+                                  ANIMATION_DURATION_MSEC,
+                                  NULL,
+                                  "fraction", self->fraction,
+                                  NULL);
+  ide_set_weak_pointer (&self->animation, animation);
+
+  g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_FRACTION]);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+ide_git_remote_callbacks_real_transfer_progress (GgitRemoteCallbacks  *callbacks,
+                                                 GgitTransferProgress *stats)
+{
+  IdeGitRemoteCallbacks *self = (IdeGitRemoteCallbacks *)callbacks;
+  guint total;
+  guint received;
+
+  g_assert (IDE_IS_GIT_REMOTE_CALLBACKS (self));
+  g_assert (stats != NULL);
+
+  total = ggit_transfer_progress_get_total_objects (stats);
+  received = ggit_transfer_progress_get_received_objects (stats);
+  if (total == 0)
+    return;
+
+  self->fraction = (gdouble)received / (gdouble)total;
+
+  /* Emit notify::fraction from the gtk+ main loop */
+  g_timeout_add (0, ide_git_remote_callbacks__notify_fraction_cb, g_object_ref (self));
+}
+
+static GgitCred *
+ide_git_remote_callbacks_real_credentials (GgitRemoteCallbacks  *callbacks,
+                                           const gchar          *url,
+                                           const gchar          *username_from_url,
+                                           GgitCredtype          allowed_types,
+                                           GError              **error)
+{
+  IdeGitRemoteCallbacks *self = (IdeGitRemoteCallbacks *)callbacks;
+  GgitCred *ret = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_GIT_REMOTE_CALLBACKS (self));
+
+  IDE_TRACE_MSG ("username=%s url=%s", username_from_url ?: "", url);
+
+  if ((allowed_types & GGIT_CREDTYPE_SSH_KEY) != 0)
+    {
+      GgitCredSshKeyFromAgent *cred;
+
+      cred = ggit_cred_ssh_key_from_agent_new (username_from_url, error);
+      ret = GGIT_CRED (cred);
+    }
+
+  if ((allowed_types & GGIT_CREDTYPE_SSH_INTERACTIVE) != 0)
+    {
+      GgitCredSshInteractive *cred;
+
+      cred = ggit_cred_ssh_interactive_new (username_from_url, error);
+      ret = GGIT_CRED (cred);
+    }
+
+  IDE_RETURN (ret);
+}
+
+static void
+ide_git_remote_callbacks_finalize (GObject *object)
+{
+  IdeGitRemoteCallbacks *self = (IdeGitRemoteCallbacks *)object;
+
+  g_clear_object (&self->progress);
+
+  G_OBJECT_CLASS (ide_git_remote_callbacks_parent_class)->finalize (object);
+}
+
+static void
+ide_git_remote_callbacks_get_property (GObject    *object,
+                                       guint       prop_id,
+                                       GValue     *value,
+                                       GParamSpec *pspec)
+{
+  IdeGitRemoteCallbacks *self = IDE_GIT_REMOTE_CALLBACKS (object);
+
+  switch (prop_id)
+    {
+    case PROP_FRACTION:
+      g_value_set_double (value, ide_git_remote_callbacks_get_fraction (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_git_remote_callbacks_class_init (IdeGitRemoteCallbacksClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GgitRemoteCallbacksClass *callbacks_class = GGIT_REMOTE_CALLBACKS_CLASS (klass);
+
+  object_class->finalize = ide_git_remote_callbacks_finalize;
+  object_class->get_property = ide_git_remote_callbacks_get_property;
+
+  callbacks_class->transfer_progress = ide_git_remote_callbacks_real_transfer_progress;
+  callbacks_class->credentials = ide_git_remote_callbacks_real_credentials;
+
+  gParamSpecs [PROP_FRACTION] =
+    g_param_spec_double ("fraction",
+                         _("Fraction"),
+                         _("A fraction containing the operatoin progress."),
+                         0,
+                         1.0,
+                         0.0,
+                         (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_FRACTION, gParamSpecs [PROP_FRACTION]);
+}
+
+static void
+ide_git_remote_callbacks_init (IdeGitRemoteCallbacks *self)
+{
+  self->progress = g_object_new (IDE_TYPE_PROGRESS, NULL);
+}
diff --git a/libide/git/ide-git-remote-callbacks.h b/libide/git/ide-git-remote-callbacks.h
new file mode 100644
index 0000000..702b51a
--- /dev/null
+++ b/libide/git/ide-git-remote-callbacks.h
@@ -0,0 +1,48 @@
+/* ide-git-remote-callbacks.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_GIT_REMOTE_CALLBACKS_H
+#define IDE_GIT_REMOTE_CALLBACKS_H
+
+#include <libgit2-glib/ggit.h>
+
+#include "ide-progress.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_GIT_REMOTE_CALLBACKS            (ide_git_remote_callbacks_get_type())
+#define IDE_GIT_REMOTE_CALLBACKS(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
IDE_TYPE_GIT_REMOTE_CALLBACKS, IdeGitRemoteCallbacks))
+#define IDE_GIT_REMOTE_CALLBACKS_CONST(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
IDE_TYPE_GIT_REMOTE_CALLBACKS, IdeGitRemoteCallbacks const))
+#define IDE_GIT_REMOTE_CALLBACKS_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  
IDE_TYPE_GIT_REMOTE_CALLBACKS, IdeGitRemoteCallbacksClass))
+#define IDE_IS_GIT_REMOTE_CALLBACKS(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
IDE_TYPE_GIT_REMOTE_CALLBACKS))
+#define IDE_IS_GIT_REMOTE_CALLBACKS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  
IDE_TYPE_GIT_REMOTE_CALLBACKS))
+#define IDE_GIT_REMOTE_CALLBACKS_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  
IDE_TYPE_GIT_REMOTE_CALLBACKS, IdeGitRemoteCallbacksClass))
+
+typedef struct _IdeGitRemoteCallbacks      IdeGitRemoteCallbacks;
+typedef struct _IdeGitRemoteCallbacksClass IdeGitRemoteCallbacksClass;
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeGitRemoteCallbacks, g_object_unref)
+
+GType                ide_git_remote_callbacks_get_type     (void);
+GgitRemoteCallbacks *ide_git_remote_callbacks_new          (void);
+gdouble              ide_git_remote_callbacks_get_fraction (IdeGitRemoteCallbacks *self);
+IdeProgress         *ide_git_remote_callbacks_get_progress (IdeGitRemoteCallbacks *self);
+
+G_END_DECLS
+
+#endif /* IDE_GIT_REMOTE_CALLBACKS_H */
diff --git a/libide/ide.h b/libide/ide.h
index 9a90b70..e7bb7da 100644
--- a/libide/ide.h
+++ b/libide/ide.h
@@ -95,6 +95,7 @@ G_BEGIN_DECLS
 #include "devhelp/ide-devhelp-search-result.h"
 #include "directory/ide-directory-build-system.h"
 #include "directory/ide-directory-vcs.h"
+#include "git/ide-git-remote-callbacks.h"
 #include "git/ide-git-search-result.h"
 #include "git/ide-git-vcs.h"
 #include "local/ide-local-device.h"


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