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