1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2006 Carlos Garcia Campos <carlosgc@gnome.org>
5 * Evince is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Evince is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <glib/gstdio.h>
25 #include "ev-document-misc.h"
26 #include "ev-file-helpers.h"
29 struct _EvImagePrivate {
36 #define EV_IMAGE_GET_PRIVATE(object) \
37 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_IMAGE, EvImagePrivate))
39 G_DEFINE_TYPE (EvImage, ev_image, G_TYPE_OBJECT)
42 ev_image_finalize (GObject *object)
44 EvImage *image = EV_IMAGE (object);
46 if (image->priv->pixbuf) {
47 g_object_unref (image->priv->pixbuf);
48 image->priv->pixbuf = NULL;
51 if (image->priv->tmp_uri) {
54 filename = g_filename_from_uri (image->priv->tmp_uri, NULL, NULL);
55 ev_tmp_filename_unlink (filename);
57 g_free (image->priv->tmp_uri);
58 image->priv->tmp_uri = NULL;
61 (* G_OBJECT_CLASS (ev_image_parent_class)->finalize) (object);
65 ev_image_class_init (EvImageClass *klass)
67 GObjectClass *g_object_class;
69 g_object_class = G_OBJECT_CLASS (klass);
71 g_type_class_add_private (g_object_class, sizeof (EvImagePrivate));
73 g_object_class->finalize = ev_image_finalize;
77 ev_image_init (EvImage *image)
79 image->priv = EV_IMAGE_GET_PRIVATE (image);
83 ev_image_new (gint page,
88 image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
89 image->priv->page = page;
90 image->priv->id = img_id;
96 ev_image_new_from_pixbuf (GdkPixbuf *pixbuf)
100 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
102 image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
103 image->priv->pixbuf = g_object_ref (pixbuf);
109 ev_image_get_page (EvImage *image)
111 g_return_val_if_fail (EV_IS_IMAGE (image), -1);
113 return image->priv->page;
117 ev_image_get_id (EvImage *image)
119 g_return_val_if_fail (EV_IS_IMAGE (image), -1);
121 return image->priv->id;
125 ev_image_get_pixbuf (EvImage *image)
127 g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
128 g_return_val_if_fail (GDK_IS_PIXBUF (image->priv->pixbuf), NULL);
130 return image->priv->pixbuf;
134 ev_image_save_tmp (EvImage *image,
137 GError *error = NULL;
138 gchar *filename = NULL;
141 g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
142 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
144 if (image->priv->tmp_uri)
145 return image->priv->tmp_uri;
147 if ((fd = ev_mkstemp ("image.XXXXXX.png", &filename, &error)) == -1)
150 gdk_pixbuf_save (pixbuf, filename,
152 "compression", "3", NULL);
156 image->priv->tmp_uri = g_filename_to_uri (filename, NULL, &error);
157 if (image->priv->tmp_uri == NULL)
162 return image->priv->tmp_uri;
167 /* Erro saving image */
168 g_warning ("Error saving image: %s", error->message);
169 g_error_free (error);
176 ev_image_get_tmp_uri (EvImage *image)
178 g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
180 return image->priv->tmp_uri;