Use G_DEFINE_[BOXED|POINTER]_TYPE instead of handwritten code
[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       is_in = FALSE;
1512       if (direction != NULL)
1513         {
1514           if (strcmp (direction, "in") == 0)
1515             is_in = TRUE;
1516           else if (strcmp (direction, "out") == 0)
1517             is_in = FALSE;
1518           else
1519             {
1520               g_set_error (error,
1521                            G_MARKUP_ERROR,
1522                            G_MARKUP_ERROR_INVALID_CONTENT,
1523                            "Unknown value '%s' of direction attribute",
1524                            direction);
1525               goto out;
1526             }
1527         }
1528
1529       if (is_in && strcmp (stack->next->data, "signal") == 0)
1530         {
1531           g_set_error_literal (error,
1532                                G_MARKUP_ERROR,
1533                                G_MARKUP_ERROR_INVALID_CONTENT,
1534                                "Only direction 'out' is allowed for <arg> elements embedded in <signal>");
1535           goto out;
1536         }
1537
1538       if (name == NULL)
1539         name_to_use = g_strdup_printf ("arg_%d", data->num_args);
1540       else
1541         name_to_use = g_strdup (name);
1542       data->num_args++;
1543
1544       if (is_in)
1545         {
1546           g_dbus_arg_info_set (data,
1547                                parse_data_get_arg (data, TRUE),
1548                                name_to_use,
1549                                type,
1550                                NULL);
1551           data->last_arg_was_in = TRUE;
1552         }
1553       else
1554         {
1555           g_dbus_arg_info_set (data,
1556                                parse_data_get_out_arg (data, TRUE),
1557                                name_to_use,
1558                                type,
1559                                NULL);
1560           data->last_arg_was_in = FALSE;
1561
1562         }
1563
1564       g_free (name_to_use);
1565     }
1566   /* ---------------------------------------------------------------------------------------------------- */
1567   else if (strcmp (element_name, "annotation") == 0)
1568     {
1569       if (g_slist_length (stack) < 2 ||
1570           (strcmp (stack->next->data, "node") != 0 &&
1571            strcmp (stack->next->data, "interface") != 0 &&
1572            strcmp (stack->next->data, "signal") != 0 &&
1573            strcmp (stack->next->data, "method") != 0 &&
1574            strcmp (stack->next->data, "property") != 0 &&
1575            strcmp (stack->next->data, "arg") != 0 &&
1576            strcmp (stack->next->data, "annotation") != 0))
1577         {
1578           g_set_error_literal (error,
1579                                G_MARKUP_ERROR,
1580                                G_MARKUP_ERROR_INVALID_CONTENT,
1581                                "<annotation> elements can only be embedded in <node>, <interface>, <signal>, <method>, <property>, <arg> or <annotation> elements");
1582           goto out;
1583         }
1584
1585       if (!g_markup_collect_attributes (element_name,
1586                                         attribute_names,
1587                                         attribute_values,
1588                                         error,
1589                                         G_MARKUP_COLLECT_STRING, "name", &name,
1590                                         G_MARKUP_COLLECT_STRING, "value", &value,
1591                                         G_MARKUP_COLLECT_INVALID))
1592         goto out;
1593
1594       g_dbus_annotation_info_set (data,
1595                                   parse_data_get_annotation (data, TRUE),
1596                                   name,
1597                                   value,
1598                                   NULL);
1599     }
1600   /* ---------------------------------------------------------------------------------------------------- */
1601   else
1602     {
1603       /* don't bail on unknown elements; just ignore them */
1604     }
1605   /* ---------------------------------------------------------------------------------------------------- */
1606
1607   /* push the currently retrieved annotations on the stack and prepare a new one */
1608   data->annotations_stack = g_slist_prepend (data->annotations_stack, data->annotations);
1609   data->annotations = NULL;
1610   parse_data_steal_annotations (data, NULL);
1611
1612  out:
1613   ;
1614 }
1615
1616 /* ---------------------------------------------------------------------------------------------------- */
1617
1618 static GDBusAnnotationInfo **
1619 steal_annotations (ParseData *data)
1620 {
1621   return parse_data_steal_annotations (data, NULL);
1622 }
1623
1624
1625 static void
1626 parser_end_element (GMarkupParseContext  *context,
1627                     const gchar          *element_name,
1628                     gpointer              user_data,
1629                     GError              **error)
1630 {
1631   ParseData *data = user_data;
1632   gboolean have_popped_annotations;
1633
1634   have_popped_annotations = FALSE;
1635
1636   if (strcmp (element_name, "node") == 0)
1637     {
1638       guint num_nodes;
1639       guint num_interfaces;
1640       GDBusNodeInfo **nodes;
1641       GDBusInterfaceInfo **interfaces;
1642
1643       nodes = parse_data_steal_nodes (data, &num_nodes);
1644       interfaces = parse_data_steal_interfaces (data, &num_interfaces);
1645
1646       /* destroy the nodes, interfaces for scope we're exiting and and pop the nodes, interfaces from the
1647        * scope we're reentering
1648        */
1649       parse_data_free_interfaces (data);
1650       data->interfaces = (GPtrArray *) data->interfaces_stack->data;
1651       data->interfaces_stack = g_slist_remove (data->interfaces_stack, data->interfaces_stack->data);
1652
1653       parse_data_free_nodes (data);
1654       data->nodes = (GPtrArray *) data->nodes_stack->data;
1655       data->nodes_stack = g_slist_remove (data->nodes_stack, data->nodes_stack->data);
1656
1657       g_dbus_node_info_set (data,
1658                             parse_data_get_node (data, FALSE),
1659                             NULL,
1660                             num_interfaces,
1661                             interfaces,
1662                             num_nodes,
1663                             nodes,
1664                             steal_annotations (data));
1665
1666     }
1667   else if (strcmp (element_name, "interface") == 0)
1668     {
1669       guint num_methods;
1670       guint num_signals;
1671       guint num_properties;
1672       GDBusMethodInfo **methods;
1673       GDBusSignalInfo **signals;
1674       GDBusPropertyInfo **properties;
1675
1676       methods    = parse_data_steal_methods    (data, &num_methods);
1677       signals    = parse_data_steal_signals    (data, &num_signals);
1678       properties = parse_data_steal_properties (data, &num_properties);
1679
1680       g_dbus_interface_info_set (data,
1681                                  parse_data_get_interface (data, FALSE),
1682                                  NULL,
1683                                  num_methods,
1684                                  methods,
1685                                  num_signals,
1686                                  signals,
1687                                  num_properties,
1688                                  properties,
1689                                  steal_annotations (data));
1690
1691     }
1692   else if (strcmp (element_name, "method") == 0)
1693     {
1694       guint in_num_args;
1695       guint out_num_args;
1696       GDBusArgInfo **in_args;
1697       GDBusArgInfo **out_args;
1698
1699       in_args  = parse_data_steal_args     (data, &in_num_args);
1700       out_args = parse_data_steal_out_args (data, &out_num_args);
1701
1702       g_dbus_method_info_set (data,
1703                               parse_data_get_method (data, FALSE),
1704                               NULL,
1705                               in_num_args,
1706                               in_args,
1707                               out_num_args,
1708                               out_args,
1709                               steal_annotations (data));
1710     }
1711   else if (strcmp (element_name, "signal") == 0)
1712     {
1713       guint num_args;
1714       GDBusArgInfo **args;
1715
1716       args = parse_data_steal_out_args (data, &num_args);
1717
1718       g_dbus_signal_info_set (data,
1719                               parse_data_get_signal (data, FALSE),
1720                               NULL,
1721                               num_args,
1722                               args,
1723                               steal_annotations (data));
1724     }
1725   else if (strcmp (element_name, "property") == 0)
1726     {
1727       g_dbus_property_info_set (data,
1728                                 parse_data_get_property (data, FALSE),
1729                                 NULL,
1730                                 NULL,
1731                                 G_DBUS_PROPERTY_INFO_FLAGS_NONE,
1732                                 steal_annotations (data));
1733     }
1734   else if (strcmp (element_name, "arg") == 0)
1735     {
1736       g_dbus_arg_info_set (data,
1737                            data->last_arg_was_in ? parse_data_get_arg (data, FALSE) : parse_data_get_out_arg (data, FALSE),
1738                            NULL,
1739                            NULL,
1740                            steal_annotations (data));
1741     }
1742   else if (strcmp (element_name, "annotation") == 0)
1743     {
1744       GDBusAnnotationInfo **embedded_annotations;
1745
1746       embedded_annotations = steal_annotations (data);
1747
1748       /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1749       parse_data_free_annotations (data);
1750       data->annotations = (GPtrArray *) data->annotations_stack->data;
1751       data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1752
1753       have_popped_annotations = TRUE;
1754
1755       g_dbus_annotation_info_set (data,
1756                                   parse_data_get_annotation (data, FALSE),
1757                                   NULL,
1758                                   NULL,
1759                                   embedded_annotations);
1760     }
1761   else
1762     {
1763       /* don't bail on unknown elements; just ignore them */
1764     }
1765
1766   if (!have_popped_annotations)
1767     {
1768       /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1769       parse_data_free_annotations (data);
1770       data->annotations = (GPtrArray *) data->annotations_stack->data;
1771       data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1772     }
1773 }
1774
1775 /* ---------------------------------------------------------------------------------------------------- */
1776
1777 static void
1778 parser_error (GMarkupParseContext *context,
1779               GError              *error,
1780               gpointer             user_data)
1781 {
1782   gint line_number;
1783   gint char_number;
1784
1785   g_markup_parse_context_get_position (context, &line_number, &char_number);
1786
1787   g_prefix_error (&error, "%d:%d: ",
1788                   line_number,
1789                   char_number);
1790 }
1791
1792 /* ---------------------------------------------------------------------------------------------------- */
1793
1794 /**
1795  * g_dbus_node_info_new_for_xml:
1796  * @xml_data: Valid D-Bus introspection XML.
1797  * @error: Return location for error.
1798  *
1799  * Parses @xml_data and returns a #GDBusNodeInfo representing the data.
1800  *
1801  * Returns: A #GDBusNodeInfo structure or %NULL if @error is set. Free
1802  * with g_dbus_node_info_unref().
1803  *
1804  * Since: 2.26
1805  */
1806 GDBusNodeInfo *
1807 g_dbus_node_info_new_for_xml (const gchar  *xml_data,
1808                               GError      **error)
1809 {
1810   GDBusNodeInfo *ret;
1811   GMarkupParseContext *context;
1812   GMarkupParser *parser;
1813   guint num_nodes;
1814   ParseData *data;
1815   GDBusNodeInfo **ughret;
1816
1817   ret = NULL;
1818   parser = NULL;
1819   context = NULL;
1820
1821   parser = g_new0 (GMarkupParser, 1);
1822   parser->start_element = parser_start_element;
1823   parser->end_element   = parser_end_element;
1824   parser->error         = parser_error;
1825
1826   data = parse_data_new ();
1827   context = g_markup_parse_context_new (parser,
1828                                         0,
1829                                         data,
1830                                         (GDestroyNotify) parse_data_free);
1831
1832   if (!g_markup_parse_context_parse (context,
1833                                      xml_data,
1834                                      strlen (xml_data),
1835                                      error))
1836     goto out;
1837
1838   ughret = parse_data_steal_nodes (data, &num_nodes);
1839
1840   if (num_nodes != 1)
1841     {
1842       guint n;
1843
1844       g_set_error (error,
1845                    G_MARKUP_ERROR,
1846                    G_MARKUP_ERROR_INVALID_CONTENT,
1847                    "Expected a single node in introspection XML, found %d",
1848                    num_nodes);
1849
1850       /* clean up */
1851       for (n = 0; n < num_nodes; n++)
1852         {
1853           for (n = 0; n < num_nodes; n++)
1854             g_dbus_node_info_unref (&(ret[n]));
1855         }
1856       g_free (ret);
1857       ret = NULL;
1858     }
1859
1860   ret = ughret[0];
1861   g_free (ughret);
1862
1863  out:
1864   if (parser != NULL)
1865     g_free (parser);
1866   if (context != NULL)
1867     g_markup_parse_context_free (context);
1868
1869   return ret;
1870 }
1871
1872 /* ---------------------------------------------------------------------------------------------------- */
1873
1874 /**
1875  * g_dbus_annotation_info_lookup:
1876  * @annotations: A %NULL-terminated array of annotations or %NULL.
1877  * @name: The name of the annotation to look up.
1878  *
1879  * Looks up the value of an annotation.
1880  *
1881  * This cost of this function is O(n) in number of annotations.
1882  *
1883  * Returns: The value or %NULL if not found. Do not free, it is owned by @annotations.
1884  *
1885  * Since: 2.26
1886  */
1887 const gchar *
1888 g_dbus_annotation_info_lookup (GDBusAnnotationInfo **annotations,
1889                                const gchar          *name)
1890 {
1891   guint n;
1892   const gchar *ret;
1893
1894   ret = NULL;
1895   for (n = 0; annotations != NULL && annotations[n] != NULL; n++)
1896     {
1897       if (g_strcmp0 (annotations[n]->key, name) == 0)
1898         {
1899           ret = annotations[n]->value;
1900           goto out;
1901         }
1902     }
1903
1904  out:
1905   return ret;
1906 }
1907
1908 /* ---------------------------------------------------------------------------------------------------- */
1909
1910 /**
1911  * g_dbus_interface_info_lookup_method:
1912  * @info: A #GDBusInterfaceInfo.
1913  * @name: A D-Bus method name (typically in CamelCase)
1914  *
1915  * Looks up information about a method.
1916  *
1917  * This cost of this function is O(n) in number of methods.
1918  *
1919  * Returns: A #GDBusMethodInfo or %NULL if not found. Do not free, it is owned by @info.
1920  *
1921  * Since: 2.26
1922  */
1923 GDBusMethodInfo *
1924 g_dbus_interface_info_lookup_method (GDBusInterfaceInfo *info,
1925                                      const gchar        *name)
1926 {
1927   guint n;
1928   GDBusMethodInfo *result;
1929
1930   for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
1931     {
1932       GDBusMethodInfo *i = info->methods[n];
1933
1934       if (g_strcmp0 (i->name, name) == 0)
1935         {
1936           result = i;
1937           goto out;
1938         }
1939     }
1940
1941   result = NULL;
1942
1943  out:
1944   return result;
1945 }
1946
1947 /* ---------------------------------------------------------------------------------------------------- */
1948
1949 /**
1950  * g_dbus_interface_info_lookup_signal:
1951  * @info: A #GDBusInterfaceInfo.
1952  * @name: A D-Bus signal name (typically in CamelCase)
1953  *
1954  * Looks up information about a signal.
1955  *
1956  * This cost of this function is O(n) in number of signals.
1957  *
1958  * Returns: A #GDBusSignalInfo or %NULL if not found. Do not free, it is owned by @info.
1959  *
1960  * Since: 2.26
1961  */
1962 GDBusSignalInfo *
1963 g_dbus_interface_info_lookup_signal (GDBusInterfaceInfo *info,
1964                                      const gchar        *name)
1965 {
1966   guint n;
1967   GDBusSignalInfo *result;
1968
1969   for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
1970     {
1971       GDBusSignalInfo *i = info->signals[n];
1972
1973       if (g_strcmp0 (i->name, name) == 0)
1974         {
1975           result = i;
1976           goto out;
1977         }
1978     }
1979
1980   result = NULL;
1981
1982  out:
1983   return result;
1984 }
1985
1986 /* ---------------------------------------------------------------------------------------------------- */
1987
1988 /**
1989  * g_dbus_interface_info_lookup_property:
1990  * @info: A #GDBusInterfaceInfo.
1991  * @name: A D-Bus property name (typically in CamelCase).
1992  *
1993  * Looks up information about a property.
1994  *
1995  * This cost of this function is O(n) in number of properties.
1996  *
1997  * Returns: A #GDBusPropertyInfo or %NULL if not found. Do not free, it is owned by @info.
1998  *
1999  * Since: 2.26
2000  */
2001 GDBusPropertyInfo *
2002 g_dbus_interface_info_lookup_property (GDBusInterfaceInfo *info,
2003                                        const gchar        *name)
2004 {
2005   guint n;
2006   GDBusPropertyInfo *result;
2007
2008   for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
2009     {
2010       GDBusPropertyInfo *i = info->properties[n];
2011
2012       if (g_strcmp0 (i->name, name) == 0)
2013         {
2014           result = i;
2015           goto out;
2016         }
2017     }
2018
2019   result = NULL;
2020
2021  out:
2022   return result;
2023 }
2024
2025 /* ---------------------------------------------------------------------------------------------------- */
2026
2027 /**
2028  * g_dbus_node_info_lookup_interface:
2029  * @info: A #GDBusNodeInfo.
2030  * @name: A D-Bus interface name.
2031  *
2032  * Looks up information about an interface.
2033  *
2034  * This cost of this function is O(n) in number of interfaces.
2035  *
2036  * Returns: A #GDBusInterfaceInfo or %NULL if not found. Do not free, it is owned by @info.
2037  *
2038  * Since: 2.26
2039  */
2040 GDBusInterfaceInfo *
2041 g_dbus_node_info_lookup_interface (GDBusNodeInfo *info,
2042                                    const gchar   *name)
2043 {
2044   guint n;
2045   GDBusInterfaceInfo *result;
2046
2047   for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
2048     {
2049       GDBusInterfaceInfo *i = info->interfaces[n];
2050
2051       if (g_strcmp0 (i->name, name) == 0)
2052         {
2053           result = i;
2054           goto out;
2055         }
2056     }
2057
2058   result = NULL;
2059
2060  out:
2061   return result;
2062 }