1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
4 * Copyright (C) 2005 Red Hat, Inc
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.
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.
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.
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"
37 #include <glib/gstdio.h>
38 #include <glib/gi18n.h>
41 static void ev_job_init (EvJob *job);
42 static void ev_job_class_init (EvJobClass *class);
43 static void ev_job_links_init (EvJobLinks *job);
44 static void ev_job_links_class_init (EvJobLinksClass *class);
45 static void ev_job_attachments_init (EvJobAttachments *job);
46 static void ev_job_attachments_class_init (EvJobAttachmentsClass *class);
47 static void ev_job_render_init (EvJobRender *job);
48 static void ev_job_render_class_init (EvJobRenderClass *class);
49 static void ev_job_thumbnail_init (EvJobThumbnail *job);
50 static void ev_job_thumbnail_class_init (EvJobThumbnailClass *class);
51 static void ev_job_load_init (EvJobLoad *job);
52 static void ev_job_load_class_init (EvJobLoadClass *class);
53 static void ev_job_save_init (EvJobSave *job);
54 static void ev_job_save_class_init (EvJobSaveClass *class);
55 static void ev_job_print_init (EvJobPrint *job);
56 static void ev_job_print_class_init (EvJobPrintClass *class);
74 static guint job_signals[LAST_SIGNAL] = { 0 };
75 static guint job_render_signals[RENDER_LAST_SIGNAL] = { 0 };
76 static guint job_fonts_signals[FONTS_LAST_SIGNAL] = { 0 };
78 G_DEFINE_ABSTRACT_TYPE (EvJob, ev_job, G_TYPE_OBJECT)
79 G_DEFINE_TYPE (EvJobLinks, ev_job_links, EV_TYPE_JOB)
80 G_DEFINE_TYPE (EvJobAttachments, ev_job_attachments, EV_TYPE_JOB)
81 G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB)
82 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
83 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
84 G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB)
85 G_DEFINE_TYPE (EvJobSave, ev_job_save, EV_TYPE_JOB)
86 G_DEFINE_TYPE (EvJobPrint, ev_job_print, EV_TYPE_JOB)
90 ev_job_init (EvJob *job)
92 job->cancellable = g_cancellable_new ();
96 ev_job_dispose (GObject *object)
100 job = EV_JOB (object);
103 g_object_unref (job->document);
104 job->document = NULL;
107 if (job->cancellable) {
108 g_object_unref (job->cancellable);
109 job->cancellable = NULL;
113 g_error_free (job->error);
117 (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object);
121 ev_job_class_init (EvJobClass *class)
123 GObjectClass *oclass;
125 oclass = G_OBJECT_CLASS (class);
127 oclass->dispose = ev_job_dispose;
129 job_signals[CANCELLED] =
130 g_signal_new ("cancelled",
133 G_STRUCT_OFFSET (EvJobClass, cancelled),
135 g_cclosure_marshal_VOID__VOID,
137 job_signals [FINISHED] =
138 g_signal_new ("finished",
141 G_STRUCT_OFFSET (EvJobClass, finished),
143 g_cclosure_marshal_VOID__VOID,
148 emit_finished (EvJob *job)
150 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
152 job->idle_finished_id = 0;
154 if (job->cancelled) {
155 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit finished", EV_GET_TYPE_NAME (job), job);
157 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
158 g_signal_emit (job, job_signals[FINISHED], 0);
165 ev_job_emit_finished (EvJob *job)
167 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
169 if (g_cancellable_is_cancelled (job->cancellable)) {
170 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, returning", EV_GET_TYPE_NAME (job), job);
174 job->finished = TRUE;
176 if (job->run_mode == EV_JOB_RUN_THREAD) {
177 job->idle_finished_id =
178 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
179 (GSourceFunc)emit_finished,
181 (GDestroyNotify)g_object_unref);
183 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
184 g_signal_emit (job, job_signals[FINISHED], 0);
189 ev_job_run (EvJob *job)
191 EvJobClass *class = EV_JOB_GET_CLASS (job);
193 return class->run (job);
197 ev_job_cancel (EvJob *job)
199 if (job->cancelled || (job->finished && job->idle_finished_id == 0))
202 ev_debug_message (DEBUG_JOBS, "job %s (%p) cancelled", EV_GET_TYPE_NAME (job), job);
203 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
205 /* This should never be called from a thread */
206 job->cancelled = TRUE;
207 g_cancellable_cancel (job->cancellable);
208 g_signal_emit (job, job_signals[CANCELLED], 0);
212 ev_job_failed (EvJob *job,
221 if (job->failed || job->finished)
224 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
228 va_start (args, format);
229 message = g_strdup_vprintf (format, args);
232 job->error = g_error_new (domain, code, message);
235 ev_job_emit_finished (job);
239 ev_job_failed_from_error (EvJob *job,
242 if (job->failed || job->finished)
245 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
248 job->error = g_error_copy (error);
250 ev_job_emit_finished (job);
254 ev_job_succeeded (EvJob *job)
259 ev_debug_message (DEBUG_JOBS, "job %s (%p) succeeded", EV_GET_TYPE_NAME (job), job);
262 ev_job_emit_finished (job);
266 ev_job_is_finished (EvJob *job)
268 return job->finished;
272 ev_job_is_failed (EvJob *job)
278 ev_job_get_run_mode (EvJob *job)
280 return job->run_mode;
284 ev_job_set_run_mode (EvJob *job,
285 EvJobRunMode run_mode)
287 job->run_mode = run_mode;
292 ev_job_links_init (EvJobLinks *job)
294 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
298 ev_job_links_dispose (GObject *object)
302 ev_debug_message (DEBUG_JOBS, NULL);
304 job = EV_JOB_LINKS (object);
307 g_object_unref (job->model);
311 (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object);
315 ev_job_links_run (EvJob *job)
317 EvJobLinks *job_links = EV_JOB_LINKS (job);
319 ev_debug_message (DEBUG_JOBS, NULL);
320 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
322 ev_document_doc_mutex_lock ();
323 job_links->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (job->document));
324 ev_document_doc_mutex_unlock ();
326 ev_job_succeeded (job);
332 ev_job_links_class_init (EvJobLinksClass *class)
334 GObjectClass *oclass = G_OBJECT_CLASS (class);
335 EvJobClass *job_class = EV_JOB_CLASS (class);
337 oclass->dispose = ev_job_links_dispose;
338 job_class->run = ev_job_links_run;
342 ev_job_links_new (EvDocument *document)
346 ev_debug_message (DEBUG_JOBS, NULL);
348 job = g_object_new (EV_TYPE_JOB_LINKS, NULL);
349 job->document = g_object_ref (document);
354 /* EvJobAttachments */
356 ev_job_attachments_init (EvJobAttachments *job)
358 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
362 ev_job_attachments_dispose (GObject *object)
364 EvJobAttachments *job;
366 ev_debug_message (DEBUG_JOBS, NULL);
368 job = EV_JOB_ATTACHMENTS (object);
370 if (job->attachments) {
371 g_list_foreach (job->attachments, (GFunc)g_object_unref, NULL);
372 g_list_free (job->attachments);
373 job->attachments = NULL;
376 (* G_OBJECT_CLASS (ev_job_attachments_parent_class)->dispose) (object);
380 ev_job_attachments_run (EvJob *job)
382 EvJobAttachments *job_attachments = EV_JOB_ATTACHMENTS (job);
384 ev_debug_message (DEBUG_JOBS, NULL);
385 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
387 ev_document_doc_mutex_lock ();
388 job_attachments->attachments = ev_document_get_attachments (job->document);
389 ev_document_doc_mutex_unlock ();
391 ev_job_succeeded (job);
397 ev_job_attachments_class_init (EvJobAttachmentsClass *class)
399 GObjectClass *oclass = G_OBJECT_CLASS (class);
400 EvJobClass *job_class = EV_JOB_CLASS (class);
402 oclass->dispose = ev_job_attachments_dispose;
403 job_class->run = ev_job_attachments_run;
407 ev_job_attachments_new (EvDocument *document)
411 ev_debug_message (DEBUG_JOBS, NULL);
413 job = g_object_new (EV_TYPE_JOB_ATTACHMENTS, NULL);
414 job->document = g_object_ref (document);
421 ev_job_render_init (EvJobRender *job)
423 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
427 ev_job_render_dispose (GObject *object)
431 job = EV_JOB_RENDER (object);
434 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job->ev_page->index, job);
435 g_object_unref (job->ev_page);
440 cairo_surface_destroy (job->surface);
444 if (job->selection) {
445 cairo_surface_destroy (job->selection);
446 job->selection = NULL;
449 if (job->selection_region) {
450 gdk_region_destroy (job->selection_region);
451 job->selection_region = NULL;
454 (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
458 notify_page_ready (EvJobRender *job)
460 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->ev_page->index, job);
461 ev_profiler_stop (EV_PROFILE_JOBS, "Rendering page %d", job->ev_page->index);
463 if (EV_JOB (job)->cancelled) {
464 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit page_ready", EV_GET_TYPE_NAME (job), job);
466 g_signal_emit (job, job_render_signals[PAGE_READY], 0);
473 ev_job_render_page_ready (EvJobRender *job)
475 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->ev_page->index, job);
477 job->page_ready = TRUE;
478 g_idle_add_full (G_PRIORITY_HIGH_IDLE,
479 (GSourceFunc)notify_page_ready,
481 (GDestroyNotify)g_object_unref);
485 ev_job_render_run (EvJob *job)
487 EvJobRender *job_render = EV_JOB_RENDER (job);
490 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_render->page, job);
491 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
493 ev_document_doc_mutex_lock ();
495 ev_profiler_start (EV_PROFILE_JOBS, "Rendering page %d", job_render->page);
497 ev_document_fc_mutex_lock ();
499 job_render->ev_page = ev_document_get_page (job->document, job_render->page);
500 rc = ev_render_context_new (job_render->ev_page, job_render->rotation, job_render->scale);
502 job_render->surface = ev_document_render (job->document, rc);
503 /* If job was cancelled during the page rendering,
504 * we return now, so that the thread is finished ASAP
506 if (g_cancellable_is_cancelled (job->cancellable)) {
507 ev_document_fc_mutex_unlock ();
508 ev_document_doc_mutex_unlock ();
514 if ((job_render->flags & EV_RENDER_INCLUDE_SELECTION) && EV_IS_SELECTION (job->document)) {
515 ev_selection_render_selection (EV_SELECTION (job->document),
517 &(job_render->selection),
518 &(job_render->selection_points),
520 job_render->selection_style,
521 &(job_render->text), &(job_render->base));
522 job_render->selection_region =
523 ev_selection_get_selection_region (EV_SELECTION (job->document),
525 job_render->selection_style,
526 &(job_render->selection_points));
529 ev_job_render_page_ready (job_render);
531 ev_document_fc_mutex_unlock ();
533 if ((job_render->flags & EV_RENDER_INCLUDE_TEXT) && EV_IS_SELECTION (job->document))
534 job_render->text_mapping =
535 ev_selection_get_selection_map (EV_SELECTION (job->document), rc);
536 if ((job_render->flags & EV_RENDER_INCLUDE_LINKS) && EV_IS_DOCUMENT_LINKS (job->document))
537 job_render->link_mapping =
538 ev_document_links_get_links (EV_DOCUMENT_LINKS (job->document), job_render->page);
539 if ((job_render->flags & EV_RENDER_INCLUDE_FORMS) && EV_IS_DOCUMENT_FORMS (job->document))
540 job_render->form_field_mapping =
541 ev_document_forms_get_form_fields (EV_DOCUMENT_FORMS (job->document),
542 job_render->ev_page);
543 if ((job_render->flags & EV_RENDER_INCLUDE_IMAGES) && EV_IS_DOCUMENT_IMAGES (job->document))
544 job_render->image_mapping =
545 ev_document_images_get_image_mapping (EV_DOCUMENT_IMAGES (job->document),
548 ev_document_doc_mutex_unlock ();
550 ev_job_succeeded (job);
556 ev_job_render_class_init (EvJobRenderClass *class)
558 GObjectClass *oclass = G_OBJECT_CLASS (class);
559 EvJobClass *job_class = EV_JOB_CLASS (class);
561 job_render_signals [PAGE_READY] =
562 g_signal_new ("page-ready",
565 G_STRUCT_OFFSET (EvJobRenderClass, page_ready),
567 g_cclosure_marshal_VOID__VOID,
570 oclass->dispose = ev_job_render_dispose;
571 job_class->run = ev_job_render_run;
575 ev_job_render_new (EvDocument *document,
585 ev_debug_message (DEBUG_JOBS, "page: %d", page);
587 job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
589 EV_JOB (job)->document = g_object_ref (document);
591 job->rotation = rotation;
593 job->target_width = width;
594 job->target_height = height;
601 ev_job_render_set_selection_info (EvJobRender *job,
602 EvRectangle *selection_points,
603 EvSelectionStyle selection_style,
607 job->flags |= EV_RENDER_INCLUDE_SELECTION;
609 job->selection_points = *selection_points;
610 job->selection_style = selection_style;
617 ev_job_thumbnail_init (EvJobThumbnail *job)
619 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
623 ev_job_thumbnail_dispose (GObject *object)
627 job = EV_JOB_THUMBNAIL (object);
629 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->page, job);
631 if (job->thumbnail) {
632 g_object_unref (job->thumbnail);
633 job->thumbnail = NULL;
636 (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
640 ev_job_thumbnail_run (EvJob *job)
642 EvJobThumbnail *job_thumb = EV_JOB_THUMBNAIL (job);
646 ev_debug_message (DEBUG_JOBS, "%d (%p)", job_thumb->page, job);
647 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
649 ev_document_doc_mutex_lock ();
651 page = ev_document_get_page (job->document, job_thumb->page);
652 rc = ev_render_context_new (page, job_thumb->rotation, job_thumb->scale);
653 g_object_unref (page);
655 job_thumb->thumbnail = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (job->document),
658 ev_document_doc_mutex_unlock ();
660 ev_job_succeeded (job);
666 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
668 GObjectClass *oclass = G_OBJECT_CLASS (class);
669 EvJobClass *job_class = EV_JOB_CLASS (class);
671 oclass->dispose = ev_job_thumbnail_dispose;
672 job_class->run = ev_job_thumbnail_run;
676 ev_job_thumbnail_new (EvDocument *document,
683 ev_debug_message (DEBUG_JOBS, "%d", page);
685 job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
687 EV_JOB (job)->document = g_object_ref (document);
689 job->rotation = rotation;
697 ev_job_fonts_init (EvJobFonts *job)
699 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
703 ev_job_fonts_run (EvJob *job)
705 EvJobFonts *job_fonts = EV_JOB_FONTS (job);
706 EvDocumentFonts *fonts = EV_DOCUMENT_FONTS (job->document);
708 ev_debug_message (DEBUG_JOBS, NULL);
710 /* Do not block the main loop */
711 if (!ev_document_doc_mutex_trylock ())
714 if (!ev_document_fc_mutex_trylock ())
717 #ifdef EV_ENABLE_DEBUG
718 /* We use the #ifdef in this case because of the if */
719 if (ev_document_fonts_get_progress (fonts) == 0)
720 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
723 job_fonts->scan_completed = !ev_document_fonts_scan (fonts, 20);
724 g_signal_emit (job_fonts, job_fonts_signals[UPDATED], 0,
725 ev_document_fonts_get_progress (fonts));
727 ev_document_fc_mutex_unlock ();
728 ev_document_doc_mutex_unlock ();
730 if (job_fonts->scan_completed)
731 ev_job_succeeded (job);
733 return !job_fonts->scan_completed;
737 ev_job_fonts_class_init (EvJobFontsClass *class)
739 EvJobClass *job_class = EV_JOB_CLASS (class);
741 job_class->run = ev_job_fonts_run;
743 job_fonts_signals[UPDATED] =
744 g_signal_new ("updated",
747 G_STRUCT_OFFSET (EvJobFontsClass, updated),
749 g_cclosure_marshal_VOID__DOUBLE,
755 ev_job_fonts_new (EvDocument *document)
759 ev_debug_message (DEBUG_JOBS, NULL);
761 job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
763 EV_JOB (job)->document = g_object_ref (document);
770 ev_job_load_init (EvJobLoad *job)
772 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
776 ev_job_load_dispose (GObject *object)
778 EvJobLoad *job = EV_JOB_LOAD (object);
780 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
788 g_free (job->password);
789 job->password = NULL;
793 g_object_unref (job->dest);
797 if (job->search_string) {
798 g_free (job->search_string);
799 job->search_string = NULL;
802 (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object);
806 ev_job_load_run (EvJob *job)
808 EvJobLoad *job_load = EV_JOB_LOAD (job);
809 GError *error = NULL;
811 ev_debug_message (DEBUG_JOBS, "%s", job_load->uri);
812 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
814 ev_document_fc_mutex_lock ();
816 /* This job may already have a document even if the job didn't complete
817 because, e.g., a password is required - if so, just reload rather than
818 creating a new instance */
820 if (job_load->password) {
821 ev_document_security_set_password (EV_DOCUMENT_SECURITY (job->document),
826 job->finished = FALSE;
827 g_clear_error (&job->error);
829 ev_document_load (job->document,
833 job->document = ev_document_factory_get_document (job_load->uri,
837 ev_document_fc_mutex_unlock ();
840 ev_job_failed_from_error (job, error);
841 g_error_free (error);
843 ev_job_succeeded (job);
850 ev_job_load_class_init (EvJobLoadClass *class)
852 GObjectClass *oclass = G_OBJECT_CLASS (class);
853 EvJobClass *job_class = EV_JOB_CLASS (class);
855 oclass->dispose = ev_job_load_dispose;
856 job_class->run = ev_job_load_run;
860 ev_job_load_new (const gchar *uri,
862 EvWindowRunMode mode,
863 const gchar *search_string)
867 ev_debug_message (DEBUG_JOBS, "%s", uri);
869 job = g_object_new (EV_TYPE_JOB_LOAD, NULL);
871 job->uri = g_strdup (uri);
872 job->dest = dest ? g_object_ref (dest) : NULL;
874 job->search_string = search_string ? g_strdup (search_string) : NULL;
880 ev_job_load_set_uri (EvJobLoad *job, const gchar *uri)
882 ev_debug_message (DEBUG_JOBS, "%s", uri);
886 job->uri = g_strdup (uri);
890 ev_job_load_set_password (EvJobLoad *job, const gchar *password)
892 ev_debug_message (DEBUG_JOBS, NULL);
895 g_free (job->password);
896 job->password = password ? g_strdup (password) : NULL;
901 ev_job_save_init (EvJobSave *job)
903 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
907 ev_job_save_dispose (GObject *object)
909 EvJobSave *job = EV_JOB_SAVE (object);
911 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
918 if (job->document_uri) {
919 g_free (job->document_uri);
920 job->document_uri = NULL;
923 (* G_OBJECT_CLASS (ev_job_save_parent_class)->dispose) (object);
927 ev_job_save_run (EvJob *job)
929 EvJobSave *job_save = EV_JOB_SAVE (job);
934 GError *error = NULL;
936 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", job_save->uri, job_save->document_uri);
937 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
939 filename = ev_tmp_filename ("saveacopy");
940 tmp_filename = g_strdup_printf ("%s.XXXXXX", filename);
943 fd = g_mkstemp (tmp_filename);
946 gint save_errno = errno;
948 display_name = g_filename_display_name (tmp_filename);
951 g_file_error_from_errno (save_errno),
952 _("Failed to create file “%s”: %s"),
953 display_name, g_strerror (save_errno));
954 g_free (display_name);
955 g_free (tmp_filename);
960 ev_document_doc_mutex_lock ();
962 /* Save document to temp filename */
963 local_uri = g_filename_to_uri (tmp_filename, NULL, NULL);
964 ev_document_save (job->document, local_uri, &error);
967 ev_document_doc_mutex_unlock ();
971 ev_job_failed_from_error (job, error);
972 g_error_free (error);
977 /* If original document was compressed,
978 * compress it again before saving
980 if (g_object_get_data (G_OBJECT (job->document), "uri-uncompressed")) {
981 EvCompressionType ctype = EV_COMPRESSION_NONE;
985 ext = g_strrstr (job_save->document_uri, ".gz");
986 if (ext && g_ascii_strcasecmp (ext, ".gz") == 0)
987 ctype = EV_COMPRESSION_GZIP;
989 ext = g_strrstr (job_save->document_uri, ".bz2");
990 if (ext && g_ascii_strcasecmp (ext, ".bz2") == 0)
991 ctype = EV_COMPRESSION_BZIP2;
993 uri_comp = ev_file_compress (local_uri, ctype, &error);
995 ev_tmp_filename_unlink (tmp_filename);
997 if (!uri_comp || error) {
1000 local_uri = uri_comp;
1004 g_free (tmp_filename);
1008 ev_job_failed_from_error (job, error);
1009 g_error_free (error);
1017 ev_xfer_uri_simple (local_uri, job_save->uri, &error);
1018 ev_tmp_uri_unlink (local_uri);
1021 ev_job_failed_from_error (job, error);
1022 g_error_free (error);
1024 ev_job_succeeded (job);
1031 ev_job_save_class_init (EvJobSaveClass *class)
1033 GObjectClass *oclass = G_OBJECT_CLASS (class);
1034 EvJobClass *job_class = EV_JOB_CLASS (class);
1036 oclass->dispose = ev_job_save_dispose;
1037 job_class->run = ev_job_save_run;
1041 ev_job_save_new (EvDocument *document,
1043 const gchar *document_uri)
1047 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", uri, document_uri);
1049 job = g_object_new (EV_TYPE_JOB_SAVE, NULL);
1051 EV_JOB (job)->document = g_object_ref (document);
1052 job->uri = g_strdup (uri);
1053 job->document_uri = g_strdup (document_uri);
1055 return EV_JOB (job);
1060 ev_job_print_init (EvJobPrint *job)
1062 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1066 ev_job_print_dispose (GObject *object)
1070 job = EV_JOB_PRINT (object);
1072 ev_debug_message (DEBUG_JOBS, NULL);
1074 if (job->temp_file) {
1075 g_unlink (job->temp_file);
1076 g_free (job->temp_file);
1077 job->temp_file = NULL;
1081 g_free (job->ranges);
1086 (* G_OBJECT_CLASS (ev_job_print_parent_class)->dispose) (object);
1090 ev_print_job_get_first_page (EvJobPrint *job)
1093 gint first_page = G_MAXINT;
1095 if (job->n_ranges == 0)
1098 for (i = 0; i < job->n_ranges; i++) {
1099 if (job->ranges[i].start < first_page)
1100 first_page = job->ranges[i].start;
1103 return MAX (0, first_page);
1107 ev_print_job_get_last_page (EvJobPrint *job)
1110 gint last_page = G_MININT;
1113 max_page = ev_document_get_n_pages (EV_JOB (job)->document) - 1;
1115 if (job->n_ranges == 0)
1118 for (i = 0; i < job->n_ranges; i++) {
1119 if (job->ranges[i].end > last_page)
1120 last_page = job->ranges[i].end;
1123 return MIN (max_page, last_page);
1127 ev_print_job_print_page_in_set (EvJobPrint *job,
1130 switch (job->page_set) {
1131 case EV_PRINT_PAGE_SET_EVEN:
1132 return page % 2 == 0;
1133 case EV_PRINT_PAGE_SET_ODD:
1134 return page % 2 != 0;
1135 case EV_PRINT_PAGE_SET_ALL:
1143 ev_job_print_get_page_list (EvJobPrint *job,
1146 gint i, j, page, max_page;
1150 max_page = ev_document_get_n_pages (EV_JOB (job)->document) - 1;
1152 for (i = 0; i < job->n_ranges; i++) {
1156 if (job->ranges[i].start == -1)
1157 job->ranges[i].start = 0;
1158 if (job->ranges[i].end == -1)
1159 job->ranges[i].end = max_page;
1161 if (job->ranges[i].start > max_page)
1164 start = job->ranges[i].start + 1;
1165 end = job->ranges[i].end <= max_page ? job->ranges[i].end + 1 : max_page + 1;
1166 rsize = end - start + 1;
1168 switch (job->page_set) {
1169 case EV_PRINT_PAGE_SET_EVEN:
1170 pages += start % 2 == 0 ? (rsize / 2) + (rsize % 2) : (rsize / 2);
1172 case EV_PRINT_PAGE_SET_ODD:
1173 pages += start % 2 != 0 ? (rsize / 2) + (rsize % 2) : (rsize / 2);
1186 page_list = g_new (gint, pages);
1189 for (i = 0; i < job->n_ranges; i++) {
1190 for (j = job->ranges[i].start; j <= job->ranges[i].end; j++) {
1194 if (ev_print_job_print_page_in_set (job, j + 1))
1195 page_list[page++] = j;
1203 ev_job_print_run (EvJob *job)
1205 EvDocument *document = EV_JOB (job)->document;
1206 EvJobPrint *job_print = EV_JOB_PRINT (job);
1207 EvFileExporterContext fc;
1208 EvRenderContext *rc;
1216 GError *error = NULL;
1218 ev_debug_message (DEBUG_JOBS, NULL);
1219 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1221 if (job_print->temp_file)
1222 g_free (job_print->temp_file);
1223 job_print->temp_file = NULL;
1225 filename = g_strdup_printf ("evince_print.%s.XXXXXX", job_print->format);
1226 fd = g_file_open_tmp (filename, &job_print->temp_file, &error);
1229 ev_job_failed_from_error (job, error);
1230 g_error_free (error);
1235 page_list = ev_job_print_get_page_list (job_print, &n_pages);
1239 ev_job_succeeded (job);
1244 first_page = ev_print_job_get_first_page (job_print);
1245 last_page = ev_print_job_get_last_page (job_print);
1247 fc.format = g_ascii_strcasecmp (job_print->format, "pdf") == 0 ?
1248 EV_FILE_FORMAT_PDF : EV_FILE_FORMAT_PS;
1249 fc.filename = job_print->temp_file;
1250 fc.first_page = MIN (first_page, last_page);
1251 fc.last_page = MAX (first_page, last_page);
1252 fc.paper_width = job_print->width;
1253 fc.paper_height = job_print->height;
1255 fc.pages_per_sheet = MAX (1, job_print->pages_per_sheet);
1257 rc = ev_render_context_new (NULL, 0, 1.0);
1259 ev_document_doc_mutex_lock ();
1260 ev_file_exporter_begin (EV_FILE_EXPORTER (document), &fc);
1262 for (i = 0; i < job_print->copies; i++) {
1266 step = job_print->reverse ? -1 * job_print->pages_per_sheet : job_print->pages_per_sheet;
1267 page = job_print->reverse ? ((n_pages - 1) / job_print->pages_per_sheet) * job_print->pages_per_sheet : 0;
1268 n_copies = job_print->collate ? 1 : job_print->copies;
1270 while ((job_print->reverse && (page >= 0)) || (!job_print->reverse && (page < n_pages))) {
1273 for (k = 0; k < n_copies; k++) {
1274 ev_file_exporter_begin_page (EV_FILE_EXPORTER (document));
1276 for (j = 0; j < job_print->pages_per_sheet; j++) {
1281 if (p < 0 || p >= n_pages)
1284 ev_page = ev_document_get_page (document, page_list[p]);
1285 ev_render_context_set_page (rc, ev_page);
1286 g_object_unref (ev_page);
1288 ev_file_exporter_do_page (EV_FILE_EXPORTER (document), rc);
1291 ev_file_exporter_end_page (EV_FILE_EXPORTER (document));
1297 if (!job_print->collate)
1301 ev_file_exporter_end (EV_FILE_EXPORTER (document));
1302 ev_document_doc_mutex_unlock ();
1306 g_object_unref (rc);
1308 ev_job_succeeded (job);
1314 ev_job_print_class_init (EvJobPrintClass *class)
1316 GObjectClass *oclass = G_OBJECT_CLASS (class);
1317 EvJobClass *job_class = EV_JOB_CLASS (class);
1319 oclass->dispose = ev_job_print_dispose;
1320 job_class->run = ev_job_print_run;
1324 ev_job_print_new (EvDocument *document,
1325 const gchar *format,
1328 EvPrintRange *ranges,
1330 EvPrintPageSet page_set,
1331 gint pages_per_sheet,
1338 ev_debug_message (DEBUG_JOBS, "format: %s, width: %f, height:%f,"
1339 "n_ranges: %d, pages_per_sheet: %d, copies: %d,"
1340 "collate: %s, reverse: %s",
1341 format, width, height, n_ranges, pages_per_sheet, copies,
1342 collate ? "True" : "False", reverse ? "True" : "False");
1344 job = g_object_new (EV_TYPE_JOB_PRINT, NULL);
1346 EV_JOB (job)->document = g_object_ref (document);
1348 job->format = format;
1350 job->temp_file = NULL;
1353 job->height = height;
1355 job->ranges = ranges;
1356 job->n_ranges = n_ranges;
1358 job->page_set = page_set;
1360 job->pages_per_sheet = CLAMP (pages_per_sheet, 1, 16);
1362 job->copies = copies;
1363 job->collate = collate;
1364 job->reverse = reverse;
1366 return EV_JOB (job);