gtkprintjob.c 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#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"
38
#include "gtkprinter-private.h"
39 40 41 42 43 44 45 46 47 48 49
#include "gtkprintbackend.h"
#include "gtkalias.h"

#ifndef O_BINARY
#define O_BINARY 0
#endif

struct _GtkPrintJobPrivate
{
  gchar *title;

50
  GIOChannel *spool_io;
51 52 53 54 55 56 57 58
  cairo_surface_t *surface;

  GtkPrintStatus status;
  GtkPrintBackend *backend;  
  GtkPrinter *printer;
  GtkPrintSettings *settings;
  GtkPageSetup *page_setup;

59 60 61 62
  guint printer_set : 1;
  guint page_setup_set : 1;
  guint settings_set  : 1;
  guint track_print_status : 1;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
};


#define GTK_PRINT_JOB_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_PRINT_JOB, GtkPrintJobPrivate))

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);
static GObject* gtk_print_job_constructor  (GType                  type,
					    guint                  n_construct_properties,
					    GObjectConstructParam *construct_params);

enum {
  STATUS_CHANGED,
  LAST_SIGNAL
};

enum {
  PROP_0,
89 90 91 92 93
  PROP_TITLE,
  PROP_PRINTER,
  PROP_PAGE_SETUP,
  PROP_SETTINGS,
  PROP_TRACK_PRINT_STATUS
94 95 96 97
};

static guint signals[LAST_SIGNAL] = { 0 };

Matthias Clasen's avatar
Matthias Clasen committed
98
G_DEFINE_TYPE (GtkPrintJob, gtk_print_job, G_TYPE_OBJECT)
99 100 101 102 103 104 105 106 107 108 109 110 111 112

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

  object_class->finalize = gtk_print_job_finalize;
  object_class->constructor = gtk_print_job_constructor;
  object_class->set_property = gtk_print_job_set_property;
  object_class->get_property = gtk_print_job_get_property;

  g_type_class_add_private (class, sizeof (GtkPrintJobPrivate));

113 114
  g_object_class_install_property (object_class,
                                   PROP_TITLE,
115 116 117 118 119 120 121
                                   g_param_spec_string ("title",
						        P_("Title"),
						        P_("Title of the print job"),
						        NULL,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

122 123
  g_object_class_install_property (object_class,
                                   PROP_PRINTER,
124 125 126 127 128 129 130
                                   g_param_spec_object ("printer",
						        P_("Printer"),
						        P_("Printer to print the job to"),
						        GTK_TYPE_PRINTER,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

131 132
  g_object_class_install_property (object_class,
                                   PROP_SETTINGS,
133 134 135 136 137 138 139
                                   g_param_spec_object ("settings",
						        P_("Settings"),
						        P_("Printer settings"),
						        GTK_TYPE_PRINT_SETTINGS,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

140 141
  g_object_class_install_property (object_class,
                                   PROP_PAGE_SETUP,
142 143 144 145 146 147 148
                                   g_param_spec_object ("page-setup",
						        P_("Page Setup"),
						        P_("Page Setup"),
						        GTK_TYPE_PAGE_SETUP,
							GTK_PARAM_READWRITE |
						        G_PARAM_CONSTRUCT_ONLY));

149 150 151 152 153 154 155 156 157 158 159
  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));
  

160 161 162 163 164 165 166 167 168
  /**
   * GtkPrintJob::status-changed:
   * @job: the #GtkPrintJob object on which the signal was emitted
   *
   * Gets emitted when the status of a job changes. The signal handler
   * can use gtk_print_job_get_status() to obtain the new status.
   *
   * Since: 2.10
   */
169
  signals[STATUS_CHANGED] =
Matthias Clasen's avatar
Matthias Clasen committed
170 171 172 173 174 175 176
    g_signal_new (I_("status-changed"),
		  G_TYPE_FROM_CLASS (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintJobClass, status_changed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
177 178 179
}

static void
180
gtk_print_job_init (GtkPrintJob *job)
181
{
182
  GtkPrintJobPrivate *priv;
183

184
  priv = job->priv = GTK_PRINT_JOB_GET_PRIVATE (job); 
185

186
  priv->spool_io = NULL;
187 188 189 190 191 192 193 194 195 196

  priv->title = g_strdup ("");
  priv->surface = NULL;
  priv->backend = NULL;
  priv->printer = NULL;

  priv->printer_set = FALSE;
  priv->settings_set = FALSE;
  priv->page_setup_set = FALSE;
  priv->status = GTK_PRINT_STATUS_INITIAL;
197 198
  priv->track_print_status = FALSE;
  
199 200 201 202 203 204 205 206 207
  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;
208 209 210 211 212 213 214 215 216
}


static GObject*
gtk_print_job_constructor (GType                  type,
			   guint                  n_construct_properties,
			   GObjectConstructParam *construct_params)
{
  GtkPrintJob *job;
217
  GtkPrintJobPrivate *priv;
218 219 220 221 222 223 224 225
  GObject *object;

  object =
    G_OBJECT_CLASS (gtk_print_job_parent_class)->constructor (type,
							      n_construct_properties,
							      construct_params);

  job = GTK_PRINT_JOB (object);
226 227 228 229 230

  priv = job->priv;
  g_assert (priv->printer_set &&
	    priv->settings_set &&
	    priv->page_setup_set);
231
  
232
  _gtk_printer_prepare_for_print (priv->printer,
233
				  job,
234 235
				  priv->settings,
				  priv->page_setup);
236 237 238 239 240 241 242 243

  return object;
}


static void
gtk_print_job_finalize (GObject *object)
{
244 245
  GtkPrintJob *job = GTK_PRINT_JOB (object);
  GtkPrintJobPrivate *priv = job->priv;
246

247
  if (priv->spool_io != NULL)
248
    {
249 250
      g_io_channel_unref (priv->spool_io);
      priv->spool_io = NULL;
251 252
    }
  
253 254
  if (priv->backend)
    g_object_unref (priv->backend);
255

256 257
  if (priv->printer)
    g_object_unref (priv->printer);
258

259 260
  if (priv->surface)
    cairo_surface_destroy (priv->surface);
261

262 263
  if (priv->settings)
    g_object_unref (priv->settings);
264
  
265 266
  if (priv->page_setup)
    g_object_unref (priv->page_setup);
267

268 269
  g_free (job->page_ranges);
  job->page_ranges = NULL;
270
  
271 272 273 274
  g_free (priv->title);
  priv->title = NULL;

  G_OBJECT_CLASS (gtk_print_job_parent_class)->finalize (object);
275 276 277 278
}

/**
 * gtk_print_job_new:
279 280 281 282
 * @title: the job title
 * @printer: a #GtkPrinter
 * @settings: a #GtkPrintSettings
 * @page_setup: a #GtkPageSetup
283 284 285 286 287
 *
 * Creates a new #GtkPrintJob.
 *
 * Return value: a new #GtkPrintJob
 *
288
 * Since: 2.10
289 290
 **/
GtkPrintJob *
291 292
gtk_print_job_new (const gchar      *title,
		   GtkPrinter       *printer,
293
		   GtkPrintSettings *settings,
294
		   GtkPageSetup     *page_setup)
295 296 297 298 299 300 301 302 303 304 305
{
  GObject *result;
  result = g_object_new (GTK_TYPE_PRINT_JOB,
                         "title", title,
			 "printer", printer,
			 "settings", settings,
			 "page-setup", page_setup,
			 NULL);
  return (GtkPrintJob *) result;
}

306 307 308 309 310 311 312 313 314 315
/**
 * gtk_print_job_get_settings:
 * @job: a #GtkPrintJob
 * 
 * Gets the #GtkPrintSettings of the print job.
 * 
 * Return value: the settings of @job
 *
 * Since: 2.10
 */
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 326 327 328 329 330 331 332 333
/**
 * gtk_print_job_get_printer:
 * @job: a #GtkPrintJob
 * 
 * Gets the #GtkPrinter of the print job.
 * 
 * Return value: the printer of @job
 *
 * Since: 2.10
 */
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 344 345 346 347 348 349 350 351 352 353
/**
 * gtk_print_job_get_title:
 * @job: a #GtkPrintJob
 * 
 * Gets the job title.
 * 
 * Return value: the title of @job
 *
 * Since: 2.10
 */
G_CONST_RETURN gchar *
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 362 363 364 365 366 367 368 369
/**
 * gtk_print_job_get_status:
 * @job: a #GtkPrintJob
 * 
 * Gets the status of the print job.
 * 
 * Return value: the status of @job
 *
 * Since: 2.10
 */
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 379 380 381
}

void
gtk_print_job_set_status (GtkPrintJob   *job,
			  GtkPrintStatus status)
{
382 383
  GtkPrintJobPrivate *priv;

384 385
  g_return_if_fail (GTK_IS_PRINT_JOB (job));

386 387 388
  priv = job->priv;

  if (priv->status == status)
389 390
    return;

391
  priv->status = status;
392 393 394
  g_signal_emit (job, signals[STATUS_CHANGED], 0);
}

Matthias Clasen's avatar
Matthias Clasen committed
395 396 397 398 399 400 401 402 403
/**
 * gtk_print_job_set_source_file:
 * @job: a #GtkPrintJob
 * @filename: the file to be printed
 * @error: return location for errors
 * 
 * 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,
Matthias Clasen's avatar
Matthias Clasen committed
404 405
 * but on many platforms PDF may work too). See 
 * gtk_printer_accepts_pdf() and gtk_printer_accepts_ps().
Matthias Clasen's avatar
Matthias Clasen committed
406 407 408 409 410
 * 
 * Returns: %FALSE if an error occurred
 *
 * Since: 2.10
 **/
411 412
gboolean
gtk_print_job_set_source_file (GtkPrintJob *job,
413
			       const gchar *filename,
414 415
			       GError     **error)
{
416
  GtkPrintJobPrivate *priv;
417 418 419
  GError *tmp_error;

  tmp_error = NULL;
420

421
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);
422 423 424

  priv = job->priv;

425 426 427 428
  priv->spool_io = g_io_channel_new_file (filename, "r", &tmp_error);

  if (tmp_error == NULL)
    g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error);
429

430 431 432
  if (tmp_error != NULL)
    {
      g_propagate_error (error, tmp_error);
433 434
      return FALSE;
    }
435 436

  return TRUE;
437 438
}

439 440 441 442 443 444 445 446 447 448 449 450
/**
 * gtk_print_job_get_surface:
 * @job: a #GtkPrintJob
 * @error: return location for errors, or %NULL
 * 
 * Gets a cairo surface onto which the pages of
 * the print job should be rendered.
 * 
 * Return value: the cairo surface of @job
 *
 * Since: 2.10
 **/
451
cairo_surface_t *
452 453
gtk_print_job_get_surface (GtkPrintJob  *job,
			   GError      **error)
454
{
455
  GtkPrintJobPrivate *priv;
456 457
  gchar *filename;
  gdouble width, height;
458
  GtkPaperSize *paper_size;
459 460 461 462 463
  int fd;
  GError *tmp_error;

  tmp_error = NULL;

464 465
  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL);

466 467 468 469 470
  priv = job->priv;

  if (priv->surface)
    return priv->surface;
 
471
  g_return_val_if_fail (priv->spool_io == NULL, NULL);
472
 
473 474 475 476 477
  fd = g_file_open_tmp ("gtkprint_XXXXXX", 
			 &filename, 
			 &tmp_error);
  if (fd == -1)
    {
478
      g_free (filename);
479 480 481
      g_propagate_error (error, tmp_error);
      return NULL;
    }
482

483 484 485 486 487 488
  fchmod (fd, S_IRUSR | S_IWUSR);
  
#ifdef G_ENABLE_DEBUG 
  /* If we are debugging printing don't delete the tmp files */
  if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
#endif /* G_ENABLE_DEBUG */
489
  g_unlink (filename);
490
  g_free (filename);
491

492
  paper_size = gtk_page_setup_get_paper_size (priv->page_setup);
493 494
  width = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
  height = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
495 496 497 498
 
  priv->spool_io = g_io_channel_unix_new (fd);
  g_io_channel_set_close_on_unref (priv->spool_io, TRUE);
  g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error);
499
  
500 501 502 503 504 505 506 507
  if (tmp_error != NULL)
    {
      g_io_channel_unref (priv->spool_io);
      priv->spool_io = NULL;
      g_propagate_error (error, tmp_error);
      return NULL;
    }

508
  priv->surface = _gtk_printer_create_cairo_surface (priv->printer,
Matthias Clasen's avatar
Matthias Clasen committed
509
						     priv->settings,
Matthias Clasen's avatar
Matthias Clasen committed
510
						     width, height,
511 512
						     priv->spool_io);
  
513
  return priv->surface;
514 515
}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
/**
 * gtk_print_job_set_track_print_status:
 * @job: a #GtkPrintJob
 * @track_status: %TRUE to track status after printing
 * 
 * If track_status is %TRUE, the print job will try to continue report
 * 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.
 *
 * Since: 2.10
 */
void
gtk_print_job_set_track_print_status (GtkPrintJob *job,
Matthias Clasen's avatar
Matthias Clasen committed
533
				      gboolean     track_status)
534 535 536 537 538 539 540
{
  GtkPrintJobPrivate *priv;

  g_return_if_fail (GTK_IS_PRINT_JOB (job));

  priv = job->priv;

Matthias Clasen's avatar
Matthias Clasen committed
541 542 543 544 545 546
  track_status = track_status != FALSE;

  if (priv->track_print_status != track_status)
    {
      priv->track_print_status = track_status;
      
547
      g_object_notify (G_OBJECT (job), "track-print-status");
Matthias Clasen's avatar
Matthias Clasen committed
548
    }
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
}

/**
 * gtk_print_job_get_track_print_status:
 * @job: a #GtkPrintJob
 *
 * Returns wheter jobs will be tracked after printing.
 * For details, see gtk_print_job_set_track_print_status().
 *
 * Return value: %TRUE if print job status will be reported after printing
 *
 * Since: 2.10
 */
gboolean
gtk_print_job_get_track_print_status (GtkPrintJob *job)
{
  GtkPrintJobPrivate *priv;

  g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE);

  priv = job->priv;
  
  return priv->track_print_status;
}

574 575 576 577 578 579 580 581
static void
gtk_print_job_set_property (GObject      *object,
	                    guint         prop_id,
	                    const GValue *value,
                            GParamSpec   *pspec)

{
  GtkPrintJob *job = GTK_PRINT_JOB (object);
582
  GtkPrintJobPrivate *priv = job->priv;
583 584 585 586
  GtkPrintSettings *settings;

  switch (prop_id)
    {
587
    case PROP_TITLE:
588
      g_free (priv->title);
589
      priv->title = g_value_dup_string (value);
590 591
      break;
    
592
    case PROP_PRINTER:
593 594 595
      priv->printer = GTK_PRINTER (g_value_dup_object (value));
      priv->printer_set = TRUE;
      priv->backend = g_object_ref (gtk_printer_get_backend (priv->printer));
596 597
      break;

598
    case PROP_PAGE_SETUP:
599 600
      priv->page_setup = GTK_PAGE_SETUP (g_value_dup_object (value));
      priv->page_setup_set = TRUE;
601 602
      break;
      
603
    case PROP_SETTINGS:
604 605 606
      /* 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));
607 608
      priv->settings = gtk_print_settings_copy (settings);
      priv->settings_set = TRUE;
609 610
      break;

611 612 613 614
    case PROP_TRACK_PRINT_STATUS:
      gtk_print_job_set_track_print_status (job, g_value_get_boolean (value));
      break;

615 616 617 618 619 620 621 622 623 624 625 626 627
    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);
628
  GtkPrintJobPrivate *priv = job->priv;
629 630 631

  switch (prop_id)
    {
632
    case PROP_TITLE:
633
      g_value_set_string (value, priv->title);
634
      break;
635
    case PROP_PRINTER:
636
      g_value_set_object (value, priv->printer);
637
      break;
638
    case PROP_SETTINGS:
639
      g_value_set_object (value, priv->settings);
640
      break;
641
    case PROP_PAGE_SETUP:
642
      g_value_set_object (value, priv->page_setup);
643
      break;
644 645 646
    case PROP_TRACK_PRINT_STATUS:
      g_value_set_boolean (value, priv->track_print_status);
      break;
647 648 649 650 651 652
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

653 654 655
/**
 * gtk_print_job_send:
 * @job: a GtkPrintJob
656
 * @callback: function to call when the job completes or an error occurs
657 658 659 660 661 662 663
 * @user_data: user data that gets passed to @callback
 * @dnotify: destroy notify for @user_data
 * 
 * Sends the print job off to the printer.  
 * 
 * Since: 2.10
 **/
664
void
665 666 667
gtk_print_job_send (GtkPrintJob             *job,
                    GtkPrintJobCompleteFunc  callback,
                    gpointer                 user_data,
668
		    GDestroyNotify           dnotify)
669
{
670 671
  GtkPrintJobPrivate *priv;

672
  g_return_if_fail (GTK_IS_PRINT_JOB (job));
673 674

  priv = job->priv;
675
  g_return_if_fail (priv->spool_io != NULL);
676 677
  
  gtk_print_job_set_status (job, GTK_PRINT_STATUS_SENDING_DATA);
678 679 680
  
  g_io_channel_seek_position (priv->spool_io, 0, G_SEEK_SET, NULL);
  
Matthias Clasen's avatar
Matthias Clasen committed
681
  gtk_print_backend_print_stream (priv->backend, job,
682 683
				  priv->spool_io,
                                  callback, user_data, dnotify);
684 685
}

686

687 688
#define __GTK_PRINT_JOB_C__
#include "gtkaliasdef.c"