[gtk/a11y/atspi] a11y: Add utility API to turn objects into references



commit 12b2418ccf25652315ef0bee657e169363c72987
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sat Oct 10 12:54:09 2020 +0100

    a11y: Add utility API to turn objects into references
    
    The ATSPI interface is full of object references in the form of "(so)"
    variants, containing the unique name of the connection as a string, and
    the object reference as a path.
    
    Both GtkAtSpiRoot and GtkAtSpiContext references are passed around as
    such, so we should have some convenience API that does it correctly.

 gtk/a11y/gtkatspicontext.c        | 33 ++++++++++++++++++++++-----------
 gtk/a11y/gtkatspicontextprivate.h |  3 +++
 gtk/a11y/gtkatspiroot.c           | 21 ++++++++-------------
 gtk/a11y/gtkatspirootprivate.h    |  6 ++----
 4 files changed, 35 insertions(+), 28 deletions(-)
---
diff --git a/gtk/a11y/gtkatspicontext.c b/gtk/a11y/gtkatspicontext.c
index 1da3d20fc6..12cec6f9aa 100644
--- a/gtk/a11y/gtkatspicontext.c
+++ b/gtk/a11y/gtkatspicontext.c
@@ -129,11 +129,13 @@ handle_accessible_method (GDBusConnection       *connection,
     }
   else if (g_strcmp0 (method_name, "GetApplication") == 0)
     {
-      const char *name, *path;
+      GVariantBuilder builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE ("((so))"));
 
-      gtk_at_spi_root_get_application (self->root, &name, &path);
+      g_variant_builder_open (&builder, G_VARIANT_TYPE ("(so)"));
+      g_variant_builder_add_value (&builder, gtk_at_spi_root_to_ref (self->root));
+      g_variant_builder_close (&builder);
 
-      g_dbus_method_invocation_return_value (invocation, g_variant_new ("((so))", name, path));
+      g_dbus_method_invocation_return_value (invocation, g_variant_builder_end (&builder));
     }
   else if (g_strcmp0 (method_name, "GetChildAtIndex") == 0)
     {
@@ -173,10 +175,7 @@ handle_accessible_get_property (GDBusConnection       *connection,
 
       if (parent == NULL)
         {
-          const char *name, *path;
-
-          gtk_at_spi_root_get_application (self->root, &name, &path);
-          res = g_variant_new ("(so)", name, path);
+          res = gtk_at_spi_root_to_ref (self->root);
         }
       else
         {
@@ -184,13 +183,11 @@ handle_accessible_get_property (GDBusConnection       *connection,
             gtk_accessible_get_at_context (GTK_ACCESSIBLE (parent));
 
           if (parent_context != NULL)
-            res = g_variant_new ("(so)",
-                                 g_dbus_connection_get_unique_name (self->connection),
-                                 GTK_AT_SPI_CONTEXT (parent_context)->context_path);
+            res = gtk_at_spi_context_to_ref (GTK_AT_SPI_CONTEXT (parent_context));
         }
 
       if (res == NULL)
-        res = g_variant_new ("(so)", "", "/org/a11y/atspi/null");
+        res = gtk_at_spi_null_ref ();
     }
   else if (g_strcmp0 (property_name, "ChildCount") == 0)
     res = g_variant_new_int32 (0);
@@ -560,3 +557,17 @@ gtk_at_spi_context_get_context_path (GtkAtSpiContext *self)
 
   return self->context_path;
 }
+
+GVariant *
+gtk_at_spi_context_to_ref (GtkAtSpiContext *self)
+{
+  g_return_val_if_fail (GTK_IS_AT_SPI_CONTEXT (self), NULL);
+
+  if (self->context_path == NULL)
+    return gtk_at_spi_null_ref ();
+
+  return g_variant_new ("(so)",
+                        g_dbus_connection_get_unique_name (self->connection),
+                        self->context_path);
+
+}
diff --git a/gtk/a11y/gtkatspicontextprivate.h b/gtk/a11y/gtkatspicontextprivate.h
index 7c02efed7a..b0cd407579 100644
--- a/gtk/a11y/gtkatspicontextprivate.h
+++ b/gtk/a11y/gtkatspicontextprivate.h
@@ -36,4 +36,7 @@ gtk_at_spi_create_context (GtkAccessibleRole  accessible_role,
 const char *
 gtk_at_spi_context_get_context_path (GtkAtSpiContext *self);
 
+GVariant *
+gtk_at_spi_context_to_ref (GtkAtSpiContext *self);
+
 G_END_DECLS
diff --git a/gtk/a11y/gtkatspiroot.c b/gtk/a11y/gtkatspiroot.c
index e1a76b1e95..61ed9656a7 100644
--- a/gtk/a11y/gtkatspiroot.c
+++ b/gtk/a11y/gtkatspiroot.c
@@ -242,10 +242,8 @@ handle_accessible_method (GDBusConnection       *connection,
     }
   else if (g_strcmp0 (method_name, "GetApplication") == 0)
     {
-      g_dbus_method_invocation_return_value (invocation,
-                                             g_variant_new ("((so))",
-                                                            self->desktop_name,
-                                                            self->desktop_path));
+      GVariant *res = gtk_at_spi_root_to_ref (self);
+      g_dbus_method_invocation_return_value (invocation, res);
     }
   else if (g_strcmp0 (method_name, "GetChildAtIndex") == 0)
     {
@@ -514,15 +512,12 @@ gtk_at_spi_root_get_cache (GtkAtSpiRoot *self)
   return self->cache;
 }
 
-void
-gtk_at_spi_root_get_application (GtkAtSpiRoot *self,
-                                 const char **name,
-                                 const char **path)
+GVariant *
+gtk_at_spi_root_to_ref (GtkAtSpiRoot *self)
 {
-  g_return_if_fail (GTK_IS_AT_SPI_ROOT (self));
+  g_return_val_if_fail (GTK_IS_AT_SPI_ROOT (self), NULL);
 
-  if (name != NULL)
-    *name = self->desktop_name;
-  if (path != NULL)
-    *path = self->desktop_path;
+  return g_variant_new ("((so))",
+                        self->desktop_name,
+                        self->desktop_path);
 }
diff --git a/gtk/a11y/gtkatspirootprivate.h b/gtk/a11y/gtkatspirootprivate.h
index 268c8a161f..0ce5e6b693 100644
--- a/gtk/a11y/gtkatspirootprivate.h
+++ b/gtk/a11y/gtkatspirootprivate.h
@@ -39,9 +39,7 @@ gtk_at_spi_root_get_connection (GtkAtSpiRoot *self);
 GtkAtSpiCache *
 gtk_at_spi_root_get_cache (GtkAtSpiRoot *self);
 
-void
-gtk_at_spi_root_get_application (GtkAtSpiRoot *self,
-                                 const char **name,
-                                 const char **path);
+GVariant *
+gtk_at_spi_root_to_ref (GtkAtSpiRoot *self);
 
 G_END_DECLS


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