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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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"
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-document-attachments.h"
39 #include "ev-document-text.h"
43 #include <glib/gstdio.h>
44 #include <glib/gi18n-lib.h>
47 static void ev_job_init (EvJob *job);
48 static void ev_job_class_init (EvJobClass *class);
49 static void ev_job_links_init (EvJobLinks *job);
50 static void ev_job_links_class_init (EvJobLinksClass *class);
51 static void ev_job_attachments_init (EvJobAttachments *job);
52 static void ev_job_attachments_class_init (EvJobAttachmentsClass *class);
53 static void ev_job_annots_init (EvJobAnnots *job);
54 static void ev_job_annots_class_init (EvJobAnnotsClass *class);
55 static void ev_job_render_init (EvJobRender *job);
56 static void ev_job_render_class_init (EvJobRenderClass *class);
57 static void ev_job_page_data_init (EvJobPageData *job);
58 static void ev_job_page_data_class_init (EvJobPageDataClass *class);
59 static void ev_job_thumbnail_init (EvJobThumbnail *job);
60 static void ev_job_thumbnail_class_init (EvJobThumbnailClass *class);
61 static void ev_job_load_init (EvJobLoad *job);
62 static void ev_job_load_class_init (EvJobLoadClass *class);
63 static void ev_job_save_init (EvJobSave *job);
64 static void ev_job_save_class_init (EvJobSaveClass *class);
65 static void ev_job_find_init (EvJobFind *job);
66 static void ev_job_find_class_init (EvJobFindClass *class);
67 static void ev_job_layers_init (EvJobLayers *job);
68 static void ev_job_layers_class_init (EvJobLayersClass *class);
69 static void ev_job_export_init (EvJobExport *job);
70 static void ev_job_export_class_init (EvJobExportClass *class);
71 static void ev_job_print_init (EvJobPrint *job);
72 static void ev_job_print_class_init (EvJobPrintClass *class);
90 static guint job_signals[LAST_SIGNAL] = { 0 };
91 static guint job_fonts_signals[FONTS_LAST_SIGNAL] = { 0 };
92 static guint job_find_signals[FIND_LAST_SIGNAL] = { 0 };
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 (EvJobAnnots, ev_job_annots, EV_TYPE_JOB)
98 G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB)
99 G_DEFINE_TYPE (EvJobPageData, ev_job_page_data, EV_TYPE_JOB)
100 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
101 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
102 G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB)
103 G_DEFINE_TYPE (EvJobSave, ev_job_save, EV_TYPE_JOB)
104 G_DEFINE_TYPE (EvJobFind, ev_job_find, EV_TYPE_JOB)
105 G_DEFINE_TYPE (EvJobLayers, ev_job_layers, EV_TYPE_JOB)
106 G_DEFINE_TYPE (EvJobExport, ev_job_export, EV_TYPE_JOB)
107 G_DEFINE_TYPE (EvJobPrint, ev_job_print, EV_TYPE_JOB)
111 ev_job_init (EvJob *job)
113 job->cancellable = g_cancellable_new ();
117 ev_job_dispose (GObject *object)
121 job = EV_JOB (object);
124 g_object_unref (job->document);
125 job->document = NULL;
128 if (job->cancellable) {
129 g_object_unref (job->cancellable);
130 job->cancellable = NULL;
134 g_error_free (job->error);
138 (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object);
142 ev_job_class_init (EvJobClass *class)
144 GObjectClass *oclass;
146 oclass = G_OBJECT_CLASS (class);
148 oclass->dispose = ev_job_dispose;
150 job_signals[CANCELLED] =
151 g_signal_new ("cancelled",
154 G_STRUCT_OFFSET (EvJobClass, cancelled),
156 g_cclosure_marshal_VOID__VOID,
158 job_signals [FINISHED] =
159 g_signal_new ("finished",
162 G_STRUCT_OFFSET (EvJobClass, finished),
164 g_cclosure_marshal_VOID__VOID,
169 emit_finished (EvJob *job)
171 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
173 job->idle_finished_id = 0;
175 if (job->cancelled) {
176 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit finished", EV_GET_TYPE_NAME (job), job);
178 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
179 g_signal_emit (job, job_signals[FINISHED], 0);
186 ev_job_emit_finished (EvJob *job)
188 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
190 if (g_cancellable_is_cancelled (job->cancellable)) {
191 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, returning", EV_GET_TYPE_NAME (job), job);
195 job->finished = TRUE;
197 if (job->run_mode == EV_JOB_RUN_THREAD) {
198 job->idle_finished_id =
199 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
200 (GSourceFunc)emit_finished,
202 (GDestroyNotify)g_object_unref);
204 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
205 g_signal_emit (job, job_signals[FINISHED], 0);
210 ev_job_run (EvJob *job)
212 EvJobClass *class = EV_JOB_GET_CLASS (job);
214 return class->run (job);
218 ev_job_cancel (EvJob *job)
220 if (job->cancelled || (job->finished && job->idle_finished_id == 0))
223 ev_debug_message (DEBUG_JOBS, "job %s (%p) cancelled", EV_GET_TYPE_NAME (job), job);
224 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
226 /* This should never be called from a thread */
227 job->cancelled = TRUE;
228 g_cancellable_cancel (job->cancellable);
229 g_signal_emit (job, job_signals[CANCELLED], 0);
233 ev_job_failed (EvJob *job,
242 if (job->failed || job->finished)
245 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
249 va_start (args, format);
250 message = g_strdup_vprintf (format, args);
253 job->error = g_error_new_literal (domain, code, message);
256 ev_job_emit_finished (job);
260 ev_job_failed_from_error (EvJob *job,
263 if (job->failed || job->finished)
266 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
269 job->error = g_error_copy (error);
271 ev_job_emit_finished (job);
275 ev_job_succeeded (EvJob *job)
280 ev_debug_message (DEBUG_JOBS, "job %s (%p) succeeded", EV_GET_TYPE_NAME (job), job);
283 ev_job_emit_finished (job);
287 ev_job_is_finished (EvJob *job)
289 return job->finished;
293 ev_job_is_failed (EvJob *job)
299 ev_job_get_run_mode (EvJob *job)
301 return job->run_mode;
305 ev_job_set_run_mode (EvJob *job,
306 EvJobRunMode run_mode)
308 job->run_mode = run_mode;
313 ev_job_links_init (EvJobLinks *job)
315 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
319 ev_job_links_dispose (GObject *object)
323 ev_debug_message (DEBUG_JOBS, NULL);
325 job = EV_JOB_LINKS (object);
328 g_object_unref (job->model);
332 (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object);
336 ev_job_links_run (EvJob *job)
338 EvJobLinks *job_links = EV_JOB_LINKS (job);
340 ev_debug_message (DEBUG_JOBS, NULL);
341 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
343 ev_document_doc_mutex_lock ();
344 job_links->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (job->document));
345 ev_document_doc_mutex_unlock ();
347 ev_job_succeeded (job);
353 ev_job_links_class_init (EvJobLinksClass *class)
355 GObjectClass *oclass = G_OBJECT_CLASS (class);
356 EvJobClass *job_class = EV_JOB_CLASS (class);
358 oclass->dispose = ev_job_links_dispose;
359 job_class->run = ev_job_links_run;
363 ev_job_links_new (EvDocument *document)
367 ev_debug_message (DEBUG_JOBS, NULL);
369 job = g_object_new (EV_TYPE_JOB_LINKS, NULL);
370 job->document = g_object_ref (document);
375 /* EvJobAttachments */
377 ev_job_attachments_init (EvJobAttachments *job)
379 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
383 ev_job_attachments_dispose (GObject *object)
385 EvJobAttachments *job;
387 ev_debug_message (DEBUG_JOBS, NULL);
389 job = EV_JOB_ATTACHMENTS (object);
391 if (job->attachments) {
392 g_list_foreach (job->attachments, (GFunc)g_object_unref, NULL);
393 g_list_free (job->attachments);
394 job->attachments = NULL;
397 (* G_OBJECT_CLASS (ev_job_attachments_parent_class)->dispose) (object);
401 ev_job_attachments_run (EvJob *job)
403 EvJobAttachments *job_attachments = EV_JOB_ATTACHMENTS (job);
405 ev_debug_message (DEBUG_JOBS, NULL);
406 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
408 ev_document_doc_mutex_lock ();
409 job_attachments->attachments =
410 ev_document_attachments_get_attachments (EV_DOCUMENT_ATTACHMENTS (job->document));
411 ev_document_doc_mutex_unlock ();
413 ev_job_succeeded (job);
419 ev_job_attachments_class_init (EvJobAttachmentsClass *class)
421 GObjectClass *oclass = G_OBJECT_CLASS (class);
422 EvJobClass *job_class = EV_JOB_CLASS (class);
424 oclass->dispose = ev_job_attachments_dispose;
425 job_class->run = ev_job_attachments_run;
429 ev_job_attachments_new (EvDocument *document)
433 ev_debug_message (DEBUG_JOBS, NULL);
435 job = g_object_new (EV_TYPE_JOB_ATTACHMENTS, NULL);
436 job->document = g_object_ref (document);
443 ev_job_annots_init (EvJobAnnots *job)
445 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
449 ev_job_annots_dispose (GObject *object)
453 ev_debug_message (DEBUG_JOBS, NULL);
455 job = EV_JOB_ANNOTS (object);
458 g_list_foreach (job->annots, (GFunc)ev_mapping_list_unref, NULL);
459 g_list_free (job->annots);
463 G_OBJECT_CLASS (ev_job_annots_parent_class)->dispose (object);
467 ev_job_annots_run (EvJob *job)
469 EvJobAnnots *job_annots = EV_JOB_ANNOTS (job);
472 ev_debug_message (DEBUG_JOBS, NULL);
473 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
475 ev_document_doc_mutex_lock ();
476 for (i = 0; i < ev_document_get_n_pages (job->document); i++) {
477 EvMappingList *mapping_list;
480 page = ev_document_get_page (job->document, i);
481 mapping_list = ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document),
483 g_object_unref (page);
486 job_annots->annots = g_list_prepend (job_annots->annots, mapping_list);
488 ev_document_doc_mutex_unlock ();
490 job_annots->annots = g_list_reverse (job_annots->annots);
492 ev_job_succeeded (job);
498 ev_job_annots_class_init (EvJobAnnotsClass *class)
500 GObjectClass *oclass = G_OBJECT_CLASS (class);
501 EvJobClass *job_class = EV_JOB_CLASS (class);
503 oclass->dispose = ev_job_annots_dispose;
504 job_class->run = ev_job_annots_run;
508 ev_job_annots_new (EvDocument *document)
512 ev_debug_message (DEBUG_JOBS, NULL);
514 job = g_object_new (EV_TYPE_JOB_ANNOTS, NULL);
515 job->document = g_object_ref (document);
522 ev_job_render_init (EvJobRender *job)
524 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
528 ev_job_render_dispose (GObject *object)
532 job = EV_JOB_RENDER (object);
534 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job->page, job);
537 cairo_surface_destroy (job->surface);
541 if (job->selection) {
542 cairo_surface_destroy (job->selection);
543 job->selection = NULL;
546 if (job->selection_region) {
547 cairo_region_destroy (job->selection_region);
548 job->selection_region = NULL;
551 (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
555 ev_job_render_run (EvJob *job)
557 EvJobRender *job_render = EV_JOB_RENDER (job);
561 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_render->page, job);
562 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
564 ev_document_doc_mutex_lock ();
566 ev_profiler_start (EV_PROFILE_JOBS, "Rendering page %d", job_render->page);
568 ev_document_fc_mutex_lock ();
570 ev_page = ev_document_get_page (job->document, job_render->page);
571 rc = ev_render_context_new (ev_page, job_render->rotation, job_render->scale);
572 g_object_unref (ev_page);
574 job_render->surface = ev_document_render (job->document, rc);
575 /* If job was cancelled during the page rendering,
576 * we return now, so that the thread is finished ASAP
578 if (g_cancellable_is_cancelled (job->cancellable)) {
579 ev_document_fc_mutex_unlock ();
580 ev_document_doc_mutex_unlock ();
586 if (job_render->include_selection && EV_IS_SELECTION (job->document)) {
587 ev_selection_render_selection (EV_SELECTION (job->document),
589 &(job_render->selection),
590 &(job_render->selection_points),
592 job_render->selection_style,
593 &(job_render->text), &(job_render->base));
594 job_render->selection_region =
595 ev_selection_get_selection_region (EV_SELECTION (job->document),
597 job_render->selection_style,
598 &(job_render->selection_points));
603 ev_document_fc_mutex_unlock ();
604 ev_document_doc_mutex_unlock ();
606 ev_job_succeeded (job);
612 ev_job_render_class_init (EvJobRenderClass *class)
614 GObjectClass *oclass = G_OBJECT_CLASS (class);
615 EvJobClass *job_class = EV_JOB_CLASS (class);
617 oclass->dispose = ev_job_render_dispose;
618 job_class->run = ev_job_render_run;
622 ev_job_render_new (EvDocument *document,
631 ev_debug_message (DEBUG_JOBS, "page: %d", page);
633 job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
635 EV_JOB (job)->document = g_object_ref (document);
637 job->rotation = rotation;
639 job->target_width = width;
640 job->target_height = height;
646 ev_job_render_set_selection_info (EvJobRender *job,
647 EvRectangle *selection_points,
648 EvSelectionStyle selection_style,
652 job->include_selection = TRUE;
654 job->selection_points = *selection_points;
655 job->selection_style = selection_style;
662 ev_job_page_data_init (EvJobPageData *job)
664 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
668 ev_job_page_data_run (EvJob *job)
670 EvJobPageData *job_pd = EV_JOB_PAGE_DATA (job);
673 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_pd->page, job);
674 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
676 ev_document_doc_mutex_lock ();
677 ev_page = ev_document_get_page (job->document, job_pd->page);
679 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT_MAPPING) && EV_IS_DOCUMENT_TEXT (job->document))
680 job_pd->text_mapping =
681 ev_document_text_get_text_mapping (EV_DOCUMENT_TEXT (job->document), ev_page);
682 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT) && EV_IS_DOCUMENT_TEXT (job->document))
684 ev_document_text_get_text (EV_DOCUMENT_TEXT (job->document), ev_page);
685 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT) && EV_IS_DOCUMENT_TEXT (job->document))
686 ev_document_text_get_text_layout (EV_DOCUMENT_TEXT (job->document),
688 &(job_pd->text_layout),
689 &(job_pd->text_layout_length));
690 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_LINKS) && EV_IS_DOCUMENT_LINKS (job->document))
691 job_pd->link_mapping =
692 ev_document_links_get_links (EV_DOCUMENT_LINKS (job->document), ev_page);
693 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_FORMS) && EV_IS_DOCUMENT_FORMS (job->document))
694 job_pd->form_field_mapping =
695 ev_document_forms_get_form_fields (EV_DOCUMENT_FORMS (job->document),
697 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_IMAGES) && EV_IS_DOCUMENT_IMAGES (job->document))
698 job_pd->image_mapping =
699 ev_document_images_get_image_mapping (EV_DOCUMENT_IMAGES (job->document),
701 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_ANNOTS) && EV_IS_DOCUMENT_ANNOTATIONS (job->document))
702 job_pd->annot_mapping =
703 ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document),
705 g_object_unref (ev_page);
706 ev_document_doc_mutex_unlock ();
708 ev_job_succeeded (job);
714 ev_job_page_data_class_init (EvJobPageDataClass *class)
716 EvJobClass *job_class = EV_JOB_CLASS (class);
718 job_class->run = ev_job_page_data_run;
722 ev_job_page_data_new (EvDocument *document,
724 EvJobPageDataFlags flags)
728 ev_debug_message (DEBUG_JOBS, "%d", page);
730 job = g_object_new (EV_TYPE_JOB_PAGE_DATA, NULL);
732 EV_JOB (job)->document = g_object_ref (document);
741 ev_job_thumbnail_init (EvJobThumbnail *job)
743 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
747 ev_job_thumbnail_dispose (GObject *object)
751 job = EV_JOB_THUMBNAIL (object);
753 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->page, job);
755 if (job->thumbnail) {
756 g_object_unref (job->thumbnail);
757 job->thumbnail = NULL;
760 (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
764 ev_job_thumbnail_run (EvJob *job)
766 EvJobThumbnail *job_thumb = EV_JOB_THUMBNAIL (job);
770 ev_debug_message (DEBUG_JOBS, "%d (%p)", job_thumb->page, job);
771 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
773 ev_document_doc_mutex_lock ();
775 page = ev_document_get_page (job->document, job_thumb->page);
776 rc = ev_render_context_new (page, job_thumb->rotation, job_thumb->scale);
777 g_object_unref (page);
779 job_thumb->thumbnail = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (job->document),
782 ev_document_doc_mutex_unlock ();
784 ev_job_succeeded (job);
790 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
792 GObjectClass *oclass = G_OBJECT_CLASS (class);
793 EvJobClass *job_class = EV_JOB_CLASS (class);
795 oclass->dispose = ev_job_thumbnail_dispose;
796 job_class->run = ev_job_thumbnail_run;
800 ev_job_thumbnail_new (EvDocument *document,
807 ev_debug_message (DEBUG_JOBS, "%d", page);
809 job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
811 EV_JOB (job)->document = g_object_ref (document);
813 job->rotation = rotation;
821 ev_job_fonts_init (EvJobFonts *job)
823 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
827 ev_job_fonts_run (EvJob *job)
829 EvJobFonts *job_fonts = EV_JOB_FONTS (job);
830 EvDocumentFonts *fonts = EV_DOCUMENT_FONTS (job->document);
832 ev_debug_message (DEBUG_JOBS, NULL);
834 /* Do not block the main loop */
835 if (!ev_document_doc_mutex_trylock ())
838 if (!ev_document_fc_mutex_trylock ())
841 #ifdef EV_ENABLE_DEBUG
842 /* We use the #ifdef in this case because of the if */
843 if (ev_document_fonts_get_progress (fonts) == 0)
844 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
847 job_fonts->scan_completed = !ev_document_fonts_scan (fonts, 20);
848 g_signal_emit (job_fonts, job_fonts_signals[FONTS_UPDATED], 0,
849 ev_document_fonts_get_progress (fonts));
851 ev_document_fc_mutex_unlock ();
852 ev_document_doc_mutex_unlock ();
854 if (job_fonts->scan_completed)
855 ev_job_succeeded (job);
857 return !job_fonts->scan_completed;
861 ev_job_fonts_class_init (EvJobFontsClass *class)
863 EvJobClass *job_class = EV_JOB_CLASS (class);
865 job_class->run = ev_job_fonts_run;
867 job_fonts_signals[FONTS_UPDATED] =
868 g_signal_new ("updated",
871 G_STRUCT_OFFSET (EvJobFontsClass, updated),
873 g_cclosure_marshal_VOID__DOUBLE,
879 ev_job_fonts_new (EvDocument *document)
883 ev_debug_message (DEBUG_JOBS, NULL);
885 job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
887 EV_JOB (job)->document = g_object_ref (document);
894 ev_job_load_init (EvJobLoad *job)
896 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
900 ev_job_load_dispose (GObject *object)
902 EvJobLoad *job = EV_JOB_LOAD (object);
904 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
912 g_free (job->password);
913 job->password = NULL;
916 (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object);
920 ev_job_load_run (EvJob *job)
922 EvJobLoad *job_load = EV_JOB_LOAD (job);
923 GError *error = NULL;
925 ev_debug_message (DEBUG_JOBS, "%s", job_load->uri);
926 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
928 ev_document_fc_mutex_lock ();
930 /* This job may already have a document even if the job didn't complete
931 because, e.g., a password is required - if so, just reload rather than
932 creating a new instance */
934 const gchar *uncompressed_uri;
936 if (job_load->password) {
937 ev_document_security_set_password (EV_DOCUMENT_SECURITY (job->document),
942 job->finished = FALSE;
943 g_clear_error (&job->error);
945 uncompressed_uri = g_object_get_data (G_OBJECT (job->document),
947 ev_document_load (job->document,
948 uncompressed_uri ? uncompressed_uri : job_load->uri,
951 job->document = ev_document_factory_get_document (job_load->uri,
955 ev_document_fc_mutex_unlock ();
958 ev_job_failed_from_error (job, error);
959 g_error_free (error);
961 ev_job_succeeded (job);
968 ev_job_load_class_init (EvJobLoadClass *class)
970 GObjectClass *oclass = G_OBJECT_CLASS (class);
971 EvJobClass *job_class = EV_JOB_CLASS (class);
973 oclass->dispose = ev_job_load_dispose;
974 job_class->run = ev_job_load_run;
978 ev_job_load_new (const gchar *uri)
982 ev_debug_message (DEBUG_JOBS, "%s", uri);
984 job = g_object_new (EV_TYPE_JOB_LOAD, NULL);
985 job->uri = g_strdup (uri);
991 ev_job_load_set_uri (EvJobLoad *job, const gchar *uri)
993 ev_debug_message (DEBUG_JOBS, "%s", uri);
997 job->uri = g_strdup (uri);
1001 ev_job_load_set_password (EvJobLoad *job, const gchar *password)
1003 ev_debug_message (DEBUG_JOBS, NULL);
1006 g_free (job->password);
1007 job->password = password ? g_strdup (password) : NULL;
1012 ev_job_save_init (EvJobSave *job)
1014 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1018 ev_job_save_dispose (GObject *object)
1020 EvJobSave *job = EV_JOB_SAVE (object);
1022 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
1029 if (job->document_uri) {
1030 g_free (job->document_uri);
1031 job->document_uri = NULL;
1034 (* G_OBJECT_CLASS (ev_job_save_parent_class)->dispose) (object);
1038 ev_job_save_run (EvJob *job)
1040 EvJobSave *job_save = EV_JOB_SAVE (job);
1042 gchar *tmp_filename = NULL;
1044 GError *error = NULL;
1046 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", job_save->uri, job_save->document_uri);
1047 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1049 fd = ev_mkstemp ("saveacopy.XXXXXX", &tmp_filename, &error);
1051 ev_job_failed_from_error (job, error);
1052 g_error_free (error);
1057 ev_document_doc_mutex_lock ();
1059 /* Save document to temp filename */
1060 local_uri = g_filename_to_uri (tmp_filename, NULL, &error);
1061 if (local_uri != NULL) {
1062 ev_document_save (job->document, local_uri, &error);
1067 ev_document_doc_mutex_unlock ();
1071 ev_job_failed_from_error (job, error);
1072 g_error_free (error);
1077 /* If original document was compressed,
1078 * compress it again before saving
1080 if (g_object_get_data (G_OBJECT (job->document), "uri-uncompressed")) {
1081 EvCompressionType ctype = EV_COMPRESSION_NONE;
1085 ext = g_strrstr (job_save->document_uri, ".gz");
1086 if (ext && g_ascii_strcasecmp (ext, ".gz") == 0)
1087 ctype = EV_COMPRESSION_GZIP;
1089 ext = g_strrstr (job_save->document_uri, ".bz2");
1090 if (ext && g_ascii_strcasecmp (ext, ".bz2") == 0)
1091 ctype = EV_COMPRESSION_BZIP2;
1093 uri_comp = ev_file_compress (local_uri, ctype, &error);
1095 g_unlink (tmp_filename);
1097 if (!uri_comp || error) {
1100 local_uri = uri_comp;
1104 g_free (tmp_filename);
1108 ev_job_failed_from_error (job, error);
1109 g_error_free (error);
1117 ev_xfer_uri_simple (local_uri, job_save->uri, &error);
1118 ev_tmp_uri_unlink (local_uri);
1121 ev_job_failed_from_error (job, error);
1122 g_error_free (error);
1124 ev_job_succeeded (job);
1131 ev_job_save_class_init (EvJobSaveClass *class)
1133 GObjectClass *oclass = G_OBJECT_CLASS (class);
1134 EvJobClass *job_class = EV_JOB_CLASS (class);
1136 oclass->dispose = ev_job_save_dispose;
1137 job_class->run = ev_job_save_run;
1141 ev_job_save_new (EvDocument *document,
1143 const gchar *document_uri)
1147 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", uri, document_uri);
1149 job = g_object_new (EV_TYPE_JOB_SAVE, NULL);
1151 EV_JOB (job)->document = g_object_ref (document);
1152 job->uri = g_strdup (uri);
1153 job->document_uri = g_strdup (document_uri);
1155 return EV_JOB (job);
1160 ev_job_find_init (EvJobFind *job)
1162 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
1166 ev_job_find_dispose (GObject *object)
1168 EvJobFind *job = EV_JOB_FIND (object);
1170 ev_debug_message (DEBUG_JOBS, NULL);
1180 for (i = 0; i < job->n_pages; i++) {
1181 g_list_foreach (job->pages[i], (GFunc)ev_rectangle_free, NULL);
1182 g_list_free (job->pages[i]);
1185 g_free (job->pages);
1189 (* G_OBJECT_CLASS (ev_job_find_parent_class)->dispose) (object);
1193 ev_job_find_run (EvJob *job)
1195 EvJobFind *job_find = EV_JOB_FIND (job);
1196 EvDocumentFind *find = EV_DOCUMENT_FIND (job->document);
1200 ev_debug_message (DEBUG_JOBS, NULL);
1202 /* Do not block the main loop */
1203 if (!ev_document_doc_mutex_trylock ())
1206 #ifdef EV_ENABLE_DEBUG
1207 /* We use the #ifdef in this case because of the if */
1208 if (job_find->current_page == job_find->start_page)
1209 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1212 ev_page = ev_document_get_page (job->document, job_find->current_page);
1213 matches = ev_document_find_find_text (find, ev_page, job_find->text,
1214 job_find->case_sensitive);
1215 g_object_unref (ev_page);
1217 ev_document_doc_mutex_unlock ();
1219 if (!job_find->has_results)
1220 job_find->has_results = (matches != NULL);
1222 job_find->pages[job_find->current_page] = matches;
1223 g_signal_emit (job_find, job_find_signals[FIND_UPDATED], 0, job_find->current_page);
1225 job_find->current_page = (job_find->current_page + 1) % job_find->n_pages;
1226 if (job_find->current_page == job_find->start_page) {
1227 ev_job_succeeded (job);
1236 ev_job_find_class_init (EvJobFindClass *class)
1238 EvJobClass *job_class = EV_JOB_CLASS (class);
1239 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1241 job_class->run = ev_job_find_run;
1242 gobject_class->dispose = ev_job_find_dispose;
1244 job_find_signals[FIND_UPDATED] =
1245 g_signal_new ("updated",
1248 G_STRUCT_OFFSET (EvJobFindClass, updated),
1250 g_cclosure_marshal_VOID__INT,
1256 ev_job_find_new (EvDocument *document,
1260 gboolean case_sensitive)
1264 ev_debug_message (DEBUG_JOBS, NULL);
1266 job = g_object_new (EV_TYPE_JOB_FIND, NULL);
1268 EV_JOB (job)->document = g_object_ref (document);
1269 job->start_page = start_page;
1270 job->current_page = start_page;
1271 job->n_pages = n_pages;
1272 job->pages = g_new0 (GList *, n_pages);
1273 job->text = g_strdup (text);
1274 job->case_sensitive = case_sensitive;
1275 job->has_results = FALSE;
1277 return EV_JOB (job);
1281 ev_job_find_get_n_results (EvJobFind *job,
1284 return g_list_length (job->pages[page]);
1288 ev_job_find_get_progress (EvJobFind *job)
1292 if (ev_job_is_finished (EV_JOB (job)))
1295 if (job->current_page > job->start_page) {
1296 pages_done = job->current_page - job->start_page + 1;
1297 } else if (job->current_page == job->start_page) {
1298 pages_done = job->n_pages;
1300 pages_done = job->n_pages - job->start_page + job->current_page;
1303 return pages_done / (gdouble) job->n_pages;
1307 ev_job_find_has_results (EvJobFind *job)
1309 return job->has_results;
1313 ev_job_find_get_results (EvJobFind *job)
1320 ev_job_layers_init (EvJobLayers *job)
1322 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1326 ev_job_layers_dispose (GObject *object)
1330 ev_debug_message (DEBUG_JOBS, NULL);
1332 job = EV_JOB_LAYERS (object);
1335 g_object_unref (job->model);
1339 (* G_OBJECT_CLASS (ev_job_layers_parent_class)->dispose) (object);
1343 ev_job_layers_run (EvJob *job)
1345 EvJobLayers *job_layers = EV_JOB_LAYERS (job);
1347 ev_debug_message (DEBUG_JOBS, NULL);
1348 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1350 ev_document_doc_mutex_lock ();
1351 job_layers->model = ev_document_layers_get_layers (EV_DOCUMENT_LAYERS (job->document));
1352 ev_document_doc_mutex_unlock ();
1354 ev_job_succeeded (job);
1360 ev_job_layers_class_init (EvJobLayersClass *class)
1362 GObjectClass *oclass = G_OBJECT_CLASS (class);
1363 EvJobClass *job_class = EV_JOB_CLASS (class);
1365 oclass->dispose = ev_job_layers_dispose;
1366 job_class->run = ev_job_layers_run;
1370 ev_job_layers_new (EvDocument *document)
1374 ev_debug_message (DEBUG_JOBS, NULL);
1376 job = g_object_new (EV_TYPE_JOB_LAYERS, NULL);
1377 job->document = g_object_ref (document);
1384 ev_job_export_init (EvJobExport *job)
1386 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1391 ev_job_export_dispose (GObject *object)
1395 ev_debug_message (DEBUG_JOBS, NULL);
1397 job = EV_JOB_EXPORT (object);
1400 g_object_unref (job->rc);
1404 (* G_OBJECT_CLASS (ev_job_export_parent_class)->dispose) (object);
1408 ev_job_export_run (EvJob *job)
1410 EvJobExport *job_export = EV_JOB_EXPORT (job);
1413 g_assert (job_export->page != -1);
1415 ev_debug_message (DEBUG_JOBS, NULL);
1416 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1418 ev_document_doc_mutex_lock ();
1420 ev_page = ev_document_get_page (job->document, job_export->page);
1421 if (job_export->rc) {
1422 job->failed = FALSE;
1423 job->finished = FALSE;
1424 g_clear_error (&job->error);
1426 ev_render_context_set_page (job_export->rc, ev_page);
1428 job_export->rc = ev_render_context_new (ev_page, 0, 1.0);
1430 g_object_unref (ev_page);
1432 ev_file_exporter_do_page (EV_FILE_EXPORTER (job->document), job_export->rc);
1434 ev_document_doc_mutex_unlock ();
1436 ev_job_succeeded (job);
1442 ev_job_export_class_init (EvJobExportClass *class)
1444 GObjectClass *oclass = G_OBJECT_CLASS (class);
1445 EvJobClass *job_class = EV_JOB_CLASS (class);
1447 oclass->dispose = ev_job_export_dispose;
1448 job_class->run = ev_job_export_run;
1452 ev_job_export_new (EvDocument *document)
1456 ev_debug_message (DEBUG_JOBS, NULL);
1458 job = g_object_new (EV_TYPE_JOB_EXPORT, NULL);
1459 job->document = g_object_ref (document);
1465 ev_job_export_set_page (EvJobExport *job,
1473 ev_job_print_init (EvJobPrint *job)
1475 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1480 ev_job_print_dispose (GObject *object)
1484 ev_debug_message (DEBUG_JOBS, NULL);
1486 job = EV_JOB_PRINT (object);
1489 cairo_destroy (job->cr);
1493 (* G_OBJECT_CLASS (ev_job_print_parent_class)->dispose) (object);
1497 ev_job_print_run (EvJob *job)
1499 EvJobPrint *job_print = EV_JOB_PRINT (job);
1501 cairo_status_t cr_status;
1503 g_assert (job_print->page != -1);
1504 g_assert (job_print->cr != NULL);
1506 ev_debug_message (DEBUG_JOBS, NULL);
1507 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1509 job->failed = FALSE;
1510 job->finished = FALSE;
1511 g_clear_error (&job->error);
1513 ev_document_doc_mutex_lock ();
1515 ev_page = ev_document_get_page (job->document, job_print->page);
1516 ev_document_print_print_page (EV_DOCUMENT_PRINT (job->document),
1517 ev_page, job_print->cr);
1518 g_object_unref (ev_page);
1520 ev_document_doc_mutex_unlock ();
1522 cr_status = cairo_status (job_print->cr);
1523 if (cr_status == CAIRO_STATUS_SUCCESS) {
1524 ev_job_succeeded (job);
1528 GTK_PRINT_ERROR_GENERAL,
1529 _("Failed to print page %d: %s"),
1531 cairo_status_to_string (cr_status));
1538 ev_job_print_class_init (EvJobPrintClass *class)
1540 GObjectClass *oclass = G_OBJECT_CLASS (class);
1541 EvJobClass *job_class = EV_JOB_CLASS (class);
1543 oclass->dispose = ev_job_print_dispose;
1544 job_class->run = ev_job_print_run;
1548 ev_job_print_new (EvDocument *document)
1552 ev_debug_message (DEBUG_JOBS, NULL);
1554 job = g_object_new (EV_TYPE_JOB_PRINT, NULL);
1555 job->document = g_object_ref (document);
1561 ev_job_print_set_page (EvJobPrint *job,
1568 ev_job_print_set_cairo (EvJobPrint *job,
1575 cairo_destroy (job->cr);
1576 job->cr = cr ? cairo_reference (cr) : NULL;