]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
A libdocument/ev-init.[ch]: Add single init/shutdown method. Bug #569117.
[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 <config.h>
20
21 #include <evince-document.h>
22
23 #include <gio/gio.h>
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define THUMBNAIL_SIZE 128
29
30 struct AsyncData {
31         EvDocument  *document;
32         const gchar *output;
33         gint         size;
34         gboolean     success;
35 };
36
37 static EvDocument *
38 evince_thumbnailer_get_document (const gchar *uri)
39 {
40         EvDocument *document = NULL;
41         GError     *error = NULL;
42
43         document = ev_document_factory_get_document  (uri, &error);
44         if (error) {
45                 if (error->domain == EV_DOCUMENT_ERROR &&
46                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
47                         /* FIXME: Create a thumb for cryp docs */
48                         g_error_free (error);
49                         return NULL;
50                 }
51                 g_error_free (error);
52                 return NULL;
53         }
54         
55         return document;
56 }
57
58 static gboolean
59 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
60 {
61         EvRenderContext *rc;
62         double width, height;
63         GdkPixbuf *pixbuf;
64         EvPage *page;
65
66         page = ev_document_get_page (document, 0);
67         
68         ev_document_get_page_size (document, page, &width, &height);
69
70         rc = ev_render_context_new (page, 0, size / width);
71         pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (document),
72                                                        rc, FALSE);
73         g_object_unref (rc);
74         g_object_unref (page);
75         
76         if (pixbuf != NULL) {
77                 const char *overlaid_icon_name = NULL;
78
79                 if (overlaid_icon_name) {
80                         GdkPixbuf *overlaid_pixbuf;
81
82                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
83                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
84                         g_free (overlaid_icon_path);
85                         if (overlaid_pixbuf != NULL) {
86                                 int delta_height, delta_width;
87                                 
88                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
89                                         gdk_pixbuf_get_width (overlaid_pixbuf);
90                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
91                                         gdk_pixbuf_get_height (overlaid_pixbuf);
92                                 
93                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
94                                                       delta_width, delta_height,
95                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
96                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
97                                                       delta_width, delta_height,
98                                                       1, 1,
99                                                       GDK_INTERP_NEAREST, 100);
100                                 
101                                 g_object_unref  (overlaid_pixbuf);
102                         }
103                 }
104                 
105                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
106                         g_object_unref  (pixbuf);
107                         return TRUE;
108                 }
109
110                 g_object_unref  (pixbuf);
111         }
112         
113         return FALSE;
114 }
115
116 static gpointer
117 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
118 {
119         ev_document_doc_mutex_lock ();
120         data->success = evince_thumbnail_pngenc_get (data->document,
121                                                      data->output,
122                                                      data->size);
123         ev_document_doc_mutex_unlock ();
124         
125         g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
126         
127         return NULL;
128 }
129
130 int
131 main (int argc, char *argv[])
132 {
133         EvDocument *document;
134         const char *input;
135         const char *output;
136         int         size;
137         char       *uri;
138         GFile      *file;
139
140         if (argc <= 2 || argc > 5 || strcmp (argv[1], "-h") == 0 ||
141             strcmp (argv[1], "--help") == 0) {
142                 g_print ("Usage: %s [-s <size>] <input> <output>\n", argv[0]);
143                 return -1;
144         }
145
146         if (!strcmp (argv[1], "-s")) {
147                 input = argv[3];
148                 output = argv[4];
149                 size = atoi (argv[2]);
150         } else {
151                 input = argv[1];
152                 output = argv[2];
153                 size = THUMBNAIL_SIZE;
154         }
155
156         if (size < 40) {
157                 g_print ("Size cannot be smaller than 40 pixels\n");
158                 return -1;
159         }
160
161         g_type_init ();
162
163         if (!g_thread_supported ())
164                 g_thread_init (NULL);
165
166         if (!ev_init ())
167                 return -1;
168
169         file = g_file_new_for_commandline_arg (input);
170         uri = g_file_get_uri (file);
171         document = evince_thumbnailer_get_document (uri);
172
173         g_object_unref (file);
174         g_free (uri);
175
176         if (!document) {
177                 ev_shutdown ();
178                 return -2;
179         }
180
181         if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
182                 g_object_unref (document);
183                 ev_shutdown ();
184                 return -2;
185         }
186
187         if (EV_IS_ASYNC_RENDERER (document)) {
188                 struct AsyncData data;
189
190                 gtk_init (&argc, &argv);
191                 
192                 data.document = document;
193                 data.output = output;
194                 data.size = size;
195
196                 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
197                                  &data, FALSE, NULL);
198                 
199                 gtk_main ();
200
201                 g_object_unref (document);
202                 ev_shutdown ();
203
204                 return data.success ? 0 : -2;
205         }
206
207         if (!evince_thumbnail_pngenc_get (document, output, size)) {
208                 g_object_unref (document);
209                 ev_shutdown ();
210                 return -2;
211         }
212
213         g_object_unref (document);
214         ev_shutdown ();
215
216         return 0;
217 }