[gnome-flashback] input-sources: move GfIBusManager to GfInputSourceIBus



commit 2ad7488b5afaeccf64b737049970a6dec8d12956
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Thu Jan 2 03:51:36 2020 +0200

    input-sources: move GfIBusManager to GfInputSourceIBus

 .../libinput-sources/gf-input-source-ibus.c        | 150 ++++++++++++++++++++-
 .../libinput-sources/gf-input-source-ibus.h        |   6 +
 .../libinput-sources/gf-input-source-manager.c     |  40 +++---
 gnome-flashback/libinput-sources/gf-input-source.c | 112 +--------------
 gnome-flashback/libinput-sources/gf-input-source.h |   8 +-
 .../libinput-sources/gf-input-sources.c            |  83 ++++++++----
 6 files changed, 245 insertions(+), 154 deletions(-)
---
diff --git a/gnome-flashback/libinput-sources/gf-input-source-ibus.c 
b/gnome-flashback/libinput-sources/gf-input-source-ibus.c
index 52e9f3c..b0138d8 100644
--- a/gnome-flashback/libinput-sources/gf-input-source-ibus.c
+++ b/gnome-flashback/libinput-sources/gf-input-source-ibus.c
@@ -18,19 +18,167 @@
 #include "config.h"
 #include "gf-input-source-ibus.h"
 
+#include "gf-ibus-manager.h"
+
 struct _GfInputSourceIBus
 {
-  GfInputSource parent;
+  GfInputSource  parent;
+
+  GfIBusManager *ibus_manager;
+
+  IBusPropList  *prop_list;
+
+  char          *xkb_id;
+};
+
+enum
+{
+  PROP_0,
+
+  PROP_IBUS_MANAGER,
+
+  LAST_PROP
 };
 
+static GParamSpec *ibus_properties[LAST_PROP] = { NULL };
+
 G_DEFINE_TYPE (GfInputSourceIBus, gf_input_source_ibus, GF_TYPE_INPUT_SOURCE)
 
+static void
+gf_input_source_ibus_constructed (GObject *object)
+{
+  GfInputSourceIBus *self;
+  const char *id;
+  IBusEngineDesc *engine_desc;
+  const char *layout_variant;
+  const char *layout;
+
+  self = GF_INPUT_SOURCE_IBUS (object);
+
+  G_OBJECT_CLASS (gf_input_source_ibus_parent_class)->constructed (object);
+
+  id = gf_input_source_get_id (GF_INPUT_SOURCE (self));
+
+  engine_desc = gf_ibus_manager_get_engine_desc (self->ibus_manager, id);
+  g_assert (engine_desc != NULL);
+
+  layout = ibus_engine_desc_get_layout (engine_desc);
+  layout_variant = ibus_engine_desc_get_layout_variant (engine_desc);
+
+  if (layout_variant != NULL && *layout_variant != '\0')
+    self->xkb_id = g_strdup_printf ("%s+%s", layout, layout_variant);
+  else
+    self->xkb_id = g_strdup (layout);
+}
+
+static void
+gf_input_source_ibus_dispose (GObject *object)
+{
+  GfInputSourceIBus *self;
+
+  self = GF_INPUT_SOURCE_IBUS (object);
+
+  g_clear_object (&self->ibus_manager);
+  g_clear_object (&self->prop_list);
+
+  G_OBJECT_CLASS (gf_input_source_ibus_parent_class)->dispose (object);
+}
+
+static void
+gf_input_source_ibus_finalize (GObject *object)
+{
+  GfInputSourceIBus *self;
+
+  self = GF_INPUT_SOURCE_IBUS (object);
+
+  g_clear_pointer (&self->xkb_id, g_free);
+
+  G_OBJECT_CLASS (gf_input_source_ibus_parent_class)->finalize (object);
+}
+
+static void
+gf_input_source_ibus_set_property (GObject      *object,
+                                   unsigned int  prop_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+{
+  GfInputSourceIBus *self;
+
+  self = GF_INPUT_SOURCE_IBUS (object);
+
+  switch (prop_id)
+    {
+      case PROP_IBUS_MANAGER:
+        self->ibus_manager = g_value_dup_object (value);
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        break;
+    }
+}
+
+static const char *
+gf_input_source_ibus_get_xkb_id (GfInputSource *input_source)
+{
+  GfInputSourceIBus *self;
+
+  self = GF_INPUT_SOURCE_IBUS (input_source);
+
+  return self->xkb_id;
+}
+
+static void
+install_properties (GObjectClass *object_class)
+{
+  ibus_properties[PROP_IBUS_MANAGER] =
+    g_param_spec_object ("ibus-manager",
+                         "ibus-manager",
+                         "ibus-manager",
+                         GF_TYPE_IBUS_MANAGER,
+                         G_PARAM_CONSTRUCT_ONLY |
+                         G_PARAM_WRITABLE |
+                         G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (object_class, LAST_PROP, ibus_properties);
+}
+
 static void
 gf_input_source_ibus_class_init (GfInputSourceIBusClass *self_class)
 {
+  GObjectClass *object_class;
+  GfInputSourceClass *input_source_class;
+
+  object_class = G_OBJECT_CLASS (self_class);
+  input_source_class = GF_INPUT_SOURCE_CLASS (self_class);
+
+  object_class->constructed = gf_input_source_ibus_constructed;
+  object_class->dispose = gf_input_source_ibus_dispose;
+  object_class->finalize = gf_input_source_ibus_finalize;
+  object_class->set_property = gf_input_source_ibus_set_property;
+
+  input_source_class->get_xkb_id = gf_input_source_ibus_get_xkb_id;
+
+  install_properties (object_class);
 }
 
 static void
 gf_input_source_ibus_init (GfInputSourceIBus *self)
 {
 }
+
+IBusPropList *
+gf_input_source_ibus_get_properties (GfInputSourceIBus *self)
+{
+  return self->prop_list;
+}
+
+void
+gf_input_source_ibus_set_properties (GfInputSourceIBus *self,
+                                     IBusPropList      *prop_list)
+{
+  g_clear_object (&self->prop_list);
+
+  if (prop_list != NULL)
+    self->prop_list = g_object_ref (prop_list);
+}
diff --git a/gnome-flashback/libinput-sources/gf-input-source-ibus.h 
b/gnome-flashback/libinput-sources/gf-input-source-ibus.h
index 2411389..eece92c 100644
--- a/gnome-flashback/libinput-sources/gf-input-source-ibus.h
+++ b/gnome-flashback/libinput-sources/gf-input-source-ibus.h
@@ -18,6 +18,7 @@
 #ifndef GF_INPUT_SOURCE_IBUS_H
 #define GF_INPUT_SOURCE_IBUS_H
 
+#include <ibus-1.0/ibus.h>
 #include "gf-input-source.h"
 
 G_BEGIN_DECLS
@@ -26,6 +27,11 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (GfInputSourceIBus, gf_input_source_ibus,
                       GF, INPUT_SOURCE_IBUS, GfInputSource)
 
+IBusPropList *gf_input_source_ibus_get_properties (GfInputSourceIBus *self);
+
+void          gf_input_source_ibus_set_properties (GfInputSourceIBus *self,
+                                                   IBusPropList      *prop_list);
+
 G_END_DECLS
 
 #endif
diff --git a/gnome-flashback/libinput-sources/gf-input-source-manager.c 
b/gnome-flashback/libinput-sources/gf-input-source-manager.c
index 87b8683..24d6126 100644
--- a/gnome-flashback/libinput-sources/gf-input-source-manager.c
+++ b/gnome-flashback/libinput-sources/gf-input-source-manager.c
@@ -1189,7 +1189,6 @@ sources_changed_cb (GfInputSourceSettings *settings,
 
   for (l = source_infos; l != NULL; l = g_list_next (l))
     {
-      GType gtype;
       SourceInfo *info;
       gint position;
       GfInputSource *source;
@@ -1198,18 +1197,26 @@ sources_changed_cb (GfInputSourceSettings *settings,
       position = g_list_position (source_infos, l);
 
       if (g_strcmp0 (info->type, INPUT_SOURCE_TYPE_IBUS) == 0)
-        gtype = GF_TYPE_INPUT_SOURCE_IBUS;
+        {
+          source = g_object_new (GF_TYPE_INPUT_SOURCE_IBUS,
+                                 "ibus-manager", manager->ibus_manager,
+                                 "type", info->type,
+                                 "id", info->id,
+                                 "display-name", info->display_name,
+                                 "short-name", info->short_name,
+                                 "index", position,
+                                 NULL);
+        }
       else
-        gtype = GF_TYPE_INPUT_SOURCE_XKB;
-
-      source = g_object_new (gtype,
-                             "ibus-manager", manager->ibus_manager,
-                             "type", info->type,
-                             "id", info->id,
-                             "display-name", info->display_name,
-                             "short-name", info->short_name,
-                             "index", position,
-                             NULL);
+        {
+          source = g_object_new (GF_TYPE_INPUT_SOURCE_XKB,
+                                 "type", info->type,
+                                 "id", info->id,
+                                 "display-name", info->display_name,
+                                 "short-name", info->short_name,
+                                 "index", position,
+                                 NULL);
+        }
 
       gf_input_source_set_icon_file (source, info->icon_file);
 
@@ -1357,10 +1364,11 @@ properties_registered_cb (GfIBusManager *ibus_manager,
   source = (GfInputSource *) g_hash_table_lookup (manager->ibus_sources,
                                                   engine_name);
 
-  if (!source)
+  if (!source || !GF_IS_INPUT_SOURCE_IBUS (source))
     return;
 
-  gf_input_source_set_properties (source, prop_list);
+  gf_input_source_ibus_set_properties (GF_INPUT_SOURCE_IBUS (source),
+                                       prop_list);
 
   if (compare_sources (source, manager->current_source))
     g_signal_emit (manager, signals[SIGNAL_CURRENT_SOURCE_CHANGED], 0, NULL);
@@ -1423,10 +1431,10 @@ property_updated_cb (GfIBusManager *ibus_manager,
   source = (GfInputSource *) g_hash_table_lookup (manager->ibus_sources,
                                                   engine_name);
 
-  if (!source)
+  if (!source || !GF_IS_INPUT_SOURCE_IBUS (source))
     return;
 
-  prop_list = gf_input_source_get_properties (source);
+  prop_list = gf_input_source_ibus_get_properties (GF_INPUT_SOURCE_IBUS (source));
 
   if (!update_sub_property (prop_list, property))
     return;
diff --git a/gnome-flashback/libinput-sources/gf-input-source.c 
b/gnome-flashback/libinput-sources/gf-input-source.c
index 06e1656..01c810f 100644
--- a/gnome-flashback/libinput-sources/gf-input-source.c
+++ b/gnome-flashback/libinput-sources/gf-input-source.c
@@ -21,22 +21,14 @@
 
 #include <string.h>
 
-#include "gf-ibus-manager.h"
-
 typedef struct
 {
-  GfIBusManager *ibus_manager;
-
   gchar         *type;
   gchar         *id;
   gchar         *display_name;
   gchar         *short_name;
   guint          index;
 
-  gchar         *xkb_id;
-
-  IBusPropList  *prop_list;
-
   char          *icon_file;
 } GfInputSourcePrivate;
 
@@ -54,8 +46,6 @@ enum
 {
   PROP_0,
 
-  PROP_IBUS_MANAGER,
-
   PROP_TYPE,
   PROP_ID,
   PROP_DISPLAY_NAME,
@@ -69,30 +59,6 @@ static GParamSpec *properties[LAST_PROP] = { NULL };
 
 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GfInputSource, gf_input_source, G_TYPE_OBJECT)
 
-static gchar *
-get_xkb_id (GfInputSource *source)
-{
-  GfInputSourcePrivate *priv;
-  IBusEngineDesc *engine_desc;
-  const gchar *layout_variant;
-  const gchar *layout;
-
-  priv = gf_input_source_get_instance_private (source);
-
-  engine_desc = gf_ibus_manager_get_engine_desc (priv->ibus_manager, priv->id);
-
-  if (!engine_desc)
-    return g_strdup (priv->id);
-
-  layout = ibus_engine_desc_get_layout (engine_desc);
-  layout_variant = ibus_engine_desc_get_layout_variant (engine_desc);
-
-  if (layout_variant && strlen (layout_variant) > 0)
-    return g_strdup_printf ("%s+%s", layout, layout_variant);
-  else
-    return g_strdup (layout);
-}
-
 static void
 gf_input_source_get_property (GObject    *object,
                               guint       prop_id,
@@ -107,10 +73,6 @@ gf_input_source_get_property (GObject    *object,
 
   switch (prop_id)
     {
-      case PROP_IBUS_MANAGER:
-        g_value_set_object (value, priv->ibus_manager);
-        break;
-
       case PROP_TYPE:
         g_value_set_string (value, priv->type);
         break;
@@ -151,10 +113,6 @@ gf_input_source_set_property (GObject      *object,
 
   switch (prop_id)
     {
-      case PROP_IBUS_MANAGER:
-        priv->ibus_manager = g_value_get_object (value);
-        break;
-
       case PROP_TYPE:
         priv->type = g_value_dup_string (value);
         break;
@@ -181,20 +139,6 @@ gf_input_source_set_property (GObject      *object,
     }
 }
 
-static void
-gf_input_source_dispose (GObject *object)
-{
-  GfInputSource *source;
-  GfInputSourcePrivate *priv;
-
-  source = GF_INPUT_SOURCE (object);
-  priv = gf_input_source_get_instance_private (source);
-
-  g_clear_object (&priv->prop_list);
-
-  G_OBJECT_CLASS (gf_input_source_parent_class)->dispose (object);
-}
-
 static void
 gf_input_source_finalize (GObject *object)
 {
@@ -208,26 +152,11 @@ gf_input_source_finalize (GObject *object)
   g_free (priv->id);
   g_free (priv->display_name);
   g_free (priv->short_name);
-  g_free (priv->xkb_id);
   g_free (priv->icon_file);
 
   G_OBJECT_CLASS (gf_input_source_parent_class)->finalize (object);
 }
 
-static void
-gf_input_source_constructed (GObject *object)
-{
-  GfInputSource *source;
-  GfInputSourcePrivate *priv;
-
-  source = GF_INPUT_SOURCE (object);
-  priv = gf_input_source_get_instance_private (source);
-
-  G_OBJECT_CLASS (gf_input_source_parent_class)->constructed (object);
-
-  priv->xkb_id = get_xkb_id (source);
-}
-
 static void
 gf_input_source_class_init (GfInputSourceClass *source_class)
 {
@@ -235,8 +164,6 @@ gf_input_source_class_init (GfInputSourceClass *source_class)
 
   object_class = G_OBJECT_CLASS (source_class);
 
-  object_class->constructed = gf_input_source_constructed;
-  object_class->dispose = gf_input_source_dispose;
   object_class->finalize = gf_input_source_finalize;
   object_class->get_property = gf_input_source_get_property;
   object_class->set_property = gf_input_source_set_property;
@@ -250,13 +177,6 @@ gf_input_source_class_init (GfInputSourceClass *source_class)
     g_signal_new ("changed", G_OBJECT_CLASS_TYPE (source_class),
                   G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
 
-  properties[PROP_IBUS_MANAGER] =
-    g_param_spec_object ("ibus-manager", "IBus Manager",
-                         "The instance of IBus Manager used by the input-sources module",
-                         GF_TYPE_IBUS_MANAGER,
-                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
-                         G_PARAM_STATIC_STRINGS);
-
   properties[PROP_TYPE] =
     g_param_spec_string ("type", "type", "The type of the input source",
                          NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
@@ -363,11 +283,17 @@ gf_input_source_get_index (GfInputSource *source)
 const gchar *
 gf_input_source_get_xkb_id (GfInputSource *source)
 {
+  GfInputSourceClass *input_source_class;
   GfInputSourcePrivate *priv;
 
+  input_source_class = GF_INPUT_SOURCE_GET_CLASS (source);
+
+  if (input_source_class->get_xkb_id != NULL)
+    return input_source_class->get_xkb_id (source);
+
   priv = gf_input_source_get_instance_private (source);
 
-  return priv->xkb_id;
+  return priv->id;
 }
 
 void
@@ -377,30 +303,6 @@ gf_input_source_activate (GfInputSource *source,
   g_signal_emit (source, signals[SIGNAL_ACTIVATE], 0, interactive);
 }
 
-IBusPropList *
-gf_input_source_get_properties (GfInputSource *source)
-{
-  GfInputSourcePrivate *priv;
-
-  priv = gf_input_source_get_instance_private (source);
-
-  return priv->prop_list;
-}
-
-void
-gf_input_source_set_properties (GfInputSource *source,
-                                IBusPropList  *prop_list)
-{
-  GfInputSourcePrivate *priv;
-
-  priv = gf_input_source_get_instance_private (source);
-
-  g_clear_object (&priv->prop_list);
-
-  if (prop_list != NULL)
-    priv->prop_list = g_object_ref (prop_list);
-}
-
 const char *
 gf_input_source_get_icon_file (GfInputSource *self)
 {
diff --git a/gnome-flashback/libinput-sources/gf-input-source.h 
b/gnome-flashback/libinput-sources/gf-input-source.h
index a31f749..45e1b38 100644
--- a/gnome-flashback/libinput-sources/gf-input-source.h
+++ b/gnome-flashback/libinput-sources/gf-input-source.h
@@ -20,7 +20,6 @@
 #define GF_INPUT_SOURCE_H
 
 #include <glib-object.h>
-#include <ibus-1.0/ibus.h>
 
 G_BEGIN_DECLS
 
@@ -31,6 +30,8 @@ G_DECLARE_DERIVABLE_TYPE (GfInputSource, gf_input_source,
 struct _GfInputSourceClass
 {
   GObjectClass parent_class;
+
+  const char * (* get_xkb_id) (GfInputSource *self);
 };
 
 const gchar   *gf_input_source_get_source_type  (GfInputSource *source);
@@ -51,11 +52,6 @@ const gchar   *gf_input_source_get_xkb_id       (GfInputSource *source);
 void           gf_input_source_activate         (GfInputSource *source,
                                                  gboolean       interactive);
 
-IBusPropList  *gf_input_source_get_properties   (GfInputSource *source);
-
-void           gf_input_source_set_properties   (GfInputSource *source,
-                                                 IBusPropList  *prop_list);
-
 const char    *gf_input_source_get_icon_file    (GfInputSource *self);
 
 void           gf_input_source_set_icon_file    (GfInputSource *self,
diff --git a/gnome-flashback/libinput-sources/gf-input-sources.c 
b/gnome-flashback/libinput-sources/gf-input-sources.c
index 8235202..8da11d7 100644
--- a/gnome-flashback/libinput-sources/gf-input-sources.c
+++ b/gnome-flashback/libinput-sources/gf-input-sources.c
@@ -26,6 +26,7 @@
 #include "dbus/gf-input-sources-gen.h"
 #include "gf-ibus-manager.h"
 #include "gf-input-sources.h"
+#include "gf-input-source-ibus.h"
 #include "gf-input-source-manager.h"
 #include "gf-input-source.h"
 
@@ -66,44 +67,51 @@ append_icon_info (GVariantBuilder *builder,
   const char *display_name;
   const char *icon_text;
   const char *icon_file;
-  IBusPropList *prop_list;
 
   display_name = gf_input_source_get_display_name (self->current_source);
   icon_text = gf_input_source_get_short_name (self->current_source);
   icon_file = gf_input_source_get_icon_file (self->current_source);
-  prop_list = gf_input_source_get_properties (self->current_source);
 
-  if (prop_list != NULL)
+  if (GF_IS_INPUT_SOURCE_IBUS (self->current_source))
     {
-      guint i;
+      GfInputSourceIBus *ibus_source;
+      IBusPropList *prop_list;
+
+      ibus_source = GF_INPUT_SOURCE_IBUS (self->current_source);
+      prop_list = gf_input_source_ibus_get_properties (ibus_source);
 
-      for (i = 0; i < prop_list->properties->len; i++)
+      if (prop_list != NULL)
         {
-          IBusProperty *prop;
-          const char *key;
-          IBusText *symbol;
-          IBusText *label;
-          const char *tmp;
+          guint i;
+
+          for (i = 0; i < prop_list->properties->len; i++)
+            {
+              IBusProperty *prop;
+              const char *key;
+              IBusText *symbol;
+              IBusText *label;
+              const char *tmp;
 
-          prop = ibus_prop_list_get (prop_list, i);
+              prop = ibus_prop_list_get (prop_list, i);
 
-          if (!ibus_property_get_visible (prop))
-            continue;
+              if (!ibus_property_get_visible (prop))
+                continue;
 
-          key = ibus_property_get_key (prop);
-          if (g_strcmp0 (key, "InputMode") != 0)
-            continue;
+              key = ibus_property_get_key (prop);
+              if (g_strcmp0 (key, "InputMode") != 0)
+                continue;
 
-          symbol = ibus_property_get_symbol (prop);
-          label = ibus_property_get_label (prop);
+              symbol = ibus_property_get_symbol (prop);
+              label = ibus_property_get_label (prop);
 
-          if (symbol != NULL)
-            tmp = ibus_text_get_text (symbol);
-          else
-            tmp = ibus_text_get_text (label);
+              if (symbol != NULL)
+                tmp = ibus_text_get_text (symbol);
+              else
+                tmp = ibus_text_get_text (label);
 
-          if (tmp != NULL && *tmp != '\0' && g_utf8_strlen (tmp, -1) < 3)
-            icon_text = tmp;
+              if (tmp != NULL && *tmp != '\0' && g_utf8_strlen (tmp, -1) < 3)
+                icon_text = tmp;
+            }
         }
     }
 
@@ -245,7 +253,18 @@ append_properties (GVariantBuilder *builder,
   IBusPropList *prop_list;
   GVariant *properties;
 
-  prop_list = gf_input_source_get_properties (self->current_source);
+  if (GF_IS_INPUT_SOURCE_IBUS (self->current_source))
+    {
+      GfInputSourceIBus *ibus_source;
+
+      ibus_source = GF_INPUT_SOURCE_IBUS (self->current_source);
+      prop_list = gf_input_source_ibus_get_properties (ibus_source);
+    }
+  else
+    {
+      prop_list = NULL;
+    }
+
   properties = prop_list_to_variant (prop_list);
 
   g_variant_builder_add (builder, "{sv}", "properties", properties);
@@ -502,10 +521,22 @@ handle_activate_property_cb (GfInputSourcesGen     *object,
                              const char            *key,
                              GfInputSources        *self)
 {
+  GfInputSourceIBus *ibus_source;
   IBusPropList *prop_list;
   IBusProperty *prop;
 
-  prop_list = gf_input_source_get_properties (self->current_source);
+  if (!GF_IS_INPUT_SOURCE_IBUS (self->current_source))
+    {
+      g_dbus_method_invocation_return_error (invocation,
+                                             G_DBUS_ERROR,
+                                             G_DBUS_ERROR_INVALID_ARGS,
+                                             "Input source does not have properties");
+
+      return TRUE;
+    }
+
+  ibus_source = GF_INPUT_SOURCE_IBUS (self->current_source);
+  prop_list = gf_input_source_ibus_get_properties (ibus_source);
 
   if (prop_list == NULL)
     {


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