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