[virt-tools-list] [virt-viewer 2/3] Allow to save to other formats than png
Christophe Fergeau
cfergeau at redhat.com
Tue Nov 13 12:06:38 UTC 2012
Currently, the screenshots can only be saved to png. This commit
checks if the file extension is a known one, and will save to this
format if it is. Otherwise it will fallback to saving to png.
---
src/virt-viewer-window.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 53 insertions(+), 2 deletions(-)
diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
index 9ca2cf7..aec51ae 100644
--- a/src/virt-viewer-window.c
+++ b/src/virt-viewer-window.c
@@ -750,15 +750,66 @@ virt_viewer_window_menu_view_resize(GtkWidget *menu,
virt_viewer_display_set_auto_resize(priv->display, priv->auto_resize);
}
+static void add_if_writable (GdkPixbufFormat *data, GHashTable *formats)
+{
+ if (gdk_pixbuf_format_is_writable(data)) {
+ gchar **extensions;
+ gchar **it;
+ extensions = gdk_pixbuf_format_get_extensions(data);
+ for (it = extensions; *it != NULL; it++) {
+ g_hash_table_insert(formats, g_strdup(*it), data);
+ }
+ g_strfreev(extensions);
+ }
+}
+
+static GHashTable *init_image_formats(void)
+{
+ GHashTable *format_map;
+ GSList *formats = gdk_pixbuf_get_formats();
+
+ format_map = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+ g_slist_foreach(formats, (GFunc)add_if_writable, format_map);
+ g_slist_free (formats);
+
+ return format_map;
+}
+
+static GdkPixbufFormat *get_image_format(const char *filename)
+{
+ static GOnce image_formats_once = G_ONCE_INIT;
+ const char *ext;
+
+ g_once(&image_formats_once, (GThreadFunc)init_image_formats, NULL);
+
+ ext = strrchr(filename, '.');
+ if (ext == NULL)
+ return NULL;
+
+ ext++; //skip '.'
+
+ return g_hash_table_lookup(image_formats_once.retval, ext);
+}
+
static void
virt_viewer_window_save_screenshot(VirtViewerWindow *self,
const char *file)
{
VirtViewerWindowPrivate *priv = self->priv;
GdkPixbuf *pix = virt_viewer_display_get_pixbuf(VIRT_VIEWER_DISPLAY(priv->display));
+ GdkPixbufFormat *format = get_image_format(file);
+
+ if (format == NULL) {
+ g_debug("unknown file extension, falling back to png");
+ gdk_pixbuf_save(pix, file, "png", NULL,
+ "tEXt::Generator App", PACKAGE, NULL);
+ } else {
+ char *type = gdk_pixbuf_format_get_name(format);
+ g_debug("saving to %s", type);
+ gdk_pixbuf_save(pix, file, type, NULL, NULL);
+ g_free(type);
+ }
- gdk_pixbuf_save(pix, file, "png", NULL,
- "tEXt::Generator App", PACKAGE, NULL);
g_object_unref(pix);
}
--
1.8.0
More information about the virt-tools-list
mailing list