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 File Reference
#include <config.h>
#include <evince-document.h>
#include <gio/gio.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
+ Include dependency graph for evince-thumbnailer.c:

Go to the source code of this file.

Data Structures

struct  AsyncData
 

Macros

#define THUMBNAIL_SIZE   128
 
#define DEFAULT_SLEEP_TIME   (15 * G_USEC_PER_SEC) /* 15 seconds */
 

Functions

static G_GNUC_NORETURN gpointer time_monitor (gpointer data)
 
static void time_monitor_start (const char *input)
 
static void time_monitor_stop (void)
 
static void delete_temp_file (GFile *file)
 
static char * get_target_uri (GFile *file)
 
static char * get_local_path (GFile *file)
 
static EvDocumentevince_thumbnailer_get_document (GFile *file)
 
static gboolean evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
 
static gpointer evince_thumbnail_pngenc_get_async (struct AsyncData *data)
 
static void print_usage (GOptionContext *context)
 
int main (int argc, char *argv[])
 

Variables

static gboolean finished = TRUE
 
static gint size = THUMBNAIL_SIZE
 
static gboolean time_limit = TRUE
 
static const gchar ** file_arguments
 
static const GOptionEntry goption_options []
 

Macro Definition Documentation

#define DEFAULT_SLEEP_TIME   (15 * G_USEC_PER_SEC) /* 15 seconds */

Definition at line 39 of file evince-thumbnailer.c.

#define THUMBNAIL_SIZE   128

Definition at line 38 of file evince-thumbnailer.c.

Function Documentation

static void delete_temp_file ( GFile *  file)
static

Definition at line 98 of file evince-thumbnailer.c.

99 {
100  ev_tmp_file_unlink (file);
101  g_object_unref (file);
102 }

+ Here is the caller graph for this function:

static gboolean evince_thumbnail_pngenc_get ( EvDocument document,
const char *  thumbnail,
int  size 
)
static

Definition at line 198 of file evince-thumbnailer.c.

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 }

+ Here is the caller graph for this function:

static gpointer evince_thumbnail_pngenc_get_async ( struct AsyncData data)
static

Definition at line 227 of file evince-thumbnailer.c.

228 {
231  data->output,
232  data->size);
234 
235  g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
236 
237  return NULL;
238 }

+ Here is the caller graph for this function:

static EvDocument* evince_thumbnailer_get_document ( GFile *  file)
static

Definition at line 130 of file evince-thumbnailer.c.

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 }

+ Here is the caller graph for this function:

static char* get_local_path ( GFile *  file)
static

Definition at line 120 of file evince-thumbnailer.c.

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 }

+ Here is the caller graph for this function:

static char* get_target_uri ( GFile *  file)
static

Definition at line 105 of file evince-thumbnailer.c.

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 }

+ Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 251 of file evince-thumbnailer.c.

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 }
static void print_usage ( GOptionContext *  context)
static

Definition at line 241 of file evince-thumbnailer.c.

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 }

+ Here is the caller graph for this function:

static G_GNUC_NORETURN gpointer time_monitor ( gpointer  data)
static

Definition at line 63 of file evince-thumbnailer.c.

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 }

+ Here is the caller graph for this function:

static void time_monitor_start ( const char *  input)
static

Definition at line 84 of file evince-thumbnailer.c.

85 {
86  finished = FALSE;
87 
88  g_thread_new ("ThumbnailerTimer", time_monitor, (gpointer) input);
89 }

+ Here is the caller graph for this function:

static void time_monitor_stop ( void  )
static

Definition at line 92 of file evince-thumbnailer.c.

93 {
94  finished = TRUE;
95 }

+ Here is the caller graph for this function:

Variable Documentation

const gchar** file_arguments
static

Definition at line 45 of file evince-thumbnailer.c.

gboolean finished = TRUE
static

Definition at line 41 of file evince-thumbnailer.c.

const GOptionEntry goption_options[]
static
Initial value:
= {
{ "size", 's', 0, G_OPTION_ARG_INT, &size, NULL, "SIZE" },
{ "no-limit", 'l', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &time_limit, "Don't limit the thumbnailing time to 15 seconds", NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, "<input> <ouput>" },
{ NULL }
}

Definition at line 47 of file evince-thumbnailer.c.

gint size = THUMBNAIL_SIZE
static

Definition at line 43 of file evince-thumbnailer.c.

gboolean time_limit = TRUE
static

Definition at line 44 of file evince-thumbnailer.c.