1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2004, Anders Carlsson <andersca@gnome.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "pixbuf-document.h"
21 #include "ev-document-thumbnails.h"
23 #include <libgnomevfs/gnome-vfs-uri.h>
24 #include <libgnomevfs/gnome-vfs-utils.h>
25 #include <libgnomevfs/gnome-vfs-ops.h>
26 #include <libgnomevfs/gnome-vfs-xfer.h>
28 struct _PixbufDocumentClass
30 GObjectClass parent_class;
33 struct _PixbufDocument
35 GObject parent_instance;
42 typedef struct _PixbufDocumentClass PixbufDocumentClass;
44 static void pixbuf_document_document_iface_init (EvDocumentIface *iface);
45 static void pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
47 G_DEFINE_TYPE_WITH_CODE (PixbufDocument, pixbuf_document, G_TYPE_OBJECT,
48 { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
49 pixbuf_document_document_iface_init);
50 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
51 pixbuf_document_document_thumbnails_iface_init)
55 pixbuf_document_load (EvDocument *document,
59 PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
64 /* FIXME: We could actually load uris */
65 filename = g_filename_from_uri (uri, NULL, error);
69 pixbuf = gdk_pixbuf_new_from_file (filename, error);
74 pixbuf_document->pixbuf = pixbuf;
75 g_free (pixbuf_document->uri);
76 pixbuf_document->uri = g_strdup (uri);
82 pixbuf_document_save (EvDocument *document,
86 PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
87 GnomeVFSResult result;
88 GnomeVFSURI *source_uri;
89 GnomeVFSURI *target_uri;
91 if (!pixbuf_document->uri)
94 source_uri = gnome_vfs_uri_new (pixbuf_document->uri);
95 target_uri = gnome_vfs_uri_new (uri);
97 result = gnome_vfs_xfer_uri (source_uri, target_uri,
98 GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
99 GNOME_VFS_XFER_ERROR_MODE_ABORT,
100 GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
103 gnome_vfs_uri_unref (target_uri);
104 gnome_vfs_uri_unref (source_uri);
106 if (result != GNOME_VFS_OK)
110 gnome_vfs_result_to_string (result));
111 return (result == GNOME_VFS_OK);
115 pixbuf_document_get_n_pages (EvDocument *document)
121 pixbuf_document_get_page_size (EvDocument *document,
126 PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
128 *width = gdk_pixbuf_get_width (pixbuf_document->pixbuf);
129 *height = gdk_pixbuf_get_height (pixbuf_document->pixbuf);
133 pixbuf_document_render_pixbuf (EvDocument *document,
136 PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
137 GdkPixbuf *scaled_pixbuf, *rotated_pixbuf;
139 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf,
140 gdk_pixbuf_get_width (pixbuf_document->pixbuf) * rc->scale,
141 gdk_pixbuf_get_height (pixbuf_document->pixbuf) * rc->scale,
142 GDK_INTERP_BILINEAR);
144 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
145 g_object_unref (scaled_pixbuf);
147 return rotated_pixbuf;
151 pixbuf_document_finalize (GObject *object)
153 PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (object);
155 g_object_unref (pixbuf_document->pixbuf);
156 g_free (pixbuf_document->uri);
158 G_OBJECT_CLASS (pixbuf_document_parent_class)->finalize (object);
162 pixbuf_document_class_init (PixbufDocumentClass *klass)
164 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
166 gobject_class->finalize = pixbuf_document_finalize;
170 pixbuf_document_can_get_text (EvDocument *document)
175 static EvDocumentInfo *
176 pixbuf_document_get_info (EvDocument *document)
178 EvDocumentInfo *info;
180 info = g_new0 (EvDocumentInfo, 1);
181 info->fields_mask = 0;
187 pixbuf_document_document_iface_init (EvDocumentIface *iface)
189 iface->load = pixbuf_document_load;
190 iface->save = pixbuf_document_save;
191 iface->can_get_text = pixbuf_document_can_get_text;
192 iface->get_n_pages = pixbuf_document_get_n_pages;
193 iface->get_page_size = pixbuf_document_get_page_size;
194 iface->render_pixbuf = pixbuf_document_render_pixbuf;
195 iface->get_info = pixbuf_document_get_info;
199 pixbuf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
205 PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
206 GdkPixbuf *pixbuf, *rotated_pixbuf;
207 gdouble scale_factor;
210 scale_factor = (gdouble)size / gdk_pixbuf_get_width (pixbuf_document->pixbuf);
212 height = gdk_pixbuf_get_height (pixbuf_document->pixbuf) * scale_factor;
214 pixbuf = gdk_pixbuf_scale_simple (pixbuf_document->pixbuf, size, height,
215 GDK_INTERP_BILINEAR);
217 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rotation);
218 g_object_unref (pixbuf);
220 return rotated_pixbuf;
224 pixbuf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
226 gint suggested_width,
230 PixbufDocument *pixbuf_document = PIXBUF_DOCUMENT (document);
233 page_ratio = ((double)gdk_pixbuf_get_height (pixbuf_document->pixbuf)) /
234 gdk_pixbuf_get_width (pixbuf_document->pixbuf);
235 *width = suggested_width;
236 *height = (gint) (suggested_width * page_ratio);
240 pixbuf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
242 iface->get_thumbnail = pixbuf_document_thumbnails_get_thumbnail;
243 iface->get_dimensions = pixbuf_document_thumbnails_get_dimensions;
248 pixbuf_document_init (PixbufDocument *pixbuf_document)