2009-03-10 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / cspi / spi-main.c
index fe8f402..aeda398 100644 (file)
@@ -93,6 +93,7 @@ static const char *interfaces[] =
   SPI_DBUS_INTERFACE_ACCESSIBLE,
   SPI_DBUS_INTERFACE_ACTION,
   SPI_DBUS_INTERFACE_APPLICATION,
+  SPI_DBUS_INTERFACE_COLLECTION,
   SPI_DBUS_INTERFACE_COMPONENT,
   SPI_DBUS_INTERFACE_DOCUMENT,
   SPI_DBUS_INTERFACE_EDITABLE_TEXT,
@@ -218,6 +219,10 @@ cspi_object_unref_internal (Accessible *accessible, gboolean defunct)
     {
       g_free (accessible->v.path);
     }
+    if (accessible->states)
+      spi_state_set_cache_unref (accessible->states);
+    g_free (accessible->description);
+    g_free (accessible->name);
     g_free(accessible);
   }
 }
@@ -319,6 +324,7 @@ typedef struct
   char *name;
   dbus_uint32_t role;
   char *description;
+  GArray *state_bitflags;
 } CACHE_ADDITION;
 
 /* Update the cache with added/modified objects and free the array */
@@ -370,8 +376,10 @@ handle_additions (CSpiApplication*app, GArray *additions)
     a->role = ca->role;
     if (a->description) g_free (a->description);
     a->description = ca->description;
+    a->states = spi_state_set_cache_new (ca->state_bitflags);
     g_array_free (ca->interfaces, TRUE);
     g_array_free (ca->children, TRUE);
+    /* spi_state_set_cache_new frees state_bitflags */
     /* This is a bit of a hack since ref_accessible sets ref_count to 2
      * for a new object, one of the refs being for the cache */
     cspi_object_unref (a);
@@ -407,7 +415,7 @@ add_app_to_desktop (Accessible *a, const char *bus_name)
   char *root_path;
 
   dbus_error_init (&error);
-  if (dbind_connection_method_call (bus, bus_name, "/org/freedesktop/atspi/tree", spi_interface_tree, "getRoot", &error, "=>o", &root_path))
+  if (dbind_method_call_reentrant (bus, bus_name, "/org/freedesktop/atspi/tree", spi_interface_tree, "getRoot", &error, "=>o", &root_path))
   {
     Accessible *obj = cspi_ref_accessible (bus_name, root_path);
     if (obj)
@@ -474,38 +482,34 @@ remove_app_from_desktop (Accessible *a, const char *bus_name)
   return TRUE;
 }
 
+static Accessible *desktop;
+
 static Accessible *
-ref_accessible_desktop (CSpiApplication *app, const char *path)
+ref_accessible_desktop (CSpiApplication *app)
 {
-  char *path_dup;
   DBusError error;
   GArray *apps = NULL;
   GArray *additions;
   gint i;
 
-  Accessible *a = g_hash_table_lookup (app->hash, path);
-  if (a)
+  if (desktop)
   {
-    cspi_object_ref (a);
-    return a;
+    cspi_object_ref (desktop);
+    return desktop;
   }
-  path_dup = g_strdup (path);
-  if (!path_dup) return NULL;
-  a = g_new0 (Accessible, 1);
-  if (!a)
+  desktop = g_new0 (Accessible, 1);
+  if (!desktop)
   {
-    g_free (path_dup);
     return NULL;
   }
-  g_hash_table_insert (app->hash, path_dup, a);
-  a->app = app;
-  a->v.path = path_dup;
-  a->ref_count = 2;    /* one for the caller, one for the hash */
-  cspi_dbus_get_property (a, SPI_DBUS_INTERFACE_ACCESSIBLE, "name", NULL, "s", &a->name);
+  g_hash_table_insert (app->hash, "", desktop);
+  desktop->app = app;
+  desktop->ref_count = 2;      /* one for the caller, one for the hash */
+  desktop->name = g_strdup ("");
   dbus_error_init (&error);
-  if (!dbind_connection_method_call (bus, spi_bus_registry, a->v.path, spi_interface_desktop, "getChildren", &error, "=>as", &apps))
+  if (!dbind_method_call_reentrant (bus, spi_bus_registry, spi_path_registry, spi_interface_registry, "getApplications", &error, "=>as", &apps))
   {
-    g_error ("Couldn't get desktop children: %s", error.message);
+    g_error ("Couldn't get application list: %s", error.message);
   }
   for (i = 0; i < apps->len; i++)
   {
@@ -518,16 +522,16 @@ ref_accessible_desktop (CSpiApplication *app, const char *path)
     CSpiApplication *app = cspi_get_application (app_name);
     additions = NULL;
     dbus_error_init (&error);
-    dbind_connection_method_call (bus, app_name, "/org/freedesktop/atspi/tree", spi_interface_tree, "getTree", &error, "=>a(ooaoassus)", &additions);
+    dbind_method_call_reentrant (bus, app_name, "/org/freedesktop/atspi/tree", spi_interface_tree, "getTree", &error, "=>a(ooaoassusau)", &additions);
     if (error.message)
     {
       g_warning ("getTree (%s): %s", app_name, error.message);
     }
     handle_additions (app, additions);
-    add_app_to_desktop (a, app_name);
+    add_app_to_desktop (desktop, app_name);
   }
   g_array_free (apps, TRUE);
-  return a;
+  return desktop;
 }
 
 Accessible *
@@ -537,7 +541,7 @@ cspi_ref_accessible (const char *app, const char *path)
   if (!a) return NULL;
   if ( APP_IS_REGISTRY(a))
   {
-    return ref_accessible_desktop (a, path);
+    return ref_accessible_desktop (a);
   }
   return ref_accessible (a, path);
 }
@@ -554,7 +558,7 @@ typedef struct
   GArray *removals;
 } CacheSignalData;
 
-static const char *cacheSignalType = "a(ooaoassus)ao";
+static const char *cacheSignalType = "a(ooaoassusau)ao";
 
 static DBusHandlerResult
 cspi_dbus_handle_update_tree (DBusConnection *bus, DBusMessage *message, void *user_data)
@@ -580,7 +584,7 @@ cspi_dbus_handle_update_tree (DBusConnection *bus, DBusMessage *message, void *u
 }
 
 static DBusHandlerResult
-cspi_dbus_handle_add_application (DBusConnection *bus, DBusMessage *message, void *user_data)
+cspi_dbus_handle_register_application (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
   DBusError error;
   dbus_uint32_t v;
@@ -588,14 +592,8 @@ cspi_dbus_handle_add_application (DBusConnection *bus, DBusMessage *message, voi
   char *bus_name;
 
   dbus_error_init (&error);
-  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_UINT32, &v, DBUS_TYPE_STRING, &bus_name, DBUS_TYPE_INVALID))
-  {
-    g_warning ("Error processing %s: %s\n", dbus_message_get_member(message), error.message);
-    dbus_error_free (&error);
-    return DBUS_HANDLER_RESULT_HANDLED;
-  }
-  a = cspi_ref_accessible (spi_bus_registry, dbus_message_get_path(message));
-  if (add_app_to_desktop (a, bus_name))
+  a = cspi_ref_accessible (spi_bus_registry, spi_path_registry);
+  if (add_app_to_desktop (a, dbus_message_get_sender (message)))
   {
     send_children_changed (a, g_list_last (a->children)->data, TRUE);
   }
@@ -604,21 +602,21 @@ cspi_dbus_handle_add_application (DBusConnection *bus, DBusMessage *message, voi
 }
 
 static DBusHandlerResult
-cspi_dbus_handle_remove_application (DBusConnection *bus, DBusMessage *message, void *user_data)
+cspi_dbus_handle_deregister_application (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
-  DBusError error;
-  dbus_uint32_t v;
   Accessible *a;
+  DBusError error;
   char *bus_name;
-
   dbus_error_init (&error);
-  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_UINT32, &v, DBUS_TYPE_STRING, &bus_name, DBUS_TYPE_INVALID))
+  if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &bus_name, DBUS_TYPE_INVALID))
   {
     g_warning ("Error processing %s: %s\n", dbus_message_get_member(message), error.message);
     dbus_error_free (&error);
     return DBUS_HANDLER_RESULT_HANDLED;
   }
-  a = cspi_ref_accessible (spi_bus_registry, dbus_message_get_path(message));
+
+  a = cspi_ref_accessible (spi_bus_registry, spi_path_registry);
   remove_app_from_desktop (a, bus_name);
   cspi_object_unref (a);
   return DBUS_HANDLER_RESULT_HANDLED;
@@ -635,7 +633,7 @@ cspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
   char *bus_name;
 
   if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
-      !strcmp (interface, SPI_DBUS_INTERFACE_ACCESSIBLE))
+      !strncmp (interface, "org.freedesktop.atspi.Event.", 28))
   {
     return cspi_dbus_handle_event (bus, message, data);
   }
@@ -647,17 +645,28 @@ cspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
   {
     return cspi_dbus_handle_update_tree (bus, message, data);
   }
-  if (dbus_message_is_signal (message, spi_interface_registry, "applicationAdd"))
+  if (dbus_message_is_method_call (message, spi_interface_registry, "registerApplication"))
   {
-    return cspi_dbus_handle_add_application (bus, message, data);
+    return cspi_dbus_handle_register_application (bus, message, data);
   }
-  if (dbus_message_is_signal (message, spi_interface_registry, "applicationRemove"))
+  if (dbus_message_is_method_call (message, spi_interface_registry, "deregisterApplication"))
   {
-    return cspi_dbus_handle_remove_application (bus, message, data);
+    return cspi_dbus_handle_deregister_application (bus, message, data);
   }
   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
+static const char *signal_interfaces[] =
+{
+  "org.freedesktop.atspi.Event.Object",
+  "org.freedesktop.atspi.Event.Window",
+  "org.freedesktop.atspi.Event.Mouse",
+  "org.freedesktop.atspi.Event.Terminal",
+  "org.freedesktop.atspi.Event.Document",
+  "org.freedesktop.atspi.Event.Focus",
+  NULL
+};
+
 /**
  * SPI_init:
  *
@@ -670,6 +679,7 @@ SPI_init (void)
 {
   DBusError error;
   char *match;
+  int i;
 
   if (SPI_inited)
     {
@@ -697,9 +707,15 @@ SPI_init (void)
   dbus_error_init (&error);
   dbus_bus_add_match (bus, match, &error);
   g_free (match);
-  match = g_strdup_printf ("type='signal',sender='%s'", spi_bus_registry);
+  match = g_strdup_printf ("type='method_call',interface='%s'", spi_interface_registry);
   dbus_bus_add_match (bus, match, &error);
   g_free (match);
+  for (i = 0; signal_interfaces[i]; i++)
+  {
+    match = g_strdup_printf ("type='signal',interface='%s'", signal_interfaces[i]);
+    dbus_bus_add_match (bus, match, &error);
+    g_free (match);
+  }
   return 0;
 }
 
@@ -1007,7 +1023,7 @@ get_path (Accessible *obj)
 {
   if (APP_IS_REGISTRY (obj->app))
   {
-    return g_strdup_printf (SPI_DBUS_PATH_DESKTOP);
+    return g_strdup_printf (SPI_DBUS_PATH_REGISTRY);
   }
   return g_strdup_printf ("/org/freedesktop/atspi/accessible/%d", obj->v.id);
 }
@@ -1023,7 +1039,7 @@ cspi_dbus_call (Accessible *obj, const char *interface, const char *method, DBus
   if (!error) error = &err;
   dbus_error_init (error);
   va_start (args, type);
-  retval = dbind_connection_method_call_va (SPI_bus(), obj->app->bus_name, path, interface, method, error, type, args);
+  retval = dbind_method_call_reentrant_va (SPI_bus(), obj->app->bus_name, path, interface, method, error, type, args);
   va_end (args);
   g_free (path);
   if (dbus_error_is_set (error))