static void walk_resource_path (const char *path, int level, int size) { char **children = g_resources_enumerate_children (path, 0, NULL); int i; if (!children) return; for (i = 0; children[i]; i++) { const char *child = children[i]; char *subpath; GBytes *data; if (level == 0) { size = atol (child); if (size <= 0) continue; } subpath = g_build_path ("/", path, child, NULL); data = g_resources_lookup_data (subpath, 0, NULL); if (data) { GdkPixbuf *pixbuf = gdk_pixbuf_new_from_resource (subpath, NULL); if (pixbuf && size > 0 && strchr (child, '.')) { char *iconname = g_strdup (child); strchr(iconname, '.')[0] = 0; if (gnm_debug_flag ("icons")) g_printerr ("Defining icon %s at size %d\n", iconname, size); gtk_icon_theme_add_builtin_icon (iconname, size, pixbuf); g_object_unref (pixbuf); g_free (iconname); } g_bytes_unref (data); } else walk_resource_path (subpath, level + 1, size); g_free (subpath); } g_strfreev (children); }
/** * Debug dump of all resources compile din via GResource. */ static void list_gresource(void) { const char *nspath = "/org/netsurf"; char **reslist; char **cur; GError* gerror = NULL; reslist = g_resources_enumerate_children(nspath, G_RESOURCE_LOOKUP_FLAGS_NONE, &gerror); if (gerror) { LOG("gerror %s", gerror->message); g_error_free(gerror); } else { cur = reslist; while (cur != NULL && *cur != NULL) { LOG("gres %s", *cur); cur++; } g_strfreev(reslist); } }
static GFileEnumerator * _g_resource_file_enumerator_new (GResourceFile *file, const char *attributes, GFileQueryInfoFlags flags, GCancellable *cancellable, GError **error) { GResourceFileEnumerator *resource; char **children; gboolean res; children = g_resources_enumerate_children (file->path, 0, NULL); if (children == NULL && strcmp ("/", file->path) != 0) { res = g_resources_get_info (file->path, 0, NULL, NULL, NULL); if (res) g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY, _("The resource at '%s' is not a directory"), file->path); else g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, _("The resource at '%s' does not exist"), file->path); return NULL; } resource = g_object_new (G_TYPE_RESOURCE_FILE_ENUMERATOR, "container", file, NULL); resource->children = children; resource->path = g_strdup (file->path); resource->attributes = g_strdup (attributes); resource->flags = flags; return G_FILE_ENUMERATOR (resource); }
static GFileInfo * g_resource_file_query_info (GFile *file, const char *attributes, GFileQueryInfoFlags flags, GCancellable *cancellable, GError **error) { GResourceFile *resource = G_RESOURCE_FILE (file); GError *my_error = NULL; GFileInfo *info; GFileAttributeMatcher *matcher; gboolean res; gsize size; guint32 resource_flags; char **children; gboolean is_dir; char *base; is_dir = FALSE; children = g_resources_enumerate_children (resource->path, 0, NULL); if (children != NULL) { g_strfreev (children); is_dir = TRUE; } /* root is always there */ if (strcmp ("/", resource->path) == 0) is_dir = TRUE; if (!is_dir) { res = g_resources_get_info (resource->path, 0, &size, &resource_flags, &my_error); if (!res) { if (g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, _("The resource at '%s' does not exist"), resource->path); } else g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, my_error->message); g_clear_error (&my_error); return FALSE; } } matcher = g_file_attribute_matcher_new (attributes); info = g_file_info_new (); base = g_resource_file_get_basename (file); g_file_info_set_name (info, base); _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ, TRUE); _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE, FALSE); _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE, FALSE); _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME, FALSE); _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE, FALSE); _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH, FALSE); if (is_dir) { g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY); } else { GBytes *bytes; char *content_type; g_file_info_set_file_type (info, G_FILE_TYPE_REGULAR); g_file_info_set_size (info, size); if ((_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) || ((~resource_flags & G_RESOURCE_FLAGS_COMPRESSED) && _g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))) && (bytes = g_resources_lookup_data (resource->path, 0, NULL))) { const guchar *data; gsize data_size; data = g_bytes_get_data (bytes, &data_size); content_type = g_content_type_guess (base, data, data_size, NULL); g_bytes_unref (bytes); } else content_type = NULL; if (content_type) { _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE, content_type); _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type); g_free (content_type); } } g_free (base); g_file_attribute_matcher_unref (matcher); return info; }
static void add_data_tab (const gchar *demoname) { gchar *resource_dir, *resource_name, *content_type, *content_mime; gchar **resources; GBytes *bytes; GtkWidget *widget, *label; guint i; resource_dir = g_strconcat ("/", demoname, NULL); resources = g_resources_enumerate_children (resource_dir, 0, NULL); if (resources == NULL) { g_free (resource_dir); return; } for (i = 0; resources[i]; i++) { resource_name = g_strconcat (resource_dir, "/", resources[i], NULL); bytes = g_resources_lookup_data (resource_name, 0, NULL); g_assert (bytes); content_type = g_content_type_guess (resource_name, 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")) { GtkTextBuffer *buffer; GtkWidget *textview; widget = create_text (&textview, FALSE); buffer = gtk_text_buffer_new (NULL); gtk_text_buffer_set_text (buffer, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes)); gtk_text_view_set_buffer (GTK_TEXT_VIEW (textview), buffer); } else { g_warning ("Don't know how to display resource '%s' of type '%s'\n", resource_name, content_mime); widget = NULL; } gtk_widget_show_all (widget); label = gtk_label_new (resources[i]); gtk_widget_show (label); gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label); gtk_container_child_set (GTK_CONTAINER (notebook), GTK_WIDGET (widget), "tab-expand", TRUE, NULL); g_free (content_mime); g_free (content_type); g_free (resource_name); g_bytes_unref (bytes); } g_strfreev (resources); g_free (resource_dir); }
static void add_data_tab (const gchar *demoname) { gchar *resource_dir, *resource_name; gchar **resources; GtkWidget *widget, *label; guint i; resource_dir = g_strconcat ("/", demoname, NULL); resources = g_resources_enumerate_children (resource_dir, 0, NULL); if (resources == NULL) { g_free (resource_dir); return; } for (i = 0; resources[i]; i++) { 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); g_object_unref (widget); bytes = g_resources_lookup_data (resource_name, 0, NULL); g_assert (bytes); if (g_utf8_validate (g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), NULL)) { /* Looks like it parses as text. Dump it into a textview then! */ GtkTextBuffer *buffer; GtkWidget *textview; widget = create_text (&textview, FALSE); buffer = gtk_text_buffer_new (NULL); gtk_text_buffer_set_text (buffer, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes)); gtk_text_view_set_buffer (GTK_TEXT_VIEW (textview), buffer); } else { g_warning ("Don't know how to display resource '%s'\n", resource_name); widget = NULL; } g_bytes_unref (bytes); } gtk_widget_show_all (widget); label = gtk_label_new (resources[i]); gtk_widget_show (label); gtk_notebook_append_page (GTK_NOTEBOOK (notebook), widget, label); gtk_container_child_set (GTK_CONTAINER (notebook), GTK_WIDGET (widget), "tab-expand", TRUE, NULL); g_free (resource_name); } g_strfreev (resources); g_free (resource_dir); }