summaryrefslogtreecommitdiff
path: root/libspi/introspectable.c
blob: ce691fdb8cacc5cc4d7269e019bffe04d10a15f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2008 Codethink Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include <dbus.h>

#include "droute.h"

/*
 * This file contains an implementation of the D-Bus introspectable interface.
 * For ATK objects.
 */

/*
 * Provides the dist install path for the introspection directory.
 */
#if !defined ATSPI_DBUS_INTROSPECTION_DIRECTORY
    #error "No introspection XML directory defined"
#endif

/*
 * This may be modified at run time to support 
 * command parameters for the introspection directory.
 */
char *atspi_introspection_directory = ATSPI_DBUS_INTROSPECTION_DIRECTORY;

static const char *introspection_header =
"<?xml version=\"1.0\"?>\n";

static const char *introspection_node_element =
"<node name=\"%s\">\n";

static const char *introspection_footer =
"</node>";

static void
append_interface (GString *str, const char *interface)
{
  char *filename;
  char *contents;
  gsize len;

  GError *err = NULL;

  filename = g_build_filename(introspection_directory, interface);

  if (g_file_get_contents(filename, &contents, &len, &err))
    {
      g_string_append_len(str, contents, len);
    }
  else
    {
      g_warning("AT-SPI: Cannot find introspection XML file %s - %s",
		filename, err->message);
      g_error_free();
    }

  g_string_append(str, "\n");
  g_free(filename);
  g_free(contents);
}

/*
 * There is an installation directory with files containing introspection xml.
 * Each file is named after the interface it describes.
 *
 * This function finds the names of each interface that the ATK object supports
 * and mem copies the introspection data from the file into the message.
 *
 * TODO - There could be some wicked caching here, but probably not neccessary.
 */
static DBusMessage *
impl_introspect (DBusConnection *bus, DBusMessage *message,
		 void *user_data)
{
  AtkObject *object;
  const char *path;
  GString *output;

  DBusMessage *reply;

  path = dbus_message_get_path(message);
  object = spi_dbus_get_path(path);

  output = g_string_new(introspection_header);
  
  g_string_append_printf(output, introspection_node_element, path);

  if (ATK_IS_ACTION (o))
      append_interface(output, "org.freedesktop.atspi.Action");

  if (ATK_IS_COMPONENT (o))
      bonobo_object_add_interface (bonobo_object (retval), BONOBO_OBJECT (spi_component_interface_new (o)));
      append_interface(output, "org.freedesktop.atspi.Component");

  if (ATK_IS_EDITABLE_TEXT (o))
      append_interface(output, "org.freedesktop.atspi.EditableText");
  else if (ATK_IS_TEXT (o))
      append_interface(output, "org.freedesktop.atspi.Text");

  if (ATK_IS_HYPERTEXT (o))
      bonobo_object_add_interface (bonobo_object (retval), BONOBO_OBJECT (spi_hypertext_interface_new (o)));
      append_interface(output, "org.freedesktop.atspi.Hypertext");

  if (ATK_IS_IMAGE (o))
      bonobo_object_add_interface (bonobo_object (retval), BONOBO_OBJECT (spi_image_interface_new (o)));
      append_interface(output, "org.freedesktop.atspi.Image");

  if (ATK_IS_SELECTION (o))
      bonobo_object_add_interface (bonobo_object (retval), BONOBO_OBJECT (spi_selection_interface_new (o)));
      append_interface(output, "org.freedesktop.atspi.Selection");

  if (ATK_IS_TABLE (o))
      bonobo_object_add_interface (bonobo_object (retval), BONOBO_OBJECT (spi_table_interface_new (o)));
      append_interface(output, "org.freedesktop.atspi.Table");

  if (ATK_IS_VALUE (o))
      append_interface(output, "org.freedesktop.atspi.Value");

  if (ATK_IS_STREAMABLE_CONTENT (o))
      append_interface(output, "org.freedesktop.atspi.StreamableContent");

  if (ATK_IS_DOCUMENT (o))
    {
      append_interface(output, "org.freedesktop.atspi.Collection");
      append_interface(output, "org.freedesktop.atspi.Document");
    }

  if (ATK_IS_HYPERLINK_IMPL (o))
      append_interface(output, "org.freedesktop.atspi.Hyperlink");

  g_string_append(introspection_footer);
  final = g_string_free(output, FALSE);

  reply = dbus_message_new_method_return (message);
  g_assert(reply != NULL);
  dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
			   DBUS_TYPE_INVALID);

  g_free(final);
  return reply;
}


static DRouteMethod methods[] = {
  {impl_introspect, "Introspect"},
  {NULL, NULL}
};

/*
 * Adds the introspectable interface to the DRoute object provided
 */
void
spi_initialize_introspectable (DRouteData *data)
{
  droute_add_interface (data, "org.freedesktop.atspi.Accessible",
			methods, NULL,
			(DRouteGetDatumFunction) spi_dbus_get_path, NULL);
};