1 /* gtkbuilderparser.c
2 * Copyright (C) 2006-2007 Async Open Source,
3 * Johan Dahlin <jdahlin@async.com.br>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * 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
21 #include "config.h"
22
23 #include <string.h>
24 #include <gmodule.h>
25
26 #include "gtktypeutils.h"
27 #include "gtkbuilderprivate.h"
28 #include "gtkbuilder.h"
29 #include "gtkbuildable.h"
30 #include "gtkdebug.h"
31 #include "gtkversion.h"
32 #include "gtktypeutils.h"
33 #include "gtkintl.h"
34 #include "gtkalias.h"
35
36 static void free_property_info (PropertyInfo *info);
37 static void free_object_info (ObjectInfo *info);
38
39 static inline void
state_push(ParserData * data,gpointer info)40 state_push (ParserData *data, gpointer info)
41 {
42 data->stack = g_slist_prepend (data->stack, info);
43 }
44
45 static inline gpointer
state_peek(ParserData * data)46 state_peek (ParserData *data)
47 {
48 if (!data->stack)
49 return NULL;
50
51 return data->stack->data;
52 }
53
54 static inline gpointer
state_pop(ParserData * data)55 state_pop (ParserData *data)
56 {
57 gpointer old = NULL;
58
59 if (!data->stack)
60 return NULL;
61
62 old = data->stack->data;
63 data->stack = g_slist_delete_link (data->stack, data->stack);
64 return old;
65 }
66 #define state_peek_info(data, st) ((st*)state_peek(data))
67 #define state_pop_info(data, st) ((st*)state_pop(data))
68
69 static void
error_missing_attribute(ParserData * data,const gchar * tag,const gchar * attribute,GError ** error)70 error_missing_attribute (ParserData *data,
71 const gchar *tag,
72 const gchar *attribute,
73 GError **error)
74 {
75 gint line_number, char_number;
76
77 g_markup_parse_context_get_position (data->ctx,
78 &line_number,
79 &char_number);
80
81 g_set_error (error,
82 GTK_BUILDER_ERROR,
83 GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
84 "%s:%d:%d <%s> requires attribute \"%s\"",
85 data->filename,
86 line_number, char_number, tag, attribute);
87 }
88
89 static void
error_invalid_attribute(ParserData * data,const gchar * tag,const gchar * attribute,GError ** error)90 error_invalid_attribute (ParserData *data,
91 const gchar *tag,
92 const gchar *attribute,
93 GError **error)
94 {
95 gint line_number, char_number;
96
97 g_markup_parse_context_get_position (data->ctx,
98 &line_number,
99 &char_number);
100
101 g_set_error (error,
102 GTK_BUILDER_ERROR,
103 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
104 "%s:%d:%d '%s' is not a valid attribute of <%s>",
105 data->filename,
106 line_number, char_number, attribute, tag);
107 }
108
109 static void
error_invalid_tag(ParserData * data,const gchar * tag,const gchar * expected,GError ** error)110 error_invalid_tag (ParserData *data,
111 const gchar *tag,
112 const gchar *expected,
113 GError **error)
114 {
115 gint line_number, char_number;
116
117 g_markup_parse_context_get_position (data->ctx,
118 &line_number,
119 &char_number);
120
121 if (expected)
122 g_set_error (error,
123 GTK_BUILDER_ERROR,
124 GTK_BUILDER_ERROR_INVALID_TAG,
125 "%s:%d:%d '%s' is not a valid tag here, expected a '%s' tag",
126 data->filename,
127 line_number, char_number, tag, expected);
128 else
129 g_set_error (error,
130 GTK_BUILDER_ERROR,
131 GTK_BUILDER_ERROR_INVALID_TAG,
132 "%s:%d:%d '%s' is not a valid tag here",
133 data->filename,
134 line_number, char_number, tag);
135 }
136
137 gboolean
_gtk_builder_boolean_from_string(const gchar * string,gboolean * value,GError ** error)138 _gtk_builder_boolean_from_string (const gchar *string,
139 gboolean *value,
140 GError **error)
141 {
142 gboolean retval = TRUE;
143 int length;
144
145 g_assert (string != NULL);
146 length = strlen (string);
147
148 if (length == 0)
149 retval = FALSE;
150 else if (length == 1)
151 {
152 gchar c = g_ascii_tolower (string[0]);
153 if (c == 'y' || c == 't' || c == '1')
154 *value = TRUE;
155 else if (c == 'n' || c == 'f' || c == '0')
156 *value = FALSE;
157 else
158 retval = FALSE;
159 }
160 else
161 {
162 gchar *lower = g_ascii_strdown (string, length);
163
164 if (strcmp (lower, "yes") == 0 || strcmp (lower, "true") == 0)
165 *value = TRUE;
166 else if (strcmp (lower, "no") == 0 || strcmp (lower, "false") == 0)
167 *value = FALSE;
168 else
169 retval = FALSE;
170 g_free (lower);
171 }
172
173 if (!retval)
174 g_set_error (error,
175 GTK_BUILDER_ERROR,
176 GTK_BUILDER_ERROR_INVALID_VALUE,
177 "could not parse boolean `%s'",
178 string);
179
180 return retval;
181 }
182
183 static GObject *
builder_construct(ParserData * data,ObjectInfo * object_info,GError ** error)184 builder_construct (ParserData *data,
185 ObjectInfo *object_info,
186 GError **error)
187 {
188 GObject *object;
189
190 g_assert (object_info != NULL);
191
192 if (object_info->object)
193 return object_info->object;
194
195 object_info->properties = g_slist_reverse (object_info->properties);
196
197 object = _gtk_builder_construct (data->builder, object_info, error);
198 if (!object)
199 return NULL;
200
201 g_assert (G_IS_OBJECT (object));
202
203 object_info->object = object;
204
205 return object;
206 }
207
208 static gchar *
_get_type_by_symbol(const gchar * symbol)209 _get_type_by_symbol (const gchar* symbol)
210 {
211 static GModule *module = NULL;
212 GTypeGetFunc func;
213 GType type;
214
215 if (!module)
216 module = g_module_open (NULL, 0);
217
218 if (!g_module_symbol (module, symbol, (gpointer)&func))
219 return NULL;
220
221 type = func ();
222 if (type == G_TYPE_INVALID)
223 return NULL;
224
225 return g_strdup (g_type_name (type));
226 }
227
228 static void
parse_requires(ParserData * data,const gchar * element_name,const gchar ** names,const gchar ** values,GError ** error)229 parse_requires (ParserData *data,
230 const gchar *element_name,
231 const gchar **names,
232 const gchar **values,
233 GError **error)
234 {
235 RequiresInfo *req_info;
236 const gchar *library = NULL;
237 const gchar *version = NULL;
238 gchar **split;
239 gint i, version_major = 0, version_minor = 0;
240 gint line_number, char_number;
241
242 g_markup_parse_context_get_position (data->ctx,
243 &line_number,
244 &char_number);
245
246 for (i = 0; names[i] != NULL; i++)
247 {
248 if (strcmp (names[i], "lib") == 0)
249 library = values[i];
250 else if (strcmp (names[i], "version") == 0)
251 version = values[i];
252 else
253 error_invalid_attribute (data, element_name, names[i], error);
254 }
255
256 if (!library || !version)
257 {
258 error_missing_attribute (data, element_name,
259 version ? "lib" : "version", error);
260 return;
261 }
262
263 if (!(split = g_strsplit (version, ".", 2)) || !split[0] || !split[1])
264 {
265 g_set_error (error,
266 GTK_BUILDER_ERROR,
267 GTK_BUILDER_ERROR_INVALID_VALUE,
268 "%s:%d:%d <%s> attribute has malformed value \"%s\"",
269 data->filename,
270 line_number, char_number, "version", version);
271 return;
272 }
273 version_major = g_ascii_strtoll (split[0], NULL, 10);
274 version_minor = g_ascii_strtoll (split[1], NULL, 10);
275 g_strfreev (split);
276
277 req_info = g_slice_new0 (RequiresInfo);
278 req_info->library = g_strdup (library);
279 req_info->major = version_major;
280 req_info->minor = version_minor;
281 state_push (data, req_info);
282 req_info->tag.name = element_name;
283 }
284
285 static gboolean
is_requested_object(const gchar * object,ParserData * data)286 is_requested_object (const gchar *object,
287 ParserData *data)
288 {
289 GSList *l;
290
291 for (l = data->requested_objects; l; l = l->next)
292 {
293 if (strcmp (l->data, object) == 0)
294 return TRUE;
295 }
296
297 return FALSE;
298 }
299
300 static void
parse_object(GMarkupParseContext * context,ParserData * data,const gchar * element_name,const gchar ** names,const gchar ** values,GError ** error)301 parse_object (GMarkupParseContext *context,
302 ParserData *data,
303 const gchar *element_name,
304 const gchar **names,
305 const gchar **values,
306 GError **error)
307 {
308 ObjectInfo *object_info;
309 ChildInfo* child_info;
310 int i;
311 gchar *object_class = NULL;
312 gchar *object_id = NULL;
313 gchar *constructor = NULL;
314 gint line, line2;
315
316 child_info = state_peek_info (data, ChildInfo);
317 if (child_info && strcmp (child_info->tag.name, "object") == 0)
318 {
319 error_invalid_tag (data, element_name, NULL, error);
320 return;
321 }
322
323 for (i = 0; names[i] != NULL; i++)
324 {
325 if (strcmp (names[i], "class") == 0)
326 object_class = g_strdup (values[i]);
327 else if (strcmp (names[i], "id") == 0)
328 object_id = g_strdup (values[i]);
329 else if (strcmp (names[i], "constructor") == 0)
330 constructor = g_strdup (values[i]);
331 else if (strcmp (names[i], "type-func") == 0)
332 {
333 /* Call the GType function, and return the name of the GType,
334 * it's guaranteed afterwards that g_type_from_name on the name
335 * will return our GType
336 */
337 object_class = _get_type_by_symbol (values[i]);
338 if (!object_class)
339 {
340 g_markup_parse_context_get_position (context, &line, NULL);
341 g_set_error (error, GTK_BUILDER_ERROR,
342 GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
343 _("Invalid type function on line %d: '%s'"),
344 line, values[i]);
345 return;
346 }
347 }
348 else
349 {
350 error_invalid_attribute (data, element_name, names[i], error);
351 return;
352 }
353 }
354
355 if (!object_class)
356 {
357 error_missing_attribute (data, element_name, "class", error);
358 return;
359 }
360
361 if (!object_id)
362 {
363 error_missing_attribute (data, element_name, "id", error);
364 return;
365 }
366
367 ++data->cur_object_level;
368
369 /* check if we reached a requested object (if it is specified) */
370 if (data->requested_objects && !data->inside_requested_object)
371 {
372 if (is_requested_object (object_id, data))
373 {
374 data->requested_object_level = data->cur_object_level;
375
376 GTK_NOTE (BUILDER, g_print ("requested object \"%s\" found at level %d\n",
377 object_id,
378 data->requested_object_level));
379
380 data->inside_requested_object = TRUE;
381 }
382 else
383 {
384 g_free (object_class);
385 g_free (object_id);
386 g_free (constructor);
387 return;
388 }
389 }
390
391 object_info = g_slice_new0 (ObjectInfo);
392 object_info->class_name = object_class;
393 object_info->id = object_id;
394 object_info->constructor = constructor;
395 state_push (data, object_info);
396 object_info->tag.name = element_name;
397
398 if (child_info)
399 object_info->parent = (CommonInfo*)child_info;
400
401 g_markup_parse_context_get_position (context, &line, NULL);
402 line2 = GPOINTER_TO_INT (g_hash_table_lookup (data->object_ids, object_id));
403 if (line2 != 0)
404 {
405 g_set_error (error, GTK_BUILDER_ERROR,
406 GTK_BUILDER_ERROR_DUPLICATE_ID,
407 _("Duplicate object ID '%s' on line %d (previously on line %d)"),
408 object_id, line, line2);
409 return;
410 }
411
412
413 g_hash_table_insert (data->object_ids, g_strdup (object_id), GINT_TO_POINTER (line));
414 }
415
416 static void
free_object_info(ObjectInfo * info)417 free_object_info (ObjectInfo *info)
418 {
419 /* Do not free the signal items, which GtkBuilder takes ownership of */
420 g_slist_free (info->signals);
421 g_slist_foreach (info->properties,
422 (GFunc)free_property_info, NULL);
423 g_slist_free (info->properties);
424 g_free (info->constructor);
425 g_free (info->class_name);
426 g_free (info->id);
427 g_slice_free (ObjectInfo, info);
428 }
429
430 static void
parse_child(ParserData * data,const gchar * element_name,const gchar ** names,const gchar ** values,GError ** error)431 parse_child (ParserData *data,
432 const gchar *element_name,
433 const gchar **names,
434 const gchar **values,
435 GError **error)
436
437 {
438 ObjectInfo* object_info;
439 ChildInfo *child_info;
440 guint i;
441
442 object_info = state_peek_info (data, ObjectInfo);
443 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
444 {
445 error_invalid_tag (data, element_name, NULL, error);
446 return;
447 }
448
449 child_info = g_slice_new0 (ChildInfo);
450 state_push (data, child_info);
451 child_info->tag.name = element_name;
452 for (i = 0; names[i]; i++)
453 {
454 if (strcmp (names[i], "type") == 0)
455 child_info->type = g_strdup (values[i]);
456 else if (strcmp (names[i], "internal-child") == 0)
457 child_info->internal_child = g_strdup (values[i]);
458 else
459 error_invalid_attribute (data, element_name, names[i], error);
460 }
461
462 child_info->parent = (CommonInfo*)object_info;
463
464 object_info->object = builder_construct (data, object_info, error);
465 }
466
467 static void
free_child_info(ChildInfo * info)468 free_child_info (ChildInfo *info)
469 {
470 g_free (info->type);
471 g_free (info->internal_child);
472 g_slice_free (ChildInfo, info);
473 }
474
475 static void
parse_property(ParserData * data,const gchar * element_name,const gchar ** names,const gchar ** values,GError ** error)476 parse_property (ParserData *data,
477 const gchar *element_name,
478 const gchar **names,
479 const gchar **values,
480 GError **error)
481 {
482 PropertyInfo *info;
483 gchar *name = NULL;
484 gchar *context = NULL;
485 gboolean translatable = FALSE;
486 ObjectInfo *object_info;
487 int i;
488
489 object_info = state_peek_info (data, ObjectInfo);
490 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
491 {
492 error_invalid_tag (data, element_name, NULL, error);
493 return;
494 }
495
496 for (i = 0; names[i] != NULL; i++)
497 {
498 if (strcmp (names[i], "name") == 0)
499 name = g_strdelimit (g_strdup (values[i]), "_", '-');
500 else if (strcmp (names[i], "translatable") == 0)
501 {
502 if (!_gtk_builder_boolean_from_string (values[i], &translatable,
503 error))
504 return;
505 }
506 else if (strcmp (names[i], "comments") == 0)
507 {
508 /* do nothing, comments are for translators */
509 }
510 else if (strcmp (names[i], "context") == 0)
511 {
512 context = g_strdup (values[i]);
513 }
514 else
515 {
516 error_invalid_attribute (data, element_name, names[i], error);
517 return;
518 }
519 }
520
521 if (!name)
522 {
523 error_missing_attribute (data, element_name, "name", error);
524 return;
525 }
526
527 info = g_slice_new0 (PropertyInfo);
528 info->name = name;
529 info->translatable = translatable;
530 info->context = context;
531 info->text = g_string_new ("");
532 state_push (data, info);
533
534 info->tag.name = element_name;
535 }
536
537 static void
free_property_info(PropertyInfo * info)538 free_property_info (PropertyInfo *info)
539 {
540 g_free (info->data);
541 g_free (info->name);
542 g_slice_free (PropertyInfo, info);
543 }
544
545 static void
parse_signal(ParserData * data,const gchar * element_name,const gchar ** names,const gchar ** values,GError ** error)546 parse_signal (ParserData *data,
547 const gchar *element_name,
548 const gchar **names,
549 const gchar **values,
550 GError **error)
551 {
552 SignalInfo *info;
553 gchar *name = NULL;
554 gchar *handler = NULL;
555 gchar *object = NULL;
556 gboolean after = FALSE;
557 gboolean swapped = FALSE;
558 gboolean swapped_set = FALSE;
559 ObjectInfo *object_info;
560 int i;
561
562 object_info = state_peek_info (data, ObjectInfo);
563 if (!object_info || strcmp (object_info->tag.name, "object") != 0)
564 {
565 error_invalid_tag (data, element_name, NULL, error);
566 return;
567 }
568
569 for (i = 0; names[i] != NULL; i++)
570 {
571 if (strcmp (names[i], "name") == 0)
572 name = g_strdup (values[i]);
573 else if (strcmp (names[i], "handler") == 0)
574 handler = g_strdup (values[i]);
575 else if (strcmp (names[i], "after") == 0)
576 {
577 if (!_gtk_builder_boolean_from_string (values[i], &after, error))
578 return;
579 }
580 else if (strcmp (names[i], "swapped") == 0)
581 {
582 if (!_gtk_builder_boolean_from_string (values[i], &swapped, error))
583 return;
584 swapped_set = TRUE;
585 }
586 else if (strcmp (names[i], "object") == 0)
587 object = g_strdup (values[i]);
588 else if (strcmp (names[i], "last_modification_time") == 0)
589 /* parse but ignore */
590 ;
591 else
592 {
593 error_invalid_attribute (data, element_name, names[i], error);
594 return;
595 }
596 }
597
598 if (!name)
599 {
600 error_missing_attribute (data, element_name, "name", error);
601 return;
602 }
603 else if (!handler)
604 {
605 error_missing_attribute (data, element_name, "handler", error);
606 return;
607 }
608
609 /* Swapped defaults to FALSE except when object is set */
610 if (object && !swapped_set)
611 swapped = TRUE;
612
613 info = g_slice_new0 (SignalInfo);
614 info->name = name;
615 info->handler = handler;
616 if (after)
617 info->flags |= G_CONNECT_AFTER;
618 if (swapped)
619 info->flags |= G_CONNECT_SWAPPED;
620 info->connect_object_name = object;
621 state_push (data, info);
622
623 info->tag.name = element_name;
624 }
625
626 /* Called by GtkBuilder */
627 void
_free_signal_info(SignalInfo * info,gpointer user_data)628 _free_signal_info (SignalInfo *info,
629 gpointer user_data)
630 {
631 g_free (info->name);
632 g_free (info->handler);
633 g_free (info->connect_object_name);
634 g_free (info->object_name);
635 g_slice_free (SignalInfo, info);
636 }
637
638 void
_free_requires_info(RequiresInfo * info,gpointer user_data)639 _free_requires_info (RequiresInfo *info,
640 gpointer user_data)
641 {
642 g_free (info->library);
643 g_slice_free (RequiresInfo, info);
644 }
645
646 static void
parse_interface(ParserData * data,const gchar * element_name,const gchar ** names,const gchar ** values,GError ** error)647 parse_interface (ParserData *data,
648 const gchar *element_name,
649 const gchar **names,
650 const gchar **values,
651 GError **error)
652 {
653 int i;
654
655 for (i = 0; names[i] != NULL; i++)
656 {
657 if (strcmp (names[i], "domain") == 0)
658 {
659 if (data->domain)
660 {
661 if (strcmp (data->domain, values[i]) == 0)
662 continue;
663 else
664 g_warning ("%s: interface domain '%s' overrides "
665 "programically set domain '%s'",
666 data->filename,
667 values[i],
668 data->domain
669 );
670
671 g_free (data->domain);
672 }
673
674 data->domain = g_strdup (values[i]);
675 gtk_builder_set_translation_domain (data->builder, data->domain);
676 }
677 else
678 error_invalid_attribute (data, "interface", names[i], error);
679 }
680 }
681
682 static SubParser *
create_subparser(GObject * object,GObject * child,const gchar * element_name,GMarkupParser * parser,gpointer user_data)683 create_subparser (GObject *object,
684 GObject *child,
685 const gchar *element_name,
686 GMarkupParser *parser,
687 gpointer user_data)
688 {
689 SubParser *subparser;
690
691 subparser = g_slice_new0 (SubParser);
692 subparser->object = object;
693 subparser->child = child;
694 subparser->tagname = g_strdup (element_name);
695 subparser->start = element_name;
696 subparser->parser = g_memdup (parser, sizeof (GMarkupParser));
697 subparser->data = user_data;
698
699 return subparser;
700 }
701
702 static void
free_subparser(SubParser * subparser)703 free_subparser (SubParser *subparser)
704 {
705 g_free (subparser->tagname);
706 g_slice_free (SubParser, subparser);
707 }
708
709 static gboolean
subparser_start(GMarkupParseContext * context,const gchar * element_name,const gchar ** names,const gchar ** values,ParserData * data,GError ** error)710 subparser_start (GMarkupParseContext *context,
711 const gchar *element_name,
712 const gchar **names,
713 const gchar **values,
714 ParserData *data,
715 GError **error)
716 {
717 SubParser *subparser = data->subparser;
718
719 if (!subparser->start &&
720 strcmp (element_name, subparser->tagname) == 0)
721 subparser->start = element_name;
722
723 if (subparser->start)
724 {
725 if (subparser->parser->start_element)
726 subparser->parser->start_element (context,
727 element_name, names, values,
728 subparser->data, error);
729 return FALSE;
730 }
731 return TRUE;
732 }
733
734 static void
subparser_end(GMarkupParseContext * context,const gchar * element_name,ParserData * data,GError ** error)735 subparser_end (GMarkupParseContext *context,
736 const gchar *element_name,
737 ParserData *data,
738 GError **error)
739 {
740 if (data->subparser->parser->end_element)
741 data->subparser->parser->end_element (context, element_name,
742 data->subparser->data, error);
743
744 if (!strcmp (data->subparser->start, element_name) == 0)
745 return;
746
747 gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
748 data->builder,
749 data->subparser->child,
750 element_name,
751 data->subparser->data);
752 g_free (data->subparser->parser);
753
754 if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
755 data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
756 data->subparser);
757 else
758 free_subparser (data->subparser);
759
760 data->subparser = NULL;
761 }
762
763 static gboolean
parse_custom(GMarkupParseContext * context,const gchar * element_name,const gchar ** names,const gchar ** values,ParserData * data,GError ** error)764 parse_custom (GMarkupParseContext *context,
765 const gchar *element_name,
766 const gchar **names,
767 const gchar **values,
768 ParserData *data,
769 GError **error)
770 {
771 CommonInfo* parent_info;
772 GMarkupParser parser;
773 gpointer subparser_data;
774 GObject *object;
775 GObject *child;
776
777 parent_info = state_peek_info (data, CommonInfo);
778 if (!parent_info)
779 return FALSE;
780
781 if (strcmp (parent_info->tag.name, "object") == 0)
782 {
783 ObjectInfo* object_info = (ObjectInfo*)parent_info;
784 if (!object_info->object)
785 {
786 object_info->properties = g_slist_reverse (object_info->properties);
787 object_info->object = _gtk_builder_construct (data->builder,
788 object_info,
789 error);
790 if (!object_info->object)
791 return TRUE; /* A GError is already set */
792 }
793 g_assert (object_info->object);
794 object = object_info->object;
795 child = NULL;
796 }
797 else if (strcmp (parent_info->tag.name, "child") == 0)
798 {
799 ChildInfo* child_info = (ChildInfo*)parent_info;
800
801 _gtk_builder_add (data->builder, child_info);
802
803 object = ((ObjectInfo*)child_info->parent)->object;
804 child = child_info->object;
805 }
806 else
807 return FALSE;
808
809 if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
810 data->builder,
811 child,
812 element_name,
813 &parser,
814 &subparser_data))
815 return FALSE;
816
817 data->subparser = create_subparser (object, child, element_name,
818 &parser, subparser_data);
819
820 if (parser.start_element)
821 parser.start_element (context,
822 element_name, names, values,
823 subparser_data, error);
824 return TRUE;
825 }
826
827 static void
start_element(GMarkupParseContext * context,const gchar * element_name,const gchar ** names,const gchar ** values,gpointer user_data,GError ** error)828 start_element (GMarkupParseContext *context,
829 const gchar *element_name,
830 const gchar **names,
831 const gchar **values,
832 gpointer user_data,
833 GError **error)
834 {
835 ParserData *data = (ParserData*)user_data;
836
837 #ifdef GTK_ENABLE_DEBUG
838 if (gtk_debug_flags & GTK_DEBUG_BUILDER)
839 {
840 GString *tags = g_string_new ("");
841 int i;
842 for (i = 0; names[i]; i++)
843 g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
844
845 if (i)
846 {
847 g_string_insert_c (tags, 0, ' ');
848 g_string_truncate (tags, tags->len - 1);
849 }
850 g_print ("<%s%s>\n", element_name, tags->str);
851 g_string_free (tags, TRUE);
852 }
853 #endif
854
855 if (!data->last_element && strcmp (element_name, "interface") != 0)
856 {
857 g_set_error (error, GTK_BUILDER_ERROR,
858 GTK_BUILDER_ERROR_UNHANDLED_TAG,
859 _("Invalid root element: '%s'"),
860 element_name);
861 return;
862 }
863 data->last_element = element_name;
864
865 if (data->subparser)
866 if (!subparser_start (context, element_name, names, values,
867 data, error))
868 return;
869
870 if (strcmp (element_name, "requires") == 0)
871 parse_requires (data, element_name, names, values, error);
872 else if (strcmp (element_name, "object") == 0)
873 parse_object (context, data, element_name, names, values, error);
874 else if (data->requested_objects && !data->inside_requested_object)
875 {
876 /* If outside a requested object, simply ignore this tag */
877 return;
878 }
879 else if (strcmp (element_name, "child") == 0)
880 parse_child (data, element_name, names, values, error);
881 else if (strcmp (element_name, "property") == 0)
882 parse_property (data, element_name, names, values, error);
883 else if (strcmp (element_name, "signal") == 0)
884 parse_signal (data, element_name, names, values, error);
885 else if (strcmp (element_name, "interface") == 0)
886 parse_interface (data, element_name, names, values, error);
887 else if (strcmp (element_name, "placeholder") == 0)
888 {
889 /* placeholder has no special treatmeant, but it needs an
890 * if clause to avoid an error below.
891 */
892 }
893 else
894 if (!parse_custom (context, element_name, names, values,
895 data, error))
896 g_set_error (error, GTK_BUILDER_ERROR,
897 GTK_BUILDER_ERROR_UNHANDLED_TAG,
898 _("Unhandled tag: '%s'"),
899 element_name);
900 }
901
902 gchar *
_gtk_builder_parser_translate(const gchar * domain,const gchar * context,const gchar * text)903 _gtk_builder_parser_translate (const gchar *domain,
904 const gchar *context,
905 const gchar *text)
906 {
907 const char *s;
908
909 if (context)
910 s = g_dpgettext2 (domain, context, text);
911 else
912 s = g_dgettext (domain, text);
913
914 return g_strdup (s);
915 }
916
917 /* Called for close tags </foo> */
918 static void
end_element(GMarkupParseContext * context,const gchar * element_name,gpointer user_data,GError ** error)919 end_element (GMarkupParseContext *context,
920 const gchar *element_name,
921 gpointer user_data,
922 GError **error)
923 {
924 ParserData *data = (ParserData*)user_data;
925
926 GTK_NOTE (BUILDER, g_print ("</%s>\n", element_name));
927
928 if (data->subparser && data->subparser->start)
929 {
930 subparser_end (context, element_name, data, error);
931 return;
932 }
933
934 if (strcmp (element_name, "requires") == 0)
935 {
936 RequiresInfo *req_info = state_pop_info (data, RequiresInfo);
937
938 /* TODO: Allow third party widget developers to check thier
939 * required versions, possibly throw a signal allowing them
940 * to check thier library versions here.
941 */
942 if (!strcmp (req_info->library, "gtk+"))
943 {
944 if (!GTK_CHECK_VERSION (req_info->major, req_info->minor, 0))
945 g_set_error (error,
946 GTK_BUILDER_ERROR,
947 GTK_BUILDER_ERROR_VERSION_MISMATCH,
948 "%s: required %s version %d.%d, current version is %d.%d",
949 data->filename, req_info->library,
950 req_info->major, req_info->minor,
951 GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
952 }
953 _free_requires_info (req_info, NULL);
954 }
955 else if (strcmp (element_name, "interface") == 0)
956 {
957 }
958 else if (data->requested_objects && !data->inside_requested_object)
959 {
960 /* If outside a requested object, simply ignore this tag */
961 return;
962 }
963 else if (strcmp (element_name, "object") == 0)
964 {
965 ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
966 ChildInfo* child_info = state_peek_info (data, ChildInfo);
967
968 if (data->requested_objects && data->inside_requested_object &&
969 (data->cur_object_level == data->requested_object_level))
970 {
971 GTK_NOTE (BUILDER, g_print ("requested object end found at level %d\n",
972 data->requested_object_level));
973
974 data->inside_requested_object = FALSE;
975 }
976
977 --data->cur_object_level;
978
979 g_assert (data->cur_object_level >= 0);
980
981 object_info->object = builder_construct (data, object_info, error);
982 if (!object_info->object)
983 {
984 free_object_info (object_info);
985 return;
986 }
987 if (child_info)
988 child_info->object = object_info->object;
989
990 if (GTK_IS_BUILDABLE (object_info->object) &&
991 GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
992 data->finalizers = g_slist_prepend (data->finalizers, object_info->object);
993 _gtk_builder_add_signals (data->builder, object_info->signals);
994
995 free_object_info (object_info);
996 }
997 else if (strcmp (element_name, "property") == 0)
998 {
999 PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
1000 CommonInfo *info = state_peek_info (data, CommonInfo);
1001
1002 /* Normal properties */
1003 if (strcmp (info->tag.name, "object") == 0)
1004 {
1005 ObjectInfo *object_info = (ObjectInfo*)info;
1006
1007 if (prop_info->translatable && prop_info->text->len)
1008 {
1009 prop_info->data = _gtk_builder_parser_translate (data->domain,
1010 prop_info->context,
1011 prop_info->text->str);
1012 g_string_free (prop_info->text, TRUE);
1013 }
1014 else
1015 {
1016 prop_info->data = g_string_free (prop_info->text, FALSE);
1017
1018 }
1019
1020 object_info->properties =
1021 g_slist_prepend (object_info->properties, prop_info);
1022 }
1023 else
1024 g_assert_not_reached ();
1025 }
1026 else if (strcmp (element_name, "child") == 0)
1027 {
1028 ChildInfo *child_info = state_pop_info (data, ChildInfo);
1029
1030 _gtk_builder_add (data->builder, child_info);
1031
1032 free_child_info (child_info);
1033 }
1034 else if (strcmp (element_name, "signal") == 0)
1035 {
1036 SignalInfo *signal_info = state_pop_info (data, SignalInfo);
1037 ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
1038 signal_info->object_name = g_strdup (object_info->id);
1039 object_info->signals =
1040 g_slist_prepend (object_info->signals, signal_info);
1041 }
1042 else if (strcmp (element_name, "placeholder") == 0)
1043 {
1044 }
1045 else
1046 {
1047 g_assert_not_reached ();
1048 }
1049 }
1050
1051 /* Called for character data */
1052 /* text is not nul-terminated */
1053 static void
XXXtext(GMarkupParseContext * context,const gchar * text,gsize text_len,gpointer user_data,GError ** error)1054 XXXtext (GMarkupParseContext *context,
1055 const gchar *text,
1056 gsize text_len,
1057 gpointer user_data,
1058 GError **error)
1059 {
1060 ParserData *data = (ParserData*)user_data;
1061 CommonInfo *info;
1062
1063 if (data->subparser && data->subparser->start)
1064 {
1065 GError *tmp_error = NULL;
1066
1067 if (data->subparser->parser->text)
1068 data->subparser->parser->text (context, text, text_len,
1069 data->subparser->data, &tmp_error);
1070 if (tmp_error)
1071 g_propagate_error (error, tmp_error);
1072 return;
1073 }
1074
1075 if (!data->stack)
1076 return;
1077
1078 info = state_peek_info (data, CommonInfo);
1079 g_assert (info != NULL);
1080
1081 if (strcmp (g_markup_parse_context_get_element (context), "property") == 0)
1082 {
1083 PropertyInfo *prop_info = (PropertyInfo*)info;
1084
1085 g_string_append_len (prop_info->text, text, text_len);
1086 }
1087 }
1088
1089 static void
free_info(CommonInfo * info)1090 free_info (CommonInfo *info)
1091 {
1092 if (strcmp (info->tag.name, "object") == 0)
1093 free_object_info ((ObjectInfo *)info);
1094 else if (strcmp (info->tag.name, "child") == 0)
1095 free_child_info ((ChildInfo *)info);
1096 else if (strcmp (info->tag.name, "property") == 0)
1097 free_property_info ((PropertyInfo *)info);
1098 else if (strcmp (info->tag.name, "signal") == 0)
1099 _free_signal_info ((SignalInfo *)info, NULL);
1100 else if (strcmp (info->tag.name, "requires") == 0)
1101 _free_requires_info ((RequiresInfo *)info, NULL);
1102 else
1103 g_assert_not_reached ();
1104 }
1105
1106 static const GMarkupParser parser = {
1107 start_element,
1108 end_element,
1109 XXXtext,
1110 NULL,
1111 NULL
1112 };
1113
1114 void
_gtk_builder_parser_parse_buffer(GtkBuilder * builder,const gchar * filename,const gchar * buffer,gsize length,gchar ** requested_objs,GError ** error)1115 _gtk_builder_parser_parse_buffer (GtkBuilder *builder,
1116 const gchar *filename,
1117 const gchar *buffer,
1118 gsize length,
1119 gchar **requested_objs,
1120 GError **error)
1121 {
1122 const gchar* domain;
1123 ParserData *data;
1124 GSList *l;
1125
1126 /* Store the original domain so that interface domain attribute can be
1127 * applied for the builder and the original domain can be restored after
1128 * parsing has finished. This allows subparsers to translate elements with
1129 * gtk_builder_get_translation_domain() without breaking the ABI or API
1130 */
1131 domain = gtk_builder_get_translation_domain (builder);
1132
1133 data = g_new0 (ParserData, 1);
1134 data->builder = builder;
1135 data->filename = filename;
1136 data->domain = g_strdup (domain);
1137 data->object_ids = g_hash_table_new_full (g_str_hash, g_str_equal,
1138 (GDestroyNotify)g_free, NULL);
1139
1140 data->requested_objects = NULL;
1141 if (requested_objs)
1142 {
1143 gint i;
1144
1145 data->inside_requested_object = FALSE;
1146 for (i = 0; requested_objs[i]; ++i)
1147 {
1148 data->requested_objects = g_slist_prepend (data->requested_objects,
1149 g_strdup (requested_objs[i]));
1150 }
1151 }
1152 else
1153 {
1154 /* get all the objects */
1155 data->inside_requested_object = TRUE;
1156 }
1157
1158 data->ctx = g_markup_parse_context_new (&parser,
1159 G_MARKUP_TREAT_CDATA_AS_TEXT,
1160 data, NULL);
1161
1162 if (!g_markup_parse_context_parse (data->ctx, buffer, length, error))
1163 goto out;
1164
1165 _gtk_builder_finish (builder);
1166
1167 /* Custom parser_finished */
1168 data->custom_finalizers = g_slist_reverse (data->custom_finalizers);
1169 for (l = data->custom_finalizers; l; l = l->next)
1170 {
1171 SubParser *sub = (SubParser*)l->data;
1172
1173 gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
1174 builder,
1175 sub->child,
1176 sub->tagname,
1177 sub->data);
1178 }
1179
1180 /* Common parser_finished, for all created objects */
1181 data->finalizers = g_slist_reverse (data->finalizers);
1182 for (l = data->finalizers; l; l = l->next)
1183 {
1184 GtkBuildable *buildable = (GtkBuildable*)l->data;
1185 gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
1186 }
1187
1188 out:
1189
1190 g_slist_foreach (data->stack, (GFunc)free_info, NULL);
1191 g_slist_free (data->stack);
1192 g_slist_foreach (data->custom_finalizers, (GFunc)free_subparser, NULL);
1193 g_slist_free (data->custom_finalizers);
1194 g_slist_free (data->finalizers);
1195 g_slist_foreach (data->requested_objects, (GFunc) g_free, NULL);
1196 g_slist_free (data->requested_objects);
1197 g_free (data->domain);
1198 g_hash_table_destroy (data->object_ids);
1199 g_markup_parse_context_free (data->ctx);
1200 g_free (data);
1201
1202 /* restore the original domain */
1203 gtk_builder_set_translation_domain (builder, domain);
1204 }
1205