2008-09-25 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / registryd / desktop.c
index 70138fb..2b08d02 100644 (file)
 #include <config.h>
 #include <stdio.h>
 #include <string.h>
-#include "desktop.h"
-#include "registry.h"
 #include <atk/atkcomponent.h>
 #include <gdk/gdkscreen.h>
 #include <gdk/gdkx.h>
 
+#include <spi-common/spi-dbus.h>
+
+#include "desktop.h"
+#include "registry.h"
+
 G_DEFINE_TYPE(SpiDesktop, spi_desktop, G_TYPE_OBJECT)
 
 /* SpiDesktop signals */
@@ -46,11 +49,6 @@ static guint spi_desktop_signals[LAST_SIGNAL];
 /* Our parent Gtk object type */
 #define PARENT_TYPE SPI_ACCESSIBLE_TYPE
 
-typedef struct {
-       SpiDesktop *desktop;
-       const char *path;
-} Application;
-
 static gboolean exiting = FALSE;
 
 /* A pointer to our parent object class */
@@ -162,9 +160,9 @@ spi_desktop_dispose (GObject *object)
 
   while (desktop->applications)
     {
-      Application *app = desktop->applications->data;
+      SpiDesktopApplication *app = desktop->applications->data;
       g_assert (app != NULL);
-      spi_desktop_remove_application (desktop, app->path);
+      spi_desktop_remove_application (desktop, app->bus_name);
     }
 
   G_OBJECT_CLASS (parent_class)->dispose (object); 
@@ -192,8 +190,8 @@ impl_desktop_get_child_at_index (DBusConnection *bus, DBusMessage *message, void
   SpiDesktop *desktop = SPI_REGISTRY(user_data)->desktop;
   DBusError error;
   dbus_int32_t index;
-  Application *app;
-  const char *path;
+  SpiDesktopApplication *app;
+  const char *bus_name;
   DBusMessage *reply;
 
   dbus_error_init (&error);
@@ -202,17 +200,65 @@ impl_desktop_get_child_at_index (DBusConnection *bus, DBusMessage *message, void
     return spi_dbus_general_error (message);
   }
   app = g_list_nth_data (desktop->applications, index);
-  path = (app? app->path: SPI_DBUS_PATH_NULL);
+  bus_name = (app? app->bus_name: "");
 
   reply = dbus_message_new_method_return (message);
   if (reply)
     {
-      dbus_message_append_args (reply, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
+      dbus_message_append_args (reply, DBUS_TYPE_STRING, &bus_name, DBUS_TYPE_INVALID);
     }
 
   return reply;
 }
 
+static DBusMessage *
+impl_desktop_get_children (DBusConnection *bus, DBusMessage *message, void *user_data)
+{
+  SpiDesktop *desktop = SPI_REGISTRY(user_data)->desktop;
+  DBusError error;
+  gint count;
+  gint i;
+  SpiDesktopApplication *app;
+  DBusMessage *reply;
+  DBusMessageIter iter, iter_array;
+
+  reply = dbus_message_new_method_return (message);
+  if (!reply) return NULL;
+  dbus_message_iter_init_append (reply, &iter);
+  if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s", &iter_array))
+  {
+    goto oom;
+  }
+  count = g_list_length (desktop->applications);
+  for (i = 0; i < count; i++)
+  {
+    app = g_list_nth_data (desktop->applications, i);
+    if (!app)
+    {
+      g_warning ("Null app\n");
+      continue;
+    }
+    dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_STRING, &app->bus_name);
+  }
+  if (!dbus_message_iter_close_container (&iter, &iter_array))
+  {
+    goto oom;
+  }
+  return reply;
+oom:
+  // TODO: Handle out of memory
+  return reply;
+}
+
+static dbus_bool_t
+impl_get_name (const char *path, DBusMessageIter * iter, void *user_data)
+{
+  if (strcmp (path, SPI_DBUS_PATH_DESKTOP) != 0)
+    return FALSE;
+  // TODO: call atk_object_get_name
+  return droute_return_v_string (iter, "main");
+}
+
 static void
 spi_desktop_exiting (void)
 {
@@ -256,27 +302,25 @@ spi_desktop_new (void)
 }
 
 static void
-abnormal_application_termination (gpointer object, Application *app)
+abnormal_application_termination (gpointer object, SpiDesktopApplication *app)
 {
   g_return_if_fail (SPI_IS_DESKTOP (app->desktop));
 
   if (!exiting)
-    spi_desktop_remove_application (app->desktop, app->path);
+    spi_desktop_remove_application (app->desktop, app->bus_name);
 }
 
 void
 spi_desktop_add_application (SpiDesktop *desktop,
                             const char *application)
 {
-  Application       *app;
+  SpiDesktopApplication       *app;
 
   g_return_if_fail (SPI_IS_DESKTOP (desktop));
 
-  spi_desktop_remove_application (desktop, application);
-
-  app = g_new (Application, 1);
+  app = g_new (SpiDesktopApplication, 1);
   app->desktop = desktop;
-  app->path = application;
+  app->bus_name = g_strdup (application);
 
       desktop->applications = g_list_append (desktop->applications, app);
 
@@ -289,54 +333,61 @@ spi_desktop_add_application (SpiDesktop *desktop,
 
 void
 spi_desktop_remove_application (SpiDesktop *desktop,
-                               const char *path)
+                               const char *bus_name)
 {
   guint idx;
   GList *l;
+  SpiDesktopApplication *app;
 
-  g_return_if_fail (path != NULL);
+  g_return_if_fail (  bus_name != NULL);
   g_return_if_fail (SPI_IS_DESKTOP (desktop));
 
   idx = 0;
   for (l = desktop->applications; l; l = l->next)
     {
-      Application *app = (Application *) l->data;
+      app = (SpiDesktopApplication *) l->data;
 
-      if (!strcmp(app->path, path))
+      if (!strcmp(app->bus_name, bus_name))
         {
          break;
        }
       idx++;
     }
 
-  if (l)
-    {
-      Application *app = (Application *) l->data;
+  if (!l) return;
 
-      desktop->applications = g_list_delete_link (desktop->applications, l);
+  g_signal_emit (G_OBJECT (desktop), spi_desktop_signals[APPLICATION_REMOVED], 0, idx);
 
-      // TODO: unlisten for broken app, if appropriate
-      g_free (app);
-      
-      g_signal_emit (G_OBJECT (desktop), spi_desktop_signals[APPLICATION_REMOVED], 0, idx);
-    }
+  desktop->applications = g_list_delete_link (desktop->applications, l);
+
+  g_free (app->bus_name);
+  g_free (app);
 }
 
-static DRouteMethod methods[] =
+static DRouteMethod methods_desktop[] =
 {
   { impl_desktop_get_child_at_index, "getChildAtIndex" },
+  { impl_desktop_get_children, "getChildren" },
   { NULL, NULL }
 };
 
-static DRouteProperty properties[] =
+static DRouteProperty properties_desktop[] =
 {
   { impl_desktop_get_child_count, NULL, "getChildCount" },
   { NULL, NULL, NULL }
 };
 
+static DRouteProperty properties_accessible[] =
+{
+  { impl_get_name, "getName" },
+  { NULL, NULL }
+};
+
 void
 spi_registry_initialize_desktop_interface (DRouteData * data)
 {
-  droute_add_interface (data, "org.freedesktop.atspi.Desktop", methods,
-                       properties, NULL, NULL);
+  droute_add_interface (data, SPI_DBUS_INTERFACE_DESKTOP, methods_desktop,
+                       properties_desktop, NULL, NULL);
+  droute_add_interface (data, SPI_DBUS_INTERFACE_ACCESSIBLE, NULL,
+                       properties_accessible, NULL, NULL);
 };