]> www.fi.muni.cz Git - evince.git/blob - shell/ev-jobs.c
e54812c27d131a295956efcb35cc7003ddf973e1
[evince.git] / shell / ev-jobs.c
1 #include "ev-jobs.h"
2 #include "ev-job-queue.h"
3 #include "ev-document-thumbnails.h"
4 #include "ev-document-links.h"
5 #include "ev-document-factory.h"
6 #include "ev-file-helpers.h"
7 #include "ev-document-fonts.h"
8 #include "ev-selection.h"
9 #include "ev-async-renderer.h"
10 #include "ev-file-exporter.h"
11 #include "ev-window.h"
12
13 #include <glib/gstdio.h>
14 #include <unistd.h>
15 #include <libgnomevfs/gnome-vfs-uri.h>
16 #include <libgnomevfs/gnome-vfs-utils.h>
17 #include <libgnomevfs/gnome-vfs-ops.h>
18 #include <libgnomevfs/gnome-vfs-xfer.h>
19
20 static void ev_job_init                 (EvJob               *job);
21 static void ev_job_class_init           (EvJobClass          *class);
22 static void ev_job_links_init           (EvJobLinks          *job);
23 static void ev_job_links_class_init     (EvJobLinksClass     *class);
24 static void ev_job_render_init          (EvJobRender         *job);
25 static void ev_job_render_class_init    (EvJobRenderClass    *class);
26 static void ev_job_thumbnail_init       (EvJobThumbnail      *job);
27 static void ev_job_thumbnail_class_init (EvJobThumbnailClass *class);
28 static void ev_job_xfer_init            (EvJobXfer           *job);
29 static void ev_job_xfer_class_init      (EvJobXferClass      *class);
30 static void ev_job_print_init           (EvJobPrint          *job);
31 static void ev_job_print_class_init     (EvJobPrintClass     *class);
32
33 enum
34 {
35         FINISHED,
36         LAST_SIGNAL
37 };
38
39 static guint job_signals[LAST_SIGNAL] = { 0 };
40
41 G_DEFINE_TYPE (EvJob, ev_job, G_TYPE_OBJECT)
42 G_DEFINE_TYPE (EvJobLinks, ev_job_links, EV_TYPE_JOB)
43 G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB)
44 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
45 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
46 G_DEFINE_TYPE (EvJobXfer, ev_job_xfer, EV_TYPE_JOB)
47 G_DEFINE_TYPE (EvJobPrint, ev_job_print, EV_TYPE_JOB)
48
49 static void ev_job_init (EvJob *job) { /* Do Nothing */ }
50
51 static void
52 ev_job_dispose (GObject *object)
53 {
54         EvJob *job;
55
56         job = EV_JOB (object);
57
58         if (job->document) {
59                 g_object_unref (job->document);
60                 job->document = NULL;
61         }
62
63         (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object);
64 }
65
66 static void
67 ev_job_class_init (EvJobClass *class)
68 {
69         GObjectClass *oclass;
70
71         oclass = G_OBJECT_CLASS (class);
72
73         oclass->dispose = ev_job_dispose;
74
75         job_signals [FINISHED] =
76                 g_signal_new ("finished",
77                               EV_TYPE_JOB,
78                               G_SIGNAL_RUN_LAST,
79                               G_STRUCT_OFFSET (EvJobClass, finished),
80                               NULL, NULL,
81                               g_cclosure_marshal_VOID__VOID,
82                               G_TYPE_NONE, 0);
83 }
84
85
86 static void ev_job_links_init (EvJobLinks *job) { /* Do Nothing */ }
87
88 static void
89 ev_job_links_dispose (GObject *object)
90 {
91         EvJobLinks *job;
92
93         job = EV_JOB_LINKS (object);
94
95         if (job->model) {
96                 g_object_unref (job->model);
97                 job->model = NULL;
98         }
99
100         (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object);
101 }
102
103 static void
104 ev_job_links_class_init (EvJobLinksClass *class)
105 {
106         GObjectClass *oclass;
107
108         oclass = G_OBJECT_CLASS (class);
109
110         oclass->dispose = ev_job_links_dispose;
111 }
112
113
114 static void ev_job_render_init (EvJobRender *job) { /* Do Nothing */ }
115
116 static void
117 ev_job_render_dispose (GObject *object)
118 {
119         EvJobRender *job;
120
121         job = EV_JOB_RENDER (object);
122
123         if (job->pixbuf) {
124                 g_object_unref (job->pixbuf);
125                 job->pixbuf = NULL;
126         }
127
128         if (job->rc) {
129                 g_object_unref (job->rc);
130                 job->rc = NULL;
131         }
132
133         if (job->selection) {
134                 g_object_unref (job->selection);
135                 job->selection = NULL;
136         }
137
138         if (job->selection_region) {
139                 gdk_region_destroy (job->selection_region);
140                 job->selection_region = NULL;
141         }
142
143         (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
144 }
145
146 static void
147 ev_job_render_class_init (EvJobRenderClass *class)
148 {
149         GObjectClass *oclass;
150
151         oclass = G_OBJECT_CLASS (class);
152
153         oclass->dispose = ev_job_render_dispose;
154 }
155
156 static void ev_job_thumbnail_init (EvJobThumbnail *job) { /* Do Nothing */ }
157
158 static void
159 ev_job_thumbnail_dispose (GObject *object)
160 {
161         EvJobThumbnail *job;
162
163         job = EV_JOB_THUMBNAIL (object);
164
165         if (job->thumbnail) {
166                 g_object_unref (job->thumbnail);
167                 job->thumbnail = NULL;
168         }
169
170         (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
171 }
172
173 static void
174 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
175 {
176         GObjectClass *oclass;
177
178         oclass = G_OBJECT_CLASS (class);
179
180         oclass->dispose = ev_job_thumbnail_dispose;
181 }
182
183 static void ev_job_print_init (EvJobPrint *job) { /* Do Nothing */ }
184
185 static void
186 ev_job_print_dispose (GObject *object)
187 {
188         EvJobPrint *job;
189
190         job = EV_JOB_PRINT (object);
191
192         if (job->temp_file) {
193                 g_unlink (job->temp_file);
194                 g_free (job->temp_file);
195                 job->temp_file = NULL;
196         }
197
198         if (job->error) {
199                 g_error_free (job->error);
200                 job->error = NULL;
201         }
202
203         if (job->ranges) {
204                 g_free (job->ranges);
205                 job->ranges = NULL;
206                 job->n_ranges = 0;
207         }
208
209         (* G_OBJECT_CLASS (ev_job_print_parent_class)->dispose) (object);
210 }
211
212 static void
213 ev_job_print_class_init (EvJobPrintClass *class)
214 {
215         GObjectClass *oclass;
216
217         oclass = G_OBJECT_CLASS (class);
218
219         oclass->dispose = ev_job_print_dispose;
220 }
221
222 /* Public functions */
223 void
224 ev_job_finished (EvJob *job)
225 {
226         g_return_if_fail (EV_IS_JOB (job));
227
228         g_signal_emit (job, job_signals[FINISHED], 0);
229 }
230
231 EvJob *
232 ev_job_links_new (EvDocument *document)
233 {
234         EvJob *job;
235
236         job = g_object_new (EV_TYPE_JOB_LINKS, NULL);
237         job->document = g_object_ref (document);
238
239         return job;
240 }
241
242 void
243 ev_job_links_run (EvJobLinks *job)
244 {
245         g_return_if_fail (EV_IS_JOB_LINKS (job));
246
247         ev_document_doc_mutex_lock ();
248         job->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (EV_JOB (job)->document));
249         EV_JOB (job)->finished = TRUE;
250         ev_document_doc_mutex_unlock ();
251 }
252
253
254 EvJob *
255 ev_job_render_new (EvDocument      *document,
256                    EvRenderContext *rc,
257                    gint             width,
258                    gint             height,
259                    EvRectangle     *selection_points,
260                    GdkColor        *text,
261                    GdkColor        *base,
262                    gboolean         include_links,
263                    gboolean         include_text,
264                    gboolean         include_selection)
265 {
266         EvJobRender *job;
267
268         g_return_val_if_fail (EV_IS_RENDER_CONTEXT (rc), NULL);
269         if (include_selection)
270                 g_return_val_if_fail (selection_points != NULL, NULL);
271
272         job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
273
274         EV_JOB (job)->document = g_object_ref (document);
275         job->rc = g_object_ref (rc);
276         job->target_width = width;
277         job->target_height = height;
278         job->text = *text;
279         job->base = *base;
280         job->include_links = include_links;
281         job->include_text = include_text;
282         job->include_selection = include_selection;
283
284         if (include_selection)
285                 job->selection_points = *selection_points;
286
287         if (EV_IS_ASYNC_RENDERER (document)) {  
288                 EV_JOB (job)->async = TRUE;
289         }
290
291         return EV_JOB (job);
292 }
293
294 static void
295 render_finished_cb (EvDocument *document, GdkPixbuf *pixbuf, EvJobRender *job)
296 {
297         g_signal_handlers_disconnect_by_func (EV_JOB (job)->document,
298                                               render_finished_cb, job);
299
300         EV_JOB (job)->finished = TRUE;
301         job->pixbuf = g_object_ref (pixbuf);
302         ev_job_finished (EV_JOB (job));
303 }
304
305 void
306 ev_job_render_run (EvJobRender *job)
307 {
308         g_return_if_fail (EV_IS_JOB_RENDER (job));
309
310         ev_document_doc_mutex_lock ();
311
312         if (EV_JOB (job)->async) {
313                 EvAsyncRenderer *renderer = EV_ASYNC_RENDERER (EV_JOB (job)->document);
314                 ev_async_renderer_render_pixbuf (renderer, job->rc->page, job->rc->scale,
315                                                  job->rc->rotation);
316                 g_signal_connect (EV_JOB (job)->document, "render_finished",
317                                   G_CALLBACK (render_finished_cb), job);
318         } else {
319                 ev_document_fc_mutex_lock ();
320                 
321                 job->pixbuf = ev_document_render_pixbuf (EV_JOB (job)->document, job->rc);
322                 if (job->include_links && EV_IS_DOCUMENT_LINKS (EV_JOB (job)->document))
323                         job->link_mapping =
324                                 ev_document_links_get_links (EV_DOCUMENT_LINKS (EV_JOB (job)->document),
325                                                              job->rc->page);
326                 if (job->include_text && EV_IS_SELECTION (EV_JOB (job)->document))
327                         job->text_mapping =
328                                 ev_selection_get_selection_map (EV_SELECTION (EV_JOB (job)->document),
329                                                                 job->rc);
330                 if (job->include_selection && EV_IS_SELECTION (EV_JOB (job)->document)) {
331                         ev_selection_render_selection (EV_SELECTION (EV_JOB (job)->document),
332                                                        job->rc,
333                                                        &(job->selection),
334                                                        &(job->selection_points),
335                                                        NULL,
336                                                        &(job->text), &(job->base));
337                         job->selection_region =
338                                 ev_selection_get_selection_region (EV_SELECTION (EV_JOB (job)->document),
339                                                                    job->rc,
340                                                                    &(job->selection_points));
341                 }
342                 
343                 ev_document_fc_mutex_unlock ();
344                 EV_JOB (job)->finished = TRUE;
345         }
346
347         ev_document_doc_mutex_unlock ();
348 }
349
350 EvJob *
351 ev_job_thumbnail_new (EvDocument   *document,
352                       gint          page,
353                       int           rotation,
354                       gint          requested_width)
355 {
356         EvJobThumbnail *job;
357
358         job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
359
360         EV_JOB (job)->document = g_object_ref (document);
361         job->page = page;
362         job->rotation = rotation;
363         job->requested_width = requested_width;
364
365         return EV_JOB (job);
366 }
367
368 void
369 ev_job_thumbnail_run (EvJobThumbnail *job)
370 {
371         g_return_if_fail (EV_IS_JOB_THUMBNAIL (job));
372
373         ev_document_doc_mutex_lock ();
374
375         job->thumbnail =
376                 ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (EV_JOB (job)->document),
377                                                       job->page,
378                                                       job->rotation,
379                                                       job->requested_width,
380                                                       TRUE);
381         EV_JOB (job)->finished = TRUE;
382
383         ev_document_doc_mutex_unlock ();
384 }
385
386 static void ev_job_fonts_init (EvJobFonts *job) { /* Do Nothing */ }
387
388 static void ev_job_fonts_class_init (EvJobFontsClass *class) { /* Do Nothing */ }
389
390 EvJob *
391 ev_job_fonts_new (EvDocument *document)
392 {
393         EvJobFonts *job;
394
395         job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
396
397         EV_JOB (job)->document = g_object_ref (document);
398
399         return EV_JOB (job);
400 }
401
402 void
403 ev_job_fonts_run (EvJobFonts *job)
404 {
405         EvDocumentFonts *fonts;
406
407         g_return_if_fail (EV_IS_JOB_FONTS (job));
408
409         ev_document_doc_mutex_lock ();
410         
411         fonts = EV_DOCUMENT_FONTS (EV_JOB (job)->document);
412         ev_document_fc_mutex_lock ();
413         job->scan_completed = !ev_document_fonts_scan (fonts, 20);
414         ev_document_fc_mutex_unlock ();
415         
416         EV_JOB (job)->finished = TRUE;
417
418         ev_document_doc_mutex_unlock ();
419 }
420
421 static void ev_job_xfer_init (EvJobXfer *job) { /* Do Nothing */ }
422
423 static void
424 ev_job_xfer_dispose (GObject *object)
425 {
426         EvJobXfer *job = EV_JOB_XFER (object);
427
428         if (job->uri) {
429                 g_free (job->uri);
430                 job->uri = NULL;
431         }
432
433         if (job->local_uri) {
434                 g_free (job->local_uri);
435                 job->local_uri = NULL;
436         }
437
438         if (job->error) {
439                 g_error_free (job->error);
440                 job->error = NULL;
441         }
442
443         if (job->dest) {
444                 g_object_unref (job->dest);
445                 job->dest = NULL;
446         }
447
448         (* G_OBJECT_CLASS (ev_job_xfer_parent_class)->dispose) (object);
449 }
450
451 static void
452 ev_job_xfer_class_init (EvJobXferClass *class)
453 {
454         GObjectClass *oclass;
455
456         oclass = G_OBJECT_CLASS (class);
457
458         oclass->dispose = ev_job_xfer_dispose;
459 }
460
461
462 EvJob *
463 ev_job_xfer_new (const gchar *uri, EvLinkDest *dest, EvWindowRunMode mode)
464 {
465         EvJobXfer *job;
466
467         job = g_object_new (EV_TYPE_JOB_XFER, NULL);
468
469         job->uri = g_strdup (uri);
470         if (dest)
471                 job->dest = g_object_ref (dest);
472
473         job->mode = mode;
474
475         return EV_JOB (job);
476 }
477
478 void
479 ev_job_xfer_run (EvJobXfer *job)
480 {
481         GnomeVFSURI *source_uri;
482         GnomeVFSURI *target_uri;
483
484         g_return_if_fail (EV_IS_JOB_XFER (job));
485         
486         if (job->error) {
487                 g_error_free (job->error);
488                 job->error = NULL;
489         }
490         
491         /* This job may already have a document even if the job didn't complete
492            because, e.g., a password is required - if so, just reload rather than
493            creating a new instance */
494         if (EV_JOB (job)->document) {
495                 ev_document_load (EV_JOB (job)->document,
496                                   job->local_uri ? job->local_uri : job->uri,
497                                   &job->error);
498                 EV_JOB (job)->finished = TRUE;
499                 return;
500         }
501
502         source_uri = gnome_vfs_uri_new (job->uri);
503         if (!gnome_vfs_uri_is_local (source_uri) && !job->local_uri) {
504                 char *tmp_name;
505                 char *base_name;
506                 
507                 /* We'd like to keep extension of source uri since
508                  * it helps to resolve some mime types, say cbz */
509                 
510                 tmp_name = ev_tmp_filename ();
511                 base_name = gnome_vfs_uri_extract_short_name (source_uri);
512                 job->local_uri = g_strconcat ("file:", tmp_name, "-", base_name, NULL);
513                 g_free (base_name);
514                 g_free (tmp_name);
515                 
516                 target_uri = gnome_vfs_uri_new (job->local_uri);
517
518                 gnome_vfs_xfer_uri (source_uri, target_uri, 
519                                     GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
520                                     GNOME_VFS_XFER_ERROR_MODE_ABORT,
521                                     GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
522                                     NULL,
523                                     job);
524                 gnome_vfs_uri_unref (target_uri);
525         }
526         gnome_vfs_uri_unref (source_uri);
527
528         EV_JOB(job)->document = ev_document_factory_get_document (job->local_uri ? job->local_uri : job->uri, &job->error);
529         EV_JOB (job)->finished = TRUE;
530
531         return;
532 }
533
534 EvJob *
535 ev_job_print_new (EvDocument    *document,
536                   const gchar   *format,
537                   gdouble        width,
538                   gdouble        height,
539                   EvPrintRange  *ranges,
540                   gint           n_ranges,
541                   EvPrintPageSet page_set,
542                   gint           copies,
543                   gdouble        collate,
544                   gdouble        reverse)
545 {
546         EvJobPrint *job;
547
548         job = g_object_new (EV_TYPE_JOB_PRINT, NULL);
549
550         EV_JOB (job)->document = g_object_ref (document);
551
552         job->format = format;
553         
554         job->temp_file = NULL;
555         job->error = NULL;
556
557         job->width = width;
558         job->height = height;
559
560         job->ranges = ranges;
561         job->n_ranges = n_ranges;
562
563         job->page_set = page_set;
564         
565         job->copies = copies;
566         job->collate = collate;
567         job->reverse = reverse;
568         
569         return EV_JOB (job);
570 }
571
572 static gint
573 ev_print_job_get_first_page (EvJobPrint *job)
574 {
575         gint i;
576         gint first_page = G_MAXINT;
577         
578         if (job->n_ranges == 0)
579                 return 0;
580
581         for (i = 0; i < job->n_ranges; i++) {
582                 if (job->ranges[i].start < first_page)
583                         first_page = job->ranges[i].start;
584         }
585
586         return MAX (0, first_page);
587 }
588
589 static gint
590 ev_print_job_get_last_page (EvJobPrint *job)
591 {
592         gint i;
593         gint last_page = G_MININT;
594         gint max_page;
595
596         max_page = ev_document_get_n_pages (EV_JOB (job)->document) - 1;
597
598         if (job->n_ranges == 0)
599                 return max_page;
600
601         for (i = 0; i < job->n_ranges; i++) {
602                 if (job->ranges[i].end > last_page)
603                         last_page = job->ranges[i].end;
604         }
605
606         return MIN (max_page, last_page);
607 }
608
609 static gboolean
610 ev_print_job_print_page_in_range (EvJobPrint *job,
611                                   gint        page)
612 {
613         gint i;
614
615         for (i = 0; i < job->n_ranges; i++) {
616                 if (page >= job->ranges[i].start &&
617                     page <= job->ranges[i].end)
618                         return TRUE;
619         }
620
621         return FALSE;
622 }
623
624 static gboolean
625 ev_print_job_print_page_in_set (EvJobPrint *job,
626                                 gint        page)
627 {
628         switch (job->page_set) {
629                 case EV_PRINT_PAGE_SET_EVEN:
630                         return page % 2 == 0;
631                 case EV_PRINT_PAGE_SET_ODD:
632                         return page % 2 != 0;
633                 case EV_PRINT_PAGE_SET_ALL:
634                         return TRUE;
635         }
636
637         return FALSE;
638 }
639
640 static void
641 ev_job_print_do_page (EvJobPrint *job, gint page)
642 {
643         EvDocument      *document = EV_JOB (job)->document;
644         EvRenderContext *rc;
645
646         rc = ev_render_context_new (0, page, 1.0);
647         ev_file_exporter_do_page (EV_FILE_EXPORTER (document), rc);
648         g_object_unref (rc);
649 }
650
651 void
652 ev_job_print_run (EvJobPrint *job)
653 {
654         EvDocument *document = EV_JOB (job)->document;
655         gint        fd;
656         gint        last_page;
657         gint        first_page;
658         gint        i;
659         gchar      *filename;
660         
661         g_return_if_fail (EV_IS_JOB_PRINT (job));
662
663         if (job->temp_file)
664                 g_free (job->temp_file);
665         job->temp_file = NULL;
666         
667         if (job->error)
668                 g_error_free (job->error);
669         job->error = NULL;
670
671         filename = g_strdup_printf ("evince_print.%s.XXXXXX", job->format);
672         fd = g_file_open_tmp (filename, &job->temp_file, &job->error);
673         g_free (filename);
674         if (fd <= -1) {
675                 EV_JOB (job)->finished = TRUE;
676                 return;
677         }
678
679         first_page = ev_print_job_get_first_page (job);
680         last_page = ev_print_job_get_last_page (job);
681
682         ev_document_doc_mutex_lock ();
683         ev_file_exporter_begin (EV_FILE_EXPORTER (document),
684                                 g_ascii_strcasecmp (job->format, "pdf") == 0 ?
685                                 EV_FILE_FORMAT_PDF : EV_FILE_FORMAT_PS,
686                                 job->temp_file,
687                                 MIN (first_page, last_page),
688                                 MAX (first_page, last_page),
689                                 job->width, job->height, FALSE);
690         ev_document_doc_mutex_unlock ();
691
692         for (i = 0; i < job->copies; i++) {
693                 gint page, step;
694                 
695                 step = job->reverse ? -1 : 1;
696                 page = job->reverse ? last_page : first_page;
697                 
698                 while ((job->reverse && (page >= first_page)) ||
699                        (!job->reverse && (page <= last_page))) {
700                         gint n_pages = 1;
701                         gint j;
702
703                         if (job->n_ranges > 0 &&
704                             !ev_print_job_print_page_in_range (job, page)) {
705                                 page += step;
706                                 continue;
707                         }
708
709                         if (!ev_print_job_print_page_in_set (job, page + 1)) {
710                                 page += step;
711                                 continue;
712                         }
713
714                         if (job->collate)
715                                 n_pages = job->copies;
716
717                         for (j = 0; j < n_pages; j++) {
718                                 ev_document_doc_mutex_lock ();
719                                 ev_job_print_do_page (job, page);
720                                 ev_document_doc_mutex_unlock ();
721                         }
722
723                         page += step;
724                 }
725
726                 if (job->collate)
727                         break;
728         }
729
730         ev_document_doc_mutex_lock ();
731         ev_file_exporter_end (EV_FILE_EXPORTER (document));
732         ev_document_doc_mutex_unlock ();
733
734         close (fd);
735         
736         EV_JOB (job)->finished = TRUE;
737 }