Modified to support char type.
[platform/upstream/dbus.git] / glib / dbus-gobject.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gobject.c Exporting a GObject remotely
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.0
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <config.h>
25 #include "dbus-glib.h"
26 #include "dbus-gtest.h"
27 #include "dbus-gutils.h"
28 #include <string.h>
29
30 /**
31  * @addtogroup DBusGLibInternals
32  * @{
33  */
34
35 static GStaticMutex info_hash_mutex = G_STATIC_MUTEX_INIT;
36 static GHashTable *info_hash = NULL;
37
38 static char*
39 wincaps_to_uscore (const char *caps)
40 {
41   const char *p;
42   GString *str;
43
44   str = g_string_new (NULL);
45   p = caps;
46   while (*p)
47     {
48       if (g_ascii_isupper (*p))
49         {
50           if (str->len > 0 &&
51               (str->len < 2 || str->str[str->len-2] != '_'))
52             g_string_append_c (str, '_');
53           g_string_append_c (str, g_ascii_tolower (*p));
54         }
55       else
56         {
57           g_string_append_c (str, *p);
58         }
59       ++p;
60     }
61
62   return g_string_free (str, FALSE);
63 }
64
65 static char*
66 uscore_to_wincaps (const char *uscore)
67 {
68   const char *p;
69   GString *str;
70   gboolean last_was_uscore;
71
72   last_was_uscore = TRUE;
73   
74   str = g_string_new (NULL);
75   p = uscore;
76   while (*p)
77     {
78       if (*p == '-' || *p == '_')
79         {
80           last_was_uscore = TRUE;
81         }
82       else
83         {
84           if (last_was_uscore)
85             {
86               g_string_append_c (str, g_ascii_toupper (*p));
87               last_was_uscore = FALSE;
88             }
89           else
90             g_string_append_c (str, *p);
91         }
92       ++p;
93     }
94
95   return g_string_free (str, FALSE);
96 }
97
98 static void
99 gobject_unregister_function (DBusConnection  *connection,
100                              void            *user_data)
101 {
102   GObject *object;
103
104   object = G_OBJECT (user_data);
105
106   /* FIXME */
107
108 }
109
110 static int
111 gtype_to_dbus_type (GType type)
112 {
113   switch (type)
114     {
115     case G_TYPE_CHAR:
116     case G_TYPE_UCHAR:
117       return DBUS_TYPE_BYTE;
118       
119     case G_TYPE_BOOLEAN:
120       return DBUS_TYPE_BOOLEAN;
121
122       /* long gets cut to 32 bits so the remote API is consistent
123        * on all architectures
124        */
125       
126     case G_TYPE_LONG:
127     case G_TYPE_INT:
128       return DBUS_TYPE_INT32;
129     case G_TYPE_ULONG:
130     case G_TYPE_UINT:
131       return DBUS_TYPE_UINT32;
132
133     case G_TYPE_INT64:
134       return DBUS_TYPE_INT64;
135
136     case G_TYPE_UINT64:
137       return DBUS_TYPE_UINT64;
138       
139     case G_TYPE_FLOAT:
140     case G_TYPE_DOUBLE:
141       return DBUS_TYPE_DOUBLE;
142
143     case G_TYPE_STRING:
144       return DBUS_TYPE_STRING;
145
146     default:
147       return DBUS_TYPE_INVALID;
148     }
149 }
150
151 static const char *
152 dbus_type_to_string (int type)
153 {
154   switch (type)
155     {
156     case DBUS_TYPE_INVALID:
157       return "invalid";
158     case DBUS_TYPE_NIL:
159       return "nil";
160     case DBUS_TYPE_BOOLEAN:
161       return "boolean";
162     case DBUS_TYPE_INT32:
163       return "int32";
164     case DBUS_TYPE_UINT32:
165       return "uint32";
166     case DBUS_TYPE_DOUBLE:
167       return "double";
168     case DBUS_TYPE_STRING:
169       return "string";
170     case DBUS_TYPE_CUSTOM:
171       return "custom";
172     case DBUS_TYPE_ARRAY:
173       return "array";
174     case DBUS_TYPE_DICT:
175       return "dict";
176     default:
177       return "unknown";
178     }
179 }
180
181 static DBusHandlerResult
182 handle_introspect (DBusConnection *connection,
183                    DBusMessage    *message,
184                    GObject        *object)
185 {
186   GString *xml;
187   GParamSpec **specs;
188   unsigned int n_specs;
189   unsigned int i;
190   GType last_type;
191   DBusMessage *ret;
192   char **path;
193   char **children;
194   
195   if (!dbus_message_get_path_decomposed (message, &path))
196     g_error ("Out of memory");
197
198   if (!dbus_connection_list_registered (connection, (const char**) path,
199                                         &children))
200     g_error ("Out of memory");
201   
202   xml = g_string_new (NULL);
203
204   g_string_append (xml, "<node>\n");
205
206   last_type = G_TYPE_INVALID;
207
208   specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
209                                           &n_specs);
210
211   i = 0;
212   while (i < n_specs)
213     {
214       GParamSpec *spec = specs[i];
215       gboolean can_set;
216       gboolean can_get;
217       char *s;
218       int dbus_type;
219       
220       dbus_type = gtype_to_dbus_type (G_PARAM_SPEC_VALUE_TYPE (spec));
221       if (dbus_type == DBUS_TYPE_INVALID)
222         goto next;
223       
224       if (spec->owner_type != last_type)
225         {
226           if (last_type != G_TYPE_INVALID)
227             g_string_append (xml, "  </interface>\n");
228
229
230           /* FIXME what should the namespace on the interface be in
231            * general?  should people be able to set it for their
232            * objects?
233            */
234           
235           g_string_append (xml, "  <interface name=\"org.gtk.objects.");
236           g_string_append (xml, g_type_name (spec->owner_type));
237           g_string_append (xml, "\">\n");
238
239           last_type = spec->owner_type;
240         }
241
242       can_set = ((spec->flags & G_PARAM_WRITABLE) != 0 &&
243                     (spec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
244
245       can_get = (spec->flags & G_PARAM_READABLE) != 0;
246
247       s = uscore_to_wincaps (spec->name);
248       
249       if (can_set)
250         {
251           g_string_append (xml, "    <method name=\"set_");
252           g_string_append (xml, s);
253           g_string_append (xml, "\">\n");
254           
255           g_string_append (xml, "      <arg type=\"");
256           g_string_append (xml, dbus_type_to_string (dbus_type));
257           g_string_append (xml, "\"/>\n");
258         }
259
260       if (can_get)
261         {
262           g_string_append (xml, "    <method name=\"get_");
263           g_string_append (xml, s);
264           g_string_append (xml, "\">\n");
265           
266           g_string_append (xml, "      <arg type=\"");
267           g_string_append (xml, dbus_type_to_string (dbus_type));
268           g_string_append (xml, "\" direction=\"out\"/>\n");
269         }
270
271       g_free (s);
272
273     next:
274       ++i;
275     }
276
277   if (last_type != G_TYPE_INVALID)
278     g_string_append (xml, "  </interface>\n");
279
280   g_free (specs);
281
282   /* Append child nodes */
283   
284   i = 0;
285   while (children[i])
286     {
287       g_string_append_printf (xml, "  <node name=\"%s\"/>\n",
288                               children[i]);
289       ++i;
290     }
291   
292   /* Close the XML, and send it to the requesting app */
293
294   g_string_append (xml, "</node>\n");
295
296   ret = dbus_message_new_method_return (message);
297   if (ret == NULL)
298     g_error ("Out of memory");
299
300   dbus_message_append_args (message,
301                             DBUS_TYPE_STRING, xml->str,
302                             DBUS_TYPE_INVALID);
303
304   dbus_connection_send (connection, message, NULL);
305   dbus_message_unref (message);
306
307   g_string_free (xml, TRUE);
308
309   dbus_free_string_array (path);
310   dbus_free_string_array (children);
311   
312   return DBUS_HANDLER_RESULT_HANDLED;
313 }
314
315 static DBusMessage*
316 set_object_property (DBusConnection *connection,
317                      DBusMessage    *message,
318                      GObject        *object,
319                      GParamSpec     *pspec)
320 {
321   GValue value;
322   DBusMessageIter iter;
323   int type;
324   gboolean can_set;
325   DBusMessage *ret;
326
327   dbus_message_iter_init (message, &iter);
328   type = dbus_message_get_type (message);
329
330   can_set = TRUE;
331   switch (type)
332     {
333     case DBUS_TYPE_BYTE:
334       {
335         unsigned char b;
336
337         b = dbus_message_iter_get_byte (&iter);
338
339         g_value_init (&value, G_TYPE_UCHAR);
340
341         g_value_set_uchar (&value, b);
342       }
343       break;
344     case DBUS_TYPE_BOOLEAN:
345       {
346         gboolean b;
347
348         b = dbus_message_iter_get_boolean (&iter);
349
350         g_value_init (&value, G_TYPE_BOOLEAN);
351
352         g_value_set_boolean (&value, b);
353       }
354       break;
355     case DBUS_TYPE_INT32:
356       {
357         gint32 i;
358
359         i = dbus_message_iter_get_int32 (&iter);
360
361         g_value_init (&value, G_TYPE_INT);
362
363         g_value_set_int (&value, i);
364       }
365       break;
366     case DBUS_TYPE_UINT32:
367       {
368         guint32 i;
369
370         i = dbus_message_iter_get_uint32 (&iter);
371
372         g_value_init (&value, G_TYPE_UINT);
373
374         g_value_set_uint (&value, i);
375       }
376       break;
377     case DBUS_TYPE_INT64:
378       {
379         gint64 i;
380
381         i = dbus_message_iter_get_int64 (&iter);
382
383         g_value_init (&value, G_TYPE_INT64);
384
385         g_value_set_int64 (&value, i);
386       }
387       break;
388     case DBUS_TYPE_UINT64:
389       {
390         guint64 i;
391
392         i = dbus_message_iter_get_uint64 (&iter);
393
394         g_value_init (&value, G_TYPE_UINT64);
395
396         g_value_set_uint64 (&value, i);
397       }
398       break;
399     case DBUS_TYPE_DOUBLE:
400       {
401         double d;
402
403         d = dbus_message_iter_get_double (&iter);
404
405         g_value_init (&value, G_TYPE_DOUBLE);
406
407         g_value_set_double (&value, d);
408       }
409       break;
410     case DBUS_TYPE_STRING:
411       {
412         char *s;
413
414         /* FIXME use a const string accessor */
415
416         s = dbus_message_iter_get_string (&iter);
417
418         g_value_init (&value, G_TYPE_STRING);
419
420         g_value_set_string (&value, s);
421
422         g_free (s);
423       }
424       break;
425
426       /* FIXME array and other types, especially byte array
427        * converted to G_TYPE_STRING
428        */
429
430     default:
431       can_set = FALSE;
432       break;
433     }
434
435   /* The g_object_set_property() will transform some types, e.g. it
436    * will let you use a uchar to set an int property etc. Note that
437    * any error in value range or value conversion will just
438    * g_warning(). These GObject skels are not for secure applications.
439    */
440
441   if (can_set)
442     {
443       g_object_set_property (object,
444                              pspec->name,
445                              &value);
446
447       ret = dbus_message_new_method_return (message);
448       if (ret == NULL)
449         g_error ("out of memory");
450
451       g_value_unset (&value);
452     }
453   else
454     {
455       ret = dbus_message_new_error (message,
456                                     DBUS_ERROR_INVALID_ARGS,
457                                     "Argument's D-BUS type can't be converted to a GType");
458       if (ret == NULL)
459         g_error ("out of memory");
460     }
461
462   return ret;
463 }
464
465 static DBusMessage*
466 get_object_property (DBusConnection *connection,
467                      DBusMessage    *message,
468                      GObject        *object,
469                      GParamSpec     *pspec)
470 {
471   GType value_type;
472   gboolean can_get;
473   DBusMessage *ret;
474   GValue value;
475   DBusMessageIter iter;
476
477   value_type = G_PARAM_SPEC_VALUE_TYPE (pspec);
478
479   ret = dbus_message_new_method_return (message);
480   if (ret == NULL)
481     g_error ("out of memory");
482
483   can_get = TRUE;
484   g_value_init (&value, value_type);
485   g_object_get_property (object, pspec->name, &value);
486
487   value_type = G_VALUE_TYPE (&value);
488
489   dbus_message_append_iter_init (message, &iter);
490   
491   switch (value_type)
492     {
493     case G_TYPE_CHAR:
494       dbus_message_iter_append_byte (&iter,
495                                      g_value_get_char (&value));
496       break;
497     case G_TYPE_UCHAR:
498       dbus_message_iter_append_byte (&iter,
499                                      g_value_get_uchar (&value));
500       break;
501     case G_TYPE_BOOLEAN:
502       dbus_message_iter_append_boolean (&iter,
503                                         g_value_get_boolean (&value));
504       break;
505     case G_TYPE_INT:
506       dbus_message_iter_append_int32 (&iter,
507                                       g_value_get_int (&value));
508       break;
509     case G_TYPE_UINT:
510       dbus_message_iter_append_uint32 (&iter,
511                                        g_value_get_uint (&value));
512       break;
513       /* long gets cut to 32 bits so the remote API is consistent
514        * on all architectures
515        */
516     case G_TYPE_LONG:
517       dbus_message_iter_append_int32 (&iter,
518                                       g_value_get_long (&value));
519       break;
520     case G_TYPE_ULONG:
521       dbus_message_iter_append_uint32 (&iter,
522                                        g_value_get_ulong (&value));
523       break;
524     case G_TYPE_INT64:
525       dbus_message_iter_append_int64 (&iter,
526                                       g_value_get_int64 (&value));
527       break;
528     case G_TYPE_UINT64:
529       dbus_message_iter_append_uint64 (&iter,
530                                        g_value_get_uint64 (&value));
531       break;
532     case G_TYPE_FLOAT:
533       dbus_message_iter_append_double (&iter,
534                                        g_value_get_float (&value));
535       break;
536     case G_TYPE_DOUBLE:
537       dbus_message_iter_append_double (&iter,
538                                        g_value_get_double (&value));
539       break;
540     case G_TYPE_STRING:
541       /* FIXME, the GValue string may not be valid UTF-8 */
542       dbus_message_iter_append_string (&iter,
543                                        g_value_get_string (&value));
544       break;
545     default:
546       can_get = FALSE;
547       break;
548     }
549
550   g_value_unset (&value);
551
552   if (!can_get)
553     {
554       dbus_message_unref (ret);
555       ret = dbus_message_new_error (message,
556                                     DBUS_ERROR_UNKNOWN_METHOD,
557                                     "Can't convert GType of object property to a D-BUS type");
558     }
559
560   return ret;
561 }
562
563 static DBusHandlerResult
564 gobject_message_function (DBusConnection  *connection,
565                           DBusMessage     *message,
566                           void            *user_data)
567 {
568   const DBusGObjectInfo *info;
569   GParamSpec *pspec;
570   GObject *object;
571   const char *member;
572   gboolean setter;
573   gboolean getter;
574   char *s;
575
576   object = G_OBJECT (user_data);
577
578   if (dbus_message_is_method_call (message,
579                                    DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
580                                    "Introspect"))
581     return handle_introspect (connection, message, object);
582
583   member = dbus_message_get_member (message);
584
585   /* Try the metainfo, which lets us invoke methods */
586
587   g_static_mutex_lock (&info_hash_mutex);
588   /* FIXME this needs to walk up the inheritance tree, not
589    * just look at the most-derived class
590    */
591   info = g_hash_table_lookup (info_hash,
592                               G_OBJECT_GET_CLASS (object));
593   g_static_mutex_unlock (&info_hash_mutex);
594
595   if (info != NULL)
596     {
597
598
599
600     }
601
602   /* If no metainfo, we can still do properties and signals
603    * via standard GLib introspection
604    */
605   setter = (member[0] == 's' && member[1] == 'e' && member[2] == 't' && member[3] == '_');
606   getter = (member[0] == 'g' && member[1] == 'e' && member[2] == 't' && member[3] == '_');
607
608   if (!(setter || getter))
609     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
610
611   s = wincaps_to_uscore (&member[4]);
612
613   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object),
614                                         s);
615
616   g_free (s);
617
618   if (pspec != NULL)
619     {
620       DBusMessage *ret;
621
622       if (setter)
623         {
624           ret = set_object_property (connection, message,
625                                      object, pspec);
626         }
627       else if (getter)
628         {
629           ret = get_object_property (connection, message,
630                                      object, pspec);
631         }
632       else
633         {
634           g_assert_not_reached ();
635           ret = NULL;
636         }
637
638       g_assert (ret != NULL);
639
640       dbus_connection_send (connection, ret, NULL);
641       dbus_message_unref (ret);
642       return DBUS_HANDLER_RESULT_HANDLED;
643     }
644
645   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
646 }
647
648 static DBusObjectPathVTable gobject_dbus_vtable = {
649   gobject_unregister_function,
650   gobject_message_function,
651   NULL
652 };
653
654 /** @} */ /* end of internals */
655
656 /**
657  * @addtogroup DBusGLib
658  * @{
659  */
660
661 /**
662  * Install introspection information about the given object class
663  * sufficient to allow methods on the object to be invoked by name.
664  * The introspection information is normally generated by
665  * dbus-glib-tool, then this function is called in the
666  * class_init() for the object class.
667  *
668  * Once introspection information has been installed, instances of the
669  * object registered with dbus_connection_register_g_object() can have
670  * their methods invoked remotely.
671  *
672  * @param object_class class struct of the object
673  * @param info introspection data generated by dbus-glib-tool
674  */
675 void
676 dbus_g_object_class_install_info (GObjectClass          *object_class,
677                                   const DBusGObjectInfo *info)
678 {
679   g_return_if_fail (G_IS_OBJECT_CLASS (object_class));
680
681   g_static_mutex_lock (&info_hash_mutex);
682
683   if (info_hash == NULL)
684     {
685       info_hash = g_hash_table_new (NULL, NULL); /* direct hash */
686     }
687
688   g_hash_table_replace (info_hash, object_class, (void*) info);
689
690   g_static_mutex_unlock (&info_hash_mutex);
691 }
692
693 /**
694  * Registers a GObject at the given path. Properties, methods, and signals
695  * of the object can then be accessed remotely. Methods are only available
696  * if method introspection data has been added to the object's class
697  * with g_object_class_install_info().
698  *
699  * The registration will be cancelled if either the DBusConnection or
700  * the GObject gets finalized.
701  *
702  * @param connection the D-BUS connection
703  * @param at_path the path where the object will live (the object's name)
704  * @param object the object
705  */
706 void
707 dbus_connection_register_g_object (DBusConnection        *connection,
708                                    const char            *at_path,
709                                    GObject               *object)
710 {
711   char **split;
712
713   g_return_if_fail (connection != NULL);
714   g_return_if_fail (at_path != NULL);
715   g_return_if_fail (G_IS_OBJECT (object));
716
717   split = _dbus_gutils_split_path (at_path);
718
719   if (!dbus_connection_register_object_path (connection,
720                                              (const char**) split,
721                                              &gobject_dbus_vtable,
722                                              object))
723     g_error ("Failed to register GObject with DBusConnection");
724
725   g_strfreev (split);
726
727   /* FIXME set up memory management (so we break the
728    * registration if object or connection vanishes)
729    */
730 }
731
732 /** @} */ /* end of public API */
733
734 #ifdef DBUS_BUILD_TESTS
735 #include <stdlib.h>
736
737 /**
738  * @ingroup DBusGLibInternals
739  * Unit test for GLib GObject integration ("skeletons")
740  * @returns #TRUE on success.
741  */
742 dbus_bool_t
743 _dbus_gobject_test (const char *test_data_dir)
744 {
745   int i;
746   static struct { const char *wincaps; const char *uscore; } name_pairs[] = {
747     { "SetFoo", "set_foo" },
748     { "Foo", "foo" },
749     { "GetFooBar", "get_foo_bar" },
750     { "Hello", "hello" }
751     
752     /* Impossible-to-handle cases */
753     /* { "FrobateUIHandler", "frobate_ui_handler" } */
754   };
755
756   i = 0;
757   while (i < (int) G_N_ELEMENTS (name_pairs))
758     {
759       char *uscore;
760       char *wincaps;
761
762       uscore = wincaps_to_uscore (name_pairs[i].wincaps);
763       wincaps = uscore_to_wincaps (name_pairs[i].uscore);
764
765       if (strcmp (uscore, name_pairs[i].uscore) != 0)
766         {
767           g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
768                       name_pairs[i].wincaps, name_pairs[i].uscore,
769                       uscore);
770           exit (1);
771         }
772       
773       if (strcmp (wincaps, name_pairs[i].wincaps) != 0)
774         {
775           g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
776                       name_pairs[i].uscore, name_pairs[i].wincaps,
777                       wincaps);
778           exit (1);
779         }
780       
781       g_free (uscore);
782       g_free (wincaps);
783
784       ++i;
785     }
786   
787   return TRUE;
788 }
789
790 #endif /* DBUS_BUILD_TESTS */