[gnome-builder/wip/chergert/dspy] dspy: start on dspy plugin



commit 4af651531b2493805c9fbae9ee1c44da050db9cd
Author: Christian Hergert <chergert redhat com>
Date:   Thu Apr 11 14:41:12 2019 -0700

    dspy: start on dspy plugin

 meson_options.txt                           |   1 +
 src/plugins/dspy/dspy-connection-model.c    | 365 ++++++++++++++++++++++++++++
 src/plugins/dspy/dspy-connection-model.h    |  36 +++
 src/plugins/dspy/dspy-name.c                | 164 +++++++++++++
 src/plugins/dspy/dspy-name.h                |  41 ++++
 src/plugins/dspy/dspy-plugin.c              |  34 +++
 src/plugins/dspy/dspy.gresource.xml         |   8 +
 src/plugins/dspy/dspy.plugin                |   9 +
 src/plugins/dspy/gbp-dspy-surface.c         | 178 ++++++++++++++
 src/plugins/dspy/gbp-dspy-surface.h         |  33 +++
 src/plugins/dspy/gbp-dspy-surface.ui        |  46 ++++
 src/plugins/dspy/gbp-dspy-workspace-addin.c |  92 +++++++
 src/plugins/dspy/gbp-dspy-workspace-addin.h |  31 +++
 src/plugins/dspy/gtk/menus.ui               |  15 ++
 src/plugins/dspy/meson.build                |  19 ++
 src/plugins/meson.build                     |   2 +
 16 files changed, 1074 insertions(+)
---
diff --git a/meson_options.txt b/meson_options.txt
index 57fe9301c..3d6fc2e40 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -31,6 +31,7 @@ option('plugin_color_picker', type: 'boolean')
 option('plugin_ctags', type: 'boolean')
 option('plugin_devhelp', type: 'boolean')
 option('plugin_deviced', type: 'boolean', value: false)
+option('plugin_dspy', type: 'boolean')
 option('plugin_editorconfig', type: 'boolean')
 option('plugin_eslint', type: 'boolean')
 option('plugin_file_search', type: 'boolean')
diff --git a/src/plugins/dspy/dspy-connection-model.c b/src/plugins/dspy/dspy-connection-model.c
new file mode 100644
index 000000000..68f2d6ad9
--- /dev/null
+++ b/src/plugins/dspy/dspy-connection-model.c
@@ -0,0 +1,365 @@
+/* dspy-connection-model.c
+ *
+ * Copyright 2019 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "dspy-connection-model"
+
+#include "config.h"
+
+#include "dspy-connection-model.h"
+#include "dspy-name.h"
+
+struct _DspyConnectionModel
+{
+  GObject          parent;
+
+  GDBusConnection *connection;
+  GCancellable    *cancellable;
+  GSequence       *peers;
+  GDBusProxy      *bus_proxy;
+
+  guint            name_owner_changed_handler;
+};
+
+enum {
+  PROP_0,
+  PROP_CONNECTION,
+  N_PROPS
+};
+
+static guint
+dspy_connection_model_get_n_items (GListModel *model)
+{
+  DspyConnectionModel *self = DSPY_CONNECTION_MODEL (model);
+  return g_sequence_get_length (self->peers);
+}
+
+static GType
+dspy_connection_model_get_item_type (GListModel *model)
+{
+  /* XXX: switch to type */
+  return G_TYPE_OBJECT;
+}
+
+static gpointer
+dspy_connection_model_get_item (GListModel *model,
+                                guint       position)
+{
+  DspyConnectionModel *self = DSPY_CONNECTION_MODEL (model);
+  GSequenceIter *iter = g_sequence_get_iter_at_pos (self->peers, position);
+
+  if (g_sequence_iter_is_end (iter))
+    return NULL;
+
+  return g_object_ref (g_sequence_get (iter));
+}
+
+static void
+list_model_iface_init (GListModelInterface *iface)
+{
+  iface->get_item_type = dspy_connection_model_get_item_type;
+  iface->get_n_items = dspy_connection_model_get_n_items;
+  iface->get_item = dspy_connection_model_get_item;
+}
+
+G_DEFINE_TYPE_WITH_CODE (DspyConnectionModel, dspy_connection_model, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+dspy_connection_model_finalize (GObject *object)
+{
+  DspyConnectionModel *self = (DspyConnectionModel *)object;
+
+  g_assert (self->connection == NULL);
+  g_assert (self->cancellable == NULL);
+
+  g_clear_object (&self->connection);
+  g_clear_object (&self->cancellable);
+  g_clear_pointer (&self->peers, g_sequence_free);
+
+  G_OBJECT_CLASS (dspy_connection_model_parent_class)->finalize (object);
+}
+
+static void
+dspy_connection_model_dispose (GObject *object)
+{
+  DspyConnectionModel *self = (DspyConnectionModel *)object;
+
+  dspy_connection_model_set_connection (self, NULL);
+
+  G_OBJECT_CLASS (dspy_connection_model_parent_class)->dispose (object);
+}
+
+static void
+dspy_connection_model_get_property (GObject    *object,
+                                    guint       prop_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+  DspyConnectionModel *self = DSPY_CONNECTION_MODEL (object);
+
+  switch (prop_id)
+    {
+    case PROP_CONNECTION:
+      g_value_set_object (value, dspy_connection_model_get_connection (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+dspy_connection_model_set_property (GObject      *object,
+                                    guint         prop_id,
+                                    const GValue *value,
+                                    GParamSpec   *pspec)
+{
+  DspyConnectionModel *self = DSPY_CONNECTION_MODEL (object);
+
+  switch (prop_id)
+    {
+    case PROP_CONNECTION:
+      dspy_connection_model_set_connection (self, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+dspy_connection_model_class_init (DspyConnectionModelClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = dspy_connection_model_dispose;
+  object_class->finalize = dspy_connection_model_finalize;
+  object_class->get_property = dspy_connection_model_get_property;
+  object_class->set_property = dspy_connection_model_set_property;
+
+  /**
+   * DspyConnectionModel:connection:
+   *
+   * The "connection" property contains the #GDBusConnection that will be monitored
+   * for changes to the bus.
+   */
+  properties [PROP_CONNECTION] =
+    g_param_spec_object ("connection",
+                         "Connection",
+                         "A GDBus connection for the source of bus changes",
+                         G_TYPE_DBUS_CONNECTION,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+dspy_connection_model_init (DspyConnectionModel *self)
+{
+  self->peers = g_sequence_new (g_object_unref);
+}
+
+static void
+dspy_connection_model_name_owner_changed_cb (GDBusConnection *connection,
+                                             const gchar     *sender_name,
+                                             const gchar     *object_path,
+                                             const gchar     *interface_name,
+                                             const gchar     *signal_name,
+                                             GVariant        *params,
+                                             gpointer         user_data)
+{
+  DspyConnectionModel *self = user_data;
+
+  g_assert (G_IS_DBUS_CONNECTION (connection));
+  g_assert (DSPY_IS_CONNECTION_MODEL (self));
+
+}
+
+static void
+dspy_connection_model_add_names (DspyConnectionModel *self,
+                                 GVariant            *names)
+{
+  const gchar * const *strv = NULL;
+  GVariantIter iter;
+
+  g_assert (DSPY_IS_CONNECTION_MODEL (self));
+  g_assert (names != NULL);
+
+  g_variant_iter_init (&iter, names);
+  if (g_variant_iter_next (&iter, "^as", &strv))
+    {
+      for (guint i = 0; strv[i]; i++)
+        {
+          GSequenceIter *seq;
+
+          seq = g_sequence_insert_sorted (self->peers,
+                                          dspy_name_new (strv[i]),
+                                          (GCompareDataFunc) dspy_name_compare,
+                                          NULL);
+          g_list_model_items_changed (G_LIST_MODEL (self),
+                                      g_sequence_iter_get_position (seq),
+                                      0, 1);
+        }
+    }
+}
+
+static void
+dspy_connection_model_list_activatable_names_cb (GObject      *object,
+                                                 GAsyncResult *result,
+                                                 gpointer      user_data)
+{
+  GDBusProxy *proxy = (GDBusProxy *)object;
+  g_autoptr(DspyConnectionModel) self = user_data;
+  g_autoptr(GVariant) ret = NULL;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (G_IS_DBUS_PROXY (proxy));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (DSPY_IS_CONNECTION_MODEL (self));
+
+  if (!(ret = g_dbus_proxy_call_finish (proxy, result, &error)))
+    g_warning ("Failed to list activatable names: %s", error->message);
+  else
+    dspy_connection_model_add_names (self, ret);
+}
+
+static void
+dspy_connection_model_list_names_cb (GObject      *object,
+                                     GAsyncResult *result,
+                                     gpointer      user_data)
+{
+  GDBusProxy *proxy = (GDBusProxy *)object;
+  g_autoptr(DspyConnectionModel) self = user_data;
+  g_autoptr(GVariant) ret = NULL;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (G_IS_DBUS_PROXY (proxy));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (DSPY_IS_CONNECTION_MODEL (self));
+
+  if (!(ret = g_dbus_proxy_call_finish (proxy, result, &error)))
+    g_warning ("Failed to list names: %s", error->message);
+  else
+    dspy_connection_model_add_names (self, ret);
+}
+
+DspyConnectionModel *
+dspy_connection_model_new (void)
+{
+  return g_object_new (DSPY_TYPE_CONNECTION_MODEL, NULL);
+}
+
+/**
+ * dspy_connection_model_get_connection:
+ * @self: a #DspyConnectionModel
+ *
+ * Gets the #GDBusConnection used for the model.
+ *
+ * Returns: (transfer none) (nullable): a #GDBusConnection or %NULL
+ */
+GDBusConnection *
+dspy_connection_model_get_connection (DspyConnectionModel *self)
+{
+  g_return_val_if_fail (DSPY_IS_CONNECTION_MODEL (self), NULL);
+
+  return self->connection;
+}
+
+void
+dspy_connection_model_set_connection (DspyConnectionModel *self,
+                                      GDBusConnection     *connection)
+{
+  g_return_if_fail (DSPY_IS_CONNECTION_MODEL (self));
+
+  if (self->connection == connection)
+    return;
+
+  if (self->connection != NULL)
+    {
+      g_cancellable_cancel (self->cancellable);
+      g_dbus_connection_signal_unsubscribe (self->connection,
+                                            self->name_owner_changed_handler);
+      g_clear_object (&self->cancellable);
+      g_clear_object (&self->connection);
+      g_clear_object (&self->bus_proxy);
+      g_clear_pointer (&self->peers, g_sequence_free);
+      self->peers = g_sequence_new (g_object_unref);
+    }
+
+  g_assert (self->cancellable == NULL);
+  g_assert (self->connection == NULL);
+  g_assert (self->bus_proxy == NULL);
+  g_assert (g_sequence_is_empty (self->peers));
+
+  if (connection != NULL)
+    {
+      g_autoptr(GError) error = NULL;
+
+      self->connection = g_object_ref (connection);
+      self->cancellable = g_cancellable_new ();
+      self->name_owner_changed_handler =
+        g_dbus_connection_signal_subscribe (self->connection,
+                                            NULL,
+                                            "org.freedesktop.DBus",
+                                            "NameOwnerChanged",
+                                            NULL,
+                                            NULL,
+                                            0,
+                                            dspy_connection_model_name_owner_changed_cb,
+                                            self,
+                                            NULL);
+      self->bus_proxy = g_dbus_proxy_new_sync (self->connection,
+                                               G_DBUS_PROXY_FLAGS_NONE,
+                                               NULL,
+                                               "org.freedesktop.DBus",
+                                               "/org/freedesktop/DBus",
+                                               "org.freedesktop.DBus",
+                                               self->cancellable, &error);
+
+      if (self->bus_proxy == NULL)
+        {
+          g_warning ("Failed to create DBus proxy: %s", error->message);
+          goto notify;
+        }
+
+      g_dbus_proxy_call (self->bus_proxy,
+                         "ListActivatableNames",
+                         g_variant_new ("()"),
+                         G_DBUS_CALL_FLAGS_NONE,
+                         -1,
+                         self->cancellable,
+                         dspy_connection_model_list_activatable_names_cb,
+                         g_object_ref (self));
+
+      g_dbus_proxy_call (self->bus_proxy,
+                         "ListNames",
+                         g_variant_new ("()"),
+                         G_DBUS_CALL_FLAGS_NONE,
+                         -1,
+                         self->cancellable,
+                         dspy_connection_model_list_names_cb,
+                         g_object_ref (self));
+    }
+
+notify:
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CONNECTION]);
+}
diff --git a/src/plugins/dspy/dspy-connection-model.h b/src/plugins/dspy/dspy-connection-model.h
new file mode 100644
index 000000000..dd5083efa
--- /dev/null
+++ b/src/plugins/dspy/dspy-connection-model.h
@@ -0,0 +1,36 @@
+/* dspy-connection-model.h
+ *
+ * Copyright 2019 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define DSPY_TYPE_CONNECTION_MODEL (dspy_connection_model_get_type())
+
+G_DECLARE_FINAL_TYPE (DspyConnectionModel, dspy_connection_model, DSPY, CONNECTION_MODEL, GObject)
+
+DspyConnectionModel *dspy_connection_model_new            (void);
+GDBusConnection     *dspy_connection_model_get_connection (DspyConnectionModel *self);
+void                 dspy_connection_model_set_connection (DspyConnectionModel *self,
+                                                           GDBusConnection     *connection);
+
+G_END_DECLS
diff --git a/src/plugins/dspy/dspy-name.c b/src/plugins/dspy/dspy-name.c
new file mode 100644
index 000000000..cee45d1d2
--- /dev/null
+++ b/src/plugins/dspy/dspy-name.c
@@ -0,0 +1,164 @@
+/* dspy-name.c
+ *
+ * Copyright 2019 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "dspy-name"
+
+#include "dspy-name.h"
+
+typedef struct
+{
+  gchar *name;
+} DspyNamePrivate;
+
+enum {
+  PROP_0,
+  PROP_NAME,
+  N_PROPS
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (DspyName, dspy_name, G_TYPE_OBJECT)
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+dspy_name_finalize (GObject *object)
+{
+  DspyName *self = (DspyName *)object;
+  DspyNamePrivate *priv = dspy_name_get_instance_private (self);
+
+  g_clear_pointer (&priv->name, g_free);
+
+  G_OBJECT_CLASS (dspy_name_parent_class)->finalize (object);
+}
+
+static void
+dspy_name_get_property (GObject    *object,
+                        guint       prop_id,
+                        GValue     *value,
+                        GParamSpec *pspec)
+{
+  DspyName *self = DSPY_NAME (object);
+
+  switch (prop_id)
+    {
+    case PROP_NAME:
+      g_value_set_string (value, dspy_name_get_name (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+dspy_name_set_property (GObject      *object,
+                        guint         prop_id,
+                        const GValue *value,
+                        GParamSpec   *pspec)
+{
+  DspyName *self = DSPY_NAME (object);
+  DspyNamePrivate *priv = dspy_name_get_instance_private (self);
+
+  switch (prop_id)
+    {
+    case PROP_NAME:
+      priv->name = g_value_dup_string (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+dspy_name_class_init (DspyNameClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = dspy_name_finalize;
+  object_class->get_property = dspy_name_get_property;
+  object_class->set_property = dspy_name_set_property;
+
+  properties [PROP_NAME] =
+    g_param_spec_string ("name",
+                         "Name",
+                         "The peer name",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+dspy_name_init (DspyName *self)
+{
+}
+
+DspyName *
+dspy_name_new (const gchar *name)
+{
+  return g_object_new (DSPY_TYPE_NAME,
+                       "name", name,
+                       NULL);
+}
+
+const gchar *
+dspy_name_get_name (DspyName *self)
+{
+  DspyNamePrivate *priv = dspy_name_get_instance_private (self);
+
+  return priv->name;
+}
+
+void
+dspy_name_set_name (DspyName    *self,
+                    const gchar *name)
+{
+  DspyNamePrivate *priv = dspy_name_get_instance_private (self);
+
+  g_return_if_fail (DSPY_IS_NAME (self));
+
+  if (g_strcmp0 (name, priv->name) != 0)
+    {
+      g_free (priv->name);
+      priv->name = g_strdup (name);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_NAME]);
+    }
+}
+
+gint
+dspy_name_compare (gconstpointer a,
+                   gconstpointer b)
+{
+  DspyName *item1 = DSPY_NAME ((gpointer)a);
+  DspyName *item2 = DSPY_NAME ((gpointer)b);
+  const gchar *name1 = dspy_name_get_name (item1);
+  const gchar *name2 = dspy_name_get_name (item2);
+
+  if (name1[0] != name2[0])
+    {
+      if (name1[0] == ':')
+        return 1;
+      if (name2[0] == ':')
+        return -1;
+    }
+
+  return g_strcmp0 (name1, name2);
+}
diff --git a/src/plugins/dspy/dspy-name.h b/src/plugins/dspy/dspy-name.h
new file mode 100644
index 000000000..48e95108c
--- /dev/null
+++ b/src/plugins/dspy/dspy-name.h
@@ -0,0 +1,41 @@
+/* dspy-name.h
+ *
+ * Copyright 2019 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define DSPY_TYPE_NAME (dspy_name_get_type())
+
+G_DECLARE_DERIVABLE_TYPE (DspyName, dspy_name, DSPY, NAME, GObject)
+
+struct _DspyNameClass
+{
+  GObjectClass parent_class;
+};
+
+DspyName    *dspy_name_new      (const gchar   *name);
+const gchar *dspy_name_get_name (DspyName      *self);
+gint         dspy_name_compare  (gconstpointer  a,
+                                 gconstpointer  b);
+
+G_END_DECLS
diff --git a/src/plugins/dspy/dspy-plugin.c b/src/plugins/dspy/dspy-plugin.c
new file mode 100644
index 000000000..85bd90efc
--- /dev/null
+++ b/src/plugins/dspy/dspy-plugin.c
@@ -0,0 +1,34 @@
+/* dspy-plugin.c
+ *
+ * Copyright 2019 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include "config.h"
+
+#include <libide-editor.h>
+#include <libpeas/peas.h>
+
+#include "gbp-dspy-workspace-addin.h"
+
+_IDE_EXTERN void
+_gbp_dspy_register_types (PeasObjectModule *module)
+{
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_WORKSPACE_ADDIN,
+                                              GBP_TYPE_DSPY_WORKSPACE_ADDIN);
+}
diff --git a/src/plugins/dspy/dspy.gresource.xml b/src/plugins/dspy/dspy.gresource.xml
new file mode 100644
index 000000000..613633bb3
--- /dev/null
+++ b/src/plugins/dspy/dspy.gresource.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/plugins/dspy">
+    <file>dspy.plugin</file>
+    <file>gtk/menus.ui</file>
+    <file preprocess="xml-stripblanks">gbp-dspy-surface.ui</file>
+  </gresource>
+</gresources>
diff --git a/src/plugins/dspy/dspy.plugin b/src/plugins/dspy/dspy.plugin
new file mode 100644
index 000000000..b98a209a9
--- /dev/null
+++ b/src/plugins/dspy/dspy.plugin
@@ -0,0 +1,9 @@
+[Plugin]
+Authors=Christian Hergert <christian hergert me>
+Builtin=true
+Copyright=Copyright © 2019 Christian Hergert
+Description=Explore DBus session and system connections
+Embedded=_gbp_dspy_register_types
+Module=dspy
+Name=DBus Connection Explorer
+X-Workspace-Kind=primary;editor;
diff --git a/src/plugins/dspy/gbp-dspy-surface.c b/src/plugins/dspy/gbp-dspy-surface.c
new file mode 100644
index 000000000..3c3fd86cf
--- /dev/null
+++ b/src/plugins/dspy/gbp-dspy-surface.c
@@ -0,0 +1,178 @@
+/* gbp-dspy-surface.c
+ *
+ * Copyright 2019 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-dspy-surface"
+
+#include "config.h"
+
+#include <dazzle.h>
+#include <glib/gi18n.h>
+
+#include "dspy-connection-model.h"
+#include "dspy-name.h"
+
+#include "gbp-dspy-surface.h"
+
+struct _GbpDspySurface
+{
+  IdeSurface parent_instance;
+
+  GtkListBox        *connections_list_box;
+  GtkScrolledWindow *connections_scroller;
+  DzlMultiPaned     *multi_paned;
+  GtkScrolledWindow *names_scroller;
+  GtkListBox        *names_list_box;
+};
+
+G_DEFINE_TYPE (GbpDspySurface, gbp_dspy_surface, IDE_TYPE_SURFACE)
+
+static GtkWidget *
+create_names_row (gpointer item,
+                  gpointer user_data)
+{
+  DspyName *name = item;
+  GtkWidget *row;
+  GtkWidget *label;
+
+  g_assert (DSPY_IS_NAME (name));
+
+  row = g_object_new (GTK_TYPE_LIST_BOX_ROW,
+                      "visible", TRUE,
+                      NULL);
+  label = g_object_new (GTK_TYPE_LABEL,
+                        "label", dspy_name_get_name (name),
+                        "xalign", 0.0f,
+                        "margin", 6,
+                        "visible", TRUE,
+                        NULL);
+  gtk_container_add (GTK_CONTAINER (row), GTK_WIDGET (label));
+
+  return row;
+}
+
+static void
+connection_row_activated_cb (GbpDspySurface *self,
+                             GtkListBoxRow  *row,
+                             GtkListBox     *list_box)
+{
+  g_autoptr(GDBusConnection) bus = NULL;
+  g_autoptr(DspyConnectionModel) model = NULL;
+  const gchar *addr;
+
+  g_assert (GBP_IS_DSPY_SURFACE (self));
+  g_assert (GTK_IS_LIST_BOX_ROW (row));
+  g_assert (GTK_IS_LIST_BOX (list_box));
+
+  if ((addr = g_object_get_data (G_OBJECT (row), "ADDRESS")))
+    bus = g_dbus_connection_new_for_address_sync (addr,
+                                                  (G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION |
+                                                   G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING),
+                                                  NULL, NULL, NULL);
+  else
+    bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
+
+  model = dspy_connection_model_new ();
+  dspy_connection_model_set_connection (model, bus);
+
+  gtk_list_box_bind_model (self->names_list_box,
+                           G_LIST_MODEL (model),
+                           create_names_row, NULL, NULL);
+}
+
+static void
+add_connection (GbpDspySurface *self,
+                const gchar    *name,
+                const gchar    *addr)
+{
+  GtkWidget *row;
+  GtkWidget *title;
+
+  g_assert (GBP_IS_DSPY_SURFACE (self));
+
+  row = g_object_new (GTK_TYPE_LIST_BOX_ROW,
+                      "visible", TRUE,
+                      NULL);
+  g_object_set_data_full (G_OBJECT (row), "ADDRESS", g_strdup (addr), g_free);
+  title = g_object_new (GTK_TYPE_LABEL,
+                        "margin", 6,
+                        "visible", TRUE,
+                        "label", name,
+                        "xalign", 0.0f,
+                        NULL);
+  gtk_container_add (GTK_CONTAINER (row), title);
+  gtk_container_add (GTK_CONTAINER (self->connections_list_box), row);
+}
+
+static void
+gbp_dspy_surface_finalize (GObject *object)
+{
+  G_OBJECT_CLASS (gbp_dspy_surface_parent_class)->finalize (object);
+}
+
+static void
+gbp_dspy_surface_class_init (GbpDspySurfaceClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->finalize = gbp_dspy_surface_finalize;
+
+  gtk_widget_class_set_template_from_resource (widget_class, "/plugins/dspy/gbp-dspy-surface.ui");
+  gtk_widget_class_bind_template_child (widget_class, GbpDspySurface, connections_list_box);
+  gtk_widget_class_bind_template_child (widget_class, GbpDspySurface, connections_scroller);
+  gtk_widget_class_bind_template_child (widget_class, GbpDspySurface, multi_paned);
+  gtk_widget_class_bind_template_child (widget_class, GbpDspySurface, names_list_box);
+  gtk_widget_class_bind_template_child (widget_class, GbpDspySurface, names_scroller);
+}
+
+static void
+gbp_dspy_surface_init (GbpDspySurface *self)
+{
+  const gchar *bus;
+
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  gtk_widget_set_name (GTK_WIDGET (self), "dspy");
+  ide_surface_set_icon_name (IDE_SURFACE (self), "edit-find-symbolic");
+  ide_surface_set_title (IDE_SURFACE (self), _("DBus Inspector"));
+
+  gtk_container_child_set (GTK_CONTAINER (self->multi_paned), GTK_WIDGET (self->connections_scroller),
+                           "position", 200,
+                           NULL);
+  gtk_container_child_set (GTK_CONTAINER (self->multi_paned), GTK_WIDGET (self->names_scroller),
+                           "position", 300,
+                           NULL);
+
+  g_signal_connect_object (self->connections_list_box,
+                           "row-activated",
+                           G_CALLBACK (connection_row_activated_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  add_connection (self, _("System Bus"), NULL);
+  if ((bus = g_getenv ("DBUS_SESSION_BUS_ADDRESS")))
+    add_connection (self, _("Session Bus"), bus);
+}
+
+GbpDspySurface *
+gbp_dspy_surface_new (void)
+{
+  return g_object_new (GBP_TYPE_DSPY_SURFACE, NULL);
+}
diff --git a/src/plugins/dspy/gbp-dspy-surface.h b/src/plugins/dspy/gbp-dspy-surface.h
new file mode 100644
index 000000000..2ec6a35f7
--- /dev/null
+++ b/src/plugins/dspy/gbp-dspy-surface.h
@@ -0,0 +1,33 @@
+/* gbp-dspy-surface.h
+ *
+ * Copyright 2019 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <libide-gui.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_DSPY_SURFACE (gbp_dspy_surface_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpDspySurface, gbp_dspy_surface, GBP, DSPY_SURFACE, IdeSurface)
+
+GbpDspySurface *gbp_dspy_surface_new (void);
+
+G_END_DECLS
diff --git a/src/plugins/dspy/gbp-dspy-surface.ui b/src/plugins/dspy/gbp-dspy-surface.ui
new file mode 100644
index 000000000..e95ffe1af
--- /dev/null
+++ b/src/plugins/dspy/gbp-dspy-surface.ui
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GbpDspySurface" parent="IdeSurface">
+    <child>
+      <object class="DzlMultiPaned" id="multi_paned">
+        <property name="orientation">horizontal</property>
+        <property name="visible">true</property>
+        <child>
+          <object class="GtkScrolledWindow" id="connections_scroller">
+            <property name="hscrollbar-policy">never</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkListBox" id="connections_list_box">
+                <property name="selection-mode">browse</property>
+                <property name="visible">true</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkScrolledWindow" id="names_scroller">
+            <property name="hscrollbar-policy">never</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkListBox" id="names_list_box">
+                <property name="selection-mode">browse</property>
+                <property name="visible">true</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkScrolledWindow">
+            <property name="hexpand">true</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkTreeView">
+                <property name="visible">true</property>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/plugins/dspy/gbp-dspy-workspace-addin.c b/src/plugins/dspy/gbp-dspy-workspace-addin.c
new file mode 100644
index 000000000..4ddc43e18
--- /dev/null
+++ b/src/plugins/dspy/gbp-dspy-workspace-addin.c
@@ -0,0 +1,92 @@
+/* gbp-dspy-workspace-addin.c
+ *
+ * Copyright 2019 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-dspy-workspace-addin"
+
+#include <libide-editor.h>
+
+#include "dspy-connection-model.h"
+
+#include "gbp-dspy-surface.h"
+#include "gbp-dspy-workspace-addin.h"
+
+struct _GbpDspyWorkspaceAddin
+{
+  GObject              parent_instance;
+  DspyConnectionModel *model;
+  GbpDspySurface      *surface;
+};
+
+static void
+gbp_dspy_workspace_addin_load (IdeWorkspaceAddin *addin,
+                               IdeWorkspace      *workspace)
+{
+  GbpDspyWorkspaceAddin *self = (GbpDspyWorkspaceAddin *)addin;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_DSPY_WORKSPACE_ADDIN (addin));
+  g_assert (IDE_IS_PRIMARY_WORKSPACE (workspace) || IDE_IS_EDITOR_WORKSPACE (workspace));
+
+  self->surface = gbp_dspy_surface_new ();
+  g_signal_connect (self->surface,
+                    "destroy",
+                    G_CALLBACK (gtk_widget_destroyed),
+                    &self->surface);
+  ide_workspace_add_surface (workspace, IDE_SURFACE (self->surface));
+  gtk_widget_show (GTK_WIDGET (self->surface));
+
+  //self->model = dspy_connection_model_new ();
+  //dspy_connection_model_set_connection (self->model, g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL));
+}
+
+static void
+gbp_dspy_workspace_addin_unload (IdeWorkspaceAddin *addin,
+                                 IdeWorkspace      *workspace)
+{
+  GbpDspyWorkspaceAddin *self = (GbpDspyWorkspaceAddin *)addin;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_DSPY_WORKSPACE_ADDIN (addin));
+  g_assert (IDE_IS_PRIMARY_WORKSPACE (workspace) || IDE_IS_EDITOR_WORKSPACE (workspace));
+
+  if (self->surface != NULL)
+    gtk_widget_destroy (GTK_WIDGET (self->surface));
+
+}
+
+static void
+workspace_addin_iface_init (IdeWorkspaceAddinInterface *iface)
+{
+  iface->load = gbp_dspy_workspace_addin_load;
+  iface->unload = gbp_dspy_workspace_addin_unload;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GbpDspyWorkspaceAddin, gbp_dspy_workspace_addin, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_WORKSPACE_ADDIN, workspace_addin_iface_init))
+
+static void
+gbp_dspy_workspace_addin_class_init (GbpDspyWorkspaceAddinClass *klass)
+{
+}
+
+static void
+gbp_dspy_workspace_addin_init (GbpDspyWorkspaceAddin *self)
+{
+}
diff --git a/src/plugins/dspy/gbp-dspy-workspace-addin.h b/src/plugins/dspy/gbp-dspy-workspace-addin.h
new file mode 100644
index 000000000..e4cfec50e
--- /dev/null
+++ b/src/plugins/dspy/gbp-dspy-workspace-addin.h
@@ -0,0 +1,31 @@
+/* gbp-dspy-workspace-addin.h
+ *
+ * Copyright 2019 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <libide-gui.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_DSPY_WORKSPACE_ADDIN (gbp_dspy_workspace_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpDspyWorkspaceAddin, gbp_dspy_workspace_addin, GBP, DSPY_WORKSPACE_ADDIN, GObject)
+
+G_END_DECLS
diff --git a/src/plugins/dspy/gtk/menus.ui b/src/plugins/dspy/gtk/menus.ui
new file mode 100644
index 000000000..03ae767e4
--- /dev/null
+++ b/src/plugins/dspy/gtk/menus.ui
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <menu id="ide-primary-workspace-surfaces-menu">
+    <section id="ide-primary-workspace-surfaces-menu-section">
+      <item>
+        <attribute name="id">surface-menu-dspy</attribute>
+        <attribute name="label" translatable="yes">DBus Inspector</attribute>
+        <attribute name="role">normal</attribute>
+        <attribute name="action">win.surface</attribute>
+        <attribute name="target">dspy</attribute>
+        <attribute name="verb-icon-name">edit-find-symbolic</attribute>
+      </item>
+    </section>
+  </menu>
+</interface>
diff --git a/src/plugins/dspy/meson.build b/src/plugins/dspy/meson.build
new file mode 100644
index 000000000..3abcdfeaf
--- /dev/null
+++ b/src/plugins/dspy/meson.build
@@ -0,0 +1,19 @@
+if get_option('plugin_dspy')
+
+plugins_sources += files([
+  'dspy-plugin.c',
+  'dspy-connection-model.c',
+  'dspy-name.c',
+  'gbp-dspy-surface.c',
+  'gbp-dspy-workspace-addin.c',
+])
+
+plugin_dspy_resources = gnome.compile_resources(
+  'dspy-resources',
+  'dspy.gresource.xml',
+  c_name: 'gbp_dspy',
+)
+
+plugins_sources += plugin_dspy_resources
+
+endif
diff --git a/src/plugins/meson.build b/src/plugins/meson.build
index 0c5fd6aa4..4feeb4a41 100644
--- a/src/plugins/meson.build
+++ b/src/plugins/meson.build
@@ -56,6 +56,7 @@ subdir('devhelp')
 subdir('deviceui')
 subdir('deviced')
 subdir('doap')
+subdir('dspy')
 subdir('editor')
 subdir('editorconfig')
 subdir('emacs')
@@ -143,6 +144,7 @@ status += [
   'CTags ................. : @0@'.format(get_option('plugin_ctags')),
   'Devhelp ............... : @0@'.format(get_option('plugin_devhelp')),
   'Deviced ............... : @0@'.format(get_option('plugin_deviced')),
+  'DBus Spy .............. : @0@'.format(get_option('plugin_dspy')),
   'Editorconfig .......... : @0@'.format(get_option('plugin_editorconfig')),
   'ESLint ................ : @0@'.format(get_option('plugin_eslint')),
   'File Search ........... : @0@'.format(get_option('plugin_file_search')),



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