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.
29 #include <sys/types.h>
34 #include <cups/cups.h>
35 #include <cups/language.h>
36 #include <cups/http.h>
40 #include <cairo-pdf.h>
43 #include <glib/gstdio.h>
44 #include <glib/gi18n-lib.h>
48 #include <gtk/gtkprintbackend.h>
49 #include <gtk/gtkunixprint.h>
50 #include <gtk/gtkprinter-private.h>
52 #include "gtkprintbackendcups.h"
53 #include "gtkprintercups.h"
55 #include "gtkcupsutils.h"
58 typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
60 #define GTK_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
61 #define GTK_IS_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_CUPS))
62 #define GTK_PRINT_BACKEND_CUPS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
64 #define _CUPS_MAX_ATTEMPTS 10
65 #define _CUPS_MAX_CHUNK_SIZE 8192
67 /* define this to see warnings about ignored ppd options */
68 #undef PRINT_IGNORED_OPTIONS
70 #define _CUPS_MAP_ATTR_INT(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].integer;}
71 #define _CUPS_MAP_ATTR_STR(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].string.text;}
73 static GType print_backend_cups_type = 0;
75 typedef void (* GtkPrintCupsResponseCallbackFunc) (GtkPrintBackend *print_backend,
76 GtkCupsResult *result,
87 } GtkPrintCupsDispatchState;
94 GtkCupsRequest *request;
96 GtkPrintBackendCups *backend;
98 } GtkPrintCupsDispatchWatch;
100 struct _GtkPrintBackendCupsClass
102 GtkPrintBackendClass parent_class;
105 struct _GtkPrintBackendCups
107 GtkPrintBackend parent_instance;
109 char *default_printer;
111 guint list_printers_poll;
112 guint list_printers_pending : 1;
113 guint got_default_printer : 1;
114 guint default_printer_poll;
115 GtkCupsConnectionTest *cups_connection_test;
118 char *default_cover_before;
119 char *default_cover_after;
120 int number_of_covers;
123 static GObjectClass *backend_parent_class;
125 static void gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class);
126 static void gtk_print_backend_cups_init (GtkPrintBackendCups *impl);
127 static void gtk_print_backend_cups_finalize (GObject *object);
128 static void gtk_print_backend_cups_dispose (GObject *object);
129 static void cups_get_printer_list (GtkPrintBackend *print_backend);
130 static void cups_get_default_printer (GtkPrintBackendCups *print_backend);
131 static void cups_get_local_default_printer (GtkPrintBackendCups *print_backend);
132 static void cups_request_execute (GtkPrintBackendCups *print_backend,
133 GtkCupsRequest *request,
134 GtkPrintCupsResponseCallbackFunc callback,
136 GDestroyNotify notify);
137 static void cups_printer_get_settings_from_options (GtkPrinter *printer,
138 GtkPrinterOptionSet *options,
139 GtkPrintSettings *settings);
140 static gboolean cups_printer_mark_conflicts (GtkPrinter *printer,
141 GtkPrinterOptionSet *options);
142 static GtkPrinterOptionSet *cups_printer_get_options (GtkPrinter *printer,
143 GtkPrintSettings *settings,
144 GtkPageSetup *page_setup,
145 GtkPrintCapabilities capabilities);
146 static void cups_printer_prepare_for_print (GtkPrinter *printer,
147 GtkPrintJob *print_job,
148 GtkPrintSettings *settings,
149 GtkPageSetup *page_setup);
150 static GList * cups_printer_list_papers (GtkPrinter *printer);
151 static GtkPageSetup * cups_printer_get_default_page_size (GtkPrinter *printer);
152 static void cups_printer_request_details (GtkPrinter *printer);
153 static gboolean cups_request_default_printer (GtkPrintBackendCups *print_backend);
154 static void cups_request_ppd (GtkPrinter *printer);
155 static void cups_printer_get_hard_margins (GtkPrinter *printer,
160 static GtkPrintCapabilities cups_printer_get_capabilities (GtkPrinter *printer);
161 static void set_option_from_settings (GtkPrinterOption *option,
162 GtkPrintSettings *setting);
163 static void cups_begin_polling_info (GtkPrintBackendCups *print_backend,
166 static gboolean cups_job_info_poll_timeout (gpointer user_data);
167 static void gtk_print_backend_cups_print_stream (GtkPrintBackend *backend,
170 GtkPrintJobCompleteFunc callback,
172 GDestroyNotify dnotify);
173 static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter *printer,
174 GtkPrintSettings *settings,
177 GIOChannel *cache_io);
181 gtk_print_backend_cups_register_type (GTypeModule *module)
183 static const GTypeInfo print_backend_cups_info =
185 sizeof (GtkPrintBackendCupsClass),
186 NULL, /* base_init */
187 NULL, /* base_finalize */
188 (GClassInitFunc) gtk_print_backend_cups_class_init,
189 NULL, /* class_finalize */
190 NULL, /* class_data */
191 sizeof (GtkPrintBackendCups),
193 (GInstanceInitFunc) gtk_print_backend_cups_init
196 print_backend_cups_type = g_type_module_register_type (module,
197 GTK_TYPE_PRINT_BACKEND,
198 "GtkPrintBackendCups",
199 &print_backend_cups_info, 0);
203 pb_module_init (GTypeModule *module)
206 g_print ("CUPS Backend: Initializing the CUPS print backend module\n"));
208 gtk_print_backend_cups_register_type (module);
209 gtk_printer_cups_register_type (module);
213 pb_module_exit (void)
218 G_MODULE_EXPORT GtkPrintBackend *
219 pb_module_create (void)
221 return gtk_print_backend_cups_new ();
225 * GtkPrintBackendCups
228 gtk_print_backend_cups_get_type (void)
230 return print_backend_cups_type;
234 * gtk_print_backend_cups_new:
236 * Creates a new #GtkPrintBackendCups object. #GtkPrintBackendCups
237 * implements the #GtkPrintBackend interface with direct access to
238 * the filesystem using Unix/Linux API calls
240 * Return value: the new #GtkPrintBackendCups object
243 gtk_print_backend_cups_new (void)
246 g_print ("CUPS Backend: Creating a new CUPS print backend object\n"));
248 return g_object_new (GTK_TYPE_PRINT_BACKEND_CUPS, NULL);
252 gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
254 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
255 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
257 backend_parent_class = g_type_class_peek_parent (class);
259 gobject_class->finalize = gtk_print_backend_cups_finalize;
260 gobject_class->dispose = gtk_print_backend_cups_dispose;
262 backend_class->request_printer_list = cups_get_printer_list;
263 backend_class->print_stream = gtk_print_backend_cups_print_stream;
264 backend_class->printer_request_details = cups_printer_request_details;
265 backend_class->printer_create_cairo_surface = cups_printer_create_cairo_surface;
266 backend_class->printer_get_options = cups_printer_get_options;
267 backend_class->printer_mark_conflicts = cups_printer_mark_conflicts;
268 backend_class->printer_get_settings_from_options = cups_printer_get_settings_from_options;
269 backend_class->printer_prepare_for_print = cups_printer_prepare_for_print;
270 backend_class->printer_list_papers = cups_printer_list_papers;
271 backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
272 backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
273 backend_class->printer_get_capabilities = cups_printer_get_capabilities;
276 static cairo_status_t
277 _cairo_write_to_cups (void *closure,
278 const unsigned char *data,
281 GIOChannel *io = (GIOChannel *)closure;
288 g_print ("CUPS Backend: Writing %i byte chunk to temp file\n", length));
292 g_io_channel_write_chars (io, (gchar *)data, length, &written, &error);
297 g_print ("CUPS Backend: Error writing to temp file, %s\n",
300 g_error_free (error);
301 return CAIRO_STATUS_WRITE_ERROR;
305 g_print ("CUPS Backend: Wrote %i bytes to temp file\n", written));
311 return CAIRO_STATUS_SUCCESS;
314 static cairo_surface_t *
315 cups_printer_create_cairo_surface (GtkPrinter *printer,
316 GtkPrintSettings *settings,
319 GIOChannel *cache_io)
321 cairo_surface_t *surface;
322 ppd_file_t *ppd_file = NULL;
323 ppd_attr_t *ppd_attr = NULL;
324 ppd_attr_t *ppd_attr_res = NULL;
325 ppd_attr_t *ppd_attr_screen_freq = NULL;
326 ppd_attr_t *ppd_attr_res_screen_freq = NULL;
327 gchar *res_string = NULL;
330 /* TODO: check if it is a ps or pdf printer */
332 surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
334 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
336 if (ppd_file != NULL)
338 ppd_attr = ppdFindAttr (ppd_file, "LanguageLevel", NULL);
340 if (ppd_attr != NULL)
341 level = atoi (ppd_attr->value);
343 if (gtk_print_settings_get_resolution (settings) == 0)
345 ppd_attr_res = ppdFindAttr (ppd_file, "DefaultResolution", NULL);
347 if (ppd_attr_res != NULL)
349 int res, res_x, res_y;
351 if (sscanf (ppd_attr_res->value, "%dx%ddpi", &res_x, &res_y) == 2)
353 if (res_x != 0 && res_y != 0)
354 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
356 else if (sscanf (ppd_attr_res->value, "%ddpi", &res) == 1)
359 gtk_print_settings_set_resolution (settings, res);
362 gtk_print_settings_set_resolution (settings, 300);
366 res_string = g_strdup_printf ("%ddpi",
367 gtk_print_settings_get_resolution (settings));
368 ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
371 if (ppd_attr_res_screen_freq == NULL)
373 res_string = g_strdup_printf ("%dx%ddpi",
374 gtk_print_settings_get_resolution_x (settings),
375 gtk_print_settings_get_resolution_y (settings));
376 ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
380 ppd_attr_screen_freq = ppdFindAttr (ppd_file, "ScreenFreq", NULL);
382 if (ppd_attr_res_screen_freq != NULL)
383 gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_res_screen_freq->value));
384 else if (ppd_attr_screen_freq != NULL)
385 gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_screen_freq->value));
387 gtk_print_settings_set_printer_lpi (settings, 150.0);
391 gtk_print_settings_set_resolution (settings, 300);
392 gtk_print_settings_set_printer_lpi (settings, 150.0);
396 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
399 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
401 cairo_surface_set_fallback_resolution (surface,
402 2.0 * gtk_print_settings_get_printer_lpi (settings),
403 2.0 * gtk_print_settings_get_printer_lpi (settings));
409 GtkPrintJobCompleteFunc callback;
412 GDestroyNotify dnotify;
413 } CupsPrintStreamData;
416 cups_free_print_stream_data (CupsPrintStreamData *data)
419 g_print ("CUPS Backend: %s\n", G_STRFUNC));
422 data->dnotify (data->user_data);
423 g_object_unref (data->job);
428 cups_print_cb (GtkPrintBackendCups *print_backend,
429 GtkCupsResult *result,
432 GError *error = NULL;
433 CupsPrintStreamData *ps = user_data;
435 GDK_THREADS_ENTER ();
438 g_print ("CUPS Backend: %s\n", G_STRFUNC));
440 if (gtk_cups_result_is_error (result))
441 error = g_error_new_literal (gtk_print_error_quark (),
442 GTK_PRINT_ERROR_INTERNAL_ERROR,
443 gtk_cups_result_get_error_string (result));
446 ps->callback (ps->job, ps->user_data, error);
451 ipp_attribute_t *attr; /* IPP job-id attribute */
452 ipp_t *response = gtk_cups_result_get_response (result);
454 if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
455 job_id = attr->values[0].integer;
457 if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
458 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
461 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_PENDING);
462 cups_begin_polling_info (print_backend, ps->job, job_id);
466 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED_ABORTED);
470 g_error_free (error);
472 GDK_THREADS_LEAVE ();
476 add_cups_options (const gchar *key,
480 GtkCupsRequest *request = user_data;
482 if (!g_str_has_prefix (key, "cups-"))
485 if (strcmp (value, "gtk-ignore-value") == 0)
488 key = key + strlen ("cups-");
490 gtk_cups_request_encode_option (request, key, value);
494 gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
497 GtkPrintJobCompleteFunc callback,
499 GDestroyNotify dnotify)
501 GtkPrinterCups *cups_printer;
502 CupsPrintStreamData *ps;
503 GtkCupsRequest *request;
504 GtkPrintSettings *settings;
506 char printer_absolute_uri[HTTP_MAX_URI];
509 g_print ("CUPS Backend: %s\n", G_STRFUNC));
511 cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
512 settings = gtk_print_job_get_settings (job);
514 request = gtk_cups_request_new (NULL,
519 cups_printer->device_uri);
521 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
522 httpAssembleURIf (HTTP_URI_CODING_ALL,
523 printer_absolute_uri,
524 sizeof (printer_absolute_uri),
530 gtk_printer_get_name (gtk_print_job_get_printer (job)));
532 g_snprintf (printer_absolute_uri,
533 sizeof (printer_absolute_uri),
534 "ipp://localhost:%d/printers/%s",
536 gtk_printer_get_name (gtk_print_job_get_printer (job)));
539 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
540 IPP_TAG_URI, "printer-uri",
541 NULL, printer_absolute_uri);
543 title = gtk_print_job_get_title (job);
545 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
546 IPP_TAG_NAME, "job-name",
549 gtk_print_settings_foreach (settings, add_cups_options, request);
551 ps = g_new0 (CupsPrintStreamData, 1);
552 ps->callback = callback;
553 ps->user_data = user_data;
554 ps->dnotify = dnotify;
555 ps->job = g_object_ref (job);
557 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
559 (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
561 (GDestroyNotify)cups_free_print_stream_data);
566 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
568 backend_cups->list_printers_poll = FALSE;
569 backend_cups->got_default_printer = FALSE;
570 backend_cups->list_printers_pending = FALSE;
572 backend_cups->covers = NULL;
573 backend_cups->default_cover_before = NULL;
574 backend_cups->default_cover_after = NULL;
575 backend_cups->number_of_covers = 0;
577 backend_cups->default_printer_poll = 0;
578 backend_cups->cups_connection_test = NULL;
580 cups_get_local_default_printer (backend_cups);
584 gtk_print_backend_cups_finalize (GObject *object)
586 GtkPrintBackendCups *backend_cups;
589 g_print ("CUPS Backend: finalizing CUPS backend module\n"));
591 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
593 g_free (backend_cups->default_printer);
594 backend_cups->default_printer = NULL;
596 g_strfreev (backend_cups->covers);
597 backend_cups->number_of_covers = 0;
599 g_free (backend_cups->default_cover_before);
600 g_free (backend_cups->default_cover_after);
602 gtk_cups_connection_test_free (backend_cups->cups_connection_test);
603 backend_cups->cups_connection_test = NULL;
605 backend_parent_class->finalize (object);
609 gtk_print_backend_cups_dispose (GObject *object)
611 GtkPrintBackendCups *backend_cups;
614 g_print ("CUPS Backend: %s\n", G_STRFUNC));
616 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
618 if (backend_cups->list_printers_poll > 0)
619 g_source_remove (backend_cups->list_printers_poll);
620 backend_cups->list_printers_poll = 0;
622 if (backend_cups->default_printer_poll > 0)
623 g_source_remove (backend_cups->default_printer_poll);
624 backend_cups->default_printer_poll = 0;
626 backend_parent_class->dispose (object);
631 cups_dispatch_watch_check (GSource *source)
633 GtkPrintCupsDispatchWatch *dispatch;
634 GtkCupsPollState poll_state;
638 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
640 dispatch = (GtkPrintCupsDispatchWatch *) source;
642 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
644 if (dispatch->request->http != NULL)
646 if (dispatch->data_poll == NULL)
648 dispatch->data_poll = g_new0 (GPollFD, 1);
649 g_source_add_poll (source, dispatch->data_poll);
653 if (poll_state == GTK_CUPS_HTTP_READ)
654 dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
655 else if (poll_state == GTK_CUPS_HTTP_WRITE)
656 dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
658 dispatch->data_poll->events = 0;
661 #ifdef HAVE_CUPS_API_1_2
662 dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
664 dispatch->data_poll->fd = dispatch->request->http->fd;
668 if (poll_state != GTK_CUPS_HTTP_IDLE)
669 if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
672 result = gtk_cups_request_read_write (dispatch->request);
673 if (result && dispatch->data_poll != NULL)
675 g_source_remove_poll (source, dispatch->data_poll);
676 g_free (dispatch->data_poll);
677 dispatch->data_poll = NULL;
684 cups_dispatch_watch_prepare (GSource *source,
687 GtkPrintCupsDispatchWatch *dispatch;
689 dispatch = (GtkPrintCupsDispatchWatch *) source;
692 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
696 return gtk_cups_request_read_write (dispatch->request);
700 cups_dispatch_watch_dispatch (GSource *source,
701 GSourceFunc callback,
704 GtkPrintCupsDispatchWatch *dispatch;
705 GtkPrintCupsResponseCallbackFunc ep_callback;
706 GtkCupsResult *result;
708 g_assert (callback != NULL);
710 ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
712 dispatch = (GtkPrintCupsDispatchWatch *) source;
714 result = gtk_cups_request_get_result (dispatch->request);
717 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
719 if (gtk_cups_result_is_error (result))
722 g_print("Error result: %s (type %i, status %i, code %i)\n",
723 gtk_cups_result_get_error_string (result),
724 gtk_cups_result_get_error_type (result),
725 gtk_cups_result_get_error_status (result),
726 gtk_cups_result_get_error_code (result)));
729 ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
735 cups_dispatch_watch_finalize (GSource *source)
737 GtkPrintCupsDispatchWatch *dispatch;
740 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
742 dispatch = (GtkPrintCupsDispatchWatch *) source;
744 gtk_cups_request_free (dispatch->request);
746 if (dispatch->backend)
748 /* We need to unref this at idle time, because it might be the
749 * last reference to this module causing the code to be
750 * unloaded (including this particular function!)
751 * Update: Doing this at idle caused a deadlock taking the
752 * mainloop context lock while being in a GSource callout for
753 * multithreaded apps. So, for now we just disable unloading
754 * of print backends. See _gtk_print_backend_create for the
757 g_object_unref (dispatch->backend);
758 dispatch->backend = NULL;
761 g_free (dispatch->data_poll);
764 static GSourceFuncs _cups_dispatch_watch_funcs = {
765 cups_dispatch_watch_prepare,
766 cups_dispatch_watch_check,
767 cups_dispatch_watch_dispatch,
768 cups_dispatch_watch_finalize
773 cups_request_execute (GtkPrintBackendCups *print_backend,
774 GtkCupsRequest *request,
775 GtkPrintCupsResponseCallbackFunc callback,
777 GDestroyNotify notify)
779 GtkPrintCupsDispatchWatch *dispatch;
781 dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
782 sizeof (GtkPrintCupsDispatchWatch));
785 g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
787 dispatch->request = request;
788 dispatch->backend = g_object_ref (print_backend);
789 dispatch->data_poll = NULL;
791 g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
793 g_source_attach ((GSource *) dispatch, NULL);
794 g_source_unref ((GSource *) dispatch);
799 cups_request_printer_info_cb (GtkPrintBackendCups *backend,
800 GtkCupsResult *result,
803 ipp_attribute_t *attr;
806 GtkPrinterCups *cups_printer;
812 gboolean status_changed;
814 g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
816 printer_name = (gchar *)user_data;
817 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
821 g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
826 g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
830 cups_printer = GTK_PRINTER_CUPS (printer);
832 if (gtk_cups_result_is_error (result))
834 if (gtk_printer_is_new (printer))
836 gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
841 return; /* TODO: mark as inactive printer */
844 response = gtk_cups_result_get_response (result);
846 /* TODO: determine printer type and use correct icon */
847 gtk_printer_set_icon_name (printer, "gtk-print");
853 for (attr = response->attrs; attr != NULL; attr = attr->next)
858 _CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
859 _CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
860 _CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
861 _CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
862 _CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
865 status_changed = gtk_printer_set_job_count (printer, job_count);
867 status_changed |= gtk_printer_set_location (printer, loc);
868 status_changed |= gtk_printer_set_description (printer, desc);
869 status_changed |= gtk_printer_set_state_message (printer, state_msg);
872 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
873 "printer-status-changed", printer);
877 cups_request_printer_info (GtkPrintBackendCups *print_backend,
878 const gchar *printer_name)
880 GtkCupsRequest *request;
882 static const char * const pattrs[] = /* Attributes we're interested in */
886 "printer-state-message",
889 "job-sheets-supported",
893 request = gtk_cups_request_new (NULL,
895 IPP_GET_PRINTER_ATTRIBUTES,
900 printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
902 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
903 "printer-uri", NULL, printer_uri);
906 g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
908 g_free (printer_uri);
910 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
911 "requested-attributes", G_N_ELEMENTS (pattrs),
914 cups_request_execute (print_backend,
916 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
917 g_strdup (printer_name),
918 (GDestroyNotify) g_free);
923 GtkPrintBackendCups *print_backend;
930 job_object_died (gpointer user_data,
931 GObject *where_the_object_was)
933 CupsJobPollData *data = user_data;
938 cups_job_poll_data_free (CupsJobPollData *data)
941 g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
947 cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
948 GtkCupsResult *result,
951 CupsJobPollData *data = user_data;
952 ipp_attribute_t *attr;
957 GDK_THREADS_ENTER ();
959 if (data->job == NULL)
961 cups_job_poll_data_free (data);
967 response = gtk_cups_result_get_response (result);
970 for (attr = response->attrs; attr != NULL; attr = attr->next)
975 _CUPS_MAP_ATTR_INT (attr, state, "job-state");
981 case IPP_JOB_PENDING:
983 case IPP_JOB_STOPPED:
984 gtk_print_job_set_status (data->job,
985 GTK_PRINT_STATUS_PENDING);
987 case IPP_JOB_PROCESSING:
988 gtk_print_job_set_status (data->job,
989 GTK_PRINT_STATUS_PRINTING);
992 case IPP_JOB_CANCELLED:
993 case IPP_JOB_ABORTED:
994 gtk_print_job_set_status (data->job,
995 GTK_PRINT_STATUS_FINISHED_ABORTED);
999 case IPP_JOB_COMPLETED:
1000 gtk_print_job_set_status (data->job,
1001 GTK_PRINT_STATUS_FINISHED);
1006 if (!done && data->job != NULL)
1010 if (data->counter < 5)
1012 else if (data->counter < 10)
1017 g_timeout_add (timeout, cups_job_info_poll_timeout, data);
1020 cups_job_poll_data_free (data);
1023 GDK_THREADS_LEAVE ();
1027 cups_request_job_info (CupsJobPollData *data)
1029 GtkCupsRequest *request;
1032 request = gtk_cups_request_new (NULL,
1034 IPP_GET_JOB_ATTRIBUTES,
1039 job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
1040 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
1041 "job-uri", NULL, job_uri);
1044 cups_request_execute (data->print_backend,
1046 (GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
1052 cups_job_info_poll_timeout (gpointer user_data)
1054 CupsJobPollData *data = user_data;
1056 if (data->job == NULL)
1057 cups_job_poll_data_free (data);
1059 cups_request_job_info (data);
1065 cups_begin_polling_info (GtkPrintBackendCups *print_backend,
1069 CupsJobPollData *data;
1071 data = g_new0 (CupsJobPollData, 1);
1073 data->print_backend = print_backend;
1075 data->job_id = job_id;
1078 g_object_weak_ref (G_OBJECT (job), job_object_died, data);
1080 cups_request_job_info (data);
1084 mark_printer_inactive (GtkPrinter *printer,
1085 GtkPrintBackend *backend)
1087 gtk_printer_set_is_active (printer, FALSE);
1088 g_signal_emit_by_name (backend, "printer-removed", printer);
1092 find_printer (GtkPrinter *printer,
1093 const gchar *find_name)
1095 const gchar *printer_name;
1097 printer_name = gtk_printer_get_name (printer);
1098 return g_ascii_strcasecmp (printer_name, find_name);
1102 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
1103 GtkCupsResult *result,
1106 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
1107 ipp_attribute_t *attr;
1109 gboolean list_has_changed;
1110 GList *removed_printer_checklist;
1112 GDK_THREADS_ENTER ();
1114 list_has_changed = FALSE;
1117 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1119 cups_backend->list_printers_pending = FALSE;
1121 if (gtk_cups_result_is_error (result))
1124 g_warning ("CUPS Backend: Error getting printer list: %s",
1125 gtk_cups_result_get_error_string (result)));
1130 /* Gather the names of the printers in the current queue
1131 * so we may check to see if they were removed
1133 removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
1135 response = gtk_cups_result_get_response (result);
1137 for (attr = response->attrs; attr != NULL; attr = attr->next)
1139 GtkPrinter *printer;
1140 const gchar *printer_name = NULL;
1141 const gchar *printer_uri = NULL;
1142 const gchar *member_uris = NULL;
1143 const gchar *location = NULL;
1144 const gchar *description = NULL;
1145 const gchar *state_msg = NULL;
1148 gboolean status_changed = FALSE;
1151 const gchar *reason_msg = NULL;
1152 gchar *reason_msg_desc = NULL;
1153 gchar *tmp_msg = NULL;
1154 gchar *tmp_msg2 = NULL;
1155 gint printer_state_reason_level = 0; /* 0 - none, 1 - report, 2 - warning, 3 - error */
1156 gboolean interested_in = FALSE;
1157 gboolean found = FALSE;
1158 static const char * const reasons[] = /* Reasons we're interested in */
1164 "marker-supply-low",
1165 "marker-supply-empty",
1171 "connecting-to-device",
1174 static const char * reasons_descs[] =
1176 N_("Printer '%s' is low on toner."),
1177 N_("Printer '%s' has no toner left."),
1178 /* Translators: "Developer" like on photo development context */
1179 N_("Printer '%s' is low on developer."),
1180 /* Translators: "Developer" like on photo development context */
1181 N_("Printer '%s' is out of developer."),
1182 /* Translators: "marker" is one color bin of the printer */
1183 N_("Printer '%s' is low on at least one marker supply."),
1184 /* Translators: "marker" is one color bin of the printer */
1185 N_("Printer '%s' is out of at least one marker supply."),
1186 N_("The cover is open on printer '%s'."),
1187 N_("The door is open on printer '%s'."),
1188 N_("Printer '%s' is low on paper."),
1189 N_("Printer '%s' is out of paper."),
1190 N_("Printer '%s' is currently off-line."),
1191 N_("Printer '%s' may not be connected."),
1192 N_("There is a problem on printer '%s'.")
1194 gboolean is_paused = FALSE;
1195 gboolean is_accepting_jobs = TRUE;
1196 gboolean default_printer = FALSE;
1197 gboolean got_printer_type = FALSE;
1199 /* Skip leading attributes until we hit a printer...
1201 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1207 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1209 if (strcmp (attr->name, "printer-name") == 0 &&
1210 attr->value_tag == IPP_TAG_NAME)
1211 printer_name = attr->values[0].string.text;
1212 else if (strcmp (attr->name, "printer-uri-supported") == 0 &&
1213 attr->value_tag == IPP_TAG_URI)
1214 printer_uri = attr->values[0].string.text;
1215 else if (strcmp (attr->name, "member-uris") == 0 &&
1216 attr->value_tag == IPP_TAG_URI)
1217 member_uris = attr->values[0].string.text;
1218 else if (strcmp (attr->name, "printer-location") == 0)
1219 location = attr->values[0].string.text;
1220 else if (strcmp (attr->name, "printer-info") == 0)
1221 description = attr->values[0].string.text;
1222 else if (strcmp (attr->name, "printer-state-message") == 0)
1223 state_msg = attr->values[0].string.text;
1224 else if (strcmp (attr->name, "printer-state-reasons") == 0)
1225 /* Store most important reason to reason_msg and set
1226 its importance at printer_state_reason_level */
1228 for (i = 0; i < attr->num_values; i++)
1230 if (strcmp (attr->values[i].string.text, "none") != 0)
1232 /* Sets is_paused flag for paused printer. */
1233 if (strcmp (attr->values[i].string.text, "paused") == 0)
1238 interested_in = FALSE;
1239 for (j = 0; j < G_N_ELEMENTS (reasons); j++)
1240 if (strncmp (attr->values[i].string.text, reasons[j], strlen (reasons[j])) == 0)
1242 interested_in = TRUE;
1248 if (g_str_has_suffix (attr->values[i].string.text, "-report"))
1250 if (printer_state_reason_level <= 1)
1252 reason_msg = attr->values[i].string.text;
1253 printer_state_reason_level = 1;
1256 else if (g_str_has_suffix (attr->values[i].string.text, "-warning"))
1258 if (printer_state_reason_level <= 2)
1260 reason_msg = attr->values[i].string.text;
1261 printer_state_reason_level = 2;
1264 else /* It is error in the case of no suffix. */
1266 reason_msg = attr->values[i].string.text;
1267 printer_state_reason_level = 3;
1273 else if (strcmp (attr->name, "printer-state") == 0)
1274 state = attr->values[0].integer;
1275 else if (strcmp (attr->name, "queued-job-count") == 0)
1276 job_count = attr->values[0].integer;
1277 else if (strcmp (attr->name, "printer-is-accepting-jobs") == 0)
1279 if (attr->values[0].boolean == 1)
1280 is_accepting_jobs = TRUE;
1282 is_accepting_jobs = FALSE;
1284 else if (strcmp (attr->name, "job-sheets-supported") == 0)
1286 if (cups_backend->covers == NULL)
1288 cups_backend->number_of_covers = attr->num_values;
1289 cups_backend->covers = g_new (char *, cups_backend->number_of_covers + 1);
1291 for (i = 0; i < cups_backend->number_of_covers; i++)
1292 cups_backend->covers[i] = g_strdup (attr->values[i].string.text);
1294 cups_backend->covers[cups_backend->number_of_covers] = NULL;
1297 else if (strcmp (attr->name, "job-sheets-default") == 0)
1299 if ( (cups_backend->default_cover_before == NULL) && (cups_backend->default_cover_after == NULL))
1301 if (attr->num_values == 2)
1303 cups_backend->default_cover_before = g_strdup (attr->values[0].string.text);
1304 cups_backend->default_cover_after = g_strdup (attr->values[1].string.text);
1308 else if (strcmp (attr->name, "printer-type") == 0)
1310 got_printer_type = TRUE;
1311 if (attr->values[0].integer & 0x00020000)
1312 default_printer = TRUE;
1314 default_printer = FALSE;
1319 g_print ("CUPS Backend: Attribute %s ignored", attr->name));
1325 if (printer_name == NULL ||
1326 (printer_uri == NULL && member_uris == NULL))
1334 if (got_printer_type)
1336 if (default_printer && !cups_backend->got_default_printer)
1338 cups_backend->got_default_printer = TRUE;
1339 cups_backend->default_printer = g_strdup (printer_name);
1344 if (!cups_backend->got_default_printer)
1345 cups_get_default_printer (cups_backend);
1348 /* remove name from checklist if it was found */
1349 node = g_list_find_custom (removed_printer_checklist, printer_name, (GCompareFunc) find_printer);
1350 removed_printer_checklist = g_list_delete_link (removed_printer_checklist, node);
1352 printer = gtk_print_backend_find_printer (backend, printer_name);
1355 GtkPrinterCups *cups_printer;
1356 char uri[HTTP_MAX_URI]; /* Printer URI */
1357 char method[HTTP_MAX_URI]; /* Method/scheme name */
1358 char username[HTTP_MAX_URI]; /* Username:password */
1359 char hostname[HTTP_MAX_URI]; /* Hostname */
1360 char resource[HTTP_MAX_URI]; /* Resource name */
1361 int port; /* Port number */
1362 char *cups_server; /* CUPS server */
1364 list_has_changed = TRUE;
1365 cups_printer = gtk_printer_cups_new (printer_name, backend);
1367 cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
1369 /* Check to see if we are looking at a class */
1372 cups_printer->printer_uri = g_strdup (member_uris);
1373 /* TODO if member_uris is a class we need to recursivly find a printer */
1375 g_print ("CUPS Backend: Found class with printer %s\n", member_uris));
1379 cups_printer->printer_uri = g_strdup (printer_uri);
1381 g_print ("CUPS Backend: Found printer %s\n", printer_uri));
1384 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
1385 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
1386 method, sizeof (method),
1387 username, sizeof (username),
1388 hostname, sizeof (hostname),
1390 resource, sizeof (resource));
1393 httpSeparate (cups_printer->printer_uri,
1401 if (strncmp (resource, "/printers/", 10) == 0)
1403 cups_printer->ppd_name = g_strdup (resource + 10);
1405 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
1408 gethostname (uri, sizeof (uri));
1409 cups_server = g_strdup (cupsServer());
1411 if (strcasecmp (uri, hostname) == 0)
1412 strcpy (hostname, "localhost");
1414 /* if the cups server is local and listening at a unix domain socket
1415 * then use the socket connection
1417 if ((strstr (hostname, "localhost") != NULL) &&
1418 (cups_server[0] == '/'))
1419 strcpy (hostname, cups_server);
1421 g_free (cups_server);
1423 cups_printer->hostname = g_strdup (hostname);
1424 cups_printer->port = port;
1426 printer = GTK_PRINTER (cups_printer);
1428 if (cups_backend->default_printer != NULL &&
1429 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
1430 gtk_printer_set_is_default (printer, TRUE);
1433 gtk_print_backend_add_printer (backend, printer);
1436 g_object_ref (printer);
1438 gtk_printer_set_is_paused (printer, is_paused);
1439 gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
1441 if (!gtk_printer_is_active (printer))
1443 gtk_printer_set_is_active (printer, TRUE);
1444 gtk_printer_set_is_new (printer, TRUE);
1445 list_has_changed = TRUE;
1448 if (gtk_printer_is_new (printer))
1450 g_signal_emit_by_name (backend, "printer-added", printer);
1452 gtk_printer_set_is_new (printer, FALSE);
1456 /* Getting printer info with separate requests overwhelms cups
1457 * when the printer list has more than a handful of printers.
1459 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
1462 GTK_PRINTER_CUPS (printer)->state = state;
1463 status_changed = gtk_printer_set_job_count (printer, job_count);
1464 status_changed |= gtk_printer_set_location (printer, location);
1465 status_changed |= gtk_printer_set_description (printer, description);
1467 if (state_msg != NULL && strlen (state_msg) == 0)
1469 if (is_paused && !is_accepting_jobs)
1470 /* Translators: this is a printer status. */
1471 tmp_msg2 = g_strdup ( N_("Paused ; Rejecting Jobs"));
1472 if (is_paused && is_accepting_jobs)
1473 /* Translators: this is a printer status. */
1474 tmp_msg2 = g_strdup ( N_("Paused"));
1475 if (!is_paused && !is_accepting_jobs)
1476 /* Translators: this is a printer status. */
1477 tmp_msg2 = g_strdup ( N_("Rejecting Jobs"));
1479 if (tmp_msg2 != NULL)
1480 state_msg = tmp_msg2;
1483 /* Set description of the reason and combine it with printer-state-message. */
1484 if ( (reason_msg != NULL))
1486 for (i = 0; i < G_N_ELEMENTS (reasons); i++)
1488 if (strncmp (reason_msg, reasons[i], strlen (reasons[i])) == 0)
1490 reason_msg_desc = g_strdup_printf (reasons_descs[i], printer_name);
1497 printer_state_reason_level = 0;
1499 if (printer_state_reason_level >= 2)
1501 if (strlen (state_msg) == 0)
1502 state_msg = reason_msg_desc;
1505 tmp_msg = g_strjoin (" ; ", state_msg, reason_msg_desc, NULL);
1506 state_msg = tmp_msg;
1511 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1512 status_changed |= gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
1514 if (tmp_msg != NULL)
1517 if (tmp_msg2 != NULL)
1520 if (reason_msg_desc != NULL)
1521 g_free (reason_msg_desc);
1523 /* Set printer icon according to importance
1524 (none, report, warning, error - report is omitted). */
1525 if (printer_state_reason_level == 3)
1526 gtk_printer_set_icon_name (printer, "gtk-print-error");
1527 else if (printer_state_reason_level == 2)
1528 gtk_printer_set_icon_name (printer, "gtk-print-warning");
1529 else if (gtk_printer_is_paused (printer))
1530 gtk_printer_set_icon_name (printer, "gtk-print-paused");
1532 gtk_printer_set_icon_name (printer, "gtk-print");
1535 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1536 "printer-status-changed", printer);
1538 /* The ref is held by GtkPrintBackend, in add_printer() */
1539 g_object_unref (printer);
1545 /* look at the removed printers checklist and mark any printer
1546 as inactive if it is in the list, emitting a printer_removed signal */
1547 if (removed_printer_checklist != NULL)
1549 g_list_foreach (removed_printer_checklist, (GFunc) mark_printer_inactive, backend);
1550 g_list_free (removed_printer_checklist);
1551 list_has_changed = TRUE;
1555 if (list_has_changed)
1556 g_signal_emit_by_name (backend, "printer-list-changed");
1558 gtk_print_backend_set_list_done (backend);
1560 GDK_THREADS_LEAVE ();
1564 update_backend_status (GtkPrintBackendCups *cups_backend,
1565 GtkCupsConnectionState state)
1569 case GTK_CUPS_CONNECTION_NOT_AVAILABLE:
1570 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_UNAVAILABLE, NULL);
1572 case GTK_CUPS_CONNECTION_AVAILABLE:
1573 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_OK, NULL);
1580 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
1582 GtkCupsConnectionState state;
1583 GtkCupsRequest *request;
1584 static const char * const pattrs[] = /* Attributes we're interested in */
1587 "printer-uri-supported",
1591 "printer-state-message",
1592 "printer-state-reasons",
1595 "printer-is-accepting-jobs",
1596 "job-sheets-supported",
1597 "job-sheets-default",
1601 if (cups_backend->list_printers_pending)
1604 state = gtk_cups_connection_test_get_state (cups_backend->cups_connection_test);
1605 update_backend_status (cups_backend, state);
1607 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
1610 cups_backend->list_printers_pending = TRUE;
1612 request = gtk_cups_request_new (NULL,
1619 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1620 "requested-attributes", G_N_ELEMENTS (pattrs),
1623 cups_request_execute (cups_backend,
1625 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
1633 cups_get_printer_list (GtkPrintBackend *backend)
1635 GtkPrintBackendCups *cups_backend;
1637 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
1639 if (cups_backend->cups_connection_test == NULL)
1640 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
1642 if (cups_backend->list_printers_poll == 0)
1644 if (cups_request_printer_list (cups_backend))
1645 cups_backend->list_printers_poll = gdk_threads_add_timeout_seconds (3,
1646 (GSourceFunc) cups_request_printer_list,
1652 GtkPrinterCups *printer;
1658 get_ppd_data_free (GetPPDData *data)
1661 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1662 httpClose (data->http);
1663 g_io_channel_unref (data->ppd_io);
1664 g_object_unref (data->printer);
1669 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
1670 GtkCupsResult *result,
1674 GtkPrinter *printer;
1676 GDK_THREADS_ENTER ();
1679 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1681 printer = GTK_PRINTER (data->printer);
1682 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
1684 if (gtk_cups_result_is_error (result))
1686 gboolean success = FALSE;
1688 /* if we get a 404 then it is just a raw printer without a ppd
1690 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
1691 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
1693 gtk_printer_set_has_details (printer, TRUE);
1697 g_signal_emit_by_name (printer, "details-acquired", success);
1701 response = gtk_cups_result_get_response (result);
1703 /* let ppdOpenFd take over the ownership of the open file */
1704 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
1705 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
1707 ppdMarkDefaults (data->printer->ppd_file);
1709 gtk_printer_set_has_details (printer, TRUE);
1710 g_signal_emit_by_name (printer, "details-acquired", TRUE);
1713 GDK_THREADS_LEAVE ();
1717 cups_request_ppd (GtkPrinter *printer)
1720 GtkPrintBackend *print_backend;
1721 GtkPrinterCups *cups_printer;
1722 GtkCupsRequest *request;
1729 cups_printer = GTK_PRINTER_CUPS (printer);
1734 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1736 http = httpConnectEncrypt (cups_printer->hostname,
1740 data = g_new0 (GetPPDData, 1);
1742 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
1746 #ifdef G_ENABLE_DEBUG
1747 /* If we are debugging printing don't delete the tmp files */
1748 if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
1749 unlink (ppd_filename);
1751 unlink (ppd_filename);
1752 #endif /* G_ENABLE_DEBUG */
1757 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
1759 g_error_free (error);
1761 g_free (ppd_filename);
1764 g_signal_emit_by_name (printer, "details-acquired", FALSE);
1769 fchmod (fd, S_IRUSR | S_IWUSR);
1770 data->ppd_io = g_io_channel_unix_new (fd);
1771 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
1772 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
1774 data->printer = g_object_ref (printer);
1776 resource = g_strdup_printf ("/printers/%s.ppd",
1777 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
1779 request = gtk_cups_request_new (data->http,
1783 cups_printer->hostname,
1787 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
1790 g_free (ppd_filename);
1792 cups_printer->reading_ppd = TRUE;
1794 print_backend = gtk_printer_get_backend (printer);
1796 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
1798 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
1800 (GDestroyNotify)get_ppd_data_free);
1803 /* Ordering matters for default preference */
1804 static const char *lpoptions_locations[] = {
1805 "/etc/cups/lpoptions",
1811 cups_parse_user_default_printer (const char *filename,
1812 char **printer_name)
1815 char line[1024], *lineptr, *defname = NULL;
1817 if ((fp = g_fopen (filename, "r")) == NULL)
1820 while (fgets (line, sizeof (line), fp) != NULL)
1822 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
1826 while (isspace (*lineptr))
1833 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
1838 if (*printer_name != NULL)
1839 g_free (*printer_name);
1841 *printer_name = g_strdup (defname);
1848 cups_get_user_default_printer (char **printer_name)
1852 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1854 if (g_path_is_absolute (lpoptions_locations[i]))
1856 cups_parse_user_default_printer (lpoptions_locations[i],
1863 filename = g_build_filename (g_get_home_dir (),
1864 lpoptions_locations[i], NULL);
1865 cups_parse_user_default_printer (filename, printer_name);
1872 cups_parse_user_options (const char *filename,
1873 const char *printer_name,
1875 cups_option_t **options)
1878 gchar line[1024], *lineptr, *name;
1880 if ((fp = g_fopen (filename, "r")) == NULL)
1883 while (fgets (line, sizeof (line), fp) != NULL)
1885 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
1887 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
1892 /* Skip leading whitespace */
1893 while (isspace (*lineptr))
1899 /* NUL-terminate the name, stripping the instance name */
1901 while (!isspace (*lineptr) && *lineptr)
1903 if (*lineptr == '/')
1913 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
1916 /* We found our printer, parse the options */
1917 num_options = cupsParseOptions (lineptr, num_options, options);
1926 cups_get_user_options (const char *printer_name,
1928 cups_option_t **options)
1932 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
1934 if (g_path_is_absolute (lpoptions_locations[i]))
1936 num_options = cups_parse_user_options (lpoptions_locations[i],
1945 filename = g_build_filename (g_get_home_dir (),
1946 lpoptions_locations[i], NULL);
1947 num_options = cups_parse_user_options (filename, printer_name,
1948 num_options, options);
1956 /* This function requests default printer from a CUPS server in regular intervals.
1957 * In the case of unreachable CUPS server the request is repeated later.
1958 * The default printer is not requested in the case of previous success.
1961 cups_get_default_printer (GtkPrintBackendCups *backend)
1963 GtkPrintBackendCups *cups_backend;
1965 cups_backend = backend;
1967 if (cups_backend->cups_connection_test == NULL)
1968 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
1970 if (cups_backend->default_printer_poll == 0)
1972 if (cups_request_default_printer (cups_backend))
1973 cups_backend->default_printer_poll = gdk_threads_add_timeout (500,
1974 (GSourceFunc) cups_request_default_printer,
1979 /* This function gets default printer from local settings.*/
1981 cups_get_local_default_printer (GtkPrintBackendCups *backend)
1986 if ((str = g_getenv ("LPDEST")) != NULL)
1988 backend->default_printer = g_strdup (str);
1989 backend->got_default_printer = TRUE;
1992 else if ((str = g_getenv ("PRINTER")) != NULL &&
1993 strcmp (str, "lp") != 0)
1995 backend->default_printer = g_strdup (str);
1996 backend->got_default_printer = TRUE;
2000 /* Figure out user setting for default printer */
2001 cups_get_user_default_printer (&name);
2004 backend->default_printer = name;
2005 backend->got_default_printer = TRUE;
2011 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
2012 GtkCupsResult *result,
2016 ipp_attribute_t *attr;
2017 GtkPrinter *printer;
2019 GDK_THREADS_ENTER ();
2021 response = gtk_cups_result_get_response (result);
2023 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
2024 print_backend->default_printer = g_strdup (attr->values[0].string.text);
2026 print_backend->got_default_printer = TRUE;
2028 if (print_backend->default_printer != NULL)
2030 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (print_backend), print_backend->default_printer);
2031 if (printer != NULL)
2033 gtk_printer_set_is_default (printer, TRUE);
2034 g_signal_emit_by_name (GTK_PRINT_BACKEND (print_backend), "printer-status-changed", printer);
2038 /* Make sure to kick off get_printers if we are polling it,
2039 * as we could have blocked this reading the default printer
2041 if (print_backend->list_printers_poll != 0)
2042 cups_request_printer_list (print_backend);
2044 GDK_THREADS_LEAVE ();
2048 cups_request_default_printer (GtkPrintBackendCups *print_backend)
2050 GtkCupsConnectionState state;
2051 GtkCupsRequest *request;
2053 state = gtk_cups_connection_test_get_state (print_backend->cups_connection_test);
2054 update_backend_status (print_backend, state);
2056 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2059 request = gtk_cups_request_new (NULL,
2066 cups_request_execute (print_backend,
2068 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
2069 g_object_ref (print_backend),
2076 cups_printer_request_details (GtkPrinter *printer)
2078 GtkPrinterCups *cups_printer;
2080 cups_printer = GTK_PRINTER_CUPS (printer);
2081 if (!cups_printer->reading_ppd &&
2082 gtk_printer_cups_get_ppd (cups_printer) == NULL)
2083 cups_request_ppd (printer);
2087 ppd_text_to_utf8 (ppd_file_t *ppd_file,
2090 const char *encoding = NULL;
2093 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
2095 return g_strdup (text);
2097 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
2099 encoding = "ISO-8859-1";
2101 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
2103 encoding = "ISO-8859-2";
2105 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
2107 encoding = "ISO-8859-5";
2109 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
2111 encoding = "SHIFT-JIS";
2113 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
2115 encoding = "MACINTOSH";
2117 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
2119 encoding = "WINDOWS-1252";
2123 /* Fallback, try iso-8859-1... */
2124 encoding = "ISO-8859-1";
2127 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
2132 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
2133 res = g_strdup ("???");
2139 /* TODO: Add more translations for common settings here */
2141 static const struct {
2142 const char *keyword;
2143 const char *translation;
2144 } cups_option_translations[] = {
2145 { "Duplex", N_("Two Sided") },
2146 { "MediaType", N_("Paper Type") },
2147 { "InputSlot", N_("Paper Source") },
2148 { "OutputBin", N_("Output Tray") },
2149 { "Resolution", N_("Resolution") },
2150 { "PreFilter", N_("GhostScript pre-filtering") },
2154 static const struct {
2155 const char *keyword;
2157 const char *translation;
2158 } cups_choice_translations[] = {
2159 { "Duplex", "None", N_("One Sided") },
2160 /* Translators: this is an option of "Two Sided" */
2161 { "Duplex", "DuplexNoTumble", N_("Long Edge (Standard)") },
2162 /* Translators: this is an option of "Two Sided" */
2163 { "Duplex", "DuplexTumble", N_("Short Edge (Flip)") },
2164 /* Translators: this is an option of "Paper Source" */
2165 { "InputSlot", "Auto", N_("Auto Select") },
2166 /* Translators: this is an option of "Paper Source" */
2167 { "InputSlot", "AutoSelect", N_("Auto Select") },
2168 /* Translators: this is an option of "Paper Source" */
2169 { "InputSlot", "Default", N_("Printer Default") },
2170 /* Translators: this is an option of "Paper Source" */
2171 { "InputSlot", "None", N_("Printer Default") },
2172 /* Translators: this is an option of "Paper Source" */
2173 { "InputSlot", "PrinterDefault", N_("Printer Default") },
2174 /* Translators: this is an option of "Paper Source" */
2175 { "InputSlot", "Unspecified", N_("Auto Select") },
2176 /* Translators: this is an option of "Resolution" */
2177 { "Resolution", "default", N_("Printer Default") },
2178 /* Translators: this is an option of "GhostScript" */
2179 { "PreFilter", "EmbedFonts", N_("Embed GhostScript fonts only") },
2180 /* Translators: this is an option of "GhostScript" */
2181 { "PreFilter", "Level1", N_("Convert to PS level 1") },
2182 /* Translators: this is an option of "GhostScript" */
2183 { "PreFilter", "Level2", N_("Convert to PS level 2") },
2184 /* Translators: this is an option of "GhostScript" */
2185 { "PreFilter", "No", N_("No pre-filtering") },
2188 static const struct {
2190 const char *translation;
2191 } cups_group_translations[] = {
2192 { "Miscellaneous", N_("Miscellaneous") },
2195 static const struct {
2196 const char *ppd_keyword;
2198 } option_names[] = {
2199 {"Duplex", "gtk-duplex" },
2200 {"MediaType", "gtk-paper-type"},
2201 {"InputSlot", "gtk-paper-source"},
2202 {"OutputBin", "gtk-output-tray"},
2205 /* keep sorted when changing */
2206 static const char *color_option_whitelist[] = {
2207 "BRColorEnhancement",
2213 "BlackSubstitution",
2221 "RPSBlackOverPrint",
2225 /* keep sorted when changing */
2226 static const char *color_group_whitelist[] = {
2233 "HPColorOptionsPanel",
2236 /* keep sorted when changing */
2237 static const char *image_quality_option_whitelist[] = {
2239 "BRHalfTonePattern",
2249 "HPGraphicsHalftone",
2264 /* keep sorted when changing */
2265 static const char *image_quality_group_whitelist[] = {
2272 /* keep sorted when changing */
2273 static const char * finishing_option_whitelist[] = {
2286 "StapleOrientation",
2292 /* keep sorted when changing */
2293 static const char *finishing_group_whitelist[] = {
2302 /* keep sorted when changing */
2303 static const char *cups_option_blacklist[] = {
2312 get_option_text (ppd_file_t *ppd_file,
2313 ppd_option_t *option)
2318 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
2320 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
2321 return g_strdup (_(cups_option_translations[i].translation));
2324 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
2326 /* Some ppd files have spaces in the text before the colon */
2333 get_choice_text (ppd_file_t *ppd_file,
2334 ppd_choice_t *choice)
2337 ppd_option_t *option = choice->option;
2338 const char *keyword = option->keyword;
2340 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
2342 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
2343 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
2344 return g_strdup (_(cups_choice_translations[i].translation));
2346 return ppd_text_to_utf8 (ppd_file, choice->text);
2350 group_has_option (ppd_group_t *group,
2351 ppd_option_t *option)
2358 if (group->num_options > 0 &&
2359 option >= group->options && option < group->options + group->num_options)
2362 for (i = 0; i < group->num_subgroups; i++)
2364 if (group_has_option (&group->subgroups[i],option))
2371 set_option_off (GtkPrinterOption *option)
2373 /* Any of these will do, _set only applies the value
2374 * if its allowed of the option */
2375 gtk_printer_option_set (option, "False");
2376 gtk_printer_option_set (option, "Off");
2377 gtk_printer_option_set (option, "None");
2381 value_is_off (const char *value)
2383 return (strcasecmp (value, "None") == 0 ||
2384 strcasecmp (value, "Off") == 0 ||
2385 strcasecmp (value, "False") == 0);
2389 ppd_group_name (ppd_group_t *group)
2391 #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)
2399 available_choices (ppd_file_t *ppd,
2400 ppd_option_t *option,
2401 ppd_choice_t ***available,
2402 gboolean keep_if_only_one_option)
2404 ppd_option_t *other_option;
2407 ppd_const_t *constraint;
2408 const char *choice, *other_choice;
2409 ppd_option_t *option1, *option2;
2410 ppd_group_t *installed_options;
2412 gboolean all_default;
2418 conflicts = g_new0 (char, option->num_choices);
2420 installed_options = NULL;
2421 for (i = 0; i < ppd->num_groups; i++)
2425 name = ppd_group_name (&ppd->groups[i]);
2426 if (strcmp (name, "InstallableOptions") == 0)
2428 installed_options = &ppd->groups[i];
2433 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
2435 option1 = ppdFindOption (ppd, constraint->option1);
2436 if (option1 == NULL)
2439 option2 = ppdFindOption (ppd, constraint->option2);
2440 if (option2 == NULL)
2443 if (option == option1)
2445 choice = constraint->choice1;
2446 other_option = option2;
2447 other_choice = constraint->choice2;
2449 else if (option == option2)
2451 choice = constraint->choice2;
2452 other_option = option1;
2453 other_choice = constraint->choice1;
2458 /* We only care of conflicts with installed_options and
2460 if (!group_has_option (installed_options, other_option) &&
2461 (strcmp (other_option->keyword, "PageSize") != 0))
2464 if (*other_choice == 0)
2466 /* Conflict only if the installed option is not off */
2467 if (value_is_off (other_option->defchoice))
2470 /* Conflict if the installed option has the specified default */
2471 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
2476 /* Conflict with all non-off choices */
2477 for (j = 0; j < option->num_choices; j++)
2479 if (!value_is_off (option->choices[j].choice))
2485 for (j = 0; j < option->num_choices; j++)
2487 if (strcasecmp (option->choices[j].choice, choice) == 0)
2495 for (j = 0; j < option->num_choices; j++)
2499 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
2500 all_default = FALSE;
2503 if ((all_default && !keep_if_only_one_option) ||
2504 (num_conflicts == option->num_choices))
2511 /* Some ppds don't have a "use printer default" option for
2512 * InputSlot. This means you always have to select a particular slot,
2513 * and you can't auto-pick source based on the paper size. To support
2514 * this we always add an auto option if there isn't one already. If
2515 * the user chooses the generated option we don't send any InputSlot
2516 * value when printing. The way we detect existing auto-cases is based
2517 * on feedback from Michael Sweet of cups fame.
2520 if (strcmp (option->keyword, "InputSlot") == 0)
2522 gboolean found_auto = FALSE;
2523 for (j = 0; j < option->num_choices; j++)
2527 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
2528 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
2529 strcmp (option->choices[j].choice, "Default") == 0 ||
2530 strcmp (option->choices[j].choice, "None") == 0 ||
2531 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
2532 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
2533 option->choices[j].code == NULL ||
2534 option->choices[j].code[0] == 0)
2548 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
2551 for (j = 0; j < option->num_choices; j++)
2554 (*available)[i++] = &option->choices[j];
2558 (*available)[i++] = NULL;
2563 return option->num_choices - num_conflicts + add_auto;
2566 static GtkPrinterOption *
2567 create_pickone_option (ppd_file_t *ppd_file,
2568 ppd_option_t *ppd_option,
2569 const gchar *gtk_name)
2571 GtkPrinterOption *option;
2572 ppd_choice_t **available;
2576 #ifdef HAVE_CUPS_API_1_2
2577 ppd_coption_t *coption;
2580 g_assert (ppd_option->ui == PPD_UI_PICKONE);
2584 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2588 /* right now only support one parameter per custom option
2589 * if more than one print warning and only offer the default choices
2592 label = get_option_text (ppd_file, ppd_option);
2594 #ifdef HAVE_CUPS_API_1_2
2595 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
2599 ppd_cparam_t *cparam;
2601 cparam = ppdFirstCustomParam (coption);
2603 if (ppdNextCustomParam (coption) == NULL)
2605 switch (cparam->type)
2607 case PPD_CUSTOM_INT:
2608 option = gtk_printer_option_new (gtk_name, label,
2609 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
2611 case PPD_CUSTOM_PASSCODE:
2612 option = gtk_printer_option_new (gtk_name, label,
2613 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
2615 case PPD_CUSTOM_PASSWORD:
2616 option = gtk_printer_option_new (gtk_name, label,
2617 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
2619 case PPD_CUSTOM_REAL:
2620 option = gtk_printer_option_new (gtk_name, label,
2621 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
2623 case PPD_CUSTOM_STRING:
2624 option = gtk_printer_option_new (gtk_name, label,
2625 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
2627 #ifdef PRINT_IGNORED_OPTIONS
2628 case PPD_CUSTOM_POINTS:
2629 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
2631 case PPD_CUSTOM_CURVE:
2632 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
2634 case PPD_CUSTOM_INVCURVE:
2635 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
2642 #ifdef PRINT_IGNORED_OPTIONS
2644 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
2647 #endif /* HAVE_CUPS_API_1_2 */
2650 option = gtk_printer_option_new (gtk_name, label,
2651 GTK_PRINTER_OPTION_TYPE_PICKONE);
2654 gtk_printer_option_allocate_choices (option, n_choices);
2655 for (i = 0; i < n_choices; i++)
2657 if (available[i] == NULL)
2659 /* This was auto-added */
2660 option->choices[i] = g_strdup ("gtk-ignore-value");
2661 option->choices_display[i] = g_strdup (_("Printer Default"));
2665 option->choices[i] = g_strdup (available[i]->choice);
2666 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
2669 gtk_printer_option_set (option, ppd_option->defchoice);
2671 #ifdef PRINT_IGNORED_OPTIONS
2673 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
2680 static GtkPrinterOption *
2681 create_boolean_option (ppd_file_t *ppd_file,
2682 ppd_option_t *ppd_option,
2683 const gchar *gtk_name)
2685 GtkPrinterOption *option;
2686 ppd_choice_t **available;
2690 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
2694 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2697 label = get_option_text (ppd_file, ppd_option);
2698 option = gtk_printer_option_new (gtk_name, label,
2699 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
2702 gtk_printer_option_allocate_choices (option, 2);
2703 option->choices[0] = g_strdup ("True");
2704 option->choices_display[0] = g_strdup ("True");
2705 option->choices[1] = g_strdup ("False");
2706 option->choices_display[1] = g_strdup ("False");
2708 gtk_printer_option_set (option, ppd_option->defchoice);
2710 #ifdef PRINT_IGNORED_OPTIONS
2712 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
2720 get_option_name (const gchar *keyword)
2724 for (i = 0; i < G_N_ELEMENTS (option_names); i++)
2725 if (strcmp (option_names[i].ppd_keyword, keyword) == 0)
2726 return g_strdup (option_names[i].name);
2728 return g_strdup_printf ("cups-%s", keyword);
2732 strptr_cmp (const void *a,
2735 char **aa = (char **)a;
2736 char **bb = (char **)b;
2737 return strcmp (*aa, *bb);
2742 string_in_table (gchar *str,
2743 const gchar *table[],
2746 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
2749 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
2752 handle_option (GtkPrinterOptionSet *set,
2753 ppd_file_t *ppd_file,
2754 ppd_option_t *ppd_option,
2755 ppd_group_t *toplevel_group,
2756 GtkPrintSettings *settings)
2758 GtkPrinterOption *option;
2762 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
2765 name = get_option_name (ppd_option->keyword);
2768 if (ppd_option->ui == PPD_UI_PICKONE)
2770 option = create_pickone_option (ppd_file, ppd_option, name);
2772 else if (ppd_option->ui == PPD_UI_BOOLEAN)
2774 option = create_boolean_option (ppd_file, ppd_option, name);
2776 #ifdef PRINT_IGNORED_OPTIONS
2778 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
2785 name = ppd_group_name (toplevel_group);
2786 if (STRING_IN_TABLE (name,
2787 color_group_whitelist) ||
2788 STRING_IN_TABLE (ppd_option->keyword,
2789 color_option_whitelist))
2791 option->group = g_strdup ("ColorPage");
2793 else if (STRING_IN_TABLE (name,
2794 image_quality_group_whitelist) ||
2795 STRING_IN_TABLE (ppd_option->keyword,
2796 image_quality_option_whitelist))
2798 option->group = g_strdup ("ImageQualityPage");
2800 else if (STRING_IN_TABLE (name,
2801 finishing_group_whitelist) ||
2802 STRING_IN_TABLE (ppd_option->keyword,
2803 finishing_option_whitelist))
2805 option->group = g_strdup ("FinishingPage");
2809 for (i = 0; i < G_N_ELEMENTS (cups_group_translations); i++)
2811 if (strcmp (cups_group_translations[i].name, toplevel_group->name) == 0)
2813 option->group = g_strdup (_(cups_group_translations[i].translation));
2818 if (i == G_N_ELEMENTS (cups_group_translations))
2819 option->group = g_strdup (toplevel_group->text);
2822 set_option_from_settings (option, settings);
2824 gtk_printer_option_set_add (set, option);
2831 handle_group (GtkPrinterOptionSet *set,
2832 ppd_file_t *ppd_file,
2834 ppd_group_t *toplevel_group,
2835 GtkPrintSettings *settings)
2840 /* Ignore installable options */
2841 name = ppd_group_name (toplevel_group);
2842 if (strcmp (name, "InstallableOptions") == 0)
2845 for (i = 0; i < group->num_options; i++)
2846 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
2848 for (i = 0; i < group->num_subgroups; i++)
2849 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
2853 static GtkPrinterOptionSet *
2854 cups_printer_get_options (GtkPrinter *printer,
2855 GtkPrintSettings *settings,
2856 GtkPageSetup *page_setup,
2857 GtkPrintCapabilities capabilities)
2859 GtkPrinterOptionSet *set;
2860 GtkPrinterOption *option;
2861 ppd_file_t *ppd_file;
2863 char *print_at[] = { "now", "at", "on-hold" };
2864 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
2865 char *prio[] = {"100", "80", "50", "30" };
2866 /* Translators: These strings name the possible values of the
2867 * job priority option in the print dialog
2869 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
2870 char *n_up_layout[] = { "lrtb", "lrbt", "rltb", "rlbt", "tblr", "tbrl", "btlr", "btrl" };
2871 /* Translators: These strings name the possible arrangements of
2872 * multiple pages on a sheet when printing
2874 char *n_up_layout_display[] = { N_("Left to right, top to bottom"), N_("Left to right, bottom to top"),
2875 N_("Right to left, top to bottom"), N_("Right to left, bottom to top"),
2876 N_("Top to bottom, left to right"), N_("Top to bottom, right to left"),
2877 N_("Bottom to top, left to right"), N_("Bottom to top, right to left") };
2880 cups_option_t *opts = NULL;
2881 GtkPrintBackendCups *backend;
2882 GtkTextDirection text_direction;
2885 set = gtk_printer_option_set_new ();
2887 /* Cups specific, non-ppd related settings */
2889 /* Translators, this string is used to label the pages-per-sheet option
2890 * in the print dialog
2892 option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2893 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
2895 gtk_printer_option_set (option, "1");
2896 set_option_from_settings (option, settings);
2897 gtk_printer_option_set_add (set, option);
2898 g_object_unref (option);
2900 if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
2902 for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
2903 n_up_layout_display[i] = _(n_up_layout_display[i]);
2905 /* Translators, this string is used to label the option in the print
2906 * dialog that controls in what order multiple pages are arranged
2908 option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2909 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
2910 n_up_layout, n_up_layout_display);
2912 text_direction = gtk_widget_get_default_direction ();
2913 if (text_direction == GTK_TEXT_DIR_LTR)
2914 gtk_printer_option_set (option, "lrtb");
2916 gtk_printer_option_set (option, "rltb");
2918 set_option_from_settings (option, settings);
2919 gtk_printer_option_set_add (set, option);
2920 g_object_unref (option);
2923 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
2924 prio_display[i] = _(prio_display[i]);
2926 /* Translators, this string is used to label the job priority option
2927 * in the print dialog
2929 option = gtk_printer_option_new ("gtk-job-prio", _("Job Priority"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2930 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
2931 prio, prio_display);
2932 gtk_printer_option_set (option, "50");
2933 set_option_from_settings (option, settings);
2934 gtk_printer_option_set_add (set, option);
2935 g_object_unref (option);
2937 /* Translators, this string is used to label the billing info entry
2938 * in the print dialog
2940 option = gtk_printer_option_new ("gtk-billing-info", _("Billing Info"), GTK_PRINTER_OPTION_TYPE_STRING);
2941 gtk_printer_option_set (option, "");
2942 set_option_from_settings (option, settings);
2943 gtk_printer_option_set_add (set, option);
2944 g_object_unref (option);
2946 backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
2948 if (backend != NULL)
2950 char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
2951 /* Translators, these strings are names for various 'standard' cover
2952 * pages that the printing system may support.
2954 char *cover_display_default[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
2955 char **cover = NULL;
2956 char **cover_display = NULL;
2957 char **cover_display_translated = NULL;
2958 gint num_of_covers = 0;
2962 num_of_covers = backend->number_of_covers;
2963 cover = g_new (char *, num_of_covers + 1);
2964 cover[num_of_covers] = NULL;
2965 cover_display = g_new (char *, num_of_covers + 1);
2966 cover_display[num_of_covers] = NULL;
2967 cover_display_translated = g_new (char *, num_of_covers + 1);
2968 cover_display_translated[num_of_covers] = NULL;
2970 for (i = 0; i < num_of_covers; i++)
2972 cover[i] = g_strdup (backend->covers[i]);
2974 for (j = 0; j < G_N_ELEMENTS (cover_default); j++)
2975 if (strcmp (cover_default[j], cover[i]) == 0)
2977 value = cover_display_default[j];
2980 cover_display[i] = (value != NULL) ? g_strdup (value) : g_strdup (backend->covers[i]);
2983 for (i = 0; i < num_of_covers; i++)
2984 cover_display_translated[i] = _(cover_display[i]);
2986 /* Translators, this is the label used for the option in the print
2987 * dialog that controls the front cover page.
2989 option = gtk_printer_option_new ("gtk-cover-before", _("Before"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2990 gtk_printer_option_choices_from_array (option, num_of_covers,
2991 cover, cover_display_translated);
2993 if (backend->default_cover_before != NULL)
2994 gtk_printer_option_set (option, backend->default_cover_before);
2996 gtk_printer_option_set (option, "none");
2997 set_option_from_settings (option, settings);
2998 gtk_printer_option_set_add (set, option);
2999 g_object_unref (option);
3001 /* Translators, this is the label used for the option in the print
3002 * dialog that controls the back cover page.
3004 option = gtk_printer_option_new ("gtk-cover-after", _("After"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3005 gtk_printer_option_choices_from_array (option, num_of_covers,
3006 cover, cover_display_translated);
3007 if (backend->default_cover_after != NULL)
3008 gtk_printer_option_set (option, backend->default_cover_after);
3010 gtk_printer_option_set (option, "none");
3011 set_option_from_settings (option, settings);
3012 gtk_printer_option_set_add (set, option);
3013 g_object_unref (option);
3016 g_strfreev (cover_display);
3017 g_free (cover_display_translated);
3020 /* Translators: this is the name of the option that controls when
3021 * a print job is printed. Possible values are 'now', a specified time,
3024 option = gtk_printer_option_new ("gtk-print-time", _("Print at"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3025 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
3026 print_at, print_at);
3027 gtk_printer_option_set (option, "now");
3028 set_option_from_settings (option, settings);
3029 gtk_printer_option_set_add (set, option);
3030 g_object_unref (option);
3032 /* Translators: this is the name of the option that allows the user
3033 * to specify a time when a print job will be printed.
3035 option = gtk_printer_option_new ("gtk-print-time-text", _("Print at time"), GTK_PRINTER_OPTION_TYPE_STRING);
3036 gtk_printer_option_set (option, "");
3037 set_option_from_settings (option, settings);
3038 gtk_printer_option_set_add (set, option);
3039 g_object_unref (option);
3041 /* Printer (ppd) specific settings */
3042 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3045 GtkPaperSize *paper_size;
3046 ppd_option_t *option;
3047 const gchar *ppd_name;
3049 ppdMarkDefaults (ppd_file);
3051 paper_size = gtk_page_setup_get_paper_size (page_setup);
3053 option = ppdFindOption (ppd_file, "PageSize");
3054 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
3057 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
3061 char width[G_ASCII_DTOSTR_BUF_SIZE];
3062 char height[G_ASCII_DTOSTR_BUF_SIZE];
3064 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3065 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3066 /* Translators: this format is used to display a custom paper
3067 * size. The two placeholders are replaced with the width and height
3068 * in points. E.g: "Custom 230.4x142.9"
3070 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
3071 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
3072 g_free (custom_name);
3075 for (i = 0; i < ppd_file->num_groups; i++)
3076 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
3079 /* Now honor the user set defaults for this printer */
3080 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
3082 for (i = 0; i < num_opts; i++)
3084 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
3087 name = get_option_name (opts[i].name);
3088 option = gtk_printer_option_set_lookup (set, name);
3090 gtk_printer_option_set (option, opts[i].value);
3094 cupsFreeOptions (num_opts, opts);
3101 mark_option_from_set (GtkPrinterOptionSet *set,
3102 ppd_file_t *ppd_file,
3103 ppd_option_t *ppd_option)
3105 GtkPrinterOption *option;
3106 char *name = get_option_name (ppd_option->keyword);
3108 option = gtk_printer_option_set_lookup (set, name);
3111 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
3118 mark_group_from_set (GtkPrinterOptionSet *set,
3119 ppd_file_t *ppd_file,
3124 for (i = 0; i < group->num_options; i++)
3125 mark_option_from_set (set, ppd_file, &group->options[i]);
3127 for (i = 0; i < group->num_subgroups; i++)
3128 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
3132 set_conflicts_from_option (GtkPrinterOptionSet *set,
3133 ppd_file_t *ppd_file,
3134 ppd_option_t *ppd_option)
3136 GtkPrinterOption *option;
3139 if (ppd_option->conflicted)
3141 name = get_option_name (ppd_option->keyword);
3142 option = gtk_printer_option_set_lookup (set, name);
3145 gtk_printer_option_set_has_conflict (option, TRUE);
3146 #ifdef PRINT_IGNORED_OPTIONS
3148 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
3156 set_conflicts_from_group (GtkPrinterOptionSet *set,
3157 ppd_file_t *ppd_file,
3162 for (i = 0; i < group->num_options; i++)
3163 set_conflicts_from_option (set, ppd_file, &group->options[i]);
3165 for (i = 0; i < group->num_subgroups; i++)
3166 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
3170 cups_printer_mark_conflicts (GtkPrinter *printer,
3171 GtkPrinterOptionSet *options)
3173 ppd_file_t *ppd_file;
3177 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3179 if (ppd_file == NULL)
3182 ppdMarkDefaults (ppd_file);
3184 for (i = 0; i < ppd_file->num_groups; i++)
3185 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
3187 num_conflicts = ppdConflicts (ppd_file);
3189 if (num_conflicts > 0)
3191 for (i = 0; i < ppd_file->num_groups; i++)
3192 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
3195 return num_conflicts > 0;
3199 GtkPrinter *printer;
3200 GtkPrinterOptionSet *options;
3201 GtkPrintSettings *settings;
3202 ppd_file_t *ppd_file;
3207 const char *standard;
3211 map_settings_to_option (GtkPrinterOption *option,
3212 const NameMapping table[],
3214 GtkPrintSettings *settings,
3215 const gchar *standard_name,
3216 const gchar *cups_name)
3220 const char *cups_value;
3221 const char *standard_value;
3223 /* If the cups-specific setting is set, always use that */
3224 name = g_strdup_printf ("cups-%s", cups_name);
3225 cups_value = gtk_print_settings_get (settings, name);
3228 if (cups_value != NULL)
3230 gtk_printer_option_set (option, cups_value);
3234 /* Otherwise we try to convert from the general setting */
3235 standard_value = gtk_print_settings_get (settings, standard_name);
3236 if (standard_value == NULL)
3239 for (i = 0; i < n_elements; i++)
3241 if (table[i].cups == NULL && table[i].standard == NULL)
3243 gtk_printer_option_set (option, standard_value);
3246 else if (table[i].cups == NULL &&
3247 strcmp (table[i].standard, standard_value) == 0)
3249 set_option_off (option);
3252 else if (strcmp (table[i].standard, standard_value) == 0)
3254 gtk_printer_option_set (option, table[i].cups);
3261 map_option_to_settings (const gchar *value,
3262 const NameMapping table[],
3264 GtkPrintSettings *settings,
3265 const gchar *standard_name,
3266 const gchar *cups_name)
3271 for (i = 0; i < n_elements; i++)
3273 if (table[i].cups == NULL && table[i].standard == NULL)
3275 gtk_print_settings_set (settings,
3280 else if (table[i].cups == NULL && table[i].standard != NULL)
3282 if (value_is_off (value))
3284 gtk_print_settings_set (settings,
3290 else if (strcmp (table[i].cups, value) == 0)
3292 gtk_print_settings_set (settings,
3299 /* Always set the corresponding cups-specific setting */
3300 name = g_strdup_printf ("cups-%s", cups_name);
3301 gtk_print_settings_set (settings, name, value);
3306 static const NameMapping paper_source_map[] = {
3307 { "Lower", "lower"},
3308 { "Middle", "middle"},
3309 { "Upper", "upper"},
3311 { "Envelope", "envelope"},
3312 { "Cassette", "cassette"},
3313 { "LargeCapacity", "large-capacity"},
3314 { "AnySmallFormat", "small-format"},
3315 { "AnyLargeFormat", "large-format"},
3319 static const NameMapping output_tray_map[] = {
3320 { "Upper", "upper"},
3321 { "Lower", "lower"},
3326 static const NameMapping duplex_map[] = {
3327 { "DuplexTumble", "vertical" },
3328 { "DuplexNoTumble", "horizontal" },
3332 static const NameMapping output_mode_map[] = {
3333 { "Standard", "normal" },
3334 { "Normal", "normal" },
3335 { "Draft", "draft" },
3336 { "Fast", "draft" },
3339 static const NameMapping media_type_map[] = {
3340 { "Transparency", "transparency"},
3341 { "Standard", "stationery"},
3345 static const NameMapping all_map[] = {
3351 set_option_from_settings (GtkPrinterOption *option,
3352 GtkPrintSettings *settings)
3354 const char *cups_value;
3357 if (settings == NULL)
3360 if (strcmp (option->name, "gtk-paper-source") == 0)
3361 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
3362 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3363 else if (strcmp (option->name, "gtk-output-tray") == 0)
3364 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
3365 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3366 else if (strcmp (option->name, "gtk-duplex") == 0)
3367 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
3368 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3369 else if (strcmp (option->name, "cups-OutputMode") == 0)
3370 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
3371 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3372 else if (strcmp (option->name, "cups-Resolution") == 0)
3374 cups_value = gtk_print_settings_get (settings, option->name);
3376 gtk_printer_option_set (option, cups_value);
3379 int res = gtk_print_settings_get_resolution (settings);
3380 int res_x = gtk_print_settings_get_resolution_x (settings);
3381 int res_y = gtk_print_settings_get_resolution_y (settings);
3385 value = g_strdup_printf ("%dx%ddpi", res_x, res_y);
3386 gtk_printer_option_set (option, value);
3391 value = g_strdup_printf ("%ddpi", res);
3392 gtk_printer_option_set (option, value);
3397 else if (strcmp (option->name, "gtk-paper-type") == 0)
3398 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
3399 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3400 else if (strcmp (option->name, "gtk-n-up") == 0)
3402 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
3403 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3405 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
3407 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
3408 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
3410 else if (strcmp (option->name, "gtk-billing-info") == 0)
3412 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
3414 gtk_printer_option_set (option, cups_value);
3416 else if (strcmp (option->name, "gtk-job-prio") == 0)
3418 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
3420 gtk_printer_option_set (option, cups_value);
3422 else if (strcmp (option->name, "gtk-cover-before") == 0)
3424 cups_value = gtk_print_settings_get (settings, "cover-before");
3426 gtk_printer_option_set (option, cups_value);
3428 else if (strcmp (option->name, "gtk-cover-after") == 0)
3430 cups_value = gtk_print_settings_get (settings, "cover-after");
3432 gtk_printer_option_set (option, cups_value);
3434 else if (strcmp (option->name, "gtk-print-time") == 0)
3436 cups_value = gtk_print_settings_get (settings, "print-at");
3438 gtk_printer_option_set (option, cups_value);
3440 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3442 cups_value = gtk_print_settings_get (settings, "print-at-time");
3444 gtk_printer_option_set (option, cups_value);
3446 else if (g_str_has_prefix (option->name, "cups-"))
3448 cups_value = gtk_print_settings_get (settings, option->name);
3450 gtk_printer_option_set (option, cups_value);
3455 foreach_option_get_settings (GtkPrinterOption *option,
3458 struct OptionData *data = user_data;
3459 GtkPrintSettings *settings = data->settings;
3462 value = option->value;
3464 if (strcmp (option->name, "gtk-paper-source") == 0)
3465 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
3466 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3467 else if (strcmp (option->name, "gtk-output-tray") == 0)
3468 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
3469 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3470 else if (strcmp (option->name, "gtk-duplex") == 0)
3471 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
3472 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3473 else if (strcmp (option->name, "cups-OutputMode") == 0)
3474 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
3475 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3476 else if (strcmp (option->name, "cups-Resolution") == 0)
3478 int res, res_x, res_y;
3480 if (sscanf (value, "%dx%ddpi", &res_x, &res_y) == 2)
3482 if (res_x != 0 && res_y != 0)
3483 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
3485 else if (sscanf (value, "%ddpi", &res) == 1)
3488 gtk_print_settings_set_resolution (settings, res);
3491 gtk_print_settings_set (settings, option->name, value);
3493 else if (strcmp (option->name, "gtk-paper-type") == 0)
3494 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
3495 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3496 else if (strcmp (option->name, "gtk-n-up") == 0)
3497 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3498 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3499 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
3500 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3501 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
3502 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
3503 gtk_print_settings_set (settings, "cups-job-billing", value);
3504 else if (strcmp (option->name, "gtk-job-prio") == 0)
3505 gtk_print_settings_set (settings, "cups-job-priority", value);
3506 else if (strcmp (option->name, "gtk-cover-before") == 0)
3507 gtk_print_settings_set (settings, "cover-before", value);
3508 else if (strcmp (option->name, "gtk-cover-after") == 0)
3509 gtk_print_settings_set (settings, "cover-after", value);
3510 else if (strcmp (option->name, "gtk-print-time") == 0)
3511 gtk_print_settings_set (settings, "print-at", value);
3512 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3513 gtk_print_settings_set (settings, "print-at-time", value);
3514 else if (g_str_has_prefix (option->name, "cups-"))
3515 gtk_print_settings_set (settings, option->name, value);
3519 supports_am_pm (void)
3521 struct tm tmp_tm = { 0 };
3525 length = strftime (time, sizeof (time), "%p", &tmp_tm);
3530 /* Converts local time to UTC time. Local time has to be in HH:MM format or
3531 * in HH:MM:SS format or in HH:MM:SS {am, pm} format or in HH:MM {am, pm} format
3532 * or in HH {am, pm} format.
3533 * Returns a newly allocated string holding UTC time in HH:MM:SS format
3537 localtime_to_utctime (const char *local_time)
3539 const char *formats_0[] = {" %I : %M : %S %p ", " %H : %M : %S ", " %I : %M %p ", " %H : %M ", " %I %p "};
3540 const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
3541 const char *end = NULL;
3542 struct tm *actual_local_time;
3543 struct tm *actual_utc_time;
3544 struct tm local_print_time;
3545 struct tm utc_print_time;
3546 struct tm diff_time;
3547 gchar *utc_time = NULL;
3550 if (local_time == NULL || local_time[0] == '\0')
3553 n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
3555 for (i = 0; i < n; i++)
3557 local_print_time.tm_hour = 0;
3558 local_print_time.tm_min = 0;
3559 local_print_time.tm_sec = 0;
3561 if (supports_am_pm ())
3562 end = strptime (local_time, formats_0[i], &local_print_time);
3564 end = strptime (local_time, formats_1[i], &local_print_time);
3566 if (end != NULL && end[0] == '\0')
3570 if (end != NULL && end[0] == '\0')
3575 actual_utc_time = g_memdup (gmtime (&rawtime), sizeof (struct tm));
3576 actual_local_time = g_memdup (localtime (&rawtime), sizeof (struct tm));
3578 diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
3579 diff_time.tm_min = actual_utc_time->tm_min - actual_local_time->tm_min;
3580 diff_time.tm_sec = actual_utc_time->tm_sec - actual_local_time->tm_sec;
3582 utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
3583 utc_print_time.tm_min = ((local_print_time.tm_min + diff_time.tm_min) + 60) % 60;
3584 utc_print_time.tm_sec = ((local_print_time.tm_sec + diff_time.tm_sec) + 60) % 60;
3586 utc_time = g_strdup_printf ("%02d:%02d:%02d",
3587 utc_print_time.tm_hour,
3588 utc_print_time.tm_min,
3589 utc_print_time.tm_sec);
3596 cups_printer_get_settings_from_options (GtkPrinter *printer,
3597 GtkPrinterOptionSet *options,
3598 GtkPrintSettings *settings)
3600 struct OptionData data;
3601 const char *print_at, *print_at_time;
3603 data.printer = printer;
3604 data.options = options;
3605 data.settings = settings;
3606 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3608 if (data.ppd_file != NULL)
3610 GtkPrinterOption *cover_before, *cover_after;
3612 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
3614 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
3615 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
3616 if (cover_before && cover_after)
3618 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
3619 gtk_print_settings_set (settings, "cups-job-sheets", value);
3623 print_at = gtk_print_settings_get (settings, "print-at");
3624 print_at_time = gtk_print_settings_get (settings, "print-at-time");
3626 if (strcmp (print_at, "at") == 0)
3628 gchar *utc_time = NULL;
3630 utc_time = localtime_to_utctime (print_at_time);
3632 if (utc_time != NULL)
3634 gtk_print_settings_set (settings, "cups-job-hold-until", utc_time);
3638 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
3640 else if (strcmp (print_at, "on-hold") == 0)
3641 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
3646 cups_printer_prepare_for_print (GtkPrinter *printer,
3647 GtkPrintJob *print_job,
3648 GtkPrintSettings *settings,
3649 GtkPageSetup *page_setup)
3651 GtkPageSet page_set;
3652 GtkPaperSize *paper_size;
3653 const char *ppd_paper_name;
3656 print_job->print_pages = gtk_print_settings_get_print_pages (settings);
3657 print_job->page_ranges = NULL;
3658 print_job->num_page_ranges = 0;
3660 if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
3661 print_job->page_ranges =
3662 gtk_print_settings_get_page_ranges (settings,
3663 &print_job->num_page_ranges);
3665 if (gtk_print_settings_get_collate (settings))
3666 gtk_print_settings_set (settings, "cups-Collate", "True");
3667 print_job->collate = FALSE;
3669 if (gtk_print_settings_get_reverse (settings))
3670 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
3671 print_job->reverse = FALSE;
3673 if (gtk_print_settings_get_n_copies (settings) > 1)
3674 gtk_print_settings_set_int (settings, "cups-copies",
3675 gtk_print_settings_get_n_copies (settings));
3676 print_job->num_copies = 1;
3678 scale = gtk_print_settings_get_scale (settings);
3679 print_job->scale = 1.0;
3681 print_job->scale = scale/100.0;
3683 page_set = gtk_print_settings_get_page_set (settings);
3684 if (page_set == GTK_PAGE_SET_EVEN)
3685 gtk_print_settings_set (settings, "cups-page-set", "even");
3686 else if (page_set == GTK_PAGE_SET_ODD)
3687 gtk_print_settings_set (settings, "cups-page-set", "odd");
3688 print_job->page_set = GTK_PAGE_SET_ALL;
3690 paper_size = gtk_page_setup_get_paper_size (page_setup);
3691 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
3692 if (ppd_paper_name != NULL)
3693 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
3696 char width[G_ASCII_DTOSTR_BUF_SIZE];
3697 char height[G_ASCII_DTOSTR_BUF_SIZE];
3700 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3701 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3702 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
3703 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
3704 g_free (custom_name);
3707 print_job->rotate_to_orientation = TRUE;
3710 static GtkPageSetup *
3711 create_page_setup (ppd_file_t *ppd_file,
3715 GtkPageSetup *page_setup;
3716 GtkPaperSize *paper_size;
3717 ppd_option_t *option;
3718 ppd_choice_t *choice;
3720 display_name = NULL;
3721 option = ppdFindOption (ppd_file, "PageSize");
3724 choice = ppdFindChoice (option, size->name);
3726 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
3729 if (display_name == NULL)
3730 display_name = g_strdup (size->name);
3732 page_setup = gtk_page_setup_new ();
3733 paper_size = gtk_paper_size_new_from_ppd (size->name,
3737 gtk_page_setup_set_paper_size (page_setup, paper_size);
3738 gtk_paper_size_free (paper_size);
3740 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
3741 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
3742 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
3743 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
3745 g_free (display_name);
3751 cups_printer_list_papers (GtkPrinter *printer)
3753 ppd_file_t *ppd_file;
3755 GtkPageSetup *page_setup;
3759 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3760 if (ppd_file == NULL)
3765 for (i = 0; i < ppd_file->num_sizes; i++)
3767 size = &ppd_file->sizes[i];
3769 page_setup = create_page_setup (ppd_file, size);
3771 l = g_list_prepend (l, page_setup);
3774 return g_list_reverse (l);
3777 static GtkPageSetup *
3778 cups_printer_get_default_page_size (GtkPrinter *printer)
3780 ppd_file_t *ppd_file;
3782 ppd_option_t *option;
3785 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3786 if (ppd_file == NULL)
3789 option = ppdFindOption (ppd_file, "PageSize");
3790 size = ppdPageSize (ppd_file, option->defchoice);
3792 return create_page_setup (ppd_file, size);
3796 cups_printer_get_hard_margins (GtkPrinter *printer,
3802 ppd_file_t *ppd_file;
3804 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3805 if (ppd_file == NULL)
3808 *left = ppd_file->custom_margins[0];
3809 *bottom = ppd_file->custom_margins[1];
3810 *right = ppd_file->custom_margins[2];
3811 *top = ppd_file->custom_margins[3];
3814 static GtkPrintCapabilities
3815 cups_printer_get_capabilities (GtkPrinter *printer)
3818 GTK_PRINT_CAPABILITY_COPIES |
3819 GTK_PRINT_CAPABILITY_COLLATE |
3820 GTK_PRINT_CAPABILITY_REVERSE |
3821 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 1 && CUPS_VERSION_PATCH >= 15) || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR > 1) || CUPS_VERSION_MAJOR > 1
3822 GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT |
3824 GTK_PRINT_CAPABILITY_NUMBER_UP;