Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
evince-thumbnailer.c
Go to the documentation of this file.
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 <locale.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #ifdef G_OS_WIN32
30 #include <io.h>
31 #include <conio.h>
32 #if !(_WIN32_WINNT >= 0x0500)
33 #error "_WIN32_WINNT must be defined >= 0x0500"
34 #endif
35 #include <windows.h>
36 #endif
37 
38 #define THUMBNAIL_SIZE 128
39 #define DEFAULT_SLEEP_TIME (15 * G_USEC_PER_SEC) /* 15 seconds */
40 
41 static gboolean finished = TRUE;
42 
43 static gint size = THUMBNAIL_SIZE;
44 static gboolean time_limit = TRUE;
45 static const gchar **file_arguments;
46 
47 static const GOptionEntry goption_options[] = {
48  { "size", 's', 0, G_OPTION_ARG_INT, &size, NULL, "SIZE" },
49  { "no-limit", 'l', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &time_limit, "Don't limit the thumbnailing time to 15 seconds", NULL },
50  { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, "<input> <ouput>" },
51  { NULL }
52 };
53 
54 struct AsyncData {
56  const gchar *output;
57  gint size;
58  gboolean success;
59 };
60 
61 /* Time monitor: copied from totem */
62 G_GNUC_NORETURN static gpointer
63 time_monitor (gpointer data)
64 {
65  const gchar *app_name;
66 
67  g_usleep (DEFAULT_SLEEP_TIME);
68 
69  if (finished)
70  g_thread_exit (NULL);
71 
72  app_name = g_get_application_name ();
73  if (app_name == NULL)
74  app_name = g_get_prgname ();
75  g_printerr ("%s couldn't process file: '%s'\n"
76  "Reason: Took too much time to process.\n",
77  app_name,
78  (const char *) data);
79 
80  exit (0);
81 }
82 
83 static void
84 time_monitor_start (const char *input)
85 {
86  finished = FALSE;
87 
88  g_thread_new ("ThumbnailerTimer", time_monitor, (gpointer) input);
89 }
90 
91 static void
93 {
94  finished = TRUE;
95 }
96 
97 static void
98 delete_temp_file (GFile *file)
99 {
100  ev_tmp_file_unlink (file);
101  g_object_unref (file);
102 }
103 
104 static char *
105 get_target_uri (GFile *file)
106 {
107  GFileInfo *info;
108  char *target;
109 
110  info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI, G_FILE_QUERY_INFO_NONE, NULL, NULL);
111  if (info == NULL)
112  return NULL;
113  target = g_strdup (g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI));
114  g_object_unref (info);
115 
116  return target;
117 }
118 
119 static char *
120 get_local_path (GFile *file)
121 {
122  if (g_file_has_uri_scheme (file, "trash") != FALSE ||
123  g_file_has_uri_scheme (file, "recent") != FALSE) {
124  return get_target_uri (file);
125  }
126  return g_file_get_path (file);
127 }
128 
129 static EvDocument *
131 {
132  EvDocument *document = NULL;
133  gchar *uri, *path;
134  GFile *tmp_file = NULL;
135  GError *error = NULL;
136 
137  path = get_local_path (file);
138 
139  if (!path) {
140  gchar *base_name, *template;
141 
142  base_name = g_file_get_basename (file);
143  template = g_strdup_printf ("document.XXXXXX-%s", base_name);
144  g_free (base_name);
145 
146  tmp_file = ev_mkstemp_file (template, &error);
147  g_free (template);
148  if (!tmp_file) {
149  g_printerr ("Error loading remote document: %s\n", error->message);
150  g_error_free (error);
151 
152  return NULL;
153  }
154 
155  g_file_copy (file, tmp_file, G_FILE_COPY_OVERWRITE,
156  NULL, NULL, NULL, &error);
157  if (error) {
158  g_printerr ("Error loading remote document: %s\n", error->message);
159  g_error_free (error);
160  g_object_unref (tmp_file);
161 
162  return NULL;
163  }
164  uri = g_file_get_uri (tmp_file);
165  } else {
166  uri = g_filename_to_uri (path, NULL, NULL);
167  g_free (path);
168  }
169 
171  if (tmp_file) {
172  if (document) {
173  g_object_weak_ref (G_OBJECT (document),
174  (GWeakNotify)delete_temp_file,
175  tmp_file);
176  } else {
177  ev_tmp_file_unlink (tmp_file);
178  g_object_unref (tmp_file);
179  }
180  }
181  g_free (uri);
182  if (error) {
183  if (error->domain == EV_DOCUMENT_ERROR &&
184  error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
185  /* FIXME: Create a thumb for cryp docs */
186  g_error_free (error);
187  return NULL;
188  }
189  g_printerr ("Error loading document: %s\n", error->message);
190  g_error_free (error);
191  return NULL;
192  }
193 
194  return document;
195 }
196 
197 static gboolean
198 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
199 {
200  EvRenderContext *rc;
201  double width, height;
202  GdkPixbuf *pixbuf;
203  EvPage *page;
204 
205  page = ev_document_get_page (document, 0);
206 
207  ev_document_get_page_size (document, 0, &width, &height);
208 
209  rc = ev_render_context_new (page, 0, size / MAX (height, width));
210  pixbuf = ev_document_get_thumbnail (document, rc);
211  g_object_unref (rc);
212  g_object_unref (page);
213 
214  if (pixbuf != NULL) {
215  if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
216  g_object_unref (pixbuf);
217  return TRUE;
218  }
219 
220  g_object_unref (pixbuf);
221  }
222 
223  return FALSE;
224 }
225 
226 static gpointer
228 {
231  data->output,
232  data->size);
234 
235  g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
236 
237  return NULL;
238 }
239 
240 static void
241 print_usage (GOptionContext *context)
242 {
243  gchar *help;
244 
245  help = g_option_context_get_help (context, TRUE, NULL);
246  g_print ("%s", help);
247  g_free (help);
248 }
249 
250 int
251 main (int argc, char *argv[])
252 {
253  EvDocument *document;
254  GOptionContext *context;
255  const char *input;
256  const char *output;
257  GFile *file;
258  GError *error = NULL;
259 
260  setlocale (LC_ALL, "");
261 
262  context = g_option_context_new ("- GNOME Document Thumbnailer");
263  g_option_context_add_main_entries (context, goption_options, NULL);
264 
265  if (!g_option_context_parse (context, &argc, &argv, &error)) {
266  g_printerr ("%s\n", error->message);
267  g_error_free (error);
268  print_usage (context);
269  g_option_context_free (context);
270 
271  return -1;
272  }
273 
274  input = file_arguments ? file_arguments[0] : NULL;
275  output = input ? file_arguments[1] : NULL;
276  if (!input || !output) {
277  print_usage (context);
278  g_option_context_free (context);
279 
280  return -1;
281  }
282 
283  g_option_context_free (context);
284 
285  if (size < 1) {
286  g_printerr ("Size cannot be smaller than 1 pixel\n");
287  return -1;
288  }
289 
290  input = file_arguments[0];
291  output = file_arguments[1];
292 
293  if (!ev_init ())
294  return -1;
295 
296  file = g_file_new_for_commandline_arg (input);
297  document = evince_thumbnailer_get_document (file);
298  g_object_unref (file);
299 
300  if (!document) {
301  ev_shutdown ();
302  return -2;
303  }
304 
305  if (time_limit)
306  time_monitor_start (input);
307 
308  if (EV_IS_ASYNC_RENDERER (document)) {
309  struct AsyncData data;
310 
311  gtk_init (&argc, &argv);
312 
313  data.document = document;
314  data.output = output;
315  data.size = size;
316 
317  g_thread_new ("ThmbnlrAsyncRndr",
318  (GThreadFunc) evince_thumbnail_pngenc_get_async,
319  &data);
320 
321  gtk_main ();
322 
323  g_object_unref (document);
324  ev_shutdown ();
325 
326  return data.success ? 0 : -2;
327  }
328 
329  if (!evince_thumbnail_pngenc_get (document, output, size)) {
330  g_object_unref (document);
331  ev_shutdown ();
332  return -2;
333  }
334 
336  g_object_unref (document);
337  ev_shutdown ();
338 
339  return 0;
340 }