]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
On Windows, when called from a console, get console output (works only
[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 EvDocument *
57 evince_thumbnailer_get_document (const gchar *uri)
58 {
59         EvDocument *document = NULL;
60         GError     *error = NULL;
61
62         document = ev_document_factory_get_document  (uri, &error);
63         if (error) {
64                 if (error->domain == EV_DOCUMENT_ERROR &&
65                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
66                         /* FIXME: Create a thumb for cryp docs */
67                         g_error_free (error);
68                         return NULL;
69                 }
70                 g_error_free (error);
71                 return NULL;
72         }
73         
74         return document;
75 }
76
77 static gboolean
78 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
79 {
80         EvRenderContext *rc;
81         double width, height;
82         GdkPixbuf *pixbuf;
83         EvPage *page;
84
85         page = ev_document_get_page (document, 0);
86         
87         ev_document_get_page_size (document, page, &width, &height);
88
89         rc = ev_render_context_new (page, 0, size / width);
90         pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (document),
91                                                        rc, FALSE);
92         g_object_unref (rc);
93         g_object_unref (page);
94         
95         if (pixbuf != NULL) {
96                 const char *overlaid_icon_name = NULL;
97
98                 if (overlaid_icon_name) {
99                         GdkPixbuf *overlaid_pixbuf;
100
101 #ifdef G_OS_WIN32
102                         gchar *dir = g_win32_get_package_installation_directory_of_module (NULL);
103                         gchar *overlaid_icon_path = g_build_filename (dir, "share", "evince", overlaid_icon_name, NULL);
104                         g_free (dir);
105 #else
106                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
107 #endif
108                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
109                         g_free (overlaid_icon_path);
110                         if (overlaid_pixbuf != NULL) {
111                                 int delta_height, delta_width;
112                                 
113                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
114                                         gdk_pixbuf_get_width (overlaid_pixbuf);
115                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
116                                         gdk_pixbuf_get_height (overlaid_pixbuf);
117                                 
118                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
119                                                       delta_width, delta_height,
120                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
121                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
122                                                       delta_width, delta_height,
123                                                       1, 1,
124                                                       GDK_INTERP_NEAREST, 100);
125                                 
126                                 g_object_unref  (overlaid_pixbuf);
127                         }
128                 }
129                 
130                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
131                         g_object_unref  (pixbuf);
132                         return TRUE;
133                 }
134
135                 g_object_unref  (pixbuf);
136         }
137         
138         return FALSE;
139 }
140
141 static gpointer
142 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
143 {
144         ev_document_doc_mutex_lock ();
145         data->success = evince_thumbnail_pngenc_get (data->document,
146                                                      data->output,
147                                                      data->size);
148         ev_document_doc_mutex_unlock ();
149         
150         g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
151         
152         return NULL;
153 }
154
155 static void
156 print_usage (GOptionContext *context)
157 {
158         gchar *help;
159
160         help = g_option_context_get_help (context, TRUE, NULL);
161         g_print ("%s", help);
162         g_free (help);
163 }
164
165 int
166 main (int argc, char *argv[])
167 {
168         EvDocument     *document;
169         GOptionContext *context;
170         const char     *input;
171         const char     *output;
172         char           *uri;
173         GFile          *file;
174         GError         *error = NULL;
175
176         context = g_option_context_new ("- GNOME Document Thumbnailer");
177         g_option_context_add_main_entries (context, goption_options, NULL);
178
179         if (!g_option_context_parse (context, &argc, &argv, &error)) {
180                 g_printerr ("%s\n", error->message);
181                 g_error_free (error);
182                 print_usage (context);
183                 g_option_context_free (context);
184
185                 return -1;
186         }
187
188         input = file_arguments ? file_arguments[0] : NULL;
189         output = input ? file_arguments[1] : NULL;
190         if (!input || !output) {
191                 print_usage (context);
192                 g_option_context_free (context);
193
194                 return -1;
195         }
196         
197         g_option_context_free (context);
198
199         if (size < 1) {
200                 g_print ("Size cannot be smaller than 1 pixel\n");
201                 return -1;
202         }
203
204         input = file_arguments[0];
205         output = file_arguments[1];
206
207         g_type_init ();
208
209         if (!g_thread_supported ())
210                 g_thread_init (NULL);
211
212         if (!ev_init ())
213                 return -1;
214
215         file = g_file_new_for_commandline_arg (input);
216         uri = g_file_get_uri (file);
217         document = evince_thumbnailer_get_document (uri);
218
219         g_object_unref (file);
220         g_free (uri);
221
222         if (!document) {
223                 ev_shutdown ();
224                 return -2;
225         }
226
227         if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
228                 g_object_unref (document);
229                 ev_shutdown ();
230                 return -2;
231         }
232
233         if (EV_IS_ASYNC_RENDERER (document)) {
234                 struct AsyncData data;
235
236                 gtk_init (&argc, &argv);
237                 
238                 data.document = document;
239                 data.output = output;
240                 data.size = size;
241
242                 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
243                                  &data, FALSE, NULL);
244                 
245                 gtk_main ();
246
247                 g_object_unref (document);
248                 ev_shutdown ();
249
250                 return data.success ? 0 : -2;
251         }
252
253         if (!evince_thumbnail_pngenc_get (document, output, size)) {
254                 g_object_unref (document);
255                 ev_shutdown ();
256                 return -2;
257         }
258
259         g_object_unref (document);
260         ev_shutdown ();
261
262         return 0;
263 }