]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
*** empty log message ***
[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 #include <libgnomevfs/gnome-vfs-ops.h>
24
25 #include <ev-document.h>
26 #include <ev-document-thumbnails.h>
27 #include <ev-document-factory.h>
28
29 #include <string.h>
30
31 #define THUMBNAIL_SIZE 128
32
33 static EvDocument *
34 get_document_from_uri (const char *uri, gboolean slow, gchar **mime_type)
35 {
36         EvDocument *document = NULL;
37         GnomeVFSFileInfo *info;
38         GnomeVFSResult result;
39
40         info = gnome_vfs_file_info_new ();
41         result = gnome_vfs_get_file_info (uri, info,
42                                           GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
43                                           GNOME_VFS_FILE_INFO_FOLLOW_LINKS | 
44                                           (slow ? GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE : 0));
45         if (result != GNOME_VFS_OK || info->mime_type == NULL) {
46                 goto end;
47         } 
48         
49         document = ev_document_factory_get_document (info->mime_type);
50         if (mime_type != NULL) {
51                 *mime_type = info->mime_type ? g_strdup (info->mime_type) : NULL;
52         }
53
54 end:
55         gnome_vfs_file_info_unref (info);       
56         return document;
57 }
58
59 static gboolean
60 evince_thumbnail_pngenc_get (const char *uri, const char *thumbnail, int size)
61 {
62         EvDocument *document = NULL;
63         GError *error = NULL;
64         GdkPixbuf *pixbuf;
65         char *mime_type = NULL;
66
67         document = get_document_from_uri (uri, FALSE, &mime_type);
68         if (document == NULL) {
69                 document = get_document_from_uri (uri, TRUE, &mime_type);
70         }
71         if (document == NULL) {
72                 return FALSE;
73         }
74
75         if (!ev_document_load (document, uri, &error)) {
76                 if (error->domain == EV_DOCUMENT_ERROR &&
77                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
78                         /* FIXME: Create a thumb for cryp docs */
79                 }
80                 g_error_free (error);
81                 return FALSE;
82         }
83
84         if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
85                 return FALSE;
86         }
87
88         pixbuf = ev_document_thumbnails_get_thumbnail
89                         (EV_DOCUMENT_THUMBNAILS (document), 0, 0, size, FALSE);
90         
91         if (pixbuf != NULL) {
92                 const char *overlaid_icon_name = NULL;
93
94                 if (strcmp (mime_type, "application/pdf") == 0) {
95                         overlaid_icon_name = "pdf-icon.png";
96                 }
97
98                 if (overlaid_icon_name) {
99                         GdkPixbuf *overlaid_pixbuf;
100
101                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
102                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
103                         g_free (overlaid_icon_path);
104                         if (overlaid_pixbuf != NULL) {
105                                 int delta_height, delta_width;
106                                 
107                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
108                                         gdk_pixbuf_get_width (overlaid_pixbuf);
109                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
110                                         gdk_pixbuf_get_height (overlaid_pixbuf);
111                                 
112                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
113                                                       delta_width, delta_height,
114                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
115                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
116                                                       delta_width, delta_height,
117                                                       1, 1,
118                                                       GDK_INTERP_NEAREST, 100);
119                                 
120                                 gdk_pixbuf_unref  (overlaid_pixbuf);
121                         }
122                 }
123                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
124                         gdk_pixbuf_unref  (pixbuf);
125                         g_object_unref (document);
126                         return TRUE;
127                 } else {
128                         gdk_pixbuf_unref  (pixbuf);
129                         g_object_unref (document);
130                 }
131         }
132         return FALSE;
133 }
134
135 int
136 main (int argc, char *argv[])
137 {
138         int res;
139         char *input, *output;
140         int size;
141         char *uri;
142
143         if (argc <= 2 || argc > 5 || strcmp (argv[1], "-h") == 0 ||
144             strcmp (argv[1], "--help") == 0) {
145                 g_print ("Usage: %s [-s <size>] <input> <output>\n", argv[0]);
146                 return -1;
147         }
148
149         res = gnome_vfs_init ();
150
151         if (!strcmp (argv[1], "-s")) {
152                 input = argv[3];
153                 output = argv[4];
154                 size = g_strtod (argv[2], NULL);
155         } else {
156                 input = argv[1];
157                 output = argv[2];
158                 size = THUMBNAIL_SIZE;
159         }
160
161         if (size < 40) {
162                 g_print ("Size cannot be smaller than 40 pixels\n");
163                 return -1;
164         }
165
166         uri = gnome_vfs_make_uri_from_shell_arg (input);
167
168         if (evince_thumbnail_pngenc_get (uri, output, size)) {
169                 g_free (uri);
170                 return 0;
171         } else {
172                 g_free (uri);
173                 return -2;
174         }
175 }