gtkprintjob.c 23.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* GtkPrintJob
 * Copyright (C) 2006 John (J5) Palmieri  <johnp@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16
17
 */

Matthias Clasen's avatar
Matthias Clasen committed
18
/**
Matthias Clasen's avatar
Matthias Clasen committed
19
 * GtkPrintJob:
Matthias Clasen's avatar
Matthias Clasen committed
20
 *
Matthias Clasen's avatar
Matthias Clasen committed
21
 * A `GtkPrintJob` object represents a job that is sent to a printer.
Matthias Clasen's avatar
Matthias Clasen committed
22
 *
Matthias Clasen's avatar
Matthias Clasen committed
23
24
25
26
27
 * You only need to deal directly with print jobs if you use the
 * non-portable [class@Gtk.PrintUnixDialog] API.
 *
 * Use [method@Gtk.PrintJob.get_surface] to obtain the cairo surface
 * onto which the pages must be drawn. Use [method@Gtk.PrintJob.send]
28
 * to send the finished job to the printer. If you don’t use cairo
Matthias Clasen's avatar
Matthias Clasen committed
29
30
 * `GtkPrintJob` also supports printing of manually generated PostScript,
 * via [method@Gtk.PrintJob.set_source_file].
Matthias Clasen's avatar
Matthias Clasen committed
31
 */
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>

#include <glib/gstdio.h>
#include "gtkintl.h"
#include "gtkprivate.h"

#include "gtkprintjob.h"
#include "gtkprinter.h"
Matthias Clasen's avatar
Matthias Clasen committed
50
#include "gtkprinterprivate.h"
51
#include "gtkprintbackendprivate.h"
52
53
54
55
56

#ifndef O_BINARY
#define O_BINARY 0
#endif

Matthias Clasen's avatar
Matthias Clasen committed
57
58
59
60
61
62
typedef struct _GtkPrintJobClass     GtkPrintJobClass;

struct _GtkPrintJob
{
  GObject parent_instance;

Benjamin Otte's avatar
Benjamin Otte committed
63
  char *title;
64

65
  GIOChannel *spool_io;
66
67
68
  cairo_surface_t *surface;

  GtkPrintStatus status;
69
  GtkPrintBackend *backend;
70
71
72
73
  GtkPrinter *printer;
  GtkPrintSettings *settings;
  GtkPageSetup *page_setup;

74
75
  GtkPrintPages print_pages;
  GtkPageRange *page_ranges;
Benjamin Otte's avatar
Benjamin Otte committed
76
  int num_page_ranges;
77
  GtkPageSet page_set;
Benjamin Otte's avatar
Benjamin Otte committed
78
  int num_copies;
79
  double scale;
80
81
82
83
84
85
86
87
88
89
  guint number_up;
  GtkNumberUpLayout number_up_layout;

  guint printer_set           : 1;
  guint page_setup_set        : 1;
  guint settings_set          : 1;
  guint track_print_status    : 1;
  guint rotate_to_orientation : 1;
  guint collate               : 1;
  guint reverse               : 1;
90
91
};

92
93
94
95
96
97
98
struct _GtkPrintJobClass
{
  GObjectClass parent_class;

  void (*status_changed) (GtkPrintJob *job);
};

99
100
101
102
103
104
105
106
107
static void     gtk_print_job_finalize     (GObject               *object);
static void     gtk_print_job_set_property (GObject               *object,
					    guint                  prop_id,
					    const GValue          *value,
					    GParamSpec            *pspec);
static void     gtk_print_job_get_property (GObject               *object,
					    guint                  prop_id,
					    GValue                *value,
					    GParamSpec            *pspec);
108
static void     gtk_print_job_constructed  (GObject               *object);
109
110
111
112
113
114
115
116

enum {
  STATUS_CHANGED,
  LAST_SIGNAL
};

enum {
  PROP_0,
117
118
119
120
121
  PROP_TITLE,
  PROP_PRINTER,
  PROP_PAGE_SETUP,
  PROP_SETTINGS,
  PROP_TRACK_PRINT_STATUS
122
123
124
125
};

static guint signals[LAST_SIGNAL] = { 0 };

126
G_DEFINE_TYPE (GtkPrintJob, gtk_print_job, G_TYPE_OBJECT)
127
128
129
130
131
132
133
134

static void
gtk_print_job_class_init (GtkPrintJobClass *class)
{
  GObjectClass *object_class;
  object_class = (GObjectClass *) class;

  object_class->finalize = gtk_print_job_finalize;
135
  object_class->constructed = gtk_print_job_constructed;
136
137
138
  object_class->set_property = gtk_print_job_set_property;
  object_class->get_property = gtk_print_job_get_property;

Matthias Clasen's avatar
Matthias Clasen committed
139
140
141
142
143
  /**
   * GtkPrintJob:title: (attributes org.gtk.Property.get=gtk_print_job_get_title)
   *
   * The title of the print job.
   */
144
145
  g_object_class_install_property (object_class,
                                   PROP_TITLE,
146
                                   g_param_spec_string ("title", NULL, NULL,
147
148
149
150
						        NULL,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

Matthias Clasen's avatar
Matthias Clasen committed
151
152
153
154
155
  /**
   * GtkPrintJob:printer: (attributes org.gtk.Property.get=gtk_print_job_get_printer)
   *
   * The printer to send the job to.
   */
156
157
  g_object_class_install_property (object_class,
                                   PROP_PRINTER,
158
                                   g_param_spec_object ("printer", NULL, NULL,
159
160
161
162
						        GTK_TYPE_PRINTER,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

Matthias Clasen's avatar
Matthias Clasen committed
163
164
165
166
167
  /**
   * GtkPrintJob:settings: (attributes org.gtk.Property.get=gtk_print_job_get_settings)
   *
   * Printer settings.
   */
168
169
  g_object_class_install_property (object_class,
                                   PROP_SETTINGS,
170
                                   g_param_spec_object ("settings", NULL, NULL,
171
172
173
174
						        GTK_TYPE_PRINT_SETTINGS,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

Matthias Clasen's avatar
Matthias Clasen committed
175
176
177
178
179
  /**
   * GtkPrintJob:page-setup:
   *
   * Page setup.
   */
180
181
  g_object_class_install_property (object_class,
                                   PROP_PAGE_SETUP,
182
                                   g_param_spec_object ("page-setup", NULL, NULL,
183
184
185
186
						        GTK_TYPE_PAGE_SETUP,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

Matthias Clasen's avatar
Matthias Clasen committed
187
188
189
190
191
192
  /**
   * GtkPrintJob:track-print-status: (attributes org.gtk.Property.get=gtk_print_job_get_track_print_status org.gtk.Property.set=gtk_print_job_set_track_print_status)
   *
   * %TRUE if the print job will continue to emit status-changed
   * signals after the print data has been setn to the printer.
   */
193
194
  g_object_class_install_property (object_class,
				   PROP_TRACK_PRINT_STATUS,
195
				   g_param_spec_boolean ("track-print-status", NULL, NULL,
196
197
198
							 FALSE,
							 GTK_PARAM_READWRITE));

199
200
  /**
   * GtkPrintJob::status-changed:
Matthias Clasen's avatar
Matthias Clasen committed
201
   * @job: the `GtkPrintJob` object on which the signal was emitted
202
   *
Matthias Clasen's avatar
Matthias Clasen committed
203
204
205
206
   * Emitted when the status of a job changes.
   *
   * The signal handler can use [method@Gtk.PrintJob.get_status]
   * to obtain the new status.
207
   */
208
  signals[STATUS_CHANGED] =
Matthias Clasen's avatar
Matthias Clasen committed
209
210
211
212
213
    g_signal_new (I_("status-changed"),
		  G_TYPE_FROM_CLASS (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintJobClass, status_changed),
		  NULL, NULL,
214
		  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
215
		  G_TYPE_NONE, 0);
216
217
218
}

static void
219
gtk_print_job_init (GtkPrintJob *job)
220
{
221
  job->spool_io = NULL;
222

223
224
225
226
  job->title = g_strdup ("");
  job->surface = NULL;
  job->backend = NULL;
  job->printer = NULL;
227

228
229
230
231
232
  job->printer_set = FALSE;
  job->settings_set = FALSE;
  job->page_setup_set = FALSE;
  job->status = GTK_PRINT_STATUS_INITIAL;
  job->track_print_status = FALSE;
233

234
235
236
237
238
239
240
241
242
243
244
  job->print_pages = GTK_PRINT_PAGES_ALL;
  job->page_ranges = NULL;
  job->num_page_ranges = 0;
  job->collate = FALSE;
  job->reverse = FALSE;
  job->num_copies = 1;
  job->scale = 1.0;
  job->page_set = GTK_PAGE_SET_ALL;
  job->rotate_to_orientation = FALSE;
  job->number_up = 1;
  job->number_up_layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
245
246
247
}


248
249
static void
gtk_print_job_constructed (GObject *object)
250
{
251
  GtkPrintJob *job = GTK_PRINT_JOB (object);
252

253
  G_OBJECT_CLASS (gtk_print_job_parent_class)->constructed (object);
254

255
256
257
  g_assert (job->printer_set &&
	    job->settings_set &&
	    job->page_setup_set);
258
  
259
  _gtk_printer_prepare_for_print (job->printer,
260
				  job,
261
262
				  job->settings,
				  job->page_setup);
263
264
265
266
267
268
}


static void
gtk_print_job_finalize (GObject *object)
{
269
  GtkPrintJob *job = GTK_PRINT_JOB (object);
270

271
272
  if (job->surface)
    cairo_surface_destroy (job->surface);
273

274
275
  if (job->backend)
    g_object_unref (job->backend);
276

277
  if (job->spool_io != NULL)
278
    {
279
280
      g_io_channel_unref (job->spool_io);
      job->spool_io = NULL;
281
    }
282

283
284
  if (job->printer)
    g_object_unref (job->printer);
285

286
287
  if (job->settings)
    g_object_unref (job->settings);
288

289
290
  if (job->page_setup)
    g_object_unref (job->page_setup);
291

292
293
  g_free (job->page_ranges);
  job->page_ranges = NULL;
294

295
296
  g_free (job->title);
  job->title = NULL;
297
298

  G_OBJECT_CLASS (gtk_print_job_parent_class)->finalize (object);
299
300
301
302
}

/**
 * gtk_print_job_new:
303
 * @title: the job title
Matthias Clasen's avatar
Matthias Clasen committed
304
305
306
 * @printer: a `GtkPrinter`
 * @settings: a `GtkPrintSettings`
 * @page_setup: a `GtkPageSetup`
307
 *
Matthias Clasen's avatar
Matthias Clasen committed
308
 * Creates a new `GtkPrintJob`.
309
 *
Matthias Clasen's avatar
Matthias Clasen committed
310
311
 * Returns: a new `GtkPrintJob`
 */
312
GtkPrintJob *
Benjamin Otte's avatar
Benjamin Otte committed
313
gtk_print_job_new (const char       *title,
314
		   GtkPrinter       *printer,
315
		   GtkPrintSettings *settings,
316
		   GtkPageSetup     *page_setup)
317
318
319
320
321
322
323
324
325
326
327
{
  GObject *result;
  result = g_object_new (GTK_TYPE_PRINT_JOB,
                         "title", title,
			 "printer", printer,
			 "settings", settings,
			 "page-setup", page_setup,
			 NULL);
  return (GtkPrintJob *) result;
}

328
/**
Matthias Clasen's avatar
Matthias Clasen committed
329
330
331
332
333
 * gtk_print_job_get_settings: (attributes org.gtk.Method.get_property=settings)
 * @job: a `GtkPrintJob`
 *
 * Gets the `GtkPrintSettings` of the print job.
 *
334
 * Returns: (transfer none): the settings of @job
335
 */
336
GtkPrintSettings *
337
gtk_print_job_get_settings (GtkPrintJob *job)
338
{
339
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
340
  
341
  return job->settings;
342
343
}

344
/**
Matthias Clasen's avatar
Matthias Clasen committed
345
346
347
348
349
 * gtk_print_job_get_printer: (attributes org.gtk.Method.get_property=printer)
 * @job: a `GtkPrintJob`
 *
 * Gets the `GtkPrinter` of the print job.
 *
350
 * Returns: (transfer none): the printer of @job
351
 */
352
GtkPrinter *
353
gtk_print_job_get_printer (GtkPrintJob *job)
354
{
355
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
356
  
357
  return job->printer;
358
359
}

360
/**
Matthias Clasen's avatar
Matthias Clasen committed
361
362
363
 * gtk_print_job_get_title: (attributes org.gtk.Method.get_property=title)
 * @job: a `GtkPrintJob`
 *
364
 * Gets the job title.
Matthias Clasen's avatar
Matthias Clasen committed
365
 *
366
 * Returns: the title of @job
367
 */
Benjamin Otte's avatar
Benjamin Otte committed
368
const char *
369
gtk_print_job_get_title (GtkPrintJob *job)
370
{
371
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
372
  
373
  return job->title;
374
375
}

376
377
/**
 * gtk_print_job_get_status:
Matthias Clasen's avatar
Matthias Clasen committed
378
379
 * @job: a `GtkPrintJob`
 *
380
 * Gets the status of the print job.
Matthias Clasen's avatar
Matthias Clasen committed
381
 *
382
 * Returns: the status of @job
383
 */
384
GtkPrintStatus
385
gtk_print_job_get_status (GtkPrintJob *job)
386
{
387
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), GTK_PRINT_STATUS_FINISHED);
388
  
389
  return job->status;
390
391
392
393
394
395
}

void
gtk_print_job_set_status (GtkPrintJob   *job,
			  GtkPrintStatus status)
{
396
397
  g_return_if_fail (GTK_IS_PRINT_JOB (job));

398
  if (job->status == status)
399
400
    return;

401
  job->status = status;
402
403
404
  g_signal_emit (job, signals[STATUS_CHANGED], 0);
}

Matthias Clasen's avatar
Matthias Clasen committed
405
406
/**
 * gtk_print_job_set_source_file:
Matthias Clasen's avatar
Matthias Clasen committed
407
 * @job: a `GtkPrintJob`
408
 * @filename: (type filename): the file to be printed
Matthias Clasen's avatar
Matthias Clasen committed
409
 * @error: return location for errors
Matthias Clasen's avatar
Matthias Clasen committed
410
411
412
413
414
415
416
417
418
 *
 * Make the `GtkPrintJob` send an existing document to the
 * printing system.
 *
 * The file can be in any format understood by the platforms
 * printing system (typically PostScript, but on many platforms
 * PDF may work too). See [method@Gtk.Printer.accepts_pdf] and
 * [method@Gtk.Printer.accepts_ps].
 *
Matthias Clasen's avatar
Matthias Clasen committed
419
 * Returns: %FALSE if an error occurred
Matthias Clasen's avatar
Matthias Clasen committed
420
 */
421
422
gboolean
gtk_print_job_set_source_file (GtkPrintJob *job,
Benjamin Otte's avatar
Benjamin Otte committed
423
			       const char *filename,
424
425
			       GError     **error)
{
426
427
428
  GError *tmp_error;

  tmp_error = NULL;
429

430
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
431

432
433
434
  if (job->spool_io != NULL)
    g_io_channel_unref (job->spool_io);

435
  job->spool_io = g_io_channel_new_file (filename, "r", &tmp_error);
436
437

  if (tmp_error == NULL)
438
    g_io_channel_set_encoding (job->spool_io, NULL, &tmp_error);
439

440
441
442
  if (tmp_error != NULL)
    {
      g_propagate_error (error, tmp_error);
443
444
      return FALSE;
    }
445
446

  return TRUE;
447
448
}

449
450
/**
 * gtk_print_job_set_source_fd:
Matthias Clasen's avatar
Matthias Clasen committed
451
 * @job: a `GtkPrintJob`
452
453
454
 * @fd: a file descriptor
 * @error: return location for errors
 *
Matthias Clasen's avatar
Matthias Clasen committed
455
456
457
458
459
460
461
 * Make the `GtkPrintJob` send an existing document to the
 * printing system.
 *
 * The file can be in any format understood by the platforms
 * printing system (typically PostScript, but on many platforms
 * PDF may work too). See [method@Gtk.Printer.accepts_pdf] and
 * [method@Gtk.Printer.accepts_ps].
462
 *
Matthias Clasen's avatar
Matthias Clasen committed
463
 * This is similar to [method@Gtk.PrintJob.set_source_file],
464
465
466
467
468
469
470
471
472
473
474
475
476
 * but takes expects an open file descriptor for the file,
 * instead of a filename.
 *
 * Returns: %FALSE if an error occurred
 */
gboolean
gtk_print_job_set_source_fd (GtkPrintJob  *job,
                             int           fd,
                             GError      **error)
{
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
  g_return_val_if_fail (fd >= 0, FALSE);

477
478
479
  if (job->spool_io != NULL)
    g_io_channel_unref (job->spool_io);

480
481
  job->spool_io = g_io_channel_unix_new (fd);
  if (g_io_channel_set_encoding (job->spool_io, NULL, error) != G_IO_STATUS_NORMAL)
482
483
484
485
486
    return FALSE;

  return TRUE;
}

487
488
/**
 * gtk_print_job_get_surface:
Matthias Clasen's avatar
Matthias Clasen committed
489
 * @job: a `GtkPrintJob`
Matthias Clasen's avatar
Matthias Clasen committed
490
 * @error: (nullable): return location for errors
Matthias Clasen's avatar
Matthias Clasen committed
491
 *
492
493
 * Gets a cairo surface onto which the pages of
 * the print job should be rendered.
Matthias Clasen's avatar
Matthias Clasen committed
494
 *
495
 * Returns: (transfer none): the cairo surface of @job
Matthias Clasen's avatar
Matthias Clasen committed
496
 */
497
cairo_surface_t *
498
499
gtk_print_job_get_surface (GtkPrintJob  *job,
			   GError      **error)
500
{
Benjamin Otte's avatar
Benjamin Otte committed
501
  char *filename = NULL;
502
  double width, height;
503
  GtkPaperSize *paper_size;
504
505
506
507
508
  int fd;
  GError *tmp_error;

  tmp_error = NULL;

509
510
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);

511
512
  if (job->surface)
    return job->surface;
513
 
514
  g_return_val_if_fail (job->spool_io == NULL, NULL);
515
 
516
517
518
519
520
  fd = g_file_open_tmp ("gtkprint_XXXXXX", 
			 &filename, 
			 &tmp_error);
  if (fd == -1)
    {
521
      g_free (filename);
522
523
524
      g_propagate_error (error, tmp_error);
      return NULL;
    }
525

526
527
  fchmod (fd, S_IRUSR | S_IWUSR);
  
Matthias Clasen's avatar
Matthias Clasen committed
528
#ifdef G_ENABLE_DEBUG
529
  /* If we are debugging printing don't delete the tmp files */
Matthias Clasen's avatar
Matthias Clasen committed
530
531
  if (GTK_DEBUG_CHECK (PRINTING)) ;
  else
532
#endif /* G_ENABLE_DEBUG */
533
  g_unlink (filename);
534
  g_free (filename);
535

536
  paper_size = gtk_page_setup_get_paper_size (job->page_setup);
537
538
  width = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
  height = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
539
 
540
541
542
  job->spool_io = g_io_channel_unix_new (fd);
  g_io_channel_set_close_on_unref (job->spool_io, TRUE);
  g_io_channel_set_encoding (job->spool_io, NULL, &tmp_error);
543
  
544
545
  if (tmp_error != NULL)
    {
546
547
      g_io_channel_unref (job->spool_io);
      job->spool_io = NULL;
548
549
550
551
      g_propagate_error (error, tmp_error);
      return NULL;
    }

552
553
  job->surface = _gtk_printer_create_cairo_surface (job->printer,
						     job->settings,
Matthias Clasen's avatar
Matthias Clasen committed
554
						     width, height,
555
						     job->spool_io);
556
  
557
  return job->surface;
558
559
}

560
/**
Matthias Clasen's avatar
Matthias Clasen committed
561
562
 * gtk_print_job_set_track_print_status: (attributes org.gtk.Method.set_property=track-print-status)
 * @job: a `GtkPrintJob`
563
 * @track_status: %TRUE to track status after printing
Matthias Clasen's avatar
Matthias Clasen committed
564
 *
565
 * If track_status is %TRUE, the print job will try to continue report
Matthias Clasen's avatar
Matthias Clasen committed
566
567
568
569
570
571
572
 * on the status of the print job in the printer queues and printer.
 *
 * This can allow your application to show things like “out of paper”
 * issues, and when the print job actually reaches the printer.
 *
 * This function is often implemented using some form of polling,
 * so it should not be enabled unless needed.
573
574
575
 */
void
gtk_print_job_set_track_print_status (GtkPrintJob *job,
Matthias Clasen's avatar
Matthias Clasen committed
576
				      gboolean     track_status)
577
578
579
{
  g_return_if_fail (GTK_IS_PRINT_JOB (job));

Matthias Clasen's avatar
Matthias Clasen committed
580
581
  track_status = track_status != FALSE;

582
  if (job->track_print_status != track_status)
Matthias Clasen's avatar
Matthias Clasen committed
583
    {
584
      job->track_print_status = track_status;
Matthias Clasen's avatar
Matthias Clasen committed
585
      
586
      g_object_notify (G_OBJECT (job), "track-print-status");
Matthias Clasen's avatar
Matthias Clasen committed
587
    }
588
589
590
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
591
592
 * gtk_print_job_get_track_print_status: (attributes org.gtk.Method.get_property=track-print-status)
 * @job: a `GtkPrintJob`
593
 *
594
 * Returns whether jobs will be tracked after printing.
Matthias Clasen's avatar
Matthias Clasen committed
595
596
 *
 * For details, see [method@Gtk.PrintJob.set_track_print_status].
597
 *
598
 * Returns: %TRUE if print job status will be reported after printing
599
600
601
602
603
604
 */
gboolean
gtk_print_job_get_track_print_status (GtkPrintJob *job)
{
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
  
605
  return job->track_print_status;
606
607
}

608
609
610
611
612
613
614
615
616
617
618
619
static void
gtk_print_job_set_property (GObject      *object,
	                    guint         prop_id,
	                    const GValue *value,
                            GParamSpec   *pspec)

{
  GtkPrintJob *job = GTK_PRINT_JOB (object);
  GtkPrintSettings *settings;

  switch (prop_id)
    {
620
    case PROP_TITLE:
621
622
      g_free (job->title);
      job->title = g_value_dup_string (value);
623
624
      break;
    
625
    case PROP_PRINTER:
626
627
628
      job->printer = GTK_PRINTER (g_value_dup_object (value));
      job->printer_set = TRUE;
      job->backend = g_object_ref (gtk_printer_get_backend (job->printer));
629
630
      break;

631
    case PROP_PAGE_SETUP:
632
633
      job->page_setup = GTK_PAGE_SETUP (g_value_dup_object (value));
      job->page_setup_set = TRUE;
634
635
      break;
      
636
    case PROP_SETTINGS:
637
638
639
      /* We save a copy of the settings since we modify
       * if when preparing the printer job. */
      settings = GTK_PRINT_SETTINGS (g_value_get_object (value));
640
641
      job->settings = gtk_print_settings_copy (settings);
      job->settings_set = TRUE;
642
643
      break;

644
645
646
647
    case PROP_TRACK_PRINT_STATUS:
      gtk_print_job_set_track_print_status (job, g_value_get_boolean (value));
      break;

648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_print_job_get_property (GObject    *object,
			    guint       prop_id,
			    GValue     *value,
			    GParamSpec *pspec)
{
  GtkPrintJob *job = GTK_PRINT_JOB (object);

  switch (prop_id)
    {
664
    case PROP_TITLE:
665
      g_value_set_string (value, job->title);
666
      break;
667
    case PROP_PRINTER:
668
      g_value_set_object (value, job->printer);
669
      break;
670
    case PROP_SETTINGS:
671
      g_value_set_object (value, job->settings);
672
      break;
673
    case PROP_PAGE_SETUP:
674
      g_value_set_object (value, job->page_setup);
675
      break;
676
    case PROP_TRACK_PRINT_STATUS:
677
      g_value_set_boolean (value, job->track_print_status);
678
      break;
679
680
681
682
683
684
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

685
686
/**
 * gtk_print_job_send:
Matthias Clasen's avatar
Matthias Clasen committed
687
 * @job: a `GtkPrintJob`
688
 * @callback: function to call when the job completes or an error occurs
689
 * @user_data: (closure): user data that gets passed to @callback
690
 * @dnotify: destroy notify for @user_data
Matthias Clasen's avatar
Matthias Clasen committed
691
692
693
 *
 * Sends the print job off to the printer.
 */
694
void
695
696
697
gtk_print_job_send (GtkPrintJob             *job,
                    GtkPrintJobCompleteFunc  callback,
                    gpointer                 user_data,
698
		    GDestroyNotify           dnotify)
699
{
700
  g_return_if_fail (GTK_IS_PRINT_JOB (job));
701
  g_return_if_fail (job->spool_io != NULL);
702
703
  
  gtk_print_job_set_status (job, GTK_PRINT_STATUS_SENDING_DATA);
704
  
705
  g_io_channel_seek_position (job->spool_io, 0, G_SEEK_SET, NULL);
706
  
707
708
  gtk_print_backend_print_stream (job->backend, job,
				  job->spool_io,
709
                                  callback, user_data, dnotify);
710
}
711
712
713

/**
 * gtk_print_job_get_pages:
Matthias Clasen's avatar
Matthias Clasen committed
714
 * @job: a `GtkPrintJob`
715
 *
Matthias Clasen's avatar
Matthias Clasen committed
716
 * Gets the `GtkPrintPages` setting for this job.
717
 *
Matthias Clasen's avatar
Matthias Clasen committed
718
 * Returns: the `GtkPrintPages` setting
719
720
721
722
 */
GtkPrintPages
gtk_print_job_get_pages (GtkPrintJob *job)
{
723
  return job->print_pages;
724
725
726
727
}

/**
 * gtk_print_job_set_pages:
Matthias Clasen's avatar
Matthias Clasen committed
728
729
 * @job: a `GtkPrintJob`
 * @pages: the `GtkPrintPages` setting
730
 *
Matthias Clasen's avatar
Matthias Clasen committed
731
 * Sets the `GtkPrintPages` setting for this job.
732
733
734
735
736
 */
void
gtk_print_job_set_pages (GtkPrintJob   *job,
                         GtkPrintPages  pages)
{
737
  job->print_pages = pages;
738
739
740
741
}

/**
 * gtk_print_job_get_page_ranges:
Matthias Clasen's avatar
Matthias Clasen committed
742
 * @job: a `GtkPrintJob`
743
744
745
746
 * @n_ranges: (out): return location for the number of ranges
 *
 * Gets the page ranges for this job.
 *
747
 * Returns: (array length=n_ranges) (transfer none): a pointer to an
Matthias Clasen's avatar
Matthias Clasen committed
748
 *   array of `GtkPageRange` structs
749
750
751
 */
GtkPageRange *
gtk_print_job_get_page_ranges (GtkPrintJob *job,
Benjamin Otte's avatar
Benjamin Otte committed
752
                               int         *n_ranges)
753
{
754
755
  *n_ranges = job->num_page_ranges;
  return job->page_ranges;
756
757
758
759
}

/**
 * gtk_print_job_set_page_ranges:
Matthias Clasen's avatar
Matthias Clasen committed
760
 * @job: a `GtkPrintJob`
761
 * @ranges: (array length=n_ranges) (transfer full): pointer to an array of
Matthias Clasen's avatar
Matthias Clasen committed
762
 *    `GtkPageRange` structs
763
764
765
766
767
768
769
 * @n_ranges: the length of the @ranges array
 *
 * Sets the page ranges for this job.
 */
void
gtk_print_job_set_page_ranges (GtkPrintJob  *job,
                               GtkPageRange *ranges,
Benjamin Otte's avatar
Benjamin Otte committed
770
                               int           n_ranges)
771
{
772
773
774
  g_free (job->page_ranges);
  job->page_ranges = ranges;
  job->num_page_ranges = n_ranges;
775
776
777
778
}

/**
 * gtk_print_job_get_page_set:
Matthias Clasen's avatar
Matthias Clasen committed
779
 * @job: a `GtkPrintJob`
780
 *
Matthias Clasen's avatar
Matthias Clasen committed
781
 * Gets the `GtkPageSet` setting for this job.
782
 *
Matthias Clasen's avatar
Matthias Clasen committed
783
 * Returns: the `GtkPageSet` setting
784
785
786
787
 */
GtkPageSet
gtk_print_job_get_page_set (GtkPrintJob *job)
{
788
  return job->page_set;
789
790
791
792
}

/**
 * gtk_print_job_set_page_set:
Matthias Clasen's avatar
Matthias Clasen committed
793
794
 * @job: a `GtkPrintJob`
 * @page_set: a `GtkPageSet` setting
795
 *
Matthias Clasen's avatar
Matthias Clasen committed
796
 * Sets the `GtkPageSet` setting for this job.
797
798
799
800
801
 */
void
gtk_print_job_set_page_set (GtkPrintJob *job,
                            GtkPageSet   page_set)
{
802
  job->page_set = page_set;
803
804
805
806
}

/**
 * gtk_print_job_get_num_copies:
Matthias Clasen's avatar
Matthias Clasen committed
807
 * @job: a `GtkPrintJob`
808
809
810
811
812
 *
 * Gets the number of copies of this job.
 *
 * Returns: the number of copies
 */
Benjamin Otte's avatar
Benjamin Otte committed
813
int
814
815
gtk_print_job_get_num_copies (GtkPrintJob *job)
{
816
  return job->num_copies;
817
818
819
820
}

/**
 * gtk_print_job_set_num_copies:
Matthias Clasen's avatar
Matthias Clasen committed
821
 * @job: a `GtkPrintJob`
822
823
824
825
826
827
 * @num_copies: the number of copies
 *
 * Sets the number of copies for this job.
 */
void
gtk_print_job_set_num_copies (GtkPrintJob *job,
Benjamin Otte's avatar
Benjamin Otte committed
828
                              int          num_copies)
829
{
830
  job->num_copies = num_copies;
831
832
833
834
}

/**
 * gtk_print_job_get_scale:
Matthias Clasen's avatar
Matthias Clasen committed
835
 * @job: a `GtkPrintJob`
836
 *
Matthias Clasen's avatar
Matthias Clasen committed
837
 * Gets the scale for this job.
838
839
840
 *
 * Returns: the scale
 */
841
double
842
843
844
gtk_print_job_get_scale (GtkPrintJob *job)

{
845
  return job->scale;
846
847
848
849
}

/**
 * gtk_print_job_set_scale:
Matthias Clasen's avatar
Matthias Clasen committed
850
 * @job: a `GtkPrintJob`
851
852
 * @scale: the scale
 *
Matthias Clasen's avatar
Matthias Clasen committed
853
854
855
 * Sets the scale for this job.
 *
 * 1.0 means unscaled.
856
857
858
 */
void
gtk_print_job_set_scale (GtkPrintJob *job,
859
                         double       scale)
860
{
861
  job->scale = scale;
862
863
864
865
}

/**
 * gtk_print_job_get_n_up:
Matthias Clasen's avatar
Matthias Clasen committed
866
 * @job: a `GtkPrintJob`
867
868
869
870
871
872
873
874
 *
 * Gets the n-up setting for this job.
 *
 * Returns: the n-up setting
 */
guint
gtk_print_job_get_n_up (GtkPrintJob *job)
{
875
  return job->number_up;
876
877
878
879
}

/**
 * gtk_print_job_set_n_up:
Matthias Clasen's avatar
Matthias Clasen committed
880
 * @job: a `GtkPrintJob`
881
882
883
884
885
886
887
888
 * @n_up: the n-up value
 *
 * Sets the n-up setting for this job.
 */
void
gtk_print_job_set_n_up (GtkPrintJob *job,
                        guint        n_up)
{
889
  job->number_up = n_up;
890
891
892
893
}

/**
 * gtk_print_job_get_n_up_layout:
Matthias Clasen's avatar
Matthias Clasen committed
894
 * @job: a `GtkPrintJob`
895
896
897
898
899
900
901
902
 *
 * Gets the n-up layout setting for this job.
 *
 * Returns: the n-up layout
 */
GtkNumberUpLayout
gtk_print_job_get_n_up_layout (GtkPrintJob *job)
{
903
  return job->number_up_layout;
904
905
906
907
}

/**
 * gtk_print_job_set_n_up_layout:
Matthias Clasen's avatar
Matthias Clasen committed
908
 * @job: a `GtkPrintJob`
909
910
911
912
913
914
915
916
 * @layout: the n-up layout setting
 *
 * Sets the n-up layout setting for this job.
 */
void
gtk_print_job_set_n_up_layout (GtkPrintJob       *job,
                               GtkNumberUpLayout  layout)
{
917
  job->number_up_layout = layout;
918
919
920
921
}

/**
 * gtk_print_job_get_rotate:
Matthias Clasen's avatar
Matthias Clasen committed
922
 * @job: a `GtkPrintJob`
923
924
925
926
927
928
929
930
 *
 * Gets whether the job is printed rotated.
 *
 * Returns: whether the job is printed rotated
 */
gboolean
gtk_print_job_get_rotate (GtkPrintJob *job)
{
931
  return job->rotate_to_orientation;
932
933
934
935
}

/**
 * gtk_print_job_set_rotate:
Matthias Clasen's avatar
Matthias Clasen committed
936
 * @job: a `GtkPrintJob`
937
938
939
940
941
942
943
944
 * @rotate: whether to print rotated
 *
 * Sets whether this job is printed rotated.
 */
void
gtk_print_job_set_rotate (GtkPrintJob *job,
                          gboolean     rotate)
{
945
  job->rotate_to_orientation = rotate;
946
947
948
949
}

/**
 * gtk_print_job_get_collate:
Matthias Clasen's avatar
Matthias Clasen committed
950
 * @job: a `GtkPrintJob`
951
952
953
954
955
956
957
958
 *
 * Gets whether this job is printed collated.
 *
 * Returns: whether the job is printed collated
 */
gboolean
gtk_print_job_get_collate (GtkPrintJob *job)
{
959
  return job->collate;
960
961
962
}

/**
963
 * gtk_print_job_set_collate:
Matthias Clasen's avatar
Matthias Clasen committed
964
 * @job: a `GtkPrintJob`
965
966
967
968
969
970
971
972
 * @collate: whether the job is printed collated
 *
 * Sets whether this job is printed collated.
 */
void
gtk_print_job_set_collate (GtkPrintJob *job,
                           gboolean     collate)
{
973
  job->collate = collate;
974
975
976
977
}

/**
 * gtk_print_job_get_reverse:
Matthias Clasen's avatar
Matthias Clasen committed
978
 * @job: a `GtkPrintJob`
979
980
981
982
983
984
985
986
 *
 * Gets whether this job is printed reversed.
 *
 * Returns: whether the job is printed reversed.
 */
gboolean
gtk_print_job_get_reverse (GtkPrintJob *job)
{
987
  return job->reverse;
988
989
990
991
}

/**
 * gtk_print_job_set_reverse:
Matthias Clasen's avatar
Matthias Clasen committed
992
 * @job: a `GtkPrintJob`
993
994
995
996
997
998
999
1000
 * @reverse: whether the job is printed reversed
 *
 * Sets whether this job is printed reversed.
 */
void
gtk_print_job_set_reverse (GtkPrintJob *job,
                           gboolean     reverse)
{
1001
  job->reverse = reverse;
1002
}