]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
Change api to lookup from uri. Do fast lookup first, if the type is
[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 <libgnomevfs/gnome-vfs-mime-utils.h>
20 #include <libgnomevfs/gnome-vfs-uri.h>
21 #include <libgnomevfs/gnome-vfs-utils.h>
22 #include <libgnomevfs/gnome-vfs-init.h>
23
24 #include <ev-document.h>
25 #include <ev-document-types.h>
26 #include <ev-document-thumbnails.h>
27
28 #include <string.h>
29
30 #define THUMBNAIL_SIZE 128
31
32 static gboolean
33 evince_thumbnail_pngenc_get (const char *uri, const char *thumbnail, int size)
34 {
35         EvDocument *document = NULL;
36         GError *error = NULL;
37         GdkPixbuf *pixbuf;
38         GType document_type;
39         char *mime_type = NULL;
40
41         document_type = ev_document_type_lookup (uri, &mime_type);
42         if (document_type == G_TYPE_INVALID)
43                 return FALSE;
44
45         document = g_object_new (document_type, NULL);
46
47         if (!ev_document_load (document, uri, &error)) {
48                 if (error->domain == EV_DOCUMENT_ERROR &&
49                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
50                         /* FIXME: Create a thumb for cryp docs */
51                 }
52                 g_error_free (error);
53                 return FALSE;
54         }
55
56         if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
57                 return FALSE;
58         }
59
60         pixbuf = ev_document_thumbnails_get_thumbnail
61                         (EV_DOCUMENT_THUMBNAILS (document), 0, size, FALSE);
62         
63         if (pixbuf != NULL) {
64                 const char *overlaid_icon_name = NULL;
65
66                 if (strcmp (mime_type, "application/pdf") == 0) {
67                         overlaid_icon_name = "pdf-icon.png";
68                 }
69
70                 if (overlaid_icon_name) {
71                         GdkPixbuf *overlaid_pixbuf;
72
73                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
74                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
75                         g_free (overlaid_icon_path);
76                         if (overlaid_pixbuf != NULL) {
77                                 int delta_height, delta_width;
78                                 
79                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
80                                         gdk_pixbuf_get_width (overlaid_pixbuf);
81                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
82                                         gdk_pixbuf_get_height (overlaid_pixbuf);
83                                 
84                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
85                                                       delta_width, delta_height,
86                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
87                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
88                                                       delta_width, delta_height,
89                                                       1, 1,
90                                                       GDK_INTERP_NEAREST, 100);
91                                 
92                                 gdk_pixbuf_unref  (overlaid_pixbuf);
93                         }
94                 }
95                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
96                         gdk_pixbuf_unref  (pixbuf);
97                         g_object_unref (document);
98                         return TRUE;
99                 } else {
100                         gdk_pixbuf_unref  (pixbuf);
101                         g_object_unref (document);
102                 }
103         }
104         return FALSE;
105 }
106
107 int
108 main (int argc, char *argv[])
109 {
110         int res;
111         char *input, *output;
112         int size;
113         char *uri;
114
115         if (argc <= 2 || argc > 5 || strcmp (argv[1], "-h") == 0 ||
116             strcmp (argv[1], "--help") == 0) {
117                 g_print ("Usage: %s [-s <size>] <input> <output>\n", argv[0]);
118                 return -1;
119         }
120
121         res = gnome_vfs_init ();
122
123         if (!strcmp (argv[1], "-s")) {
124                 input = argv[3];
125                 output = argv[4];
126                 size = g_strtod (argv[2], NULL);
127         } else {
128                 input = argv[1];
129                 output = argv[2];
130                 size = THUMBNAIL_SIZE;
131         }
132
133         if (size < 40) {
134                 g_print ("Size cannot be smaller than 40 pixels\n");
135                 return -1;
136         }
137
138         uri = gnome_vfs_make_uri_from_shell_arg (input);
139
140         if (evince_thumbnail_pngenc_get (uri, output, size)) {
141                 g_free (uri);
142                 return 0;
143         } else {
144                 g_free (uri);
145                 return -2;
146         }
147 }