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 char *default_cover_before;
112 char *default_cover_after;
113 int number_of_covers;
116 static GObjectClass *backend_parent_class;
118 static void gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class);
119 static void gtk_print_backend_cups_init (GtkPrintBackendCups *impl);
120 static void gtk_print_backend_cups_finalize (GObject *object);
121 static void gtk_print_backend_cups_dispose (GObject *object);
122 static void cups_get_printer_list (GtkPrintBackend *print_backend);
123 static void cups_request_execute (GtkPrintBackendCups *print_backend,
124 GtkCupsRequest *request,
125 GtkPrintCupsResponseCallbackFunc callback,
127 GDestroyNotify notify);
128 static void cups_printer_get_settings_from_options (GtkPrinter *printer,
129 GtkPrinterOptionSet *options,
130 GtkPrintSettings *settings);
131 static gboolean cups_printer_mark_conflicts (GtkPrinter *printer,
132 GtkPrinterOptionSet *options);
133 static GtkPrinterOptionSet *cups_printer_get_options (GtkPrinter *printer,
134 GtkPrintSettings *settings,
135 GtkPageSetup *page_setup,
136 GtkPrintCapabilities capabilities);
137 static void cups_printer_prepare_for_print (GtkPrinter *printer,
138 GtkPrintJob *print_job,
139 GtkPrintSettings *settings,
140 GtkPageSetup *page_setup);
141 static GList * cups_printer_list_papers (GtkPrinter *printer);
142 static GtkPageSetup * cups_printer_get_default_page_size (GtkPrinter *printer);
143 static void cups_printer_request_details (GtkPrinter *printer);
144 static void cups_request_default_printer (GtkPrintBackendCups *print_backend);
145 static void cups_request_ppd (GtkPrinter *printer);
146 static void cups_printer_get_hard_margins (GtkPrinter *printer,
151 static GtkPrintCapabilities cups_printer_get_capabilities (GtkPrinter *printer);
152 static void set_option_from_settings (GtkPrinterOption *option,
153 GtkPrintSettings *setting);
154 static void cups_begin_polling_info (GtkPrintBackendCups *print_backend,
157 static gboolean cups_job_info_poll_timeout (gpointer user_data);
158 static void gtk_print_backend_cups_print_stream (GtkPrintBackend *backend,
161 GtkPrintJobCompleteFunc callback,
163 GDestroyNotify dnotify);
164 static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter *printer,
165 GtkPrintSettings *settings,
168 GIOChannel *cache_io);
172 gtk_print_backend_cups_register_type (GTypeModule *module)
174 static const GTypeInfo print_backend_cups_info =
176 sizeof (GtkPrintBackendCupsClass),
177 NULL, /* base_init */
178 NULL, /* base_finalize */
179 (GClassInitFunc) gtk_print_backend_cups_class_init,
180 NULL, /* class_finalize */
181 NULL, /* class_data */
182 sizeof (GtkPrintBackendCups),
184 (GInstanceInitFunc) gtk_print_backend_cups_init
187 print_backend_cups_type = g_type_module_register_type (module,
188 GTK_TYPE_PRINT_BACKEND,
189 "GtkPrintBackendCups",
190 &print_backend_cups_info, 0);
194 pb_module_init (GTypeModule *module)
197 g_print ("CUPS Backend: Initializing the CUPS print backend module\n"));
199 gtk_print_backend_cups_register_type (module);
200 gtk_printer_cups_register_type (module);
204 pb_module_exit (void)
209 G_MODULE_EXPORT GtkPrintBackend *
210 pb_module_create (void)
212 return gtk_print_backend_cups_new ();
216 * GtkPrintBackendCups
219 gtk_print_backend_cups_get_type (void)
221 return print_backend_cups_type;
225 * gtk_print_backend_cups_new:
227 * Creates a new #GtkPrintBackendCups object. #GtkPrintBackendCups
228 * implements the #GtkPrintBackend interface with direct access to
229 * the filesystem using Unix/Linux API calls
231 * Return value: the new #GtkPrintBackendCups object
234 gtk_print_backend_cups_new (void)
237 g_print ("CUPS Backend: Creating a new CUPS print backend object\n"));
239 return g_object_new (GTK_TYPE_PRINT_BACKEND_CUPS, NULL);
243 gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
245 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
246 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
248 backend_parent_class = g_type_class_peek_parent (class);
250 gobject_class->finalize = gtk_print_backend_cups_finalize;
251 gobject_class->dispose = gtk_print_backend_cups_dispose;
253 backend_class->request_printer_list = cups_get_printer_list;
254 backend_class->print_stream = gtk_print_backend_cups_print_stream;
255 backend_class->printer_request_details = cups_printer_request_details;
256 backend_class->printer_create_cairo_surface = cups_printer_create_cairo_surface;
257 backend_class->printer_get_options = cups_printer_get_options;
258 backend_class->printer_mark_conflicts = cups_printer_mark_conflicts;
259 backend_class->printer_get_settings_from_options = cups_printer_get_settings_from_options;
260 backend_class->printer_prepare_for_print = cups_printer_prepare_for_print;
261 backend_class->printer_list_papers = cups_printer_list_papers;
262 backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
263 backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
264 backend_class->printer_get_capabilities = cups_printer_get_capabilities;
267 static cairo_status_t
268 _cairo_write_to_cups (void *closure,
269 const unsigned char *data,
272 GIOChannel *io = (GIOChannel *)closure;
279 g_print ("CUPS Backend: Writing %i byte chunk to temp file\n", length));
283 g_io_channel_write_chars (io, (gchar *)data, length, &written, &error);
288 g_print ("CUPS Backend: Error writing to temp file, %s\n",
291 g_error_free (error);
292 return CAIRO_STATUS_WRITE_ERROR;
296 g_print ("CUPS Backend: Wrote %i bytes to temp file\n", written));
302 return CAIRO_STATUS_SUCCESS;
305 static cairo_surface_t *
306 cups_printer_create_cairo_surface (GtkPrinter *printer,
307 GtkPrintSettings *settings,
310 GIOChannel *cache_io)
312 cairo_surface_t *surface;
313 ppd_file_t *ppd_file = NULL;
314 ppd_attr_t *ppd_attr = NULL;
317 /* TODO: check if it is a ps or pdf printer */
319 surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
321 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
323 if (ppd_file != NULL)
325 ppd_attr = ppdFindAttr (ppd_file, "LanguageLevel", NULL);
327 if (ppd_attr != NULL)
328 level = atoi (ppd_attr->value);
332 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
335 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
337 /* TODO: DPI from settings object? */
338 cairo_surface_set_fallback_resolution (surface, 300, 300);
344 GtkPrintJobCompleteFunc callback;
347 GDestroyNotify dnotify;
348 } CupsPrintStreamData;
351 cups_free_print_stream_data (CupsPrintStreamData *data)
354 g_print ("CUPS Backend: %s\n", G_STRFUNC));
357 data->dnotify (data->user_data);
358 g_object_unref (data->job);
363 cups_print_cb (GtkPrintBackendCups *print_backend,
364 GtkCupsResult *result,
367 GError *error = NULL;
368 CupsPrintStreamData *ps = user_data;
370 GDK_THREADS_ENTER ();
373 g_print ("CUPS Backend: %s\n", G_STRFUNC));
375 if (gtk_cups_result_is_error (result))
376 error = g_error_new_literal (gtk_print_error_quark (),
377 GTK_PRINT_ERROR_INTERNAL_ERROR,
378 gtk_cups_result_get_error_string (result));
381 ps->callback (ps->job, ps->user_data, error);
386 ipp_attribute_t *attr; /* IPP job-id attribute */
387 ipp_t *response = gtk_cups_result_get_response (result);
389 if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
390 job_id = attr->values[0].integer;
392 if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
393 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
396 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_PENDING);
397 cups_begin_polling_info (print_backend, ps->job, job_id);
401 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED_ABORTED);
405 g_error_free (error);
407 GDK_THREADS_LEAVE ();
411 add_cups_options (const gchar *key,
415 GtkCupsRequest *request = user_data;
417 if (!g_str_has_prefix (key, "cups-"))
420 if (strcmp (value, "gtk-ignore-value") == 0)
423 key = key + strlen ("cups-");
425 gtk_cups_request_encode_option (request, key, value);
429 gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
432 GtkPrintJobCompleteFunc callback,
434 GDestroyNotify dnotify)
436 GtkPrinterCups *cups_printer;
437 CupsPrintStreamData *ps;
438 GtkCupsRequest *request;
439 GtkPrintSettings *settings;
441 char printer_absolute_uri[HTTP_MAX_URI];
444 g_print ("CUPS Backend: %s\n", G_STRFUNC));
446 cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
447 settings = gtk_print_job_get_settings (job);
449 request = gtk_cups_request_new (NULL,
454 cups_printer->device_uri);
456 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
457 httpAssembleURIf (HTTP_URI_CODING_ALL,
458 printer_absolute_uri,
459 sizeof (printer_absolute_uri),
465 gtk_printer_get_name (gtk_print_job_get_printer (job)));
467 g_snprintf (printer_absolute_uri,
468 sizeof (printer_absolute_uri),
469 "ipp://localhost:%d/printers/%s",
471 gtk_printer_get_name (gtk_print_job_get_printer (job)));
474 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
475 IPP_TAG_URI, "printer-uri",
476 NULL, printer_absolute_uri);
478 title = gtk_print_job_get_title (job);
480 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
481 IPP_TAG_NAME, "job-name",
484 gtk_print_settings_foreach (settings, add_cups_options, request);
486 ps = g_new0 (CupsPrintStreamData, 1);
487 ps->callback = callback;
488 ps->user_data = user_data;
489 ps->dnotify = dnotify;
490 ps->job = g_object_ref (job);
492 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
494 (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
496 (GDestroyNotify)cups_free_print_stream_data);
501 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
503 backend_cups->list_printers_poll = FALSE;
504 backend_cups->got_default_printer = FALSE;
505 backend_cups->list_printers_pending = FALSE;
507 backend_cups->covers = NULL;
508 backend_cups->default_cover_before = NULL;
509 backend_cups->default_cover_after = NULL;
510 backend_cups->number_of_covers = 0;
512 cups_request_default_printer (backend_cups);
516 gtk_print_backend_cups_finalize (GObject *object)
518 GtkPrintBackendCups *backend_cups;
521 g_print ("CUPS Backend: finalizing CUPS backend module\n"));
523 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
525 g_free (backend_cups->default_printer);
526 backend_cups->default_printer = NULL;
528 g_strfreev (backend_cups->covers);
529 backend_cups->number_of_covers = 0;
531 g_free (backend_cups->default_cover_before);
532 g_free (backend_cups->default_cover_after);
534 backend_parent_class->finalize (object);
538 gtk_print_backend_cups_dispose (GObject *object)
540 GtkPrintBackendCups *backend_cups;
543 g_print ("CUPS Backend: %s\n", G_STRFUNC));
545 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
547 if (backend_cups->list_printers_poll > 0)
548 g_source_remove (backend_cups->list_printers_poll);
549 backend_cups->list_printers_poll = 0;
551 backend_parent_class->dispose (object);
556 cups_dispatch_watch_check (GSource *source)
558 GtkPrintCupsDispatchWatch *dispatch;
559 GtkCupsPollState poll_state;
563 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
565 dispatch = (GtkPrintCupsDispatchWatch *) source;
567 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
569 if (dispatch->request->http != NULL)
571 if (dispatch->data_poll == NULL)
573 dispatch->data_poll = g_new0 (GPollFD, 1);
574 g_source_add_poll (source, dispatch->data_poll);
578 if (poll_state == GTK_CUPS_HTTP_READ)
579 dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
580 else if (poll_state == GTK_CUPS_HTTP_WRITE)
581 dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
583 dispatch->data_poll->events = 0;
586 #ifdef HAVE_CUPS_API_1_2
587 dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
589 dispatch->data_poll->fd = dispatch->request->http->fd;
593 if (poll_state != GTK_CUPS_HTTP_IDLE)
594 if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
597 result = gtk_cups_request_read_write (dispatch->request);
598 if (result && dispatch->data_poll != NULL)
600 g_source_remove_poll (source, dispatch->data_poll);
601 g_free (dispatch->data_poll);
602 dispatch->data_poll = NULL;
609 cups_dispatch_watch_prepare (GSource *source,
612 GtkPrintCupsDispatchWatch *dispatch;
614 dispatch = (GtkPrintCupsDispatchWatch *) source;
617 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
621 return gtk_cups_request_read_write (dispatch->request);
625 cups_dispatch_watch_dispatch (GSource *source,
626 GSourceFunc callback,
629 GtkPrintCupsDispatchWatch *dispatch;
630 GtkPrintCupsResponseCallbackFunc ep_callback;
631 GtkCupsResult *result;
633 g_assert (callback != NULL);
635 ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
637 dispatch = (GtkPrintCupsDispatchWatch *) source;
639 result = gtk_cups_request_get_result (dispatch->request);
642 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
644 if (gtk_cups_result_is_error (result))
647 g_print("Error result: %s (type %i, status %i, code %i)\n",
648 gtk_cups_result_get_error_string (result),
649 gtk_cups_result_get_error_type (result),
650 gtk_cups_result_get_error_status (result),
651 gtk_cups_result_get_error_code (result)));
654 ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
660 cups_dispatch_watch_finalize (GSource *source)
662 GtkPrintCupsDispatchWatch *dispatch;
665 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
667 dispatch = (GtkPrintCupsDispatchWatch *) source;
669 gtk_cups_request_free (dispatch->request);
671 if (dispatch->backend)
673 /* We need to unref this at idle time, because it might be the
674 * last reference to this module causing the code to be
675 * unloaded (including this particular function!)
676 * Update: Doing this at idle caused a deadlock taking the
677 * mainloop context lock while being in a GSource callout for
678 * multithreaded apps. So, for now we just disable unloading
679 * of print backends. See _gtk_print_backend_create for the
682 g_object_unref (dispatch->backend);
683 dispatch->backend = NULL;
686 g_free (dispatch->data_poll);
689 static GSourceFuncs _cups_dispatch_watch_funcs = {
690 cups_dispatch_watch_prepare,
691 cups_dispatch_watch_check,
692 cups_dispatch_watch_dispatch,
693 cups_dispatch_watch_finalize
698 cups_request_execute (GtkPrintBackendCups *print_backend,
699 GtkCupsRequest *request,
700 GtkPrintCupsResponseCallbackFunc callback,
702 GDestroyNotify notify)
704 GtkPrintCupsDispatchWatch *dispatch;
706 dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
707 sizeof (GtkPrintCupsDispatchWatch));
710 g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
712 dispatch->request = request;
713 dispatch->backend = g_object_ref (print_backend);
714 dispatch->data_poll = NULL;
716 g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
718 g_source_attach ((GSource *) dispatch, NULL);
719 g_source_unref ((GSource *) dispatch);
724 cups_request_printer_info_cb (GtkPrintBackendCups *backend,
725 GtkCupsResult *result,
728 ipp_attribute_t *attr;
731 GtkPrinterCups *cups_printer;
737 gboolean status_changed;
739 g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
741 printer_name = (gchar *)user_data;
742 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
746 g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
751 g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
755 cups_printer = GTK_PRINTER_CUPS (printer);
757 if (gtk_cups_result_is_error (result))
759 if (gtk_printer_is_new (printer))
761 gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
766 return; /* TODO: mark as inactive printer */
769 response = gtk_cups_result_get_response (result);
771 /* TODO: determine printer type and use correct icon */
772 gtk_printer_set_icon_name (printer, "gtk-print");
778 for (attr = response->attrs; attr != NULL; attr = attr->next)
783 _CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
784 _CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
785 _CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
786 _CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
787 _CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
790 status_changed = gtk_printer_set_job_count (printer, job_count);
792 status_changed |= gtk_printer_set_location (printer, loc);
793 status_changed |= gtk_printer_set_description (printer, desc);
794 status_changed |= gtk_printer_set_state_message (printer, state_msg);
797 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
798 "printer-status-changed", printer);
802 cups_request_printer_info (GtkPrintBackendCups *print_backend,
803 const gchar *printer_name)
805 GtkCupsRequest *request;
807 static const char * const pattrs[] = /* Attributes we're interested in */
811 "printer-state-message",
814 "job-sheets-supported",
818 request = gtk_cups_request_new (NULL,
820 IPP_GET_PRINTER_ATTRIBUTES,
825 printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
827 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
828 "printer-uri", NULL, printer_uri);
831 g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
833 g_free (printer_uri);
835 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
836 "requested-attributes", G_N_ELEMENTS (pattrs),
839 cups_request_execute (print_backend,
841 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
842 g_strdup (printer_name),
843 (GDestroyNotify) g_free);
848 GtkPrintBackendCups *print_backend;
855 job_object_died (gpointer user_data,
856 GObject *where_the_object_was)
858 CupsJobPollData *data = user_data;
863 cups_job_poll_data_free (CupsJobPollData *data)
866 g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
872 cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
873 GtkCupsResult *result,
876 CupsJobPollData *data = user_data;
877 ipp_attribute_t *attr;
882 GDK_THREADS_ENTER ();
884 if (data->job == NULL)
886 cups_job_poll_data_free (data);
892 response = gtk_cups_result_get_response (result);
895 for (attr = response->attrs; attr != NULL; attr = attr->next)
900 _CUPS_MAP_ATTR_INT (attr, state, "job-state");
906 case IPP_JOB_PENDING:
908 case IPP_JOB_STOPPED:
909 gtk_print_job_set_status (data->job,
910 GTK_PRINT_STATUS_PENDING);
912 case IPP_JOB_PROCESSING:
913 gtk_print_job_set_status (data->job,
914 GTK_PRINT_STATUS_PRINTING);
917 case IPP_JOB_CANCELLED:
918 case IPP_JOB_ABORTED:
919 gtk_print_job_set_status (data->job,
920 GTK_PRINT_STATUS_FINISHED_ABORTED);
924 case IPP_JOB_COMPLETED:
925 gtk_print_job_set_status (data->job,
926 GTK_PRINT_STATUS_FINISHED);
931 if (!done && data->job != NULL)
935 if (data->counter < 5)
937 else if (data->counter < 10)
942 g_timeout_add (timeout, cups_job_info_poll_timeout, data);
945 cups_job_poll_data_free (data);
948 GDK_THREADS_LEAVE ();
952 cups_request_job_info (CupsJobPollData *data)
954 GtkCupsRequest *request;
957 request = gtk_cups_request_new (NULL,
959 IPP_GET_JOB_ATTRIBUTES,
964 job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
965 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
966 "job-uri", NULL, job_uri);
969 cups_request_execute (data->print_backend,
971 (GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
977 cups_job_info_poll_timeout (gpointer user_data)
979 CupsJobPollData *data = user_data;
981 if (data->job == NULL)
982 cups_job_poll_data_free (data);
984 cups_request_job_info (data);
990 cups_begin_polling_info (GtkPrintBackendCups *print_backend,
994 CupsJobPollData *data;
996 data = g_new0 (CupsJobPollData, 1);
998 data->print_backend = print_backend;
1000 data->job_id = job_id;
1003 g_object_weak_ref (G_OBJECT (job), job_object_died, data);
1005 cups_request_job_info (data);
1009 mark_printer_inactive (GtkPrinter *printer,
1010 GtkPrintBackend *backend)
1012 gtk_printer_set_is_active (printer, FALSE);
1013 g_signal_emit_by_name (backend, "printer-removed", printer);
1017 find_printer (GtkPrinter *printer,
1018 const gchar *find_name)
1020 const gchar *printer_name;
1022 printer_name = gtk_printer_get_name (printer);
1023 return g_ascii_strcasecmp (printer_name, find_name);
1027 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
1028 GtkCupsResult *result,
1031 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
1032 ipp_attribute_t *attr;
1034 gboolean list_has_changed;
1035 GList *removed_printer_checklist;
1037 GDK_THREADS_ENTER ();
1039 list_has_changed = FALSE;
1042 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1044 cups_backend->list_printers_pending = FALSE;
1046 if (gtk_cups_result_is_error (result))
1049 g_warning ("CUPS Backend: Error getting printer list: %s",
1050 gtk_cups_result_get_error_string (result)));
1055 /* Gather the names of the printers in the current queue
1056 * so we may check to see if they were removed
1058 removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
1060 response = gtk_cups_result_get_response (result);
1062 for (attr = response->attrs; attr != NULL; attr = attr->next)
1064 GtkPrinter *printer;
1065 const gchar *printer_name = NULL;
1066 const gchar *printer_uri = NULL;
1067 const gchar *member_uris = NULL;
1068 const gchar *location = NULL;
1069 const gchar *description = NULL;
1070 const gchar *state_msg = NULL;
1073 gboolean status_changed = FALSE;
1076 const gchar *reason_msg = NULL;
1077 gchar *reason_msg_desc = NULL;
1078 gchar *tmp_msg = NULL;
1079 gchar *tmp_msg2 = NULL;
1080 gint printer_state_reason_level = 0; /* 0 - none, 1 - report, 2 - warning, 3 - error */
1081 gboolean interested_in = FALSE;
1082 gboolean found = FALSE;
1083 static const char * const reasons[] = /* Reasons we're interested in */
1089 "marker-supply-low",
1090 "marker-supply-empty",
1096 "connecting-to-device",
1099 static const char * reasons_descs[] =
1101 N_("Printer '%s' is low on toner."),
1102 N_("Printer '%s' has no toner left."),
1103 N_("Printer '%s' is low on developer."),
1104 N_("Printer '%s' is out of developer."),
1105 N_("Printer '%s' is low on at least one marker supply."),
1106 N_("Printer '%s' is out of at least one marker supply."),
1107 N_("The cover is open on printer '%s'."),
1108 N_("The door is open on printer '%s'."),
1109 N_("Printer '%s' is low on paper."),
1110 N_("Printer '%s' is out of paper."),
1111 N_("Printer '%s' is currently off-line."),
1112 N_("Printer '%s' may not be connected."),
1113 N_("There is a problem on printer '%s'.")
1115 gboolean is_paused = FALSE;
1116 gboolean is_accepting_jobs = TRUE;
1118 /* Skip leading attributes until we hit a printer...
1120 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1126 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1128 if (strcmp (attr->name, "printer-name") == 0 &&
1129 attr->value_tag == IPP_TAG_NAME)
1130 printer_name = attr->values[0].string.text;
1131 else if (strcmp (attr->name, "printer-uri-supported") == 0 &&
1132 attr->value_tag == IPP_TAG_URI)
1133 printer_uri = attr->values[0].string.text;
1134 else if (strcmp (attr->name, "member-uris") == 0 &&
1135 attr->value_tag == IPP_TAG_URI)
1136 member_uris = attr->values[0].string.text;
1137 else if (strcmp (attr->name, "printer-location") == 0)
1138 location = attr->values[0].string.text;
1139 else if (strcmp (attr->name, "printer-info") == 0)
1140 description = attr->values[0].string.text;
1141 else if (strcmp (attr->name, "printer-state-message") == 0)
1142 state_msg = attr->values[0].string.text;
1143 else if (strcmp (attr->name, "printer-state-reasons") == 0)
1144 /* Store most important reason to reason_msg and set
1145 its importance at printer_state_reason_level */
1147 for (i = 0; i < attr->num_values; i++)
1149 if (strcmp (attr->values[i].string.text, "none") != 0)
1151 /* Sets is_paused flag for paused printer. */
1152 if (strcmp (attr->values[i].string.text, "paused") == 0)
1157 interested_in = FALSE;
1158 for (j = 0; j < G_N_ELEMENTS (reasons); j++)
1159 if (strncmp (attr->values[i].string.text, reasons[j], strlen (reasons[j])) == 0)
1161 interested_in = TRUE;
1167 if (g_str_has_suffix (attr->values[i].string.text, "-report"))
1169 if (printer_state_reason_level <= 1)
1171 reason_msg = attr->values[i].string.text;
1172 printer_state_reason_level = 1;
1175 else if (g_str_has_suffix (attr->values[i].string.text, "-warning"))
1177 if (printer_state_reason_level <= 2)
1179 reason_msg = attr->values[i].string.text;
1180 printer_state_reason_level = 2;
1183 else /* It is error in the case of no suffix. */
1185 reason_msg = attr->values[i].string.text;
1186 printer_state_reason_level = 3;
1192 else if (strcmp (attr->name, "printer-state") == 0)
1193 state = attr->values[0].integer;
1194 else if (strcmp (attr->name, "queued-job-count") == 0)
1195 job_count = attr->values[0].integer;
1196 else if (strcmp (attr->name, "printer-is-accepting-jobs") == 0)
1198 if (attr->values[0].boolean == 1)
1199 is_accepting_jobs = TRUE;
1201 is_accepting_jobs = FALSE;
1203 else if (strcmp (attr->name, "job-sheets-supported") == 0)
1205 if (cups_backend->covers == NULL)
1207 cups_backend->number_of_covers = attr->num_values;
1208 cups_backend->covers = g_new (char *, cups_backend->number_of_covers + 1);
1210 for (i = 0; i < cups_backend->number_of_covers; i++)
1211 cups_backend->covers[i] = g_strdup (attr->values[i].string.text);
1213 cups_backend->covers[cups_backend->number_of_covers] = NULL;
1216 else if (strcmp (attr->name, "job-sheets-default") == 0)
1218 if ( (cups_backend->default_cover_before == NULL) && (cups_backend->default_cover_after == NULL))
1220 if (attr->num_values == 2)
1222 cups_backend->default_cover_before = g_strdup (attr->values[0].string.text);
1223 cups_backend->default_cover_after = g_strdup (attr->values[1].string.text);
1230 g_print ("CUPS Backend: Attribute %s ignored", attr->name));
1236 if (printer_name == NULL ||
1237 (printer_uri == NULL && member_uris == NULL))
1245 /* remove name from checklist if it was found */
1246 node = g_list_find_custom (removed_printer_checklist, printer_name, (GCompareFunc) find_printer);
1247 removed_printer_checklist = g_list_delete_link (removed_printer_checklist, node);
1249 printer = gtk_print_backend_find_printer (backend, printer_name);
1252 GtkPrinterCups *cups_printer;
1253 char uri[HTTP_MAX_URI]; /* Printer URI */
1254 char method[HTTP_MAX_URI]; /* Method/scheme name */
1255 char username[HTTP_MAX_URI]; /* Username:password */
1256 char hostname[HTTP_MAX_URI]; /* Hostname */
1257 char resource[HTTP_MAX_URI]; /* Resource name */
1258 int port; /* Port number */
1260 list_has_changed = TRUE;
1261 cups_printer = gtk_printer_cups_new (printer_name, backend);
1263 cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
1265 /* Check to see if we are looking at a class */
1268 cups_printer->printer_uri = g_strdup (member_uris);
1269 /* TODO if member_uris is a class we need to recursivly find a printer */
1271 g_print ("CUPS Backend: Found class with printer %s\n", member_uris));
1275 cups_printer->printer_uri = g_strdup (printer_uri);
1277 g_print ("CUPS Backend: Found printer %s\n", printer_uri));
1280 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
1281 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
1282 method, sizeof (method),
1283 username, sizeof (username),
1284 hostname, sizeof (hostname),
1286 resource, sizeof (resource));
1289 httpSeparate (cups_printer->printer_uri,
1297 if (strncmp (resource, "/printers/", 10) == 0)
1299 cups_printer->ppd_name = g_strdup (resource + 10);
1301 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
1304 gethostname (uri, sizeof (uri));
1305 if (strcasecmp (uri, hostname) == 0)
1306 strcpy (hostname, "localhost");
1308 cups_printer->hostname = g_strdup (hostname);
1309 cups_printer->port = port;
1311 printer = GTK_PRINTER (cups_printer);
1313 if (cups_backend->default_printer != NULL &&
1314 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
1315 gtk_printer_set_is_default (printer, TRUE);
1318 gtk_print_backend_add_printer (backend, printer);
1321 g_object_ref (printer);
1323 gtk_printer_set_is_paused (printer, is_paused);
1324 gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
1326 if (!gtk_printer_is_active (printer))
1328 gtk_printer_set_is_active (printer, TRUE);
1329 gtk_printer_set_is_new (printer, TRUE);
1330 list_has_changed = TRUE;
1333 if (gtk_printer_is_new (printer))
1335 g_signal_emit_by_name (backend, "printer-added", printer);
1337 gtk_printer_set_is_new (printer, FALSE);
1341 /* Getting printer info with separate requests overwhelms cups
1342 * when the printer list has more than a handful of printers.
1344 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
1347 GTK_PRINTER_CUPS (printer)->state = state;
1348 status_changed = gtk_printer_set_job_count (printer, job_count);
1349 status_changed |= gtk_printer_set_location (printer, location);
1350 status_changed |= gtk_printer_set_description (printer, description);
1352 if (state_msg != NULL && strlen (state_msg) == 0)
1354 if (is_paused && !is_accepting_jobs)
1355 tmp_msg2 = g_strdup ( N_("Paused ; Rejecting Jobs"));
1356 if (is_paused && is_accepting_jobs)
1357 tmp_msg2 = g_strdup ( N_("Paused"));
1358 if (!is_paused && !is_accepting_jobs)
1359 tmp_msg2 = g_strdup ( N_("Rejecting Jobs"));
1361 if (tmp_msg2 != NULL)
1362 state_msg = tmp_msg2;
1365 /* Set description of the reason and combine it with printer-state-message. */
1366 if ( (reason_msg != NULL))
1368 for (i = 0; i < G_N_ELEMENTS (reasons); i++)
1370 if (strncmp (reason_msg, reasons[i], strlen (reasons[i])) == 0)
1372 reason_msg_desc = g_strdup_printf (reasons_descs[i], printer_name);
1379 printer_state_reason_level = 0;
1381 if (printer_state_reason_level >= 2)
1383 if (strlen (state_msg) == 0)
1384 state_msg = reason_msg_desc;
1387 tmp_msg = g_strjoin (" ; ", state_msg, reason_msg_desc, NULL);
1388 state_msg = tmp_msg;
1393 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1394 status_changed |= gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
1396 if (tmp_msg != NULL)
1399 if (tmp_msg2 != NULL)
1402 if (reason_msg_desc != NULL)
1403 g_free (reason_msg_desc);
1405 /* Set printer icon according to importance
1406 (none, report, warning, error - report is omitted). */
1407 if (printer_state_reason_level == 3)
1408 gtk_printer_set_icon_name (printer, "gtk-print-error");
1409 else if (printer_state_reason_level == 2)
1410 gtk_printer_set_icon_name (printer, "gtk-print-warning");
1411 else if (gtk_printer_is_paused (printer))
1412 gtk_printer_set_icon_name (printer, "gtk-print-paused");
1414 gtk_printer_set_icon_name (printer, "gtk-print");
1417 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1418 "printer-status-changed", printer);
1420 /* The ref is held by GtkPrintBackend, in add_printer() */
1421 g_object_unref (printer);
1427 /* look at the removed printers checklist and mark any printer
1428 as inactive if it is in the list, emitting a printer_removed signal */
1429 if (removed_printer_checklist != NULL)
1431 g_list_foreach (removed_printer_checklist, (GFunc) mark_printer_inactive, backend);
1432 g_list_free (removed_printer_checklist);
1433 list_has_changed = TRUE;
1437 if (list_has_changed)
1438 g_signal_emit_by_name (backend, "printer-list-changed");
1440 gtk_print_backend_set_list_done (backend);
1442 GDK_THREADS_LEAVE ();
1446 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
1448 GtkCupsRequest *request;
1449 static const char * const pattrs[] = /* Attributes we're interested in */
1452 "printer-uri-supported",
1456 "printer-state-message",
1457 "printer-state-reasons",
1460 "printer-is-accepting-jobs",
1461 "job-sheets-supported",
1462 "job-sheets-default"
1465 if (cups_backend->list_printers_pending ||
1466 !cups_backend->got_default_printer)
1469 cups_backend->list_printers_pending = TRUE;
1471 request = gtk_cups_request_new (NULL,
1478 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1479 "requested-attributes", G_N_ELEMENTS (pattrs),
1482 cups_request_execute (cups_backend,
1484 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
1492 cups_get_printer_list (GtkPrintBackend *backend)
1494 GtkPrintBackendCups *cups_backend;
1496 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
1497 if (cups_backend->list_printers_poll == 0)
1499 cups_request_printer_list (cups_backend);
1500 cups_backend->list_printers_poll = gdk_threads_add_timeout (3000,
1501 (GSourceFunc) cups_request_printer_list,
1507 GtkPrinterCups *printer;
1513 get_ppd_data_free (GetPPDData *data)
1516 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1517 httpClose (data->http);
1518 g_io_channel_unref (data->ppd_io);
1519 g_object_unref (data->printer);
1524 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
1525 GtkCupsResult *result,
1529 GtkPrinter *printer;
1531 GDK_THREADS_ENTER ();
1534 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1536 printer = GTK_PRINTER (data->printer);
1537 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
1539 if (gtk_cups_result_is_error (result))
1541 gboolean success = FALSE;
1543 /* if we get a 404 then it is just a raw printer without a ppd
1545 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
1546 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
1548 gtk_printer_set_has_details (printer, TRUE);
1552 g_signal_emit_by_name (printer, "details-acquired", success);
1556 response = gtk_cups_result_get_response (result);
1558 /* let ppdOpenFd take over the ownership of the open file */
1559 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
1560 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
1562 ppdMarkDefaults (data->printer->ppd_file);
1564 gtk_printer_set_has_details (printer, TRUE);
1565 g_signal_emit_by_name (printer, "details-acquired", TRUE);
1568 GDK_THREADS_LEAVE ();
1572 cups_request_ppd (GtkPrinter *printer)
1575 GtkPrintBackend *print_backend;
1576 GtkPrinterCups *cups_printer;
1577 GtkCupsRequest *request;
1584 cups_printer = GTK_PRINTER_CUPS (printer);
1589 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1591 http = httpConnectEncrypt (cups_printer->hostname,
1595 data = g_new0 (GetPPDData, 1);
1597 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
1601 #ifdef G_ENABLE_DEBUG
1602 /* If we are debugging printing don't delete the tmp files */
1603 if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
1604 unlink (ppd_filename);
1606 unlink (ppd_filename);
1607 #endif /* G_ENABLE_DEBUG */
1612 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
1614 g_error_free (error);
1616 g_free (ppd_filename);
1619 g_signal_emit_by_name (printer, "details-acquired", FALSE);
1624 fchmod (fd, S_IRUSR | S_IWUSR);
1625 data->ppd_io = g_io_channel_unix_new (fd);
1626 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
1627 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
1629 data->printer = g_object_ref (printer);
1631 resource = g_strdup_printf ("/printers/%s.ppd",
1632 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
1634 request = gtk_cups_request_new (data->http,
1638 cups_printer->hostname,
1642 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
1645 g_free (ppd_filename);
1647 cups_printer->reading_ppd = TRUE;
1649 print_backend = gtk_printer_get_backend (printer);
1651 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
1653 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
1655 (GDestroyNotify)get_ppd_data_free);
1658 /* Ordering matters for default preference */
1659 static const char *lpoptions_locations[] = {
1660 "/etc/cups/lpoptions",
1666 cups_parse_user_default_printer (const char *filename,
1667 char **printer_name)
1670 char line[1024], *lineptr, *defname = NULL;
1672 if ((fp = g_fopen (filename, "r")) == NULL)
1675 while (fgets (line, sizeof (line), fp) != NULL)
1677 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
1681 while (isspace (*lineptr))
1688 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
1693 if (*printer_name != NULL)
1694 g_free (*printer_name);
1696 *printer_name = g_strdup (defname);
1703 cups_get_user_default_printer (char **printer_name)
1707 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1709 if (g_path_is_absolute (lpoptions_locations[i]))
1711 cups_parse_user_default_printer (lpoptions_locations[i],
1718 filename = g_build_filename (g_get_home_dir (),
1719 lpoptions_locations[i], NULL);
1720 cups_parse_user_default_printer (filename, printer_name);
1727 cups_parse_user_options (const char *filename,
1728 const char *printer_name,
1730 cups_option_t **options)
1733 gchar line[1024], *lineptr, *name;
1735 if ((fp = g_fopen (filename, "r")) == NULL)
1738 while (fgets (line, sizeof (line), fp) != NULL)
1740 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
1742 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
1747 /* Skip leading whitespace */
1748 while (isspace (*lineptr))
1754 /* NUL-terminate the name, stripping the instance name */
1756 while (!isspace (*lineptr) && *lineptr)
1758 if (*lineptr == '/')
1768 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
1771 /* We found our printer, parse the options */
1772 num_options = cupsParseOptions (lineptr, num_options, options);
1781 cups_get_user_options (const char *printer_name,
1783 cups_option_t **options)
1787 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1789 if (g_path_is_absolute (lpoptions_locations[i]))
1791 num_options = cups_parse_user_options (lpoptions_locations[i],
1800 filename = g_build_filename (g_get_home_dir (),
1801 lpoptions_locations[i], NULL);
1802 num_options = cups_parse_user_options (filename, printer_name,
1803 num_options, options);
1812 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
1813 GtkCupsResult *result,
1817 ipp_attribute_t *attr;
1819 response = gtk_cups_result_get_response (result);
1821 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
1822 print_backend->default_printer = g_strdup (attr->values[0].string.text);
1824 print_backend->got_default_printer = TRUE;
1826 /* Make sure to kick off get_printers if we are polling it,
1827 * as we could have blocked this reading the default printer
1829 if (print_backend->list_printers_poll != 0)
1830 cups_request_printer_list (print_backend);
1834 cups_request_default_printer (GtkPrintBackendCups *print_backend)
1836 GtkCupsRequest *request;
1840 if ((str = g_getenv ("LPDEST")) != NULL)
1842 print_backend->default_printer = g_strdup (str);
1843 print_backend->got_default_printer = TRUE;
1846 else if ((str = g_getenv ("PRINTER")) != NULL &&
1847 strcmp (str, "lp") != 0)
1849 print_backend->default_printer = g_strdup (str);
1850 print_backend->got_default_printer = TRUE;
1854 /* Figure out user setting for default printer */
1855 cups_get_user_default_printer (&name);
1858 print_backend->default_printer = name;
1859 print_backend->got_default_printer = TRUE;
1863 request = gtk_cups_request_new (NULL,
1870 cups_request_execute (print_backend,
1872 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
1873 g_object_ref (print_backend),
1878 cups_printer_request_details (GtkPrinter *printer)
1880 GtkPrinterCups *cups_printer;
1882 cups_printer = GTK_PRINTER_CUPS (printer);
1883 if (!cups_printer->reading_ppd &&
1884 gtk_printer_cups_get_ppd (cups_printer) == NULL)
1885 cups_request_ppd (printer);
1889 ppd_text_to_utf8 (ppd_file_t *ppd_file,
1892 const char *encoding = NULL;
1895 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
1897 return g_strdup (text);
1899 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
1901 encoding = "ISO-8859-1";
1903 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
1905 encoding = "ISO-8859-2";
1907 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
1909 encoding = "ISO-8859-5";
1911 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
1913 encoding = "SHIFT-JIS";
1915 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
1917 encoding = "MACINTOSH";
1919 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
1921 encoding = "WINDOWS-1252";
1925 /* Fallback, try iso-8859-1... */
1926 encoding = "ISO-8859-1";
1929 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
1934 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
1935 res = g_strdup ("???");
1941 /* TODO: Add more translations for common settings here */
1943 static const struct {
1944 const char *keyword;
1945 const char *translation;
1946 } cups_option_translations[] = {
1947 { "Duplex", N_("Two Sided") },
1948 { "MediaType", N_("Paper Type") },
1949 { "InputSlot", N_("Paper Source") },
1950 { "OutputBin", N_("Output Tray") },
1954 static const struct {
1955 const char *keyword;
1957 const char *translation;
1958 } cups_choice_translations[] = {
1959 { "Duplex", "None", N_("One Sided") },
1960 { "InputSlot", "Auto", N_("Auto Select") },
1961 { "InputSlot", "AutoSelect", N_("Auto Select") },
1962 { "InputSlot", "Default", N_("Printer Default") },
1963 { "InputSlot", "None", N_("Printer Default") },
1964 { "InputSlot", "PrinterDefault", N_("Printer Default") },
1965 { "InputSlot", "Unspecified", N_("Auto Select") },
1968 static const struct {
1969 const char *ppd_keyword;
1971 } option_names[] = {
1972 {"Duplex", "gtk-duplex" },
1973 {"MediaType", "gtk-paper-type"},
1974 {"InputSlot", "gtk-paper-source"},
1975 {"OutputBin", "gtk-output-tray"},
1978 /* keep sorted when changing */
1979 static const char *color_option_whitelist[] = {
1980 "BRColorEnhancement",
1986 "BlackSubstitution",
1994 "RPSBlackOverPrint",
1998 /* keep sorted when changing */
1999 static const char *color_group_whitelist[] = {
2006 "HPColorOptionsPanel",
2009 /* keep sorted when changing */
2010 static const char *image_quality_option_whitelist[] = {
2012 "BRHalfTonePattern",
2022 "HPGraphicsHalftone",
2037 /* keep sorted when changing */
2038 static const char *image_quality_group_whitelist[] = {
2045 /* keep sorted when changing */
2046 static const char * finishing_option_whitelist[] = {
2059 "StapleOrientation",
2065 /* keep sorted when changing */
2066 static const char *finishing_group_whitelist[] = {
2075 /* keep sorted when changing */
2076 static const char *cups_option_blacklist[] = {
2085 get_option_text (ppd_file_t *ppd_file,
2086 ppd_option_t *option)
2091 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
2093 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
2094 return g_strdup (_(cups_option_translations[i].translation));
2097 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
2099 /* Some ppd files have spaces in the text before the colon */
2106 get_choice_text (ppd_file_t *ppd_file,
2107 ppd_choice_t *choice)
2110 ppd_option_t *option = choice->option;
2111 const char *keyword = option->keyword;
2113 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
2115 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
2116 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
2117 return g_strdup (_(cups_choice_translations[i].translation));
2119 return ppd_text_to_utf8 (ppd_file, choice->text);
2123 group_has_option (ppd_group_t *group,
2124 ppd_option_t *option)
2131 if (group->num_options > 0 &&
2132 option >= group->options && option < group->options + group->num_options)
2135 for (i = 0; i < group->num_subgroups; i++)
2137 if (group_has_option (&group->subgroups[i],option))
2144 set_option_off (GtkPrinterOption *option)
2146 /* Any of these will do, _set only applies the value
2147 * if its allowed of the option */
2148 gtk_printer_option_set (option, "False");
2149 gtk_printer_option_set (option, "Off");
2150 gtk_printer_option_set (option, "None");
2154 value_is_off (const char *value)
2156 return (strcasecmp (value, "None") == 0 ||
2157 strcasecmp (value, "Off") == 0 ||
2158 strcasecmp (value, "False") == 0);
2162 ppd_group_name (ppd_group_t *group)
2164 #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)
2172 available_choices (ppd_file_t *ppd,
2173 ppd_option_t *option,
2174 ppd_choice_t ***available,
2175 gboolean keep_if_only_one_option)
2177 ppd_option_t *other_option;
2180 ppd_const_t *constraint;
2181 const char *choice, *other_choice;
2182 ppd_option_t *option1, *option2;
2183 ppd_group_t *installed_options;
2185 gboolean all_default;
2191 conflicts = g_new0 (char, option->num_choices);
2193 installed_options = NULL;
2194 for (i = 0; i < ppd->num_groups; i++)
2198 name = ppd_group_name (&ppd->groups[i]);
2199 if (strcmp (name, "InstallableOptions") == 0)
2201 installed_options = &ppd->groups[i];
2206 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
2208 option1 = ppdFindOption (ppd, constraint->option1);
2209 if (option1 == NULL)
2212 option2 = ppdFindOption (ppd, constraint->option2);
2213 if (option2 == NULL)
2216 if (option == option1)
2218 choice = constraint->choice1;
2219 other_option = option2;
2220 other_choice = constraint->choice2;
2222 else if (option == option2)
2224 choice = constraint->choice2;
2225 other_option = option1;
2226 other_choice = constraint->choice1;
2231 /* We only care of conflicts with installed_options and
2233 if (!group_has_option (installed_options, other_option) &&
2234 (strcmp (other_option->keyword, "PageSize") != 0))
2237 if (*other_choice == 0)
2239 /* Conflict only if the installed option is not off */
2240 if (value_is_off (other_option->defchoice))
2243 /* Conflict if the installed option has the specified default */
2244 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
2249 /* Conflict with all non-off choices */
2250 for (j = 0; j < option->num_choices; j++)
2252 if (!value_is_off (option->choices[j].choice))
2258 for (j = 0; j < option->num_choices; j++)
2260 if (strcasecmp (option->choices[j].choice, choice) == 0)
2268 for (j = 0; j < option->num_choices; j++)
2272 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
2273 all_default = FALSE;
2276 if ((all_default && !keep_if_only_one_option) ||
2277 (num_conflicts == option->num_choices))
2284 /* Some ppds don't have a "use printer default" option for
2285 * InputSlot. This means you always have to select a particular slot,
2286 * and you can't auto-pick source based on the paper size. To support
2287 * this we always add an auto option if there isn't one already. If
2288 * the user chooses the generated option we don't send any InputSlot
2289 * value when printing. The way we detect existing auto-cases is based
2290 * on feedback from Michael Sweet of cups fame.
2293 if (strcmp (option->keyword, "InputSlot") == 0)
2295 gboolean found_auto = FALSE;
2296 for (j = 0; j < option->num_choices; j++)
2300 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
2301 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
2302 strcmp (option->choices[j].choice, "Default") == 0 ||
2303 strcmp (option->choices[j].choice, "None") == 0 ||
2304 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
2305 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
2306 option->choices[j].code == NULL ||
2307 option->choices[j].code[0] == 0)
2321 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
2324 for (j = 0; j < option->num_choices; j++)
2327 (*available)[i++] = &option->choices[j];
2331 (*available)[i++] = NULL;
2336 return option->num_choices - num_conflicts + add_auto;
2339 static GtkPrinterOption *
2340 create_pickone_option (ppd_file_t *ppd_file,
2341 ppd_option_t *ppd_option,
2342 const gchar *gtk_name)
2344 GtkPrinterOption *option;
2345 ppd_choice_t **available;
2349 #ifdef HAVE_CUPS_API_1_2
2350 ppd_coption_t *coption;
2353 g_assert (ppd_option->ui == PPD_UI_PICKONE);
2357 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2361 /* right now only support one parameter per custom option
2362 * if more than one print warning and only offer the default choices
2365 label = get_option_text (ppd_file, ppd_option);
2367 #ifdef HAVE_CUPS_API_1_2
2368 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
2372 ppd_cparam_t *cparam;
2374 cparam = ppdFirstCustomParam (coption);
2376 if (ppdNextCustomParam (coption) == NULL)
2378 switch (cparam->type)
2380 case PPD_CUSTOM_INT:
2381 option = gtk_printer_option_new (gtk_name, label,
2382 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
2384 case PPD_CUSTOM_PASSCODE:
2385 option = gtk_printer_option_new (gtk_name, label,
2386 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
2388 case PPD_CUSTOM_PASSWORD:
2389 option = gtk_printer_option_new (gtk_name, label,
2390 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
2392 case PPD_CUSTOM_REAL:
2393 option = gtk_printer_option_new (gtk_name, label,
2394 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
2396 case PPD_CUSTOM_STRING:
2397 option = gtk_printer_option_new (gtk_name, label,
2398 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
2400 #ifdef PRINT_IGNORED_OPTIONS
2401 case PPD_CUSTOM_POINTS:
2402 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
2404 case PPD_CUSTOM_CURVE:
2405 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
2407 case PPD_CUSTOM_INVCURVE:
2408 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
2415 #ifdef PRINT_IGNORED_OPTIONS
2417 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
2420 #endif /* HAVE_CUPS_API_1_2 */
2423 option = gtk_printer_option_new (gtk_name, label,
2424 GTK_PRINTER_OPTION_TYPE_PICKONE);
2427 gtk_printer_option_allocate_choices (option, n_choices);
2428 for (i = 0; i < n_choices; i++)
2430 if (available[i] == NULL)
2432 /* This was auto-added */
2433 option->choices[i] = g_strdup ("gtk-ignore-value");
2434 option->choices_display[i] = g_strdup (_("Printer Default"));
2438 option->choices[i] = g_strdup (available[i]->choice);
2439 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
2442 gtk_printer_option_set (option, ppd_option->defchoice);
2444 #ifdef PRINT_IGNORED_OPTIONS
2446 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
2453 static GtkPrinterOption *
2454 create_boolean_option (ppd_file_t *ppd_file,
2455 ppd_option_t *ppd_option,
2456 const gchar *gtk_name)
2458 GtkPrinterOption *option;
2459 ppd_choice_t **available;
2463 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
2467 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2470 label = get_option_text (ppd_file, ppd_option);
2471 option = gtk_printer_option_new (gtk_name, label,
2472 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
2475 gtk_printer_option_allocate_choices (option, 2);
2476 option->choices[0] = g_strdup ("True");
2477 option->choices_display[0] = g_strdup ("True");
2478 option->choices[1] = g_strdup ("False");
2479 option->choices_display[1] = g_strdup ("False");
2481 gtk_printer_option_set (option, ppd_option->defchoice);
2483 #ifdef PRINT_IGNORED_OPTIONS
2485 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
2493 get_option_name (const gchar *keyword)
2497 for (i = 0; i < G_N_ELEMENTS (option_names); i++)
2498 if (strcmp (option_names[i].ppd_keyword, keyword) == 0)
2499 return g_strdup (option_names[i].name);
2501 return g_strdup_printf ("cups-%s", keyword);
2505 strptr_cmp (const void *a,
2508 char **aa = (char **)a;
2509 char **bb = (char **)b;
2510 return strcmp (*aa, *bb);
2515 string_in_table (gchar *str,
2516 const gchar *table[],
2519 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
2522 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
2525 handle_option (GtkPrinterOptionSet *set,
2526 ppd_file_t *ppd_file,
2527 ppd_option_t *ppd_option,
2528 ppd_group_t *toplevel_group,
2529 GtkPrintSettings *settings)
2531 GtkPrinterOption *option;
2534 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
2537 name = get_option_name (ppd_option->keyword);
2540 if (ppd_option->ui == PPD_UI_PICKONE)
2542 option = create_pickone_option (ppd_file, ppd_option, name);
2544 else if (ppd_option->ui == PPD_UI_BOOLEAN)
2546 option = create_boolean_option (ppd_file, ppd_option, name);
2548 #ifdef PRINT_IGNORED_OPTIONS
2550 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
2557 name = ppd_group_name (toplevel_group);
2558 if (STRING_IN_TABLE (name,
2559 color_group_whitelist) ||
2560 STRING_IN_TABLE (ppd_option->keyword,
2561 color_option_whitelist))
2563 option->group = g_strdup ("ColorPage");
2565 else if (STRING_IN_TABLE (name,
2566 image_quality_group_whitelist) ||
2567 STRING_IN_TABLE (ppd_option->keyword,
2568 image_quality_option_whitelist))
2570 option->group = g_strdup ("ImageQualityPage");
2572 else if (STRING_IN_TABLE (name,
2573 finishing_group_whitelist) ||
2574 STRING_IN_TABLE (ppd_option->keyword,
2575 finishing_option_whitelist))
2577 option->group = g_strdup ("FinishingPage");
2581 option->group = g_strdup (toplevel_group->text);
2584 set_option_from_settings (option, settings);
2586 gtk_printer_option_set_add (set, option);
2593 handle_group (GtkPrinterOptionSet *set,
2594 ppd_file_t *ppd_file,
2596 ppd_group_t *toplevel_group,
2597 GtkPrintSettings *settings)
2602 /* Ignore installable options */
2603 name = ppd_group_name (toplevel_group);
2604 if (strcmp (name, "InstallableOptions") == 0)
2607 for (i = 0; i < group->num_options; i++)
2608 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
2610 for (i = 0; i < group->num_subgroups; i++)
2611 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
2615 static GtkPrinterOptionSet *
2616 cups_printer_get_options (GtkPrinter *printer,
2617 GtkPrintSettings *settings,
2618 GtkPageSetup *page_setup,
2619 GtkPrintCapabilities capabilities)
2621 GtkPrinterOptionSet *set;
2622 GtkPrinterOption *option;
2623 ppd_file_t *ppd_file;
2625 char *print_at[] = { "now", "at", "on-hold" };
2626 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
2627 char *prio[] = {"100", "80", "50", "30" };
2628 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
2631 cups_option_t *opts = NULL;
2632 GtkPrintBackendCups *backend;
2635 set = gtk_printer_option_set_new ();
2637 /* Cups specific, non-ppd related settings */
2639 option = gtk_printer_option_new ("gtk-n-up", "Pages Per Sheet", GTK_PRINTER_OPTION_TYPE_PICKONE);
2640 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
2642 gtk_printer_option_set (option, "1");
2643 set_option_from_settings (option, settings);
2644 gtk_printer_option_set_add (set, option);
2645 g_object_unref (option);
2647 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
2648 prio_display[i] = _(prio_display[i]);
2650 option = gtk_printer_option_new ("gtk-job-prio", "Job Priority", GTK_PRINTER_OPTION_TYPE_PICKONE);
2651 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
2652 prio, prio_display);
2653 gtk_printer_option_set (option, "50");
2654 set_option_from_settings (option, settings);
2655 gtk_printer_option_set_add (set, option);
2656 g_object_unref (option);
2658 option = gtk_printer_option_new ("gtk-billing-info", "Billing Info", GTK_PRINTER_OPTION_TYPE_STRING);
2659 gtk_printer_option_set (option, "");
2660 set_option_from_settings (option, settings);
2661 gtk_printer_option_set_add (set, option);
2662 g_object_unref (option);
2664 backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
2666 if (backend != NULL)
2668 char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
2669 char *cover_display_default[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
2670 char **cover = NULL;
2671 char **cover_display = NULL;
2672 gint num_of_covers = 0;
2676 num_of_covers = backend->number_of_covers;
2677 cover = g_new (char *, num_of_covers + 1);
2678 cover[num_of_covers] = NULL;
2679 cover_display = g_new (char *, num_of_covers + 1);
2680 cover_display[num_of_covers] = NULL;
2682 for (i = 0; i < num_of_covers; i++)
2684 cover[i] = g_strdup (backend->covers[i]);
2686 for (j = 0; j < G_N_ELEMENTS (cover_default); j++)
2687 if (strcmp (cover_default[j], cover[i]) == 0)
2689 value = cover_display_default[j];
2692 cover_display[i] = (value != NULL) ? g_strdup (value) : g_strdup (backend->covers[i]);
2695 for (i = 0; i < num_of_covers; i++)
2696 cover_display[i] = _(cover_display[i]);
2698 option = gtk_printer_option_new ("gtk-cover-before", "Before", GTK_PRINTER_OPTION_TYPE_PICKONE);
2699 gtk_printer_option_choices_from_array (option, num_of_covers,
2700 cover, cover_display);
2702 if (backend->default_cover_before != NULL)
2703 gtk_printer_option_set (option, backend->default_cover_before);
2705 gtk_printer_option_set (option, "none");
2706 set_option_from_settings (option, settings);
2707 gtk_printer_option_set_add (set, option);
2708 g_object_unref (option);
2710 option = gtk_printer_option_new ("gtk-cover-after", "After", GTK_PRINTER_OPTION_TYPE_PICKONE);
2711 gtk_printer_option_choices_from_array (option, num_of_covers,
2712 cover, cover_display);
2713 if (backend->default_cover_after != NULL)
2714 gtk_printer_option_set (option, backend->default_cover_after);
2716 gtk_printer_option_set (option, "none");
2717 set_option_from_settings (option, settings);
2718 gtk_printer_option_set_add (set, option);
2719 g_object_unref (option);
2722 g_strfreev (cover_display);
2725 option = gtk_printer_option_new ("gtk-print-time", "Print at", GTK_PRINTER_OPTION_TYPE_PICKONE);
2726 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
2727 print_at, print_at);
2728 gtk_printer_option_set (option, "now");
2729 set_option_from_settings (option, settings);
2730 gtk_printer_option_set_add (set, option);
2731 g_object_unref (option);
2733 option = gtk_printer_option_new ("gtk-print-time-text", "Print at time", GTK_PRINTER_OPTION_TYPE_STRING);
2734 gtk_printer_option_set (option, "");
2735 set_option_from_settings (option, settings);
2736 gtk_printer_option_set_add (set, option);
2737 g_object_unref (option);
2739 /* Printer (ppd) specific settings */
2740 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2743 GtkPaperSize *paper_size;
2744 ppd_option_t *option;
2745 const gchar *ppd_name;
2747 ppdMarkDefaults (ppd_file);
2749 paper_size = gtk_page_setup_get_paper_size (page_setup);
2751 option = ppdFindOption (ppd_file, "PageSize");
2752 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
2755 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
2759 char width[G_ASCII_DTOSTR_BUF_SIZE];
2760 char height[G_ASCII_DTOSTR_BUF_SIZE];
2762 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
2763 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
2764 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
2765 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
2766 g_free (custom_name);
2769 for (i = 0; i < ppd_file->num_groups; i++)
2770 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
2773 /* Now honor the user set defaults for this printer */
2774 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
2776 for (i = 0; i < num_opts; i++)
2778 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
2781 name = get_option_name (opts[i].name);
2782 option = gtk_printer_option_set_lookup (set, name);
2784 gtk_printer_option_set (option, opts[i].value);
2788 cupsFreeOptions (num_opts, opts);
2795 mark_option_from_set (GtkPrinterOptionSet *set,
2796 ppd_file_t *ppd_file,
2797 ppd_option_t *ppd_option)
2799 GtkPrinterOption *option;
2800 char *name = get_option_name (ppd_option->keyword);
2802 option = gtk_printer_option_set_lookup (set, name);
2805 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
2812 mark_group_from_set (GtkPrinterOptionSet *set,
2813 ppd_file_t *ppd_file,
2818 for (i = 0; i < group->num_options; i++)
2819 mark_option_from_set (set, ppd_file, &group->options[i]);
2821 for (i = 0; i < group->num_subgroups; i++)
2822 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
2826 set_conflicts_from_option (GtkPrinterOptionSet *set,
2827 ppd_file_t *ppd_file,
2828 ppd_option_t *ppd_option)
2830 GtkPrinterOption *option;
2833 if (ppd_option->conflicted)
2835 name = get_option_name (ppd_option->keyword);
2836 option = gtk_printer_option_set_lookup (set, name);
2839 gtk_printer_option_set_has_conflict (option, TRUE);
2840 #ifdef PRINT_IGNORED_OPTIONS
2842 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
2850 set_conflicts_from_group (GtkPrinterOptionSet *set,
2851 ppd_file_t *ppd_file,
2856 for (i = 0; i < group->num_options; i++)
2857 set_conflicts_from_option (set, ppd_file, &group->options[i]);
2859 for (i = 0; i < group->num_subgroups; i++)
2860 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
2864 cups_printer_mark_conflicts (GtkPrinter *printer,
2865 GtkPrinterOptionSet *options)
2867 ppd_file_t *ppd_file;
2871 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
2873 if (ppd_file == NULL)
2876 ppdMarkDefaults (ppd_file);
2878 for (i = 0; i < ppd_file->num_groups; i++)
2879 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
2881 num_conflicts = ppdConflicts (ppd_file);
2883 if (num_conflicts > 0)
2885 for (i = 0; i < ppd_file->num_groups; i++)
2886 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
2889 return num_conflicts > 0;
2893 GtkPrinter *printer;
2894 GtkPrinterOptionSet *options;
2895 GtkPrintSettings *settings;
2896 ppd_file_t *ppd_file;
2901 const char *standard;
2905 map_settings_to_option (GtkPrinterOption *option,
2906 const NameMapping table[],
2908 GtkPrintSettings *settings,
2909 const gchar *standard_name,
2910 const gchar *cups_name)
2914 const char *cups_value;
2915 const char *standard_value;
2917 /* If the cups-specific setting is set, always use that */
2918 name = g_strdup_printf ("cups-%s", cups_name);
2919 cups_value = gtk_print_settings_get (settings, name);
2922 if (cups_value != NULL)
2924 gtk_printer_option_set (option, cups_value);
2928 /* Otherwise we try to convert from the general setting */
2929 standard_value = gtk_print_settings_get (settings, standard_name);
2930 if (standard_value == NULL)
2933 for (i = 0; i < n_elements; i++)
2935 if (table[i].cups == NULL && table[i].standard == NULL)
2937 gtk_printer_option_set (option, standard_value);
2940 else if (table[i].cups == NULL &&
2941 strcmp (table[i].standard, standard_value) == 0)
2943 set_option_off (option);
2946 else if (strcmp (table[i].standard, standard_value) == 0)
2948 gtk_printer_option_set (option, table[i].cups);
2955 map_option_to_settings (const gchar *value,
2956 const NameMapping table[],
2958 GtkPrintSettings *settings,
2959 const gchar *standard_name,
2960 const gchar *cups_name)
2965 for (i = 0; i < n_elements; i++)
2967 if (table[i].cups == NULL && table[i].standard == NULL)
2969 gtk_print_settings_set (settings,
2974 else if (table[i].cups == NULL && table[i].standard != NULL)
2976 if (value_is_off (value))
2978 gtk_print_settings_set (settings,
2984 else if (strcmp (table[i].cups, value) == 0)
2986 gtk_print_settings_set (settings,
2993 /* Always set the corresponding cups-specific setting */
2994 name = g_strdup_printf ("cups-%s", cups_name);
2995 gtk_print_settings_set (settings, name, value);
3000 static const NameMapping paper_source_map[] = {
3001 { "Lower", "lower"},
3002 { "Middle", "middle"},
3003 { "Upper", "upper"},
3005 { "Envelope", "envelope"},
3006 { "Cassette", "cassette"},
3007 { "LargeCapacity", "large-capacity"},
3008 { "AnySmallFormat", "small-format"},
3009 { "AnyLargeFormat", "large-format"},
3013 static const NameMapping output_tray_map[] = {
3014 { "Upper", "upper"},
3015 { "Lower", "lower"},
3020 static const NameMapping duplex_map[] = {
3021 { "DuplexTumble", "vertical" },
3022 { "DuplexNoTumble", "horizontal" },
3026 static const NameMapping output_mode_map[] = {
3027 { "Standard", "normal" },
3028 { "Normal", "normal" },
3029 { "Draft", "draft" },
3030 { "Fast", "draft" },
3033 static const NameMapping media_type_map[] = {
3034 { "Transparency", "transparency"},
3035 { "Standard", "stationery"},
3039 static const NameMapping all_map[] = {
3045 set_option_from_settings (GtkPrinterOption *option,
3046 GtkPrintSettings *settings)
3048 const char *cups_value;
3051 if (settings == NULL)
3054 if (strcmp (option->name, "gtk-paper-source") == 0)
3055 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
3056 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3057 else if (strcmp (option->name, "gtk-output-tray") == 0)
3058 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
3059 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3060 else if (strcmp (option->name, "gtk-duplex") == 0)
3061 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
3062 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3063 else if (strcmp (option->name, "cups-OutputMode") == 0)
3064 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
3065 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3066 else if (strcmp (option->name, "cups-Resolution") == 0)
3068 cups_value = gtk_print_settings_get (settings, option->name);
3070 gtk_printer_option_set (option, cups_value);
3073 int res = gtk_print_settings_get_resolution (settings);
3076 value = g_strdup_printf ("%ddpi", res);
3077 gtk_printer_option_set (option, value);
3082 else if (strcmp (option->name, "gtk-paper-type") == 0)
3083 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
3084 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3085 else if (strcmp (option->name, "gtk-n-up") == 0)
3087 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
3088 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3090 else if (strcmp (option->name, "gtk-billing-info") == 0)
3092 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
3094 gtk_printer_option_set (option, cups_value);
3096 else if (strcmp (option->name, "gtk-job-prio") == 0)
3098 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
3100 gtk_printer_option_set (option, cups_value);
3102 else if (strcmp (option->name, "gtk-cover-before") == 0)
3104 cups_value = gtk_print_settings_get (settings, "cover-before");
3106 gtk_printer_option_set (option, cups_value);
3108 else if (strcmp (option->name, "gtk-cover-after") == 0)
3110 cups_value = gtk_print_settings_get (settings, "cover-after");
3112 gtk_printer_option_set (option, cups_value);
3114 else if (strcmp (option->name, "gtk-print-time") == 0)
3116 cups_value = gtk_print_settings_get (settings, "print-at");
3118 gtk_printer_option_set (option, cups_value);
3120 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3122 cups_value = gtk_print_settings_get (settings, "print-at-time");
3124 gtk_printer_option_set (option, cups_value);
3126 else if (g_str_has_prefix (option->name, "cups-"))
3128 cups_value = gtk_print_settings_get (settings, option->name);
3130 gtk_printer_option_set (option, cups_value);
3135 foreach_option_get_settings (GtkPrinterOption *option,
3138 struct OptionData *data = user_data;
3139 GtkPrintSettings *settings = data->settings;
3142 value = option->value;
3144 if (strcmp (option->name, "gtk-paper-source") == 0)
3145 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
3146 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3147 else if (strcmp (option->name, "gtk-output-tray") == 0)
3148 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
3149 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3150 else if (strcmp (option->name, "gtk-duplex") == 0)
3151 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
3152 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3153 else if (strcmp (option->name, "cups-OutputMode") == 0)
3154 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
3155 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3156 else if (strcmp (option->name, "cups-Resolution") == 0)
3158 int res = atoi (value);
3159 /* TODO: What if resolution is on XXXxYYYdpi form? */
3161 gtk_print_settings_set_resolution (settings, res);
3162 gtk_print_settings_set (settings, option->name, value);
3164 else if (strcmp (option->name, "gtk-paper-type") == 0)
3165 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
3166 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3167 else if (strcmp (option->name, "gtk-n-up") == 0)
3168 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3169 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3170 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
3171 gtk_print_settings_set (settings, "cups-job-billing", value);
3172 else if (strcmp (option->name, "gtk-job-prio") == 0)
3173 gtk_print_settings_set (settings, "cups-job-priority", value);
3174 else if (strcmp (option->name, "gtk-cover-before") == 0)
3175 gtk_print_settings_set (settings, "cover-before", value);
3176 else if (strcmp (option->name, "gtk-cover-after") == 0)
3177 gtk_print_settings_set (settings, "cover-after", value);
3178 else if (strcmp (option->name, "gtk-print-time") == 0)
3179 gtk_print_settings_set (settings, "print-at", value);
3180 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3181 gtk_print_settings_set (settings, "print-at-time", value);
3182 else if (g_str_has_prefix (option->name, "cups-"))
3183 gtk_print_settings_set (settings, option->name, value);
3187 cups_printer_get_settings_from_options (GtkPrinter *printer,
3188 GtkPrinterOptionSet *options,
3189 GtkPrintSettings *settings)
3191 struct OptionData data;
3192 const char *print_at, *print_at_time;
3194 data.printer = printer;
3195 data.options = options;
3196 data.settings = settings;
3197 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3199 if (data.ppd_file != NULL)
3201 GtkPrinterOption *cover_before, *cover_after;
3203 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
3205 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
3206 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
3207 if (cover_before && cover_after)
3209 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
3210 gtk_print_settings_set (settings, "cups-job-sheets", value);
3214 print_at = gtk_print_settings_get (settings, "print-at");
3215 print_at_time = gtk_print_settings_get (settings, "print-at-time");
3216 if (strcmp (print_at, "at") == 0)
3217 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
3218 else if (strcmp (print_at, "on-hold") == 0)
3219 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
3224 cups_printer_prepare_for_print (GtkPrinter *printer,
3225 GtkPrintJob *print_job,
3226 GtkPrintSettings *settings,
3227 GtkPageSetup *page_setup)
3229 GtkPageSet page_set;
3230 GtkPaperSize *paper_size;
3231 const char *ppd_paper_name;
3234 print_job->print_pages = gtk_print_settings_get_print_pages (settings);
3235 print_job->page_ranges = NULL;
3236 print_job->num_page_ranges = 0;
3238 if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
3239 print_job->page_ranges =
3240 gtk_print_settings_get_page_ranges (settings,
3241 &print_job->num_page_ranges);
3243 if (gtk_print_settings_get_collate (settings))
3244 gtk_print_settings_set (settings, "cups-Collate", "True");
3245 print_job->collate = FALSE;
3247 if (gtk_print_settings_get_reverse (settings))
3248 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
3249 print_job->reverse = FALSE;
3251 if (gtk_print_settings_get_n_copies (settings) > 1)
3252 gtk_print_settings_set_int (settings, "cups-copies",
3253 gtk_print_settings_get_n_copies (settings));
3254 print_job->num_copies = 1;
3256 scale = gtk_print_settings_get_scale (settings);
3257 print_job->scale = 1.0;
3259 print_job->scale = scale/100.0;
3261 page_set = gtk_print_settings_get_page_set (settings);
3262 if (page_set == GTK_PAGE_SET_EVEN)
3263 gtk_print_settings_set (settings, "cups-page-set", "even");
3264 else if (page_set == GTK_PAGE_SET_ODD)
3265 gtk_print_settings_set (settings, "cups-page-set", "odd");
3266 print_job->page_set = GTK_PAGE_SET_ALL;
3268 paper_size = gtk_page_setup_get_paper_size (page_setup);
3269 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
3270 if (ppd_paper_name != NULL)
3271 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
3274 char width[G_ASCII_DTOSTR_BUF_SIZE];
3275 char height[G_ASCII_DTOSTR_BUF_SIZE];
3278 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3279 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3280 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
3281 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
3282 g_free (custom_name);
3285 print_job->rotate_to_orientation = TRUE;
3288 static GtkPageSetup *
3289 create_page_setup (ppd_file_t *ppd_file,
3293 GtkPageSetup *page_setup;
3294 GtkPaperSize *paper_size;
3295 ppd_option_t *option;
3296 ppd_choice_t *choice;
3298 display_name = NULL;
3299 option = ppdFindOption (ppd_file, "PageSize");
3302 choice = ppdFindChoice (option, size->name);
3304 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
3307 if (display_name == NULL)
3308 display_name = g_strdup (size->name);
3310 page_setup = gtk_page_setup_new ();
3311 paper_size = gtk_paper_size_new_from_ppd (size->name,
3315 gtk_page_setup_set_paper_size (page_setup, paper_size);
3316 gtk_paper_size_free (paper_size);
3318 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
3319 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
3320 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
3321 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
3323 g_free (display_name);
3329 cups_printer_list_papers (GtkPrinter *printer)
3331 ppd_file_t *ppd_file;
3333 GtkPageSetup *page_setup;
3337 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3338 if (ppd_file == NULL)
3343 for (i = 0; i < ppd_file->num_sizes; i++)
3345 size = &ppd_file->sizes[i];
3347 page_setup = create_page_setup (ppd_file, size);
3349 l = g_list_prepend (l, page_setup);
3352 return g_list_reverse (l);
3355 static GtkPageSetup *
3356 cups_printer_get_default_page_size (GtkPrinter *printer)
3358 ppd_file_t *ppd_file;
3360 ppd_option_t *option;
3363 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3364 if (ppd_file == NULL)
3367 option = ppdFindOption (ppd_file, "PageSize");
3368 size = ppdPageSize (ppd_file, option->defchoice);
3370 return create_page_setup (ppd_file, size);
3374 cups_printer_get_hard_margins (GtkPrinter *printer,
3380 ppd_file_t *ppd_file;
3382 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3383 if (ppd_file == NULL)
3386 *left = ppd_file->custom_margins[0];
3387 *bottom = ppd_file->custom_margins[1];
3388 *right = ppd_file->custom_margins[2];
3389 *top = ppd_file->custom_margins[3];
3392 static GtkPrintCapabilities
3393 cups_printer_get_capabilities (GtkPrinter *printer)
3396 GTK_PRINT_CAPABILITY_COPIES |
3397 GTK_PRINT_CAPABILITY_COLLATE |
3398 GTK_PRINT_CAPABILITY_REVERSE |
3399 GTK_PRINT_CAPABILITY_NUMBER_UP;