[gnome-builder/wip/commands] commands: add command-text property to GbPythonCommand



commit 0dff9689181896b6c2250c4f81c16398f5c1bf0a
Author: Christian Hergert <christian hergert me>
Date:   Tue Oct 7 17:54:48 2014 -0700

    commands: add command-text property to GbPythonCommand

 src/commands/gb-python-command.c |  110 +++++++++++++++++++++++++++++++++++--
 src/commands/gb-python-command.h |    2 +-
 2 files changed, 105 insertions(+), 7 deletions(-)
---
diff --git a/src/commands/gb-python-command.c b/src/commands/gb-python-command.c
index 648ce09..c68f243 100644
--- a/src/commands/gb-python-command.c
+++ b/src/commands/gb-python-command.c
@@ -21,12 +21,50 @@
 
 #include "gb-python-command.h"
 
-G_DEFINE_TYPE (GbPythonCommand, gb_python_command, GB_TYPE_COMMAND)
+struct _GbPythonCommandPrivate
+{
+  gchar *command_text;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GbPythonCommand, gb_python_command, GB_TYPE_COMMAND)
+
+enum {
+  PROP_0,
+  PROP_COMMAND_TEXT,
+  LAST_PROP
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
 
 GbCommand *
-gb_python_command_new (void)
+gb_python_command_new (const gchar *command_text)
+{
+  return g_object_new (GB_TYPE_PYTHON_COMMAND,
+                       "command-text", command_text,
+                       NULL);
+}
+
+const gchar *
+gb_python_command_get_command_text (GbPythonCommand *command)
+{
+  g_return_val_if_fail (GB_IS_PYTHON_COMMAND (command), NULL);
+
+  return command->priv->command_text;
+}
+
+void
+gb_python_command_set_command_text (GbPythonCommand *command,
+                                    const gchar     *command_text)
 {
-  return g_object_new (GB_TYPE_PYTHON_COMMAND, NULL);
+  g_return_if_fail (GB_IS_PYTHON_COMMAND (command));
+
+  if (command->priv->command_text != command_text)
+    {
+      g_free (command->priv->command_text);
+      command->priv->command_text = g_strdup (command_text);
+      g_object_notify_by_pspec (G_OBJECT (command),
+                                gParamSpecs [PROP_COMMAND_TEXT]);
+    }
 }
 
 static GbCommandTask *
@@ -34,7 +72,7 @@ gb_python_command_execute (GbCommand   *command,
                            GVariant    *parameters,
                            GbWorkbench *workbench)
 {
-  const gchar *command_text = "hex(10)";
+  GbPythonCommandPrivate *priv;
   GbCommandTask *task;
   PyObject *ret;
   PyObject *locals;
@@ -43,9 +81,14 @@ gb_python_command_execute (GbCommand   *command,
   g_return_val_if_fail (GB_IS_PYTHON_COMMAND (command), NULL);
   g_return_val_if_fail (GB_IS_WORKBENCH (workbench), NULL);
 
+  priv = GB_PYTHON_COMMAND (command)->priv;
+
+  if (!priv->command_text)
+    return NULL;
+
   locals = PyDict_New ();
 
-  if ((ret = PyRun_String (command_text, Py_single_input,
+  if ((ret = PyRun_String (priv->command_text, Py_single_input,
                            PyEval_GetBuiltins (), locals)))
     {
       if (!PyString_Check (ret))
@@ -67,7 +110,7 @@ gb_python_command_execute (GbCommand   *command,
 
   task = g_object_new (GB_TYPE_COMMAND_TASK,
                        "active", FALSE,
-                       "command-text", command_text,
+                       "command-text", priv->command_text,
                        "progress", 1.0,
                        "result-text", result_text,
                        NULL);
@@ -79,16 +122,71 @@ gb_python_command_execute (GbCommand   *command,
 }
 
 static void
+gb_python_command_get_property (GObject    *object,
+                                guint       prop_id,
+                                GValue     *value,
+                                GParamSpec *pspec)
+{
+  GbPythonCommand *self = (GbPythonCommand *)object;
+
+  switch (prop_id)
+    {
+    case PROP_COMMAND_TEXT:
+      g_value_set_string (value, gb_python_command_get_command_text (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_python_command_set_property (GObject      *object,
+                                guint         prop_id,
+                                const GValue *value,
+                                GParamSpec   *pspec)
+{
+  GbPythonCommand *self = (GbPythonCommand *)object;
+
+  switch (prop_id)
+    {
+    case PROP_COMMAND_TEXT:
+      gb_python_command_set_command_text (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
 gb_python_command_class_init (GbPythonCommandClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GbCommandClass *command_class = GB_COMMAND_CLASS (klass);
 
+  object_class->set_property = gb_python_command_set_property;
+  object_class->get_property = gb_python_command_get_property;
+
   command_class->execute = gb_python_command_execute;
+
+  gParamSpecs [PROP_COMMAND_TEXT] =
+    g_param_spec_string ("command-text",
+                         _("Command Text"),
+                         _("The command text for the python evaluation."),
+                         NULL,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_COMMAND_TEXT,
+                                   gParamSpecs [PROP_COMMAND_TEXT]);
 }
 
 static void
 gb_python_command_init (GbPythonCommand *self)
 {
+  self->priv = gb_python_command_get_instance_private (self);
+
   gb_command_set_name (GB_COMMAND (self), "python");
   gb_command_set_description (GB_COMMAND (self),
                               _("Run a command in Python."));
diff --git a/src/commands/gb-python-command.h b/src/commands/gb-python-command.h
index 2b2e0d2..da3f901 100644
--- a/src/commands/gb-python-command.h
+++ b/src/commands/gb-python-command.h
@@ -49,7 +49,7 @@ struct _GbPythonCommandClass
 };
 
 GType      gb_python_command_get_type (void) G_GNUC_CONST;
-GbCommand *gb_python_command_new      (void);
+GbCommand *gb_python_command_new      (const gchar *command_text);
 
 G_END_DECLS
 


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