]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
[dualscreen] fix crash on ctrl+w and fix control window closing
[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_get_thumbnail (document, rc);
141         g_object_unref (rc);
142         g_object_unref (page);
143         
144         if (pixbuf != NULL) {
145                 const char *overlaid_icon_name = NULL;
146
147                 if (overlaid_icon_name) {
148                         GdkPixbuf *overlaid_pixbuf;
149
150 #ifdef G_OS_WIN32
151                         gchar *dir = g_win32_get_package_installation_directory_of_module (NULL);
152                         gchar *overlaid_icon_path = g_build_filename (dir, "share", "evince", overlaid_icon_name, NULL);
153                         g_free (dir);
154 #else
155                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", EVINCEDATADIR, overlaid_icon_name);
156 #endif
157                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
158                         g_free (overlaid_icon_path);
159                         if (overlaid_pixbuf != NULL) {
160                                 int delta_height, delta_width;
161                                 
162                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
163                                         gdk_pixbuf_get_width (overlaid_pixbuf);
164                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
165                                         gdk_pixbuf_get_height (overlaid_pixbuf);
166                                 
167                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
168                                                       delta_width, delta_height,
169                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
170                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
171                                                       delta_width, delta_height,
172                                                       1, 1,
173                                                       GDK_INTERP_NEAREST, 100);
174                                 
175                                 g_object_unref  (overlaid_pixbuf);
176                         }
177                 }
178                 
179                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
180                         g_object_unref  (pixbuf);
181                         return TRUE;
182                 }
183
184                 g_object_unref  (pixbuf);
185         }
186         
187         return FALSE;
188 }
189
190 static gpointer
191 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
192 {
193         ev_document_doc_mutex_lock ();
194         data->success = evince_thumbnail_pngenc_get (data->document,
195                                                      data->output,
196                                                      data->size);
197         ev_document_doc_mutex_unlock ();
198         
199         g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
200         
201         return NULL;
202 }
203
204 static void
205 print_usage (GOptionContext *context)
206 {
207         gchar *help;
208
209         help = g_option_context_get_help (context, TRUE, NULL);
210         g_print ("%s", help);
211         g_free (help);
212 }
213
214 int
215 main (int argc, char *argv[])
216 {
217         EvDocument     *document;
218         GOptionContext *context;
219         const char     *input;
220         const char     *output;
221         GFile          *file;
222         GError         *error = NULL;
223
224         context = g_option_context_new ("- GNOME Document Thumbnailer");
225         g_option_context_add_main_entries (context, goption_options, NULL);
226
227         if (!g_option_context_parse (context, &argc, &argv, &error)) {
228                 g_printerr ("%s\n", error->message);
229                 g_error_free (error);
230                 print_usage (context);
231                 g_option_context_free (context);
232
233                 return -1;
234         }
235
236         input = file_arguments ? file_arguments[0] : NULL;
237         output = input ? file_arguments[1] : NULL;
238         if (!input || !output) {
239                 print_usage (context);
240                 g_option_context_free (context);
241
242                 return -1;
243         }
244         
245         g_option_context_free (context);
246
247         if (size < 1) {
248                 g_print ("Size cannot be smaller than 1 pixel\n");
249                 return -1;
250         }
251
252         input = file_arguments[0];
253         output = file_arguments[1];
254
255         g_type_init ();
256
257         if (!g_thread_supported ())
258                 g_thread_init (NULL);
259
260         if (!ev_init ())
261                 return -1;
262
263         file = g_file_new_for_commandline_arg (input);
264         document = evince_thumbnailer_get_document (file);
265         g_object_unref (file);
266
267         if (!document) {
268                 ev_shutdown ();
269                 return -2;
270         }
271
272         if (EV_IS_ASYNC_RENDERER (document)) {
273                 struct AsyncData data;
274
275                 gtk_init (&argc, &argv);
276                 
277                 data.document = document;
278                 data.output = output;
279                 data.size = size;
280
281                 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
282                                  &data, FALSE, NULL);
283                 
284                 gtk_main ();
285
286                 g_object_unref (document);
287                 ev_shutdown ();
288
289                 return data.success ? 0 : -2;
290         }
291
292         if (!evince_thumbnail_pngenc_get (document, output, size)) {
293                 g_object_unref (document);
294                 ev_shutdown ();
295                 return -2;
296         }
297
298         g_object_unref (document);
299         ev_shutdown ();
300
301         return 0;
302 }