]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
[build] Rename DATADIR with EVINCEDATADIR
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #ifdef G_OS_WIN32
29 #include <io.h>
30 #include <conio.h>
31 #if !(_WIN32_WINNT >= 0x0500)
32 #error "_WIN32_WINNT must be defined >= 0x0500"
33 #endif
34 #include <windows.h>
35 #endif
36
37 #define THUMBNAIL_SIZE 128
38
39 static gint size = THUMBNAIL_SIZE;
40 static const gchar **file_arguments;
41
42 static const GOptionEntry goption_options[] = {
43         { "size", 's', 0, G_OPTION_ARG_INT, &size, NULL, "SIZE" },
44         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, "<input> <ouput>" },
45         { NULL }
46 };
47
48 struct AsyncData {
49         EvDocument  *document;
50         const gchar *output;
51         gint         size;
52         gboolean     success;
53 };
54
55 static void
56 delete_temp_file (GFile *file)
57 {
58         ev_tmp_file_unlink (file);
59         g_object_unref (file);
60 }
61
62 static EvDocument *
63 evince_thumbnailer_get_document (GFile *file)
64 {
65         EvDocument *document = NULL;
66         gchar      *uri;
67         GFile      *tmp_file = NULL;
68         GError     *error = NULL;
69
70         if (!g_file_is_native (file)) {
71                 gchar *base_name, *template;
72
73                 base_name = g_file_get_basename (file);
74                 template = g_strdup_printf ("document.XXXXXX-%s", base_name);
75                 g_free (base_name);
76
77                 tmp_file = ev_mkstemp_file (template, &error);
78                 g_free (template);
79                 if (!tmp_file) {
80                         g_printerr ("Error loading remote document: %s\n", error->message);
81                         g_error_free (error);
82
83                         return NULL;
84                 }
85
86                 g_file_copy (file, tmp_file, G_FILE_COPY_OVERWRITE,
87                              NULL, NULL, NULL, &error);
88                 if (error) {
89                         g_printerr ("Error loading remote document: %s\n", error->message);
90                         g_error_free (error);
91                         g_object_unref (tmp_file);
92
93                         return NULL;
94                 }
95                 uri = g_file_get_uri (tmp_file);
96         } else {
97                 uri = g_file_get_uri (file);
98         }
99
100         document = ev_document_factory_get_document (uri, &error);
101         if (tmp_file) {
102                 if (document) {
103                         g_object_weak_ref (G_OBJECT (document),
104                                            (GWeakNotify)delete_temp_file,
105                                            tmp_file);
106                 } else {
107                         ev_tmp_file_unlink (tmp_file);
108                         g_object_unref (tmp_file);
109                 }
110         }
111         g_free (uri);
112         if (error) {
113                 if (error->domain == EV_DOCUMENT_ERROR &&
114                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
115                         /* FIXME: Create a thumb for cryp docs */
116                         g_error_free (error);
117                         return NULL;
118                 }
119                 g_printerr ("Error loading document: %s\n", error->message);
120                 g_error_free (error);
121                 return NULL;
122         }
123
124         return document;
125 }
126
127 static gboolean
128 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
129 {
130         EvRenderContext *rc;
131         double width, height;
132         GdkPixbuf *pixbuf;
133         EvPage *page;
134
135         page = ev_document_get_page (document, 0);
136         
137         ev_document_get_page_size (document, 0, &width, &height);
138
139         rc = ev_render_context_new (page, 0, size / width);
140         pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (document),
141                                                        rc, FALSE);
142         g_object_unref (rc);
143         g_object_unref (page);
144         
145         if (pixbuf != NULL) {
146                 const char *overlaid_icon_name = NULL;
147
148                 if (overlaid_icon_name) {
149                         GdkPixbuf *overlaid_pixbuf;
150
151 #ifdef G_OS_WIN32
152                         gchar *dir = g_win32_get_package_installation_directory_of_module (NULL);
153                         gchar *overlaid_icon_path = g_build_filename (dir, "share", "evince", overlaid_icon_name, NULL);
154                         g_free (dir);
155 #else
156                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", EVINCEDATADIR, overlaid_icon_name);
157 #endif
158                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
159                         g_free (overlaid_icon_path);
160                         if (overlaid_pixbuf != NULL) {
161                                 int delta_height, delta_width;
162                                 
163                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
164                                         gdk_pixbuf_get_width (overlaid_pixbuf);
165                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
166                                         gdk_pixbuf_get_height (overlaid_pixbuf);
167                                 
168                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
169                                                       delta_width, delta_height,
170                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
171                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
172                                                       delta_width, delta_height,
173                                                       1, 1,
174                                                       GDK_INTERP_NEAREST, 100);
175                                 
176                                 g_object_unref  (overlaid_pixbuf);
177                         }
178                 }
179                 
180                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
181                         g_object_unref  (pixbuf);
182                         return TRUE;
183                 }
184
185                 g_object_unref  (pixbuf);
186         }
187         
188         return FALSE;
189 }
190
191 static gpointer
192 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
193 {
194         ev_document_doc_mutex_lock ();
195         data->success = evince_thumbnail_pngenc_get (data->document,
196                                                      data->output,
197                                                      data->size);
198         ev_document_doc_mutex_unlock ();
199         
200         g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
201         
202         return NULL;
203 }
204
205 static void
206 print_usage (GOptionContext *context)
207 {
208         gchar *help;
209
210         help = g_option_context_get_help (context, TRUE, NULL);
211         g_print ("%s", help);
212         g_free (help);
213 }
214
215 int
216 main (int argc, char *argv[])
217 {
218         EvDocument     *document;
219         GOptionContext *context;
220         const char     *input;
221         const char     *output;
222         GFile          *file;
223         GError         *error = NULL;
224
225         context = g_option_context_new ("- GNOME Document Thumbnailer");
226         g_option_context_add_main_entries (context, goption_options, NULL);
227
228         if (!g_option_context_parse (context, &argc, &argv, &error)) {
229                 g_printerr ("%s\n", error->message);
230                 g_error_free (error);
231                 print_usage (context);
232                 g_option_context_free (context);
233
234                 return -1;
235         }
236
237         input = file_arguments ? file_arguments[0] : NULL;
238         output = input ? file_arguments[1] : NULL;
239         if (!input || !output) {
240                 print_usage (context);
241                 g_option_context_free (context);
242
243                 return -1;
244         }
245         
246         g_option_context_free (context);
247
248         if (size < 1) {
249                 g_print ("Size cannot be smaller than 1 pixel\n");
250                 return -1;
251         }
252
253         input = file_arguments[0];
254         output = file_arguments[1];
255
256         g_type_init ();
257
258         if (!g_thread_supported ())
259                 g_thread_init (NULL);
260
261         if (!ev_init ())
262                 return -1;
263
264         file = g_file_new_for_commandline_arg (input);
265         document = evince_thumbnailer_get_document (file);
266         g_object_unref (file);
267
268         if (!document) {
269                 ev_shutdown ();
270                 return -2;
271         }
272
273         if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
274                 g_object_unref (document);
275                 ev_shutdown ();
276                 return -2;
277         }
278
279         if (EV_IS_ASYNC_RENDERER (document)) {
280                 struct AsyncData data;
281
282                 gtk_init (&argc, &argv);
283                 
284                 data.document = document;
285                 data.output = output;
286                 data.size = size;
287
288                 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
289                                  &data, FALSE, NULL);
290                 
291                 gtk_main ();
292
293                 g_object_unref (document);
294                 ev_shutdown ();
295
296                 return data.success ? 0 : -2;
297         }
298
299         if (!evince_thumbnail_pngenc_get (document, output, size)) {
300                 g_object_unref (document);
301                 ev_shutdown ();
302                 return -2;
303         }
304
305         g_object_unref (document);
306         ev_shutdown ();
307
308         return 0;
309 }