[gnome-builder/wip/commands] commands: work in progress on command-task.



commit afbc17fdc06b1da8a45398952fbf2ce6290a9237
Author: Christian Hergert <christian hergert me>
Date:   Mon Oct 6 23:02:38 2014 -0700

    commands: work in progress on command-task.
    
    command-task will hold our state from a command. it allows commands to
    do things pretty how they want but standardize the result set to be
    viewed in the commandbar.

 src/commands/gb-command-result.c |  221 ------------------------------
 src/commands/gb-command-result.h |   65 ---------
 src/commands/gb-command-task.c   |  279 ++++++++++++++++++++++++++++++++++++++
 src/commands/gb-command-task.h   |   79 +++++++++++
 src/commands/gb-command.c        |   37 +++---
 src/commands/gb-command.h        |   35 ++----
 src/gnome-builder.mk             |    4 +-
 7 files changed, 390 insertions(+), 330 deletions(-)
---
diff --git a/src/commands/gb-command-task.c b/src/commands/gb-command-task.c
new file mode 100644
index 0000000..9abcf31
--- /dev/null
+++ b/src/commands/gb-command-task.c
@@ -0,0 +1,279 @@
+/* gb-command-task.c
+ *
+ * Copyright (C) 2014 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 "gb-command-task.h"
+
+/**
+ * SECTION:gb-command-task
+ * @title: GbCommandTask
+ * @short_description: Tasks executed by commands
+ * 
+ * #GbCommandTask is the class representing a task as executed by the command.
+ * They have both command-text and result-text properties that may be rendered
+ * by the #GbCommandBar.
+ */
+
+struct _GbCommandTaskPrivate
+{
+  gboolean  active;
+  gdouble   progress;
+  gchar    *command_text;
+  gchar    *result_text;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GbCommandTask, gb_command_task, G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_ACTIVE,
+  PROP_PROGRESS,
+  PROP_COMMAND_TEXT,
+  PROP_RESULT_TEXT,
+  LAST_PROP
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+
+GbCommandTask *
+gb_command_task_new (void)
+{
+  return g_object_new (GB_TYPE_COMMAND_TASK, NULL);
+}
+
+gboolean
+gb_command_task_get_active (GbCommandTask *task)
+{
+  g_return_val_if_fail (GB_IS_COMMAND_TASK (task), FALSE);
+
+  return task->priv->active;
+}
+
+void
+gb_command_task_set_active (GbCommandTask *task,
+                            gboolean       active)
+{
+  g_return_if_fail (GB_IS_COMMAND_TASK (task));
+
+  if (active != task->priv->active)
+    {
+      task->priv->active = active;
+      g_object_notify_by_pspec (G_OBJECT (task), gParamSpecs [PROP_ACTIVE]);
+    }
+}
+
+const gchar *
+gb_command_task_get_command_text (GbCommandTask *task)
+{
+  g_return_val_if_fail (GB_IS_COMMAND_TASK (task), NULL);
+
+  return task->priv->command_text;
+}
+
+void
+gb_command_task_set_command_text (GbCommandTask *task,
+                                  const gchar   *command_text)
+{
+  g_return_if_fail (GB_IS_COMMAND_TASK (task));
+  
+  if (command_text != task->priv->command_text)
+    {
+      g_free (task->priv->command_text);
+      task->priv->command_text = g_strdup (command_text);
+      g_object_notify_by_pspec (G_OBJECT (task),
+                                gParamSpecs [PROP_COMMAND_TEXT]);
+    }
+}
+
+gdouble
+gb_command_task_get_progress (GbCommandTask *task)
+{
+  g_return_val_if_fail (GB_IS_COMMAND_TASK (task), 0.0);
+
+  return task->priv->progress;
+}
+
+void
+gb_command_task_set_progress (GbCommandTask *task,
+                              gdouble        progress)
+{
+  g_return_if_fail (GB_IS_COMMAND_TASK (task));
+  g_return_if_fail (progress >= 0.0);
+  g_return_if_fail (progress <= 1.0);
+
+  if (progress != task->priv->progress)
+    {
+      task->priv->progress = progress;
+      g_object_notify_by_pspec (G_OBJECT (task), gParamSpecs [PROP_PROGRESS]);
+    }
+}
+
+const gchar *
+gb_command_task_get_result_text (GbCommandTask *task)
+{
+  g_return_val_if_fail (GB_IS_COMMAND_TASK (task), NULL);
+
+  return task->priv->result_text;
+}
+
+void
+gb_command_task_set_result_text (GbCommandTask *task,
+                                 const gchar   *result_text)
+{
+  g_return_if_fail (GB_IS_COMMAND_TASK (task));
+
+  if (result_text != task->priv->result_text)
+    {
+      g_free (task->priv->result_text);
+      task->priv->result_text = g_strdup (result_text);
+      g_object_notify_by_pspec (G_OBJECT (task),
+                                gParamSpecs [PROP_RESULT_TEXT]);
+    }
+}
+
+static void
+gb_command_task_finalize (GObject *object)
+{
+  GbCommandTaskPrivate *priv = GB_COMMAND_TASK (object)->priv;
+
+  g_clear_pointer (&priv->command_text, g_free);
+  g_clear_pointer (&priv->result_text, g_free);
+
+  G_OBJECT_CLASS (gb_command_task_parent_class)->finalize (object);
+}
+
+static void
+gb_command_task_get_property (GObject    *object,
+                              guint       prop_id,
+                              GValue     *value,
+                              GParamSpec *pspec)
+{
+  GbCommandTask *self = GB_COMMAND_TASK (object);
+
+  switch (prop_id)
+    {
+    case PROP_ACTIVE:
+      g_value_set_boolean (value, gb_command_task_get_active (self));
+      break;
+
+    case PROP_COMMAND_TEXT:
+      g_value_set_string (value, gb_command_task_get_command_text (self));
+      break;
+
+    case PROP_PROGRESS:
+      g_value_set_double (value, gb_command_task_get_progress (self));
+      break;
+
+    case PROP_RESULT_TEXT:
+      g_value_set_string (value, gb_command_task_get_result_text (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_command_task_set_property (GObject      *object,
+                              guint         prop_id,
+                              const GValue *value,
+                              GParamSpec   *pspec)
+{
+  GbCommandTask *self = GB_COMMAND_TASK (object);
+
+  switch (prop_id)
+    {
+    case PROP_ACTIVE:
+      gb_command_task_set_active (self, g_value_get_boolean (value));
+      break;
+
+    case PROP_COMMAND_TEXT:
+      gb_command_task_set_command_text (self, g_value_get_string (value));
+      break;
+
+    case PROP_PROGRESS:
+      gb_command_task_set_progress (self, g_value_get_double (value));
+      break;
+
+    case PROP_RESULT_TEXT:
+      gb_command_task_set_result_text (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_command_task_class_init (GbCommandTaskClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gb_command_task_finalize;
+  object_class->get_property = gb_command_task_get_property;
+  object_class->set_property = gb_command_task_set_property;
+
+  gParamSpecs [PROP_ACTIVE] =
+    g_param_spec_boolean ("active",
+                          _("Active"),
+                          _("If the task is active."),
+                          FALSE,
+                          (G_PARAM_READWRITE |
+                           G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_ACTIVE,
+                                   gParamSpecs [PROP_ACTIVE]);
+
+  gParamSpecs [PROP_COMMAND_TEXT] =
+    g_param_spec_string ("command-text",
+                         _("Command Text"),
+                         _("The text for the command."),
+                         NULL,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_COMMAND_TEXT,
+                                   gParamSpecs [PROP_COMMAND_TEXT]);
+  
+  gParamSpecs [PROP_PROGRESS] =
+    g_param_spec_double ("progress",
+                         _("Progress"),
+                         _("The progress of the task if available."),
+                         0.0,
+                         1.0,
+                         0.0,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_PROGRESS,
+                                   gParamSpecs [PROP_PROGRESS]);
+
+  gParamSpecs [PROP_RESULT_TEXT] =
+    g_param_spec_string ("result-text",
+                         _("Result Text"),
+                         _("The result display string of the command."),
+                         NULL,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_RESULT_TEXT,
+                                   gParamSpecs [PROP_RESULT_TEXT]);
+}
+
+static void
+gb_command_task_init (GbCommandTask *self)
+{
+  self->priv = gb_command_task_get_instance_private (self);
+}
diff --git a/src/commands/gb-command-task.h b/src/commands/gb-command-task.h
new file mode 100644
index 0000000..95fb524
--- /dev/null
+++ b/src/commands/gb-command-task.h
@@ -0,0 +1,79 @@
+/* gb-command-task.h
+ *
+ * Copyright (C) 2014 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 GB_COMMAND_TASK_H
+#define GB_COMMAND_TASK_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_COMMAND_TASK            (gb_command_task_get_type())
+#define GB_COMMAND_TASK(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GB_TYPE_COMMAND_TASK, 
GbCommandTask))
+#define GB_COMMAND_TASK_CONST(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GB_TYPE_COMMAND_TASK, 
GbCommandTask const))
+#define GB_COMMAND_TASK_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GB_TYPE_COMMAND_TASK, 
GbCommandTaskClass))
+#define GB_IS_COMMAND_TASK(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GB_TYPE_COMMAND_TASK))
+#define GB_IS_COMMAND_TASK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GB_TYPE_COMMAND_TASK))
+#define GB_COMMAND_TASK_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GB_TYPE_COMMAND_TASK, 
GbCommandTaskClass))
+
+typedef struct _GbCommandTask        GbCommandTask;
+typedef struct _GbCommandTaskClass   GbCommandTaskClass;
+typedef struct _GbCommandTaskPrivate GbCommandTaskPrivate;
+
+struct _GbCommandTask
+{
+  GObject parent;
+
+  /*< private >*/
+  GbCommandTaskPrivate *priv;
+};
+
+struct _GbCommandTaskClass
+{
+  GObjectClass parent;
+
+  void (*run) (GbCommandTask *task);
+
+  gpointer _padding1;
+  gpointer _padding2;
+  gpointer _padding3;
+  gpointer _padding4;
+  gpointer _padding5;
+  gpointer _padding6;
+  gpointer _padding7;
+};
+
+GType          gb_command_task_get_type         (void) G_GNUC_CONST;
+GbCommandTask *gb_command_task_new              (void);
+void           gb_command_task_run              (GbCommandTask *task);
+gboolean       gb_command_task_get_active       (GbCommandTask *task);
+void           gb_command_task_set_active       (GbCommandTask *task,
+                                                 gboolean       active);
+const gchar   *gb_command_task_get_result_text  (GbCommandTask *task);
+void           gb_command_task_set_result_text  (GbCommandTask *task,
+                                                 const gchar   *result_text);
+const gchar   *gb_command_task_get_command_text (GbCommandTask *task);
+void           gb_command_task_set_command_text (GbCommandTask *task,
+                                                 const gchar   *command_text);
+gdouble        gb_command_task_get_progress     (GbCommandTask *task);
+void           gb_command_task_set_progress     (GbCommandTask *task,
+                                                 gdouble        progress);
+
+G_END_DECLS
+
+#endif /* GB_COMMAND_TASK_H */
diff --git a/src/commands/gb-command.c b/src/commands/gb-command.c
index 35b6969..827e508 100644
--- a/src/commands/gb-command.c
+++ b/src/commands/gb-command.c
@@ -36,8 +36,7 @@ enum {
 };
 
 enum {
-  EXECUTE_ASYNC,
-  EXECUTE_FINISH,
+  EXECUTE,
   LAST_SIGNAL
 };
 
@@ -87,25 +86,16 @@ gb_command_set_description (GbCommand   *command,
     }
 }
 
-void
-gb_command_execute_async (GbCommand           *command,
-                          GVariant            *parameter,
-                          GCancellable        *cancellable,
-                          GAsyncReadyCallback  callback,
-                          gpointer             user_data)
+GbCommandTask *
+gb_command_execute (GbCommand *command)
 {
-  g_return_if_fail (GB_IS_COMMAND (command));
-  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+  GbCommandTask *task = NULL;
 
-}
+  g_return_val_if_fail (GB_IS_COMMAND (command), NULL);
 
-GbCommandResult *
-gb_command_execute_finish (GbCommand     *command,
-                           GAsyncResult  *result,
-                           GError       **error)
-{
-  g_return_if_fail (GB_IS_COMMAND (command));
-  g_return_if_fail (G_IS_ASYNC_RESULT (result));
+  g_signal_emit (command, gSignals [EXECUTE], 0, &task);
+
+  return task;
 }
 
 static void
@@ -194,6 +184,17 @@ gb_command_class_init (GbCommandClass *klass)
                           G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (object_class, PROP_NAME,
                                    gParamSpecs [PROP_NAME]);
+
+  gSignals [EXECUTE] =
+    g_signal_new ("execute",
+                  GB_TYPE_COMMAND,
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (GbCommandClass, execute),
+                  g_signal_accumulator_first_wins,
+                  NULL,
+                  g_cclosure_marshal_VOID__VOID,
+                  GB_TYPE_COMMAND_TASK,
+                  0);
 }
 
 static void
diff --git a/src/commands/gb-command.h b/src/commands/gb-command.h
index 560cb52..96b7fcc 100644
--- a/src/commands/gb-command.h
+++ b/src/commands/gb-command.h
@@ -21,7 +21,7 @@
 
 #include <gio/gio.h>
 
-#include "gb-command-result.h"
+#include "gb-command-task.h"
 
 G_BEGIN_DECLS
 
@@ -48,15 +48,8 @@ struct _GbCommand
 struct _GbCommandClass
 {
   GObjectClass parent;
-  
-  void             (*execute_async)  (GbCommand            *command,
-                                      GVariant             *parameter,
-                                      GCancellable         *cancellable,
-                                      GAsyncReadyCallback   callback,
-                                      gpointer              user_data);
-  GbCommandResult *(*execute_finish) (GbCommand            *command,
-                                      GAsyncResult         *result,
-                                      GError              **error);
+
+  GbCommandTask *(*execute) (GbCommand *command);
 
   gpointer _padding1;
   gpointer _padding2;
@@ -64,24 +57,18 @@ struct _GbCommandClass
   gpointer _padding4;
   gpointer _padding5;
   gpointer _padding6;
+  gpointer _padding7;
 };
 
 GType           gb_command_get_type        (void) G_GNUC_CONST;
 GbCommand      *gb_command_new             (void);
-const gchar    *gb_command_get_name        (GbCommand            *command);
-void            gb_command_set_name        (GbCommand            *command,
-                                            const gchar          *name);
-const gchar    *gb_command_get_description (GbCommand            *command);
-void            gb_command_set_description (GbCommand            *command,
-                                            const gchar          *description);
-void            gb_command_execute_async   (GbCommand            *command,
-                                            GVariant             *parameter,
-                                            GCancellable         *cancellable,
-                                            GAsyncReadyCallback   callback,
-                                            gpointer              user_data);
-GbCommandResult *gb_command_execute_finish (GbCommand            *command,
-                                            GAsyncResult         *result,
-                                            GError              **error);
+const gchar    *gb_command_get_name        (GbCommand   *command);
+void            gb_command_set_name        (GbCommand   *command,
+                                            const gchar *name);
+const gchar    *gb_command_get_description (GbCommand   *command);
+void            gb_command_set_description (GbCommand   *command,
+                                            const gchar *description);
+GbCommandTask  *gb_command_execute         (GbCommand   *command);
 
 G_END_DECLS
 
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index 9522d6e..e984ebf 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -15,8 +15,8 @@ libgnome_builder_la_SOURCES = \
        src/commands/gb-command.h \
        src/commands/gb-command-manager.c \
        src/commands/gb-command-manager.h \
-       src/commands/gb-command-result.c \
-       src/commands/gb-command-result.h \
+       src/commands/gb-command-task.c \
+       src/commands/gb-command-task.h \
        src/devhelp/gb-devhelp-navigation-item.c \
        src/devhelp/gb-devhelp-navigation-item.h \
        src/devhelp/gb-devhelp-tab.c \


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