]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-image.c
[dualscreen] fix crash on ctrl+w and fix control window closing
[evince.git] / libdocument / ev-image.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2006 Carlos Garcia Campos <carlosgc@gnome.org>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <config.h>
21
22 #include <glib/gstdio.h>
23 #include <unistd.h>
24
25 #include "ev-document-misc.h"
26 #include "ev-file-helpers.h"
27 #include "ev-image.h"
28
29 struct _EvImagePrivate {
30         gint       page;
31         gint       id;
32         GdkPixbuf *pixbuf;
33         gchar     *tmp_uri;
34 };
35
36 #define EV_IMAGE_GET_PRIVATE(object) \
37                 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_IMAGE, EvImagePrivate))
38
39 G_DEFINE_TYPE (EvImage, ev_image, G_TYPE_OBJECT)
40
41 static void
42 ev_image_finalize (GObject *object)
43 {
44         EvImage *image = EV_IMAGE (object);
45
46         if (image->priv->pixbuf) {
47                 g_object_unref (image->priv->pixbuf);
48                 image->priv->pixbuf = NULL;
49         }
50
51         if (image->priv->tmp_uri) {
52                 gchar *filename;
53
54                 filename = g_filename_from_uri (image->priv->tmp_uri, NULL, NULL);
55                 ev_tmp_filename_unlink (filename);
56                 g_free (filename);
57                 g_free (image->priv->tmp_uri);
58                 image->priv->tmp_uri = NULL;
59         }
60
61         (* G_OBJECT_CLASS (ev_image_parent_class)->finalize) (object);
62 }
63
64 static void
65 ev_image_class_init (EvImageClass *klass)
66 {
67         GObjectClass *g_object_class;
68
69         g_object_class = G_OBJECT_CLASS (klass);
70
71         g_type_class_add_private (g_object_class, sizeof (EvImagePrivate));
72
73         g_object_class->finalize = ev_image_finalize;
74 }
75
76 static void
77 ev_image_init (EvImage *image)
78 {
79         image->priv = EV_IMAGE_GET_PRIVATE (image);
80 }
81
82 EvImage *
83 ev_image_new (gint page,
84               gint img_id)
85 {
86         EvImage *image;
87
88         image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
89         image->priv->page = page;
90         image->priv->id = img_id;
91
92         return image;
93 }
94
95 EvImage *
96 ev_image_new_from_pixbuf (GdkPixbuf *pixbuf)
97 {
98         EvImage *image;
99
100         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
101
102         image = EV_IMAGE (g_object_new (EV_TYPE_IMAGE, NULL));
103         image->priv->pixbuf = g_object_ref (pixbuf);
104
105         return image;
106 }
107
108 gint
109 ev_image_get_page (EvImage *image)
110 {
111         g_return_val_if_fail (EV_IS_IMAGE (image), -1);
112
113         return image->priv->page;
114 }
115
116 gint
117 ev_image_get_id (EvImage *image)
118 {
119         g_return_val_if_fail (EV_IS_IMAGE (image), -1);
120
121         return image->priv->id;
122 }
123
124 GdkPixbuf *
125 ev_image_get_pixbuf (EvImage *image)
126 {
127         g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
128         g_return_val_if_fail (GDK_IS_PIXBUF (image->priv->pixbuf), NULL);
129
130         return image->priv->pixbuf;
131 }
132
133 const gchar *
134 ev_image_save_tmp (EvImage   *image,
135                    GdkPixbuf *pixbuf)
136 {
137         GError *error = NULL;
138         gchar  *filename = NULL;
139         int fd;
140         
141         g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
142         g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
143
144         if (image->priv->tmp_uri)
145                 return image->priv->tmp_uri;
146
147         if ((fd = ev_mkstemp ("image.XXXXXX.png", &filename, &error)) == -1)
148                 goto had_error;
149
150         gdk_pixbuf_save (pixbuf, filename,
151                          "png", &error,
152                          "compression", "3", NULL);
153         close (fd);
154
155         if (!error) {
156                 image->priv->tmp_uri = g_filename_to_uri (filename, NULL, &error);
157                 if (image->priv->tmp_uri == NULL)
158                         goto had_error;
159
160                 g_free (filename);
161                 
162                 return image->priv->tmp_uri;
163         }
164
165     had_error:
166
167         /* Erro saving image */
168         g_warning ("Error saving image: %s", error->message);
169         g_error_free (error);
170         g_free (filename);
171
172         return NULL;
173 }
174
175 const gchar *
176 ev_image_get_tmp_uri (EvImage *image)
177 {
178         g_return_val_if_fail (EV_IS_IMAGE (image), NULL);
179
180         return image->priv->tmp_uri;
181 }