1 /* GTK - The GIMP Toolkit
2 * gtkprintbackendcups.h: Default implementation of GtkPrintBackend
3 * for the Common Unix Print System (CUPS)
4 * Copyright (C) 2006, 2007 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include <sys/types.h>
29 #include <cups/cups.h>
30 #include <cups/language.h>
31 #include <cups/http.h>
35 #include <cairo-pdf.h>
38 #include <glib/gstdio.h>
39 #include <glib/gi18n-lib.h>
43 #include <gtk/gtkprintbackend.h>
44 #include <gtk/gtkunixprint.h>
45 #include <gtk/gtkprinter-private.h>
47 #include "gtkprintbackendcups.h"
48 #include "gtkprintercups.h"
50 #include "gtkcupsutils.h"
53 typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
55 #define GTK_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
56 #define GTK_IS_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_CUPS))
57 #define GTK_PRINT_BACKEND_CUPS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
59 #define _CUPS_MAX_ATTEMPTS 10
60 #define _CUPS_MAX_CHUNK_SIZE 8192
62 /* define this to see warnings about ignored ppd options */
63 #undef PRINT_IGNORED_OPTIONS
65 #define _CUPS_MAP_ATTR_INT(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].integer;}
66 #define _CUPS_MAP_ATTR_STR(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].string.text;}
68 static GType print_backend_cups_type = 0;
70 typedef void (* GtkPrintCupsResponseCallbackFunc) (GtkPrintBackend *print_backend,
71 GtkCupsResult *result,
82 } GtkPrintCupsDispatchState;
89 GtkCupsRequest *request;
91 GtkPrintBackendCups *backend;
93 } GtkPrintCupsDispatchWatch;
95 struct _GtkPrintBackendCupsClass
97 GtkPrintBackendClass parent_class;
100 struct _GtkPrintBackendCups
102 GtkPrintBackend parent_instance;
104 char *default_printer;
106 guint list_printers_poll;
107 guint list_printers_pending : 1;
108 guint got_default_printer : 1;
109 guint default_printer_poll;
110 GtkCupsConnectionTest *default_printer_connection_test;
113 char *default_cover_before;
114 char *default_cover_after;
115 int number_of_covers;
118 static GObjectClass *backend_parent_class;
120 static void gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class);
121 static void gtk_print_backend_cups_init (GtkPrintBackendCups *impl);
122 static void gtk_print_backend_cups_finalize (GObject *object);
123 static void gtk_print_backend_cups_dispose (GObject *object);
124 static void cups_get_printer_list (GtkPrintBackend *print_backend);
125 static void cups_get_default_printer (GtkPrintBackendCups *print_backend);
126 static void cups_request_execute (GtkPrintBackendCups *print_backend,
127 GtkCupsRequest *request,
128 GtkPrintCupsResponseCallbackFunc callback,
130 GDestroyNotify notify);
131 static void cups_printer_get_settings_from_options (GtkPrinter *printer,
132 GtkPrinterOptionSet *options,
133 GtkPrintSettings *settings);
134 static gboolean cups_printer_mark_conflicts (GtkPrinter *printer,
135 GtkPrinterOptionSet *options);
136 static GtkPrinterOptionSet *cups_printer_get_options (GtkPrinter *printer,
137 GtkPrintSettings *settings,
138 GtkPageSetup *page_setup,
139 GtkPrintCapabilities capabilities);
140 static void cups_printer_prepare_for_print (GtkPrinter *printer,
141 GtkPrintJob *print_job,
142 GtkPrintSettings *settings,
143 GtkPageSetup *page_setup);
144 static GList * cups_printer_list_papers (GtkPrinter *printer);
145 static GtkPageSetup * cups_printer_get_default_page_size (GtkPrinter *printer);
146 static void cups_printer_request_details (GtkPrinter *printer);
147 static gboolean cups_request_default_printer (GtkPrintBackendCups *print_backend);
148 static void cups_request_ppd (GtkPrinter *printer);
149 static void cups_printer_get_hard_margins (GtkPrinter *printer,
154 static GtkPrintCapabilities cups_printer_get_capabilities (GtkPrinter *printer);
155 static void set_option_from_settings (GtkPrinterOption *option,
156 GtkPrintSettings *setting);
157 static void cups_begin_polling_info (GtkPrintBackendCups *print_backend,
160 static gboolean cups_job_info_poll_timeout (gpointer user_data);
161 static void gtk_print_backend_cups_print_stream (GtkPrintBackend *backend,
164 GtkPrintJobCompleteFunc callback,
166 GDestroyNotify dnotify);
167 static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter *printer,
168 GtkPrintSettings *settings,
171 GIOChannel *cache_io);
175 gtk_print_backend_cups_register_type (GTypeModule *module)
177 static const GTypeInfo print_backend_cups_info =
179 sizeof (GtkPrintBackendCupsClass),
180 NULL, /* base_init */
181 NULL, /* base_finalize */
182 (GClassInitFunc) gtk_print_backend_cups_class_init,
183 NULL, /* class_finalize */
184 NULL, /* class_data */
185 sizeof (GtkPrintBackendCups),
187 (GInstanceInitFunc) gtk_print_backend_cups_init
190 print_backend_cups_type = g_type_module_register_type (module,
191 GTK_TYPE_PRINT_BACKEND,
192 "GtkPrintBackendCups",
193 &print_backend_cups_info, 0);
197 pb_module_init (GTypeModule *module)
200 g_print ("CUPS Backend: Initializing the CUPS print backend module\n"));
202 gtk_print_backend_cups_register_type (module);
203 gtk_printer_cups_register_type (module);
207 pb_module_exit (void)
212 G_MODULE_EXPORT GtkPrintBackend *
213 pb_module_create (void)
215 return gtk_print_backend_cups_new ();
219 * GtkPrintBackendCups
222 gtk_print_backend_cups_get_type (void)
224 return print_backend_cups_type;
228 * gtk_print_backend_cups_new:
230 * Creates a new #GtkPrintBackendCups object. #GtkPrintBackendCups
231 * implements the #GtkPrintBackend interface with direct access to
232 * the filesystem using Unix/Linux API calls
234 * Return value: the new #GtkPrintBackendCups object
237 gtk_print_backend_cups_new (void)
240 g_print ("CUPS Backend: Creating a new CUPS print backend object\n"));
242 return g_object_new (GTK_TYPE_PRINT_BACKEND_CUPS, NULL);
246 gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
248 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
249 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
251 backend_parent_class = g_type_class_peek_parent (class);
253 gobject_class->finalize = gtk_print_backend_cups_finalize;
254 gobject_class->dispose = gtk_print_backend_cups_dispose;
256 backend_class->request_printer_list = cups_get_printer_list;
257 backend_class->print_stream = gtk_print_backend_cups_print_stream;
258 backend_class->printer_request_details = cups_printer_request_details;
259 backend_class->printer_create_cairo_surface = cups_printer_create_cairo_surface;
260 backend_class->printer_get_options = cups_printer_get_options;
261 backend_class->printer_mark_conflicts = cups_printer_mark_conflicts;
262 backend_class->printer_get_settings_from_options = cups_printer_get_settings_from_options;
263 backend_class->printer_prepare_for_print = cups_printer_prepare_for_print;
264 backend_class->printer_list_papers = cups_printer_list_papers;
265 backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
266 backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
267 backend_class->printer_get_capabilities = cups_printer_get_capabilities;
270 static cairo_status_t
271 _cairo_write_to_cups (void *closure,
272 const unsigned char *data,
275 GIOChannel *io = (GIOChannel *)closure;
282 g_print ("CUPS Backend: Writing %i byte chunk to temp file\n", length));
286 g_io_channel_write_chars (io, (gchar *)data, length, &written, &error);
291 g_print ("CUPS Backend: Error writing to temp file, %s\n",
294 g_error_free (error);
295 return CAIRO_STATUS_WRITE_ERROR;
299 g_print ("CUPS Backend: Wrote %i bytes to temp file\n", written));
305 return CAIRO_STATUS_SUCCESS;
308 static cairo_surface_t *
309 cups_printer_create_cairo_surface (GtkPrinter *printer,
310 GtkPrintSettings *settings,
313 GIOChannel *cache_io)
315 cairo_surface_t *surface;
316 ppd_file_t *ppd_file = NULL;
317 ppd_attr_t *ppd_attr = NULL;
320 /* TODO: check if it is a ps or pdf printer */
322 surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
324 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
326 if (ppd_file != NULL)
328 ppd_attr = ppdFindAttr (ppd_file, "LanguageLevel", NULL);
330 if (ppd_attr != NULL)
331 level = atoi (ppd_attr->value);
335 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
338 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
340 /* TODO: DPI from settings object? */
341 cairo_surface_set_fallback_resolution (surface, 300, 300);
347 GtkPrintJobCompleteFunc callback;
350 GDestroyNotify dnotify;
351 } CupsPrintStreamData;
354 cups_free_print_stream_data (CupsPrintStreamData *data)
357 g_print ("CUPS Backend: %s\n", G_STRFUNC));
360 data->dnotify (data->user_data);
361 g_object_unref (data->job);
366 cups_print_cb (GtkPrintBackendCups *print_backend,
367 GtkCupsResult *result,
370 GError *error = NULL;
371 CupsPrintStreamData *ps = user_data;
373 GDK_THREADS_ENTER ();
376 g_print ("CUPS Backend: %s\n", G_STRFUNC));
378 if (gtk_cups_result_is_error (result))
379 error = g_error_new_literal (gtk_print_error_quark (),
380 GTK_PRINT_ERROR_INTERNAL_ERROR,
381 gtk_cups_result_get_error_string (result));
384 ps->callback (ps->job, ps->user_data, error);
389 ipp_attribute_t *attr; /* IPP job-id attribute */
390 ipp_t *response = gtk_cups_result_get_response (result);
392 if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
393 job_id = attr->values[0].integer;
395 if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
396 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
399 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_PENDING);
400 cups_begin_polling_info (print_backend, ps->job, job_id);
404 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED_ABORTED);
408 g_error_free (error);
410 GDK_THREADS_LEAVE ();
414 add_cups_options (const gchar *key,
418 GtkCupsRequest *request = user_data;
420 if (!g_str_has_prefix (key, "cups-"))
423 if (strcmp (value, "gtk-ignore-value") == 0)
426 key = key + strlen ("cups-");
428 gtk_cups_request_encode_option (request, key, value);
432 gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
435 GtkPrintJobCompleteFunc callback,
437 GDestroyNotify dnotify)
439 GtkPrinterCups *cups_printer;
440 CupsPrintStreamData *ps;
441 GtkCupsRequest *request;
442 GtkPrintSettings *settings;
444 char printer_absolute_uri[HTTP_MAX_URI];
447 g_print ("CUPS Backend: %s\n", G_STRFUNC));
449 cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
450 settings = gtk_print_job_get_settings (job);
452 request = gtk_cups_request_new (NULL,
457 cups_printer->device_uri);
459 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
460 httpAssembleURIf (HTTP_URI_CODING_ALL,
461 printer_absolute_uri,
462 sizeof (printer_absolute_uri),
468 gtk_printer_get_name (gtk_print_job_get_printer (job)));
470 g_snprintf (printer_absolute_uri,
471 sizeof (printer_absolute_uri),
472 "ipp://localhost:%d/printers/%s",
474 gtk_printer_get_name (gtk_print_job_get_printer (job)));
477 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
478 IPP_TAG_URI, "printer-uri",
479 NULL, printer_absolute_uri);
481 title = gtk_print_job_get_title (job);
483 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
484 IPP_TAG_NAME, "job-name",
487 gtk_print_settings_foreach (settings, add_cups_options, request);
489 ps = g_new0 (CupsPrintStreamData, 1);
490 ps->callback = callback;
491 ps->user_data = user_data;
492 ps->dnotify = dnotify;
493 ps->job = g_object_ref (job);
495 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
497 (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
499 (GDestroyNotify)cups_free_print_stream_data);
504 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
506 backend_cups->list_printers_poll = FALSE;
507 backend_cups->got_default_printer = FALSE;
508 backend_cups->list_printers_pending = FALSE;
510 backend_cups->covers = NULL;
511 backend_cups->default_cover_before = NULL;
512 backend_cups->default_cover_after = NULL;
513 backend_cups->number_of_covers = 0;
515 backend_cups->default_printer_poll = 0;
516 backend_cups->default_printer_connection_test = NULL;
518 cups_get_default_printer (backend_cups);
522 gtk_print_backend_cups_finalize (GObject *object)
524 GtkPrintBackendCups *backend_cups;
527 g_print ("CUPS Backend: finalizing CUPS backend module\n"));
529 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
531 g_free (backend_cups->default_printer);
532 backend_cups->default_printer = NULL;
534 g_strfreev (backend_cups->covers);
535 backend_cups->number_of_covers = 0;
537 g_free (backend_cups->default_cover_before);
538 g_free (backend_cups->default_cover_after);
540 gtk_cups_connection_test_free (backend_cups->default_printer_connection_test);
542 backend_parent_class->finalize (object);
546 gtk_print_backend_cups_dispose (GObject *object)
548 GtkPrintBackendCups *backend_cups;
551 g_print ("CUPS Backend: %s\n", G_STRFUNC));
553 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
555 if (backend_cups->list_printers_poll > 0)
556 g_source_remove (backend_cups->list_printers_poll);
557 backend_cups->list_printers_poll = 0;
559 if (backend_cups->default_printer_poll > 0)
560 g_source_remove (backend_cups->default_printer_poll);
561 backend_cups->default_printer_poll = 0;
563 backend_parent_class->dispose (object);
568 cups_dispatch_watch_check (GSource *source)
570 GtkPrintCupsDispatchWatch *dispatch;
571 GtkCupsPollState poll_state;
575 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
577 dispatch = (GtkPrintCupsDispatchWatch *) source;
579 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
581 if (dispatch->request->http != NULL)
583 if (dispatch->data_poll == NULL)
585 dispatch->data_poll = g_new0 (GPollFD, 1);
586 g_source_add_poll (source, dispatch->data_poll);
590 if (poll_state == GTK_CUPS_HTTP_READ)
591 dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
592 else if (poll_state == GTK_CUPS_HTTP_WRITE)
593 dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
595 dispatch->data_poll->events = 0;
598 #ifdef HAVE_CUPS_API_1_2
599 dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
601 dispatch->data_poll->fd = dispatch->request->http->fd;
605 if (poll_state != GTK_CUPS_HTTP_IDLE)
606 if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
609 result = gtk_cups_request_read_write (dispatch->request);
610 if (result && dispatch->data_poll != NULL)
612 g_source_remove_poll (source, dispatch->data_poll);
613 g_free (dispatch->data_poll);
614 dispatch->data_poll = NULL;
621 cups_dispatch_watch_prepare (GSource *source,
624 GtkPrintCupsDispatchWatch *dispatch;
626 dispatch = (GtkPrintCupsDispatchWatch *) source;
629 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
633 return gtk_cups_request_read_write (dispatch->request);
637 cups_dispatch_watch_dispatch (GSource *source,
638 GSourceFunc callback,
641 GtkPrintCupsDispatchWatch *dispatch;
642 GtkPrintCupsResponseCallbackFunc ep_callback;
643 GtkCupsResult *result;
645 g_assert (callback != NULL);
647 ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
649 dispatch = (GtkPrintCupsDispatchWatch *) source;
651 result = gtk_cups_request_get_result (dispatch->request);
654 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
656 if (gtk_cups_result_is_error (result))
659 g_print("Error result: %s (type %i, status %i, code %i)\n",
660 gtk_cups_result_get_error_string (result),
661 gtk_cups_result_get_error_type (result),
662 gtk_cups_result_get_error_status (result),
663 gtk_cups_result_get_error_code (result)));
666 ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
672 cups_dispatch_watch_finalize (GSource *source)
674 GtkPrintCupsDispatchWatch *dispatch;
677 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
679 dispatch = (GtkPrintCupsDispatchWatch *) source;
681 gtk_cups_request_free (dispatch->request);
683 if (dispatch->backend)
685 /* We need to unref this at idle time, because it might be the
686 * last reference to this module causing the code to be
687 * unloaded (including this particular function!)
688 * Update: Doing this at idle caused a deadlock taking the
689 * mainloop context lock while being in a GSource callout for
690 * multithreaded apps. So, for now we just disable unloading
691 * of print backends. See _gtk_print_backend_create for the
694 g_object_unref (dispatch->backend);
695 dispatch->backend = NULL;
698 g_free (dispatch->data_poll);
701 static GSourceFuncs _cups_dispatch_watch_funcs = {
702 cups_dispatch_watch_prepare,
703 cups_dispatch_watch_check,
704 cups_dispatch_watch_dispatch,
705 cups_dispatch_watch_finalize
710 cups_request_execute (GtkPrintBackendCups *print_backend,
711 GtkCupsRequest *request,
712 GtkPrintCupsResponseCallbackFunc callback,
714 GDestroyNotify notify)
716 GtkPrintCupsDispatchWatch *dispatch;
718 dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
719 sizeof (GtkPrintCupsDispatchWatch));
722 g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
724 dispatch->request = request;
725 dispatch->backend = g_object_ref (print_backend);
726 dispatch->data_poll = NULL;
728 g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
730 g_source_attach ((GSource *) dispatch, NULL);
731 g_source_unref ((GSource *) dispatch);
736 cups_request_printer_info_cb (GtkPrintBackendCups *backend,
737 GtkCupsResult *result,
740 ipp_attribute_t *attr;
743 GtkPrinterCups *cups_printer;
749 gboolean status_changed;
751 g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
753 printer_name = (gchar *)user_data;
754 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
758 g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
763 g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
767 cups_printer = GTK_PRINTER_CUPS (printer);
769 if (gtk_cups_result_is_error (result))
771 if (gtk_printer_is_new (printer))
773 gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
778 return; /* TODO: mark as inactive printer */
781 response = gtk_cups_result_get_response (result);
783 /* TODO: determine printer type and use correct icon */
784 gtk_printer_set_icon_name (printer, "gtk-print");
790 for (attr = response->attrs; attr != NULL; attr = attr->next)
795 _CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
796 _CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
797 _CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
798 _CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
799 _CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
802 status_changed = gtk_printer_set_job_count (printer, job_count);
804 status_changed |= gtk_printer_set_location (printer, loc);
805 status_changed |= gtk_printer_set_description (printer, desc);
806 status_changed |= gtk_printer_set_state_message (printer, state_msg);
809 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
810 "printer-status-changed", printer);
814 cups_request_printer_info (GtkPrintBackendCups *print_backend,
815 const gchar *printer_name)
817 GtkCupsRequest *request;
819 static const char * const pattrs[] = /* Attributes we're interested in */
823 "printer-state-message",
826 "job-sheets-supported",
830 request = gtk_cups_request_new (NULL,
832 IPP_GET_PRINTER_ATTRIBUTES,
837 printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
839 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
840 "printer-uri", NULL, printer_uri);
843 g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
845 g_free (printer_uri);
847 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
848 "requested-attributes", G_N_ELEMENTS (pattrs),
851 cups_request_execute (print_backend,
853 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
854 g_strdup (printer_name),
855 (GDestroyNotify) g_free);
860 GtkPrintBackendCups *print_backend;
867 job_object_died (gpointer user_data,
868 GObject *where_the_object_was)
870 CupsJobPollData *data = user_data;
875 cups_job_poll_data_free (CupsJobPollData *data)
878 g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
884 cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
885 GtkCupsResult *result,
888 CupsJobPollData *data = user_data;
889 ipp_attribute_t *attr;
894 GDK_THREADS_ENTER ();
896 if (data->job == NULL)
898 cups_job_poll_data_free (data);
904 response = gtk_cups_result_get_response (result);
907 for (attr = response->attrs; attr != NULL; attr = attr->next)
912 _CUPS_MAP_ATTR_INT (attr, state, "job-state");
918 case IPP_JOB_PENDING:
920 case IPP_JOB_STOPPED:
921 gtk_print_job_set_status (data->job,
922 GTK_PRINT_STATUS_PENDING);
924 case IPP_JOB_PROCESSING:
925 gtk_print_job_set_status (data->job,
926 GTK_PRINT_STATUS_PRINTING);
929 case IPP_JOB_CANCELLED:
930 case IPP_JOB_ABORTED:
931 gtk_print_job_set_status (data->job,
932 GTK_PRINT_STATUS_FINISHED_ABORTED);
936 case IPP_JOB_COMPLETED:
937 gtk_print_job_set_status (data->job,
938 GTK_PRINT_STATUS_FINISHED);
943 if (!done && data->job != NULL)
947 if (data->counter < 5)
949 else if (data->counter < 10)
954 g_timeout_add (timeout, cups_job_info_poll_timeout, data);
957 cups_job_poll_data_free (data);
960 GDK_THREADS_LEAVE ();
964 cups_request_job_info (CupsJobPollData *data)
966 GtkCupsRequest *request;
969 request = gtk_cups_request_new (NULL,
971 IPP_GET_JOB_ATTRIBUTES,
976 job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
977 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
978 "job-uri", NULL, job_uri);
981 cups_request_execute (data->print_backend,
983 (GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
989 cups_job_info_poll_timeout (gpointer user_data)
991 CupsJobPollData *data = user_data;
993 if (data->job == NULL)
994 cups_job_poll_data_free (data);
996 cups_request_job_info (data);
1002 cups_begin_polling_info (GtkPrintBackendCups *print_backend,
1006 CupsJobPollData *data;
1008 data = g_new0 (CupsJobPollData, 1);
1010 data->print_backend = print_backend;
1012 data->job_id = job_id;
1015 g_object_weak_ref (G_OBJECT (job), job_object_died, data);
1017 cups_request_job_info (data);
1021 mark_printer_inactive (GtkPrinter *printer,
1022 GtkPrintBackend *backend)
1024 gtk_printer_set_is_active (printer, FALSE);
1025 g_signal_emit_by_name (backend, "printer-removed", printer);
1029 find_printer (GtkPrinter *printer,
1030 const gchar *find_name)
1032 const gchar *printer_name;
1034 printer_name = gtk_printer_get_name (printer);
1035 return g_ascii_strcasecmp (printer_name, find_name);
1039 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
1040 GtkCupsResult *result,
1043 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
1044 ipp_attribute_t *attr;
1046 gboolean list_has_changed;
1047 GList *removed_printer_checklist;
1049 GDK_THREADS_ENTER ();
1051 list_has_changed = FALSE;
1054 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1056 cups_backend->list_printers_pending = FALSE;
1058 if (gtk_cups_result_is_error (result))
1061 g_warning ("CUPS Backend: Error getting printer list: %s",
1062 gtk_cups_result_get_error_string (result)));
1067 /* Gather the names of the printers in the current queue
1068 * so we may check to see if they were removed
1070 removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
1072 response = gtk_cups_result_get_response (result);
1074 for (attr = response->attrs; attr != NULL; attr = attr->next)
1076 GtkPrinter *printer;
1077 const gchar *printer_name = NULL;
1078 const gchar *printer_uri = NULL;
1079 const gchar *member_uris = NULL;
1080 const gchar *location = NULL;
1081 const gchar *description = NULL;
1082 const gchar *state_msg = NULL;
1085 gboolean status_changed = FALSE;
1088 const gchar *reason_msg = NULL;
1089 gchar *reason_msg_desc = NULL;
1090 gchar *tmp_msg = NULL;
1091 gchar *tmp_msg2 = NULL;
1092 gint printer_state_reason_level = 0; /* 0 - none, 1 - report, 2 - warning, 3 - error */
1093 gboolean interested_in = FALSE;
1094 gboolean found = FALSE;
1095 static const char * const reasons[] = /* Reasons we're interested in */
1101 "marker-supply-low",
1102 "marker-supply-empty",
1108 "connecting-to-device",
1111 static const char * reasons_descs[] =
1113 N_("Printer '%s' is low on toner."),
1114 N_("Printer '%s' has no toner left."),
1115 /* Translators: "Developer" like on photo development context */
1116 N_("Printer '%s' is low on developer."),
1117 /* Translators: "Developer" like on photo development context */
1118 N_("Printer '%s' is out of developer."),
1119 /* Translators: "marker" is one color bin of the printer */
1120 N_("Printer '%s' is low on at least one marker supply."),
1121 /* Translators: "marker" is one color bin of the printer */
1122 N_("Printer '%s' is out of at least one marker supply."),
1123 N_("The cover is open on printer '%s'."),
1124 N_("The door is open on printer '%s'."),
1125 N_("Printer '%s' is low on paper."),
1126 N_("Printer '%s' is out of paper."),
1127 N_("Printer '%s' is currently off-line."),
1128 N_("Printer '%s' may not be connected."),
1129 N_("There is a problem on printer '%s'.")
1131 gboolean is_paused = FALSE;
1132 gboolean is_accepting_jobs = TRUE;
1134 /* Skip leading attributes until we hit a printer...
1136 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1142 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1144 if (strcmp (attr->name, "printer-name") == 0 &&
1145 attr->value_tag == IPP_TAG_NAME)
1146 printer_name = attr->values[0].string.text;
1147 else if (strcmp (attr->name, "printer-uri-supported") == 0 &&
1148 attr->value_tag == IPP_TAG_URI)
1149 printer_uri = attr->values[0].string.text;
1150 else if (strcmp (attr->name, "member-uris") == 0 &&
1151 attr->value_tag == IPP_TAG_URI)
1152 member_uris = attr->values[0].string.text;
1153 else if (strcmp (attr->name, "printer-location") == 0)
1154 location = attr->values[0].string.text;
1155 else if (strcmp (attr->name, "printer-info") == 0)
1156 description = attr->values[0].string.text;
1157 else if (strcmp (attr->name, "printer-state-message") == 0)
1158 state_msg = attr->values[0].string.text;
1159 else if (strcmp (attr->name, "printer-state-reasons") == 0)
1160 /* Store most important reason to reason_msg and set
1161 its importance at printer_state_reason_level */
1163 for (i = 0; i < attr->num_values; i++)
1165 if (strcmp (attr->values[i].string.text, "none") != 0)
1167 /* Sets is_paused flag for paused printer. */
1168 if (strcmp (attr->values[i].string.text, "paused") == 0)
1173 interested_in = FALSE;
1174 for (j = 0; j < G_N_ELEMENTS (reasons); j++)
1175 if (strncmp (attr->values[i].string.text, reasons[j], strlen (reasons[j])) == 0)
1177 interested_in = TRUE;
1183 if (g_str_has_suffix (attr->values[i].string.text, "-report"))
1185 if (printer_state_reason_level <= 1)
1187 reason_msg = attr->values[i].string.text;
1188 printer_state_reason_level = 1;
1191 else if (g_str_has_suffix (attr->values[i].string.text, "-warning"))
1193 if (printer_state_reason_level <= 2)
1195 reason_msg = attr->values[i].string.text;
1196 printer_state_reason_level = 2;
1199 else /* It is error in the case of no suffix. */
1201 reason_msg = attr->values[i].string.text;
1202 printer_state_reason_level = 3;
1208 else if (strcmp (attr->name, "printer-state") == 0)
1209 state = attr->values[0].integer;
1210 else if (strcmp (attr->name, "queued-job-count") == 0)
1211 job_count = attr->values[0].integer;
1212 else if (strcmp (attr->name, "printer-is-accepting-jobs") == 0)
1214 if (attr->values[0].boolean == 1)
1215 is_accepting_jobs = TRUE;
1217 is_accepting_jobs = FALSE;
1219 else if (strcmp (attr->name, "job-sheets-supported") == 0)
1221 if (cups_backend->covers == NULL)
1223 cups_backend->number_of_covers = attr->num_values;
1224 cups_backend->covers = g_new (char *, cups_backend->number_of_covers + 1);
1226 for (i = 0; i < cups_backend->number_of_covers; i++)
1227 cups_backend->covers[i] = g_strdup (attr->values[i].string.text);
1229 cups_backend->covers[cups_backend->number_of_covers] = NULL;
1232 else if (strcmp (attr->name, "job-sheets-default") == 0)
1234 if ( (cups_backend->default_cover_before == NULL) && (cups_backend->default_cover_after == NULL))
1236 if (attr->num_values == 2)
1238 cups_backend->default_cover_before = g_strdup (attr->values[0].string.text);
1239 cups_backend->default_cover_after = g_strdup (attr->values[1].string.text);
1246 g_print ("CUPS Backend: Attribute %s ignored", attr->name));
1252 if (printer_name == NULL ||
1253 (printer_uri == NULL && member_uris == NULL))
1261 /* remove name from checklist if it was found */
1262 node = g_list_find_custom (removed_printer_checklist, printer_name, (GCompareFunc) find_printer);
1263 removed_printer_checklist = g_list_delete_link (removed_printer_checklist, node);
1265 printer = gtk_print_backend_find_printer (backend, printer_name);
1268 GtkPrinterCups *cups_printer;
1269 char uri[HTTP_MAX_URI]; /* Printer URI */
1270 char method[HTTP_MAX_URI]; /* Method/scheme name */
1271 char username[HTTP_MAX_URI]; /* Username:password */
1272 char hostname[HTTP_MAX_URI]; /* Hostname */
1273 char resource[HTTP_MAX_URI]; /* Resource name */
1274 int port; /* Port number */
1276 list_has_changed = TRUE;
1277 cups_printer = gtk_printer_cups_new (printer_name, backend);
1279 cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
1281 /* Check to see if we are looking at a class */
1284 cups_printer->printer_uri = g_strdup (member_uris);
1285 /* TODO if member_uris is a class we need to recursivly find a printer */
1287 g_print ("CUPS Backend: Found class with printer %s\n", member_uris));
1291 cups_printer->printer_uri = g_strdup (printer_uri);
1293 g_print ("CUPS Backend: Found printer %s\n", printer_uri));
1296 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
1297 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
1298 method, sizeof (method),
1299 username, sizeof (username),
1300 hostname, sizeof (hostname),
1302 resource, sizeof (resource));
1305 httpSeparate (cups_printer->printer_uri,
1313 if (strncmp (resource, "/printers/", 10) == 0)
1315 cups_printer->ppd_name = g_strdup (resource + 10);
1317 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
1320 gethostname (uri, sizeof (uri));
1321 if (strcasecmp (uri, hostname) == 0)
1322 strcpy (hostname, "localhost");
1324 cups_printer->hostname = g_strdup (hostname);
1325 cups_printer->port = port;
1327 printer = GTK_PRINTER (cups_printer);
1329 if (cups_backend->default_printer != NULL &&
1330 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
1331 gtk_printer_set_is_default (printer, TRUE);
1334 gtk_print_backend_add_printer (backend, printer);
1337 g_object_ref (printer);
1339 gtk_printer_set_is_paused (printer, is_paused);
1340 gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
1342 if (!gtk_printer_is_active (printer))
1344 gtk_printer_set_is_active (printer, TRUE);
1345 gtk_printer_set_is_new (printer, TRUE);
1346 list_has_changed = TRUE;
1349 if (gtk_printer_is_new (printer))
1351 g_signal_emit_by_name (backend, "printer-added", printer);
1353 gtk_printer_set_is_new (printer, FALSE);
1357 /* Getting printer info with separate requests overwhelms cups
1358 * when the printer list has more than a handful of printers.
1360 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
1363 GTK_PRINTER_CUPS (printer)->state = state;
1364 status_changed = gtk_printer_set_job_count (printer, job_count);
1365 status_changed |= gtk_printer_set_location (printer, location);
1366 status_changed |= gtk_printer_set_description (printer, description);
1368 if (state_msg != NULL && strlen (state_msg) == 0)
1370 if (is_paused && !is_accepting_jobs)
1371 /* Translators: this is a printer status. */
1372 tmp_msg2 = g_strdup ( N_("Paused ; Rejecting Jobs"));
1373 if (is_paused && is_accepting_jobs)
1374 /* Translators: this is a printer status. */
1375 tmp_msg2 = g_strdup ( N_("Paused"));
1376 if (!is_paused && !is_accepting_jobs)
1377 /* Translators: this is a printer status. */
1378 tmp_msg2 = g_strdup ( N_("Rejecting Jobs"));
1380 if (tmp_msg2 != NULL)
1381 state_msg = tmp_msg2;
1384 /* Set description of the reason and combine it with printer-state-message. */
1385 if ( (reason_msg != NULL))
1387 for (i = 0; i < G_N_ELEMENTS (reasons); i++)
1389 if (strncmp (reason_msg, reasons[i], strlen (reasons[i])) == 0)
1391 reason_msg_desc = g_strdup_printf (reasons_descs[i], printer_name);
1398 printer_state_reason_level = 0;
1400 if (printer_state_reason_level >= 2)
1402 if (strlen (state_msg) == 0)
1403 state_msg = reason_msg_desc;
1406 tmp_msg = g_strjoin (" ; ", state_msg, reason_msg_desc, NULL);
1407 state_msg = tmp_msg;
1412 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1413 status_changed |= gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
1415 if (tmp_msg != NULL)
1418 if (tmp_msg2 != NULL)
1421 if (reason_msg_desc != NULL)
1422 g_free (reason_msg_desc);
1424 /* Set printer icon according to importance
1425 (none, report, warning, error - report is omitted). */
1426 if (printer_state_reason_level == 3)
1427 gtk_printer_set_icon_name (printer, "gtk-print-error");
1428 else if (printer_state_reason_level == 2)
1429 gtk_printer_set_icon_name (printer, "gtk-print-warning");
1430 else if (gtk_printer_is_paused (printer))
1431 gtk_printer_set_icon_name (printer, "gtk-print-paused");
1433 gtk_printer_set_icon_name (printer, "gtk-print");
1436 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1437 "printer-status-changed", printer);
1439 /* The ref is held by GtkPrintBackend, in add_printer() */
1440 g_object_unref (printer);
1446 /* look at the removed printers checklist and mark any printer
1447 as inactive if it is in the list, emitting a printer_removed signal */
1448 if (removed_printer_checklist != NULL)
1450 g_list_foreach (removed_printer_checklist, (GFunc) mark_printer_inactive, backend);
1451 g_list_free (removed_printer_checklist);
1452 list_has_changed = TRUE;
1456 if (list_has_changed)
1457 g_signal_emit_by_name (backend, "printer-list-changed");
1459 gtk_print_backend_set_list_done (backend);
1461 GDK_THREADS_LEAVE ();
1465 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
1467 GtkCupsRequest *request;
1468 static const char * const pattrs[] = /* Attributes we're interested in */
1471 "printer-uri-supported",
1475 "printer-state-message",
1476 "printer-state-reasons",
1479 "printer-is-accepting-jobs",
1480 "job-sheets-supported",
1481 "job-sheets-default"
1484 if (cups_backend->list_printers_pending ||
1485 !cups_backend->got_default_printer)
1488 cups_backend->list_printers_pending = TRUE;
1490 request = gtk_cups_request_new (NULL,
1497 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1498 "requested-attributes", G_N_ELEMENTS (pattrs),
1501 cups_request_execute (cups_backend,
1503 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
1511 cups_get_printer_list (GtkPrintBackend *backend)
1513 GtkPrintBackendCups *cups_backend;
1515 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
1516 if (cups_backend->list_printers_poll == 0)
1518 cups_request_printer_list (cups_backend);
1519 cups_backend->list_printers_poll = gdk_threads_add_timeout_seconds (3,
1520 (GSourceFunc) cups_request_printer_list,
1526 GtkPrinterCups *printer;
1532 get_ppd_data_free (GetPPDData *data)
1535 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1536 httpClose (data->http);
1537 g_io_channel_unref (data->ppd_io);
1538 g_object_unref (data->printer);
1543 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
1544 GtkCupsResult *result,
1548 GtkPrinter *printer;
1550 GDK_THREADS_ENTER ();
1553 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1555 printer = GTK_PRINTER (data->printer);
1556 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
1558 if (gtk_cups_result_is_error (result))
1560 gboolean success = FALSE;
1562 /* if we get a 404 then it is just a raw printer without a ppd
1564 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
1565 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
1567 gtk_printer_set_has_details (printer, TRUE);
1571 g_signal_emit_by_name (printer, "details-acquired", success);
1575 response = gtk_cups_result_get_response (result);
1577 /* let ppdOpenFd take over the ownership of the open file */
1578 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
1579 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
1581 ppdMarkDefaults (data->printer->ppd_file);
1583 gtk_printer_set_has_details (printer, TRUE);
1584 g_signal_emit_by_name (printer, "details-acquired", TRUE);
1587 GDK_THREADS_LEAVE ();
1591 cups_request_ppd (GtkPrinter *printer)
1594 GtkPrintBackend *print_backend;
1595 GtkPrinterCups *cups_printer;
1596 GtkCupsRequest *request;
1603 cups_printer = GTK_PRINTER_CUPS (printer);
1608 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1610 http = httpConnectEncrypt (cups_printer->hostname,
1614 data = g_new0 (GetPPDData, 1);
1616 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
1620 #ifdef G_ENABLE_DEBUG
1621 /* If we are debugging printing don't delete the tmp files */
1622 if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
1623 unlink (ppd_filename);
1625 unlink (ppd_filename);
1626 #endif /* G_ENABLE_DEBUG */
1631 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
1633 g_error_free (error);
1635 g_free (ppd_filename);
1638 g_signal_emit_by_name (printer, "details-acquired", FALSE);
1643 fchmod (fd, S_IRUSR | S_IWUSR);
1644 data->ppd_io = g_io_channel_unix_new (fd);
1645 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
1646 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
1648 data->printer = g_object_ref (printer);
1650 resource = g_strdup_printf ("/printers/%s.ppd",
1651 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
1653 request = gtk_cups_request_new (data->http,
1657 cups_printer->hostname,
1661 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
1664 g_free (ppd_filename);
1666 cups_printer->reading_ppd = TRUE;
1668 print_backend = gtk_printer_get_backend (printer);
1670 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
1672 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
1674 (GDestroyNotify)get_ppd_data_free);
1677 /* Ordering matters for default preference */
1678 static const char *lpoptions_locations[] = {
1679 "/etc/cups/lpoptions",
1685 cups_parse_user_default_printer (const char *filename,
1686 char **printer_name)
1689 char line[1024], *lineptr, *defname = NULL;
1691 if ((fp = g_fopen (filename, "r")) == NULL)
1694 while (fgets (line, sizeof (line), fp) != NULL)
1696 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
1700 while (isspace (*lineptr))
1707 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
1712 if (*printer_name != NULL)
1713 g_free (*printer_name);
1715 *printer_name = g_strdup (defname);
1722 cups_get_user_default_printer (char **printer_name)
1726 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1728 if (g_path_is_absolute (lpoptions_locations[i]))
1730 cups_parse_user_default_printer (lpoptions_locations[i],
1737 filename = g_build_filename (g_get_home_dir (),
1738 lpoptions_locations[i], NULL);
1739 cups_parse_user_default_printer (filename, printer_name);
1746 cups_parse_user_options (const char *filename,
1747 const char *printer_name,
1749 cups_option_t **options)
1752 gchar line[1024], *lineptr, *name;
1754 if ((fp = g_fopen (filename, "r")) == NULL)
1757 while (fgets (line, sizeof (line), fp) != NULL)
1759 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
1761 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
1766 /* Skip leading whitespace */
1767 while (isspace (*lineptr))
1773 /* NUL-terminate the name, stripping the instance name */
1775 while (!isspace (*lineptr) && *lineptr)
1777 if (*lineptr == '/')
1787 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
1790 /* We found our printer, parse the options */
1791 num_options = cupsParseOptions (lineptr, num_options, options);
1800 cups_get_user_options (const char *printer_name,
1802 cups_option_t **options)
1806 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1808 if (g_path_is_absolute (lpoptions_locations[i]))
1810 num_options = cups_parse_user_options (lpoptions_locations[i],
1819 filename = g_build_filename (g_get_home_dir (),
1820 lpoptions_locations[i], NULL);
1821 num_options = cups_parse_user_options (filename, printer_name,
1822 num_options, options);
1830 /* This function requests default printer from a CUPS server in regular intervals.
1831 * In the case of unreachable CUPS server the request is repeated later.
1832 * The default printer is not requested in the case of previous success.
1835 cups_get_default_printer (GtkPrintBackendCups *backend)
1837 GtkPrintBackendCups *cups_backend;
1839 cups_backend = backend;
1841 cups_backend->default_printer_connection_test = gtk_cups_connection_test_new (NULL);
1842 if (cups_backend->default_printer_poll == 0)
1844 if (cups_request_default_printer (cups_backend))
1845 cups_backend->default_printer_poll = gdk_threads_add_timeout_seconds (1,
1846 (GSourceFunc) cups_request_default_printer,
1852 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
1853 GtkCupsResult *result,
1857 ipp_attribute_t *attr;
1859 GDK_THREADS_ENTER ();
1861 response = gtk_cups_result_get_response (result);
1863 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
1864 print_backend->default_printer = g_strdup (attr->values[0].string.text);
1866 print_backend->got_default_printer = TRUE;
1868 /* Make sure to kick off get_printers if we are polling it,
1869 * as we could have blocked this reading the default printer
1871 if (print_backend->list_printers_poll != 0)
1872 cups_request_printer_list (print_backend);
1874 GDK_THREADS_LEAVE ();
1878 cups_request_default_printer (GtkPrintBackendCups *print_backend)
1880 GtkCupsRequest *request;
1884 if (!gtk_cups_connection_test_is_server_available (print_backend->default_printer_connection_test))
1887 gtk_cups_connection_test_free (print_backend->default_printer_connection_test);
1888 print_backend->default_printer_connection_test = NULL;
1890 if ((str = g_getenv ("LPDEST")) != NULL)
1892 print_backend->default_printer = g_strdup (str);
1893 print_backend->got_default_printer = TRUE;
1896 else if ((str = g_getenv ("PRINTER")) != NULL &&
1897 strcmp (str, "lp") != 0)
1899 print_backend->default_printer = g_strdup (str);
1900 print_backend->got_default_printer = TRUE;
1904 /* Figure out user setting for default printer */
1905 cups_get_user_default_printer (&name);
1908 print_backend->default_printer = name;
1909 print_backend->got_default_printer = TRUE;
1913 request = gtk_cups_request_new (NULL,
1920 cups_request_execute (print_backend,
1922 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
1923 g_object_ref (print_backend),
1930 cups_printer_request_details (GtkPrinter *printer)
1932 GtkPrinterCups *cups_printer;
1934 cups_printer = GTK_PRINTER_CUPS (printer);
1935 if (!cups_printer->reading_ppd &&
1936 gtk_printer_cups_get_ppd (cups_printer) == NULL)
1937 cups_request_ppd (printer);
1941 ppd_text_to_utf8 (ppd_file_t *ppd_file,
1944 const char *encoding = NULL;
1947 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
1949 return g_strdup (text);
1951 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
1953 encoding = "ISO-8859-1";
1955 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
1957 encoding = "ISO-8859-2";
1959 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
1961 encoding = "ISO-8859-5";
1963 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
1965 encoding = "SHIFT-JIS";
1967 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
1969 encoding = "MACINTOSH";
1971 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
1973 encoding = "WINDOWS-1252";
1977 /* Fallback, try iso-8859-1... */
1978 encoding = "ISO-8859-1";
1981 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
1986 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
1987 res = g_strdup ("???");
1993 /* TODO: Add more translations for common settings here */
1995 static const struct {
1996 const char *keyword;
1997 const char *translation;
1998 } cups_option_translations[] = {
1999 { "Duplex", N_("Two Sided") },
2000 { "MediaType", N_("Paper Type") },
2001 { "InputSlot", N_("Paper Source") },
2002 { "OutputBin", N_("Output Tray") },
2006 static const struct {
2007 const char *keyword;
2009 const char *translation;
2010 } cups_choice_translations[] = {
2011 { "Duplex", "None", N_("One Sided") },
2012 /* Translators: this is an option of "Paper Source" */
2013 { "InputSlot", "Auto", N_("Auto Select") },
2014 /* Translators: this is an option of "Paper Source" */
2015 { "InputSlot", "AutoSelect", N_("Auto Select") },
2016 /* Translators: this is an option of "Paper Source" */
2017 { "InputSlot", "Default", N_("Printer Default") },
2018 /* Translators: this is an option of "Paper Source" */
2019 { "InputSlot", "None", N_("Printer Default") },
2020 /* Translators: this is an option of "Paper Source" */
2021 { "InputSlot", "PrinterDefault", N_("Printer Default") },
2022 /* Translators: this is an option of "Paper Source" */
2023 { "InputSlot", "Unspecified", N_("Auto Select") },
2026 static const struct {
2027 const char *ppd_keyword;
2029 } option_names[] = {
2030 {"Duplex", "gtk-duplex" },
2031 {"MediaType", "gtk-paper-type"},
2032 {"InputSlot", "gtk-paper-source"},
2033 {"OutputBin", "gtk-output-tray"},
2036 /* keep sorted when changing */
2037 static const char *color_option_whitelist[] = {
2038 "BRColorEnhancement",
2044 "BlackSubstitution",
2052 "RPSBlackOverPrint",
2056 /* keep sorted when changing */
2057 static const char *color_group_whitelist[] = {
2064 "HPColorOptionsPanel",
2067 /* keep sorted when changing */
2068 static const char *image_quality_option_whitelist[] = {
2070 "BRHalfTonePattern",
2080 "HPGraphicsHalftone",
2095 /* keep sorted when changing */
2096 static const char *image_quality_group_whitelist[] = {
2103 /* keep sorted when changing */
2104 static const char * finishing_option_whitelist[] = {
2117 "StapleOrientation",
2123 /* keep sorted when changing */
2124 static const char *finishing_group_whitelist[] = {
2133 /* keep sorted when changing */
2134 static const char *cups_option_blacklist[] = {
2143 get_option_text (ppd_file_t *ppd_file,
2144 ppd_option_t *option)
2149 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
2151 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
2152 return g_strdup (_(cups_option_translations[i].translation));
2155 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
2157 /* Some ppd files have spaces in the text before the colon */
2164 get_choice_text (ppd_file_t *ppd_file,
2165 ppd_choice_t *choice)
2168 ppd_option_t *option = choice->option;
2169 const char *keyword = option->keyword;
2171 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
2173 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
2174 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
2175 return g_strdup (_(cups_choice_translations[i].translation));
2177 return ppd_text_to_utf8 (ppd_file, choice->text);
2181 group_has_option (ppd_group_t *group,
2182 ppd_option_t *option)
2189 if (group->num_options > 0 &&
2190 option >= group->options && option < group->options + group->num_options)
2193 for (i = 0; i < group->num_subgroups; i++)
2195 if (group_has_option (&group->subgroups[i],option))
2202 set_option_off (GtkPrinterOption *option)
2204 /* Any of these will do, _set only applies the value
2205 * if its allowed of the option */
2206 gtk_printer_option_set (option, "False");
2207 gtk_printer_option_set (option, "Off");
2208 gtk_printer_option_set (option, "None");
2212 value_is_off (const char *value)
2214 return (strcasecmp (value, "None") == 0 ||
2215 strcasecmp (value, "Off") == 0 ||
2216 strcasecmp (value, "False") == 0);
2220 ppd_group_name (ppd_group_t *group)
2222 #if CUPS_VERSION_MAJOR > 1 || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR > 1) || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR == 1 && CUPS_VERSION_PATCH >= 18)
2230 available_choices (ppd_file_t *ppd,
2231 ppd_option_t *option,
2232 ppd_choice_t ***available,
2233 gboolean keep_if_only_one_option)
2235 ppd_option_t *other_option;
2238 ppd_const_t *constraint;
2239 const char *choice, *other_choice;
2240 ppd_option_t *option1, *option2;
2241 ppd_group_t *installed_options;
2243 gboolean all_default;
2249 conflicts = g_new0 (char, option->num_choices);
2251 installed_options = NULL;
2252 for (i = 0; i < ppd->num_groups; i++)
2256 name = ppd_group_name (&ppd->groups[i]);
2257 if (strcmp (name, "InstallableOptions") == 0)
2259 installed_options = &ppd->groups[i];
2264 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
2266 option1 = ppdFindOption (ppd, constraint->option1);
2267 if (option1 == NULL)
2270 option2 = ppdFindOption (ppd, constraint->option2);
2271 if (option2 == NULL)
2274 if (option == option1)
2276 choice = constraint->choice1;
2277 other_option = option2;
2278 other_choice = constraint->choice2;
2280 else if (option == option2)
2282 choice = constraint->choice2;
2283 other_option = option1;
2284 other_choice = constraint->choice1;
2289 /* We only care of conflicts with installed_options and
2291 if (!group_has_option (installed_options, other_option) &&
2292 (strcmp (other_option->keyword, "PageSize") != 0))
2295 if (*other_choice == 0)
2297 /* Conflict only if the installed option is not off */
2298 if (value_is_off (other_option->defchoice))
2301 /* Conflict if the installed option has the specified default */
2302 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
2307 /* Conflict with all non-off choices */
2308 for (j = 0; j < option->num_choices; j++)
2310 if (!value_is_off (option->choices[j].choice))
2316 for (j = 0; j < option->num_choices; j++)
2318 if (strcasecmp (option->choices[j].choice, choice) == 0)
2326 for (j = 0; j < option->num_choices; j++)
2330 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
2331 all_default = FALSE;
2334 if ((all_default && !keep_if_only_one_option) ||
2335 (num_conflicts == option->num_choices))
2342 /* Some ppds don't have a "use printer default" option for
2343 * InputSlot. This means you always have to select a particular slot,
2344 * and you can't auto-pick source based on the paper size. To support
2345 * this we always add an auto option if there isn't one already. If
2346 * the user chooses the generated option we don't send any InputSlot
2347 * value when printing. The way we detect existing auto-cases is based
2348 * on feedback from Michael Sweet of cups fame.
2351 if (strcmp (option->keyword, "InputSlot") == 0)
2353 gboolean found_auto = FALSE;
2354 for (j = 0; j < option->num_choices; j++)
2358 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
2359 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
2360 strcmp (option->choices[j].choice, "Default") == 0 ||
2361 strcmp (option->choices[j].choice, "None") == 0 ||
2362 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
2363 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
2364 option->choices[j].code == NULL ||
2365 option->choices[j].code[0] == 0)
2379 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
2382 for (j = 0; j < option->num_choices; j++)
2385 (*available)[i++] = &option->choices[j];
2389 (*available)[i++] = NULL;
2394 return option->num_choices - num_conflicts + add_auto;
2397 static GtkPrinterOption *
2398 create_pickone_option (ppd_file_t *ppd_file,
2399 ppd_option_t *ppd_option,
2400 const gchar *gtk_name)
2402 GtkPrinterOption *option;
2403 ppd_choice_t **available;
2407 #ifdef HAVE_CUPS_API_1_2
2408 ppd_coption_t *coption;
2411 g_assert (ppd_option->ui == PPD_UI_PICKONE);
2415 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2419 /* right now only support one parameter per custom option
2420 * if more than one print warning and only offer the default choices
2423 label = get_option_text (ppd_file, ppd_option);
2425 #ifdef HAVE_CUPS_API_1_2
2426 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
2430 ppd_cparam_t *cparam;
2432 cparam = ppdFirstCustomParam (coption);
2434 if (ppdNextCustomParam (coption) == NULL)
2436 switch (cparam->type)
2438 case PPD_CUSTOM_INT:
2439 option = gtk_printer_option_new (gtk_name, label,
2440 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
2442 case PPD_CUSTOM_PASSCODE:
2443 option = gtk_printer_option_new (gtk_name, label,
2444 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
2446 case PPD_CUSTOM_PASSWORD:
2447 option = gtk_printer_option_new (gtk_name, label,
2448 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
2450 case PPD_CUSTOM_REAL:
2451 option = gtk_printer_option_new (gtk_name, label,
2452 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
2454 case PPD_CUSTOM_STRING:
2455 option = gtk_printer_option_new (gtk_name, label,
2456 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
2458 #ifdef PRINT_IGNORED_OPTIONS
2459 case PPD_CUSTOM_POINTS:
2460 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
2462 case PPD_CUSTOM_CURVE:
2463 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
2465 case PPD_CUSTOM_INVCURVE:
2466 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
2473 #ifdef PRINT_IGNORED_OPTIONS
2475 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
2478 #endif /* HAVE_CUPS_API_1_2 */
2481 option = gtk_printer_option_new (gtk_name, label,
2482 GTK_PRINTER_OPTION_TYPE_PICKONE);
2485 gtk_printer_option_allocate_choices (option, n_choices);
2486 for (i = 0; i < n_choices; i++)
2488 if (available[i] == NULL)
2490 /* This was auto-added */
2491 option->choices[i] = g_strdup ("gtk-ignore-value");
2492 option->choices_display[i] = g_strdup (_("Printer Default"));
2496 option->choices[i] = g_strdup (available[i]->choice);
2497 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
2500 gtk_printer_option_set (option, ppd_option->defchoice);
2502 #ifdef PRINT_IGNORED_OPTIONS
2504 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
2511 static GtkPrinterOption *
2512 create_boolean_option (ppd_file_t *ppd_file,
2513 ppd_option_t *ppd_option,
2514 const gchar *gtk_name)
2516 GtkPrinterOption *option;
2517 ppd_choice_t **available;
2521 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
2525 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2528 label = get_option_text (ppd_file, ppd_option);
2529 option = gtk_printer_option_new (gtk_name, label,
2530 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
2533 gtk_printer_option_allocate_choices (option, 2);
2534 option->choices[0] = g_strdup ("True");
2535 option->choices_display[0] = g_strdup ("True");
2536 option->choices[1] = g_strdup ("False");
2537 option->choices_display[1] = g_strdup ("False");
2539 gtk_printer_option_set (option, ppd_option->defchoice);
2541 #ifdef PRINT_IGNORED_OPTIONS
2543 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
2551 get_option_name (const gchar *keyword)
2555 for (i = 0; i < G_N_ELEMENTS (option_names); i++)
2556 if (strcmp (option_names[i].ppd_keyword, keyword) == 0)
2557 return g_strdup (option_names[i].name);
2559 return g_strdup_printf ("cups-%s", keyword);
2563 strptr_cmp (const void *a,
2566 char **aa = (char **)a;
2567 char **bb = (char **)b;
2568 return strcmp (*aa, *bb);
2573 string_in_table (gchar *str,
2574 const gchar *table[],
2577 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
2580 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
2583 handle_option (GtkPrinterOptionSet *set,
2584 ppd_file_t *ppd_file,
2585 ppd_option_t *ppd_option,
2586 ppd_group_t *toplevel_group,
2587 GtkPrintSettings *settings)
2589 GtkPrinterOption *option;
2592 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
2595 name = get_option_name (ppd_option->keyword);
2598 if (ppd_option->ui == PPD_UI_PICKONE)
2600 option = create_pickone_option (ppd_file, ppd_option, name);
2602 else if (ppd_option->ui == PPD_UI_BOOLEAN)
2604 option = create_boolean_option (ppd_file, ppd_option, name);
2606 #ifdef PRINT_IGNORED_OPTIONS
2608 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
2615 name = ppd_group_name (toplevel_group);
2616 if (STRING_IN_TABLE (name,
2617 color_group_whitelist) ||
2618 STRING_IN_TABLE (ppd_option->keyword,
2619 color_option_whitelist))
2621 option->group = g_strdup ("ColorPage");
2623 else if (STRING_IN_TABLE (name,
2624 image_quality_group_whitelist) ||
2625 STRING_IN_TABLE (ppd_option->keyword,
2626 image_quality_option_whitelist))
2628 option->group = g_strdup ("ImageQualityPage");
2630 else if (STRING_IN_TABLE (name,
2631 finishing_group_whitelist) ||
2632 STRING_IN_TABLE (ppd_option->keyword,
2633 finishing_option_whitelist))
2635 option->group = g_strdup ("FinishingPage");
2639 option->group = g_strdup (toplevel_group->text);
2642 set_option_from_settings (option, settings);
2644 gtk_printer_option_set_add (set, option);
2651 handle_group (GtkPrinterOptionSet *set,
2652 ppd_file_t *ppd_file,
2654 ppd_group_t *toplevel_group,
2655 GtkPrintSettings *settings)
2660 /* Ignore installable options */
2661 name = ppd_group_name (toplevel_group);
2662 if (strcmp (name, "InstallableOptions") == 0)
2665 for (i = 0; i < group->num_options; i++)
2666 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
2668 for (i = 0; i < group->num_subgroups; i++)
2669 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
2673 static GtkPrinterOptionSet *
2674 cups_printer_get_options (GtkPrinter *printer,
2675 GtkPrintSettings *settings,
2676 GtkPageSetup *page_setup,
2677 GtkPrintCapabilities capabilities)
2679 GtkPrinterOptionSet *set;
2680 GtkPrinterOption *option;
2681 ppd_file_t *ppd_file;
2683 char *print_at[] = { "now", "at", "on-hold" };
2684 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
2685 char *prio[] = {"100", "80", "50", "30" };
2686 /* Translators: These strings name the possible values of the
2687 * job priority option in the print dialog
2689 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
2690 char *n_up_layout[] = { "lrtb", "lrbt", "rltb", "rlbt", "tblr", "tbrl", "btlr", "btrl" };
2691 /* Translators: These strings name the possible arrangements of
2692 * multiple pages on a sheet when printing
2694 char *n_up_layout_display[] = { N_("Left to right, top to bottom"), N_("Left to right, bottom to top"),
2695 N_("Right to left, top to bottom"), N_("Right to left, bottom to top"),
2696 N_("Top to bottom, left to right"), N_("Top to bottom, right to left"),
2697 N_("Bottom to top, left to right"), N_("Bottom to top, right to left") };
2700 cups_option_t *opts = NULL;
2701 GtkPrintBackendCups *backend;
2702 GtkTextDirection text_direction;
2705 set = gtk_printer_option_set_new ();
2707 /* Cups specific, non-ppd related settings */
2709 /* Translators, this string is used to label the pages-per-sheet option
2710 * in the print dialog
2712 option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2713 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
2715 gtk_printer_option_set (option, "1");
2716 set_option_from_settings (option, settings);
2717 gtk_printer_option_set_add (set, option);
2718 g_object_unref (option);
2720 if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
2722 for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
2723 n_up_layout_display[i] = _(n_up_layout_display[i]);
2725 /* Translators, this string is used to label the option in the print
2726 * dialog that controls in what order multiple pages are arranged
2728 option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2729 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
2730 n_up_layout, n_up_layout_display);
2732 text_direction = gtk_widget_get_default_direction ();
2733 if (text_direction == GTK_TEXT_DIR_LTR)
2734 gtk_printer_option_set (option, "lrtb");
2736 gtk_printer_option_set (option, "rltb");
2738 set_option_from_settings (option, settings);
2739 gtk_printer_option_set_add (set, option);
2740 g_object_unref (option);
2743 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
2744 prio_display[i] = _(prio_display[i]);
2746 /* Translators, this string is used to label the job priority option
2747 * in the print dialog
2749 option = gtk_printer_option_new ("gtk-job-prio", _("Job Priority"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2750 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
2751 prio, prio_display);
2752 gtk_printer_option_set (option, "50");
2753 set_option_from_settings (option, settings);
2754 gtk_printer_option_set_add (set, option);
2755 g_object_unref (option);
2757 /* Translators, this string is used to label the billing info entry
2758 * in the print dialog
2760 option = gtk_printer_option_new ("gtk-billing-info", _("Billing Info"), GTK_PRINTER_OPTION_TYPE_STRING);
2761 gtk_printer_option_set (option, "");
2762 set_option_from_settings (option, settings);
2763 gtk_printer_option_set_add (set, option);
2764 g_object_unref (option);
2766 backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
2768 if (backend != NULL)
2770 char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
2771 /* Translators, these strings are names for various 'standard' cover
2772 * pages that the printing system may support.
2774 char *cover_display_default[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
2775 char **cover = NULL;
2776 char **cover_display = NULL;
2777 char **cover_display_translated = NULL;
2778 gint num_of_covers = 0;
2782 num_of_covers = backend->number_of_covers;
2783 cover = g_new (char *, num_of_covers + 1);
2784 cover[num_of_covers] = NULL;
2785 cover_display = g_new (char *, num_of_covers + 1);
2786 cover_display[num_of_covers] = NULL;
2787 cover_display_translated = g_new (char *, num_of_covers + 1);
2788 cover_display_translated[num_of_covers] = NULL;
2790 for (i = 0; i < num_of_covers; i++)
2792 cover[i] = g_strdup (backend->covers[i]);
2794 for (j = 0; j < G_N_ELEMENTS (cover_default); j++)
2795 if (strcmp (cover_default[j], cover[i]) == 0)
2797 value = cover_display_default[j];
2800 cover_display[i] = (value != NULL) ? g_strdup (value) : g_strdup (backend->covers[i]);
2803 for (i = 0; i < num_of_covers; i++)
2804 cover_display_translated[i] = _(cover_display[i]);
2806 /* Translators, this is the label used for the option in the print
2807 * dialog that controls the front cover page.
2809 option = gtk_printer_option_new ("gtk-cover-before", _("Before"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2810 gtk_printer_option_choices_from_array (option, num_of_covers,
2811 cover, cover_display_translated);
2813 if (backend->default_cover_before != NULL)
2814 gtk_printer_option_set (option, backend->default_cover_before);
2816 gtk_printer_option_set (option, "none");
2817 set_option_from_settings (option, settings);
2818 gtk_printer_option_set_add (set, option);
2819 g_object_unref (option);
2821 /* Translators, this is the label used for the option in the print
2822 * dialog that controls the back cover page.
2824 option = gtk_printer_option_new ("gtk-cover-after", _("After"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2825 gtk_printer_option_choices_from_array (option, num_of_covers,
2826 cover, cover_display_translated);
2827 if (backend->default_cover_after != NULL)
2828 gtk_printer_option_set (option, backend->default_cover_after);
2830 gtk_printer_option_set (option, "none");
2831 set_option_from_settings (option, settings);
2832 gtk_printer_option_set_add (set, option);
2833 g_object_unref (option);
2836 g_strfreev (cover_display);
2837 g_free (cover_display_translated);
2840 /* Translators: this is the name of the option that controls when
2841 * a print job is printed. Possible values are 'now', a specified time,
2844 option = gtk_printer_option_new ("gtk-print-time", _("Print at"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2845 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
2846 print_at, print_at);
2847 gtk_printer_option_set (option, "now");
2848 set_option_from_settings (option, settings);
2849 gtk_printer_option_set_add (set, option);
2850 g_object_unref (option);
2852 /* Translators: this is the name of the option that allows the user
2853 * to specify a time when a print job will be printed.
2855 option = gtk_printer_option_new ("gtk-print-time-text", _("Print at time"), GTK_PRINTER_OPTION_TYPE_STRING);
2856 gtk_printer_option_set (option, "");
2857 set_option_from_settings (option, settings);
2858 gtk_printer_option_set_add (set, option);
2859 g_object_unref (option);
2861 /* Printer (ppd) specific settings */
2862 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2865 GtkPaperSize *paper_size;
2866 ppd_option_t *option;
2867 const gchar *ppd_name;
2869 ppdMarkDefaults (ppd_file);
2871 paper_size = gtk_page_setup_get_paper_size (page_setup);
2873 option = ppdFindOption (ppd_file, "PageSize");
2874 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
2877 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
2881 char width[G_ASCII_DTOSTR_BUF_SIZE];
2882 char height[G_ASCII_DTOSTR_BUF_SIZE];
2884 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
2885 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
2886 /* Translators: this format is used to display a custom paper
2887 * size. The two placeholders are replaced with the width and height
2888 * in points. E.g: "Custom 230.4x142.9"
2890 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
2891 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
2892 g_free (custom_name);
2895 for (i = 0; i < ppd_file->num_groups; i++)
2896 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
2899 /* Now honor the user set defaults for this printer */
2900 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
2902 for (i = 0; i < num_opts; i++)
2904 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
2907 name = get_option_name (opts[i].name);
2908 option = gtk_printer_option_set_lookup (set, name);
2910 gtk_printer_option_set (option, opts[i].value);
2914 cupsFreeOptions (num_opts, opts);
2921 mark_option_from_set (GtkPrinterOptionSet *set,
2922 ppd_file_t *ppd_file,
2923 ppd_option_t *ppd_option)
2925 GtkPrinterOption *option;
2926 char *name = get_option_name (ppd_option->keyword);
2928 option = gtk_printer_option_set_lookup (set, name);
2931 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
2938 mark_group_from_set (GtkPrinterOptionSet *set,
2939 ppd_file_t *ppd_file,
2944 for (i = 0; i < group->num_options; i++)
2945 mark_option_from_set (set, ppd_file, &group->options[i]);
2947 for (i = 0; i < group->num_subgroups; i++)
2948 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
2952 set_conflicts_from_option (GtkPrinterOptionSet *set,
2953 ppd_file_t *ppd_file,
2954 ppd_option_t *ppd_option)
2956 GtkPrinterOption *option;
2959 if (ppd_option->conflicted)
2961 name = get_option_name (ppd_option->keyword);
2962 option = gtk_printer_option_set_lookup (set, name);
2965 gtk_printer_option_set_has_conflict (option, TRUE);
2966 #ifdef PRINT_IGNORED_OPTIONS
2968 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
2976 set_conflicts_from_group (GtkPrinterOptionSet *set,
2977 ppd_file_t *ppd_file,
2982 for (i = 0; i < group->num_options; i++)
2983 set_conflicts_from_option (set, ppd_file, &group->options[i]);
2985 for (i = 0; i < group->num_subgroups; i++)
2986 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
2990 cups_printer_mark_conflicts (GtkPrinter *printer,
2991 GtkPrinterOptionSet *options)
2993 ppd_file_t *ppd_file;
2997 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2999 if (ppd_file == NULL)
3002 ppdMarkDefaults (ppd_file);
3004 for (i = 0; i < ppd_file->num_groups; i++)
3005 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
3007 num_conflicts = ppdConflicts (ppd_file);
3009 if (num_conflicts > 0)
3011 for (i = 0; i < ppd_file->num_groups; i++)
3012 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
3015 return num_conflicts > 0;
3019 GtkPrinter *printer;
3020 GtkPrinterOptionSet *options;
3021 GtkPrintSettings *settings;
3022 ppd_file_t *ppd_file;
3027 const char *standard;
3031 map_settings_to_option (GtkPrinterOption *option,
3032 const NameMapping table[],
3034 GtkPrintSettings *settings,
3035 const gchar *standard_name,
3036 const gchar *cups_name)
3040 const char *cups_value;
3041 const char *standard_value;
3043 /* If the cups-specific setting is set, always use that */
3044 name = g_strdup_printf ("cups-%s", cups_name);
3045 cups_value = gtk_print_settings_get (settings, name);
3048 if (cups_value != NULL)
3050 gtk_printer_option_set (option, cups_value);
3054 /* Otherwise we try to convert from the general setting */
3055 standard_value = gtk_print_settings_get (settings, standard_name);
3056 if (standard_value == NULL)
3059 for (i = 0; i < n_elements; i++)
3061 if (table[i].cups == NULL && table[i].standard == NULL)
3063 gtk_printer_option_set (option, standard_value);
3066 else if (table[i].cups == NULL &&
3067 strcmp (table[i].standard, standard_value) == 0)
3069 set_option_off (option);
3072 else if (strcmp (table[i].standard, standard_value) == 0)
3074 gtk_printer_option_set (option, table[i].cups);
3081 map_option_to_settings (const gchar *value,
3082 const NameMapping table[],
3084 GtkPrintSettings *settings,
3085 const gchar *standard_name,
3086 const gchar *cups_name)
3091 for (i = 0; i < n_elements; i++)
3093 if (table[i].cups == NULL && table[i].standard == NULL)
3095 gtk_print_settings_set (settings,
3100 else if (table[i].cups == NULL && table[i].standard != NULL)
3102 if (value_is_off (value))
3104 gtk_print_settings_set (settings,
3110 else if (strcmp (table[i].cups, value) == 0)
3112 gtk_print_settings_set (settings,
3119 /* Always set the corresponding cups-specific setting */
3120 name = g_strdup_printf ("cups-%s", cups_name);
3121 gtk_print_settings_set (settings, name, value);
3126 static const NameMapping paper_source_map[] = {
3127 { "Lower", "lower"},
3128 { "Middle", "middle"},
3129 { "Upper", "upper"},
3131 { "Envelope", "envelope"},
3132 { "Cassette", "cassette"},
3133 { "LargeCapacity", "large-capacity"},
3134 { "AnySmallFormat", "small-format"},
3135 { "AnyLargeFormat", "large-format"},
3139 static const NameMapping output_tray_map[] = {
3140 { "Upper", "upper"},
3141 { "Lower", "lower"},
3146 static const NameMapping duplex_map[] = {
3147 { "DuplexTumble", "vertical" },
3148 { "DuplexNoTumble", "horizontal" },
3152 static const NameMapping output_mode_map[] = {
3153 { "Standard", "normal" },
3154 { "Normal", "normal" },
3155 { "Draft", "draft" },
3156 { "Fast", "draft" },
3159 static const NameMapping media_type_map[] = {
3160 { "Transparency", "transparency"},
3161 { "Standard", "stationery"},
3165 static const NameMapping all_map[] = {
3171 set_option_from_settings (GtkPrinterOption *option,
3172 GtkPrintSettings *settings)
3174 const char *cups_value;
3177 if (settings == NULL)
3180 if (strcmp (option->name, "gtk-paper-source") == 0)
3181 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
3182 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3183 else if (strcmp (option->name, "gtk-output-tray") == 0)
3184 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
3185 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3186 else if (strcmp (option->name, "gtk-duplex") == 0)
3187 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
3188 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3189 else if (strcmp (option->name, "cups-OutputMode") == 0)
3190 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
3191 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3192 else if (strcmp (option->name, "cups-Resolution") == 0)
3194 cups_value = gtk_print_settings_get (settings, option->name);
3196 gtk_printer_option_set (option, cups_value);
3199 int res = gtk_print_settings_get_resolution (settings);
3202 value = g_strdup_printf ("%ddpi", res);
3203 gtk_printer_option_set (option, value);
3208 else if (strcmp (option->name, "gtk-paper-type") == 0)
3209 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
3210 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3211 else if (strcmp (option->name, "gtk-n-up") == 0)
3213 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
3214 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3216 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
3218 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
3219 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
3221 else if (strcmp (option->name, "gtk-billing-info") == 0)
3223 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
3225 gtk_printer_option_set (option, cups_value);
3227 else if (strcmp (option->name, "gtk-job-prio") == 0)
3229 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
3231 gtk_printer_option_set (option, cups_value);
3233 else if (strcmp (option->name, "gtk-cover-before") == 0)
3235 cups_value = gtk_print_settings_get (settings, "cover-before");
3237 gtk_printer_option_set (option, cups_value);
3239 else if (strcmp (option->name, "gtk-cover-after") == 0)
3241 cups_value = gtk_print_settings_get (settings, "cover-after");
3243 gtk_printer_option_set (option, cups_value);
3245 else if (strcmp (option->name, "gtk-print-time") == 0)
3247 cups_value = gtk_print_settings_get (settings, "print-at");
3249 gtk_printer_option_set (option, cups_value);
3251 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3253 cups_value = gtk_print_settings_get (settings, "print-at-time");
3255 gtk_printer_option_set (option, cups_value);
3257 else if (g_str_has_prefix (option->name, "cups-"))
3259 cups_value = gtk_print_settings_get (settings, option->name);
3261 gtk_printer_option_set (option, cups_value);
3266 foreach_option_get_settings (GtkPrinterOption *option,
3269 struct OptionData *data = user_data;
3270 GtkPrintSettings *settings = data->settings;
3273 value = option->value;
3275 if (strcmp (option->name, "gtk-paper-source") == 0)
3276 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
3277 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3278 else if (strcmp (option->name, "gtk-output-tray") == 0)
3279 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
3280 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3281 else if (strcmp (option->name, "gtk-duplex") == 0)
3282 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
3283 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3284 else if (strcmp (option->name, "cups-OutputMode") == 0)
3285 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
3286 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3287 else if (strcmp (option->name, "cups-Resolution") == 0)
3289 int res = atoi (value);
3290 /* TODO: What if resolution is on XXXxYYYdpi form? */
3292 gtk_print_settings_set_resolution (settings, res);
3293 gtk_print_settings_set (settings, option->name, value);
3295 else if (strcmp (option->name, "gtk-paper-type") == 0)
3296 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
3297 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3298 else if (strcmp (option->name, "gtk-n-up") == 0)
3299 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3300 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3301 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
3302 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3303 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
3304 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
3305 gtk_print_settings_set (settings, "cups-job-billing", value);
3306 else if (strcmp (option->name, "gtk-job-prio") == 0)
3307 gtk_print_settings_set (settings, "cups-job-priority", value);
3308 else if (strcmp (option->name, "gtk-cover-before") == 0)
3309 gtk_print_settings_set (settings, "cover-before", value);
3310 else if (strcmp (option->name, "gtk-cover-after") == 0)
3311 gtk_print_settings_set (settings, "cover-after", value);
3312 else if (strcmp (option->name, "gtk-print-time") == 0)
3313 gtk_print_settings_set (settings, "print-at", value);
3314 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3315 gtk_print_settings_set (settings, "print-at-time", value);
3316 else if (g_str_has_prefix (option->name, "cups-"))
3317 gtk_print_settings_set (settings, option->name, value);
3321 cups_printer_get_settings_from_options (GtkPrinter *printer,
3322 GtkPrinterOptionSet *options,
3323 GtkPrintSettings *settings)
3325 struct OptionData data;
3326 const char *print_at, *print_at_time;
3328 data.printer = printer;
3329 data.options = options;
3330 data.settings = settings;
3331 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3333 if (data.ppd_file != NULL)
3335 GtkPrinterOption *cover_before, *cover_after;
3337 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
3339 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
3340 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
3341 if (cover_before && cover_after)
3343 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
3344 gtk_print_settings_set (settings, "cups-job-sheets", value);
3348 print_at = gtk_print_settings_get (settings, "print-at");
3349 print_at_time = gtk_print_settings_get (settings, "print-at-time");
3350 if (strcmp (print_at, "at") == 0)
3351 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
3352 else if (strcmp (print_at, "on-hold") == 0)
3353 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
3358 cups_printer_prepare_for_print (GtkPrinter *printer,
3359 GtkPrintJob *print_job,
3360 GtkPrintSettings *settings,
3361 GtkPageSetup *page_setup)
3363 GtkPageSet page_set;
3364 GtkPaperSize *paper_size;
3365 const char *ppd_paper_name;
3368 print_job->print_pages = gtk_print_settings_get_print_pages (settings);
3369 print_job->page_ranges = NULL;
3370 print_job->num_page_ranges = 0;
3372 if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
3373 print_job->page_ranges =
3374 gtk_print_settings_get_page_ranges (settings,
3375 &print_job->num_page_ranges);
3377 if (gtk_print_settings_get_collate (settings))
3378 gtk_print_settings_set (settings, "cups-Collate", "True");
3379 print_job->collate = FALSE;
3381 if (gtk_print_settings_get_reverse (settings))
3382 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
3383 print_job->reverse = FALSE;
3385 if (gtk_print_settings_get_n_copies (settings) > 1)
3386 gtk_print_settings_set_int (settings, "cups-copies",
3387 gtk_print_settings_get_n_copies (settings));
3388 print_job->num_copies = 1;
3390 scale = gtk_print_settings_get_scale (settings);
3391 print_job->scale = 1.0;
3393 print_job->scale = scale/100.0;
3395 page_set = gtk_print_settings_get_page_set (settings);
3396 if (page_set == GTK_PAGE_SET_EVEN)
3397 gtk_print_settings_set (settings, "cups-page-set", "even");
3398 else if (page_set == GTK_PAGE_SET_ODD)
3399 gtk_print_settings_set (settings, "cups-page-set", "odd");
3400 print_job->page_set = GTK_PAGE_SET_ALL;
3402 paper_size = gtk_page_setup_get_paper_size (page_setup);
3403 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
3404 if (ppd_paper_name != NULL)
3405 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
3408 char width[G_ASCII_DTOSTR_BUF_SIZE];
3409 char height[G_ASCII_DTOSTR_BUF_SIZE];
3412 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3413 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3414 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
3415 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
3416 g_free (custom_name);
3419 print_job->rotate_to_orientation = TRUE;
3422 static GtkPageSetup *
3423 create_page_setup (ppd_file_t *ppd_file,
3427 GtkPageSetup *page_setup;
3428 GtkPaperSize *paper_size;
3429 ppd_option_t *option;
3430 ppd_choice_t *choice;
3432 display_name = NULL;
3433 option = ppdFindOption (ppd_file, "PageSize");
3436 choice = ppdFindChoice (option, size->name);
3438 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
3441 if (display_name == NULL)
3442 display_name = g_strdup (size->name);
3444 page_setup = gtk_page_setup_new ();
3445 paper_size = gtk_paper_size_new_from_ppd (size->name,
3449 gtk_page_setup_set_paper_size (page_setup, paper_size);
3450 gtk_paper_size_free (paper_size);
3452 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
3453 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
3454 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
3455 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
3457 g_free (display_name);
3463 cups_printer_list_papers (GtkPrinter *printer)
3465 ppd_file_t *ppd_file;
3467 GtkPageSetup *page_setup;
3471 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3472 if (ppd_file == NULL)
3477 for (i = 0; i < ppd_file->num_sizes; i++)
3479 size = &ppd_file->sizes[i];
3481 page_setup = create_page_setup (ppd_file, size);
3483 l = g_list_prepend (l, page_setup);
3486 return g_list_reverse (l);
3489 static GtkPageSetup *
3490 cups_printer_get_default_page_size (GtkPrinter *printer)
3492 ppd_file_t *ppd_file;
3494 ppd_option_t *option;
3497 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3498 if (ppd_file == NULL)
3501 option = ppdFindOption (ppd_file, "PageSize");
3502 size = ppdPageSize (ppd_file, option->defchoice);
3504 return create_page_setup (ppd_file, size);
3508 cups_printer_get_hard_margins (GtkPrinter *printer,
3514 ppd_file_t *ppd_file;
3516 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3517 if (ppd_file == NULL)
3520 *left = ppd_file->custom_margins[0];
3521 *bottom = ppd_file->custom_margins[1];
3522 *right = ppd_file->custom_margins[2];
3523 *top = ppd_file->custom_margins[3];
3526 static GtkPrintCapabilities
3527 cups_printer_get_capabilities (GtkPrinter *printer)
3530 GTK_PRINT_CAPABILITY_COPIES |
3531 GTK_PRINT_CAPABILITY_COLLATE |
3532 GTK_PRINT_CAPABILITY_REVERSE |
3533 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 1 && CUPS_VERSION_PATCH >= 15) || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR > 1) || CUPS_VERSION_MAJOR > 1
3534 GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT |
3536 GTK_PRINT_CAPABILITY_NUMBER_UP;