[gnome-builder/wip/chergert/transfers] transfers: stub out transfers API



commit 68e13f5d6e7205721edce845bc3d9252885988ae
Author: Christian Hergert <chergert redhat com>
Date:   Wed Aug 24 23:12:48 2016 -0700

    transfers: stub out transfers API

 data/theme/Adwaita-shared.css                |   14 +
 libide/Makefile.am                           |    6 +
 libide/ide-context.c                         |   22 ++
 libide/ide-context.h                         |    1 +
 libide/ide-types.h                           |    2 +
 libide/ide.h                                 |    2 +
 libide/resources/libide.gresource.xml        |    1 +
 libide/transfers/ide-transfer-manager.c      |  455 ++++++++++++++++++++++++++
 libide/transfers/ide-transfer-manager.h      |   46 +++
 libide/transfers/ide-transfer.c              |  109 ++++++
 libide/transfers/ide-transfer.h              |   53 +++
 libide/transfers/ide-transfers-button.c      |  234 +++++++++++++
 libide/transfers/ide-transfers-button.h      |   35 ++
 libide/transfers/ide-transfers-button.ui     |   57 ++++
 libide/workbench/ide-workbench-header-bar.ui |   32 ++-
 15 files changed, 1060 insertions(+), 9 deletions(-)
---
diff --git a/data/theme/Adwaita-shared.css b/data/theme/Adwaita-shared.css
index 37289a3..a55a7f6 100644
--- a/data/theme/Adwaita-shared.css
+++ b/data/theme/Adwaita-shared.css
@@ -210,3 +210,17 @@ popover.messagepopover .popover-action-area button:last-child {
 popover.messagepopover .popover-content-area {
   margin: 24px;
 }
+
+
+popover.transfers list {
+  background-color: transparent;
+}
+popover.transfers list row {
+  border-top: 1px solid @borders;
+}
+popover.transfers list row:first-child {
+  border-top: none;
+}
+popover.transfers list row > box {
+  padding: 10px;
+}
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 94e709c..5c7367c 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -131,6 +131,12 @@ libide_1_0_la_public_headers =                            \
        template/ide-template-base.h                      \
        template/ide-template-provider.h                  \
        threading/ide-thread-pool.h                       \
+       transfers/ide-transfer.c                          \
+       transfers/ide-transfer.h                          \
+       transfers/ide-transfer-manager.c                  \
+       transfers/ide-transfer-manager.h                  \
+       transfers/ide-transfers-button.c                  \
+       transfers/ide-transfers-button.h                  \
        tree/ide-tree-builder.h                           \
        tree/ide-tree-node.h                              \
        tree/ide-tree-types.h                             \
diff --git a/libide/ide-context.c b/libide/ide-context.c
index be20214..f7e3d62 100644
--- a/libide/ide-context.c
+++ b/libide/ide-context.c
@@ -48,6 +48,7 @@
 #include "search/ide-search-engine.h"
 #include "search/ide-search-provider.h"
 #include "snippets/ide-source-snippets-manager.h"
+#include "transfers/ide-transfer-manager.h"
 #include "util/ide-async-helper.h"
 #include "util/ide-settings.h"
 #include "vcs/ide-vcs.h"
@@ -72,6 +73,7 @@ struct _IdeContext
   IdeScriptManager         *script_manager;
   IdeSearchEngine          *search_engine;
   IdeSourceSnippetsManager *snippets_manager;
+  IdeTransferManager       *transfer_manager;
   IdeProject               *project;
   GFile                    *project_file;
   gchar                    *root_build_dir;
@@ -564,6 +566,7 @@ ide_context_finalize (GObject *object)
   g_clear_object (&self->project_file);
   g_clear_object (&self->recent_manager);
   g_clear_object (&self->runtime_manager);
+  g_clear_object (&self->transfer_manager);
   g_clear_object (&self->unsaved_files);
   g_clear_object (&self->vcs);
 
@@ -852,6 +855,10 @@ ide_context_init (IdeContext *self)
                                         "context", self,
                                         NULL);
 
+  self->transfer_manager = g_object_new (IDE_TYPE_TRANSFER_MANAGER,
+                                         "context", self,
+                                         NULL);
+
   self->unsaved_files = g_object_new (IDE_TYPE_UNSAVED_FILES,
                                       "context", self,
                                       NULL);
@@ -2195,3 +2202,18 @@ ide_context_get_run_manager (IdeContext *self)
 
   return self->run_manager;
 }
+
+/**
+ * ide_context_get_transfer_manager:
+ *
+ * Gets the #IdeTransferManager for the context.
+ *
+ * Returns: (transfer none): An #IdeTransferManager.
+ */
+IdeTransferManager *
+ide_context_get_transfer_manager (IdeContext *self)
+{
+  g_return_val_if_fail (IDE_IS_CONTEXT (self), NULL);
+
+  return self->transfer_manager;
+}
diff --git a/libide/ide-context.h b/libide/ide-context.h
index 96ea370..1a72a0d 100644
--- a/libide/ide-context.h
+++ b/libide/ide-context.h
@@ -47,6 +47,7 @@ IdeSettings              *ide_context_get_settings              (IdeContext
                                                                  const gchar          *schema_id,
                                                                  const gchar          *relative_path);
 IdeSourceSnippetsManager *ide_context_get_snippets_manager      (IdeContext           *self);
+IdeTransferManager       *ide_context_get_transfer_manager      (IdeContext           *self);
 IdeUnsavedFiles          *ide_context_get_unsaved_files         (IdeContext           *self);
 IdeVcs                   *ide_context_get_vcs                   (IdeContext           *self);
 const gchar              *ide_context_get_root_build_dir        (IdeContext           *self);
diff --git a/libide/ide-types.h b/libide/ide-types.h
index 89dedea..871fb38 100644
--- a/libide/ide-types.h
+++ b/libide/ide-types.h
@@ -123,6 +123,8 @@ typedef struct _IdeSymbol                      IdeSymbol;
 typedef struct _IdeSymbolResolver              IdeSymbolResolver;
 typedef struct _IdeSymbolResolverInterface     IdeSymbolResolverInterface;
 
+typedef struct _IdeTransferManager             IdeTransferManager;
+
 typedef struct _IdeUnsavedFiles                IdeUnsavedFiles;
 
 typedef struct _IdeUnsavedFile                 IdeUnsavedFile;
diff --git a/libide/ide.h b/libide/ide.h
index eaad665..45ccf6a 100644
--- a/libide/ide.h
+++ b/libide/ide.h
@@ -119,6 +119,8 @@ G_BEGIN_DECLS
 #include "template/ide-project-template.h"
 #include "template/ide-template-provider.h"
 #include "threading/ide-thread-pool.h"
+#include "transfers/ide-transfer.h"
+#include "transfers/ide-transfer-manager.h"
 #include "tree/ide-tree-builder.h"
 #include "tree/ide-tree-node.h"
 #include "tree/ide-tree-types.h"
diff --git a/libide/resources/libide.gresource.xml b/libide/resources/libide.gresource.xml
index ecf67b7..c18592c 100644
--- a/libide/resources/libide.gresource.xml
+++ b/libide/resources/libide.gresource.xml
@@ -76,6 +76,7 @@
     <file compressed="true" 
alias="ide-preferences-spin-button.ui">../preferences/ide-preferences-spin-button.ui</file>
     <file compressed="true" alias="ide-preferences-switch.ui">../preferences/ide-preferences-switch.ui</file>
     <file compressed="true" alias="ide-run-button.ui">../runner/ide-run-button.ui</file>
+    <file compressed="true" alias="ide-transfers-button.ui">../transfers/ide-transfers-button.ui</file>
     <file compressed="true" alias="ide-shortcuts-window.ui">../keybindings/ide-shortcuts-window.ui</file>
     <file compressed="true" 
alias="ide-workbench-header-bar.ui">../workbench/ide-workbench-header-bar.ui</file>
     <file compressed="true" alias="ide-workbench.ui">../workbench/ide-workbench.ui</file>
diff --git a/libide/transfers/ide-transfer-manager.c b/libide/transfers/ide-transfer-manager.c
new file mode 100644
index 0000000..481a833
--- /dev/null
+++ b/libide/transfers/ide-transfer-manager.c
@@ -0,0 +1,455 @@
+/* ide-transfer-manager.c
+ *
+ * Copyright (C) 2016 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
+ * 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-transfer-manager"
+
+#include "ide-context.h"
+#include "ide-debug.h"
+
+#include "transfers/ide-transfer.h"
+#include "transfers/ide-transfer-manager.h"
+
+#define DEFAULT_MAX_ACTIVE 1
+
+struct _IdeTransferManager
+{
+  GObject    parent_instance;
+  guint      max_active;
+  GPtrArray *transfers;
+};
+
+static void list_model_iface_init     (GListModelInterface *iface);
+static void ide_transfer_manager_pump (IdeTransferManager  *self);
+
+G_DEFINE_TYPE_EXTENDED (IdeTransferManager, ide_transfer_manager, IDE_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
+
+enum {
+  PROP_0,
+  PROP_HAS_ACTIVE,
+  PROP_MAX_ACTIVE,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+#define GET_BOOLEAN(obj,name)     (NULL != g_object_get_data(G_OBJECT(obj), name))
+#define SET_BOOLEAN(obj,name,val) (g_object_set_data(G_OBJECT(obj), name, GINT_TO_POINTER(val)))
+
+static void
+transfer_cancel (IdeTransfer *transfer)
+{
+  GCancellable *cancellable;
+
+  g_assert (IDE_IS_TRANSFER (transfer));
+
+  cancellable = g_object_get_data (G_OBJECT (transfer), "IDE_TRANSFER_CANCELLABLE");
+  if (G_IS_CANCELLABLE (cancellable) && !g_cancellable_is_cancelled (cancellable))
+    g_cancellable_cancel (cancellable);
+}
+
+static gboolean
+transfer_get_active (IdeTransfer *transfer)
+{
+  return GET_BOOLEAN (transfer, "IDE_TRANSFER_ACTIVE");
+}
+
+static void
+transfer_set_active (IdeTransfer *transfer,
+                     gboolean     active)
+{
+  SET_BOOLEAN (transfer, "IDE_TRANSFER_ACTIVE", active);
+}
+
+static gboolean
+transfer_get_completed (IdeTransfer *transfer)
+{
+  return GET_BOOLEAN (transfer, "IDE_TRANSFER_COMPLETED");
+}
+
+static void
+transfer_set_completed (IdeTransfer *transfer,
+                        gboolean     completed)
+{
+  SET_BOOLEAN (transfer, "IDE_TRANSFER_COMPLETED", completed);
+}
+
+static guint
+ide_transfer_manager_count_active (IdeTransferManager *self)
+{
+  guint active = 0;
+
+  g_assert (IDE_IS_TRANSFER_MANAGER (self));
+
+  for (guint i = 0; i < self->transfers->len; i++)
+    {
+      IdeTransfer *transfer = g_ptr_array_index (self->transfers, i);
+
+      if (transfer_get_active (transfer) && !transfer_get_completed (transfer))
+        active++;
+    }
+
+  return active;
+}
+
+static void
+ide_transfer_manager_execute_cb (GObject      *object,
+                                 GAsyncResult *result,
+                                 gpointer      user_data)
+{
+  IdeTransfer *transfer = (IdeTransfer *)object;
+  g_autoptr(IdeTransferManager) self = user_data;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_TRANSFER_MANAGER (self));
+  g_assert (IDE_IS_TRANSFER (transfer));
+
+  transfer_set_completed (transfer, TRUE);
+
+  if (!ide_transfer_execute_finish (transfer, result, &error))
+    {
+      IdeContext *context;
+
+      context = ide_object_get_context (IDE_OBJECT (self));
+      ide_context_warning (context, "%s", error->message);
+    }
+
+  ide_transfer_manager_pump (self);
+
+  IDE_EXIT;
+}
+
+static void
+ide_transfer_manager_begin (IdeTransferManager *self,
+                            IdeTransfer        *transfer)
+{
+  GCancellable *cancellable;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_TRANSFER_MANAGER (self));
+  g_assert (IDE_IS_TRANSFER (transfer));
+
+  transfer_set_active (transfer, TRUE);
+
+  cancellable = g_cancellable_new ();
+
+  g_object_set_data_full (G_OBJECT (transfer),
+                          "IDE_TRANSFER_CANCELLABLE",
+                          cancellable,
+                          g_object_unref);
+
+  ide_transfer_execute_async (transfer,
+                              cancellable,
+                              ide_transfer_manager_execute_cb,
+                              g_object_ref (self));
+
+  IDE_EXIT;
+}
+
+static void
+ide_transfer_manager_pump (IdeTransferManager *self)
+{
+  guint active;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_TRANSFER_MANAGER (self));
+
+  active = ide_transfer_manager_count_active (self);
+
+  if (active < self->max_active)
+    {
+      for (guint i = 0; i < self->transfers->len; i++)
+        {
+          IdeTransfer *transfer = g_ptr_array_index (self->transfers, i);
+
+          if (!transfer_get_active (transfer))
+            {
+              active++;
+              ide_transfer_manager_begin (self, transfer);
+              if (active >= self->max_active)
+                break;
+            }
+        }
+    }
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_HAS_ACTIVE]);
+
+  IDE_EXIT;
+}
+
+/**
+ * ide_transfer_manager_get_has_active:
+ *
+ * Gets if there are active transfers.
+ *
+ * Returns: %TRUE if there are active transfers.
+ */
+gboolean
+ide_transfer_manager_get_has_active (IdeTransferManager *self)
+{
+  g_return_val_if_fail (IDE_IS_TRANSFER_MANAGER (self), FALSE);
+
+  for (guint i = 0; i < self->transfers->len; i++)
+    {
+      IdeTransfer *transfer = g_ptr_array_index (self->transfers, i);
+
+      if (transfer_get_active (transfer) && !transfer_get_completed (transfer))
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
+guint
+ide_transfer_manager_get_max_active (IdeTransferManager *self)
+{
+  g_return_val_if_fail (IDE_IS_TRANSFER_MANAGER (self), 0);
+
+  return self->max_active;
+}
+
+void
+ide_transfer_manager_set_max_active (IdeTransferManager *self,
+                                     guint               max_active)
+{
+  g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
+
+  if (self->max_active != max_active)
+    {
+      self->max_active = max_active;
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_MAX_ACTIVE]);
+      ide_transfer_manager_pump (self);
+    }
+}
+
+static void
+ide_transfer_manager_finalize (GObject *object)
+{
+  IdeTransferManager *self = (IdeTransferManager *)object;
+
+  g_clear_pointer (&self->transfers, g_ptr_array_unref);
+
+  G_OBJECT_CLASS (ide_transfer_manager_parent_class)->finalize (object);
+}
+
+static void
+ide_transfer_manager_get_property (GObject    *object,
+                                   guint       prop_id,
+                                   GValue     *value,
+                                   GParamSpec *pspec)
+{
+  IdeTransferManager *self = IDE_TRANSFER_MANAGER (object);
+
+  switch (prop_id)
+    {
+    case PROP_HAS_ACTIVE:
+      g_value_set_boolean (value, ide_transfer_manager_get_has_active (self));
+      break;
+
+    case PROP_MAX_ACTIVE:
+      g_value_set_uint (value, ide_transfer_manager_get_max_active (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_transfer_manager_set_property (GObject      *object,
+                                   guint         prop_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+{
+  IdeTransferManager *self = IDE_TRANSFER_MANAGER (object);
+
+  switch (prop_id)
+    {
+    case PROP_MAX_ACTIVE:
+      ide_transfer_manager_set_max_active (self, g_value_get_uint (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_transfer_manager_class_init (IdeTransferManagerClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = ide_transfer_manager_finalize;
+  object_class->get_property = ide_transfer_manager_get_property;
+  object_class->set_property = ide_transfer_manager_set_property;
+
+  /**
+   * IdeTransferManager:has-active:
+   *
+   * If there are transfers active, this will be set.
+   */
+  properties [PROP_HAS_ACTIVE] =
+    g_param_spec_boolean ("has-active",
+                          "Has Active",
+                          "Has Active",
+                          FALSE,
+                          (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+  /**
+   * IdeTransferManager:max-active:
+   *
+   * Sets the max number of transfers to have active at one time.
+   * Set to zero for a sensible default.
+   */
+  properties [PROP_MAX_ACTIVE] =
+    g_param_spec_uint ("max-active",
+                       "Max Active",
+                       "Max Active",
+                       0,
+                       G_MAXUINT,
+                       0,
+                       (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_transfer_manager_init (IdeTransferManager *self)
+{
+  self->max_active = DEFAULT_MAX_ACTIVE;
+  self->transfers = g_ptr_array_new_with_free_func (g_object_unref);
+}
+
+
+void
+ide_transfer_manager_queue (IdeTransferManager *self,
+                            IdeTransfer        *transfer)
+{
+  guint position;
+
+  IDE_ENTRY;
+
+  g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
+  g_return_if_fail (IDE_IS_TRANSFER (transfer));
+
+  position = self->transfers->len;
+  g_ptr_array_add (self->transfers, g_object_ref (transfer));
+  g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
+  ide_transfer_manager_pump (self);
+
+  IDE_EXIT;
+}
+
+void
+ide_transfer_manager_cancel_all (IdeTransferManager *self)
+{
+  IDE_ENTRY;
+
+  g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
+
+  for (guint i = 0; i < self->transfers->len; i++)
+    {
+      IdeTransfer *transfer = g_ptr_array_index (self->transfers, i);
+
+      transfer_cancel (transfer);
+    }
+
+  IDE_EXIT;
+}
+
+void
+ide_transfer_manager_cancel (IdeTransferManager *self,
+                             IdeTransfer        *transfer)
+{
+  IDE_ENTRY;
+
+  g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
+  g_return_if_fail (IDE_IS_TRANSFER (transfer));
+
+  transfer_cancel (transfer);
+
+  IDE_EXIT;
+}
+
+/**
+ * ide_transfer_manager_clear:
+ *
+ * Removes all transfers from the manager that are completed.
+ */
+void
+ide_transfer_manager_clear (IdeTransferManager *self)
+{
+  IDE_ENTRY;
+
+  g_return_if_fail (IDE_IS_TRANSFER_MANAGER (self));
+
+  for (guint i = self->transfers->len; i > 0; i--)
+    {
+      IdeTransfer *transfer = g_ptr_array_index (self->transfers, i - 1);
+
+      if (transfer_get_completed (transfer))
+        {
+          g_ptr_array_remove_index (self->transfers, i - 1);
+          g_list_model_items_changed (G_LIST_MODEL (self), i - 1, 1, 0);
+        }
+    }
+
+  IDE_EXIT;
+}
+
+static GType
+ide_transfer_manager_get_item_type (GListModel *model)
+{
+  return IDE_TYPE_TRANSFER;
+}
+
+static guint
+ide_transfer_manager_get_n_items (GListModel *model)
+{
+  IdeTransferManager *self = (IdeTransferManager *)model;
+
+  g_assert (IDE_IS_TRANSFER_MANAGER (self));
+
+  return self->transfers->len;
+}
+
+static gpointer
+ide_transfer_manager_get_item (GListModel *model,
+                               guint       position)
+{
+  IdeTransferManager *self = (IdeTransferManager *)model;
+
+  g_assert (IDE_IS_TRANSFER_MANAGER (self));
+
+  if G_UNLIKELY (position >= self->transfers->len)
+    return NULL;
+
+  return g_object_ref (g_ptr_array_index (self->transfers, position));
+}
+
+static void
+list_model_iface_init (GListModelInterface *iface)
+{
+  iface->get_item_type = ide_transfer_manager_get_item_type;
+  iface->get_n_items = ide_transfer_manager_get_n_items;
+  iface->get_item = ide_transfer_manager_get_item;
+}
diff --git a/libide/transfers/ide-transfer-manager.h b/libide/transfers/ide-transfer-manager.h
new file mode 100644
index 0000000..b0fec0f
--- /dev/null
+++ b/libide/transfers/ide-transfer-manager.h
@@ -0,0 +1,46 @@
+/* ide-transfer-manager.h
+ *
+ * Copyright (C) 2016 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
+ * 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_TRANSFER_MANAGER_H
+#define IDE_TRANSFER_MANAGER_H
+
+#include "ide-object.h"
+
+#include "transfers/ide-transfer.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TRANSFER_MANAGER (ide_transfer_manager_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeTransferManager, ide_transfer_manager, IDE, TRANSFER_MANAGER, IdeObject)
+
+gboolean ide_transfer_manager_get_has_active (IdeTransferManager *self);
+guint    ide_transfer_manager_get_max_active (IdeTransferManager *self);
+void     ide_transfer_manager_set_max_active (IdeTransferManager *self,
+                                              guint               max_active);
+void     ide_transfer_manager_cancel         (IdeTransferManager *self,
+                                              IdeTransfer        *transfer);
+void     ide_transfer_manager_cancel_all     (IdeTransferManager *self);
+void     ide_transfer_manager_clear          (IdeTransferManager *self);
+void     ide_transfer_manager_queue          (IdeTransferManager *self,
+                                              IdeTransfer        *transfer);
+
+G_END_DECLS
+
+#endif /* IDE_TRANSFER_MANAGER_H */
+
diff --git a/libide/transfers/ide-transfer.c b/libide/transfers/ide-transfer.c
new file mode 100644
index 0000000..d379b12
--- /dev/null
+++ b/libide/transfers/ide-transfer.c
@@ -0,0 +1,109 @@
+/* ide-transfer.c
+ *
+ * Copyright (C) 2016 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
+ * 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-transfer"
+
+#include "ide-transfer.h"
+
+G_DEFINE_INTERFACE (IdeTransfer, ide_transfer, G_TYPE_OBJECT)
+
+static void
+ide_transfer_real_execute_async (IdeTransfer         *self,
+                                 GCancellable        *cancellable,
+                                 GAsyncReadyCallback  callback,
+                                 gpointer             user_data)
+{
+  g_autoptr(GTask) task = NULL;
+
+  g_assert (IDE_IS_TRANSFER (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_return_boolean (task, TRUE);
+}
+
+static gboolean
+ide_transfer_real_execute_finish (IdeTransfer   *self,
+                                  GAsyncResult  *result,
+                                  GError       **error)
+{
+  g_assert (IDE_IS_TRANSFER (self));
+  g_assert (G_IS_TASK (result));
+
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+static void
+ide_transfer_default_init (IdeTransferInterface *iface)
+{
+  iface->execute_async = ide_transfer_real_execute_async;
+  iface->execute_finish = ide_transfer_real_execute_finish;
+
+  g_object_interface_install_property (iface,
+                                       g_param_spec_string ("title",
+                                                            "Title",
+                                                            "Title",
+                                                            NULL,
+                                                            (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
+
+  g_object_interface_install_property (iface,
+                                       g_param_spec_string ("icon-name",
+                                                            "Icon Name",
+                                                            "Icon Name",
+                                                            NULL,
+                                                            (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
+
+  g_object_interface_install_property (iface,
+                                       g_param_spec_double ("progress",
+                                                            "Progress",
+                                                            "Progress",
+                                                            0.0,
+                                                            1.0,
+                                                            0.0,
+                                                            (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
+
+  g_object_interface_install_property (iface,
+                                       g_param_spec_string ("status",
+                                                            "Status",
+                                                            "Status",
+                                                            NULL,
+                                                            (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
+}
+
+void
+ide_transfer_execute_async (IdeTransfer         *self,
+                            GCancellable        *cancellable,
+                            GAsyncReadyCallback  callback,
+                            gpointer             user_data)
+{
+  g_assert (IDE_IS_TRANSFER (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  IDE_TRANSFER_GET_IFACE (self)->execute_async (self, cancellable, callback, user_data);
+}
+
+gboolean
+ide_transfer_execute_finish (IdeTransfer   *self,
+                             GAsyncResult  *result,
+                             GError       **error)
+{
+  g_return_val_if_fail (IDE_IS_TRANSFER (self), FALSE);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
+
+  return IDE_TRANSFER_GET_IFACE (self)->execute_finish (self, result, error);
+}
diff --git a/libide/transfers/ide-transfer.h b/libide/transfers/ide-transfer.h
new file mode 100644
index 0000000..a411289
--- /dev/null
+++ b/libide/transfers/ide-transfer.h
@@ -0,0 +1,53 @@
+/* ide-transfer.h
+ *
+ * Copyright (C) 2016 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
+ * 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_TRANSFER_H
+#define IDE_TRANSFER_H
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TRANSFER (ide_transfer_get_type())
+
+G_DECLARE_INTERFACE (IdeTransfer, ide_transfer, IDE, TRANSFER, GObject)
+
+struct _IdeTransferInterface
+{
+  GTypeInterface parent;
+
+  void     (*execute_async)  (IdeTransfer          *self,
+                              GCancellable         *cancellable,
+                              GAsyncReadyCallback   callback,
+                              gpointer              user_data);
+  gboolean (*execute_finish) (IdeTransfer          *self,
+                              GAsyncResult         *result,
+                              GError              **error);
+};
+
+void     ide_transfer_execute_async  (IdeTransfer          *self,
+                                      GCancellable         *cancellable,
+                                      GAsyncReadyCallback   callback,
+                                      gpointer              user_data);
+gboolean ide_transfer_execute_finish (IdeTransfer          *self,
+                                      GAsyncResult         *result,
+                                      GError              **error);
+
+G_END_DECLS
+
+#endif /* IDE_TRANSFER_H */
diff --git a/libide/transfers/ide-transfers-button.c b/libide/transfers/ide-transfers-button.c
new file mode 100644
index 0000000..1b9fac9
--- /dev/null
+++ b/libide/transfers/ide-transfers-button.c
@@ -0,0 +1,234 @@
+/* ide-transfers-button.c
+ *
+ * Copyright (C) 2016 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
+ * 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-transfers-button"
+
+#include "ide-debug.h"
+#include "ide-context.h"
+
+#include "transfers/ide-transfer.h"
+#include "transfers/ide-transfer-manager.h"
+#include "transfers/ide-transfers-button.h"
+#include "util/ide-gtk.h"
+
+struct _IdeTransfersButton
+{
+  GtkMenuButton  parent_instance;
+
+  GtkPopover    *popover;
+  GtkListBox    *list_box;
+};
+
+G_DEFINE_TYPE (IdeTransfersButton, ide_transfers_button, GTK_TYPE_MENU_BUTTON)
+
+GtkWidget *
+ide_transfers_button_new (void)
+{
+  return g_object_new (IDE_TYPE_TRANSFERS_BUTTON, NULL);
+}
+
+static gboolean
+progress_to_string (GBinding     *binding,
+                    const GValue *from_value,
+                    GValue       *to_value,
+                    gpointer      user_data)
+{
+  gdouble value = g_value_get_double (from_value);
+  gchar *format = g_strdup_printf ("%d <span size='smaller'>%%</span>", (gint)(value * 100.0));
+  g_value_take_string (to_value, format);
+  return TRUE;
+}
+
+static GtkWidget *
+create_transfer_row (gpointer item,
+                     gpointer user_data)
+{
+  IdeTransfersButton *self = user_data;
+  IdeTransfer *transfer = item;
+  GtkListBoxRow *row;
+  GtkProgressBar *progress;
+  GtkButton *button;
+  GtkImage *image;
+  GtkLabel *label;
+  GtkBox *box;
+  GtkBox *vbox;
+
+  g_assert (IDE_IS_TRANSFER (transfer));
+  g_assert (IDE_IS_TRANSFERS_BUTTON (self));
+
+  row = g_object_new (GTK_TYPE_LIST_BOX_ROW,
+                      "selectable", FALSE,
+                      "visible", TRUE,
+                      NULL);
+
+  vbox = g_object_new (GTK_TYPE_BOX,
+                       "orientation", GTK_ORIENTATION_VERTICAL,
+                       "spacing", 8,
+                       "visible", TRUE,
+                       NULL);
+  gtk_container_add (GTK_CONTAINER (row), GTK_WIDGET (vbox));
+
+  box = g_object_new (GTK_TYPE_BOX,
+                      "orientation", GTK_ORIENTATION_HORIZONTAL,
+                      "spacing", 12,
+                      "visible", TRUE,
+                      NULL);
+  gtk_container_add (GTK_CONTAINER (vbox), GTK_WIDGET (box));
+
+  image = g_object_new (GTK_TYPE_IMAGE,
+                        "visible", TRUE,
+                        NULL);
+  g_object_bind_property (item, "icon-name", image, "icon-name", G_BINDING_SYNC_CREATE);
+  gtk_container_add (GTK_CONTAINER (box), GTK_WIDGET (image));
+
+  label = g_object_new (GTK_TYPE_LABEL,
+                        "hexpand", TRUE,
+                        "visible", TRUE,
+                        "xalign", 0.0f,
+                        NULL);
+  g_object_bind_property (item, "title", label, "label", G_BINDING_SYNC_CREATE);
+  gtk_container_add (GTK_CONTAINER (box), GTK_WIDGET (label));
+
+  label = g_object_new (GTK_TYPE_LABEL,
+                        "use-markup", TRUE,
+                        "visible", TRUE,
+                        "xalign", 1.0f,
+                        NULL);
+  ide_widget_add_style_class (GTK_WIDGET (label), "dim-label");
+  g_object_bind_property_full (item, "progress", label, "label", G_BINDING_SYNC_CREATE,
+                               progress_to_string, NULL, NULL, NULL);
+  gtk_container_add (GTK_CONTAINER (box), GTK_WIDGET (label));
+
+  button = g_object_new (GTK_TYPE_BUTTON,
+                         "visible", TRUE,
+                         "child", g_object_new (GTK_TYPE_IMAGE,
+                                                "icon-name", "window-close-symbolic",
+                                                "visible", TRUE,
+                                                NULL),
+                         NULL);
+  gtk_container_add (GTK_CONTAINER (box), GTK_WIDGET (button));
+
+  progress = g_object_new (GTK_TYPE_PROGRESS_BAR,
+                           "visible", TRUE,
+                           NULL);
+  g_object_bind_property (item, "progress", progress, "fraction", G_BINDING_SYNC_CREATE);
+  gtk_container_add (GTK_CONTAINER (vbox), GTK_WIDGET (progress));
+
+  g_object_set_data_full (G_OBJECT (row), "IDE_TRANSFER", g_object_ref (transfer), g_object_unref);
+
+  return GTK_WIDGET (row);
+}
+
+static void
+ide_transfers_button_update_visibility (IdeTransfersButton *self)
+{
+  IdeTransferManager *transfer_manager;
+  IdeContext *context;
+  gboolean visible;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_TRANSFERS_BUTTON (self));
+
+  if (NULL != (context = ide_widget_get_context (GTK_WIDGET (self))) &&
+      NULL != (transfer_manager = ide_context_get_transfer_manager (context)))
+    visible = !!g_list_model_get_n_items (G_LIST_MODEL (transfer_manager));
+
+  gtk_widget_set_visible (GTK_WIDGET (self), visible);
+
+  IDE_EXIT;
+}
+
+static void
+ide_transfers_button_context_set (GtkWidget  *widget,
+                                  IdeContext *context)
+{
+  IdeTransfersButton *self = (IdeTransfersButton *)widget;
+  IdeTransferManager *transfer_manager;
+
+  g_assert (IDE_IS_TRANSFERS_BUTTON (self));
+  g_assert (!context || IDE_IS_CONTEXT (context));
+
+  if (context == NULL)
+    return;
+
+  transfer_manager = ide_context_get_transfer_manager (context);
+
+  g_signal_connect_object (transfer_manager,
+                           "items-changed",
+                           G_CALLBACK (ide_transfers_button_update_visibility),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  gtk_list_box_bind_model (self->list_box,
+                           G_LIST_MODEL (transfer_manager),
+                           create_transfer_row,
+                           self,
+                           NULL);
+
+  ide_transfers_button_update_visibility (self);
+}
+
+static void
+ide_transfers_button_clear (GSimpleAction *action,
+                            GVariant      *param,
+                            gpointer       user_data)
+{
+  IdeTransfersButton *self = user_data;
+  IdeTransferManager *transfer_manager;
+  IdeContext *context;
+
+  g_assert (G_IS_SIMPLE_ACTION (action));
+
+  if (NULL != (context = ide_widget_get_context (GTK_WIDGET (self))) &&
+      NULL != (transfer_manager = ide_context_get_transfer_manager (context)))
+    ide_transfer_manager_clear (transfer_manager);
+}
+
+static void
+ide_transfers_button_class_init (IdeTransfersButtonClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/builder/ui/ide-transfers-button.ui");
+  gtk_widget_class_bind_template_child (widget_class, IdeTransfersButton, list_box);
+  gtk_widget_class_bind_template_child (widget_class, IdeTransfersButton, popover);
+}
+
+static void
+ide_transfers_button_init (IdeTransfersButton *self)
+{
+  g_autoptr(GSimpleActionGroup) actions = NULL;
+  static const GActionEntry entries[] = {
+    { "clear", ide_transfers_button_clear },
+  };
+
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  ide_widget_set_context_handler (GTK_WIDGET (self),
+                                  ide_transfers_button_context_set);
+
+  actions = g_simple_action_group_new ();
+  g_action_map_add_action_entries (G_ACTION_MAP (actions),
+                                   entries,
+                                   G_N_ELEMENTS (entries),
+                                   self);
+  gtk_widget_insert_action_group (GTK_WIDGET (self),
+                                  "transfers",
+                                  G_ACTION_GROUP (actions));
+}
diff --git a/libide/transfers/ide-transfers-button.h b/libide/transfers/ide-transfers-button.h
new file mode 100644
index 0000000..b9c2616
--- /dev/null
+++ b/libide/transfers/ide-transfers-button.h
@@ -0,0 +1,35 @@
+/* ide-transfers-button.h
+ *
+ * Copyright (C) 2016 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
+ * 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_TRANSFERS_BUTTON_H
+#define IDE_TRANSFERS_BUTTON_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_TRANSFERS_BUTTON (ide_transfers_button_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeTransfersButton, ide_transfers_button, IDE, TRANSFERS_BUTTON, GtkMenuButton)
+
+GtkWidget *ide_transfers_button_new (void);
+
+G_END_DECLS
+
+#endif /* IDE_TRANSFERS_BUTTON_H */
+
diff --git a/libide/transfers/ide-transfers-button.ui b/libide/transfers/ide-transfers-button.ui
new file mode 100644
index 0000000..09736ed
--- /dev/null
+++ b/libide/transfers/ide-transfers-button.ui
@@ -0,0 +1,57 @@
+<?xml version="1.0"?>
+<interface>
+  <object class="GtkPopover" id="popover">
+    <property name="border-width">12</property>
+    <style>
+      <class name="transfers"/>
+    </style>
+    <child>
+      <object class="GtkBox">
+        <property name="orientation">vertical</property>
+        <property name="spacing">24</property>
+        <property name="visible">true</property>
+        <child>
+          <object class="GtkScrolledWindow">
+            <property name="min-content-width">275</property>
+            <property name="min-content-height">300</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkListBox" id="list_box">
+                <property name="visible">true</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox">
+            <property name="orientation">horizontal</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkButton">
+                <property name="action-name">transfers.clear</property>
+                <property name="label" translatable="yes">Clear _All</property>
+                <property name="use-underline">true</property>
+                <property name="visible">true</property>
+              </object>
+              <packing>
+                <property name="pack-type">end</property>
+              </packing>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </object>
+  <template class="IdeTransfersButton" parent="GtkMenuButton">
+    <property name="popover">popover</property>
+    <style>
+      <class name="image-button"/>
+    </style>
+    <child>
+      <object class="GtkImage">
+        <property name="icon-name">folder-download-symbolic</property>
+        <property name="visible">true</property>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/libide/workbench/ide-workbench-header-bar.ui b/libide/workbench/ide-workbench-header-bar.ui
index 4d4b0e4..20cc662 100644
--- a/libide/workbench/ide-workbench-header-bar.ui
+++ b/libide/workbench/ide-workbench-header-bar.ui
@@ -34,23 +34,37 @@
           </packing>
         </child>
         <child>
-          <object class="GtkMenuButton" id="menu_button">
-            <property name="tooltip-text" translatable="yes">Show workbench menu</property>
-            <property name="focus-on-click">false</property>
+          <object class="GtkBox">
+            <property name="orientation">horizontal</property>
+            <property name="spacing">6</property>
             <property name="visible">true</property>
-            <style>
-              <class name="image-button"/>
-            </style>
             <child>
-              <object class="GtkImage">
-                <property name="icon-name">open-menu-symbolic</property>
+              <object class="IdeTransfersButton" id="transfers_button">
+                <property name="tooltip-text" translatable="yes">Transfers</property>
+                <property name="focus-on-click">false</property>
                 <property name="visible">true</property>
               </object>
             </child>
+            <child>
+              <object class="GtkMenuButton" id="menu_button">
+                <property name="tooltip-text" translatable="yes">Show workbench menu</property>
+                <property name="focus-on-click">false</property>
+                <property name="visible">true</property>
+                <style>
+                  <class name="image-button"/>
+                </style>
+                <child>
+                  <object class="GtkImage">
+                    <property name="icon-name">open-menu-symbolic</property>
+                    <property name="visible">true</property>
+                  </object>
+                </child>
+              </object>
+            </child>
           </object>
           <packing>
-            <property name="pack-type">end</property>
             <property name="priority">-100000</property>
+            <property name="pack-type">end</property>
           </packing>
         </child>
         <child>



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