Do not use g_error for a few more errors, instead set the GError sent in

2008-02-29  Johan Dahlin  <johan@gnome.org>

	* gtk/gtkbuilder.c:
	* gtk/gtkbuilderparser.c:
	* gtk/gtkbuilderprivate.h:
	Do not use g_error for a few more errors, instead set
	the GError sent in through add_from_file/add_from_string.
	* tests/buildertest.c:
	Add a couple of new parsing tests.
	(#519199, Pavel Syomin)


svn path=/trunk/; revision=19681
This commit is contained in:
Johan Dahlin 2008-03-01 13:49:06 +00:00 committed by Johan Dahlin
parent 9b440c1656
commit a57b66aac2
5 changed files with 91 additions and 19 deletions

View File

@ -1,3 +1,14 @@
2008-02-29 Johan Dahlin <johan@gnome.org>
* gtk/gtkbuilder.c:
* gtk/gtkbuilderparser.c:
* gtk/gtkbuilderprivate.h:
Do not use g_error for a few more errors, instead set
the GError sent in through add_from_file/add_from_string.
* tests/buildertest.c:
Add a couple of new parsing tests.
(#519199, Pavel Syomin)
2008-02-29 Federico Mena Quintero <federico@novell.com>
* gtk/gtkfilesystemunix.c (gtk_file_system_unix_get_folder): When

View File

@ -334,7 +334,8 @@ gtk_builder_get_parameters (GtkBuilder *builder,
static GObject *
gtk_builder_get_internal_child (GtkBuilder *builder,
ObjectInfo *info,
const gchar *childname)
const gchar *childname,
GError **error)
{
GObject *obj = NULL;
@ -359,14 +360,19 @@ gtk_builder_get_internal_child (GtkBuilder *builder,
};
if (!obj)
g_error ("Unknown internal child: %s\n", childname);
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Unknown internal child: %s", childname);
}
return obj;
}
GObject *
_gtk_builder_construct (GtkBuilder *builder,
ObjectInfo *info)
ObjectInfo *info,
GError **error)
{
GArray *parameters, *construct_parameters;
GType object_type;
@ -379,7 +385,14 @@ _gtk_builder_construct (GtkBuilder *builder,
g_assert (info->class_name != NULL);
object_type = gtk_builder_get_type_from_name (builder, info->class_name);
if (object_type == G_TYPE_INVALID)
g_error ("Invalid type: %s", info->class_name);
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Invalid object type `%s'",
info->class_name);
return NULL;
}
gtk_builder_get_parameters (builder, object_type,
info->id,
@ -393,9 +406,17 @@ _gtk_builder_construct (GtkBuilder *builder,
constructor = gtk_builder_get_object (builder, info->constructor);
if (constructor == NULL)
g_error ("Unknown constructor for %s: %s\n", info->id,
info->constructor);
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Unknown object constructor for %s: %s",
info->id,
info->constructor);
g_array_free (parameters, TRUE);
g_array_free (construct_parameters, TRUE);
return NULL;
}
obj = gtk_buildable_construct_child (GTK_BUILDABLE (constructor),
builder,
info->id);
@ -406,7 +427,13 @@ _gtk_builder_construct (GtkBuilder *builder,
else if (info->parent && ((ChildInfo*)info->parent)->internal_child != NULL)
{
gchar *childname = ((ChildInfo*)info->parent)->internal_child;
obj = gtk_builder_get_internal_child (builder, info, childname);
obj = gtk_builder_get_internal_child (builder, info, childname, error);
if (!obj)
{
g_array_free (parameters, TRUE);
g_array_free (construct_parameters, TRUE);
return NULL;
}
if (construct_parameters->len)
g_warning ("Can't pass in construct-only parameters to %s", childname);
g_object_ref (obj);

View File

@ -182,8 +182,9 @@ _gtk_builder_boolean_from_string (const gchar *string,
}
static GObject *
builder_construct (ParserData *data,
ObjectInfo *object_info)
builder_construct (ParserData *data,
ObjectInfo *object_info,
GError **error)
{
GObject *object;
@ -194,7 +195,10 @@ builder_construct (ParserData *data,
object_info->properties = g_slist_reverse (object_info->properties);
object = _gtk_builder_construct (data->builder, object_info);
object = _gtk_builder_construct (data->builder, object_info, error);
if (!object)
return NULL;
g_assert (G_IS_OBJECT (object));
object_info->object = object;
@ -345,7 +349,7 @@ parse_child (ParserData *data,
child_info->parent = (CommonInfo*)object_info;
object_info->object = builder_construct (data, object_info);
object_info->object = builder_construct (data, object_info, error);
}
static void
@ -644,8 +648,13 @@ parse_custom (GMarkupParseContext *context,
{
ObjectInfo* object_info = (ObjectInfo*)parent_info;
if (!object_info->object)
object_info->object = _gtk_builder_construct (data->builder,
object_info);
{
object_info->object = _gtk_builder_construct (data->builder,
object_info,
error);
if (!object_info->object)
return TRUE; /* A GError is already set */
}
g_assert (object_info->object);
object = object_info->object;
child = NULL;
@ -804,8 +813,12 @@ end_element (GMarkupParseContext *context,
ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
ChildInfo* child_info = state_peek_info (data, ChildInfo);
object_info->object = builder_construct (data, object_info);
object_info->object = builder_construct (data, object_info, error);
if (!object_info->object)
{
free_object_info (object_info);
return;
}
if (child_info)
child_info->object = object_info->object;

View File

@ -103,7 +103,8 @@ void _gtk_builder_parser_parse_buffer (GtkBuilder *builder,
gsize length,
GError **error);
GObject * _gtk_builder_construct (GtkBuilder *builder,
ObjectInfo *info);
ObjectInfo *info,
GError **error);
void _gtk_builder_add (GtkBuilder *builder,
ChildInfo *child_info);
void _gtk_builder_add_signals (GtkBuilder *builder,

View File

@ -77,6 +77,27 @@ test_parser (void)
g_assert (error->code == GTK_BUILDER_ERROR_INVALID_TAG);
g_error_free (error);
error = NULL;
gtk_builder_add_from_string (builder, "<interface><object class=\"Unknown\" id=\"a\"></object></interface>", -1, &error);
g_assert (error != NULL);
g_assert (error->domain == GTK_BUILDER_ERROR);
g_assert (error->code == GTK_BUILDER_ERROR_INVALID_VALUE);
g_error_free (error);
error = NULL;
gtk_builder_add_from_string (builder, "<interface><object class=\"GtkWidget\" id=\"a\" constructor=\"none\"></object></interface>", -1, &error);
g_assert (error != NULL);
g_assert (error->domain == GTK_BUILDER_ERROR);
g_assert (error->code == GTK_BUILDER_ERROR_INVALID_VALUE);
g_error_free (error);
error = NULL;
gtk_builder_add_from_string (builder, "<interface><object class=\"GtkButton\" id=\"a\"><child internal-child=\"foobar\"><object class=\"GtkButton\" id=\"int\"/></child></object></interface>", -1, &error);
g_assert (error != NULL);
g_assert (error->domain == GTK_BUILDER_ERROR);
g_assert (error->code == GTK_BUILDER_ERROR_INVALID_VALUE);
g_error_free (error);
g_object_unref (builder);
}
@ -1853,6 +1874,5 @@ main (int argc, char **argv)
g_test_add_func ("/Builder/Value From String", test_value_from_string);
g_test_add_func ("/Builder/Reference Counting", test_reference_counting);
g_test_add_func ("/Builder/Window", test_window);
return g_test_run();
}