]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
Rework API a bit. Add a border flag and change sizing logic (now the size
[evince.git] / thumbnailer / evince-thumbnailer.c
1 /*
2    Copyright (C) 2005 Fernando Herrera <fherrera@onirica.com>
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include <pdf-document.h>
20
21 #include <libgnomevfs/gnome-vfs-mime-utils.h>
22 #include <libgnomevfs/gnome-vfs-uri.h>
23 #include <libgnomevfs/gnome-vfs-utils.h>
24 #include <libgnomevfs/gnome-vfs-init.h>
25
26 #include <ev-document.h>
27 #include <ev-document-thumbnails.h>
28
29 #include <string.h>
30
31 #define THUMBNAIL_SIZE 128
32
33 static gboolean
34 evince_thumbnail_pngenc_get (const char *uri, const char *thumbnail)
35 {
36         EvDocument *document = NULL;
37         char *mime_type;
38         GError *error;
39         GdkPixbuf *pixbuf;
40
41         mime_type = gnome_vfs_get_mime_type (uri);
42         if (mime_type == NULL)
43                 return FALSE;
44
45         if (!strcmp (mime_type, "application/pdf"))
46                 document = g_object_new (PDF_TYPE_DOCUMENT, NULL);
47         else
48                 return FALSE;
49
50         if (!ev_document_load (document, uri, &error)) {
51                 if (error->domain == EV_DOCUMENT_ERROR &&
52                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
53                         /* FIXME: Create a thumb for cryp docs */
54                 }
55                 g_error_free (error);
56                 return FALSE;
57         }
58
59         pixbuf = ev_document_thumbnails_get_thumbnail
60                         (EV_DOCUMENT_THUMBNAILS (document), 0, THUMBNAIL_SIZE, FALSE);
61         
62         if (pixbuf != NULL) {
63                 GdkPixbuf *pdflogo;
64
65                 pdflogo = gdk_pixbuf_new_from_file (DATADIR"/pdf-icon.png", NULL);
66                 if (pdflogo != NULL) {
67                         int delta_height, delta_width;
68
69                         delta_width = gdk_pixbuf_get_width (pixbuf) -
70                                       gdk_pixbuf_get_width (pdflogo);
71                         delta_height = gdk_pixbuf_get_height (pixbuf) -
72                                        gdk_pixbuf_get_height (pdflogo);
73
74                         gdk_pixbuf_composite (pdflogo, pixbuf,
75                                               delta_width, delta_height,
76                                               gdk_pixbuf_get_width (pdflogo),
77                                               gdk_pixbuf_get_height (pdflogo),
78                                               delta_width, delta_height,
79                                               1, 1,
80                                               GDK_INTERP_NEAREST, 100);
81
82                         gdk_pixbuf_unref  (pdflogo);
83                 }
84                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
85                         gdk_pixbuf_unref  (pixbuf);
86                         g_object_unref (document);
87                         return TRUE;
88                 } else {
89                         gdk_pixbuf_unref  (pixbuf);
90                         g_object_unref (document);
91                 }
92         }
93         return FALSE;
94 }
95
96 int
97 main (int argc, char *argv[])
98 {
99         int res;
100         char *uri;
101
102         if (argc != 3) {
103                 g_print ("%s: thumbnailer for Nautilus\n", argv[0]);
104                 g_print ("usage: %s <input-filename> <output-filename>\n", argv[0]);
105                 return -1;
106         }
107
108         res = gnome_vfs_init ();
109
110         uri = gnome_vfs_make_uri_from_shell_arg (argv[1]);
111
112         if (evince_thumbnail_pngenc_get (uri, argv[2])) {
113                 g_free (uri);
114                 return 0;
115         } else {
116                 g_free (uri);
117                 return -2;
118         }
119 }