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