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