2 * Copyright (C) 2006 John (J5) Palmieri <johnp@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 * @Short_description: Represents a print job
25 * A #GtkPrintJob object represents a job that is sent to a
26 * printer. You only need to deal directly with print jobs if
27 * you use the non-portable #GtkPrintUnixDialog API.
29 * Use gtk_print_job_get_surface() to obtain the cairo surface
30 * onto which the pages must be drawn. Use gtk_print_job_send()
31 * to send the finished job to the printer. If you don't use cairo
32 * #GtkPrintJob also supports printing of manually generated postscript,
33 * via gtk_print_job_set_source_file().
44 #include <sys/types.h>
47 #include <glib/gstdio.h>
49 #include "gtkprivate.h"
51 #include "gtkprintjob.h"
52 #include "gtkprinter.h"
53 #include "gtkprinter-private.h"
54 #include "gtkprintbackend.h"
60 struct _GtkPrintJobPrivate
65 cairo_surface_t *surface;
67 GtkPrintStatus status;
68 GtkPrintBackend *backend;
70 GtkPrintSettings *settings;
71 GtkPageSetup *page_setup;
73 GtkPrintPages print_pages;
74 GtkPageRange *page_ranges;
80 GtkNumberUpLayout number_up_layout;
82 guint printer_set : 1;
83 guint page_setup_set : 1;
84 guint settings_set : 1;
85 guint track_print_status : 1;
86 guint rotate_to_orientation : 1;
91 #define GTK_PRINT_JOB_GET_PRIVATE(o) \
92 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_PRINT_JOB, GtkPrintJobPrivate))
94 static void gtk_print_job_finalize (GObject *object);
95 static void gtk_print_job_set_property (GObject *object,
99 static void gtk_print_job_get_property (GObject *object,
103 static GObject* gtk_print_job_constructor (GType type,
104 guint n_construct_properties,
105 GObjectConstructParam *construct_params);
118 PROP_TRACK_PRINT_STATUS
121 static guint signals[LAST_SIGNAL] = { 0 };
123 G_DEFINE_TYPE (GtkPrintJob, gtk_print_job, G_TYPE_OBJECT)
126 gtk_print_job_class_init (GtkPrintJobClass *class)
128 GObjectClass *object_class;
129 object_class = (GObjectClass *) class;
131 object_class->finalize = gtk_print_job_finalize;
132 object_class->constructor = gtk_print_job_constructor;
133 object_class->set_property = gtk_print_job_set_property;
134 object_class->get_property = gtk_print_job_get_property;
136 g_type_class_add_private (class, sizeof (GtkPrintJobPrivate));
138 g_object_class_install_property (object_class,
140 g_param_spec_string ("title",
142 P_("Title of the print job"),
144 GTK_PARAM_READWRITE |
145 G_PARAM_CONSTRUCT_ONLY));
147 g_object_class_install_property (object_class,
149 g_param_spec_object ("printer",
151 P_("Printer to print the job to"),
153 GTK_PARAM_READWRITE |
154 G_PARAM_CONSTRUCT_ONLY));
156 g_object_class_install_property (object_class,
158 g_param_spec_object ("settings",
160 P_("Printer settings"),
161 GTK_TYPE_PRINT_SETTINGS,
162 GTK_PARAM_READWRITE |
163 G_PARAM_CONSTRUCT_ONLY));
165 g_object_class_install_property (object_class,
167 g_param_spec_object ("page-setup",
171 GTK_PARAM_READWRITE |
172 G_PARAM_CONSTRUCT_ONLY));
174 g_object_class_install_property (object_class,
175 PROP_TRACK_PRINT_STATUS,
176 g_param_spec_boolean ("track-print-status",
177 P_("Track Print Status"),
178 P_("TRUE if the print job will continue to emit "
179 "status-changed signals after the print data "
180 "has been sent to the printer or print server."),
182 GTK_PARAM_READWRITE));
186 * GtkPrintJob::status-changed:
187 * @job: the #GtkPrintJob object on which the signal was emitted
189 * Gets emitted when the status of a job changes. The signal handler
190 * can use gtk_print_job_get_status() to obtain the new status.
194 signals[STATUS_CHANGED] =
195 g_signal_new (I_("status-changed"),
196 G_TYPE_FROM_CLASS (class),
198 G_STRUCT_OFFSET (GtkPrintJobClass, status_changed),
200 g_cclosure_marshal_VOID__VOID,
205 gtk_print_job_init (GtkPrintJob *job)
207 GtkPrintJobPrivate *priv;
209 priv = job->priv = GTK_PRINT_JOB_GET_PRIVATE (job);
211 priv->spool_io = NULL;
213 priv->title = g_strdup ("");
214 priv->surface = NULL;
215 priv->backend = NULL;
216 priv->printer = NULL;
218 priv->printer_set = FALSE;
219 priv->settings_set = FALSE;
220 priv->page_setup_set = FALSE;
221 priv->status = GTK_PRINT_STATUS_INITIAL;
222 priv->track_print_status = FALSE;
224 priv->print_pages = GTK_PRINT_PAGES_ALL;
225 priv->page_ranges = NULL;
226 priv->num_page_ranges = 0;
227 priv->collate = FALSE;
228 priv->reverse = FALSE;
229 priv->num_copies = 1;
231 priv->page_set = GTK_PAGE_SET_ALL;
232 priv->rotate_to_orientation = FALSE;
234 priv->number_up_layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
239 gtk_print_job_constructor (GType type,
240 guint n_construct_properties,
241 GObjectConstructParam *construct_params)
244 GtkPrintJobPrivate *priv;
248 G_OBJECT_CLASS (gtk_print_job_parent_class)->constructor (type,
249 n_construct_properties,
252 job = GTK_PRINT_JOB (object);
255 g_assert (priv->printer_set &&
256 priv->settings_set &&
257 priv->page_setup_set);
259 _gtk_printer_prepare_for_print (priv->printer,
269 gtk_print_job_finalize (GObject *object)
271 GtkPrintJob *job = GTK_PRINT_JOB (object);
272 GtkPrintJobPrivate *priv = job->priv;
274 if (priv->spool_io != NULL)
276 g_io_channel_unref (priv->spool_io);
277 priv->spool_io = NULL;
281 g_object_unref (priv->backend);
284 g_object_unref (priv->printer);
287 cairo_surface_destroy (priv->surface);
290 g_object_unref (priv->settings);
292 if (priv->page_setup)
293 g_object_unref (priv->page_setup);
295 g_free (priv->page_ranges);
296 priv->page_ranges = NULL;
298 g_free (priv->title);
301 G_OBJECT_CLASS (gtk_print_job_parent_class)->finalize (object);
306 * @title: the job title
307 * @printer: a #GtkPrinter
308 * @settings: a #GtkPrintSettings
309 * @page_setup: a #GtkPageSetup
311 * Creates a new #GtkPrintJob.
313 * Return value: a new #GtkPrintJob
318 gtk_print_job_new (const gchar *title,
320 GtkPrintSettings *settings,
321 GtkPageSetup *page_setup)
324 result = g_object_new (GTK_TYPE_PRINT_JOB,
327 "settings", settings,
328 "page-setup", page_setup,
330 return (GtkPrintJob *) result;
334 * gtk_print_job_get_settings:
335 * @job: a #GtkPrintJob
337 * Gets the #GtkPrintSettings of the print job.
339 * Return value: the settings of @job
344 gtk_print_job_get_settings (GtkPrintJob *job)
346 g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
348 return job->priv->settings;
352 * gtk_print_job_get_printer:
353 * @job: a #GtkPrintJob
355 * Gets the #GtkPrinter of the print job.
357 * Return value: the printer of @job
362 gtk_print_job_get_printer (GtkPrintJob *job)
364 g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
366 return job->priv->printer;
370 * gtk_print_job_get_title:
371 * @job: a #GtkPrintJob
373 * Gets the job title.
375 * Return value: the title of @job
379 G_CONST_RETURN gchar *
380 gtk_print_job_get_title (GtkPrintJob *job)
382 g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
384 return job->priv->title;
388 * gtk_print_job_get_status:
389 * @job: a #GtkPrintJob
391 * Gets the status of the print job.
393 * Return value: the status of @job
398 gtk_print_job_get_status (GtkPrintJob *job)
400 g_return_val_if_fail (GTK_IS_PRINT_JOB (job), GTK_PRINT_STATUS_FINISHED);
402 return job->priv->status;
406 gtk_print_job_set_status (GtkPrintJob *job,
407 GtkPrintStatus status)
409 GtkPrintJobPrivate *priv;
411 g_return_if_fail (GTK_IS_PRINT_JOB (job));
415 if (priv->status == status)
418 priv->status = status;
419 g_signal_emit (job, signals[STATUS_CHANGED], 0);
423 * gtk_print_job_set_source_file:
424 * @job: a #GtkPrintJob
425 * @filename: (type filename): the file to be printed
426 * @error: return location for errors
428 * Make the #GtkPrintJob send an existing document to the
429 * printing system. The file can be in any format understood
430 * by the platforms printing system (typically PostScript,
431 * but on many platforms PDF may work too). See
432 * gtk_printer_accepts_pdf() and gtk_printer_accepts_ps().
434 * Returns: %FALSE if an error occurred
439 gtk_print_job_set_source_file (GtkPrintJob *job,
440 const gchar *filename,
443 GtkPrintJobPrivate *priv;
448 g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
452 priv->spool_io = g_io_channel_new_file (filename, "r", &tmp_error);
454 if (tmp_error == NULL)
455 g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error);
457 if (tmp_error != NULL)
459 g_propagate_error (error, tmp_error);
467 * gtk_print_job_get_surface:
468 * @job: a #GtkPrintJob
469 * @error: (allow-none): return location for errors, or %NULL
471 * Gets a cairo surface onto which the pages of
472 * the print job should be rendered.
474 * Return value: the cairo surface of @job
479 gtk_print_job_get_surface (GtkPrintJob *job,
482 GtkPrintJobPrivate *priv;
484 gdouble width, height;
485 GtkPaperSize *paper_size;
491 g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
496 return priv->surface;
498 g_return_val_if_fail (priv->spool_io == NULL, NULL);
500 fd = g_file_open_tmp ("gtkprint_XXXXXX",
506 g_propagate_error (error, tmp_error);
510 fchmod (fd, S_IRUSR | S_IWUSR);
512 #ifdef G_ENABLE_DEBUG
513 /* If we are debugging printing don't delete the tmp files */
514 if (!(gtk_get_debug_flags () & GTK_DEBUG_PRINTING))
515 #endif /* G_ENABLE_DEBUG */
519 paper_size = gtk_page_setup_get_paper_size (priv->page_setup);
520 width = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
521 height = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
523 priv->spool_io = g_io_channel_unix_new (fd);
524 g_io_channel_set_close_on_unref (priv->spool_io, TRUE);
525 g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error);
527 if (tmp_error != NULL)
529 g_io_channel_unref (priv->spool_io);
530 priv->spool_io = NULL;
531 g_propagate_error (error, tmp_error);
535 priv->surface = _gtk_printer_create_cairo_surface (priv->printer,
540 return priv->surface;
544 * gtk_print_job_set_track_print_status:
545 * @job: a #GtkPrintJob
546 * @track_status: %TRUE to track status after printing
548 * If track_status is %TRUE, the print job will try to continue report
549 * on the status of the print job in the printer queues and printer. This
550 * can allow your application to show things like "out of paper" issues,
551 * and when the print job actually reaches the printer.
553 * This function is often implemented using some form of polling, so it should
554 * not be enabled unless needed.
559 gtk_print_job_set_track_print_status (GtkPrintJob *job,
560 gboolean track_status)
562 GtkPrintJobPrivate *priv;
564 g_return_if_fail (GTK_IS_PRINT_JOB (job));
568 track_status = track_status != FALSE;
570 if (priv->track_print_status != track_status)
572 priv->track_print_status = track_status;
574 g_object_notify (G_OBJECT (job), "track-print-status");
579 * gtk_print_job_get_track_print_status:
580 * @job: a #GtkPrintJob
582 * Returns wheter jobs will be tracked after printing.
583 * For details, see gtk_print_job_set_track_print_status().
585 * Return value: %TRUE if print job status will be reported after printing
590 gtk_print_job_get_track_print_status (GtkPrintJob *job)
592 GtkPrintJobPrivate *priv;
594 g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
598 return priv->track_print_status;
602 gtk_print_job_set_property (GObject *object,
608 GtkPrintJob *job = GTK_PRINT_JOB (object);
609 GtkPrintJobPrivate *priv = job->priv;
610 GtkPrintSettings *settings;
615 g_free (priv->title);
616 priv->title = g_value_dup_string (value);
620 priv->printer = GTK_PRINTER (g_value_dup_object (value));
621 priv->printer_set = TRUE;
622 priv->backend = g_object_ref (gtk_printer_get_backend (priv->printer));
625 case PROP_PAGE_SETUP:
626 priv->page_setup = GTK_PAGE_SETUP (g_value_dup_object (value));
627 priv->page_setup_set = TRUE;
631 /* We save a copy of the settings since we modify
632 * if when preparing the printer job. */
633 settings = GTK_PRINT_SETTINGS (g_value_get_object (value));
634 priv->settings = gtk_print_settings_copy (settings);
635 priv->settings_set = TRUE;
638 case PROP_TRACK_PRINT_STATUS:
639 gtk_print_job_set_track_print_status (job, g_value_get_boolean (value));
643 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649 gtk_print_job_get_property (GObject *object,
654 GtkPrintJob *job = GTK_PRINT_JOB (object);
655 GtkPrintJobPrivate *priv = job->priv;
660 g_value_set_string (value, priv->title);
663 g_value_set_object (value, priv->printer);
666 g_value_set_object (value, priv->settings);
668 case PROP_PAGE_SETUP:
669 g_value_set_object (value, priv->page_setup);
671 case PROP_TRACK_PRINT_STATUS:
672 g_value_set_boolean (value, priv->track_print_status);
675 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
681 * gtk_print_job_send:
682 * @job: a GtkPrintJob
683 * @callback: function to call when the job completes or an error occurs
684 * @user_data: user data that gets passed to @callback
685 * @dnotify: destroy notify for @user_data
687 * Sends the print job off to the printer.
692 gtk_print_job_send (GtkPrintJob *job,
693 GtkPrintJobCompleteFunc callback,
695 GDestroyNotify dnotify)
697 GtkPrintJobPrivate *priv;
699 g_return_if_fail (GTK_IS_PRINT_JOB (job));
702 g_return_if_fail (priv->spool_io != NULL);
704 gtk_print_job_set_status (job, GTK_PRINT_STATUS_SENDING_DATA);
706 g_io_channel_seek_position (priv->spool_io, 0, G_SEEK_SET, NULL);
708 gtk_print_backend_print_stream (priv->backend, job,
710 callback, user_data, dnotify);
714 * gtk_print_job_get_pages:
715 * @job: a #GtkPrintJob
717 * Gets the #GtkPrintPages setting for this job.
719 * Returns: the #GtkPrintPages setting
724 gtk_print_job_get_pages (GtkPrintJob *job)
726 return job->priv->print_pages;
730 * gtk_print_job_set_pages:
731 * @job: a #GtkPrintJob
732 * @pages: the #GtkPrintPages setting
734 * Sets the #GtkPrintPages setting for this job.
739 gtk_print_job_set_pages (GtkPrintJob *job,
742 job->priv->print_pages = pages;
746 * gtk_print_job_get_page_ranges:
747 * @job: a #GtkPrintJob
748 * @n_ranges: (out): return location for the number of ranges
750 * Gets the page ranges for this job.
752 * Returns: a pointer to an array of #GtkPageRange structs
757 gtk_print_job_get_page_ranges (GtkPrintJob *job,
760 *n_ranges = job->priv->num_page_ranges;
761 return job->priv->page_ranges;
765 * gtk_print_job_set_page_ranges:
766 * @job: a #GtkPrintJob
767 * @ranges: pointer to an array of #GtkPageRange structs
768 * @n_ranges: the length of the @ranges array
770 * Sets the page ranges for this job.
775 gtk_print_job_set_page_ranges (GtkPrintJob *job,
776 GtkPageRange *ranges,
779 job->priv->page_ranges = ranges;
780 job->priv->num_page_ranges = n_ranges;
784 * gtk_print_job_get_page_set:
785 * @job: a #GtkPrintJob
787 * Gets the #GtkPageSet setting for this job.
789 * Returns: the #GtkPageSet setting
794 gtk_print_job_get_page_set (GtkPrintJob *job)
796 return job->priv->page_set;
800 * gtk_print_job_set_page_set:
801 * @job: a #GtkPrintJob
802 * @page_set: a #GtkPageSet setting
804 * Sets the #GtkPageSet setting for this job.
809 gtk_print_job_set_page_set (GtkPrintJob *job,
812 job->priv->page_set = page_set;
816 * gtk_print_job_get_num_copies:
817 * @job: a #GtkPrintJob
819 * Gets the number of copies of this job.
821 * Returns: the number of copies
826 gtk_print_job_get_num_copies (GtkPrintJob *job)
828 return job->priv->num_copies;
832 * gtk_print_job_set_num_copies:
833 * @job: a #GtkPrintJob
834 * @num_copies: the number of copies
836 * Sets the number of copies for this job.
841 gtk_print_job_set_num_copies (GtkPrintJob *job,
844 job->priv->num_copies = num_copies;
848 * gtk_print_job_get_scale:
849 * @job: a #GtkPrintJob
851 * Gets the scale for this job (where 1.0 means unscaled).
858 gtk_print_job_get_scale (GtkPrintJob *job)
861 return job->priv->scale;
865 * gtk_print_job_set_scale:
866 * @job: a #GtkPrintJob
869 * Sets the scale for this job (where 1.0 means unscaled).
874 gtk_print_job_set_scale (GtkPrintJob *job,
877 job->priv->scale = scale;
881 * gtk_print_job_get_n_up:
882 * @job: a #GtkPrintJob
884 * Gets the n-up setting for this job.
886 * Returns: the n-up setting
891 gtk_print_job_get_n_up (GtkPrintJob *job)
893 return job->priv->number_up;
897 * gtk_print_job_set_n_up:
898 * @job: a #GtkPrintJob
899 * @n_up: the n-up value
901 * Sets the n-up setting for this job.
906 gtk_print_job_set_n_up (GtkPrintJob *job,
909 job->priv->number_up = n_up;
913 * gtk_print_job_get_n_up_layout:
914 * @job: a #GtkPrintJob
916 * Gets the n-up layout setting for this job.
918 * Returns: the n-up layout
923 gtk_print_job_get_n_up_layout (GtkPrintJob *job)
925 return job->priv->number_up_layout;
929 * gtk_print_job_set_n_up_layout:
930 * @job: a #GtkPrintJob
931 * @layout: the n-up layout setting
933 * Sets the n-up layout setting for this job.
938 gtk_print_job_set_n_up_layout (GtkPrintJob *job,
939 GtkNumberUpLayout layout)
941 job->priv->number_up_layout = layout;
945 * gtk_print_job_get_rotate:
946 * @job: a #GtkPrintJob
948 * Gets whether the job is printed rotated.
950 * Returns: whether the job is printed rotated
955 gtk_print_job_get_rotate (GtkPrintJob *job)
957 return job->priv->rotate_to_orientation;
961 * gtk_print_job_set_rotate:
962 * @job: a #GtkPrintJob
963 * @rotate: whether to print rotated
965 * Sets whether this job is printed rotated.
970 gtk_print_job_set_rotate (GtkPrintJob *job,
973 job->priv->rotate_to_orientation = rotate;
977 * gtk_print_job_get_collate:
978 * @job: a #GtkPrintJob
980 * Gets whether this job is printed collated.
982 * Returns: whether the job is printed collated
987 gtk_print_job_get_collate (GtkPrintJob *job)
989 return job->priv->collate;
993 * gtk_print_job_set_collated:
994 * @job: a #GtkPrintJob
995 * @collate: whether the job is printed collated
997 * Sets whether this job is printed collated.
1002 gtk_print_job_set_collate (GtkPrintJob *job,
1005 job->priv->collate = collate;
1009 * gtk_print_job_get_reverse:
1010 * @job: a #GtkPrintJob
1012 * Gets whether this job is printed reversed.
1014 * Returns: whether the job is printed reversed.
1019 gtk_print_job_get_reverse (GtkPrintJob *job)
1021 return job->priv->reverse;
1025 * gtk_print_job_set_reverse:
1026 * @job: a #GtkPrintJob
1027 * @reverse: whether the job is printed reversed
1029 * Sets whether this job is printed reversed.
1034 gtk_print_job_set_reverse (GtkPrintJob *job,
1037 job->priv->reverse = reverse;