comparison src/dbus-server.c @ 11175:57af14280b5f

[gaim-migrate @ 13280] added DBus introspection support committer: Tailor Script <tailor@pidgin.im>
author Piotr Zielinski <zielaj>
date Sat, 30 Jul 2005 17:05:38 +0000
parents ebb02ea3c789
children 744c0708d11f
comparison
equal deleted inserted replaced
11174:9aae0a11de03 11175:57af14280b5f
192 g_slist_free(list); 192 g_slist_free(list);
193 193
194 return array; 194 return array;
195 } 195 }
196 196
197
198 /**************************************************************/ 197 /**************************************************************/
199 /* DBus bindings ... */ 198 /* DBus bindings ... */
200 /**************************************************************/ 199 /**************************************************************/
201 200
202 static DBusConnection *gaim_dbus_connection; 201 static DBusConnection *gaim_dbus_connection;
222 GaimDBusBinding *bindings; 221 GaimDBusBinding *bindings;
223 int i; 222 int i;
224 223
225 bindings = (GaimDBusBinding*) user_data; 224 bindings = (GaimDBusBinding*) user_data;
226 225
227 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL) 226 if (!dbus_message_has_path(message, DBUS_PATH_GAIM))
228 return FALSE; 227 return FALSE;
229 228
230 if (!dbus_message_has_path(message, DBUS_PATH_GAIM)) 229 name = dbus_message_get_member(message);
230
231 if (name == NULL)
231 return FALSE; 232 return FALSE;
232 233
233 name = dbus_message_get_member(message); 234 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
234
235 if (name == NULL)
236 return FALSE; 235 return FALSE;
237 236
238 for(i=0; bindings[i].name; i++) 237 for(i=0; bindings[i].name; i++)
239 if (!strcmp(name, bindings[i].name)) { 238 if (!strcmp(name, bindings[i].name)) {
240 DBusMessage *reply; 239 DBusMessage *reply;
258 } 257 }
259 258
260 return FALSE; 259 return FALSE;
261 } 260 }
262 261
262
263 /* Introspection */
264
265 static const char *gettext(const char **ptr) {
266 const char *text = *ptr;
267 *ptr += strlen(text) + 1;
268 return text;
269 }
270
271 static void
272 gaim_dbus_introspect_cb(GList **bindings_list, void *bindings) {
273 *bindings_list = g_list_prepend(*bindings_list, bindings);
274 }
275
276 static DBusMessage *gaim_dbus_introspect(DBusMessage *message)
277 {
278 DBusMessage *reply;
279 GString *str;
280 GList *bindings_list, *node;
281
282 str = g_string_sized_new(0x1000); /* fixme: why this size? */
283
284 g_string_append(str, "<!DOCTYPE node PUBLIC '-//freedesktop//DTD D-BUS Object Introspection 1.0//EN' 'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd'>\n");
285 g_string_append_printf(str, "<node name='%s'>\n", DBUS_PATH_GAIM);
286 g_string_append_printf(str, "<interface name='%s'>\n", DBUS_INTERFACE_GAIM);
287
288 bindings_list = NULL;
289 gaim_signal_emit(gaim_dbus_get_handle(), "dbus-introspect", &bindings_list);
290
291 for(node = bindings_list; node; node = node->next) {
292 GaimDBusBinding *bindings;
293 int i;
294
295 bindings = (GaimDBusBinding*) node->data;
296
297 for(i=0; bindings[i].name; i++) {
298 const char *text;
299
300 g_string_append_printf(str, "<method name='%s'>\n", bindings[i].name);
301
302 text = bindings[i].parameters;
303 while (*text) {
304 const char *name, *direction, *type;
305
306 direction = gettext(&text);
307 type = gettext(&text);
308 name = gettext(&text);
309
310 g_string_append_printf(str,
311 "<arg name='%s' type='%s' direction='%s'/>\n",
312 name, type, direction);
313 }
314 g_string_append(str, "</method>\n");
315 }
316 }
317
318 g_string_append(str, "</interface>\n</node>\n");
319
320 reply = dbus_message_new_method_return (message);
321 dbus_message_append_args(reply, DBUS_TYPE_STRING, &(str->str),
322 DBUS_TYPE_INVALID);
323 g_string_free(str, TRUE);
324 g_list_free(bindings_list);
325
326 return reply;
327
328 }
329
263 static DBusHandlerResult gaim_dbus_dispatch(DBusConnection *connection, 330 static DBusHandlerResult gaim_dbus_dispatch(DBusConnection *connection,
264 DBusMessage *message, 331 DBusMessage *message,
265 void *user_data) 332 void *user_data)
266 { 333 {
267 if (gaim_signal_emit_return_1(gaim_dbus_get_handle(), 334 if (gaim_signal_emit_return_1(gaim_dbus_get_handle(),
268 "dbus-method-called", 335 "dbus-method-called",
269 connection, message)) 336 connection, message))
270 return DBUS_HANDLER_RESULT_HANDLED; 337 return DBUS_HANDLER_RESULT_HANDLED;
271 else 338
272 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 339 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL &&
340 dbus_message_has_path(message, DBUS_PATH_GAIM) &&
341 dbus_message_has_interface(message, DBUS_INTERFACE_INTROSPECTABLE) &&
342 dbus_message_has_member(message, "Introspect"))
343 {
344 DBusMessage *reply;
345 reply = gaim_dbus_introspect(message);
346 dbus_connection_send (connection, reply, NULL);
347 dbus_message_unref(reply);
348 return DBUS_HANDLER_RESULT_HANDLED;
349 }
350
351 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
273 } 352 }
274 353
275 void gaim_dbus_register_bindings(void *handle, GaimDBusBinding *bindings) { 354 void gaim_dbus_register_bindings(void *handle, GaimDBusBinding *bindings) {
276 gaim_signal_connect(gaim_dbus_get_handle(), "dbus-method-called", 355 gaim_signal_connect(gaim_dbus_get_handle(), "dbus-method-called",
277 handle, 356 handle,
278 GAIM_CALLBACK(gaim_dbus_dispatch_cb), 357 GAIM_CALLBACK(gaim_dbus_dispatch_cb),
358 bindings);
359 gaim_signal_connect(gaim_dbus_get_handle(), "dbus-introspect",
360 handle,
361 GAIM_CALLBACK(gaim_dbus_introspect_cb),
279 bindings); 362 bindings);
280 } 363 }
281 364
282 365
283 366
323 gaim_signal_register(gaim_dbus_get_handle(), "dbus-method-called", 406 gaim_signal_register(gaim_dbus_get_handle(), "dbus-method-called",
324 gaim_marshal_BOOLEAN__POINTER_POINTER, 407 gaim_marshal_BOOLEAN__POINTER_POINTER,
325 gaim_value_new(GAIM_TYPE_BOOLEAN), 2, 408 gaim_value_new(GAIM_TYPE_BOOLEAN), 2,
326 gaim_value_new(GAIM_TYPE_POINTER), 409 gaim_value_new(GAIM_TYPE_POINTER),
327 gaim_value_new(GAIM_TYPE_POINTER)); 410 gaim_value_new(GAIM_TYPE_POINTER));
411
412 gaim_signal_register(gaim_dbus_get_handle(), "dbus-introspect",
413 gaim_marshal_VOID__POINTER, NULL, 1,
414 gaim_value_new_outgoing(GAIM_TYPE_POINTER));
328 415
329 GAIM_DBUS_REGISTER_BINDINGS(gaim_dbus_get_handle()); 416 GAIM_DBUS_REGISTER_BINDINGS(gaim_dbus_get_handle());
330 417
331 return TRUE; 418 return TRUE;
332 } 419 }
446 gaim_dbus_init_ids(); 533 gaim_dbus_init_ids();
447 return gaim_dbus_dispatch_init() ; 534 return gaim_dbus_dispatch_init() ;
448 } 535 }
449 536
450 537
538 /* Introspection support */
539
540
541