]> www.fi.muni.cz Git - evince.git/blob - libview/ev-jobs.c
[libview] Add a new job for printing using cairo
[evince.git] / libview / ev-jobs.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
4  *  Copyright (C) 2005 Red Hat, Inc
5  *
6  * Evince is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Evince is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22
23 #include "ev-jobs.h"
24 #include "ev-document-thumbnails.h"
25 #include "ev-document-links.h"
26 #include "ev-document-images.h"
27 #include "ev-document-forms.h"
28 #include "ev-file-exporter.h"
29 #include "ev-document-factory.h"
30 #include "ev-document-misc.h"
31 #include "ev-file-helpers.h"
32 #include "ev-document-fonts.h"
33 #include "ev-document-security.h"
34 #include "ev-document-find.h"
35 #include "ev-document-layers.h"
36 #include "ev-document-print.h"
37 #include "ev-document-annotations.h"
38 #include "ev-debug.h"
39
40 #include <errno.h>
41 #include <glib/gstdio.h>
42 #include <glib/gi18n-lib.h>
43 #include <unistd.h>
44
45 static void ev_job_init                   (EvJob                 *job);
46 static void ev_job_class_init             (EvJobClass            *class);
47 static void ev_job_links_init             (EvJobLinks            *job);
48 static void ev_job_links_class_init       (EvJobLinksClass       *class);
49 static void ev_job_attachments_init       (EvJobAttachments      *job);
50 static void ev_job_attachments_class_init (EvJobAttachmentsClass *class);
51 static void ev_job_render_init            (EvJobRender           *job);
52 static void ev_job_render_class_init      (EvJobRenderClass      *class);
53 static void ev_job_thumbnail_init         (EvJobThumbnail        *job);
54 static void ev_job_thumbnail_class_init   (EvJobThumbnailClass   *class);
55 static void ev_job_load_init              (EvJobLoad             *job);
56 static void ev_job_load_class_init        (EvJobLoadClass        *class);
57 static void ev_job_save_init              (EvJobSave             *job);
58 static void ev_job_save_class_init        (EvJobSaveClass        *class);
59 static void ev_job_find_init              (EvJobFind             *job);
60 static void ev_job_find_class_init        (EvJobFindClass        *class);
61 static void ev_job_layers_init            (EvJobLayers           *job);
62 static void ev_job_layers_class_init      (EvJobLayersClass      *class);
63 static void ev_job_export_init            (EvJobExport           *job);
64 static void ev_job_export_class_init      (EvJobExportClass      *class);
65 static void ev_job_print_init             (EvJobPrint            *job);
66 static void ev_job_print_class_init       (EvJobPrintClass       *class);
67
68 enum {
69         CANCELLED,
70         FINISHED,
71         LAST_SIGNAL
72 };
73
74 enum {
75         PAGE_READY,
76         RENDER_LAST_SIGNAL
77 };
78
79 enum {
80         FONTS_UPDATED,
81         FONTS_LAST_SIGNAL
82 };
83
84 enum {
85         FIND_UPDATED,
86         FIND_LAST_SIGNAL
87 };
88
89 static guint job_signals[LAST_SIGNAL] = { 0 };
90 static guint job_render_signals[RENDER_LAST_SIGNAL] = { 0 };
91 static guint job_fonts_signals[FONTS_LAST_SIGNAL] = { 0 };
92 static guint job_find_signals[FIND_LAST_SIGNAL] = { 0 };
93
94 G_DEFINE_ABSTRACT_TYPE (EvJob, ev_job, G_TYPE_OBJECT)
95 G_DEFINE_TYPE (EvJobLinks, ev_job_links, EV_TYPE_JOB)
96 G_DEFINE_TYPE (EvJobAttachments, ev_job_attachments, EV_TYPE_JOB)
97 G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB)
98 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
99 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
100 G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB)
101 G_DEFINE_TYPE (EvJobSave, ev_job_save, EV_TYPE_JOB)
102 G_DEFINE_TYPE (EvJobFind, ev_job_find, EV_TYPE_JOB)
103 G_DEFINE_TYPE (EvJobLayers, ev_job_layers, EV_TYPE_JOB)
104 G_DEFINE_TYPE (EvJobExport, ev_job_export, EV_TYPE_JOB)
105 G_DEFINE_TYPE (EvJobPrint, ev_job_print, EV_TYPE_JOB)
106
107 /* EvJob */
108 static void
109 ev_job_init (EvJob *job)
110 {
111         job->cancellable = g_cancellable_new ();
112 }
113
114 static void
115 ev_job_dispose (GObject *object)
116 {
117         EvJob *job;
118
119         job = EV_JOB (object);
120
121         if (job->document) {
122                 g_object_unref (job->document);
123                 job->document = NULL;
124         }
125
126         if (job->cancellable) {
127                 g_object_unref (job->cancellable);
128                 job->cancellable = NULL;
129         }
130
131         if (job->error) {
132                 g_error_free (job->error);
133                 job->error = NULL;
134         }
135
136         (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object);
137 }
138
139 static void
140 ev_job_class_init (EvJobClass *class)
141 {
142         GObjectClass *oclass;
143
144         oclass = G_OBJECT_CLASS (class);
145
146         oclass->dispose = ev_job_dispose;
147
148         job_signals[CANCELLED] =
149                 g_signal_new ("cancelled",
150                               EV_TYPE_JOB,
151                               G_SIGNAL_RUN_LAST,
152                               G_STRUCT_OFFSET (EvJobClass, cancelled),
153                               NULL, NULL,
154                               g_cclosure_marshal_VOID__VOID,
155                               G_TYPE_NONE, 0);
156         job_signals [FINISHED] =
157                 g_signal_new ("finished",
158                               EV_TYPE_JOB,
159                               G_SIGNAL_RUN_FIRST,
160                               G_STRUCT_OFFSET (EvJobClass, finished),
161                               NULL, NULL,
162                               g_cclosure_marshal_VOID__VOID,
163                               G_TYPE_NONE, 0);
164 }
165
166 static gboolean
167 emit_finished (EvJob *job)
168 {
169         ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
170
171         job->idle_finished_id = 0;
172         
173         if (job->cancelled) {
174                 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit finished", EV_GET_TYPE_NAME (job), job);
175         } else {
176                 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
177                 g_signal_emit (job, job_signals[FINISHED], 0);
178         }
179         
180         return FALSE;
181 }
182
183 static void
184 ev_job_emit_finished (EvJob *job)
185 {
186         ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
187
188         if (g_cancellable_is_cancelled (job->cancellable)) {
189                 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, returning", EV_GET_TYPE_NAME (job), job);
190                 return;
191         }
192         
193         job->finished = TRUE;
194         
195         if (job->run_mode == EV_JOB_RUN_THREAD) {
196                 job->idle_finished_id =
197                         g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
198                                          (GSourceFunc)emit_finished,
199                                          g_object_ref (job),
200                                          (GDestroyNotify)g_object_unref);
201         } else {
202                 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
203                 g_signal_emit (job, job_signals[FINISHED], 0);
204         }
205 }
206
207 gboolean
208 ev_job_run (EvJob *job)
209 {
210         EvJobClass *class = EV_JOB_GET_CLASS (job);
211         
212         return class->run (job);
213 }
214
215 void
216 ev_job_cancel (EvJob *job)
217 {
218         if (job->cancelled || (job->finished && job->idle_finished_id == 0))
219                 return;
220
221         ev_debug_message (DEBUG_JOBS, "job %s (%p) cancelled", EV_GET_TYPE_NAME (job), job);
222         ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
223         
224         /* This should never be called from a thread */
225         job->cancelled = TRUE;
226         g_cancellable_cancel (job->cancellable);
227         g_signal_emit (job, job_signals[CANCELLED], 0);
228 }
229
230 void
231 ev_job_failed (EvJob       *job,
232                GQuark       domain,
233                gint         code,
234                const gchar *format,
235                ...)
236 {
237         va_list args;
238         gchar  *message;
239         
240         if (job->failed || job->finished)
241                 return;
242
243         ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
244         
245         job->failed = TRUE;
246         
247         va_start (args, format);
248         message = g_strdup_vprintf (format, args);
249         va_end (args);
250         
251         job->error = g_error_new_literal (domain, code, message);
252         g_free (message);
253         
254         ev_job_emit_finished (job);                                                                                                               
255 }
256
257 void
258 ev_job_failed_from_error (EvJob  *job,
259                           GError *error)
260 {
261         if (job->failed || job->finished)
262                 return;
263         
264         ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
265
266         job->failed = TRUE;
267         job->error = g_error_copy (error);
268
269         ev_job_emit_finished (job);
270 }
271
272 void
273 ev_job_succeeded (EvJob *job)
274 {
275         if (job->finished)
276                 return;
277
278         ev_debug_message (DEBUG_JOBS, "job %s (%p) succeeded", EV_GET_TYPE_NAME (job), job);
279         
280         job->failed = FALSE;
281         ev_job_emit_finished (job);
282 }
283
284 gboolean
285 ev_job_is_finished (EvJob *job)
286 {
287         return job->finished;
288 }
289
290 gboolean
291 ev_job_is_failed (EvJob *job)
292 {
293         return job->failed;
294 }
295
296 EvJobRunMode
297 ev_job_get_run_mode (EvJob *job)
298 {
299         return job->run_mode;
300 }
301
302 void
303 ev_job_set_run_mode (EvJob       *job,
304                      EvJobRunMode run_mode)
305 {
306         job->run_mode = run_mode;
307 }
308
309 /* EvJobLinks */
310 static void
311 ev_job_links_init (EvJobLinks *job)
312 {
313         EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
314 }
315
316 static void
317 ev_job_links_dispose (GObject *object)
318 {
319         EvJobLinks *job;
320
321         ev_debug_message (DEBUG_JOBS, NULL);
322         
323         job = EV_JOB_LINKS (object);
324
325         if (job->model) {
326                 g_object_unref (job->model);
327                 job->model = NULL;
328         }
329
330         (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object);
331 }
332
333 static gboolean
334 ev_job_links_run (EvJob *job)
335 {
336         EvJobLinks *job_links = EV_JOB_LINKS (job);
337
338         ev_debug_message (DEBUG_JOBS, NULL);
339         ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
340         
341         ev_document_doc_mutex_lock ();
342         job_links->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (job->document));
343         ev_document_doc_mutex_unlock ();
344         
345         ev_job_succeeded (job);
346         
347         return FALSE;
348 }
349
350 static void
351 ev_job_links_class_init (EvJobLinksClass *class)
352 {
353         GObjectClass *oclass = G_OBJECT_CLASS (class);
354         EvJobClass   *job_class = EV_JOB_CLASS (class);
355
356         oclass->dispose = ev_job_links_dispose;
357         job_class->run = ev_job_links_run;
358 }
359
360 EvJob *
361 ev_job_links_new (EvDocument *document)
362 {
363         EvJob *job;
364
365         ev_debug_message (DEBUG_JOBS, NULL);
366
367         job = g_object_new (EV_TYPE_JOB_LINKS, NULL);
368         job->document = g_object_ref (document);
369         
370         return job;
371 }
372
373 /* EvJobAttachments */
374 static void
375 ev_job_attachments_init (EvJobAttachments *job)
376 {
377         EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
378 }
379
380 static void
381 ev_job_attachments_dispose (GObject *object)
382 {
383         EvJobAttachments *job;
384
385         ev_debug_message (DEBUG_JOBS, NULL);
386         
387         job = EV_JOB_ATTACHMENTS (object);
388
389         if (job->attachments) {
390                 g_list_foreach (job->attachments, (GFunc)g_object_unref, NULL);
391                 g_list_free (job->attachments);
392                 job->attachments = NULL;
393         }
394
395         (* G_OBJECT_CLASS (ev_job_attachments_parent_class)->dispose) (object);
396 }
397
398 static gboolean
399 ev_job_attachments_run (EvJob *job)
400 {
401         EvJobAttachments *job_attachments = EV_JOB_ATTACHMENTS (job);
402
403         ev_debug_message (DEBUG_JOBS, NULL);
404         ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
405         
406         ev_document_doc_mutex_lock ();
407         job_attachments->attachments = ev_document_get_attachments (job->document);
408         ev_document_doc_mutex_unlock ();
409         
410         ev_job_succeeded (job);
411         
412         return FALSE;
413 }
414
415 static void
416 ev_job_attachments_class_init (EvJobAttachmentsClass *class)
417 {
418         GObjectClass *oclass = G_OBJECT_CLASS (class);
419         EvJobClass   *job_class = EV_JOB_CLASS (class);
420
421         oclass->dispose = ev_job_attachments_dispose;
422         job_class->run = ev_job_attachments_run;
423 }
424
425 EvJob *
426 ev_job_attachments_new (EvDocument *document)
427 {
428         EvJob *job;
429
430         ev_debug_message (DEBUG_JOBS, NULL);
431
432         job = g_object_new (EV_TYPE_JOB_ATTACHMENTS, NULL);
433         job->document = g_object_ref (document);
434         
435         return job;
436 }
437
438 /* EvJobRender */
439 static void
440 ev_job_render_init (EvJobRender *job)
441 {
442         EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
443 }
444
445 static void
446 ev_job_render_dispose (GObject *object)
447 {
448         EvJobRender *job;
449
450         job = EV_JOB_RENDER (object);
451
452         if (job->ev_page) {
453                 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job->ev_page->index, job);
454                 g_object_unref (job->ev_page);
455                 job->ev_page = NULL;
456         }
457         
458         if (job->surface) {
459                 cairo_surface_destroy (job->surface);
460                 job->surface = NULL;
461         }
462
463         if (job->selection) {
464                 cairo_surface_destroy (job->selection);
465                 job->selection = NULL;
466         }
467
468         if (job->selection_region) {
469                 gdk_region_destroy (job->selection_region);
470                 job->selection_region = NULL;
471         }
472
473         (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
474 }
475
476 static gboolean
477 notify_page_ready (EvJobRender *job)
478 {
479         ev_debug_message (DEBUG_JOBS, "%d (%p)", job->ev_page->index, job);
480         ev_profiler_stop (EV_PROFILE_JOBS, "Rendering page %d", job->ev_page->index);
481
482         if (EV_JOB (job)->cancelled) {
483                 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit page_ready", EV_GET_TYPE_NAME (job), job);
484         } else {
485                 g_signal_emit (job, job_render_signals[PAGE_READY], 0);
486         }
487
488         return FALSE;
489 }
490
491 static void
492 ev_job_render_page_ready (EvJobRender *job)
493 {
494         ev_debug_message (DEBUG_JOBS, "%d (%p)", job->ev_page->index, job);
495         
496         job->page_ready = TRUE;
497         g_idle_add_full (G_PRIORITY_HIGH_IDLE,
498                          (GSourceFunc)notify_page_ready,
499                          g_object_ref (job),
500                          (GDestroyNotify)g_object_unref);
501 }
502
503 static gboolean
504 ev_job_render_run (EvJob *job)
505 {
506         EvJobRender     *job_render = EV_JOB_RENDER (job);
507         EvRenderContext *rc;
508
509         ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_render->page, job);
510         ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
511         
512         ev_document_doc_mutex_lock ();
513
514         ev_profiler_start (EV_PROFILE_JOBS, "Rendering page %d", job_render->page);
515                 
516         ev_document_fc_mutex_lock ();
517
518         job_render->ev_page = ev_document_get_page (job->document, job_render->page);
519         rc = ev_render_context_new (job_render->ev_page, job_render->rotation, job_render->scale);
520                 
521         job_render->surface = ev_document_render (job->document, rc);
522         /* If job was cancelled during the page rendering,
523          * we return now, so that the thread is finished ASAP
524          */
525         if (g_cancellable_is_cancelled (job->cancellable)) {
526                 ev_document_fc_mutex_unlock ();
527                 ev_document_doc_mutex_unlock ();
528                 g_object_unref (rc);
529
530                 return FALSE;
531         }
532         
533         if ((job_render->flags & EV_RENDER_INCLUDE_SELECTION) && EV_IS_SELECTION (job->document)) {
534                 ev_selection_render_selection (EV_SELECTION (job->document),
535                                                rc,
536                                                &(job_render->selection),
537                                                &(job_render->selection_points),
538                                                NULL,
539                                                job_render->selection_style,
540                                                &(job_render->text), &(job_render->base));
541                 job_render->selection_region =
542                         ev_selection_get_selection_region (EV_SELECTION (job->document),
543                                                            rc,
544                                                            job_render->selection_style,
545                                                            &(job_render->selection_points));
546         }
547
548         ev_job_render_page_ready (job_render);
549                 
550         ev_document_fc_mutex_unlock ();
551                 
552         if ((job_render->flags & EV_RENDER_INCLUDE_TEXT) && EV_IS_SELECTION (job->document))
553                 job_render->text_mapping =
554                         ev_selection_get_selection_map (EV_SELECTION (job->document), rc);
555         if ((job_render->flags & EV_RENDER_INCLUDE_LINKS) && EV_IS_DOCUMENT_LINKS (job->document))
556                 job_render->link_mapping =
557                         ev_document_links_get_links (EV_DOCUMENT_LINKS (job->document), job_render->page);
558         if ((job_render->flags & EV_RENDER_INCLUDE_FORMS) && EV_IS_DOCUMENT_FORMS (job->document))
559                 job_render->form_field_mapping =
560                         ev_document_forms_get_form_fields (EV_DOCUMENT_FORMS (job->document),
561                                                            job_render->ev_page);
562         if ((job_render->flags & EV_RENDER_INCLUDE_IMAGES) && EV_IS_DOCUMENT_IMAGES (job->document))
563                 job_render->image_mapping =
564                         ev_document_images_get_image_mapping (EV_DOCUMENT_IMAGES (job->document),
565                                                               job_render->page);
566         if ((job_render->flags & EV_RENDER_INCLUDE_ANNOTS) && EV_IS_DOCUMENT_ANNOTATIONS (job->document))
567                 job_render->annots_mapping =
568                         ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document),
569                                                                  job_render->ev_page);
570         g_object_unref (rc);
571         ev_document_doc_mutex_unlock ();
572         
573         ev_job_succeeded (job);
574         
575         return FALSE;
576 }
577
578 static void
579 ev_job_render_class_init (EvJobRenderClass *class)
580 {
581         GObjectClass *oclass = G_OBJECT_CLASS (class);
582         EvJobClass   *job_class = EV_JOB_CLASS (class);
583
584         job_render_signals [PAGE_READY] =
585                 g_signal_new ("page-ready",
586                               EV_TYPE_JOB_RENDER,
587                               G_SIGNAL_RUN_LAST,
588                               G_STRUCT_OFFSET (EvJobRenderClass, page_ready),
589                               NULL, NULL,
590                               g_cclosure_marshal_VOID__VOID,
591                               G_TYPE_NONE, 0);
592
593         oclass->dispose = ev_job_render_dispose;
594         job_class->run = ev_job_render_run;
595 }
596
597 EvJob *
598 ev_job_render_new (EvDocument   *document,
599                    gint          page,
600                    gint          rotation,
601                    gdouble       scale, 
602                    gint          width,
603                    gint          height,
604                    EvRenderFlags flags)
605 {
606         EvJobRender *job;
607
608         ev_debug_message (DEBUG_JOBS, "page: %d", page);
609         
610         job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
611
612         EV_JOB (job)->document = g_object_ref (document);
613         job->page = page;
614         job->rotation = rotation;
615         job->scale = scale;
616         job->target_width = width;
617         job->target_height = height;
618         job->flags = flags;
619
620         return EV_JOB (job);
621 }
622
623 void
624 ev_job_render_set_selection_info (EvJobRender     *job,
625                                   EvRectangle     *selection_points,
626                                   EvSelectionStyle selection_style,
627                                   GdkColor        *text,
628                                   GdkColor        *base)
629 {
630         job->flags |= EV_RENDER_INCLUDE_SELECTION;
631         
632         job->selection_points = *selection_points;
633         job->selection_style = selection_style;
634         job->text = *text;
635         job->base = *base;
636 }
637
638 /* EvJobThumbnail */
639 static void
640 ev_job_thumbnail_init (EvJobThumbnail *job)
641 {
642         EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
643 }
644
645 static void
646 ev_job_thumbnail_dispose (GObject *object)
647 {
648         EvJobThumbnail *job;
649
650         job = EV_JOB_THUMBNAIL (object);
651
652         ev_debug_message (DEBUG_JOBS, "%d (%p)", job->page, job);
653         
654         if (job->thumbnail) {
655                 g_object_unref (job->thumbnail);
656                 job->thumbnail = NULL;
657         }
658
659         (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
660 }
661
662 static gboolean
663 ev_job_thumbnail_run (EvJob *job)
664 {
665         EvJobThumbnail  *job_thumb = EV_JOB_THUMBNAIL (job);
666         EvRenderContext *rc;
667         EvPage          *page;
668
669         ev_debug_message (DEBUG_JOBS, "%d (%p)", job_thumb->page, job);
670         ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
671         
672         ev_document_doc_mutex_lock ();
673
674         page = ev_document_get_page (job->document, job_thumb->page);
675         rc = ev_render_context_new (page, job_thumb->rotation, job_thumb->scale);
676         g_object_unref (page);
677
678         job_thumb->thumbnail = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (job->document),
679                                                                      rc, TRUE);
680         g_object_unref (rc);
681         ev_document_doc_mutex_unlock ();
682
683         ev_job_succeeded (job);
684         
685         return FALSE;
686 }
687
688 static void
689 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
690 {
691         GObjectClass *oclass = G_OBJECT_CLASS (class);
692         EvJobClass   *job_class = EV_JOB_CLASS (class);
693
694         oclass->dispose = ev_job_thumbnail_dispose;
695         job_class->run = ev_job_thumbnail_run;
696 }
697
698 EvJob *
699 ev_job_thumbnail_new (EvDocument *document,
700                       gint        page,
701                       gint        rotation,
702                       gdouble     scale)
703 {
704         EvJobThumbnail *job;
705
706         ev_debug_message (DEBUG_JOBS, "%d", page);
707         
708         job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
709
710         EV_JOB (job)->document = g_object_ref (document);
711         job->page = page;
712         job->rotation = rotation;
713         job->scale = scale;
714
715         return EV_JOB (job);
716 }
717
718 /* EvJobFonts */
719 static void
720 ev_job_fonts_init (EvJobFonts *job)
721 {
722         EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
723 }
724
725 static gboolean
726 ev_job_fonts_run (EvJob *job)
727 {
728         EvJobFonts      *job_fonts = EV_JOB_FONTS (job);
729         EvDocumentFonts *fonts = EV_DOCUMENT_FONTS (job->document);
730
731         ev_debug_message (DEBUG_JOBS, NULL);
732         
733         /* Do not block the main loop */
734         if (!ev_document_doc_mutex_trylock ())
735                 return TRUE;
736         
737         if (!ev_document_fc_mutex_trylock ())
738                 return TRUE;
739
740 #ifdef EV_ENABLE_DEBUG
741         /* We use the #ifdef in this case because of the if */
742         if (ev_document_fonts_get_progress (fonts) == 0)
743                 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
744 #endif
745
746         job_fonts->scan_completed = !ev_document_fonts_scan (fonts, 20);
747         g_signal_emit (job_fonts, job_fonts_signals[FONTS_UPDATED], 0,
748                        ev_document_fonts_get_progress (fonts));
749
750         ev_document_fc_mutex_unlock ();
751         ev_document_doc_mutex_unlock ();
752
753         if (job_fonts->scan_completed)
754                 ev_job_succeeded (job);
755         
756         return !job_fonts->scan_completed;
757 }
758
759 static void
760 ev_job_fonts_class_init (EvJobFontsClass *class)
761 {
762         EvJobClass *job_class = EV_JOB_CLASS (class);
763         
764         job_class->run = ev_job_fonts_run;
765         
766         job_fonts_signals[FONTS_UPDATED] =
767                 g_signal_new ("updated",
768                               EV_TYPE_JOB_FONTS,
769                               G_SIGNAL_RUN_LAST,
770                               G_STRUCT_OFFSET (EvJobFontsClass, updated),
771                               NULL, NULL,
772                               g_cclosure_marshal_VOID__DOUBLE,
773                               G_TYPE_NONE,
774                               1, G_TYPE_DOUBLE);
775 }
776
777 EvJob *
778 ev_job_fonts_new (EvDocument *document)
779 {
780         EvJobFonts *job;
781
782         ev_debug_message (DEBUG_JOBS, NULL);
783         
784         job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
785
786         EV_JOB (job)->document = g_object_ref (document);
787
788         return EV_JOB (job);
789 }
790
791 /* EvJobLoad */
792 static void
793 ev_job_load_init (EvJobLoad *job)
794 {
795         EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
796 }
797
798 static void
799 ev_job_load_dispose (GObject *object)
800 {
801         EvJobLoad *job = EV_JOB_LOAD (object);
802
803         ev_debug_message (DEBUG_JOBS, "%s", job->uri);
804         
805         if (job->uri) {
806                 g_free (job->uri);
807                 job->uri = NULL;
808         }
809
810         if (job->password) {
811                 g_free (job->password);
812                 job->password = NULL;
813         }
814
815         (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object);
816 }
817
818 static gboolean
819 ev_job_load_run (EvJob *job)
820 {
821         EvJobLoad *job_load = EV_JOB_LOAD (job);
822         GError    *error = NULL;
823         
824         ev_debug_message (DEBUG_JOBS, "%s", job_load->uri);
825         ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
826         
827         ev_document_fc_mutex_lock ();
828
829         /* This job may already have a document even if the job didn't complete
830            because, e.g., a password is required - if so, just reload rather than
831            creating a new instance */
832         if (job->document) {
833                 if (job_load->password) {
834                         ev_document_security_set_password (EV_DOCUMENT_SECURITY (job->document),
835                                                            job_load->password);
836                 }
837                 
838                 job->failed = FALSE;
839                 job->finished = FALSE;
840                 g_clear_error (&job->error);
841                 
842                 ev_document_load (job->document,
843                                   job_load->uri,
844                                   &error);
845         } else {
846                 job->document = ev_document_factory_get_document (job_load->uri,
847                                                                   &error);
848         }
849
850         ev_document_fc_mutex_unlock ();
851
852         if (error) {
853                 ev_job_failed_from_error (job, error);
854                 g_error_free (error);
855         } else {
856                 ev_job_succeeded (job);
857         }
858
859         return FALSE;
860 }
861
862 static void
863 ev_job_load_class_init (EvJobLoadClass *class)
864 {
865         GObjectClass *oclass = G_OBJECT_CLASS (class);
866         EvJobClass   *job_class = EV_JOB_CLASS (class);
867
868         oclass->dispose = ev_job_load_dispose;
869         job_class->run = ev_job_load_run;
870 }
871
872 EvJob *
873 ev_job_load_new (const gchar *uri)
874 {
875         EvJobLoad *job;
876
877         ev_debug_message (DEBUG_JOBS, "%s", uri);
878         
879         job = g_object_new (EV_TYPE_JOB_LOAD, NULL);
880         job->uri = g_strdup (uri);
881
882         return EV_JOB (job);
883 }
884
885 void
886 ev_job_load_set_uri (EvJobLoad *job, const gchar *uri)
887 {
888         ev_debug_message (DEBUG_JOBS, "%s", uri);
889         
890         if (job->uri)
891                 g_free (job->uri);
892         job->uri = g_strdup (uri);
893 }
894
895 void
896 ev_job_load_set_password (EvJobLoad *job, const gchar *password)
897 {
898         ev_debug_message (DEBUG_JOBS, NULL);
899
900         if (job->password)
901                 g_free (job->password);
902         job->password = password ? g_strdup (password) : NULL;
903 }
904
905 /* EvJobSave */
906 static void
907 ev_job_save_init (EvJobSave *job)
908 {
909         EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
910 }
911
912 static void
913 ev_job_save_dispose (GObject *object)
914 {
915         EvJobSave *job = EV_JOB_SAVE (object);
916
917         ev_debug_message (DEBUG_JOBS, "%s", job->uri);
918         
919         if (job->uri) {
920                 g_free (job->uri);
921                 job->uri = NULL;
922         }
923
924         if (job->document_uri) {
925                 g_free (job->document_uri);
926                 job->document_uri = NULL;
927         }
928
929         (* G_OBJECT_CLASS (ev_job_save_parent_class)->dispose) (object);
930 }
931
932 static gboolean
933 ev_job_save_run (EvJob *job)
934 {
935         EvJobSave *job_save = EV_JOB_SAVE (job);
936         gint       fd;
937         gchar     *filename;
938         gchar     *tmp_filename;
939         gchar     *local_uri;
940         GError    *error = NULL;
941         
942         ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", job_save->uri, job_save->document_uri);
943         ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
944         
945         filename = ev_tmp_filename ("saveacopy");
946         tmp_filename = g_strdup_printf ("%s.XXXXXX", filename);
947         g_free (filename);
948
949         fd = g_mkstemp (tmp_filename);
950         if (fd == -1) {
951                 gchar *display_name;
952                 gint   save_errno = errno;
953
954                 display_name = g_filename_display_name (tmp_filename);
955                 ev_job_failed (job,
956                                G_FILE_ERROR,
957                                g_file_error_from_errno (save_errno),
958                                _("Failed to create file “%s”: %s"),
959                                display_name, g_strerror (save_errno));
960                 g_free (display_name);
961                 g_free (tmp_filename);
962
963                 return FALSE;
964         }
965
966         ev_document_doc_mutex_lock ();
967
968         /* Save document to temp filename */
969         local_uri = g_filename_to_uri (tmp_filename, NULL, NULL);
970         ev_document_save (job->document, local_uri, &error);
971         close (fd);
972
973         ev_document_doc_mutex_unlock ();
974
975         if (error) {
976                 g_free (local_uri);
977                 ev_job_failed_from_error (job, error);
978                 g_error_free (error);
979                 
980                 return FALSE;
981         }
982
983         /* If original document was compressed,
984          * compress it again before saving
985          */
986         if (g_object_get_data (G_OBJECT (job->document), "uri-uncompressed")) {
987                 EvCompressionType ctype = EV_COMPRESSION_NONE;
988                 const gchar      *ext;
989                 gchar            *uri_comp;
990                 
991                 ext = g_strrstr (job_save->document_uri, ".gz");
992                 if (ext && g_ascii_strcasecmp (ext, ".gz") == 0)
993                         ctype = EV_COMPRESSION_GZIP;
994                 
995                 ext = g_strrstr (job_save->document_uri, ".bz2");
996                 if (ext && g_ascii_strcasecmp (ext, ".bz2") == 0)
997                         ctype = EV_COMPRESSION_BZIP2;
998
999                 uri_comp = ev_file_compress (local_uri, ctype, &error);
1000                 g_free (local_uri);
1001                 ev_tmp_filename_unlink (tmp_filename);
1002
1003                 if (!uri_comp || error) {
1004                         local_uri = NULL;
1005                 } else {
1006                         local_uri = uri_comp;
1007                 }
1008         }
1009
1010         g_free (tmp_filename);
1011         
1012         if (error) {
1013                 g_free (local_uri);
1014                 ev_job_failed_from_error (job, error);
1015                 g_error_free (error);
1016                 
1017                 return FALSE;
1018         }
1019
1020         if (!local_uri)
1021                 return FALSE;
1022
1023         ev_xfer_uri_simple (local_uri, job_save->uri, &error);
1024         ev_tmp_uri_unlink (local_uri);
1025
1026         if (error) {
1027                 ev_job_failed_from_error (job, error);
1028                 g_error_free (error);
1029         } else {
1030                 ev_job_succeeded (job);
1031         }
1032         
1033         return FALSE;
1034 }
1035
1036 static void
1037 ev_job_save_class_init (EvJobSaveClass *class)
1038 {
1039         GObjectClass *oclass = G_OBJECT_CLASS (class);
1040         EvJobClass   *job_class = EV_JOB_CLASS (class);
1041
1042         oclass->dispose = ev_job_save_dispose;
1043         job_class->run = ev_job_save_run;
1044 }
1045
1046 EvJob *
1047 ev_job_save_new (EvDocument  *document,
1048                  const gchar *uri,
1049                  const gchar *document_uri)
1050 {
1051         EvJobSave *job;
1052
1053         ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", uri, document_uri);
1054
1055         job = g_object_new (EV_TYPE_JOB_SAVE, NULL);
1056
1057         EV_JOB (job)->document = g_object_ref (document);
1058         job->uri = g_strdup (uri);
1059         job->document_uri = g_strdup (document_uri);
1060
1061         return EV_JOB (job);
1062 }
1063
1064 /* EvJobFind */
1065 static void
1066 ev_job_find_init (EvJobFind *job)
1067 {
1068         EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
1069 }
1070
1071 static void
1072 ev_job_find_dispose (GObject *object)
1073 {
1074         EvJobFind *job = EV_JOB_FIND (object);
1075
1076         ev_debug_message (DEBUG_JOBS, NULL);
1077
1078         if (job->text) {
1079                 g_free (job->text);
1080                 job->text = NULL;
1081         }
1082
1083         if (job->pages) {
1084                 gint i;
1085
1086                 for (i = 0; i < job->n_pages; i++) {
1087                         g_list_foreach (job->pages[i], (GFunc)g_free, NULL);
1088                         g_list_free (job->pages[i]);
1089                 }
1090
1091                 g_free (job->pages);
1092                 job->pages = NULL;
1093         }
1094         
1095         (* G_OBJECT_CLASS (ev_job_find_parent_class)->dispose) (object);
1096 }
1097
1098 static gboolean
1099 ev_job_find_run (EvJob *job)
1100 {
1101         EvJobFind      *job_find = EV_JOB_FIND (job);
1102         EvDocumentFind *find = EV_DOCUMENT_FIND (job->document);
1103         EvPage         *ev_page;
1104         GList          *matches;
1105
1106         ev_debug_message (DEBUG_JOBS, NULL);
1107         
1108         /* Do not block the main loop */
1109         if (!ev_document_doc_mutex_trylock ())
1110                 return TRUE;
1111         
1112 #ifdef EV_ENABLE_DEBUG
1113         /* We use the #ifdef in this case because of the if */
1114         if (job_find->current_page == job_find->start_page)
1115                 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1116 #endif
1117
1118         ev_page = ev_document_get_page (job->document, job_find->current_page);
1119         matches = ev_document_find_find_text (find, ev_page, job_find->text,
1120                                               job_find->case_sensitive);
1121         g_object_unref (ev_page);
1122         
1123         ev_document_doc_mutex_unlock ();
1124
1125         if (!job_find->has_results)
1126                 job_find->has_results = (matches != NULL);
1127
1128         job_find->pages[job_find->current_page] = matches;
1129         g_signal_emit (job_find, job_find_signals[FIND_UPDATED], 0, job_find->current_page);
1130                        
1131         job_find->current_page = (job_find->current_page + 1) % job_find->n_pages;
1132         if (job_find->current_page == job_find->start_page) {
1133                 ev_job_succeeded (job);
1134
1135                 return FALSE;
1136         }
1137
1138         return TRUE;
1139 }
1140
1141 static void
1142 ev_job_find_class_init (EvJobFindClass *class)
1143 {
1144         EvJobClass   *job_class = EV_JOB_CLASS (class);
1145         GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1146         
1147         job_class->run = ev_job_find_run;
1148         gobject_class->dispose = ev_job_find_dispose;
1149         
1150         job_find_signals[FIND_UPDATED] =
1151                 g_signal_new ("updated",
1152                               EV_TYPE_JOB_FIND,
1153                               G_SIGNAL_RUN_LAST,
1154                               G_STRUCT_OFFSET (EvJobFindClass, updated),
1155                               NULL, NULL,
1156                               g_cclosure_marshal_VOID__INT,
1157                               G_TYPE_NONE,
1158                               1, G_TYPE_INT);
1159 }
1160
1161 EvJob *
1162 ev_job_find_new (EvDocument  *document,
1163                  gint         start_page,
1164                  gint         n_pages,
1165                  const gchar *text,
1166                  gboolean     case_sensitive)
1167 {
1168         EvJobFind *job;
1169         
1170         ev_debug_message (DEBUG_JOBS, NULL);
1171         
1172         job = g_object_new (EV_TYPE_JOB_FIND, NULL);
1173
1174         EV_JOB (job)->document = g_object_ref (document);
1175         job->start_page = start_page;
1176         job->current_page = start_page;
1177         job->n_pages = n_pages;
1178         job->pages = g_new0 (GList *, n_pages);
1179         job->text = g_strdup (text);
1180         job->case_sensitive = case_sensitive;
1181         job->has_results = FALSE;
1182
1183         return EV_JOB (job);
1184 }
1185
1186 gint
1187 ev_job_find_get_n_results (EvJobFind *job,
1188                            gint       page)
1189 {
1190         return g_list_length (job->pages[page]);
1191 }
1192
1193 gdouble
1194 ev_job_find_get_progress (EvJobFind *job)
1195 {
1196         gint pages_done;
1197
1198         if (ev_job_is_finished (EV_JOB (job)))
1199                 return 1.0;
1200         
1201         if (job->current_page > job->start_page) {
1202                 pages_done = job->current_page - job->start_page + 1;
1203         } else if (job->current_page == job->start_page) {
1204                 pages_done = job->n_pages;
1205         } else {
1206                 pages_done = job->n_pages - job->start_page + job->current_page;
1207         }
1208
1209         return pages_done / (gdouble) job->n_pages;
1210 }
1211
1212 gboolean
1213 ev_job_find_has_results (EvJobFind *job)
1214 {
1215         return job->has_results;
1216 }
1217
1218 GList **
1219 ev_job_find_get_results (EvJobFind *job)
1220 {
1221         return job->pages;
1222 }
1223
1224 /* EvJobLayers */
1225 static void
1226 ev_job_layers_init (EvJobLayers *job)
1227 {
1228         EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1229 }
1230
1231 static void
1232 ev_job_layers_dispose (GObject *object)
1233 {
1234         EvJobLayers *job;
1235
1236         ev_debug_message (DEBUG_JOBS, NULL);
1237         
1238         job = EV_JOB_LAYERS (object);
1239
1240         if (job->model) {
1241                 g_object_unref (job->model);
1242                 job->model = NULL;
1243         }
1244
1245         (* G_OBJECT_CLASS (ev_job_layers_parent_class)->dispose) (object);
1246 }
1247
1248 static gboolean
1249 ev_job_layers_run (EvJob *job)
1250 {
1251         EvJobLayers *job_layers = EV_JOB_LAYERS (job);
1252
1253         ev_debug_message (DEBUG_JOBS, NULL);
1254         ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1255         
1256         ev_document_doc_mutex_lock ();
1257         job_layers->model = ev_document_layers_get_layers (EV_DOCUMENT_LAYERS (job->document));
1258         ev_document_doc_mutex_unlock ();
1259         
1260         ev_job_succeeded (job);
1261         
1262         return FALSE;
1263 }
1264
1265 static void
1266 ev_job_layers_class_init (EvJobLayersClass *class)
1267 {
1268         GObjectClass *oclass = G_OBJECT_CLASS (class);
1269         EvJobClass   *job_class = EV_JOB_CLASS (class);
1270
1271         oclass->dispose = ev_job_layers_dispose;
1272         job_class->run = ev_job_layers_run;
1273 }
1274
1275 EvJob *
1276 ev_job_layers_new (EvDocument *document)
1277 {
1278         EvJob *job;
1279
1280         ev_debug_message (DEBUG_JOBS, NULL);
1281
1282         job = g_object_new (EV_TYPE_JOB_LAYERS, NULL);
1283         job->document = g_object_ref (document);
1284         
1285         return job;
1286 }
1287
1288 /* EvJobExport */
1289 static void
1290 ev_job_export_init (EvJobExport *job)
1291 {
1292         EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1293         job->page = -1;
1294 }
1295
1296 static void
1297 ev_job_export_dispose (GObject *object)
1298 {
1299         EvJobExport *job;
1300
1301         ev_debug_message (DEBUG_JOBS, NULL);
1302         
1303         job = EV_JOB_EXPORT (object);
1304
1305         if (job->rc) {
1306                 g_object_unref (job->rc);
1307                 job->rc = NULL;
1308         }
1309
1310         (* G_OBJECT_CLASS (ev_job_export_parent_class)->dispose) (object);
1311 }
1312
1313 static gboolean
1314 ev_job_export_run (EvJob *job)
1315 {
1316         EvJobExport *job_export = EV_JOB_EXPORT (job);
1317         EvPage      *ev_page;
1318
1319         g_assert (job_export->page != -1);
1320
1321         ev_debug_message (DEBUG_JOBS, NULL);
1322         ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1323         
1324         ev_document_doc_mutex_lock ();
1325         
1326         ev_page = ev_document_get_page (job->document, job_export->page);
1327         if (job_export->rc) {
1328                 job->failed = FALSE;
1329                 job->finished = FALSE;
1330                 g_clear_error (&job->error);
1331                 
1332                 ev_render_context_set_page (job_export->rc, ev_page);
1333         } else {
1334                 job_export->rc = ev_render_context_new (ev_page, 0, 1.0);
1335         }
1336         g_object_unref (ev_page);
1337         
1338         ev_file_exporter_do_page (EV_FILE_EXPORTER (job->document), job_export->rc);
1339         
1340         ev_document_doc_mutex_unlock ();
1341         
1342         ev_job_succeeded (job);
1343         
1344         return FALSE;
1345 }
1346
1347 static void
1348 ev_job_export_class_init (EvJobExportClass *class)
1349 {
1350         GObjectClass *oclass = G_OBJECT_CLASS (class);
1351         EvJobClass   *job_class = EV_JOB_CLASS (class);
1352
1353         oclass->dispose = ev_job_export_dispose;
1354         job_class->run = ev_job_export_run;
1355 }
1356
1357 EvJob *
1358 ev_job_export_new (EvDocument *document)
1359 {
1360         EvJob *job;
1361
1362         ev_debug_message (DEBUG_JOBS, NULL);
1363
1364         job = g_object_new (EV_TYPE_JOB_EXPORT, NULL);
1365         job->document = g_object_ref (document);
1366         
1367         return job;
1368 }
1369
1370 void
1371 ev_job_export_set_page (EvJobExport *job,
1372                         gint         page)
1373 {
1374         job->page = page;
1375 }
1376
1377 /* EvJobPrint */
1378 static void
1379 ev_job_print_init (EvJobPrint *job)
1380 {
1381         EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1382         job->page = -1;
1383 }
1384
1385 static void
1386 ev_job_print_dispose (GObject *object)
1387 {
1388         EvJobPrint *job;
1389
1390         ev_debug_message (DEBUG_JOBS, NULL);
1391
1392         job = EV_JOB_PRINT (object);
1393
1394         if (job->cr) {
1395                 cairo_destroy (job->cr);
1396                 job->cr = NULL;
1397         }
1398
1399         (* G_OBJECT_CLASS (ev_job_print_parent_class)->dispose) (object);
1400 }
1401
1402 static gboolean
1403 ev_job_print_run (EvJob *job)
1404 {
1405         EvJobPrint     *job_print = EV_JOB_PRINT (job);
1406         EvPage         *ev_page;
1407         cairo_status_t  cr_status;
1408
1409         g_assert (job_print->page != -1);
1410         g_assert (job_print->cr != NULL);
1411
1412         ev_debug_message (DEBUG_JOBS, NULL);
1413         ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1414
1415         job->failed = FALSE;
1416         job->finished = FALSE;
1417         g_clear_error (&job->error);
1418
1419         ev_document_doc_mutex_lock ();
1420
1421         ev_page = ev_document_get_page (job->document, job_print->page);
1422         ev_document_print_print_page (EV_DOCUMENT_PRINT (job->document),
1423                                       ev_page, job_print->cr);
1424         g_object_unref (ev_page);
1425
1426         ev_document_doc_mutex_unlock ();
1427
1428         cr_status = cairo_status (job_print->cr);
1429         if (cr_status == CAIRO_STATUS_SUCCESS) {
1430                 ev_job_succeeded (job);
1431         } else {
1432                 ev_job_failed (job,
1433                                GTK_PRINT_ERROR,
1434                                GTK_PRINT_ERROR_GENERAL,
1435                                _("Failed to print page %d: %s"),
1436                                job_print->page,
1437                                cairo_status_to_string (cr_status));
1438         }
1439
1440         return FALSE;
1441 }
1442
1443 static void
1444 ev_job_print_class_init (EvJobPrintClass *class)
1445 {
1446         GObjectClass *oclass = G_OBJECT_CLASS (class);
1447         EvJobClass   *job_class = EV_JOB_CLASS (class);
1448
1449         oclass->dispose = ev_job_print_dispose;
1450         job_class->run = ev_job_print_run;
1451 }
1452
1453 EvJob *
1454 ev_job_print_new (EvDocument *document)
1455 {
1456         EvJob *job;
1457
1458         ev_debug_message (DEBUG_JOBS, NULL);
1459
1460         job = g_object_new (EV_TYPE_JOB_PRINT, NULL);
1461         job->document = g_object_ref (document);
1462
1463         return job;
1464 }
1465
1466 void
1467 ev_job_print_set_page (EvJobPrint *job,
1468                        gint        page)
1469 {
1470         job->page = page;
1471 }
1472
1473 void
1474 ev_job_print_set_cairo (EvJobPrint *job,
1475                         cairo_t    *cr)
1476 {
1477         if (job->cr == cr)
1478                 return;
1479
1480         if (job->cr)
1481                 cairo_destroy (job->cr);
1482         job->cr = cr ? cairo_reference (cr) : NULL;
1483 }