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"
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_render_init (EvJobRender *job);
53 static void ev_job_render_class_init (EvJobRenderClass *class);
54 static void ev_job_page_data_init (EvJobPageData *job);
55 static void ev_job_page_data_class_init (EvJobPageDataClass *class);
56 static void ev_job_thumbnail_init (EvJobThumbnail *job);
57 static void ev_job_thumbnail_class_init (EvJobThumbnailClass *class);
58 static void ev_job_load_init (EvJobLoad *job);
59 static void ev_job_load_class_init (EvJobLoadClass *class);
60 static void ev_job_save_init (EvJobSave *job);
61 static void ev_job_save_class_init (EvJobSaveClass *class);
62 static void ev_job_find_init (EvJobFind *job);
63 static void ev_job_find_class_init (EvJobFindClass *class);
64 static void ev_job_layers_init (EvJobLayers *job);
65 static void ev_job_layers_class_init (EvJobLayersClass *class);
66 static void ev_job_export_init (EvJobExport *job);
67 static void ev_job_export_class_init (EvJobExportClass *class);
68 static void ev_job_print_init (EvJobPrint *job);
69 static void ev_job_print_class_init (EvJobPrintClass *class);
87 static guint job_signals[LAST_SIGNAL] = { 0 };
88 static guint job_fonts_signals[FONTS_LAST_SIGNAL] = { 0 };
89 static guint job_find_signals[FIND_LAST_SIGNAL] = { 0 };
91 G_DEFINE_ABSTRACT_TYPE (EvJob, ev_job, G_TYPE_OBJECT)
92 G_DEFINE_TYPE (EvJobLinks, ev_job_links, EV_TYPE_JOB)
93 G_DEFINE_TYPE (EvJobAttachments, ev_job_attachments, EV_TYPE_JOB)
94 G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB)
95 G_DEFINE_TYPE (EvJobPageData, ev_job_page_data, EV_TYPE_JOB)
96 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
97 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
98 G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB)
99 G_DEFINE_TYPE (EvJobSave, ev_job_save, EV_TYPE_JOB)
100 G_DEFINE_TYPE (EvJobFind, ev_job_find, EV_TYPE_JOB)
101 G_DEFINE_TYPE (EvJobLayers, ev_job_layers, EV_TYPE_JOB)
102 G_DEFINE_TYPE (EvJobExport, ev_job_export, EV_TYPE_JOB)
103 G_DEFINE_TYPE (EvJobPrint, ev_job_print, EV_TYPE_JOB)
107 ev_job_init (EvJob *job)
109 job->cancellable = g_cancellable_new ();
113 ev_job_dispose (GObject *object)
117 job = EV_JOB (object);
120 g_object_unref (job->document);
121 job->document = NULL;
124 if (job->cancellable) {
125 g_object_unref (job->cancellable);
126 job->cancellable = NULL;
130 g_error_free (job->error);
134 (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object);
138 ev_job_class_init (EvJobClass *class)
140 GObjectClass *oclass;
142 oclass = G_OBJECT_CLASS (class);
144 oclass->dispose = ev_job_dispose;
146 job_signals[CANCELLED] =
147 g_signal_new ("cancelled",
150 G_STRUCT_OFFSET (EvJobClass, cancelled),
152 g_cclosure_marshal_VOID__VOID,
154 job_signals [FINISHED] =
155 g_signal_new ("finished",
158 G_STRUCT_OFFSET (EvJobClass, finished),
160 g_cclosure_marshal_VOID__VOID,
165 emit_finished (EvJob *job)
167 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
169 job->idle_finished_id = 0;
171 if (job->cancelled) {
172 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit finished", EV_GET_TYPE_NAME (job), job);
174 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
175 g_signal_emit (job, job_signals[FINISHED], 0);
182 ev_job_emit_finished (EvJob *job)
184 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
186 if (g_cancellable_is_cancelled (job->cancellable)) {
187 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, returning", EV_GET_TYPE_NAME (job), job);
191 job->finished = TRUE;
193 if (job->run_mode == EV_JOB_RUN_THREAD) {
194 job->idle_finished_id =
195 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
196 (GSourceFunc)emit_finished,
198 (GDestroyNotify)g_object_unref);
200 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
201 g_signal_emit (job, job_signals[FINISHED], 0);
206 ev_job_run (EvJob *job)
208 EvJobClass *class = EV_JOB_GET_CLASS (job);
210 return class->run (job);
214 ev_job_cancel (EvJob *job)
216 if (job->cancelled || (job->finished && job->idle_finished_id == 0))
219 ev_debug_message (DEBUG_JOBS, "job %s (%p) cancelled", EV_GET_TYPE_NAME (job), job);
220 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
222 /* This should never be called from a thread */
223 job->cancelled = TRUE;
224 g_cancellable_cancel (job->cancellable);
225 g_signal_emit (job, job_signals[CANCELLED], 0);
229 ev_job_failed (EvJob *job,
238 if (job->failed || job->finished)
241 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
245 va_start (args, format);
246 message = g_strdup_vprintf (format, args);
249 job->error = g_error_new_literal (domain, code, message);
252 ev_job_emit_finished (job);
256 ev_job_failed_from_error (EvJob *job,
259 if (job->failed || job->finished)
262 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
265 job->error = g_error_copy (error);
267 ev_job_emit_finished (job);
271 ev_job_succeeded (EvJob *job)
276 ev_debug_message (DEBUG_JOBS, "job %s (%p) succeeded", EV_GET_TYPE_NAME (job), job);
279 ev_job_emit_finished (job);
283 ev_job_is_finished (EvJob *job)
285 return job->finished;
289 ev_job_is_failed (EvJob *job)
295 ev_job_get_run_mode (EvJob *job)
297 return job->run_mode;
301 ev_job_set_run_mode (EvJob *job,
302 EvJobRunMode run_mode)
304 job->run_mode = run_mode;
309 ev_job_links_init (EvJobLinks *job)
311 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
315 ev_job_links_dispose (GObject *object)
319 ev_debug_message (DEBUG_JOBS, NULL);
321 job = EV_JOB_LINKS (object);
324 g_object_unref (job->model);
328 (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object);
332 ev_job_links_run (EvJob *job)
334 EvJobLinks *job_links = EV_JOB_LINKS (job);
336 ev_debug_message (DEBUG_JOBS, NULL);
337 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
339 ev_document_doc_mutex_lock ();
340 job_links->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (job->document));
341 ev_document_doc_mutex_unlock ();
343 ev_job_succeeded (job);
349 ev_job_links_class_init (EvJobLinksClass *class)
351 GObjectClass *oclass = G_OBJECT_CLASS (class);
352 EvJobClass *job_class = EV_JOB_CLASS (class);
354 oclass->dispose = ev_job_links_dispose;
355 job_class->run = ev_job_links_run;
359 ev_job_links_new (EvDocument *document)
363 ev_debug_message (DEBUG_JOBS, NULL);
365 job = g_object_new (EV_TYPE_JOB_LINKS, NULL);
366 job->document = g_object_ref (document);
371 /* EvJobAttachments */
373 ev_job_attachments_init (EvJobAttachments *job)
375 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
379 ev_job_attachments_dispose (GObject *object)
381 EvJobAttachments *job;
383 ev_debug_message (DEBUG_JOBS, NULL);
385 job = EV_JOB_ATTACHMENTS (object);
387 if (job->attachments) {
388 g_list_foreach (job->attachments, (GFunc)g_object_unref, NULL);
389 g_list_free (job->attachments);
390 job->attachments = NULL;
393 (* G_OBJECT_CLASS (ev_job_attachments_parent_class)->dispose) (object);
397 ev_job_attachments_run (EvJob *job)
399 EvJobAttachments *job_attachments = EV_JOB_ATTACHMENTS (job);
401 ev_debug_message (DEBUG_JOBS, NULL);
402 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
404 ev_document_doc_mutex_lock ();
405 job_attachments->attachments =
406 ev_document_attachments_get_attachments (EV_DOCUMENT_ATTACHMENTS (job->document));
407 ev_document_doc_mutex_unlock ();
409 ev_job_succeeded (job);
415 ev_job_attachments_class_init (EvJobAttachmentsClass *class)
417 GObjectClass *oclass = G_OBJECT_CLASS (class);
418 EvJobClass *job_class = EV_JOB_CLASS (class);
420 oclass->dispose = ev_job_attachments_dispose;
421 job_class->run = ev_job_attachments_run;
425 ev_job_attachments_new (EvDocument *document)
429 ev_debug_message (DEBUG_JOBS, NULL);
431 job = g_object_new (EV_TYPE_JOB_ATTACHMENTS, NULL);
432 job->document = g_object_ref (document);
439 ev_job_render_init (EvJobRender *job)
441 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
445 ev_job_render_dispose (GObject *object)
449 job = EV_JOB_RENDER (object);
451 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job->page, job);
454 cairo_surface_destroy (job->surface);
458 if (job->selection) {
459 cairo_surface_destroy (job->selection);
460 job->selection = NULL;
463 if (job->selection_region) {
464 gdk_region_destroy (job->selection_region);
465 job->selection_region = NULL;
468 (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
472 ev_job_render_run (EvJob *job)
474 EvJobRender *job_render = EV_JOB_RENDER (job);
478 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_render->page, job);
479 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
481 ev_document_doc_mutex_lock ();
483 ev_profiler_start (EV_PROFILE_JOBS, "Rendering page %d", job_render->page);
485 ev_document_fc_mutex_lock ();
487 ev_page = ev_document_get_page (job->document, job_render->page);
488 rc = ev_render_context_new (ev_page, job_render->rotation, job_render->scale);
489 g_object_unref (ev_page);
491 job_render->surface = ev_document_render (job->document, rc);
492 /* If job was cancelled during the page rendering,
493 * we return now, so that the thread is finished ASAP
495 if (g_cancellable_is_cancelled (job->cancellable)) {
496 ev_document_fc_mutex_unlock ();
497 ev_document_doc_mutex_unlock ();
503 if (job_render->include_selection && EV_IS_SELECTION (job->document)) {
504 ev_selection_render_selection (EV_SELECTION (job->document),
506 &(job_render->selection),
507 &(job_render->selection_points),
509 job_render->selection_style,
510 &(job_render->text), &(job_render->base));
511 job_render->selection_region =
512 ev_selection_get_selection_region (EV_SELECTION (job->document),
514 job_render->selection_style,
515 &(job_render->selection_points));
520 ev_document_fc_mutex_unlock ();
521 ev_document_doc_mutex_unlock ();
523 ev_job_succeeded (job);
529 ev_job_render_class_init (EvJobRenderClass *class)
531 GObjectClass *oclass = G_OBJECT_CLASS (class);
532 EvJobClass *job_class = EV_JOB_CLASS (class);
534 oclass->dispose = ev_job_render_dispose;
535 job_class->run = ev_job_render_run;
539 ev_job_render_new (EvDocument *document,
548 ev_debug_message (DEBUG_JOBS, "page: %d", page);
550 job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
552 EV_JOB (job)->document = g_object_ref (document);
554 job->rotation = rotation;
556 job->target_width = width;
557 job->target_height = height;
563 ev_job_render_set_selection_info (EvJobRender *job,
564 EvRectangle *selection_points,
565 EvSelectionStyle selection_style,
569 job->include_selection = TRUE;
571 job->selection_points = *selection_points;
572 job->selection_style = selection_style;
579 ev_job_page_data_init (EvJobPageData *job)
581 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
585 ev_job_page_data_run (EvJob *job)
587 EvJobPageData *job_pd = EV_JOB_PAGE_DATA (job);
590 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_pd->page, job);
591 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
593 ev_document_doc_mutex_lock ();
594 ev_page = ev_document_get_page (job->document, job_pd->page);
596 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT) && EV_IS_SELECTION (job->document))
597 job_pd->text_mapping =
598 ev_selection_get_selection_map (EV_SELECTION (job->document), ev_page);
599 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_LINKS) && EV_IS_DOCUMENT_LINKS (job->document))
600 job_pd->link_mapping =
601 ev_document_links_get_links (EV_DOCUMENT_LINKS (job->document), ev_page);
602 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_FORMS) && EV_IS_DOCUMENT_FORMS (job->document))
603 job_pd->form_field_mapping =
604 ev_document_forms_get_form_fields (EV_DOCUMENT_FORMS (job->document),
606 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_IMAGES) && EV_IS_DOCUMENT_IMAGES (job->document))
607 job_pd->image_mapping =
608 ev_document_images_get_image_mapping (EV_DOCUMENT_IMAGES (job->document),
610 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_ANNOTS) && EV_IS_DOCUMENT_ANNOTATIONS (job->document))
611 job_pd->annot_mapping =
612 ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document),
614 g_object_unref (ev_page);
615 ev_document_doc_mutex_unlock ();
617 ev_job_succeeded (job);
623 ev_job_page_data_class_init (EvJobPageDataClass *class)
625 EvJobClass *job_class = EV_JOB_CLASS (class);
627 job_class->run = ev_job_page_data_run;
631 ev_job_page_data_new (EvDocument *document,
633 EvJobPageDataFlags flags)
637 ev_debug_message (DEBUG_JOBS, "%d", page);
639 job = g_object_new (EV_TYPE_JOB_PAGE_DATA, NULL);
641 EV_JOB (job)->document = g_object_ref (document);
650 ev_job_thumbnail_init (EvJobThumbnail *job)
652 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
656 ev_job_thumbnail_dispose (GObject *object)
660 job = EV_JOB_THUMBNAIL (object);
662 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->page, job);
664 if (job->thumbnail) {
665 g_object_unref (job->thumbnail);
666 job->thumbnail = NULL;
669 (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
673 ev_job_thumbnail_run (EvJob *job)
675 EvJobThumbnail *job_thumb = EV_JOB_THUMBNAIL (job);
679 ev_debug_message (DEBUG_JOBS, "%d (%p)", job_thumb->page, job);
680 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
682 ev_document_doc_mutex_lock ();
684 page = ev_document_get_page (job->document, job_thumb->page);
685 rc = ev_render_context_new (page, job_thumb->rotation, job_thumb->scale);
686 g_object_unref (page);
688 job_thumb->thumbnail = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (job->document),
691 ev_document_doc_mutex_unlock ();
693 ev_job_succeeded (job);
699 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
701 GObjectClass *oclass = G_OBJECT_CLASS (class);
702 EvJobClass *job_class = EV_JOB_CLASS (class);
704 oclass->dispose = ev_job_thumbnail_dispose;
705 job_class->run = ev_job_thumbnail_run;
709 ev_job_thumbnail_new (EvDocument *document,
716 ev_debug_message (DEBUG_JOBS, "%d", page);
718 job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
720 EV_JOB (job)->document = g_object_ref (document);
722 job->rotation = rotation;
730 ev_job_fonts_init (EvJobFonts *job)
732 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
736 ev_job_fonts_run (EvJob *job)
738 EvJobFonts *job_fonts = EV_JOB_FONTS (job);
739 EvDocumentFonts *fonts = EV_DOCUMENT_FONTS (job->document);
741 ev_debug_message (DEBUG_JOBS, NULL);
743 /* Do not block the main loop */
744 if (!ev_document_doc_mutex_trylock ())
747 if (!ev_document_fc_mutex_trylock ())
750 #ifdef EV_ENABLE_DEBUG
751 /* We use the #ifdef in this case because of the if */
752 if (ev_document_fonts_get_progress (fonts) == 0)
753 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
756 job_fonts->scan_completed = !ev_document_fonts_scan (fonts, 20);
757 g_signal_emit (job_fonts, job_fonts_signals[FONTS_UPDATED], 0,
758 ev_document_fonts_get_progress (fonts));
760 ev_document_fc_mutex_unlock ();
761 ev_document_doc_mutex_unlock ();
763 if (job_fonts->scan_completed)
764 ev_job_succeeded (job);
766 return !job_fonts->scan_completed;
770 ev_job_fonts_class_init (EvJobFontsClass *class)
772 EvJobClass *job_class = EV_JOB_CLASS (class);
774 job_class->run = ev_job_fonts_run;
776 job_fonts_signals[FONTS_UPDATED] =
777 g_signal_new ("updated",
780 G_STRUCT_OFFSET (EvJobFontsClass, updated),
782 g_cclosure_marshal_VOID__DOUBLE,
788 ev_job_fonts_new (EvDocument *document)
792 ev_debug_message (DEBUG_JOBS, NULL);
794 job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
796 EV_JOB (job)->document = g_object_ref (document);
803 ev_job_load_init (EvJobLoad *job)
805 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
809 ev_job_load_dispose (GObject *object)
811 EvJobLoad *job = EV_JOB_LOAD (object);
813 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
821 g_free (job->password);
822 job->password = NULL;
825 (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object);
829 ev_job_load_run (EvJob *job)
831 EvJobLoad *job_load = EV_JOB_LOAD (job);
832 GError *error = NULL;
834 ev_debug_message (DEBUG_JOBS, "%s", job_load->uri);
835 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
837 ev_document_fc_mutex_lock ();
839 /* This job may already have a document even if the job didn't complete
840 because, e.g., a password is required - if so, just reload rather than
841 creating a new instance */
843 const gchar *uncompressed_uri;
845 if (job_load->password) {
846 ev_document_security_set_password (EV_DOCUMENT_SECURITY (job->document),
851 job->finished = FALSE;
852 g_clear_error (&job->error);
854 uncompressed_uri = g_object_get_data (G_OBJECT (job->document),
856 ev_document_load (job->document,
857 uncompressed_uri ? uncompressed_uri : job_load->uri,
860 job->document = ev_document_factory_get_document (job_load->uri,
864 ev_document_fc_mutex_unlock ();
867 ev_job_failed_from_error (job, error);
868 g_error_free (error);
870 ev_job_succeeded (job);
877 ev_job_load_class_init (EvJobLoadClass *class)
879 GObjectClass *oclass = G_OBJECT_CLASS (class);
880 EvJobClass *job_class = EV_JOB_CLASS (class);
882 oclass->dispose = ev_job_load_dispose;
883 job_class->run = ev_job_load_run;
887 ev_job_load_new (const gchar *uri)
891 ev_debug_message (DEBUG_JOBS, "%s", uri);
893 job = g_object_new (EV_TYPE_JOB_LOAD, NULL);
894 job->uri = g_strdup (uri);
900 ev_job_load_set_uri (EvJobLoad *job, const gchar *uri)
902 ev_debug_message (DEBUG_JOBS, "%s", uri);
906 job->uri = g_strdup (uri);
910 ev_job_load_set_password (EvJobLoad *job, const gchar *password)
912 ev_debug_message (DEBUG_JOBS, NULL);
915 g_free (job->password);
916 job->password = password ? g_strdup (password) : NULL;
921 ev_job_save_init (EvJobSave *job)
923 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
927 ev_job_save_dispose (GObject *object)
929 EvJobSave *job = EV_JOB_SAVE (object);
931 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
938 if (job->document_uri) {
939 g_free (job->document_uri);
940 job->document_uri = NULL;
943 (* G_OBJECT_CLASS (ev_job_save_parent_class)->dispose) (object);
947 ev_job_save_run (EvJob *job)
949 EvJobSave *job_save = EV_JOB_SAVE (job);
951 gchar *tmp_filename = NULL;
953 GError *error = NULL;
955 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", job_save->uri, job_save->document_uri);
956 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
958 fd = ev_mkstemp ("saveacopy.XXXXXX", &tmp_filename, &error);
960 ev_job_failed_from_error (job, error);
961 g_error_free (error);
966 ev_document_doc_mutex_lock ();
968 /* Save document to temp filename */
969 local_uri = g_filename_to_uri (tmp_filename, NULL, &error);
970 if (local_uri != NULL) {
971 ev_document_save (job->document, local_uri, &error);
976 ev_document_doc_mutex_unlock ();
980 ev_job_failed_from_error (job, error);
981 g_error_free (error);
986 /* If original document was compressed,
987 * compress it again before saving
989 if (g_object_get_data (G_OBJECT (job->document), "uri-uncompressed")) {
990 EvCompressionType ctype = EV_COMPRESSION_NONE;
994 ext = g_strrstr (job_save->document_uri, ".gz");
995 if (ext && g_ascii_strcasecmp (ext, ".gz") == 0)
996 ctype = EV_COMPRESSION_GZIP;
998 ext = g_strrstr (job_save->document_uri, ".bz2");
999 if (ext && g_ascii_strcasecmp (ext, ".bz2") == 0)
1000 ctype = EV_COMPRESSION_BZIP2;
1002 uri_comp = ev_file_compress (local_uri, ctype, &error);
1004 g_unlink (tmp_filename);
1006 if (!uri_comp || error) {
1009 local_uri = uri_comp;
1013 g_free (tmp_filename);
1017 ev_job_failed_from_error (job, error);
1018 g_error_free (error);
1026 ev_xfer_uri_simple (local_uri, job_save->uri, &error);
1027 ev_tmp_uri_unlink (local_uri);
1030 ev_job_failed_from_error (job, error);
1031 g_error_free (error);
1033 ev_job_succeeded (job);
1040 ev_job_save_class_init (EvJobSaveClass *class)
1042 GObjectClass *oclass = G_OBJECT_CLASS (class);
1043 EvJobClass *job_class = EV_JOB_CLASS (class);
1045 oclass->dispose = ev_job_save_dispose;
1046 job_class->run = ev_job_save_run;
1050 ev_job_save_new (EvDocument *document,
1052 const gchar *document_uri)
1056 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", uri, document_uri);
1058 job = g_object_new (EV_TYPE_JOB_SAVE, NULL);
1060 EV_JOB (job)->document = g_object_ref (document);
1061 job->uri = g_strdup (uri);
1062 job->document_uri = g_strdup (document_uri);
1064 return EV_JOB (job);
1069 ev_job_find_init (EvJobFind *job)
1071 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
1075 ev_job_find_dispose (GObject *object)
1077 EvJobFind *job = EV_JOB_FIND (object);
1079 ev_debug_message (DEBUG_JOBS, NULL);
1089 for (i = 0; i < job->n_pages; i++) {
1090 g_list_foreach (job->pages[i], (GFunc)ev_rectangle_free, NULL);
1091 g_list_free (job->pages[i]);
1094 g_free (job->pages);
1098 (* G_OBJECT_CLASS (ev_job_find_parent_class)->dispose) (object);
1102 ev_job_find_run (EvJob *job)
1104 EvJobFind *job_find = EV_JOB_FIND (job);
1105 EvDocumentFind *find = EV_DOCUMENT_FIND (job->document);
1109 ev_debug_message (DEBUG_JOBS, NULL);
1111 /* Do not block the main loop */
1112 if (!ev_document_doc_mutex_trylock ())
1115 #ifdef EV_ENABLE_DEBUG
1116 /* We use the #ifdef in this case because of the if */
1117 if (job_find->current_page == job_find->start_page)
1118 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1121 ev_page = ev_document_get_page (job->document, job_find->current_page);
1122 matches = ev_document_find_find_text (find, ev_page, job_find->text,
1123 job_find->case_sensitive);
1124 g_object_unref (ev_page);
1126 ev_document_doc_mutex_unlock ();
1128 if (!job_find->has_results)
1129 job_find->has_results = (matches != NULL);
1131 job_find->pages[job_find->current_page] = matches;
1132 g_signal_emit (job_find, job_find_signals[FIND_UPDATED], 0, job_find->current_page);
1134 job_find->current_page = (job_find->current_page + 1) % job_find->n_pages;
1135 if (job_find->current_page == job_find->start_page) {
1136 ev_job_succeeded (job);
1145 ev_job_find_class_init (EvJobFindClass *class)
1147 EvJobClass *job_class = EV_JOB_CLASS (class);
1148 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1150 job_class->run = ev_job_find_run;
1151 gobject_class->dispose = ev_job_find_dispose;
1153 job_find_signals[FIND_UPDATED] =
1154 g_signal_new ("updated",
1157 G_STRUCT_OFFSET (EvJobFindClass, updated),
1159 g_cclosure_marshal_VOID__INT,
1165 ev_job_find_new (EvDocument *document,
1169 gboolean case_sensitive)
1173 ev_debug_message (DEBUG_JOBS, NULL);
1175 job = g_object_new (EV_TYPE_JOB_FIND, NULL);
1177 EV_JOB (job)->document = g_object_ref (document);
1178 job->start_page = start_page;
1179 job->current_page = start_page;
1180 job->n_pages = n_pages;
1181 job->pages = g_new0 (GList *, n_pages);
1182 job->text = g_strdup (text);
1183 job->case_sensitive = case_sensitive;
1184 job->has_results = FALSE;
1186 return EV_JOB (job);
1190 ev_job_find_get_n_results (EvJobFind *job,
1193 return g_list_length (job->pages[page]);
1197 ev_job_find_get_progress (EvJobFind *job)
1201 if (ev_job_is_finished (EV_JOB (job)))
1204 if (job->current_page > job->start_page) {
1205 pages_done = job->current_page - job->start_page + 1;
1206 } else if (job->current_page == job->start_page) {
1207 pages_done = job->n_pages;
1209 pages_done = job->n_pages - job->start_page + job->current_page;
1212 return pages_done / (gdouble) job->n_pages;
1216 ev_job_find_has_results (EvJobFind *job)
1218 return job->has_results;
1222 ev_job_find_get_results (EvJobFind *job)
1229 ev_job_layers_init (EvJobLayers *job)
1231 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1235 ev_job_layers_dispose (GObject *object)
1239 ev_debug_message (DEBUG_JOBS, NULL);
1241 job = EV_JOB_LAYERS (object);
1244 g_object_unref (job->model);
1248 (* G_OBJECT_CLASS (ev_job_layers_parent_class)->dispose) (object);
1252 ev_job_layers_run (EvJob *job)
1254 EvJobLayers *job_layers = EV_JOB_LAYERS (job);
1256 ev_debug_message (DEBUG_JOBS, NULL);
1257 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1259 ev_document_doc_mutex_lock ();
1260 job_layers->model = ev_document_layers_get_layers (EV_DOCUMENT_LAYERS (job->document));
1261 ev_document_doc_mutex_unlock ();
1263 ev_job_succeeded (job);
1269 ev_job_layers_class_init (EvJobLayersClass *class)
1271 GObjectClass *oclass = G_OBJECT_CLASS (class);
1272 EvJobClass *job_class = EV_JOB_CLASS (class);
1274 oclass->dispose = ev_job_layers_dispose;
1275 job_class->run = ev_job_layers_run;
1279 ev_job_layers_new (EvDocument *document)
1283 ev_debug_message (DEBUG_JOBS, NULL);
1285 job = g_object_new (EV_TYPE_JOB_LAYERS, NULL);
1286 job->document = g_object_ref (document);
1293 ev_job_export_init (EvJobExport *job)
1295 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1300 ev_job_export_dispose (GObject *object)
1304 ev_debug_message (DEBUG_JOBS, NULL);
1306 job = EV_JOB_EXPORT (object);
1309 g_object_unref (job->rc);
1313 (* G_OBJECT_CLASS (ev_job_export_parent_class)->dispose) (object);
1317 ev_job_export_run (EvJob *job)
1319 EvJobExport *job_export = EV_JOB_EXPORT (job);
1322 g_assert (job_export->page != -1);
1324 ev_debug_message (DEBUG_JOBS, NULL);
1325 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1327 ev_document_doc_mutex_lock ();
1329 ev_page = ev_document_get_page (job->document, job_export->page);
1330 if (job_export->rc) {
1331 job->failed = FALSE;
1332 job->finished = FALSE;
1333 g_clear_error (&job->error);
1335 ev_render_context_set_page (job_export->rc, ev_page);
1337 job_export->rc = ev_render_context_new (ev_page, 0, 1.0);
1339 g_object_unref (ev_page);
1341 ev_file_exporter_do_page (EV_FILE_EXPORTER (job->document), job_export->rc);
1343 ev_document_doc_mutex_unlock ();
1345 ev_job_succeeded (job);
1351 ev_job_export_class_init (EvJobExportClass *class)
1353 GObjectClass *oclass = G_OBJECT_CLASS (class);
1354 EvJobClass *job_class = EV_JOB_CLASS (class);
1356 oclass->dispose = ev_job_export_dispose;
1357 job_class->run = ev_job_export_run;
1361 ev_job_export_new (EvDocument *document)
1365 ev_debug_message (DEBUG_JOBS, NULL);
1367 job = g_object_new (EV_TYPE_JOB_EXPORT, NULL);
1368 job->document = g_object_ref (document);
1374 ev_job_export_set_page (EvJobExport *job,
1382 ev_job_print_init (EvJobPrint *job)
1384 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1389 ev_job_print_dispose (GObject *object)
1393 ev_debug_message (DEBUG_JOBS, NULL);
1395 job = EV_JOB_PRINT (object);
1398 cairo_destroy (job->cr);
1402 (* G_OBJECT_CLASS (ev_job_print_parent_class)->dispose) (object);
1406 ev_job_print_run (EvJob *job)
1408 EvJobPrint *job_print = EV_JOB_PRINT (job);
1410 cairo_status_t cr_status;
1412 g_assert (job_print->page != -1);
1413 g_assert (job_print->cr != NULL);
1415 ev_debug_message (DEBUG_JOBS, NULL);
1416 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1418 job->failed = FALSE;
1419 job->finished = FALSE;
1420 g_clear_error (&job->error);
1422 ev_document_doc_mutex_lock ();
1424 ev_page = ev_document_get_page (job->document, job_print->page);
1425 ev_document_print_print_page (EV_DOCUMENT_PRINT (job->document),
1426 ev_page, job_print->cr);
1427 g_object_unref (ev_page);
1429 ev_document_doc_mutex_unlock ();
1431 cr_status = cairo_status (job_print->cr);
1432 if (cr_status == CAIRO_STATUS_SUCCESS) {
1433 ev_job_succeeded (job);
1437 GTK_PRINT_ERROR_GENERAL,
1438 _("Failed to print page %d: %s"),
1440 cairo_status_to_string (cr_status));
1447 ev_job_print_class_init (EvJobPrintClass *class)
1449 GObjectClass *oclass = G_OBJECT_CLASS (class);
1450 EvJobClass *job_class = EV_JOB_CLASS (class);
1452 oclass->dispose = ev_job_print_dispose;
1453 job_class->run = ev_job_print_run;
1457 ev_job_print_new (EvDocument *document)
1461 ev_debug_message (DEBUG_JOBS, NULL);
1463 job = g_object_new (EV_TYPE_JOB_PRINT, NULL);
1464 job->document = g_object_ref (document);
1470 ev_job_print_set_page (EvJobPrint *job,
1477 ev_job_print_set_cairo (EvJobPrint *job,
1484 cairo_destroy (job->cr);
1485 job->cr = cr ? cairo_reference (cr) : NULL;