2 Copyright (C) 2005 Fernando Herrera <fherrera@onirica.com>
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.
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.
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.
21 #include <evince-document.h>
34 #define _WIN32_WINNT 0x0500
38 #define THUMBNAIL_SIZE 128
40 static gint size = THUMBNAIL_SIZE;
41 static const gchar **file_arguments;
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>" },
57 delete_temp_file (GFile *file)
59 ev_tmp_file_unlink (file);
60 g_object_unref (file);
64 evince_thumbnailer_get_document (GFile *file)
66 EvDocument *document = NULL;
68 GFile *tmp_file = NULL;
71 if (!g_file_is_native (file)) {
72 gchar *base_name, *template;
74 base_name = g_file_get_basename (file);
75 template = g_strdup_printf ("document.XXXXXX-%s", base_name);
78 tmp_file = ev_mkstemp_file (template, &error);
81 g_printerr ("Error loading remote document: %s\n", error->message);
87 g_file_copy (file, tmp_file, G_FILE_COPY_OVERWRITE,
88 NULL, NULL, NULL, &error);
90 g_printerr ("Error loading remote document: %s\n", error->message);
92 g_object_unref (tmp_file);
96 uri = g_file_get_uri (tmp_file);
98 uri = g_file_get_uri (file);
101 document = ev_document_factory_get_document (uri, &error);
104 g_object_weak_ref (G_OBJECT (document),
105 (GWeakNotify)delete_temp_file,
108 ev_tmp_file_unlink (tmp_file);
109 g_object_unref (tmp_file);
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);
120 g_printerr ("Error loading document: %s\n", error->message);
121 g_error_free (error);
129 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
132 double width, height;
136 page = ev_document_get_page (document, 0);
138 ev_document_get_page_size (document, 0, &width, &height);
140 rc = ev_render_context_new (page, 0, size / width);
141 pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (document),
144 g_object_unref (page);
146 if (pixbuf != NULL) {
147 const char *overlaid_icon_name = NULL;
149 if (overlaid_icon_name) {
150 GdkPixbuf *overlaid_pixbuf;
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);
157 gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
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;
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);
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,
175 GDK_INTERP_NEAREST, 100);
177 g_object_unref (overlaid_pixbuf);
181 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
182 g_object_unref (pixbuf);
186 g_object_unref (pixbuf);
193 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
195 ev_document_doc_mutex_lock ();
196 data->success = evince_thumbnail_pngenc_get (data->document,
199 ev_document_doc_mutex_unlock ();
201 g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
207 print_usage (GOptionContext *context)
211 help = g_option_context_get_help (context, TRUE, NULL);
212 g_print ("%s", help);
217 main (int argc, char *argv[])
219 EvDocument *document;
220 GOptionContext *context;
224 GError *error = NULL;
226 context = g_option_context_new ("- GNOME Document Thumbnailer");
227 g_option_context_add_main_entries (context, goption_options, NULL);
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);
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);
247 g_option_context_free (context);
250 g_print ("Size cannot be smaller than 1 pixel\n");
254 input = file_arguments[0];
255 output = file_arguments[1];
259 if (!g_thread_supported ())
260 g_thread_init (NULL);
265 file = g_file_new_for_commandline_arg (input);
266 document = evince_thumbnailer_get_document (file);
267 g_object_unref (file);
274 if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
275 g_object_unref (document);
280 if (EV_IS_ASYNC_RENDERER (document)) {
281 struct AsyncData data;
283 gtk_init (&argc, &argv);
285 data.document = document;
286 data.output = output;
289 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
294 g_object_unref (document);
297 return data.success ? 0 : -2;
300 if (!evince_thumbnail_pngenc_get (document, output, size)) {
301 g_object_unref (document);
306 g_object_unref (document);