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