gtkprinter.c 31.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GtkPrinter
 * 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 18 19 20 21 22 23 24 25 26 27 28 29
 */

#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "gtkintl.h"
#include "gtkprivate.h"

#include "gtkprinter.h"
#include "gtkprinter-private.h"
#include "gtkprintbackend.h"
#include "gtkprintjob.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

/**
 * SECTION:gtkprinter
 * @Short_description: Represents a printer
 * @Title: GtkPrinter
 *
 * A #GtkPrinter object represents a printer. You only need to
 * deal directly with printers if you use the non-portable
 * #GtkPrintUnixDialog API.
 *
 * A #GtkPrinter allows to get status information about the printer,
 * such as its description, its location, the number of queued jobs,
 * etc. Most importantly, a #GtkPrinter object can be used to create
 * a #GtkPrintJob object, which lets you print to the printer.
 *
 * Printing support was added in GTK+ 2.10.
 */


49 50 51 52 53 54 55 56 57
static void gtk_printer_finalize     (GObject *object);

struct _GtkPrinterPrivate
{
  gchar *name;
  gchar *location;
  gchar *description;
  gchar *icon_name;

58 59 60 61 62 63 64 65 66
  guint is_active         : 1;
  guint is_paused         : 1;
  guint is_accepting_jobs : 1;
  guint is_new            : 1;
  guint is_virtual        : 1;
  guint is_default        : 1;
  guint has_details       : 1;
  guint accepts_pdf       : 1;
  guint accepts_ps        : 1;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

  gchar *state_message;  
  gint job_count;

  GtkPrintBackend *backend;
};

enum {
  DETAILS_ACQUIRED,
  LAST_SIGNAL
};

enum {
  PROP_0,
  PROP_NAME,
  PROP_BACKEND,
  PROP_IS_VIRTUAL,
  PROP_STATE_MESSAGE,
  PROP_LOCATION,
  PROP_ICON_NAME,
87 88
  PROP_JOB_COUNT,
  PROP_ACCEPTS_PDF,
89 90 91
  PROP_ACCEPTS_PS,
  PROP_PAUSED,
  PROP_ACCEPTING_JOBS
92 93 94 95 96 97 98 99 100 101 102 103 104
};

static guint signals[LAST_SIGNAL] = { 0 };

static void gtk_printer_set_property (GObject      *object,
				      guint         prop_id,
				      const GValue *value,
				      GParamSpec   *pspec);
static void gtk_printer_get_property (GObject      *object,
				      guint         prop_id,
				      GValue       *value,
				      GParamSpec   *pspec);

105
G_DEFINE_TYPE_WITH_PRIVATE (GtkPrinter, gtk_printer, G_TYPE_OBJECT)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

static void
gtk_printer_class_init (GtkPrinterClass *class)
{
  GObjectClass *object_class;
  object_class = (GObjectClass *) class;

  object_class->finalize = gtk_printer_finalize;

  object_class->set_property = gtk_printer_set_property;
  object_class->get_property = gtk_printer_get_property;

  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_NAME,
                                   g_param_spec_string ("name",
						        P_("Name"),
						        P_("Name of the printer"),
123
						        "",
124 125 126 127 128 129 130 131 132 133 134 135
							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_BACKEND,
                                   g_param_spec_object ("backend",
						        P_("Backend"),
						        P_("Backend for the printer"),
						        GTK_TYPE_PRINT_BACKEND,
							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_IS_VIRTUAL,
                                   g_param_spec_boolean ("is-virtual",
							 P_("Is Virtual"),
136
							 P_("FALSE if this represents a real hardware printer"),
137 138
							 FALSE,
							 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
139 140 141 142 143
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_ACCEPTS_PDF,
                                   g_param_spec_boolean ("accepts-pdf",
							 P_("Accepts PDF"),
							 P_("TRUE if this printer can accept PDF"),
144
							 FALSE,
145 146 147 148 149 150 151 152
							 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_ACCEPTS_PS,
                                   g_param_spec_boolean ("accepts-ps",
							 P_("Accepts PostScript"),
							 P_("TRUE if this printer can accept PostScript"),
							 TRUE,
							 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
153 154 155 156 157
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_STATE_MESSAGE,
                                   g_param_spec_string ("state-message",
						        P_("State Message"),
						        P_("String giving the current state of the printer"),
158
						        "",
159 160 161 162 163 164
							GTK_PARAM_READABLE));
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_LOCATION,
                                   g_param_spec_string ("location",
						        P_("Location"),
						        P_("The location of the printer"),
165
						        "",
166 167 168 169 170 171
							GTK_PARAM_READABLE));
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_ICON_NAME,
                                   g_param_spec_string ("icon-name",
						        P_("Icon Name"),
						        P_("The icon name to use for the printer"),
172
						        "",
173 174 175 176 177 178 179 180 181 182 183
							GTK_PARAM_READABLE));
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_JOB_COUNT,
				   g_param_spec_int ("job-count",
 						     P_("Job Count"),
 						     P_("Number of jobs queued in the printer"),
 						     0,
 						     G_MAXINT,
 						     0,
 						     GTK_PARAM_READABLE));

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  /**
   * GtkPrinter:paused:
   *
   * This property is %TRUE if this printer is paused. 
   * A paused printer still accepts jobs, but it does 
   * not print them.
   *
   * Since: 2.14
   */
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_PAUSED,
                                   g_param_spec_boolean ("paused",
							 P_("Paused Printer"),
							 P_("TRUE if this printer is paused"),
							 FALSE,
							 GTK_PARAM_READABLE));
  /**
   * GtkPrinter:accepting-jobs:
   *
   * This property is %TRUE if the printer is accepting jobs.
   *
   * Since: 2.14
   */ 
  g_object_class_install_property (G_OBJECT_CLASS (class),
                                   PROP_ACCEPTING_JOBS,
                                   g_param_spec_boolean ("accepting-jobs",
							 P_("Accepting Jobs"),
							 P_("TRUE if this printer is accepting new jobs"),
							 TRUE,
							 GTK_PARAM_READABLE));

215 216 217 218 219 220 221 222 223 224 225
  /**
   * GtkPrinter::details-acquired:
   * @printer: the #GtkPrinter on which the signal is emitted
   * @success: %TRUE if the details were successfully acquired
   *
   * Gets emitted in response to a request for detailed information
   * about a printer from the print backend. The @success parameter
   * indicates if the information was actually obtained.
   *
   * Since: 2.10
   */
226
  signals[DETAILS_ACQUIRED] =
Matthias Clasen's avatar
Matthias Clasen committed
227 228 229 230 231 232 233
    g_signal_new (I_("details-acquired"),
		  G_TYPE_FROM_CLASS (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrinterClass, details_acquired),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__BOOLEAN,
		  G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
234 235 236 237 238
}

static void
gtk_printer_init (GtkPrinter *printer)
{
239
  GtkPrinterPrivate *priv;
240

241
  priv = printer->priv = gtk_printer_get_instance_private (printer);
242

243 244 245 246
  priv->name = NULL;
  priv->location = NULL;
  priv->description = NULL;
  priv->icon_name = NULL;
247

248
  priv->is_active = TRUE;
249 250
  priv->is_paused = FALSE;
  priv->is_accepting_jobs = TRUE;
251 252
  priv->is_new = TRUE;
  priv->has_details = FALSE;
253
  priv->accepts_pdf = FALSE;
254
  priv->accepts_ps = TRUE;
255 256 257

  priv->state_message = NULL;  
  priv->job_count = 0;
258 259 260 261 262 263
}

static void
gtk_printer_finalize (GObject *object)
{
  GtkPrinter *printer = GTK_PRINTER (object);
264
  GtkPrinterPrivate *priv = printer->priv;
265

266 267 268 269 270
  g_free (priv->name);
  g_free (priv->location);
  g_free (priv->description);
  g_free (priv->state_message);
  g_free (priv->icon_name);
271

Alexander Larsson's avatar
Alexander Larsson committed
272 273 274
  if (priv->backend)
    g_object_unref (priv->backend);

275
  G_OBJECT_CLASS (gtk_printer_parent_class)->finalize (object);
276 277 278 279 280 281 282 283 284
}

static void
gtk_printer_set_property (GObject         *object,
			  guint            prop_id,
			  const GValue    *value,
			  GParamSpec      *pspec)
{
  GtkPrinter *printer = GTK_PRINTER (object);
285 286
  GtkPrinterPrivate *priv = printer->priv;

287 288 289
  switch (prop_id)
    {
    case PROP_NAME:
290
      priv->name = g_value_dup_string (value);
291 292 293
      break;
    
    case PROP_BACKEND:
Alexander Larsson's avatar
Alexander Larsson committed
294
      priv->backend = GTK_PRINT_BACKEND (g_value_dup_object (value));
295 296 297
      break;

    case PROP_IS_VIRTUAL:
298
      priv->is_virtual = g_value_get_boolean (value);
299 300
      break;

301 302 303 304 305 306 307 308
    case PROP_ACCEPTS_PDF:
      priv->accepts_pdf = g_value_get_boolean (value);
      break;

    case PROP_ACCEPTS_PS:
      priv->accepts_ps = g_value_get_boolean (value);
      break;

309 310 311 312 313 314 315 316 317 318 319 320 321
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_printer_get_property (GObject    *object,
			  guint       prop_id,
			  GValue     *value,
			  GParamSpec *pspec)
{
  GtkPrinter *printer = GTK_PRINTER (object);
322
  GtkPrinterPrivate *priv = printer->priv;
323 324 325 326

  switch (prop_id)
    {
    case PROP_NAME:
327 328
      if (priv->name)
	g_value_set_string (value, priv->name);
329
      else
330
	g_value_set_static_string (value, "");
331 332
      break;
    case PROP_BACKEND:
333
      g_value_set_object (value, priv->backend);
334 335
      break;
    case PROP_STATE_MESSAGE:
336 337
      if (priv->state_message)
	g_value_set_string (value, priv->state_message);
338
      else
339
	g_value_set_static_string (value, "");
340 341
      break;
    case PROP_LOCATION:
342 343
      if (priv->location)
	g_value_set_string (value, priv->location);
344
      else
345
	g_value_set_static_string (value, "");
346 347
      break;
    case PROP_ICON_NAME:
348 349
      if (priv->icon_name)
	g_value_set_string (value, priv->icon_name);
350
      else
351
	g_value_set_static_string (value, "");
352 353
      break;
    case PROP_JOB_COUNT:
354
      g_value_set_int (value, priv->job_count);
355
      break;
356 357 358 359 360 361 362 363 364
    case PROP_IS_VIRTUAL:
      g_value_set_boolean (value, priv->is_virtual);
      break;
    case PROP_ACCEPTS_PDF:
      g_value_set_boolean (value, priv->accepts_pdf);
      break;
    case PROP_ACCEPTS_PS:
      g_value_set_boolean (value, priv->accepts_ps);
      break;
365 366 367 368 369 370
    case PROP_PAUSED:
      g_value_set_boolean (value, priv->is_paused);
      break;
    case PROP_ACCEPTING_JOBS:
      g_value_set_boolean (value, priv->is_accepting_jobs);
      break;
371 372 373 374 375 376 377 378
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

/**
 * gtk_printer_new:
379 380
 * @name: the name of the printer
 * @backend: a #GtkPrintBackend
Matthias Clasen's avatar
Matthias Clasen committed
381
 * @virtual_: whether the printer is virtual
382 383 384
 *
 * Creates a new #GtkPrinter.
 *
385
 * Returns: a new #GtkPrinter
386
 *
387
 * Since: 2.10
388 389
 **/
GtkPrinter *
390
gtk_printer_new (const gchar     *name,
391
		 GtkPrintBackend *backend,
Matthias Clasen's avatar
Matthias Clasen committed
392
		 gboolean         virtual_)
393 394 395 396 397 398
{
  GObject *result;
  
  result = g_object_new (GTK_TYPE_PRINTER,
			 "name", name,
			 "backend", backend,
Matthias Clasen's avatar
Matthias Clasen committed
399
			 "is-virtual", virtual_,
400 401 402 403 404
                         NULL);

  return (GtkPrinter *) result;
}

405 406 407 408 409 410
/**
 * gtk_printer_get_backend:
 * @printer: a #GtkPrinter
 * 
 * Returns the backend of the printer.
 * 
411
 * Returns: (transfer none): the backend of @printer
412 413 414
 * 
 * Since: 2.10
 */
415 416 417 418 419 420 421 422
GtkPrintBackend *
gtk_printer_get_backend (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), NULL);
  
  return printer->priv->backend;
}

423 424 425 426 427 428
/**
 * gtk_printer_get_name:
 * @printer: a #GtkPrinter
 * 
 * Returns the name of the printer.
 * 
429
 * Returns: the name of @printer
430 431 432
 *
 * Since: 2.10
 */
433
const gchar *
434 435 436 437 438 439 440
gtk_printer_get_name (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), NULL);

  return printer->priv->name;
}

441 442 443 444 445 446
/**
 * gtk_printer_get_description:
 * @printer: a #GtkPrinter
 * 
 * Gets the description of the printer.
 * 
447
 * Returns: the description of @printer
448 449 450
 *
 * Since: 2.10
 */
451
const gchar *
452 453 454 455 456 457 458 459
gtk_printer_get_description (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), NULL);
  
  return printer->priv->description;
}

gboolean
460 461
gtk_printer_set_description (GtkPrinter  *printer,
			     const gchar *description)
462
{
463 464
  GtkPrinterPrivate *priv;

465 466
  g_return_val_if_fail (GTK_IS_PRINTER (printer), FALSE);

467 468
  priv = printer->priv;

469
  if (g_strcmp0 (priv->description, description) == 0)
470 471
    return FALSE;

472 473
  g_free (priv->description);
  priv->description = g_strdup (description);
474 475 476 477
  
  return TRUE;
}

478 479 480 481 482 483 484
/**
 * gtk_printer_get_state_message:
 * @printer: a #GtkPrinter
 * 
 * Returns the state message describing the current state
 * of the printer.
 * 
485
 * Returns: the state message of @printer
486 487 488
 *
 * Since: 2.10
 */
489
const gchar *
490 491 492 493 494 495 496 497
gtk_printer_get_state_message (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), NULL);

  return printer->priv->state_message;
}

gboolean
498 499
gtk_printer_set_state_message (GtkPrinter  *printer,
			       const gchar *message)
500
{
501 502
  GtkPrinterPrivate *priv;

503 504
  g_return_val_if_fail (GTK_IS_PRINTER (printer), FALSE);

505 506
  priv = printer->priv;

507
  if (g_strcmp0 (priv->state_message, message) == 0)
508 509
    return FALSE;

510 511
  g_free (priv->state_message);
  priv->state_message = g_strdup (message);
512 513 514 515 516
  g_object_notify (G_OBJECT (printer), "state-message");

  return TRUE;
}

517 518 519 520
/**
 * gtk_printer_get_location:
 * @printer: a #GtkPrinter
 * 
Matthias Clasen's avatar
Matthias Clasen committed
521
 * Returns a description of the location of the printer.
522
 * 
523
 * Returns: the location of @printer
524 525 526
 *
 * Since: 2.10
 */
527
const gchar *
528 529 530 531 532 533 534 535
gtk_printer_get_location (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), NULL);

  return printer->priv->location;
}

gboolean
536 537
gtk_printer_set_location (GtkPrinter  *printer,
			  const gchar *location)
538
{
539 540
  GtkPrinterPrivate *priv;

541 542
  g_return_val_if_fail (GTK_IS_PRINTER (printer), FALSE);

543 544
  priv = printer->priv;

545
  if (g_strcmp0 (priv->location, location) == 0)
546 547
    return FALSE;

548 549
  g_free (priv->location);
  priv->location = g_strdup (location);
550 551 552 553 554
  g_object_notify (G_OBJECT (printer), "location");
  
  return TRUE;
}

555
/**
556 557 558 559 560
 * gtk_printer_get_icon_name:
 * @printer: a #GtkPrinter
 * 
 * Gets the name of the icon to use for the printer.
 * 
561
 * Returns: the icon name for @printer
562 563 564
 *
 * Since: 2.10
 */
565
const gchar *
566 567 568 569 570 571 572 573
gtk_printer_get_icon_name (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), NULL);

  return printer->priv->icon_name;
}

void
574 575
gtk_printer_set_icon_name (GtkPrinter  *printer,
			   const gchar *icon)
576
{
577 578
  GtkPrinterPrivate *priv;

579 580
  g_return_if_fail (GTK_IS_PRINTER (printer));

581 582 583 584
  priv = printer->priv;

  g_free (priv->icon_name);
  priv->icon_name = g_strdup (icon);
585 586 587
  g_object_notify (G_OBJECT (printer), "icon-name");
}

588 589 590 591 592 593
/**
 * gtk_printer_get_job_count:
 * @printer: a #GtkPrinter
 * 
 * Gets the number of jobs currently queued on the printer.
 * 
594
 * Returns: the number of jobs on @printer
595 596 597
 *
 * Since: 2.10
 */
598 599 600 601 602 603 604 605 606 607
gint 
gtk_printer_get_job_count (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), 0);

  return printer->priv->job_count;
}

gboolean
gtk_printer_set_job_count (GtkPrinter *printer,
608
			   gint        count)
609
{
610 611
  GtkPrinterPrivate *priv;

612 613
  g_return_val_if_fail (GTK_IS_PRINTER (printer), FALSE);

614 615 616
  priv = printer->priv;

  if (priv->job_count == count)
617 618
    return FALSE;

619
  priv->job_count = count;
620 621 622 623 624 625
  
  g_object_notify (G_OBJECT (printer), "job-count");
  
  return TRUE;
}

626 627 628 629 630 631
/**
 * gtk_printer_has_details:
 * @printer: a #GtkPrinter
 * 
 * Returns whether the printer details are available.
 * 
632
 * Returns: %TRUE if @printer details are available
633 634 635
 *
 * Since: 2.12
 */
636
gboolean
637
gtk_printer_has_details (GtkPrinter *printer)
638
{
639 640
  g_return_val_if_fail (GTK_IS_PRINTER (printer), FALSE);

641 642 643 644 645 646 647 648 649 650
  return printer->priv->has_details;
}

void
gtk_printer_set_has_details (GtkPrinter *printer,
			     gboolean val)
{
  printer->priv->has_details = val;
}

651 652 653 654 655 656 657
/**
 * gtk_printer_is_active:
 * @printer: a #GtkPrinter
 * 
 * Returns whether the printer is currently active (i.e. 
 * accepts new jobs).
 * 
658
 * Returns: %TRUE if @printer is active
659 660 661
 *
 * Since: 2.10
 */
662 663 664 665 666 667 668 669 670 671 672 673
gboolean
gtk_printer_is_active (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), TRUE);
  
  return printer->priv->is_active;
}

void
gtk_printer_set_is_active (GtkPrinter *printer,
			   gboolean val)
{
674 675
  g_return_if_fail (GTK_IS_PRINTER (printer));

676 677 678
  printer->priv->is_active = val;
}

679 680 681 682 683 684 685 686
/**
 * gtk_printer_is_paused:
 * @printer: a #GtkPrinter
 * 
 * Returns whether the printer is currently paused.
 * A paused printer still accepts jobs, but it is not
 * printing them.
 * 
687
 * Returns: %TRUE if @printer is paused
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
 *
 * Since: 2.14
 */
gboolean
gtk_printer_is_paused (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), TRUE);
  
  return printer->priv->is_paused;
}

gboolean
gtk_printer_set_is_paused (GtkPrinter *printer,
			   gboolean    val)
{
  GtkPrinterPrivate *priv;

  g_return_val_if_fail (GTK_IS_PRINTER (printer), FALSE);

  priv = printer->priv;

  if (val == priv->is_paused)
    return FALSE;

  priv->is_paused = val;

  return TRUE;
}

/**
 * gtk_printer_is_accepting_jobs:
 * @printer: a #GtkPrinter
 * 
 * Returns whether the printer is accepting jobs
 * 
723
 * Returns: %TRUE if @printer is accepting jobs
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
 *
 * Since: 2.14
 */
gboolean
gtk_printer_is_accepting_jobs (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), TRUE);
  
  return printer->priv->is_accepting_jobs;
}

gboolean
gtk_printer_set_is_accepting_jobs (GtkPrinter *printer,
				   gboolean val)
{
  GtkPrinterPrivate *priv;

  g_return_val_if_fail (GTK_IS_PRINTER (printer), FALSE);

  priv = printer->priv;

  if (val == priv->is_accepting_jobs)
    return FALSE;

  priv->is_accepting_jobs = val;

  return TRUE;
}
752

753 754 755 756 757 758 759 760
/**
 * gtk_printer_is_virtual:
 * @printer: a #GtkPrinter
 * 
 * Returns whether the printer is virtual (i.e. does not
 * represent actual printer hardware, but something like 
 * a CUPS class).
 * 
761
 * Returns: %TRUE if @printer is virtual
762 763 764
 *
 * Since: 2.10
 */
765 766 767 768 769 770 771 772
gboolean
gtk_printer_is_virtual (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), TRUE);
  
  return printer->priv->is_virtual;
}

773 774 775 776 777 778 779
/**
 * gtk_printer_accepts_pdf:
 * @printer: a #GtkPrinter
 *
 * Returns whether the printer accepts input in
 * PDF format.  
 *
780
 * Returns: %TRUE if @printer accepts PDF
781 782 783
 *
 * Since: 2.10
 */
784 785 786 787 788 789 790 791
gboolean 
gtk_printer_accepts_pdf (GtkPrinter *printer)
{ 
  g_return_val_if_fail (GTK_IS_PRINTER (printer), TRUE);
  
  return printer->priv->accepts_pdf;
}

792 793 794 795 796 797 798 799 800
void
gtk_printer_set_accepts_pdf (GtkPrinter *printer,
			     gboolean val)
{
  g_return_if_fail (GTK_IS_PRINTER (printer));

  printer->priv->accepts_pdf = val;
}

801 802 803 804 805 806 807
/**
 * gtk_printer_accepts_ps:
 * @printer: a #GtkPrinter
 *
 * Returns whether the printer accepts input in
 * PostScript format.  
 *
808
 * Returns: %TRUE if @printer accepts PostScript
809 810 811
 *
 * Since: 2.10
 */
812 813 814 815 816 817 818 819
gboolean 
gtk_printer_accepts_ps (GtkPrinter *printer)
{ 
  g_return_val_if_fail (GTK_IS_PRINTER (printer), TRUE);
  
  return printer->priv->accepts_ps;
}

820 821 822 823 824 825 826 827 828
void
gtk_printer_set_accepts_ps (GtkPrinter *printer,
			    gboolean val)
{
  g_return_if_fail (GTK_IS_PRINTER (printer));

  printer->priv->accepts_ps = val;
}

829 830 831 832 833 834 835 836 837 838 839 840
gboolean
gtk_printer_is_new (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), FALSE);
  
  return printer->priv->is_new;
}

void
gtk_printer_set_is_new (GtkPrinter *printer,
			gboolean val)
{
841 842
  g_return_if_fail (GTK_IS_PRINTER (printer));

843 844 845 846
  printer->priv->is_new = val;
}


847 848 849 850 851 852
/**
 * gtk_printer_is_default:
 * @printer: a #GtkPrinter
 * 
 * Returns whether the printer is the default printer.
 * 
853
 * Returns: %TRUE if @printer is the default
854 855 856
 *
 * Since: 2.10
 */
857 858 859 860 861 862 863 864 865 866
gboolean
gtk_printer_is_default (GtkPrinter *printer)
{
  g_return_val_if_fail (GTK_IS_PRINTER (printer), FALSE);
  
  return printer->priv->is_default;
}

void
gtk_printer_set_is_default (GtkPrinter *printer,
867
			    gboolean    val)
868
{
869 870
  g_return_if_fail (GTK_IS_PRINTER (printer));

871
  printer->priv->is_default = val;
872 873
}

874 875 876 877 878
/**
 * gtk_printer_request_details:
 * @printer: a #GtkPrinter
 * 
 * Requests the printer details. When the details are available,
Matthias Clasen's avatar
Matthias Clasen committed
879
 * the #GtkPrinter::details-acquired signal will be emitted on @printer.
880 881 882
 * 
 * Since: 2.12
 */
883
void
884
gtk_printer_request_details (GtkPrinter *printer)
885
{
886 887 888 889 890
  GtkPrintBackendClass *backend_class;

  g_return_if_fail (GTK_IS_PRINTER (printer));

  backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
891
  backend_class->printer_request_details (printer);
892 893 894
}

GtkPrinterOptionSet *
895 896 897 898
_gtk_printer_get_options (GtkPrinter           *printer,
			  GtkPrintSettings     *settings,
			  GtkPageSetup         *page_setup,
			  GtkPrintCapabilities  capabilities)
899
{
Alexander Larsson's avatar
Alexander Larsson committed
900
  GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
901
  return backend_class->printer_get_options (printer, settings, page_setup, capabilities);
902 903 904 905 906 907
}

gboolean
_gtk_printer_mark_conflicts (GtkPrinter          *printer,
			     GtkPrinterOptionSet *options)
{
Alexander Larsson's avatar
Alexander Larsson committed
908 909
  GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
  return backend_class->printer_mark_conflicts (printer, options);
910 911 912 913 914 915 916
}
  
void
_gtk_printer_get_settings_from_options (GtkPrinter          *printer,
					GtkPrinterOptionSet *options,
					GtkPrintSettings    *settings)
{
Alexander Larsson's avatar
Alexander Larsson committed
917
  GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
918
  backend_class->printer_get_settings_from_options (printer, options, settings);
919 920 921
}

void
922 923
_gtk_printer_prepare_for_print (GtkPrinter       *printer,
				GtkPrintJob      *print_job,
924
				GtkPrintSettings *settings,
925
				GtkPageSetup     *page_setup)
926
{
Alexander Larsson's avatar
Alexander Larsson committed
927
  GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
928
  backend_class->printer_prepare_for_print (printer, print_job, settings, page_setup);
929 930 931
}

cairo_surface_t *
932 933 934 935
_gtk_printer_create_cairo_surface (GtkPrinter       *printer,
				   GtkPrintSettings *settings,
				   gdouble           width, 
				   gdouble           height,
936
				   GIOChannel       *cache_io)
937
{
Alexander Larsson's avatar
Alexander Larsson committed
938
  GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
939

940
  return backend_class->printer_create_cairo_surface (printer, settings,
941
						      width, height, cache_io);
942 943
}

944 945
/**
 * gtk_printer_list_papers:
Matthias Clasen's avatar
Matthias Clasen committed
946
 * @printer: a #GtkPrinter
947
 * 
948
 * Lists all the paper sizes @printer supports.
949
 * This will return and empty list unless the printer’s details are 
Matthias Clasen's avatar
Matthias Clasen committed
950
 * available, see gtk_printer_has_details() and gtk_printer_request_details().
951
 *
952
 * Returns: (element-type GtkPageSetup) (transfer full): a newly allocated list of newly allocated #GtkPageSetup s.
953 954 955
 *
 * Since: 2.12
 */
956
GList  *
957
gtk_printer_list_papers (GtkPrinter *printer)
958
{
959 960 961
  GtkPrintBackendClass *backend_class;

  g_return_val_if_fail (GTK_IS_PRINTER (printer), NULL);
962

963
  backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
Alexander Larsson's avatar
Alexander Larsson committed
964
  return backend_class->printer_list_papers (printer);
965 966
}

967 968 969
/**
 * gtk_printer_get_default_page_size:
 * @printer: a #GtkPrinter
970
 *
971 972
 * Returns default page size of @printer.
 * 
973
 * Returns: a newly allocated #GtkPageSetup with default page size of the printer.
974
 *
975
 * Since: 2.14
976 977 978 979 980 981 982 983 984 985 986 987
 */
GtkPageSetup  *
gtk_printer_get_default_page_size (GtkPrinter *printer)
{
  GtkPrintBackendClass *backend_class;

  g_return_val_if_fail (GTK_IS_PRINTER (printer), NULL);

  backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
  return backend_class->printer_get_default_page_size (printer);
}

988 989 990
/**
 * gtk_printer_get_hard_margins:
 * @printer: a #GtkPrinter
991 992 993 994
 * @top: (out): a location to store the top margin in
 * @bottom: (out): a location to store the bottom margin in
 * @left: (out): a location to store the left margin in
 * @right: (out): a location to store the right margin in
995 996 997 998
 *
 * Retrieve the hard margins of @printer, i.e. the margins that define
 * the area at the borders of the paper that the printer cannot print to.
 *
999
 * Note: This will not succeed unless the printer’s details are available,
1000 1001
 * see gtk_printer_has_details() and gtk_printer_request_details().
 *
1002
 * Returns: %TRUE iff the hard margins were retrieved
1003
 *
1004
 * Since: 2.20
1005 1006 1007 1008 1009 1010 1011
 */
gboolean
gtk_printer_get_hard_margins (GtkPrinter *printer,
			      gdouble    *top,
			      gdouble    *bottom,
			      gdouble    *left,
			      gdouble    *right)
1012
{
Alexander Larsson's avatar
Alexander Larsson committed
1013
  GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
1014

1015
  return backend_class->printer_get_hard_margins (printer, top, bottom, left, right);
Alexander Larsson's avatar
Alexander Larsson committed
1016 1017
}

1018 1019 1020 1021
/**
 * gtk_printer_get_capabilities:
 * @printer: a #GtkPrinter
 * 
1022
 * Returns the printer’s capabilities.
1023
 *
1024
 * This is useful when you’re using #GtkPrintUnixDialog’s manual-capabilities 
Matthias Clasen's avatar
Matthias Clasen committed
1025 1026
 * setting and need to know which settings the printer can handle and which 
 * you must handle yourself.
1027
 *
1028
 * This will return 0 unless the printer’s details are available, see
1029
 * gtk_printer_has_details() and gtk_printer_request_details().
1030
 *
1031
 * Returns: the printer’s capabilities
1032 1033 1034
 *
 * Since: 2.12
 */
1035
GtkPrintCapabilities
1036
gtk_printer_get_capabilities (GtkPrinter *printer)
1037
{
1038
  GtkPrintBackendClass *backend_class;
1039

1040 1041 1042
  g_return_val_if_fail (GTK_IS_PRINTER (printer), 0);

  backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
1043 1044 1045
  return backend_class->printer_get_capabilities (printer);
}

1046 1047 1048 1049 1050 1051 1052
/**
 * gtk_printer_compare:
 * @a: a #GtkPrinter
 * @b: another #GtkPrinter
 *
 * Compares two printers.
 * 
1053
 * Returns: 0 if the printer match, a negative value if @a < @b, 
1054
 *   or a positive value if @a > @b
1055 1056 1057
 *
 * Since: 2.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1058
gint
1059 1060
gtk_printer_compare (GtkPrinter *a, 
                     GtkPrinter *b)
Alexander Larsson's avatar
Alexander Larsson committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
{
  const char *name_a, *name_b;
  
  g_assert (GTK_IS_PRINTER (a) && GTK_IS_PRINTER (b));
  
  name_a = gtk_printer_get_name (a);
  name_b = gtk_printer_get_name (b);
  if (name_a == NULL  && name_b == NULL)
    return 0;
  else if (name_a == NULL)
    return G_MAXINT;
  else if (name_b == NULL)
    return G_MININT;
  else
    return g_ascii_strcasecmp (name_a, name_b);
1076 1077
}

Matthias Clasen's avatar
Matthias Clasen committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102

typedef struct 
{
  GList *backends;
  GtkPrinterFunc func;
  gpointer data;
  GDestroyNotify destroy;
  GMainLoop *loop;
} PrinterList;

static void list_done_cb (GtkPrintBackend *backend, 
			  PrinterList     *printer_list);

static void
stop_enumeration (PrinterList *printer_list)
{
  GList *list, *next;
  GtkPrintBackend *backend;

  for (list = printer_list->backends; list; list = next)
    {
      next = list->next;
      backend = GTK_PRINT_BACKEND (list->data);
      list_done_cb (backend, printer_list);
    }
1103
}
Matthias Clasen's avatar
Matthias Clasen committed
1104

1105 1106 1107
static void 
free_printer_list (PrinterList *printer_list)
{
Matthias Clasen's avatar
Matthias Clasen committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
  if (printer_list->destroy)
    printer_list->destroy (printer_list->data);

  if (printer_list->loop)
    {    
      g_main_loop_quit (printer_list->loop);
      g_main_loop_unref (printer_list->loop);
    }

  g_free (printer_list);
}

1120
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
1121 1122 1123 1124 1125
list_added_cb (GtkPrintBackend *backend, 
	       GtkPrinter      *printer, 
	       PrinterList     *printer_list)
{
  if (printer_list->func (printer, printer_list->data))
1126 1127 1128 1129 1130 1131
    {
      stop_enumeration (printer_list);
      return TRUE;
    }

  return FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
1132 1133
}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
static void
backend_status_changed (GObject    *object,
                        GParamSpec *pspec,
                        gpointer    data)
{
  GtkPrintBackend *backend = GTK_PRINT_BACKEND (object);
  PrinterList *printer_list = data;
  GtkPrintBackendStatus status;

  g_object_get (backend, "status", &status, NULL);
 
  if (status == GTK_PRINT_BACKEND_STATUS_UNAVAILABLE)
    list_done_cb (backend, printer_list);  
}

Matthias Clasen's avatar
Matthias Clasen committed
1149
static void
1150 1151
list_printers_remove_backend (PrinterList     *printer_list,
                              GtkPrintBackend *backend)
Matthias Clasen's avatar
Matthias Clasen committed
1152 1153 1154 1155 1156 1157
{
  printer_list->backends = g_list_remove (printer_list->backends, backend);
  gtk_print_backend_destroy (backend);
  g_object_unref (backend);

  if (printer_list->backends == NULL)
1158
    free_printer_list (printer_list);
Matthias Clasen's avatar
Matthias Clasen committed
1159 1160
}

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
static void
list_done_cb (GtkPrintBackend *backend,
	      PrinterList     *printer_list)
{
  g_signal_handlers_disconnect_by_func (backend, list_added_cb, printer_list);
  g_signal_handlers_disconnect_by_func (backend, list_done_cb, printer_list);
  g_signal_handlers_disconnect_by_func (backend, backend_status_changed, printer_list);

  list_printers_remove_backend(printer_list, backend);
}

1172
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
1173 1174 1175
list_printers_init (PrinterList     *printer_list,
		    GtkPrintBackend *backend)
{
1176
  GList *list, *node;
1177
  GtkPrintBackendStatus status;
Matthias Clasen's avatar
Matthias Clasen committed
1178 1179 1180 1181

  list = gtk_print_backend_get_printer_list (backend);

  for (node = list; node != NULL; node = node->next)
1182 1183 1184 1185 1186 1187 1188
    {
      if (list_added_cb (backend, node->data, printer_list))
        {
          g_list_free (list);
          return TRUE;
        }
    }
Matthias Clasen's avatar
Matthias Clasen committed
1189 1190 1191

  g_list_free (list);

1192 1193 1194 1195
  g_object_get (backend, "status", &status, NULL);
  
  if (status == GTK_PRINT_BACKEND_STATUS_UNAVAILABLE || 
      gtk_print_backend_printer_list_is_done (backend))
1196
    list_printers_remove_backend(printer_list, backend);
Matthias Clasen's avatar
Matthias Clasen committed
1197 1198 1199 1200 1201 1202 1203 1204
  else
    {
      g_signal_connect (backend, "printer-added", 
			(GCallback) list_added_cb, 
			printer_list);
      g_signal_connect (backend, "printer-list-done", 
			(GCallback) list_done_cb, 
			printer_list);
1205 1206 1207
      g_signal_connect (backend, "notify::status", 
                        (GCallback) backend_status_changed,
                        printer_list);     
Matthias Clasen's avatar
Matthias Clasen committed
1208 1209
    }

1210
  return FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
}

/**
 * gtk_enumerate_printers:
 * @func: a function to call for each printer
 * @data: user data to pass to @func
 * @destroy: function to call if @data is no longer needed
 * @wait: if %TRUE, wait in a recursive mainloop until
 *    all printers are enumerated; otherwise return early
 *
1221
 * Calls a function for all #GtkPrinters. 
Matthias Clasen's avatar
Matthias Clasen committed
1222
 * If @func returns %TRUE, the enumeration is stopped.
Matthias Clasen's avatar
Matthias Clasen committed
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
 *
 * Since: 2.10
 */
void
gtk_enumerate_printers (GtkPrinterFunc func,
			gpointer       data,
			GDestroyNotify destroy,
			gboolean       wait)
{
  PrinterList *printer_list;
Matthias Clasen's avatar
Matthias Clasen committed
1233
  GList *node, *next;
Matthias Clasen's avatar
Matthias Clasen committed
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
  GtkPrintBackend *backend;

  printer_list = g_new0 (PrinterList, 1);

  printer_list->func = func;
  printer_list->data = data;
  printer_list->destroy = destroy;

  if (g_module_supported ())
    printer_list->backends = gtk_print_backend_load_modules ();
1244 1245 1246 1247 1248 1249
  
  if (printer_list->backends == NULL)
    {
      free_printer_list (printer_list);
      return;
    }
Matthias Clasen's avatar
Matthias Clasen committed
1250 1251 1252 1253 1254

  for (node = printer_list->backends; node != NULL; node = next)
    {
      next = node->next;
      backend = GTK_PRINT_BACKEND (node->data);
1255 1256
      if (list_printers_init (printer_list, backend))
        return;
Matthias Clasen's avatar
Matthias Clasen committed
1257 1258 1259 1260 1261 1262
    }

  if (wait && printer_list->backends)
    {
      printer_list->loop = g_main_loop_new (NULL, FALSE);

1263
      gdk_threads_leave ();  
Matthias Clasen's avatar
Matthias Clasen committed
1264
      g_main_loop_run (printer_list->loop);
1265
      gdk_threads_enter ();  
Matthias Clasen's avatar
Matthias Clasen committed
1266 1267 1268
    }
}

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
GType
gtk_print_capabilities_get_type (void)
{
  static GType etype = 0;

  if (G_UNLIKELY (etype == 0))
    {
      static const GFlagsValue values[] = {
        { GTK_PRINT_CAPABILITY_PAGE_SET, "GTK_PRINT_CAPABILITY_PAGE_SET", "page-set" },
        { GTK_PRINT_CAPABILITY_COPIES, "GTK_PRINT_CAPABILITY_COPIES", "copies" },
        { GTK_PRINT_CAPABILITY_COLLATE, "GTK_PRINT_CAPABILITY_COLLATE", "collate" },
        { GTK_PRINT_CAPABILITY_REVERSE, "GTK_PRINT_CAPABILITY_REVERSE", "reverse" },
        { GTK_PRINT_CAPABILITY_SCALE, "GTK_PRINT_CAPABILITY_SCALE", "scale" },
        { GTK_PRINT_CAPABILITY_GENERATE_PDF, "GTK_PRINT_CAPABILITY_GENERATE_PDF", "generate-pdf" },
        { GTK_PRINT_CAPABILITY_GENERATE_PS, "GTK_PRINT_CAPABILITY_GENERATE_PS", "generate-ps" },
        { GTK_PRINT_CAPABILITY_PREVIEW, "GTK_PRINT_CAPABILITY_PREVIEW", "preview" },
1285
	{ GTK_PRINT_CAPABILITY_NUMBER_UP, "GTK_PRINT_CAPABILITY_NUMBER_UP", "number-up"},
1286
        { GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT, "GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT", "number-up-layout" },
1287 1288 1289 1290 1291 1292 1293 1294
        { 0, NULL, NULL }
      };

      etype = g_flags_register_static (I_("GtkPrintCapabilities"), values);
    }

  return etype;
}