GtkDialog: Use the new helpers

Instead of issuing g_warning, fill the provided GError.
This lets us test this error handling, and is the right
thing to do. Use the new GtkBuilder helpers and
g_markup_collect_attributes to do so.
This commit is contained in:
Matthias Clasen 2015-04-25 19:33:52 -04:00
parent f11fedb719
commit 4ee291c8f1

View File

@ -42,6 +42,7 @@
#include "gtkbindings.h" #include "gtkbindings.h"
#include "gtkprivate.h" #include "gtkprivate.h"
#include "gtkbuildable.h" #include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
#include "gtksettings.h" #include "gtksettings.h"
#include "gtktypebuiltins.h" #include "gtktypebuiltins.h"
#include "deprecated/gtkstock.h" #include "deprecated/gtkstock.h"
@ -1672,119 +1673,144 @@ typedef struct {
GtkDialog *dialog; GtkDialog *dialog;
GtkBuilder *builder; GtkBuilder *builder;
GSList *items; GSList *items;
gchar *response; gint response_id;
gboolean is_default; gboolean is_default;
} ActionWidgetsSubParserData; gboolean is_text;
GString *string;
static gint gboolean in_action_widgets;
parse_response_id (const gchar *response_attr) } SubParserData;
{
int response_id;
GEnumClass *enum_class = NULL;
GEnumValue *enum_value;
response_id = g_ascii_strtoll (response_attr, NULL, 10);
if (response_id != 0)
goto out;
enum_class = g_type_class_ref (GTK_TYPE_RESPONSE_TYPE);
enum_value = g_enum_get_value_by_nick (enum_class, response_attr);
if (enum_value == NULL)
goto out;
response_id = enum_value->value;
out:
if (enum_class)
g_type_class_unref (enum_class);
return response_id;
}
static void static void
attributes_start_element (GMarkupParseContext *context, parser_start_element (GMarkupParseContext *context,
const gchar *element_name, const gchar *element_name,
const gchar **names, const gchar **names,
const gchar **values, const gchar **values,
gpointer user_data, gpointer user_data,
GError **error) GError **error)
{ {
ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data; SubParserData *data = (SubParserData*)user_data;
guint i;
if (strcmp (element_name, "action-widget") == 0) if (strcmp (element_name, "action-widget") == 0)
{ {
for (i = 0; names[i]; i++) const gchar *response;
gboolean is_default = FALSE;
GValue gvalue = G_VALUE_INIT;
if (!_gtk_builder_check_parent (data->builder, context, "action-widgets", error))
return;
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING, "response", &response,
G_MARKUP_COLLECT_BOOLEAN|G_MARKUP_COLLECT_OPTIONAL, "default", &is_default,
G_MARKUP_COLLECT_INVALID))
{ {
if (strcmp (names[i], "response") == 0) _gtk_builder_prefix_error (data->builder, context, error);
parser_data->response = g_strdup (values[i]); return;
else if (strcmp (names[i], "default") == 0)
parser_data->is_default = TRUE;
} }
if (!gtk_builder_value_from_string_type (data->builder, GTK_TYPE_RESPONSE_TYPE, response, &gvalue, error))
{
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
data->response_id = g_value_get_enum (&gvalue);
data->is_default = is_default;
data->is_text = TRUE;
g_string_set_size (data->string, 0);
} }
else if (strcmp (element_name, "action-widgets") == 0) else if (strcmp (element_name, "action-widgets") == 0)
return; {
if (!_gtk_builder_check_parent (data->builder, context, "object", error))
return;
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_INVALID, NULL, NULL,
G_MARKUP_COLLECT_INVALID))
_gtk_builder_prefix_error (data->builder, context, error);
data->in_action_widgets = TRUE;
}
else else
g_warning ("Unsupported tag for GtkDialog: %s\n", element_name); {
_gtk_builder_error_unhandled_tag (data->builder, context,
"GtkDialog", element_name,
error);
}
} }
static void static void
attributes_text_element (GMarkupParseContext *context, parser_text_element (GMarkupParseContext *context,
const gchar *text, const gchar *text,
gsize text_len, gsize text_len,
gpointer user_data, gpointer user_data,
GError **error) GError **error)
{ {
ActionWidgetsSubParserData *parser_data = (ActionWidgetsSubParserData*)user_data; SubParserData *data = (SubParserData*)user_data;
ActionWidgetInfo *item;
if (data->is_text)
if (!parser_data->response) g_string_append_len (data->string, text, text_len);
return;
item = g_new (ActionWidgetInfo, 1);
item->widget_name = g_strndup (text, text_len);
item->response_id = parse_response_id (parser_data->response);
g_free (parser_data->response);
item->is_default = parser_data->is_default;
parser_data->items = g_slist_prepend (parser_data->items, item);
parser_data->response = NULL;
parser_data->is_default = FALSE;
} }
static const GMarkupParser attributes_parser = static void
parser_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
SubParserData *data = (SubParserData*)user_data;
if (data->is_text)
{
ActionWidgetInfo *item;
item = g_new (ActionWidgetInfo, 1);
item->widget_name = g_strdup (data->string->str);
item->response_id = data->response_id;
item->is_default = data->is_default;
data->items = g_slist_prepend (data->items, item);
data->is_default = FALSE;
data->is_text = FALSE;
}
}
static const GMarkupParser sub_parser =
{ {
attributes_start_element, parser_start_element,
NULL, parser_end_element,
attributes_text_element, parser_text_element,
}; };
static gboolean static gboolean
gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable, gtk_dialog_buildable_custom_tag_start (GtkBuildable *buildable,
GtkBuilder *builder, GtkBuilder *builder,
GObject *child, GObject *child,
const gchar *tagname, const gchar *tagname,
GMarkupParser *parser, GMarkupParser *parser,
gpointer *data) gpointer *parser_data)
{ {
ActionWidgetsSubParserData *parser_data; SubParserData *data;
if (child) if (child)
return FALSE; return FALSE;
if (strcmp (tagname, "action-widgets") == 0) if (strcmp (tagname, "action-widgets") == 0)
{ {
parser_data = g_slice_new0 (ActionWidgetsSubParserData); data = g_slice_new0 (SubParserData);
parser_data->dialog = GTK_DIALOG (buildable); data->dialog = GTK_DIALOG (buildable);
parser_data->items = NULL; data->builder = builder;
data->string = g_string_new ("");
data->items = NULL;
data->in_action_widgets = FALSE;
*parser = attributes_parser; *parser = sub_parser;
*data = parser_data; *parser_data = data;
return TRUE; return TRUE;
} }
return parent_buildable_iface->custom_tag_start (buildable, builder, child, return parent_buildable_iface->custom_tag_start (buildable, builder, child,
tagname, parser, data); tagname, parser, parser_data);
} }
static void static void
@ -1797,7 +1823,7 @@ gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
GtkDialog *dialog = GTK_DIALOG (buildable); GtkDialog *dialog = GTK_DIALOG (buildable);
GtkDialogPrivate *priv = dialog->priv; GtkDialogPrivate *priv = dialog->priv;
GSList *l; GSList *l;
ActionWidgetsSubParserData *parser_data; SubParserData *data;
GObject *object; GObject *object;
ResponseData *ad; ResponseData *ad;
guint signal_id; guint signal_id;
@ -1809,10 +1835,10 @@ gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
return; return;
} }
parser_data = (ActionWidgetsSubParserData*)user_data; data = (SubParserData*)user_data;
parser_data->items = g_slist_reverse (parser_data->items); data->items = g_slist_reverse (data->items);
for (l = parser_data->items; l; l = l->next) for (l = data->items; l; l = l->next)
{ {
ActionWidgetInfo *item = l->data; ActionWidgetInfo *item = l->data;
gboolean is_action; gboolean is_action;
@ -1867,8 +1893,10 @@ gtk_dialog_buildable_custom_finished (GtkBuildable *buildable,
g_free (item->widget_name); g_free (item->widget_name);
g_free (item); g_free (item);
} }
g_slist_free (parser_data->items);
g_slice_free (ActionWidgetsSubParserData, parser_data); g_slist_free (data->items);
g_string_free (data->string, TRUE);
g_slice_free (SubParserData, data);
update_suggested_action (dialog); update_suggested_action (dialog);
} }
@ -1889,7 +1917,7 @@ gtk_dialog_buildable_add_child (GtkBuildable *buildable,
priv->headerbar = GTK_WIDGET (child); priv->headerbar = GTK_WIDGET (child);
gtk_window_set_titlebar (GTK_WINDOW (buildable), priv->headerbar); gtk_window_set_titlebar (GTK_WINDOW (buildable), priv->headerbar);
} }
else if (g_strcmp0 (type, "action") == 0) else if (g_str_equal (type, "action"))
gtk_dialog_add_action_widget (GTK_DIALOG (buildable), GTK_WIDGET (child), GTK_RESPONSE_NONE); gtk_dialog_add_action_widget (GTK_DIALOG (buildable), GTK_WIDGET (child), GTK_RESPONSE_NONE);
else else
GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type); GTK_BUILDER_WARN_INVALID_CHILD_TYPE (buildable, type);