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") },
2152 static const struct {
2153 const char *keyword;
2155 const char *translation;
2156 } cups_choice_translations[] = {
2157 { "Duplex", "None", N_("One Sided") },
2158 /* Translators: this is an option of "Paper Source" */
2159 { "InputSlot", "Auto", N_("Auto Select") },
2160 /* Translators: this is an option of "Paper Source" */
2161 { "InputSlot", "AutoSelect", N_("Auto Select") },
2162 /* Translators: this is an option of "Paper Source" */
2163 { "InputSlot", "Default", N_("Printer Default") },
2164 /* Translators: this is an option of "Paper Source" */
2165 { "InputSlot", "None", N_("Printer Default") },
2166 /* Translators: this is an option of "Paper Source" */
2167 { "InputSlot", "PrinterDefault", N_("Printer Default") },
2168 /* Translators: this is an option of "Paper Source" */
2169 { "InputSlot", "Unspecified", N_("Auto Select") },
2172 static const struct {
2173 const char *ppd_keyword;
2175 } option_names[] = {
2176 {"Duplex", "gtk-duplex" },
2177 {"MediaType", "gtk-paper-type"},
2178 {"InputSlot", "gtk-paper-source"},
2179 {"OutputBin", "gtk-output-tray"},
2182 /* keep sorted when changing */
2183 static const char *color_option_whitelist[] = {
2184 "BRColorEnhancement",
2190 "BlackSubstitution",
2198 "RPSBlackOverPrint",
2202 /* keep sorted when changing */
2203 static const char *color_group_whitelist[] = {
2210 "HPColorOptionsPanel",
2213 /* keep sorted when changing */
2214 static const char *image_quality_option_whitelist[] = {
2216 "BRHalfTonePattern",
2226 "HPGraphicsHalftone",
2241 /* keep sorted when changing */
2242 static const char *image_quality_group_whitelist[] = {
2249 /* keep sorted when changing */
2250 static const char * finishing_option_whitelist[] = {
2263 "StapleOrientation",
2269 /* keep sorted when changing */
2270 static const char *finishing_group_whitelist[] = {
2279 /* keep sorted when changing */
2280 static const char *cups_option_blacklist[] = {
2289 get_option_text (ppd_file_t *ppd_file,
2290 ppd_option_t *option)
2295 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
2297 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
2298 return g_strdup (_(cups_option_translations[i].translation));
2301 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
2303 /* Some ppd files have spaces in the text before the colon */
2310 get_choice_text (ppd_file_t *ppd_file,
2311 ppd_choice_t *choice)
2314 ppd_option_t *option = choice->option;
2315 const char *keyword = option->keyword;
2317 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
2319 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
2320 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
2321 return g_strdup (_(cups_choice_translations[i].translation));
2323 return ppd_text_to_utf8 (ppd_file, choice->text);
2327 group_has_option (ppd_group_t *group,
2328 ppd_option_t *option)
2335 if (group->num_options > 0 &&
2336 option >= group->options && option < group->options + group->num_options)
2339 for (i = 0; i < group->num_subgroups; i++)
2341 if (group_has_option (&group->subgroups[i],option))
2348 set_option_off (GtkPrinterOption *option)
2350 /* Any of these will do, _set only applies the value
2351 * if its allowed of the option */
2352 gtk_printer_option_set (option, "False");
2353 gtk_printer_option_set (option, "Off");
2354 gtk_printer_option_set (option, "None");
2358 value_is_off (const char *value)
2360 return (strcasecmp (value, "None") == 0 ||
2361 strcasecmp (value, "Off") == 0 ||
2362 strcasecmp (value, "False") == 0);
2366 ppd_group_name (ppd_group_t *group)
2368 #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)
2376 available_choices (ppd_file_t *ppd,
2377 ppd_option_t *option,
2378 ppd_choice_t ***available,
2379 gboolean keep_if_only_one_option)
2381 ppd_option_t *other_option;
2384 ppd_const_t *constraint;
2385 const char *choice, *other_choice;
2386 ppd_option_t *option1, *option2;
2387 ppd_group_t *installed_options;
2389 gboolean all_default;
2395 conflicts = g_new0 (char, option->num_choices);
2397 installed_options = NULL;
2398 for (i = 0; i < ppd->num_groups; i++)
2402 name = ppd_group_name (&ppd->groups[i]);
2403 if (strcmp (name, "InstallableOptions") == 0)
2405 installed_options = &ppd->groups[i];
2410 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
2412 option1 = ppdFindOption (ppd, constraint->option1);
2413 if (option1 == NULL)
2416 option2 = ppdFindOption (ppd, constraint->option2);
2417 if (option2 == NULL)
2420 if (option == option1)
2422 choice = constraint->choice1;
2423 other_option = option2;
2424 other_choice = constraint->choice2;
2426 else if (option == option2)
2428 choice = constraint->choice2;
2429 other_option = option1;
2430 other_choice = constraint->choice1;
2435 /* We only care of conflicts with installed_options and
2437 if (!group_has_option (installed_options, other_option) &&
2438 (strcmp (other_option->keyword, "PageSize") != 0))
2441 if (*other_choice == 0)
2443 /* Conflict only if the installed option is not off */
2444 if (value_is_off (other_option->defchoice))
2447 /* Conflict if the installed option has the specified default */
2448 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
2453 /* Conflict with all non-off choices */
2454 for (j = 0; j < option->num_choices; j++)
2456 if (!value_is_off (option->choices[j].choice))
2462 for (j = 0; j < option->num_choices; j++)
2464 if (strcasecmp (option->choices[j].choice, choice) == 0)
2472 for (j = 0; j < option->num_choices; j++)
2476 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
2477 all_default = FALSE;
2480 if ((all_default && !keep_if_only_one_option) ||
2481 (num_conflicts == option->num_choices))
2488 /* Some ppds don't have a "use printer default" option for
2489 * InputSlot. This means you always have to select a particular slot,
2490 * and you can't auto-pick source based on the paper size. To support
2491 * this we always add an auto option if there isn't one already. If
2492 * the user chooses the generated option we don't send any InputSlot
2493 * value when printing. The way we detect existing auto-cases is based
2494 * on feedback from Michael Sweet of cups fame.
2497 if (strcmp (option->keyword, "InputSlot") == 0)
2499 gboolean found_auto = FALSE;
2500 for (j = 0; j < option->num_choices; j++)
2504 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
2505 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
2506 strcmp (option->choices[j].choice, "Default") == 0 ||
2507 strcmp (option->choices[j].choice, "None") == 0 ||
2508 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
2509 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
2510 option->choices[j].code == NULL ||
2511 option->choices[j].code[0] == 0)
2525 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
2528 for (j = 0; j < option->num_choices; j++)
2531 (*available)[i++] = &option->choices[j];
2535 (*available)[i++] = NULL;
2540 return option->num_choices - num_conflicts + add_auto;
2543 static GtkPrinterOption *
2544 create_pickone_option (ppd_file_t *ppd_file,
2545 ppd_option_t *ppd_option,
2546 const gchar *gtk_name)
2548 GtkPrinterOption *option;
2549 ppd_choice_t **available;
2553 #ifdef HAVE_CUPS_API_1_2
2554 ppd_coption_t *coption;
2557 g_assert (ppd_option->ui == PPD_UI_PICKONE);
2561 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2565 /* right now only support one parameter per custom option
2566 * if more than one print warning and only offer the default choices
2569 label = get_option_text (ppd_file, ppd_option);
2571 #ifdef HAVE_CUPS_API_1_2
2572 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
2576 ppd_cparam_t *cparam;
2578 cparam = ppdFirstCustomParam (coption);
2580 if (ppdNextCustomParam (coption) == NULL)
2582 switch (cparam->type)
2584 case PPD_CUSTOM_INT:
2585 option = gtk_printer_option_new (gtk_name, label,
2586 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
2588 case PPD_CUSTOM_PASSCODE:
2589 option = gtk_printer_option_new (gtk_name, label,
2590 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
2592 case PPD_CUSTOM_PASSWORD:
2593 option = gtk_printer_option_new (gtk_name, label,
2594 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
2596 case PPD_CUSTOM_REAL:
2597 option = gtk_printer_option_new (gtk_name, label,
2598 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
2600 case PPD_CUSTOM_STRING:
2601 option = gtk_printer_option_new (gtk_name, label,
2602 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
2604 #ifdef PRINT_IGNORED_OPTIONS
2605 case PPD_CUSTOM_POINTS:
2606 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
2608 case PPD_CUSTOM_CURVE:
2609 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
2611 case PPD_CUSTOM_INVCURVE:
2612 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
2619 #ifdef PRINT_IGNORED_OPTIONS
2621 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
2624 #endif /* HAVE_CUPS_API_1_2 */
2627 option = gtk_printer_option_new (gtk_name, label,
2628 GTK_PRINTER_OPTION_TYPE_PICKONE);
2631 gtk_printer_option_allocate_choices (option, n_choices);
2632 for (i = 0; i < n_choices; i++)
2634 if (available[i] == NULL)
2636 /* This was auto-added */
2637 option->choices[i] = g_strdup ("gtk-ignore-value");
2638 option->choices_display[i] = g_strdup (_("Printer Default"));
2642 option->choices[i] = g_strdup (available[i]->choice);
2643 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
2646 gtk_printer_option_set (option, ppd_option->defchoice);
2648 #ifdef PRINT_IGNORED_OPTIONS
2650 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
2657 static GtkPrinterOption *
2658 create_boolean_option (ppd_file_t *ppd_file,
2659 ppd_option_t *ppd_option,
2660 const gchar *gtk_name)
2662 GtkPrinterOption *option;
2663 ppd_choice_t **available;
2667 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
2671 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
2674 label = get_option_text (ppd_file, ppd_option);
2675 option = gtk_printer_option_new (gtk_name, label,
2676 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
2679 gtk_printer_option_allocate_choices (option, 2);
2680 option->choices[0] = g_strdup ("True");
2681 option->choices_display[0] = g_strdup ("True");
2682 option->choices[1] = g_strdup ("False");
2683 option->choices_display[1] = g_strdup ("False");
2685 gtk_printer_option_set (option, ppd_option->defchoice);
2687 #ifdef PRINT_IGNORED_OPTIONS
2689 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
2697 get_option_name (const gchar *keyword)
2701 for (i = 0; i < G_N_ELEMENTS (option_names); i++)
2702 if (strcmp (option_names[i].ppd_keyword, keyword) == 0)
2703 return g_strdup (option_names[i].name);
2705 return g_strdup_printf ("cups-%s", keyword);
2709 strptr_cmp (const void *a,
2712 char **aa = (char **)a;
2713 char **bb = (char **)b;
2714 return strcmp (*aa, *bb);
2719 string_in_table (gchar *str,
2720 const gchar *table[],
2723 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
2726 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
2729 handle_option (GtkPrinterOptionSet *set,
2730 ppd_file_t *ppd_file,
2731 ppd_option_t *ppd_option,
2732 ppd_group_t *toplevel_group,
2733 GtkPrintSettings *settings)
2735 GtkPrinterOption *option;
2738 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
2741 name = get_option_name (ppd_option->keyword);
2744 if (ppd_option->ui == PPD_UI_PICKONE)
2746 option = create_pickone_option (ppd_file, ppd_option, name);
2748 else if (ppd_option->ui == PPD_UI_BOOLEAN)
2750 option = create_boolean_option (ppd_file, ppd_option, name);
2752 #ifdef PRINT_IGNORED_OPTIONS
2754 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
2761 name = ppd_group_name (toplevel_group);
2762 if (STRING_IN_TABLE (name,
2763 color_group_whitelist) ||
2764 STRING_IN_TABLE (ppd_option->keyword,
2765 color_option_whitelist))
2767 option->group = g_strdup ("ColorPage");
2769 else if (STRING_IN_TABLE (name,
2770 image_quality_group_whitelist) ||
2771 STRING_IN_TABLE (ppd_option->keyword,
2772 image_quality_option_whitelist))
2774 option->group = g_strdup ("ImageQualityPage");
2776 else if (STRING_IN_TABLE (name,
2777 finishing_group_whitelist) ||
2778 STRING_IN_TABLE (ppd_option->keyword,
2779 finishing_option_whitelist))
2781 option->group = g_strdup ("FinishingPage");
2785 option->group = g_strdup (toplevel_group->text);
2788 set_option_from_settings (option, settings);
2790 gtk_printer_option_set_add (set, option);
2797 handle_group (GtkPrinterOptionSet *set,
2798 ppd_file_t *ppd_file,
2800 ppd_group_t *toplevel_group,
2801 GtkPrintSettings *settings)
2806 /* Ignore installable options */
2807 name = ppd_group_name (toplevel_group);
2808 if (strcmp (name, "InstallableOptions") == 0)
2811 for (i = 0; i < group->num_options; i++)
2812 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
2814 for (i = 0; i < group->num_subgroups; i++)
2815 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
2819 static GtkPrinterOptionSet *
2820 cups_printer_get_options (GtkPrinter *printer,
2821 GtkPrintSettings *settings,
2822 GtkPageSetup *page_setup,
2823 GtkPrintCapabilities capabilities)
2825 GtkPrinterOptionSet *set;
2826 GtkPrinterOption *option;
2827 ppd_file_t *ppd_file;
2829 char *print_at[] = { "now", "at", "on-hold" };
2830 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
2831 char *prio[] = {"100", "80", "50", "30" };
2832 /* Translators: These strings name the possible values of the
2833 * job priority option in the print dialog
2835 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
2836 char *n_up_layout[] = { "lrtb", "lrbt", "rltb", "rlbt", "tblr", "tbrl", "btlr", "btrl" };
2837 /* Translators: These strings name the possible arrangements of
2838 * multiple pages on a sheet when printing
2840 char *n_up_layout_display[] = { N_("Left to right, top to bottom"), N_("Left to right, bottom to top"),
2841 N_("Right to left, top to bottom"), N_("Right to left, bottom to top"),
2842 N_("Top to bottom, left to right"), N_("Top to bottom, right to left"),
2843 N_("Bottom to top, left to right"), N_("Bottom to top, right to left") };
2846 cups_option_t *opts = NULL;
2847 GtkPrintBackendCups *backend;
2848 GtkTextDirection text_direction;
2851 set = gtk_printer_option_set_new ();
2853 /* Cups specific, non-ppd related settings */
2855 /* Translators, this string is used to label the pages-per-sheet option
2856 * in the print dialog
2858 option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2859 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
2861 gtk_printer_option_set (option, "1");
2862 set_option_from_settings (option, settings);
2863 gtk_printer_option_set_add (set, option);
2864 g_object_unref (option);
2866 if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
2868 for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
2869 n_up_layout_display[i] = _(n_up_layout_display[i]);
2871 /* Translators, this string is used to label the option in the print
2872 * dialog that controls in what order multiple pages are arranged
2874 option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2875 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
2876 n_up_layout, n_up_layout_display);
2878 text_direction = gtk_widget_get_default_direction ();
2879 if (text_direction == GTK_TEXT_DIR_LTR)
2880 gtk_printer_option_set (option, "lrtb");
2882 gtk_printer_option_set (option, "rltb");
2884 set_option_from_settings (option, settings);
2885 gtk_printer_option_set_add (set, option);
2886 g_object_unref (option);
2889 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
2890 prio_display[i] = _(prio_display[i]);
2892 /* Translators, this string is used to label the job priority option
2893 * in the print dialog
2895 option = gtk_printer_option_new ("gtk-job-prio", _("Job Priority"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2896 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
2897 prio, prio_display);
2898 gtk_printer_option_set (option, "50");
2899 set_option_from_settings (option, settings);
2900 gtk_printer_option_set_add (set, option);
2901 g_object_unref (option);
2903 /* Translators, this string is used to label the billing info entry
2904 * in the print dialog
2906 option = gtk_printer_option_new ("gtk-billing-info", _("Billing Info"), GTK_PRINTER_OPTION_TYPE_STRING);
2907 gtk_printer_option_set (option, "");
2908 set_option_from_settings (option, settings);
2909 gtk_printer_option_set_add (set, option);
2910 g_object_unref (option);
2912 backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
2914 if (backend != NULL)
2916 char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
2917 /* Translators, these strings are names for various 'standard' cover
2918 * pages that the printing system may support.
2920 char *cover_display_default[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
2921 char **cover = NULL;
2922 char **cover_display = NULL;
2923 char **cover_display_translated = NULL;
2924 gint num_of_covers = 0;
2928 num_of_covers = backend->number_of_covers;
2929 cover = g_new (char *, num_of_covers + 1);
2930 cover[num_of_covers] = NULL;
2931 cover_display = g_new (char *, num_of_covers + 1);
2932 cover_display[num_of_covers] = NULL;
2933 cover_display_translated = g_new (char *, num_of_covers + 1);
2934 cover_display_translated[num_of_covers] = NULL;
2936 for (i = 0; i < num_of_covers; i++)
2938 cover[i] = g_strdup (backend->covers[i]);
2940 for (j = 0; j < G_N_ELEMENTS (cover_default); j++)
2941 if (strcmp (cover_default[j], cover[i]) == 0)
2943 value = cover_display_default[j];
2946 cover_display[i] = (value != NULL) ? g_strdup (value) : g_strdup (backend->covers[i]);
2949 for (i = 0; i < num_of_covers; i++)
2950 cover_display_translated[i] = _(cover_display[i]);
2952 /* Translators, this is the label used for the option in the print
2953 * dialog that controls the front cover page.
2955 option = gtk_printer_option_new ("gtk-cover-before", _("Before"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2956 gtk_printer_option_choices_from_array (option, num_of_covers,
2957 cover, cover_display_translated);
2959 if (backend->default_cover_before != NULL)
2960 gtk_printer_option_set (option, backend->default_cover_before);
2962 gtk_printer_option_set (option, "none");
2963 set_option_from_settings (option, settings);
2964 gtk_printer_option_set_add (set, option);
2965 g_object_unref (option);
2967 /* Translators, this is the label used for the option in the print
2968 * dialog that controls the back cover page.
2970 option = gtk_printer_option_new ("gtk-cover-after", _("After"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2971 gtk_printer_option_choices_from_array (option, num_of_covers,
2972 cover, cover_display_translated);
2973 if (backend->default_cover_after != NULL)
2974 gtk_printer_option_set (option, backend->default_cover_after);
2976 gtk_printer_option_set (option, "none");
2977 set_option_from_settings (option, settings);
2978 gtk_printer_option_set_add (set, option);
2979 g_object_unref (option);
2982 g_strfreev (cover_display);
2983 g_free (cover_display_translated);
2986 /* Translators: this is the name of the option that controls when
2987 * a print job is printed. Possible values are 'now', a specified time,
2990 option = gtk_printer_option_new ("gtk-print-time", _("Print at"), GTK_PRINTER_OPTION_TYPE_PICKONE);
2991 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
2992 print_at, print_at);
2993 gtk_printer_option_set (option, "now");
2994 set_option_from_settings (option, settings);
2995 gtk_printer_option_set_add (set, option);
2996 g_object_unref (option);
2998 /* Translators: this is the name of the option that allows the user
2999 * to specify a time when a print job will be printed.
3001 option = gtk_printer_option_new ("gtk-print-time-text", _("Print at time"), GTK_PRINTER_OPTION_TYPE_STRING);
3002 gtk_printer_option_set (option, "");
3003 set_option_from_settings (option, settings);
3004 gtk_printer_option_set_add (set, option);
3005 g_object_unref (option);
3007 /* Printer (ppd) specific settings */
3008 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3011 GtkPaperSize *paper_size;
3012 ppd_option_t *option;
3013 const gchar *ppd_name;
3015 ppdMarkDefaults (ppd_file);
3017 paper_size = gtk_page_setup_get_paper_size (page_setup);
3019 option = ppdFindOption (ppd_file, "PageSize");
3020 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
3023 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
3027 char width[G_ASCII_DTOSTR_BUF_SIZE];
3028 char height[G_ASCII_DTOSTR_BUF_SIZE];
3030 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3031 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3032 /* Translators: this format is used to display a custom paper
3033 * size. The two placeholders are replaced with the width and height
3034 * in points. E.g: "Custom 230.4x142.9"
3036 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
3037 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
3038 g_free (custom_name);
3041 for (i = 0; i < ppd_file->num_groups; i++)
3042 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
3045 /* Now honor the user set defaults for this printer */
3046 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
3048 for (i = 0; i < num_opts; i++)
3050 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
3053 name = get_option_name (opts[i].name);
3054 option = gtk_printer_option_set_lookup (set, name);
3056 gtk_printer_option_set (option, opts[i].value);
3060 cupsFreeOptions (num_opts, opts);
3067 mark_option_from_set (GtkPrinterOptionSet *set,
3068 ppd_file_t *ppd_file,
3069 ppd_option_t *ppd_option)
3071 GtkPrinterOption *option;
3072 char *name = get_option_name (ppd_option->keyword);
3074 option = gtk_printer_option_set_lookup (set, name);
3077 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
3084 mark_group_from_set (GtkPrinterOptionSet *set,
3085 ppd_file_t *ppd_file,
3090 for (i = 0; i < group->num_options; i++)
3091 mark_option_from_set (set, ppd_file, &group->options[i]);
3093 for (i = 0; i < group->num_subgroups; i++)
3094 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
3098 set_conflicts_from_option (GtkPrinterOptionSet *set,
3099 ppd_file_t *ppd_file,
3100 ppd_option_t *ppd_option)
3102 GtkPrinterOption *option;
3105 if (ppd_option->conflicted)
3107 name = get_option_name (ppd_option->keyword);
3108 option = gtk_printer_option_set_lookup (set, name);
3111 gtk_printer_option_set_has_conflict (option, TRUE);
3112 #ifdef PRINT_IGNORED_OPTIONS
3114 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
3122 set_conflicts_from_group (GtkPrinterOptionSet *set,
3123 ppd_file_t *ppd_file,
3128 for (i = 0; i < group->num_options; i++)
3129 set_conflicts_from_option (set, ppd_file, &group->options[i]);
3131 for (i = 0; i < group->num_subgroups; i++)
3132 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
3136 cups_printer_mark_conflicts (GtkPrinter *printer,
3137 GtkPrinterOptionSet *options)
3139 ppd_file_t *ppd_file;
3143 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3145 if (ppd_file == NULL)
3148 ppdMarkDefaults (ppd_file);
3150 for (i = 0; i < ppd_file->num_groups; i++)
3151 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
3153 num_conflicts = ppdConflicts (ppd_file);
3155 if (num_conflicts > 0)
3157 for (i = 0; i < ppd_file->num_groups; i++)
3158 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
3161 return num_conflicts > 0;
3165 GtkPrinter *printer;
3166 GtkPrinterOptionSet *options;
3167 GtkPrintSettings *settings;
3168 ppd_file_t *ppd_file;
3173 const char *standard;
3177 map_settings_to_option (GtkPrinterOption *option,
3178 const NameMapping table[],
3180 GtkPrintSettings *settings,
3181 const gchar *standard_name,
3182 const gchar *cups_name)
3186 const char *cups_value;
3187 const char *standard_value;
3189 /* If the cups-specific setting is set, always use that */
3190 name = g_strdup_printf ("cups-%s", cups_name);
3191 cups_value = gtk_print_settings_get (settings, name);
3194 if (cups_value != NULL)
3196 gtk_printer_option_set (option, cups_value);
3200 /* Otherwise we try to convert from the general setting */
3201 standard_value = gtk_print_settings_get (settings, standard_name);
3202 if (standard_value == NULL)
3205 for (i = 0; i < n_elements; i++)
3207 if (table[i].cups == NULL && table[i].standard == NULL)
3209 gtk_printer_option_set (option, standard_value);
3212 else if (table[i].cups == NULL &&
3213 strcmp (table[i].standard, standard_value) == 0)
3215 set_option_off (option);
3218 else if (strcmp (table[i].standard, standard_value) == 0)
3220 gtk_printer_option_set (option, table[i].cups);
3227 map_option_to_settings (const gchar *value,
3228 const NameMapping table[],
3230 GtkPrintSettings *settings,
3231 const gchar *standard_name,
3232 const gchar *cups_name)
3237 for (i = 0; i < n_elements; i++)
3239 if (table[i].cups == NULL && table[i].standard == NULL)
3241 gtk_print_settings_set (settings,
3246 else if (table[i].cups == NULL && table[i].standard != NULL)
3248 if (value_is_off (value))
3250 gtk_print_settings_set (settings,
3256 else if (strcmp (table[i].cups, value) == 0)
3258 gtk_print_settings_set (settings,
3265 /* Always set the corresponding cups-specific setting */
3266 name = g_strdup_printf ("cups-%s", cups_name);
3267 gtk_print_settings_set (settings, name, value);
3272 static const NameMapping paper_source_map[] = {
3273 { "Lower", "lower"},
3274 { "Middle", "middle"},
3275 { "Upper", "upper"},
3277 { "Envelope", "envelope"},
3278 { "Cassette", "cassette"},
3279 { "LargeCapacity", "large-capacity"},
3280 { "AnySmallFormat", "small-format"},
3281 { "AnyLargeFormat", "large-format"},
3285 static const NameMapping output_tray_map[] = {
3286 { "Upper", "upper"},
3287 { "Lower", "lower"},
3292 static const NameMapping duplex_map[] = {
3293 { "DuplexTumble", "vertical" },
3294 { "DuplexNoTumble", "horizontal" },
3298 static const NameMapping output_mode_map[] = {
3299 { "Standard", "normal" },
3300 { "Normal", "normal" },
3301 { "Draft", "draft" },
3302 { "Fast", "draft" },
3305 static const NameMapping media_type_map[] = {
3306 { "Transparency", "transparency"},
3307 { "Standard", "stationery"},
3311 static const NameMapping all_map[] = {
3317 set_option_from_settings (GtkPrinterOption *option,
3318 GtkPrintSettings *settings)
3320 const char *cups_value;
3323 if (settings == NULL)
3326 if (strcmp (option->name, "gtk-paper-source") == 0)
3327 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
3328 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3329 else if (strcmp (option->name, "gtk-output-tray") == 0)
3330 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
3331 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3332 else if (strcmp (option->name, "gtk-duplex") == 0)
3333 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
3334 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3335 else if (strcmp (option->name, "cups-OutputMode") == 0)
3336 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
3337 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3338 else if (strcmp (option->name, "cups-Resolution") == 0)
3340 cups_value = gtk_print_settings_get (settings, option->name);
3342 gtk_printer_option_set (option, cups_value);
3345 int res = gtk_print_settings_get_resolution (settings);
3346 int res_x = gtk_print_settings_get_resolution_x (settings);
3347 int res_y = gtk_print_settings_get_resolution_y (settings);
3351 value = g_strdup_printf ("%dx%ddpi", res_x, res_y);
3352 gtk_printer_option_set (option, value);
3357 value = g_strdup_printf ("%ddpi", res);
3358 gtk_printer_option_set (option, value);
3363 else if (strcmp (option->name, "gtk-paper-type") == 0)
3364 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
3365 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3366 else if (strcmp (option->name, "gtk-n-up") == 0)
3368 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
3369 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3371 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
3373 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
3374 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
3376 else if (strcmp (option->name, "gtk-billing-info") == 0)
3378 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
3380 gtk_printer_option_set (option, cups_value);
3382 else if (strcmp (option->name, "gtk-job-prio") == 0)
3384 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
3386 gtk_printer_option_set (option, cups_value);
3388 else if (strcmp (option->name, "gtk-cover-before") == 0)
3390 cups_value = gtk_print_settings_get (settings, "cover-before");
3392 gtk_printer_option_set (option, cups_value);
3394 else if (strcmp (option->name, "gtk-cover-after") == 0)
3396 cups_value = gtk_print_settings_get (settings, "cover-after");
3398 gtk_printer_option_set (option, cups_value);
3400 else if (strcmp (option->name, "gtk-print-time") == 0)
3402 cups_value = gtk_print_settings_get (settings, "print-at");
3404 gtk_printer_option_set (option, cups_value);
3406 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3408 cups_value = gtk_print_settings_get (settings, "print-at-time");
3410 gtk_printer_option_set (option, cups_value);
3412 else if (g_str_has_prefix (option->name, "cups-"))
3414 cups_value = gtk_print_settings_get (settings, option->name);
3416 gtk_printer_option_set (option, cups_value);
3421 foreach_option_get_settings (GtkPrinterOption *option,
3424 struct OptionData *data = user_data;
3425 GtkPrintSettings *settings = data->settings;
3428 value = option->value;
3430 if (strcmp (option->name, "gtk-paper-source") == 0)
3431 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
3432 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
3433 else if (strcmp (option->name, "gtk-output-tray") == 0)
3434 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
3435 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
3436 else if (strcmp (option->name, "gtk-duplex") == 0)
3437 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
3438 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
3439 else if (strcmp (option->name, "cups-OutputMode") == 0)
3440 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
3441 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
3442 else if (strcmp (option->name, "cups-Resolution") == 0)
3444 int res, res_x, res_y;
3446 if (sscanf (value, "%dx%ddpi", &res_x, &res_y) == 2)
3448 if (res_x != 0 && res_y != 0)
3449 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
3451 else if (sscanf (value, "%ddpi", &res) == 1)
3454 gtk_print_settings_set_resolution (settings, res);
3457 gtk_print_settings_set (settings, option->name, value);
3459 else if (strcmp (option->name, "gtk-paper-type") == 0)
3460 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
3461 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
3462 else if (strcmp (option->name, "gtk-n-up") == 0)
3463 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3464 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
3465 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
3466 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
3467 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
3468 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
3469 gtk_print_settings_set (settings, "cups-job-billing", value);
3470 else if (strcmp (option->name, "gtk-job-prio") == 0)
3471 gtk_print_settings_set (settings, "cups-job-priority", value);
3472 else if (strcmp (option->name, "gtk-cover-before") == 0)
3473 gtk_print_settings_set (settings, "cover-before", value);
3474 else if (strcmp (option->name, "gtk-cover-after") == 0)
3475 gtk_print_settings_set (settings, "cover-after", value);
3476 else if (strcmp (option->name, "gtk-print-time") == 0)
3477 gtk_print_settings_set (settings, "print-at", value);
3478 else if (strcmp (option->name, "gtk-print-time-text") == 0)
3479 gtk_print_settings_set (settings, "print-at-time", value);
3480 else if (g_str_has_prefix (option->name, "cups-"))
3481 gtk_print_settings_set (settings, option->name, value);
3485 supports_am_pm (void)
3487 struct tm tmp_tm = { 0 };
3491 length = strftime (time, sizeof (time), "%p", &tmp_tm);
3496 /* Converts local time to UTC time. Local time has to be in HH:MM format or
3497 * in HH:MM:SS format or in HH:MM:SS {am, pm} format or in HH:MM {am, pm} format
3498 * or in HH {am, pm} format.
3499 * Returns a newly allocated string holding UTC time in HH:MM:SS format
3503 localtime_to_utctime (const char *local_time)
3505 const char *formats_0[] = {" %I : %M : %S %p ", " %H : %M : %S ", " %I : %M %p ", " %H : %M ", " %I %p "};
3506 const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
3507 const char *end = NULL;
3508 struct tm *actual_local_time;
3509 struct tm *actual_utc_time;
3510 struct tm local_print_time;
3511 struct tm utc_print_time;
3512 struct tm diff_time;
3513 gchar *utc_time = NULL;
3516 if (local_time == NULL || local_time[0] == '\0')
3519 n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
3521 for (i = 0; i < n; i++)
3523 local_print_time.tm_hour = 0;
3524 local_print_time.tm_min = 0;
3525 local_print_time.tm_sec = 0;
3527 if (supports_am_pm ())
3528 end = strptime (local_time, formats_0[i], &local_print_time);
3530 end = strptime (local_time, formats_1[i], &local_print_time);
3532 if (end != NULL && end[0] == '\0')
3536 if (end != NULL && end[0] == '\0')
3541 actual_utc_time = g_memdup (gmtime (&rawtime), sizeof (struct tm));
3542 actual_local_time = g_memdup (localtime (&rawtime), sizeof (struct tm));
3544 diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
3545 diff_time.tm_min = actual_utc_time->tm_min - actual_local_time->tm_min;
3546 diff_time.tm_sec = actual_utc_time->tm_sec - actual_local_time->tm_sec;
3548 utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
3549 utc_print_time.tm_min = ((local_print_time.tm_min + diff_time.tm_min) + 60) % 60;
3550 utc_print_time.tm_sec = ((local_print_time.tm_sec + diff_time.tm_sec) + 60) % 60;
3552 utc_time = g_strdup_printf ("%02d:%02d:%02d",
3553 utc_print_time.tm_hour,
3554 utc_print_time.tm_min,
3555 utc_print_time.tm_sec);
3562 cups_printer_get_settings_from_options (GtkPrinter *printer,
3563 GtkPrinterOptionSet *options,
3564 GtkPrintSettings *settings)
3566 struct OptionData data;
3567 const char *print_at, *print_at_time;
3569 data.printer = printer;
3570 data.options = options;
3571 data.settings = settings;
3572 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3574 if (data.ppd_file != NULL)
3576 GtkPrinterOption *cover_before, *cover_after;
3578 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
3580 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
3581 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
3582 if (cover_before && cover_after)
3584 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
3585 gtk_print_settings_set (settings, "cups-job-sheets", value);
3589 print_at = gtk_print_settings_get (settings, "print-at");
3590 print_at_time = gtk_print_settings_get (settings, "print-at-time");
3592 if (strcmp (print_at, "at") == 0)
3594 gchar *utc_time = NULL;
3596 utc_time = localtime_to_utctime (print_at_time);
3598 if (utc_time != NULL)
3600 gtk_print_settings_set (settings, "cups-job-hold-until", utc_time);
3604 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
3606 else if (strcmp (print_at, "on-hold") == 0)
3607 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
3612 cups_printer_prepare_for_print (GtkPrinter *printer,
3613 GtkPrintJob *print_job,
3614 GtkPrintSettings *settings,
3615 GtkPageSetup *page_setup)
3617 GtkPageSet page_set;
3618 GtkPaperSize *paper_size;
3619 const char *ppd_paper_name;
3622 print_job->print_pages = gtk_print_settings_get_print_pages (settings);
3623 print_job->page_ranges = NULL;
3624 print_job->num_page_ranges = 0;
3626 if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
3627 print_job->page_ranges =
3628 gtk_print_settings_get_page_ranges (settings,
3629 &print_job->num_page_ranges);
3631 if (gtk_print_settings_get_collate (settings))
3632 gtk_print_settings_set (settings, "cups-Collate", "True");
3633 print_job->collate = FALSE;
3635 if (gtk_print_settings_get_reverse (settings))
3636 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
3637 print_job->reverse = FALSE;
3639 if (gtk_print_settings_get_n_copies (settings) > 1)
3640 gtk_print_settings_set_int (settings, "cups-copies",
3641 gtk_print_settings_get_n_copies (settings));
3642 print_job->num_copies = 1;
3644 scale = gtk_print_settings_get_scale (settings);
3645 print_job->scale = 1.0;
3647 print_job->scale = scale/100.0;
3649 page_set = gtk_print_settings_get_page_set (settings);
3650 if (page_set == GTK_PAGE_SET_EVEN)
3651 gtk_print_settings_set (settings, "cups-page-set", "even");
3652 else if (page_set == GTK_PAGE_SET_ODD)
3653 gtk_print_settings_set (settings, "cups-page-set", "odd");
3654 print_job->page_set = GTK_PAGE_SET_ALL;
3656 paper_size = gtk_page_setup_get_paper_size (page_setup);
3657 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
3658 if (ppd_paper_name != NULL)
3659 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
3662 char width[G_ASCII_DTOSTR_BUF_SIZE];
3663 char height[G_ASCII_DTOSTR_BUF_SIZE];
3666 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3667 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3668 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
3669 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
3670 g_free (custom_name);
3673 print_job->rotate_to_orientation = TRUE;
3676 static GtkPageSetup *
3677 create_page_setup (ppd_file_t *ppd_file,
3681 GtkPageSetup *page_setup;
3682 GtkPaperSize *paper_size;
3683 ppd_option_t *option;
3684 ppd_choice_t *choice;
3686 display_name = NULL;
3687 option = ppdFindOption (ppd_file, "PageSize");
3690 choice = ppdFindChoice (option, size->name);
3692 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
3695 if (display_name == NULL)
3696 display_name = g_strdup (size->name);
3698 page_setup = gtk_page_setup_new ();
3699 paper_size = gtk_paper_size_new_from_ppd (size->name,
3703 gtk_page_setup_set_paper_size (page_setup, paper_size);
3704 gtk_paper_size_free (paper_size);
3706 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
3707 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
3708 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
3709 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
3711 g_free (display_name);
3717 cups_printer_list_papers (GtkPrinter *printer)
3719 ppd_file_t *ppd_file;
3721 GtkPageSetup *page_setup;
3725 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3726 if (ppd_file == NULL)
3731 for (i = 0; i < ppd_file->num_sizes; i++)
3733 size = &ppd_file->sizes[i];
3735 page_setup = create_page_setup (ppd_file, size);
3737 l = g_list_prepend (l, page_setup);
3740 return g_list_reverse (l);
3743 static GtkPageSetup *
3744 cups_printer_get_default_page_size (GtkPrinter *printer)
3746 ppd_file_t *ppd_file;
3748 ppd_option_t *option;
3751 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3752 if (ppd_file == NULL)
3755 option = ppdFindOption (ppd_file, "PageSize");
3756 size = ppdPageSize (ppd_file, option->defchoice);
3758 return create_page_setup (ppd_file, size);
3762 cups_printer_get_hard_margins (GtkPrinter *printer,
3768 ppd_file_t *ppd_file;
3770 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3771 if (ppd_file == NULL)
3774 *left = ppd_file->custom_margins[0];
3775 *bottom = ppd_file->custom_margins[1];
3776 *right = ppd_file->custom_margins[2];
3777 *top = ppd_file->custom_margins[3];
3780 static GtkPrintCapabilities
3781 cups_printer_get_capabilities (GtkPrinter *printer)
3784 GTK_PRINT_CAPABILITY_COPIES |
3785 GTK_PRINT_CAPABILITY_COLLATE |
3786 GTK_PRINT_CAPABILITY_REVERSE |
3787 #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
3788 GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT |
3790 GTK_PRINT_CAPABILITY_NUMBER_UP;