gtkprintjob.c 24 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
147
148
149
150
151
152
                                   g_param_spec_string ("title",
						        P_("Title"),
						        P_("Title of the print job"),
						        NULL,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

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

Matthias Clasen's avatar
Matthias Clasen committed
167
168
169
170
171
  /**
   * GtkPrintJob:settings: (attributes org.gtk.Property.get=gtk_print_job_get_settings)
   *
   * Printer settings.
   */
172
173
  g_object_class_install_property (object_class,
                                   PROP_SETTINGS,
174
175
176
177
178
179
180
                                   g_param_spec_object ("settings",
						        P_("Settings"),
						        P_("Printer settings"),
						        GTK_TYPE_PRINT_SETTINGS,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

Matthias Clasen's avatar
Matthias Clasen committed
181
182
183
184
185
  /**
   * GtkPrintJob:page-setup:
   *
   * Page setup.
   */
186
187
  g_object_class_install_property (object_class,
                                   PROP_PAGE_SETUP,
188
189
190
191
192
193
194
                                   g_param_spec_object ("page-setup",
						        P_("Page Setup"),
						        P_("Page Setup"),
						        GTK_TYPE_PAGE_SETUP,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

Matthias Clasen's avatar
Matthias Clasen committed
195
196
197
198
199
200
  /**
   * 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.
   */
201
202
203
204
205
206
207
208
209
210
  g_object_class_install_property (object_class,
				   PROP_TRACK_PRINT_STATUS,
				   g_param_spec_boolean ("track-print-status",
							 P_("Track Print Status"),
							 P_("TRUE if the print job will continue to emit "
							    "status-changed signals after the print data "
							    "has been sent to the printer or print server."),
							 FALSE,
							 GTK_PARAM_READWRITE));

211
212
  /**
   * GtkPrintJob::status-changed:
Matthias Clasen's avatar
Matthias Clasen committed
213
   * @job: the `GtkPrintJob` object on which the signal was emitted
214
   *
Matthias Clasen's avatar
Matthias Clasen committed
215
216
217
218
   * Emitted when the status of a job changes.
   *
   * The signal handler can use [method@Gtk.PrintJob.get_status]
   * to obtain the new status.
219
   */
220
  signals[STATUS_CHANGED] =
Matthias Clasen's avatar
Matthias Clasen committed
221
222
223
224
225
    g_signal_new (I_("status-changed"),
		  G_TYPE_FROM_CLASS (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintJobClass, status_changed),
		  NULL, NULL,
226
		  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
227
		  G_TYPE_NONE, 0);
228
229
230
}

static void
231
gtk_print_job_init (GtkPrintJob *job)
232
{
233
  job->spool_io = NULL;
234

235
236
237
238
  job->title = g_strdup ("");
  job->surface = NULL;
  job->backend = NULL;
  job->printer = NULL;
239

240
241
242
243
244
  job->printer_set = FALSE;
  job->settings_set = FALSE;
  job->page_setup_set = FALSE;
  job->status = GTK_PRINT_STATUS_INITIAL;
  job->track_print_status = FALSE;
245

246
247
248
249
250
251
252
253
254
255
256
  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;
257
258
259
}


260
261
static void
gtk_print_job_constructed (GObject *object)
262
{
263
  GtkPrintJob *job = GTK_PRINT_JOB (object);
264

265
  G_OBJECT_CLASS (gtk_print_job_parent_class)->constructed (object);
266

267
268
269
  g_assert (job->printer_set &&
	    job->settings_set &&
	    job->page_setup_set);
270
  
271
  _gtk_printer_prepare_for_print (job->printer,
272
				  job,
273
274
				  job->settings,
				  job->page_setup);
275
276
277
278
279
280
}


static void
gtk_print_job_finalize (GObject *object)
{
281
  GtkPrintJob *job = GTK_PRINT_JOB (object);
282

283
284
  if (job->surface)
    cairo_surface_destroy (job->surface);
285

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

289
  if (job->spool_io != NULL)
290
    {
291
292
      g_io_channel_unref (job->spool_io);
      job->spool_io = NULL;
293
    }
294

295
296
  if (job->printer)
    g_object_unref (job->printer);
297

298
299
  if (job->settings)
    g_object_unref (job->settings);
300

301
302
  if (job->page_setup)
    g_object_unref (job->page_setup);
303

304
305
  g_free (job->page_ranges);
  job->page_ranges = NULL;
306

307
308
  g_free (job->title);
  job->title = NULL;
309
310

  G_OBJECT_CLASS (gtk_print_job_parent_class)->finalize (object);
311
312
313
314
}

/**
 * gtk_print_job_new:
315
 * @title: the job title
Matthias Clasen's avatar
Matthias Clasen committed
316
317
318
 * @printer: a `GtkPrinter`
 * @settings: a `GtkPrintSettings`
 * @page_setup: a `GtkPageSetup`
319
 *
Matthias Clasen's avatar
Matthias Clasen committed
320
 * Creates a new `GtkPrintJob`.
321
 *
Matthias Clasen's avatar
Matthias Clasen committed
322
323
 * Returns: a new `GtkPrintJob`
 */
324
GtkPrintJob *
Benjamin Otte's avatar
Benjamin Otte committed
325
gtk_print_job_new (const char       *title,
326
		   GtkPrinter       *printer,
327
		   GtkPrintSettings *settings,
328
		   GtkPageSetup     *page_setup)
329
330
331
332
333
334
335
336
337
338
339
{
  GObject *result;
  result = g_object_new (GTK_TYPE_PRINT_JOB,
                         "title", title,
			 "printer", printer,
			 "settings", settings,
			 "page-setup", page_setup,
			 NULL);
  return (GtkPrintJob *) result;
}

340
/**
Matthias Clasen's avatar
Matthias Clasen committed
341
342
343
344
345
 * gtk_print_job_get_settings: (attributes org.gtk.Method.get_property=settings)
 * @job: a `GtkPrintJob`
 *
 * Gets the `GtkPrintSettings` of the print job.
 *
346
 * Returns: (transfer none): the settings of @job
347
 */
348
GtkPrintSettings *
349
gtk_print_job_get_settings (GtkPrintJob *job)
350
{
351
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
352
  
353
  return job->settings;
354
355
}

356
/**
Matthias Clasen's avatar
Matthias Clasen committed
357
358
359
360
361
 * gtk_print_job_get_printer: (attributes org.gtk.Method.get_property=printer)
 * @job: a `GtkPrintJob`
 *
 * Gets the `GtkPrinter` of the print job.
 *
362
 * Returns: (transfer none): the printer of @job
363
 */
364
GtkPrinter *
365
gtk_print_job_get_printer (GtkPrintJob *job)
366
{
367
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
368
  
369
  return job->printer;
370
371
}

372
/**
Matthias Clasen's avatar
Matthias Clasen committed
373
374
375
 * gtk_print_job_get_title: (attributes org.gtk.Method.get_property=title)
 * @job: a `GtkPrintJob`
 *
376
 * Gets the job title.
Matthias Clasen's avatar
Matthias Clasen committed
377
 *
378
 * Returns: the title of @job
379
 */
Benjamin Otte's avatar
Benjamin Otte committed
380
const char *
381
gtk_print_job_get_title (GtkPrintJob *job)
382
{
383
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);
384
  
385
  return job->title;
386
387
}

388
389
/**
 * gtk_print_job_get_status:
Matthias Clasen's avatar
Matthias Clasen committed
390
391
 * @job: a `GtkPrintJob`
 *
392
 * Gets the status of the print job.
Matthias Clasen's avatar
Matthias Clasen committed
393
 *
394
 * Returns: the status of @job
395
 */
396
GtkPrintStatus
397
gtk_print_job_get_status (GtkPrintJob *job)
398
{
399
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), GTK_PRINT_STATUS_FINISHED);
400
  
401
  return job->status;
402
403
404
405
406
407
}

void
gtk_print_job_set_status (GtkPrintJob   *job,
			  GtkPrintStatus status)
{
408
409
  g_return_if_fail (GTK_IS_PRINT_JOB (job));

410
  if (job->status == status)
411
412
    return;

413
  job->status = status;
414
415
416
  g_signal_emit (job, signals[STATUS_CHANGED], 0);
}

Matthias Clasen's avatar
Matthias Clasen committed
417
418
/**
 * gtk_print_job_set_source_file:
Matthias Clasen's avatar
Matthias Clasen committed
419
 * @job: a `GtkPrintJob`
420
 * @filename: (type filename): the file to be printed
Matthias Clasen's avatar
Matthias Clasen committed
421
 * @error: return location for errors
Matthias Clasen's avatar
Matthias Clasen committed
422
423
424
425
426
427
428
429
430
 *
 * 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
431
 * Returns: %FALSE if an error occurred
Matthias Clasen's avatar
Matthias Clasen committed
432
 */
433
434
gboolean
gtk_print_job_set_source_file (GtkPrintJob *job,
Benjamin Otte's avatar
Benjamin Otte committed
435
			       const char *filename,
436
437
			       GError     **error)
{
438
439
440
  GError *tmp_error;

  tmp_error = NULL;
441

442
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
443

444
  job->spool_io = g_io_channel_new_file (filename, "r", &tmp_error);
445
446

  if (tmp_error == NULL)
447
    g_io_channel_set_encoding (job->spool_io, NULL, &tmp_error);
448

449
450
451
  if (tmp_error != NULL)
    {
      g_propagate_error (error, tmp_error);
452
453
      return FALSE;
    }
454
455

  return TRUE;
456
457
}

458
459
/**
 * gtk_print_job_set_source_fd:
Matthias Clasen's avatar
Matthias Clasen committed
460
 * @job: a `GtkPrintJob`
461
462
463
 * @fd: a file descriptor
 * @error: return location for errors
 *
Matthias Clasen's avatar
Matthias Clasen committed
464
465
466
467
468
469
470
 * 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].
471
 *
Matthias Clasen's avatar
Matthias Clasen committed
472
 * This is similar to [method@Gtk.PrintJob.set_source_file],
473
474
475
476
477
478
479
480
481
482
483
484
485
 * 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);

486
487
  job->spool_io = g_io_channel_unix_new (fd);
  if (g_io_channel_set_encoding (job->spool_io, NULL, error) != G_IO_STATUS_NORMAL)
488
489
490
491
492
    return FALSE;

  return TRUE;
}

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

  tmp_error = NULL;

515
516
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);

517
518
  if (job->surface)
    return job->surface;
519
 
520
  g_return_val_if_fail (job->spool_io == NULL, NULL);
521
 
522
523
524
525
526
  fd = g_file_open_tmp ("gtkprint_XXXXXX", 
			 &filename, 
			 &tmp_error);
  if (fd == -1)
    {
527
      g_free (filename);
528
529
530
      g_propagate_error (error, tmp_error);
      return NULL;
    }
531

532
533
  fchmod (fd, S_IRUSR | S_IWUSR);
  
Matthias Clasen's avatar
Matthias Clasen committed
534
#ifdef G_ENABLE_DEBUG
535
  /* If we are debugging printing don't delete the tmp files */
Matthias Clasen's avatar
Matthias Clasen committed
536
537
  if (GTK_DEBUG_CHECK (PRINTING)) ;
  else
538
#endif /* G_ENABLE_DEBUG */
539
  g_unlink (filename);
540
  g_free (filename);
541

542
  paper_size = gtk_page_setup_get_paper_size (job->page_setup);
543
544
  width = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
  height = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
545
 
546
547
548
  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);
549
  
550
551
  if (tmp_error != NULL)
    {
552
553
      g_io_channel_unref (job->spool_io);
      job->spool_io = NULL;
554
555
556
557
      g_propagate_error (error, tmp_error);
      return NULL;
    }

558
559
  job->surface = _gtk_printer_create_cairo_surface (job->printer,
						     job->settings,
Matthias Clasen's avatar
Matthias Clasen committed
560
						     width, height,
561
						     job->spool_io);
562
  
563
  return job->surface;
564
565
}

566
/**
Matthias Clasen's avatar
Matthias Clasen committed
567
568
 * gtk_print_job_set_track_print_status: (attributes org.gtk.Method.set_property=track-print-status)
 * @job: a `GtkPrintJob`
569
 * @track_status: %TRUE to track status after printing
Matthias Clasen's avatar
Matthias Clasen committed
570
 *
571
 * If track_status is %TRUE, the print job will try to continue report
Matthias Clasen's avatar
Matthias Clasen committed
572
573
574
575
576
577
578
 * 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.
579
580
581
 */
void
gtk_print_job_set_track_print_status (GtkPrintJob *job,
Matthias Clasen's avatar
Matthias Clasen committed
582
				      gboolean     track_status)
583
584
585
{
  g_return_if_fail (GTK_IS_PRINT_JOB (job));

Matthias Clasen's avatar
Matthias Clasen committed
586
587
  track_status = track_status != FALSE;

588
  if (job->track_print_status != track_status)
Matthias Clasen's avatar
Matthias Clasen committed
589
    {
590
      job->track_print_status = track_status;
Matthias Clasen's avatar
Matthias Clasen committed
591
      
592
      g_object_notify (G_OBJECT (job), "track-print-status");
Matthias Clasen's avatar
Matthias Clasen committed
593
    }
594
595
596
}

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

614
615
616
617
618
619
620
621
622
623
624
625
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)
    {
626
    case PROP_TITLE:
627
628
      g_free (job->title);
      job->title = g_value_dup_string (value);
629
630
      break;
    
631
    case PROP_PRINTER:
632
633
634
      job->printer = GTK_PRINTER (g_value_dup_object (value));
      job->printer_set = TRUE;
      job->backend = g_object_ref (gtk_printer_get_backend (job->printer));
635
636
      break;

637
    case PROP_PAGE_SETUP:
638
639
      job->page_setup = GTK_PAGE_SETUP (g_value_dup_object (value));
      job->page_setup_set = TRUE;
640
641
      break;
      
642
    case PROP_SETTINGS:
643
644
645
      /* 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));
646
647
      job->settings = gtk_print_settings_copy (settings);
      job->settings_set = TRUE;
648
649
      break;

650
651
652
653
    case PROP_TRACK_PRINT_STATUS:
      gtk_print_job_set_track_print_status (job, g_value_get_boolean (value));
      break;

654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
    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)
    {
670
    case PROP_TITLE:
671
      g_value_set_string (value, job->title);
672
      break;
673
    case PROP_PRINTER:
674
      g_value_set_object (value, job->printer);
675
      break;
676
    case PROP_SETTINGS:
677
      g_value_set_object (value, job->settings);
678
      break;
679
    case PROP_PAGE_SETUP:
680
      g_value_set_object (value, job->page_setup);
681
      break;
682
    case PROP_TRACK_PRINT_STATUS:
683
      g_value_set_boolean (value, job->track_print_status);
684
      break;
685
686
687
688
689
690
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

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

/**
 * gtk_print_job_get_pages:
Matthias Clasen's avatar
Matthias Clasen committed
720
 * @job: a `GtkPrintJob`
721
 *
Matthias Clasen's avatar
Matthias Clasen committed
722
 * Gets the `GtkPrintPages` setting for this job.
723
 *
Matthias Clasen's avatar
Matthias Clasen committed
724
 * Returns: the `GtkPrintPages` setting
725
726
727
728
 */
GtkPrintPages
gtk_print_job_get_pages (GtkPrintJob *job)
{
729
  return job->print_pages;
730
731
732
733
}

/**
 * gtk_print_job_set_pages:
Matthias Clasen's avatar
Matthias Clasen committed
734
735
 * @job: a `GtkPrintJob`
 * @pages: the `GtkPrintPages` setting
736
 *
Matthias Clasen's avatar
Matthias Clasen committed
737
 * Sets the `GtkPrintPages` setting for this job.
738
739
740
741
742
 */
void
gtk_print_job_set_pages (GtkPrintJob   *job,
                         GtkPrintPages  pages)
{
743
  job->print_pages = pages;
744
745
746
747
}

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

/**
 * gtk_print_job_set_page_ranges:
Matthias Clasen's avatar
Matthias Clasen committed
766
 * @job: a `GtkPrintJob`
767
 * @ranges: (array length=n_ranges) (transfer full): pointer to an array of
Matthias Clasen's avatar
Matthias Clasen committed
768
 *    `GtkPageRange` structs
769
770
771
772
773
774
775
 * @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
776
                               int           n_ranges)
777
{
778
779
780
  g_free (job->page_ranges);
  job->page_ranges = ranges;
  job->num_page_ranges = n_ranges;
781
782
783
784
}

/**
 * gtk_print_job_get_page_set:
Matthias Clasen's avatar
Matthias Clasen committed
785
 * @job: a `GtkPrintJob`
786
 *
Matthias Clasen's avatar
Matthias Clasen committed
787
 * Gets the `GtkPageSet` setting for this job.
788
 *
Matthias Clasen's avatar
Matthias Clasen committed
789
 * Returns: the `GtkPageSet` setting
790
791
792
793
 */
GtkPageSet
gtk_print_job_get_page_set (GtkPrintJob *job)
{
794
  return job->page_set;
795
796
797
798
}

/**
 * gtk_print_job_set_page_set:
Matthias Clasen's avatar
Matthias Clasen committed
799
800
 * @job: a `GtkPrintJob`
 * @page_set: a `GtkPageSet` setting
801
 *
Matthias Clasen's avatar
Matthias Clasen committed
802
 * Sets the `GtkPageSet` setting for this job.
803
804
805
806
807
 */
void
gtk_print_job_set_page_set (GtkPrintJob *job,
                            GtkPageSet   page_set)
{
808
  job->page_set = page_set;
809
810
811
812
}

/**
 * gtk_print_job_get_num_copies:
Matthias Clasen's avatar
Matthias Clasen committed
813
 * @job: a `GtkPrintJob`
814
815
816
817
818
 *
 * Gets the number of copies of this job.
 *
 * Returns: the number of copies
 */
Benjamin Otte's avatar
Benjamin Otte committed
819
int
820
821
gtk_print_job_get_num_copies (GtkPrintJob *job)
{
822
  return job->num_copies;
823
824
825
826
}

/**
 * gtk_print_job_set_num_copies:
Matthias Clasen's avatar
Matthias Clasen committed
827
 * @job: a `GtkPrintJob`
828
829
830
831
832
833
 * @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
834
                              int          num_copies)
835
{
836
  job->num_copies = num_copies;
837
838
839
840
}

/**
 * gtk_print_job_get_scale:
Matthias Clasen's avatar
Matthias Clasen committed
841
 * @job: a `GtkPrintJob`
842
 *
Matthias Clasen's avatar
Matthias Clasen committed
843
 * Gets the scale for this job.
844
845
846
 *
 * Returns: the scale
 */
847
double
848
849
850
gtk_print_job_get_scale (GtkPrintJob *job)

{
851
  return job->scale;
852
853
854
855
}

/**
 * gtk_print_job_set_scale:
Matthias Clasen's avatar
Matthias Clasen committed
856
 * @job: a `GtkPrintJob`
857
858
 * @scale: the scale
 *
Matthias Clasen's avatar
Matthias Clasen committed
859
860
861
 * Sets the scale for this job.
 *
 * 1.0 means unscaled.
862
863
864
 */
void
gtk_print_job_set_scale (GtkPrintJob *job,
865
                         double       scale)
866
{
867
  job->scale = scale;
868
869
870
871
}

/**
 * gtk_print_job_get_n_up:
Matthias Clasen's avatar
Matthias Clasen committed
872
 * @job: a `GtkPrintJob`
873
874
875
876
877
878
879
880
 *
 * Gets the n-up setting for this job.
 *
 * Returns: the n-up setting
 */
guint
gtk_print_job_get_n_up (GtkPrintJob *job)
{
881
  return job->number_up;
882
883
884
885
}

/**
 * gtk_print_job_set_n_up:
Matthias Clasen's avatar
Matthias Clasen committed
886
 * @job: a `GtkPrintJob`
887
888
889
890
891
892
893
894
 * @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)
{
895
  job->number_up = n_up;
896
897
898
899
}

/**
 * gtk_print_job_get_n_up_layout:
Matthias Clasen's avatar
Matthias Clasen committed
900
 * @job: a `GtkPrintJob`
901
902
903
904
905
906
907
908
 *
 * Gets the n-up layout setting for this job.
 *
 * Returns: the n-up layout
 */
GtkNumberUpLayout
gtk_print_job_get_n_up_layout (GtkPrintJob *job)
{
909
  return job->number_up_layout;
910
911
912
913
}

/**
 * gtk_print_job_set_n_up_layout:
Matthias Clasen's avatar
Matthias Clasen committed
914
 * @job: a `GtkPrintJob`
915
916
917
918
919
920
921
922
 * @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)
{
923
  job->number_up_layout = layout;
924
925
926
927
}

/**
 * gtk_print_job_get_rotate:
Matthias Clasen's avatar
Matthias Clasen committed
928
 * @job: a `GtkPrintJob`
929
930
931
932
933
934
935
936
 *
 * Gets whether the job is printed rotated.
 *
 * Returns: whether the job is printed rotated
 */
gboolean
gtk_print_job_get_rotate (GtkPrintJob *job)
{
937
  return job->rotate_to_orientation;
938
939
940
941
}

/**
 * gtk_print_job_set_rotate:
Matthias Clasen's avatar
Matthias Clasen committed
942
 * @job: a `GtkPrintJob`
943
944
945
946
947
948
949
950
 * @rotate: whether to print rotated
 *
 * Sets whether this job is printed rotated.
 */
void
gtk_print_job_set_rotate (GtkPrintJob *job,
                          gboolean     rotate)
{
951
  job->rotate_to_orientation = rotate;
952
953
954
955
}

/**
 * gtk_print_job_get_collate:
Matthias Clasen's avatar
Matthias Clasen committed
956
 * @job: a `GtkPrintJob`
957
958
959
960
961
962
963
964
 *
 * Gets whether this job is printed collated.
 *
 * Returns: whether the job is printed collated
 */
gboolean
gtk_print_job_get_collate (GtkPrintJob *job)
{
965
  return job->collate;
966
967
968
}

/**
969
 * gtk_print_job_set_collate:
Matthias Clasen's avatar
Matthias Clasen committed
970
 * @job: a `GtkPrintJob`
971
972
973
974
975
976
977
978
 * @collate: whether the job is printed collated
 *
 * Sets whether this job is printed collated.
 */
void
gtk_print_job_set_collate (GtkPrintJob *job,
                           gboolean     collate)
{
979
  job->collate = collate;
980
981
982
983
}

/**
 * gtk_print_job_get_reverse:
Matthias Clasen's avatar
Matthias Clasen committed
984
 * @job: a `GtkPrintJob`
985
986
987
988
989
990
991
992
 *
 * Gets whether this job is printed reversed.
 *
 * Returns: whether the job is printed reversed.
 */
gboolean
gtk_print_job_get_reverse (GtkPrintJob *job)
{
993
  return job->reverse;
994
995
996
997
}

/**
 * gtk_print_job_set_reverse:
Matthias Clasen's avatar
Matthias Clasen committed
998
 * @job: a `GtkPrintJob`
999
1000
1001
1002
1003
1004
1005
1006
 * @reverse: whether the job is printed reversed
 *
 * Sets whether this job is printed reversed.
 */
void
gtk_print_job_set_reverse (GtkPrintJob *job,
                           gboolean     reverse)
{
1007
  job->reverse = reverse;
1008
}