]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
Add a nautilus thumbnailer. Based on patch by Fernando Herrera
[evince.git] / thumbnailer / evince-thumbnailer.c
1 #include <pdf-document.h>
2
3 #include <libgnomevfs/gnome-vfs-mime-utils.h>
4 #include <libgnomevfs/gnome-vfs-uri.h>
5 #include <libgnomevfs/gnome-vfs-utils.h>
6 #include <libgnomevfs/gnome-vfs-init.h>
7
8 #include <ev-document.h>
9 #include <ev-document-thumbnails.h>
10
11 #include <string.h>
12
13 static gboolean
14 evince_thumbnail_pngenc_get (const char *uri, const char *thumbnail)
15 {
16         EvDocument *document = NULL;
17         char *mime_type;
18         GError *error;
19         GdkPixbuf *pixbuf;
20
21         mime_type = gnome_vfs_get_mime_type (uri);
22         if (mime_type == NULL)
23                 return FALSE;
24
25         if (!strcmp (mime_type, "application/pdf"))
26                 document = g_object_new (PDF_TYPE_DOCUMENT, NULL);
27         else
28                 return FALSE;
29
30         if (!ev_document_load (document, uri, &error)) {
31                 if (error->domain == EV_DOCUMENT_ERROR &&
32                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
33                         /* FIXME: Create a thumb for cryp docs */
34                 }
35                 g_error_free (error);
36                 return FALSE;
37         }
38
39         pixbuf = ev_document_thumbnails_get_thumbnail
40                         (EV_DOCUMENT_THUMBNAILS (document), 1, 100);
41         
42         if (pixbuf != NULL) {
43                 GdkPixbuf *pdflogo;
44
45                 pdflogo = gdk_pixbuf_new_from_file (DATADIR"/pdf-icon.png", NULL);
46                 if (pdflogo != NULL) {
47                         int delta_height, delta_width;
48
49                         delta_width = gdk_pixbuf_get_width (pixbuf) -
50                                       gdk_pixbuf_get_width (pdflogo);
51                         delta_height = gdk_pixbuf_get_height (pixbuf) -
52                                        gdk_pixbuf_get_height (pdflogo);
53
54                         gdk_pixbuf_composite (pdflogo, pixbuf,
55                                               delta_width, delta_height,
56                                               gdk_pixbuf_get_width (pdflogo),
57                                               gdk_pixbuf_get_height (pdflogo),
58                                               delta_width, delta_height,
59                                               1, 1,
60                                               GDK_INTERP_NEAREST, 100);
61
62                         gdk_pixbuf_unref  (pdflogo);
63                 }
64                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
65                         gdk_pixbuf_unref  (pixbuf);
66                         g_object_unref (document);
67                         return TRUE;
68                 } else {
69                         gdk_pixbuf_unref  (pixbuf);
70                         g_object_unref (document);
71                 }
72         }
73         return FALSE;
74 }
75
76 int
77 main (int argc, char *argv[])
78 {
79         int res;
80         char *uri;
81
82         if (argc != 3) {
83                 g_print ("%s: thumbnailer for Nautilus\n", argv[0]);
84                 g_print ("usage: %s <input-filename> <output-filename>\n", argv[0]);
85                 return -1;
86         }
87
88         res = gnome_vfs_init ();
89
90         uri = gnome_vfs_make_uri_from_shell_arg (argv[1]);
91
92         if (evince_thumbnail_pngenc_get (uri, argv[2])) {
93                 g_free (uri);
94                 return 0;
95         } else {
96                 g_free (uri);
97                 return -2;
98         }
99 }