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/gtkprinter.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;
438 g_print ("CUPS Backend: %s\n", G_STRFUNC));
440 cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
441 settings = gtk_print_job_get_settings (job);
443 request = gtk_cups_request_new (NULL,
447 cups_printer->hostname,
448 cups_printer->device_uri);
450 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
451 IPP_TAG_URI, "printer-uri",
452 NULL, cups_printer->printer_uri);
454 title = gtk_print_job_get_title (job);
456 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
457 IPP_TAG_NAME, "job-name",
460 gtk_print_settings_foreach (settings, add_cups_options, request);
462 ps = g_new0 (CupsPrintStreamData, 1);
463 ps->callback = callback;
464 ps->user_data = user_data;
465 ps->dnotify = dnotify;
466 ps->job = g_object_ref (job);
468 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
470 (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
472 (GDestroyNotify)cups_free_print_stream_data);
477 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
479 backend_cups->list_printers_poll = FALSE;
480 backend_cups->got_default_printer = FALSE;
481 backend_cups->list_printers_pending = FALSE;
483 cups_request_default_printer (backend_cups);
487 gtk_print_backend_cups_finalize (GObject *object)
489 GtkPrintBackendCups *backend_cups;
492 g_print ("CUPS Backend: finalizing CUPS backend module\n"));
494 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
496 g_free (backend_cups->default_printer);
497 backend_cups->default_printer = NULL;
499 backend_parent_class->finalize (object);
503 gtk_print_backend_cups_dispose (GObject *object)
505 GtkPrintBackendCups *backend_cups;
508 g_print ("CUPS Backend: %s\n", G_STRFUNC));
510 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
512 if (backend_cups->list_printers_poll > 0)
513 g_source_remove (backend_cups->list_printers_poll);
514 backend_cups->list_printers_poll = 0;
516 backend_parent_class->dispose (object);
521 cups_dispatch_watch_check (GSource *source)
523 GtkPrintCupsDispatchWatch *dispatch;
524 GtkCupsPollState poll_state;
528 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
530 dispatch = (GtkPrintCupsDispatchWatch *) source;
532 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
534 if (dispatch->request->http != NULL)
536 if (dispatch->data_poll == NULL)
538 dispatch->data_poll = g_new0 (GPollFD, 1);
539 g_source_add_poll (source, dispatch->data_poll);
543 if (poll_state == GTK_CUPS_HTTP_READ)
544 dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
545 else if (poll_state == GTK_CUPS_HTTP_WRITE)
546 dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
548 dispatch->data_poll->events = 0;
551 #ifdef HAVE_CUPS_API_1_2
552 dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
554 dispatch->data_poll->fd = dispatch->request->http->fd;
558 if (poll_state != GTK_CUPS_HTTP_IDLE)
559 if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
562 result = gtk_cups_request_read_write (dispatch->request);
563 if (result && dispatch->data_poll != NULL)
565 g_source_remove_poll (source, dispatch->data_poll);
566 g_free (dispatch->data_poll);
567 dispatch->data_poll = NULL;
574 cups_dispatch_watch_prepare (GSource *source,
577 GtkPrintCupsDispatchWatch *dispatch;
579 dispatch = (GtkPrintCupsDispatchWatch *) source;
582 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
586 return gtk_cups_request_read_write (dispatch->request);
590 cups_dispatch_watch_dispatch (GSource *source,
591 GSourceFunc callback,
594 GtkPrintCupsDispatchWatch *dispatch;
595 GtkPrintCupsResponseCallbackFunc ep_callback;
596 GtkCupsResult *result;
598 g_assert (callback != NULL);
600 ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
602 dispatch = (GtkPrintCupsDispatchWatch *) source;
604 result = gtk_cups_request_get_result (dispatch->request);
607 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
609 if (gtk_cups_result_is_error (result))
612 g_print("Error result: %s (type %i, status %i, code %i)\n",
613 gtk_cups_result_get_error_string (result),
614 gtk_cups_result_get_error_type (result),
615 gtk_cups_result_get_error_status (result),
616 gtk_cups_result_get_error_code (result)));
619 ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
625 cups_dispatch_watch_finalize (GSource *source)
627 GtkPrintCupsDispatchWatch *dispatch;
630 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
632 dispatch = (GtkPrintCupsDispatchWatch *) source;
634 gtk_cups_request_free (dispatch->request);
636 if (dispatch->backend)
638 /* We need to unref this at idle time, because it might be the
639 * last reference to this module causing the code to be
640 * unloaded (including this particular function!)
641 * Update: Doing this at idle caused a deadlock taking the
642 * mainloop context lock while being in a GSource callout for
643 * multithreaded apps. So, for now we just disable unloading
644 * of print backends. See _gtk_print_backend_create for the
647 g_object_unref (dispatch->backend);
648 dispatch->backend = NULL;
651 g_free (dispatch->data_poll);
654 static GSourceFuncs _cups_dispatch_watch_funcs = {
655 cups_dispatch_watch_prepare,
656 cups_dispatch_watch_check,
657 cups_dispatch_watch_dispatch,
658 cups_dispatch_watch_finalize
663 cups_request_execute (GtkPrintBackendCups *print_backend,
664 GtkCupsRequest *request,
665 GtkPrintCupsResponseCallbackFunc callback,
667 GDestroyNotify notify)
669 GtkPrintCupsDispatchWatch *dispatch;
671 dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
672 sizeof (GtkPrintCupsDispatchWatch));
675 g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
677 dispatch->request = request;
678 dispatch->backend = g_object_ref (print_backend);
679 dispatch->data_poll = NULL;
681 g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
683 g_source_attach ((GSource *) dispatch, NULL);
684 g_source_unref ((GSource *) dispatch);
689 cups_request_printer_info_cb (GtkPrintBackendCups *backend,
690 GtkCupsResult *result,
693 ipp_attribute_t *attr;
696 GtkPrinterCups *cups_printer;
702 gboolean status_changed;
704 g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
706 printer_name = (gchar *)user_data;
707 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
711 g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
716 g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
720 cups_printer = GTK_PRINTER_CUPS (printer);
722 if (gtk_cups_result_is_error (result))
724 if (gtk_printer_is_new (printer))
726 gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
731 return; /* TODO: mark as inactive printer */
734 response = gtk_cups_result_get_response (result);
736 /* TODO: determine printer type and use correct icon */
737 gtk_printer_set_icon_name (printer, "gtk-print");
743 for (attr = response->attrs; attr != NULL; attr = attr->next)
748 _CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
749 _CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
750 _CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
751 _CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
752 _CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
755 status_changed = gtk_printer_set_job_count (printer, job_count);
757 status_changed |= gtk_printer_set_location (printer, loc);
758 status_changed |= gtk_printer_set_description (printer, desc);
759 status_changed |= gtk_printer_set_state_message (printer, state_msg);
762 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
763 "printer-status-changed", printer);
767 cups_request_printer_info (GtkPrintBackendCups *print_backend,
768 const gchar *printer_name)
770 GtkCupsRequest *request;
772 static const char * const pattrs[] = /* Attributes we're interested in */
776 "printer-state-message",
781 request = gtk_cups_request_new (NULL,
783 IPP_GET_PRINTER_ATTRIBUTES,
788 printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
790 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
791 "printer-uri", NULL, printer_uri);
794 g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
796 g_free (printer_uri);
798 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
799 "requested-attributes", G_N_ELEMENTS (pattrs),
802 cups_request_execute (print_backend,
804 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
805 g_strdup (printer_name),
806 (GDestroyNotify) g_free);
811 GtkPrintBackendCups *print_backend;
818 job_object_died (gpointer user_data,
819 GObject *where_the_object_was)
821 CupsJobPollData *data = user_data;
826 cups_job_poll_data_free (CupsJobPollData *data)
829 g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
835 cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
836 GtkCupsResult *result,
839 CupsJobPollData *data = user_data;
840 ipp_attribute_t *attr;
845 GDK_THREADS_ENTER ();
847 if (data->job == NULL)
849 cups_job_poll_data_free (data);
855 response = gtk_cups_result_get_response (result);
858 for (attr = response->attrs; attr != NULL; attr = attr->next)
863 _CUPS_MAP_ATTR_INT (attr, state, "job-state");
869 case IPP_JOB_PENDING:
871 case IPP_JOB_STOPPED:
872 gtk_print_job_set_status (data->job,
873 GTK_PRINT_STATUS_PENDING);
875 case IPP_JOB_PROCESSING:
876 gtk_print_job_set_status (data->job,
877 GTK_PRINT_STATUS_PRINTING);
880 case IPP_JOB_CANCELLED:
881 case IPP_JOB_ABORTED:
882 gtk_print_job_set_status (data->job,
883 GTK_PRINT_STATUS_FINISHED_ABORTED);
887 case IPP_JOB_COMPLETED:
888 gtk_print_job_set_status (data->job,
889 GTK_PRINT_STATUS_FINISHED);
894 if (!done && data->job != NULL)
898 if (data->counter < 5)
900 else if (data->counter < 10)
905 g_timeout_add (timeout, cups_job_info_poll_timeout, data);
908 cups_job_poll_data_free (data);
911 GDK_THREADS_LEAVE ();
915 cups_request_job_info (CupsJobPollData *data)
917 GtkCupsRequest *request;
920 request = gtk_cups_request_new (NULL,
922 IPP_GET_JOB_ATTRIBUTES,
927 job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
928 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
929 "job-uri", NULL, job_uri);
932 cups_request_execute (data->print_backend,
934 (GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
940 cups_job_info_poll_timeout (gpointer user_data)
942 CupsJobPollData *data = user_data;
944 if (data->job == NULL)
945 cups_job_poll_data_free (data);
947 cups_request_job_info (data);
953 cups_begin_polling_info (GtkPrintBackendCups *print_backend,
957 CupsJobPollData *data;
959 data = g_new0 (CupsJobPollData, 1);
961 data->print_backend = print_backend;
963 data->job_id = job_id;
966 g_object_weak_ref (G_OBJECT (job), job_object_died, data);
968 cups_request_job_info (data);
972 mark_printer_inactive (GtkPrinter *printer,
973 GtkPrintBackend *backend)
975 gtk_printer_set_is_active (printer, FALSE);
976 g_signal_emit_by_name (backend, "printer-removed", printer);
980 find_printer (GtkPrinter *printer,
981 const gchar *find_name)
983 const gchar *printer_name;
985 printer_name = gtk_printer_get_name (printer);
986 return g_ascii_strcasecmp (printer_name, find_name);
990 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
991 GtkCupsResult *result,
994 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
995 ipp_attribute_t *attr;
997 gboolean list_has_changed;
998 GList *removed_printer_checklist;
1000 GDK_THREADS_ENTER ();
1002 list_has_changed = FALSE;
1005 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1007 cups_backend->list_printers_pending = FALSE;
1009 if (gtk_cups_result_is_error (result))
1012 g_warning ("CUPS Backend: Error getting printer list: %s",
1013 gtk_cups_result_get_error_string (result)));
1018 /* Gather the names of the printers in the current queue
1019 * so we may check to see if they were removed
1021 removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
1023 response = gtk_cups_result_get_response (result);
1025 for (attr = response->attrs; attr != NULL; attr = attr->next)
1027 GtkPrinter *printer;
1028 const gchar *printer_name = NULL;
1029 const gchar *printer_uri = NULL;
1030 const gchar *member_uris = NULL;
1031 const gchar *location = NULL;
1032 const gchar *description = NULL;
1033 const gchar *state_msg = NULL;
1036 gboolean status_changed = FALSE;
1039 const gchar *reason_msg = NULL;
1040 gchar *reason_msg_desc = NULL;
1041 gchar *tmp_msg = NULL;
1042 gint printer_state_reason_level = 0; /* 0 - none, 1 - report, 2 - warning, 3 - error */
1043 gboolean interested_in = FALSE;
1044 gboolean found = FALSE;
1045 static const char * const reasons[] = /* Reasons we're interested in */
1051 "marker-supply-low",
1052 "marker-supply-empty",
1058 "connecting-to-device",
1061 static const char * reasons_descs[] =
1063 N_("Printer '%s' is low on toner."),
1064 N_("Printer '%s' has no toner left."),
1065 N_("Printer '%s' is low on developer."),
1066 N_("Printer '%s' is out of developer."),
1067 N_("Printer '%s' is low on at least one marker supply."),
1068 N_("Printer '%s' is out of at least one marker supply."),
1069 N_("The cover is open on printer '%s'."),
1070 N_("The door is open on printer '%s'."),
1071 N_("Printer '%s' is low on paper."),
1072 N_("Printer '%s' is out of paper."),
1073 N_("Printer '%s' is currently off-line."),
1074 N_("Printer '%s' may not be connected."),
1075 N_("There is a problem on printer '%s'.")
1078 /* Skip leading attributes until we hit a printer...
1080 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1086 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1088 if (strcmp (attr->name, "printer-name") == 0 &&
1089 attr->value_tag == IPP_TAG_NAME)
1090 printer_name = attr->values[0].string.text;
1091 else if (strcmp (attr->name, "printer-uri-supported") == 0 &&
1092 attr->value_tag == IPP_TAG_URI)
1093 printer_uri = attr->values[0].string.text;
1094 else if (strcmp (attr->name, "member-uris") == 0 &&
1095 attr->value_tag == IPP_TAG_URI)
1096 member_uris = attr->values[0].string.text;
1097 else if (strcmp (attr->name, "printer-location") == 0)
1098 location = attr->values[0].string.text;
1099 else if (strcmp (attr->name, "printer-info") == 0)
1100 description = attr->values[0].string.text;
1101 else if (strcmp (attr->name, "printer-state-message") == 0)
1102 state_msg = attr->values[0].string.text;
1103 else if (strcmp (attr->name, "printer-state-reasons") == 0)
1104 /* Store most important reason to reason_msg and set
1105 its importance at printer_state_reason_level */
1107 for (i = 0; i < attr->num_values; i++)
1109 if (strcmp (attr->values[i].string.text, "none") != 0)
1111 interested_in = FALSE;
1112 for (j = 0; j < G_N_ELEMENTS (reasons); j++)
1113 if (strncmp (attr->values[i].string.text, reasons[j], strlen (reasons[j])) == 0)
1115 interested_in = TRUE;
1121 if (g_str_has_suffix (attr->values[i].string.text, "-report"))
1123 if (printer_state_reason_level <= 1)
1125 reason_msg = attr->values[i].string.text;
1126 printer_state_reason_level = 1;
1129 else if (g_str_has_suffix (attr->values[i].string.text, "-warning"))
1131 if (printer_state_reason_level <= 2)
1133 reason_msg = attr->values[i].string.text;
1134 printer_state_reason_level = 2;
1137 else /* It is error in the case of no suffix. */
1139 reason_msg = attr->values[i].string.text;
1140 printer_state_reason_level = 3;
1146 else if (strcmp (attr->name, "printer-state") == 0)
1147 state = attr->values[0].integer;
1148 else if (strcmp (attr->name, "queued-job-count") == 0)
1149 job_count = attr->values[0].integer;
1153 g_print ("CUPS Backend: Attribute %s ignored", attr->name));
1159 if (printer_name == NULL ||
1160 (printer_uri == NULL && member_uris == NULL))
1168 /* remove name from checklist if it was found */
1169 node = g_list_find_custom (removed_printer_checklist, printer_name, (GCompareFunc) find_printer);
1170 removed_printer_checklist = g_list_delete_link (removed_printer_checklist, node);
1172 printer = gtk_print_backend_find_printer (backend, printer_name);
1175 GtkPrinterCups *cups_printer;
1176 char uri[HTTP_MAX_URI]; /* Printer URI */
1177 char method[HTTP_MAX_URI]; /* Method/scheme name */
1178 char username[HTTP_MAX_URI]; /* Username:password */
1179 char hostname[HTTP_MAX_URI]; /* Hostname */
1180 char resource[HTTP_MAX_URI]; /* Resource name */
1181 int port; /* Port number */
1183 list_has_changed = TRUE;
1184 cups_printer = gtk_printer_cups_new (printer_name, backend);
1186 cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
1188 /* Check to see if we are looking at a class */
1191 cups_printer->printer_uri = g_strdup (member_uris);
1192 /* TODO if member_uris is a class we need to recursivly find a printer */
1194 g_print ("CUPS Backend: Found class with printer %s\n", member_uris));
1198 cups_printer->printer_uri = g_strdup (printer_uri);
1200 g_print ("CUPS Backend: Found printer %s\n", printer_uri));
1203 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
1204 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
1205 method, sizeof (method),
1206 username, sizeof (username),
1207 hostname, sizeof (hostname),
1209 resource, sizeof (resource));
1212 httpSeparate (cups_printer->printer_uri,
1220 if (strncmp (resource, "/printers/", 10) == 0)
1222 cups_printer->ppd_name = g_strdup (resource + 10);
1224 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
1227 gethostname (uri, sizeof (uri));
1228 if (strcasecmp (uri, hostname) == 0)
1229 strcpy (hostname, "localhost");
1231 cups_printer->hostname = g_strdup (hostname);
1232 cups_printer->port = port;
1234 printer = GTK_PRINTER (cups_printer);
1236 if (cups_backend->default_printer != NULL &&
1237 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
1238 gtk_printer_set_is_default (printer, TRUE);
1241 gtk_print_backend_add_printer (backend, printer);
1244 g_object_ref (printer);
1246 if (!gtk_printer_is_active (printer))
1248 gtk_printer_set_is_active (printer, TRUE);
1249 gtk_printer_set_is_new (printer, TRUE);
1250 list_has_changed = TRUE;
1253 if (gtk_printer_is_new (printer))
1255 g_signal_emit_by_name (backend, "printer-added", printer);
1257 gtk_printer_set_is_new (printer, FALSE);
1261 /* Getting printer info with separate requests overwhelms cups
1262 * when the printer list has more than a handful of printers.
1264 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
1267 GTK_PRINTER_CUPS (printer)->state = state;
1268 status_changed = gtk_printer_set_job_count (printer, job_count);
1269 status_changed |= gtk_printer_set_location (printer, location);
1270 status_changed |= gtk_printer_set_description (printer, description);
1272 /* Set description of the reason and combine it with printer-state-message. */
1273 if ( (reason_msg != NULL))
1275 for (i = 0; i < G_N_ELEMENTS (reasons); i++)
1277 if (strncmp (reason_msg, reasons[i], strlen (reasons[i])) == 0)
1279 reason_msg_desc = g_strdup_printf (reasons_descs[i], printer_name);
1286 printer_state_reason_level = 0;
1288 if (printer_state_reason_level >= 2)
1290 if (strlen (state_msg) == 0)
1291 state_msg = reason_msg_desc;
1294 tmp_msg = g_strjoin (" ; ", state_msg, reason_msg_desc, NULL);
1295 state_msg = tmp_msg;
1300 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1302 if (tmp_msg != NULL)
1305 if (reason_msg_desc != NULL)
1306 g_free (reason_msg_desc);
1308 /* Set printer icon according to importance
1309 (none, report, warning, error - report is omitted). */
1310 if (printer_state_reason_level == 3)
1311 gtk_printer_set_icon_name (printer, "gtk-print-error");
1312 else if (printer_state_reason_level == 2)
1313 gtk_printer_set_icon_name (printer, "gtk-print-warning");
1315 gtk_printer_set_icon_name (printer, "gtk-print");
1318 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1319 "printer-status-changed", printer);
1321 /* The ref is held by GtkPrintBackend, in add_printer() */
1322 g_object_unref (printer);
1328 /* look at the removed printers checklist and mark any printer
1329 as inactive if it is in the list, emitting a printer_removed signal */
1330 if (removed_printer_checklist != NULL)
1332 g_list_foreach (removed_printer_checklist, (GFunc) mark_printer_inactive, backend);
1333 g_list_free (removed_printer_checklist);
1334 list_has_changed = TRUE;
1338 if (list_has_changed)
1339 g_signal_emit_by_name (backend, "printer-list-changed");
1341 gtk_print_backend_set_list_done (backend);
1343 GDK_THREADS_LEAVE ();
1347 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
1349 GtkCupsRequest *request;
1350 static const char * const pattrs[] = /* Attributes we're interested in */
1353 "printer-uri-supported",
1357 "printer-state-message",
1358 "printer-state-reasons",
1363 if (cups_backend->list_printers_pending ||
1364 !cups_backend->got_default_printer)
1367 cups_backend->list_printers_pending = TRUE;
1369 request = gtk_cups_request_new (NULL,
1376 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1377 "requested-attributes", G_N_ELEMENTS (pattrs),
1380 cups_request_execute (cups_backend,
1382 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
1390 cups_get_printer_list (GtkPrintBackend *backend)
1392 GtkPrintBackendCups *cups_backend;
1394 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
1395 if (cups_backend->list_printers_poll == 0)
1397 cups_request_printer_list (cups_backend);
1398 cups_backend->list_printers_poll = gdk_threads_add_timeout (3000,
1399 (GSourceFunc) cups_request_printer_list,
1405 GtkPrinterCups *printer;
1411 get_ppd_data_free (GetPPDData *data)
1414 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1415 httpClose (data->http);
1416 g_io_channel_unref (data->ppd_io);
1417 g_object_unref (data->printer);
1422 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
1423 GtkCupsResult *result,
1427 GtkPrinter *printer;
1429 GDK_THREADS_ENTER ();
1432 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1434 printer = GTK_PRINTER (data->printer);
1435 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
1437 if (gtk_cups_result_is_error (result))
1439 gboolean success = FALSE;
1441 /* if we get a 404 then it is just a raw printer without a ppd
1443 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
1444 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
1446 gtk_printer_set_has_details (printer, TRUE);
1450 g_signal_emit_by_name (printer, "details-acquired", success);
1454 response = gtk_cups_result_get_response (result);
1456 /* let ppdOpenFd take over the ownership of the open file */
1457 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
1458 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
1460 ppdMarkDefaults (data->printer->ppd_file);
1462 gtk_printer_set_has_details (printer, TRUE);
1463 g_signal_emit_by_name (printer, "details-acquired", TRUE);
1466 GDK_THREADS_LEAVE ();
1470 cups_request_ppd (GtkPrinter *printer)
1473 GtkPrintBackend *print_backend;
1474 GtkPrinterCups *cups_printer;
1475 GtkCupsRequest *request;
1482 cups_printer = GTK_PRINTER_CUPS (printer);
1487 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1489 http = httpConnectEncrypt (cups_printer->hostname,
1493 data = g_new0 (GetPPDData, 1);
1495 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
1499 #ifdef G_ENABLE_DEBUG
1500 /* If we are debugging printing don't delete the tmp files */
1501 if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
1502 unlink (ppd_filename);
1504 unlink (ppd_filename);
1505 #endif /* G_ENABLE_DEBUG */
1510 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
1512 g_error_free (error);
1514 g_free (ppd_filename);
1517 g_signal_emit_by_name (printer, "details-acquired", FALSE);
1522 fchmod (fd, S_IRUSR | S_IWUSR);
1523 data->ppd_io = g_io_channel_unix_new (fd);
1524 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
1525 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
1527 data->printer = g_object_ref (printer);
1529 resource = g_strdup_printf ("/printers/%s.ppd",
1530 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
1532 request = gtk_cups_request_new (data->http,
1536 cups_printer->hostname,
1540 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
1543 g_free (ppd_filename);
1545 cups_printer->reading_ppd = TRUE;
1547 print_backend = gtk_printer_get_backend (printer);
1549 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
1551 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
1553 (GDestroyNotify)get_ppd_data_free);
1556 /* Ordering matters for default preference */
1557 static const char *lpoptions_locations[] = {
1558 "/etc/cups/lpoptions",
1564 cups_parse_user_default_printer (const char *filename,
1565 char **printer_name)
1568 char line[1024], *lineptr, *defname = NULL;
1570 if ((fp = g_fopen (filename, "r")) == NULL)
1573 while (fgets (line, sizeof (line), fp) != NULL)
1575 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
1579 while (isspace (*lineptr))
1586 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
1591 if (*printer_name != NULL)
1592 g_free (*printer_name);
1594 *printer_name = g_strdup (defname);
1601 cups_get_user_default_printer (char **printer_name)
1605 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1607 if (g_path_is_absolute (lpoptions_locations[i]))
1609 cups_parse_user_default_printer (lpoptions_locations[i],
1616 filename = g_build_filename (g_get_home_dir (),
1617 lpoptions_locations[i], NULL);
1618 cups_parse_user_default_printer (filename, printer_name);
1625 cups_parse_user_options (const char *filename,
1626 const char *printer_name,
1628 cups_option_t **options)
1631 gchar line[1024], *lineptr, *name;
1633 if ((fp = g_fopen (filename, "r")) == NULL)
1636 while (fgets (line, sizeof (line), fp) != NULL)
1638 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
1640 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
1645 /* Skip leading whitespace */
1646 while (isspace (*lineptr))
1652 /* NUL-terminate the name, stripping the instance name */
1654 while (!isspace (*lineptr) && *lineptr)
1656 if (*lineptr == '/')
1666 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
1669 /* We found our printer, parse the options */
1670 num_options = cupsParseOptions (lineptr, num_options, options);
1679 cups_get_user_options (const char *printer_name,
1681 cups_option_t **options)
1685 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1687 if (g_path_is_absolute (lpoptions_locations[i]))
1689 num_options = cups_parse_user_options (lpoptions_locations[i],
1698 filename = g_build_filename (g_get_home_dir (),
1699 lpoptions_locations[i], NULL);
1700 num_options = cups_parse_user_options (filename, printer_name,
1701 num_options, options);
1710 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
1711 GtkCupsResult *result,
1715 ipp_attribute_t *attr;
1717 response = gtk_cups_result_get_response (result);
1719 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
1720 print_backend->default_printer = g_strdup (attr->values[0].string.text);
1722 print_backend->got_default_printer = TRUE;
1724 /* Make sure to kick off get_printers if we are polling it,
1725 * as we could have blocked this reading the default printer
1727 if (print_backend->list_printers_poll != 0)
1728 cups_request_printer_list (print_backend);
1732 cups_request_default_printer (GtkPrintBackendCups *print_backend)
1734 GtkCupsRequest *request;
1738 if ((str = g_getenv ("LPDEST")) != NULL)
1740 print_backend->default_printer = g_strdup (str);
1741 print_backend->got_default_printer = TRUE;
1744 else if ((str = g_getenv ("PRINTER")) != NULL &&
1745 strcmp (str, "lp") != 0)
1747 print_backend->default_printer = g_strdup (str);
1748 print_backend->got_default_printer = TRUE;
1752 /* Figure out user setting for default printer */
1753 cups_get_user_default_printer (&name);
1756 print_backend->default_printer = name;
1757 print_backend->got_default_printer = TRUE;
1761 request = gtk_cups_request_new (NULL,
1768 cups_request_execute (print_backend,
1770 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
1771 g_object_ref (print_backend),
1776 cups_printer_request_details (GtkPrinter *printer)
1778 GtkPrinterCups *cups_printer;
1780 cups_printer = GTK_PRINTER_CUPS (printer);
1781 if (!cups_printer->reading_ppd &&
1782 gtk_printer_cups_get_ppd (cups_printer) == NULL)
1783 cups_request_ppd (printer);
1787 ppd_text_to_utf8 (ppd_file_t *ppd_file,
1790 const char *encoding = NULL;
1793 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
1795 return g_strdup (text);
1797 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
1799 encoding = "ISO-8859-1";
1801 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
1803 encoding = "ISO-8859-2";
1805 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
1807 encoding = "ISO-8859-5";
1809 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
1811 encoding = "SHIFT-JIS";
1813 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
1815 encoding = "MACINTOSH";
1817 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
1819 encoding = "WINDOWS-1252";
1823 /* Fallback, try iso-8859-1... */
1824 encoding = "ISO-8859-1";
1827 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
1832 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
1833 res = g_strdup ("???");
1839 /* TODO: Add more translations for common settings here */
1841 static const struct {
1842 const char *keyword;
1843 const char *translation;
1844 } cups_option_translations[] = {
1845 { "Duplex", N_("Two Sided") },
1846 { "MediaType", N_("Paper Type") },
1847 { "InputSlot", N_("Paper Source") },
1848 { "OutputBin", N_("Output Tray") },
1852 static const struct {
1853 const char *keyword;
1855 const char *translation;
1856 } cups_choice_translations[] = {
1857 { "Duplex", "None", N_("One Sided") },
1858 { "InputSlot", "Auto", N_("Auto Select") },
1859 { "InputSlot", "AutoSelect", N_("Auto Select") },
1860 { "InputSlot", "Default", N_("Printer Default") },
1861 { "InputSlot", "None", N_("Printer Default") },
1862 { "InputSlot", "PrinterDefault", N_("Printer Default") },
1863 { "InputSlot", "Unspecified", N_("Auto Select") },
1866 static const struct {
1867 const char *ppd_keyword;
1869 } option_names[] = {
1870 {"Duplex", "gtk-duplex" },
1871 {"MediaType", "gtk-paper-type"},
1872 {"InputSlot", "gtk-paper-source"},
1873 {"OutputBin", "gtk-output-tray"},
1876 /* keep sorted when changing */
1877 static const char *color_option_whitelist[] = {
1878 "BRColorEnhancement",
1884 "BlackSubstitution",
1892 "RPSBlackOverPrint",
1896 /* keep sorted when changing */
1897 static const char *color_group_whitelist[] = {
1904 "HPColorOptionsPanel",
1907 /* keep sorted when changing */
1908 static const char *image_quality_option_whitelist[] = {
1910 "BRHalfTonePattern",
1920 "HPGraphicsHalftone",
1935 /* keep sorted when changing */
1936 static const char *image_quality_group_whitelist[] = {
1943 /* keep sorted when changing */
1944 static const char * finishing_option_whitelist[] = {
1957 "StapleOrientation",
1963 /* keep sorted when changing */
1964 static const char *finishing_group_whitelist[] = {
1973 /* keep sorted when changing */
1974 static const char *cups_option_blacklist[] = {
1983 get_option_text (ppd_file_t *ppd_file,
1984 ppd_option_t *option)
1989 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
1991 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
1992 return g_strdup (_(cups_option_translations[i].translation));
1995 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
1997 /* Some ppd files have spaces in the text before the colon */
2004 get_choice_text (ppd_file_t *ppd_file,
2005 ppd_choice_t *choice)
2008 ppd_option_t *option = choice->option;
2009 const char *keyword = option->keyword;
2011 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
2013 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
2014 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
2015 return g_strdup (_(cups_choice_translations[i].translation));
2017 return ppd_text_to_utf8 (ppd_file, choice->text);
2021 group_has_option (ppd_group_t *group,
2022 ppd_option_t *option)
2029 if (group->num_options > 0 &&
2030 option >= group->options && option < group->options + group->num_options)
2033 for (i = 0; i < group->num_subgroups; i++)
2035 if (group_has_option (&group->subgroups[i],option))
2042 set_option_off (GtkPrinterOption *option)
2044 /* Any of these will do, _set only applies the value
2045 * if its allowed of the option */
2046 gtk_printer_option_set (option, "False");
2047 gtk_printer_option_set (option, "Off");
2048 gtk_printer_option_set (option, "None");
2052 value_is_off (const char *value)
2054 return (strcasecmp (value, "None") == 0 ||
2055 strcasecmp (value, "Off") == 0 ||
2056 strcasecmp (value, "False") == 0);
2060 ppd_group_name (ppd_group_t *group)
2062 #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)
2070 available_choices (ppd_file_t *ppd,
2071 ppd_option_t *option,
2072 ppd_choice_t ***available,
2073 gboolean keep_if_only_one_option)
2075 ppd_option_t *other_option;
2078 ppd_const_t *constraint;
2079 const char *choice, *other_choice;
2080 ppd_option_t *option1, *option2;
2081 ppd_group_t *installed_options;
2083 gboolean all_default;
2089 conflicts = g_new0 (char, option->num_choices);
2091 installed_options = NULL;
2092 for (i = 0; i < ppd->num_groups; i++)
2096 name = ppd_group_name (&ppd->groups[i]);
2097 if (strcmp (name, "InstallableOptions") == 0)
2099 installed_options = &ppd->groups[i];
2104 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
2106 option1 = ppdFindOption (ppd, constraint->option1);
2107 if (option1 == NULL)
2110 option2 = ppdFindOption (ppd, constraint->option2);
2111 if (option2 == NULL)
2114 if (option == option1)
2116 choice = constraint->choice1;
2117 other_option = option2;
2118 other_choice = constraint->choice2;
2120 else if (option == option2)
2122 choice = constraint->choice2;
2123 other_option = option1;
2124 other_choice = constraint->choice1;
2129 /* We only care of conflicts with installed_options and
2131 if (!group_has_option (installed_options, other_option) &&
2132 (strcmp (other_option->keyword, "PageSize") != 0))
2135 if (*other_choice == 0)
2137 /* Conflict only if the installed option is not off */
2138 if (value_is_off (other_option->defchoice))
2141 /* Conflict if the installed option has the specified default */
2142 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
2147 /* Conflict with all non-off choices */
2148 for (j = 0; j < option->num_choices; j++)
2150 if (!value_is_off (option->choices[j].choice))
2156 for (j = 0; j < option->num_choices; j++)
2158 if (strcasecmp (option->choices[j].choice, choice) == 0)
2166 for (j = 0; j < option->num_choices; j++)
2170 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
2171 all_default = FALSE;
2174 if ((all_default && !keep_if_only_one_option) ||
2175 (num_conflicts == option->num_choices))
2182 /* Some ppds don't have a "use printer default" option for
2183 * InputSlot. This means you always have to select a particular slot,
2184 * and you can't auto-pick source based on the paper size. To support
2185 * this we always add an auto option if there isn't one already. If
2186 * the user chooses the generated option we don't send any InputSlot
2187 * value when printing. The way we detect existing auto-cases is based
2188 * on feedback from Michael Sweet of cups fame.
2191 if (strcmp (option->keyword, "InputSlot") == 0)
2193 gboolean found_auto = FALSE;
2194 for (j = 0; j < option->num_choices; j++)
2198 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
2199 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
2200 strcmp (option->choices[j].choice, "Default") == 0 ||
2201 strcmp (option->choices[j].choice, "None") == 0 ||
2202 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
2203 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
2204 option->choices[j].code == NULL ||
2205 option->choices[j].code[0] == 0)
2219 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
2222 for (j = 0; j < option->num_choices; j++)
2225 (*available)[i++] = &option->choices[j];
2229 (*available)[i++] = NULL;
2234 return option->num_choices - num_conflicts + add_auto;
2237 static GtkPrinterOption *
2238 create_pickone_option (ppd_file_t *ppd_file,
2239 ppd_option_t *ppd_option,
2240 const gchar *gtk_name)
2242 GtkPrinterOption *option;
2243 ppd_choice_t **available;
2247 #ifdef HAVE_CUPS_API_1_2
2248 ppd_coption_t *coption;
2251 g_assert (ppd_option->ui == PPD_UI_PICKONE);
2255 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2259 /* right now only support one parameter per custom option
2260 * if more than one print warning and only offer the default choices
2263 label = get_option_text (ppd_file, ppd_option);
2265 #ifdef HAVE_CUPS_API_1_2
2266 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
2270 ppd_cparam_t *cparam;
2272 cparam = ppdFirstCustomParam (coption);
2274 if (ppdNextCustomParam (coption) == NULL)
2276 switch (cparam->type)
2278 case PPD_CUSTOM_INT:
2279 option = gtk_printer_option_new (gtk_name, label,
2280 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
2282 case PPD_CUSTOM_PASSCODE:
2283 option = gtk_printer_option_new (gtk_name, label,
2284 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
2286 case PPD_CUSTOM_PASSWORD:
2287 option = gtk_printer_option_new (gtk_name, label,
2288 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
2290 case PPD_CUSTOM_REAL:
2291 option = gtk_printer_option_new (gtk_name, label,
2292 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
2294 case PPD_CUSTOM_STRING:
2295 option = gtk_printer_option_new (gtk_name, label,
2296 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
2298 #ifdef PRINT_IGNORED_OPTIONS
2299 case PPD_CUSTOM_POINTS:
2300 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
2302 case PPD_CUSTOM_CURVE:
2303 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
2305 case PPD_CUSTOM_INVCURVE:
2306 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
2313 #ifdef PRINT_IGNORED_OPTIONS
2315 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
2318 #endif /* HAVE_CUPS_API_1_2 */
2321 option = gtk_printer_option_new (gtk_name, label,
2322 GTK_PRINTER_OPTION_TYPE_PICKONE);
2325 gtk_printer_option_allocate_choices (option, n_choices);
2326 for (i = 0; i < n_choices; i++)
2328 if (available[i] == NULL)
2330 /* This was auto-added */
2331 option->choices[i] = g_strdup ("gtk-ignore-value");
2332 option->choices_display[i] = g_strdup (_("Printer Default"));
2336 option->choices[i] = g_strdup (available[i]->choice);
2337 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
2340 gtk_printer_option_set (option, ppd_option->defchoice);
2342 #ifdef PRINT_IGNORED_OPTIONS
2344 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
2351 static GtkPrinterOption *
2352 create_boolean_option (ppd_file_t *ppd_file,
2353 ppd_option_t *ppd_option,
2354 const gchar *gtk_name)
2356 GtkPrinterOption *option;
2357 ppd_choice_t **available;
2361 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
2365 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2368 label = get_option_text (ppd_file, ppd_option);
2369 option = gtk_printer_option_new (gtk_name, label,
2370 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
2373 gtk_printer_option_allocate_choices (option, 2);
2374 option->choices[0] = g_strdup ("True");
2375 option->choices_display[0] = g_strdup ("True");
2376 option->choices[1] = g_strdup ("False");
2377 option->choices_display[1] = g_strdup ("False");
2379 gtk_printer_option_set (option, ppd_option->defchoice);
2381 #ifdef PRINT_IGNORED_OPTIONS
2383 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
2391 get_option_name (const gchar *keyword)
2395 for (i = 0; i < G_N_ELEMENTS (option_names); i++)
2396 if (strcmp (option_names[i].ppd_keyword, keyword) == 0)
2397 return g_strdup (option_names[i].name);
2399 return g_strdup_printf ("cups-%s", keyword);
2403 strptr_cmp (const void *a,
2406 char **aa = (char **)a;
2407 char **bb = (char **)b;
2408 return strcmp (*aa, *bb);
2413 string_in_table (gchar *str,
2414 const gchar *table[],
2417 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
2420 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
2423 handle_option (GtkPrinterOptionSet *set,
2424 ppd_file_t *ppd_file,
2425 ppd_option_t *ppd_option,
2426 ppd_group_t *toplevel_group,
2427 GtkPrintSettings *settings)
2429 GtkPrinterOption *option;
2432 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
2435 name = get_option_name (ppd_option->keyword);
2438 if (ppd_option->ui == PPD_UI_PICKONE)
2440 option = create_pickone_option (ppd_file, ppd_option, name);
2442 else if (ppd_option->ui == PPD_UI_BOOLEAN)
2444 option = create_boolean_option (ppd_file, ppd_option, name);
2446 #ifdef PRINT_IGNORED_OPTIONS
2448 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
2455 name = ppd_group_name (toplevel_group);
2456 if (STRING_IN_TABLE (name,
2457 color_group_whitelist) ||
2458 STRING_IN_TABLE (ppd_option->keyword,
2459 color_option_whitelist))
2461 option->group = g_strdup ("ColorPage");
2463 else if (STRING_IN_TABLE (name,
2464 image_quality_group_whitelist) ||
2465 STRING_IN_TABLE (ppd_option->keyword,
2466 image_quality_option_whitelist))
2468 option->group = g_strdup ("ImageQualityPage");
2470 else if (STRING_IN_TABLE (name,
2471 finishing_group_whitelist) ||
2472 STRING_IN_TABLE (ppd_option->keyword,
2473 finishing_option_whitelist))
2475 option->group = g_strdup ("FinishingPage");
2479 option->group = g_strdup (toplevel_group->text);
2482 set_option_from_settings (option, settings);
2484 gtk_printer_option_set_add (set, option);
2491 handle_group (GtkPrinterOptionSet *set,
2492 ppd_file_t *ppd_file,
2494 ppd_group_t *toplevel_group,
2495 GtkPrintSettings *settings)
2500 /* Ignore installable options */
2501 name = ppd_group_name (toplevel_group);
2502 if (strcmp (name, "InstallableOptions") == 0)
2505 for (i = 0; i < group->num_options; i++)
2506 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
2508 for (i = 0; i < group->num_subgroups; i++)
2509 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
2513 static GtkPrinterOptionSet *
2514 cups_printer_get_options (GtkPrinter *printer,
2515 GtkPrintSettings *settings,
2516 GtkPageSetup *page_setup,
2517 GtkPrintCapabilities capabilities)
2519 GtkPrinterOptionSet *set;
2520 GtkPrinterOption *option;
2521 ppd_file_t *ppd_file;
2523 char *print_at[] = { "now", "at", "on-hold" };
2524 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
2525 char *prio[] = {"100", "80", "50", "30" };
2526 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
2527 char *cover[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
2528 char *cover_display[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
2531 cups_option_t *opts = NULL;
2534 set = gtk_printer_option_set_new ();
2536 /* Cups specific, non-ppd related settings */
2538 option = gtk_printer_option_new ("gtk-n-up", "Pages Per Sheet", GTK_PRINTER_OPTION_TYPE_PICKONE);
2539 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
2541 gtk_printer_option_set (option, "1");
2542 set_option_from_settings (option, settings);
2543 gtk_printer_option_set_add (set, option);
2544 g_object_unref (option);
2546 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
2547 prio_display[i] = _(prio_display[i]);
2549 option = gtk_printer_option_new ("gtk-job-prio", "Job Priority", GTK_PRINTER_OPTION_TYPE_PICKONE);
2550 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
2551 prio, prio_display);
2552 gtk_printer_option_set (option, "50");
2553 set_option_from_settings (option, settings);
2554 gtk_printer_option_set_add (set, option);
2555 g_object_unref (option);
2557 option = gtk_printer_option_new ("gtk-billing-info", "Billing Info", GTK_PRINTER_OPTION_TYPE_STRING);
2558 gtk_printer_option_set (option, "");
2559 set_option_from_settings (option, settings);
2560 gtk_printer_option_set_add (set, option);
2561 g_object_unref (option);
2563 for (i = 0; i < G_N_ELEMENTS(cover_display); i++)
2564 cover_display[i] = _(cover_display[i]);
2566 option = gtk_printer_option_new ("gtk-cover-before", "Before", GTK_PRINTER_OPTION_TYPE_PICKONE);
2567 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (cover),
2568 cover, cover_display);
2569 gtk_printer_option_set (option, "none");
2570 set_option_from_settings (option, settings);
2571 gtk_printer_option_set_add (set, option);
2572 g_object_unref (option);
2574 option = gtk_printer_option_new ("gtk-cover-after", "After", GTK_PRINTER_OPTION_TYPE_PICKONE);
2575 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (cover),
2576 cover, cover_display);
2577 gtk_printer_option_set (option, "none");
2578 set_option_from_settings (option, settings);
2579 gtk_printer_option_set_add (set, option);
2580 g_object_unref (option);
2582 option = gtk_printer_option_new ("gtk-print-time", "Print at", GTK_PRINTER_OPTION_TYPE_PICKONE);
2583 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
2584 print_at, print_at);
2585 gtk_printer_option_set (option, "now");
2586 set_option_from_settings (option, settings);
2587 gtk_printer_option_set_add (set, option);
2588 g_object_unref (option);
2590 option = gtk_printer_option_new ("gtk-print-time-text", "Print at time", GTK_PRINTER_OPTION_TYPE_STRING);
2591 gtk_printer_option_set (option, "");
2592 set_option_from_settings (option, settings);
2593 gtk_printer_option_set_add (set, option);
2594 g_object_unref (option);
2596 /* Printer (ppd) specific settings */
2597 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2600 GtkPaperSize *paper_size;
2601 ppd_option_t *option;
2602 const gchar *ppd_name;
2604 ppdMarkDefaults (ppd_file);
2606 paper_size = gtk_page_setup_get_paper_size (page_setup);
2608 option = ppdFindOption (ppd_file, "PageSize");
2609 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
2612 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
2616 char width[G_ASCII_DTOSTR_BUF_SIZE];
2617 char height[G_ASCII_DTOSTR_BUF_SIZE];
2619 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
2620 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
2621 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
2622 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
2623 g_free (custom_name);
2626 for (i = 0; i < ppd_file->num_groups; i++)
2627 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
2630 /* Now honor the user set defaults for this printer */
2631 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
2633 for (i = 0; i < num_opts; i++)
2635 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
2638 name = get_option_name (opts[i].name);
2639 option = gtk_printer_option_set_lookup (set, name);
2641 gtk_printer_option_set (option, opts[i].value);
2645 cupsFreeOptions (num_opts, opts);
2652 mark_option_from_set (GtkPrinterOptionSet *set,
2653 ppd_file_t *ppd_file,
2654 ppd_option_t *ppd_option)
2656 GtkPrinterOption *option;
2657 char *name = get_option_name (ppd_option->keyword);
2659 option = gtk_printer_option_set_lookup (set, name);
2662 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
2669 mark_group_from_set (GtkPrinterOptionSet *set,
2670 ppd_file_t *ppd_file,
2675 for (i = 0; i < group->num_options; i++)
2676 mark_option_from_set (set, ppd_file, &group->options[i]);
2678 for (i = 0; i < group->num_subgroups; i++)
2679 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
2683 set_conflicts_from_option (GtkPrinterOptionSet *set,
2684 ppd_file_t *ppd_file,
2685 ppd_option_t *ppd_option)
2687 GtkPrinterOption *option;
2690 if (ppd_option->conflicted)
2692 name = get_option_name (ppd_option->keyword);
2693 option = gtk_printer_option_set_lookup (set, name);
2696 gtk_printer_option_set_has_conflict (option, TRUE);
2697 #ifdef PRINT_IGNORED_OPTIONS
2699 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
2707 set_conflicts_from_group (GtkPrinterOptionSet *set,
2708 ppd_file_t *ppd_file,
2713 for (i = 0; i < group->num_options; i++)
2714 set_conflicts_from_option (set, ppd_file, &group->options[i]);
2716 for (i = 0; i < group->num_subgroups; i++)
2717 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
2721 cups_printer_mark_conflicts (GtkPrinter *printer,
2722 GtkPrinterOptionSet *options)
2724 ppd_file_t *ppd_file;
2728 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2730 if (ppd_file == NULL)
2733 ppdMarkDefaults (ppd_file);
2735 for (i = 0; i < ppd_file->num_groups; i++)
2736 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
2738 num_conflicts = ppdConflicts (ppd_file);
2740 if (num_conflicts > 0)
2742 for (i = 0; i < ppd_file->num_groups; i++)
2743 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
2746 return num_conflicts > 0;
2750 GtkPrinter *printer;
2751 GtkPrinterOptionSet *options;
2752 GtkPrintSettings *settings;
2753 ppd_file_t *ppd_file;
2758 const char *standard;
2762 map_settings_to_option (GtkPrinterOption *option,
2763 const NameMapping table[],
2765 GtkPrintSettings *settings,
2766 const gchar *standard_name,
2767 const gchar *cups_name)
2771 const char *cups_value;
2772 const char *standard_value;
2774 /* If the cups-specific setting is set, always use that */
2775 name = g_strdup_printf ("cups-%s", cups_name);
2776 cups_value = gtk_print_settings_get (settings, name);
2779 if (cups_value != NULL)
2781 gtk_printer_option_set (option, cups_value);
2785 /* Otherwise we try to convert from the general setting */
2786 standard_value = gtk_print_settings_get (settings, standard_name);
2787 if (standard_value == NULL)
2790 for (i = 0; i < n_elements; i++)
2792 if (table[i].cups == NULL && table[i].standard == NULL)
2794 gtk_printer_option_set (option, standard_value);
2797 else if (table[i].cups == NULL &&
2798 strcmp (table[i].standard, standard_value) == 0)
2800 set_option_off (option);
2803 else if (strcmp (table[i].standard, standard_value) == 0)
2805 gtk_printer_option_set (option, table[i].cups);
2812 map_option_to_settings (const gchar *value,
2813 const NameMapping table[],
2815 GtkPrintSettings *settings,
2816 const gchar *standard_name,
2817 const gchar *cups_name)
2822 for (i = 0; i < n_elements; i++)
2824 if (table[i].cups == NULL && table[i].standard == NULL)
2826 gtk_print_settings_set (settings,
2831 else if (table[i].cups == NULL && table[i].standard != NULL)
2833 if (value_is_off (value))
2835 gtk_print_settings_set (settings,
2841 else if (strcmp (table[i].cups, value) == 0)
2843 gtk_print_settings_set (settings,
2850 /* Always set the corresponding cups-specific setting */
2851 name = g_strdup_printf ("cups-%s", cups_name);
2852 gtk_print_settings_set (settings, name, value);
2857 static const NameMapping paper_source_map[] = {
2858 { "Lower", "lower"},
2859 { "Middle", "middle"},
2860 { "Upper", "upper"},
2862 { "Envelope", "envelope"},
2863 { "Cassette", "cassette"},
2864 { "LargeCapacity", "large-capacity"},
2865 { "AnySmallFormat", "small-format"},
2866 { "AnyLargeFormat", "large-format"},
2870 static const NameMapping output_tray_map[] = {
2871 { "Upper", "upper"},
2872 { "Lower", "lower"},
2877 static const NameMapping duplex_map[] = {
2878 { "DuplexTumble", "vertical" },
2879 { "DuplexNoTumble", "horizontal" },
2883 static const NameMapping output_mode_map[] = {
2884 { "Standard", "normal" },
2885 { "Normal", "normal" },
2886 { "Draft", "draft" },
2887 { "Fast", "draft" },
2890 static const NameMapping media_type_map[] = {
2891 { "Transparency", "transparency"},
2892 { "Standard", "stationery"},
2896 static const NameMapping all_map[] = {
2902 set_option_from_settings (GtkPrinterOption *option,
2903 GtkPrintSettings *settings)
2905 const char *cups_value;
2908 if (settings == NULL)
2911 if (strcmp (option->name, "gtk-paper-source") == 0)
2912 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
2913 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
2914 else if (strcmp (option->name, "gtk-output-tray") == 0)
2915 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
2916 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
2917 else if (strcmp (option->name, "gtk-duplex") == 0)
2918 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
2919 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
2920 else if (strcmp (option->name, "cups-OutputMode") == 0)
2921 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
2922 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
2923 else if (strcmp (option->name, "cups-Resolution") == 0)
2925 cups_value = gtk_print_settings_get (settings, option->name);
2927 gtk_printer_option_set (option, cups_value);
2930 int res = gtk_print_settings_get_resolution (settings);
2933 value = g_strdup_printf ("%ddpi", res);
2934 gtk_printer_option_set (option, value);
2939 else if (strcmp (option->name, "gtk-paper-type") == 0)
2940 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
2941 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
2942 else if (strcmp (option->name, "gtk-n-up") == 0)
2944 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
2945 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
2947 else if (strcmp (option->name, "gtk-billing-info") == 0)
2949 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
2951 gtk_printer_option_set (option, cups_value);
2953 else if (strcmp (option->name, "gtk-job-prio") == 0)
2955 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
2957 gtk_printer_option_set (option, cups_value);
2959 else if (strcmp (option->name, "gtk-cover-before") == 0)
2961 cups_value = gtk_print_settings_get (settings, "cover-before");
2963 gtk_printer_option_set (option, cups_value);
2965 else if (strcmp (option->name, "gtk-cover-after") == 0)
2967 cups_value = gtk_print_settings_get (settings, "cover-after");
2969 gtk_printer_option_set (option, cups_value);
2971 else if (strcmp (option->name, "gtk-print-time") == 0)
2973 cups_value = gtk_print_settings_get (settings, "print-at");
2975 gtk_printer_option_set (option, cups_value);
2977 else if (strcmp (option->name, "gtk-print-time-text") == 0)
2979 cups_value = gtk_print_settings_get (settings, "print-at-time");
2981 gtk_printer_option_set (option, cups_value);
2983 else if (g_str_has_prefix (option->name, "cups-"))
2985 cups_value = gtk_print_settings_get (settings, option->name);
2987 gtk_printer_option_set (option, cups_value);
2992 foreach_option_get_settings (GtkPrinterOption *option,
2995 struct OptionData *data = user_data;
2996 GtkPrintSettings *settings = data->settings;
2999 value = option->value;
3001 if (strcmp (option->name, "gtk-paper-source") == 0)
3002 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
3003 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3004 else if (strcmp (option->name, "gtk-output-tray") == 0)
3005 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
3006 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3007 else if (strcmp (option->name, "gtk-duplex") == 0)
3008 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
3009 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3010 else if (strcmp (option->name, "cups-OutputMode") == 0)
3011 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
3012 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3013 else if (strcmp (option->name, "cups-Resolution") == 0)
3015 int res = atoi (value);
3016 /* TODO: What if resolution is on XXXxYYYdpi form? */
3018 gtk_print_settings_set_resolution (settings, res);
3019 gtk_print_settings_set (settings, option->name, value);
3021 else if (strcmp (option->name, "gtk-paper-type") == 0)
3022 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
3023 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3024 else if (strcmp (option->name, "gtk-n-up") == 0)
3025 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3026 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3027 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
3028 gtk_print_settings_set (settings, "cups-job-billing", value);
3029 else if (strcmp (option->name, "gtk-job-prio") == 0)
3030 gtk_print_settings_set (settings, "cups-job-priority", value);
3031 else if (strcmp (option->name, "gtk-cover-before") == 0)
3032 gtk_print_settings_set (settings, "cover-before", value);
3033 else if (strcmp (option->name, "gtk-cover-after") == 0)
3034 gtk_print_settings_set (settings, "cover-after", value);
3035 else if (strcmp (option->name, "gtk-print-time") == 0)
3036 gtk_print_settings_set (settings, "print-at", value);
3037 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3038 gtk_print_settings_set (settings, "print-at-time", value);
3039 else if (g_str_has_prefix (option->name, "cups-"))
3040 gtk_print_settings_set (settings, option->name, value);
3044 cups_printer_get_settings_from_options (GtkPrinter *printer,
3045 GtkPrinterOptionSet *options,
3046 GtkPrintSettings *settings)
3048 struct OptionData data;
3049 const char *print_at, *print_at_time;
3051 data.printer = printer;
3052 data.options = options;
3053 data.settings = settings;
3054 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3056 if (data.ppd_file != NULL)
3058 GtkPrinterOption *cover_before, *cover_after;
3060 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
3062 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
3063 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
3064 if (cover_before && cover_after)
3066 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
3067 gtk_print_settings_set (settings, "cups-job-sheets", value);
3071 print_at = gtk_print_settings_get (settings, "print-at");
3072 print_at_time = gtk_print_settings_get (settings, "print-at-time");
3073 if (strcmp (print_at, "at") == 0)
3074 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
3075 else if (strcmp (print_at, "on-hold") == 0)
3076 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
3081 cups_printer_prepare_for_print (GtkPrinter *printer,
3082 GtkPrintJob *print_job,
3083 GtkPrintSettings *settings,
3084 GtkPageSetup *page_setup)
3086 GtkPageSet page_set;
3087 GtkPaperSize *paper_size;
3088 const char *ppd_paper_name;
3091 print_job->print_pages = gtk_print_settings_get_print_pages (settings);
3092 print_job->page_ranges = NULL;
3093 print_job->num_page_ranges = 0;
3095 if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
3096 print_job->page_ranges =
3097 gtk_print_settings_get_page_ranges (settings,
3098 &print_job->num_page_ranges);
3100 if (gtk_print_settings_get_collate (settings))
3101 gtk_print_settings_set (settings, "cups-Collate", "True");
3102 print_job->collate = FALSE;
3104 if (gtk_print_settings_get_reverse (settings))
3105 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
3106 print_job->reverse = FALSE;
3108 if (gtk_print_settings_get_n_copies (settings) > 1)
3109 gtk_print_settings_set_int (settings, "cups-copies",
3110 gtk_print_settings_get_n_copies (settings));
3111 print_job->num_copies = 1;
3113 scale = gtk_print_settings_get_scale (settings);
3114 print_job->scale = 1.0;
3116 print_job->scale = scale/100.0;
3118 page_set = gtk_print_settings_get_page_set (settings);
3119 if (page_set == GTK_PAGE_SET_EVEN)
3120 gtk_print_settings_set (settings, "cups-page-set", "even");
3121 else if (page_set == GTK_PAGE_SET_ODD)
3122 gtk_print_settings_set (settings, "cups-page-set", "odd");
3123 print_job->page_set = GTK_PAGE_SET_ALL;
3125 paper_size = gtk_page_setup_get_paper_size (page_setup);
3126 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
3127 if (ppd_paper_name != NULL)
3128 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
3131 char width[G_ASCII_DTOSTR_BUF_SIZE];
3132 char height[G_ASCII_DTOSTR_BUF_SIZE];
3135 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3136 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3137 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
3138 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
3139 g_free (custom_name);
3142 print_job->rotate_to_orientation = TRUE;
3145 static GtkPageSetup *
3146 create_page_setup (ppd_file_t *ppd_file,
3150 GtkPageSetup *page_setup;
3151 GtkPaperSize *paper_size;
3152 ppd_option_t *option;
3153 ppd_choice_t *choice;
3155 display_name = NULL;
3156 option = ppdFindOption (ppd_file, "PageSize");
3159 choice = ppdFindChoice (option, size->name);
3161 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
3164 if (display_name == NULL)
3165 display_name = g_strdup (size->name);
3167 page_setup = gtk_page_setup_new ();
3168 paper_size = gtk_paper_size_new_from_ppd (size->name,
3172 gtk_page_setup_set_paper_size (page_setup, paper_size);
3173 gtk_paper_size_free (paper_size);
3175 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
3176 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
3177 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
3178 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
3180 g_free (display_name);
3186 cups_printer_list_papers (GtkPrinter *printer)
3188 ppd_file_t *ppd_file;
3190 GtkPageSetup *page_setup;
3194 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3195 if (ppd_file == NULL)
3200 for (i = 0; i < ppd_file->num_sizes; i++)
3202 size = &ppd_file->sizes[i];
3204 page_setup = create_page_setup (ppd_file, size);
3206 l = g_list_prepend (l, page_setup);
3209 return g_list_reverse (l);
3212 static GtkPageSetup *
3213 cups_printer_get_default_page_size (GtkPrinter *printer)
3215 ppd_file_t *ppd_file;
3217 ppd_option_t *option;
3220 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3221 if (ppd_file == NULL)
3224 option = ppdFindOption (ppd_file, "PageSize");
3225 size = ppdPageSize (ppd_file, option->defchoice);
3227 return create_page_setup (ppd_file, size);
3231 cups_printer_get_hard_margins (GtkPrinter *printer,
3237 ppd_file_t *ppd_file;
3239 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3240 if (ppd_file == NULL)
3243 *left = ppd_file->custom_margins[0];
3244 *bottom = ppd_file->custom_margins[1];
3245 *right = ppd_file->custom_margins[2];
3246 *top = ppd_file->custom_margins[3];
3249 static GtkPrintCapabilities
3250 cups_printer_get_capabilities (GtkPrinter *printer)
3253 GTK_PRINT_CAPABILITY_COPIES |
3254 GTK_PRINT_CAPABILITY_COLLATE |
3255 GTK_PRINT_CAPABILITY_REVERSE |
3256 GTK_PRINT_CAPABILITY_NUMBER_UP;