Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev-print-operation.c
Go to the documentation of this file.
1 /* this file is part of evince, a gnome document viewer
2  *
3  * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
4  *
5  * Evince is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Evince is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include <config.h>
21 
22 #include "ev-print-operation.h"
23 
24 #if GTKUNIXPRINT_ENABLED
25 #include <gtk/gtkunixprint.h>
26 #endif
27 #include <glib/gi18n.h>
28 #include <glib/gstdio.h>
29 #include <unistd.h>
30 
31 #include "ev-jobs.h"
32 #include "ev-job-scheduler.h"
33 
34 enum {
37 };
38 
39 enum {
44 };
45 
46 static guint signals[LAST_SIGNAL] = { 0 };
47 
49  GObject parent;
50 
52 
53  gboolean print_preview;
54 
55  /* Progress */
56  gchar *status;
57  gdouble progress;
58 };
59 
61  GObjectClass parent_class;
62 
64  gint current_page);
66  GtkPrintSettings *print_settings);
67  GtkPrintSettings *(* get_print_settings) (EvPrintOperation *op);
69  GtkPageSetup *page_setup);
70  GtkPageSetup *(* get_default_page_setup) (EvPrintOperation *op);
72  const gchar *job_name);
73  const gchar *(* get_job_name) (EvPrintOperation *op);
74  void (* run) (EvPrintOperation *op,
75  GtkWindow *parent);
76  void (* cancel) (EvPrintOperation *op);
77  void (* get_error) (EvPrintOperation *op,
78  GError **error);
80  gboolean embed);
82 
83  /* signals */
84  void (* done) (EvPrintOperation *op,
85  GtkPrintOperationResult result);
86  void (* begin_print) (EvPrintOperation *op);
88 };
89 
90 G_DEFINE_ABSTRACT_TYPE (EvPrintOperation, ev_print_operation, G_TYPE_OBJECT)
91 
92 static void
93 ev_print_operation_finalize (GObject *object)
94 {
95  EvPrintOperation *op = EV_PRINT_OPERATION (object);
96 
97  if (op->document) {
98  g_object_unref (op->document);
99  op->document = NULL;
100  }
101 
102  if (op->status) {
103  g_free (op->status);
104  op->status = NULL;
105  }
106 
107  (* G_OBJECT_CLASS (ev_print_operation_parent_class)->finalize) (object);
108 }
109 
110 static void
112  guint prop_id,
113  const GValue *value,
114  GParamSpec *pspec)
115 {
116  EvPrintOperation *op = EV_PRINT_OPERATION (object);
117 
118  switch (prop_id) {
119  case PROP_DOCUMENT:
120  op->document = g_value_dup_object (value);
121  break;
122  default:
123  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124  }
125 }
126 
127 static void
129 {
130 }
131 
132 static void
134 {
135  GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
136 
137  g_object_class->set_property = ev_print_operation_set_property;
138  g_object_class->finalize = ev_print_operation_finalize;
139 
140  g_object_class_install_property (g_object_class,
142  g_param_spec_object ("document",
143  "Document",
144  "The document to print",
146  G_PARAM_WRITABLE |
147  G_PARAM_CONSTRUCT_ONLY |
148  G_PARAM_STATIC_STRINGS));
149  signals[DONE] =
150  g_signal_new ("done",
151  G_TYPE_FROM_CLASS (g_object_class),
152  G_SIGNAL_RUN_LAST,
153  G_STRUCT_OFFSET (EvPrintOperationClass, done),
154  NULL, NULL,
155  g_cclosure_marshal_VOID__ENUM,
156  G_TYPE_NONE, 1,
157  GTK_TYPE_PRINT_OPERATION_RESULT);
159  g_signal_new ("begin_print",
160  G_TYPE_FROM_CLASS (g_object_class),
161  G_SIGNAL_RUN_LAST,
162  G_STRUCT_OFFSET (EvPrintOperationClass, begin_print),
163  NULL, NULL,
164  g_cclosure_marshal_VOID__VOID,
165  G_TYPE_NONE, 0);
167  g_signal_new ("status_changed",
168  G_TYPE_FROM_CLASS (g_object_class),
169  G_SIGNAL_RUN_LAST,
170  G_STRUCT_OFFSET (EvPrintOperationClass, status_changed),
171  NULL, NULL,
172  g_cclosure_marshal_VOID__VOID,
173  G_TYPE_NONE, 0);
174 }
175 
176 /* Public methods */
177 void
179  gint current_page)
180 {
181  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
182 
183  g_return_if_fail (EV_IS_PRINT_OPERATION (op));
184  g_return_if_fail (current_page >= 0);
185 
186  class->set_current_page (op, current_page);
187 }
188 
189 void
191  GtkPrintSettings *print_settings)
192 {
193  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
194 
195  g_return_if_fail (EV_IS_PRINT_OPERATION (op));
196  g_return_if_fail (GTK_IS_PRINT_SETTINGS (print_settings));
197 
198  class->set_print_settings (op, print_settings);
199 }
200 
207 GtkPrintSettings *
209 {
210  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
211 
212  g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
213 
214  return class->get_print_settings (op);
215 }
216 
217 void
219  GtkPageSetup *page_setup)
220 {
221  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
222 
223  g_return_if_fail (EV_IS_PRINT_OPERATION (op));
224  g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
225 
226  class->set_default_page_setup (op, page_setup);
227 }
228 
235 GtkPageSetup *
237 {
238  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
239 
240  g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
241 
242  return class->get_default_page_setup (op);
243 }
244 
245 void
247  const gchar *job_name)
248 {
249  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
250 
251  g_return_if_fail (EV_IS_PRINT_OPERATION (op));
252  g_return_if_fail (job_name != NULL);
253 
254  class->set_job_name (op, job_name);
255 }
256 
257 const gchar *
259 {
260  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
261 
262  g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
263 
264  return class->get_job_name (op);
265 }
266 
267 void
269  GtkWindow *parent)
270 {
271  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
272 
273  g_return_if_fail (EV_IS_PRINT_OPERATION (op));
274 
275  class->run (op, parent);
276 }
277 
278 void
280 {
281  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
282 
283  g_return_if_fail (EV_IS_PRINT_OPERATION (op));
284 
285  class->cancel (op);
286 }
287 
288 void
290  GError **error)
291 {
292  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
293 
294  g_return_if_fail (EV_IS_PRINT_OPERATION (op));
295 
296  class->get_error (op, error);
297 }
298 
299 void
301  gboolean embed)
302 {
303  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
304 
305  g_return_if_fail (EV_IS_PRINT_OPERATION (op));
306 
307  class->set_embed_page_setup (op, embed);
308 }
309 
310 gboolean
312 {
313  EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
314 
315  g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), FALSE);
316 
317  return class->get_embed_page_setup (op);
318 }
319 
320 const gchar *
322 {
323  g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
324 
325  return op->status ? op->status : "";
326 }
327 
328 gdouble
330 {
331  g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), 0.0);
332 
333  return op->progress;
334 }
335 
336 static void
338  gint page,
339  gint n_pages,
340  gdouble progress)
341 {
342  if (op->status && op->progress == progress)
343  return;
344 
345  g_free (op->status);
346 
347  if (op->print_preview) {
348  if (page == -1) {
349  /* Initial state */
350  op->status = g_strdup (_("Preparing preview…"));
351  } else if (page > n_pages) {
352  op->status = g_strdup (_("Finishing…"));
353  } else {
354  op->status = g_strdup_printf (_("Generating preview: page %d of %d"),
355  page, n_pages);
356  }
357  } else {
358  if (page == -1) {
359  /* Initial state */
360  op->status = g_strdup (_("Preparing to print…"));
361  } else if (page > n_pages) {
362  op->status = g_strdup (_("Finishing…"));
363  } else {
364  op->status = g_strdup_printf (_("Printing page %d of %d…"),
365  page, n_pages);
366  }
367  }
368 
369  op->progress = MIN (1.0, progress);
370 
371  g_signal_emit (op, signals[STATUS_CHANGED], 0);
372 }
373 
374 #if GTKUNIXPRINT_ENABLED
375 
376 /* Export interface */
377 #define EV_TYPE_PRINT_OPERATION_EXPORT (ev_print_operation_export_get_type())
378 #define EV_PRINT_OPERATION_EXPORT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_EXPORT, EvPrintOperationExport))
379 #define EV_PRINT_OPERATION_EXPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION_EXPORT, EvPrintOperationExportClass))
380 #define EV_IS_PRINT_OPERATION_EXPORT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION_EXPORT))
381 
382 typedef struct _EvPrintOperationExport EvPrintOperationExport;
383 typedef struct _EvPrintOperationExportClass EvPrintOperationExportClass;
384 
385 static GType ev_print_operation_export_get_type (void) G_GNUC_CONST;
386 
387 static void ev_print_operation_export_begin (EvPrintOperationExport *export);
388 static gboolean export_print_page (EvPrintOperationExport *export);
389 static void export_cancel (EvPrintOperationExport *export);
390 
391 struct _EvPrintOperationExport {
392  EvPrintOperation parent;
393 
394  GtkWindow *parent_window;
395  EvJob *job_export;
396  GError *error;
397 
398  gint n_pages;
399  gint current_page;
400  GtkPrinter *printer;
401  GtkPageSetup *page_setup;
402  GtkPrintSettings *print_settings;
403  GtkPageSet page_set;
404  gint copies;
405  guint collate : 1;
406  guint reverse : 1;
407  gint pages_per_sheet;
408  gint fd;
409  gchar *temp_file;
410  gchar *job_name;
411  gboolean embed_page_setup;
412 
413  guint idle_id;
414 
415  /* Context */
417  gint n_pages_to_print;
418  gint uncollated_copies;
419  gint collated_copies;
420  gint uncollated, collated, total;
421 
422  gint sheet, page_count;
423 
424  gint range, n_ranges;
425  GtkPageRange *ranges;
426  GtkPageRange one_range;
427 
428  gint page, start, end, inc;
429 };
430 
431 struct _EvPrintOperationExportClass {
433 };
434 
435 G_DEFINE_TYPE (EvPrintOperationExport, ev_print_operation_export, EV_TYPE_PRINT_OPERATION)
436 
437 /* Internal print queue */
438 static GHashTable *print_queue = NULL;
439 
440 static void
441 queue_free (GQueue *queue)
442 {
443  g_queue_foreach (queue, (GFunc)g_object_unref, NULL);
444  g_queue_free (queue);
445 }
446 
447 static void
448 ev_print_queue_init (void)
449 {
450  if (G_UNLIKELY (print_queue == NULL)) {
451  print_queue = g_hash_table_new_full (g_direct_hash,
452  g_direct_equal,
453  NULL,
454  (GDestroyNotify)queue_free);
455  }
456 }
457 
458 static void
459 remove_document_queue (gpointer data,
460  GObject *document)
461 {
462  if (print_queue)
463  g_hash_table_remove (print_queue, document);
464 }
465 
466 static gboolean
467 ev_print_queue_is_empty (EvDocument *document)
468 {
469  GQueue *queue;
470 
471  queue = g_hash_table_lookup (print_queue, document);
472  return (!queue || g_queue_is_empty (queue));
473 }
474 
475 static void
476 ev_print_queue_push (EvPrintOperation *op)
477 {
478  GQueue *queue;
479 
480  queue = g_hash_table_lookup (print_queue, op->document);
481  if (!queue) {
482  queue = g_queue_new ();
483  g_hash_table_insert (print_queue,
484  op->document,
485  queue);
486  g_object_weak_ref (G_OBJECT (op->document),
487  (GWeakNotify)remove_document_queue,
488  NULL);
489  }
490 
491  g_queue_push_head (queue, g_object_ref (op));
492 }
493 
494 static EvPrintOperation *
495 ev_print_queue_pop (EvDocument *document)
496 {
497  EvPrintOperation *op;
498  GQueue *queue;
499 
500  queue = g_hash_table_lookup (print_queue, document);
501  if (!queue || g_queue_is_empty (queue))
502  return NULL;
503 
504  op = g_queue_pop_tail (queue);
505  g_object_unref (op);
506 
507  return op;
508 }
509 
510 static EvPrintOperation *
511 ev_print_queue_peek (EvDocument *document)
512 {
513  GQueue *queue;
514 
515  queue = g_hash_table_lookup (print_queue, document);
516  if (!queue || g_queue_is_empty (queue))
517  return NULL;
518 
519  return g_queue_peek_tail (queue);
520 }
521 
522 static void
523 ev_print_operation_export_set_current_page (EvPrintOperation *op,
524  gint current_page)
525 {
526  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
527 
528  g_return_if_fail (current_page < export->n_pages);
529 
530  export->current_page = current_page;
531 }
532 
533 static void
534 ev_print_operation_export_set_print_settings (EvPrintOperation *op,
535  GtkPrintSettings *print_settings)
536 {
537  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
538 
539  if (print_settings == export->print_settings)
540  return;
541 
542  g_object_ref (print_settings);
543  if (export->print_settings)
544  g_object_unref (export->print_settings);
545  export->print_settings = print_settings;
546 }
547 
548 static GtkPrintSettings *
549 ev_print_operation_export_get_print_settings (EvPrintOperation *op)
550 {
551  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
552 
553  return export->print_settings;
554 }
555 
556 static void
557 ev_print_operation_export_set_default_page_setup (EvPrintOperation *op,
558  GtkPageSetup *page_setup)
559 {
560  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
561 
562  if (page_setup == export->page_setup)
563  return;
564 
565  g_object_ref (page_setup);
566  if (export->page_setup)
567  g_object_unref (export->page_setup);
568  export->page_setup = page_setup;
569 }
570 
571 static GtkPageSetup *
572 ev_print_operation_export_get_default_page_setup (EvPrintOperation *op)
573 {
574  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
575 
576  return export->page_setup;
577 }
578 
579 static void
580 ev_print_operation_export_set_job_name (EvPrintOperation *op,
581  const gchar *job_name)
582 {
583  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
584 
585  g_free (export->job_name);
586  export->job_name = g_strdup (job_name);
587 }
588 
589 static const gchar *
590 ev_print_operation_export_get_job_name (EvPrintOperation *op)
591 {
592  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
593 
594  return export->job_name;
595 }
596 
597 static void
598 ev_print_operation_export_set_printer (EvPrintOperationExport *export,
599  GtkPrinter *printer)
600 {
601  if (printer == export->printer)
602  return;
603 
604  g_object_ref (printer);
605  if (export->printer)
606  g_object_unref (export->printer);
607  export->printer = printer;
608 }
609 
610 static void
611 find_range (EvPrintOperationExport *export)
612 {
613  GtkPageRange *range;
614 
615  range = &export->ranges[export->range];
616 
617  if (export->inc < 0) {
618  export->start = range->end;
619  export->end = range->start - 1;
620  } else {
621  export->start = range->start;
622  export->end = range->end + 1;
623  }
624 }
625 
626 static gboolean
627 clamp_ranges (EvPrintOperationExport *export)
628 {
629  gint num_of_correct_ranges = 0;
630  gint n_pages_to_print = 0;
631  gint i;
632  gboolean null_flag = FALSE;
633 
634  for (i = 0; i < export->n_ranges; i++) {
635  gint n_pages;
636 
637  if ((export->ranges[i].start >= 0) &&
638  (export->ranges[i].start < export->n_pages) &&
639  (export->ranges[i].end >= 0) &&
640  (export->ranges[i].end < export->n_pages)) {
641  export->ranges[num_of_correct_ranges] = export->ranges[i];
642  num_of_correct_ranges++;
643  } else if ((export->ranges[i].start >= 0) &&
644  (export->ranges[i].start < export->n_pages) &&
645  (export->ranges[i].end >= export->n_pages)) {
646  export->ranges[i].end = export->n_pages - 1;
647  export->ranges[num_of_correct_ranges] = export->ranges[i];
648  num_of_correct_ranges++;
649  } else if ((export->ranges[i].end >= 0) &&
650  (export->ranges[i].end < export->n_pages) &&
651  (export->ranges[i].start < 0)) {
652  export->ranges[i].start = 0;
653  export->ranges[num_of_correct_ranges] = export->ranges[i];
654  num_of_correct_ranges++;
655  }
656 
657  n_pages = export->ranges[i].end - export->ranges[i].start + 1;
658  if (export->page_set == GTK_PAGE_SET_ALL) {
659  n_pages_to_print += n_pages;
660  } else if (n_pages % 2 == 0) {
661  n_pages_to_print += n_pages / 2;
662  } else if (export->page_set == GTK_PAGE_SET_EVEN) {
663  if (n_pages==1 && export->ranges[i].start % 2 == 0)
664  null_flag = TRUE;
665  else
666  n_pages_to_print += export->ranges[i].start % 2 == 0 ?
667  n_pages / 2 : (n_pages / 2) + 1;
668  } else if (export->page_set == GTK_PAGE_SET_ODD) {
669  if (n_pages==1 && export->ranges[i].start % 2 != 0)
670  null_flag = TRUE;
671  else
672  n_pages_to_print += export->ranges[i].start % 2 == 0 ?
673  (n_pages / 2) + 1 : n_pages / 2;
674  }
675  }
676 
677  if (null_flag && !n_pages_to_print) {
678  return FALSE;
679  } else {
680  export->n_ranges = num_of_correct_ranges;
681  export->n_pages_to_print = n_pages_to_print;
682  return TRUE;
683  }
684 }
685 
686 static void
687 get_first_and_last_page (EvPrintOperationExport *export,
688  gint *first,
689  gint *last)
690 {
691  gint i;
692  gint first_page = G_MAXINT;
693  gint last_page = G_MININT;
694  gint max_page = export->n_pages - 1;
695 
696  if (export->n_ranges == 0) {
697  *first = 0;
698  *last = max_page;
699 
700  return;
701  }
702 
703  for (i = 0; i < export->n_ranges; i++) {
704  if (export->ranges[i].start < first_page)
705  first_page = export->ranges[i].start;
706  if (export->ranges[i].end > last_page)
707  last_page = export->ranges[i].end;
708  }
709 
710  *first = MAX (0, first_page);
711  *last = MIN (max_page, last_page);
712 }
713 
714 static gboolean
715 export_print_inc_page (EvPrintOperationExport *export)
716 {
717  do {
718  export->page += export->inc;
719 
720  /* note: when NOT collating, page_count is increased in export_print_page */
721  if (export->collate) {
722  export->page_count++;
723  export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
724  }
725 
726  if (export->page == export->end) {
727  export->range += export->inc;
728  if (export->range == -1 || export->range == export->n_ranges) {
729  export->uncollated++;
730 
731  /* when printing multiple collated copies & multiple pages per sheet we want to
732  * prevent the next copy bleeding into the last sheet of the previous one
733  * we've reached the last range to be printed now, so this is the time to do it */
734  if (export->pages_per_sheet > 1 && export->collate == 1 &&
735  (export->page_count - 1) % export->pages_per_sheet != 0) {
736 
737  EvPrintOperation *op = EV_PRINT_OPERATION (export);
739 
740  /* keep track of all blanks but only actualise those
741  * which are in the current odd / even sheet set */
742 
743  export->page_count += export->pages_per_sheet - (export->page_count - 1) % export->pages_per_sheet;
744  if (export->page_set == GTK_PAGE_SET_ALL ||
745  (export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0) ||
746  (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1) ) {
748  }
750  export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
751  }
752 
753  if (export->uncollated == export->uncollated_copies)
754  return FALSE;
755 
756  export->range = export->inc < 0 ? export->n_ranges - 1 : 0;
757  }
758  find_range (export);
759  export->page = export->start;
760  }
761 
762  /* in/decrement the page number until we reach the first page on the next EVEN or ODD sheet
763  * if we're not collating, we have to make sure that this is done only once! */
764  } while ( export->collate == 1 &&
765  ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 1) ||
766  (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 0)));
767 
768  return TRUE;
769 }
770 
771 static void
772 ev_print_operation_export_clear_temp_file (EvPrintOperationExport *export)
773 {
774  if (!export->temp_file)
775  return;
776 
777  g_unlink (export->temp_file);
778  g_free (export->temp_file);
779  export->temp_file = NULL;
780 }
781 
782 static void
783 ev_print_operation_export_run_next (EvPrintOperationExport *export)
784 {
785  EvPrintOperation *op = EV_PRINT_OPERATION (export);
786  EvPrintOperation *next;
787  EvDocument *document;
788 
789  /* First pop the current job */
790  document = op->document;
791  ev_print_queue_pop (document);
792 
793  next = ev_print_queue_peek (document);
794  if (next)
795  ev_print_operation_export_begin (EV_PRINT_OPERATION_EXPORT (next));
796 }
797 
798 static void
799 gtk_print_job_finished (GtkPrintJob *print_job,
800  EvPrintOperationExport *export,
801  GError *error)
802 {
803  EvPrintOperation *op = EV_PRINT_OPERATION (export);
804 
805  if (error) {
806  g_set_error_literal (&export->error,
807  GTK_PRINT_ERROR,
808  GTK_PRINT_ERROR_GENERAL,
809  error->message);
810  g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
811  } else {
812  g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
813  }
814 
815  ev_print_operation_export_clear_temp_file (export);
816  g_object_unref (print_job);
817 
818  ev_print_operation_export_run_next (export);
819 }
820 
821 static void
822 export_print_done (EvPrintOperationExport *export)
823 {
824  EvPrintOperation *op = EV_PRINT_OPERATION (export);
825  GtkPrintSettings *settings;
826  EvFileExporterCapabilities capabilities;
827  GError *error = NULL;
828 
829  g_assert (export->temp_file != NULL);
830 
831  /* Some printers take into account some print settings,
832  * and others don't. However we have exported the document
833  * to a ps or pdf file according to such print settings. So,
834  * we want to send the exported file to printer with those
835  * settings set to default values.
836  */
837  settings = gtk_print_settings_copy (export->print_settings);
838  capabilities = ev_file_exporter_get_capabilities (EV_FILE_EXPORTER (op->document));
839 
840  gtk_print_settings_set_page_ranges (settings, NULL, 0);
841  gtk_print_settings_set_print_pages (settings, GTK_PRINT_PAGES_ALL);
842  if (capabilities & EV_FILE_EXPORTER_CAN_COPIES)
843  gtk_print_settings_set_n_copies (settings, 1);
844  if (capabilities & EV_FILE_EXPORTER_CAN_PAGE_SET)
845  gtk_print_settings_set_page_set (settings, GTK_PAGE_SET_ALL);
846  if (capabilities & EV_FILE_EXPORTER_CAN_SCALE)
847  gtk_print_settings_set_scale (settings, 1.0);
848  if (capabilities & EV_FILE_EXPORTER_CAN_COLLATE)
849  gtk_print_settings_set_collate (settings, FALSE);
850  if (capabilities & EV_FILE_EXPORTER_CAN_REVERSE)
851  gtk_print_settings_set_reverse (settings, FALSE);
852  if (capabilities & EV_FILE_EXPORTER_CAN_NUMBER_UP) {
853  gtk_print_settings_set_number_up (settings, 1);
854  gtk_print_settings_set_int (settings, "cups-"GTK_PRINT_SETTINGS_NUMBER_UP, 1);
855  }
856 
857  if (op->print_preview) {
858  GKeyFile *key_file;
859  gchar *data = NULL;
860  gsize data_len;
861  gchar *print_settings_file = NULL;
862 
863  key_file = g_key_file_new ();
864 
865  gtk_print_settings_to_key_file (settings, key_file, NULL);
866  gtk_page_setup_to_key_file (export->page_setup, key_file, NULL);
867  g_key_file_set_string (key_file, "Print Job", "title", export->job_name);
868 
869  data = g_key_file_to_data (key_file, &data_len, &error);
870  if (data) {
871  gint fd;
872 
873  fd = g_file_open_tmp ("print-settingsXXXXXX", &print_settings_file, &error);
874  if (!error)
875  g_file_set_contents (print_settings_file, data, data_len, &error);
876  close (fd);
877 
878  g_free (data);
879  }
880 
881  g_key_file_free (key_file);
882 
883  if (!error) {
884  gchar *cmd;
885  gchar *quoted_filename;
886  gchar *quoted_settings_filename;
887  GAppInfo *app;
888  GdkAppLaunchContext *ctx;
889 
890  quoted_filename = g_shell_quote (export->temp_file);
891  quoted_settings_filename = g_shell_quote (print_settings_file);
892  cmd = g_strdup_printf ("evince-previewer --unlink-tempfile --print-settings %s %s",
893  quoted_settings_filename, quoted_filename);
894 
895  g_free (quoted_filename);
896  g_free (quoted_settings_filename);
897 
898  app = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
899 
900  if (app != NULL) {
901  ctx = gdk_display_get_app_launch_context (gtk_widget_get_display (GTK_WIDGET (export->parent_window)));
902  gdk_app_launch_context_set_screen (ctx, gtk_window_get_screen (export->parent_window));
903 
904  g_app_info_launch (app, NULL, G_APP_LAUNCH_CONTEXT (ctx), &error);
905 
906  g_object_unref (app);
907  g_object_unref (ctx);
908  }
909 
910  g_free (cmd);
911  }
912 
913  if (error) {
914  if (print_settings_file)
915  g_unlink (print_settings_file);
916  g_free (print_settings_file);
917  } else {
918  g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
919  /* temp_file will be deleted by the previewer */
920 
921  ev_print_operation_export_run_next (export);
922  }
923  } else {
924  GtkPrintJob *job;
925 
926  job = gtk_print_job_new (export->job_name,
927  export->printer,
928  settings,
929  export->page_setup);
930  gtk_print_job_set_source_file (job, export->temp_file, &error);
931  if (!error){
932  gtk_print_job_send (job,
933  (GtkPrintJobCompleteFunc)gtk_print_job_finished,
934  g_object_ref (export),
935  (GDestroyNotify)g_object_unref);
936  }
937  }
938  g_object_unref (settings);
939 
940  if (error) {
941  g_set_error_literal (&export->error,
942  GTK_PRINT_ERROR,
943  GTK_PRINT_ERROR_GENERAL,
944  error->message);
945  g_error_free (error);
946  ev_print_operation_export_clear_temp_file (export);
947  g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
948 
949  ev_print_operation_export_run_next (export);
950  }
951 }
952 
953 static void
954 export_print_page_idle_finished (EvPrintOperationExport *export)
955 {
956  export->idle_id = 0;
957 }
958 
959 static void
960 export_job_finished (EvJobExport *job,
961  EvPrintOperationExport *export)
962 {
963  EvPrintOperation *op = EV_PRINT_OPERATION (export);
964 
965  if (export->pages_per_sheet == 1 ||
966  ( export->page_count % export->pages_per_sheet == 0 &&
967  ( export->page_set == GTK_PAGE_SET_ALL ||
968  ( export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0 ) ||
969  ( export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1 ) ) ) ) {
970 
974  }
975 
976  /* Reschedule */
977  export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
978  (GSourceFunc)export_print_page,
979  export,
980  (GDestroyNotify)export_print_page_idle_finished);
981 }
982 
983 static void
984 export_job_cancelled (EvJobExport *job,
985  EvPrintOperationExport *export)
986 {
987  export_cancel (export);
988 }
989 
990 static void
991 export_cancel (EvPrintOperationExport *export)
992 {
993  EvPrintOperation *op = EV_PRINT_OPERATION (export);
994 
995  if (export->idle_id > 0)
996  g_source_remove (export->idle_id);
997  export->idle_id = 0;
998 
999  if (export->job_export) {
1000  g_signal_handlers_disconnect_by_func (export->job_export,
1001  export_job_finished,
1002  export);
1003  g_signal_handlers_disconnect_by_func (export->job_export,
1004  export_job_cancelled,
1005  export);
1006  g_object_unref (export->job_export);
1007  export->job_export = NULL;
1008  }
1009 
1010  if (export->fd != -1) {
1011  close (export->fd);
1012  export->fd = -1;
1013  }
1014 
1015  ev_print_operation_export_clear_temp_file (export);
1016 
1017  g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
1018 
1019  ev_print_operation_export_run_next (export);
1020 }
1021 
1022 static void
1023 update_progress (EvPrintOperationExport *export)
1024 {
1025  EvPrintOperation *op = EV_PRINT_OPERATION (export);
1026 
1027  ev_print_operation_update_status (op, export->total,
1028  export->n_pages_to_print,
1029  export->total / (gdouble)export->n_pages_to_print);
1030 }
1031 
1032 static gboolean
1033 export_print_page (EvPrintOperationExport *export)
1034 {
1035  EvPrintOperation *op = EV_PRINT_OPERATION (export);
1036 
1037  if (!export->temp_file)
1038  return FALSE; /* cancelled */
1039 
1040  export->total++;
1041  export->collated++;
1042 
1043  /* note: when collating, page_count is increased in export_print_inc_page */
1044  if (!export->collate) {
1045  export->page_count++;
1046  export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
1047  }
1048 
1049  if (export->collated == export->collated_copies) {
1050  export->collated = 0;
1051  if (!export_print_inc_page (export)) {
1053  ev_file_exporter_end (EV_FILE_EXPORTER (op->document));
1055 
1056  close (export->fd);
1057  export->fd = -1;
1058  update_progress (export);
1059  export_print_done (export);
1060 
1061  return FALSE;
1062  }
1063  }
1064 
1065  /* we're not collating and we've reached a sheet from the wrong sheet set */
1066  if (!export->collate &&
1067  ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) ||
1068  (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1))) {
1069 
1070  do {
1071  export->page_count++;
1072  export->collated++;
1073  export->sheet = 1 + (export->page_count - 1) / export->pages_per_sheet;
1074 
1075  if (export->collated == export->collated_copies) {
1076  export->collated = 0;
1077 
1078  if (!export_print_inc_page (export)) {
1080  ev_file_exporter_end (EV_FILE_EXPORTER (op->document));
1082 
1083  close (export->fd);
1084  export->fd = -1;
1085 
1086  update_progress (export);
1087 
1088  export_print_done (export);
1089  return FALSE;
1090  }
1091  }
1092 
1093  } while ((export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 != 0) ||
1094  (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 != 1));
1095 
1096  }
1097 
1098  if (export->pages_per_sheet == 1 ||
1099  (export->page_count % export->pages_per_sheet == 1 &&
1100  (export->page_set == GTK_PAGE_SET_ALL ||
1101  (export->page_set == GTK_PAGE_SET_EVEN && export->sheet % 2 == 0) ||
1102  (export->page_set == GTK_PAGE_SET_ODD && export->sheet % 2 == 1)))) {
1106  }
1107 
1108  if (!export->job_export) {
1109  export->job_export = ev_job_export_new (op->document);
1110  g_signal_connect (export->job_export, "finished",
1111  G_CALLBACK (export_job_finished),
1112  (gpointer)export);
1113  g_signal_connect (export->job_export, "cancelled",
1114  G_CALLBACK (export_job_cancelled),
1115  (gpointer)export);
1116  }
1117 
1118  ev_job_export_set_page (EV_JOB_EXPORT (export->job_export), export->page);
1119  ev_job_scheduler_push_job (export->job_export, EV_JOB_PRIORITY_NONE);
1120 
1121  update_progress (export);
1122 
1123  return FALSE;
1124 }
1125 
1126 static void
1127 ev_print_operation_export_begin (EvPrintOperationExport *export)
1128 {
1129  EvPrintOperation *op = EV_PRINT_OPERATION (export);
1130 
1131  if (!export->temp_file)
1132  return; /* cancelled */
1133 
1135  ev_file_exporter_begin (EV_FILE_EXPORTER (op->document), &export->fc);
1137 
1138  export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1139  (GSourceFunc)export_print_page,
1140  export,
1141  (GDestroyNotify)export_print_page_idle_finished);
1142 }
1143 
1144 static EvFileExporterFormat
1145 get_file_exporter_format (EvFileExporter *exporter,
1146  GtkPrintSettings *print_settings)
1147 {
1148  const gchar *file_format;
1150 
1151  file_format = gtk_print_settings_get (print_settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
1152  if (file_format != NULL) {
1153  format = g_ascii_strcasecmp (file_format, "pdf") == 0 ?
1155  } else {
1156  if (ev_file_exporter_get_capabilities (exporter) &
1158  format = EV_FILE_FORMAT_PDF;
1159  else
1160  format = EV_FILE_FORMAT_PS;
1161  }
1162 
1163  return format;
1164 }
1165 
1166 static void
1167 ev_print_operation_export_print_dialog_response_cb (GtkDialog *dialog,
1168  gint response,
1169  EvPrintOperationExport *export)
1170 {
1171  GtkPrintPages print_pages;
1172  GtkPrintSettings *print_settings;
1173  GtkPageSetup *page_setup;
1174  GtkPrinter *printer;
1175  gdouble scale;
1176  gdouble width;
1177  gdouble height;
1178  gint first_page;
1179  gint last_page;
1180  gchar *filename;
1181  GError *error = NULL;
1182  EvPrintOperation *op = EV_PRINT_OPERATION (export);
1183  EvFileExporterFormat format;
1184 
1185  if (response != GTK_RESPONSE_OK &&
1186  response != GTK_RESPONSE_APPLY) {
1187  gtk_widget_destroy (GTK_WIDGET (dialog));
1188  g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
1189 
1190  return;
1191  }
1192 
1193  op->print_preview = (response == GTK_RESPONSE_APPLY);
1194 
1195  printer = gtk_print_unix_dialog_get_selected_printer (GTK_PRINT_UNIX_DIALOG (dialog));
1196  ev_print_operation_export_set_printer (export, printer);
1197 
1198  print_settings = gtk_print_unix_dialog_get_settings (GTK_PRINT_UNIX_DIALOG (dialog));
1199  ev_print_operation_export_set_print_settings (op, print_settings);
1200 
1201  page_setup = gtk_print_unix_dialog_get_page_setup (GTK_PRINT_UNIX_DIALOG (dialog));
1202  ev_print_operation_export_set_default_page_setup (op, page_setup);
1203 
1204  format = get_file_exporter_format (EV_FILE_EXPORTER (op->document),
1205  print_settings);
1206 
1207  if ((format == EV_FILE_FORMAT_PS && !gtk_printer_accepts_ps (export->printer)) ||
1208  (format == EV_FILE_FORMAT_PDF && !gtk_printer_accepts_pdf (export->printer))) {
1209  gtk_widget_destroy (GTK_WIDGET (dialog));
1210 
1211  g_set_error_literal (&export->error,
1212  GTK_PRINT_ERROR,
1213  GTK_PRINT_ERROR_GENERAL,
1214  _("Requested format is not supported by this printer."));
1215  g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1216 
1217  return;
1218  }
1219 
1220  filename = g_strdup_printf ("evince_print.%s.XXXXXX", format == EV_FILE_FORMAT_PDF ? "pdf" : "ps");
1221  export->fd = g_file_open_tmp (filename, &export->temp_file, &error);
1222  g_free (filename);
1223  if (export->fd <= -1) {
1224  gtk_widget_destroy (GTK_WIDGET (dialog));
1225 
1226  g_set_error_literal (&export->error,
1227  GTK_PRINT_ERROR,
1228  GTK_PRINT_ERROR_GENERAL,
1229  error->message);
1230  g_error_free (error);
1231  g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1232 
1233  return;
1234  }
1235 
1236  export->current_page = gtk_print_unix_dialog_get_current_page (GTK_PRINT_UNIX_DIALOG (dialog));
1237  export->page_set = gtk_print_settings_get_page_set (print_settings);
1238  print_pages = gtk_print_settings_get_print_pages (print_settings);
1239 
1240  switch (print_pages) {
1241  case GTK_PRINT_PAGES_CURRENT:
1242  export->ranges = &export->one_range;
1243 
1244  export->ranges[0].start = export->current_page;
1245  export->ranges[0].end = export->current_page;
1246  export->n_ranges = 1;
1247 
1248  break;
1249  case GTK_PRINT_PAGES_RANGES: {
1250  gint i;
1251 
1252  export->ranges = gtk_print_settings_get_page_ranges (print_settings, &export->n_ranges);
1253  for (i = 0; i < export->n_ranges; i++)
1254  if (export->ranges[i].end == -1 || export->ranges[i].end >= export->n_pages)
1255  export->ranges[i].end = export->n_pages - 1;
1256  }
1257  break;
1258  default:
1259  g_warning ("Unsupported print pages setting\n");
1260  case GTK_PRINT_PAGES_ALL:
1261  export->ranges = &export->one_range;
1262 
1263  export->ranges[0].start = 0;
1264  export->ranges[0].end = export->n_pages - 1;
1265  export->n_ranges = 1;
1266 
1267  break;
1268  }
1269 
1270  if (export->n_ranges < 1 || !clamp_ranges (export)) {
1271  GtkWidget *message_dialog;
1272 
1273  message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
1274  GTK_DIALOG_MODAL,
1275  GTK_MESSAGE_WARNING,
1276  GTK_BUTTONS_CLOSE,
1277  "%s", _("Invalid page selection"));
1278  gtk_window_set_title (GTK_WINDOW (message_dialog), _("Warning"));
1279  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (message_dialog),
1280  "%s", _("Your print range selection does not include any pages"));
1281  g_signal_connect (message_dialog, "response",
1282  G_CALLBACK (gtk_widget_destroy),
1283  NULL);
1284  gtk_widget_show (message_dialog);
1285 
1286  return;
1287  } else ev_print_operation_update_status (op, -1, -1, 0.0);
1288 
1289  width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1290  height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1291  scale = gtk_print_settings_get_scale (print_settings) * 0.01;
1292  if (scale != 1.0) {
1293  width *= scale;
1294  height *= scale;
1295  }
1296 
1297  export->pages_per_sheet = MAX (1, gtk_print_settings_get_number_up (print_settings));
1298 
1299  export->copies = gtk_print_settings_get_n_copies (print_settings);
1300  export->collate = gtk_print_settings_get_collate (print_settings);
1301  export->reverse = gtk_print_settings_get_reverse (print_settings);
1302 
1303  if (export->collate) {
1304  export->uncollated_copies = export->copies;
1305  export->collated_copies = 1;
1306  } else {
1307  export->uncollated_copies = 1;
1308  export->collated_copies = export->copies;
1309  }
1310 
1311  if (export->reverse) {
1312  export->range = export->n_ranges - 1;
1313  export->inc = -1;
1314  } else {
1315  export->range = 0;
1316  export->inc = 1;
1317  }
1318  find_range (export);
1319 
1320  export->page = export->start - export->inc;
1321  export->collated = export->collated_copies - 1;
1322 
1323  get_first_and_last_page (export, &first_page, &last_page);
1324 
1325  export->fc.format = format;
1326  export->fc.filename = export->temp_file;
1327  export->fc.first_page = MIN (first_page, last_page);
1328  export->fc.last_page = MAX (first_page, last_page);
1329  export->fc.paper_width = width;
1330  export->fc.paper_height = height;
1331  export->fc.duplex = FALSE;
1332  export->fc.pages_per_sheet = export->pages_per_sheet;
1333 
1334  if (ev_print_queue_is_empty (op->document))
1335  ev_print_operation_export_begin (export);
1336 
1337  ev_print_queue_push (op);
1338 
1339  g_signal_emit (op, signals[BEGIN_PRINT], 0);
1340 
1341  gtk_widget_destroy (GTK_WIDGET (dialog));
1342 }
1343 
1344 static void
1345 ev_print_operation_export_run (EvPrintOperation *op,
1346  GtkWindow *parent)
1347 {
1348  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1349  GtkWidget *dialog;
1350  GtkPrintCapabilities capabilities;
1351 
1352  ev_print_queue_init ();
1353 
1354  export->parent_window = parent;
1355  export->error = NULL;
1356 
1357  /* translators: Title of the print dialog */
1358  dialog = gtk_print_unix_dialog_new (_("Print"), parent);
1359  gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
1360 
1361  capabilities = GTK_PRINT_CAPABILITY_PREVIEW |
1363  gtk_print_unix_dialog_set_manual_capabilities (GTK_PRINT_UNIX_DIALOG (dialog),
1364  capabilities);
1365 
1366  gtk_print_unix_dialog_set_embed_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
1367  export->embed_page_setup);
1368 
1369  gtk_print_unix_dialog_set_current_page (GTK_PRINT_UNIX_DIALOG (dialog),
1370  export->current_page);
1371 
1372  gtk_print_unix_dialog_set_settings (GTK_PRINT_UNIX_DIALOG (dialog),
1373  export->print_settings);
1374 
1375  if (export->page_setup)
1376  gtk_print_unix_dialog_set_page_setup (GTK_PRINT_UNIX_DIALOG (dialog),
1377  export->page_setup);
1378 
1379  g_signal_connect (dialog, "response",
1380  G_CALLBACK (ev_print_operation_export_print_dialog_response_cb),
1381  export);
1382 
1383  gtk_window_present (GTK_WINDOW (dialog));
1384 }
1385 
1386 static void
1387 ev_print_operation_export_cancel (EvPrintOperation *op)
1388 {
1389  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1390 
1391  if (export->job_export &&
1392  !ev_job_is_finished (export->job_export)) {
1393  ev_job_cancel (export->job_export);
1394  } else {
1395  export_cancel (export);
1396  }
1397 }
1398 
1399 static void
1400 ev_print_operation_export_get_error (EvPrintOperation *op,
1401  GError **error)
1402 {
1403  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1404 
1405  g_propagate_error (error, export->error);
1406  export->error = NULL;
1407 }
1408 
1409 static void
1410 ev_print_operation_export_set_embed_page_setup (EvPrintOperation *op,
1411  gboolean embed)
1412 {
1413  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1414 
1415  export->embed_page_setup = embed;
1416 }
1417 
1418 static gboolean
1419 ev_print_operation_export_get_embed_page_setup (EvPrintOperation *op)
1420 {
1421  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
1422 
1423  return export->embed_page_setup;
1424 }
1425 
1426 static void
1427 ev_print_operation_export_finalize (GObject *object)
1428 {
1429  EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (object);
1430 
1431  if (export->idle_id > 0) {
1432  g_source_remove (export->idle_id);
1433  export->idle_id = 0;
1434  }
1435 
1436  if (export->fd != -1) {
1437  close (export->fd);
1438  export->fd = -1;
1439  }
1440 
1441  if (export->ranges) {
1442  if (export->ranges != &export->one_range)
1443  g_free (export->ranges);
1444  export->ranges = NULL;
1445  export->n_ranges = 0;
1446  }
1447 
1448  if (export->temp_file) {
1449  g_free (export->temp_file);
1450  export->temp_file = NULL;
1451  }
1452 
1453  if (export->job_name) {
1454  g_free (export->job_name);
1455  export->job_name = NULL;
1456  }
1457 
1458  if (export->job_export) {
1459  if (!ev_job_is_finished (export->job_export))
1460  ev_job_cancel (export->job_export);
1461  g_signal_handlers_disconnect_by_func (export->job_export,
1462  export_job_finished,
1463  export);
1464  g_signal_handlers_disconnect_by_func (export->job_export,
1465  export_job_cancelled,
1466  export);
1467  g_object_unref (export->job_export);
1468  export->job_export = NULL;
1469  }
1470 
1471  if (export->error) {
1472  g_error_free (export->error);
1473  export->error = NULL;
1474  }
1475 
1476  if (export->print_settings) {
1477  g_object_unref (export->print_settings);
1478  export->print_settings = NULL;
1479  }
1480 
1481  if (export->page_setup) {
1482  g_object_unref (export->page_setup);
1483  export->page_setup = NULL;
1484  }
1485 
1486  if (export->printer) {
1487  g_object_unref (export->printer);
1488  export->printer = NULL;
1489  }
1490 
1491  (* G_OBJECT_CLASS (ev_print_operation_export_parent_class)->finalize) (object);
1492 }
1493 
1494 static void
1495 ev_print_operation_export_init (EvPrintOperationExport *export)
1496 {
1497  /* sheets are counted from 1 to be physical */
1498  export->sheet = 1;
1499 }
1500 
1501 static GObject *
1502 ev_print_operation_export_constructor (GType type,
1503  guint n_construct_properties,
1504  GObjectConstructParam *construct_params)
1505 {
1506  GObject *object;
1507  EvPrintOperationExport *export;
1508  EvPrintOperation *op;
1509 
1510  object = G_OBJECT_CLASS (ev_print_operation_export_parent_class)->constructor (type,
1511  n_construct_properties,
1512  construct_params);
1513  export = EV_PRINT_OPERATION_EXPORT (object);
1514  op = EV_PRINT_OPERATION (object);
1515  export->n_pages = ev_document_get_n_pages (op->document);
1516 
1517  return object;
1518 }
1519 
1520 static void
1521 ev_print_operation_export_class_init (EvPrintOperationExportClass *klass)
1522 {
1523  GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
1524  EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass);
1525 
1526  ev_print_op_class->set_current_page = ev_print_operation_export_set_current_page;
1527  ev_print_op_class->set_print_settings = ev_print_operation_export_set_print_settings;
1528  ev_print_op_class->get_print_settings = ev_print_operation_export_get_print_settings;
1529  ev_print_op_class->set_default_page_setup = ev_print_operation_export_set_default_page_setup;
1530  ev_print_op_class->get_default_page_setup = ev_print_operation_export_get_default_page_setup;
1531  ev_print_op_class->set_job_name = ev_print_operation_export_set_job_name;
1532  ev_print_op_class->get_job_name = ev_print_operation_export_get_job_name;
1533  ev_print_op_class->run = ev_print_operation_export_run;
1534  ev_print_op_class->cancel = ev_print_operation_export_cancel;
1535  ev_print_op_class->get_error = ev_print_operation_export_get_error;
1536  ev_print_op_class->set_embed_page_setup = ev_print_operation_export_set_embed_page_setup;
1537  ev_print_op_class->get_embed_page_setup = ev_print_operation_export_get_embed_page_setup;
1538 
1539  g_object_class->constructor = ev_print_operation_export_constructor;
1540  g_object_class->finalize = ev_print_operation_export_finalize;
1541 }
1542 
1543 #endif /* GTKUNIXPRINT_ENABLED */
1544 
1545 /* Print to cairo interface */
1546 #define EV_TYPE_PRINT_OPERATION_PRINT (ev_print_operation_print_get_type())
1547 #define EV_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrint))
1548 #define EV_PRINT_OPERATION_PRINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_PRINT_OPERATION_PRINT, EvPrintOperationPrintClass))
1549 #define EV_IS_PRINT_OPERATION_PRINT(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_PRINT_OPERATION_PRINT))
1550 
1553 
1554 static GType ev_print_operation_print_get_type (void) G_GNUC_CONST;
1555 
1556 typedef enum {
1557  EV_SCALE_NONE,
1558  EV_SCALE_SHRINK_TO_PRINTABLE_AREA,
1559  EV_SCALE_FIT_TO_PRINTABLE_AREA
1561 
1562 #define EV_PRINT_SETTING_PAGE_SCALE "evince-print-setting-page-scale"
1563 #define EV_PRINT_SETTING_AUTOROTATE "evince-print-setting-page-autorotate"
1564 #define EV_PRINT_SETTING_PAGE_SIZE "evince-print-setting-page-size"
1565 
1568 
1569  GtkPrintOperation *op;
1571  gint total;
1573  gchar *job_name;
1574 
1575  /* Page handling tab */
1576  GtkWidget *scale_combo;
1578  GtkWidget *autorotate_button;
1579  gboolean autorotate;
1580  GtkWidget *source_button;
1582 };
1583 
1586 };
1587 
1588 G_DEFINE_TYPE (EvPrintOperationPrint, ev_print_operation_print, EV_TYPE_PRINT_OPERATION)
1589 
1590 static void
1592  gint current_page)
1593 {
1595 
1596  gtk_print_operation_set_current_page (print->op, current_page);
1597 }
1598 
1599 static void
1601  GtkPrintSettings *print_settings)
1602 {
1604 
1605  gtk_print_operation_set_print_settings (print->op, print_settings);
1606 }
1607 
1608 static GtkPrintSettings *
1610 {
1612 
1613  return gtk_print_operation_get_print_settings (print->op);
1614 }
1615 
1616 static void
1618  GtkPageSetup *page_setup)
1619 {
1621 
1622  gtk_print_operation_set_default_page_setup (print->op, page_setup);
1623 }
1624 
1625 static GtkPageSetup *
1627 {
1629 
1630  return gtk_print_operation_get_default_page_setup (print->op);
1631 }
1632 
1633 static void
1635  const gchar *job_name)
1636 {
1638 
1639  g_free (print->job_name);
1640  print->job_name = g_strdup (job_name);
1641 
1642  gtk_print_operation_set_job_name (print->op, print->job_name);
1643 }
1644 
1645 static const gchar *
1647 {
1649 
1650  if (!print->job_name) {
1651  gchar *name;
1652 
1653  g_object_get (print->op, "job_name", &name, NULL);
1654  print->job_name = name;
1655  }
1656 
1657  return print->job_name;
1658 }
1659 
1660 static void
1662  GtkWindow *parent)
1663 {
1665 
1666  gtk_print_operation_run (print->op,
1667  GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1668  parent, NULL);
1669 }
1670 
1671 static void
1673 {
1675 
1676  if (print->job_print)
1677  ev_job_cancel (print->job_print);
1678  else
1679  gtk_print_operation_cancel (print->op);
1680 }
1681 
1682 static void
1684  GError **error)
1685 {
1687 
1688  gtk_print_operation_get_error (print->op, error);
1689 }
1690 
1691 static void
1693  gboolean embed)
1694 {
1696 
1697  gtk_print_operation_set_embed_page_setup (print->op, embed);
1698 }
1699 
1700 static gboolean
1702 {
1704 
1705  return gtk_print_operation_get_embed_page_setup (print->op);
1706 }
1707 
1708 static void
1710  GtkPrintContext *context)
1711 {
1712  EvPrintOperation *op = EV_PRINT_OPERATION (print);
1713  gint n_pages;
1714 
1715  n_pages = ev_document_get_n_pages (op->document);
1716  gtk_print_operation_set_n_pages (print->op, n_pages);
1717  ev_print_operation_update_status (op, -1, n_pages, 0);
1718 
1719  g_signal_emit (op, signals[BEGIN_PRINT], 0);
1720 }
1721 
1722 static void
1724  GtkPrintOperationResult result)
1725 {
1726  EvPrintOperation *op = EV_PRINT_OPERATION (print);
1727 
1729 
1730  g_signal_emit (op, signals[DONE], 0, result);
1731 }
1732 
1733 static void
1735 {
1736  GtkPrintStatus status;
1737 
1738  status = gtk_print_operation_get_status (print->op);
1739  if (status == GTK_PRINT_STATUS_GENERATING_DATA)
1740  print->n_pages_to_print = gtk_print_operation_get_n_pages_to_print (print->op);
1741 }
1742 
1743 static void
1745  EvPrintOperationPrint *print)
1746 {
1747  EvPrintOperation *op = EV_PRINT_OPERATION (print);
1748 
1749  gtk_print_operation_draw_page_finish (print->op);
1750 
1751  print->total++;
1753  print->n_pages_to_print,
1754  print->total / (gdouble)print->n_pages_to_print);
1755  ev_job_print_set_cairo (job, NULL);
1756 }
1757 
1758 static gboolean
1760 {
1762  return TRUE;
1763 
1764  gtk_print_operation_draw_page_finish (print->op);
1765 
1766  return FALSE;
1767 }
1768 
1769 static void
1771  EvPrintOperationPrint *print)
1772 {
1773  /* Finish the current page, so that draw-page
1774  * is emitted again and it will cancel the
1775  * print operation. If the job is still
1776  * running, wait until it finishes.
1777  */
1779  g_idle_add ((GSourceFunc)draw_page_finish_idle, print);
1780  else
1781  gtk_print_operation_draw_page_finish (print->op);
1782 }
1783 
1784 static void
1786  GtkPrintContext *context,
1787  gint page_nr,
1788  GtkPageSetup *setup)
1789 {
1790  EvPrintOperation *op = EV_PRINT_OPERATION (print);
1791  gdouble width, height;
1792  GtkPaperSize *paper_size;
1793 
1794  ev_document_get_page_size (op->document, page_nr,
1795  &width, &height);
1796 
1797  if (print->use_source_size) {
1798  paper_size = gtk_paper_size_new_custom ("custom", "custom",
1799  width, height, GTK_UNIT_POINTS);
1800  gtk_page_setup_set_paper_size_and_default_margins (setup, paper_size);
1801  gtk_paper_size_free (paper_size);
1802  }
1803 
1804  if (print->autorotate) {
1805  if (width > height)
1806  gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
1807  else
1808  gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_PORTRAIT);
1809  }
1810 }
1811 
1812 static void
1813 _print_context_get_hard_margins (GtkPrintContext *context,
1814  gdouble *top,
1815  gdouble *bottom,
1816  gdouble *left,
1817  gdouble *right)
1818 {
1819  if (!gtk_print_context_get_hard_margins (context, top, bottom, left, right)) {
1820  *top = 0;
1821  *bottom = 0;
1822  *left = 0;
1823  *right = 0;
1824  }
1825 }
1826 
1827 static void
1829  gint page,
1830  gdouble *width,
1831  gdouble *height,
1832  gdouble *manual_scale)
1833 {
1834  GtkPrintSettings *settings;
1835 
1836  ev_document_get_page_size (EV_PRINT_OPERATION (print)->document,
1837  page, width, height);
1838 
1839  settings = gtk_print_operation_get_print_settings (print->op);
1840  *manual_scale = gtk_print_settings_get_scale (settings) / 100.0;
1841  if (*manual_scale == 1.0)
1842  return;
1843 
1844  *width *= *manual_scale;
1845  *height *= *manual_scale;
1846 }
1847 
1848 static void
1850  GtkPrintContext *context,
1851  gint page)
1852 {
1853  EvPrintOperation *op = EV_PRINT_OPERATION (print);
1854  cairo_t *cr;
1855  gdouble cr_width, cr_height;
1856  gdouble width, height, manual_scale, scale;
1857  gdouble x_scale, y_scale;
1858  gdouble x_offset, y_offset;
1859  gdouble top, bottom, left, right;
1860 
1861  gtk_print_operation_set_defer_drawing (print->op);
1862 
1863  if (!print->job_print) {
1864  print->job_print = ev_job_print_new (op->document);
1865  g_signal_connect (G_OBJECT (print->job_print), "finished",
1866  G_CALLBACK (print_job_finished),
1867  (gpointer)print);
1868  g_signal_connect (G_OBJECT (print->job_print), "cancelled",
1869  G_CALLBACK (print_job_cancelled),
1870  (gpointer)print);
1871  } else if (g_cancellable_is_cancelled (print->job_print->cancellable)) {
1872  gtk_print_operation_cancel (print->op);
1874  return;
1875  }
1876 
1877  ev_job_print_set_page (EV_JOB_PRINT (print->job_print), page);
1878 
1879  cr = gtk_print_context_get_cairo_context (context);
1880  cr_width = gtk_print_context_get_width (context);
1881  cr_height = gtk_print_context_get_height (context);
1882  ev_print_operation_print_get_scaled_page_size (print, page, &width, &height, &manual_scale);
1883 
1884  if (print->page_scale == EV_SCALE_NONE) {
1885  /* Center document page on the printed page */
1886  if (print->autorotate) {
1887  x_offset = (cr_width - width) / (2 * manual_scale);
1888  y_offset = (cr_height - height) / (2 * manual_scale);
1889  cairo_translate (cr, x_offset, y_offset);
1890  }
1891  } else {
1892  _print_context_get_hard_margins (context, &top, &bottom, &left, &right);
1893 
1894  x_scale = (cr_width - left - right) / width;
1895  y_scale = (cr_height - top - bottom) / height;
1896  scale = MIN (x_scale, y_scale);
1897 
1898  /* Ignore scale > 1 when shrinking to printable area */
1899  if (scale > 1.0 && print->page_scale == EV_SCALE_SHRINK_TO_PRINTABLE_AREA)
1900  scale = 1.0;
1901 
1902  if (print->autorotate) {
1903  x_offset = (cr_width - scale * width) / (2 * manual_scale);
1904  y_offset = (cr_height - scale * height) / (2 * manual_scale);
1905  cairo_translate (cr, x_offset, y_offset);
1906 
1907  /* Ensure document page is within the margins. The
1908  * scale guarantees the document will fit in the
1909  * margins so we just need to check each side and
1910  * if it overhangs the margin, translate it to the
1911  * margin. */
1912  if (x_offset < left)
1913  cairo_translate (cr, left - x_offset, 0);
1914 
1915  if (x_offset < right)
1916  cairo_translate (cr, -(right - x_offset), 0);
1917 
1918  if (y_offset < top)
1919  cairo_translate (cr, 0, top - y_offset);
1920 
1921  if (y_offset < bottom)
1922  cairo_translate (cr, 0, -(bottom - y_offset));
1923  } else {
1924  cairo_translate (cr, left, top);
1925  }
1926 
1927  if (print->page_scale == EV_SCALE_FIT_TO_PRINTABLE_AREA || scale < 1.0) {
1928  cairo_scale (cr, scale, scale);
1929  }
1930  }
1931 
1934 }
1935 
1936 static GObject *
1938  GtkPrintContext *context)
1939 {
1940  GtkPrintSettings *settings;
1941  GtkWidget *label;
1942  GtkWidget *grid;
1943  EvPrintScale page_scale;
1944  gboolean autorotate;
1945  gboolean use_source_size;
1946 
1947  settings = gtk_print_operation_get_print_settings (print->op);
1948  page_scale = gtk_print_settings_get_int_with_default (settings, EV_PRINT_SETTING_PAGE_SCALE, 1);
1949  autorotate = gtk_print_settings_has_key (settings, EV_PRINT_SETTING_AUTOROTATE) ?
1950  gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_AUTOROTATE) :
1951  TRUE;
1952  use_source_size = gtk_print_settings_get_bool (settings, EV_PRINT_SETTING_PAGE_SIZE);
1953 
1954  grid = gtk_grid_new ();
1955  gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
1956  gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
1957  gtk_container_set_border_width (GTK_CONTAINER (grid), 12);
1958 
1959  label = gtk_label_new (_("Page Scaling:"));
1960  gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
1961  gtk_widget_show (label);
1962 
1963  print->scale_combo = gtk_combo_box_text_new ();
1964  /* translators: Value for 'Page Scaling:' to not scale the document pages on printing */
1965  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (print->scale_combo), _("None"));
1966  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (print->scale_combo), _("Shrink to Printable Area"));
1967  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (print->scale_combo), _("Fit to Printable Area"));
1968  gtk_combo_box_set_active (GTK_COMBO_BOX (print->scale_combo), page_scale);
1969  gtk_widget_set_tooltip_text (print->scale_combo,
1970  _("Scale document pages to fit the selected printer page. Select from one of the following:\n"
1971  "\n"
1972  "• “None”: No page scaling is performed.\n"
1973  "\n"
1974  "• “Shrink to Printable Area”: Document pages larger than the printable area"
1975  " are reduced to fit the printable area of the printer page.\n"
1976  "\n"
1977  "• “Fit to Printable Area”: Document pages are enlarged or reduced as"
1978  " required to fit the printable area of the printer page.\n"));
1979  gtk_grid_attach (GTK_GRID (grid), print->scale_combo, 1, 0, 1, 1);
1980  gtk_widget_show (print->scale_combo);
1981 
1982  print->autorotate_button = gtk_check_button_new_with_label (_("Auto Rotate and Center"));
1983  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->autorotate_button), autorotate);
1984  gtk_widget_set_tooltip_text (print->autorotate_button,
1985  _("Rotate printer page orientation of each page to match orientation of each document page. "
1986  "Document pages will be centered within the printer page."));
1987  gtk_grid_attach (GTK_GRID (grid), print->autorotate_button, 0, 1, 2, 1);
1988  gtk_widget_show (print->autorotate_button);
1989 
1990  print->source_button = gtk_check_button_new_with_label (_("Select page size using document page size"));
1991  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (print->source_button), use_source_size);
1992  gtk_widget_set_tooltip_text (print->source_button, _("When enabled, each page will be printed on "
1993  "the same size paper as the document page."));
1994  gtk_grid_attach (GTK_GRID (grid), print->source_button, 0, 2, 2, 1);
1995  gtk_widget_show (print->source_button);
1996 
1997  return G_OBJECT (grid);
1998 }
1999 
2000 static void
2002  GtkPrintContext *context)
2003 {
2004  GtkPrintSettings *settings;
2005 
2006  print->page_scale = gtk_combo_box_get_active (GTK_COMBO_BOX (print->scale_combo));
2007  print->autorotate = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->autorotate_button));
2008  print->use_source_size = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (print->source_button));
2009  settings = gtk_print_operation_get_print_settings (print->op);
2010  gtk_print_settings_set_int (settings, EV_PRINT_SETTING_PAGE_SCALE, print->page_scale);
2011  gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_AUTOROTATE, print->autorotate);
2012  gtk_print_settings_set_bool (settings, EV_PRINT_SETTING_PAGE_SIZE, print->use_source_size);
2013 }
2014 
2015 static gboolean
2017 {
2018  EV_PRINT_OPERATION (print)->print_preview = TRUE;
2019 
2020  return FALSE;
2021 }
2022 
2023 static void
2025 {
2027  GApplication *application;
2028 
2029  if (print->op) {
2030  g_object_unref (print->op);
2031  print->op = NULL;
2032  }
2033 
2034  if (print->job_name) {
2035  g_free (print->job_name);
2036  print->job_name = NULL;
2037  }
2038 
2039  if (print->job_print) {
2040  if (!ev_job_is_finished (print->job_print))
2041  ev_job_cancel (print->job_print);
2042  g_signal_handlers_disconnect_by_func (print->job_print,
2044  print);
2045  g_signal_handlers_disconnect_by_func (print->job_print,
2047  print);
2048  g_object_unref (print->job_print);
2049  print->job_print = NULL;
2050  }
2051 
2052  (* G_OBJECT_CLASS (ev_print_operation_print_parent_class)->finalize) (object);
2053 
2054  application = g_application_get_default ();
2055  if (application)
2056  g_application_release (application);
2057 }
2058 
2059 static void
2061 {
2062  GApplication *application;
2063 
2064  print->op = gtk_print_operation_new ();
2065  g_signal_connect_swapped (print->op, "begin_print",
2067  print);
2068  g_signal_connect_swapped (print->op, "done",
2069  G_CALLBACK (ev_print_operation_print_done),
2070  print);
2071  g_signal_connect_swapped (print->op, "draw_page",
2073  print);
2074  g_signal_connect_swapped (print->op, "status_changed",
2076  print);
2077  g_signal_connect_swapped (print->op, "request_page_setup",
2079  print);
2080  g_signal_connect_swapped (print->op, "create_custom_widget",
2082  print);
2083  g_signal_connect_swapped (print->op, "custom_widget_apply",
2085  print);
2086  g_signal_connect_swapped (print->op, "preview",
2087  G_CALLBACK (ev_print_operation_print_preview),
2088  print);
2089  gtk_print_operation_set_allow_async (print->op, TRUE);
2090  gtk_print_operation_set_use_full_page (print->op, TRUE);
2091  gtk_print_operation_set_unit (print->op, GTK_UNIT_POINTS);
2092  gtk_print_operation_set_custom_tab_label (print->op, _("Page Handling"));
2093 
2094  application = g_application_get_default ();
2095  if (application)
2096  g_application_hold (application);
2097 }
2098 
2099 static void
2101 {
2102  GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
2103  EvPrintOperationClass *ev_print_op_class = EV_PRINT_OPERATION_CLASS (klass);
2104 
2112  ev_print_op_class->run = ev_print_operation_print_run;
2113  ev_print_op_class->cancel = ev_print_operation_print_cancel;
2114  ev_print_op_class->get_error = ev_print_operation_print_get_error;
2117 
2118  g_object_class->finalize = ev_print_operation_print_finalize;
2119 }
2120 
2121 gboolean
2123 {
2124 #if GTKUNIXPRINT_ENABLED
2125  return (EV_IS_FILE_EXPORTER(document) || EV_IS_DOCUMENT_PRINT(document));
2126 #else
2127  return EV_IS_DOCUMENT_PRINT(document);
2128 #endif /* GTKUNIXPRINT_ENABLED */
2129 }
2130 
2131 /* Factory method */
2134 {
2135  EvPrintOperation *op = NULL;
2136 
2137  g_return_val_if_fail (ev_print_operation_exists_for_document (document), NULL);
2138 
2139  if (EV_IS_DOCUMENT_PRINT (document))
2141  "document", document, NULL));
2142  else
2143 #if GTKUNIXPRINT_ENABLED
2144  op = EV_PRINT_OPERATION (g_object_new (EV_TYPE_PRINT_OPERATION_EXPORT,
2145  "document", document, NULL));
2146 #else
2147  op = NULL;
2148 #endif
2149  return op;
2150 }