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-links.h"
25 #include "ev-document-images.h"
26 #include "ev-document-forms.h"
27 #include "ev-file-exporter.h"
28 #include "ev-document-factory.h"
29 #include "ev-document-misc.h"
30 #include "ev-file-helpers.h"
31 #include "ev-document-fonts.h"
32 #include "ev-document-security.h"
33 #include "ev-document-find.h"
34 #include "ev-document-layers.h"
35 #include "ev-document-print.h"
36 #include "ev-document-annotations.h"
37 #include "ev-document-attachments.h"
38 #include "ev-document-text.h"
42 #include <glib/gstdio.h>
43 #include <glib/gi18n-lib.h>
46 static void ev_job_init (EvJob *job);
47 static void ev_job_class_init (EvJobClass *class);
48 static void ev_job_links_init (EvJobLinks *job);
49 static void ev_job_links_class_init (EvJobLinksClass *class);
50 static void ev_job_attachments_init (EvJobAttachments *job);
51 static void ev_job_attachments_class_init (EvJobAttachmentsClass *class);
52 static void ev_job_annots_init (EvJobAnnots *job);
53 static void ev_job_annots_class_init (EvJobAnnotsClass *class);
54 static void ev_job_render_init (EvJobRender *job);
55 static void ev_job_render_class_init (EvJobRenderClass *class);
56 static void ev_job_page_data_init (EvJobPageData *job);
57 static void ev_job_page_data_class_init (EvJobPageDataClass *class);
58 static void ev_job_thumbnail_init (EvJobThumbnail *job);
59 static void ev_job_thumbnail_class_init (EvJobThumbnailClass *class);
60 static void ev_job_load_init (EvJobLoad *job);
61 static void ev_job_load_class_init (EvJobLoadClass *class);
62 static void ev_job_save_init (EvJobSave *job);
63 static void ev_job_save_class_init (EvJobSaveClass *class);
64 static void ev_job_find_init (EvJobFind *job);
65 static void ev_job_find_class_init (EvJobFindClass *class);
66 static void ev_job_layers_init (EvJobLayers *job);
67 static void ev_job_layers_class_init (EvJobLayersClass *class);
68 static void ev_job_export_init (EvJobExport *job);
69 static void ev_job_export_class_init (EvJobExportClass *class);
70 static void ev_job_print_init (EvJobPrint *job);
71 static void ev_job_print_class_init (EvJobPrintClass *class);
89 static guint job_signals[LAST_SIGNAL] = { 0 };
90 static guint job_fonts_signals[FONTS_LAST_SIGNAL] = { 0 };
91 static guint job_find_signals[FIND_LAST_SIGNAL] = { 0 };
93 G_DEFINE_ABSTRACT_TYPE (EvJob, ev_job, G_TYPE_OBJECT)
94 G_DEFINE_TYPE (EvJobLinks, ev_job_links, EV_TYPE_JOB)
95 G_DEFINE_TYPE (EvJobAttachments, ev_job_attachments, EV_TYPE_JOB)
96 G_DEFINE_TYPE (EvJobAnnots, ev_job_annots, EV_TYPE_JOB)
97 G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB)
98 G_DEFINE_TYPE (EvJobPageData, ev_job_page_data, EV_TYPE_JOB)
99 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
100 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
101 G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB)
102 G_DEFINE_TYPE (EvJobSave, ev_job_save, EV_TYPE_JOB)
103 G_DEFINE_TYPE (EvJobFind, ev_job_find, EV_TYPE_JOB)
104 G_DEFINE_TYPE (EvJobLayers, ev_job_layers, EV_TYPE_JOB)
105 G_DEFINE_TYPE (EvJobExport, ev_job_export, EV_TYPE_JOB)
106 G_DEFINE_TYPE (EvJobPrint, ev_job_print, EV_TYPE_JOB)
110 ev_job_init (EvJob *job)
112 job->cancellable = g_cancellable_new ();
116 ev_job_dispose (GObject *object)
120 job = EV_JOB (object);
123 g_object_unref (job->document);
124 job->document = NULL;
127 if (job->cancellable) {
128 g_object_unref (job->cancellable);
129 job->cancellable = NULL;
133 g_error_free (job->error);
137 (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object);
141 ev_job_class_init (EvJobClass *class)
143 GObjectClass *oclass;
145 oclass = G_OBJECT_CLASS (class);
147 oclass->dispose = ev_job_dispose;
149 job_signals[CANCELLED] =
150 g_signal_new ("cancelled",
153 G_STRUCT_OFFSET (EvJobClass, cancelled),
155 g_cclosure_marshal_VOID__VOID,
157 job_signals [FINISHED] =
158 g_signal_new ("finished",
161 G_STRUCT_OFFSET (EvJobClass, finished),
163 g_cclosure_marshal_VOID__VOID,
168 emit_finished (EvJob *job)
170 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
172 job->idle_finished_id = 0;
174 if (job->cancelled) {
175 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit finished", EV_GET_TYPE_NAME (job), job);
177 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
178 g_signal_emit (job, job_signals[FINISHED], 0);
185 ev_job_emit_finished (EvJob *job)
187 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
189 if (g_cancellable_is_cancelled (job->cancellable)) {
190 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, returning", EV_GET_TYPE_NAME (job), job);
194 job->finished = TRUE;
196 if (job->run_mode == EV_JOB_RUN_THREAD) {
197 job->idle_finished_id =
198 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
199 (GSourceFunc)emit_finished,
201 (GDestroyNotify)g_object_unref);
203 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
204 g_signal_emit (job, job_signals[FINISHED], 0);
209 ev_job_run (EvJob *job)
211 EvJobClass *class = EV_JOB_GET_CLASS (job);
213 return class->run (job);
217 ev_job_cancel (EvJob *job)
222 ev_debug_message (DEBUG_JOBS, "job %s (%p) cancelled", EV_GET_TYPE_NAME (job), job);
223 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
225 /* This should never be called from a thread */
226 job->cancelled = TRUE;
227 g_cancellable_cancel (job->cancellable);
229 if (job->finished && job->idle_finished_id == 0)
232 g_signal_emit (job, job_signals[CANCELLED], 0);
236 ev_job_failed (EvJob *job,
245 if (job->failed || job->finished)
248 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
252 va_start (args, format);
253 message = g_strdup_vprintf (format, args);
256 job->error = g_error_new_literal (domain, code, message);
259 ev_job_emit_finished (job);
263 ev_job_failed_from_error (EvJob *job,
266 if (job->failed || job->finished)
269 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
272 job->error = g_error_copy (error);
274 ev_job_emit_finished (job);
278 ev_job_succeeded (EvJob *job)
283 ev_debug_message (DEBUG_JOBS, "job %s (%p) succeeded", EV_GET_TYPE_NAME (job), job);
286 ev_job_emit_finished (job);
290 ev_job_is_finished (EvJob *job)
292 return job->finished;
296 ev_job_is_failed (EvJob *job)
302 ev_job_get_run_mode (EvJob *job)
304 return job->run_mode;
308 ev_job_set_run_mode (EvJob *job,
309 EvJobRunMode run_mode)
311 job->run_mode = run_mode;
316 ev_job_links_init (EvJobLinks *job)
318 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
322 ev_job_links_dispose (GObject *object)
326 ev_debug_message (DEBUG_JOBS, NULL);
328 job = EV_JOB_LINKS (object);
331 g_object_unref (job->model);
335 (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object);
339 fill_page_labels (GtkTreeModel *tree_model,
344 EvDocumentLinks *document_links;
348 gtk_tree_model_get (tree_model, iter,
349 EV_DOCUMENT_LINKS_COLUMN_LINK, &link,
355 document_links = EV_DOCUMENT_LINKS (job->document);
356 page_label = ev_document_links_get_link_page_label (document_links, link);
360 gtk_tree_store_set (GTK_TREE_STORE (tree_model), iter,
361 EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL, page_label,
365 g_object_unref (link);
371 ev_job_links_run (EvJob *job)
373 EvJobLinks *job_links = EV_JOB_LINKS (job);
375 ev_debug_message (DEBUG_JOBS, NULL);
376 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
378 ev_document_doc_mutex_lock ();
379 job_links->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (job->document));
380 ev_document_doc_mutex_unlock ();
382 gtk_tree_model_foreach (job_links->model, (GtkTreeModelForeachFunc)fill_page_labels, job);
384 ev_job_succeeded (job);
390 ev_job_links_class_init (EvJobLinksClass *class)
392 GObjectClass *oclass = G_OBJECT_CLASS (class);
393 EvJobClass *job_class = EV_JOB_CLASS (class);
395 oclass->dispose = ev_job_links_dispose;
396 job_class->run = ev_job_links_run;
400 ev_job_links_new (EvDocument *document)
404 ev_debug_message (DEBUG_JOBS, NULL);
406 job = g_object_new (EV_TYPE_JOB_LINKS, NULL);
407 job->document = g_object_ref (document);
412 /* EvJobAttachments */
414 ev_job_attachments_init (EvJobAttachments *job)
416 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
420 ev_job_attachments_dispose (GObject *object)
422 EvJobAttachments *job;
424 ev_debug_message (DEBUG_JOBS, NULL);
426 job = EV_JOB_ATTACHMENTS (object);
428 if (job->attachments) {
429 g_list_foreach (job->attachments, (GFunc)g_object_unref, NULL);
430 g_list_free (job->attachments);
431 job->attachments = NULL;
434 (* G_OBJECT_CLASS (ev_job_attachments_parent_class)->dispose) (object);
438 ev_job_attachments_run (EvJob *job)
440 EvJobAttachments *job_attachments = EV_JOB_ATTACHMENTS (job);
442 ev_debug_message (DEBUG_JOBS, NULL);
443 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
445 ev_document_doc_mutex_lock ();
446 job_attachments->attachments =
447 ev_document_attachments_get_attachments (EV_DOCUMENT_ATTACHMENTS (job->document));
448 ev_document_doc_mutex_unlock ();
450 ev_job_succeeded (job);
456 ev_job_attachments_class_init (EvJobAttachmentsClass *class)
458 GObjectClass *oclass = G_OBJECT_CLASS (class);
459 EvJobClass *job_class = EV_JOB_CLASS (class);
461 oclass->dispose = ev_job_attachments_dispose;
462 job_class->run = ev_job_attachments_run;
466 ev_job_attachments_new (EvDocument *document)
470 ev_debug_message (DEBUG_JOBS, NULL);
472 job = g_object_new (EV_TYPE_JOB_ATTACHMENTS, NULL);
473 job->document = g_object_ref (document);
480 ev_job_annots_init (EvJobAnnots *job)
482 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
486 ev_job_annots_dispose (GObject *object)
490 ev_debug_message (DEBUG_JOBS, NULL);
492 job = EV_JOB_ANNOTS (object);
495 g_list_foreach (job->annots, (GFunc)ev_mapping_list_unref, NULL);
496 g_list_free (job->annots);
500 G_OBJECT_CLASS (ev_job_annots_parent_class)->dispose (object);
504 ev_job_annots_run (EvJob *job)
506 EvJobAnnots *job_annots = EV_JOB_ANNOTS (job);
509 ev_debug_message (DEBUG_JOBS, NULL);
510 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
512 ev_document_doc_mutex_lock ();
513 for (i = 0; i < ev_document_get_n_pages (job->document); i++) {
514 EvMappingList *mapping_list;
517 page = ev_document_get_page (job->document, i);
518 mapping_list = ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document),
520 g_object_unref (page);
523 job_annots->annots = g_list_prepend (job_annots->annots, mapping_list);
525 ev_document_doc_mutex_unlock ();
527 job_annots->annots = g_list_reverse (job_annots->annots);
529 ev_job_succeeded (job);
535 ev_job_annots_class_init (EvJobAnnotsClass *class)
537 GObjectClass *oclass = G_OBJECT_CLASS (class);
538 EvJobClass *job_class = EV_JOB_CLASS (class);
540 oclass->dispose = ev_job_annots_dispose;
541 job_class->run = ev_job_annots_run;
545 ev_job_annots_new (EvDocument *document)
549 ev_debug_message (DEBUG_JOBS, NULL);
551 job = g_object_new (EV_TYPE_JOB_ANNOTS, NULL);
552 job->document = g_object_ref (document);
559 ev_job_render_init (EvJobRender *job)
561 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
565 ev_job_render_dispose (GObject *object)
569 job = EV_JOB_RENDER (object);
571 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job->page, job);
574 cairo_surface_destroy (job->surface);
578 if (job->selection) {
579 cairo_surface_destroy (job->selection);
580 job->selection = NULL;
583 if (job->selection_region) {
584 cairo_region_destroy (job->selection_region);
585 job->selection_region = NULL;
588 (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
592 ev_job_render_run (EvJob *job)
594 EvJobRender *job_render = EV_JOB_RENDER (job);
598 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_render->page, job);
599 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
601 ev_document_doc_mutex_lock ();
603 ev_profiler_start (EV_PROFILE_JOBS, "Rendering page %d", job_render->page);
605 ev_document_fc_mutex_lock ();
607 ev_page = ev_document_get_page (job->document, job_render->page);
608 rc = ev_render_context_new (ev_page, job_render->rotation, job_render->scale);
609 g_object_unref (ev_page);
611 job_render->surface = ev_document_render (job->document, rc);
612 /* If job was cancelled during the page rendering,
613 * we return now, so that the thread is finished ASAP
615 if (g_cancellable_is_cancelled (job->cancellable)) {
616 ev_document_fc_mutex_unlock ();
617 ev_document_doc_mutex_unlock ();
623 if (job_render->include_selection && EV_IS_SELECTION (job->document)) {
624 ev_selection_render_selection (EV_SELECTION (job->document),
626 &(job_render->selection),
627 &(job_render->selection_points),
629 job_render->selection_style,
630 &(job_render->text), &(job_render->base));
631 job_render->selection_region =
632 ev_selection_get_selection_region (EV_SELECTION (job->document),
634 job_render->selection_style,
635 &(job_render->selection_points));
640 ev_document_fc_mutex_unlock ();
641 ev_document_doc_mutex_unlock ();
643 ev_job_succeeded (job);
649 ev_job_render_class_init (EvJobRenderClass *class)
651 GObjectClass *oclass = G_OBJECT_CLASS (class);
652 EvJobClass *job_class = EV_JOB_CLASS (class);
654 oclass->dispose = ev_job_render_dispose;
655 job_class->run = ev_job_render_run;
659 ev_job_render_new (EvDocument *document,
668 ev_debug_message (DEBUG_JOBS, "page: %d", page);
670 job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
672 EV_JOB (job)->document = g_object_ref (document);
674 job->rotation = rotation;
676 job->target_width = width;
677 job->target_height = height;
683 ev_job_render_set_selection_info (EvJobRender *job,
684 EvRectangle *selection_points,
685 EvSelectionStyle selection_style,
689 job->include_selection = TRUE;
691 job->selection_points = *selection_points;
692 job->selection_style = selection_style;
699 ev_job_page_data_init (EvJobPageData *job)
701 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
705 ev_job_page_data_run (EvJob *job)
707 EvJobPageData *job_pd = EV_JOB_PAGE_DATA (job);
710 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_pd->page, job);
711 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
713 ev_document_doc_mutex_lock ();
714 ev_page = ev_document_get_page (job->document, job_pd->page);
716 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT_MAPPING) && EV_IS_DOCUMENT_TEXT (job->document))
717 job_pd->text_mapping =
718 ev_document_text_get_text_mapping (EV_DOCUMENT_TEXT (job->document), ev_page);
719 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT) && EV_IS_DOCUMENT_TEXT (job->document))
721 ev_document_text_get_text (EV_DOCUMENT_TEXT (job->document), ev_page);
722 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT_LAYOUT) && EV_IS_DOCUMENT_TEXT (job->document))
723 ev_document_text_get_text_layout (EV_DOCUMENT_TEXT (job->document),
725 &(job_pd->text_layout),
726 &(job_pd->text_layout_length));
727 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_LINKS) && EV_IS_DOCUMENT_LINKS (job->document))
728 job_pd->link_mapping =
729 ev_document_links_get_links (EV_DOCUMENT_LINKS (job->document), ev_page);
730 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_FORMS) && EV_IS_DOCUMENT_FORMS (job->document))
731 job_pd->form_field_mapping =
732 ev_document_forms_get_form_fields (EV_DOCUMENT_FORMS (job->document),
734 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_IMAGES) && EV_IS_DOCUMENT_IMAGES (job->document))
735 job_pd->image_mapping =
736 ev_document_images_get_image_mapping (EV_DOCUMENT_IMAGES (job->document),
738 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_ANNOTS) && EV_IS_DOCUMENT_ANNOTATIONS (job->document))
739 job_pd->annot_mapping =
740 ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document),
742 g_object_unref (ev_page);
743 ev_document_doc_mutex_unlock ();
745 ev_job_succeeded (job);
751 ev_job_page_data_class_init (EvJobPageDataClass *class)
753 EvJobClass *job_class = EV_JOB_CLASS (class);
755 job_class->run = ev_job_page_data_run;
759 ev_job_page_data_new (EvDocument *document,
761 EvJobPageDataFlags flags)
765 ev_debug_message (DEBUG_JOBS, "%d", page);
767 job = g_object_new (EV_TYPE_JOB_PAGE_DATA, NULL);
769 EV_JOB (job)->document = g_object_ref (document);
778 ev_job_thumbnail_init (EvJobThumbnail *job)
780 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
784 ev_job_thumbnail_dispose (GObject *object)
788 job = EV_JOB_THUMBNAIL (object);
790 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->page, job);
792 if (job->thumbnail) {
793 g_object_unref (job->thumbnail);
794 job->thumbnail = NULL;
797 (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
801 ev_job_thumbnail_run (EvJob *job)
803 EvJobThumbnail *job_thumb = EV_JOB_THUMBNAIL (job);
808 ev_debug_message (DEBUG_JOBS, "%d (%p)", job_thumb->page, job);
809 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
811 ev_document_doc_mutex_lock ();
813 page = ev_document_get_page (job->document, job_thumb->page);
814 rc = ev_render_context_new (page, job_thumb->rotation, job_thumb->scale);
815 g_object_unref (page);
817 pixbuf = ev_document_get_thumbnail (job->document, rc);
819 ev_document_doc_mutex_unlock ();
822 job_thumb->thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
823 g_object_unref (pixbuf);
825 ev_job_succeeded (job);
831 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
833 GObjectClass *oclass = G_OBJECT_CLASS (class);
834 EvJobClass *job_class = EV_JOB_CLASS (class);
836 oclass->dispose = ev_job_thumbnail_dispose;
837 job_class->run = ev_job_thumbnail_run;
841 ev_job_thumbnail_new (EvDocument *document,
848 ev_debug_message (DEBUG_JOBS, "%d", page);
850 job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
852 EV_JOB (job)->document = g_object_ref (document);
854 job->rotation = rotation;
862 ev_job_fonts_init (EvJobFonts *job)
864 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
868 ev_job_fonts_run (EvJob *job)
870 EvJobFonts *job_fonts = EV_JOB_FONTS (job);
871 EvDocumentFonts *fonts = EV_DOCUMENT_FONTS (job->document);
873 ev_debug_message (DEBUG_JOBS, NULL);
875 /* Do not block the main loop */
876 if (!ev_document_doc_mutex_trylock ())
879 if (!ev_document_fc_mutex_trylock ())
882 #ifdef EV_ENABLE_DEBUG
883 /* We use the #ifdef in this case because of the if */
884 if (ev_document_fonts_get_progress (fonts) == 0)
885 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
888 job_fonts->scan_completed = !ev_document_fonts_scan (fonts, 20);
889 g_signal_emit (job_fonts, job_fonts_signals[FONTS_UPDATED], 0,
890 ev_document_fonts_get_progress (fonts));
892 ev_document_fc_mutex_unlock ();
893 ev_document_doc_mutex_unlock ();
895 if (job_fonts->scan_completed)
896 ev_job_succeeded (job);
898 return !job_fonts->scan_completed;
902 ev_job_fonts_class_init (EvJobFontsClass *class)
904 EvJobClass *job_class = EV_JOB_CLASS (class);
906 job_class->run = ev_job_fonts_run;
908 job_fonts_signals[FONTS_UPDATED] =
909 g_signal_new ("updated",
912 G_STRUCT_OFFSET (EvJobFontsClass, updated),
914 g_cclosure_marshal_VOID__DOUBLE,
920 ev_job_fonts_new (EvDocument *document)
924 ev_debug_message (DEBUG_JOBS, NULL);
926 job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
928 EV_JOB (job)->document = g_object_ref (document);
935 ev_job_load_init (EvJobLoad *job)
937 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
941 ev_job_load_dispose (GObject *object)
943 EvJobLoad *job = EV_JOB_LOAD (object);
945 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
953 g_free (job->password);
954 job->password = NULL;
957 (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object);
961 ev_job_load_run (EvJob *job)
963 EvJobLoad *job_load = EV_JOB_LOAD (job);
964 GError *error = NULL;
966 ev_debug_message (DEBUG_JOBS, "%s", job_load->uri);
967 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
969 ev_document_fc_mutex_lock ();
971 /* This job may already have a document even if the job didn't complete
972 because, e.g., a password is required - if so, just reload rather than
973 creating a new instance */
975 const gchar *uncompressed_uri;
977 if (job_load->password) {
978 ev_document_security_set_password (EV_DOCUMENT_SECURITY (job->document),
983 job->finished = FALSE;
984 g_clear_error (&job->error);
986 uncompressed_uri = g_object_get_data (G_OBJECT (job->document),
988 ev_document_load (job->document,
989 uncompressed_uri ? uncompressed_uri : job_load->uri,
992 job->document = ev_document_factory_get_document (job_load->uri,
996 ev_document_fc_mutex_unlock ();
999 ev_job_failed_from_error (job, error);
1000 g_error_free (error);
1002 ev_job_succeeded (job);
1009 ev_job_load_class_init (EvJobLoadClass *class)
1011 GObjectClass *oclass = G_OBJECT_CLASS (class);
1012 EvJobClass *job_class = EV_JOB_CLASS (class);
1014 oclass->dispose = ev_job_load_dispose;
1015 job_class->run = ev_job_load_run;
1019 ev_job_load_new (const gchar *uri)
1023 ev_debug_message (DEBUG_JOBS, "%s", uri);
1025 job = g_object_new (EV_TYPE_JOB_LOAD, NULL);
1026 job->uri = g_strdup (uri);
1028 return EV_JOB (job);
1032 ev_job_load_set_uri (EvJobLoad *job, const gchar *uri)
1034 ev_debug_message (DEBUG_JOBS, "%s", uri);
1038 job->uri = g_strdup (uri);
1042 ev_job_load_set_password (EvJobLoad *job, const gchar *password)
1044 ev_debug_message (DEBUG_JOBS, NULL);
1047 g_free (job->password);
1048 job->password = password ? g_strdup (password) : NULL;
1053 ev_job_save_init (EvJobSave *job)
1055 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1059 ev_job_save_dispose (GObject *object)
1061 EvJobSave *job = EV_JOB_SAVE (object);
1063 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
1070 if (job->document_uri) {
1071 g_free (job->document_uri);
1072 job->document_uri = NULL;
1075 (* G_OBJECT_CLASS (ev_job_save_parent_class)->dispose) (object);
1079 ev_job_save_run (EvJob *job)
1081 EvJobSave *job_save = EV_JOB_SAVE (job);
1083 gchar *tmp_filename = NULL;
1085 GError *error = NULL;
1087 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", job_save->uri, job_save->document_uri);
1088 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1090 fd = ev_mkstemp ("saveacopy.XXXXXX", &tmp_filename, &error);
1092 ev_job_failed_from_error (job, error);
1093 g_error_free (error);
1098 ev_document_doc_mutex_lock ();
1100 /* Save document to temp filename */
1101 local_uri = g_filename_to_uri (tmp_filename, NULL, &error);
1102 if (local_uri != NULL) {
1103 ev_document_save (job->document, local_uri, &error);
1108 ev_document_doc_mutex_unlock ();
1112 ev_job_failed_from_error (job, error);
1113 g_error_free (error);
1118 /* If original document was compressed,
1119 * compress it again before saving
1121 if (g_object_get_data (G_OBJECT (job->document), "uri-uncompressed")) {
1122 EvCompressionType ctype = EV_COMPRESSION_NONE;
1126 ext = g_strrstr (job_save->document_uri, ".gz");
1127 if (ext && g_ascii_strcasecmp (ext, ".gz") == 0)
1128 ctype = EV_COMPRESSION_GZIP;
1130 ext = g_strrstr (job_save->document_uri, ".bz2");
1131 if (ext && g_ascii_strcasecmp (ext, ".bz2") == 0)
1132 ctype = EV_COMPRESSION_BZIP2;
1134 uri_comp = ev_file_compress (local_uri, ctype, &error);
1136 g_unlink (tmp_filename);
1138 if (!uri_comp || error) {
1141 local_uri = uri_comp;
1145 g_free (tmp_filename);
1149 ev_job_failed_from_error (job, error);
1150 g_error_free (error);
1158 ev_xfer_uri_simple (local_uri, job_save->uri, &error);
1159 ev_tmp_uri_unlink (local_uri);
1162 ev_job_failed_from_error (job, error);
1163 g_error_free (error);
1165 ev_job_succeeded (job);
1172 ev_job_save_class_init (EvJobSaveClass *class)
1174 GObjectClass *oclass = G_OBJECT_CLASS (class);
1175 EvJobClass *job_class = EV_JOB_CLASS (class);
1177 oclass->dispose = ev_job_save_dispose;
1178 job_class->run = ev_job_save_run;
1182 ev_job_save_new (EvDocument *document,
1184 const gchar *document_uri)
1188 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", uri, document_uri);
1190 job = g_object_new (EV_TYPE_JOB_SAVE, NULL);
1192 EV_JOB (job)->document = g_object_ref (document);
1193 job->uri = g_strdup (uri);
1194 job->document_uri = g_strdup (document_uri);
1196 return EV_JOB (job);
1201 ev_job_find_init (EvJobFind *job)
1203 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
1207 ev_job_find_dispose (GObject *object)
1209 EvJobFind *job = EV_JOB_FIND (object);
1211 ev_debug_message (DEBUG_JOBS, NULL);
1221 for (i = 0; i < job->n_pages; i++) {
1222 g_list_foreach (job->pages[i], (GFunc)ev_rectangle_free, NULL);
1223 g_list_free (job->pages[i]);
1226 g_free (job->pages);
1230 (* G_OBJECT_CLASS (ev_job_find_parent_class)->dispose) (object);
1234 ev_job_find_run (EvJob *job)
1236 EvJobFind *job_find = EV_JOB_FIND (job);
1237 EvDocumentFind *find = EV_DOCUMENT_FIND (job->document);
1241 ev_debug_message (DEBUG_JOBS, NULL);
1243 /* Do not block the main loop */
1244 if (!ev_document_doc_mutex_trylock ())
1247 #ifdef EV_ENABLE_DEBUG
1248 /* We use the #ifdef in this case because of the if */
1249 if (job_find->current_page == job_find->start_page)
1250 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1253 ev_page = ev_document_get_page (job->document, job_find->current_page);
1254 matches = ev_document_find_find_text (find, ev_page, job_find->text,
1255 job_find->case_sensitive);
1256 g_object_unref (ev_page);
1258 ev_document_doc_mutex_unlock ();
1260 if (!job_find->has_results)
1261 job_find->has_results = (matches != NULL);
1263 job_find->pages[job_find->current_page] = matches;
1264 g_signal_emit (job_find, job_find_signals[FIND_UPDATED], 0, job_find->current_page);
1266 job_find->current_page = (job_find->current_page + 1) % job_find->n_pages;
1267 if (job_find->current_page == job_find->start_page) {
1268 ev_job_succeeded (job);
1277 ev_job_find_class_init (EvJobFindClass *class)
1279 EvJobClass *job_class = EV_JOB_CLASS (class);
1280 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1282 job_class->run = ev_job_find_run;
1283 gobject_class->dispose = ev_job_find_dispose;
1285 job_find_signals[FIND_UPDATED] =
1286 g_signal_new ("updated",
1289 G_STRUCT_OFFSET (EvJobFindClass, updated),
1291 g_cclosure_marshal_VOID__INT,
1297 ev_job_find_new (EvDocument *document,
1301 gboolean case_sensitive)
1305 ev_debug_message (DEBUG_JOBS, NULL);
1307 job = g_object_new (EV_TYPE_JOB_FIND, NULL);
1309 EV_JOB (job)->document = g_object_ref (document);
1310 job->start_page = start_page;
1311 job->current_page = start_page;
1312 job->n_pages = n_pages;
1313 job->pages = g_new0 (GList *, n_pages);
1314 job->text = g_strdup (text);
1315 job->case_sensitive = case_sensitive;
1316 job->has_results = FALSE;
1318 return EV_JOB (job);
1322 ev_job_find_get_n_results (EvJobFind *job,
1325 return g_list_length (job->pages[page]);
1329 ev_job_find_get_progress (EvJobFind *job)
1333 if (ev_job_is_finished (EV_JOB (job)))
1336 if (job->current_page > job->start_page) {
1337 pages_done = job->current_page - job->start_page + 1;
1338 } else if (job->current_page == job->start_page) {
1339 pages_done = job->n_pages;
1341 pages_done = job->n_pages - job->start_page + job->current_page;
1344 return pages_done / (gdouble) job->n_pages;
1348 ev_job_find_has_results (EvJobFind *job)
1350 return job->has_results;
1354 ev_job_find_get_results (EvJobFind *job)
1361 ev_job_layers_init (EvJobLayers *job)
1363 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1367 ev_job_layers_dispose (GObject *object)
1371 ev_debug_message (DEBUG_JOBS, NULL);
1373 job = EV_JOB_LAYERS (object);
1376 g_object_unref (job->model);
1380 (* G_OBJECT_CLASS (ev_job_layers_parent_class)->dispose) (object);
1384 ev_job_layers_run (EvJob *job)
1386 EvJobLayers *job_layers = EV_JOB_LAYERS (job);
1388 ev_debug_message (DEBUG_JOBS, NULL);
1389 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1391 ev_document_doc_mutex_lock ();
1392 job_layers->model = ev_document_layers_get_layers (EV_DOCUMENT_LAYERS (job->document));
1393 ev_document_doc_mutex_unlock ();
1395 ev_job_succeeded (job);
1401 ev_job_layers_class_init (EvJobLayersClass *class)
1403 GObjectClass *oclass = G_OBJECT_CLASS (class);
1404 EvJobClass *job_class = EV_JOB_CLASS (class);
1406 oclass->dispose = ev_job_layers_dispose;
1407 job_class->run = ev_job_layers_run;
1411 ev_job_layers_new (EvDocument *document)
1415 ev_debug_message (DEBUG_JOBS, NULL);
1417 job = g_object_new (EV_TYPE_JOB_LAYERS, NULL);
1418 job->document = g_object_ref (document);
1425 ev_job_export_init (EvJobExport *job)
1427 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1432 ev_job_export_dispose (GObject *object)
1436 ev_debug_message (DEBUG_JOBS, NULL);
1438 job = EV_JOB_EXPORT (object);
1441 g_object_unref (job->rc);
1445 (* G_OBJECT_CLASS (ev_job_export_parent_class)->dispose) (object);
1449 ev_job_export_run (EvJob *job)
1451 EvJobExport *job_export = EV_JOB_EXPORT (job);
1454 g_assert (job_export->page != -1);
1456 ev_debug_message (DEBUG_JOBS, NULL);
1457 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1459 ev_document_doc_mutex_lock ();
1461 ev_page = ev_document_get_page (job->document, job_export->page);
1462 if (job_export->rc) {
1463 job->failed = FALSE;
1464 job->finished = FALSE;
1465 g_clear_error (&job->error);
1467 ev_render_context_set_page (job_export->rc, ev_page);
1469 job_export->rc = ev_render_context_new (ev_page, 0, 1.0);
1471 g_object_unref (ev_page);
1473 ev_file_exporter_do_page (EV_FILE_EXPORTER (job->document), job_export->rc);
1475 ev_document_doc_mutex_unlock ();
1477 ev_job_succeeded (job);
1483 ev_job_export_class_init (EvJobExportClass *class)
1485 GObjectClass *oclass = G_OBJECT_CLASS (class);
1486 EvJobClass *job_class = EV_JOB_CLASS (class);
1488 oclass->dispose = ev_job_export_dispose;
1489 job_class->run = ev_job_export_run;
1493 ev_job_export_new (EvDocument *document)
1497 ev_debug_message (DEBUG_JOBS, NULL);
1499 job = g_object_new (EV_TYPE_JOB_EXPORT, NULL);
1500 job->document = g_object_ref (document);
1506 ev_job_export_set_page (EvJobExport *job,
1514 ev_job_print_init (EvJobPrint *job)
1516 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1521 ev_job_print_dispose (GObject *object)
1525 ev_debug_message (DEBUG_JOBS, NULL);
1527 job = EV_JOB_PRINT (object);
1530 cairo_destroy (job->cr);
1534 (* G_OBJECT_CLASS (ev_job_print_parent_class)->dispose) (object);
1538 ev_job_print_run (EvJob *job)
1540 EvJobPrint *job_print = EV_JOB_PRINT (job);
1542 cairo_status_t cr_status;
1544 g_assert (job_print->page != -1);
1545 g_assert (job_print->cr != NULL);
1547 ev_debug_message (DEBUG_JOBS, NULL);
1548 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1550 job->failed = FALSE;
1551 job->finished = FALSE;
1552 g_clear_error (&job->error);
1554 ev_document_doc_mutex_lock ();
1556 ev_page = ev_document_get_page (job->document, job_print->page);
1557 ev_document_print_print_page (EV_DOCUMENT_PRINT (job->document),
1558 ev_page, job_print->cr);
1559 g_object_unref (ev_page);
1561 ev_document_doc_mutex_unlock ();
1563 if (g_cancellable_is_cancelled (job->cancellable))
1566 cr_status = cairo_status (job_print->cr);
1567 if (cr_status == CAIRO_STATUS_SUCCESS) {
1568 ev_job_succeeded (job);
1572 GTK_PRINT_ERROR_GENERAL,
1573 _("Failed to print page %d: %s"),
1575 cairo_status_to_string (cr_status));
1582 ev_job_print_class_init (EvJobPrintClass *class)
1584 GObjectClass *oclass = G_OBJECT_CLASS (class);
1585 EvJobClass *job_class = EV_JOB_CLASS (class);
1587 oclass->dispose = ev_job_print_dispose;
1588 job_class->run = ev_job_print_run;
1592 ev_job_print_new (EvDocument *document)
1596 ev_debug_message (DEBUG_JOBS, NULL);
1598 job = g_object_new (EV_TYPE_JOB_PRINT, NULL);
1599 job->document = g_object_ref (document);
1605 ev_job_print_set_page (EvJobPrint *job,
1612 ev_job_print_set_cairo (EvJobPrint *job,
1619 cairo_destroy (job->cr);
1620 job->cr = cr ? cairo_reference (cr) : NULL;