1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
4 * Copyright (C) 2005 Red Hat, Inc
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24 #include "ev-document-thumbnails.h"
25 #include "ev-document-links.h"
26 #include "ev-document-images.h"
27 #include "ev-document-forms.h"
28 #include "ev-file-exporter.h"
29 #include "ev-document-factory.h"
30 #include "ev-document-misc.h"
31 #include "ev-file-helpers.h"
32 #include "ev-document-fonts.h"
33 #include "ev-document-security.h"
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);
452 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job->ev_page->index, job);
453 g_object_unref (job->ev_page);
458 cairo_surface_destroy (job->surface);
462 if (job->selection) {
463 cairo_surface_destroy (job->selection);
464 job->selection = NULL;
467 if (job->selection_region) {
468 gdk_region_destroy (job->selection_region);
469 job->selection_region = NULL;
472 (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
476 ev_job_render_run (EvJob *job)
478 EvJobRender *job_render = EV_JOB_RENDER (job);
481 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_render->page, job);
482 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
484 ev_document_doc_mutex_lock ();
486 ev_profiler_start (EV_PROFILE_JOBS, "Rendering page %d", job_render->page);
488 ev_document_fc_mutex_lock ();
490 job_render->ev_page = ev_document_get_page (job->document, job_render->page);
491 rc = ev_render_context_new (job_render->ev_page, job_render->rotation, job_render->scale);
493 job_render->surface = ev_document_render (job->document, rc);
494 /* If job was cancelled during the page rendering,
495 * we return now, so that the thread is finished ASAP
497 if (g_cancellable_is_cancelled (job->cancellable)) {
498 ev_document_fc_mutex_unlock ();
499 ev_document_doc_mutex_unlock ();
505 if (job_render->include_selection && EV_IS_SELECTION (job->document)) {
506 ev_selection_render_selection (EV_SELECTION (job->document),
508 &(job_render->selection),
509 &(job_render->selection_points),
511 job_render->selection_style,
512 &(job_render->text), &(job_render->base));
513 job_render->selection_region =
514 ev_selection_get_selection_region (EV_SELECTION (job->document),
516 job_render->selection_style,
517 &(job_render->selection_points));
522 ev_document_fc_mutex_unlock ();
523 ev_document_doc_mutex_unlock ();
525 ev_job_succeeded (job);
531 ev_job_render_class_init (EvJobRenderClass *class)
533 GObjectClass *oclass = G_OBJECT_CLASS (class);
534 EvJobClass *job_class = EV_JOB_CLASS (class);
536 oclass->dispose = ev_job_render_dispose;
537 job_class->run = ev_job_render_run;
541 ev_job_render_new (EvDocument *document,
550 ev_debug_message (DEBUG_JOBS, "page: %d", page);
552 job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
554 EV_JOB (job)->document = g_object_ref (document);
556 job->rotation = rotation;
558 job->target_width = width;
559 job->target_height = height;
565 ev_job_render_set_selection_info (EvJobRender *job,
566 EvRectangle *selection_points,
567 EvSelectionStyle selection_style,
571 job->include_selection = TRUE;
573 job->selection_points = *selection_points;
574 job->selection_style = selection_style;
581 ev_job_page_data_init (EvJobPageData *job)
583 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
587 ev_job_page_data_run (EvJob *job)
589 EvJobPageData *job_pd = EV_JOB_PAGE_DATA (job);
592 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_pd->page, job);
593 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
595 ev_document_doc_mutex_lock ();
596 ev_page = ev_document_get_page (job->document, job_pd->page);
598 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_TEXT) && EV_IS_SELECTION (job->document))
599 job_pd->text_mapping =
600 ev_selection_get_selection_map (EV_SELECTION (job->document), ev_page);
601 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_LINKS) && EV_IS_DOCUMENT_LINKS (job->document))
602 job_pd->link_mapping =
603 ev_document_links_get_links (EV_DOCUMENT_LINKS (job->document), job_pd->page);
604 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_FORMS) && EV_IS_DOCUMENT_FORMS (job->document))
605 job_pd->form_field_mapping =
606 ev_document_forms_get_form_fields (EV_DOCUMENT_FORMS (job->document),
608 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_IMAGES) && EV_IS_DOCUMENT_IMAGES (job->document))
609 job_pd->image_mapping =
610 ev_document_images_get_image_mapping (EV_DOCUMENT_IMAGES (job->document),
612 if ((job_pd->flags & EV_PAGE_DATA_INCLUDE_ANNOTS) && EV_IS_DOCUMENT_ANNOTATIONS (job->document))
613 job_pd->annot_mapping =
614 ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document),
616 g_object_unref (ev_page);
617 ev_document_doc_mutex_unlock ();
619 ev_job_succeeded (job);
625 ev_job_page_data_class_init (EvJobPageDataClass *class)
627 EvJobClass *job_class = EV_JOB_CLASS (class);
629 job_class->run = ev_job_page_data_run;
633 ev_job_page_data_new (EvDocument *document,
635 EvJobPageDataFlags flags)
639 ev_debug_message (DEBUG_JOBS, "%d", page);
641 job = g_object_new (EV_TYPE_JOB_PAGE_DATA, NULL);
643 EV_JOB (job)->document = g_object_ref (document);
652 ev_job_thumbnail_init (EvJobThumbnail *job)
654 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
658 ev_job_thumbnail_dispose (GObject *object)
662 job = EV_JOB_THUMBNAIL (object);
664 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->page, job);
666 if (job->thumbnail) {
667 g_object_unref (job->thumbnail);
668 job->thumbnail = NULL;
671 (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
675 ev_job_thumbnail_run (EvJob *job)
677 EvJobThumbnail *job_thumb = EV_JOB_THUMBNAIL (job);
681 ev_debug_message (DEBUG_JOBS, "%d (%p)", job_thumb->page, job);
682 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
684 ev_document_doc_mutex_lock ();
686 page = ev_document_get_page (job->document, job_thumb->page);
687 rc = ev_render_context_new (page, job_thumb->rotation, job_thumb->scale);
688 g_object_unref (page);
690 job_thumb->thumbnail = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (job->document),
693 ev_document_doc_mutex_unlock ();
695 ev_job_succeeded (job);
701 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
703 GObjectClass *oclass = G_OBJECT_CLASS (class);
704 EvJobClass *job_class = EV_JOB_CLASS (class);
706 oclass->dispose = ev_job_thumbnail_dispose;
707 job_class->run = ev_job_thumbnail_run;
711 ev_job_thumbnail_new (EvDocument *document,
718 ev_debug_message (DEBUG_JOBS, "%d", page);
720 job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
722 EV_JOB (job)->document = g_object_ref (document);
724 job->rotation = rotation;
732 ev_job_fonts_init (EvJobFonts *job)
734 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
738 ev_job_fonts_run (EvJob *job)
740 EvJobFonts *job_fonts = EV_JOB_FONTS (job);
741 EvDocumentFonts *fonts = EV_DOCUMENT_FONTS (job->document);
743 ev_debug_message (DEBUG_JOBS, NULL);
745 /* Do not block the main loop */
746 if (!ev_document_doc_mutex_trylock ())
749 if (!ev_document_fc_mutex_trylock ())
752 #ifdef EV_ENABLE_DEBUG
753 /* We use the #ifdef in this case because of the if */
754 if (ev_document_fonts_get_progress (fonts) == 0)
755 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
758 job_fonts->scan_completed = !ev_document_fonts_scan (fonts, 20);
759 g_signal_emit (job_fonts, job_fonts_signals[FONTS_UPDATED], 0,
760 ev_document_fonts_get_progress (fonts));
762 ev_document_fc_mutex_unlock ();
763 ev_document_doc_mutex_unlock ();
765 if (job_fonts->scan_completed)
766 ev_job_succeeded (job);
768 return !job_fonts->scan_completed;
772 ev_job_fonts_class_init (EvJobFontsClass *class)
774 EvJobClass *job_class = EV_JOB_CLASS (class);
776 job_class->run = ev_job_fonts_run;
778 job_fonts_signals[FONTS_UPDATED] =
779 g_signal_new ("updated",
782 G_STRUCT_OFFSET (EvJobFontsClass, updated),
784 g_cclosure_marshal_VOID__DOUBLE,
790 ev_job_fonts_new (EvDocument *document)
794 ev_debug_message (DEBUG_JOBS, NULL);
796 job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
798 EV_JOB (job)->document = g_object_ref (document);
805 ev_job_load_init (EvJobLoad *job)
807 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
811 ev_job_load_dispose (GObject *object)
813 EvJobLoad *job = EV_JOB_LOAD (object);
815 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
823 g_free (job->password);
824 job->password = NULL;
827 (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object);
831 ev_job_load_run (EvJob *job)
833 EvJobLoad *job_load = EV_JOB_LOAD (job);
834 GError *error = NULL;
836 ev_debug_message (DEBUG_JOBS, "%s", job_load->uri);
837 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
839 ev_document_fc_mutex_lock ();
841 /* This job may already have a document even if the job didn't complete
842 because, e.g., a password is required - if so, just reload rather than
843 creating a new instance */
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 ev_document_load (job->document,
858 job->document = ev_document_factory_get_document (job_load->uri,
862 ev_document_fc_mutex_unlock ();
865 ev_job_failed_from_error (job, error);
866 g_error_free (error);
868 ev_job_succeeded (job);
875 ev_job_load_class_init (EvJobLoadClass *class)
877 GObjectClass *oclass = G_OBJECT_CLASS (class);
878 EvJobClass *job_class = EV_JOB_CLASS (class);
880 oclass->dispose = ev_job_load_dispose;
881 job_class->run = ev_job_load_run;
885 ev_job_load_new (const gchar *uri)
889 ev_debug_message (DEBUG_JOBS, "%s", uri);
891 job = g_object_new (EV_TYPE_JOB_LOAD, NULL);
892 job->uri = g_strdup (uri);
898 ev_job_load_set_uri (EvJobLoad *job, const gchar *uri)
900 ev_debug_message (DEBUG_JOBS, "%s", uri);
904 job->uri = g_strdup (uri);
908 ev_job_load_set_password (EvJobLoad *job, const gchar *password)
910 ev_debug_message (DEBUG_JOBS, NULL);
913 g_free (job->password);
914 job->password = password ? g_strdup (password) : NULL;
919 ev_job_save_init (EvJobSave *job)
921 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
925 ev_job_save_dispose (GObject *object)
927 EvJobSave *job = EV_JOB_SAVE (object);
929 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
936 if (job->document_uri) {
937 g_free (job->document_uri);
938 job->document_uri = NULL;
941 (* G_OBJECT_CLASS (ev_job_save_parent_class)->dispose) (object);
945 ev_job_save_run (EvJob *job)
947 EvJobSave *job_save = EV_JOB_SAVE (job);
949 gchar *tmp_filename = NULL;
951 GError *error = NULL;
953 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", job_save->uri, job_save->document_uri);
954 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
956 fd = ev_mkstemp ("saveacopy.XXXXXX", &tmp_filename, &error);
958 ev_job_failed_from_error (job, error);
959 g_error_free (error);
964 ev_document_doc_mutex_lock ();
966 /* Save document to temp filename */
967 local_uri = g_filename_to_uri (tmp_filename, NULL, &error);
968 if (local_uri != NULL) {
969 ev_document_save (job->document, local_uri, &error);
974 ev_document_doc_mutex_unlock ();
978 ev_job_failed_from_error (job, error);
979 g_error_free (error);
984 /* If original document was compressed,
985 * compress it again before saving
987 if (g_object_get_data (G_OBJECT (job->document), "uri-uncompressed")) {
988 EvCompressionType ctype = EV_COMPRESSION_NONE;
992 ext = g_strrstr (job_save->document_uri, ".gz");
993 if (ext && g_ascii_strcasecmp (ext, ".gz") == 0)
994 ctype = EV_COMPRESSION_GZIP;
996 ext = g_strrstr (job_save->document_uri, ".bz2");
997 if (ext && g_ascii_strcasecmp (ext, ".bz2") == 0)
998 ctype = EV_COMPRESSION_BZIP2;
1000 uri_comp = ev_file_compress (local_uri, ctype, &error);
1002 g_unlink (tmp_filename);
1004 if (!uri_comp || error) {
1007 local_uri = uri_comp;
1011 g_free (tmp_filename);
1015 ev_job_failed_from_error (job, error);
1016 g_error_free (error);
1024 ev_xfer_uri_simple (local_uri, job_save->uri, &error);
1025 ev_tmp_uri_unlink (local_uri);
1028 ev_job_failed_from_error (job, error);
1029 g_error_free (error);
1031 ev_job_succeeded (job);
1038 ev_job_save_class_init (EvJobSaveClass *class)
1040 GObjectClass *oclass = G_OBJECT_CLASS (class);
1041 EvJobClass *job_class = EV_JOB_CLASS (class);
1043 oclass->dispose = ev_job_save_dispose;
1044 job_class->run = ev_job_save_run;
1048 ev_job_save_new (EvDocument *document,
1050 const gchar *document_uri)
1054 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", uri, document_uri);
1056 job = g_object_new (EV_TYPE_JOB_SAVE, NULL);
1058 EV_JOB (job)->document = g_object_ref (document);
1059 job->uri = g_strdup (uri);
1060 job->document_uri = g_strdup (document_uri);
1062 return EV_JOB (job);
1067 ev_job_find_init (EvJobFind *job)
1069 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
1073 ev_job_find_dispose (GObject *object)
1075 EvJobFind *job = EV_JOB_FIND (object);
1077 ev_debug_message (DEBUG_JOBS, NULL);
1087 for (i = 0; i < job->n_pages; i++) {
1088 g_list_foreach (job->pages[i], (GFunc)g_free, NULL);
1089 g_list_free (job->pages[i]);
1092 g_free (job->pages);
1096 (* G_OBJECT_CLASS (ev_job_find_parent_class)->dispose) (object);
1100 ev_job_find_run (EvJob *job)
1102 EvJobFind *job_find = EV_JOB_FIND (job);
1103 EvDocumentFind *find = EV_DOCUMENT_FIND (job->document);
1107 ev_debug_message (DEBUG_JOBS, NULL);
1109 /* Do not block the main loop */
1110 if (!ev_document_doc_mutex_trylock ())
1113 #ifdef EV_ENABLE_DEBUG
1114 /* We use the #ifdef in this case because of the if */
1115 if (job_find->current_page == job_find->start_page)
1116 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1119 ev_page = ev_document_get_page (job->document, job_find->current_page);
1120 matches = ev_document_find_find_text (find, ev_page, job_find->text,
1121 job_find->case_sensitive);
1122 g_object_unref (ev_page);
1124 ev_document_doc_mutex_unlock ();
1126 if (!job_find->has_results)
1127 job_find->has_results = (matches != NULL);
1129 job_find->pages[job_find->current_page] = matches;
1130 g_signal_emit (job_find, job_find_signals[FIND_UPDATED], 0, job_find->current_page);
1132 job_find->current_page = (job_find->current_page + 1) % job_find->n_pages;
1133 if (job_find->current_page == job_find->start_page) {
1134 ev_job_succeeded (job);
1143 ev_job_find_class_init (EvJobFindClass *class)
1145 EvJobClass *job_class = EV_JOB_CLASS (class);
1146 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1148 job_class->run = ev_job_find_run;
1149 gobject_class->dispose = ev_job_find_dispose;
1151 job_find_signals[FIND_UPDATED] =
1152 g_signal_new ("updated",
1155 G_STRUCT_OFFSET (EvJobFindClass, updated),
1157 g_cclosure_marshal_VOID__INT,
1163 ev_job_find_new (EvDocument *document,
1167 gboolean case_sensitive)
1171 ev_debug_message (DEBUG_JOBS, NULL);
1173 job = g_object_new (EV_TYPE_JOB_FIND, NULL);
1175 EV_JOB (job)->document = g_object_ref (document);
1176 job->start_page = start_page;
1177 job->current_page = start_page;
1178 job->n_pages = n_pages;
1179 job->pages = g_new0 (GList *, n_pages);
1180 job->text = g_strdup (text);
1181 job->case_sensitive = case_sensitive;
1182 job->has_results = FALSE;
1184 return EV_JOB (job);
1188 ev_job_find_get_n_results (EvJobFind *job,
1191 return g_list_length (job->pages[page]);
1195 ev_job_find_get_progress (EvJobFind *job)
1199 if (ev_job_is_finished (EV_JOB (job)))
1202 if (job->current_page > job->start_page) {
1203 pages_done = job->current_page - job->start_page + 1;
1204 } else if (job->current_page == job->start_page) {
1205 pages_done = job->n_pages;
1207 pages_done = job->n_pages - job->start_page + job->current_page;
1210 return pages_done / (gdouble) job->n_pages;
1214 ev_job_find_has_results (EvJobFind *job)
1216 return job->has_results;
1220 ev_job_find_get_results (EvJobFind *job)
1227 ev_job_layers_init (EvJobLayers *job)
1229 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1233 ev_job_layers_dispose (GObject *object)
1237 ev_debug_message (DEBUG_JOBS, NULL);
1239 job = EV_JOB_LAYERS (object);
1242 g_object_unref (job->model);
1246 (* G_OBJECT_CLASS (ev_job_layers_parent_class)->dispose) (object);
1250 ev_job_layers_run (EvJob *job)
1252 EvJobLayers *job_layers = EV_JOB_LAYERS (job);
1254 ev_debug_message (DEBUG_JOBS, NULL);
1255 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1257 ev_document_doc_mutex_lock ();
1258 job_layers->model = ev_document_layers_get_layers (EV_DOCUMENT_LAYERS (job->document));
1259 ev_document_doc_mutex_unlock ();
1261 ev_job_succeeded (job);
1267 ev_job_layers_class_init (EvJobLayersClass *class)
1269 GObjectClass *oclass = G_OBJECT_CLASS (class);
1270 EvJobClass *job_class = EV_JOB_CLASS (class);
1272 oclass->dispose = ev_job_layers_dispose;
1273 job_class->run = ev_job_layers_run;
1277 ev_job_layers_new (EvDocument *document)
1281 ev_debug_message (DEBUG_JOBS, NULL);
1283 job = g_object_new (EV_TYPE_JOB_LAYERS, NULL);
1284 job->document = g_object_ref (document);
1291 ev_job_export_init (EvJobExport *job)
1293 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1298 ev_job_export_dispose (GObject *object)
1302 ev_debug_message (DEBUG_JOBS, NULL);
1304 job = EV_JOB_EXPORT (object);
1307 g_object_unref (job->rc);
1311 (* G_OBJECT_CLASS (ev_job_export_parent_class)->dispose) (object);
1315 ev_job_export_run (EvJob *job)
1317 EvJobExport *job_export = EV_JOB_EXPORT (job);
1320 g_assert (job_export->page != -1);
1322 ev_debug_message (DEBUG_JOBS, NULL);
1323 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1325 ev_document_doc_mutex_lock ();
1327 ev_page = ev_document_get_page (job->document, job_export->page);
1328 if (job_export->rc) {
1329 job->failed = FALSE;
1330 job->finished = FALSE;
1331 g_clear_error (&job->error);
1333 ev_render_context_set_page (job_export->rc, ev_page);
1335 job_export->rc = ev_render_context_new (ev_page, 0, 1.0);
1337 g_object_unref (ev_page);
1339 ev_file_exporter_do_page (EV_FILE_EXPORTER (job->document), job_export->rc);
1341 ev_document_doc_mutex_unlock ();
1343 ev_job_succeeded (job);
1349 ev_job_export_class_init (EvJobExportClass *class)
1351 GObjectClass *oclass = G_OBJECT_CLASS (class);
1352 EvJobClass *job_class = EV_JOB_CLASS (class);
1354 oclass->dispose = ev_job_export_dispose;
1355 job_class->run = ev_job_export_run;
1359 ev_job_export_new (EvDocument *document)
1363 ev_debug_message (DEBUG_JOBS, NULL);
1365 job = g_object_new (EV_TYPE_JOB_EXPORT, NULL);
1366 job->document = g_object_ref (document);
1372 ev_job_export_set_page (EvJobExport *job,
1380 ev_job_print_init (EvJobPrint *job)
1382 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1387 ev_job_print_dispose (GObject *object)
1391 ev_debug_message (DEBUG_JOBS, NULL);
1393 job = EV_JOB_PRINT (object);
1396 cairo_destroy (job->cr);
1400 (* G_OBJECT_CLASS (ev_job_print_parent_class)->dispose) (object);
1404 ev_job_print_run (EvJob *job)
1406 EvJobPrint *job_print = EV_JOB_PRINT (job);
1408 cairo_status_t cr_status;
1410 g_assert (job_print->page != -1);
1411 g_assert (job_print->cr != NULL);
1413 ev_debug_message (DEBUG_JOBS, NULL);
1414 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1416 job->failed = FALSE;
1417 job->finished = FALSE;
1418 g_clear_error (&job->error);
1420 ev_document_doc_mutex_lock ();
1422 ev_page = ev_document_get_page (job->document, job_print->page);
1423 ev_document_print_print_page (EV_DOCUMENT_PRINT (job->document),
1424 ev_page, job_print->cr);
1425 g_object_unref (ev_page);
1427 ev_document_doc_mutex_unlock ();
1429 cr_status = cairo_status (job_print->cr);
1430 if (cr_status == CAIRO_STATUS_SUCCESS) {
1431 ev_job_succeeded (job);
1435 GTK_PRINT_ERROR_GENERAL,
1436 _("Failed to print page %d: %s"),
1438 cairo_status_to_string (cr_status));
1445 ev_job_print_class_init (EvJobPrintClass *class)
1447 GObjectClass *oclass = G_OBJECT_CLASS (class);
1448 EvJobClass *job_class = EV_JOB_CLASS (class);
1450 oclass->dispose = ev_job_print_dispose;
1451 job_class->run = ev_job_print_run;
1455 ev_job_print_new (EvDocument *document)
1459 ev_debug_message (DEBUG_JOBS, NULL);
1461 job = g_object_new (EV_TYPE_JOB_PRINT, NULL);
1462 job->document = g_object_ref (document);
1468 ev_job_print_set_page (EvJobPrint *job,
1475 ev_job_print_set_cairo (EvJobPrint *job,
1482 cairo_destroy (job->cr);
1483 job->cr = cr ? cairo_reference (cr) : NULL;