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;
111 static GObjectClass *backend_parent_class;
113 static void gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class);
114 static void gtk_print_backend_cups_init (GtkPrintBackendCups *impl);
115 static void gtk_print_backend_cups_finalize (GObject *object);
116 static void gtk_print_backend_cups_dispose (GObject *object);
117 static void cups_get_printer_list (GtkPrintBackend *print_backend);
118 static void cups_request_execute (GtkPrintBackendCups *print_backend,
119 GtkCupsRequest *request,
120 GtkPrintCupsResponseCallbackFunc callback,
122 GDestroyNotify notify);
123 static void cups_printer_get_settings_from_options (GtkPrinter *printer,
124 GtkPrinterOptionSet *options,
125 GtkPrintSettings *settings);
126 static gboolean cups_printer_mark_conflicts (GtkPrinter *printer,
127 GtkPrinterOptionSet *options);
128 static GtkPrinterOptionSet *cups_printer_get_options (GtkPrinter *printer,
129 GtkPrintSettings *settings,
130 GtkPageSetup *page_setup,
131 GtkPrintCapabilities capabilities);
132 static void cups_printer_prepare_for_print (GtkPrinter *printer,
133 GtkPrintJob *print_job,
134 GtkPrintSettings *settings,
135 GtkPageSetup *page_setup);
136 static GList * cups_printer_list_papers (GtkPrinter *printer);
137 static GtkPageSetup * cups_printer_get_default_page_size (GtkPrinter *printer);
138 static void cups_printer_request_details (GtkPrinter *printer);
139 static void cups_request_default_printer (GtkPrintBackendCups *print_backend);
140 static void cups_request_ppd (GtkPrinter *printer);
141 static void cups_printer_get_hard_margins (GtkPrinter *printer,
146 static GtkPrintCapabilities cups_printer_get_capabilities (GtkPrinter *printer);
147 static void set_option_from_settings (GtkPrinterOption *option,
148 GtkPrintSettings *setting);
149 static void cups_begin_polling_info (GtkPrintBackendCups *print_backend,
152 static gboolean cups_job_info_poll_timeout (gpointer user_data);
153 static void gtk_print_backend_cups_print_stream (GtkPrintBackend *backend,
156 GtkPrintJobCompleteFunc callback,
158 GDestroyNotify dnotify);
159 static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter *printer,
160 GtkPrintSettings *settings,
163 GIOChannel *cache_io);
167 gtk_print_backend_cups_register_type (GTypeModule *module)
169 static const GTypeInfo print_backend_cups_info =
171 sizeof (GtkPrintBackendCupsClass),
172 NULL, /* base_init */
173 NULL, /* base_finalize */
174 (GClassInitFunc) gtk_print_backend_cups_class_init,
175 NULL, /* class_finalize */
176 NULL, /* class_data */
177 sizeof (GtkPrintBackendCups),
179 (GInstanceInitFunc) gtk_print_backend_cups_init
182 print_backend_cups_type = g_type_module_register_type (module,
183 GTK_TYPE_PRINT_BACKEND,
184 "GtkPrintBackendCups",
185 &print_backend_cups_info, 0);
189 pb_module_init (GTypeModule *module)
192 g_print ("CUPS Backend: Initializing the CUPS print backend module\n"));
194 gtk_print_backend_cups_register_type (module);
195 gtk_printer_cups_register_type (module);
199 pb_module_exit (void)
204 G_MODULE_EXPORT GtkPrintBackend *
205 pb_module_create (void)
207 return gtk_print_backend_cups_new ();
211 * GtkPrintBackendCups
214 gtk_print_backend_cups_get_type (void)
216 return print_backend_cups_type;
220 * gtk_print_backend_cups_new:
222 * Creates a new #GtkPrintBackendCups object. #GtkPrintBackendCups
223 * implements the #GtkPrintBackend interface with direct access to
224 * the filesystem using Unix/Linux API calls
226 * Return value: the new #GtkPrintBackendCups object
229 gtk_print_backend_cups_new (void)
232 g_print ("CUPS Backend: Creating a new CUPS print backend object\n"));
234 return g_object_new (GTK_TYPE_PRINT_BACKEND_CUPS, NULL);
238 gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
240 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
241 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
243 backend_parent_class = g_type_class_peek_parent (class);
245 gobject_class->finalize = gtk_print_backend_cups_finalize;
246 gobject_class->dispose = gtk_print_backend_cups_dispose;
248 backend_class->request_printer_list = cups_get_printer_list;
249 backend_class->print_stream = gtk_print_backend_cups_print_stream;
250 backend_class->printer_request_details = cups_printer_request_details;
251 backend_class->printer_create_cairo_surface = cups_printer_create_cairo_surface;
252 backend_class->printer_get_options = cups_printer_get_options;
253 backend_class->printer_mark_conflicts = cups_printer_mark_conflicts;
254 backend_class->printer_get_settings_from_options = cups_printer_get_settings_from_options;
255 backend_class->printer_prepare_for_print = cups_printer_prepare_for_print;
256 backend_class->printer_list_papers = cups_printer_list_papers;
257 backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
258 backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
259 backend_class->printer_get_capabilities = cups_printer_get_capabilities;
262 static cairo_status_t
263 _cairo_write_to_cups (void *closure,
264 const unsigned char *data,
267 GIOChannel *io = (GIOChannel *)closure;
274 g_print ("CUPS Backend: Writing %i byte chunk to temp file\n", length));
278 g_io_channel_write_chars (io, (gchar *)data, length, &written, &error);
283 g_print ("CUPS Backend: Error writing to temp file, %s\n",
286 g_error_free (error);
287 return CAIRO_STATUS_WRITE_ERROR;
291 g_print ("CUPS Backend: Wrote %i bytes to temp file\n", written));
297 return CAIRO_STATUS_SUCCESS;
300 static cairo_surface_t *
301 cups_printer_create_cairo_surface (GtkPrinter *printer,
302 GtkPrintSettings *settings,
305 GIOChannel *cache_io)
307 cairo_surface_t *surface;
308 ppd_file_t *ppd_file = NULL;
309 ppd_attr_t *ppd_attr = NULL;
312 /* TODO: check if it is a ps or pdf printer */
314 surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
316 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
318 if (ppd_file != NULL)
320 ppd_attr = ppdFindAttr (ppd_file, "LanguageLevel", NULL);
322 if (ppd_attr != NULL)
323 level = atoi (ppd_attr->value);
327 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
330 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
332 /* TODO: DPI from settings object? */
333 cairo_surface_set_fallback_resolution (surface, 300, 300);
339 GtkPrintJobCompleteFunc callback;
342 GDestroyNotify dnotify;
343 } CupsPrintStreamData;
346 cups_free_print_stream_data (CupsPrintStreamData *data)
349 g_print ("CUPS Backend: %s\n", G_STRFUNC));
352 data->dnotify (data->user_data);
353 g_object_unref (data->job);
358 cups_print_cb (GtkPrintBackendCups *print_backend,
359 GtkCupsResult *result,
362 GError *error = NULL;
363 CupsPrintStreamData *ps = user_data;
365 GDK_THREADS_ENTER ();
368 g_print ("CUPS Backend: %s\n", G_STRFUNC));
370 if (gtk_cups_result_is_error (result))
371 error = g_error_new_literal (gtk_print_error_quark (),
372 GTK_PRINT_ERROR_INTERNAL_ERROR,
373 gtk_cups_result_get_error_string (result));
376 ps->callback (ps->job, ps->user_data, error);
381 ipp_attribute_t *attr; /* IPP job-id attribute */
382 ipp_t *response = gtk_cups_result_get_response (result);
384 if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
385 job_id = attr->values[0].integer;
387 if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
388 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
391 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_PENDING);
392 cups_begin_polling_info (print_backend, ps->job, job_id);
396 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED_ABORTED);
400 g_error_free (error);
402 GDK_THREADS_LEAVE ();
406 add_cups_options (const gchar *key,
410 GtkCupsRequest *request = user_data;
412 if (!g_str_has_prefix (key, "cups-"))
415 if (strcmp (value, "gtk-ignore-value") == 0)
418 key = key + strlen ("cups-");
420 gtk_cups_request_encode_option (request, key, value);
424 gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
427 GtkPrintJobCompleteFunc callback,
429 GDestroyNotify dnotify)
431 GtkPrinterCups *cups_printer;
432 CupsPrintStreamData *ps;
433 GtkCupsRequest *request;
434 GtkPrintSettings *settings;
436 char printer_absolute_uri[HTTP_MAX_URI];
439 g_print ("CUPS Backend: %s\n", G_STRFUNC));
441 cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
442 settings = gtk_print_job_get_settings (job);
444 request = gtk_cups_request_new (NULL,
449 cups_printer->device_uri);
451 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
452 httpAssembleURIf (HTTP_URI_CODING_ALL,
453 printer_absolute_uri,
454 sizeof (printer_absolute_uri),
460 gtk_printer_get_name (gtk_print_job_get_printer (job)));
462 g_snprintf (printer_absolute_uri,
463 sizeof (printer_absolute_uri),
464 "ipp://localhost:%d/printers/%s",
466 gtk_printer_get_name (gtk_print_job_get_printer (job)));
469 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
470 IPP_TAG_URI, "printer-uri",
471 NULL, printer_absolute_uri);
473 title = gtk_print_job_get_title (job);
475 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
476 IPP_TAG_NAME, "job-name",
479 gtk_print_settings_foreach (settings, add_cups_options, request);
481 ps = g_new0 (CupsPrintStreamData, 1);
482 ps->callback = callback;
483 ps->user_data = user_data;
484 ps->dnotify = dnotify;
485 ps->job = g_object_ref (job);
487 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
489 (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
491 (GDestroyNotify)cups_free_print_stream_data);
496 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
498 backend_cups->list_printers_poll = FALSE;
499 backend_cups->got_default_printer = FALSE;
500 backend_cups->list_printers_pending = FALSE;
502 cups_request_default_printer (backend_cups);
506 gtk_print_backend_cups_finalize (GObject *object)
508 GtkPrintBackendCups *backend_cups;
511 g_print ("CUPS Backend: finalizing CUPS backend module\n"));
513 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
515 g_free (backend_cups->default_printer);
516 backend_cups->default_printer = NULL;
518 backend_parent_class->finalize (object);
522 gtk_print_backend_cups_dispose (GObject *object)
524 GtkPrintBackendCups *backend_cups;
527 g_print ("CUPS Backend: %s\n", G_STRFUNC));
529 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
531 if (backend_cups->list_printers_poll > 0)
532 g_source_remove (backend_cups->list_printers_poll);
533 backend_cups->list_printers_poll = 0;
535 backend_parent_class->dispose (object);
540 cups_dispatch_watch_check (GSource *source)
542 GtkPrintCupsDispatchWatch *dispatch;
543 GtkCupsPollState poll_state;
547 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
549 dispatch = (GtkPrintCupsDispatchWatch *) source;
551 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
553 if (dispatch->request->http != NULL)
555 if (dispatch->data_poll == NULL)
557 dispatch->data_poll = g_new0 (GPollFD, 1);
558 g_source_add_poll (source, dispatch->data_poll);
562 if (poll_state == GTK_CUPS_HTTP_READ)
563 dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
564 else if (poll_state == GTK_CUPS_HTTP_WRITE)
565 dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
567 dispatch->data_poll->events = 0;
570 #ifdef HAVE_CUPS_API_1_2
571 dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
573 dispatch->data_poll->fd = dispatch->request->http->fd;
577 if (poll_state != GTK_CUPS_HTTP_IDLE)
578 if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
581 result = gtk_cups_request_read_write (dispatch->request);
582 if (result && dispatch->data_poll != NULL)
584 g_source_remove_poll (source, dispatch->data_poll);
585 g_free (dispatch->data_poll);
586 dispatch->data_poll = NULL;
593 cups_dispatch_watch_prepare (GSource *source,
596 GtkPrintCupsDispatchWatch *dispatch;
598 dispatch = (GtkPrintCupsDispatchWatch *) source;
601 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
605 return gtk_cups_request_read_write (dispatch->request);
609 cups_dispatch_watch_dispatch (GSource *source,
610 GSourceFunc callback,
613 GtkPrintCupsDispatchWatch *dispatch;
614 GtkPrintCupsResponseCallbackFunc ep_callback;
615 GtkCupsResult *result;
617 g_assert (callback != NULL);
619 ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
621 dispatch = (GtkPrintCupsDispatchWatch *) source;
623 result = gtk_cups_request_get_result (dispatch->request);
626 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
628 if (gtk_cups_result_is_error (result))
631 g_print("Error result: %s (type %i, status %i, code %i)\n",
632 gtk_cups_result_get_error_string (result),
633 gtk_cups_result_get_error_type (result),
634 gtk_cups_result_get_error_status (result),
635 gtk_cups_result_get_error_code (result)));
638 ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
644 cups_dispatch_watch_finalize (GSource *source)
646 GtkPrintCupsDispatchWatch *dispatch;
649 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
651 dispatch = (GtkPrintCupsDispatchWatch *) source;
653 gtk_cups_request_free (dispatch->request);
655 if (dispatch->backend)
657 /* We need to unref this at idle time, because it might be the
658 * last reference to this module causing the code to be
659 * unloaded (including this particular function!)
660 * Update: Doing this at idle caused a deadlock taking the
661 * mainloop context lock while being in a GSource callout for
662 * multithreaded apps. So, for now we just disable unloading
663 * of print backends. See _gtk_print_backend_create for the
666 g_object_unref (dispatch->backend);
667 dispatch->backend = NULL;
670 g_free (dispatch->data_poll);
673 static GSourceFuncs _cups_dispatch_watch_funcs = {
674 cups_dispatch_watch_prepare,
675 cups_dispatch_watch_check,
676 cups_dispatch_watch_dispatch,
677 cups_dispatch_watch_finalize
682 cups_request_execute (GtkPrintBackendCups *print_backend,
683 GtkCupsRequest *request,
684 GtkPrintCupsResponseCallbackFunc callback,
686 GDestroyNotify notify)
688 GtkPrintCupsDispatchWatch *dispatch;
690 dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
691 sizeof (GtkPrintCupsDispatchWatch));
694 g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
696 dispatch->request = request;
697 dispatch->backend = g_object_ref (print_backend);
698 dispatch->data_poll = NULL;
700 g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
702 g_source_attach ((GSource *) dispatch, NULL);
703 g_source_unref ((GSource *) dispatch);
708 cups_request_printer_info_cb (GtkPrintBackendCups *backend,
709 GtkCupsResult *result,
712 ipp_attribute_t *attr;
715 GtkPrinterCups *cups_printer;
721 gboolean status_changed;
723 g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
725 printer_name = (gchar *)user_data;
726 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
730 g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
735 g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
739 cups_printer = GTK_PRINTER_CUPS (printer);
741 if (gtk_cups_result_is_error (result))
743 if (gtk_printer_is_new (printer))
745 gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
750 return; /* TODO: mark as inactive printer */
753 response = gtk_cups_result_get_response (result);
755 /* TODO: determine printer type and use correct icon */
756 gtk_printer_set_icon_name (printer, "gtk-print");
762 for (attr = response->attrs; attr != NULL; attr = attr->next)
767 _CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
768 _CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
769 _CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
770 _CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
771 _CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
774 status_changed = gtk_printer_set_job_count (printer, job_count);
776 status_changed |= gtk_printer_set_location (printer, loc);
777 status_changed |= gtk_printer_set_description (printer, desc);
778 status_changed |= gtk_printer_set_state_message (printer, state_msg);
781 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
782 "printer-status-changed", printer);
786 cups_request_printer_info (GtkPrintBackendCups *print_backend,
787 const gchar *printer_name)
789 GtkCupsRequest *request;
791 static const char * const pattrs[] = /* Attributes we're interested in */
795 "printer-state-message",
800 request = gtk_cups_request_new (NULL,
802 IPP_GET_PRINTER_ATTRIBUTES,
807 printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
809 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
810 "printer-uri", NULL, printer_uri);
813 g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
815 g_free (printer_uri);
817 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
818 "requested-attributes", G_N_ELEMENTS (pattrs),
821 cups_request_execute (print_backend,
823 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
824 g_strdup (printer_name),
825 (GDestroyNotify) g_free);
830 GtkPrintBackendCups *print_backend;
837 job_object_died (gpointer user_data,
838 GObject *where_the_object_was)
840 CupsJobPollData *data = user_data;
845 cups_job_poll_data_free (CupsJobPollData *data)
848 g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
854 cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
855 GtkCupsResult *result,
858 CupsJobPollData *data = user_data;
859 ipp_attribute_t *attr;
864 GDK_THREADS_ENTER ();
866 if (data->job == NULL)
868 cups_job_poll_data_free (data);
874 response = gtk_cups_result_get_response (result);
877 for (attr = response->attrs; attr != NULL; attr = attr->next)
882 _CUPS_MAP_ATTR_INT (attr, state, "job-state");
888 case IPP_JOB_PENDING:
890 case IPP_JOB_STOPPED:
891 gtk_print_job_set_status (data->job,
892 GTK_PRINT_STATUS_PENDING);
894 case IPP_JOB_PROCESSING:
895 gtk_print_job_set_status (data->job,
896 GTK_PRINT_STATUS_PRINTING);
899 case IPP_JOB_CANCELLED:
900 case IPP_JOB_ABORTED:
901 gtk_print_job_set_status (data->job,
902 GTK_PRINT_STATUS_FINISHED_ABORTED);
906 case IPP_JOB_COMPLETED:
907 gtk_print_job_set_status (data->job,
908 GTK_PRINT_STATUS_FINISHED);
913 if (!done && data->job != NULL)
917 if (data->counter < 5)
919 else if (data->counter < 10)
924 g_timeout_add (timeout, cups_job_info_poll_timeout, data);
927 cups_job_poll_data_free (data);
930 GDK_THREADS_LEAVE ();
934 cups_request_job_info (CupsJobPollData *data)
936 GtkCupsRequest *request;
939 request = gtk_cups_request_new (NULL,
941 IPP_GET_JOB_ATTRIBUTES,
946 job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
947 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
948 "job-uri", NULL, job_uri);
951 cups_request_execute (data->print_backend,
953 (GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
959 cups_job_info_poll_timeout (gpointer user_data)
961 CupsJobPollData *data = user_data;
963 if (data->job == NULL)
964 cups_job_poll_data_free (data);
966 cups_request_job_info (data);
972 cups_begin_polling_info (GtkPrintBackendCups *print_backend,
976 CupsJobPollData *data;
978 data = g_new0 (CupsJobPollData, 1);
980 data->print_backend = print_backend;
982 data->job_id = job_id;
985 g_object_weak_ref (G_OBJECT (job), job_object_died, data);
987 cups_request_job_info (data);
991 mark_printer_inactive (GtkPrinter *printer,
992 GtkPrintBackend *backend)
994 gtk_printer_set_is_active (printer, FALSE);
995 g_signal_emit_by_name (backend, "printer-removed", printer);
999 find_printer (GtkPrinter *printer,
1000 const gchar *find_name)
1002 const gchar *printer_name;
1004 printer_name = gtk_printer_get_name (printer);
1005 return g_ascii_strcasecmp (printer_name, find_name);
1009 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
1010 GtkCupsResult *result,
1013 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
1014 ipp_attribute_t *attr;
1016 gboolean list_has_changed;
1017 GList *removed_printer_checklist;
1019 GDK_THREADS_ENTER ();
1021 list_has_changed = FALSE;
1024 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1026 cups_backend->list_printers_pending = FALSE;
1028 if (gtk_cups_result_is_error (result))
1031 g_warning ("CUPS Backend: Error getting printer list: %s",
1032 gtk_cups_result_get_error_string (result)));
1037 /* Gather the names of the printers in the current queue
1038 * so we may check to see if they were removed
1040 removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
1042 response = gtk_cups_result_get_response (result);
1044 for (attr = response->attrs; attr != NULL; attr = attr->next)
1046 GtkPrinter *printer;
1047 const gchar *printer_name = NULL;
1048 const gchar *printer_uri = NULL;
1049 const gchar *member_uris = NULL;
1050 const gchar *location = NULL;
1051 const gchar *description = NULL;
1052 const gchar *state_msg = NULL;
1055 gboolean status_changed = FALSE;
1058 const gchar *reason_msg = NULL;
1059 gchar *reason_msg_desc = NULL;
1060 gchar *tmp_msg = NULL;
1061 gint printer_state_reason_level = 0; /* 0 - none, 1 - report, 2 - warning, 3 - error */
1062 gboolean interested_in = FALSE;
1063 gboolean found = FALSE;
1064 static const char * const reasons[] = /* Reasons we're interested in */
1070 "marker-supply-low",
1071 "marker-supply-empty",
1077 "connecting-to-device",
1080 static const char * reasons_descs[] =
1082 N_("Printer '%s' is low on toner."),
1083 N_("Printer '%s' has no toner left."),
1084 N_("Printer '%s' is low on developer."),
1085 N_("Printer '%s' is out of developer."),
1086 N_("Printer '%s' is low on at least one marker supply."),
1087 N_("Printer '%s' is out of at least one marker supply."),
1088 N_("The cover is open on printer '%s'."),
1089 N_("The door is open on printer '%s'."),
1090 N_("Printer '%s' is low on paper."),
1091 N_("Printer '%s' is out of paper."),
1092 N_("Printer '%s' is currently off-line."),
1093 N_("Printer '%s' may not be connected."),
1094 N_("There is a problem on printer '%s'.")
1097 /* Skip leading attributes until we hit a printer...
1099 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1105 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1107 if (strcmp (attr->name, "printer-name") == 0 &&
1108 attr->value_tag == IPP_TAG_NAME)
1109 printer_name = attr->values[0].string.text;
1110 else if (strcmp (attr->name, "printer-uri-supported") == 0 &&
1111 attr->value_tag == IPP_TAG_URI)
1112 printer_uri = attr->values[0].string.text;
1113 else if (strcmp (attr->name, "member-uris") == 0 &&
1114 attr->value_tag == IPP_TAG_URI)
1115 member_uris = attr->values[0].string.text;
1116 else if (strcmp (attr->name, "printer-location") == 0)
1117 location = attr->values[0].string.text;
1118 else if (strcmp (attr->name, "printer-info") == 0)
1119 description = attr->values[0].string.text;
1120 else if (strcmp (attr->name, "printer-state-message") == 0)
1121 state_msg = attr->values[0].string.text;
1122 else if (strcmp (attr->name, "printer-state-reasons") == 0)
1123 /* Store most important reason to reason_msg and set
1124 its importance at printer_state_reason_level */
1126 for (i = 0; i < attr->num_values; i++)
1128 if (strcmp (attr->values[i].string.text, "none") != 0)
1130 interested_in = FALSE;
1131 for (j = 0; j < G_N_ELEMENTS (reasons); j++)
1132 if (strncmp (attr->values[i].string.text, reasons[j], strlen (reasons[j])) == 0)
1134 interested_in = TRUE;
1140 if (g_str_has_suffix (attr->values[i].string.text, "-report"))
1142 if (printer_state_reason_level <= 1)
1144 reason_msg = attr->values[i].string.text;
1145 printer_state_reason_level = 1;
1148 else if (g_str_has_suffix (attr->values[i].string.text, "-warning"))
1150 if (printer_state_reason_level <= 2)
1152 reason_msg = attr->values[i].string.text;
1153 printer_state_reason_level = 2;
1156 else /* It is error in the case of no suffix. */
1158 reason_msg = attr->values[i].string.text;
1159 printer_state_reason_level = 3;
1165 else if (strcmp (attr->name, "printer-state") == 0)
1166 state = attr->values[0].integer;
1167 else if (strcmp (attr->name, "queued-job-count") == 0)
1168 job_count = attr->values[0].integer;
1172 g_print ("CUPS Backend: Attribute %s ignored", attr->name));
1178 if (printer_name == NULL ||
1179 (printer_uri == NULL && member_uris == NULL))
1187 /* remove name from checklist if it was found */
1188 node = g_list_find_custom (removed_printer_checklist, printer_name, (GCompareFunc) find_printer);
1189 removed_printer_checklist = g_list_delete_link (removed_printer_checklist, node);
1191 printer = gtk_print_backend_find_printer (backend, printer_name);
1194 GtkPrinterCups *cups_printer;
1195 char uri[HTTP_MAX_URI]; /* Printer URI */
1196 char method[HTTP_MAX_URI]; /* Method/scheme name */
1197 char username[HTTP_MAX_URI]; /* Username:password */
1198 char hostname[HTTP_MAX_URI]; /* Hostname */
1199 char resource[HTTP_MAX_URI]; /* Resource name */
1200 int port; /* Port number */
1202 list_has_changed = TRUE;
1203 cups_printer = gtk_printer_cups_new (printer_name, backend);
1205 cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
1207 /* Check to see if we are looking at a class */
1210 cups_printer->printer_uri = g_strdup (member_uris);
1211 /* TODO if member_uris is a class we need to recursivly find a printer */
1213 g_print ("CUPS Backend: Found class with printer %s\n", member_uris));
1217 cups_printer->printer_uri = g_strdup (printer_uri);
1219 g_print ("CUPS Backend: Found printer %s\n", printer_uri));
1222 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
1223 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
1224 method, sizeof (method),
1225 username, sizeof (username),
1226 hostname, sizeof (hostname),
1228 resource, sizeof (resource));
1231 httpSeparate (cups_printer->printer_uri,
1239 if (strncmp (resource, "/printers/", 10) == 0)
1241 cups_printer->ppd_name = g_strdup (resource + 10);
1243 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
1246 gethostname (uri, sizeof (uri));
1247 if (strcasecmp (uri, hostname) == 0)
1248 strcpy (hostname, "localhost");
1250 cups_printer->hostname = g_strdup (hostname);
1251 cups_printer->port = port;
1253 printer = GTK_PRINTER (cups_printer);
1255 if (cups_backend->default_printer != NULL &&
1256 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
1257 gtk_printer_set_is_default (printer, TRUE);
1260 gtk_print_backend_add_printer (backend, printer);
1263 g_object_ref (printer);
1265 if (!gtk_printer_is_active (printer))
1267 gtk_printer_set_is_active (printer, TRUE);
1268 gtk_printer_set_is_new (printer, TRUE);
1269 list_has_changed = TRUE;
1272 if (gtk_printer_is_new (printer))
1274 g_signal_emit_by_name (backend, "printer-added", printer);
1276 gtk_printer_set_is_new (printer, FALSE);
1280 /* Getting printer info with separate requests overwhelms cups
1281 * when the printer list has more than a handful of printers.
1283 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
1286 GTK_PRINTER_CUPS (printer)->state = state;
1287 status_changed = gtk_printer_set_job_count (printer, job_count);
1288 status_changed |= gtk_printer_set_location (printer, location);
1289 status_changed |= gtk_printer_set_description (printer, description);
1291 /* Set description of the reason and combine it with printer-state-message. */
1292 if ( (reason_msg != NULL))
1294 for (i = 0; i < G_N_ELEMENTS (reasons); i++)
1296 if (strncmp (reason_msg, reasons[i], strlen (reasons[i])) == 0)
1298 reason_msg_desc = g_strdup_printf (reasons_descs[i], printer_name);
1305 printer_state_reason_level = 0;
1307 if (printer_state_reason_level >= 2)
1309 if (strlen (state_msg) == 0)
1310 state_msg = reason_msg_desc;
1313 tmp_msg = g_strjoin (" ; ", state_msg, reason_msg_desc, NULL);
1314 state_msg = tmp_msg;
1319 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1321 if (tmp_msg != NULL)
1324 if (reason_msg_desc != NULL)
1325 g_free (reason_msg_desc);
1327 /* Set printer icon according to importance
1328 (none, report, warning, error - report is omitted). */
1329 if (printer_state_reason_level == 3)
1330 gtk_printer_set_icon_name (printer, "gtk-print-error");
1331 else if (printer_state_reason_level == 2)
1332 gtk_printer_set_icon_name (printer, "gtk-print-warning");
1334 gtk_printer_set_icon_name (printer, "gtk-print");
1337 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1338 "printer-status-changed", printer);
1340 /* The ref is held by GtkPrintBackend, in add_printer() */
1341 g_object_unref (printer);
1347 /* look at the removed printers checklist and mark any printer
1348 as inactive if it is in the list, emitting a printer_removed signal */
1349 if (removed_printer_checklist != NULL)
1351 g_list_foreach (removed_printer_checklist, (GFunc) mark_printer_inactive, backend);
1352 g_list_free (removed_printer_checklist);
1353 list_has_changed = TRUE;
1357 if (list_has_changed)
1358 g_signal_emit_by_name (backend, "printer-list-changed");
1360 gtk_print_backend_set_list_done (backend);
1362 GDK_THREADS_LEAVE ();
1366 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
1368 GtkCupsRequest *request;
1369 static const char * const pattrs[] = /* Attributes we're interested in */
1372 "printer-uri-supported",
1376 "printer-state-message",
1377 "printer-state-reasons",
1382 if (cups_backend->list_printers_pending ||
1383 !cups_backend->got_default_printer)
1386 cups_backend->list_printers_pending = TRUE;
1388 request = gtk_cups_request_new (NULL,
1395 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1396 "requested-attributes", G_N_ELEMENTS (pattrs),
1399 cups_request_execute (cups_backend,
1401 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
1409 cups_get_printer_list (GtkPrintBackend *backend)
1411 GtkPrintBackendCups *cups_backend;
1413 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
1414 if (cups_backend->list_printers_poll == 0)
1416 cups_request_printer_list (cups_backend);
1417 cups_backend->list_printers_poll = gdk_threads_add_timeout (3000,
1418 (GSourceFunc) cups_request_printer_list,
1424 GtkPrinterCups *printer;
1430 get_ppd_data_free (GetPPDData *data)
1433 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1434 httpClose (data->http);
1435 g_io_channel_unref (data->ppd_io);
1436 g_object_unref (data->printer);
1441 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
1442 GtkCupsResult *result,
1446 GtkPrinter *printer;
1448 GDK_THREADS_ENTER ();
1451 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1453 printer = GTK_PRINTER (data->printer);
1454 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
1456 if (gtk_cups_result_is_error (result))
1458 gboolean success = FALSE;
1460 /* if we get a 404 then it is just a raw printer without a ppd
1462 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
1463 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
1465 gtk_printer_set_has_details (printer, TRUE);
1469 g_signal_emit_by_name (printer, "details-acquired", success);
1473 response = gtk_cups_result_get_response (result);
1475 /* let ppdOpenFd take over the ownership of the open file */
1476 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
1477 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
1479 ppdMarkDefaults (data->printer->ppd_file);
1481 gtk_printer_set_has_details (printer, TRUE);
1482 g_signal_emit_by_name (printer, "details-acquired", TRUE);
1485 GDK_THREADS_LEAVE ();
1489 cups_request_ppd (GtkPrinter *printer)
1492 GtkPrintBackend *print_backend;
1493 GtkPrinterCups *cups_printer;
1494 GtkCupsRequest *request;
1501 cups_printer = GTK_PRINTER_CUPS (printer);
1506 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1508 http = httpConnectEncrypt (cups_printer->hostname,
1512 data = g_new0 (GetPPDData, 1);
1514 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
1518 #ifdef G_ENABLE_DEBUG
1519 /* If we are debugging printing don't delete the tmp files */
1520 if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
1521 unlink (ppd_filename);
1523 unlink (ppd_filename);
1524 #endif /* G_ENABLE_DEBUG */
1529 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
1531 g_error_free (error);
1533 g_free (ppd_filename);
1536 g_signal_emit_by_name (printer, "details-acquired", FALSE);
1541 fchmod (fd, S_IRUSR | S_IWUSR);
1542 data->ppd_io = g_io_channel_unix_new (fd);
1543 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
1544 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
1546 data->printer = g_object_ref (printer);
1548 resource = g_strdup_printf ("/printers/%s.ppd",
1549 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
1551 request = gtk_cups_request_new (data->http,
1555 cups_printer->hostname,
1559 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
1562 g_free (ppd_filename);
1564 cups_printer->reading_ppd = TRUE;
1566 print_backend = gtk_printer_get_backend (printer);
1568 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
1570 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
1572 (GDestroyNotify)get_ppd_data_free);
1575 /* Ordering matters for default preference */
1576 static const char *lpoptions_locations[] = {
1577 "/etc/cups/lpoptions",
1583 cups_parse_user_default_printer (const char *filename,
1584 char **printer_name)
1587 char line[1024], *lineptr, *defname = NULL;
1589 if ((fp = g_fopen (filename, "r")) == NULL)
1592 while (fgets (line, sizeof (line), fp) != NULL)
1594 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
1598 while (isspace (*lineptr))
1605 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
1610 if (*printer_name != NULL)
1611 g_free (*printer_name);
1613 *printer_name = g_strdup (defname);
1620 cups_get_user_default_printer (char **printer_name)
1624 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1626 if (g_path_is_absolute (lpoptions_locations[i]))
1628 cups_parse_user_default_printer (lpoptions_locations[i],
1635 filename = g_build_filename (g_get_home_dir (),
1636 lpoptions_locations[i], NULL);
1637 cups_parse_user_default_printer (filename, printer_name);
1644 cups_parse_user_options (const char *filename,
1645 const char *printer_name,
1647 cups_option_t **options)
1650 gchar line[1024], *lineptr, *name;
1652 if ((fp = g_fopen (filename, "r")) == NULL)
1655 while (fgets (line, sizeof (line), fp) != NULL)
1657 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
1659 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
1664 /* Skip leading whitespace */
1665 while (isspace (*lineptr))
1671 /* NUL-terminate the name, stripping the instance name */
1673 while (!isspace (*lineptr) && *lineptr)
1675 if (*lineptr == '/')
1685 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
1688 /* We found our printer, parse the options */
1689 num_options = cupsParseOptions (lineptr, num_options, options);
1698 cups_get_user_options (const char *printer_name,
1700 cups_option_t **options)
1704 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1706 if (g_path_is_absolute (lpoptions_locations[i]))
1708 num_options = cups_parse_user_options (lpoptions_locations[i],
1717 filename = g_build_filename (g_get_home_dir (),
1718 lpoptions_locations[i], NULL);
1719 num_options = cups_parse_user_options (filename, printer_name,
1720 num_options, options);
1729 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
1730 GtkCupsResult *result,
1734 ipp_attribute_t *attr;
1736 response = gtk_cups_result_get_response (result);
1738 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
1739 print_backend->default_printer = g_strdup (attr->values[0].string.text);
1741 print_backend->got_default_printer = TRUE;
1743 /* Make sure to kick off get_printers if we are polling it,
1744 * as we could have blocked this reading the default printer
1746 if (print_backend->list_printers_poll != 0)
1747 cups_request_printer_list (print_backend);
1751 cups_request_default_printer (GtkPrintBackendCups *print_backend)
1753 GtkCupsRequest *request;
1757 if ((str = g_getenv ("LPDEST")) != NULL)
1759 print_backend->default_printer = g_strdup (str);
1760 print_backend->got_default_printer = TRUE;
1763 else if ((str = g_getenv ("PRINTER")) != NULL &&
1764 strcmp (str, "lp") != 0)
1766 print_backend->default_printer = g_strdup (str);
1767 print_backend->got_default_printer = TRUE;
1771 /* Figure out user setting for default printer */
1772 cups_get_user_default_printer (&name);
1775 print_backend->default_printer = name;
1776 print_backend->got_default_printer = TRUE;
1780 request = gtk_cups_request_new (NULL,
1787 cups_request_execute (print_backend,
1789 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
1790 g_object_ref (print_backend),
1795 cups_printer_request_details (GtkPrinter *printer)
1797 GtkPrinterCups *cups_printer;
1799 cups_printer = GTK_PRINTER_CUPS (printer);
1800 if (!cups_printer->reading_ppd &&
1801 gtk_printer_cups_get_ppd (cups_printer) == NULL)
1802 cups_request_ppd (printer);
1806 ppd_text_to_utf8 (ppd_file_t *ppd_file,
1809 const char *encoding = NULL;
1812 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
1814 return g_strdup (text);
1816 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
1818 encoding = "ISO-8859-1";
1820 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
1822 encoding = "ISO-8859-2";
1824 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
1826 encoding = "ISO-8859-5";
1828 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
1830 encoding = "SHIFT-JIS";
1832 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
1834 encoding = "MACINTOSH";
1836 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
1838 encoding = "WINDOWS-1252";
1842 /* Fallback, try iso-8859-1... */
1843 encoding = "ISO-8859-1";
1846 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
1851 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
1852 res = g_strdup ("???");
1858 /* TODO: Add more translations for common settings here */
1860 static const struct {
1861 const char *keyword;
1862 const char *translation;
1863 } cups_option_translations[] = {
1864 { "Duplex", N_("Two Sided") },
1865 { "MediaType", N_("Paper Type") },
1866 { "InputSlot", N_("Paper Source") },
1867 { "OutputBin", N_("Output Tray") },
1871 static const struct {
1872 const char *keyword;
1874 const char *translation;
1875 } cups_choice_translations[] = {
1876 { "Duplex", "None", N_("One Sided") },
1877 { "InputSlot", "Auto", N_("Auto Select") },
1878 { "InputSlot", "AutoSelect", N_("Auto Select") },
1879 { "InputSlot", "Default", N_("Printer Default") },
1880 { "InputSlot", "None", N_("Printer Default") },
1881 { "InputSlot", "PrinterDefault", N_("Printer Default") },
1882 { "InputSlot", "Unspecified", N_("Auto Select") },
1885 static const struct {
1886 const char *ppd_keyword;
1888 } option_names[] = {
1889 {"Duplex", "gtk-duplex" },
1890 {"MediaType", "gtk-paper-type"},
1891 {"InputSlot", "gtk-paper-source"},
1892 {"OutputBin", "gtk-output-tray"},
1895 /* keep sorted when changing */
1896 static const char *color_option_whitelist[] = {
1897 "BRColorEnhancement",
1903 "BlackSubstitution",
1911 "RPSBlackOverPrint",
1915 /* keep sorted when changing */
1916 static const char *color_group_whitelist[] = {
1923 "HPColorOptionsPanel",
1926 /* keep sorted when changing */
1927 static const char *image_quality_option_whitelist[] = {
1929 "BRHalfTonePattern",
1939 "HPGraphicsHalftone",
1954 /* keep sorted when changing */
1955 static const char *image_quality_group_whitelist[] = {
1962 /* keep sorted when changing */
1963 static const char * finishing_option_whitelist[] = {
1976 "StapleOrientation",
1982 /* keep sorted when changing */
1983 static const char *finishing_group_whitelist[] = {
1992 /* keep sorted when changing */
1993 static const char *cups_option_blacklist[] = {
2002 get_option_text (ppd_file_t *ppd_file,
2003 ppd_option_t *option)
2008 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
2010 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
2011 return g_strdup (_(cups_option_translations[i].translation));
2014 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
2016 /* Some ppd files have spaces in the text before the colon */
2023 get_choice_text (ppd_file_t *ppd_file,
2024 ppd_choice_t *choice)
2027 ppd_option_t *option = choice->option;
2028 const char *keyword = option->keyword;
2030 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
2032 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
2033 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
2034 return g_strdup (_(cups_choice_translations[i].translation));
2036 return ppd_text_to_utf8 (ppd_file, choice->text);
2040 group_has_option (ppd_group_t *group,
2041 ppd_option_t *option)
2048 if (group->num_options > 0 &&
2049 option >= group->options && option < group->options + group->num_options)
2052 for (i = 0; i < group->num_subgroups; i++)
2054 if (group_has_option (&group->subgroups[i],option))
2061 set_option_off (GtkPrinterOption *option)
2063 /* Any of these will do, _set only applies the value
2064 * if its allowed of the option */
2065 gtk_printer_option_set (option, "False");
2066 gtk_printer_option_set (option, "Off");
2067 gtk_printer_option_set (option, "None");
2071 value_is_off (const char *value)
2073 return (strcasecmp (value, "None") == 0 ||
2074 strcasecmp (value, "Off") == 0 ||
2075 strcasecmp (value, "False") == 0);
2079 ppd_group_name (ppd_group_t *group)
2081 #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)
2089 available_choices (ppd_file_t *ppd,
2090 ppd_option_t *option,
2091 ppd_choice_t ***available,
2092 gboolean keep_if_only_one_option)
2094 ppd_option_t *other_option;
2097 ppd_const_t *constraint;
2098 const char *choice, *other_choice;
2099 ppd_option_t *option1, *option2;
2100 ppd_group_t *installed_options;
2102 gboolean all_default;
2108 conflicts = g_new0 (char, option->num_choices);
2110 installed_options = NULL;
2111 for (i = 0; i < ppd->num_groups; i++)
2115 name = ppd_group_name (&ppd->groups[i]);
2116 if (strcmp (name, "InstallableOptions") == 0)
2118 installed_options = &ppd->groups[i];
2123 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
2125 option1 = ppdFindOption (ppd, constraint->option1);
2126 if (option1 == NULL)
2129 option2 = ppdFindOption (ppd, constraint->option2);
2130 if (option2 == NULL)
2133 if (option == option1)
2135 choice = constraint->choice1;
2136 other_option = option2;
2137 other_choice = constraint->choice2;
2139 else if (option == option2)
2141 choice = constraint->choice2;
2142 other_option = option1;
2143 other_choice = constraint->choice1;
2148 /* We only care of conflicts with installed_options and
2150 if (!group_has_option (installed_options, other_option) &&
2151 (strcmp (other_option->keyword, "PageSize") != 0))
2154 if (*other_choice == 0)
2156 /* Conflict only if the installed option is not off */
2157 if (value_is_off (other_option->defchoice))
2160 /* Conflict if the installed option has the specified default */
2161 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
2166 /* Conflict with all non-off choices */
2167 for (j = 0; j < option->num_choices; j++)
2169 if (!value_is_off (option->choices[j].choice))
2175 for (j = 0; j < option->num_choices; j++)
2177 if (strcasecmp (option->choices[j].choice, choice) == 0)
2185 for (j = 0; j < option->num_choices; j++)
2189 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
2190 all_default = FALSE;
2193 if ((all_default && !keep_if_only_one_option) ||
2194 (num_conflicts == option->num_choices))
2201 /* Some ppds don't have a "use printer default" option for
2202 * InputSlot. This means you always have to select a particular slot,
2203 * and you can't auto-pick source based on the paper size. To support
2204 * this we always add an auto option if there isn't one already. If
2205 * the user chooses the generated option we don't send any InputSlot
2206 * value when printing. The way we detect existing auto-cases is based
2207 * on feedback from Michael Sweet of cups fame.
2210 if (strcmp (option->keyword, "InputSlot") == 0)
2212 gboolean found_auto = FALSE;
2213 for (j = 0; j < option->num_choices; j++)
2217 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
2218 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
2219 strcmp (option->choices[j].choice, "Default") == 0 ||
2220 strcmp (option->choices[j].choice, "None") == 0 ||
2221 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
2222 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
2223 option->choices[j].code == NULL ||
2224 option->choices[j].code[0] == 0)
2238 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
2241 for (j = 0; j < option->num_choices; j++)
2244 (*available)[i++] = &option->choices[j];
2248 (*available)[i++] = NULL;
2253 return option->num_choices - num_conflicts + add_auto;
2256 static GtkPrinterOption *
2257 create_pickone_option (ppd_file_t *ppd_file,
2258 ppd_option_t *ppd_option,
2259 const gchar *gtk_name)
2261 GtkPrinterOption *option;
2262 ppd_choice_t **available;
2266 #ifdef HAVE_CUPS_API_1_2
2267 ppd_coption_t *coption;
2270 g_assert (ppd_option->ui == PPD_UI_PICKONE);
2274 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2278 /* right now only support one parameter per custom option
2279 * if more than one print warning and only offer the default choices
2282 label = get_option_text (ppd_file, ppd_option);
2284 #ifdef HAVE_CUPS_API_1_2
2285 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
2289 ppd_cparam_t *cparam;
2291 cparam = ppdFirstCustomParam (coption);
2293 if (ppdNextCustomParam (coption) == NULL)
2295 switch (cparam->type)
2297 case PPD_CUSTOM_INT:
2298 option = gtk_printer_option_new (gtk_name, label,
2299 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
2301 case PPD_CUSTOM_PASSCODE:
2302 option = gtk_printer_option_new (gtk_name, label,
2303 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
2305 case PPD_CUSTOM_PASSWORD:
2306 option = gtk_printer_option_new (gtk_name, label,
2307 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
2309 case PPD_CUSTOM_REAL:
2310 option = gtk_printer_option_new (gtk_name, label,
2311 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
2313 case PPD_CUSTOM_STRING:
2314 option = gtk_printer_option_new (gtk_name, label,
2315 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
2317 #ifdef PRINT_IGNORED_OPTIONS
2318 case PPD_CUSTOM_POINTS:
2319 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
2321 case PPD_CUSTOM_CURVE:
2322 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
2324 case PPD_CUSTOM_INVCURVE:
2325 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
2332 #ifdef PRINT_IGNORED_OPTIONS
2334 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
2337 #endif /* HAVE_CUPS_API_1_2 */
2340 option = gtk_printer_option_new (gtk_name, label,
2341 GTK_PRINTER_OPTION_TYPE_PICKONE);
2344 gtk_printer_option_allocate_choices (option, n_choices);
2345 for (i = 0; i < n_choices; i++)
2347 if (available[i] == NULL)
2349 /* This was auto-added */
2350 option->choices[i] = g_strdup ("gtk-ignore-value");
2351 option->choices_display[i] = g_strdup (_("Printer Default"));
2355 option->choices[i] = g_strdup (available[i]->choice);
2356 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
2359 gtk_printer_option_set (option, ppd_option->defchoice);
2361 #ifdef PRINT_IGNORED_OPTIONS
2363 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
2370 static GtkPrinterOption *
2371 create_boolean_option (ppd_file_t *ppd_file,
2372 ppd_option_t *ppd_option,
2373 const gchar *gtk_name)
2375 GtkPrinterOption *option;
2376 ppd_choice_t **available;
2380 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
2384 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2387 label = get_option_text (ppd_file, ppd_option);
2388 option = gtk_printer_option_new (gtk_name, label,
2389 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
2392 gtk_printer_option_allocate_choices (option, 2);
2393 option->choices[0] = g_strdup ("True");
2394 option->choices_display[0] = g_strdup ("True");
2395 option->choices[1] = g_strdup ("False");
2396 option->choices_display[1] = g_strdup ("False");
2398 gtk_printer_option_set (option, ppd_option->defchoice);
2400 #ifdef PRINT_IGNORED_OPTIONS
2402 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
2410 get_option_name (const gchar *keyword)
2414 for (i = 0; i < G_N_ELEMENTS (option_names); i++)
2415 if (strcmp (option_names[i].ppd_keyword, keyword) == 0)
2416 return g_strdup (option_names[i].name);
2418 return g_strdup_printf ("cups-%s", keyword);
2422 strptr_cmp (const void *a,
2425 char **aa = (char **)a;
2426 char **bb = (char **)b;
2427 return strcmp (*aa, *bb);
2432 string_in_table (gchar *str,
2433 const gchar *table[],
2436 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
2439 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
2442 handle_option (GtkPrinterOptionSet *set,
2443 ppd_file_t *ppd_file,
2444 ppd_option_t *ppd_option,
2445 ppd_group_t *toplevel_group,
2446 GtkPrintSettings *settings)
2448 GtkPrinterOption *option;
2451 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
2454 name = get_option_name (ppd_option->keyword);
2457 if (ppd_option->ui == PPD_UI_PICKONE)
2459 option = create_pickone_option (ppd_file, ppd_option, name);
2461 else if (ppd_option->ui == PPD_UI_BOOLEAN)
2463 option = create_boolean_option (ppd_file, ppd_option, name);
2465 #ifdef PRINT_IGNORED_OPTIONS
2467 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
2474 name = ppd_group_name (toplevel_group);
2475 if (STRING_IN_TABLE (name,
2476 color_group_whitelist) ||
2477 STRING_IN_TABLE (ppd_option->keyword,
2478 color_option_whitelist))
2480 option->group = g_strdup ("ColorPage");
2482 else if (STRING_IN_TABLE (name,
2483 image_quality_group_whitelist) ||
2484 STRING_IN_TABLE (ppd_option->keyword,
2485 image_quality_option_whitelist))
2487 option->group = g_strdup ("ImageQualityPage");
2489 else if (STRING_IN_TABLE (name,
2490 finishing_group_whitelist) ||
2491 STRING_IN_TABLE (ppd_option->keyword,
2492 finishing_option_whitelist))
2494 option->group = g_strdup ("FinishingPage");
2498 option->group = g_strdup (toplevel_group->text);
2501 set_option_from_settings (option, settings);
2503 gtk_printer_option_set_add (set, option);
2510 handle_group (GtkPrinterOptionSet *set,
2511 ppd_file_t *ppd_file,
2513 ppd_group_t *toplevel_group,
2514 GtkPrintSettings *settings)
2519 /* Ignore installable options */
2520 name = ppd_group_name (toplevel_group);
2521 if (strcmp (name, "InstallableOptions") == 0)
2524 for (i = 0; i < group->num_options; i++)
2525 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
2527 for (i = 0; i < group->num_subgroups; i++)
2528 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
2532 static GtkPrinterOptionSet *
2533 cups_printer_get_options (GtkPrinter *printer,
2534 GtkPrintSettings *settings,
2535 GtkPageSetup *page_setup,
2536 GtkPrintCapabilities capabilities)
2538 GtkPrinterOptionSet *set;
2539 GtkPrinterOption *option;
2540 ppd_file_t *ppd_file;
2542 char *print_at[] = { "now", "at", "on-hold" };
2543 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
2544 char *prio[] = {"100", "80", "50", "30" };
2545 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
2546 char *cover[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
2547 char *cover_display[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
2550 cups_option_t *opts = NULL;
2553 set = gtk_printer_option_set_new ();
2555 /* Cups specific, non-ppd related settings */
2557 option = gtk_printer_option_new ("gtk-n-up", "Pages Per Sheet", GTK_PRINTER_OPTION_TYPE_PICKONE);
2558 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
2560 gtk_printer_option_set (option, "1");
2561 set_option_from_settings (option, settings);
2562 gtk_printer_option_set_add (set, option);
2563 g_object_unref (option);
2565 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
2566 prio_display[i] = _(prio_display[i]);
2568 option = gtk_printer_option_new ("gtk-job-prio", "Job Priority", GTK_PRINTER_OPTION_TYPE_PICKONE);
2569 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
2570 prio, prio_display);
2571 gtk_printer_option_set (option, "50");
2572 set_option_from_settings (option, settings);
2573 gtk_printer_option_set_add (set, option);
2574 g_object_unref (option);
2576 option = gtk_printer_option_new ("gtk-billing-info", "Billing Info", GTK_PRINTER_OPTION_TYPE_STRING);
2577 gtk_printer_option_set (option, "");
2578 set_option_from_settings (option, settings);
2579 gtk_printer_option_set_add (set, option);
2580 g_object_unref (option);
2582 for (i = 0; i < G_N_ELEMENTS(cover_display); i++)
2583 cover_display[i] = _(cover_display[i]);
2585 option = gtk_printer_option_new ("gtk-cover-before", "Before", GTK_PRINTER_OPTION_TYPE_PICKONE);
2586 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (cover),
2587 cover, cover_display);
2588 gtk_printer_option_set (option, "none");
2589 set_option_from_settings (option, settings);
2590 gtk_printer_option_set_add (set, option);
2591 g_object_unref (option);
2593 option = gtk_printer_option_new ("gtk-cover-after", "After", GTK_PRINTER_OPTION_TYPE_PICKONE);
2594 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (cover),
2595 cover, cover_display);
2596 gtk_printer_option_set (option, "none");
2597 set_option_from_settings (option, settings);
2598 gtk_printer_option_set_add (set, option);
2599 g_object_unref (option);
2601 option = gtk_printer_option_new ("gtk-print-time", "Print at", GTK_PRINTER_OPTION_TYPE_PICKONE);
2602 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
2603 print_at, print_at);
2604 gtk_printer_option_set (option, "now");
2605 set_option_from_settings (option, settings);
2606 gtk_printer_option_set_add (set, option);
2607 g_object_unref (option);
2609 option = gtk_printer_option_new ("gtk-print-time-text", "Print at time", GTK_PRINTER_OPTION_TYPE_STRING);
2610 gtk_printer_option_set (option, "");
2611 set_option_from_settings (option, settings);
2612 gtk_printer_option_set_add (set, option);
2613 g_object_unref (option);
2615 /* Printer (ppd) specific settings */
2616 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2619 GtkPaperSize *paper_size;
2620 ppd_option_t *option;
2621 const gchar *ppd_name;
2623 ppdMarkDefaults (ppd_file);
2625 paper_size = gtk_page_setup_get_paper_size (page_setup);
2627 option = ppdFindOption (ppd_file, "PageSize");
2628 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
2631 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
2635 char width[G_ASCII_DTOSTR_BUF_SIZE];
2636 char height[G_ASCII_DTOSTR_BUF_SIZE];
2638 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
2639 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
2640 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
2641 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
2642 g_free (custom_name);
2645 for (i = 0; i < ppd_file->num_groups; i++)
2646 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
2649 /* Now honor the user set defaults for this printer */
2650 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
2652 for (i = 0; i < num_opts; i++)
2654 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
2657 name = get_option_name (opts[i].name);
2658 option = gtk_printer_option_set_lookup (set, name);
2660 gtk_printer_option_set (option, opts[i].value);
2664 cupsFreeOptions (num_opts, opts);
2671 mark_option_from_set (GtkPrinterOptionSet *set,
2672 ppd_file_t *ppd_file,
2673 ppd_option_t *ppd_option)
2675 GtkPrinterOption *option;
2676 char *name = get_option_name (ppd_option->keyword);
2678 option = gtk_printer_option_set_lookup (set, name);
2681 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
2688 mark_group_from_set (GtkPrinterOptionSet *set,
2689 ppd_file_t *ppd_file,
2694 for (i = 0; i < group->num_options; i++)
2695 mark_option_from_set (set, ppd_file, &group->options[i]);
2697 for (i = 0; i < group->num_subgroups; i++)
2698 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
2702 set_conflicts_from_option (GtkPrinterOptionSet *set,
2703 ppd_file_t *ppd_file,
2704 ppd_option_t *ppd_option)
2706 GtkPrinterOption *option;
2709 if (ppd_option->conflicted)
2711 name = get_option_name (ppd_option->keyword);
2712 option = gtk_printer_option_set_lookup (set, name);
2715 gtk_printer_option_set_has_conflict (option, TRUE);
2716 #ifdef PRINT_IGNORED_OPTIONS
2718 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
2726 set_conflicts_from_group (GtkPrinterOptionSet *set,
2727 ppd_file_t *ppd_file,
2732 for (i = 0; i < group->num_options; i++)
2733 set_conflicts_from_option (set, ppd_file, &group->options[i]);
2735 for (i = 0; i < group->num_subgroups; i++)
2736 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
2740 cups_printer_mark_conflicts (GtkPrinter *printer,
2741 GtkPrinterOptionSet *options)
2743 ppd_file_t *ppd_file;
2747 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2749 if (ppd_file == NULL)
2752 ppdMarkDefaults (ppd_file);
2754 for (i = 0; i < ppd_file->num_groups; i++)
2755 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
2757 num_conflicts = ppdConflicts (ppd_file);
2759 if (num_conflicts > 0)
2761 for (i = 0; i < ppd_file->num_groups; i++)
2762 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
2765 return num_conflicts > 0;
2769 GtkPrinter *printer;
2770 GtkPrinterOptionSet *options;
2771 GtkPrintSettings *settings;
2772 ppd_file_t *ppd_file;
2777 const char *standard;
2781 map_settings_to_option (GtkPrinterOption *option,
2782 const NameMapping table[],
2784 GtkPrintSettings *settings,
2785 const gchar *standard_name,
2786 const gchar *cups_name)
2790 const char *cups_value;
2791 const char *standard_value;
2793 /* If the cups-specific setting is set, always use that */
2794 name = g_strdup_printf ("cups-%s", cups_name);
2795 cups_value = gtk_print_settings_get (settings, name);
2798 if (cups_value != NULL)
2800 gtk_printer_option_set (option, cups_value);
2804 /* Otherwise we try to convert from the general setting */
2805 standard_value = gtk_print_settings_get (settings, standard_name);
2806 if (standard_value == NULL)
2809 for (i = 0; i < n_elements; i++)
2811 if (table[i].cups == NULL && table[i].standard == NULL)
2813 gtk_printer_option_set (option, standard_value);
2816 else if (table[i].cups == NULL &&
2817 strcmp (table[i].standard, standard_value) == 0)
2819 set_option_off (option);
2822 else if (strcmp (table[i].standard, standard_value) == 0)
2824 gtk_printer_option_set (option, table[i].cups);
2831 map_option_to_settings (const gchar *value,
2832 const NameMapping table[],
2834 GtkPrintSettings *settings,
2835 const gchar *standard_name,
2836 const gchar *cups_name)
2841 for (i = 0; i < n_elements; i++)
2843 if (table[i].cups == NULL && table[i].standard == NULL)
2845 gtk_print_settings_set (settings,
2850 else if (table[i].cups == NULL && table[i].standard != NULL)
2852 if (value_is_off (value))
2854 gtk_print_settings_set (settings,
2860 else if (strcmp (table[i].cups, value) == 0)
2862 gtk_print_settings_set (settings,
2869 /* Always set the corresponding cups-specific setting */
2870 name = g_strdup_printf ("cups-%s", cups_name);
2871 gtk_print_settings_set (settings, name, value);
2876 static const NameMapping paper_source_map[] = {
2877 { "Lower", "lower"},
2878 { "Middle", "middle"},
2879 { "Upper", "upper"},
2881 { "Envelope", "envelope"},
2882 { "Cassette", "cassette"},
2883 { "LargeCapacity", "large-capacity"},
2884 { "AnySmallFormat", "small-format"},
2885 { "AnyLargeFormat", "large-format"},
2889 static const NameMapping output_tray_map[] = {
2890 { "Upper", "upper"},
2891 { "Lower", "lower"},
2896 static const NameMapping duplex_map[] = {
2897 { "DuplexTumble", "vertical" },
2898 { "DuplexNoTumble", "horizontal" },
2902 static const NameMapping output_mode_map[] = {
2903 { "Standard", "normal" },
2904 { "Normal", "normal" },
2905 { "Draft", "draft" },
2906 { "Fast", "draft" },
2909 static const NameMapping media_type_map[] = {
2910 { "Transparency", "transparency"},
2911 { "Standard", "stationery"},
2915 static const NameMapping all_map[] = {
2921 set_option_from_settings (GtkPrinterOption *option,
2922 GtkPrintSettings *settings)
2924 const char *cups_value;
2927 if (settings == NULL)
2930 if (strcmp (option->name, "gtk-paper-source") == 0)
2931 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
2932 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
2933 else if (strcmp (option->name, "gtk-output-tray") == 0)
2934 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
2935 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
2936 else if (strcmp (option->name, "gtk-duplex") == 0)
2937 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
2938 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
2939 else if (strcmp (option->name, "cups-OutputMode") == 0)
2940 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
2941 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
2942 else if (strcmp (option->name, "cups-Resolution") == 0)
2944 cups_value = gtk_print_settings_get (settings, option->name);
2946 gtk_printer_option_set (option, cups_value);
2949 int res = gtk_print_settings_get_resolution (settings);
2952 value = g_strdup_printf ("%ddpi", res);
2953 gtk_printer_option_set (option, value);
2958 else if (strcmp (option->name, "gtk-paper-type") == 0)
2959 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
2960 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
2961 else if (strcmp (option->name, "gtk-n-up") == 0)
2963 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
2964 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
2966 else if (strcmp (option->name, "gtk-billing-info") == 0)
2968 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
2970 gtk_printer_option_set (option, cups_value);
2972 else if (strcmp (option->name, "gtk-job-prio") == 0)
2974 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
2976 gtk_printer_option_set (option, cups_value);
2978 else if (strcmp (option->name, "gtk-cover-before") == 0)
2980 cups_value = gtk_print_settings_get (settings, "cover-before");
2982 gtk_printer_option_set (option, cups_value);
2984 else if (strcmp (option->name, "gtk-cover-after") == 0)
2986 cups_value = gtk_print_settings_get (settings, "cover-after");
2988 gtk_printer_option_set (option, cups_value);
2990 else if (strcmp (option->name, "gtk-print-time") == 0)
2992 cups_value = gtk_print_settings_get (settings, "print-at");
2994 gtk_printer_option_set (option, cups_value);
2996 else if (strcmp (option->name, "gtk-print-time-text") == 0)
2998 cups_value = gtk_print_settings_get (settings, "print-at-time");
3000 gtk_printer_option_set (option, cups_value);
3002 else if (g_str_has_prefix (option->name, "cups-"))
3004 cups_value = gtk_print_settings_get (settings, option->name);
3006 gtk_printer_option_set (option, cups_value);
3011 foreach_option_get_settings (GtkPrinterOption *option,
3014 struct OptionData *data = user_data;
3015 GtkPrintSettings *settings = data->settings;
3018 value = option->value;
3020 if (strcmp (option->name, "gtk-paper-source") == 0)
3021 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
3022 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3023 else if (strcmp (option->name, "gtk-output-tray") == 0)
3024 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
3025 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3026 else if (strcmp (option->name, "gtk-duplex") == 0)
3027 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
3028 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3029 else if (strcmp (option->name, "cups-OutputMode") == 0)
3030 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
3031 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3032 else if (strcmp (option->name, "cups-Resolution") == 0)
3034 int res = atoi (value);
3035 /* TODO: What if resolution is on XXXxYYYdpi form? */
3037 gtk_print_settings_set_resolution (settings, res);
3038 gtk_print_settings_set (settings, option->name, value);
3040 else if (strcmp (option->name, "gtk-paper-type") == 0)
3041 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
3042 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3043 else if (strcmp (option->name, "gtk-n-up") == 0)
3044 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3045 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3046 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
3047 gtk_print_settings_set (settings, "cups-job-billing", value);
3048 else if (strcmp (option->name, "gtk-job-prio") == 0)
3049 gtk_print_settings_set (settings, "cups-job-priority", value);
3050 else if (strcmp (option->name, "gtk-cover-before") == 0)
3051 gtk_print_settings_set (settings, "cover-before", value);
3052 else if (strcmp (option->name, "gtk-cover-after") == 0)
3053 gtk_print_settings_set (settings, "cover-after", value);
3054 else if (strcmp (option->name, "gtk-print-time") == 0)
3055 gtk_print_settings_set (settings, "print-at", value);
3056 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3057 gtk_print_settings_set (settings, "print-at-time", value);
3058 else if (g_str_has_prefix (option->name, "cups-"))
3059 gtk_print_settings_set (settings, option->name, value);
3063 cups_printer_get_settings_from_options (GtkPrinter *printer,
3064 GtkPrinterOptionSet *options,
3065 GtkPrintSettings *settings)
3067 struct OptionData data;
3068 const char *print_at, *print_at_time;
3070 data.printer = printer;
3071 data.options = options;
3072 data.settings = settings;
3073 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3075 if (data.ppd_file != NULL)
3077 GtkPrinterOption *cover_before, *cover_after;
3079 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
3081 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
3082 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
3083 if (cover_before && cover_after)
3085 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
3086 gtk_print_settings_set (settings, "cups-job-sheets", value);
3090 print_at = gtk_print_settings_get (settings, "print-at");
3091 print_at_time = gtk_print_settings_get (settings, "print-at-time");
3092 if (strcmp (print_at, "at") == 0)
3093 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
3094 else if (strcmp (print_at, "on-hold") == 0)
3095 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
3100 cups_printer_prepare_for_print (GtkPrinter *printer,
3101 GtkPrintJob *print_job,
3102 GtkPrintSettings *settings,
3103 GtkPageSetup *page_setup)
3105 GtkPageSet page_set;
3106 GtkPaperSize *paper_size;
3107 const char *ppd_paper_name;
3110 print_job->print_pages = gtk_print_settings_get_print_pages (settings);
3111 print_job->page_ranges = NULL;
3112 print_job->num_page_ranges = 0;
3114 if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
3115 print_job->page_ranges =
3116 gtk_print_settings_get_page_ranges (settings,
3117 &print_job->num_page_ranges);
3119 if (gtk_print_settings_get_collate (settings))
3120 gtk_print_settings_set (settings, "cups-Collate", "True");
3121 print_job->collate = FALSE;
3123 if (gtk_print_settings_get_reverse (settings))
3124 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
3125 print_job->reverse = FALSE;
3127 if (gtk_print_settings_get_n_copies (settings) > 1)
3128 gtk_print_settings_set_int (settings, "cups-copies",
3129 gtk_print_settings_get_n_copies (settings));
3130 print_job->num_copies = 1;
3132 scale = gtk_print_settings_get_scale (settings);
3133 print_job->scale = 1.0;
3135 print_job->scale = scale/100.0;
3137 page_set = gtk_print_settings_get_page_set (settings);
3138 if (page_set == GTK_PAGE_SET_EVEN)
3139 gtk_print_settings_set (settings, "cups-page-set", "even");
3140 else if (page_set == GTK_PAGE_SET_ODD)
3141 gtk_print_settings_set (settings, "cups-page-set", "odd");
3142 print_job->page_set = GTK_PAGE_SET_ALL;
3144 paper_size = gtk_page_setup_get_paper_size (page_setup);
3145 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
3146 if (ppd_paper_name != NULL)
3147 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
3150 char width[G_ASCII_DTOSTR_BUF_SIZE];
3151 char height[G_ASCII_DTOSTR_BUF_SIZE];
3154 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3155 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3156 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
3157 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
3158 g_free (custom_name);
3161 print_job->rotate_to_orientation = TRUE;
3164 static GtkPageSetup *
3165 create_page_setup (ppd_file_t *ppd_file,
3169 GtkPageSetup *page_setup;
3170 GtkPaperSize *paper_size;
3171 ppd_option_t *option;
3172 ppd_choice_t *choice;
3174 display_name = NULL;
3175 option = ppdFindOption (ppd_file, "PageSize");
3178 choice = ppdFindChoice (option, size->name);
3180 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
3183 if (display_name == NULL)
3184 display_name = g_strdup (size->name);
3186 page_setup = gtk_page_setup_new ();
3187 paper_size = gtk_paper_size_new_from_ppd (size->name,
3191 gtk_page_setup_set_paper_size (page_setup, paper_size);
3192 gtk_paper_size_free (paper_size);
3194 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
3195 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
3196 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
3197 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
3199 g_free (display_name);
3205 cups_printer_list_papers (GtkPrinter *printer)
3207 ppd_file_t *ppd_file;
3209 GtkPageSetup *page_setup;
3213 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3214 if (ppd_file == NULL)
3219 for (i = 0; i < ppd_file->num_sizes; i++)
3221 size = &ppd_file->sizes[i];
3223 page_setup = create_page_setup (ppd_file, size);
3225 l = g_list_prepend (l, page_setup);
3228 return g_list_reverse (l);
3231 static GtkPageSetup *
3232 cups_printer_get_default_page_size (GtkPrinter *printer)
3234 ppd_file_t *ppd_file;
3236 ppd_option_t *option;
3239 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3240 if (ppd_file == NULL)
3243 option = ppdFindOption (ppd_file, "PageSize");
3244 size = ppdPageSize (ppd_file, option->defchoice);
3246 return create_page_setup (ppd_file, size);
3250 cups_printer_get_hard_margins (GtkPrinter *printer,
3256 ppd_file_t *ppd_file;
3258 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3259 if (ppd_file == NULL)
3262 *left = ppd_file->custom_margins[0];
3263 *bottom = ppd_file->custom_margins[1];
3264 *right = ppd_file->custom_margins[2];
3265 *top = ppd_file->custom_margins[3];
3268 static GtkPrintCapabilities
3269 cups_printer_get_capabilities (GtkPrinter *printer)
3272 GTK_PRINT_CAPABILITY_COPIES |
3273 GTK_PRINT_CAPABILITY_COLLATE |
3274 GTK_PRINT_CAPABILITY_REVERSE |
3275 GTK_PRINT_CAPABILITY_NUMBER_UP;