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"
41 #include <glib/gstdio.h>
42 #include <glib/gi18n-lib.h>
45 static void ev_job_init (EvJob *job);
46 static void ev_job_class_init (EvJobClass *class);
47 static void ev_job_links_init (EvJobLinks *job);
48 static void ev_job_links_class_init (EvJobLinksClass *class);
49 static void ev_job_attachments_init (EvJobAttachments *job);
50 static void ev_job_attachments_class_init (EvJobAttachmentsClass *class);
51 static void ev_job_render_init (EvJobRender *job);
52 static void ev_job_render_class_init (EvJobRenderClass *class);
53 static void ev_job_thumbnail_init (EvJobThumbnail *job);
54 static void ev_job_thumbnail_class_init (EvJobThumbnailClass *class);
55 static void ev_job_load_init (EvJobLoad *job);
56 static void ev_job_load_class_init (EvJobLoadClass *class);
57 static void ev_job_save_init (EvJobSave *job);
58 static void ev_job_save_class_init (EvJobSaveClass *class);
59 static void ev_job_find_init (EvJobFind *job);
60 static void ev_job_find_class_init (EvJobFindClass *class);
61 static void ev_job_layers_init (EvJobLayers *job);
62 static void ev_job_layers_class_init (EvJobLayersClass *class);
63 static void ev_job_export_init (EvJobExport *job);
64 static void ev_job_export_class_init (EvJobExportClass *class);
65 static void ev_job_print_init (EvJobPrint *job);
66 static void ev_job_print_class_init (EvJobPrintClass *class);
89 static guint job_signals[LAST_SIGNAL] = { 0 };
90 static guint job_render_signals[RENDER_LAST_SIGNAL] = { 0 };
91 static guint job_fonts_signals[FONTS_LAST_SIGNAL] = { 0 };
92 static guint job_find_signals[FIND_LAST_SIGNAL] = { 0 };
94 G_DEFINE_ABSTRACT_TYPE (EvJob, ev_job, G_TYPE_OBJECT)
95 G_DEFINE_TYPE (EvJobLinks, ev_job_links, EV_TYPE_JOB)
96 G_DEFINE_TYPE (EvJobAttachments, ev_job_attachments, EV_TYPE_JOB)
97 G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB)
98 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
99 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
100 G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB)
101 G_DEFINE_TYPE (EvJobSave, ev_job_save, EV_TYPE_JOB)
102 G_DEFINE_TYPE (EvJobFind, ev_job_find, EV_TYPE_JOB)
103 G_DEFINE_TYPE (EvJobLayers, ev_job_layers, EV_TYPE_JOB)
104 G_DEFINE_TYPE (EvJobExport, ev_job_export, EV_TYPE_JOB)
105 G_DEFINE_TYPE (EvJobPrint, ev_job_print, EV_TYPE_JOB)
109 ev_job_init (EvJob *job)
111 job->cancellable = g_cancellable_new ();
115 ev_job_dispose (GObject *object)
119 job = EV_JOB (object);
122 g_object_unref (job->document);
123 job->document = NULL;
126 if (job->cancellable) {
127 g_object_unref (job->cancellable);
128 job->cancellable = NULL;
132 g_error_free (job->error);
136 (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object);
140 ev_job_class_init (EvJobClass *class)
142 GObjectClass *oclass;
144 oclass = G_OBJECT_CLASS (class);
146 oclass->dispose = ev_job_dispose;
148 job_signals[CANCELLED] =
149 g_signal_new ("cancelled",
152 G_STRUCT_OFFSET (EvJobClass, cancelled),
154 g_cclosure_marshal_VOID__VOID,
156 job_signals [FINISHED] =
157 g_signal_new ("finished",
160 G_STRUCT_OFFSET (EvJobClass, finished),
162 g_cclosure_marshal_VOID__VOID,
167 emit_finished (EvJob *job)
169 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
171 job->idle_finished_id = 0;
173 if (job->cancelled) {
174 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit finished", EV_GET_TYPE_NAME (job), job);
176 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
177 g_signal_emit (job, job_signals[FINISHED], 0);
184 ev_job_emit_finished (EvJob *job)
186 ev_debug_message (DEBUG_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
188 if (g_cancellable_is_cancelled (job->cancellable)) {
189 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, returning", EV_GET_TYPE_NAME (job), job);
193 job->finished = TRUE;
195 if (job->run_mode == EV_JOB_RUN_THREAD) {
196 job->idle_finished_id =
197 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
198 (GSourceFunc)emit_finished,
200 (GDestroyNotify)g_object_unref);
202 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
203 g_signal_emit (job, job_signals[FINISHED], 0);
208 ev_job_run (EvJob *job)
210 EvJobClass *class = EV_JOB_GET_CLASS (job);
212 return class->run (job);
216 ev_job_cancel (EvJob *job)
218 if (job->cancelled || (job->finished && job->idle_finished_id == 0))
221 ev_debug_message (DEBUG_JOBS, "job %s (%p) cancelled", EV_GET_TYPE_NAME (job), job);
222 ev_profiler_stop (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
224 /* This should never be called from a thread */
225 job->cancelled = TRUE;
226 g_cancellable_cancel (job->cancellable);
227 g_signal_emit (job, job_signals[CANCELLED], 0);
231 ev_job_failed (EvJob *job,
240 if (job->failed || job->finished)
243 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
247 va_start (args, format);
248 message = g_strdup_vprintf (format, args);
251 job->error = g_error_new_literal (domain, code, message);
254 ev_job_emit_finished (job);
258 ev_job_failed_from_error (EvJob *job,
261 if (job->failed || job->finished)
264 ev_debug_message (DEBUG_JOBS, "job %s (%p) failed", EV_GET_TYPE_NAME (job), job);
267 job->error = g_error_copy (error);
269 ev_job_emit_finished (job);
273 ev_job_succeeded (EvJob *job)
278 ev_debug_message (DEBUG_JOBS, "job %s (%p) succeeded", EV_GET_TYPE_NAME (job), job);
281 ev_job_emit_finished (job);
285 ev_job_is_finished (EvJob *job)
287 return job->finished;
291 ev_job_is_failed (EvJob *job)
297 ev_job_get_run_mode (EvJob *job)
299 return job->run_mode;
303 ev_job_set_run_mode (EvJob *job,
304 EvJobRunMode run_mode)
306 job->run_mode = run_mode;
311 ev_job_links_init (EvJobLinks *job)
313 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
317 ev_job_links_dispose (GObject *object)
321 ev_debug_message (DEBUG_JOBS, NULL);
323 job = EV_JOB_LINKS (object);
326 g_object_unref (job->model);
330 (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object);
334 ev_job_links_run (EvJob *job)
336 EvJobLinks *job_links = EV_JOB_LINKS (job);
338 ev_debug_message (DEBUG_JOBS, NULL);
339 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
341 ev_document_doc_mutex_lock ();
342 job_links->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (job->document));
343 ev_document_doc_mutex_unlock ();
345 ev_job_succeeded (job);
351 ev_job_links_class_init (EvJobLinksClass *class)
353 GObjectClass *oclass = G_OBJECT_CLASS (class);
354 EvJobClass *job_class = EV_JOB_CLASS (class);
356 oclass->dispose = ev_job_links_dispose;
357 job_class->run = ev_job_links_run;
361 ev_job_links_new (EvDocument *document)
365 ev_debug_message (DEBUG_JOBS, NULL);
367 job = g_object_new (EV_TYPE_JOB_LINKS, NULL);
368 job->document = g_object_ref (document);
373 /* EvJobAttachments */
375 ev_job_attachments_init (EvJobAttachments *job)
377 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
381 ev_job_attachments_dispose (GObject *object)
383 EvJobAttachments *job;
385 ev_debug_message (DEBUG_JOBS, NULL);
387 job = EV_JOB_ATTACHMENTS (object);
389 if (job->attachments) {
390 g_list_foreach (job->attachments, (GFunc)g_object_unref, NULL);
391 g_list_free (job->attachments);
392 job->attachments = NULL;
395 (* G_OBJECT_CLASS (ev_job_attachments_parent_class)->dispose) (object);
399 ev_job_attachments_run (EvJob *job)
401 EvJobAttachments *job_attachments = EV_JOB_ATTACHMENTS (job);
403 ev_debug_message (DEBUG_JOBS, NULL);
404 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
406 ev_document_doc_mutex_lock ();
407 job_attachments->attachments = ev_document_get_attachments (job->document);
408 ev_document_doc_mutex_unlock ();
410 ev_job_succeeded (job);
416 ev_job_attachments_class_init (EvJobAttachmentsClass *class)
418 GObjectClass *oclass = G_OBJECT_CLASS (class);
419 EvJobClass *job_class = EV_JOB_CLASS (class);
421 oclass->dispose = ev_job_attachments_dispose;
422 job_class->run = ev_job_attachments_run;
426 ev_job_attachments_new (EvDocument *document)
430 ev_debug_message (DEBUG_JOBS, NULL);
432 job = g_object_new (EV_TYPE_JOB_ATTACHMENTS, NULL);
433 job->document = g_object_ref (document);
440 ev_job_render_init (EvJobRender *job)
442 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
446 ev_job_render_dispose (GObject *object)
450 job = EV_JOB_RENDER (object);
453 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job->ev_page->index, job);
454 g_object_unref (job->ev_page);
459 cairo_surface_destroy (job->surface);
463 if (job->selection) {
464 cairo_surface_destroy (job->selection);
465 job->selection = NULL;
468 if (job->selection_region) {
469 gdk_region_destroy (job->selection_region);
470 job->selection_region = NULL;
473 (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
477 notify_page_ready (EvJobRender *job)
479 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->ev_page->index, job);
480 ev_profiler_stop (EV_PROFILE_JOBS, "Rendering page %d", job->ev_page->index);
482 if (EV_JOB (job)->cancelled) {
483 ev_debug_message (DEBUG_JOBS, "%s (%p) job was cancelled, do not emit page_ready", EV_GET_TYPE_NAME (job), job);
485 g_signal_emit (job, job_render_signals[PAGE_READY], 0);
492 ev_job_render_page_ready (EvJobRender *job)
494 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->ev_page->index, job);
496 job->page_ready = TRUE;
497 g_idle_add_full (G_PRIORITY_HIGH_IDLE,
498 (GSourceFunc)notify_page_ready,
500 (GDestroyNotify)g_object_unref);
504 ev_job_render_run (EvJob *job)
506 EvJobRender *job_render = EV_JOB_RENDER (job);
509 ev_debug_message (DEBUG_JOBS, "page: %d (%p)", job_render->page, job);
510 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
512 ev_document_doc_mutex_lock ();
514 ev_profiler_start (EV_PROFILE_JOBS, "Rendering page %d", job_render->page);
516 ev_document_fc_mutex_lock ();
518 job_render->ev_page = ev_document_get_page (job->document, job_render->page);
519 rc = ev_render_context_new (job_render->ev_page, job_render->rotation, job_render->scale);
521 job_render->surface = ev_document_render (job->document, rc);
522 /* If job was cancelled during the page rendering,
523 * we return now, so that the thread is finished ASAP
525 if (g_cancellable_is_cancelled (job->cancellable)) {
526 ev_document_fc_mutex_unlock ();
527 ev_document_doc_mutex_unlock ();
533 if ((job_render->flags & EV_RENDER_INCLUDE_SELECTION) && EV_IS_SELECTION (job->document)) {
534 ev_selection_render_selection (EV_SELECTION (job->document),
536 &(job_render->selection),
537 &(job_render->selection_points),
539 job_render->selection_style,
540 &(job_render->text), &(job_render->base));
541 job_render->selection_region =
542 ev_selection_get_selection_region (EV_SELECTION (job->document),
544 job_render->selection_style,
545 &(job_render->selection_points));
548 ev_job_render_page_ready (job_render);
550 ev_document_fc_mutex_unlock ();
552 if ((job_render->flags & EV_RENDER_INCLUDE_TEXT) && EV_IS_SELECTION (job->document))
553 job_render->text_mapping =
554 ev_selection_get_selection_map (EV_SELECTION (job->document), rc);
555 if ((job_render->flags & EV_RENDER_INCLUDE_LINKS) && EV_IS_DOCUMENT_LINKS (job->document))
556 job_render->link_mapping =
557 ev_document_links_get_links (EV_DOCUMENT_LINKS (job->document), job_render->page);
558 if ((job_render->flags & EV_RENDER_INCLUDE_FORMS) && EV_IS_DOCUMENT_FORMS (job->document))
559 job_render->form_field_mapping =
560 ev_document_forms_get_form_fields (EV_DOCUMENT_FORMS (job->document),
561 job_render->ev_page);
562 if ((job_render->flags & EV_RENDER_INCLUDE_IMAGES) && EV_IS_DOCUMENT_IMAGES (job->document))
563 job_render->image_mapping =
564 ev_document_images_get_image_mapping (EV_DOCUMENT_IMAGES (job->document),
566 if ((job_render->flags & EV_RENDER_INCLUDE_ANNOTS) && EV_IS_DOCUMENT_ANNOTATIONS (job->document))
567 job_render->annots_mapping =
568 ev_document_annotations_get_annotations (EV_DOCUMENT_ANNOTATIONS (job->document),
569 job_render->ev_page);
571 ev_document_doc_mutex_unlock ();
573 ev_job_succeeded (job);
579 ev_job_render_class_init (EvJobRenderClass *class)
581 GObjectClass *oclass = G_OBJECT_CLASS (class);
582 EvJobClass *job_class = EV_JOB_CLASS (class);
584 job_render_signals [PAGE_READY] =
585 g_signal_new ("page-ready",
588 G_STRUCT_OFFSET (EvJobRenderClass, page_ready),
590 g_cclosure_marshal_VOID__VOID,
593 oclass->dispose = ev_job_render_dispose;
594 job_class->run = ev_job_render_run;
598 ev_job_render_new (EvDocument *document,
608 ev_debug_message (DEBUG_JOBS, "page: %d", page);
610 job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
612 EV_JOB (job)->document = g_object_ref (document);
614 job->rotation = rotation;
616 job->target_width = width;
617 job->target_height = height;
624 ev_job_render_set_selection_info (EvJobRender *job,
625 EvRectangle *selection_points,
626 EvSelectionStyle selection_style,
630 job->flags |= EV_RENDER_INCLUDE_SELECTION;
632 job->selection_points = *selection_points;
633 job->selection_style = selection_style;
640 ev_job_thumbnail_init (EvJobThumbnail *job)
642 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
646 ev_job_thumbnail_dispose (GObject *object)
650 job = EV_JOB_THUMBNAIL (object);
652 ev_debug_message (DEBUG_JOBS, "%d (%p)", job->page, job);
654 if (job->thumbnail) {
655 g_object_unref (job->thumbnail);
656 job->thumbnail = NULL;
659 (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
663 ev_job_thumbnail_run (EvJob *job)
665 EvJobThumbnail *job_thumb = EV_JOB_THUMBNAIL (job);
669 ev_debug_message (DEBUG_JOBS, "%d (%p)", job_thumb->page, job);
670 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
672 ev_document_doc_mutex_lock ();
674 page = ev_document_get_page (job->document, job_thumb->page);
675 rc = ev_render_context_new (page, job_thumb->rotation, job_thumb->scale);
676 g_object_unref (page);
678 job_thumb->thumbnail = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (job->document),
681 ev_document_doc_mutex_unlock ();
683 ev_job_succeeded (job);
689 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
691 GObjectClass *oclass = G_OBJECT_CLASS (class);
692 EvJobClass *job_class = EV_JOB_CLASS (class);
694 oclass->dispose = ev_job_thumbnail_dispose;
695 job_class->run = ev_job_thumbnail_run;
699 ev_job_thumbnail_new (EvDocument *document,
706 ev_debug_message (DEBUG_JOBS, "%d", page);
708 job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
710 EV_JOB (job)->document = g_object_ref (document);
712 job->rotation = rotation;
720 ev_job_fonts_init (EvJobFonts *job)
722 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
726 ev_job_fonts_run (EvJob *job)
728 EvJobFonts *job_fonts = EV_JOB_FONTS (job);
729 EvDocumentFonts *fonts = EV_DOCUMENT_FONTS (job->document);
731 ev_debug_message (DEBUG_JOBS, NULL);
733 /* Do not block the main loop */
734 if (!ev_document_doc_mutex_trylock ())
737 if (!ev_document_fc_mutex_trylock ())
740 #ifdef EV_ENABLE_DEBUG
741 /* We use the #ifdef in this case because of the if */
742 if (ev_document_fonts_get_progress (fonts) == 0)
743 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
746 job_fonts->scan_completed = !ev_document_fonts_scan (fonts, 20);
747 g_signal_emit (job_fonts, job_fonts_signals[FONTS_UPDATED], 0,
748 ev_document_fonts_get_progress (fonts));
750 ev_document_fc_mutex_unlock ();
751 ev_document_doc_mutex_unlock ();
753 if (job_fonts->scan_completed)
754 ev_job_succeeded (job);
756 return !job_fonts->scan_completed;
760 ev_job_fonts_class_init (EvJobFontsClass *class)
762 EvJobClass *job_class = EV_JOB_CLASS (class);
764 job_class->run = ev_job_fonts_run;
766 job_fonts_signals[FONTS_UPDATED] =
767 g_signal_new ("updated",
770 G_STRUCT_OFFSET (EvJobFontsClass, updated),
772 g_cclosure_marshal_VOID__DOUBLE,
778 ev_job_fonts_new (EvDocument *document)
782 ev_debug_message (DEBUG_JOBS, NULL);
784 job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
786 EV_JOB (job)->document = g_object_ref (document);
793 ev_job_load_init (EvJobLoad *job)
795 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
799 ev_job_load_dispose (GObject *object)
801 EvJobLoad *job = EV_JOB_LOAD (object);
803 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
811 g_free (job->password);
812 job->password = NULL;
815 (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object);
819 ev_job_load_run (EvJob *job)
821 EvJobLoad *job_load = EV_JOB_LOAD (job);
822 GError *error = NULL;
824 ev_debug_message (DEBUG_JOBS, "%s", job_load->uri);
825 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
827 ev_document_fc_mutex_lock ();
829 /* This job may already have a document even if the job didn't complete
830 because, e.g., a password is required - if so, just reload rather than
831 creating a new instance */
833 if (job_load->password) {
834 ev_document_security_set_password (EV_DOCUMENT_SECURITY (job->document),
839 job->finished = FALSE;
840 g_clear_error (&job->error);
842 ev_document_load (job->document,
846 job->document = ev_document_factory_get_document (job_load->uri,
850 ev_document_fc_mutex_unlock ();
853 ev_job_failed_from_error (job, error);
854 g_error_free (error);
856 ev_job_succeeded (job);
863 ev_job_load_class_init (EvJobLoadClass *class)
865 GObjectClass *oclass = G_OBJECT_CLASS (class);
866 EvJobClass *job_class = EV_JOB_CLASS (class);
868 oclass->dispose = ev_job_load_dispose;
869 job_class->run = ev_job_load_run;
873 ev_job_load_new (const gchar *uri)
877 ev_debug_message (DEBUG_JOBS, "%s", uri);
879 job = g_object_new (EV_TYPE_JOB_LOAD, NULL);
880 job->uri = g_strdup (uri);
886 ev_job_load_set_uri (EvJobLoad *job, const gchar *uri)
888 ev_debug_message (DEBUG_JOBS, "%s", uri);
892 job->uri = g_strdup (uri);
896 ev_job_load_set_password (EvJobLoad *job, const gchar *password)
898 ev_debug_message (DEBUG_JOBS, NULL);
901 g_free (job->password);
902 job->password = password ? g_strdup (password) : NULL;
907 ev_job_save_init (EvJobSave *job)
909 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
913 ev_job_save_dispose (GObject *object)
915 EvJobSave *job = EV_JOB_SAVE (object);
917 ev_debug_message (DEBUG_JOBS, "%s", job->uri);
924 if (job->document_uri) {
925 g_free (job->document_uri);
926 job->document_uri = NULL;
929 (* G_OBJECT_CLASS (ev_job_save_parent_class)->dispose) (object);
933 ev_job_save_run (EvJob *job)
935 EvJobSave *job_save = EV_JOB_SAVE (job);
940 GError *error = NULL;
942 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", job_save->uri, job_save->document_uri);
943 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
945 filename = ev_tmp_filename ("saveacopy");
946 tmp_filename = g_strdup_printf ("%s.XXXXXX", filename);
949 fd = g_mkstemp (tmp_filename);
952 gint save_errno = errno;
954 display_name = g_filename_display_name (tmp_filename);
957 g_file_error_from_errno (save_errno),
958 _("Failed to create file “%s”: %s"),
959 display_name, g_strerror (save_errno));
960 g_free (display_name);
961 g_free (tmp_filename);
966 ev_document_doc_mutex_lock ();
968 /* Save document to temp filename */
969 local_uri = g_filename_to_uri (tmp_filename, NULL, NULL);
970 ev_document_save (job->document, local_uri, &error);
973 ev_document_doc_mutex_unlock ();
977 ev_job_failed_from_error (job, error);
978 g_error_free (error);
983 /* If original document was compressed,
984 * compress it again before saving
986 if (g_object_get_data (G_OBJECT (job->document), "uri-uncompressed")) {
987 EvCompressionType ctype = EV_COMPRESSION_NONE;
991 ext = g_strrstr (job_save->document_uri, ".gz");
992 if (ext && g_ascii_strcasecmp (ext, ".gz") == 0)
993 ctype = EV_COMPRESSION_GZIP;
995 ext = g_strrstr (job_save->document_uri, ".bz2");
996 if (ext && g_ascii_strcasecmp (ext, ".bz2") == 0)
997 ctype = EV_COMPRESSION_BZIP2;
999 uri_comp = ev_file_compress (local_uri, ctype, &error);
1001 ev_tmp_filename_unlink (tmp_filename);
1003 if (!uri_comp || error) {
1006 local_uri = uri_comp;
1010 g_free (tmp_filename);
1014 ev_job_failed_from_error (job, error);
1015 g_error_free (error);
1023 ev_xfer_uri_simple (local_uri, job_save->uri, &error);
1024 ev_tmp_uri_unlink (local_uri);
1027 ev_job_failed_from_error (job, error);
1028 g_error_free (error);
1030 ev_job_succeeded (job);
1037 ev_job_save_class_init (EvJobSaveClass *class)
1039 GObjectClass *oclass = G_OBJECT_CLASS (class);
1040 EvJobClass *job_class = EV_JOB_CLASS (class);
1042 oclass->dispose = ev_job_save_dispose;
1043 job_class->run = ev_job_save_run;
1047 ev_job_save_new (EvDocument *document,
1049 const gchar *document_uri)
1053 ev_debug_message (DEBUG_JOBS, "uri: %s, document_uri: %s", uri, document_uri);
1055 job = g_object_new (EV_TYPE_JOB_SAVE, NULL);
1057 EV_JOB (job)->document = g_object_ref (document);
1058 job->uri = g_strdup (uri);
1059 job->document_uri = g_strdup (document_uri);
1061 return EV_JOB (job);
1066 ev_job_find_init (EvJobFind *job)
1068 EV_JOB (job)->run_mode = EV_JOB_RUN_MAIN_LOOP;
1072 ev_job_find_dispose (GObject *object)
1074 EvJobFind *job = EV_JOB_FIND (object);
1076 ev_debug_message (DEBUG_JOBS, NULL);
1086 for (i = 0; i < job->n_pages; i++) {
1087 g_list_foreach (job->pages[i], (GFunc)g_free, NULL);
1088 g_list_free (job->pages[i]);
1091 g_free (job->pages);
1095 (* G_OBJECT_CLASS (ev_job_find_parent_class)->dispose) (object);
1099 ev_job_find_run (EvJob *job)
1101 EvJobFind *job_find = EV_JOB_FIND (job);
1102 EvDocumentFind *find = EV_DOCUMENT_FIND (job->document);
1106 ev_debug_message (DEBUG_JOBS, NULL);
1108 /* Do not block the main loop */
1109 if (!ev_document_doc_mutex_trylock ())
1112 #ifdef EV_ENABLE_DEBUG
1113 /* We use the #ifdef in this case because of the if */
1114 if (job_find->current_page == job_find->start_page)
1115 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1118 ev_page = ev_document_get_page (job->document, job_find->current_page);
1119 matches = ev_document_find_find_text (find, ev_page, job_find->text,
1120 job_find->case_sensitive);
1121 g_object_unref (ev_page);
1123 ev_document_doc_mutex_unlock ();
1125 if (!job_find->has_results)
1126 job_find->has_results = (matches != NULL);
1128 job_find->pages[job_find->current_page] = matches;
1129 g_signal_emit (job_find, job_find_signals[FIND_UPDATED], 0, job_find->current_page);
1131 job_find->current_page = (job_find->current_page + 1) % job_find->n_pages;
1132 if (job_find->current_page == job_find->start_page) {
1133 ev_job_succeeded (job);
1142 ev_job_find_class_init (EvJobFindClass *class)
1144 EvJobClass *job_class = EV_JOB_CLASS (class);
1145 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
1147 job_class->run = ev_job_find_run;
1148 gobject_class->dispose = ev_job_find_dispose;
1150 job_find_signals[FIND_UPDATED] =
1151 g_signal_new ("updated",
1154 G_STRUCT_OFFSET (EvJobFindClass, updated),
1156 g_cclosure_marshal_VOID__INT,
1162 ev_job_find_new (EvDocument *document,
1166 gboolean case_sensitive)
1170 ev_debug_message (DEBUG_JOBS, NULL);
1172 job = g_object_new (EV_TYPE_JOB_FIND, NULL);
1174 EV_JOB (job)->document = g_object_ref (document);
1175 job->start_page = start_page;
1176 job->current_page = start_page;
1177 job->n_pages = n_pages;
1178 job->pages = g_new0 (GList *, n_pages);
1179 job->text = g_strdup (text);
1180 job->case_sensitive = case_sensitive;
1181 job->has_results = FALSE;
1183 return EV_JOB (job);
1187 ev_job_find_get_n_results (EvJobFind *job,
1190 return g_list_length (job->pages[page]);
1194 ev_job_find_get_progress (EvJobFind *job)
1198 if (ev_job_is_finished (EV_JOB (job)))
1201 if (job->current_page > job->start_page) {
1202 pages_done = job->current_page - job->start_page + 1;
1203 } else if (job->current_page == job->start_page) {
1204 pages_done = job->n_pages;
1206 pages_done = job->n_pages - job->start_page + job->current_page;
1209 return pages_done / (gdouble) job->n_pages;
1213 ev_job_find_has_results (EvJobFind *job)
1215 return job->has_results;
1219 ev_job_find_get_results (EvJobFind *job)
1226 ev_job_layers_init (EvJobLayers *job)
1228 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1232 ev_job_layers_dispose (GObject *object)
1236 ev_debug_message (DEBUG_JOBS, NULL);
1238 job = EV_JOB_LAYERS (object);
1241 g_object_unref (job->model);
1245 (* G_OBJECT_CLASS (ev_job_layers_parent_class)->dispose) (object);
1249 ev_job_layers_run (EvJob *job)
1251 EvJobLayers *job_layers = EV_JOB_LAYERS (job);
1253 ev_debug_message (DEBUG_JOBS, NULL);
1254 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1256 ev_document_doc_mutex_lock ();
1257 job_layers->model = ev_document_layers_get_layers (EV_DOCUMENT_LAYERS (job->document));
1258 ev_document_doc_mutex_unlock ();
1260 ev_job_succeeded (job);
1266 ev_job_layers_class_init (EvJobLayersClass *class)
1268 GObjectClass *oclass = G_OBJECT_CLASS (class);
1269 EvJobClass *job_class = EV_JOB_CLASS (class);
1271 oclass->dispose = ev_job_layers_dispose;
1272 job_class->run = ev_job_layers_run;
1276 ev_job_layers_new (EvDocument *document)
1280 ev_debug_message (DEBUG_JOBS, NULL);
1282 job = g_object_new (EV_TYPE_JOB_LAYERS, NULL);
1283 job->document = g_object_ref (document);
1290 ev_job_export_init (EvJobExport *job)
1292 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1297 ev_job_export_dispose (GObject *object)
1301 ev_debug_message (DEBUG_JOBS, NULL);
1303 job = EV_JOB_EXPORT (object);
1306 g_object_unref (job->rc);
1310 (* G_OBJECT_CLASS (ev_job_export_parent_class)->dispose) (object);
1314 ev_job_export_run (EvJob *job)
1316 EvJobExport *job_export = EV_JOB_EXPORT (job);
1319 g_assert (job_export->page != -1);
1321 ev_debug_message (DEBUG_JOBS, NULL);
1322 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1324 ev_document_doc_mutex_lock ();
1326 ev_page = ev_document_get_page (job->document, job_export->page);
1327 if (job_export->rc) {
1328 job->failed = FALSE;
1329 job->finished = FALSE;
1330 g_clear_error (&job->error);
1332 ev_render_context_set_page (job_export->rc, ev_page);
1334 job_export->rc = ev_render_context_new (ev_page, 0, 1.0);
1336 g_object_unref (ev_page);
1338 ev_file_exporter_do_page (EV_FILE_EXPORTER (job->document), job_export->rc);
1340 ev_document_doc_mutex_unlock ();
1342 ev_job_succeeded (job);
1348 ev_job_export_class_init (EvJobExportClass *class)
1350 GObjectClass *oclass = G_OBJECT_CLASS (class);
1351 EvJobClass *job_class = EV_JOB_CLASS (class);
1353 oclass->dispose = ev_job_export_dispose;
1354 job_class->run = ev_job_export_run;
1358 ev_job_export_new (EvDocument *document)
1362 ev_debug_message (DEBUG_JOBS, NULL);
1364 job = g_object_new (EV_TYPE_JOB_EXPORT, NULL);
1365 job->document = g_object_ref (document);
1371 ev_job_export_set_page (EvJobExport *job,
1379 ev_job_print_init (EvJobPrint *job)
1381 EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
1386 ev_job_print_dispose (GObject *object)
1390 ev_debug_message (DEBUG_JOBS, NULL);
1392 job = EV_JOB_PRINT (object);
1395 cairo_destroy (job->cr);
1399 (* G_OBJECT_CLASS (ev_job_print_parent_class)->dispose) (object);
1403 ev_job_print_run (EvJob *job)
1405 EvJobPrint *job_print = EV_JOB_PRINT (job);
1407 cairo_status_t cr_status;
1409 g_assert (job_print->page != -1);
1410 g_assert (job_print->cr != NULL);
1412 ev_debug_message (DEBUG_JOBS, NULL);
1413 ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
1415 job->failed = FALSE;
1416 job->finished = FALSE;
1417 g_clear_error (&job->error);
1419 ev_document_doc_mutex_lock ();
1421 ev_page = ev_document_get_page (job->document, job_print->page);
1422 ev_document_print_print_page (EV_DOCUMENT_PRINT (job->document),
1423 ev_page, job_print->cr);
1424 g_object_unref (ev_page);
1426 ev_document_doc_mutex_unlock ();
1428 cr_status = cairo_status (job_print->cr);
1429 if (cr_status == CAIRO_STATUS_SUCCESS) {
1430 ev_job_succeeded (job);
1434 GTK_PRINT_ERROR_GENERAL,
1435 _("Failed to print page %d: %s"),
1437 cairo_status_to_string (cr_status));
1444 ev_job_print_class_init (EvJobPrintClass *class)
1446 GObjectClass *oclass = G_OBJECT_CLASS (class);
1447 EvJobClass *job_class = EV_JOB_CLASS (class);
1449 oclass->dispose = ev_job_print_dispose;
1450 job_class->run = ev_job_print_run;
1454 ev_job_print_new (EvDocument *document)
1458 ev_debug_message (DEBUG_JOBS, NULL);
1460 job = g_object_new (EV_TYPE_JOB_PRINT, NULL);
1461 job->document = g_object_ref (document);
1467 ev_job_print_set_page (EvJobPrint *job,
1474 ev_job_print_set_cairo (EvJobPrint *job,
1481 cairo_destroy (job->cr);
1482 job->cr = cr ? cairo_reference (cr) : NULL;