[gnome-builder/wip/commands2] commands: add GbCommandProvider API.



commit e55e67e02900cac0cfcbf263b01dd777991e0d21
Author: Christian Hergert <christian hergert me>
Date:   Thu Oct 9 00:14:42 2014 -0700

    commands: add GbCommandProvider API.

 src/commands/gb-command-provider.c |  186 ++++++++++++++++++++++++++++++++++++
 src/commands/gb-command-provider.h |   72 ++++++++++++++
 src/gnome-builder.mk               |    2 +
 3 files changed, 260 insertions(+), 0 deletions(-)
---
diff --git a/src/commands/gb-command-provider.c b/src/commands/gb-command-provider.c
new file mode 100644
index 0000000..004a1d6
--- /dev/null
+++ b/src/commands/gb-command-provider.c
@@ -0,0 +1,186 @@
+/* gb-command-provider.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-provider.h"
+
+struct _GbCommandProviderPrivate
+{
+  gint priority;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GbCommandProvider, gb_command_provider,
+                            G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_PRIORITY,
+  LAST_PROP
+};
+
+enum {
+  LOOKUP,
+  LAST_SIGNAL
+};
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+static guint gSignals [LAST_SIGNAL];
+
+GbCommandProvider *
+gb_command_provider_new (void)
+{
+  return g_object_new (GB_TYPE_COMMAND_PROVIDER, NULL);
+}
+
+gint
+gb_command_provider_get_priority (GbCommandProvider *provider)
+{
+  g_return_val_if_fail (GB_IS_COMMAND_PROVIDER (provider), 0);
+
+  return provider->priv->priority;
+}
+
+void
+gb_command_provider_set_priority (GbCommandProvider *provider,
+                                  gint               priority)
+{
+  g_return_if_fail (GB_IS_COMMAND_PROVIDER (provider));
+
+  if (provider->priv->priority != priority)
+    {
+      provider->priv->priority = priority;
+      g_object_notify_by_pspec (G_OBJECT (provider),
+                                gParamSpecs [PROP_PRIORITY]);
+    }
+}
+
+GAction *
+gb_command_provider_lookup (GbCommandProvider  *provider,
+                            const gchar        *command_text,
+                            GVariant          **parameter)
+{
+  GAction *ret = NULL;
+
+  g_return_val_if_fail (GB_IS_COMMAND_PROVIDER (provider), NULL);
+  g_return_val_if_fail (command_text, NULL);
+
+  if (parameter)
+    *parameter = NULL;
+
+  g_signal_emit (provider, gSignals [LOOKUP], 0, command_text, provider, &ret);
+
+  return ret;
+}
+
+static void
+gb_command_provider_get_property (GObject    *object,
+                                  guint       prop_id,
+                                  GValue     *value,
+                                  GParamSpec *pspec)
+{
+  GbCommandProvider *self = GB_COMMAND_PROVIDER (object);
+
+  switch (prop_id)
+    {
+    case PROP_PRIORITY:
+      g_value_set_int (value, gb_command_provider_get_priority (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_command_provider_set_property (GObject      *object,
+                                  guint         prop_id,
+                                  const GValue *value,
+                                  GParamSpec   *pspec)
+{
+  GbCommandProvider *self = GB_COMMAND_PROVIDER (object);
+
+  switch (prop_id)
+    {
+    case PROP_PRIORITY:
+      gb_command_provider_set_priority (self, g_value_get_int (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gb_command_provider_class_init (GbCommandProviderClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = gb_command_provider_get_property;
+  object_class->set_property = gb_command_provider_set_property;
+
+  /**
+   * GbCommandProvider:priority:
+   *
+   * The priority property denotes the order in which providers should be
+   * queried. During the lookup process, providers are queried in order of
+   * priority to parse the command text and resolve a GAction and optional
+   * parameters.
+   */
+  gParamSpecs [PROP_PRIORITY] =
+    g_param_spec_int ("priority",
+                      _("Priority"),
+                      _("The priority of the command provider."),
+                      G_MININT,
+                      G_MAXINT,
+                      0,
+                      (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_PRIORITY,
+                                   gParamSpecs [PROP_PRIORITY]);
+
+  /**
+   * GbCommandProvider::lookup:
+   * @command_text: (in): the command line text to be processed.
+   * @parameter: (out): a location to store any parsed parameters.
+   *
+   * This signal is emitted when a request to parse the command text is
+   * received. Only the first handler will respond to the action. The
+   * callee should return a GAction if successful, otherwise %NULL.
+   * 
+   * If successful, the callee can set @parameter, to specify the
+   * parameters that should be passed to the resulting action.
+   */
+  gSignals [LOOKUP] =
+    g_signal_new ("lookup",
+                  GB_TYPE_COMMAND_PROVIDER,
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (GbCommandProviderClass, lookup),
+                  g_signal_accumulator_first_wins,
+                  NULL,
+                  NULL,
+                  G_TYPE_ACTION,
+                  2,
+                  G_TYPE_STRING,
+                  G_TYPE_POINTER);
+}
+
+static void
+gb_command_provider_init (GbCommandProvider *self)
+{
+  self->priv = gb_command_provider_get_instance_private (self);
+}
diff --git a/src/commands/gb-command-provider.h b/src/commands/gb-command-provider.h
new file mode 100644
index 0000000..3540530
--- /dev/null
+++ b/src/commands/gb-command-provider.h
@@ -0,0 +1,72 @@
+/* gb-command-provider.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_PROVIDER_H
+#define GB_COMMAND_PROVIDER_H
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_COMMAND_PROVIDER            (gb_command_provider_get_type())
+#define GB_COMMAND_PROVIDER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GB_TYPE_COMMAND_PROVIDER, 
GbCommandProvider))
+#define GB_COMMAND_PROVIDER_CONST(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GB_TYPE_COMMAND_PROVIDER, 
GbCommandProvider const))
+#define GB_COMMAND_PROVIDER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GB_TYPE_COMMAND_PROVIDER, 
GbCommandProviderClass))
+#define GB_IS_COMMAND_PROVIDER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GB_TYPE_COMMAND_PROVIDER))
+#define GB_IS_COMMAND_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GB_TYPE_COMMAND_PROVIDER))
+#define GB_COMMAND_PROVIDER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GB_TYPE_COMMAND_PROVIDER, 
GbCommandProviderClass))
+
+typedef struct _GbCommandProvider        GbCommandProvider;
+typedef struct _GbCommandProviderClass   GbCommandProviderClass;
+typedef struct _GbCommandProviderPrivate GbCommandProviderPrivate;
+
+struct _GbCommandProvider
+{
+  GObject parent;
+
+  /*< private >*/
+  GbCommandProviderPrivate *priv;
+};
+
+struct _GbCommandProviderClass
+{
+  GObjectClass parent;
+
+  GAction *(*lookup) (GbCommandProvider  *provider,
+                      const gchar        *command_text,
+                      GVariant          **parameters);
+
+  gpointer _padding1;
+  gpointer _padding2;
+  gpointer _padding3;
+  gpointer _padding4;
+  gpointer _padding5;
+};
+
+GType              gb_command_provider_get_type     (void) G_GNUC_CONST;
+GbCommandProvider *gb_command_provider_new          (void);
+gint               gb_command_provider_get_priority (GbCommandProvider  *provider);
+void               gb_command_provider_set_priority (GbCommandProvider  *provider,
+                                                     gint                priority);
+GAction           *gb_command_provider_lookup       (GbCommandProvider  *provider,
+                                                     const gchar        *command_text,
+                                                     GVariant          **parameters);
+
+G_END_DECLS
+
+#endif /* GB_COMMAND_PROVIDER_H */
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index 405e02e..29ffd0e 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -9,6 +9,8 @@ libgnome_builder_la_SOURCES = \
        src/animation/gb-frame-source.h \
        src/app/gb-application.c \
        src/app/gb-application.h \
+       src/commands/gb-command-provider.c \
+       src/commands/gb-command-provider.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]