Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gdbusintrospection.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "gdbusintrospection.h"
29
30 #include "glibintl.h"
31
32 /**
33  * SECTION:gdbusintrospection
34  * @title: D-Bus Introspection Data
35  * @short_description: Node and interface description data structures
36  * @include: gio/gio.h
37  *
38  * Various data structures and convenience routines to parse and
39  * generate D-Bus introspection XML. Introspection information is
40  * used when registering objects with g_dbus_connection_register_object().
41  *
42  * The format of D-Bus introspection XML is specified in the
43  * <link linkend="http://dbus.freedesktop.org/doc/dbus-specification.html&num;introspection-format">D-Bus specification</link>.
44  */
45
46 /* ---------------------------------------------------------------------------------------------------- */
47
48 #define _MY_DEFINE_BOXED_TYPE(TypeName, type_name) \
49   G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name##_ref, type_name##_unref)
50
51 _MY_DEFINE_BOXED_TYPE (GDBusNodeInfo,       g_dbus_node_info);
52 _MY_DEFINE_BOXED_TYPE (GDBusInterfaceInfo,  g_dbus_interface_info);
53 _MY_DEFINE_BOXED_TYPE (GDBusMethodInfo,     g_dbus_method_info);
54 _MY_DEFINE_BOXED_TYPE (GDBusSignalInfo,     g_dbus_signal_info);
55 _MY_DEFINE_BOXED_TYPE (GDBusPropertyInfo,   g_dbus_property_info);
56 _MY_DEFINE_BOXED_TYPE (GDBusArgInfo,        g_dbus_arg_info);
57 _MY_DEFINE_BOXED_TYPE (GDBusAnnotationInfo, g_dbus_annotation_info);
58
59 /* ---------------------------------------------------------------------------------------------------- */
60
61 typedef struct
62 {
63   /* stuff we are currently collecting */
64   GPtrArray *args;
65   GPtrArray *out_args;
66   GPtrArray *methods;
67   GPtrArray *signals;
68   GPtrArray *properties;
69   GPtrArray *interfaces;
70   GPtrArray *nodes;
71   GPtrArray *annotations;
72
73   /* A list of GPtrArray's containing annotations */
74   GSList *annotations_stack;
75
76   /* A list of GPtrArray's containing interfaces */
77   GSList *interfaces_stack;
78
79   /* A list of GPtrArray's containing nodes */
80   GSList *nodes_stack;
81
82   /* Whether the direction was "in" for last parsed arg */
83   gboolean last_arg_was_in;
84
85   /* Number of args currently being collected; used for assigning
86    * names to args without a "name" attribute
87    */
88   guint num_args;
89
90 } ParseData;
91
92 /* ---------------------------------------------------------------------------------------------------- */
93
94 /**
95  * g_dbus_node_info_ref:
96  * @info: A #GDBusNodeInfo
97  *
98  * If @info is statically allocated does nothing. Otherwise increases
99  * the reference count.
100  *
101  * Returns: The same @info.
102  *
103  * Since: 2.26
104  */
105 GDBusNodeInfo *
106 g_dbus_node_info_ref (GDBusNodeInfo *info)
107 {
108   if (info->ref_count == -1)
109     return info;
110   g_atomic_int_inc (&info->ref_count);
111   return info;
112 }
113
114 /**
115  * g_dbus_interface_info_ref:
116  * @info: A #GDBusInterfaceInfo
117  *
118  * If @info is statically allocated does nothing. Otherwise increases
119  * the reference count.
120  *
121  * Returns: The same @info.
122  *
123  * Since: 2.26
124  */
125 GDBusInterfaceInfo *
126 g_dbus_interface_info_ref (GDBusInterfaceInfo *info)
127 {
128   if (info->ref_count == -1)
129     return info;
130   g_atomic_int_inc (&info->ref_count);
131   return info;
132 }
133
134 /**
135  * g_dbus_method_info_ref:
136  * @info: A #GDBusMethodInfo
137  *
138  * If @info is statically allocated does nothing. Otherwise increases
139  * the reference count.
140  *
141  * Returns: The same @info.
142  *
143  * Since: 2.26
144  */
145 GDBusMethodInfo *
146 g_dbus_method_info_ref (GDBusMethodInfo *info)
147 {
148   if (info->ref_count == -1)
149     return info;
150   g_atomic_int_inc (&info->ref_count);
151   return info;
152 }
153
154 /**
155  * g_dbus_signal_info_ref:
156  * @info: A #GDBusSignalInfo
157  *
158  * If @info is statically allocated does nothing. Otherwise increases
159  * the reference count.
160  *
161  * Returns: The same @info.
162  *
163  * Since: 2.26
164  */
165 GDBusSignalInfo *
166 g_dbus_signal_info_ref (GDBusSignalInfo *info)
167 {
168   if (info->ref_count == -1)
169     return info;
170   g_atomic_int_inc (&info->ref_count);
171   return info;
172 }
173
174 /**
175  * g_dbus_property_info_ref:
176  * @info: A #GDBusPropertyInfo
177  *
178  * If @info is statically allocated does nothing. Otherwise increases
179  * the reference count.
180  *
181  * Returns: The same @info.
182  *
183  * Since: 2.26
184  */
185 GDBusPropertyInfo *
186 g_dbus_property_info_ref (GDBusPropertyInfo *info)
187 {
188   if (info->ref_count == -1)
189     return info;
190   g_atomic_int_inc (&info->ref_count);
191   return info;
192 }
193
194 /**
195  * g_dbus_arg_info_ref:
196  * @info: A #GDBusArgInfo
197  *
198  * If @info is statically allocated does nothing. Otherwise increases
199  * the reference count.
200  *
201  * Returns: The same @info.
202  *
203  * Since: 2.26
204  */
205 GDBusArgInfo *
206 g_dbus_arg_info_ref (GDBusArgInfo *info)
207 {
208   if (info->ref_count == -1)
209     return info;
210   g_atomic_int_inc (&info->ref_count);
211   return info;
212 }
213
214 /**
215  * g_dbus_annotation_info_ref:
216  * @info: A #GDBusNodeInfo
217  *
218  * If @info is statically allocated does nothing. Otherwise increases
219  * the reference count.
220  *
221  * Returns: The same @info.
222  *
223  * Since: 2.26
224  */
225 GDBusAnnotationInfo *
226 g_dbus_annotation_info_ref (GDBusAnnotationInfo *info)
227 {
228   if (info->ref_count == -1)
229     return info;
230   g_atomic_int_inc (&info->ref_count);
231   return info;
232 }
233
234 /* ---------------------------------------------------------------------------------------------------- */
235
236 static void
237 free_null_terminated_array (gpointer array, GDestroyNotify unref_func)
238 {
239   guint n;
240   gpointer *p = array;
241   if (p == NULL)
242     return;
243   for (n = 0; p[n] != NULL; n++)
244     unref_func (p[n]);
245   g_free (p);
246 }
247
248 /**
249  * g_dbus_annotation_info_unref:
250  * @info: A #GDBusAnnotationInfo.
251  *
252  * If @info is statically allocated, does nothing. Otherwise decreases
253  * the reference count of @info. When its reference count drops to 0,
254  * the memory used is freed.
255  *
256  * Since: 2.26
257  */
258 void
259 g_dbus_annotation_info_unref (GDBusAnnotationInfo *info)
260 {
261   if (info->ref_count == -1)
262     return;
263   if (g_atomic_int_dec_and_test (&info->ref_count))
264     {
265       g_free (info->key);
266       g_free (info->value);
267       free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
268       g_free (info);
269     }
270 }
271
272 /**
273  * g_dbus_arg_info_unref:
274  * @info: A #GDBusArgInfo.
275  *
276  * If @info is statically allocated, does nothing. Otherwise decreases
277  * the reference count of @info. When its reference count drops to 0,
278  * the memory used is freed.
279  *
280  * Since: 2.26
281  */
282 void
283 g_dbus_arg_info_unref (GDBusArgInfo *info)
284 {
285   if (info->ref_count == -1)
286     return;
287   if (g_atomic_int_dec_and_test (&info->ref_count))
288     {
289       g_free (info->name);
290       g_free (info->signature);
291       free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
292       g_free (info);
293     }
294 }
295
296 /**
297  * g_dbus_method_info_unref:
298  * @info: A #GDBusMethodInfo.
299  *
300  * If @info is statically allocated, does nothing. Otherwise decreases
301  * the reference count of @info. When its reference count drops to 0,
302  * the memory used is freed.
303  *
304  * Since: 2.26
305  */
306 void
307 g_dbus_method_info_unref (GDBusMethodInfo *info)
308 {
309   if (info->ref_count == -1)
310     return;
311   if (g_atomic_int_dec_and_test (&info->ref_count))
312     {
313       g_free (info->name);
314       free_null_terminated_array (info->in_args, (GDestroyNotify) g_dbus_arg_info_unref);
315       free_null_terminated_array (info->out_args, (GDestroyNotify) g_dbus_arg_info_unref);
316       free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
317       g_free (info);
318     }
319 }
320
321 /**
322  * g_dbus_signal_info_unref:
323  * @info: A #GDBusSignalInfo.
324  *
325  * If @info is statically allocated, does nothing. Otherwise decreases
326  * the reference count of @info. When its reference count drops to 0,
327  * the memory used is freed.
328  *
329  * Since: 2.26
330  */
331 void
332 g_dbus_signal_info_unref (GDBusSignalInfo *info)
333 {
334   if (info->ref_count == -1)
335     return;
336   if (g_atomic_int_dec_and_test (&info->ref_count))
337     {
338       g_free (info->name);
339       free_null_terminated_array (info->args, (GDestroyNotify) g_dbus_arg_info_unref);
340       free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
341       g_free (info);
342     }
343 }
344
345 /**
346  * g_dbus_property_info_unref:
347  * @info: A #GDBusPropertyInfo.
348  *
349  * If @info is statically allocated, does nothing. Otherwise decreases
350  * the reference count of @info. When its reference count drops to 0,
351  * the memory used is freed.
352  *
353  * Since: 2.26
354  */
355 void
356 g_dbus_property_info_unref (GDBusPropertyInfo *info)
357 {
358   if (info->ref_count == -1)
359     return;
360   if (g_atomic_int_dec_and_test (&info->ref_count))
361     {
362       g_free (info->name);
363       g_free (info->signature);
364       free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
365       g_free (info);
366     }
367 }
368
369 /**
370  * g_dbus_interface_info_unref:
371  * @info: A #GDBusInterfaceInfo.
372  *
373  * If @info is statically allocated, does nothing. Otherwise decreases
374  * the reference count of @info. When its reference count drops to 0,
375  * the memory used is freed.
376  *
377  * Since: 2.26
378  */
379 void
380 g_dbus_interface_info_unref (GDBusInterfaceInfo *info)
381 {
382   if (info->ref_count == -1)
383     return;
384   if (g_atomic_int_dec_and_test (&info->ref_count))
385     {
386       g_free (info->name);
387       free_null_terminated_array (info->methods, (GDestroyNotify) g_dbus_method_info_unref);
388       free_null_terminated_array (info->signals, (GDestroyNotify) g_dbus_signal_info_unref);
389       free_null_terminated_array (info->properties, (GDestroyNotify) g_dbus_property_info_unref);
390       free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
391       g_free (info);
392     }
393 }
394
395 /**
396  * g_dbus_node_info_unref:
397  * @info: A #GDBusNodeInfo.
398  *
399  * If @info is statically allocated, does nothing. Otherwise decreases
400  * the reference count of @info. When its reference count drops to 0,
401  * the memory used is freed.
402  *
403  * Since: 2.26
404  */
405 void
406 g_dbus_node_info_unref (GDBusNodeInfo *info)
407 {
408   if (info->ref_count == -1)
409     return;
410   if (g_atomic_int_dec_and_test (&info->ref_count))
411     {
412       g_free (info->path);
413       free_null_terminated_array (info->interfaces, (GDestroyNotify) g_dbus_interface_info_unref);
414       free_null_terminated_array (info->nodes, (GDestroyNotify) g_dbus_node_info_unref);
415       free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
416       g_free (info);
417     }
418 }
419
420 /* ---------------------------------------------------------------------------------------------------- */
421
422 static void
423 g_dbus_annotation_info_set (ParseData                      *data,
424                             GDBusAnnotationInfo            *info,
425                             const gchar                    *key,
426                             const gchar                    *value,
427                             GDBusAnnotationInfo **embedded_annotations)
428 {
429   info->ref_count = 1;
430
431   if (key != NULL)
432     info->key = g_strdup (key);
433
434   if (value != NULL)
435     info->value = g_strdup (value);
436
437   if (embedded_annotations != NULL)
438     info->annotations = embedded_annotations;
439 }
440
441 static void
442 g_dbus_arg_info_set (ParseData            *data,
443                      GDBusArgInfo         *info,
444                      const gchar          *name,
445                      const gchar          *signature,
446                      GDBusAnnotationInfo **annotations)
447 {
448   info->ref_count = 1;
449
450   /* name may be NULL - TODO: compute name? */
451   if (name != NULL)
452     info->name = g_strdup (name);
453
454   if (signature != NULL)
455     info->signature = g_strdup (signature);
456
457   if (annotations != NULL)
458     info->annotations = annotations;
459 }
460
461 static void
462 g_dbus_method_info_set (ParseData            *data,
463                         GDBusMethodInfo      *info,
464                         const gchar          *name,
465                         guint                 in_num_args,
466                         GDBusArgInfo        **in_args,
467                         guint                 out_num_args,
468                         GDBusArgInfo        **out_args,
469                         GDBusAnnotationInfo **annotations)
470 {
471   info->ref_count = 1;
472
473   if (name != NULL)
474     info->name = g_strdup (name);
475
476   if (in_num_args != 0)
477     {
478       //info->in_num_args = in_num_args;
479       info->in_args = in_args;
480     }
481
482   if (out_num_args != 0)
483     {
484       //info->out_num_args = out_num_args;
485       info->out_args = out_args;
486     }
487
488   if (annotations != NULL)
489     info->annotations = annotations;
490 }
491
492 static void
493 g_dbus_signal_info_set (ParseData            *data,
494                         GDBusSignalInfo      *info,
495                         const gchar          *name,
496                         guint                 num_args,
497                         GDBusArgInfo        **args,
498                         GDBusAnnotationInfo **annotations)
499 {
500   info->ref_count = 1;
501
502   if (name != NULL)
503     info->name = g_strdup (name);
504
505   if (num_args != 0)
506     {
507       //info->num_args = num_args;
508       info->args = args;
509     }
510
511   if (annotations != NULL)
512     {
513       info->annotations = annotations;
514     }
515 }
516
517 static void
518 g_dbus_property_info_set (ParseData               *data,
519                           GDBusPropertyInfo       *info,
520                           const gchar             *name,
521                           const gchar             *signature,
522                           GDBusPropertyInfoFlags   flags,
523                           GDBusAnnotationInfo    **annotations)
524 {
525   info->ref_count = 1;
526
527   if (name != NULL)
528     info->name = g_strdup (name);
529
530   if (flags != G_DBUS_PROPERTY_INFO_FLAGS_NONE)
531     info->flags = flags;
532
533   if (signature != NULL)
534     {
535       info->signature = g_strdup (signature);
536     }
537
538   if (annotations != NULL)
539     {
540       info->annotations = annotations;
541     }
542 }
543
544 static void
545 g_dbus_interface_info_set (ParseData            *data,
546                            GDBusInterfaceInfo   *info,
547                            const gchar          *name,
548                            guint                 num_methods,
549                            GDBusMethodInfo     **methods,
550                            guint                 num_signals,
551                            GDBusSignalInfo     **signals,
552                            guint                 num_properties,
553                            GDBusPropertyInfo   **properties,
554                            GDBusAnnotationInfo **annotations)
555 {
556   info->ref_count = 1;
557
558   if (name != NULL)
559     {
560       info->name = g_strdup (name);
561     }
562
563   if (num_methods != 0)
564     {
565       //info->num_methods    = num_methods;
566       info->methods        = methods;
567     }
568
569   if (num_signals != 0)
570     {
571       //info->num_signals    = num_signals;
572       info->signals        = signals;
573     }
574
575   if (num_properties != 0)
576     {
577       //info->num_properties = num_properties;
578       info->properties     = properties;
579     }
580
581   if (annotations != NULL)
582     {
583       info->annotations = annotations;
584     }
585 }
586
587 static void
588 g_dbus_node_info_set (ParseData            *data,
589                       GDBusNodeInfo        *info,
590                       const gchar          *path,
591                       guint                 num_interfaces,
592                       GDBusInterfaceInfo  **interfaces,
593                       guint                 num_nodes,
594                       GDBusNodeInfo       **nodes,
595                       GDBusAnnotationInfo **annotations)
596 {
597   info->ref_count = 1;
598
599   if (path != NULL)
600     {
601       info->path = g_strdup (path);
602       /* TODO: relative / absolute path snafu */
603     }
604
605   if (num_interfaces != 0)
606     {
607       //info->num_interfaces = num_interfaces;
608       info->interfaces     = interfaces;
609     }
610
611   if (num_nodes != 0)
612     {
613       //info->num_nodes      = num_nodes;
614       info->nodes          = nodes;
615     }
616
617   if (annotations != NULL)
618     {
619       info->annotations = annotations;
620     }
621
622 }
623
624 /* ---------------------------------------------------------------------------------------------------- */
625
626 static void
627 g_dbus_annotation_info_generate_xml (GDBusAnnotationInfo        *info,
628                                      guint                       indent,
629                                      GString                    *string_builder)
630 {
631   guint n;
632
633   g_string_append_printf (string_builder, "%*s<annotation name=\"%s\" value=\"%s\"",
634                           indent, "",
635                           info->key,
636                           info->value);
637
638   if (info->annotations == NULL)
639     {
640       g_string_append (string_builder, "/>\n");
641     }
642   else
643     {
644       g_string_append (string_builder, ">\n");
645
646       for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
647         g_dbus_annotation_info_generate_xml (info->annotations[n],
648                                              indent + 2,
649                                              string_builder);
650
651       g_string_append_printf (string_builder, "%*s</annotation>\n",
652                               indent, "");
653     }
654
655 }
656
657 static void
658 g_dbus_arg_info_generate_xml (GDBusArgInfo        *info,
659                               guint                indent,
660                               const gchar         *extra_attributes,
661                               GString             *string_builder)
662 {
663   guint n;
664
665   g_string_append_printf (string_builder, "%*s<arg type=\"%s\"",
666                           indent, "",
667                           info->signature);
668
669   if (info->name != NULL)
670     g_string_append_printf (string_builder, " name=\"%s\"", info->name);
671
672   if (extra_attributes != NULL)
673     g_string_append_printf (string_builder, " %s", extra_attributes);
674
675   if (info->annotations == NULL)
676     {
677       g_string_append (string_builder, "/>\n");
678     }
679   else
680     {
681       g_string_append (string_builder, ">\n");
682
683       for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
684         g_dbus_annotation_info_generate_xml (info->annotations[n],
685                                              indent + 2,
686                                              string_builder);
687
688       g_string_append_printf (string_builder, "%*s</arg>\n", indent, "");
689     }
690
691 }
692
693 static void
694 g_dbus_method_info_generate_xml (GDBusMethodInfo        *info,
695                                  guint                   indent,
696                                  GString                *string_builder)
697 {
698   guint n;
699
700   g_string_append_printf (string_builder, "%*s<method name=\"%s\"",
701                           indent, "",
702                           info->name);
703
704   if (info->annotations == NULL && info->in_args == NULL && info->out_args == NULL)
705     {
706       g_string_append (string_builder, "/>\n");
707     }
708   else
709     {
710       g_string_append (string_builder, ">\n");
711
712       for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
713         g_dbus_annotation_info_generate_xml (info->annotations[n],
714                                              indent + 2,
715                                              string_builder);
716
717       for (n = 0; info->in_args != NULL && info->in_args[n] != NULL; n++)
718         g_dbus_arg_info_generate_xml (info->in_args[n],
719                                       indent + 2,
720                                       "direction=\"in\"",
721                                       string_builder);
722
723       for (n = 0; info->out_args != NULL && info->out_args[n] != NULL; n++)
724         g_dbus_arg_info_generate_xml (info->out_args[n],
725                                       indent + 2,
726                                       "direction=\"out\"",
727                                       string_builder);
728
729       g_string_append_printf (string_builder, "%*s</method>\n", indent, "");
730     }
731 }
732
733 static void
734 g_dbus_signal_info_generate_xml (GDBusSignalInfo        *info,
735                                  guint                   indent,
736                                  GString                *string_builder)
737 {
738   guint n;
739
740   g_string_append_printf (string_builder, "%*s<signal name=\"%s\"",
741                           indent, "",
742                           info->name);
743
744   if (info->annotations == NULL && info->args == NULL)
745     {
746       g_string_append (string_builder, "/>\n");
747     }
748   else
749     {
750       g_string_append (string_builder, ">\n");
751
752       for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
753         g_dbus_annotation_info_generate_xml (info->annotations[n],
754                                              indent + 2,
755                                              string_builder);
756
757       for (n = 0; info->args != NULL && info->args[n] != NULL; n++)
758         g_dbus_arg_info_generate_xml (info->args[n],
759                                       indent + 2,
760                                       NULL,
761                                       string_builder);
762
763       g_string_append_printf (string_builder, "%*s</signal>\n", indent, "");
764     }
765 }
766
767 static void
768 g_dbus_property_info_generate_xml (GDBusPropertyInfo        *info,
769                                    guint                     indent,
770                                    GString                  *string_builder)
771 {
772   guint n;
773   const gchar *access_string;
774
775   if ((info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE) &&
776       (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
777     {
778       access_string = "readwrite";
779     }
780   else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
781     {
782       access_string = "read";
783     }
784   else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE)
785     {
786       access_string = "write";
787     }
788   else
789     {
790       g_assert_not_reached ();
791     }
792
793   g_string_append_printf (string_builder, "%*s<property type=\"%s\" name=\"%s\" access=\"%s\"",
794                           indent, "",
795                           info->signature,
796                           info->name,
797                           access_string);
798
799   if (info->annotations == NULL)
800     {
801       g_string_append (string_builder, "/>\n");
802     }
803   else
804     {
805       g_string_append (string_builder, ">\n");
806
807       for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
808         g_dbus_annotation_info_generate_xml (info->annotations[n],
809                                                indent + 2,
810                                                string_builder);
811
812       g_string_append_printf (string_builder, "%*s</property>\n", indent, "");
813     }
814
815 }
816
817 /**
818  * g_dbus_interface_info_generate_xml:
819  * @info: A #GDBusNodeInfo
820  * @indent: Indentation level.
821  * @string_builder: A #GString to to append XML data to.
822  *
823  * Appends an XML representation of @info (and its children) to @string_builder.
824  *
825  * This function is typically used for generating introspection XML
826  * documents at run-time for handling the
827  * <literal>org.freedesktop.DBus.Introspectable.Introspect</literal>
828  * method.
829  *
830  * Since: 2.26
831  */
832 void
833 g_dbus_interface_info_generate_xml (GDBusInterfaceInfo        *info,
834                                     guint                      indent,
835                                     GString                   *string_builder)
836 {
837   guint n;
838
839   g_string_append_printf (string_builder, "%*s<interface name=\"%s\">\n",
840                           indent, "",
841                           info->name);
842
843   for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
844     g_dbus_annotation_info_generate_xml (info->annotations[n],
845                                          indent + 2,
846                                          string_builder);
847
848   for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
849     g_dbus_method_info_generate_xml (info->methods[n],
850                                      indent + 2,
851                                      string_builder);
852
853   for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
854     g_dbus_signal_info_generate_xml (info->signals[n],
855                                      indent + 2,
856                                      string_builder);
857
858   for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
859     g_dbus_property_info_generate_xml (info->properties[n],
860                                        indent + 2,
861                                        string_builder);
862
863   g_string_append_printf (string_builder, "%*s</interface>\n", indent, "");
864 }
865
866 /**
867  * g_dbus_node_info_generate_xml:
868  * @info: A #GDBusNodeInfo.
869  * @indent: Indentation level.
870  * @string_builder: A #GString to to append XML data to.
871  *
872  * Appends an XML representation of @info (and its children) to @string_builder.
873  *
874  * This function is typically used for generating introspection XML documents at run-time for
875  * handling the <literal>org.freedesktop.DBus.Introspectable.Introspect</literal> method.
876  *
877  * Since: 2.26
878  */
879 void
880 g_dbus_node_info_generate_xml (GDBusNodeInfo        *info,
881                                guint                 indent,
882                                GString              *string_builder)
883 {
884   guint n;
885
886   g_string_append_printf (string_builder, "%*s<node", indent, "");
887   if (info->path != NULL)
888     g_string_append_printf (string_builder, " name=\"%s\"", info->path);
889
890   if (info->interfaces == NULL && info->nodes == NULL && info->annotations == NULL)
891     {
892       g_string_append (string_builder, "/>\n");
893     }
894   else
895     {
896       g_string_append (string_builder, ">\n");
897
898       for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
899         g_dbus_annotation_info_generate_xml (info->annotations[n],
900                                              indent + 2,
901                                              string_builder);
902
903       for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
904         g_dbus_interface_info_generate_xml (info->interfaces[n],
905                                             indent + 2,
906                                             string_builder);
907
908       for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
909         g_dbus_node_info_generate_xml (info->nodes[n],
910                                        indent + 2,
911                                        string_builder);
912
913       g_string_append_printf (string_builder, "%*s</node>\n", indent, "");
914     }
915 }
916
917 /* ---------------------------------------------------------------------------------------------------- */
918
919 static GDBusAnnotationInfo **
920 parse_data_steal_annotations (ParseData *data,
921                               guint     *out_num_elements)
922 {
923   GDBusAnnotationInfo **ret;
924   if (out_num_elements != NULL)
925     *out_num_elements = data->annotations->len;
926   if (data->annotations == NULL)
927     ret = NULL;
928   else
929     {
930       g_ptr_array_add (data->annotations, NULL);
931       ret = (GDBusAnnotationInfo **) g_ptr_array_free (data->annotations, FALSE);
932     }
933   data->annotations = g_ptr_array_new ();
934   return ret;
935 }
936
937 static GDBusArgInfo **
938 parse_data_steal_args (ParseData *data,
939                        guint     *out_num_elements)
940 {
941   GDBusArgInfo **ret;
942   if (out_num_elements != NULL)
943     *out_num_elements = data->args->len;
944   if (data->args == NULL)
945     ret = NULL;
946   else
947     {
948       g_ptr_array_add (data->args, NULL);
949       ret = (GDBusArgInfo **) g_ptr_array_free (data->args, FALSE);
950     }
951   data->args = g_ptr_array_new ();
952   return ret;
953 }
954
955 static GDBusArgInfo **
956 parse_data_steal_out_args (ParseData *data,
957                            guint     *out_num_elements)
958 {
959   GDBusArgInfo **ret;
960   if (out_num_elements != NULL)
961     *out_num_elements = data->out_args->len;
962   if (data->out_args == NULL)
963     ret = NULL;
964   else
965     {
966       g_ptr_array_add (data->out_args, NULL);
967       ret = (GDBusArgInfo **) g_ptr_array_free (data->out_args, FALSE);
968     }
969   data->out_args = g_ptr_array_new ();
970   return ret;
971 }
972
973 static GDBusMethodInfo **
974 parse_data_steal_methods (ParseData *data,
975                           guint     *out_num_elements)
976 {
977   GDBusMethodInfo **ret;
978   if (out_num_elements != NULL)
979     *out_num_elements = data->methods->len;
980   if (data->methods == NULL)
981     ret = NULL;
982   else
983     {
984       g_ptr_array_add (data->methods, NULL);
985       ret = (GDBusMethodInfo **) g_ptr_array_free (data->methods, FALSE);
986     }
987   data->methods = g_ptr_array_new ();
988   return ret;
989 }
990
991 static GDBusSignalInfo **
992 parse_data_steal_signals (ParseData *data,
993                           guint     *out_num_elements)
994 {
995   GDBusSignalInfo **ret;
996   if (out_num_elements != NULL)
997     *out_num_elements = data->signals->len;
998   if (data->signals == NULL)
999     ret = NULL;
1000   else
1001     {
1002       g_ptr_array_add (data->signals, NULL);
1003       ret = (GDBusSignalInfo **) g_ptr_array_free (data->signals, FALSE);
1004     }
1005   data->signals = g_ptr_array_new ();
1006   return ret;
1007 }
1008
1009 static GDBusPropertyInfo **
1010 parse_data_steal_properties (ParseData *data,
1011                              guint     *out_num_elements)
1012 {
1013   GDBusPropertyInfo **ret;
1014   if (out_num_elements != NULL)
1015     *out_num_elements = data->properties->len;
1016   if (data->properties == NULL)
1017     ret = NULL;
1018   else
1019     {
1020       g_ptr_array_add (data->properties, NULL);
1021       ret = (GDBusPropertyInfo **) g_ptr_array_free (data->properties, FALSE);
1022     }
1023   data->properties = g_ptr_array_new ();
1024   return ret;
1025 }
1026
1027 static GDBusInterfaceInfo **
1028 parse_data_steal_interfaces (ParseData *data,
1029                              guint     *out_num_elements)
1030 {
1031   GDBusInterfaceInfo **ret;
1032   if (out_num_elements != NULL)
1033     *out_num_elements = data->interfaces->len;
1034   if (data->interfaces == NULL)
1035     ret = NULL;
1036   else
1037     {
1038       g_ptr_array_add (data->interfaces, NULL);
1039       ret = (GDBusInterfaceInfo **) g_ptr_array_free (data->interfaces, FALSE);
1040     }
1041   data->interfaces = g_ptr_array_new ();
1042   return ret;
1043 }
1044
1045 static GDBusNodeInfo **
1046 parse_data_steal_nodes (ParseData *data,
1047                         guint     *out_num_elements)
1048 {
1049   GDBusNodeInfo **ret;
1050   if (out_num_elements != NULL)
1051     *out_num_elements = data->nodes->len;
1052   if (data->nodes == NULL)
1053     ret = NULL;
1054   else
1055     {
1056       g_ptr_array_add (data->nodes, NULL);
1057       ret = (GDBusNodeInfo **) g_ptr_array_free (data->nodes, FALSE);
1058     }
1059   data->nodes = g_ptr_array_new ();
1060   return ret;
1061 }
1062
1063 /* ---------------------------------------------------------------------------------------------------- */
1064
1065 static void
1066 parse_data_free_annotations (ParseData *data)
1067 {
1068   if (data->annotations == NULL)
1069     return;
1070   g_ptr_array_foreach (data->annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
1071   g_ptr_array_free (data->annotations, TRUE);
1072   data->annotations = NULL;
1073 }
1074
1075 static void
1076 parse_data_free_args (ParseData *data)
1077 {
1078   if (data->args == NULL)
1079     return;
1080   g_ptr_array_foreach (data->args, (GFunc) g_dbus_arg_info_unref, NULL);
1081   g_ptr_array_free (data->args, TRUE);
1082   data->args = NULL;
1083 }
1084
1085 static void
1086 parse_data_free_out_args (ParseData *data)
1087 {
1088   if (data->out_args == NULL)
1089     return;
1090   g_ptr_array_foreach (data->out_args, (GFunc) g_dbus_arg_info_unref, NULL);
1091   g_ptr_array_free (data->out_args, TRUE);
1092   data->out_args = NULL;
1093 }
1094
1095 static void
1096 parse_data_free_methods (ParseData *data)
1097 {
1098   if (data->methods == NULL)
1099     return;
1100   g_ptr_array_foreach (data->methods, (GFunc) g_dbus_method_info_unref, NULL);
1101   g_ptr_array_free (data->methods, TRUE);
1102   data->methods = NULL;
1103 }
1104
1105 static void
1106 parse_data_free_signals (ParseData *data)
1107 {
1108   if (data->signals == NULL)
1109     return;
1110   g_ptr_array_foreach (data->signals, (GFunc) g_dbus_signal_info_unref, NULL);
1111   g_ptr_array_free (data->signals, TRUE);
1112   data->signals = NULL;
1113 }
1114
1115 static void
1116 parse_data_free_properties (ParseData *data)
1117 {
1118   if (data->properties == NULL)
1119     return;
1120   g_ptr_array_foreach (data->properties, (GFunc) g_dbus_property_info_unref, NULL);
1121   g_ptr_array_free (data->properties, TRUE);
1122   data->properties = NULL;
1123 }
1124
1125 static void
1126 parse_data_free_interfaces (ParseData *data)
1127 {
1128   if (data->interfaces == NULL)
1129     return;
1130   g_ptr_array_foreach (data->interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
1131   g_ptr_array_free (data->interfaces, TRUE);
1132   data->interfaces = NULL;
1133 }
1134
1135 static void
1136 parse_data_free_nodes (ParseData *data)
1137 {
1138   if (data->nodes == NULL)
1139     return;
1140   g_ptr_array_foreach (data->nodes, (GFunc) g_dbus_node_info_unref, NULL);
1141   g_ptr_array_free (data->nodes, TRUE);
1142   data->nodes = NULL;
1143 }
1144
1145 /* ---------------------------------------------------------------------------------------------------- */
1146
1147 static GDBusAnnotationInfo *
1148 parse_data_get_annotation (ParseData *data,
1149                            gboolean   create_new)
1150 {
1151   if (create_new)
1152     g_ptr_array_add (data->annotations, g_new0 (GDBusAnnotationInfo, 1));
1153   return data->annotations->pdata[data->annotations->len - 1];
1154 }
1155
1156 static GDBusArgInfo *
1157 parse_data_get_arg (ParseData *data,
1158                     gboolean   create_new)
1159 {
1160   if (create_new)
1161     g_ptr_array_add (data->args, g_new0 (GDBusArgInfo, 1));
1162   return data->args->pdata[data->args->len - 1];
1163 }
1164
1165 static GDBusArgInfo *
1166 parse_data_get_out_arg (ParseData *data,
1167                         gboolean   create_new)
1168 {
1169   if (create_new)
1170     g_ptr_array_add (data->out_args, g_new0 (GDBusArgInfo, 1));
1171   return data->out_args->pdata[data->out_args->len - 1];
1172 }
1173
1174 static GDBusMethodInfo *
1175 parse_data_get_method (ParseData *data,
1176                        gboolean   create_new)
1177 {
1178   if (create_new)
1179     g_ptr_array_add (data->methods, g_new0 (GDBusMethodInfo, 1));
1180   return data->methods->pdata[data->methods->len - 1];
1181 }
1182
1183 static GDBusSignalInfo *
1184 parse_data_get_signal (ParseData *data,
1185                        gboolean   create_new)
1186 {
1187   if (create_new)
1188     g_ptr_array_add (data->signals, g_new0 (GDBusSignalInfo, 1));
1189   return data->signals->pdata[data->signals->len - 1];
1190 }
1191
1192 static GDBusPropertyInfo *
1193 parse_data_get_property (ParseData *data,
1194                          gboolean   create_new)
1195 {
1196   if (create_new)
1197     g_ptr_array_add (data->properties, g_new0 (GDBusPropertyInfo, 1));
1198   return data->properties->pdata[data->properties->len - 1];
1199 }
1200
1201 static GDBusInterfaceInfo *
1202 parse_data_get_interface (ParseData *data,
1203                           gboolean   create_new)
1204 {
1205   if (create_new)
1206     g_ptr_array_add (data->interfaces, g_new0 (GDBusInterfaceInfo, 1));
1207   return data->interfaces->pdata[data->interfaces->len - 1];
1208 }
1209
1210 static GDBusNodeInfo *
1211 parse_data_get_node (ParseData *data,
1212                      gboolean   create_new)
1213 {
1214   if (create_new)
1215     g_ptr_array_add (data->nodes, g_new0 (GDBusNodeInfo, 1));
1216   return data->nodes->pdata[data->nodes->len - 1];
1217 }
1218
1219 /* ---------------------------------------------------------------------------------------------------- */
1220
1221 static ParseData *
1222 parse_data_new (void)
1223 {
1224   ParseData *data;
1225
1226   data = g_new0 (ParseData, 1);
1227
1228   /* initialize arrays */
1229   parse_data_steal_annotations (data, NULL);
1230   parse_data_steal_args (data, NULL);
1231   parse_data_steal_out_args (data, NULL);
1232   parse_data_steal_methods (data, NULL);
1233   parse_data_steal_signals (data, NULL);
1234   parse_data_steal_properties (data, NULL);
1235   parse_data_steal_interfaces (data, NULL);
1236   parse_data_steal_nodes (data, NULL);
1237
1238   return data;
1239 }
1240
1241 static void
1242 parse_data_free (ParseData *data)
1243 {
1244   GSList *l;
1245
1246   /* free stack of annotation arrays */
1247   for (l = data->annotations_stack; l != NULL; l = l->next)
1248     {
1249       GPtrArray *annotations = l->data;
1250       g_ptr_array_foreach (annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
1251       g_ptr_array_free (annotations, TRUE);
1252     }
1253   g_slist_free (data->annotations_stack);
1254
1255   /* free stack of interface arrays */
1256   for (l = data->interfaces_stack; l != NULL; l = l->next)
1257     {
1258       GPtrArray *interfaces = l->data;
1259       g_ptr_array_foreach (interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
1260       g_ptr_array_free (interfaces, TRUE);
1261     }
1262   g_slist_free (data->interfaces_stack);
1263
1264   /* free stack of node arrays */
1265   for (l = data->nodes_stack; l != NULL; l = l->next)
1266     {
1267       GPtrArray *nodes = l->data;
1268       g_ptr_array_foreach (nodes, (GFunc) g_dbus_node_info_unref, NULL);
1269       g_ptr_array_free (nodes, TRUE);
1270     }
1271   g_slist_free (data->nodes_stack);
1272
1273   /* free arrays (data->annotations, data->interfaces and data->nodes have been freed above) */
1274   parse_data_free_args (data);
1275   parse_data_free_out_args (data);
1276   parse_data_free_methods (data);
1277   parse_data_free_signals (data);
1278   parse_data_free_properties (data);
1279
1280   g_free (data);
1281 }
1282
1283 /* ---------------------------------------------------------------------------------------------------- */
1284
1285 static void
1286 parser_start_element (GMarkupParseContext  *context,
1287                       const gchar          *element_name,
1288                       const gchar         **attribute_names,
1289                       const gchar         **attribute_values,
1290                       gpointer              user_data,
1291                       GError              **error)
1292 {
1293   ParseData *data = user_data;
1294   GSList *stack;
1295   const gchar *name;
1296   const gchar *type;
1297   const gchar *access;
1298   const gchar *direction;
1299   const gchar *value;
1300
1301   name = NULL;
1302   type = NULL;
1303   access = NULL;
1304   direction = NULL;
1305   value = NULL;
1306
1307   stack = (GSList *) g_markup_parse_context_get_element_stack (context);
1308
1309   /* ---------------------------------------------------------------------------------------------------- */
1310   if (strcmp (element_name, "node") == 0)
1311     {
1312       if (!(g_slist_length (stack) >= 1 || strcmp (stack->next->data, "node") != 0))
1313         {
1314           g_set_error_literal (error,
1315                                G_MARKUP_ERROR,
1316                                G_MARKUP_ERROR_INVALID_CONTENT,
1317                                "<node> elements can only be top-level or embedded in other <node> elements");
1318           goto out;
1319         }
1320
1321       if (!g_markup_collect_attributes (element_name,
1322                                         attribute_names,
1323                                         attribute_values,
1324                                         error,
1325                                         G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
1326                                         /* some hand-written introspection XML documents use this */
1327                                         G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "xmlns:doc", NULL,
1328                                         G_MARKUP_COLLECT_INVALID))
1329         goto out;
1330
1331       g_dbus_node_info_set (data,
1332                             parse_data_get_node (data, TRUE),
1333                             name,
1334                             0, NULL,
1335                             0, NULL,
1336                             NULL);
1337
1338       /* push the currently retrieved interfaces and nodes on the stack and prepare new arrays */
1339       data->interfaces_stack = g_slist_prepend (data->interfaces_stack, data->interfaces);
1340       data->interfaces = NULL;
1341       parse_data_steal_interfaces (data, NULL);
1342
1343       data->nodes_stack = g_slist_prepend (data->nodes_stack, data->nodes);
1344       data->nodes = NULL;
1345       parse_data_steal_nodes (data, NULL);
1346
1347     }
1348   /* ---------------------------------------------------------------------------------------------------- */
1349   else if (strcmp (element_name, "interface") == 0)
1350     {
1351       if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "node") != 0)
1352         {
1353           g_set_error_literal (error,
1354                                G_MARKUP_ERROR,
1355                                G_MARKUP_ERROR_INVALID_CONTENT,
1356                                "<interface> elements can only be embedded in <node> elements");
1357           goto out;
1358         }
1359
1360       if (!g_markup_collect_attributes (element_name,
1361                                         attribute_names,
1362                                         attribute_values,
1363                                         error,
1364                                         G_MARKUP_COLLECT_STRING, "name", &name,
1365                                         G_MARKUP_COLLECT_INVALID))
1366         goto out;
1367
1368       g_dbus_interface_info_set (data,
1369                                  parse_data_get_interface (data, TRUE),
1370                                  name,
1371                                  0, NULL,
1372                                  0, NULL,
1373                                  0, NULL,
1374                                  NULL);
1375
1376     }
1377   /* ---------------------------------------------------------------------------------------------------- */
1378   else if (strcmp (element_name, "method") == 0)
1379     {
1380       if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1381         {
1382           g_set_error_literal (error,
1383                                G_MARKUP_ERROR,
1384                                G_MARKUP_ERROR_INVALID_CONTENT,
1385                                "<method> elements can only be embedded in <interface> elements");
1386           goto out;
1387         }
1388
1389       if (!g_markup_collect_attributes (element_name,
1390                                         attribute_names,
1391                                         attribute_values,
1392                                         error,
1393                                         G_MARKUP_COLLECT_STRING, "name", &name,
1394                                         G_MARKUP_COLLECT_INVALID))
1395         goto out;
1396
1397       g_dbus_method_info_set (data,
1398                               parse_data_get_method (data, TRUE),
1399                               name,
1400                               0, NULL,
1401                               0, NULL,
1402                               NULL);
1403
1404       data->num_args = 0;
1405
1406     }
1407   /* ---------------------------------------------------------------------------------------------------- */
1408   else if (strcmp (element_name, "signal") == 0)
1409     {
1410       if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1411         {
1412           g_set_error_literal (error,
1413                                G_MARKUP_ERROR,
1414                                G_MARKUP_ERROR_INVALID_CONTENT,
1415                                "<signal> elements can only be embedded in <interface> elements");
1416           goto out;
1417         }
1418
1419       if (!g_markup_collect_attributes (element_name,
1420                                         attribute_names,
1421                                         attribute_values,
1422                                         error,
1423                                         G_MARKUP_COLLECT_STRING, "name", &name,
1424                                         G_MARKUP_COLLECT_INVALID))
1425         goto out;
1426
1427       g_dbus_signal_info_set (data,
1428                               parse_data_get_signal (data, TRUE),
1429                               name,
1430                               0, NULL,
1431                               NULL);
1432
1433       data->num_args = 0;
1434
1435     }
1436   /* ---------------------------------------------------------------------------------------------------- */
1437   else if (strcmp (element_name, "property") == 0)
1438     {
1439       GDBusPropertyInfoFlags flags;
1440
1441       if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1442         {
1443           g_set_error_literal (error,
1444                                G_MARKUP_ERROR,
1445                                G_MARKUP_ERROR_INVALID_CONTENT,
1446                                "<property> elements can only be embedded in <interface> elements");
1447           goto out;
1448         }
1449
1450       if (!g_markup_collect_attributes (element_name,
1451                                         attribute_names,
1452                                         attribute_values,
1453                                         error,
1454                                         G_MARKUP_COLLECT_STRING, "name", &name,
1455                                         G_MARKUP_COLLECT_STRING, "type", &type,
1456                                         G_MARKUP_COLLECT_STRING, "access", &access,
1457                                         G_MARKUP_COLLECT_INVALID))
1458         goto out;
1459
1460       if (strcmp (access, "read") == 0)
1461         flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE;
1462       else if (strcmp (access, "write") == 0)
1463         flags = G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
1464       else if (strcmp (access, "readwrite") == 0)
1465         flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
1466       else
1467         {
1468           g_set_error (error,
1469                        G_MARKUP_ERROR,
1470                        G_MARKUP_ERROR_INVALID_CONTENT,
1471                        "Unknown value '%s' of access attribute for element <property>",
1472                        access);
1473           goto out;
1474         }
1475
1476       g_dbus_property_info_set (data,
1477                                 parse_data_get_property (data, TRUE),
1478                                 name,
1479                                 type,
1480                                 flags,
1481                                 NULL);
1482
1483     }
1484   /* ---------------------------------------------------------------------------------------------------- */
1485   else if (strcmp (element_name, "arg") == 0)
1486     {
1487       gboolean is_in;
1488       gchar *name_to_use;
1489
1490       if (g_slist_length (stack) < 2 ||
1491           (strcmp (stack->next->data, "method") != 0 &&
1492            strcmp (stack->next->data, "signal") != 0))
1493         {
1494           g_set_error_literal (error,
1495                                G_MARKUP_ERROR,
1496                                G_MARKUP_ERROR_INVALID_CONTENT,
1497                                "<arg> elements can only be embedded in <method> or <signal> elements");
1498           goto out;
1499         }
1500
1501       if (!g_markup_collect_attributes (element_name,
1502                                         attribute_names,
1503                                         attribute_values,
1504                                         error,
1505                                         G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
1506                                         G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "direction", &direction,
1507                                         G_MARKUP_COLLECT_STRING, "type", &type,
1508                                         G_MARKUP_COLLECT_INVALID))
1509         goto out;
1510
1511       if (strcmp (stack->next->data, "method") == 0)
1512         is_in = TRUE;
1513       else
1514         is_in = FALSE;
1515       if (direction != NULL)
1516         {
1517           if (strcmp (direction, "in") == 0)
1518             is_in = TRUE;
1519           else if (strcmp (direction, "out") == 0)
1520             is_in = FALSE;
1521           else
1522             {
1523               g_set_error (error,
1524                            G_MARKUP_ERROR,
1525                            G_MARKUP_ERROR_INVALID_CONTENT,
1526                            "Unknown value '%s' of direction attribute",
1527                            direction);
1528               goto out;
1529             }
1530         }
1531
1532       if (is_in && strcmp (stack->next->data, "signal") == 0)
1533         {
1534           g_set_error_literal (error,
1535                                G_MARKUP_ERROR,
1536                                G_MARKUP_ERROR_INVALID_CONTENT,
1537                                "Only direction 'out' is allowed for <arg> elements embedded in <signal>");
1538           goto out;
1539         }
1540
1541       if (name == NULL)
1542         name_to_use = g_strdup_printf ("arg_%d", data->num_args);
1543       else
1544         name_to_use = g_strdup (name);
1545       data->num_args++;
1546
1547       if (is_in)
1548         {
1549           g_dbus_arg_info_set (data,
1550                                parse_data_get_arg (data, TRUE),
1551                                name_to_use,
1552                                type,
1553                                NULL);
1554           data->last_arg_was_in = TRUE;
1555         }
1556       else
1557         {
1558           g_dbus_arg_info_set (data,
1559                                parse_data_get_out_arg (data, TRUE),
1560                                name_to_use,
1561                                type,
1562                                NULL);
1563           data->last_arg_was_in = FALSE;
1564
1565         }
1566
1567       g_free (name_to_use);
1568     }
1569   /* ---------------------------------------------------------------------------------------------------- */
1570   else if (strcmp (element_name, "annotation") == 0)
1571     {
1572       if (g_slist_length (stack) < 2 ||
1573           (strcmp (stack->next->data, "node") != 0 &&
1574            strcmp (stack->next->data, "interface") != 0 &&
1575            strcmp (stack->next->data, "signal") != 0 &&
1576            strcmp (stack->next->data, "method") != 0 &&
1577            strcmp (stack->next->data, "property") != 0 &&
1578            strcmp (stack->next->data, "arg") != 0 &&
1579            strcmp (stack->next->data, "annotation") != 0))
1580         {
1581           g_set_error_literal (error,
1582                                G_MARKUP_ERROR,
1583                                G_MARKUP_ERROR_INVALID_CONTENT,
1584                                "<annotation> elements can only be embedded in <node>, <interface>, <signal>, <method>, <property>, <arg> or <annotation> elements");
1585           goto out;
1586         }
1587
1588       if (!g_markup_collect_attributes (element_name,
1589                                         attribute_names,
1590                                         attribute_values,
1591                                         error,
1592                                         G_MARKUP_COLLECT_STRING, "name", &name,
1593                                         G_MARKUP_COLLECT_STRING, "value", &value,
1594                                         G_MARKUP_COLLECT_INVALID))
1595         goto out;
1596
1597       g_dbus_annotation_info_set (data,
1598                                   parse_data_get_annotation (data, TRUE),
1599                                   name,
1600                                   value,
1601                                   NULL);
1602     }
1603   /* ---------------------------------------------------------------------------------------------------- */
1604   else
1605     {
1606       /* don't bail on unknown elements; just ignore them */
1607     }
1608   /* ---------------------------------------------------------------------------------------------------- */
1609
1610   /* push the currently retrieved annotations on the stack and prepare a new one */
1611   data->annotations_stack = g_slist_prepend (data->annotations_stack, data->annotations);
1612   data->annotations = NULL;
1613   parse_data_steal_annotations (data, NULL);
1614
1615  out:
1616   ;
1617 }
1618
1619 /* ---------------------------------------------------------------------------------------------------- */
1620
1621 static GDBusAnnotationInfo **
1622 steal_annotations (ParseData *data)
1623 {
1624   return parse_data_steal_annotations (data, NULL);
1625 }
1626
1627
1628 static void
1629 parser_end_element (GMarkupParseContext  *context,
1630                     const gchar          *element_name,
1631                     gpointer              user_data,
1632                     GError              **error)
1633 {
1634   ParseData *data = user_data;
1635   gboolean have_popped_annotations;
1636
1637   have_popped_annotations = FALSE;
1638
1639   if (strcmp (element_name, "node") == 0)
1640     {
1641       guint num_nodes;
1642       guint num_interfaces;
1643       GDBusNodeInfo **nodes;
1644       GDBusInterfaceInfo **interfaces;
1645
1646       nodes = parse_data_steal_nodes (data, &num_nodes);
1647       interfaces = parse_data_steal_interfaces (data, &num_interfaces);
1648
1649       /* destroy the nodes, interfaces for scope we're exiting and and pop the nodes, interfaces from the
1650        * scope we're reentering
1651        */
1652       parse_data_free_interfaces (data);
1653       data->interfaces = (GPtrArray *) data->interfaces_stack->data;
1654       data->interfaces_stack = g_slist_remove (data->interfaces_stack, data->interfaces_stack->data);
1655
1656       parse_data_free_nodes (data);
1657       data->nodes = (GPtrArray *) data->nodes_stack->data;
1658       data->nodes_stack = g_slist_remove (data->nodes_stack, data->nodes_stack->data);
1659
1660       g_dbus_node_info_set (data,
1661                             parse_data_get_node (data, FALSE),
1662                             NULL,
1663                             num_interfaces,
1664                             interfaces,
1665                             num_nodes,
1666                             nodes,
1667                             steal_annotations (data));
1668
1669     }
1670   else if (strcmp (element_name, "interface") == 0)
1671     {
1672       guint num_methods;
1673       guint num_signals;
1674       guint num_properties;
1675       GDBusMethodInfo **methods;
1676       GDBusSignalInfo **signals;
1677       GDBusPropertyInfo **properties;
1678
1679       methods    = parse_data_steal_methods    (data, &num_methods);
1680       signals    = parse_data_steal_signals    (data, &num_signals);
1681       properties = parse_data_steal_properties (data, &num_properties);
1682
1683       g_dbus_interface_info_set (data,
1684                                  parse_data_get_interface (data, FALSE),
1685                                  NULL,
1686                                  num_methods,
1687                                  methods,
1688                                  num_signals,
1689                                  signals,
1690                                  num_properties,
1691                                  properties,
1692                                  steal_annotations (data));
1693
1694     }
1695   else if (strcmp (element_name, "method") == 0)
1696     {
1697       guint in_num_args;
1698       guint out_num_args;
1699       GDBusArgInfo **in_args;
1700       GDBusArgInfo **out_args;
1701
1702       in_args  = parse_data_steal_args     (data, &in_num_args);
1703       out_args = parse_data_steal_out_args (data, &out_num_args);
1704
1705       g_dbus_method_info_set (data,
1706                               parse_data_get_method (data, FALSE),
1707                               NULL,
1708                               in_num_args,
1709                               in_args,
1710                               out_num_args,
1711                               out_args,
1712                               steal_annotations (data));
1713     }
1714   else if (strcmp (element_name, "signal") == 0)
1715     {
1716       guint num_args;
1717       GDBusArgInfo **args;
1718
1719       args = parse_data_steal_out_args (data, &num_args);
1720
1721       g_dbus_signal_info_set (data,
1722                               parse_data_get_signal (data, FALSE),
1723                               NULL,
1724                               num_args,
1725                               args,
1726                               steal_annotations (data));
1727     }
1728   else if (strcmp (element_name, "property") == 0)
1729     {
1730       g_dbus_property_info_set (data,
1731                                 parse_data_get_property (data, FALSE),
1732                                 NULL,
1733                                 NULL,
1734                                 G_DBUS_PROPERTY_INFO_FLAGS_NONE,
1735                                 steal_annotations (data));
1736     }
1737   else if (strcmp (element_name, "arg") == 0)
1738     {
1739       g_dbus_arg_info_set (data,
1740                            data->last_arg_was_in ? parse_data_get_arg (data, FALSE) : parse_data_get_out_arg (data, FALSE),
1741                            NULL,
1742                            NULL,
1743                            steal_annotations (data));
1744     }
1745   else if (strcmp (element_name, "annotation") == 0)
1746     {
1747       GDBusAnnotationInfo **embedded_annotations;
1748
1749       embedded_annotations = steal_annotations (data);
1750
1751       /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1752       parse_data_free_annotations (data);
1753       data->annotations = (GPtrArray *) data->annotations_stack->data;
1754       data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1755
1756       have_popped_annotations = TRUE;
1757
1758       g_dbus_annotation_info_set (data,
1759                                   parse_data_get_annotation (data, FALSE),
1760                                   NULL,
1761                                   NULL,
1762                                   embedded_annotations);
1763     }
1764   else
1765     {
1766       /* don't bail on unknown elements; just ignore them */
1767     }
1768
1769   if (!have_popped_annotations)
1770     {
1771       /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1772       parse_data_free_annotations (data);
1773       data->annotations = (GPtrArray *) data->annotations_stack->data;
1774       data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1775     }
1776 }
1777
1778 /* ---------------------------------------------------------------------------------------------------- */
1779
1780 static void
1781 parser_error (GMarkupParseContext *context,
1782               GError              *error,
1783               gpointer             user_data)
1784 {
1785   gint line_number;
1786   gint char_number;
1787
1788   g_markup_parse_context_get_position (context, &line_number, &char_number);
1789
1790   g_prefix_error (&error, "%d:%d: ",
1791                   line_number,
1792                   char_number);
1793 }
1794
1795 /* ---------------------------------------------------------------------------------------------------- */
1796
1797 /**
1798  * g_dbus_node_info_new_for_xml:
1799  * @xml_data: Valid D-Bus introspection XML.
1800  * @error: Return location for error.
1801  *
1802  * Parses @xml_data and returns a #GDBusNodeInfo representing the data.
1803  *
1804  * Returns: A #GDBusNodeInfo structure or %NULL if @error is set. Free
1805  * with g_dbus_node_info_unref().
1806  *
1807  * Since: 2.26
1808  */
1809 GDBusNodeInfo *
1810 g_dbus_node_info_new_for_xml (const gchar  *xml_data,
1811                               GError      **error)
1812 {
1813   GDBusNodeInfo *ret;
1814   GMarkupParseContext *context;
1815   GMarkupParser *parser;
1816   guint num_nodes;
1817   ParseData *data;
1818   GDBusNodeInfo **ughret;
1819
1820   ret = NULL;
1821   parser = NULL;
1822   context = NULL;
1823
1824   parser = g_new0 (GMarkupParser, 1);
1825   parser->start_element = parser_start_element;
1826   parser->end_element   = parser_end_element;
1827   parser->error         = parser_error;
1828
1829   data = parse_data_new ();
1830   context = g_markup_parse_context_new (parser,
1831                                         0,
1832                                         data,
1833                                         (GDestroyNotify) parse_data_free);
1834
1835   if (!g_markup_parse_context_parse (context,
1836                                      xml_data,
1837                                      strlen (xml_data),
1838                                      error))
1839     goto out;
1840
1841   ughret = parse_data_steal_nodes (data, &num_nodes);
1842
1843   if (num_nodes != 1)
1844     {
1845       guint n;
1846
1847       g_set_error (error,
1848                    G_MARKUP_ERROR,
1849                    G_MARKUP_ERROR_INVALID_CONTENT,
1850                    "Expected a single node in introspection XML, found %d",
1851                    num_nodes);
1852
1853       /* clean up */
1854       for (n = 0; n < num_nodes; n++)
1855         {
1856           for (n = 0; n < num_nodes; n++)
1857             g_dbus_node_info_unref (&(ret[n]));
1858         }
1859       g_free (ret);
1860       ret = NULL;
1861     }
1862
1863   ret = ughret[0];
1864   g_free (ughret);
1865
1866  out:
1867   if (parser != NULL)
1868     g_free (parser);
1869   if (context != NULL)
1870     g_markup_parse_context_free (context);
1871
1872   return ret;
1873 }
1874
1875 /* ---------------------------------------------------------------------------------------------------- */
1876
1877 /**
1878  * g_dbus_annotation_info_lookup:
1879  * @annotations: A %NULL-terminated array of annotations or %NULL.
1880  * @name: The name of the annotation to look up.
1881  *
1882  * Looks up the value of an annotation.
1883  *
1884  * This cost of this function is O(n) in number of annotations.
1885  *
1886  * Returns: The value or %NULL if not found. Do not free, it is owned by @annotations.
1887  *
1888  * Since: 2.26
1889  */
1890 const gchar *
1891 g_dbus_annotation_info_lookup (GDBusAnnotationInfo **annotations,
1892                                const gchar          *name)
1893 {
1894   guint n;
1895   const gchar *ret;
1896
1897   ret = NULL;
1898   for (n = 0; annotations != NULL && annotations[n] != NULL; n++)
1899     {
1900       if (g_strcmp0 (annotations[n]->key, name) == 0)
1901         {
1902           ret = annotations[n]->value;
1903           goto out;
1904         }
1905     }
1906
1907  out:
1908   return ret;
1909 }
1910
1911 /* ---------------------------------------------------------------------------------------------------- */
1912
1913 /**
1914  * g_dbus_interface_info_lookup_method:
1915  * @info: A #GDBusInterfaceInfo.
1916  * @name: A D-Bus method name (typically in CamelCase)
1917  *
1918  * Looks up information about a method.
1919  *
1920  * This cost of this function is O(n) in number of methods.
1921  *
1922  * Returns: A #GDBusMethodInfo or %NULL if not found. Do not free, it is owned by @info.
1923  *
1924  * Since: 2.26
1925  */
1926 GDBusMethodInfo *
1927 g_dbus_interface_info_lookup_method (GDBusInterfaceInfo *info,
1928                                      const gchar        *name)
1929 {
1930   guint n;
1931   GDBusMethodInfo *result;
1932
1933   for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
1934     {
1935       GDBusMethodInfo *i = info->methods[n];
1936
1937       if (g_strcmp0 (i->name, name) == 0)
1938         {
1939           result = i;
1940           goto out;
1941         }
1942     }
1943
1944   result = NULL;
1945
1946  out:
1947   return result;
1948 }
1949
1950 /* ---------------------------------------------------------------------------------------------------- */
1951
1952 /**
1953  * g_dbus_interface_info_lookup_signal:
1954  * @info: A #GDBusInterfaceInfo.
1955  * @name: A D-Bus signal name (typically in CamelCase)
1956  *
1957  * Looks up information about a signal.
1958  *
1959  * This cost of this function is O(n) in number of signals.
1960  *
1961  * Returns: A #GDBusSignalInfo or %NULL if not found. Do not free, it is owned by @info.
1962  *
1963  * Since: 2.26
1964  */
1965 GDBusSignalInfo *
1966 g_dbus_interface_info_lookup_signal (GDBusInterfaceInfo *info,
1967                                      const gchar        *name)
1968 {
1969   guint n;
1970   GDBusSignalInfo *result;
1971
1972   for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
1973     {
1974       GDBusSignalInfo *i = info->signals[n];
1975
1976       if (g_strcmp0 (i->name, name) == 0)
1977         {
1978           result = i;
1979           goto out;
1980         }
1981     }
1982
1983   result = NULL;
1984
1985  out:
1986   return result;
1987 }
1988
1989 /* ---------------------------------------------------------------------------------------------------- */
1990
1991 /**
1992  * g_dbus_interface_info_lookup_property:
1993  * @info: A #GDBusInterfaceInfo.
1994  * @name: A D-Bus property name (typically in CamelCase).
1995  *
1996  * Looks up information about a property.
1997  *
1998  * This cost of this function is O(n) in number of properties.
1999  *
2000  * Returns: A #GDBusPropertyInfo or %NULL if not found. Do not free, it is owned by @info.
2001  *
2002  * Since: 2.26
2003  */
2004 GDBusPropertyInfo *
2005 g_dbus_interface_info_lookup_property (GDBusInterfaceInfo *info,
2006                                        const gchar        *name)
2007 {
2008   guint n;
2009   GDBusPropertyInfo *result;
2010
2011   for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
2012     {
2013       GDBusPropertyInfo *i = info->properties[n];
2014
2015       if (g_strcmp0 (i->name, name) == 0)
2016         {
2017           result = i;
2018           goto out;
2019         }
2020     }
2021
2022   result = NULL;
2023
2024  out:
2025   return result;
2026 }
2027
2028 /* ---------------------------------------------------------------------------------------------------- */
2029
2030 /**
2031  * g_dbus_node_info_lookup_interface:
2032  * @info: A #GDBusNodeInfo.
2033  * @name: A D-Bus interface name.
2034  *
2035  * Looks up information about an interface.
2036  *
2037  * This cost of this function is O(n) in number of interfaces.
2038  *
2039  * Returns: A #GDBusInterfaceInfo or %NULL if not found. Do not free, it is owned by @info.
2040  *
2041  * Since: 2.26
2042  */
2043 GDBusInterfaceInfo *
2044 g_dbus_node_info_lookup_interface (GDBusNodeInfo *info,
2045                                    const gchar   *name)
2046 {
2047   guint n;
2048   GDBusInterfaceInfo *result;
2049
2050   for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
2051     {
2052       GDBusInterfaceInfo *i = info->interfaces[n];
2053
2054       if (g_strcmp0 (i->name, name) == 0)
2055         {
2056           result = i;
2057           goto out;
2058         }
2059     }
2060
2061   result = NULL;
2062
2063  out:
2064   return result;
2065 }