]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintjob.c
Bug 549810 – Memory leaks in printing code
[~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 }
209
210
211 static GObject*
212 gtk_print_job_constructor (GType                  type,
213                            guint                  n_construct_properties,
214                            GObjectConstructParam *construct_params)
215 {
216   GtkPrintJob *job;
217   GtkPrintJobPrivate *priv;
218   GObject *object;
219
220   object =
221     G_OBJECT_CLASS (gtk_print_job_parent_class)->constructor (type,
222                                                               n_construct_properties,
223                                                               construct_params);
224
225   job = GTK_PRINT_JOB (object);
226
227   priv = job->priv;
228   g_assert (priv->printer_set &&
229             priv->settings_set &&
230             priv->page_setup_set);
231   
232   _gtk_printer_prepare_for_print (priv->printer,
233                                   job,
234                                   priv->settings,
235                                   priv->page_setup);
236
237   return object;
238 }
239
240
241 static void
242 gtk_print_job_finalize (GObject *object)
243 {
244   GtkPrintJob *job = GTK_PRINT_JOB (object);
245   GtkPrintJobPrivate *priv = job->priv;
246
247   if (priv->spool_io != NULL)
248     {
249       g_io_channel_unref (priv->spool_io);
250       priv->spool_io = NULL;
251     }
252   
253   if (priv->backend)
254     g_object_unref (priv->backend);
255
256   if (priv->printer)
257     g_object_unref (priv->printer);
258
259   if (priv->surface)
260     cairo_surface_destroy (priv->surface);
261
262   if (priv->settings)
263     g_object_unref (priv->settings);
264   
265   if (priv->page_setup)
266     g_object_unref (priv->page_setup);
267
268   g_free (job->page_ranges);
269   job->page_ranges = NULL;
270   
271   g_free (priv->title);
272   priv->title = NULL;
273
274   G_OBJECT_CLASS (gtk_print_job_parent_class)->finalize (object);
275 }
276
277 /**
278  * gtk_print_job_new:
279  * @title: the job title
280  * @printer: a #GtkPrinter
281  * @settings: a #GtkPrintSettings
282  * @page_setup: a #GtkPageSetup
283  *
284  * Creates a new #GtkPrintJob.
285  *
286  * Return value: a new #GtkPrintJob
287  *
288  * Since: 2.10
289  **/
290 GtkPrintJob *
291 gtk_print_job_new (const gchar      *title,
292                    GtkPrinter       *printer,
293                    GtkPrintSettings *settings,
294                    GtkPageSetup     *page_setup)
295 {
296   GObject *result;
297   result = g_object_new (GTK_TYPE_PRINT_JOB,
298                          "title", title,
299                          "printer", printer,
300                          "settings", settings,
301                          "page-setup", page_setup,
302                          NULL);
303   return (GtkPrintJob *) result;
304 }
305
306 /**
307  * gtk_print_job_get_settings:
308  * @job: a #GtkPrintJob
309  * 
310  * Gets the #GtkPrintSettings of the print job.
311  * 
312  * Return value: the settings of @job
313  *
314  * Since: 2.10
315  */
316 GtkPrintSettings *
317 gtk_print_job_get_settings (GtkPrintJob *job)
318 {
319   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
320   
321   return job->priv->settings;
322 }
323
324 /**
325  * gtk_print_job_get_printer:
326  * @job: a #GtkPrintJob
327  * 
328  * Gets the #GtkPrinter of the print job.
329  * 
330  * Return value: the printer of @job
331  *
332  * Since: 2.10
333  */
334 GtkPrinter *
335 gtk_print_job_get_printer (GtkPrintJob *job)
336 {
337   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
338   
339   return job->priv->printer;
340 }
341
342 /**
343  * gtk_print_job_get_title:
344  * @job: a #GtkPrintJob
345  * 
346  * Gets the job title.
347  * 
348  * Return value: the title of @job
349  *
350  * Since: 2.10
351  */
352 G_CONST_RETURN gchar *
353 gtk_print_job_get_title (GtkPrintJob *job)
354 {
355   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
356   
357   return job->priv->title;
358 }
359
360 /**
361  * gtk_print_job_get_status:
362  * @job: a #GtkPrintJob
363  * 
364  * Gets the status of the print job.
365  * 
366  * Return value: the status of @job
367  *
368  * Since: 2.10
369  */
370 GtkPrintStatus
371 gtk_print_job_get_status (GtkPrintJob *job)
372 {
373   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), GTK_PRINT_STATUS_FINISHED);
374   
375   return job->priv->status;
376 }
377
378 void
379 gtk_print_job_set_status (GtkPrintJob   *job,
380                           GtkPrintStatus status)
381 {
382   GtkPrintJobPrivate *priv;
383
384   g_return_if_fail (GTK_IS_PRINT_JOB (job));
385
386   priv = job->priv;
387
388   if (priv->status == status)
389     return;
390
391   priv->status = status;
392   g_signal_emit (job, signals[STATUS_CHANGED], 0);
393 }
394
395 /**
396  * gtk_print_job_set_source_file:
397  * @job: a #GtkPrintJob
398  * @filename: the file to be printed
399  * @error: return location for errors
400  * 
401  * Make the #GtkPrintJob send an existing document to the 
402  * printing system. The file can be in any format understood
403  * by the platforms printing system (typically PostScript,
404  * but on many platforms PDF may work too). See 
405  * gtk_printer_accepts_pdf() and gtk_printer_accepts_ps().
406  * 
407  * Returns: %FALSE if an error occurred
408  *
409  * Since: 2.10
410  **/
411 gboolean
412 gtk_print_job_set_source_file (GtkPrintJob *job,
413                                const gchar *filename,
414                                GError     **error)
415 {
416   GtkPrintJobPrivate *priv;
417   GError *tmp_error;
418
419   tmp_error = NULL;
420
421   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
422
423   priv = job->priv;
424
425   priv->spool_io = g_io_channel_new_file (filename, "r", &tmp_error);
426
427   if (tmp_error == NULL)
428     g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error);
429
430   if (tmp_error != NULL)
431     {
432       g_propagate_error (error, tmp_error);
433       return FALSE;
434     }
435
436   return TRUE;
437 }
438
439 /**
440  * gtk_print_job_get_surface:
441  * @job: a #GtkPrintJob
442  * @error: return location for errors, or %NULL
443  * 
444  * Gets a cairo surface onto which the pages of
445  * the print job should be rendered.
446  * 
447  * Return value: the cairo surface of @job
448  *
449  * Since: 2.10
450  **/
451 cairo_surface_t *
452 gtk_print_job_get_surface (GtkPrintJob  *job,
453                            GError      **error)
454 {
455   GtkPrintJobPrivate *priv;
456   gchar *filename;
457   gdouble width, height;
458   GtkPaperSize *paper_size;
459   int fd;
460   GError *tmp_error;
461
462   tmp_error = NULL;
463
464   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
465
466   priv = job->priv;
467
468   if (priv->surface)
469     return priv->surface;
470  
471   g_return_val_if_fail (priv->spool_io == NULL, NULL);
472  
473   fd = g_file_open_tmp ("gtkprint_XXXXXX", 
474                          &filename, 
475                          &tmp_error);
476   if (fd == -1)
477     {
478       g_free (filename);
479       g_propagate_error (error, tmp_error);
480       return NULL;
481     }
482
483   fchmod (fd, S_IRUSR | S_IWUSR);
484   
485 #ifdef G_ENABLE_DEBUG 
486   /* If we are debugging printing don't delete the tmp files */
487   if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
488 #endif /* G_ENABLE_DEBUG */
489   g_unlink (filename);
490   g_free (filename);
491
492   paper_size = gtk_page_setup_get_paper_size (priv->page_setup);
493   width = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
494   height = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
495  
496   priv->spool_io = g_io_channel_unix_new (fd);
497   g_io_channel_set_close_on_unref (priv->spool_io, TRUE);
498   g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error);
499   
500   if (tmp_error != NULL)
501     {
502       g_io_channel_unref (priv->spool_io);
503       priv->spool_io = NULL;
504       g_propagate_error (error, tmp_error);
505       return NULL;
506     }
507
508   priv->surface = _gtk_printer_create_cairo_surface (priv->printer,
509                                                      priv->settings,
510                                                      width, height,
511                                                      priv->spool_io);
512   
513   return priv->surface;
514 }
515
516 /**
517  * gtk_print_job_set_track_print_status:
518  * @job: a #GtkPrintJob
519  * @track_status: %TRUE to track status after printing
520  * 
521  * If track_status is %TRUE, the print job will try to continue report
522  * on the status of the print job in the printer queues and printer. This
523  * can allow your application to show things like "out of paper" issues,
524  * and when the print job actually reaches the printer.
525  * 
526  * This function is often implemented using some form of polling, so it should
527  * not be enabled unless needed.
528  *
529  * Since: 2.10
530  */
531 void
532 gtk_print_job_set_track_print_status (GtkPrintJob *job,
533                                       gboolean     track_status)
534 {
535   GtkPrintJobPrivate *priv;
536
537   g_return_if_fail (GTK_IS_PRINT_JOB (job));
538
539   priv = job->priv;
540
541   track_status = track_status != FALSE;
542
543   if (priv->track_print_status != track_status)
544     {
545       priv->track_print_status = track_status;
546       
547       g_object_notify (G_OBJECT (job), "track-print-status");
548     }
549 }
550
551 /**
552  * gtk_print_job_get_track_print_status:
553  * @job: a #GtkPrintJob
554  *
555  * Returns wheter jobs will be tracked after printing.
556  * For details, see gtk_print_job_set_track_print_status().
557  *
558  * Return value: %TRUE if print job status will be reported after printing
559  *
560  * Since: 2.10
561  */
562 gboolean
563 gtk_print_job_get_track_print_status (GtkPrintJob *job)
564 {
565   GtkPrintJobPrivate *priv;
566
567   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
568
569   priv = job->priv;
570   
571   return priv->track_print_status;
572 }
573
574 static void
575 gtk_print_job_set_property (GObject      *object,
576                             guint         prop_id,
577                             const GValue *value,
578                             GParamSpec   *pspec)
579
580 {
581   GtkPrintJob *job = GTK_PRINT_JOB (object);
582   GtkPrintJobPrivate *priv = job->priv;
583   GtkPrintSettings *settings;
584
585   switch (prop_id)
586     {
587     case PROP_TITLE:
588       g_free (priv->title);
589       priv->title = g_value_dup_string (value);
590       break;
591     
592     case PROP_PRINTER:
593       priv->printer = GTK_PRINTER (g_value_dup_object (value));
594       priv->printer_set = TRUE;
595       priv->backend = g_object_ref (gtk_printer_get_backend (priv->printer));
596       break;
597
598     case PROP_PAGE_SETUP:
599       priv->page_setup = GTK_PAGE_SETUP (g_value_dup_object (value));
600       priv->page_setup_set = TRUE;
601       break;
602       
603     case PROP_SETTINGS:
604       /* We save a copy of the settings since we modify
605        * if when preparing the printer job. */
606       settings = GTK_PRINT_SETTINGS (g_value_get_object (value));
607       priv->settings = gtk_print_settings_copy (settings);
608       priv->settings_set = TRUE;
609       break;
610
611     case PROP_TRACK_PRINT_STATUS:
612       gtk_print_job_set_track_print_status (job, g_value_get_boolean (value));
613       break;
614
615     default:
616       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
617       break;
618     }
619 }
620
621 static void
622 gtk_print_job_get_property (GObject    *object,
623                             guint       prop_id,
624                             GValue     *value,
625                             GParamSpec *pspec)
626 {
627   GtkPrintJob *job = GTK_PRINT_JOB (object);
628   GtkPrintJobPrivate *priv = job->priv;
629
630   switch (prop_id)
631     {
632     case PROP_TITLE:
633       g_value_set_string (value, priv->title);
634       break;
635     case PROP_PRINTER:
636       g_value_set_object (value, priv->printer);
637       break;
638     case PROP_SETTINGS:
639       g_value_set_object (value, priv->settings);
640       break;
641     case PROP_PAGE_SETUP:
642       g_value_set_object (value, priv->page_setup);
643       break;
644     case PROP_TRACK_PRINT_STATUS:
645       g_value_set_boolean (value, priv->track_print_status);
646       break;
647     default:
648       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649       break;
650     }
651 }
652
653 /**
654  * gtk_print_job_send:
655  * @job: a GtkPrintJob
656  * @callback: function to call when the job completes or an error occurs
657  * @user_data: user data that gets passed to @callback
658  * @dnotify: destroy notify for @user_data
659  * 
660  * Sends the print job off to the printer.  
661  * 
662  * Since: 2.10
663  **/
664 void
665 gtk_print_job_send (GtkPrintJob             *job,
666                     GtkPrintJobCompleteFunc  callback,
667                     gpointer                 user_data,
668                     GDestroyNotify           dnotify)
669 {
670   GtkPrintJobPrivate *priv;
671
672   g_return_if_fail (GTK_IS_PRINT_JOB (job));
673
674   priv = job->priv;
675   g_return_if_fail (priv->spool_io != NULL);
676   
677   gtk_print_job_set_status (job, GTK_PRINT_STATUS_SENDING_DATA);
678   
679   g_io_channel_seek_position (priv->spool_io, 0, G_SEEK_SET, NULL);
680   
681   gtk_print_backend_print_stream (priv->backend, job,
682                                   priv->spool_io,
683                                   callback, user_data, dnotify);
684 }
685
686
687 #define __GTK_PRINT_JOB_C__
688 #include "gtkaliasdef.c"