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 gchar *tmp_msg2 = NULL;
1062 gint printer_state_reason_level = 0; /* 0 - none, 1 - report, 2 - warning, 3 - error */
1063 gboolean interested_in = FALSE;
1064 gboolean found = FALSE;
1065 static const char * const reasons[] = /* Reasons we're interested in */
1071 "marker-supply-low",
1072 "marker-supply-empty",
1078 "connecting-to-device",
1081 static const char * reasons_descs[] =
1083 N_("Printer '%s' is low on toner."),
1084 N_("Printer '%s' has no toner left."),
1085 N_("Printer '%s' is low on developer."),
1086 N_("Printer '%s' is out of developer."),
1087 N_("Printer '%s' is low on at least one marker supply."),
1088 N_("Printer '%s' is out of at least one marker supply."),
1089 N_("The cover is open on printer '%s'."),
1090 N_("The door is open on printer '%s'."),
1091 N_("Printer '%s' is low on paper."),
1092 N_("Printer '%s' is out of paper."),
1093 N_("Printer '%s' is currently off-line."),
1094 N_("Printer '%s' may not be connected."),
1095 N_("There is a problem on printer '%s'.")
1097 gboolean is_paused = FALSE;
1098 gboolean is_accepting_jobs = TRUE;
1100 /* Skip leading attributes until we hit a printer...
1102 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1108 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1110 if (strcmp (attr->name, "printer-name") == 0 &&
1111 attr->value_tag == IPP_TAG_NAME)
1112 printer_name = attr->values[0].string.text;
1113 else if (strcmp (attr->name, "printer-uri-supported") == 0 &&
1114 attr->value_tag == IPP_TAG_URI)
1115 printer_uri = attr->values[0].string.text;
1116 else if (strcmp (attr->name, "member-uris") == 0 &&
1117 attr->value_tag == IPP_TAG_URI)
1118 member_uris = attr->values[0].string.text;
1119 else if (strcmp (attr->name, "printer-location") == 0)
1120 location = attr->values[0].string.text;
1121 else if (strcmp (attr->name, "printer-info") == 0)
1122 description = attr->values[0].string.text;
1123 else if (strcmp (attr->name, "printer-state-message") == 0)
1124 state_msg = attr->values[0].string.text;
1125 else if (strcmp (attr->name, "printer-state-reasons") == 0)
1126 /* Store most important reason to reason_msg and set
1127 its importance at printer_state_reason_level */
1129 for (i = 0; i < attr->num_values; i++)
1131 if (strcmp (attr->values[i].string.text, "none") != 0)
1133 /* Sets is_paused flag for paused printer. */
1134 if (strcmp (attr->values[i].string.text, "paused") == 0)
1139 interested_in = FALSE;
1140 for (j = 0; j < G_N_ELEMENTS (reasons); j++)
1141 if (strncmp (attr->values[i].string.text, reasons[j], strlen (reasons[j])) == 0)
1143 interested_in = TRUE;
1149 if (g_str_has_suffix (attr->values[i].string.text, "-report"))
1151 if (printer_state_reason_level <= 1)
1153 reason_msg = attr->values[i].string.text;
1154 printer_state_reason_level = 1;
1157 else if (g_str_has_suffix (attr->values[i].string.text, "-warning"))
1159 if (printer_state_reason_level <= 2)
1161 reason_msg = attr->values[i].string.text;
1162 printer_state_reason_level = 2;
1165 else /* It is error in the case of no suffix. */
1167 reason_msg = attr->values[i].string.text;
1168 printer_state_reason_level = 3;
1174 else if (strcmp (attr->name, "printer-state") == 0)
1175 state = attr->values[0].integer;
1176 else if (strcmp (attr->name, "queued-job-count") == 0)
1177 job_count = attr->values[0].integer;
1178 else if (strcmp (attr->name, "printer-is-accepting-jobs") == 0)
1180 if (attr->values[0].boolean == 1)
1181 is_accepting_jobs = TRUE;
1183 is_accepting_jobs = FALSE;
1188 g_print ("CUPS Backend: Attribute %s ignored", attr->name));
1194 if (printer_name == NULL ||
1195 (printer_uri == NULL && member_uris == NULL))
1203 /* remove name from checklist if it was found */
1204 node = g_list_find_custom (removed_printer_checklist, printer_name, (GCompareFunc) find_printer);
1205 removed_printer_checklist = g_list_delete_link (removed_printer_checklist, node);
1207 printer = gtk_print_backend_find_printer (backend, printer_name);
1210 GtkPrinterCups *cups_printer;
1211 char uri[HTTP_MAX_URI]; /* Printer URI */
1212 char method[HTTP_MAX_URI]; /* Method/scheme name */
1213 char username[HTTP_MAX_URI]; /* Username:password */
1214 char hostname[HTTP_MAX_URI]; /* Hostname */
1215 char resource[HTTP_MAX_URI]; /* Resource name */
1216 int port; /* Port number */
1218 list_has_changed = TRUE;
1219 cups_printer = gtk_printer_cups_new (printer_name, backend);
1221 cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
1223 /* Check to see if we are looking at a class */
1226 cups_printer->printer_uri = g_strdup (member_uris);
1227 /* TODO if member_uris is a class we need to recursivly find a printer */
1229 g_print ("CUPS Backend: Found class with printer %s\n", member_uris));
1233 cups_printer->printer_uri = g_strdup (printer_uri);
1235 g_print ("CUPS Backend: Found printer %s\n", printer_uri));
1238 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
1239 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
1240 method, sizeof (method),
1241 username, sizeof (username),
1242 hostname, sizeof (hostname),
1244 resource, sizeof (resource));
1247 httpSeparate (cups_printer->printer_uri,
1255 if (strncmp (resource, "/printers/", 10) == 0)
1257 cups_printer->ppd_name = g_strdup (resource + 10);
1259 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
1262 gethostname (uri, sizeof (uri));
1263 if (strcasecmp (uri, hostname) == 0)
1264 strcpy (hostname, "localhost");
1266 cups_printer->hostname = g_strdup (hostname);
1267 cups_printer->port = port;
1269 printer = GTK_PRINTER (cups_printer);
1271 if (cups_backend->default_printer != NULL &&
1272 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
1273 gtk_printer_set_is_default (printer, TRUE);
1276 gtk_print_backend_add_printer (backend, printer);
1279 g_object_ref (printer);
1281 gtk_printer_set_is_paused (printer, is_paused);
1282 gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
1284 if (!gtk_printer_is_active (printer))
1286 gtk_printer_set_is_active (printer, TRUE);
1287 gtk_printer_set_is_new (printer, TRUE);
1288 list_has_changed = TRUE;
1291 if (gtk_printer_is_new (printer))
1293 g_signal_emit_by_name (backend, "printer-added", printer);
1295 gtk_printer_set_is_new (printer, FALSE);
1299 /* Getting printer info with separate requests overwhelms cups
1300 * when the printer list has more than a handful of printers.
1302 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
1305 GTK_PRINTER_CUPS (printer)->state = state;
1306 status_changed = gtk_printer_set_job_count (printer, job_count);
1307 status_changed |= gtk_printer_set_location (printer, location);
1308 status_changed |= gtk_printer_set_description (printer, description);
1310 if (state_msg != NULL && strlen (state_msg) == 0)
1312 if (is_paused && !is_accepting_jobs)
1313 tmp_msg2 = g_strdup ( N_("Paused ; Rejecting Jobs"));
1314 if (is_paused && is_accepting_jobs)
1315 tmp_msg2 = g_strdup ( N_("Paused"));
1316 if (!is_paused && !is_accepting_jobs)
1317 tmp_msg2 = g_strdup ( N_("Rejecting Jobs"));
1319 if (tmp_msg2 != NULL)
1320 state_msg = tmp_msg2;
1323 /* Set description of the reason and combine it with printer-state-message. */
1324 if ( (reason_msg != NULL))
1326 for (i = 0; i < G_N_ELEMENTS (reasons); i++)
1328 if (strncmp (reason_msg, reasons[i], strlen (reasons[i])) == 0)
1330 reason_msg_desc = g_strdup_printf (reasons_descs[i], printer_name);
1337 printer_state_reason_level = 0;
1339 if (printer_state_reason_level >= 2)
1341 if (strlen (state_msg) == 0)
1342 state_msg = reason_msg_desc;
1345 tmp_msg = g_strjoin (" ; ", state_msg, reason_msg_desc, NULL);
1346 state_msg = tmp_msg;
1351 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1352 status_changed |= gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
1354 if (tmp_msg != NULL)
1357 if (tmp_msg2 != NULL)
1360 if (reason_msg_desc != NULL)
1361 g_free (reason_msg_desc);
1363 /* Set printer icon according to importance
1364 (none, report, warning, error - report is omitted). */
1365 if (printer_state_reason_level == 3)
1366 gtk_printer_set_icon_name (printer, "gtk-print-error");
1367 else if (printer_state_reason_level == 2)
1368 gtk_printer_set_icon_name (printer, "gtk-print-warning");
1369 else if (gtk_printer_is_paused (printer))
1370 gtk_printer_set_icon_name (printer, "gtk-print-paused");
1372 gtk_printer_set_icon_name (printer, "gtk-print");
1375 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1376 "printer-status-changed", printer);
1378 /* The ref is held by GtkPrintBackend, in add_printer() */
1379 g_object_unref (printer);
1385 /* look at the removed printers checklist and mark any printer
1386 as inactive if it is in the list, emitting a printer_removed signal */
1387 if (removed_printer_checklist != NULL)
1389 g_list_foreach (removed_printer_checklist, (GFunc) mark_printer_inactive, backend);
1390 g_list_free (removed_printer_checklist);
1391 list_has_changed = TRUE;
1395 if (list_has_changed)
1396 g_signal_emit_by_name (backend, "printer-list-changed");
1398 gtk_print_backend_set_list_done (backend);
1400 GDK_THREADS_LEAVE ();
1404 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
1406 GtkCupsRequest *request;
1407 static const char * const pattrs[] = /* Attributes we're interested in */
1410 "printer-uri-supported",
1414 "printer-state-message",
1415 "printer-state-reasons",
1418 "printer-is-accepting-jobs"
1421 if (cups_backend->list_printers_pending ||
1422 !cups_backend->got_default_printer)
1425 cups_backend->list_printers_pending = TRUE;
1427 request = gtk_cups_request_new (NULL,
1434 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1435 "requested-attributes", G_N_ELEMENTS (pattrs),
1438 cups_request_execute (cups_backend,
1440 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
1448 cups_get_printer_list (GtkPrintBackend *backend)
1450 GtkPrintBackendCups *cups_backend;
1452 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
1453 if (cups_backend->list_printers_poll == 0)
1455 cups_request_printer_list (cups_backend);
1456 cups_backend->list_printers_poll = gdk_threads_add_timeout (3000,
1457 (GSourceFunc) cups_request_printer_list,
1463 GtkPrinterCups *printer;
1469 get_ppd_data_free (GetPPDData *data)
1472 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1473 httpClose (data->http);
1474 g_io_channel_unref (data->ppd_io);
1475 g_object_unref (data->printer);
1480 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
1481 GtkCupsResult *result,
1485 GtkPrinter *printer;
1487 GDK_THREADS_ENTER ();
1490 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1492 printer = GTK_PRINTER (data->printer);
1493 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
1495 if (gtk_cups_result_is_error (result))
1497 gboolean success = FALSE;
1499 /* if we get a 404 then it is just a raw printer without a ppd
1501 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
1502 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
1504 gtk_printer_set_has_details (printer, TRUE);
1508 g_signal_emit_by_name (printer, "details-acquired", success);
1512 response = gtk_cups_result_get_response (result);
1514 /* let ppdOpenFd take over the ownership of the open file */
1515 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
1516 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
1518 ppdMarkDefaults (data->printer->ppd_file);
1520 gtk_printer_set_has_details (printer, TRUE);
1521 g_signal_emit_by_name (printer, "details-acquired", TRUE);
1524 GDK_THREADS_LEAVE ();
1528 cups_request_ppd (GtkPrinter *printer)
1531 GtkPrintBackend *print_backend;
1532 GtkPrinterCups *cups_printer;
1533 GtkCupsRequest *request;
1540 cups_printer = GTK_PRINTER_CUPS (printer);
1545 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1547 http = httpConnectEncrypt (cups_printer->hostname,
1551 data = g_new0 (GetPPDData, 1);
1553 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
1557 #ifdef G_ENABLE_DEBUG
1558 /* If we are debugging printing don't delete the tmp files */
1559 if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
1560 unlink (ppd_filename);
1562 unlink (ppd_filename);
1563 #endif /* G_ENABLE_DEBUG */
1568 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
1570 g_error_free (error);
1572 g_free (ppd_filename);
1575 g_signal_emit_by_name (printer, "details-acquired", FALSE);
1580 fchmod (fd, S_IRUSR | S_IWUSR);
1581 data->ppd_io = g_io_channel_unix_new (fd);
1582 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
1583 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
1585 data->printer = g_object_ref (printer);
1587 resource = g_strdup_printf ("/printers/%s.ppd",
1588 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
1590 request = gtk_cups_request_new (data->http,
1594 cups_printer->hostname,
1598 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
1601 g_free (ppd_filename);
1603 cups_printer->reading_ppd = TRUE;
1605 print_backend = gtk_printer_get_backend (printer);
1607 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
1609 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
1611 (GDestroyNotify)get_ppd_data_free);
1614 /* Ordering matters for default preference */
1615 static const char *lpoptions_locations[] = {
1616 "/etc/cups/lpoptions",
1622 cups_parse_user_default_printer (const char *filename,
1623 char **printer_name)
1626 char line[1024], *lineptr, *defname = NULL;
1628 if ((fp = g_fopen (filename, "r")) == NULL)
1631 while (fgets (line, sizeof (line), fp) != NULL)
1633 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
1637 while (isspace (*lineptr))
1644 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
1649 if (*printer_name != NULL)
1650 g_free (*printer_name);
1652 *printer_name = g_strdup (defname);
1659 cups_get_user_default_printer (char **printer_name)
1663 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1665 if (g_path_is_absolute (lpoptions_locations[i]))
1667 cups_parse_user_default_printer (lpoptions_locations[i],
1674 filename = g_build_filename (g_get_home_dir (),
1675 lpoptions_locations[i], NULL);
1676 cups_parse_user_default_printer (filename, printer_name);
1683 cups_parse_user_options (const char *filename,
1684 const char *printer_name,
1686 cups_option_t **options)
1689 gchar line[1024], *lineptr, *name;
1691 if ((fp = g_fopen (filename, "r")) == NULL)
1694 while (fgets (line, sizeof (line), fp) != NULL)
1696 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
1698 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
1703 /* Skip leading whitespace */
1704 while (isspace (*lineptr))
1710 /* NUL-terminate the name, stripping the instance name */
1712 while (!isspace (*lineptr) && *lineptr)
1714 if (*lineptr == '/')
1724 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
1727 /* We found our printer, parse the options */
1728 num_options = cupsParseOptions (lineptr, num_options, options);
1737 cups_get_user_options (const char *printer_name,
1739 cups_option_t **options)
1743 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1745 if (g_path_is_absolute (lpoptions_locations[i]))
1747 num_options = cups_parse_user_options (lpoptions_locations[i],
1756 filename = g_build_filename (g_get_home_dir (),
1757 lpoptions_locations[i], NULL);
1758 num_options = cups_parse_user_options (filename, printer_name,
1759 num_options, options);
1768 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
1769 GtkCupsResult *result,
1773 ipp_attribute_t *attr;
1775 response = gtk_cups_result_get_response (result);
1777 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
1778 print_backend->default_printer = g_strdup (attr->values[0].string.text);
1780 print_backend->got_default_printer = TRUE;
1782 /* Make sure to kick off get_printers if we are polling it,
1783 * as we could have blocked this reading the default printer
1785 if (print_backend->list_printers_poll != 0)
1786 cups_request_printer_list (print_backend);
1790 cups_request_default_printer (GtkPrintBackendCups *print_backend)
1792 GtkCupsRequest *request;
1796 if ((str = g_getenv ("LPDEST")) != NULL)
1798 print_backend->default_printer = g_strdup (str);
1799 print_backend->got_default_printer = TRUE;
1802 else if ((str = g_getenv ("PRINTER")) != NULL &&
1803 strcmp (str, "lp") != 0)
1805 print_backend->default_printer = g_strdup (str);
1806 print_backend->got_default_printer = TRUE;
1810 /* Figure out user setting for default printer */
1811 cups_get_user_default_printer (&name);
1814 print_backend->default_printer = name;
1815 print_backend->got_default_printer = TRUE;
1819 request = gtk_cups_request_new (NULL,
1826 cups_request_execute (print_backend,
1828 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
1829 g_object_ref (print_backend),
1834 cups_printer_request_details (GtkPrinter *printer)
1836 GtkPrinterCups *cups_printer;
1838 cups_printer = GTK_PRINTER_CUPS (printer);
1839 if (!cups_printer->reading_ppd &&
1840 gtk_printer_cups_get_ppd (cups_printer) == NULL)
1841 cups_request_ppd (printer);
1845 ppd_text_to_utf8 (ppd_file_t *ppd_file,
1848 const char *encoding = NULL;
1851 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
1853 return g_strdup (text);
1855 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
1857 encoding = "ISO-8859-1";
1859 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
1861 encoding = "ISO-8859-2";
1863 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
1865 encoding = "ISO-8859-5";
1867 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
1869 encoding = "SHIFT-JIS";
1871 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
1873 encoding = "MACINTOSH";
1875 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
1877 encoding = "WINDOWS-1252";
1881 /* Fallback, try iso-8859-1... */
1882 encoding = "ISO-8859-1";
1885 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
1890 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
1891 res = g_strdup ("???");
1897 /* TODO: Add more translations for common settings here */
1899 static const struct {
1900 const char *keyword;
1901 const char *translation;
1902 } cups_option_translations[] = {
1903 { "Duplex", N_("Two Sided") },
1904 { "MediaType", N_("Paper Type") },
1905 { "InputSlot", N_("Paper Source") },
1906 { "OutputBin", N_("Output Tray") },
1910 static const struct {
1911 const char *keyword;
1913 const char *translation;
1914 } cups_choice_translations[] = {
1915 { "Duplex", "None", N_("One Sided") },
1916 { "InputSlot", "Auto", N_("Auto Select") },
1917 { "InputSlot", "AutoSelect", N_("Auto Select") },
1918 { "InputSlot", "Default", N_("Printer Default") },
1919 { "InputSlot", "None", N_("Printer Default") },
1920 { "InputSlot", "PrinterDefault", N_("Printer Default") },
1921 { "InputSlot", "Unspecified", N_("Auto Select") },
1924 static const struct {
1925 const char *ppd_keyword;
1927 } option_names[] = {
1928 {"Duplex", "gtk-duplex" },
1929 {"MediaType", "gtk-paper-type"},
1930 {"InputSlot", "gtk-paper-source"},
1931 {"OutputBin", "gtk-output-tray"},
1934 /* keep sorted when changing */
1935 static const char *color_option_whitelist[] = {
1936 "BRColorEnhancement",
1942 "BlackSubstitution",
1950 "RPSBlackOverPrint",
1954 /* keep sorted when changing */
1955 static const char *color_group_whitelist[] = {
1962 "HPColorOptionsPanel",
1965 /* keep sorted when changing */
1966 static const char *image_quality_option_whitelist[] = {
1968 "BRHalfTonePattern",
1978 "HPGraphicsHalftone",
1993 /* keep sorted when changing */
1994 static const char *image_quality_group_whitelist[] = {
2001 /* keep sorted when changing */
2002 static const char * finishing_option_whitelist[] = {
2015 "StapleOrientation",
2021 /* keep sorted when changing */
2022 static const char *finishing_group_whitelist[] = {
2031 /* keep sorted when changing */
2032 static const char *cups_option_blacklist[] = {
2041 get_option_text (ppd_file_t *ppd_file,
2042 ppd_option_t *option)
2047 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
2049 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
2050 return g_strdup (_(cups_option_translations[i].translation));
2053 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
2055 /* Some ppd files have spaces in the text before the colon */
2062 get_choice_text (ppd_file_t *ppd_file,
2063 ppd_choice_t *choice)
2066 ppd_option_t *option = choice->option;
2067 const char *keyword = option->keyword;
2069 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
2071 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
2072 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
2073 return g_strdup (_(cups_choice_translations[i].translation));
2075 return ppd_text_to_utf8 (ppd_file, choice->text);
2079 group_has_option (ppd_group_t *group,
2080 ppd_option_t *option)
2087 if (group->num_options > 0 &&
2088 option >= group->options && option < group->options + group->num_options)
2091 for (i = 0; i < group->num_subgroups; i++)
2093 if (group_has_option (&group->subgroups[i],option))
2100 set_option_off (GtkPrinterOption *option)
2102 /* Any of these will do, _set only applies the value
2103 * if its allowed of the option */
2104 gtk_printer_option_set (option, "False");
2105 gtk_printer_option_set (option, "Off");
2106 gtk_printer_option_set (option, "None");
2110 value_is_off (const char *value)
2112 return (strcasecmp (value, "None") == 0 ||
2113 strcasecmp (value, "Off") == 0 ||
2114 strcasecmp (value, "False") == 0);
2118 ppd_group_name (ppd_group_t *group)
2120 #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)
2128 available_choices (ppd_file_t *ppd,
2129 ppd_option_t *option,
2130 ppd_choice_t ***available,
2131 gboolean keep_if_only_one_option)
2133 ppd_option_t *other_option;
2136 ppd_const_t *constraint;
2137 const char *choice, *other_choice;
2138 ppd_option_t *option1, *option2;
2139 ppd_group_t *installed_options;
2141 gboolean all_default;
2147 conflicts = g_new0 (char, option->num_choices);
2149 installed_options = NULL;
2150 for (i = 0; i < ppd->num_groups; i++)
2154 name = ppd_group_name (&ppd->groups[i]);
2155 if (strcmp (name, "InstallableOptions") == 0)
2157 installed_options = &ppd->groups[i];
2162 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
2164 option1 = ppdFindOption (ppd, constraint->option1);
2165 if (option1 == NULL)
2168 option2 = ppdFindOption (ppd, constraint->option2);
2169 if (option2 == NULL)
2172 if (option == option1)
2174 choice = constraint->choice1;
2175 other_option = option2;
2176 other_choice = constraint->choice2;
2178 else if (option == option2)
2180 choice = constraint->choice2;
2181 other_option = option1;
2182 other_choice = constraint->choice1;
2187 /* We only care of conflicts with installed_options and
2189 if (!group_has_option (installed_options, other_option) &&
2190 (strcmp (other_option->keyword, "PageSize") != 0))
2193 if (*other_choice == 0)
2195 /* Conflict only if the installed option is not off */
2196 if (value_is_off (other_option->defchoice))
2199 /* Conflict if the installed option has the specified default */
2200 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
2205 /* Conflict with all non-off choices */
2206 for (j = 0; j < option->num_choices; j++)
2208 if (!value_is_off (option->choices[j].choice))
2214 for (j = 0; j < option->num_choices; j++)
2216 if (strcasecmp (option->choices[j].choice, choice) == 0)
2224 for (j = 0; j < option->num_choices; j++)
2228 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
2229 all_default = FALSE;
2232 if ((all_default && !keep_if_only_one_option) ||
2233 (num_conflicts == option->num_choices))
2240 /* Some ppds don't have a "use printer default" option for
2241 * InputSlot. This means you always have to select a particular slot,
2242 * and you can't auto-pick source based on the paper size. To support
2243 * this we always add an auto option if there isn't one already. If
2244 * the user chooses the generated option we don't send any InputSlot
2245 * value when printing. The way we detect existing auto-cases is based
2246 * on feedback from Michael Sweet of cups fame.
2249 if (strcmp (option->keyword, "InputSlot") == 0)
2251 gboolean found_auto = FALSE;
2252 for (j = 0; j < option->num_choices; j++)
2256 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
2257 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
2258 strcmp (option->choices[j].choice, "Default") == 0 ||
2259 strcmp (option->choices[j].choice, "None") == 0 ||
2260 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
2261 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
2262 option->choices[j].code == NULL ||
2263 option->choices[j].code[0] == 0)
2277 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
2280 for (j = 0; j < option->num_choices; j++)
2283 (*available)[i++] = &option->choices[j];
2287 (*available)[i++] = NULL;
2292 return option->num_choices - num_conflicts + add_auto;
2295 static GtkPrinterOption *
2296 create_pickone_option (ppd_file_t *ppd_file,
2297 ppd_option_t *ppd_option,
2298 const gchar *gtk_name)
2300 GtkPrinterOption *option;
2301 ppd_choice_t **available;
2305 #ifdef HAVE_CUPS_API_1_2
2306 ppd_coption_t *coption;
2309 g_assert (ppd_option->ui == PPD_UI_PICKONE);
2313 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2317 /* right now only support one parameter per custom option
2318 * if more than one print warning and only offer the default choices
2321 label = get_option_text (ppd_file, ppd_option);
2323 #ifdef HAVE_CUPS_API_1_2
2324 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
2328 ppd_cparam_t *cparam;
2330 cparam = ppdFirstCustomParam (coption);
2332 if (ppdNextCustomParam (coption) == NULL)
2334 switch (cparam->type)
2336 case PPD_CUSTOM_INT:
2337 option = gtk_printer_option_new (gtk_name, label,
2338 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
2340 case PPD_CUSTOM_PASSCODE:
2341 option = gtk_printer_option_new (gtk_name, label,
2342 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
2344 case PPD_CUSTOM_PASSWORD:
2345 option = gtk_printer_option_new (gtk_name, label,
2346 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
2348 case PPD_CUSTOM_REAL:
2349 option = gtk_printer_option_new (gtk_name, label,
2350 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
2352 case PPD_CUSTOM_STRING:
2353 option = gtk_printer_option_new (gtk_name, label,
2354 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
2356 #ifdef PRINT_IGNORED_OPTIONS
2357 case PPD_CUSTOM_POINTS:
2358 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
2360 case PPD_CUSTOM_CURVE:
2361 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
2363 case PPD_CUSTOM_INVCURVE:
2364 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
2371 #ifdef PRINT_IGNORED_OPTIONS
2373 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
2376 #endif /* HAVE_CUPS_API_1_2 */
2379 option = gtk_printer_option_new (gtk_name, label,
2380 GTK_PRINTER_OPTION_TYPE_PICKONE);
2383 gtk_printer_option_allocate_choices (option, n_choices);
2384 for (i = 0; i < n_choices; i++)
2386 if (available[i] == NULL)
2388 /* This was auto-added */
2389 option->choices[i] = g_strdup ("gtk-ignore-value");
2390 option->choices_display[i] = g_strdup (_("Printer Default"));
2394 option->choices[i] = g_strdup (available[i]->choice);
2395 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
2398 gtk_printer_option_set (option, ppd_option->defchoice);
2400 #ifdef PRINT_IGNORED_OPTIONS
2402 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
2409 static GtkPrinterOption *
2410 create_boolean_option (ppd_file_t *ppd_file,
2411 ppd_option_t *ppd_option,
2412 const gchar *gtk_name)
2414 GtkPrinterOption *option;
2415 ppd_choice_t **available;
2419 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
2423 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2426 label = get_option_text (ppd_file, ppd_option);
2427 option = gtk_printer_option_new (gtk_name, label,
2428 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
2431 gtk_printer_option_allocate_choices (option, 2);
2432 option->choices[0] = g_strdup ("True");
2433 option->choices_display[0] = g_strdup ("True");
2434 option->choices[1] = g_strdup ("False");
2435 option->choices_display[1] = g_strdup ("False");
2437 gtk_printer_option_set (option, ppd_option->defchoice);
2439 #ifdef PRINT_IGNORED_OPTIONS
2441 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
2449 get_option_name (const gchar *keyword)
2453 for (i = 0; i < G_N_ELEMENTS (option_names); i++)
2454 if (strcmp (option_names[i].ppd_keyword, keyword) == 0)
2455 return g_strdup (option_names[i].name);
2457 return g_strdup_printf ("cups-%s", keyword);
2461 strptr_cmp (const void *a,
2464 char **aa = (char **)a;
2465 char **bb = (char **)b;
2466 return strcmp (*aa, *bb);
2471 string_in_table (gchar *str,
2472 const gchar *table[],
2475 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
2478 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
2481 handle_option (GtkPrinterOptionSet *set,
2482 ppd_file_t *ppd_file,
2483 ppd_option_t *ppd_option,
2484 ppd_group_t *toplevel_group,
2485 GtkPrintSettings *settings)
2487 GtkPrinterOption *option;
2490 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
2493 name = get_option_name (ppd_option->keyword);
2496 if (ppd_option->ui == PPD_UI_PICKONE)
2498 option = create_pickone_option (ppd_file, ppd_option, name);
2500 else if (ppd_option->ui == PPD_UI_BOOLEAN)
2502 option = create_boolean_option (ppd_file, ppd_option, name);
2504 #ifdef PRINT_IGNORED_OPTIONS
2506 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
2513 name = ppd_group_name (toplevel_group);
2514 if (STRING_IN_TABLE (name,
2515 color_group_whitelist) ||
2516 STRING_IN_TABLE (ppd_option->keyword,
2517 color_option_whitelist))
2519 option->group = g_strdup ("ColorPage");
2521 else if (STRING_IN_TABLE (name,
2522 image_quality_group_whitelist) ||
2523 STRING_IN_TABLE (ppd_option->keyword,
2524 image_quality_option_whitelist))
2526 option->group = g_strdup ("ImageQualityPage");
2528 else if (STRING_IN_TABLE (name,
2529 finishing_group_whitelist) ||
2530 STRING_IN_TABLE (ppd_option->keyword,
2531 finishing_option_whitelist))
2533 option->group = g_strdup ("FinishingPage");
2537 option->group = g_strdup (toplevel_group->text);
2540 set_option_from_settings (option, settings);
2542 gtk_printer_option_set_add (set, option);
2549 handle_group (GtkPrinterOptionSet *set,
2550 ppd_file_t *ppd_file,
2552 ppd_group_t *toplevel_group,
2553 GtkPrintSettings *settings)
2558 /* Ignore installable options */
2559 name = ppd_group_name (toplevel_group);
2560 if (strcmp (name, "InstallableOptions") == 0)
2563 for (i = 0; i < group->num_options; i++)
2564 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
2566 for (i = 0; i < group->num_subgroups; i++)
2567 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
2571 static GtkPrinterOptionSet *
2572 cups_printer_get_options (GtkPrinter *printer,
2573 GtkPrintSettings *settings,
2574 GtkPageSetup *page_setup,
2575 GtkPrintCapabilities capabilities)
2577 GtkPrinterOptionSet *set;
2578 GtkPrinterOption *option;
2579 ppd_file_t *ppd_file;
2581 char *print_at[] = { "now", "at", "on-hold" };
2582 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
2583 char *prio[] = {"100", "80", "50", "30" };
2584 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
2585 char *cover[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
2586 char *cover_display[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
2589 cups_option_t *opts = NULL;
2592 set = gtk_printer_option_set_new ();
2594 /* Cups specific, non-ppd related settings */
2596 option = gtk_printer_option_new ("gtk-n-up", "Pages Per Sheet", GTK_PRINTER_OPTION_TYPE_PICKONE);
2597 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
2599 gtk_printer_option_set (option, "1");
2600 set_option_from_settings (option, settings);
2601 gtk_printer_option_set_add (set, option);
2602 g_object_unref (option);
2604 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
2605 prio_display[i] = _(prio_display[i]);
2607 option = gtk_printer_option_new ("gtk-job-prio", "Job Priority", GTK_PRINTER_OPTION_TYPE_PICKONE);
2608 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
2609 prio, prio_display);
2610 gtk_printer_option_set (option, "50");
2611 set_option_from_settings (option, settings);
2612 gtk_printer_option_set_add (set, option);
2613 g_object_unref (option);
2615 option = gtk_printer_option_new ("gtk-billing-info", "Billing Info", GTK_PRINTER_OPTION_TYPE_STRING);
2616 gtk_printer_option_set (option, "");
2617 set_option_from_settings (option, settings);
2618 gtk_printer_option_set_add (set, option);
2619 g_object_unref (option);
2621 for (i = 0; i < G_N_ELEMENTS(cover_display); i++)
2622 cover_display[i] = _(cover_display[i]);
2624 option = gtk_printer_option_new ("gtk-cover-before", "Before", GTK_PRINTER_OPTION_TYPE_PICKONE);
2625 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (cover),
2626 cover, cover_display);
2627 gtk_printer_option_set (option, "none");
2628 set_option_from_settings (option, settings);
2629 gtk_printer_option_set_add (set, option);
2630 g_object_unref (option);
2632 option = gtk_printer_option_new ("gtk-cover-after", "After", GTK_PRINTER_OPTION_TYPE_PICKONE);
2633 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (cover),
2634 cover, cover_display);
2635 gtk_printer_option_set (option, "none");
2636 set_option_from_settings (option, settings);
2637 gtk_printer_option_set_add (set, option);
2638 g_object_unref (option);
2640 option = gtk_printer_option_new ("gtk-print-time", "Print at", GTK_PRINTER_OPTION_TYPE_PICKONE);
2641 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
2642 print_at, print_at);
2643 gtk_printer_option_set (option, "now");
2644 set_option_from_settings (option, settings);
2645 gtk_printer_option_set_add (set, option);
2646 g_object_unref (option);
2648 option = gtk_printer_option_new ("gtk-print-time-text", "Print at time", GTK_PRINTER_OPTION_TYPE_STRING);
2649 gtk_printer_option_set (option, "");
2650 set_option_from_settings (option, settings);
2651 gtk_printer_option_set_add (set, option);
2652 g_object_unref (option);
2654 /* Printer (ppd) specific settings */
2655 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2658 GtkPaperSize *paper_size;
2659 ppd_option_t *option;
2660 const gchar *ppd_name;
2662 ppdMarkDefaults (ppd_file);
2664 paper_size = gtk_page_setup_get_paper_size (page_setup);
2666 option = ppdFindOption (ppd_file, "PageSize");
2667 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
2670 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
2674 char width[G_ASCII_DTOSTR_BUF_SIZE];
2675 char height[G_ASCII_DTOSTR_BUF_SIZE];
2677 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
2678 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
2679 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
2680 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
2681 g_free (custom_name);
2684 for (i = 0; i < ppd_file->num_groups; i++)
2685 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
2688 /* Now honor the user set defaults for this printer */
2689 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
2691 for (i = 0; i < num_opts; i++)
2693 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
2696 name = get_option_name (opts[i].name);
2697 option = gtk_printer_option_set_lookup (set, name);
2699 gtk_printer_option_set (option, opts[i].value);
2703 cupsFreeOptions (num_opts, opts);
2710 mark_option_from_set (GtkPrinterOptionSet *set,
2711 ppd_file_t *ppd_file,
2712 ppd_option_t *ppd_option)
2714 GtkPrinterOption *option;
2715 char *name = get_option_name (ppd_option->keyword);
2717 option = gtk_printer_option_set_lookup (set, name);
2720 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
2727 mark_group_from_set (GtkPrinterOptionSet *set,
2728 ppd_file_t *ppd_file,
2733 for (i = 0; i < group->num_options; i++)
2734 mark_option_from_set (set, ppd_file, &group->options[i]);
2736 for (i = 0; i < group->num_subgroups; i++)
2737 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
2741 set_conflicts_from_option (GtkPrinterOptionSet *set,
2742 ppd_file_t *ppd_file,
2743 ppd_option_t *ppd_option)
2745 GtkPrinterOption *option;
2748 if (ppd_option->conflicted)
2750 name = get_option_name (ppd_option->keyword);
2751 option = gtk_printer_option_set_lookup (set, name);
2754 gtk_printer_option_set_has_conflict (option, TRUE);
2755 #ifdef PRINT_IGNORED_OPTIONS
2757 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
2765 set_conflicts_from_group (GtkPrinterOptionSet *set,
2766 ppd_file_t *ppd_file,
2771 for (i = 0; i < group->num_options; i++)
2772 set_conflicts_from_option (set, ppd_file, &group->options[i]);
2774 for (i = 0; i < group->num_subgroups; i++)
2775 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
2779 cups_printer_mark_conflicts (GtkPrinter *printer,
2780 GtkPrinterOptionSet *options)
2782 ppd_file_t *ppd_file;
2786 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2788 if (ppd_file == NULL)
2791 ppdMarkDefaults (ppd_file);
2793 for (i = 0; i < ppd_file->num_groups; i++)
2794 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
2796 num_conflicts = ppdConflicts (ppd_file);
2798 if (num_conflicts > 0)
2800 for (i = 0; i < ppd_file->num_groups; i++)
2801 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
2804 return num_conflicts > 0;
2808 GtkPrinter *printer;
2809 GtkPrinterOptionSet *options;
2810 GtkPrintSettings *settings;
2811 ppd_file_t *ppd_file;
2816 const char *standard;
2820 map_settings_to_option (GtkPrinterOption *option,
2821 const NameMapping table[],
2823 GtkPrintSettings *settings,
2824 const gchar *standard_name,
2825 const gchar *cups_name)
2829 const char *cups_value;
2830 const char *standard_value;
2832 /* If the cups-specific setting is set, always use that */
2833 name = g_strdup_printf ("cups-%s", cups_name);
2834 cups_value = gtk_print_settings_get (settings, name);
2837 if (cups_value != NULL)
2839 gtk_printer_option_set (option, cups_value);
2843 /* Otherwise we try to convert from the general setting */
2844 standard_value = gtk_print_settings_get (settings, standard_name);
2845 if (standard_value == NULL)
2848 for (i = 0; i < n_elements; i++)
2850 if (table[i].cups == NULL && table[i].standard == NULL)
2852 gtk_printer_option_set (option, standard_value);
2855 else if (table[i].cups == NULL &&
2856 strcmp (table[i].standard, standard_value) == 0)
2858 set_option_off (option);
2861 else if (strcmp (table[i].standard, standard_value) == 0)
2863 gtk_printer_option_set (option, table[i].cups);
2870 map_option_to_settings (const gchar *value,
2871 const NameMapping table[],
2873 GtkPrintSettings *settings,
2874 const gchar *standard_name,
2875 const gchar *cups_name)
2880 for (i = 0; i < n_elements; i++)
2882 if (table[i].cups == NULL && table[i].standard == NULL)
2884 gtk_print_settings_set (settings,
2889 else if (table[i].cups == NULL && table[i].standard != NULL)
2891 if (value_is_off (value))
2893 gtk_print_settings_set (settings,
2899 else if (strcmp (table[i].cups, value) == 0)
2901 gtk_print_settings_set (settings,
2908 /* Always set the corresponding cups-specific setting */
2909 name = g_strdup_printf ("cups-%s", cups_name);
2910 gtk_print_settings_set (settings, name, value);
2915 static const NameMapping paper_source_map[] = {
2916 { "Lower", "lower"},
2917 { "Middle", "middle"},
2918 { "Upper", "upper"},
2920 { "Envelope", "envelope"},
2921 { "Cassette", "cassette"},
2922 { "LargeCapacity", "large-capacity"},
2923 { "AnySmallFormat", "small-format"},
2924 { "AnyLargeFormat", "large-format"},
2928 static const NameMapping output_tray_map[] = {
2929 { "Upper", "upper"},
2930 { "Lower", "lower"},
2935 static const NameMapping duplex_map[] = {
2936 { "DuplexTumble", "vertical" },
2937 { "DuplexNoTumble", "horizontal" },
2941 static const NameMapping output_mode_map[] = {
2942 { "Standard", "normal" },
2943 { "Normal", "normal" },
2944 { "Draft", "draft" },
2945 { "Fast", "draft" },
2948 static const NameMapping media_type_map[] = {
2949 { "Transparency", "transparency"},
2950 { "Standard", "stationery"},
2954 static const NameMapping all_map[] = {
2960 set_option_from_settings (GtkPrinterOption *option,
2961 GtkPrintSettings *settings)
2963 const char *cups_value;
2966 if (settings == NULL)
2969 if (strcmp (option->name, "gtk-paper-source") == 0)
2970 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
2971 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
2972 else if (strcmp (option->name, "gtk-output-tray") == 0)
2973 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
2974 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
2975 else if (strcmp (option->name, "gtk-duplex") == 0)
2976 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
2977 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
2978 else if (strcmp (option->name, "cups-OutputMode") == 0)
2979 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
2980 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
2981 else if (strcmp (option->name, "cups-Resolution") == 0)
2983 cups_value = gtk_print_settings_get (settings, option->name);
2985 gtk_printer_option_set (option, cups_value);
2988 int res = gtk_print_settings_get_resolution (settings);
2991 value = g_strdup_printf ("%ddpi", res);
2992 gtk_printer_option_set (option, value);
2997 else if (strcmp (option->name, "gtk-paper-type") == 0)
2998 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
2999 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3000 else if (strcmp (option->name, "gtk-n-up") == 0)
3002 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
3003 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3005 else if (strcmp (option->name, "gtk-billing-info") == 0)
3007 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
3009 gtk_printer_option_set (option, cups_value);
3011 else if (strcmp (option->name, "gtk-job-prio") == 0)
3013 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
3015 gtk_printer_option_set (option, cups_value);
3017 else if (strcmp (option->name, "gtk-cover-before") == 0)
3019 cups_value = gtk_print_settings_get (settings, "cover-before");
3021 gtk_printer_option_set (option, cups_value);
3023 else if (strcmp (option->name, "gtk-cover-after") == 0)
3025 cups_value = gtk_print_settings_get (settings, "cover-after");
3027 gtk_printer_option_set (option, cups_value);
3029 else if (strcmp (option->name, "gtk-print-time") == 0)
3031 cups_value = gtk_print_settings_get (settings, "print-at");
3033 gtk_printer_option_set (option, cups_value);
3035 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3037 cups_value = gtk_print_settings_get (settings, "print-at-time");
3039 gtk_printer_option_set (option, cups_value);
3041 else if (g_str_has_prefix (option->name, "cups-"))
3043 cups_value = gtk_print_settings_get (settings, option->name);
3045 gtk_printer_option_set (option, cups_value);
3050 foreach_option_get_settings (GtkPrinterOption *option,
3053 struct OptionData *data = user_data;
3054 GtkPrintSettings *settings = data->settings;
3057 value = option->value;
3059 if (strcmp (option->name, "gtk-paper-source") == 0)
3060 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
3061 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3062 else if (strcmp (option->name, "gtk-output-tray") == 0)
3063 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
3064 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3065 else if (strcmp (option->name, "gtk-duplex") == 0)
3066 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
3067 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3068 else if (strcmp (option->name, "cups-OutputMode") == 0)
3069 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
3070 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3071 else if (strcmp (option->name, "cups-Resolution") == 0)
3073 int res = atoi (value);
3074 /* TODO: What if resolution is on XXXxYYYdpi form? */
3076 gtk_print_settings_set_resolution (settings, res);
3077 gtk_print_settings_set (settings, option->name, value);
3079 else if (strcmp (option->name, "gtk-paper-type") == 0)
3080 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
3081 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3082 else if (strcmp (option->name, "gtk-n-up") == 0)
3083 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3084 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3085 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
3086 gtk_print_settings_set (settings, "cups-job-billing", value);
3087 else if (strcmp (option->name, "gtk-job-prio") == 0)
3088 gtk_print_settings_set (settings, "cups-job-priority", value);
3089 else if (strcmp (option->name, "gtk-cover-before") == 0)
3090 gtk_print_settings_set (settings, "cover-before", value);
3091 else if (strcmp (option->name, "gtk-cover-after") == 0)
3092 gtk_print_settings_set (settings, "cover-after", value);
3093 else if (strcmp (option->name, "gtk-print-time") == 0)
3094 gtk_print_settings_set (settings, "print-at", value);
3095 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3096 gtk_print_settings_set (settings, "print-at-time", value);
3097 else if (g_str_has_prefix (option->name, "cups-"))
3098 gtk_print_settings_set (settings, option->name, value);
3102 cups_printer_get_settings_from_options (GtkPrinter *printer,
3103 GtkPrinterOptionSet *options,
3104 GtkPrintSettings *settings)
3106 struct OptionData data;
3107 const char *print_at, *print_at_time;
3109 data.printer = printer;
3110 data.options = options;
3111 data.settings = settings;
3112 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3114 if (data.ppd_file != NULL)
3116 GtkPrinterOption *cover_before, *cover_after;
3118 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
3120 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
3121 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
3122 if (cover_before && cover_after)
3124 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
3125 gtk_print_settings_set (settings, "cups-job-sheets", value);
3129 print_at = gtk_print_settings_get (settings, "print-at");
3130 print_at_time = gtk_print_settings_get (settings, "print-at-time");
3131 if (strcmp (print_at, "at") == 0)
3132 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
3133 else if (strcmp (print_at, "on-hold") == 0)
3134 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
3139 cups_printer_prepare_for_print (GtkPrinter *printer,
3140 GtkPrintJob *print_job,
3141 GtkPrintSettings *settings,
3142 GtkPageSetup *page_setup)
3144 GtkPageSet page_set;
3145 GtkPaperSize *paper_size;
3146 const char *ppd_paper_name;
3149 print_job->print_pages = gtk_print_settings_get_print_pages (settings);
3150 print_job->page_ranges = NULL;
3151 print_job->num_page_ranges = 0;
3153 if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
3154 print_job->page_ranges =
3155 gtk_print_settings_get_page_ranges (settings,
3156 &print_job->num_page_ranges);
3158 if (gtk_print_settings_get_collate (settings))
3159 gtk_print_settings_set (settings, "cups-Collate", "True");
3160 print_job->collate = FALSE;
3162 if (gtk_print_settings_get_reverse (settings))
3163 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
3164 print_job->reverse = FALSE;
3166 if (gtk_print_settings_get_n_copies (settings) > 1)
3167 gtk_print_settings_set_int (settings, "cups-copies",
3168 gtk_print_settings_get_n_copies (settings));
3169 print_job->num_copies = 1;
3171 scale = gtk_print_settings_get_scale (settings);
3172 print_job->scale = 1.0;
3174 print_job->scale = scale/100.0;
3176 page_set = gtk_print_settings_get_page_set (settings);
3177 if (page_set == GTK_PAGE_SET_EVEN)
3178 gtk_print_settings_set (settings, "cups-page-set", "even");
3179 else if (page_set == GTK_PAGE_SET_ODD)
3180 gtk_print_settings_set (settings, "cups-page-set", "odd");
3181 print_job->page_set = GTK_PAGE_SET_ALL;
3183 paper_size = gtk_page_setup_get_paper_size (page_setup);
3184 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
3185 if (ppd_paper_name != NULL)
3186 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
3189 char width[G_ASCII_DTOSTR_BUF_SIZE];
3190 char height[G_ASCII_DTOSTR_BUF_SIZE];
3193 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3194 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3195 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
3196 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
3197 g_free (custom_name);
3200 print_job->rotate_to_orientation = TRUE;
3203 static GtkPageSetup *
3204 create_page_setup (ppd_file_t *ppd_file,
3208 GtkPageSetup *page_setup;
3209 GtkPaperSize *paper_size;
3210 ppd_option_t *option;
3211 ppd_choice_t *choice;
3213 display_name = NULL;
3214 option = ppdFindOption (ppd_file, "PageSize");
3217 choice = ppdFindChoice (option, size->name);
3219 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
3222 if (display_name == NULL)
3223 display_name = g_strdup (size->name);
3225 page_setup = gtk_page_setup_new ();
3226 paper_size = gtk_paper_size_new_from_ppd (size->name,
3230 gtk_page_setup_set_paper_size (page_setup, paper_size);
3231 gtk_paper_size_free (paper_size);
3233 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
3234 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
3235 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
3236 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
3238 g_free (display_name);
3244 cups_printer_list_papers (GtkPrinter *printer)
3246 ppd_file_t *ppd_file;
3248 GtkPageSetup *page_setup;
3252 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3253 if (ppd_file == NULL)
3258 for (i = 0; i < ppd_file->num_sizes; i++)
3260 size = &ppd_file->sizes[i];
3262 page_setup = create_page_setup (ppd_file, size);
3264 l = g_list_prepend (l, page_setup);
3267 return g_list_reverse (l);
3270 static GtkPageSetup *
3271 cups_printer_get_default_page_size (GtkPrinter *printer)
3273 ppd_file_t *ppd_file;
3275 ppd_option_t *option;
3278 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3279 if (ppd_file == NULL)
3282 option = ppdFindOption (ppd_file, "PageSize");
3283 size = ppdPageSize (ppd_file, option->defchoice);
3285 return create_page_setup (ppd_file, size);
3289 cups_printer_get_hard_margins (GtkPrinter *printer,
3295 ppd_file_t *ppd_file;
3297 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3298 if (ppd_file == NULL)
3301 *left = ppd_file->custom_margins[0];
3302 *bottom = ppd_file->custom_margins[1];
3303 *right = ppd_file->custom_margins[2];
3304 *top = ppd_file->custom_margins[3];
3307 static GtkPrintCapabilities
3308 cups_printer_get_capabilities (GtkPrinter *printer)
3311 GTK_PRINT_CAPABILITY_COPIES |
3312 GTK_PRINT_CAPABILITY_COLLATE |
3313 GTK_PRINT_CAPABILITY_REVERSE |
3314 GTK_PRINT_CAPABILITY_NUMBER_UP;