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, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
32 #include <cups/cups.h>
33 #include <cups/language.h>
34 #include <cups/http.h>
38 #include <cairo-pdf.h>
41 #include <glib/gstdio.h>
42 #include <glib/gi18n-lib.h>
46 #include <gtk/gtkprintbackend.h>
47 #include <gtk/gtkunixprint.h>
48 #include <gtk/gtkprinter-private.h>
50 #include "gtkprintbackendcups.h"
51 #include "gtkprintercups.h"
53 #include "gtkcupsutils.h"
59 typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
61 #define GTK_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
62 #define GTK_IS_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_CUPS))
63 #define GTK_PRINT_BACKEND_CUPS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
65 #define _CUPS_MAX_ATTEMPTS 10
66 #define _CUPS_MAX_CHUNK_SIZE 8192
68 /* define this to see warnings about ignored ppd options */
69 #undef PRINT_IGNORED_OPTIONS
71 #define _CUPS_MAP_ATTR_INT(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].integer;}
72 #define _CUPS_MAP_ATTR_STR(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].string.text;}
74 static GType print_backend_cups_type = 0;
76 typedef void (* GtkPrintCupsResponseCallbackFunc) (GtkPrintBackend *print_backend,
77 GtkCupsResult *result,
88 } GtkPrintCupsDispatchState;
95 GtkCupsRequest *request;
96 GtkCupsPollState poll_state;
98 GtkPrintBackendCups *backend;
99 GtkPrintCupsResponseCallbackFunc callback;
100 gpointer callback_data;
102 } GtkPrintCupsDispatchWatch;
104 struct _GtkPrintBackendCupsClass
106 GtkPrintBackendClass parent_class;
109 struct _GtkPrintBackendCups
111 GtkPrintBackend parent_instance;
113 char *default_printer;
115 guint list_printers_poll;
116 guint list_printers_pending : 1;
117 gint list_printers_attempts;
118 guint got_default_printer : 1;
119 guint default_printer_poll;
120 GtkCupsConnectionTest *cups_connection_test;
124 int number_of_covers;
129 gboolean authentication_lock;
131 CdClient *colord_client;
135 static GObjectClass *backend_parent_class;
137 static void gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class);
138 static void gtk_print_backend_cups_init (GtkPrintBackendCups *impl);
139 static void gtk_print_backend_cups_finalize (GObject *object);
140 static void gtk_print_backend_cups_dispose (GObject *object);
141 static void cups_get_printer_list (GtkPrintBackend *print_backend);
142 static void cups_get_default_printer (GtkPrintBackendCups *print_backend);
143 static void cups_get_local_default_printer (GtkPrintBackendCups *print_backend);
144 static void cups_request_execute (GtkPrintBackendCups *print_backend,
145 GtkCupsRequest *request,
146 GtkPrintCupsResponseCallbackFunc callback,
148 GDestroyNotify notify);
149 static void cups_printer_get_settings_from_options (GtkPrinter *printer,
150 GtkPrinterOptionSet *options,
151 GtkPrintSettings *settings);
152 static gboolean cups_printer_mark_conflicts (GtkPrinter *printer,
153 GtkPrinterOptionSet *options);
154 static GtkPrinterOptionSet *cups_printer_get_options (GtkPrinter *printer,
155 GtkPrintSettings *settings,
156 GtkPageSetup *page_setup,
157 GtkPrintCapabilities capabilities);
158 static void cups_printer_prepare_for_print (GtkPrinter *printer,
159 GtkPrintJob *print_job,
160 GtkPrintSettings *settings,
161 GtkPageSetup *page_setup);
162 static GList * cups_printer_list_papers (GtkPrinter *printer);
163 static GtkPageSetup * cups_printer_get_default_page_size (GtkPrinter *printer);
164 static void cups_printer_request_details (GtkPrinter *printer);
165 static gboolean cups_request_default_printer (GtkPrintBackendCups *print_backend);
166 static gboolean cups_request_ppd (GtkPrinter *printer);
167 static gboolean cups_printer_get_hard_margins (GtkPrinter *printer,
172 static GtkPrintCapabilities cups_printer_get_capabilities (GtkPrinter *printer);
173 static void set_option_from_settings (GtkPrinterOption *option,
174 GtkPrintSettings *setting);
175 static void cups_begin_polling_info (GtkPrintBackendCups *print_backend,
178 static gboolean cups_job_info_poll_timeout (gpointer user_data);
179 static void gtk_print_backend_cups_print_stream (GtkPrintBackend *backend,
182 GtkPrintJobCompleteFunc callback,
184 GDestroyNotify dnotify);
185 static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter *printer,
186 GtkPrintSettings *settings,
189 GIOChannel *cache_io);
191 static void gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
192 gchar **auth_info_required,
195 void overwrite_and_free (gpointer data);
196 static gboolean is_address_local (const gchar *address);
197 static gboolean request_auth_info (gpointer data);
200 gtk_print_backend_cups_register_type (GTypeModule *module)
202 const GTypeInfo print_backend_cups_info =
204 sizeof (GtkPrintBackendCupsClass),
205 NULL, /* base_init */
206 NULL, /* base_finalize */
207 (GClassInitFunc) gtk_print_backend_cups_class_init,
208 NULL, /* class_finalize */
209 NULL, /* class_data */
210 sizeof (GtkPrintBackendCups),
212 (GInstanceInitFunc) gtk_print_backend_cups_init
215 print_backend_cups_type = g_type_module_register_type (module,
216 GTK_TYPE_PRINT_BACKEND,
217 "GtkPrintBackendCups",
218 &print_backend_cups_info, 0);
222 pb_module_init (GTypeModule *module)
225 g_print ("CUPS Backend: Initializing the CUPS print backend module\n"));
227 gtk_print_backend_cups_register_type (module);
228 gtk_printer_cups_register_type (module);
232 pb_module_exit (void)
237 G_MODULE_EXPORT GtkPrintBackend *
238 pb_module_create (void)
240 return gtk_print_backend_cups_new ();
244 * GtkPrintBackendCups
247 gtk_print_backend_cups_get_type (void)
249 return print_backend_cups_type;
253 * gtk_print_backend_cups_new:
255 * Creates a new #GtkPrintBackendCups object. #GtkPrintBackendCups
256 * implements the #GtkPrintBackend interface with direct access to
257 * the filesystem using Unix/Linux API calls
259 * Return value: the new #GtkPrintBackendCups object
262 gtk_print_backend_cups_new (void)
265 g_print ("CUPS Backend: Creating a new CUPS print backend object\n"));
267 return g_object_new (GTK_TYPE_PRINT_BACKEND_CUPS, NULL);
271 gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
273 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
274 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
276 backend_parent_class = g_type_class_peek_parent (class);
278 gobject_class->finalize = gtk_print_backend_cups_finalize;
279 gobject_class->dispose = gtk_print_backend_cups_dispose;
281 backend_class->request_printer_list = cups_get_printer_list;
282 backend_class->print_stream = gtk_print_backend_cups_print_stream;
283 backend_class->printer_request_details = cups_printer_request_details;
284 backend_class->printer_create_cairo_surface = cups_printer_create_cairo_surface;
285 backend_class->printer_get_options = cups_printer_get_options;
286 backend_class->printer_mark_conflicts = cups_printer_mark_conflicts;
287 backend_class->printer_get_settings_from_options = cups_printer_get_settings_from_options;
288 backend_class->printer_prepare_for_print = cups_printer_prepare_for_print;
289 backend_class->printer_list_papers = cups_printer_list_papers;
290 backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
291 backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
292 backend_class->printer_get_capabilities = cups_printer_get_capabilities;
293 backend_class->set_password = gtk_print_backend_cups_set_password;
296 static cairo_status_t
297 _cairo_write_to_cups (void *closure,
298 const unsigned char *data,
301 GIOChannel *io = (GIOChannel *)closure;
308 g_print ("CUPS Backend: Writing %i byte chunk to temp file\n", length));
312 g_io_channel_write_chars (io, (gchar *)data, length, &written, &error);
317 g_print ("CUPS Backend: Error writing to temp file, %s\n",
320 g_error_free (error);
321 return CAIRO_STATUS_WRITE_ERROR;
325 g_print ("CUPS Backend: Wrote %"G_GSIZE_FORMAT" bytes to temp file\n", written));
331 return CAIRO_STATUS_SUCCESS;
334 static cairo_surface_t *
335 cups_printer_create_cairo_surface (GtkPrinter *printer,
336 GtkPrintSettings *settings,
339 GIOChannel *cache_io)
341 cairo_surface_t *surface;
342 ppd_file_t *ppd_file = NULL;
343 ppd_attr_t *ppd_attr = NULL;
344 ppd_attr_t *ppd_attr_res = NULL;
345 ppd_attr_t *ppd_attr_screen_freq = NULL;
346 ppd_attr_t *ppd_attr_res_screen_freq = NULL;
347 gchar *res_string = NULL;
350 if (gtk_printer_accepts_pdf (printer))
351 surface = cairo_pdf_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
353 surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
355 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
357 if (ppd_file != NULL)
359 ppd_attr = ppdFindAttr (ppd_file, "LanguageLevel", NULL);
361 if (ppd_attr != NULL)
362 level = atoi (ppd_attr->value);
364 if (gtk_print_settings_get_resolution (settings) == 0)
366 ppd_attr_res = ppdFindAttr (ppd_file, "DefaultResolution", NULL);
368 if (ppd_attr_res != NULL)
370 int res, res_x, res_y;
372 if (sscanf (ppd_attr_res->value, "%dx%ddpi", &res_x, &res_y) == 2)
374 if (res_x > 0 && res_y > 0)
375 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
377 else if (sscanf (ppd_attr_res->value, "%ddpi", &res) == 1)
380 gtk_print_settings_set_resolution (settings, res);
385 res_string = g_strdup_printf ("%ddpi",
386 gtk_print_settings_get_resolution (settings));
387 ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
390 if (ppd_attr_res_screen_freq == NULL)
392 res_string = g_strdup_printf ("%dx%ddpi",
393 gtk_print_settings_get_resolution_x (settings),
394 gtk_print_settings_get_resolution_y (settings));
395 ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
399 ppd_attr_screen_freq = ppdFindAttr (ppd_file, "ScreenFreq", NULL);
401 if (ppd_attr_res_screen_freq != NULL && atof (ppd_attr_res_screen_freq->value) > 0.0)
402 gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_res_screen_freq->value));
403 else if (ppd_attr_screen_freq != NULL && atof (ppd_attr_screen_freq->value) > 0.0)
404 gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_screen_freq->value));
407 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PS)
410 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
413 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
416 cairo_surface_set_fallback_resolution (surface,
417 2.0 * gtk_print_settings_get_printer_lpi (settings),
418 2.0 * gtk_print_settings_get_printer_lpi (settings));
424 GtkPrintJobCompleteFunc callback;
427 GDestroyNotify dnotify;
428 } CupsPrintStreamData;
431 cups_free_print_stream_data (CupsPrintStreamData *data)
434 g_print ("CUPS Backend: %s\n", G_STRFUNC));
437 data->dnotify (data->user_data);
438 g_object_unref (data->job);
443 cups_print_cb (GtkPrintBackendCups *print_backend,
444 GtkCupsResult *result,
447 GError *error = NULL;
448 CupsPrintStreamData *ps = user_data;
450 GDK_THREADS_ENTER ();
453 g_print ("CUPS Backend: %s\n", G_STRFUNC));
455 if (gtk_cups_result_is_error (result))
456 error = g_error_new_literal (gtk_print_error_quark (),
457 GTK_PRINT_ERROR_INTERNAL_ERROR,
458 gtk_cups_result_get_error_string (result));
461 ps->callback (ps->job, ps->user_data, error);
466 ipp_attribute_t *attr; /* IPP job-id attribute */
467 ipp_t *response = gtk_cups_result_get_response (result);
469 if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
470 job_id = attr->values[0].integer;
472 if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
473 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
476 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_PENDING);
477 cups_begin_polling_info (print_backend, ps->job, job_id);
481 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED_ABORTED);
485 g_error_free (error);
487 GDK_THREADS_LEAVE ();
491 GtkCupsRequest *request;
492 GtkPrinterCups *printer;
496 add_cups_options (const gchar *key,
500 CupsOptionsData *data = (CupsOptionsData *) user_data;
501 GtkCupsRequest *request = data->request;
502 GtkPrinterCups *printer = data->printer;
503 gboolean custom_value = FALSE;
504 gchar *new_value = NULL;
510 if (!g_str_has_prefix (key, "cups-"))
513 if (strcmp (value, "gtk-ignore-value") == 0)
516 key = key + strlen ("cups-");
518 if (printer && printer->ppd_file)
520 ppd_coption_t *coption;
521 gboolean found = FALSE;
522 gboolean custom_values_enabled = FALSE;
524 coption = ppdFindCustomOption (printer->ppd_file, key);
525 if (coption && coption->option)
527 for (i = 0; i < coption->option->num_choices; i++)
529 /* Are custom values enabled ? */
530 if (g_str_equal (coption->option->choices[i].choice, "Custom"))
531 custom_values_enabled = TRUE;
533 /* Is the value among available choices ? */
534 if (g_str_equal (coption->option->choices[i].choice, value))
538 if (custom_values_enabled && !found)
543 /* Add "Custom." prefix to custom values. */
546 new_value = g_strdup_printf ("Custom.%s", value);
547 gtk_cups_request_encode_option (request, key, new_value);
551 gtk_cups_request_encode_option (request, key, value);
555 gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
558 GtkPrintJobCompleteFunc callback,
560 GDestroyNotify dnotify)
562 GtkPrinterCups *cups_printer;
563 CupsPrintStreamData *ps;
564 CupsOptionsData *options_data;
565 GtkCupsRequest *request;
566 GtkPrintSettings *settings;
568 char printer_absolute_uri[HTTP_MAX_URI];
571 g_print ("CUPS Backend: %s\n", G_STRFUNC));
573 cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
574 settings = gtk_print_job_get_settings (job);
576 request = gtk_cups_request_new_with_username (NULL,
581 cups_printer->device_uri,
582 GTK_PRINT_BACKEND_CUPS (print_backend)->username);
584 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
585 httpAssembleURIf (HTTP_URI_CODING_ALL,
586 printer_absolute_uri,
587 sizeof (printer_absolute_uri),
593 gtk_printer_get_name (gtk_print_job_get_printer (job)));
595 g_snprintf (printer_absolute_uri,
596 sizeof (printer_absolute_uri),
597 "ipp://localhost:%d/printers/%s",
599 gtk_printer_get_name (gtk_print_job_get_printer (job)));
602 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
603 IPP_TAG_URI, "printer-uri",
604 NULL, printer_absolute_uri);
606 title = gtk_print_job_get_title (job);
608 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
609 IPP_TAG_NAME, "job-name",
612 options_data = g_new0 (CupsOptionsData, 1);
613 options_data->request = request;
614 options_data->printer = cups_printer;
615 gtk_print_settings_foreach (settings, add_cups_options, options_data);
616 g_free (options_data);
618 ps = g_new0 (CupsPrintStreamData, 1);
619 ps->callback = callback;
620 ps->user_data = user_data;
621 ps->dnotify = dnotify;
622 ps->job = g_object_ref (job);
624 request->need_auth_info = cups_printer->auth_info_required != NULL;
625 request->auth_info_required = g_strdupv (cups_printer->auth_info_required);
627 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
629 (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
631 (GDestroyNotify)cups_free_print_stream_data);
634 void overwrite_and_free (gpointer data)
636 gchar *password = (gchar *) data;
638 if (password != NULL)
640 memset (password, 0, strlen (password));
646 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
648 backend_cups->list_printers_poll = FALSE;
649 backend_cups->got_default_printer = FALSE;
650 backend_cups->list_printers_pending = FALSE;
651 backend_cups->list_printers_attempts = 0;
652 backend_cups->reading_ppds = 0;
654 backend_cups->requests = NULL;
655 backend_cups->auth = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, overwrite_and_free);
656 backend_cups->authentication_lock = FALSE;
658 backend_cups->covers = NULL;
659 backend_cups->number_of_covers = 0;
661 backend_cups->default_printer_poll = 0;
662 backend_cups->cups_connection_test = NULL;
664 backend_cups->username = NULL;
667 backend_cups->colord_client = cd_client_new ();
670 cups_get_local_default_printer (backend_cups);
674 gtk_print_backend_cups_finalize (GObject *object)
676 GtkPrintBackendCups *backend_cups;
679 g_print ("CUPS Backend: finalizing CUPS backend module\n"));
681 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
683 g_free (backend_cups->default_printer);
684 backend_cups->default_printer = NULL;
686 g_strfreev (backend_cups->covers);
687 backend_cups->number_of_covers = 0;
689 gtk_cups_connection_test_free (backend_cups->cups_connection_test);
690 backend_cups->cups_connection_test = NULL;
692 g_hash_table_destroy (backend_cups->auth);
694 g_free (backend_cups->username);
697 g_object_unref (backend_cups->colord_client);
700 backend_parent_class->finalize (object);
704 gtk_print_backend_cups_dispose (GObject *object)
706 GtkPrintBackendCups *backend_cups;
709 g_print ("CUPS Backend: %s\n", G_STRFUNC));
711 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
713 if (backend_cups->list_printers_poll > 0)
714 g_source_remove (backend_cups->list_printers_poll);
715 backend_cups->list_printers_poll = 0;
716 backend_cups->list_printers_attempts = 0;
718 if (backend_cups->default_printer_poll > 0)
719 g_source_remove (backend_cups->default_printer_poll);
720 backend_cups->default_printer_poll = 0;
722 backend_parent_class->dispose (object);
726 is_address_local (const gchar *address)
728 if (address[0] == '/' ||
729 strcmp (address, "127.0.0.1") == 0 ||
730 strcmp (address, "[::1]") == 0)
737 gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
738 gchar **auth_info_required,
741 GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
743 char dispatch_hostname[HTTP_MAX_URI];
744 gchar *username = NULL;
745 gchar *hostname = NULL;
746 gchar *password = NULL;
750 length = g_strv_length (auth_info_required);
752 if (auth_info != NULL)
753 for (i = 0; i < length; i++)
755 if (g_strcmp0 (auth_info_required[i], "username") == 0)
756 username = g_strdup (auth_info[i]);
757 else if (g_strcmp0 (auth_info_required[i], "hostname") == 0)
758 hostname = g_strdup (auth_info[i]);
759 else if (g_strcmp0 (auth_info_required[i], "password") == 0)
760 password = g_strdup (auth_info[i]);
763 if (hostname != NULL && username != NULL && password != NULL)
765 gchar *key = g_strconcat (username, "@", hostname, NULL);
766 g_hash_table_insert (cups_backend->auth, key, g_strdup (password));
768 g_print ("CUPS backend: storing password for %s\n", key));
771 g_free (cups_backend->username);
772 cups_backend->username = g_strdup (username);
775 for (l = cups_backend->requests; l; l = l->next)
777 GtkPrintCupsDispatchWatch *dispatch = l->data;
779 httpGetHostname (dispatch->request->http, dispatch_hostname, sizeof (dispatch_hostname));
780 if (is_address_local (dispatch_hostname))
781 strcpy (dispatch_hostname, "localhost");
783 if (dispatch->request->need_auth_info)
785 if (auth_info != NULL)
787 dispatch->request->auth_info = g_new0 (gchar *, length + 1);
788 for (i = 0; i < length; i++)
789 dispatch->request->auth_info[i] = g_strdup (auth_info[i]);
791 dispatch->backend->authentication_lock = FALSE;
792 dispatch->request->need_auth_info = FALSE;
794 else if (dispatch->request->password_state == GTK_CUPS_PASSWORD_REQUESTED || auth_info == NULL)
796 overwrite_and_free (dispatch->request->password);
797 dispatch->request->password = g_strdup (password);
798 g_free (dispatch->request->username);
799 dispatch->request->username = g_strdup (username);
800 dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
801 dispatch->backend->authentication_lock = FALSE;
807 request_password (gpointer data)
809 GtkPrintCupsDispatchWatch *dispatch = data;
810 const gchar *username;
812 gchar *prompt = NULL;
814 char hostname[HTTP_MAX_URI];
815 gchar **auth_info_required;
816 gchar **auth_info_default;
817 gchar **auth_info_display;
818 gboolean *auth_info_visible;
822 if (dispatch->backend->authentication_lock)
823 return G_SOURCE_REMOVE;
825 httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
826 if (is_address_local (hostname))
827 strcpy (hostname, "localhost");
829 if (dispatch->backend->username != NULL)
830 username = dispatch->backend->username;
832 username = cupsUser ();
834 auth_info_required = g_new0 (gchar*, length + 1);
835 auth_info_required[0] = g_strdup ("hostname");
836 auth_info_required[1] = g_strdup ("username");
837 auth_info_required[2] = g_strdup ("password");
839 auth_info_default = g_new0 (gchar*, length + 1);
840 auth_info_default[0] = g_strdup (hostname);
841 auth_info_default[1] = g_strdup (username);
843 auth_info_display = g_new0 (gchar*, length + 1);
844 auth_info_display[1] = g_strdup (_("Username:"));
845 auth_info_display[2] = g_strdup (_("Password:"));
847 auth_info_visible = g_new0 (gboolean, length + 1);
848 auth_info_visible[1] = TRUE;
850 key = g_strconcat (username, "@", hostname, NULL);
851 password = g_hash_table_lookup (dispatch->backend->auth, key);
853 if (password && dispatch->request->password_state != GTK_CUPS_PASSWORD_NOT_VALID)
856 g_print ("CUPS backend: using stored password for %s\n", key));
858 overwrite_and_free (dispatch->request->password);
859 dispatch->request->password = g_strdup (password);
860 g_free (dispatch->request->username);
861 dispatch->request->username = g_strdup (username);
862 dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
866 const char *job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
867 const char *printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
868 char *printer_name = NULL;
870 if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
871 printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
873 if (dispatch->request->password_state == GTK_CUPS_PASSWORD_NOT_VALID)
874 g_hash_table_remove (dispatch->backend->auth, key);
876 dispatch->request->password_state = GTK_CUPS_PASSWORD_REQUESTED;
878 dispatch->backend->authentication_lock = TRUE;
880 switch (dispatch->request->ipp_request->request.op.operation_id)
883 if (job_title != NULL && printer_name != NULL)
884 prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
886 prompt = g_strdup_printf ( _("Authentication is required to print a document on %s"), hostname);
888 case IPP_GET_JOB_ATTRIBUTES:
889 if (job_title != NULL)
890 prompt = g_strdup_printf ( _("Authentication is required to get attributes of job '%s'"), job_title);
892 prompt = g_strdup ( _("Authentication is required to get attributes of a job"));
894 case IPP_GET_PRINTER_ATTRIBUTES:
895 if (printer_name != NULL)
896 prompt = g_strdup_printf ( _("Authentication is required to get attributes of printer %s"), printer_name);
898 prompt = g_strdup ( _("Authentication is required to get attributes of a printer"));
900 case CUPS_GET_DEFAULT:
901 prompt = g_strdup_printf ( _("Authentication is required to get default printer of %s"), hostname);
903 case CUPS_GET_PRINTERS:
904 prompt = g_strdup_printf ( _("Authentication is required to get printers from %s"), hostname);
907 /* work around gcc warning about 0 not being a value for this enum */
908 if (dispatch->request->ipp_request->request.op.operation_id == 0)
909 prompt = g_strdup_printf ( _("Authentication is required to get a file from %s"), hostname);
911 prompt = g_strdup_printf ( _("Authentication is required on %s"), hostname);
915 g_free (printer_name);
917 g_signal_emit_by_name (dispatch->backend, "request-password",
918 auth_info_required, auth_info_default, auth_info_display, auth_info_visible, prompt);
923 for (i = 0; i < length; i++)
925 g_free (auth_info_required[i]);
926 g_free (auth_info_default[i]);
927 g_free (auth_info_display[i]);
930 g_free (auth_info_required);
931 g_free (auth_info_default);
932 g_free (auth_info_display);
933 g_free (auth_info_visible);
936 return G_SOURCE_REMOVE;
940 cups_dispatch_add_poll (GSource *source)
942 GtkPrintCupsDispatchWatch *dispatch;
943 GtkCupsPollState poll_state;
945 dispatch = (GtkPrintCupsDispatchWatch *) source;
947 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
949 /* Remove the old source if the poll state changed. */
950 if (poll_state != dispatch->poll_state && dispatch->data_poll != NULL)
952 g_source_remove_poll (source, dispatch->data_poll);
953 g_free (dispatch->data_poll);
954 dispatch->data_poll = NULL;
957 if (dispatch->request->http != NULL)
959 if (dispatch->data_poll == NULL)
961 dispatch->data_poll = g_new0 (GPollFD, 1);
962 dispatch->poll_state = poll_state;
964 if (poll_state == GTK_CUPS_HTTP_READ)
965 dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
966 else if (poll_state == GTK_CUPS_HTTP_WRITE)
967 dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
969 dispatch->data_poll->events = 0;
971 dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
972 g_source_add_poll (source, dispatch->data_poll);
978 check_auth_info (gpointer user_data)
980 GtkPrintCupsDispatchWatch *dispatch;
981 dispatch = (GtkPrintCupsDispatchWatch *) user_data;
983 if (!dispatch->request->need_auth_info)
985 if (dispatch->request->auth_info == NULL)
987 dispatch->callback (GTK_PRINT_BACKEND (dispatch->backend),
988 gtk_cups_request_get_result (dispatch->request),
989 dispatch->callback_data);
990 g_source_destroy ((GSource *) dispatch);
997 length = g_strv_length (dispatch->request->auth_info_required);
999 gtk_cups_request_ipp_add_strings (dispatch->request,
1005 (const char * const *) dispatch->request->auth_info);
1007 g_source_attach ((GSource *) dispatch, NULL);
1008 g_source_unref ((GSource *) dispatch);
1010 for (i = 0; i < length; i++)
1011 overwrite_and_free (dispatch->request->auth_info[i]);
1012 g_free (dispatch->request->auth_info);
1013 dispatch->request->auth_info = NULL;
1016 return G_SOURCE_REMOVE;
1019 return G_SOURCE_CONTINUE;
1023 request_auth_info (gpointer user_data)
1025 GtkPrintCupsDispatchWatch *dispatch;
1026 const char *job_title;
1027 const char *printer_uri;
1028 gchar *prompt = NULL;
1029 char *printer_name = NULL;
1032 gboolean *auth_info_visible = NULL;
1033 gchar **auth_info_default = NULL;
1034 gchar **auth_info_display = NULL;
1036 dispatch = (GtkPrintCupsDispatchWatch *) user_data;
1038 if (dispatch->backend->authentication_lock)
1041 job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
1042 printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
1043 length = g_strv_length (dispatch->request->auth_info_required);
1045 auth_info_visible = g_new0 (gboolean, length);
1046 auth_info_default = g_new0 (gchar *, length + 1);
1047 auth_info_display = g_new0 (gchar *, length + 1);
1049 for (i = 0; i < length; i++)
1051 if (g_strcmp0 (dispatch->request->auth_info_required[i], "domain") == 0)
1053 auth_info_display[i] = g_strdup (_("Domain:"));
1054 auth_info_default[i] = g_strdup ("WORKGROUP");
1055 auth_info_visible[i] = TRUE;
1057 else if (g_strcmp0 (dispatch->request->auth_info_required[i], "username") == 0)
1059 auth_info_display[i] = g_strdup (_("Username:"));
1060 if (dispatch->backend->username != NULL)
1061 auth_info_default[i] = g_strdup (dispatch->backend->username);
1063 auth_info_default[i] = g_strdup (cupsUser ());
1064 auth_info_visible[i] = TRUE;
1066 else if (g_strcmp0 (dispatch->request->auth_info_required[i], "password") == 0)
1068 auth_info_display[i] = g_strdup (_("Password:"));
1069 auth_info_visible[i] = FALSE;
1073 if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
1074 printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
1076 dispatch->backend->authentication_lock = TRUE;
1078 if (job_title != NULL)
1080 if (printer_name != NULL)
1081 prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
1083 prompt = g_strdup_printf ( _("Authentication is required to print document '%s'"), job_title);
1087 if (printer_name != NULL)
1088 prompt = g_strdup_printf ( _("Authentication is required to print this document on printer %s"), printer_name);
1090 prompt = g_strdup ( _("Authentication is required to print this document"));
1093 g_signal_emit_by_name (dispatch->backend, "request-password",
1094 dispatch->request->auth_info_required,
1100 for (i = 0; i < length; i++)
1102 g_free (auth_info_default[i]);
1103 g_free (auth_info_display[i]);
1106 g_free (auth_info_default);
1107 g_free (auth_info_display);
1108 g_free (printer_name);
1111 g_idle_add (check_auth_info, user_data);
1117 cups_dispatch_watch_check (GSource *source)
1119 GtkPrintCupsDispatchWatch *dispatch;
1120 GtkCupsPollState poll_state;
1124 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1126 dispatch = (GtkPrintCupsDispatchWatch *) source;
1128 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
1130 if (poll_state != GTK_CUPS_HTTP_IDLE && !dispatch->request->need_password)
1131 if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
1134 result = gtk_cups_request_read_write (dispatch->request, FALSE);
1135 if (result && dispatch->data_poll != NULL)
1137 g_source_remove_poll (source, dispatch->data_poll);
1138 g_free (dispatch->data_poll);
1139 dispatch->data_poll = NULL;
1142 if (dispatch->request->need_password && dispatch->request->password_state != GTK_CUPS_PASSWORD_REQUESTED)
1144 dispatch->request->need_password = FALSE;
1145 g_idle_add (request_password, dispatch);
1153 cups_dispatch_watch_prepare (GSource *source,
1156 GtkPrintCupsDispatchWatch *dispatch;
1159 dispatch = (GtkPrintCupsDispatchWatch *) source;
1162 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1166 result = gtk_cups_request_read_write (dispatch->request, TRUE);
1168 cups_dispatch_add_poll (source);
1174 cups_dispatch_watch_dispatch (GSource *source,
1175 GSourceFunc callback,
1178 GtkPrintCupsDispatchWatch *dispatch;
1179 GtkPrintCupsResponseCallbackFunc ep_callback;
1180 GtkCupsResult *result;
1182 g_assert (callback != NULL);
1184 ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
1186 dispatch = (GtkPrintCupsDispatchWatch *) source;
1188 result = gtk_cups_request_get_result (dispatch->request);
1191 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1193 if (gtk_cups_result_is_error (result))
1196 g_print("Error result: %s (type %i, status %i, code %i)\n",
1197 gtk_cups_result_get_error_string (result),
1198 gtk_cups_result_get_error_type (result),
1199 gtk_cups_result_get_error_status (result),
1200 gtk_cups_result_get_error_code (result)));
1203 ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
1209 cups_dispatch_watch_finalize (GSource *source)
1211 GtkPrintCupsDispatchWatch *dispatch;
1212 GtkCupsResult *result;
1215 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1217 dispatch = (GtkPrintCupsDispatchWatch *) source;
1219 result = gtk_cups_request_get_result (dispatch->request);
1220 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH)
1222 const gchar *username;
1223 gchar hostname[HTTP_MAX_URI];
1226 httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
1227 if (is_address_local (hostname))
1228 strcpy (hostname, "localhost");
1230 if (dispatch->backend->username != NULL)
1231 username = dispatch->backend->username;
1233 username = cupsUser ();
1235 key = g_strconcat (username, "@", hostname, NULL);
1237 g_print ("CUPS backend: removing stored password for %s\n", key));
1238 g_hash_table_remove (dispatch->backend->auth, key);
1241 if (dispatch->backend)
1242 dispatch->backend->authentication_lock = FALSE;
1245 gtk_cups_request_free (dispatch->request);
1247 if (dispatch->backend)
1249 /* We need to unref this at idle time, because it might be the
1250 * last reference to this module causing the code to be
1251 * unloaded (including this particular function!)
1252 * Update: Doing this at idle caused a deadlock taking the
1253 * mainloop context lock while being in a GSource callout for
1254 * multithreaded apps. So, for now we just disable unloading
1255 * of print backends. See _gtk_print_backend_create for the
1259 dispatch->backend->requests = g_list_remove (dispatch->backend->requests, dispatch);
1262 g_object_unref (dispatch->backend);
1263 dispatch->backend = NULL;
1266 if (dispatch->data_poll)
1268 g_source_remove_poll (source, dispatch->data_poll);
1269 g_free (dispatch->data_poll);
1270 dispatch->data_poll = NULL;
1274 static GSourceFuncs _cups_dispatch_watch_funcs = {
1275 cups_dispatch_watch_prepare,
1276 cups_dispatch_watch_check,
1277 cups_dispatch_watch_dispatch,
1278 cups_dispatch_watch_finalize
1283 cups_request_execute (GtkPrintBackendCups *print_backend,
1284 GtkCupsRequest *request,
1285 GtkPrintCupsResponseCallbackFunc callback,
1287 GDestroyNotify notify)
1289 GtkPrintCupsDispatchWatch *dispatch;
1291 dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
1292 sizeof (GtkPrintCupsDispatchWatch));
1293 g_source_set_name (&dispatch->source, "GTK+ CUPS backend");
1296 g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
1298 dispatch->request = request;
1299 dispatch->backend = g_object_ref (print_backend);
1300 dispatch->poll_state = GTK_CUPS_HTTP_IDLE;
1301 dispatch->data_poll = NULL;
1302 dispatch->callback = NULL;
1303 dispatch->callback_data = NULL;
1305 print_backend->requests = g_list_prepend (print_backend->requests, dispatch);
1307 g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
1309 if (request->need_auth_info)
1311 dispatch->callback = callback;
1312 dispatch->callback_data = user_data;
1313 request_auth_info (dispatch);
1317 g_source_attach ((GSource *) dispatch, NULL);
1318 g_source_unref ((GSource *) dispatch);
1324 cups_request_printer_info_cb (GtkPrintBackendCups *backend,
1325 GtkCupsResult *result,
1328 ipp_attribute_t *attr;
1330 gchar *printer_name;
1331 GtkPrinterCups *cups_printer;
1332 GtkPrinter *printer;
1337 gboolean status_changed;
1339 g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
1341 printer_name = (gchar *)user_data;
1342 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
1346 g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
1351 g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
1355 cups_printer = GTK_PRINTER_CUPS (printer);
1357 if (gtk_cups_result_is_error (result))
1359 if (gtk_printer_is_new (printer))
1361 gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
1366 return; /* TODO: mark as inactive printer */
1369 response = gtk_cups_result_get_response (result);
1371 /* TODO: determine printer type and use correct icon */
1372 gtk_printer_set_icon_name (printer, "printer");
1378 for (attr = response->attrs; attr != NULL; attr = attr->next)
1383 _CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
1384 _CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
1385 _CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
1386 _CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
1387 _CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
1390 status_changed = gtk_printer_set_job_count (printer, job_count);
1392 status_changed |= gtk_printer_set_location (printer, loc);
1393 status_changed |= gtk_printer_set_description (printer, desc);
1394 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1397 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1398 "printer-status-changed", printer);
1402 cups_request_printer_info (GtkPrintBackendCups *print_backend,
1403 const gchar *printer_name)
1405 GtkCupsRequest *request;
1407 static const char * const pattrs[] = /* Attributes we're interested in */
1411 "printer-state-message",
1414 "job-sheets-supported",
1415 "job-sheets-default"
1418 request = gtk_cups_request_new_with_username (NULL,
1420 IPP_GET_PRINTER_ATTRIBUTES,
1424 print_backend->username);
1426 printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
1428 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
1429 "printer-uri", NULL, printer_uri);
1432 g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
1434 g_free (printer_uri);
1436 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1437 "requested-attributes", G_N_ELEMENTS (pattrs),
1440 cups_request_execute (print_backend,
1442 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
1443 g_strdup (printer_name),
1444 (GDestroyNotify) g_free);
1449 GtkPrintBackendCups *print_backend;
1456 job_object_died (gpointer user_data,
1457 GObject *where_the_object_was)
1459 CupsJobPollData *data = user_data;
1464 cups_job_poll_data_free (CupsJobPollData *data)
1467 g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
1473 cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
1474 GtkCupsResult *result,
1477 CupsJobPollData *data = user_data;
1478 ipp_attribute_t *attr;
1483 GDK_THREADS_ENTER ();
1485 if (data->job == NULL)
1487 cups_job_poll_data_free (data);
1493 response = gtk_cups_result_get_response (result);
1496 for (attr = response->attrs; attr != NULL; attr = attr->next)
1501 _CUPS_MAP_ATTR_INT (attr, state, "job-state");
1507 case IPP_JOB_PENDING:
1509 case IPP_JOB_STOPPED:
1510 gtk_print_job_set_status (data->job,
1511 GTK_PRINT_STATUS_PENDING);
1513 case IPP_JOB_PROCESSING:
1514 gtk_print_job_set_status (data->job,
1515 GTK_PRINT_STATUS_PRINTING);
1518 case IPP_JOB_CANCELLED:
1519 case IPP_JOB_ABORTED:
1520 gtk_print_job_set_status (data->job,
1521 GTK_PRINT_STATUS_FINISHED_ABORTED);
1525 case IPP_JOB_COMPLETED:
1526 gtk_print_job_set_status (data->job,
1527 GTK_PRINT_STATUS_FINISHED);
1532 if (!done && data->job != NULL)
1536 if (data->counter < 5)
1538 else if (data->counter < 10)
1543 g_timeout_add (timeout, cups_job_info_poll_timeout, data);
1546 cups_job_poll_data_free (data);
1549 GDK_THREADS_LEAVE ();
1553 cups_request_job_info (CupsJobPollData *data)
1555 GtkCupsRequest *request;
1558 request = gtk_cups_request_new_with_username (NULL,
1560 IPP_GET_JOB_ATTRIBUTES,
1564 data->print_backend->username);
1566 job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
1567 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
1568 "job-uri", NULL, job_uri);
1571 cups_request_execute (data->print_backend,
1573 (GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
1579 cups_job_info_poll_timeout (gpointer user_data)
1581 CupsJobPollData *data = user_data;
1583 if (data->job == NULL)
1584 cups_job_poll_data_free (data);
1586 cups_request_job_info (data);
1588 return G_SOURCE_REMOVE;
1592 cups_begin_polling_info (GtkPrintBackendCups *print_backend,
1596 CupsJobPollData *data;
1598 data = g_new0 (CupsJobPollData, 1);
1600 data->print_backend = print_backend;
1602 data->job_id = job_id;
1605 g_object_weak_ref (G_OBJECT (job), job_object_died, data);
1607 cups_request_job_info (data);
1611 mark_printer_inactive (GtkPrinter *printer,
1612 GtkPrintBackend *backend)
1614 gtk_printer_set_is_active (printer, FALSE);
1615 g_signal_emit_by_name (backend, "printer-removed", printer);
1619 find_printer (GtkPrinter *printer,
1620 const gchar *find_name)
1622 const gchar *printer_name;
1624 printer_name = gtk_printer_get_name (printer);
1625 return g_ascii_strcasecmp (printer_name, find_name);
1629 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
1630 GtkCupsResult *result,
1633 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
1634 ipp_attribute_t *attr;
1636 gboolean list_has_changed;
1637 GList *removed_printer_checklist;
1638 gchar *remote_default_printer = NULL;
1640 GDK_THREADS_ENTER ();
1642 list_has_changed = FALSE;
1645 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1647 cups_backend->list_printers_pending = FALSE;
1649 if (gtk_cups_result_is_error (result))
1652 g_warning ("CUPS Backend: Error getting printer list: %s %d %d",
1653 gtk_cups_result_get_error_string (result),
1654 gtk_cups_result_get_error_type (result),
1655 gtk_cups_result_get_error_code (result)));
1657 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
1658 gtk_cups_result_get_error_code (result) == 1)
1660 /* Canceled by user, stop popping up more password dialogs */
1661 if (cups_backend->list_printers_poll > 0)
1662 g_source_remove (cups_backend->list_printers_poll);
1663 cups_backend->list_printers_poll = 0;
1664 cups_backend->list_printers_attempts = 0;
1670 /* Gather the names of the printers in the current queue
1671 * so we may check to see if they were removed
1673 removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
1675 response = gtk_cups_result_get_response (result);
1677 for (attr = response->attrs; attr != NULL; attr = attr->next)
1679 GtkPrinter *printer;
1680 const gchar *printer_name = NULL;
1681 const gchar *printer_uri = NULL;
1682 const gchar *member_uris = NULL;
1683 const gchar *location = NULL;
1684 const gchar *description = NULL;
1685 const gchar *state_msg = NULL;
1688 gboolean status_changed = FALSE;
1691 const gchar *reason_msg = NULL;
1692 gchar *reason_msg_desc = NULL;
1693 gchar *tmp_msg = NULL;
1694 gchar *tmp_msg2 = NULL;
1695 gint printer_state_reason_level = 0; /* 0 - none, 1 - report, 2 - warning, 3 - error */
1696 gboolean interested_in = FALSE;
1697 gboolean found = FALSE;
1698 static const char * const reasons[] = /* Reasons we're interested in */
1704 "marker-supply-low",
1705 "marker-supply-empty",
1713 static const char * reasons_descs[] =
1715 N_("Printer '%s' is low on toner."),
1716 N_("Printer '%s' has no toner left."),
1717 /* Translators: "Developer" like on photo development context */
1718 N_("Printer '%s' is low on developer."),
1719 /* Translators: "Developer" like on photo development context */
1720 N_("Printer '%s' is out of developer."),
1721 /* Translators: "marker" is one color bin of the printer */
1722 N_("Printer '%s' is low on at least one marker supply."),
1723 /* Translators: "marker" is one color bin of the printer */
1724 N_("Printer '%s' is out of at least one marker supply."),
1725 N_("The cover is open on printer '%s'."),
1726 N_("The door is open on printer '%s'."),
1727 N_("Printer '%s' is low on paper."),
1728 N_("Printer '%s' is out of paper."),
1729 N_("Printer '%s' is currently offline."),
1730 N_("There is a problem on printer '%s'.")
1732 gboolean is_paused = FALSE;
1733 gboolean is_accepting_jobs = TRUE;
1734 gboolean default_printer = FALSE;
1735 gboolean got_printer_type = FALSE;
1736 gchar *default_cover_before = NULL;
1737 gchar *default_cover_after = NULL;
1738 gboolean remote_printer = FALSE;
1739 gchar **auth_info_required = NULL;
1740 gint default_number_up = 1;
1742 /* Skip leading attributes until we hit a printer...
1744 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1750 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1752 if (strcmp (attr->name, "printer-name") == 0 &&
1753 attr->value_tag == IPP_TAG_NAME)
1754 printer_name = attr->values[0].string.text;
1755 else if (strcmp (attr->name, "printer-uri-supported") == 0 &&
1756 attr->value_tag == IPP_TAG_URI)
1757 printer_uri = attr->values[0].string.text;
1758 else if (strcmp (attr->name, "member-uris") == 0 &&
1759 attr->value_tag == IPP_TAG_URI)
1760 member_uris = attr->values[0].string.text;
1761 else if (strcmp (attr->name, "printer-location") == 0)
1762 location = attr->values[0].string.text;
1763 else if (strcmp (attr->name, "printer-info") == 0)
1764 description = attr->values[0].string.text;
1765 else if (strcmp (attr->name, "printer-state-message") == 0)
1766 state_msg = attr->values[0].string.text;
1767 else if (strcmp (attr->name, "printer-state-reasons") == 0)
1768 /* Store most important reason to reason_msg and set
1769 its importance at printer_state_reason_level */
1771 for (i = 0; i < attr->num_values; i++)
1773 if (strcmp (attr->values[i].string.text, "none") != 0)
1775 /* Sets is_paused flag for paused printer. */
1776 if (strcmp (attr->values[i].string.text, "paused") == 0)
1781 interested_in = FALSE;
1782 for (j = 0; j < G_N_ELEMENTS (reasons); j++)
1783 if (strncmp (attr->values[i].string.text, reasons[j], strlen (reasons[j])) == 0)
1785 interested_in = TRUE;
1791 if (g_str_has_suffix (attr->values[i].string.text, "-report"))
1793 if (printer_state_reason_level <= 1)
1795 reason_msg = attr->values[i].string.text;
1796 printer_state_reason_level = 1;
1799 else if (g_str_has_suffix (attr->values[i].string.text, "-warning"))
1801 if (printer_state_reason_level <= 2)
1803 reason_msg = attr->values[i].string.text;
1804 printer_state_reason_level = 2;
1807 else /* It is error in the case of no suffix. */
1809 reason_msg = attr->values[i].string.text;
1810 printer_state_reason_level = 3;
1816 else if (strcmp (attr->name, "printer-state") == 0)
1817 state = attr->values[0].integer;
1818 else if (strcmp (attr->name, "queued-job-count") == 0)
1819 job_count = attr->values[0].integer;
1820 else if (strcmp (attr->name, "printer-is-accepting-jobs") == 0)
1822 if (attr->values[0].boolean == 1)
1823 is_accepting_jobs = TRUE;
1825 is_accepting_jobs = FALSE;
1827 else if (strcmp (attr->name, "job-sheets-supported") == 0)
1829 if (cups_backend->covers == NULL)
1831 cups_backend->number_of_covers = attr->num_values;
1832 cups_backend->covers = g_new (char *, cups_backend->number_of_covers + 1);
1833 for (i = 0; i < cups_backend->number_of_covers; i++)
1834 cups_backend->covers[i] = g_strdup (attr->values[i].string.text);
1835 cups_backend->covers[cups_backend->number_of_covers] = NULL;
1838 else if (strcmp (attr->name, "job-sheets-default") == 0)
1840 if (attr->num_values == 2)
1842 default_cover_before = attr->values[0].string.text;
1843 default_cover_after = attr->values[1].string.text;
1846 else if (strcmp (attr->name, "printer-type") == 0)
1848 got_printer_type = TRUE;
1849 if (attr->values[0].integer & 0x00020000)
1850 default_printer = TRUE;
1852 default_printer = FALSE;
1854 if (attr->values[0].integer & 0x00000002)
1855 remote_printer = TRUE;
1857 remote_printer = FALSE;
1859 else if (strcmp (attr->name, "auth-info-required") == 0)
1861 if (strcmp (attr->values[0].string.text, "none") != 0)
1863 auth_info_required = g_new0 (gchar *, attr->num_values + 1);
1864 for (i = 0; i < attr->num_values; i++)
1865 auth_info_required[i] = g_strdup (attr->values[i].string.text);
1868 else if (strcmp (attr->name, "number-up-default") == 0)
1870 default_number_up = attr->values[0].integer;
1875 g_print ("CUPS Backend: Attribute %s ignored", attr->name));
1881 if (printer_name == NULL ||
1882 (printer_uri == NULL && member_uris == NULL))
1890 if (got_printer_type)
1892 if (default_printer && !cups_backend->got_default_printer)
1894 if (!remote_printer)
1896 cups_backend->got_default_printer = TRUE;
1897 cups_backend->default_printer = g_strdup (printer_name);
1901 if (remote_default_printer == NULL)
1902 remote_default_printer = g_strdup (printer_name);
1908 if (!cups_backend->got_default_printer)
1909 cups_get_default_printer (cups_backend);
1912 /* remove name from checklist if it was found */
1913 node = g_list_find_custom (removed_printer_checklist, printer_name, (GCompareFunc) find_printer);
1914 removed_printer_checklist = g_list_delete_link (removed_printer_checklist, node);
1916 printer = gtk_print_backend_find_printer (backend, printer_name);
1919 GtkPrinterCups *cups_printer;
1920 char uri[HTTP_MAX_URI]; /* Printer URI */
1921 char method[HTTP_MAX_URI]; /* Method/scheme name */
1922 char username[HTTP_MAX_URI]; /* Username:password */
1923 char hostname[HTTP_MAX_URI]; /* Hostname */
1924 char resource[HTTP_MAX_URI]; /* Resource name */
1925 int port; /* Port number */
1926 char *cups_server; /* CUPS server */
1928 list_has_changed = TRUE;
1930 cups_printer = gtk_printer_cups_new (printer_name,
1932 cups_backend->colord_client);
1934 cups_printer = gtk_printer_cups_new (printer_name, backend, NULL);
1937 cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
1939 /* Check to see if we are looking at a class */
1942 cups_printer->printer_uri = g_strdup (member_uris);
1943 /* TODO if member_uris is a class we need to recursivly find a printer */
1945 g_print ("CUPS Backend: Found class with printer %s\n", member_uris));
1949 cups_printer->printer_uri = g_strdup (printer_uri);
1951 g_print ("CUPS Backend: Found printer %s\n", printer_uri));
1954 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
1955 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
1956 method, sizeof (method),
1957 username, sizeof (username),
1958 hostname, sizeof (hostname),
1960 resource, sizeof (resource));
1963 httpSeparate (cups_printer->printer_uri,
1971 if (strncmp (resource, "/printers/", 10) == 0)
1973 cups_printer->ppd_name = g_strdup (resource + 10);
1975 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
1978 gethostname (uri, sizeof (uri));
1979 cups_server = g_strdup (cupsServer());
1981 if (strcasecmp (uri, hostname) == 0)
1982 strcpy (hostname, "localhost");
1984 /* if the cups server is local and listening at a unix domain socket
1985 * then use the socket connection
1987 if ((strstr (hostname, "localhost") != NULL) &&
1988 (cups_server[0] == '/'))
1989 strcpy (hostname, cups_server);
1991 g_free (cups_server);
1993 cups_printer->default_cover_before = g_strdup (default_cover_before);
1994 cups_printer->default_cover_after = g_strdup (default_cover_after);
1996 cups_printer->default_number_up = default_number_up;
1998 cups_printer->hostname = g_strdup (hostname);
1999 cups_printer->port = port;
2001 cups_printer->auth_info_required = g_strdupv (auth_info_required);
2002 g_strfreev (auth_info_required);
2004 printer = GTK_PRINTER (cups_printer);
2006 if (cups_backend->default_printer != NULL &&
2007 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
2008 gtk_printer_set_is_default (printer, TRUE);
2011 gtk_print_backend_add_printer (backend, printer);
2014 g_object_ref (printer);
2016 GTK_PRINTER_CUPS (printer)->remote = remote_printer;
2018 gtk_printer_set_is_paused (printer, is_paused);
2019 gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
2021 if (!gtk_printer_is_active (printer))
2023 gtk_printer_set_is_active (printer, TRUE);
2024 gtk_printer_set_is_new (printer, TRUE);
2025 list_has_changed = TRUE;
2028 if (gtk_printer_is_new (printer))
2030 g_signal_emit_by_name (backend, "printer-added", printer);
2032 gtk_printer_set_is_new (printer, FALSE);
2036 /* Getting printer info with separate requests overwhelms cups
2037 * when the printer list has more than a handful of printers.
2039 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
2042 GTK_PRINTER_CUPS (printer)->state = state;
2043 status_changed = gtk_printer_set_job_count (printer, job_count);
2044 status_changed |= gtk_printer_set_location (printer, location);
2045 status_changed |= gtk_printer_set_description (printer, description);
2047 if (state_msg != NULL && strlen (state_msg) == 0)
2049 if (is_paused && !is_accepting_jobs)
2050 /* Translators: this is a printer status. */
2051 tmp_msg2 = g_strdup ( N_("Paused ; Rejecting Jobs"));
2052 if (is_paused && is_accepting_jobs)
2053 /* Translators: this is a printer status. */
2054 tmp_msg2 = g_strdup ( N_("Paused"));
2055 if (!is_paused && !is_accepting_jobs)
2056 /* Translators: this is a printer status. */
2057 tmp_msg2 = g_strdup ( N_("Rejecting Jobs"));
2059 if (tmp_msg2 != NULL)
2060 state_msg = tmp_msg2;
2063 /* Set description of the reason and combine it with printer-state-message. */
2064 if ( (reason_msg != NULL))
2066 for (i = 0; i < G_N_ELEMENTS (reasons); i++)
2068 if (strncmp (reason_msg, reasons[i], strlen (reasons[i])) == 0)
2070 reason_msg_desc = g_strdup_printf (reasons_descs[i], printer_name);
2077 printer_state_reason_level = 0;
2079 if (printer_state_reason_level >= 2)
2081 if (strlen (state_msg) == 0)
2082 state_msg = reason_msg_desc;
2085 tmp_msg = g_strjoin (" ; ", state_msg, reason_msg_desc, NULL);
2086 state_msg = tmp_msg;
2091 status_changed |= gtk_printer_set_state_message (printer, state_msg);
2092 status_changed |= gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
2094 if (tmp_msg != NULL)
2097 if (tmp_msg2 != NULL)
2100 if (reason_msg_desc != NULL)
2101 g_free (reason_msg_desc);
2103 /* Set printer icon according to importance
2104 (none, report, warning, error - report is omitted). */
2105 if (printer_state_reason_level == 3)
2106 gtk_printer_set_icon_name (printer, "printer-error");
2107 else if (printer_state_reason_level == 2)
2108 gtk_printer_set_icon_name (printer, "printer-warning");
2109 else if (gtk_printer_is_paused (printer))
2110 gtk_printer_set_icon_name (printer, "printer-paused");
2112 gtk_printer_set_icon_name (printer, "printer");
2115 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
2116 "printer-status-changed", printer);
2118 /* The ref is held by GtkPrintBackend, in add_printer() */
2119 g_object_unref (printer);
2125 /* look at the removed printers checklist and mark any printer
2126 as inactive if it is in the list, emitting a printer_removed signal */
2127 if (removed_printer_checklist != NULL)
2129 g_list_free_full (removed_printer_checklist, (GDestroyNotify) mark_printer_inactive);
2130 list_has_changed = TRUE;
2134 if (list_has_changed)
2135 g_signal_emit_by_name (backend, "printer-list-changed");
2137 gtk_print_backend_set_list_done (backend);
2139 if (!cups_backend->got_default_printer && remote_default_printer != NULL)
2141 cups_backend->default_printer = g_strdup (remote_default_printer);
2142 cups_backend->got_default_printer = TRUE;
2143 g_free (remote_default_printer);
2145 if (cups_backend->default_printer != NULL)
2147 GtkPrinter *default_printer = NULL;
2148 default_printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (cups_backend),
2149 cups_backend->default_printer);
2150 if (default_printer != NULL)
2152 gtk_printer_set_is_default (default_printer, TRUE);
2153 g_signal_emit_by_name (GTK_PRINT_BACKEND (cups_backend),
2154 "printer-status-changed", default_printer);
2159 GDK_THREADS_LEAVE ();
2163 update_backend_status (GtkPrintBackendCups *cups_backend,
2164 GtkCupsConnectionState state)
2168 case GTK_CUPS_CONNECTION_NOT_AVAILABLE:
2169 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_UNAVAILABLE, NULL);
2171 case GTK_CUPS_CONNECTION_AVAILABLE:
2172 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_OK, NULL);
2179 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
2181 GtkCupsConnectionState state;
2182 GtkCupsRequest *request;
2183 static const char * const pattrs[] = /* Attributes we're interested in */
2186 "printer-uri-supported",
2190 "printer-state-message",
2191 "printer-state-reasons",
2194 "printer-is-accepting-jobs",
2195 "job-sheets-supported",
2196 "job-sheets-default",
2198 "auth-info-required",
2202 if (cups_backend->reading_ppds > 0 || cups_backend->list_printers_pending)
2205 state = gtk_cups_connection_test_get_state (cups_backend->cups_connection_test);
2206 update_backend_status (cups_backend, state);
2208 if (cups_backend->list_printers_attempts == 60)
2210 cups_backend->list_printers_attempts = -1;
2211 if (cups_backend->list_printers_poll > 0)
2212 g_source_remove (cups_backend->list_printers_poll);
2213 cups_backend->list_printers_poll = gdk_threads_add_timeout (200,
2214 (GSourceFunc) cups_request_printer_list,
2217 else if (cups_backend->list_printers_attempts != -1)
2218 cups_backend->list_printers_attempts++;
2220 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2223 if (cups_backend->list_printers_attempts > 0)
2224 cups_backend->list_printers_attempts = 60;
2226 cups_backend->list_printers_pending = TRUE;
2228 request = gtk_cups_request_new_with_username (NULL,
2234 cups_backend->username);
2236 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2237 "requested-attributes", G_N_ELEMENTS (pattrs),
2240 cups_request_execute (cups_backend,
2242 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
2250 cups_get_printer_list (GtkPrintBackend *backend)
2252 GtkPrintBackendCups *cups_backend;
2254 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
2256 if (cups_backend->cups_connection_test == NULL)
2257 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
2259 if (cups_backend->list_printers_poll == 0)
2261 if (cups_request_printer_list (cups_backend))
2262 cups_backend->list_printers_poll = gdk_threads_add_timeout (50,
2263 (GSourceFunc) cups_request_printer_list,
2269 GtkPrinterCups *printer;
2275 get_ppd_data_free (GetPPDData *data)
2278 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2279 httpClose (data->http);
2280 g_io_channel_unref (data->ppd_io);
2281 g_object_unref (data->printer);
2286 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
2287 GtkCupsResult *result,
2290 GtkPrinter *printer;
2292 GDK_THREADS_ENTER ();
2295 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2297 printer = GTK_PRINTER (data->printer);
2298 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
2299 print_backend->reading_ppds--;
2301 if (gtk_cups_result_is_error (result))
2303 gboolean success = FALSE;
2305 /* if we get a 404 then it is just a raw printer without a ppd
2307 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
2308 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
2310 gtk_printer_set_has_details (printer, TRUE);
2314 g_signal_emit_by_name (printer, "details-acquired", success);
2318 /* let ppdOpenFd take over the ownership of the open file */
2319 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
2320 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
2321 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
2322 ppdLocalize (data->printer->ppd_file);
2325 ppdMarkDefaults (data->printer->ppd_file);
2327 gtk_printer_set_has_details (printer, TRUE);
2328 g_signal_emit_by_name (printer, "details-acquired", TRUE);
2331 GDK_THREADS_LEAVE ();
2335 cups_request_ppd (GtkPrinter *printer)
2338 GtkPrintBackend *print_backend;
2339 GtkPrinterCups *cups_printer;
2340 GtkCupsRequest *request;
2347 cups_printer = GTK_PRINTER_CUPS (printer);
2352 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2354 if (cups_printer->remote)
2356 GtkCupsConnectionState state;
2358 state = gtk_cups_connection_test_get_state (cups_printer->remote_cups_connection_test);
2360 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS)
2362 if (cups_printer->get_remote_ppd_attempts == 60)
2364 cups_printer->get_remote_ppd_attempts = -1;
2365 if (cups_printer->get_remote_ppd_poll > 0)
2366 g_source_remove (cups_printer->get_remote_ppd_poll);
2367 cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (200,
2368 (GSourceFunc) cups_request_ppd,
2371 else if (cups_printer->get_remote_ppd_attempts != -1)
2372 cups_printer->get_remote_ppd_attempts++;
2377 gtk_cups_connection_test_free (cups_printer->remote_cups_connection_test);
2378 cups_printer->remote_cups_connection_test = NULL;
2379 cups_printer->get_remote_ppd_poll = 0;
2380 cups_printer->get_remote_ppd_attempts = 0;
2382 if (state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2384 g_signal_emit_by_name (printer, "details-acquired", FALSE);
2389 http = httpConnectEncrypt (cups_printer->hostname,
2393 data = g_new0 (GetPPDData, 1);
2395 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
2399 #ifdef G_ENABLE_DEBUG
2400 /* If we are debugging printing don't delete the tmp files */
2401 if (!(gtk_get_debug_flags () & GTK_DEBUG_PRINTING))
2402 unlink (ppd_filename);
2404 unlink (ppd_filename);
2405 #endif /* G_ENABLE_DEBUG */
2410 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
2412 g_error_free (error);
2414 g_free (ppd_filename);
2417 g_signal_emit_by_name (printer, "details-acquired", FALSE);
2422 fchmod (fd, S_IRUSR | S_IWUSR);
2423 data->ppd_io = g_io_channel_unix_new (fd);
2424 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
2425 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
2427 data->printer = g_object_ref (printer);
2429 resource = g_strdup_printf ("/printers/%s.ppd",
2430 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
2432 print_backend = gtk_printer_get_backend (printer);
2434 request = gtk_cups_request_new_with_username (data->http,
2438 cups_printer->hostname,
2440 GTK_PRINT_BACKEND_CUPS (print_backend)->username);
2443 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
2446 cups_printer->reading_ppd = TRUE;
2447 GTK_PRINT_BACKEND_CUPS (print_backend)->reading_ppds++;
2449 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
2451 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
2453 (GDestroyNotify)get_ppd_data_free);
2456 g_free (ppd_filename);
2461 /* Ordering matters for default preference */
2462 static const char *lpoptions_locations[] = {
2463 "/etc/cups/lpoptions",
2469 cups_parse_user_default_printer (const char *filename,
2470 char **printer_name)
2473 char line[1024], *lineptr, *defname = NULL;
2475 if ((fp = g_fopen (filename, "r")) == NULL)
2478 while (fgets (line, sizeof (line), fp) != NULL)
2480 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
2484 while (isspace (*lineptr))
2491 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
2496 if (*printer_name != NULL)
2497 g_free (*printer_name);
2499 *printer_name = g_strdup (defname);
2506 cups_get_user_default_printer (char **printer_name)
2510 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
2512 if (g_path_is_absolute (lpoptions_locations[i]))
2514 cups_parse_user_default_printer (lpoptions_locations[i],
2521 filename = g_build_filename (g_get_home_dir (),
2522 lpoptions_locations[i], NULL);
2523 cups_parse_user_default_printer (filename, printer_name);
2530 cups_parse_user_options (const char *filename,
2531 const char *printer_name,
2533 cups_option_t **options)
2536 gchar line[1024], *lineptr, *name;
2538 if ((fp = g_fopen (filename, "r")) == NULL)
2541 while (fgets (line, sizeof (line), fp) != NULL)
2543 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
2545 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
2550 /* Skip leading whitespace */
2551 while (isspace (*lineptr))
2557 /* NUL-terminate the name, stripping the instance name */
2559 while (!isspace (*lineptr) && *lineptr)
2561 if (*lineptr == '/')
2571 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
2574 /* We found our printer, parse the options */
2575 num_options = cupsParseOptions (lineptr, num_options, options);
2584 cups_get_user_options (const char *printer_name,
2586 cups_option_t **options)
2590 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
2592 if (g_path_is_absolute (lpoptions_locations[i]))
2594 num_options = cups_parse_user_options (lpoptions_locations[i],
2603 filename = g_build_filename (g_get_home_dir (),
2604 lpoptions_locations[i], NULL);
2605 num_options = cups_parse_user_options (filename, printer_name,
2606 num_options, options);
2614 /* This function requests default printer from a CUPS server in regular intervals.
2615 * In the case of unreachable CUPS server the request is repeated later.
2616 * The default printer is not requested in the case of previous success.
2619 cups_get_default_printer (GtkPrintBackendCups *backend)
2621 GtkPrintBackendCups *cups_backend;
2623 cups_backend = backend;
2625 if (cups_backend->cups_connection_test == NULL)
2626 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
2628 if (cups_backend->default_printer_poll == 0)
2630 if (cups_request_default_printer (cups_backend))
2631 cups_backend->default_printer_poll = gdk_threads_add_timeout (200,
2632 (GSourceFunc) cups_request_default_printer,
2637 /* This function gets default printer from local settings.*/
2639 cups_get_local_default_printer (GtkPrintBackendCups *backend)
2644 if ((str = g_getenv ("LPDEST")) != NULL)
2646 backend->default_printer = g_strdup (str);
2647 backend->got_default_printer = TRUE;
2650 else if ((str = g_getenv ("PRINTER")) != NULL &&
2651 strcmp (str, "lp") != 0)
2653 backend->default_printer = g_strdup (str);
2654 backend->got_default_printer = TRUE;
2658 /* Figure out user setting for default printer */
2659 cups_get_user_default_printer (&name);
2662 backend->default_printer = name;
2663 backend->got_default_printer = TRUE;
2669 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
2670 GtkCupsResult *result,
2674 ipp_attribute_t *attr;
2675 GtkPrinter *printer;
2677 GDK_THREADS_ENTER ();
2679 if (gtk_cups_result_is_error (result))
2681 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
2682 gtk_cups_result_get_error_code (result) == 1)
2684 /* Canceled by user, stop popping up more password dialogs */
2685 if (print_backend->list_printers_poll > 0)
2686 g_source_remove (print_backend->list_printers_poll);
2687 print_backend->list_printers_poll = 0;
2693 response = gtk_cups_result_get_response (result);
2695 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
2696 print_backend->default_printer = g_strdup (attr->values[0].string.text);
2698 print_backend->got_default_printer = TRUE;
2700 if (print_backend->default_printer != NULL)
2702 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (print_backend), print_backend->default_printer);
2703 if (printer != NULL)
2705 gtk_printer_set_is_default (printer, TRUE);
2706 g_signal_emit_by_name (GTK_PRINT_BACKEND (print_backend), "printer-status-changed", printer);
2710 /* Make sure to kick off get_printers if we are polling it,
2711 * as we could have blocked this reading the default printer
2713 if (print_backend->list_printers_poll != 0)
2714 cups_request_printer_list (print_backend);
2716 GDK_THREADS_LEAVE ();
2720 cups_request_default_printer (GtkPrintBackendCups *print_backend)
2722 GtkCupsConnectionState state;
2723 GtkCupsRequest *request;
2725 state = gtk_cups_connection_test_get_state (print_backend->cups_connection_test);
2726 update_backend_status (print_backend, state);
2728 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2731 request = gtk_cups_request_new_with_username (NULL,
2737 print_backend->username);
2739 cups_request_execute (print_backend,
2741 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
2742 g_object_ref (print_backend),
2749 cups_printer_request_details (GtkPrinter *printer)
2751 GtkPrinterCups *cups_printer;
2753 cups_printer = GTK_PRINTER_CUPS (printer);
2754 if (!cups_printer->reading_ppd &&
2755 gtk_printer_cups_get_ppd (cups_printer) == NULL)
2757 if (cups_printer->remote)
2759 if (cups_printer->get_remote_ppd_poll == 0)
2761 cups_printer->remote_cups_connection_test = gtk_cups_connection_test_new (cups_printer->hostname);
2763 if (cups_request_ppd (printer))
2764 cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (50,
2765 (GSourceFunc) cups_request_ppd,
2770 cups_request_ppd (printer);
2775 ppd_text_to_utf8 (ppd_file_t *ppd_file,
2778 const char *encoding = NULL;
2781 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
2783 return g_strdup (text);
2785 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
2787 encoding = "ISO-8859-1";
2789 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
2791 encoding = "ISO-8859-2";
2793 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
2795 encoding = "ISO-8859-5";
2797 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
2799 encoding = "SHIFT-JIS";
2801 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
2803 encoding = "MACINTOSH";
2805 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
2807 encoding = "WINDOWS-1252";
2811 /* Fallback, try iso-8859-1... */
2812 encoding = "ISO-8859-1";
2815 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
2820 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
2821 res = g_strdup ("???");
2827 /* TODO: Add more translations for common settings here */
2829 static const struct {
2830 const char *keyword;
2831 const char *translation;
2832 } cups_option_translations[] = {
2833 { "Duplex", N_("Two Sided") },
2834 { "MediaType", N_("Paper Type") },
2835 { "InputSlot", N_("Paper Source") },
2836 { "OutputBin", N_("Output Tray") },
2837 { "Resolution", N_("Resolution") },
2838 { "PreFilter", N_("GhostScript pre-filtering") },
2842 static const struct {
2843 const char *keyword;
2845 const char *translation;
2846 } cups_choice_translations[] = {
2847 { "Duplex", "None", N_("One Sided") },
2848 /* Translators: this is an option of "Two Sided" */
2849 { "Duplex", "DuplexNoTumble", N_("Long Edge (Standard)") },
2850 /* Translators: this is an option of "Two Sided" */
2851 { "Duplex", "DuplexTumble", N_("Short Edge (Flip)") },
2852 /* Translators: this is an option of "Paper Source" */
2853 { "InputSlot", "Auto", N_("Auto Select") },
2854 /* Translators: this is an option of "Paper Source" */
2855 { "InputSlot", "AutoSelect", N_("Auto Select") },
2856 /* Translators: this is an option of "Paper Source" */
2857 { "InputSlot", "Default", N_("Printer Default") },
2858 /* Translators: this is an option of "Paper Source" */
2859 { "InputSlot", "None", N_("Printer Default") },
2860 /* Translators: this is an option of "Paper Source" */
2861 { "InputSlot", "PrinterDefault", N_("Printer Default") },
2862 /* Translators: this is an option of "Paper Source" */
2863 { "InputSlot", "Unspecified", N_("Auto Select") },
2864 /* Translators: this is an option of "Resolution" */
2865 { "Resolution", "default", N_("Printer Default") },
2866 /* Translators: this is an option of "GhostScript" */
2867 { "PreFilter", "EmbedFonts", N_("Embed GhostScript fonts only") },
2868 /* Translators: this is an option of "GhostScript" */
2869 { "PreFilter", "Level1", N_("Convert to PS level 1") },
2870 /* Translators: this is an option of "GhostScript" */
2871 { "PreFilter", "Level2", N_("Convert to PS level 2") },
2872 /* Translators: this is an option of "GhostScript" */
2873 { "PreFilter", "No", N_("No pre-filtering") },
2876 static const struct {
2878 const char *translation;
2879 } cups_group_translations[] = {
2880 /* Translators: "Miscellaneous" is the label for a button, that opens
2881 up an extra panel of settings in a print dialog. */
2882 { "Miscellaneous", N_("Miscellaneous") },
2885 static const struct {
2886 const char *ppd_keyword;
2888 } ppd_option_names[] = {
2889 {"Duplex", "gtk-duplex" },
2890 {"MediaType", "gtk-paper-type"},
2891 {"InputSlot", "gtk-paper-source"},
2892 {"OutputBin", "gtk-output-tray"},
2895 static const struct {
2896 const char *lpoption;
2898 } lpoption_names[] = {
2899 {"number-up", "gtk-n-up" },
2900 {"number-up-layout", "gtk-n-up-layout"},
2901 {"job-billing", "gtk-billing-info"},
2902 {"job-priority", "gtk-job-prio"},
2905 /* keep sorted when changing */
2906 static const char *color_option_whitelist[] = {
2907 "BRColorEnhancement",
2913 "BlackSubstitution",
2921 "RPSBlackOverPrint",
2925 /* keep sorted when changing */
2926 static const char *color_group_whitelist[] = {
2933 "HPColorOptionsPanel",
2936 /* keep sorted when changing */
2937 static const char *image_quality_option_whitelist[] = {
2939 "BRHalfTonePattern",
2949 "HPGraphicsHalftone",
2964 /* keep sorted when changing */
2965 static const char *image_quality_group_whitelist[] = {
2972 /* keep sorted when changing */
2973 static const char * finishing_option_whitelist[] = {
2986 "StapleOrientation",
2992 /* keep sorted when changing */
2993 static const char *finishing_group_whitelist[] = {
3002 /* keep sorted when changing */
3003 static const char *cups_option_blacklist[] = {
3012 get_option_text (ppd_file_t *ppd_file,
3013 ppd_option_t *option)
3018 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
3020 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
3021 return g_strdup (_(cups_option_translations[i].translation));
3024 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
3026 /* Some ppd files have spaces in the text before the colon */
3033 get_choice_text (ppd_file_t *ppd_file,
3034 ppd_choice_t *choice)
3037 ppd_option_t *option = choice->option;
3038 const char *keyword = option->keyword;
3040 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
3042 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
3043 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
3044 return g_strdup (_(cups_choice_translations[i].translation));
3046 return ppd_text_to_utf8 (ppd_file, choice->text);
3050 group_has_option (ppd_group_t *group,
3051 ppd_option_t *option)
3058 if (group->num_options > 0 &&
3059 option >= group->options && option < group->options + group->num_options)
3062 for (i = 0; i < group->num_subgroups; i++)
3064 if (group_has_option (&group->subgroups[i],option))
3071 set_option_off (GtkPrinterOption *option)
3073 /* Any of these will do, _set only applies the value
3074 * if its allowed of the option */
3075 gtk_printer_option_set (option, "False");
3076 gtk_printer_option_set (option, "Off");
3077 gtk_printer_option_set (option, "None");
3081 value_is_off (const char *value)
3083 return (strcasecmp (value, "None") == 0 ||
3084 strcasecmp (value, "Off") == 0 ||
3085 strcasecmp (value, "False") == 0);
3089 ppd_group_name (ppd_group_t *group)
3091 #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)
3099 available_choices (ppd_file_t *ppd,
3100 ppd_option_t *option,
3101 ppd_choice_t ***available,
3102 gboolean keep_if_only_one_option)
3104 ppd_option_t *other_option;
3107 ppd_const_t *constraint;
3108 const char *choice, *other_choice;
3109 ppd_option_t *option1, *option2;
3110 ppd_group_t *installed_options;
3112 gboolean all_default;
3118 conflicts = g_new0 (char, option->num_choices);
3120 installed_options = NULL;
3121 for (i = 0; i < ppd->num_groups; i++)
3125 name = ppd_group_name (&ppd->groups[i]);
3126 if (strcmp (name, "InstallableOptions") == 0)
3128 installed_options = &ppd->groups[i];
3133 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
3135 option1 = ppdFindOption (ppd, constraint->option1);
3136 if (option1 == NULL)
3139 option2 = ppdFindOption (ppd, constraint->option2);
3140 if (option2 == NULL)
3143 if (option == option1)
3145 choice = constraint->choice1;
3146 other_option = option2;
3147 other_choice = constraint->choice2;
3149 else if (option == option2)
3151 choice = constraint->choice2;
3152 other_option = option1;
3153 other_choice = constraint->choice1;
3158 /* We only care of conflicts with installed_options and
3160 if (!group_has_option (installed_options, other_option) &&
3161 (strcmp (other_option->keyword, "PageSize") != 0))
3164 if (*other_choice == 0)
3166 /* Conflict only if the installed option is not off */
3167 if (value_is_off (other_option->defchoice))
3170 /* Conflict if the installed option has the specified default */
3171 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
3176 /* Conflict with all non-off choices */
3177 for (j = 0; j < option->num_choices; j++)
3179 if (!value_is_off (option->choices[j].choice))
3185 for (j = 0; j < option->num_choices; j++)
3187 if (strcasecmp (option->choices[j].choice, choice) == 0)
3195 for (j = 0; j < option->num_choices; j++)
3199 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
3200 all_default = FALSE;
3203 if ((all_default && !keep_if_only_one_option) ||
3204 (num_conflicts == option->num_choices))
3211 /* Some ppds don't have a "use printer default" option for
3212 * InputSlot. This means you always have to select a particular slot,
3213 * and you can't auto-pick source based on the paper size. To support
3214 * this we always add an auto option if there isn't one already. If
3215 * the user chooses the generated option we don't send any InputSlot
3216 * value when printing. The way we detect existing auto-cases is based
3217 * on feedback from Michael Sweet of cups fame.
3220 if (strcmp (option->keyword, "InputSlot") == 0)
3222 gboolean found_auto = FALSE;
3223 for (j = 0; j < option->num_choices; j++)
3227 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
3228 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
3229 strcmp (option->choices[j].choice, "Default") == 0 ||
3230 strcmp (option->choices[j].choice, "None") == 0 ||
3231 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
3232 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
3233 option->choices[j].code == NULL ||
3234 option->choices[j].code[0] == 0)
3248 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
3251 for (j = 0; j < option->num_choices; j++)
3254 (*available)[i++] = &option->choices[j];
3258 (*available)[i++] = NULL;
3263 return option->num_choices - num_conflicts + add_auto;
3266 static GtkPrinterOption *
3267 create_pickone_option (ppd_file_t *ppd_file,
3268 ppd_option_t *ppd_option,
3269 const gchar *gtk_name)
3271 GtkPrinterOption *option;
3272 ppd_choice_t **available;
3276 ppd_coption_t *coption;
3278 g_assert (ppd_option->ui == PPD_UI_PICKONE);
3282 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
3286 /* right now only support one parameter per custom option
3287 * if more than one print warning and only offer the default choices
3290 label = get_option_text (ppd_file, ppd_option);
3292 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
3296 ppd_cparam_t *cparam;
3298 cparam = ppdFirstCustomParam (coption);
3300 if (ppdNextCustomParam (coption) == NULL)
3302 switch (cparam->type)
3304 case PPD_CUSTOM_INT:
3305 option = gtk_printer_option_new (gtk_name, label,
3306 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
3308 case PPD_CUSTOM_PASSCODE:
3309 option = gtk_printer_option_new (gtk_name, label,
3310 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
3312 case PPD_CUSTOM_PASSWORD:
3313 option = gtk_printer_option_new (gtk_name, label,
3314 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
3316 case PPD_CUSTOM_REAL:
3317 option = gtk_printer_option_new (gtk_name, label,
3318 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
3320 case PPD_CUSTOM_STRING:
3321 option = gtk_printer_option_new (gtk_name, label,
3322 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
3324 #ifdef PRINT_IGNORED_OPTIONS
3325 case PPD_CUSTOM_POINTS:
3326 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
3328 case PPD_CUSTOM_CURVE:
3329 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
3331 case PPD_CUSTOM_INVCURVE:
3332 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
3339 #ifdef PRINT_IGNORED_OPTIONS
3341 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
3346 option = gtk_printer_option_new (gtk_name, label,
3347 GTK_PRINTER_OPTION_TYPE_PICKONE);
3350 gtk_printer_option_allocate_choices (option, n_choices);
3351 for (i = 0; i < n_choices; i++)
3353 if (available[i] == NULL)
3355 /* This was auto-added */
3356 option->choices[i] = g_strdup ("gtk-ignore-value");
3357 option->choices_display[i] = g_strdup (_("Printer Default"));
3361 option->choices[i] = g_strdup (available[i]->choice);
3362 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
3366 if (option->type != GTK_PRINTER_OPTION_TYPE_PICKONE)
3368 if (g_str_has_prefix (ppd_option->defchoice, "Custom."))
3369 gtk_printer_option_set (option, ppd_option->defchoice + 7);
3371 gtk_printer_option_set (option, ppd_option->defchoice);
3375 gtk_printer_option_set (option, ppd_option->defchoice);
3378 #ifdef PRINT_IGNORED_OPTIONS
3380 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
3387 static GtkPrinterOption *
3388 create_boolean_option (ppd_file_t *ppd_file,
3389 ppd_option_t *ppd_option,
3390 const gchar *gtk_name)
3392 GtkPrinterOption *option;
3393 ppd_choice_t **available;
3397 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
3401 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
3404 label = get_option_text (ppd_file, ppd_option);
3405 option = gtk_printer_option_new (gtk_name, label,
3406 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
3409 gtk_printer_option_allocate_choices (option, 2);
3410 option->choices[0] = g_strdup ("True");
3411 option->choices_display[0] = g_strdup ("True");
3412 option->choices[1] = g_strdup ("False");
3413 option->choices_display[1] = g_strdup ("False");
3415 gtk_printer_option_set (option, ppd_option->defchoice);
3417 #ifdef PRINT_IGNORED_OPTIONS
3419 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
3427 get_ppd_option_name (const gchar *keyword)
3431 for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
3432 if (strcmp (ppd_option_names[i].ppd_keyword, keyword) == 0)
3433 return g_strdup (ppd_option_names[i].name);
3435 return g_strdup_printf ("cups-%s", keyword);
3439 get_lpoption_name (const gchar *lpoption)
3443 for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
3444 if (strcmp (ppd_option_names[i].ppd_keyword, lpoption) == 0)
3445 return g_strdup (ppd_option_names[i].name);
3447 for (i = 0; i < G_N_ELEMENTS (lpoption_names); i++)
3448 if (strcmp (lpoption_names[i].lpoption, lpoption) == 0)
3449 return g_strdup (lpoption_names[i].name);
3451 return g_strdup_printf ("cups-%s", lpoption);
3455 strptr_cmp (const void *a,
3458 char **aa = (char **)a;
3459 char **bb = (char **)b;
3460 return strcmp (*aa, *bb);
3465 string_in_table (gchar *str,
3466 const gchar *table[],
3469 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
3472 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
3475 handle_option (GtkPrinterOptionSet *set,
3476 ppd_file_t *ppd_file,
3477 ppd_option_t *ppd_option,
3478 ppd_group_t *toplevel_group,
3479 GtkPrintSettings *settings)
3481 GtkPrinterOption *option;
3485 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
3488 name = get_ppd_option_name (ppd_option->keyword);
3491 if (ppd_option->ui == PPD_UI_PICKONE)
3493 option = create_pickone_option (ppd_file, ppd_option, name);
3495 else if (ppd_option->ui == PPD_UI_BOOLEAN)
3497 option = create_boolean_option (ppd_file, ppd_option, name);
3499 #ifdef PRINT_IGNORED_OPTIONS
3501 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
3508 name = ppd_group_name (toplevel_group);
3509 if (STRING_IN_TABLE (name,
3510 color_group_whitelist) ||
3511 STRING_IN_TABLE (ppd_option->keyword,
3512 color_option_whitelist))
3514 option->group = g_strdup ("ColorPage");
3516 else if (STRING_IN_TABLE (name,
3517 image_quality_group_whitelist) ||
3518 STRING_IN_TABLE (ppd_option->keyword,
3519 image_quality_option_whitelist))
3521 option->group = g_strdup ("ImageQualityPage");
3523 else if (STRING_IN_TABLE (name,
3524 finishing_group_whitelist) ||
3525 STRING_IN_TABLE (ppd_option->keyword,
3526 finishing_option_whitelist))
3528 option->group = g_strdup ("FinishingPage");
3532 for (i = 0; i < G_N_ELEMENTS (cups_group_translations); i++)
3534 if (strcmp (cups_group_translations[i].name, toplevel_group->name) == 0)
3536 option->group = g_strdup (_(cups_group_translations[i].translation));
3541 if (i == G_N_ELEMENTS (cups_group_translations))
3542 option->group = g_strdup (toplevel_group->text);
3545 set_option_from_settings (option, settings);
3547 gtk_printer_option_set_add (set, option);
3554 handle_group (GtkPrinterOptionSet *set,
3555 ppd_file_t *ppd_file,
3557 ppd_group_t *toplevel_group,
3558 GtkPrintSettings *settings)
3563 /* Ignore installable options */
3564 name = ppd_group_name (toplevel_group);
3565 if (strcmp (name, "InstallableOptions") == 0)
3568 for (i = 0; i < group->num_options; i++)
3569 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
3571 for (i = 0; i < group->num_subgroups; i++)
3572 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
3579 GtkPrintSettings *settings;
3580 GtkPrinter *printer;
3581 } GtkPrintBackendCupsColordHelper;
3584 colord_printer_option_set_changed_cb (GtkPrinterOptionSet *set,
3585 GtkPrintBackendCupsColordHelper *helper)
3587 gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (helper->printer),
3593 static GtkPrinterOptionSet *
3594 cups_printer_get_options (GtkPrinter *printer,
3595 GtkPrintSettings *settings,
3596 GtkPageSetup *page_setup,
3597 GtkPrintCapabilities capabilities)
3599 GtkPrinterOptionSet *set;
3600 GtkPrinterOption *option;
3601 ppd_file_t *ppd_file;
3603 char *print_at[] = { "now", "at", "on-hold" };
3604 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
3605 char *prio[] = {"100", "80", "50", "30" };
3606 /* Translators: These strings name the possible values of the
3607 * job priority option in the print dialog
3609 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
3610 char *n_up_layout[] = { "lrtb", "lrbt", "rltb", "rlbt", "tblr", "tbrl", "btlr", "btrl" };
3611 /* Translators: These strings name the possible arrangements of
3612 * multiple pages on a sheet when printing
3614 char *n_up_layout_display[] = { N_("Left to right, top to bottom"), N_("Left to right, bottom to top"),
3615 N_("Right to left, top to bottom"), N_("Right to left, bottom to top"),
3616 N_("Top to bottom, left to right"), N_("Top to bottom, right to left"),
3617 N_("Bottom to top, left to right"), N_("Bottom to top, right to left") };
3620 cups_option_t *opts = NULL;
3621 GtkPrintBackendCups *backend;
3622 GtkTextDirection text_direction;
3623 GtkPrinterCups *cups_printer = NULL;
3625 GtkPrintBackendCupsColordHelper *helper;
3627 char *default_number_up;
3629 set = gtk_printer_option_set_new ();
3631 /* Cups specific, non-ppd related settings */
3633 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
3634 prio_display[i] = _(prio_display[i]);
3636 /* Translators, this string is used to label the job priority option
3637 * in the print dialog
3639 option = gtk_printer_option_new ("gtk-job-prio", _("Job Priority"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3640 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
3641 prio, prio_display);
3642 gtk_printer_option_set (option, "50");
3643 set_option_from_settings (option, settings);
3644 gtk_printer_option_set_add (set, option);
3645 g_object_unref (option);
3647 /* Translators, this string is used to label the billing info entry
3648 * in the print dialog
3650 option = gtk_printer_option_new ("gtk-billing-info", _("Billing Info"), GTK_PRINTER_OPTION_TYPE_STRING);
3651 gtk_printer_option_set (option, "");
3652 set_option_from_settings (option, settings);
3653 gtk_printer_option_set_add (set, option);
3654 g_object_unref (option);
3656 backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
3657 cups_printer = GTK_PRINTER_CUPS (printer);
3659 if (backend != NULL && printer != NULL)
3661 char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
3662 /* Translators, these strings are names for various 'standard' cover
3663 * pages that the printing system may support.
3665 char *cover_display_default[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
3666 char **cover = NULL;
3667 char **cover_display = NULL;
3668 char **cover_display_translated = NULL;
3669 gint num_of_covers = 0;
3673 /* Translators, this string is used to label the pages-per-sheet option
3674 * in the print dialog
3676 option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3677 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
3679 default_number_up = g_strdup_printf ("%d", cups_printer->default_number_up);
3680 gtk_printer_option_set (option, default_number_up);
3681 g_free (default_number_up);
3682 set_option_from_settings (option, settings);
3683 gtk_printer_option_set_add (set, option);
3684 g_object_unref (option);
3686 if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
3688 for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
3689 n_up_layout_display[i] = _(n_up_layout_display[i]);
3691 /* Translators, this string is used to label the option in the print
3692 * dialog that controls in what order multiple pages are arranged
3694 option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3695 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
3696 n_up_layout, n_up_layout_display);
3698 text_direction = gtk_widget_get_default_direction ();
3699 if (text_direction == GTK_TEXT_DIR_LTR)
3700 gtk_printer_option_set (option, "lrtb");
3702 gtk_printer_option_set (option, "rltb");
3704 set_option_from_settings (option, settings);
3705 gtk_printer_option_set_add (set, option);
3706 g_object_unref (option);
3709 num_of_covers = backend->number_of_covers;
3710 cover = g_new (char *, num_of_covers + 1);
3711 cover[num_of_covers] = NULL;
3712 cover_display = g_new (char *, num_of_covers + 1);
3713 cover_display[num_of_covers] = NULL;
3714 cover_display_translated = g_new (char *, num_of_covers + 1);
3715 cover_display_translated[num_of_covers] = NULL;
3717 for (i = 0; i < num_of_covers; i++)
3719 cover[i] = g_strdup (backend->covers[i]);
3721 for (j = 0; j < G_N_ELEMENTS (cover_default); j++)
3722 if (strcmp (cover_default[j], cover[i]) == 0)
3724 value = cover_display_default[j];
3727 cover_display[i] = (value != NULL) ? g_strdup (value) : g_strdup (backend->covers[i]);
3730 for (i = 0; i < num_of_covers; i++)
3731 cover_display_translated[i] = _(cover_display[i]);
3733 /* Translators, this is the label used for the option in the print
3734 * dialog that controls the front cover page.
3736 option = gtk_printer_option_new ("gtk-cover-before", _("Before"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3737 gtk_printer_option_choices_from_array (option, num_of_covers,
3738 cover, cover_display_translated);
3740 if (cups_printer->default_cover_before != NULL)
3741 gtk_printer_option_set (option, cups_printer->default_cover_before);
3743 gtk_printer_option_set (option, "none");
3744 set_option_from_settings (option, settings);
3745 gtk_printer_option_set_add (set, option);
3746 g_object_unref (option);
3748 /* Translators, this is the label used for the option in the print
3749 * dialog that controls the back cover page.
3751 option = gtk_printer_option_new ("gtk-cover-after", _("After"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3752 gtk_printer_option_choices_from_array (option, num_of_covers,
3753 cover, cover_display_translated);
3754 if (cups_printer->default_cover_after != NULL)
3755 gtk_printer_option_set (option, cups_printer->default_cover_after);
3757 gtk_printer_option_set (option, "none");
3758 set_option_from_settings (option, settings);
3759 gtk_printer_option_set_add (set, option);
3760 g_object_unref (option);
3763 g_strfreev (cover_display);
3764 g_free (cover_display_translated);
3767 /* Translators: this is the name of the option that controls when
3768 * a print job is printed. Possible values are 'now', a specified time,
3771 option = gtk_printer_option_new ("gtk-print-time", _("Print at"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3772 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
3773 print_at, print_at);
3774 gtk_printer_option_set (option, "now");
3775 set_option_from_settings (option, settings);
3776 gtk_printer_option_set_add (set, option);
3777 g_object_unref (option);
3779 /* Translators: this is the name of the option that allows the user
3780 * to specify a time when a print job will be printed.
3782 option = gtk_printer_option_new ("gtk-print-time-text", _("Print at time"), GTK_PRINTER_OPTION_TYPE_STRING);
3783 gtk_printer_option_set (option, "");
3784 set_option_from_settings (option, settings);
3785 gtk_printer_option_set_add (set, option);
3786 g_object_unref (option);
3788 /* Printer (ppd) specific settings */
3789 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3792 GtkPaperSize *paper_size;
3793 ppd_option_t *option;
3794 const gchar *ppd_name;
3796 ppdMarkDefaults (ppd_file);
3798 paper_size = gtk_page_setup_get_paper_size (page_setup);
3800 option = ppdFindOption (ppd_file, "PageSize");
3801 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
3804 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
3808 char width[G_ASCII_DTOSTR_BUF_SIZE];
3809 char height[G_ASCII_DTOSTR_BUF_SIZE];
3811 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3812 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3813 /* Translators: this format is used to display a custom paper
3814 * size. The two placeholders are replaced with the width and height
3815 * in points. E.g: "Custom 230.4x142.9"
3817 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
3818 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
3819 g_free (custom_name);
3822 for (i = 0; i < ppd_file->num_groups; i++)
3823 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
3826 /* Now honor the user set defaults for this printer */
3827 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
3829 for (i = 0; i < num_opts; i++)
3831 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
3834 name = get_lpoption_name (opts[i].name);
3835 if (strcmp (name, "cups-job-sheets") == 0)
3840 values = g_strsplit (opts[i].value, ",", 2);
3841 num_values = g_strv_length (values);
3843 option = gtk_printer_option_set_lookup (set, "gtk-cover-before");
3844 if (option && num_values > 0)
3845 gtk_printer_option_set (option, g_strstrip (values[0]));
3847 option = gtk_printer_option_set_lookup (set, "gtk-cover-after");
3848 if (option && num_values > 1)
3849 gtk_printer_option_set (option, g_strstrip (values[1]));
3851 g_strfreev (values);
3853 else if (strcmp (name, "cups-job-hold-until") == 0)
3855 GtkPrinterOption *option2 = NULL;
3857 option = gtk_printer_option_set_lookup (set, "gtk-print-time-text");
3858 if (option && opts[i].value)
3860 option2 = gtk_printer_option_set_lookup (set, "gtk-print-time");
3863 if (strcmp (opts[i].value, "indefinite") == 0)
3864 gtk_printer_option_set (option2, "on-hold");
3867 gtk_printer_option_set (option2, "at");
3868 gtk_printer_option_set (option, opts[i].value);
3873 else if (strcmp (name, "cups-sides") == 0)
3875 option = gtk_printer_option_set_lookup (set, "gtk-duplex");
3876 if (option && opts[i].value)
3878 if (strcmp (opts[i].value, "two-sided-short-edge") == 0)
3879 gtk_printer_option_set (option, "DuplexTumble");
3880 else if (strcmp (opts[i].value, "two-sided-long-edge") == 0)
3881 gtk_printer_option_set (option, "DuplexNoTumble");
3886 option = gtk_printer_option_set_lookup (set, name);
3888 gtk_printer_option_set (option, opts[i].value);
3893 cupsFreeOptions (num_opts, opts);
3896 /* TRANSLATORS: this this the ICC color profile to use for this job */
3897 option = gtk_printer_option_new ("colord-profile",
3898 _("Printer Profile"),
3899 GTK_PRINTER_OPTION_TYPE_INFO);
3901 /* assign it to the color page */
3902 option->group = g_strdup ("ColorPage");
3904 /* TRANSLATORS: this is when color profile information is unavailable */
3905 gtk_printer_option_set (option, _("Unavailable"));
3906 gtk_printer_option_set_add (set, option);
3908 /* watch to see if the user changed the options */
3909 helper = g_new (GtkPrintBackendCupsColordHelper, 1);
3910 helper->printer = printer;
3911 helper->settings = settings;
3912 g_signal_connect_data (set, "changed",
3913 G_CALLBACK (colord_printer_option_set_changed_cb),
3915 (GClosureNotify) g_free,
3918 /* initial coldplug */
3919 gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (printer),
3921 g_object_bind_property (printer, "profile-title",
3932 mark_option_from_set (GtkPrinterOptionSet *set,
3933 ppd_file_t *ppd_file,
3934 ppd_option_t *ppd_option)
3936 GtkPrinterOption *option;
3937 char *name = get_ppd_option_name (ppd_option->keyword);
3939 option = gtk_printer_option_set_lookup (set, name);
3942 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
3949 mark_group_from_set (GtkPrinterOptionSet *set,
3950 ppd_file_t *ppd_file,
3955 for (i = 0; i < group->num_options; i++)
3956 mark_option_from_set (set, ppd_file, &group->options[i]);
3958 for (i = 0; i < group->num_subgroups; i++)
3959 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
3963 set_conflicts_from_option (GtkPrinterOptionSet *set,
3964 ppd_file_t *ppd_file,
3965 ppd_option_t *ppd_option)
3967 GtkPrinterOption *option;
3970 if (ppd_option->conflicted)
3972 name = get_ppd_option_name (ppd_option->keyword);
3973 option = gtk_printer_option_set_lookup (set, name);
3976 gtk_printer_option_set_has_conflict (option, TRUE);
3977 #ifdef PRINT_IGNORED_OPTIONS
3979 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
3987 set_conflicts_from_group (GtkPrinterOptionSet *set,
3988 ppd_file_t *ppd_file,
3993 for (i = 0; i < group->num_options; i++)
3994 set_conflicts_from_option (set, ppd_file, &group->options[i]);
3996 for (i = 0; i < group->num_subgroups; i++)
3997 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
4001 cups_printer_mark_conflicts (GtkPrinter *printer,
4002 GtkPrinterOptionSet *options)
4004 ppd_file_t *ppd_file;
4008 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4010 if (ppd_file == NULL)
4013 ppdMarkDefaults (ppd_file);
4015 for (i = 0; i < ppd_file->num_groups; i++)
4016 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
4018 num_conflicts = ppdConflicts (ppd_file);
4020 if (num_conflicts > 0)
4022 for (i = 0; i < ppd_file->num_groups; i++)
4023 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
4026 return num_conflicts > 0;
4030 GtkPrinter *printer;
4031 GtkPrinterOptionSet *options;
4032 GtkPrintSettings *settings;
4033 ppd_file_t *ppd_file;
4038 const char *standard;
4042 map_settings_to_option (GtkPrinterOption *option,
4043 const NameMapping table[],
4045 GtkPrintSettings *settings,
4046 const gchar *standard_name,
4047 const gchar *cups_name)
4051 const char *cups_value;
4052 const char *standard_value;
4054 /* If the cups-specific setting is set, always use that */
4055 name = g_strdup_printf ("cups-%s", cups_name);
4056 cups_value = gtk_print_settings_get (settings, name);
4059 if (cups_value != NULL)
4061 gtk_printer_option_set (option, cups_value);
4065 /* Otherwise we try to convert from the general setting */
4066 standard_value = gtk_print_settings_get (settings, standard_name);
4067 if (standard_value == NULL)
4070 for (i = 0; i < n_elements; i++)
4072 if (table[i].cups == NULL && table[i].standard == NULL)
4074 gtk_printer_option_set (option, standard_value);
4077 else if (table[i].cups == NULL &&
4078 strcmp (table[i].standard, standard_value) == 0)
4080 set_option_off (option);
4083 else if (strcmp (table[i].standard, standard_value) == 0)
4085 gtk_printer_option_set (option, table[i].cups);
4092 map_option_to_settings (const gchar *value,
4093 const NameMapping table[],
4095 GtkPrintSettings *settings,
4096 const gchar *standard_name,
4097 const gchar *cups_name)
4102 for (i = 0; i < n_elements; i++)
4104 if (table[i].cups == NULL && table[i].standard == NULL)
4106 gtk_print_settings_set (settings,
4111 else if (table[i].cups == NULL && table[i].standard != NULL)
4113 if (value_is_off (value))
4115 gtk_print_settings_set (settings,
4121 else if (strcmp (table[i].cups, value) == 0)
4123 gtk_print_settings_set (settings,
4130 /* Always set the corresponding cups-specific setting */
4131 name = g_strdup_printf ("cups-%s", cups_name);
4132 gtk_print_settings_set (settings, name, value);
4137 static const NameMapping paper_source_map[] = {
4138 { "Lower", "lower"},
4139 { "Middle", "middle"},
4140 { "Upper", "upper"},
4142 { "Envelope", "envelope"},
4143 { "Cassette", "cassette"},
4144 { "LargeCapacity", "large-capacity"},
4145 { "AnySmallFormat", "small-format"},
4146 { "AnyLargeFormat", "large-format"},
4150 static const NameMapping output_tray_map[] = {
4151 { "Upper", "upper"},
4152 { "Lower", "lower"},
4157 static const NameMapping duplex_map[] = {
4158 { "DuplexTumble", "vertical" },
4159 { "DuplexNoTumble", "horizontal" },
4163 static const NameMapping output_mode_map[] = {
4164 { "Standard", "normal" },
4165 { "Normal", "normal" },
4166 { "Draft", "draft" },
4167 { "Fast", "draft" },
4170 static const NameMapping media_type_map[] = {
4171 { "Transparency", "transparency"},
4172 { "Standard", "stationery"},
4176 static const NameMapping all_map[] = {
4182 set_option_from_settings (GtkPrinterOption *option,
4183 GtkPrintSettings *settings)
4185 const char *cups_value;
4188 if (settings == NULL)
4191 if (strcmp (option->name, "gtk-paper-source") == 0)
4192 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
4193 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
4194 else if (strcmp (option->name, "gtk-output-tray") == 0)
4195 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
4196 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
4197 else if (strcmp (option->name, "gtk-duplex") == 0)
4198 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
4199 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
4200 else if (strcmp (option->name, "cups-OutputMode") == 0)
4201 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
4202 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
4203 else if (strcmp (option->name, "cups-Resolution") == 0)
4205 cups_value = gtk_print_settings_get (settings, option->name);
4207 gtk_printer_option_set (option, cups_value);
4210 if (gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION, -1) != -1 ||
4211 gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_X, -1) != -1 ||
4212 gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_Y, -1) != -1 ||
4213 option->value == NULL || option->value[0] == '\0')
4215 int res = gtk_print_settings_get_resolution (settings);
4216 int res_x = gtk_print_settings_get_resolution_x (settings);
4217 int res_y = gtk_print_settings_get_resolution_y (settings);
4221 value = g_strdup_printf ("%dx%ddpi", res_x, res_y);
4222 gtk_printer_option_set (option, value);
4227 value = g_strdup_printf ("%ddpi", res);
4228 gtk_printer_option_set (option, value);
4234 else if (strcmp (option->name, "gtk-paper-type") == 0)
4235 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
4236 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
4237 else if (strcmp (option->name, "gtk-n-up") == 0)
4239 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
4240 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
4242 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
4244 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
4245 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
4247 else if (strcmp (option->name, "gtk-billing-info") == 0)
4249 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
4251 gtk_printer_option_set (option, cups_value);
4253 else if (strcmp (option->name, "gtk-job-prio") == 0)
4255 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
4257 gtk_printer_option_set (option, cups_value);
4259 else if (strcmp (option->name, "gtk-cover-before") == 0)
4261 cups_value = gtk_print_settings_get (settings, "cover-before");
4263 gtk_printer_option_set (option, cups_value);
4265 else if (strcmp (option->name, "gtk-cover-after") == 0)
4267 cups_value = gtk_print_settings_get (settings, "cover-after");
4269 gtk_printer_option_set (option, cups_value);
4271 else if (strcmp (option->name, "gtk-print-time") == 0)
4273 cups_value = gtk_print_settings_get (settings, "print-at");
4275 gtk_printer_option_set (option, cups_value);
4277 else if (strcmp (option->name, "gtk-print-time-text") == 0)
4279 cups_value = gtk_print_settings_get (settings, "print-at-time");
4281 gtk_printer_option_set (option, cups_value);
4283 else if (g_str_has_prefix (option->name, "cups-"))
4285 cups_value = gtk_print_settings_get (settings, option->name);
4287 gtk_printer_option_set (option, cups_value);
4292 foreach_option_get_settings (GtkPrinterOption *option,
4295 struct OptionData *data = user_data;
4296 GtkPrintSettings *settings = data->settings;
4299 value = option->value;
4301 if (strcmp (option->name, "gtk-paper-source") == 0)
4302 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
4303 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
4304 else if (strcmp (option->name, "gtk-output-tray") == 0)
4305 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
4306 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
4307 else if (strcmp (option->name, "gtk-duplex") == 0)
4308 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
4309 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
4310 else if (strcmp (option->name, "cups-OutputMode") == 0)
4311 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
4312 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
4313 else if (strcmp (option->name, "cups-Resolution") == 0)
4315 int res, res_x, res_y;
4317 if (sscanf (value, "%dx%ddpi", &res_x, &res_y) == 2)
4319 if (res_x > 0 && res_y > 0)
4320 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
4322 else if (sscanf (value, "%ddpi", &res) == 1)
4325 gtk_print_settings_set_resolution (settings, res);
4328 gtk_print_settings_set (settings, option->name, value);
4330 else if (strcmp (option->name, "gtk-paper-type") == 0)
4331 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
4332 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
4333 else if (strcmp (option->name, "gtk-n-up") == 0)
4334 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
4335 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
4336 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
4337 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
4338 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
4339 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
4340 gtk_print_settings_set (settings, "cups-job-billing", value);
4341 else if (strcmp (option->name, "gtk-job-prio") == 0)
4342 gtk_print_settings_set (settings, "cups-job-priority", value);
4343 else if (strcmp (option->name, "gtk-cover-before") == 0)
4344 gtk_print_settings_set (settings, "cover-before", value);
4345 else if (strcmp (option->name, "gtk-cover-after") == 0)
4346 gtk_print_settings_set (settings, "cover-after", value);
4347 else if (strcmp (option->name, "gtk-print-time") == 0)
4348 gtk_print_settings_set (settings, "print-at", value);
4349 else if (strcmp (option->name, "gtk-print-time-text") == 0)
4350 gtk_print_settings_set (settings, "print-at-time", value);
4351 else if (g_str_has_prefix (option->name, "cups-"))
4352 gtk_print_settings_set (settings, option->name, value);
4356 supports_am_pm (void)
4358 struct tm tmp_tm = { 0 };
4362 length = strftime (time, sizeof (time), "%p", &tmp_tm);
4367 /* Converts local time to UTC time. Local time has to be in one of these
4368 * formats: HH:MM:SS, HH:MM, HH:MM:SS {am, pm}, HH:MM {am, pm}, HH {am, pm},
4369 * {am, pm} HH:MM:SS, {am, pm} HH:MM, {am, pm} HH.
4370 * Returns a newly allocated string holding UTC time in HH:MM:SS format
4374 localtime_to_utctime (const char *local_time)
4376 const char *formats_0[] = {" %I : %M : %S %p ", " %p %I : %M : %S ",
4378 " %I : %M %p ", " %p %I : %M ",
4380 " %I %p ", " %p %I "};
4381 const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
4382 const char *end = NULL;
4383 struct tm *actual_local_time;
4384 struct tm *actual_utc_time;
4385 struct tm local_print_time;
4386 struct tm utc_print_time;
4387 struct tm diff_time;
4388 gchar *utc_time = NULL;
4391 if (local_time == NULL || local_time[0] == '\0')
4394 n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
4396 for (i = 0; i < n; i++)
4398 local_print_time.tm_hour = 0;
4399 local_print_time.tm_min = 0;
4400 local_print_time.tm_sec = 0;
4402 if (supports_am_pm ())
4403 end = strptime (local_time, formats_0[i], &local_print_time);
4405 end = strptime (local_time, formats_1[i], &local_print_time);
4407 if (end != NULL && end[0] == '\0')
4411 if (end != NULL && end[0] == '\0')
4416 actual_utc_time = g_memdup (gmtime (&rawtime), sizeof (struct tm));
4417 actual_local_time = g_memdup (localtime (&rawtime), sizeof (struct tm));
4419 diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
4420 diff_time.tm_min = actual_utc_time->tm_min - actual_local_time->tm_min;
4421 diff_time.tm_sec = actual_utc_time->tm_sec - actual_local_time->tm_sec;
4423 utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
4424 utc_print_time.tm_min = ((local_print_time.tm_min + diff_time.tm_min) + 60) % 60;
4425 utc_print_time.tm_sec = ((local_print_time.tm_sec + diff_time.tm_sec) + 60) % 60;
4427 utc_time = g_strdup_printf ("%02d:%02d:%02d",
4428 utc_print_time.tm_hour,
4429 utc_print_time.tm_min,
4430 utc_print_time.tm_sec);
4437 cups_printer_get_settings_from_options (GtkPrinter *printer,
4438 GtkPrinterOptionSet *options,
4439 GtkPrintSettings *settings)
4441 struct OptionData data;
4442 const char *print_at, *print_at_time;
4444 data.printer = printer;
4445 data.options = options;
4446 data.settings = settings;
4447 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4449 if (data.ppd_file != NULL)
4451 GtkPrinterOption *cover_before, *cover_after;
4453 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
4455 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
4456 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
4457 if (cover_before && cover_after)
4459 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
4460 gtk_print_settings_set (settings, "cups-job-sheets", value);
4464 print_at = gtk_print_settings_get (settings, "print-at");
4465 print_at_time = gtk_print_settings_get (settings, "print-at-time");
4467 if (strcmp (print_at, "at") == 0)
4469 gchar *utc_time = NULL;
4471 utc_time = localtime_to_utctime (print_at_time);
4473 if (utc_time != NULL)
4475 gtk_print_settings_set (settings, "cups-job-hold-until", utc_time);
4479 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
4481 else if (strcmp (print_at, "on-hold") == 0)
4482 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
4487 cups_printer_prepare_for_print (GtkPrinter *printer,
4488 GtkPrintJob *print_job,
4489 GtkPrintSettings *settings,
4490 GtkPageSetup *page_setup)
4492 GtkPrintPages pages;
4493 GtkPageRange *ranges;
4495 GtkPageSet page_set;
4496 GtkPaperSize *paper_size;
4497 const char *ppd_paper_name;
4500 pages = gtk_print_settings_get_print_pages (settings);
4501 gtk_print_job_set_pages (print_job, pages);
4503 if (pages == GTK_PRINT_PAGES_RANGES)
4504 ranges = gtk_print_settings_get_page_ranges (settings, &n_ranges);
4511 gtk_print_job_set_page_ranges (print_job, ranges, n_ranges);
4512 if (gtk_print_settings_get_collate (settings))
4513 gtk_print_settings_set (settings, "cups-Collate", "True");
4514 gtk_print_job_set_collate (print_job, FALSE);
4516 if (gtk_print_settings_get_reverse (settings))
4517 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
4518 gtk_print_job_set_reverse (print_job, FALSE);
4520 if (gtk_print_settings_get_n_copies (settings) > 1)
4521 gtk_print_settings_set_int (settings, "cups-copies",
4522 gtk_print_settings_get_n_copies (settings));
4523 gtk_print_job_set_num_copies (print_job, 1);
4525 scale = gtk_print_settings_get_scale (settings);
4527 gtk_print_job_set_scale (print_job, scale / 100.0);
4529 page_set = gtk_print_settings_get_page_set (settings);
4530 if (page_set == GTK_PAGE_SET_EVEN)
4531 gtk_print_settings_set (settings, "cups-page-set", "even");
4532 else if (page_set == GTK_PAGE_SET_ODD)
4533 gtk_print_settings_set (settings, "cups-page-set", "odd");
4534 gtk_print_job_set_page_set (print_job, GTK_PAGE_SET_ALL);
4536 paper_size = gtk_page_setup_get_paper_size (page_setup);
4537 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
4538 if (ppd_paper_name != NULL)
4539 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
4542 char width[G_ASCII_DTOSTR_BUF_SIZE];
4543 char height[G_ASCII_DTOSTR_BUF_SIZE];
4546 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
4547 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
4548 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
4549 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
4550 g_free (custom_name);
4553 if (gtk_print_settings_get_number_up (settings) > 1)
4555 GtkNumberUpLayout layout = gtk_print_settings_get_number_up_layout (settings);
4556 GEnumClass *enum_class;
4557 GEnumValue *enum_value;
4559 switch (gtk_page_setup_get_orientation (page_setup))
4561 case GTK_PAGE_ORIENTATION_PORTRAIT:
4563 case GTK_PAGE_ORIENTATION_LANDSCAPE:
4565 layout = layout + 2 + 4 * (1 - layout / 2);
4567 layout = layout - 3 - 2 * (layout % 2);
4569 case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
4570 layout = (layout + 3 - 2 * (layout % 2)) % 4 + 4 * (layout / 4);
4572 case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
4574 layout = layout + 5 - 2 * (layout % 2);
4576 layout = layout - 6 + 4 * (1 - (layout - 4) / 2);
4580 enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
4581 enum_value = g_enum_get_value (enum_class, layout);
4582 gtk_print_settings_set (settings, "cups-number-up-layout", enum_value->value_nick);
4583 g_type_class_unref (enum_class);
4586 gtk_print_job_set_rotate (print_job, TRUE);
4589 static GtkPageSetup *
4590 create_page_setup (ppd_file_t *ppd_file,
4594 GtkPageSetup *page_setup;
4595 GtkPaperSize *paper_size;
4596 ppd_option_t *option;
4597 ppd_choice_t *choice;
4599 display_name = NULL;
4600 option = ppdFindOption (ppd_file, "PageSize");
4603 choice = ppdFindChoice (option, size->name);
4605 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
4608 if (display_name == NULL)
4609 display_name = g_strdup (size->name);
4611 page_setup = gtk_page_setup_new ();
4612 paper_size = gtk_paper_size_new_from_ppd (size->name,
4616 gtk_page_setup_set_paper_size (page_setup, paper_size);
4617 gtk_paper_size_free (paper_size);
4619 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
4620 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
4621 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
4622 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
4624 g_free (display_name);
4630 cups_printer_list_papers (GtkPrinter *printer)
4632 ppd_file_t *ppd_file;
4634 GtkPageSetup *page_setup;
4638 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4639 if (ppd_file == NULL)
4644 for (i = 0; i < ppd_file->num_sizes; i++)
4646 size = &ppd_file->sizes[i];
4648 page_setup = create_page_setup (ppd_file, size);
4650 l = g_list_prepend (l, page_setup);
4653 return g_list_reverse (l);
4656 static GtkPageSetup *
4657 cups_printer_get_default_page_size (GtkPrinter *printer)
4659 ppd_file_t *ppd_file;
4661 ppd_option_t *option;
4664 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4665 if (ppd_file == NULL)
4668 option = ppdFindOption (ppd_file, "PageSize");
4672 size = ppdPageSize (ppd_file, option->defchoice);
4676 return create_page_setup (ppd_file, size);
4680 cups_printer_get_hard_margins (GtkPrinter *printer,
4686 ppd_file_t *ppd_file;
4688 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4689 if (ppd_file == NULL)
4692 *left = ppd_file->custom_margins[0];
4693 *bottom = ppd_file->custom_margins[1];
4694 *right = ppd_file->custom_margins[2];
4695 *top = ppd_file->custom_margins[3];
4700 static GtkPrintCapabilities
4701 cups_printer_get_capabilities (GtkPrinter *printer)
4704 GTK_PRINT_CAPABILITY_COPIES |
4705 GTK_PRINT_CAPABILITY_COLLATE |
4706 GTK_PRINT_CAPABILITY_REVERSE |
4707 #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
4708 GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT |
4710 GTK_PRINT_CAPABILITY_NUMBER_UP;