Plug/socket fixes, and remove conditional
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / bridge.c
index 00ead5c..fc05a1f 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <string.h>
 #include <atk/atk.h>
 
 #include <droute/droute.h>
 
 #include "bridge.h"
 #include "event.h"
-#include "accessible-register.h"
 #include "adaptors.h"
+#include "object.h"
+
+#include "accessible-register.h"
+#include "accessible-leasing.h"
+#include "accessible-cache.h"
 
 #include "common/spi-dbus.h"
 
@@ -51,7 +56,7 @@
 
 /*---------------------------------------------------------------------------*/
 
-SpiAppData *atk_adaptor_app_data = NULL;
+SpiBridge *spi_global_app_data = NULL;
 
 static const AtkMisc *atk_misc = NULL;
 
@@ -151,7 +156,7 @@ spi_atk_bridge_get_bus (void)
       else
         {
           if (!dbus_bus_register (bus, &error))
-            g_error ("AT-SPI: Couldn't register with bus: %s\n");
+            g_error ("AT-SPI: Couldn't register with bus: %s\n", error.message);
         }
     }
 
@@ -161,38 +166,57 @@ spi_atk_bridge_get_bus (void)
 /*---------------------------------------------------------------------------*/
 
 static void
-register_application (SpiAppData * app)
+register_application (SpiBridge * app)
 {
-  DBusMessage *message;
+  DBusMessage *message, *reply;
   DBusMessageIter iter;
   DBusError error;
-  const char *uname = NULL;
 
   dbus_error_init (&error);
 
   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
-                                          SPI_DBUS_PATH_REGISTRY,
-                                          SPI_DBUS_INTERFACE_REGISTRY,
-                                          "RegisterApplication");
-  dbus_message_set_no_reply (message, TRUE);
-
-  uname = dbus_bus_get_unique_name (app->bus);
-  if (!uname)
-    {
-      g_error ("AT-SPI: Couldn't get unique name for this connection");
-    }
+                                          SPI_DBUS_PATH_ROOT,
+                                          SPI_DBUS_INTERFACE_SOCKET,
+                                          "Embed");
 
   dbus_message_iter_init_append (message, &iter);
-  dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
-  dbus_connection_send (app->bus, message, NULL);
+  spi_object_append_reference (&iter, app->root);
+
+  reply = dbus_connection_send_with_reply_and_block (app->bus, message, -1, &error);
+
   if (message)
     dbus_message_unref (message);
+
+  if (reply)
+    {
+      DBusMessageIter iter, iter_struct;
+      gchar *app_name, *obj_path;
+
+      dbus_message_iter_init (reply, &iter);
+      dbus_message_iter_recurse (&iter, &iter_struct);
+      if (!(dbus_message_iter_get_arg_type (&iter_struct) == DBUS_TYPE_STRING))
+            g_error ("AT-SPI: Could not obtain desktop path or name\n");
+      dbus_message_iter_get_basic (&iter_struct, &app_name);
+      if (!dbus_message_iter_next (&iter_struct))
+            g_error ("AT-SPI: Could not obtain desktop name");
+      if (!(dbus_message_iter_get_arg_type (&iter_struct) == DBUS_TYPE_OBJECT_PATH))
+            g_error ("AT-SPI: Could not obtain desktop path");
+      dbus_message_iter_get_basic (&iter_struct, &obj_path);
+
+      app->desktop_name = g_strdup (app_name);
+      app->desktop_path = g_strdup (obj_path);
+    }
+  else
+    {
+      g_error ("AT-SPI: Could not embed inside desktop: %s\n", error.message);
+    }
+
 }
 
 /*---------------------------------------------------------------------------*/
 
 static void
-deregister_application (SpiAppData * app)
+deregister_application (SpiBridge * app)
 {
   DBusMessage *message;
   DBusMessageIter iter;
@@ -221,17 +245,17 @@ deregister_application (SpiAppData * app)
 static void
 exit_func (void)
 {
-  if (!atk_adaptor_app_data)
+  if (!spi_global_app_data)
     {
       return;
     }
 
   spi_atk_tidy_windows ();
   spi_atk_deregister_event_listeners ();
-  deregister_application (atk_adaptor_app_data);
+  deregister_application (spi_global_app_data);
 
-  g_free (atk_adaptor_app_data);
-  atk_adaptor_app_data = NULL;
+  g_free (spi_global_app_data);
+  spi_global_app_data = NULL;
 
   /* Not currently creating an XDisplay */
 #if 0
@@ -242,18 +266,17 @@ exit_func (void)
 
 /*---------------------------------------------------------------------------*/
 
-#ifdef __ATK_PLUG_H__
 static AtkPlugClass *plug_class;
 static AtkSocketClass *socket_class;
 
 static gchar *
 get_plug_id (AtkPlug * plug)
 {
-  const char *uname = dbus_bus_get_unique_name (atk_adaptor_app_data->bus);
+  const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
   gchar *path;
   GString *str = g_string_new (NULL);
 
-  path = atk_dbus_object_to_path (ATK_OBJECT (plug), TRUE);
+  path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
   g_string_printf (str, "%s:%s", uname, path);
   g_free (path);
   return g_string_free (str, FALSE);
@@ -266,8 +289,7 @@ socket_embed_hook (AtkSocket * socket, gchar * plug_id)
   gchar *plug_name, *plug_path;
 
   /* Force registration */
-  gchar *path = atk_dbus_object_to_path (accessible, TRUE);
-  spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
+  gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
   /* Let the plug know that it has been embedded */
   plug_name = g_strdup (plug_id);
   if (!plug_name)
@@ -282,7 +304,7 @@ socket_embed_hook (AtkSocket * socket, gchar * plug_id)
       *(plug_path++) = '\0';
       message = dbus_message_new_method_call (plug_name, plug_path, "org.freedesktop.atspi.Accessible", "Embedded");
       dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
-      dbus_connection_send (atk_adaptor_app_data->bus, message, NULL);
+      dbus_connection_send (spi_global_app_data->bus, message, NULL);
     }
   g_free (plug_name);
   g_free (path);
@@ -300,7 +322,6 @@ install_plug_hooks ()
   plug_class->get_object_id = get_plug_id;
   socket_class->embed = socket_embed_hook;
 }
-#endif
 
 gchar *atspi_dbus_name = NULL;
 static gboolean atspi_no_register = FALSE;
@@ -352,24 +373,24 @@ adaptor_init (gint * argc, gchar ** argv[])
     g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
 
   /* Allocate global data and do ATK initializations */
-  atk_adaptor_app_data = g_new0 (SpiAppData, 1);
+  spi_global_app_data = g_new0 (SpiBridge, 1);
   atk_misc = atk_misc_get_instance ();
-  atk_adaptor_app_data->root = root;
+  spi_global_app_data->root = g_object_ref (root);
 
   /* Set up D-Bus connection and register bus name */
   dbus_error_init (&error);
-  atk_adaptor_app_data->bus = spi_atk_bridge_get_bus ();
-  if (!atk_adaptor_app_data->bus)
+  spi_global_app_data->bus = spi_atk_bridge_get_bus ();
+  if (!spi_global_app_data->bus)
     {
-      g_free (atk_adaptor_app_data);
-      atk_adaptor_app_data = NULL;
+      g_free (spi_global_app_data);
+      spi_global_app_data = NULL;
       return 0;
     }
 
   if (atspi_dbus_name != NULL)
     {
       if (dbus_bus_request_name
-          (atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
+          (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
         {
           g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
         }
@@ -381,30 +402,39 @@ adaptor_init (gint * argc, gchar ** argv[])
         }
     }
 
-  dbus_connection_setup_with_g_main (atk_adaptor_app_data->bus,
+  dbus_connection_setup_with_g_main (spi_global_app_data->bus,
                                      g_main_context_default ());
 
+  /* 
+   * Create the leasing, register and cache objects.
+   * The order is important here, the cache depends on the
+   * register object.
+   */
+  spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
+  spi_global_leasing  = g_object_new (SPI_LEASING_TYPE, NULL);
+  spi_global_cache    = g_object_new (SPI_CACHE_TYPE, NULL);
+
   /* Get D-Bus introspection directory */
   introspection_directory = (char *) g_getenv ("ATSPI_INTROSPECTION_PATH");
   if (introspection_directory == NULL)
     introspection_directory = ATSPI_INTROSPECTION_PATH;
 
   /* Register droute for routing AT-SPI messages */
-  atk_adaptor_app_data->droute =
-    droute_new (atk_adaptor_app_data->bus, introspection_directory);
+  spi_global_app_data->droute =
+    droute_new (spi_global_app_data->bus, introspection_directory);
 
-  treepath = droute_add_one (atk_adaptor_app_data->droute,
-                             "/org/freedesktop/atspi/tree", NULL);
+  treepath = droute_add_one (spi_global_app_data->droute,
+                             "/org/at_spi/cache", spi_global_cache);
 
-  accpath = droute_add_many (atk_adaptor_app_data->droute,
+  accpath = droute_add_many (spi_global_app_data->droute,
                              "/org/freedesktop/atspi/accessible",
                              NULL,
                              (DRouteGetDatumFunction)
-                             atk_dbus_path_to_gobject);
+                             spi_global_register_path_to_object);
 
-  /* Register all interfaces with droute and set up application accessible db */
-  spi_initialize_tree (treepath);
 
+  /* Register all interfaces with droute and set up application accessible db */
+  spi_initialize_cache (treepath);
   spi_initialize_accessible (accpath);
   spi_initialize_application (accpath);
   spi_initialize_action (accpath);
@@ -420,20 +450,15 @@ adaptor_init (gint * argc, gchar ** argv[])
   spi_initialize_text (accpath);
   spi_initialize_value (accpath);
 
-  /* Initialize the AtkObject registration */
-  atk_dbus_initialize (atk_adaptor_app_data->root);
-
   /* Register methods to send D-Bus signals on certain ATK events */
   spi_atk_register_event_listeners ();
 
-#ifdef __ATK_PLUG_H__
   /* Hook our plug-and socket functions */
   install_plug_hooks ();
-#endif
 
   /* Register this app by sending a signal out to AT-SPI registry daemon */
   if (!atspi_no_register)
-    register_application (atk_adaptor_app_data);
+    register_application (spi_global_app_data);
 
   g_atexit (exit_func);