GtkFileFilter: 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:34:34 -04:00
parent 95ea81d6d1
commit 831c90ffad

View File

@ -69,6 +69,7 @@
#include "gtkfilefilter.h"
#include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
#include "gtkintl.h"
#include "gtkprivate.h"
@ -226,77 +227,96 @@ typedef enum {
typedef struct {
GtkFileFilter *filter;
GtkBuilder *builder;
ParserType type;
GString *string;
gboolean parsing;
} SubParserData;
static void
parser_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **names,
const gchar **values,
gpointer user_data,
GError **error)
parser_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **names,
const gchar **values,
gpointer user_data,
GError **error)
{
SubParserData *parser_data = (SubParserData*)user_data;
SubParserData *data = (SubParserData*)user_data;
if (strcmp (element_name, "mime-types") == 0)
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);
return;
}
if (strcmp (element_name, "mime-types") == 0 ||
strcmp (element_name, "patterns") == 0)
{
if (!_gtk_builder_check_parent (data->builder, context, "object", error))
return;
}
else if (strcmp (element_name, "mime-type") == 0)
{
parser_data->parsing = TRUE;
return;
if (!_gtk_builder_check_parent (data->builder, context, "mime-types", error))
return;
data->parsing = TRUE;
}
else if (strcmp (element_name, "patterns") == 0)
return;
else if (strcmp (element_name, "pattern") == 0)
{
parser_data->parsing = TRUE;
return;
if (!_gtk_builder_check_parent (data->builder, context, "patterns", error))
return;
data->parsing = TRUE;
}
else
g_warning ("Unsupported tag for GtkFileFilter: %s\n", element_name);
{
_gtk_builder_error_unhandled_tag (data->builder, context,
"GtkFileFilter", element_name,
error);
}
}
static void
parser_text_element (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
SubParserData *parser_data = (SubParserData*)user_data;
SubParserData *data = (SubParserData*)user_data;
if (parser_data->parsing)
g_string_append_len (parser_data->string, text, text_len);
if (data->parsing)
g_string_append_len (data->string, text, text_len);
}
static void
parser_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
const gchar *element_name,
gpointer user_data,
GError **error)
{
SubParserData *parser_data = (SubParserData*)user_data;
SubParserData *data = (SubParserData*)user_data;
if (parser_data->string)
if (data->string)
{
switch (parser_data->type)
{
case PARSE_MIME_TYPES:
gtk_file_filter_add_mime_type (parser_data->filter, parser_data->string->str);
break;
case PARSE_PATTERNS:
gtk_file_filter_add_pattern (parser_data->filter, parser_data->string->str);
break;
default:
break;
}
switch (data->type)
{
case PARSE_MIME_TYPES:
gtk_file_filter_add_mime_type (data->filter, data->string->str);
break;
case PARSE_PATTERNS:
gtk_file_filter_add_pattern (data->filter, data->string->str);
break;
default:
break;
}
}
g_string_set_size (parser_data->string, 0);
parser_data->parsing = FALSE;
g_string_set_size (data->string, 0);
data->parsing = FALSE;
}
static const GMarkupParser sub_parser =
@ -308,52 +328,54 @@ static const GMarkupParser sub_parser =
static gboolean
gtk_file_filter_buildable_custom_tag_start (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
GMarkupParser *parser,
gpointer *data)
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
GMarkupParser *parser,
gpointer *parser_data)
{
SubParserData *parser_data = NULL;
SubParserData *data = NULL;
if (strcmp (tagname, "mime-types") == 0)
{
parser_data = g_slice_new0 (SubParserData);
parser_data->string = g_string_new ("");
parser_data->type = PARSE_MIME_TYPES;
parser_data->filter = GTK_FILE_FILTER (buildable);
data = g_slice_new0 (SubParserData);
data->string = g_string_new ("");
data->type = PARSE_MIME_TYPES;
data->filter = GTK_FILE_FILTER (buildable);
data->builder = builder;
*parser = sub_parser;
*data = parser_data;
*parser_data = data;
}
else if (strcmp (tagname, "patterns") == 0)
{
parser_data = g_slice_new0 (SubParserData);
parser_data->string = g_string_new ("");
parser_data->type = PARSE_PATTERNS;
parser_data->filter = GTK_FILE_FILTER (buildable);
data = g_slice_new0 (SubParserData);
data->string = g_string_new ("");
data->type = PARSE_PATTERNS;
data->filter = GTK_FILE_FILTER (buildable);
data->builder = builder;
*parser = sub_parser;
*data = parser_data;
*parser_data = data;
}
return parser_data != NULL;
return data != NULL;
}
static void
gtk_file_filter_buildable_custom_tag_end (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
gpointer *data)
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
gpointer *user_data)
{
if (strcmp (tagname, "mime-types") == 0 ||
strcmp (tagname, "patterns") == 0)
{
SubParserData *parser_data = (SubParserData*)data;
SubParserData *data = (SubParserData*)user_data;
g_string_free (parser_data->string, TRUE);
g_slice_free (SubParserData, parser_data);
g_string_free (data->string, TRUE);
g_slice_free (SubParserData, data);
}
}