]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintjob.c
Add to docs. (gtk_print_job_class_init): Make track-print-status a
[~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   gint spool_file_fd;
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_file_fd = -1;
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_file_fd >= 0)
248     {
249       close (priv->spool_file_fd);
250       priv->spool_file_fd = -1;
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
418   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
419
420   priv = job->priv;
421
422   priv->spool_file_fd = g_open (filename, O_RDONLY|O_BINARY);
423   if (priv->spool_file_fd < 0)
424     {
425       gchar *display_filename = g_filename_display_name (filename);
426       int save_errno = errno;
427       
428       g_set_error (error,
429                    G_FILE_ERROR,
430                    g_file_error_from_errno (save_errno),
431                    _("Failed to open file '%s': %s"),
432                    display_filename, 
433                    g_strerror (save_errno));
434       
435       g_free (display_filename);
436
437       return FALSE;
438     }
439     return TRUE;
440 }
441
442 /**
443  * gtk_print_job_get_surface:
444  * @job: a #GtkPrintJob
445  * @error: return location for errors, or %NULL
446  * 
447  * Gets a cairo surface onto which the pages of
448  * the print job should be rendered.
449  * 
450  * Return value: the cairo surface of @job
451  *
452  * Since: 2.10
453  **/
454 cairo_surface_t *
455 gtk_print_job_get_surface (GtkPrintJob  *job,
456                            GError      **error)
457 {
458   GtkPrintJobPrivate *priv;
459   gchar *filename;
460   gdouble width, height;
461   GtkPaperSize *paper_size;
462   
463   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
464
465   priv = job->priv;
466
467   if (priv->surface)
468     return priv->surface;
469  
470   g_return_val_if_fail (priv->spool_file_fd == -1, NULL);
471  
472   priv->spool_file_fd = g_file_open_tmp ("gtkprint_XXXXXX", 
473                                          &filename, 
474                                          error);
475   if (priv->spool_file_fd == -1)
476     return NULL;
477
478   fchmod (priv->spool_file_fd, S_IRUSR | S_IWUSR);
479   unlink (filename);
480
481   paper_size = gtk_page_setup_get_paper_size (priv->page_setup);
482   width = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
483   height = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
484   
485   priv->surface = _gtk_printer_create_cairo_surface (priv->printer,
486                                                      priv->settings,
487                                                      width, height,
488                                                      priv->spool_file_fd);
489  
490   return priv->surface;
491 }
492
493 /**
494  * gtk_print_job_set_track_print_status:
495  * @job: a #GtkPrintJob
496  * @track_status: %TRUE to track status after printing
497  * 
498  * If track_status is %TRUE, the print job will try to continue report
499  * on the status of the print job in the printer queues and printer. This
500  * can allow your application to show things like "out of paper" issues,
501  * and when the print job actually reaches the printer.
502  * 
503  * This function is often implemented using some form of polling, so it should
504  * not be enabled unless needed.
505  *
506  * Since: 2.10
507  */
508 void
509 gtk_print_job_set_track_print_status (GtkPrintJob *job,
510                                       gboolean     track_status)
511 {
512   GtkPrintJobPrivate *priv;
513
514   g_return_if_fail (GTK_IS_PRINT_JOB (job));
515
516   priv = job->priv;
517
518   track_status = track_status != FALSE;
519
520   if (priv->track_print_status != track_status)
521     {
522       priv->track_print_status = track_status;
523       
524       g_object_notify (G_OBJECT (job), "track-print-status");
525     }
526 }
527
528 /**
529  * gtk_print_job_get_track_print_status:
530  * @job: a #GtkPrintJob
531  *
532  * Returns wheter jobs will be tracked after printing.
533  * For details, see gtk_print_job_set_track_print_status().
534  *
535  * Return value: %TRUE if print job status will be reported after printing
536  *
537  * Since: 2.10
538  */
539 gboolean
540 gtk_print_job_get_track_print_status (GtkPrintJob *job)
541 {
542   GtkPrintJobPrivate *priv;
543
544   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
545
546   priv = job->priv;
547   
548   return priv->track_print_status;
549 }
550
551 static void
552 gtk_print_job_set_property (GObject      *object,
553                             guint         prop_id,
554                             const GValue *value,
555                             GParamSpec   *pspec)
556
557 {
558   GtkPrintJob *job = GTK_PRINT_JOB (object);
559   GtkPrintJobPrivate *priv = job->priv;
560   GtkPrintSettings *settings;
561
562   switch (prop_id)
563     {
564     case PROP_TITLE:
565       priv->title = g_value_dup_string (value);
566       break;
567     
568     case PROP_PRINTER:
569       priv->printer = GTK_PRINTER (g_value_dup_object (value));
570       priv->printer_set = TRUE;
571       priv->backend = g_object_ref (gtk_printer_get_backend (priv->printer));
572       break;
573
574     case PROP_PAGE_SETUP:
575       priv->page_setup = GTK_PAGE_SETUP (g_value_dup_object (value));
576       priv->page_setup_set = TRUE;
577       break;
578       
579     case PROP_SETTINGS:
580       /* We save a copy of the settings since we modify
581        * if when preparing the printer job. */
582       settings = GTK_PRINT_SETTINGS (g_value_get_object (value));
583       priv->settings = gtk_print_settings_copy (settings);
584       priv->settings_set = TRUE;
585       break;
586
587     case PROP_TRACK_PRINT_STATUS:
588       gtk_print_job_set_track_print_status (job, g_value_get_boolean (value));
589       break;
590
591     default:
592       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
593       break;
594     }
595 }
596
597 static void
598 gtk_print_job_get_property (GObject    *object,
599                             guint       prop_id,
600                             GValue     *value,
601                             GParamSpec *pspec)
602 {
603   GtkPrintJob *job = GTK_PRINT_JOB (object);
604   GtkPrintJobPrivate *priv = job->priv;
605
606   switch (prop_id)
607     {
608     case PROP_TITLE:
609       g_value_set_string (value, priv->title);
610       break;
611     case PROP_PRINTER:
612       g_value_set_object (value, priv->printer);
613       break;
614     case PROP_SETTINGS:
615       g_value_set_object (value, priv->settings);
616       break;
617     case PROP_PAGE_SETUP:
618       g_value_set_object (value, priv->page_setup);
619       break;
620     case PROP_TRACK_PRINT_STATUS:
621       g_value_set_boolean (value, priv->track_print_status);
622       break;
623     default:
624       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
625       break;
626     }
627 }
628
629 /**
630  * gtk_print_job_send:
631  * @job: a GtkPrintJob
632  * @callback: function to call when the job completes
633  * @user_data: user data that gets passed to @callback
634  * @dnotify: destroy notify for @user_data
635  * @error: return location for errors, or %NULL
636  * 
637  * Sends the print job off to the printer.  
638  * 
639  * Return value: %FALSE if an error occurred
640  *
641  * Since: 2.10
642  **/
643 gboolean
644 gtk_print_job_send (GtkPrintJob             *job,
645                     GtkPrintJobCompleteFunc  callback,
646                     gpointer                 user_data,
647                     GDestroyNotify           dnotify,
648                     GError                 **error)
649 {
650   GtkPrintJobPrivate *priv;
651
652   g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
653
654   priv = job->priv;
655   g_return_val_if_fail (priv->spool_file_fd > 0, FALSE);
656   
657   gtk_print_job_set_status (job, GTK_PRINT_STATUS_SENDING_DATA);
658   lseek (priv->spool_file_fd, 0, SEEK_SET);
659   gtk_print_backend_print_stream (priv->backend,
660                                   job,
661                                   priv->spool_file_fd,
662                                   callback,
663                                   user_data,
664                                   dnotify);
665
666   return TRUE;
667 }
668
669 GType
670 gtk_print_capabilities_get_type (void)
671 {
672   static GType etype = 0;
673
674   if (etype == 0)
675     {
676       static const GFlagsValue values[] = {
677         { GTK_PRINT_CAPABILITY_PAGE_SET, "GTK_PRINT_CAPABILITY_PAGE_SET", "page-set" },
678         { GTK_PRINT_CAPABILITY_COPIES, "GTK_PRINT_CAPABILITY_COPIES", "copies" },
679         { GTK_PRINT_CAPABILITY_COLLATE, "GTK_PRINT_CAPABILITY_COLLATE", "collate" },
680         { GTK_PRINT_CAPABILITY_REVERSE, "GTK_PRINT_CAPABILITY_REVERSE", "reverse" },
681         { GTK_PRINT_CAPABILITY_SCALE, "GTK_PRINT_CAPABILITY_SCALE", "scale" },
682         { 0, NULL, NULL }
683       };
684
685       etype = g_flags_register_static (I_("GtkPrintCapabilities"), values);
686     }
687
688   return etype;
689 }
690
691
692 #define __GTK_PRINT_JOB_C__
693 #include "gtkaliasdef.c"