[gnome-builder/wip/commands2: 3/12] commands: add GbCommandManager



commit fccaf3e30064cf61eadfda265a601778b327b092
Author: Christian Hergert <christian hergert me>
Date:   Thu Oct 9 00:38:49 2014 -0700

    commands: add GbCommandManager
    
    This will provide the interface for querying providers for a GAction
    from the command text.

 src/commands/gb-command-manager.c  |  138 ++++++++++++++++++++++++++++++++++++
 src/commands/gb-command-manager.h  |   64 +++++++++++++++++
 src/commands/gb-command-provider.c |    4 +
 src/gnome-builder.mk               |    2 +
 4 files changed, 208 insertions(+), 0 deletions(-)
---
diff --git a/src/commands/gb-command-manager.c b/src/commands/gb-command-manager.c
new file mode 100644
index 0000000..5844263
--- /dev/null
+++ b/src/commands/gb-command-manager.c
@@ -0,0 +1,138 @@
+/* gb-command-manager.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 "gb-command-manager.h"
+
+struct _GbCommandManagerPrivate
+{
+  GPtrArray *providers;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GbCommandManager, gb_command_manager, G_TYPE_OBJECT)
+
+GbCommandManager *
+gb_command_manager_new (void)
+{
+  return g_object_new (GB_TYPE_COMMAND_MANAGER, NULL);
+}
+
+GbCommandManager *
+gb_command_manager_get_default (void)
+{
+  static GbCommandManager *instance;
+
+  if (!instance)
+    instance = gb_command_manager_new ();
+
+  return instance;
+}
+
+static gint
+provider_compare_func (gconstpointer a,
+                       gconstpointer b)
+{
+  GbCommandProvider **p1 = (GbCommandProvider **)a;
+  GbCommandProvider **p2 = (GbCommandProvider **)b;
+  gint i1;
+  gint i2;
+
+  i1 = gb_command_provider_get_priority (*p1);
+  i2 = gb_command_provider_get_priority (*p2);
+
+  return (i1 - i2);
+}
+
+static void
+on_notify_priority_cb (GbCommandProvider *provider,
+                       GParamSpec        *pspec,
+                       GbCommandManager  *manager)
+{
+  g_return_if_fail (GB_IS_COMMAND_PROVIDER (provider));
+  g_return_if_fail (GB_IS_COMMAND_MANAGER (manager));
+
+  g_ptr_array_sort (manager->priv->providers, provider_compare_func);
+}
+
+void
+gb_command_manager_add_provider (GbCommandManager  *manager,
+                                 GbCommandProvider *provider)
+{
+  g_return_if_fail (GB_IS_COMMAND_MANAGER (manager));
+  g_return_if_fail (GB_IS_COMMAND_PROVIDER (provider));
+
+  g_signal_connect_object (provider, "notify::priority",
+                           G_CALLBACK (on_notify_priority_cb),
+                           manager, 0);
+
+  g_ptr_array_add (manager->priv->providers, g_object_ref (provider));
+  g_ptr_array_sort (manager->priv->providers, provider_compare_func);
+}
+
+GAction *
+gb_command_manager_lookup (GbCommandManager  *manager,
+                           const gchar       *command_text,
+                           GVariant         **parameters)
+{
+  GVariant *dummy = NULL;
+  GAction *ret = NULL;
+  guint i;
+
+  g_return_val_if_fail (GB_IS_COMMAND_MANAGER (manager), NULL);
+  g_return_val_if_fail (command_text, NULL);
+
+  if (!parameters)
+    parameters = &dummy;
+
+  for (i = 0; !ret && (i < manager->priv->providers->len); i++)
+    {
+      GbCommandProvider *provider;
+
+      provider = g_ptr_array_index (manager->priv->providers, i);
+      ret = gb_command_provider_lookup (provider, command_text, parameters);
+    }
+
+  g_clear_pointer (&dummy, g_variant_unref);
+
+  return ret;
+}
+
+static void
+gb_command_manager_finalize (GObject *object)
+{
+  GbCommandManagerPrivate *priv = GB_COMMAND_MANAGER (object)->priv;
+
+  g_clear_pointer (&priv->providers, g_ptr_array_unref);
+
+  G_OBJECT_CLASS (gb_command_manager_parent_class)->finalize (object);
+}
+
+static void
+gb_command_manager_class_init (GbCommandManagerClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gb_command_manager_finalize;
+}
+
+static void
+gb_command_manager_init (GbCommandManager *self)
+{
+  self->priv = gb_command_manager_get_instance_private (self);
+
+  self->priv->providers = g_ptr_array_new_with_free_func (g_object_unref);
+}
diff --git a/src/commands/gb-command-manager.h b/src/commands/gb-command-manager.h
new file mode 100644
index 0000000..baf8136
--- /dev/null
+++ b/src/commands/gb-command-manager.h
@@ -0,0 +1,64 @@
+/* gb-command-manager.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_MANAGER_H
+#define GB_COMMAND_MANAGER_H
+
+#include <gio/gio.h>
+
+#include "gb-command-provider.h"
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_COMMAND_MANAGER            (gb_command_manager_get_type())
+#define GB_COMMAND_MANAGER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GB_TYPE_COMMAND_MANAGER, 
GbCommandManager))
+#define GB_COMMAND_MANAGER_CONST(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GB_TYPE_COMMAND_MANAGER, 
GbCommandManager const))
+#define GB_COMMAND_MANAGER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GB_TYPE_COMMAND_MANAGER, 
GbCommandManagerClass))
+#define GB_IS_COMMAND_MANAGER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GB_TYPE_COMMAND_MANAGER))
+#define GB_IS_COMMAND_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GB_TYPE_COMMAND_MANAGER))
+#define GB_COMMAND_MANAGER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GB_TYPE_COMMAND_MANAGER, 
GbCommandManagerClass))
+
+typedef struct _GbCommandManager        GbCommandManager;
+typedef struct _GbCommandManagerClass   GbCommandManagerClass;
+typedef struct _GbCommandManagerPrivate GbCommandManagerPrivate;
+
+struct _GbCommandManager
+{
+  GObject parent;
+
+  /*< private >*/
+  GbCommandManagerPrivate *priv;
+};
+
+struct _GbCommandManagerClass
+{
+  GObjectClass parent;
+};
+
+GType             gb_command_manager_get_type     (void) G_GNUC_CONST;
+GbCommandManager *gb_command_manager_new          (void);
+GbCommandManager *gb_command_manager_get_default  (void);
+GAction          *gb_command_manager_lookup       (GbCommandManager   *manager,
+                                                   const gchar        *command_text,
+                                                   GVariant          **parameters);
+void              gb_command_manager_add_provider (GbCommandManager   *manager,
+                                                   GbCommandProvider  *provider);
+
+G_END_DECLS
+
+#endif /* GB_COMMAND_MANAGER_H */
diff --git a/src/commands/gb-command-provider.c b/src/commands/gb-command-provider.c
index 004a1d6..c7ff6bd 100644
--- a/src/commands/gb-command-provider.c
+++ b/src/commands/gb-command-provider.c
@@ -141,6 +141,10 @@ gb_command_provider_class_init (GbCommandProviderClass *klass)
    * queried. During the lookup process, providers are queried in order of
    * priority to parse the command text and resolve a GAction and optional
    * parameters.
+   *
+   * Lower priorities will be queried first.
+   *
+   * A negative priority is allowed;
    */
   gParamSpecs [PROP_PRIORITY] =
     g_param_spec_int ("priority",
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index 29ffd0e..c29366e 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-manager.c \
+       src/commands/gb-command-manager.h \
        src/commands/gb-command-provider.c \
        src/commands/gb-command-provider.h \
        src/devhelp/gb-devhelp-navigation-item.c \


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