]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintjob.c
Add ability to print in number-up mode for file backend and lpr backend
[~andy/gtk] / gtk / gtkprintjob.c
1 /* GtkPrintJob
2  * Copyright (C) 2006 John (J5) Palmieri  <johnp@redhat.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 #include <glib/gstdio.h>
33 #include "gtkintl.h"
34 #include "gtkprivate.h"
35
36 #include "gtkprintjob.h"
37 #include "gtkprinter.h"
38 #include "gtkprinter-private.h"
39 #include "gtkprintbackend.h"
40 #include "gtkalias.h"
41
42 #ifndef O_BINARY
43 #define O_BINARY 0
44 #endif
45
46 struct _GtkPrintJobPrivate
47 {
48   gchar *title;
49
50   GIOChannel *spool_io;
51   cairo_surface_t *surface;
52
53   GtkPrintStatus status;
54   GtkPrintBackend *backend;  
55   GtkPrinter *printer;
56   GtkPrintSettings *settings;
57   GtkPageSetup *page_setup;
58
59   guint printer_set : 1;
60   guint page_setup_set : 1;
61   guint settings_set  : 1;
62   guint track_print_status : 1;
63 };
64
65
66 #define GTK_PRINT_JOB_GET_PRIVATE(o)  \
67    (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_PRINT_JOB, GtkPrintJobPrivate))
68
69 static void     gtk_print_job_finalize     (GObject               *object);
70 static void     gtk_print_job_set_property (GObject               *object,
71                                             guint                  prop_id,
72                                             const GValue          *value,
73                                             GParamSpec            *pspec);
74 static void     gtk_print_job_get_property (GObject               *object,
75                                             guint                  prop_id,
76                                             GValue                *value,
77                                             GParamSpec            *pspec);
78 static GObject* gtk_print_job_constructor  (GType                  type,
79                                             guint                  n_construct_properties,
80                                             GObjectConstructParam *construct_params);
81
82 enum {
83   STATUS_CHANGED,
84   LAST_SIGNAL
85 };
86
87 enum {
88   PROP_0,
89   PROP_TITLE,
90   PROP_PRINTER,
91   PROP_PAGE_SETUP,
92   PROP_SETTINGS,
93   PROP_TRACK_PRINT_STATUS
94 };
95
96 static guint signals[LAST_SIGNAL] = { 0 };
97
98 G_DEFINE_TYPE (GtkPrintJob, gtk_print_job, G_TYPE_OBJECT)
99
100 static void
101 gtk_print_job_class_init (GtkPrintJobClass *class)
102 {
103   GObjectClass *object_class;
104   object_class = (GObjectClass *) class;
105
106   object_class->finalize = gtk_print_job_finalize;
107   object_class->constructor = gtk_print_job_constructor;
108   object_class->set_property = gtk_print_job_set_property;
109   object_class->get_property = gtk_print_job_get_property;
110
111   g_type_class_add_private (class, sizeof (GtkPrintJobPrivate));
112
113   g_object_class_install_property (object_class,
114                                    PROP_TITLE,
115                                    g_param_spec_string ("title",
116                                                         P_("Title"),
117                                                         P_("Title of the print job"),
118                                                         NULL,
119                                                         GTK_PARAM_READWRITE |
120                                                         G_PARAM_CONSTRUCT_ONLY));
121
122   g_object_class_install_property (object_class,
123                                    PROP_PRINTER,
124                                    g_param_spec_object ("printer",
125                                                         P_("Printer"),
126                                                         P_("Printer to print the job to"),
127                                                         GTK_TYPE_PRINTER,
128                                                         GTK_PARAM_READWRITE |
129                                                         G_PARAM_CONSTRUCT_ONLY));
130
131   g_object_class_install_property (object_class,
132                                    PROP_SETTINGS,
133                                    g_param_spec_object ("settings",
134                                                         P_("Settings"),
135                                                         P_("Printer settings"),
136                                                         GTK_TYPE_PRINT_SETTINGS,
137                                                         GTK_PARAM_READWRITE |
138                                                         G_PARAM_CONSTRUCT_ONLY));
139
140   g_object_class_install_property (object_class,
141                                    PROP_PAGE_SETUP,
142                                    g_param_spec_object ("page-setup",
143                                                         P_("Page Setup"),
144                                                         P_("Page Setup"),
145                                                         GTK_TYPE_PAGE_SETUP,
146                                                         GTK_PARAM_READWRITE |
147                                                         G_PARAM_CONSTRUCT_ONLY));
148
149   g_object_class_install_property (object_class,
150                                    PROP_TRACK_PRINT_STATUS,
151                                    g_param_spec_boolean ("track-print-status",
152                                                          P_("Track Print Status"),
153                                                          P_("TRUE if the print job will continue to emit "
154                                                             "status-changed signals after the print data "
155                                                             "has been sent to the printer or print server."),
156                                                          FALSE,
157                                                          GTK_PARAM_READWRITE));
158   
159
160   /**
161    * GtkPrintJob::status-changed:
162    * @job: the #GtkPrintJob object on which the signal was emitted
163    *
164    * Gets emitted when the status of a job changes. The signal handler
165    * can use gtk_print_job_get_status() to obtain the new status.
166    *
167    * Since: 2.10
168    */
169   signals[STATUS_CHANGED] =
170     g_signal_new (I_("status-changed"),
171                   G_TYPE_FROM_CLASS (class),
172                   G_SIGNAL_RUN_LAST,
173                   G_STRUCT_OFFSET (GtkPrintJobClass, status_changed),
174                   NULL, NULL,
175                   g_cclosure_marshal_VOID__VOID,
176                   G_TYPE_NONE, 0);
177 }
178
179 static void
180 gtk_print_job_init (GtkPrintJob *job)
181 {
182   GtkPrintJobPrivate *priv;
183
184   priv = job->priv = GTK_PRINT_JOB_GET_PRIVATE (job); 
185
186   priv->spool_io = NULL;
187
188   priv->title = g_strdup ("");
189   priv->surface = NULL;
190   priv->backend = NULL;
191   priv->printer = NULL;
192
193   priv->printer_set = FALSE;
194   priv->settings_set = FALSE;
195   priv->page_setup_set = FALSE;
196   priv->status = GTK_PRINT_STATUS_INITIAL;
197   priv->track_print_status = FALSE;
198   
199   job->print_pages = GTK_PRINT_PAGES_ALL;
200   job->page_ranges = NULL;
201   job->num_page_ranges = 0;
202   job->collate = FALSE;
203   job->reverse = FALSE;
204   job->num_copies = 1;
205   job->scale = 1.0;
206   job->page_set = GTK_PAGE_SET_ALL;
207   job->rotate_to_orientation = FALSE;
208   job->number_up = 1;
209   job->number_up_layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
210 }
211
212
213 static GObject*
214 gtk_print_job_constructor (GType                  type,
215                            guint                  n_construct_properties,
216                            GObjectConstructParam *construct_params)
217 {
218   GtkPrintJob *job;
219   GtkPrintJobPrivate *priv;
220   GObject *object;
221
222   object =
223     G_OBJECT_CLASS (gtk_print_job_parent_class)->constructor (type,
224                                                               n_construct_properties,
225                                                               construct_params);
226
227   job = GTK_PRINT_JOB (object);
228
229   priv = job->priv;
230   g_assert (priv->printer_set &&
231             priv->settings_set &&
232             priv->page_setup_set);
233   
234   _gtk_printer_prepare_for_print (priv->printer,
235                                   job,
236                                   priv->settings,
237                                   priv->page_setup);
238
239   return object;
240 }
241
242
243 static void
244 gtk_print_job_finalize (GObject *object)
245 {
246   GtkPrintJob *job = GTK_PRINT_JOB (object);
247   GtkPrintJobPrivate *priv = job->priv;
248
249   if (priv->spool_io != NULL)
250     {
251       g_io_channel_unref (priv->spool_io);
252       priv->spool_io = NULL;
253     }
254   
255   if (priv->backend)
256     g_object_unref (priv->backend);
257
258   if (priv->printer)
259     g_object_unref (priv->printer);
260
261   if (priv->surface)
262     cairo_surface_destroy (priv->surface);
263
264   if (priv->settings)
265     g_object_unref (priv->settings);
266   
267   if (priv->page_setup)
268     g_object_unref (priv->page_setup);
269
270   g_free (job->page_ranges);
271   job->page_ranges = NULL;
272   
273   g_free (priv->title);
274   priv->title = NULL;
275
276   G_OBJECT_CLASS (gtk_print_job_parent_class)->finalize (object);
277 }
278
279 /**
280  * gtk_print_job_new:
281  * @title: the job title
282  * @printer: a #GtkPrinter
283  * @settings: a #GtkPrintSettings
284  * @page_setup: a #GtkPageSetup
285  *
286  * Creates a new #GtkPrintJob.
287  *
288  * Return value: a new #GtkPrintJob
289  *
290  * Since: 2.10
291  **/
292 GtkPrintJob *
293 gtk_print_job_new (const gchar      *title,
294                    GtkPrinter       *printer,
295                    GtkPrintSettings *settings,
296                    GtkPageSetup     *page_setup)
297 {
298   GObject *result;
299   result = g_object_new (GTK_TYPE_PRINT_JOB,
300                          "title", title,
301                          "printer", printer,
302                          "settings", settings,
303                          "page-setup", page_setup,
304                          NULL);
305   return (GtkPrintJob *) result;
306 }
307
308 /**
309  * gtk_print_job_get_settings:
310  * @job: a #GtkPrintJob
311  * 
312  * Gets the #GtkPrintSettings of the print job.
313  * 
314  * Return value: the settings of @job
315  *
316  * Since: 2.10
317  */
318 GtkPrintSettings *
319 gtk_print_job_get_settings (GtkPrintJob *job)
320 {
321   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
322   
323   return job->priv->settings;
324 }
325
326 /**
327  * gtk_print_job_get_printer:
328  * @job: a #GtkPrintJob
329  * 
330  * Gets the #GtkPrinter of the print job.
331  * 
332  * Return value: the printer of @job
333  *
334  * Since: 2.10
335  */
336 GtkPrinter *
337 gtk_print_job_get_printer (GtkPrintJob *job)
338 {
339   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
340   
341   return job->priv->printer;
342 }
343
344 /**
345  * gtk_print_job_get_title:
346  * @job: a #GtkPrintJob
347  * 
348  * Gets the job title.
349  * 
350  * Return value: the title of @job
351  *
352  * Since: 2.10
353  */
354 G_CONST_RETURN gchar *
355 gtk_print_job_get_title (GtkPrintJob *job)
356 {
357   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
358   
359   return job->priv->title;
360 }
361
362 /**
363  * gtk_print_job_get_status:
364  * @job: a #GtkPrintJob
365  * 
366  * Gets the status of the print job.
367  * 
368  * Return value: the status of @job
369  *
370  * Since: 2.10
371  */
372 GtkPrintStatus
373 gtk_print_job_get_status (GtkPrintJob *job)
374 {
375   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), GTK_PRINT_STATUS_FINISHED);
376   
377   return job->priv->status;
378 }
379
380 void
381 gtk_print_job_set_status (GtkPrintJob   *job,
382                           GtkPrintStatus status)
383 {
384   GtkPrintJobPrivate *priv;
385
386   g_return_if_fail (GTK_IS_PRINT_JOB (job));
387
388   priv = job->priv;
389
390   if (priv->status == status)
391     return;
392
393   priv->status = status;
394   g_signal_emit (job, signals[STATUS_CHANGED], 0);
395 }
396
397 /**
398  * gtk_print_job_set_source_file:
399  * @job: a #GtkPrintJob
400  * @filename: the file to be printed
401  * @error: return location for errors
402  * 
403  * Make the #GtkPrintJob send an existing document to the 
404  * printing system. The file can be in any format understood
405  * by the platforms printing system (typically PostScript,
406  * but on many platforms PDF may work too). See 
407  * gtk_printer_accepts_pdf() and gtk_printer_accepts_ps().
408  * 
409  * Returns: %FALSE if an error occurred
410  *
411  * Since: 2.10
412  **/
413 gboolean
414 gtk_print_job_set_source_file (GtkPrintJob *job,
415                                const gchar *filename,
416                                GError     **error)
417 {
418   GtkPrintJobPrivate *priv;
419   GError *tmp_error;
420
421   tmp_error = NULL;
422
423   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
424
425   priv = job->priv;
426
427   priv->spool_io = g_io_channel_new_file (filename, "r", &tmp_error);
428
429   if (tmp_error == NULL)
430     g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error);
431
432   if (tmp_error != NULL)
433     {
434       g_propagate_error (error, tmp_error);
435       return FALSE;
436     }
437
438   return TRUE;
439 }
440
441 /**
442  * gtk_print_job_get_surface:
443  * @job: a #GtkPrintJob
444  * @error: return location for errors, or %NULL
445  * 
446  * Gets a cairo surface onto which the pages of
447  * the print job should be rendered.
448  * 
449  * Return value: the cairo surface of @job
450  *
451  * Since: 2.10
452  **/
453 cairo_surface_t *
454 gtk_print_job_get_surface (GtkPrintJob  *job,
455                            GError      **error)
456 {
457   GtkPrintJobPrivate *priv;
458   gchar *filename;
459   gdouble width, height;
460   GtkPaperSize *paper_size;
461   int fd;
462   GError *tmp_error;
463
464   tmp_error = NULL;
465
466   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
467
468   priv = job->priv;
469
470   if (priv->surface)
471     return priv->surface;
472  
473   g_return_val_if_fail (priv->spool_io == NULL, NULL);
474  
475   fd = g_file_open_tmp ("gtkprint_XXXXXX", 
476                          &filename, 
477                          &tmp_error);
478   if (fd == -1)
479     {
480       g_free (filename);
481       g_propagate_error (error, tmp_error);
482       return NULL;
483     }
484
485   fchmod (fd, S_IRUSR | S_IWUSR);
486   
487 #ifdef G_ENABLE_DEBUG 
488   /* If we are debugging printing don't delete the tmp files */
489   if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
490 #endif /* G_ENABLE_DEBUG */
491   g_unlink (filename);
492   g_free (filename);
493
494   paper_size = gtk_page_setup_get_paper_size (priv->page_setup);
495   width = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
496   height = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
497  
498   priv->spool_io = g_io_channel_unix_new (fd);
499   g_io_channel_set_close_on_unref (priv->spool_io, TRUE);
500   g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error);
501   
502   if (tmp_error != NULL)
503     {
504       g_io_channel_unref (priv->spool_io);
505       priv->spool_io = NULL;
506       g_propagate_error (error, tmp_error);
507       return NULL;
508     }
509
510   priv->surface = _gtk_printer_create_cairo_surface (priv->printer,
511                                                      priv->settings,
512                                                      width, height,
513                                                      priv->spool_io);
514   
515   return priv->surface;
516 }
517
518 /**
519  * gtk_print_job_set_track_print_status:
520  * @job: a #GtkPrintJob
521  * @track_status: %TRUE to track status after printing
522  * 
523  * If track_status is %TRUE, the print job will try to continue report
524  * on the status of the print job in the printer queues and printer. This
525  * can allow your application to show things like "out of paper" issues,
526  * and when the print job actually reaches the printer.
527  * 
528  * This function is often implemented using some form of polling, so it should
529  * not be enabled unless needed.
530  *
531  * Since: 2.10
532  */
533 void
534 gtk_print_job_set_track_print_status (GtkPrintJob *job,
535                                       gboolean     track_status)
536 {
537   GtkPrintJobPrivate *priv;
538
539   g_return_if_fail (GTK_IS_PRINT_JOB (job));
540
541   priv = job->priv;
542
543   track_status = track_status != FALSE;
544
545   if (priv->track_print_status != track_status)
546     {
547       priv->track_print_status = track_status;
548       
549       g_object_notify (G_OBJECT (job), "track-print-status");
550     }
551 }
552
553 /**
554  * gtk_print_job_get_track_print_status:
555  * @job: a #GtkPrintJob
556  *
557  * Returns wheter jobs will be tracked after printing.
558  * For details, see gtk_print_job_set_track_print_status().
559  *
560  * Return value: %TRUE if print job status will be reported after printing
561  *
562  * Since: 2.10
563  */
564 gboolean
565 gtk_print_job_get_track_print_status (GtkPrintJob *job)
566 {
567   GtkPrintJobPrivate *priv;
568
569   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
570
571   priv = job->priv;
572   
573   return priv->track_print_status;
574 }
575
576 static void
577 gtk_print_job_set_property (GObject      *object,
578                             guint         prop_id,
579                             const GValue *value,
580                             GParamSpec   *pspec)
581
582 {
583   GtkPrintJob *job = GTK_PRINT_JOB (object);
584   GtkPrintJobPrivate *priv = job->priv;
585   GtkPrintSettings *settings;
586
587   switch (prop_id)
588     {
589     case PROP_TITLE:
590       g_free (priv->title);
591       priv->title = g_value_dup_string (value);
592       break;
593     
594     case PROP_PRINTER:
595       priv->printer = GTK_PRINTER (g_value_dup_object (value));
596       priv->printer_set = TRUE;
597       priv->backend = g_object_ref (gtk_printer_get_backend (priv->printer));
598       break;
599
600     case PROP_PAGE_SETUP:
601       priv->page_setup = GTK_PAGE_SETUP (g_value_dup_object (value));
602       priv->page_setup_set = TRUE;
603       break;
604       
605     case PROP_SETTINGS:
606       /* We save a copy of the settings since we modify
607        * if when preparing the printer job. */
608       settings = GTK_PRINT_SETTINGS (g_value_get_object (value));
609       priv->settings = gtk_print_settings_copy (settings);
610       priv->settings_set = TRUE;
611       break;
612
613     case PROP_TRACK_PRINT_STATUS:
614       gtk_print_job_set_track_print_status (job, g_value_get_boolean (value));
615       break;
616
617     default:
618       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619       break;
620     }
621 }
622
623 static void
624 gtk_print_job_get_property (GObject    *object,
625                             guint       prop_id,
626                             GValue     *value,
627                             GParamSpec *pspec)
628 {
629   GtkPrintJob *job = GTK_PRINT_JOB (object);
630   GtkPrintJobPrivate *priv = job->priv;
631
632   switch (prop_id)
633     {
634     case PROP_TITLE:
635       g_value_set_string (value, priv->title);
636       break;
637     case PROP_PRINTER:
638       g_value_set_object (value, priv->printer);
639       break;
640     case PROP_SETTINGS:
641       g_value_set_object (value, priv->settings);
642       break;
643     case PROP_PAGE_SETUP:
644       g_value_set_object (value, priv->page_setup);
645       break;
646     case PROP_TRACK_PRINT_STATUS:
647       g_value_set_boolean (value, priv->track_print_status);
648       break;
649     default:
650       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
651       break;
652     }
653 }
654
655 /**
656  * gtk_print_job_send:
657  * @job: a GtkPrintJob
658  * @callback: function to call when the job completes or an error occurs
659  * @user_data: user data that gets passed to @callback
660  * @dnotify: destroy notify for @user_data
661  * 
662  * Sends the print job off to the printer.  
663  * 
664  * Since: 2.10
665  **/
666 void
667 gtk_print_job_send (GtkPrintJob             *job,
668                     GtkPrintJobCompleteFunc  callback,
669                     gpointer                 user_data,
670                     GDestroyNotify           dnotify)
671 {
672   GtkPrintJobPrivate *priv;
673
674   g_return_if_fail (GTK_IS_PRINT_JOB (job));
675
676   priv = job->priv;
677   g_return_if_fail (priv->spool_io != NULL);
678   
679   gtk_print_job_set_status (job, GTK_PRINT_STATUS_SENDING_DATA);
680   
681   g_io_channel_seek_position (priv->spool_io, 0, G_SEEK_SET, NULL);
682   
683   gtk_print_backend_print_stream (priv->backend, job,
684                                   priv->spool_io,
685                                   callback, user_data, dnotify);
686 }
687
688
689 #define __GTK_PRINT_JOB_C__
690 #include "gtkaliasdef.c"