summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChristophe Fergeau <cfergeau@redhat.com>2012-11-13 12:50:10 +0100
committerChristophe Fergeau <cfergeau@redhat.com>2012-11-15 10:21:03 +0100
commitf9935c832b84a31bc91f5fcdfdf459ee3a0913fe (patch)
tree9097de2e4d10a0d212b2936fba96717c374cbd8c /src
parent45a791082ed07b4dfaa347ed434f29324dbd6815 (diff)
downloadvirt-viewer-f9935c832b84a31bc91f5fcdfdf459ee3a0913fe.tar.gz
virt-viewer-f9935c832b84a31bc91f5fcdfdf459ee3a0913fe.tar.xz
virt-viewer-f9935c832b84a31bc91f5fcdfdf459ee3a0913fe.zip
Allow to save to other formats than png
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.
Diffstat (limited to 'src')
-rw-r--r--src/virt-viewer-window.c55
1 files changed, 53 insertions, 2 deletions
diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
index 9ca2cf7..5e263f1 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);
}