gtk-demo: Don't use mad mime type guessing

... for displaying resources. Instead use the proven and way more
reliable method of trial and error.

It's less code and more portable for a start.
But most of all it displays PNM files as text if you fail to compile
the gdk-pixbuf loader for it.
This commit is contained in:
Benjamin Otte 2014-08-26 04:18:52 +02:00
parent de061a83a4
commit 4a6ecf33fc

View File

@ -516,9 +516,8 @@ static GtkWidget *create_text (GtkWidget **text_view, gboolean is_source);
static void static void
add_data_tab (const gchar *demoname) add_data_tab (const gchar *demoname)
{ {
gchar *resource_dir, *resource_name, *content_type, *content_mime; gchar *resource_dir, *resource_name;
gchar **resources; gchar **resources;
GBytes *bytes;
GtkWidget *widget, *label; GtkWidget *widget, *label;
guint i; guint i;
@ -533,28 +532,25 @@ add_data_tab (const gchar *demoname)
for (i = 0; resources[i]; i++) for (i = 0; resources[i]; i++)
{ {
resource_name = g_strconcat (resource_dir, "/", resources[i], NULL); resource_name = g_strconcat (resource_dir, "/", resources[i], NULL);
widget = gtk_image_new_from_resource (resource_name);
if (gtk_image_get_pixbuf (GTK_IMAGE (widget)) == NULL &&
gtk_image_get_animation (GTK_IMAGE (widget)) == NULL)
{
GBytes *bytes;
/* So we've used the best API available to figure out it's
* not an image. Let's try something else then.
*/
g_object_ref_sink (widget);
gtk_widget_destroy (widget);
bytes = g_resources_lookup_data (resource_name, 0, NULL); bytes = g_resources_lookup_data (resource_name, 0, NULL);
g_assert (bytes); g_assert (bytes);
content_type = g_content_type_guess (resource_name, if (g_utf8_validate (g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), NULL))
g_bytes_get_data (bytes, NULL),
g_bytes_get_size (bytes),
NULL);
content_mime = g_content_type_get_mime_type (content_type);
/* In theory we should look at all the mime types gdk-pixbuf supports
* and go from there, but we know what file types we've added.
*/
if (g_content_type_is_a (content_mime, "image/png") ||
g_content_type_is_a (content_mime, "image/gif") ||
g_content_type_is_a (content_mime, "image/jpeg"))
{
widget = gtk_image_new_from_resource (resource_name);
}
else if (g_content_type_is_a (content_mime, "text/plain") ||
g_content_type_is_a (content_mime, "application/x-ext-ui") ||
g_content_type_is_a (content_mime, "text/css"))
{ {
/* Looks like it parses as text. Dump it into a textview then! */
GtkTextBuffer *buffer; GtkTextBuffer *buffer;
GtkWidget *textview; GtkWidget *textview;
@ -565,11 +561,13 @@ add_data_tab (const gchar *demoname)
} }
else else
{ {
g_warning ("Don't know how to display resource '%s'\n", resource_name);
g_warning ("Don't know how to display resource '%s' of type '%s'\n", resource_name, content_mime);
widget = NULL; widget = NULL;
} }
g_bytes_unref (bytes);
}
gtk_widget_show_all (widget); gtk_widget_show_all (widget);
label = gtk_label_new (resources[i]); label = gtk_label_new (resources[i]);
gtk_widget_show (label); gtk_widget_show (label);
@ -579,10 +577,7 @@ add_data_tab (const gchar *demoname)
"tab-expand", TRUE, "tab-expand", TRUE,
NULL); NULL);
g_free (content_mime);
g_free (content_type);
g_free (resource_name); g_free (resource_name);
g_bytes_unref (bytes);
} }
g_strfreev (resources); g_strfreev (resources);