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>
31 /* Cups 1.6 deprecates ppdFindAttr(), ppdFindCustomOption(),
32 * ppdFirstCustomParam(), and ppdNextCustomParam() among others. This
33 * turns off the warning so that it will compile.
35 #ifdef HAVE_CUPS_API_1_6
36 # define _PPD_DEPRECATED
39 #include <cups/cups.h>
40 #include <cups/language.h>
41 #include <cups/http.h>
45 #include <cairo-pdf.h>
48 #include <glib/gstdio.h>
49 #include <glib/gi18n-lib.h>
53 #include <gtk/gtkprintbackend.h>
54 #include <gtk/gtkunixprint.h>
55 #include <gtk/gtkprinter-private.h>
57 #include "gtkprintbackendcups.h"
58 #include "gtkprintercups.h"
60 #include "gtkcupsutils.h"
66 typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
68 #define GTK_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
69 #define GTK_IS_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_CUPS))
70 #define GTK_PRINT_BACKEND_CUPS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
72 #define _CUPS_MAX_ATTEMPTS 10
73 #define _CUPS_MAX_CHUNK_SIZE 8192
75 /* define this to see warnings about ignored ppd options */
76 #undef PRINT_IGNORED_OPTIONS
78 #define _CUPS_MAP_ATTR_INT(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].integer;}
79 #define _CUPS_MAP_ATTR_STR(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].string.text;}
81 static GType print_backend_cups_type = 0;
83 typedef void (* GtkPrintCupsResponseCallbackFunc) (GtkPrintBackend *print_backend,
84 GtkCupsResult *result,
95 } GtkPrintCupsDispatchState;
102 GtkCupsRequest *request;
103 GtkCupsPollState poll_state;
105 GtkPrintBackendCups *backend;
106 GtkPrintCupsResponseCallbackFunc callback;
107 gpointer callback_data;
109 } GtkPrintCupsDispatchWatch;
111 struct _GtkPrintBackendCupsClass
113 GtkPrintBackendClass parent_class;
116 struct _GtkPrintBackendCups
118 GtkPrintBackend parent_instance;
120 char *default_printer;
122 guint list_printers_poll;
123 guint list_printers_pending : 1;
124 gint list_printers_attempts;
125 guint got_default_printer : 1;
126 guint default_printer_poll;
127 GtkCupsConnectionTest *cups_connection_test;
131 int number_of_covers;
136 gboolean authentication_lock;
138 CdClient *colord_client;
142 static GObjectClass *backend_parent_class;
144 static void gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class);
145 static void gtk_print_backend_cups_init (GtkPrintBackendCups *impl);
146 static void gtk_print_backend_cups_finalize (GObject *object);
147 static void gtk_print_backend_cups_dispose (GObject *object);
148 static void cups_get_printer_list (GtkPrintBackend *print_backend);
149 static void cups_get_default_printer (GtkPrintBackendCups *print_backend);
150 static void cups_get_local_default_printer (GtkPrintBackendCups *print_backend);
151 static void cups_request_execute (GtkPrintBackendCups *print_backend,
152 GtkCupsRequest *request,
153 GtkPrintCupsResponseCallbackFunc callback,
155 GDestroyNotify notify);
156 static void cups_printer_get_settings_from_options (GtkPrinter *printer,
157 GtkPrinterOptionSet *options,
158 GtkPrintSettings *settings);
159 static gboolean cups_printer_mark_conflicts (GtkPrinter *printer,
160 GtkPrinterOptionSet *options);
161 static GtkPrinterOptionSet *cups_printer_get_options (GtkPrinter *printer,
162 GtkPrintSettings *settings,
163 GtkPageSetup *page_setup,
164 GtkPrintCapabilities capabilities);
165 static void cups_printer_prepare_for_print (GtkPrinter *printer,
166 GtkPrintJob *print_job,
167 GtkPrintSettings *settings,
168 GtkPageSetup *page_setup);
169 static GList * cups_printer_list_papers (GtkPrinter *printer);
170 static GtkPageSetup * cups_printer_get_default_page_size (GtkPrinter *printer);
171 static void cups_printer_request_details (GtkPrinter *printer);
172 static gboolean cups_request_default_printer (GtkPrintBackendCups *print_backend);
173 static gboolean cups_request_ppd (GtkPrinter *printer);
174 static gboolean cups_printer_get_hard_margins (GtkPrinter *printer,
179 static GtkPrintCapabilities cups_printer_get_capabilities (GtkPrinter *printer);
180 static void set_option_from_settings (GtkPrinterOption *option,
181 GtkPrintSettings *setting);
182 static void cups_begin_polling_info (GtkPrintBackendCups *print_backend,
185 static gboolean cups_job_info_poll_timeout (gpointer user_data);
186 static void gtk_print_backend_cups_print_stream (GtkPrintBackend *backend,
189 GtkPrintJobCompleteFunc callback,
191 GDestroyNotify dnotify);
192 static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter *printer,
193 GtkPrintSettings *settings,
196 GIOChannel *cache_io);
198 static void gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
199 gchar **auth_info_required,
202 void overwrite_and_free (gpointer data);
203 static gboolean is_address_local (const gchar *address);
204 static gboolean request_auth_info (gpointer data);
207 gtk_print_backend_cups_register_type (GTypeModule *module)
209 const GTypeInfo print_backend_cups_info =
211 sizeof (GtkPrintBackendCupsClass),
212 NULL, /* base_init */
213 NULL, /* base_finalize */
214 (GClassInitFunc) gtk_print_backend_cups_class_init,
215 NULL, /* class_finalize */
216 NULL, /* class_data */
217 sizeof (GtkPrintBackendCups),
219 (GInstanceInitFunc) gtk_print_backend_cups_init
222 print_backend_cups_type = g_type_module_register_type (module,
223 GTK_TYPE_PRINT_BACKEND,
224 "GtkPrintBackendCups",
225 &print_backend_cups_info, 0);
229 pb_module_init (GTypeModule *module)
232 g_print ("CUPS Backend: Initializing the CUPS print backend module\n"));
234 gtk_print_backend_cups_register_type (module);
235 gtk_printer_cups_register_type (module);
239 pb_module_exit (void)
244 G_MODULE_EXPORT GtkPrintBackend *
245 pb_module_create (void)
247 return gtk_print_backend_cups_new ();
249 /* CUPS 1.6 Getter/Setter Functions CUPS 1.6 makes private most of the
250 * IPP structures and enforces access via new getter functions, which
251 * are unfortunately not available in earlier versions. We define
252 * below those getter functions as macros for use when building
253 * against earlier CUPS versions.
255 #ifndef HAVE_CUPS_API_1_6
256 #define ippGetOperation(ipp_request) ipp_request->request.op.operation_id
257 #define ippGetInteger(attr, index) attr->values[index].integer
258 #define ippGetBoolean(attr, index) attr->values[index].boolean
259 #define ippGetString(attr, index, foo) attr->values[index].string.text
260 #define ippGetValueTag(attr) attr->value_tag
261 #define ippGetName(attr) attr->name
262 #define ippGetCount(attr) attr->num_values
263 #define ippGetGroupTag(attr) attr->group_tag
266 * GtkPrintBackendCups
269 gtk_print_backend_cups_get_type (void)
271 return print_backend_cups_type;
275 * gtk_print_backend_cups_new:
277 * Creates a new #GtkPrintBackendCups object. #GtkPrintBackendCups
278 * implements the #GtkPrintBackend interface with direct access to
279 * the filesystem using Unix/Linux API calls
281 * Return value: the new #GtkPrintBackendCups object
284 gtk_print_backend_cups_new (void)
287 g_print ("CUPS Backend: Creating a new CUPS print backend object\n"));
289 return g_object_new (GTK_TYPE_PRINT_BACKEND_CUPS, NULL);
293 gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
295 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
296 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
298 backend_parent_class = g_type_class_peek_parent (class);
300 gobject_class->finalize = gtk_print_backend_cups_finalize;
301 gobject_class->dispose = gtk_print_backend_cups_dispose;
303 backend_class->request_printer_list = cups_get_printer_list;
304 backend_class->print_stream = gtk_print_backend_cups_print_stream;
305 backend_class->printer_request_details = cups_printer_request_details;
306 backend_class->printer_create_cairo_surface = cups_printer_create_cairo_surface;
307 backend_class->printer_get_options = cups_printer_get_options;
308 backend_class->printer_mark_conflicts = cups_printer_mark_conflicts;
309 backend_class->printer_get_settings_from_options = cups_printer_get_settings_from_options;
310 backend_class->printer_prepare_for_print = cups_printer_prepare_for_print;
311 backend_class->printer_list_papers = cups_printer_list_papers;
312 backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
313 backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
314 backend_class->printer_get_capabilities = cups_printer_get_capabilities;
315 backend_class->set_password = gtk_print_backend_cups_set_password;
318 static cairo_status_t
319 _cairo_write_to_cups (void *closure,
320 const unsigned char *data,
323 GIOChannel *io = (GIOChannel *)closure;
330 g_print ("CUPS Backend: Writing %i byte chunk to temp file\n", length));
334 g_io_channel_write_chars (io, (gchar *)data, length, &written, &error);
339 g_print ("CUPS Backend: Error writing to temp file, %s\n",
342 g_error_free (error);
343 return CAIRO_STATUS_WRITE_ERROR;
347 g_print ("CUPS Backend: Wrote %"G_GSIZE_FORMAT" bytes to temp file\n", written));
353 return CAIRO_STATUS_SUCCESS;
356 static cairo_surface_t *
357 cups_printer_create_cairo_surface (GtkPrinter *printer,
358 GtkPrintSettings *settings,
361 GIOChannel *cache_io)
363 cairo_surface_t *surface;
364 ppd_file_t *ppd_file = NULL;
365 ppd_attr_t *ppd_attr = NULL;
366 ppd_attr_t *ppd_attr_res = NULL;
367 ppd_attr_t *ppd_attr_screen_freq = NULL;
368 ppd_attr_t *ppd_attr_res_screen_freq = NULL;
369 gchar *res_string = NULL;
372 if (gtk_printer_accepts_pdf (printer))
373 surface = cairo_pdf_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
375 surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
377 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
379 if (ppd_file != NULL)
381 ppd_attr = ppdFindAttr (ppd_file, "LanguageLevel", NULL);
383 if (ppd_attr != NULL)
384 level = atoi (ppd_attr->value);
386 if (gtk_print_settings_get_resolution (settings) == 0)
388 ppd_attr_res = ppdFindAttr (ppd_file, "DefaultResolution", NULL);
390 if (ppd_attr_res != NULL)
392 int res, res_x, res_y;
394 if (sscanf (ppd_attr_res->value, "%dx%ddpi", &res_x, &res_y) == 2)
396 if (res_x > 0 && res_y > 0)
397 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
399 else if (sscanf (ppd_attr_res->value, "%ddpi", &res) == 1)
402 gtk_print_settings_set_resolution (settings, res);
407 res_string = g_strdup_printf ("%ddpi",
408 gtk_print_settings_get_resolution (settings));
409 ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
412 if (ppd_attr_res_screen_freq == NULL)
414 res_string = g_strdup_printf ("%dx%ddpi",
415 gtk_print_settings_get_resolution_x (settings),
416 gtk_print_settings_get_resolution_y (settings));
417 ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
421 ppd_attr_screen_freq = ppdFindAttr (ppd_file, "ScreenFreq", NULL);
423 if (ppd_attr_res_screen_freq != NULL && atof (ppd_attr_res_screen_freq->value) > 0.0)
424 gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_res_screen_freq->value));
425 else if (ppd_attr_screen_freq != NULL && atof (ppd_attr_screen_freq->value) > 0.0)
426 gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_screen_freq->value));
429 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PS)
432 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
435 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
438 cairo_surface_set_fallback_resolution (surface,
439 2.0 * gtk_print_settings_get_printer_lpi (settings),
440 2.0 * gtk_print_settings_get_printer_lpi (settings));
446 GtkPrintJobCompleteFunc callback;
449 GDestroyNotify dnotify;
450 } CupsPrintStreamData;
453 cups_free_print_stream_data (CupsPrintStreamData *data)
456 g_print ("CUPS Backend: %s\n", G_STRFUNC));
459 data->dnotify (data->user_data);
460 g_object_unref (data->job);
465 cups_print_cb (GtkPrintBackendCups *print_backend,
466 GtkCupsResult *result,
469 GError *error = NULL;
470 CupsPrintStreamData *ps = user_data;
472 gdk_threads_enter ();
475 g_print ("CUPS Backend: %s\n", G_STRFUNC));
477 if (gtk_cups_result_is_error (result))
478 error = g_error_new_literal (gtk_print_error_quark (),
479 GTK_PRINT_ERROR_INTERNAL_ERROR,
480 gtk_cups_result_get_error_string (result));
483 ps->callback (ps->job, ps->user_data, error);
488 ipp_attribute_t *attr; /* IPP job-id attribute */
489 ipp_t *response = gtk_cups_result_get_response (result);
491 if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
492 job_id = ippGetInteger (attr, 0);
494 if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
495 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
498 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_PENDING);
499 cups_begin_polling_info (print_backend, ps->job, job_id);
503 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED_ABORTED);
507 g_error_free (error);
509 gdk_threads_leave ();
513 GtkCupsRequest *request;
514 GtkPrinterCups *printer;
518 add_cups_options (const gchar *key,
522 CupsOptionsData *data = (CupsOptionsData *) user_data;
523 GtkCupsRequest *request = data->request;
524 GtkPrinterCups *printer = data->printer;
525 gboolean custom_value = FALSE;
526 gchar *new_value = NULL;
532 if (!g_str_has_prefix (key, "cups-"))
535 if (strcmp (value, "gtk-ignore-value") == 0)
538 key = key + strlen ("cups-");
540 if (printer && printer->ppd_file)
542 ppd_coption_t *coption;
543 gboolean found = FALSE;
544 gboolean custom_values_enabled = FALSE;
546 coption = ppdFindCustomOption (printer->ppd_file, key);
547 if (coption && coption->option)
549 for (i = 0; i < coption->option->num_choices; i++)
551 /* Are custom values enabled ? */
552 if (g_str_equal (coption->option->choices[i].choice, "Custom"))
553 custom_values_enabled = TRUE;
555 /* Is the value among available choices ? */
556 if (g_str_equal (coption->option->choices[i].choice, value))
560 if (custom_values_enabled && !found)
565 /* Add "Custom." prefix to custom values if not already added. */
566 if (custom_value && !g_str_has_prefix (value, "Custom."))
568 new_value = g_strdup_printf ("Custom.%s", value);
569 gtk_cups_request_encode_option (request, key, new_value);
573 gtk_cups_request_encode_option (request, key, value);
577 gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
580 GtkPrintJobCompleteFunc callback,
582 GDestroyNotify dnotify)
584 GtkPrinterCups *cups_printer;
585 CupsPrintStreamData *ps;
586 CupsOptionsData *options_data;
587 GtkCupsRequest *request;
588 GtkPrintSettings *settings;
590 char printer_absolute_uri[HTTP_MAX_URI];
593 g_print ("CUPS Backend: %s\n", G_STRFUNC));
595 cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
596 settings = gtk_print_job_get_settings (job);
598 request = gtk_cups_request_new_with_username (NULL,
603 cups_printer->device_uri,
604 GTK_PRINT_BACKEND_CUPS (print_backend)->username);
606 httpAssembleURIf (HTTP_URI_CODING_ALL,
607 printer_absolute_uri,
608 sizeof (printer_absolute_uri),
614 gtk_printer_get_name (gtk_print_job_get_printer (job)));
616 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
617 IPP_TAG_URI, "printer-uri",
618 NULL, printer_absolute_uri);
620 title = gtk_print_job_get_title (job);
622 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
623 IPP_TAG_NAME, "job-name",
626 options_data = g_new0 (CupsOptionsData, 1);
627 options_data->request = request;
628 options_data->printer = cups_printer;
629 gtk_print_settings_foreach (settings, add_cups_options, options_data);
630 g_free (options_data);
632 ps = g_new0 (CupsPrintStreamData, 1);
633 ps->callback = callback;
634 ps->user_data = user_data;
635 ps->dnotify = dnotify;
636 ps->job = g_object_ref (job);
638 request->need_auth_info = cups_printer->auth_info_required != NULL;
639 request->auth_info_required = g_strdupv (cups_printer->auth_info_required);
641 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
643 (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
645 (GDestroyNotify)cups_free_print_stream_data);
648 void overwrite_and_free (gpointer data)
650 gchar *password = (gchar *) data;
652 if (password != NULL)
654 memset (password, 0, strlen (password));
660 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
662 backend_cups->list_printers_poll = FALSE;
663 backend_cups->got_default_printer = FALSE;
664 backend_cups->list_printers_pending = FALSE;
665 backend_cups->list_printers_attempts = 0;
666 backend_cups->reading_ppds = 0;
668 backend_cups->requests = NULL;
669 backend_cups->auth = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, overwrite_and_free);
670 backend_cups->authentication_lock = FALSE;
672 backend_cups->covers = NULL;
673 backend_cups->number_of_covers = 0;
675 backend_cups->default_printer_poll = 0;
676 backend_cups->cups_connection_test = NULL;
678 backend_cups->username = NULL;
681 backend_cups->colord_client = cd_client_new ();
684 cups_get_local_default_printer (backend_cups);
688 gtk_print_backend_cups_finalize (GObject *object)
690 GtkPrintBackendCups *backend_cups;
693 g_print ("CUPS Backend: finalizing CUPS backend module\n"));
695 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
697 g_free (backend_cups->default_printer);
698 backend_cups->default_printer = NULL;
700 g_strfreev (backend_cups->covers);
701 backend_cups->number_of_covers = 0;
703 gtk_cups_connection_test_free (backend_cups->cups_connection_test);
704 backend_cups->cups_connection_test = NULL;
706 g_hash_table_destroy (backend_cups->auth);
708 g_free (backend_cups->username);
711 g_object_unref (backend_cups->colord_client);
714 backend_parent_class->finalize (object);
718 gtk_print_backend_cups_dispose (GObject *object)
720 GtkPrintBackendCups *backend_cups;
723 g_print ("CUPS Backend: %s\n", G_STRFUNC));
725 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
727 if (backend_cups->list_printers_poll > 0)
728 g_source_remove (backend_cups->list_printers_poll);
729 backend_cups->list_printers_poll = 0;
730 backend_cups->list_printers_attempts = 0;
732 if (backend_cups->default_printer_poll > 0)
733 g_source_remove (backend_cups->default_printer_poll);
734 backend_cups->default_printer_poll = 0;
736 backend_parent_class->dispose (object);
740 is_address_local (const gchar *address)
742 if (address[0] == '/' ||
743 strcmp (address, "127.0.0.1") == 0 ||
744 strcmp (address, "[::1]") == 0)
751 gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
752 gchar **auth_info_required,
755 GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
757 char dispatch_hostname[HTTP_MAX_URI];
758 gchar *username = NULL;
759 gchar *hostname = NULL;
760 gchar *password = NULL;
764 length = g_strv_length (auth_info_required);
766 if (auth_info != NULL)
767 for (i = 0; i < length; i++)
769 if (g_strcmp0 (auth_info_required[i], "username") == 0)
770 username = g_strdup (auth_info[i]);
771 else if (g_strcmp0 (auth_info_required[i], "hostname") == 0)
772 hostname = g_strdup (auth_info[i]);
773 else if (g_strcmp0 (auth_info_required[i], "password") == 0)
774 password = g_strdup (auth_info[i]);
777 if (hostname != NULL && username != NULL && password != NULL)
779 gchar *key = g_strconcat (username, "@", hostname, NULL);
780 g_hash_table_insert (cups_backend->auth, key, g_strdup (password));
782 g_print ("CUPS backend: storing password for %s\n", key));
785 g_free (cups_backend->username);
786 cups_backend->username = g_strdup (username);
789 for (l = cups_backend->requests; l; l = l->next)
791 GtkPrintCupsDispatchWatch *dispatch = l->data;
793 httpGetHostname (dispatch->request->http, dispatch_hostname, sizeof (dispatch_hostname));
794 if (is_address_local (dispatch_hostname))
795 strcpy (dispatch_hostname, "localhost");
797 if (dispatch->request->need_auth_info)
799 if (auth_info != NULL)
801 dispatch->request->auth_info = g_new0 (gchar *, length + 1);
802 for (i = 0; i < length; i++)
803 dispatch->request->auth_info[i] = g_strdup (auth_info[i]);
805 dispatch->backend->authentication_lock = FALSE;
806 dispatch->request->need_auth_info = FALSE;
808 else if (dispatch->request->password_state == GTK_CUPS_PASSWORD_REQUESTED || auth_info == NULL)
810 overwrite_and_free (dispatch->request->password);
811 dispatch->request->password = g_strdup (password);
812 g_free (dispatch->request->username);
813 dispatch->request->username = g_strdup (username);
814 dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
815 dispatch->backend->authentication_lock = FALSE;
821 request_password (gpointer data)
823 GtkPrintCupsDispatchWatch *dispatch = data;
824 const gchar *username;
826 gchar *prompt = NULL;
828 char hostname[HTTP_MAX_URI];
829 gchar **auth_info_required;
830 gchar **auth_info_default;
831 gchar **auth_info_display;
832 gboolean *auth_info_visible;
836 if (dispatch->backend->authentication_lock)
837 return G_SOURCE_REMOVE;
839 httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
840 if (is_address_local (hostname))
841 strcpy (hostname, "localhost");
843 if (dispatch->backend->username != NULL)
844 username = dispatch->backend->username;
846 username = cupsUser ();
848 auth_info_required = g_new0 (gchar*, length + 1);
849 auth_info_required[0] = g_strdup ("hostname");
850 auth_info_required[1] = g_strdup ("username");
851 auth_info_required[2] = g_strdup ("password");
853 auth_info_default = g_new0 (gchar*, length + 1);
854 auth_info_default[0] = g_strdup (hostname);
855 auth_info_default[1] = g_strdup (username);
857 auth_info_display = g_new0 (gchar*, length + 1);
858 auth_info_display[1] = g_strdup (_("Username:"));
859 auth_info_display[2] = g_strdup (_("Password:"));
861 auth_info_visible = g_new0 (gboolean, length + 1);
862 auth_info_visible[1] = TRUE;
864 key = g_strconcat (username, "@", hostname, NULL);
865 password = g_hash_table_lookup (dispatch->backend->auth, key);
867 if (password && dispatch->request->password_state != GTK_CUPS_PASSWORD_NOT_VALID)
870 g_print ("CUPS backend: using stored password for %s\n", key));
872 overwrite_and_free (dispatch->request->password);
873 dispatch->request->password = g_strdup (password);
874 g_free (dispatch->request->username);
875 dispatch->request->username = g_strdup (username);
876 dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
880 const char *job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
881 const char *printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
882 char *printer_name = NULL;
884 if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
885 printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
887 if (dispatch->request->password_state == GTK_CUPS_PASSWORD_NOT_VALID)
888 g_hash_table_remove (dispatch->backend->auth, key);
890 dispatch->request->password_state = GTK_CUPS_PASSWORD_REQUESTED;
892 dispatch->backend->authentication_lock = TRUE;
894 switch (ippGetOperation (dispatch->request->ipp_request))
897 if (job_title != NULL && printer_name != NULL)
898 prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
900 prompt = g_strdup_printf ( _("Authentication is required to print a document on %s"), hostname);
902 case IPP_GET_JOB_ATTRIBUTES:
903 if (job_title != NULL)
904 prompt = g_strdup_printf ( _("Authentication is required to get attributes of job '%s'"), job_title);
906 prompt = g_strdup ( _("Authentication is required to get attributes of a job"));
908 case IPP_GET_PRINTER_ATTRIBUTES:
909 if (printer_name != NULL)
910 prompt = g_strdup_printf ( _("Authentication is required to get attributes of printer %s"), printer_name);
912 prompt = g_strdup ( _("Authentication is required to get attributes of a printer"));
914 case CUPS_GET_DEFAULT:
915 prompt = g_strdup_printf ( _("Authentication is required to get default printer of %s"), hostname);
917 case CUPS_GET_PRINTERS:
918 prompt = g_strdup_printf ( _("Authentication is required to get printers from %s"), hostname);
921 /* work around gcc warning about 0 not being a value for this enum */
922 if (ippGetOperation (dispatch->request->ipp_request) == 0)
923 prompt = g_strdup_printf ( _("Authentication is required to get a file from %s"), hostname);
925 prompt = g_strdup_printf ( _("Authentication is required on %s"), hostname);
929 g_free (printer_name);
931 g_signal_emit_by_name (dispatch->backend, "request-password",
932 auth_info_required, auth_info_default, auth_info_display, auth_info_visible, prompt);
937 for (i = 0; i < length; i++)
939 g_free (auth_info_required[i]);
940 g_free (auth_info_default[i]);
941 g_free (auth_info_display[i]);
944 g_free (auth_info_required);
945 g_free (auth_info_default);
946 g_free (auth_info_display);
947 g_free (auth_info_visible);
950 return G_SOURCE_REMOVE;
954 cups_dispatch_add_poll (GSource *source)
956 GtkPrintCupsDispatchWatch *dispatch;
957 GtkCupsPollState poll_state;
959 dispatch = (GtkPrintCupsDispatchWatch *) source;
961 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
963 /* Remove the old source if the poll state changed. */
964 if (poll_state != dispatch->poll_state && dispatch->data_poll != NULL)
966 g_source_remove_poll (source, dispatch->data_poll);
967 g_free (dispatch->data_poll);
968 dispatch->data_poll = NULL;
971 if (dispatch->request->http != NULL)
973 if (dispatch->data_poll == NULL)
975 dispatch->data_poll = g_new0 (GPollFD, 1);
976 dispatch->poll_state = poll_state;
978 if (poll_state == GTK_CUPS_HTTP_READ)
979 dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
980 else if (poll_state == GTK_CUPS_HTTP_WRITE)
981 dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
983 dispatch->data_poll->events = 0;
985 dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
986 g_source_add_poll (source, dispatch->data_poll);
992 check_auth_info (gpointer user_data)
994 GtkPrintCupsDispatchWatch *dispatch;
995 dispatch = (GtkPrintCupsDispatchWatch *) user_data;
997 if (!dispatch->request->need_auth_info)
999 if (dispatch->request->auth_info == NULL)
1001 dispatch->callback (GTK_PRINT_BACKEND (dispatch->backend),
1002 gtk_cups_request_get_result (dispatch->request),
1003 dispatch->callback_data);
1004 g_source_destroy ((GSource *) dispatch);
1011 length = g_strv_length (dispatch->request->auth_info_required);
1013 gtk_cups_request_ipp_add_strings (dispatch->request,
1019 (const char * const *) dispatch->request->auth_info);
1021 g_source_attach ((GSource *) dispatch, NULL);
1022 g_source_unref ((GSource *) dispatch);
1024 for (i = 0; i < length; i++)
1025 overwrite_and_free (dispatch->request->auth_info[i]);
1026 g_free (dispatch->request->auth_info);
1027 dispatch->request->auth_info = NULL;
1030 return G_SOURCE_REMOVE;
1033 return G_SOURCE_CONTINUE;
1037 request_auth_info (gpointer user_data)
1039 GtkPrintCupsDispatchWatch *dispatch;
1040 const char *job_title;
1041 const char *printer_uri;
1042 gchar *prompt = NULL;
1043 char *printer_name = NULL;
1046 gboolean *auth_info_visible = NULL;
1047 gchar **auth_info_default = NULL;
1048 gchar **auth_info_display = NULL;
1050 dispatch = (GtkPrintCupsDispatchWatch *) user_data;
1052 if (dispatch->backend->authentication_lock)
1055 job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
1056 printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
1057 length = g_strv_length (dispatch->request->auth_info_required);
1059 auth_info_visible = g_new0 (gboolean, length);
1060 auth_info_default = g_new0 (gchar *, length + 1);
1061 auth_info_display = g_new0 (gchar *, length + 1);
1063 for (i = 0; i < length; i++)
1065 if (g_strcmp0 (dispatch->request->auth_info_required[i], "domain") == 0)
1067 auth_info_display[i] = g_strdup (_("Domain:"));
1068 auth_info_default[i] = g_strdup ("WORKGROUP");
1069 auth_info_visible[i] = TRUE;
1071 else if (g_strcmp0 (dispatch->request->auth_info_required[i], "username") == 0)
1073 auth_info_display[i] = g_strdup (_("Username:"));
1074 if (dispatch->backend->username != NULL)
1075 auth_info_default[i] = g_strdup (dispatch->backend->username);
1077 auth_info_default[i] = g_strdup (cupsUser ());
1078 auth_info_visible[i] = TRUE;
1080 else if (g_strcmp0 (dispatch->request->auth_info_required[i], "password") == 0)
1082 auth_info_display[i] = g_strdup (_("Password:"));
1083 auth_info_visible[i] = FALSE;
1087 if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
1088 printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
1090 dispatch->backend->authentication_lock = TRUE;
1092 if (job_title != NULL)
1094 if (printer_name != NULL)
1095 prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
1097 prompt = g_strdup_printf ( _("Authentication is required to print document '%s'"), job_title);
1101 if (printer_name != NULL)
1102 prompt = g_strdup_printf ( _("Authentication is required to print this document on printer %s"), printer_name);
1104 prompt = g_strdup ( _("Authentication is required to print this document"));
1107 g_signal_emit_by_name (dispatch->backend, "request-password",
1108 dispatch->request->auth_info_required,
1114 for (i = 0; i < length; i++)
1116 g_free (auth_info_default[i]);
1117 g_free (auth_info_display[i]);
1120 g_free (auth_info_default);
1121 g_free (auth_info_display);
1122 g_free (printer_name);
1125 g_idle_add (check_auth_info, user_data);
1131 cups_dispatch_watch_check (GSource *source)
1133 GtkPrintCupsDispatchWatch *dispatch;
1134 GtkCupsPollState poll_state;
1138 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1140 dispatch = (GtkPrintCupsDispatchWatch *) source;
1142 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
1144 if (poll_state != GTK_CUPS_HTTP_IDLE && !dispatch->request->need_password)
1145 if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
1148 result = gtk_cups_request_read_write (dispatch->request, FALSE);
1149 if (result && dispatch->data_poll != NULL)
1151 g_source_remove_poll (source, dispatch->data_poll);
1152 g_free (dispatch->data_poll);
1153 dispatch->data_poll = NULL;
1156 if (dispatch->request->need_password && dispatch->request->password_state != GTK_CUPS_PASSWORD_REQUESTED)
1158 dispatch->request->need_password = FALSE;
1159 g_idle_add (request_password, dispatch);
1167 cups_dispatch_watch_prepare (GSource *source,
1170 GtkPrintCupsDispatchWatch *dispatch;
1173 dispatch = (GtkPrintCupsDispatchWatch *) source;
1176 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1180 result = gtk_cups_request_read_write (dispatch->request, TRUE);
1182 cups_dispatch_add_poll (source);
1188 cups_dispatch_watch_dispatch (GSource *source,
1189 GSourceFunc callback,
1192 GtkPrintCupsDispatchWatch *dispatch;
1193 GtkPrintCupsResponseCallbackFunc ep_callback;
1194 GtkCupsResult *result;
1196 g_assert (callback != NULL);
1198 ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
1200 dispatch = (GtkPrintCupsDispatchWatch *) source;
1202 result = gtk_cups_request_get_result (dispatch->request);
1205 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1207 if (gtk_cups_result_is_error (result))
1210 g_print("Error result: %s (type %i, status %i, code %i)\n",
1211 gtk_cups_result_get_error_string (result),
1212 gtk_cups_result_get_error_type (result),
1213 gtk_cups_result_get_error_status (result),
1214 gtk_cups_result_get_error_code (result)));
1217 ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
1223 cups_dispatch_watch_finalize (GSource *source)
1225 GtkPrintCupsDispatchWatch *dispatch;
1226 GtkCupsResult *result;
1229 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1231 dispatch = (GtkPrintCupsDispatchWatch *) source;
1233 result = gtk_cups_request_get_result (dispatch->request);
1234 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH)
1236 const gchar *username;
1237 gchar hostname[HTTP_MAX_URI];
1240 httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
1241 if (is_address_local (hostname))
1242 strcpy (hostname, "localhost");
1244 if (dispatch->backend->username != NULL)
1245 username = dispatch->backend->username;
1247 username = cupsUser ();
1249 key = g_strconcat (username, "@", hostname, NULL);
1251 g_print ("CUPS backend: removing stored password for %s\n", key));
1252 g_hash_table_remove (dispatch->backend->auth, key);
1255 if (dispatch->backend)
1256 dispatch->backend->authentication_lock = FALSE;
1259 gtk_cups_request_free (dispatch->request);
1261 if (dispatch->backend)
1263 /* We need to unref this at idle time, because it might be the
1264 * last reference to this module causing the code to be
1265 * unloaded (including this particular function!)
1266 * Update: Doing this at idle caused a deadlock taking the
1267 * mainloop context lock while being in a GSource callout for
1268 * multithreaded apps. So, for now we just disable unloading
1269 * of print backends. See _gtk_print_backend_create for the
1273 dispatch->backend->requests = g_list_remove (dispatch->backend->requests, dispatch);
1276 g_object_unref (dispatch->backend);
1277 dispatch->backend = NULL;
1280 if (dispatch->data_poll)
1282 g_source_remove_poll (source, dispatch->data_poll);
1283 g_free (dispatch->data_poll);
1284 dispatch->data_poll = NULL;
1288 static GSourceFuncs _cups_dispatch_watch_funcs = {
1289 cups_dispatch_watch_prepare,
1290 cups_dispatch_watch_check,
1291 cups_dispatch_watch_dispatch,
1292 cups_dispatch_watch_finalize
1297 cups_request_execute (GtkPrintBackendCups *print_backend,
1298 GtkCupsRequest *request,
1299 GtkPrintCupsResponseCallbackFunc callback,
1301 GDestroyNotify notify)
1303 GtkPrintCupsDispatchWatch *dispatch;
1305 dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
1306 sizeof (GtkPrintCupsDispatchWatch));
1307 g_source_set_name (&dispatch->source, "GTK+ CUPS backend");
1310 g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
1312 dispatch->request = request;
1313 dispatch->backend = g_object_ref (print_backend);
1314 dispatch->poll_state = GTK_CUPS_HTTP_IDLE;
1315 dispatch->data_poll = NULL;
1316 dispatch->callback = NULL;
1317 dispatch->callback_data = NULL;
1319 print_backend->requests = g_list_prepend (print_backend->requests, dispatch);
1321 g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
1323 if (request->need_auth_info)
1325 dispatch->callback = callback;
1326 dispatch->callback_data = user_data;
1327 request_auth_info (dispatch);
1331 g_source_attach ((GSource *) dispatch, NULL);
1332 g_source_unref ((GSource *) dispatch);
1338 cups_request_printer_info_cb (GtkPrintBackendCups *backend,
1339 GtkCupsResult *result,
1342 ipp_attribute_t *attr;
1344 gchar *printer_name;
1345 GtkPrinterCups *cups_printer;
1346 GtkPrinter *printer;
1351 gboolean status_changed;
1353 g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
1355 printer_name = (gchar *)user_data;
1356 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
1360 g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
1365 g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
1369 cups_printer = GTK_PRINTER_CUPS (printer);
1371 if (gtk_cups_result_is_error (result))
1373 if (gtk_printer_is_new (printer))
1375 gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
1380 return; /* TODO: mark as inactive printer */
1383 response = gtk_cups_result_get_response (result);
1385 /* TODO: determine printer type and use correct icon */
1386 gtk_printer_set_icon_name (printer, "printer");
1392 for (attr = response->attrs; attr != NULL; attr = attr->next)
1397 _CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
1398 _CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
1399 _CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
1400 _CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
1401 _CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
1404 status_changed = gtk_printer_set_job_count (printer, job_count);
1406 status_changed |= gtk_printer_set_location (printer, loc);
1407 status_changed |= gtk_printer_set_description (printer, desc);
1408 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1411 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1412 "printer-status-changed", printer);
1416 cups_request_printer_info (GtkPrintBackendCups *print_backend,
1417 const gchar *printer_name)
1419 GtkCupsRequest *request;
1421 static const char * const pattrs[] = /* Attributes we're interested in */
1425 "printer-state-message",
1428 "job-sheets-supported",
1429 "job-sheets-default"
1432 request = gtk_cups_request_new_with_username (NULL,
1434 IPP_GET_PRINTER_ATTRIBUTES,
1438 print_backend->username);
1440 printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
1442 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
1443 "printer-uri", NULL, printer_uri);
1446 g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
1448 g_free (printer_uri);
1450 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1451 "requested-attributes", G_N_ELEMENTS (pattrs),
1454 cups_request_execute (print_backend,
1456 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
1457 g_strdup (printer_name),
1458 (GDestroyNotify) g_free);
1463 GtkPrintBackendCups *print_backend;
1470 job_object_died (gpointer user_data,
1471 GObject *where_the_object_was)
1473 CupsJobPollData *data = user_data;
1478 cups_job_poll_data_free (CupsJobPollData *data)
1481 g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
1487 cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
1488 GtkCupsResult *result,
1491 CupsJobPollData *data = user_data;
1492 ipp_attribute_t *attr;
1497 gdk_threads_enter ();
1499 if (data->job == NULL)
1501 cups_job_poll_data_free (data);
1507 response = gtk_cups_result_get_response (result);
1511 #ifdef HAVE_CUPS_API_1_6
1512 attr = ippFindAttribute (response, "job-state", IPP_TAG_INTEGER);
1513 state = ippGetInteger (attr, 0);
1515 for (attr = response->attrs; attr != NULL; attr = attr->next)
1520 _CUPS_MAP_ATTR_INT (attr, state, "job-state");
1527 case IPP_JOB_PENDING:
1529 case IPP_JOB_STOPPED:
1530 gtk_print_job_set_status (data->job,
1531 GTK_PRINT_STATUS_PENDING);
1533 case IPP_JOB_PROCESSING:
1534 gtk_print_job_set_status (data->job,
1535 GTK_PRINT_STATUS_PRINTING);
1538 case IPP_JOB_CANCELLED:
1539 case IPP_JOB_ABORTED:
1540 gtk_print_job_set_status (data->job,
1541 GTK_PRINT_STATUS_FINISHED_ABORTED);
1545 case IPP_JOB_COMPLETED:
1546 gtk_print_job_set_status (data->job,
1547 GTK_PRINT_STATUS_FINISHED);
1552 if (!done && data->job != NULL)
1556 if (data->counter < 5)
1558 else if (data->counter < 10)
1563 g_timeout_add (timeout, cups_job_info_poll_timeout, data);
1566 cups_job_poll_data_free (data);
1569 gdk_threads_leave ();
1573 cups_request_job_info (CupsJobPollData *data)
1575 GtkCupsRequest *request;
1578 request = gtk_cups_request_new_with_username (NULL,
1580 IPP_GET_JOB_ATTRIBUTES,
1584 data->print_backend->username);
1586 job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
1587 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
1588 "job-uri", NULL, job_uri);
1591 cups_request_execute (data->print_backend,
1593 (GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
1599 cups_job_info_poll_timeout (gpointer user_data)
1601 CupsJobPollData *data = user_data;
1603 if (data->job == NULL)
1604 cups_job_poll_data_free (data);
1606 cups_request_job_info (data);
1608 return G_SOURCE_REMOVE;
1612 cups_begin_polling_info (GtkPrintBackendCups *print_backend,
1616 CupsJobPollData *data;
1618 data = g_new0 (CupsJobPollData, 1);
1620 data->print_backend = print_backend;
1622 data->job_id = job_id;
1625 g_object_weak_ref (G_OBJECT (job), job_object_died, data);
1627 cups_request_job_info (data);
1631 mark_printer_inactive (GtkPrinter *printer,
1632 GtkPrintBackend *backend)
1634 gtk_printer_set_is_active (printer, FALSE);
1635 g_signal_emit_by_name (backend, "printer-removed", printer);
1639 find_printer (GtkPrinter *printer,
1640 const gchar *find_name)
1642 const gchar *printer_name;
1644 printer_name = gtk_printer_get_name (printer);
1645 return g_ascii_strcasecmp (printer_name, find_name);
1647 /* Printer messages we're interested in */
1648 static const char * const printer_messages[] =
1654 "marker-supply-low",
1655 "marker-supply-empty",
1663 /* Our translatable versions of the printer messages */
1664 static const char * printer_strings[] =
1666 N_("Printer '%s' is low on toner."),
1667 N_("Printer '%s' has no toner left."),
1668 /* Translators: "Developer" like on photo development context */
1669 N_("Printer '%s' is low on developer."),
1670 /* Translators: "Developer" like on photo development context */
1671 N_("Printer '%s' is out of developer."),
1672 /* Translators: "marker" is one color bin of the printer */
1673 N_("Printer '%s' is low on at least one marker supply."),
1674 /* Translators: "marker" is one color bin of the printer */
1675 N_("Printer '%s' is out of at least one marker supply."),
1676 N_("The cover is open on printer '%s'."),
1677 N_("The door is open on printer '%s'."),
1678 N_("Printer '%s' is low on paper."),
1679 N_("Printer '%s' is out of paper."),
1680 N_("Printer '%s' is currently offline."),
1681 N_("There is a problem on printer '%s'.")
1686 GTK_PRINTER_STATE_LEVEL_NONE = 0,
1687 GTK_PRINTER_STATE_LEVEL_INFO = 1,
1688 GTK_PRINTER_STATE_LEVEL_WARNING = 2,
1689 GTK_PRINTER_STATE_LEVEL_ERROR = 3
1690 } PrinterStateLevel;
1694 const gchar *printer_name;
1695 const gchar *printer_uri;
1696 const gchar *member_uris;
1697 const gchar *location;
1698 const gchar *description;
1700 const gchar *reason_msg;
1701 PrinterStateLevel reason_level;
1705 gboolean is_accepting_jobs;
1706 const gchar *default_cover_before;
1707 const gchar *default_cover_after;
1708 gboolean default_printer;
1709 gboolean got_printer_type;
1710 gboolean remote_printer;
1711 gchar **auth_info_required;
1712 gint default_number_up;
1716 cups_printer_handle_attribute (GtkPrintBackendCups *cups_backend,
1717 ipp_attribute_t *attr,
1718 PrinterSetupInfo *info)
1721 if (strcmp (ippGetName (attr), "printer-name") == 0 &&
1722 ippGetValueTag (attr) == IPP_TAG_NAME)
1723 info->printer_name = ippGetString (attr, 0, NULL);
1724 else if (strcmp (ippGetName (attr), "printer-uri-supported") == 0 &&
1725 ippGetValueTag (attr) == IPP_TAG_URI)
1726 info->printer_uri = ippGetString (attr, 0, NULL);
1727 else if (strcmp (ippGetName (attr), "member-uris") == 0 &&
1728 ippGetValueTag (attr) == IPP_TAG_URI)
1729 info->member_uris = ippGetString (attr, 0, NULL);
1730 else if (strcmp (ippGetName (attr), "printer-location") == 0)
1731 info->location = ippGetString (attr, 0, NULL);
1732 else if (strcmp (ippGetName (attr), "printer-info") == 0)
1733 info->description = ippGetString (attr, 0, NULL);
1734 else if (strcmp (ippGetName (attr), "printer-state-message") == 0)
1735 info->state_msg = g_strdup (ippGetString (attr, 0, NULL));
1736 else if (strcmp (ippGetName (attr), "printer-state-reasons") == 0)
1737 /* Store most important reason to reason_msg and set
1738 its importance at printer_state_reason_level */
1740 for (i = 0; i < ippGetCount (attr); i++)
1742 if (strcmp (ippGetString (attr, i, NULL), "none") != 0)
1744 gboolean interested_in = FALSE;
1745 /* Sets is_paused flag for paused printer. */
1746 if (strcmp (ippGetString (attr, i, NULL), "paused") == 0)
1748 info->is_paused = TRUE;
1751 for (j = 0; j < G_N_ELEMENTS (printer_messages); j++)
1752 if (strncmp (ippGetString (attr, i, NULL), printer_messages[j],
1753 strlen (printer_messages[j])) == 0)
1755 interested_in = TRUE;
1761 if (g_str_has_suffix (ippGetString (attr, i, NULL), "-report"))
1763 if (info->reason_level <= GTK_PRINTER_STATE_LEVEL_INFO)
1765 info->reason_msg = ippGetString (attr, i, NULL);
1766 info->reason_level = GTK_PRINTER_STATE_LEVEL_INFO;
1769 else if (g_str_has_suffix (ippGetString (attr, i, NULL), "-warning"))
1771 if (info->reason_level <= GTK_PRINTER_STATE_LEVEL_WARNING)
1773 info->reason_msg = ippGetString (attr, i, NULL);
1774 info->reason_level = GTK_PRINTER_STATE_LEVEL_WARNING;
1777 else /* It is error in the case of no suffix. */
1779 info->reason_msg = ippGetString (attr, i, NULL);
1780 info->reason_level = GTK_PRINTER_STATE_LEVEL_ERROR;
1786 else if (strcmp (ippGetName (attr), "printer-state") == 0)
1787 info->state = ippGetInteger (attr, 0);
1788 else if (strcmp (ippGetName (attr), "queued-job-count") == 0)
1789 info->job_count = ippGetInteger (attr, 0);
1790 else if (strcmp (ippGetName (attr), "printer-is-accepting-jobs") == 0)
1792 if (ippGetBoolean (attr, 0) == 1)
1793 info->is_accepting_jobs = TRUE;
1795 info->is_accepting_jobs = FALSE;
1797 else if (strcmp (ippGetName (attr), "job-sheets-supported") == 0)
1799 if (cups_backend->covers == NULL)
1801 cups_backend->number_of_covers = ippGetCount (attr);
1802 cups_backend->covers = g_new (char *, cups_backend->number_of_covers + 1);
1803 for (i = 0; i < cups_backend->number_of_covers; i++)
1804 cups_backend->covers[i] = g_strdup (ippGetString (attr, i, NULL));
1805 cups_backend->covers[cups_backend->number_of_covers] = NULL;
1808 else if (strcmp (ippGetName (attr), "job-sheets-default") == 0)
1810 if (ippGetCount (attr) == 2)
1812 info->default_cover_before = ippGetString (attr, 0, NULL);
1813 info->default_cover_after = ippGetString (attr, 1, NULL);
1816 else if (strcmp (ippGetName (attr), "printer-type") == 0)
1818 info->got_printer_type = TRUE;
1819 if (ippGetInteger (attr, 0) & 0x00020000)
1820 info->default_printer = TRUE;
1822 info->default_printer = FALSE;
1824 if (ippGetInteger (attr, 0) & 0x00000002)
1825 info->remote_printer = TRUE;
1827 info->remote_printer = FALSE;
1829 else if (strcmp (ippGetName (attr), "auth-info-required") == 0)
1831 if (strcmp (ippGetString (attr, 0, NULL), "none") != 0)
1833 info->auth_info_required = g_new0 (gchar *, ippGetCount (attr) + 1);
1834 for (i = 0; i < ippGetCount (attr); i++)
1835 info->auth_info_required[i] = g_strdup (ippGetString (attr, i, NULL));
1838 else if (strcmp (ippGetName (attr), "number-up-default") == 0)
1840 info->default_number_up = ippGetInteger (attr, 0);
1845 g_print ("CUPS Backend: Attribute %s ignored", ippGetName (attr)));
1850 cups_create_printer (GtkPrintBackendCups *cups_backend,
1851 PrinterSetupInfo *info)
1853 GtkPrinterCups *cups_printer;
1854 GtkPrinter *printer;
1855 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
1856 char uri[HTTP_MAX_URI]; /* Printer URI */
1857 char method[HTTP_MAX_URI]; /* Method/scheme name */
1858 char username[HTTP_MAX_URI]; /* Username:password */
1859 char hostname[HTTP_MAX_URI]; /* Hostname */
1860 char resource[HTTP_MAX_URI]; /* Resource name */
1861 int port; /* Port number */
1862 char *cups_server; /* CUPS server */
1865 cups_printer = gtk_printer_cups_new (info->printer_name,
1867 cups_backend->colord_client);
1869 cups_printer = gtk_printer_cups_new (info->printer_name, backend, NULL);
1872 cups_printer->device_uri = g_strdup_printf ("/printers/%s",
1873 info->printer_name);
1875 /* Check to see if we are looking at a class */
1876 if (info->member_uris)
1878 cups_printer->printer_uri = g_strdup (info->member_uris);
1879 /* TODO if member_uris is a class we need to recursivly find a printer */
1881 g_print ("CUPS Backend: Found class with printer %s\n",
1882 info->member_uris));
1886 cups_printer->printer_uri = g_strdup (info->printer_uri);
1888 g_print ("CUPS Backend: Found printer %s\n",
1889 info->printer_uri));
1892 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
1893 method, sizeof (method),
1894 username, sizeof (username),
1895 hostname, sizeof (hostname),
1897 resource, sizeof (resource));
1899 if (strncmp (resource, "/printers/", 10) == 0)
1901 cups_printer->ppd_name = g_strdup (resource + 10);
1903 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, info->printer_name));
1906 gethostname (uri, sizeof (uri));
1907 cups_server = g_strdup (cupsServer());
1909 if (strcasecmp (uri, hostname) == 0)
1910 strcpy (hostname, "localhost");
1912 /* if the cups server is local and listening at a unix domain socket
1913 * then use the socket connection
1915 if ((strstr (hostname, "localhost") != NULL) &&
1916 (cups_server[0] == '/'))
1917 strcpy (hostname, cups_server);
1919 g_free (cups_server);
1921 cups_printer->default_cover_before = g_strdup (info->default_cover_before);
1922 cups_printer->default_cover_after = g_strdup (info->default_cover_after);
1924 cups_printer->default_number_up = info->default_number_up;
1926 cups_printer->hostname = g_strdup (hostname);
1927 cups_printer->port = port;
1929 cups_printer->auth_info_required = g_strdupv (info->auth_info_required);
1930 g_strfreev (info->auth_info_required);
1932 printer = GTK_PRINTER (cups_printer);
1934 if (cups_backend->default_printer != NULL &&
1935 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
1936 gtk_printer_set_is_default (printer, TRUE);
1939 gtk_print_backend_add_printer (backend, printer);
1945 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
1946 GtkCupsResult *result,
1949 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
1950 ipp_attribute_t *attr;
1952 gboolean list_has_changed;
1953 GList *removed_printer_checklist;
1954 gchar *remote_default_printer = NULL;
1956 gdk_threads_enter ();
1958 list_has_changed = FALSE;
1961 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1963 cups_backend->list_printers_pending = FALSE;
1965 if (gtk_cups_result_is_error (result))
1968 g_warning ("CUPS Backend: Error getting printer list: %s %d %d",
1969 gtk_cups_result_get_error_string (result),
1970 gtk_cups_result_get_error_type (result),
1971 gtk_cups_result_get_error_code (result)));
1973 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
1974 gtk_cups_result_get_error_code (result) == 1)
1976 /* Canceled by user, stop popping up more password dialogs */
1977 if (cups_backend->list_printers_poll > 0)
1978 g_source_remove (cups_backend->list_printers_poll);
1979 cups_backend->list_printers_poll = 0;
1980 cups_backend->list_printers_attempts = 0;
1986 /* Gather the names of the printers in the current queue
1987 * so we may check to see if they were removed
1989 removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
1991 response = gtk_cups_result_get_response (result);
1992 #ifdef HAVE_CUPS_API_1_6
1993 for (attr = ippFirstAttribute (response); attr != NULL;
1994 attr = ippNextAttribute (response))
1996 GtkPrinter *printer;
1997 gboolean status_changed = FALSE;
2000 PrinterSetupInfo *info = g_slice_new0 (PrinterSetupInfo);
2002 /* Skip leading attributes until we hit a printer...
2004 while (attr != NULL && ippGetGroupTag (attr) != IPP_TAG_PRINTER)
2005 attr = ippNextAttribute (response);
2009 while (attr != NULL && ippGetGroupTag (attr) == IPP_TAG_PRINTER)
2011 cups_printer_handle_attribute (cups_backend, attr, info);
2012 attr = ippNextAttribute (response);
2015 for (attr = response->attrs; attr != NULL; attr = attr->next)
2017 GtkPrinter *printer;
2018 gboolean status_changed = FALSE;
2021 PrinterSetupInfo *info = g_slice_new0 (PrinterSetupInfo);
2022 info->default_number_up = 1;
2024 /* Skip leading attributes until we hit a printer...
2026 while (attr != NULL && ippGetGroupTag (attr) != IPP_TAG_PRINTER)
2031 while (attr != NULL && ippGetGroupTag (attr) == IPP_TAG_PRINTER)
2033 cups_printer_handle_attribute (cups_backend, attr, info);
2038 if (info->printer_name == NULL ||
2039 (info->printer_uri == NULL && info->member_uris == NULL))
2047 if (info->got_printer_type)
2049 if (info->default_printer && !cups_backend->got_default_printer)
2051 if (!info->remote_printer)
2053 cups_backend->got_default_printer = TRUE;
2054 cups_backend->default_printer = g_strdup (info->printer_name);
2058 if (remote_default_printer == NULL)
2059 remote_default_printer = g_strdup (info->printer_name);
2065 if (!cups_backend->got_default_printer)
2066 cups_get_default_printer (cups_backend);
2069 /* remove name from checklist if it was found */
2070 node = g_list_find_custom (removed_printer_checklist,
2072 (GCompareFunc) find_printer);
2073 removed_printer_checklist = g_list_delete_link (removed_printer_checklist,
2076 printer = gtk_print_backend_find_printer (backend, info->printer_name);
2079 printer = cups_create_printer (cups_backend, info);
2080 list_has_changed = TRUE;
2084 g_object_ref (printer);
2086 GTK_PRINTER_CUPS (printer)->remote = info->remote_printer;
2088 gtk_printer_set_is_paused (printer, info->is_paused);
2089 gtk_printer_set_is_accepting_jobs (printer, info->is_accepting_jobs);
2091 if (!gtk_printer_is_active (printer))
2093 gtk_printer_set_is_active (printer, TRUE);
2094 gtk_printer_set_is_new (printer, TRUE);
2095 list_has_changed = TRUE;
2098 if (gtk_printer_is_new (printer))
2100 g_signal_emit_by_name (backend, "printer-added", printer);
2102 gtk_printer_set_is_new (printer, FALSE);
2106 /* Getting printer info with separate requests overwhelms cups
2107 * when the printer list has more than a handful of printers.
2109 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
2112 GTK_PRINTER_CUPS (printer)->state = info->state;
2113 status_changed = gtk_printer_set_job_count (printer, info->job_count);
2114 status_changed |= gtk_printer_set_location (printer, info->location);
2115 status_changed |= gtk_printer_set_description (printer,
2118 if (info->state_msg != NULL && strlen (info->state_msg) == 0)
2120 gchar *tmp_msg2 = NULL;
2121 if (info->is_paused && !info->is_accepting_jobs)
2122 /* Translators: this is a printer status. */
2123 tmp_msg2 = g_strdup ( N_("Paused ; Rejecting Jobs"));
2124 if (info->is_paused && info->is_accepting_jobs)
2125 /* Translators: this is a printer status. */
2126 tmp_msg2 = g_strdup ( N_("Paused"));
2127 if (!info->is_paused && !info->is_accepting_jobs)
2128 /* Translators: this is a printer status. */
2129 tmp_msg2 = g_strdup ( N_("Rejecting Jobs"));
2131 if (tmp_msg2 != NULL)
2133 g_free (info->state_msg);
2134 info->state_msg = tmp_msg2;
2138 /* Set description of the reason and combine it with printer-state-message. */
2139 if ( (info->reason_msg != NULL))
2141 gchar *reason_msg_desc = NULL;
2142 gboolean found = FALSE;
2144 for (i = 0; i < G_N_ELEMENTS (printer_messages); i++)
2146 if (strncmp (info->reason_msg, printer_messages[i],
2147 strlen (printer_messages[i])) == 0)
2149 reason_msg_desc = g_strdup_printf (printer_strings[i],
2150 info->printer_name);
2157 info->reason_level = GTK_PRINTER_STATE_LEVEL_NONE;
2159 if (info->reason_level >= GTK_PRINTER_STATE_LEVEL_WARNING)
2161 if (strlen (info->state_msg) == 0)
2163 g_free (info->state_msg);
2164 info->state_msg = reason_msg_desc;
2165 reason_msg_desc = NULL;
2169 gchar *tmp_msg = NULL;
2170 tmp_msg = g_strjoin (" ; ", info->state_msg,
2171 reason_msg_desc, NULL);
2172 g_free (info->state_msg);
2173 info->state_msg = tmp_msg;
2176 if (reason_msg_desc != NULL)
2177 g_free (reason_msg_desc);
2180 status_changed |= gtk_printer_set_state_message (printer, info->state_msg);
2181 status_changed |= gtk_printer_set_is_accepting_jobs (printer, info->is_accepting_jobs);
2185 /* Set printer icon according to importance
2186 (none, report, warning, error - report is omitted). */
2187 if (info->reason_level == GTK_PRINTER_STATE_LEVEL_ERROR)
2188 gtk_printer_set_icon_name (printer, "printer-error");
2189 else if (info->reason_level == GTK_PRINTER_STATE_LEVEL_WARNING)
2190 gtk_printer_set_icon_name (printer, "printer-warning");
2191 else if (gtk_printer_is_paused (printer))
2192 gtk_printer_set_icon_name (printer, "printer-paused");
2194 gtk_printer_set_icon_name (printer, "printer");
2197 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
2198 "printer-status-changed", printer);
2200 /* The ref is held by GtkPrintBackend, in add_printer() */
2201 g_object_unref (printer);
2202 g_free (info->state_msg);
2203 g_slice_free (PrinterSetupInfo, info);
2209 /* look at the removed printers checklist and mark any printer
2210 as inactive if it is in the list, emitting a printer_removed signal */
2211 if (removed_printer_checklist != NULL)
2213 g_list_foreach (removed_printer_checklist, (GFunc) mark_printer_inactive, backend);
2214 g_list_free (removed_printer_checklist);
2215 list_has_changed = TRUE;
2219 if (list_has_changed)
2220 g_signal_emit_by_name (backend, "printer-list-changed");
2222 gtk_print_backend_set_list_done (backend);
2224 if (!cups_backend->got_default_printer && remote_default_printer != NULL)
2226 cups_backend->default_printer = g_strdup (remote_default_printer);
2227 cups_backend->got_default_printer = TRUE;
2228 g_free (remote_default_printer);
2230 if (cups_backend->default_printer != NULL)
2232 GtkPrinter *default_printer = NULL;
2233 default_printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (cups_backend),
2234 cups_backend->default_printer);
2235 if (default_printer != NULL)
2237 gtk_printer_set_is_default (default_printer, TRUE);
2238 g_signal_emit_by_name (GTK_PRINT_BACKEND (cups_backend),
2239 "printer-status-changed", default_printer);
2244 gdk_threads_leave ();
2248 update_backend_status (GtkPrintBackendCups *cups_backend,
2249 GtkCupsConnectionState state)
2253 case GTK_CUPS_CONNECTION_NOT_AVAILABLE:
2254 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_UNAVAILABLE, NULL);
2256 case GTK_CUPS_CONNECTION_AVAILABLE:
2257 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_OK, NULL);
2264 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
2266 GtkCupsConnectionState state;
2267 GtkCupsRequest *request;
2268 static const char * const pattrs[] = /* Attributes we're interested in */
2271 "printer-uri-supported",
2275 "printer-state-message",
2276 "printer-state-reasons",
2279 "printer-is-accepting-jobs",
2280 "job-sheets-supported",
2281 "job-sheets-default",
2283 "auth-info-required",
2287 if (cups_backend->reading_ppds > 0 || cups_backend->list_printers_pending)
2290 state = gtk_cups_connection_test_get_state (cups_backend->cups_connection_test);
2291 update_backend_status (cups_backend, state);
2293 if (cups_backend->list_printers_attempts == 60)
2295 cups_backend->list_printers_attempts = -1;
2296 if (cups_backend->list_printers_poll > 0)
2297 g_source_remove (cups_backend->list_printers_poll);
2298 cups_backend->list_printers_poll = gdk_threads_add_timeout (200,
2299 (GSourceFunc) cups_request_printer_list,
2302 else if (cups_backend->list_printers_attempts != -1)
2303 cups_backend->list_printers_attempts++;
2305 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2308 if (cups_backend->list_printers_attempts > 0)
2309 cups_backend->list_printers_attempts = 60;
2311 cups_backend->list_printers_pending = TRUE;
2313 request = gtk_cups_request_new_with_username (NULL,
2319 cups_backend->username);
2321 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2322 "requested-attributes", G_N_ELEMENTS (pattrs),
2325 cups_request_execute (cups_backend,
2327 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
2335 cups_get_printer_list (GtkPrintBackend *backend)
2337 GtkPrintBackendCups *cups_backend;
2339 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
2341 if (cups_backend->cups_connection_test == NULL)
2342 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
2344 if (cups_backend->list_printers_poll == 0)
2346 if (cups_request_printer_list (cups_backend))
2347 cups_backend->list_printers_poll = gdk_threads_add_timeout (50,
2348 (GSourceFunc) cups_request_printer_list,
2354 GtkPrinterCups *printer;
2360 get_ppd_data_free (GetPPDData *data)
2363 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2364 httpClose (data->http);
2365 g_io_channel_unref (data->ppd_io);
2366 g_object_unref (data->printer);
2371 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
2372 GtkCupsResult *result,
2375 GtkPrinter *printer;
2377 gdk_threads_enter ();
2380 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2382 printer = GTK_PRINTER (data->printer);
2383 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
2384 print_backend->reading_ppds--;
2386 if (gtk_cups_result_is_error (result))
2388 gboolean success = FALSE;
2390 /* if we get a 404 then it is just a raw printer without a ppd
2392 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
2393 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
2395 gtk_printer_set_has_details (printer, TRUE);
2399 g_signal_emit_by_name (printer, "details-acquired", success);
2403 /* let ppdOpenFd take over the ownership of the open file */
2404 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
2405 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
2406 ppdLocalize (data->printer->ppd_file);
2407 ppdMarkDefaults (data->printer->ppd_file);
2409 gtk_printer_set_has_details (printer, TRUE);
2410 g_signal_emit_by_name (printer, "details-acquired", TRUE);
2413 gdk_threads_leave ();
2417 cups_request_ppd (GtkPrinter *printer)
2420 GtkPrintBackend *print_backend;
2421 GtkPrinterCups *cups_printer;
2422 GtkCupsRequest *request;
2429 cups_printer = GTK_PRINTER_CUPS (printer);
2434 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2436 if (cups_printer->remote)
2438 GtkCupsConnectionState state;
2440 state = gtk_cups_connection_test_get_state (cups_printer->remote_cups_connection_test);
2442 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS)
2444 if (cups_printer->get_remote_ppd_attempts == 60)
2446 cups_printer->get_remote_ppd_attempts = -1;
2447 if (cups_printer->get_remote_ppd_poll > 0)
2448 g_source_remove (cups_printer->get_remote_ppd_poll);
2449 cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (200,
2450 (GSourceFunc) cups_request_ppd,
2453 else if (cups_printer->get_remote_ppd_attempts != -1)
2454 cups_printer->get_remote_ppd_attempts++;
2459 gtk_cups_connection_test_free (cups_printer->remote_cups_connection_test);
2460 cups_printer->remote_cups_connection_test = NULL;
2461 cups_printer->get_remote_ppd_poll = 0;
2462 cups_printer->get_remote_ppd_attempts = 0;
2464 if (state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2466 g_signal_emit_by_name (printer, "details-acquired", FALSE);
2471 http = httpConnectEncrypt (cups_printer->hostname,
2475 data = g_new0 (GetPPDData, 1);
2477 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
2481 #ifdef G_ENABLE_DEBUG
2482 /* If we are debugging printing don't delete the tmp files */
2483 if (!(gtk_get_debug_flags () & GTK_DEBUG_PRINTING))
2484 unlink (ppd_filename);
2486 unlink (ppd_filename);
2487 #endif /* G_ENABLE_DEBUG */
2492 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
2494 g_error_free (error);
2496 g_free (ppd_filename);
2499 g_signal_emit_by_name (printer, "details-acquired", FALSE);
2504 fchmod (fd, S_IRUSR | S_IWUSR);
2505 data->ppd_io = g_io_channel_unix_new (fd);
2506 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
2507 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
2509 data->printer = g_object_ref (printer);
2511 resource = g_strdup_printf ("/printers/%s.ppd",
2512 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
2514 print_backend = gtk_printer_get_backend (printer);
2516 request = gtk_cups_request_new_with_username (data->http,
2520 cups_printer->hostname,
2522 GTK_PRINT_BACKEND_CUPS (print_backend)->username);
2525 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
2528 cups_printer->reading_ppd = TRUE;
2529 GTK_PRINT_BACKEND_CUPS (print_backend)->reading_ppds++;
2531 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
2533 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
2535 (GDestroyNotify)get_ppd_data_free);
2538 g_free (ppd_filename);
2543 /* Ordering matters for default preference */
2544 static const char *lpoptions_locations[] = {
2545 "/etc/cups/lpoptions",
2551 cups_parse_user_default_printer (const char *filename,
2552 char **printer_name)
2555 char line[1024], *lineptr, *defname = NULL;
2557 if ((fp = g_fopen (filename, "r")) == NULL)
2560 while (fgets (line, sizeof (line), fp) != NULL)
2562 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
2566 while (isspace (*lineptr))
2573 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
2578 if (*printer_name != NULL)
2579 g_free (*printer_name);
2581 *printer_name = g_strdup (defname);
2588 cups_get_user_default_printer (char **printer_name)
2592 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
2594 if (g_path_is_absolute (lpoptions_locations[i]))
2596 cups_parse_user_default_printer (lpoptions_locations[i],
2603 filename = g_build_filename (g_get_home_dir (),
2604 lpoptions_locations[i], NULL);
2605 cups_parse_user_default_printer (filename, printer_name);
2612 cups_parse_user_options (const char *filename,
2613 const char *printer_name,
2615 cups_option_t **options)
2618 gchar line[1024], *lineptr, *name;
2620 if ((fp = g_fopen (filename, "r")) == NULL)
2623 while (fgets (line, sizeof (line), fp) != NULL)
2625 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
2627 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
2632 /* Skip leading whitespace */
2633 while (isspace (*lineptr))
2639 /* NUL-terminate the name, stripping the instance name */
2641 while (!isspace (*lineptr) && *lineptr)
2643 if (*lineptr == '/')
2653 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
2656 /* We found our printer, parse the options */
2657 num_options = cupsParseOptions (lineptr, num_options, options);
2666 cups_get_user_options (const char *printer_name,
2668 cups_option_t **options)
2672 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
2674 if (g_path_is_absolute (lpoptions_locations[i]))
2676 num_options = cups_parse_user_options (lpoptions_locations[i],
2685 filename = g_build_filename (g_get_home_dir (),
2686 lpoptions_locations[i], NULL);
2687 num_options = cups_parse_user_options (filename, printer_name,
2688 num_options, options);
2696 /* This function requests default printer from a CUPS server in regular intervals.
2697 * In the case of unreachable CUPS server the request is repeated later.
2698 * The default printer is not requested in the case of previous success.
2701 cups_get_default_printer (GtkPrintBackendCups *backend)
2703 GtkPrintBackendCups *cups_backend;
2705 cups_backend = backend;
2707 if (cups_backend->cups_connection_test == NULL)
2708 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
2710 if (cups_backend->default_printer_poll == 0)
2712 if (cups_request_default_printer (cups_backend))
2713 cups_backend->default_printer_poll = gdk_threads_add_timeout (200,
2714 (GSourceFunc) cups_request_default_printer,
2719 /* This function gets default printer from local settings.*/
2721 cups_get_local_default_printer (GtkPrintBackendCups *backend)
2726 if ((str = g_getenv ("LPDEST")) != NULL)
2728 backend->default_printer = g_strdup (str);
2729 backend->got_default_printer = TRUE;
2732 else if ((str = g_getenv ("PRINTER")) != NULL &&
2733 strcmp (str, "lp") != 0)
2735 backend->default_printer = g_strdup (str);
2736 backend->got_default_printer = TRUE;
2740 /* Figure out user setting for default printer */
2741 cups_get_user_default_printer (&name);
2744 backend->default_printer = name;
2745 backend->got_default_printer = TRUE;
2751 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
2752 GtkCupsResult *result,
2756 ipp_attribute_t *attr;
2757 GtkPrinter *printer;
2759 gdk_threads_enter ();
2761 if (gtk_cups_result_is_error (result))
2763 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
2764 gtk_cups_result_get_error_code (result) == 1)
2766 /* Canceled by user, stop popping up more password dialogs */
2767 if (print_backend->list_printers_poll > 0)
2768 g_source_remove (print_backend->list_printers_poll);
2769 print_backend->list_printers_poll = 0;
2775 response = gtk_cups_result_get_response (result);
2777 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
2778 print_backend->default_printer = g_strdup (ippGetString (attr, 0, NULL));
2780 print_backend->got_default_printer = TRUE;
2782 if (print_backend->default_printer != NULL)
2784 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (print_backend), print_backend->default_printer);
2785 if (printer != NULL)
2787 gtk_printer_set_is_default (printer, TRUE);
2788 g_signal_emit_by_name (GTK_PRINT_BACKEND (print_backend), "printer-status-changed", printer);
2792 /* Make sure to kick off get_printers if we are polling it,
2793 * as we could have blocked this reading the default printer
2795 if (print_backend->list_printers_poll != 0)
2796 cups_request_printer_list (print_backend);
2798 gdk_threads_leave ();
2802 cups_request_default_printer (GtkPrintBackendCups *print_backend)
2804 GtkCupsConnectionState state;
2805 GtkCupsRequest *request;
2807 state = gtk_cups_connection_test_get_state (print_backend->cups_connection_test);
2808 update_backend_status (print_backend, state);
2810 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2813 request = gtk_cups_request_new_with_username (NULL,
2819 print_backend->username);
2821 cups_request_execute (print_backend,
2823 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
2824 g_object_ref (print_backend),
2831 cups_printer_request_details (GtkPrinter *printer)
2833 GtkPrinterCups *cups_printer;
2835 cups_printer = GTK_PRINTER_CUPS (printer);
2836 if (!cups_printer->reading_ppd &&
2837 gtk_printer_cups_get_ppd (cups_printer) == NULL)
2839 if (cups_printer->remote)
2841 if (cups_printer->get_remote_ppd_poll == 0)
2843 cups_printer->remote_cups_connection_test = gtk_cups_connection_test_new (cups_printer->hostname);
2845 if (cups_request_ppd (printer))
2846 cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (50,
2847 (GSourceFunc) cups_request_ppd,
2852 cups_request_ppd (printer);
2857 ppd_text_to_utf8 (ppd_file_t *ppd_file,
2860 const char *encoding = NULL;
2863 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
2865 return g_strdup (text);
2867 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
2869 encoding = "ISO-8859-1";
2871 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
2873 encoding = "ISO-8859-2";
2875 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
2877 encoding = "ISO-8859-5";
2879 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
2881 encoding = "SHIFT-JIS";
2883 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
2885 encoding = "MACINTOSH";
2887 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
2889 encoding = "WINDOWS-1252";
2893 /* Fallback, try iso-8859-1... */
2894 encoding = "ISO-8859-1";
2897 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
2902 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
2903 res = g_strdup ("???");
2909 /* TODO: Add more translations for common settings here */
2911 static const struct {
2912 const char *keyword;
2913 const char *translation;
2914 } cups_option_translations[] = {
2915 { "Duplex", N_("Two Sided") },
2916 { "MediaType", N_("Paper Type") },
2917 { "InputSlot", N_("Paper Source") },
2918 { "OutputBin", N_("Output Tray") },
2919 { "Resolution", N_("Resolution") },
2920 { "PreFilter", N_("GhostScript pre-filtering") },
2924 static const struct {
2925 const char *keyword;
2927 const char *translation;
2928 } cups_choice_translations[] = {
2929 { "Duplex", "None", N_("One Sided") },
2930 /* Translators: this is an option of "Two Sided" */
2931 { "Duplex", "DuplexNoTumble", N_("Long Edge (Standard)") },
2932 /* Translators: this is an option of "Two Sided" */
2933 { "Duplex", "DuplexTumble", N_("Short Edge (Flip)") },
2934 /* Translators: this is an option of "Paper Source" */
2935 { "InputSlot", "Auto", N_("Auto Select") },
2936 /* Translators: this is an option of "Paper Source" */
2937 { "InputSlot", "AutoSelect", N_("Auto Select") },
2938 /* Translators: this is an option of "Paper Source" */
2939 { "InputSlot", "Default", N_("Printer Default") },
2940 /* Translators: this is an option of "Paper Source" */
2941 { "InputSlot", "None", N_("Printer Default") },
2942 /* Translators: this is an option of "Paper Source" */
2943 { "InputSlot", "PrinterDefault", N_("Printer Default") },
2944 /* Translators: this is an option of "Paper Source" */
2945 { "InputSlot", "Unspecified", N_("Auto Select") },
2946 /* Translators: this is an option of "Resolution" */
2947 { "Resolution", "default", N_("Printer Default") },
2948 /* Translators: this is an option of "GhostScript" */
2949 { "PreFilter", "EmbedFonts", N_("Embed GhostScript fonts only") },
2950 /* Translators: this is an option of "GhostScript" */
2951 { "PreFilter", "Level1", N_("Convert to PS level 1") },
2952 /* Translators: this is an option of "GhostScript" */
2953 { "PreFilter", "Level2", N_("Convert to PS level 2") },
2954 /* Translators: this is an option of "GhostScript" */
2955 { "PreFilter", "No", N_("No pre-filtering") },
2958 static const struct {
2960 const char *translation;
2961 } cups_group_translations[] = {
2962 /* Translators: "Miscellaneous" is the label for a button, that opens
2963 up an extra panel of settings in a print dialog. */
2964 { "Miscellaneous", N_("Miscellaneous") },
2967 static const struct {
2968 const char *ppd_keyword;
2970 } ppd_option_names[] = {
2971 {"Duplex", "gtk-duplex" },
2972 {"MediaType", "gtk-paper-type"},
2973 {"InputSlot", "gtk-paper-source"},
2974 {"OutputBin", "gtk-output-tray"},
2977 static const struct {
2978 const char *lpoption;
2980 } lpoption_names[] = {
2981 {"number-up", "gtk-n-up" },
2982 {"number-up-layout", "gtk-n-up-layout"},
2983 {"job-billing", "gtk-billing-info"},
2984 {"job-priority", "gtk-job-prio"},
2987 /* keep sorted when changing */
2988 static const char *color_option_whitelist[] = {
2989 "BRColorEnhancement",
2995 "BlackSubstitution",
3003 "RPSBlackOverPrint",
3007 /* keep sorted when changing */
3008 static const char *color_group_whitelist[] = {
3015 "HPColorOptionsPanel",
3018 /* keep sorted when changing */
3019 static const char *image_quality_option_whitelist[] = {
3021 "BRHalfTonePattern",
3031 "HPGraphicsHalftone",
3046 /* keep sorted when changing */
3047 static const char *image_quality_group_whitelist[] = {
3054 /* keep sorted when changing */
3055 static const char * finishing_option_whitelist[] = {
3068 "StapleOrientation",
3074 /* keep sorted when changing */
3075 static const char *finishing_group_whitelist[] = {
3084 /* keep sorted when changing */
3085 static const char *cups_option_blacklist[] = {
3094 get_option_text (ppd_file_t *ppd_file,
3095 ppd_option_t *option)
3100 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
3102 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
3103 return g_strdup (_(cups_option_translations[i].translation));
3106 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
3108 /* Some ppd files have spaces in the text before the colon */
3115 get_choice_text (ppd_file_t *ppd_file,
3116 ppd_choice_t *choice)
3119 ppd_option_t *option = choice->option;
3120 const char *keyword = option->keyword;
3122 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
3124 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
3125 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
3126 return g_strdup (_(cups_choice_translations[i].translation));
3128 return ppd_text_to_utf8 (ppd_file, choice->text);
3132 group_has_option (ppd_group_t *group,
3133 ppd_option_t *option)
3140 if (group->num_options > 0 &&
3141 option >= group->options && option < group->options + group->num_options)
3144 for (i = 0; i < group->num_subgroups; i++)
3146 if (group_has_option (&group->subgroups[i],option))
3153 set_option_off (GtkPrinterOption *option)
3155 /* Any of these will do, _set only applies the value
3156 * if its allowed of the option */
3157 gtk_printer_option_set (option, "False");
3158 gtk_printer_option_set (option, "Off");
3159 gtk_printer_option_set (option, "None");
3163 value_is_off (const char *value)
3165 return (strcasecmp (value, "None") == 0 ||
3166 strcasecmp (value, "Off") == 0 ||
3167 strcasecmp (value, "False") == 0);
3171 ppd_group_name (ppd_group_t *group)
3177 available_choices (ppd_file_t *ppd,
3178 ppd_option_t *option,
3179 ppd_choice_t ***available,
3180 gboolean keep_if_only_one_option)
3182 ppd_option_t *other_option;
3185 ppd_const_t *constraint;
3186 const char *choice, *other_choice;
3187 ppd_option_t *option1, *option2;
3188 ppd_group_t *installed_options;
3190 gboolean all_default;
3196 conflicts = g_new0 (char, option->num_choices);
3198 installed_options = NULL;
3199 for (i = 0; i < ppd->num_groups; i++)
3203 name = ppd_group_name (&ppd->groups[i]);
3204 if (strcmp (name, "InstallableOptions") == 0)
3206 installed_options = &ppd->groups[i];
3211 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
3213 option1 = ppdFindOption (ppd, constraint->option1);
3214 if (option1 == NULL)
3217 option2 = ppdFindOption (ppd, constraint->option2);
3218 if (option2 == NULL)
3221 if (option == option1)
3223 choice = constraint->choice1;
3224 other_option = option2;
3225 other_choice = constraint->choice2;
3227 else if (option == option2)
3229 choice = constraint->choice2;
3230 other_option = option1;
3231 other_choice = constraint->choice1;
3236 /* We only care of conflicts with installed_options and
3238 if (!group_has_option (installed_options, other_option) &&
3239 (strcmp (other_option->keyword, "PageSize") != 0))
3242 if (*other_choice == 0)
3244 /* Conflict only if the installed option is not off */
3245 if (value_is_off (other_option->defchoice))
3248 /* Conflict if the installed option has the specified default */
3249 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
3254 /* Conflict with all non-off choices */
3255 for (j = 0; j < option->num_choices; j++)
3257 if (!value_is_off (option->choices[j].choice))
3263 for (j = 0; j < option->num_choices; j++)
3265 if (strcasecmp (option->choices[j].choice, choice) == 0)
3273 for (j = 0; j < option->num_choices; j++)
3277 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
3278 all_default = FALSE;
3281 if ((all_default && !keep_if_only_one_option) ||
3282 (num_conflicts == option->num_choices))
3289 /* Some ppds don't have a "use printer default" option for
3290 * InputSlot. This means you always have to select a particular slot,
3291 * and you can't auto-pick source based on the paper size. To support
3292 * this we always add an auto option if there isn't one already. If
3293 * the user chooses the generated option we don't send any InputSlot
3294 * value when printing. The way we detect existing auto-cases is based
3295 * on feedback from Michael Sweet of cups fame.
3298 if (strcmp (option->keyword, "InputSlot") == 0)
3300 gboolean found_auto = FALSE;
3301 for (j = 0; j < option->num_choices; j++)
3305 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
3306 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
3307 strcmp (option->choices[j].choice, "Default") == 0 ||
3308 strcmp (option->choices[j].choice, "None") == 0 ||
3309 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
3310 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
3311 option->choices[j].code == NULL ||
3312 option->choices[j].code[0] == 0)
3326 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
3329 for (j = 0; j < option->num_choices; j++)
3332 (*available)[i++] = &option->choices[j];
3336 (*available)[i++] = NULL;
3341 return option->num_choices - num_conflicts + add_auto;
3344 static GtkPrinterOption *
3345 create_pickone_option (ppd_file_t *ppd_file,
3346 ppd_option_t *ppd_option,
3347 const gchar *gtk_name)
3349 GtkPrinterOption *option;
3350 ppd_choice_t **available;
3354 ppd_coption_t *coption;
3356 g_assert (ppd_option->ui == PPD_UI_PICKONE);
3360 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
3364 /* right now only support one parameter per custom option
3365 * if more than one print warning and only offer the default choices
3368 label = get_option_text (ppd_file, ppd_option);
3370 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
3374 ppd_cparam_t *cparam;
3376 cparam = ppdFirstCustomParam (coption);
3378 if (ppdNextCustomParam (coption) == NULL)
3380 switch (cparam->type)
3382 case PPD_CUSTOM_INT:
3383 option = gtk_printer_option_new (gtk_name, label,
3384 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
3386 case PPD_CUSTOM_PASSCODE:
3387 option = gtk_printer_option_new (gtk_name, label,
3388 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
3390 case PPD_CUSTOM_PASSWORD:
3391 option = gtk_printer_option_new (gtk_name, label,
3392 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
3394 case PPD_CUSTOM_REAL:
3395 option = gtk_printer_option_new (gtk_name, label,
3396 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
3398 case PPD_CUSTOM_STRING:
3399 option = gtk_printer_option_new (gtk_name, label,
3400 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
3402 #ifdef PRINT_IGNORED_OPTIONS
3403 case PPD_CUSTOM_POINTS:
3404 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
3406 case PPD_CUSTOM_CURVE:
3407 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
3409 case PPD_CUSTOM_INVCURVE:
3410 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
3417 #ifdef PRINT_IGNORED_OPTIONS
3419 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
3424 option = gtk_printer_option_new (gtk_name, label,
3425 GTK_PRINTER_OPTION_TYPE_PICKONE);
3428 gtk_printer_option_allocate_choices (option, n_choices);
3429 for (i = 0; i < n_choices; i++)
3431 if (available[i] == NULL)
3433 /* This was auto-added */
3434 option->choices[i] = g_strdup ("gtk-ignore-value");
3435 option->choices_display[i] = g_strdup (_("Printer Default"));
3439 option->choices[i] = g_strdup (available[i]->choice);
3440 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
3444 if (option->type != GTK_PRINTER_OPTION_TYPE_PICKONE)
3446 if (g_str_has_prefix (ppd_option->defchoice, "Custom."))
3447 gtk_printer_option_set (option, ppd_option->defchoice + 7);
3449 gtk_printer_option_set (option, ppd_option->defchoice);
3453 gtk_printer_option_set (option, ppd_option->defchoice);
3456 #ifdef PRINT_IGNORED_OPTIONS
3458 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
3465 static GtkPrinterOption *
3466 create_boolean_option (ppd_file_t *ppd_file,
3467 ppd_option_t *ppd_option,
3468 const gchar *gtk_name)
3470 GtkPrinterOption *option;
3471 ppd_choice_t **available;
3475 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
3479 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
3482 label = get_option_text (ppd_file, ppd_option);
3483 option = gtk_printer_option_new (gtk_name, label,
3484 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
3487 gtk_printer_option_allocate_choices (option, 2);
3488 option->choices[0] = g_strdup ("True");
3489 option->choices_display[0] = g_strdup ("True");
3490 option->choices[1] = g_strdup ("False");
3491 option->choices_display[1] = g_strdup ("False");
3493 gtk_printer_option_set (option, ppd_option->defchoice);
3495 #ifdef PRINT_IGNORED_OPTIONS
3497 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
3505 get_ppd_option_name (const gchar *keyword)
3509 for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
3510 if (strcmp (ppd_option_names[i].ppd_keyword, keyword) == 0)
3511 return g_strdup (ppd_option_names[i].name);
3513 return g_strdup_printf ("cups-%s", keyword);
3517 get_lpoption_name (const gchar *lpoption)
3521 for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
3522 if (strcmp (ppd_option_names[i].ppd_keyword, lpoption) == 0)
3523 return g_strdup (ppd_option_names[i].name);
3525 for (i = 0; i < G_N_ELEMENTS (lpoption_names); i++)
3526 if (strcmp (lpoption_names[i].lpoption, lpoption) == 0)
3527 return g_strdup (lpoption_names[i].name);
3529 return g_strdup_printf ("cups-%s", lpoption);
3533 strptr_cmp (const void *a,
3536 char **aa = (char **)a;
3537 char **bb = (char **)b;
3538 return strcmp (*aa, *bb);
3543 string_in_table (gchar *str,
3544 const gchar *table[],
3547 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
3550 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
3553 handle_option (GtkPrinterOptionSet *set,
3554 ppd_file_t *ppd_file,
3555 ppd_option_t *ppd_option,
3556 ppd_group_t *toplevel_group,
3557 GtkPrintSettings *settings)
3559 GtkPrinterOption *option;
3563 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
3566 name = get_ppd_option_name (ppd_option->keyword);
3569 if (ppd_option->ui == PPD_UI_PICKONE)
3571 option = create_pickone_option (ppd_file, ppd_option, name);
3573 else if (ppd_option->ui == PPD_UI_BOOLEAN)
3575 option = create_boolean_option (ppd_file, ppd_option, name);
3577 #ifdef PRINT_IGNORED_OPTIONS
3579 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
3586 name = ppd_group_name (toplevel_group);
3587 if (STRING_IN_TABLE (name,
3588 color_group_whitelist) ||
3589 STRING_IN_TABLE (ppd_option->keyword,
3590 color_option_whitelist))
3592 option->group = g_strdup ("ColorPage");
3594 else if (STRING_IN_TABLE (name,
3595 image_quality_group_whitelist) ||
3596 STRING_IN_TABLE (ppd_option->keyword,
3597 image_quality_option_whitelist))
3599 option->group = g_strdup ("ImageQualityPage");
3601 else if (STRING_IN_TABLE (name,
3602 finishing_group_whitelist) ||
3603 STRING_IN_TABLE (ppd_option->keyword,
3604 finishing_option_whitelist))
3606 option->group = g_strdup ("FinishingPage");
3610 for (i = 0; i < G_N_ELEMENTS (cups_group_translations); i++)
3612 if (strcmp (cups_group_translations[i].name, toplevel_group->name) == 0)
3614 option->group = g_strdup (_(cups_group_translations[i].translation));
3619 if (i == G_N_ELEMENTS (cups_group_translations))
3620 option->group = g_strdup (toplevel_group->text);
3623 set_option_from_settings (option, settings);
3625 gtk_printer_option_set_add (set, option);
3632 handle_group (GtkPrinterOptionSet *set,
3633 ppd_file_t *ppd_file,
3635 ppd_group_t *toplevel_group,
3636 GtkPrintSettings *settings)
3641 /* Ignore installable options */
3642 name = ppd_group_name (toplevel_group);
3643 if (strcmp (name, "InstallableOptions") == 0)
3646 for (i = 0; i < group->num_options; i++)
3647 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
3649 for (i = 0; i < group->num_subgroups; i++)
3650 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
3657 GtkPrintSettings *settings;
3658 GtkPrinter *printer;
3659 } GtkPrintBackendCupsColordHelper;
3662 colord_printer_option_set_changed_cb (GtkPrinterOptionSet *set,
3663 GtkPrintBackendCupsColordHelper *helper)
3665 gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (helper->printer),
3671 static GtkPrinterOptionSet *
3672 cups_printer_get_options (GtkPrinter *printer,
3673 GtkPrintSettings *settings,
3674 GtkPageSetup *page_setup,
3675 GtkPrintCapabilities capabilities)
3677 GtkPrinterOptionSet *set;
3678 GtkPrinterOption *option;
3679 ppd_file_t *ppd_file;
3681 char *print_at[] = { "now", "at", "on-hold" };
3682 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
3683 char *prio[] = {"100", "80", "50", "30" };
3684 /* Translators: These strings name the possible values of the
3685 * job priority option in the print dialog
3687 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
3688 char *n_up_layout[] = { "lrtb", "lrbt", "rltb", "rlbt", "tblr", "tbrl", "btlr", "btrl" };
3689 /* Translators: These strings name the possible arrangements of
3690 * multiple pages on a sheet when printing
3692 char *n_up_layout_display[] = { N_("Left to right, top to bottom"), N_("Left to right, bottom to top"),
3693 N_("Right to left, top to bottom"), N_("Right to left, bottom to top"),
3694 N_("Top to bottom, left to right"), N_("Top to bottom, right to left"),
3695 N_("Bottom to top, left to right"), N_("Bottom to top, right to left") };
3698 cups_option_t *opts = NULL;
3699 GtkPrintBackendCups *backend;
3700 GtkTextDirection text_direction;
3701 GtkPrinterCups *cups_printer = NULL;
3703 GtkPrintBackendCupsColordHelper *helper;
3705 char *default_number_up;
3707 set = gtk_printer_option_set_new ();
3709 /* Cups specific, non-ppd related settings */
3711 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
3712 prio_display[i] = _(prio_display[i]);
3714 /* Translators, this string is used to label the job priority option
3715 * in the print dialog
3717 option = gtk_printer_option_new ("gtk-job-prio", _("Job Priority"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3718 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
3719 prio, prio_display);
3720 gtk_printer_option_set (option, "50");
3721 set_option_from_settings (option, settings);
3722 gtk_printer_option_set_add (set, option);
3723 g_object_unref (option);
3725 /* Translators, this string is used to label the billing info entry
3726 * in the print dialog
3728 option = gtk_printer_option_new ("gtk-billing-info", _("Billing Info"), GTK_PRINTER_OPTION_TYPE_STRING);
3729 gtk_printer_option_set (option, "");
3730 set_option_from_settings (option, settings);
3731 gtk_printer_option_set_add (set, option);
3732 g_object_unref (option);
3734 backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
3735 cups_printer = GTK_PRINTER_CUPS (printer);
3737 if (backend != NULL && printer != NULL)
3739 char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
3740 /* Translators, these strings are names for various 'standard' cover
3741 * pages that the printing system may support.
3743 char *cover_display_default[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
3744 char **cover = NULL;
3745 char **cover_display = NULL;
3746 char **cover_display_translated = NULL;
3747 gint num_of_covers = 0;
3751 /* Translators, this string is used to label the pages-per-sheet option
3752 * in the print dialog
3754 option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3755 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
3757 default_number_up = g_strdup_printf ("%d", cups_printer->default_number_up);
3758 gtk_printer_option_set (option, default_number_up);
3759 g_free (default_number_up);
3760 set_option_from_settings (option, settings);
3761 gtk_printer_option_set_add (set, option);
3762 g_object_unref (option);
3764 if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
3766 for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
3767 n_up_layout_display[i] = _(n_up_layout_display[i]);
3769 /* Translators, this string is used to label the option in the print
3770 * dialog that controls in what order multiple pages are arranged
3772 option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3773 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
3774 n_up_layout, n_up_layout_display);
3776 text_direction = gtk_widget_get_default_direction ();
3777 if (text_direction == GTK_TEXT_DIR_LTR)
3778 gtk_printer_option_set (option, "lrtb");
3780 gtk_printer_option_set (option, "rltb");
3782 set_option_from_settings (option, settings);
3783 gtk_printer_option_set_add (set, option);
3784 g_object_unref (option);
3787 num_of_covers = backend->number_of_covers;
3788 cover = g_new (char *, num_of_covers + 1);
3789 cover[num_of_covers] = NULL;
3790 cover_display = g_new (char *, num_of_covers + 1);
3791 cover_display[num_of_covers] = NULL;
3792 cover_display_translated = g_new (char *, num_of_covers + 1);
3793 cover_display_translated[num_of_covers] = NULL;
3795 for (i = 0; i < num_of_covers; i++)
3797 cover[i] = g_strdup (backend->covers[i]);
3799 for (j = 0; j < G_N_ELEMENTS (cover_default); j++)
3800 if (strcmp (cover_default[j], cover[i]) == 0)
3802 value = cover_display_default[j];
3805 cover_display[i] = (value != NULL) ? g_strdup (value) : g_strdup (backend->covers[i]);
3808 for (i = 0; i < num_of_covers; i++)
3809 cover_display_translated[i] = _(cover_display[i]);
3811 /* Translators, this is the label used for the option in the print
3812 * dialog that controls the front cover page.
3814 option = gtk_printer_option_new ("gtk-cover-before", _("Before"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3815 gtk_printer_option_choices_from_array (option, num_of_covers,
3816 cover, cover_display_translated);
3818 if (cups_printer->default_cover_before != NULL)
3819 gtk_printer_option_set (option, cups_printer->default_cover_before);
3821 gtk_printer_option_set (option, "none");
3822 set_option_from_settings (option, settings);
3823 gtk_printer_option_set_add (set, option);
3824 g_object_unref (option);
3826 /* Translators, this is the label used for the option in the print
3827 * dialog that controls the back cover page.
3829 option = gtk_printer_option_new ("gtk-cover-after", _("After"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3830 gtk_printer_option_choices_from_array (option, num_of_covers,
3831 cover, cover_display_translated);
3832 if (cups_printer->default_cover_after != NULL)
3833 gtk_printer_option_set (option, cups_printer->default_cover_after);
3835 gtk_printer_option_set (option, "none");
3836 set_option_from_settings (option, settings);
3837 gtk_printer_option_set_add (set, option);
3838 g_object_unref (option);
3841 g_strfreev (cover_display);
3842 g_free (cover_display_translated);
3845 /* Translators: this is the name of the option that controls when
3846 * a print job is printed. Possible values are 'now', a specified time,
3849 option = gtk_printer_option_new ("gtk-print-time", _("Print at"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3850 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
3851 print_at, print_at);
3852 gtk_printer_option_set (option, "now");
3853 set_option_from_settings (option, settings);
3854 gtk_printer_option_set_add (set, option);
3855 g_object_unref (option);
3857 /* Translators: this is the name of the option that allows the user
3858 * to specify a time when a print job will be printed.
3860 option = gtk_printer_option_new ("gtk-print-time-text", _("Print at time"), GTK_PRINTER_OPTION_TYPE_STRING);
3861 gtk_printer_option_set (option, "");
3862 set_option_from_settings (option, settings);
3863 gtk_printer_option_set_add (set, option);
3864 g_object_unref (option);
3866 /* Printer (ppd) specific settings */
3867 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3870 GtkPaperSize *paper_size;
3871 ppd_option_t *option;
3872 const gchar *ppd_name;
3874 ppdMarkDefaults (ppd_file);
3876 paper_size = gtk_page_setup_get_paper_size (page_setup);
3878 option = ppdFindOption (ppd_file, "PageSize");
3879 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
3882 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
3886 char width[G_ASCII_DTOSTR_BUF_SIZE];
3887 char height[G_ASCII_DTOSTR_BUF_SIZE];
3889 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3890 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3891 /* Translators: this format is used to display a custom paper
3892 * size. The two placeholders are replaced with the width and height
3893 * in points. E.g: "Custom 230.4x142.9"
3895 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
3896 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
3897 g_free (custom_name);
3900 for (i = 0; i < ppd_file->num_groups; i++)
3901 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
3904 /* Now honor the user set defaults for this printer */
3905 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
3907 for (i = 0; i < num_opts; i++)
3909 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
3912 name = get_lpoption_name (opts[i].name);
3913 if (strcmp (name, "cups-job-sheets") == 0)
3918 values = g_strsplit (opts[i].value, ",", 2);
3919 num_values = g_strv_length (values);
3921 option = gtk_printer_option_set_lookup (set, "gtk-cover-before");
3922 if (option && num_values > 0)
3923 gtk_printer_option_set (option, g_strstrip (values[0]));
3925 option = gtk_printer_option_set_lookup (set, "gtk-cover-after");
3926 if (option && num_values > 1)
3927 gtk_printer_option_set (option, g_strstrip (values[1]));
3929 g_strfreev (values);
3931 else if (strcmp (name, "cups-job-hold-until") == 0)
3933 GtkPrinterOption *option2 = NULL;
3935 option = gtk_printer_option_set_lookup (set, "gtk-print-time-text");
3936 if (option && opts[i].value)
3938 option2 = gtk_printer_option_set_lookup (set, "gtk-print-time");
3941 if (strcmp (opts[i].value, "indefinite") == 0)
3942 gtk_printer_option_set (option2, "on-hold");
3945 gtk_printer_option_set (option2, "at");
3946 gtk_printer_option_set (option, opts[i].value);
3951 else if (strcmp (name, "cups-sides") == 0)
3953 option = gtk_printer_option_set_lookup (set, "gtk-duplex");
3954 if (option && opts[i].value)
3956 if (strcmp (opts[i].value, "two-sided-short-edge") == 0)
3957 gtk_printer_option_set (option, "DuplexTumble");
3958 else if (strcmp (opts[i].value, "two-sided-long-edge") == 0)
3959 gtk_printer_option_set (option, "DuplexNoTumble");
3964 option = gtk_printer_option_set_lookup (set, name);
3966 gtk_printer_option_set (option, opts[i].value);
3971 cupsFreeOptions (num_opts, opts);
3974 /* TRANSLATORS: this this the ICC color profile to use for this job */
3975 option = gtk_printer_option_new ("colord-profile",
3976 _("Printer Profile"),
3977 GTK_PRINTER_OPTION_TYPE_INFO);
3979 /* assign it to the color page */
3980 option->group = g_strdup ("ColorPage");
3982 /* TRANSLATORS: this is when color profile information is unavailable */
3983 gtk_printer_option_set (option, _("Unavailable"));
3984 gtk_printer_option_set_add (set, option);
3986 /* watch to see if the user changed the options */
3987 helper = g_new (GtkPrintBackendCupsColordHelper, 1);
3988 helper->printer = printer;
3989 helper->settings = settings;
3990 g_signal_connect_data (set, "changed",
3991 G_CALLBACK (colord_printer_option_set_changed_cb),
3993 (GClosureNotify) g_free,
3996 /* initial coldplug */
3997 gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (printer),
3999 g_object_bind_property (printer, "profile-title",
4010 mark_option_from_set (GtkPrinterOptionSet *set,
4011 ppd_file_t *ppd_file,
4012 ppd_option_t *ppd_option)
4014 GtkPrinterOption *option;
4015 char *name = get_ppd_option_name (ppd_option->keyword);
4017 option = gtk_printer_option_set_lookup (set, name);
4020 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
4027 mark_group_from_set (GtkPrinterOptionSet *set,
4028 ppd_file_t *ppd_file,
4033 for (i = 0; i < group->num_options; i++)
4034 mark_option_from_set (set, ppd_file, &group->options[i]);
4036 for (i = 0; i < group->num_subgroups; i++)
4037 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
4041 set_conflicts_from_option (GtkPrinterOptionSet *set,
4042 ppd_file_t *ppd_file,
4043 ppd_option_t *ppd_option)
4045 GtkPrinterOption *option;
4048 if (ppd_option->conflicted)
4050 name = get_ppd_option_name (ppd_option->keyword);
4051 option = gtk_printer_option_set_lookup (set, name);
4054 gtk_printer_option_set_has_conflict (option, TRUE);
4055 #ifdef PRINT_IGNORED_OPTIONS
4057 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
4065 set_conflicts_from_group (GtkPrinterOptionSet *set,
4066 ppd_file_t *ppd_file,
4071 for (i = 0; i < group->num_options; i++)
4072 set_conflicts_from_option (set, ppd_file, &group->options[i]);
4074 for (i = 0; i < group->num_subgroups; i++)
4075 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
4079 cups_printer_mark_conflicts (GtkPrinter *printer,
4080 GtkPrinterOptionSet *options)
4082 ppd_file_t *ppd_file;
4086 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4088 if (ppd_file == NULL)
4091 ppdMarkDefaults (ppd_file);
4093 for (i = 0; i < ppd_file->num_groups; i++)
4094 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
4096 num_conflicts = ppdConflicts (ppd_file);
4098 if (num_conflicts > 0)
4100 for (i = 0; i < ppd_file->num_groups; i++)
4101 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
4104 return num_conflicts > 0;
4108 GtkPrinter *printer;
4109 GtkPrinterOptionSet *options;
4110 GtkPrintSettings *settings;
4111 ppd_file_t *ppd_file;
4116 const char *standard;
4120 map_settings_to_option (GtkPrinterOption *option,
4121 const NameMapping table[],
4123 GtkPrintSettings *settings,
4124 const gchar *standard_name,
4125 const gchar *cups_name)
4129 const char *cups_value;
4130 const char *standard_value;
4132 /* If the cups-specific setting is set, always use that */
4133 name = g_strdup_printf ("cups-%s", cups_name);
4134 cups_value = gtk_print_settings_get (settings, name);
4137 if (cups_value != NULL)
4139 gtk_printer_option_set (option, cups_value);
4143 /* Otherwise we try to convert from the general setting */
4144 standard_value = gtk_print_settings_get (settings, standard_name);
4145 if (standard_value == NULL)
4148 for (i = 0; i < n_elements; i++)
4150 if (table[i].cups == NULL && table[i].standard == NULL)
4152 gtk_printer_option_set (option, standard_value);
4155 else if (table[i].cups == NULL &&
4156 strcmp (table[i].standard, standard_value) == 0)
4158 set_option_off (option);
4161 else if (strcmp (table[i].standard, standard_value) == 0)
4163 gtk_printer_option_set (option, table[i].cups);
4170 map_option_to_settings (const gchar *value,
4171 const NameMapping table[],
4173 GtkPrintSettings *settings,
4174 const gchar *standard_name,
4175 const gchar *cups_name)
4180 for (i = 0; i < n_elements; i++)
4182 if (table[i].cups == NULL && table[i].standard == NULL)
4184 gtk_print_settings_set (settings,
4189 else if (table[i].cups == NULL && table[i].standard != NULL)
4191 if (value_is_off (value))
4193 gtk_print_settings_set (settings,
4199 else if (strcmp (table[i].cups, value) == 0)
4201 gtk_print_settings_set (settings,
4208 /* Always set the corresponding cups-specific setting */
4209 name = g_strdup_printf ("cups-%s", cups_name);
4210 gtk_print_settings_set (settings, name, value);
4215 static const NameMapping paper_source_map[] = {
4216 { "Lower", "lower"},
4217 { "Middle", "middle"},
4218 { "Upper", "upper"},
4220 { "Envelope", "envelope"},
4221 { "Cassette", "cassette"},
4222 { "LargeCapacity", "large-capacity"},
4223 { "AnySmallFormat", "small-format"},
4224 { "AnyLargeFormat", "large-format"},
4228 static const NameMapping output_tray_map[] = {
4229 { "Upper", "upper"},
4230 { "Lower", "lower"},
4235 static const NameMapping duplex_map[] = {
4236 { "DuplexTumble", "vertical" },
4237 { "DuplexNoTumble", "horizontal" },
4241 static const NameMapping output_mode_map[] = {
4242 { "Standard", "normal" },
4243 { "Normal", "normal" },
4244 { "Draft", "draft" },
4245 { "Fast", "draft" },
4248 static const NameMapping media_type_map[] = {
4249 { "Transparency", "transparency"},
4250 { "Standard", "stationery"},
4254 static const NameMapping all_map[] = {
4260 set_option_from_settings (GtkPrinterOption *option,
4261 GtkPrintSettings *settings)
4263 const char *cups_value;
4266 if (settings == NULL)
4269 if (strcmp (option->name, "gtk-paper-source") == 0)
4270 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
4271 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
4272 else if (strcmp (option->name, "gtk-output-tray") == 0)
4273 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
4274 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
4275 else if (strcmp (option->name, "gtk-duplex") == 0)
4276 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
4277 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
4278 else if (strcmp (option->name, "cups-OutputMode") == 0)
4279 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
4280 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
4281 else if (strcmp (option->name, "cups-Resolution") == 0)
4283 cups_value = gtk_print_settings_get (settings, option->name);
4285 gtk_printer_option_set (option, cups_value);
4288 if (gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION, -1) != -1 ||
4289 gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_X, -1) != -1 ||
4290 gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_Y, -1) != -1 ||
4291 option->value == NULL || option->value[0] == '\0')
4293 int res = gtk_print_settings_get_resolution (settings);
4294 int res_x = gtk_print_settings_get_resolution_x (settings);
4295 int res_y = gtk_print_settings_get_resolution_y (settings);
4299 value = g_strdup_printf ("%dx%ddpi", res_x, res_y);
4300 gtk_printer_option_set (option, value);
4305 value = g_strdup_printf ("%ddpi", res);
4306 gtk_printer_option_set (option, value);
4312 else if (strcmp (option->name, "gtk-paper-type") == 0)
4313 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
4314 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
4315 else if (strcmp (option->name, "gtk-n-up") == 0)
4317 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
4318 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
4320 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
4322 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
4323 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
4325 else if (strcmp (option->name, "gtk-billing-info") == 0)
4327 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
4329 gtk_printer_option_set (option, cups_value);
4331 else if (strcmp (option->name, "gtk-job-prio") == 0)
4333 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
4335 gtk_printer_option_set (option, cups_value);
4337 else if (strcmp (option->name, "gtk-cover-before") == 0)
4339 cups_value = gtk_print_settings_get (settings, "cover-before");
4341 gtk_printer_option_set (option, cups_value);
4343 else if (strcmp (option->name, "gtk-cover-after") == 0)
4345 cups_value = gtk_print_settings_get (settings, "cover-after");
4347 gtk_printer_option_set (option, cups_value);
4349 else if (strcmp (option->name, "gtk-print-time") == 0)
4351 cups_value = gtk_print_settings_get (settings, "print-at");
4353 gtk_printer_option_set (option, cups_value);
4355 else if (strcmp (option->name, "gtk-print-time-text") == 0)
4357 cups_value = gtk_print_settings_get (settings, "print-at-time");
4359 gtk_printer_option_set (option, cups_value);
4361 else if (g_str_has_prefix (option->name, "cups-"))
4363 cups_value = gtk_print_settings_get (settings, option->name);
4365 gtk_printer_option_set (option, cups_value);
4370 foreach_option_get_settings (GtkPrinterOption *option,
4373 struct OptionData *data = user_data;
4374 GtkPrintSettings *settings = data->settings;
4377 value = option->value;
4379 if (strcmp (option->name, "gtk-paper-source") == 0)
4380 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
4381 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
4382 else if (strcmp (option->name, "gtk-output-tray") == 0)
4383 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
4384 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
4385 else if (strcmp (option->name, "gtk-duplex") == 0)
4386 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
4387 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
4388 else if (strcmp (option->name, "cups-OutputMode") == 0)
4389 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
4390 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
4391 else if (strcmp (option->name, "cups-Resolution") == 0)
4393 int res, res_x, res_y;
4395 if (sscanf (value, "%dx%ddpi", &res_x, &res_y) == 2)
4397 if (res_x > 0 && res_y > 0)
4398 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
4400 else if (sscanf (value, "%ddpi", &res) == 1)
4403 gtk_print_settings_set_resolution (settings, res);
4406 gtk_print_settings_set (settings, option->name, value);
4408 else if (strcmp (option->name, "gtk-paper-type") == 0)
4409 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
4410 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
4411 else if (strcmp (option->name, "gtk-n-up") == 0)
4412 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
4413 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
4414 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
4415 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
4416 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
4417 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
4418 gtk_print_settings_set (settings, "cups-job-billing", value);
4419 else if (strcmp (option->name, "gtk-job-prio") == 0)
4420 gtk_print_settings_set (settings, "cups-job-priority", value);
4421 else if (strcmp (option->name, "gtk-cover-before") == 0)
4422 gtk_print_settings_set (settings, "cover-before", value);
4423 else if (strcmp (option->name, "gtk-cover-after") == 0)
4424 gtk_print_settings_set (settings, "cover-after", value);
4425 else if (strcmp (option->name, "gtk-print-time") == 0)
4426 gtk_print_settings_set (settings, "print-at", value);
4427 else if (strcmp (option->name, "gtk-print-time-text") == 0)
4428 gtk_print_settings_set (settings, "print-at-time", value);
4429 else if (g_str_has_prefix (option->name, "cups-"))
4430 gtk_print_settings_set (settings, option->name, value);
4434 supports_am_pm (void)
4436 struct tm tmp_tm = { 0 };
4440 length = strftime (time, sizeof (time), "%p", &tmp_tm);
4445 /* Converts local time to UTC time. Local time has to be in one of these
4446 * formats: HH:MM:SS, HH:MM, HH:MM:SS {am, pm}, HH:MM {am, pm}, HH {am, pm},
4447 * {am, pm} HH:MM:SS, {am, pm} HH:MM, {am, pm} HH.
4448 * Returns a newly allocated string holding UTC time in HH:MM:SS format
4452 localtime_to_utctime (const char *local_time)
4454 const char *formats_0[] = {" %I : %M : %S %p ", " %p %I : %M : %S ",
4456 " %I : %M %p ", " %p %I : %M ",
4458 " %I %p ", " %p %I "};
4459 const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
4460 const char *end = NULL;
4461 struct tm *actual_local_time;
4462 struct tm *actual_utc_time;
4463 struct tm local_print_time;
4464 struct tm utc_print_time;
4465 struct tm diff_time;
4466 gchar *utc_time = NULL;
4469 if (local_time == NULL || local_time[0] == '\0')
4472 n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
4474 for (i = 0; i < n; i++)
4476 local_print_time.tm_hour = 0;
4477 local_print_time.tm_min = 0;
4478 local_print_time.tm_sec = 0;
4480 if (supports_am_pm ())
4481 end = strptime (local_time, formats_0[i], &local_print_time);
4483 end = strptime (local_time, formats_1[i], &local_print_time);
4485 if (end != NULL && end[0] == '\0')
4489 if (end != NULL && end[0] == '\0')
4494 actual_utc_time = g_memdup (gmtime (&rawtime), sizeof (struct tm));
4495 actual_local_time = g_memdup (localtime (&rawtime), sizeof (struct tm));
4497 diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
4498 diff_time.tm_min = actual_utc_time->tm_min - actual_local_time->tm_min;
4499 diff_time.tm_sec = actual_utc_time->tm_sec - actual_local_time->tm_sec;
4501 utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
4502 utc_print_time.tm_min = ((local_print_time.tm_min + diff_time.tm_min) + 60) % 60;
4503 utc_print_time.tm_sec = ((local_print_time.tm_sec + diff_time.tm_sec) + 60) % 60;
4505 utc_time = g_strdup_printf ("%02d:%02d:%02d",
4506 utc_print_time.tm_hour,
4507 utc_print_time.tm_min,
4508 utc_print_time.tm_sec);
4515 cups_printer_get_settings_from_options (GtkPrinter *printer,
4516 GtkPrinterOptionSet *options,
4517 GtkPrintSettings *settings)
4519 struct OptionData data;
4520 const char *print_at, *print_at_time;
4522 data.printer = printer;
4523 data.options = options;
4524 data.settings = settings;
4525 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4527 if (data.ppd_file != NULL)
4529 GtkPrinterOption *cover_before, *cover_after;
4531 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
4533 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
4534 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
4535 if (cover_before && cover_after)
4537 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
4538 gtk_print_settings_set (settings, "cups-job-sheets", value);
4542 print_at = gtk_print_settings_get (settings, "print-at");
4543 print_at_time = gtk_print_settings_get (settings, "print-at-time");
4545 if (strcmp (print_at, "at") == 0)
4547 gchar *utc_time = NULL;
4549 utc_time = localtime_to_utctime (print_at_time);
4551 if (utc_time != NULL)
4553 gtk_print_settings_set (settings, "cups-job-hold-until", utc_time);
4557 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
4559 else if (strcmp (print_at, "on-hold") == 0)
4560 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
4565 cups_printer_prepare_for_print (GtkPrinter *printer,
4566 GtkPrintJob *print_job,
4567 GtkPrintSettings *settings,
4568 GtkPageSetup *page_setup)
4570 GtkPrintPages pages;
4571 GtkPageRange *ranges;
4573 GtkPageSet page_set;
4574 GtkPaperSize *paper_size;
4575 const char *ppd_paper_name;
4578 pages = gtk_print_settings_get_print_pages (settings);
4579 gtk_print_job_set_pages (print_job, pages);
4581 if (pages == GTK_PRINT_PAGES_RANGES)
4582 ranges = gtk_print_settings_get_page_ranges (settings, &n_ranges);
4589 gtk_print_job_set_page_ranges (print_job, ranges, n_ranges);
4590 if (gtk_print_settings_get_collate (settings))
4591 gtk_print_settings_set (settings, "cups-Collate", "True");
4592 gtk_print_job_set_collate (print_job, FALSE);
4594 if (gtk_print_settings_get_reverse (settings))
4595 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
4596 gtk_print_job_set_reverse (print_job, FALSE);
4598 if (gtk_print_settings_get_n_copies (settings) > 1)
4599 gtk_print_settings_set_int (settings, "cups-copies",
4600 gtk_print_settings_get_n_copies (settings));
4601 gtk_print_job_set_num_copies (print_job, 1);
4603 scale = gtk_print_settings_get_scale (settings);
4605 gtk_print_job_set_scale (print_job, scale / 100.0);
4607 page_set = gtk_print_settings_get_page_set (settings);
4608 if (page_set == GTK_PAGE_SET_EVEN)
4609 gtk_print_settings_set (settings, "cups-page-set", "even");
4610 else if (page_set == GTK_PAGE_SET_ODD)
4611 gtk_print_settings_set (settings, "cups-page-set", "odd");
4612 gtk_print_job_set_page_set (print_job, GTK_PAGE_SET_ALL);
4614 paper_size = gtk_page_setup_get_paper_size (page_setup);
4615 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
4616 if (ppd_paper_name != NULL)
4617 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
4620 char width[G_ASCII_DTOSTR_BUF_SIZE];
4621 char height[G_ASCII_DTOSTR_BUF_SIZE];
4624 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
4625 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
4626 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
4627 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
4628 g_free (custom_name);
4631 if (gtk_print_settings_get_number_up (settings) > 1)
4633 GtkNumberUpLayout layout = gtk_print_settings_get_number_up_layout (settings);
4634 GEnumClass *enum_class;
4635 GEnumValue *enum_value;
4637 switch (gtk_page_setup_get_orientation (page_setup))
4639 case GTK_PAGE_ORIENTATION_PORTRAIT:
4641 case GTK_PAGE_ORIENTATION_LANDSCAPE:
4643 layout = layout + 2 + 4 * (1 - layout / 2);
4645 layout = layout - 3 - 2 * (layout % 2);
4647 case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
4648 layout = (layout + 3 - 2 * (layout % 2)) % 4 + 4 * (layout / 4);
4650 case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
4652 layout = layout + 5 - 2 * (layout % 2);
4654 layout = layout - 6 + 4 * (1 - (layout - 4) / 2);
4658 enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
4659 enum_value = g_enum_get_value (enum_class, layout);
4660 gtk_print_settings_set (settings, "cups-number-up-layout", enum_value->value_nick);
4661 g_type_class_unref (enum_class);
4664 gtk_print_job_set_rotate (print_job, TRUE);
4667 static GtkPageSetup *
4668 create_page_setup (ppd_file_t *ppd_file,
4672 GtkPageSetup *page_setup;
4673 GtkPaperSize *paper_size;
4674 ppd_option_t *option;
4675 ppd_choice_t *choice;
4677 display_name = NULL;
4678 option = ppdFindOption (ppd_file, "PageSize");
4681 choice = ppdFindChoice (option, size->name);
4683 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
4686 if (display_name == NULL)
4687 display_name = g_strdup (size->name);
4689 page_setup = gtk_page_setup_new ();
4690 paper_size = gtk_paper_size_new_from_ppd (size->name,
4694 gtk_page_setup_set_paper_size (page_setup, paper_size);
4695 gtk_paper_size_free (paper_size);
4697 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
4698 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
4699 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
4700 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
4702 g_free (display_name);
4708 cups_printer_list_papers (GtkPrinter *printer)
4710 ppd_file_t *ppd_file;
4712 GtkPageSetup *page_setup;
4716 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4717 if (ppd_file == NULL)
4722 for (i = 0; i < ppd_file->num_sizes; i++)
4724 size = &ppd_file->sizes[i];
4726 page_setup = create_page_setup (ppd_file, size);
4728 l = g_list_prepend (l, page_setup);
4731 return g_list_reverse (l);
4734 static GtkPageSetup *
4735 cups_printer_get_default_page_size (GtkPrinter *printer)
4737 ppd_file_t *ppd_file;
4739 ppd_option_t *option;
4742 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4743 if (ppd_file == NULL)
4746 option = ppdFindOption (ppd_file, "PageSize");
4750 size = ppdPageSize (ppd_file, option->defchoice);
4754 return create_page_setup (ppd_file, size);
4758 cups_printer_get_hard_margins (GtkPrinter *printer,
4764 ppd_file_t *ppd_file;
4766 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4767 if (ppd_file == NULL)
4770 *left = ppd_file->custom_margins[0];
4771 *bottom = ppd_file->custom_margins[1];
4772 *right = ppd_file->custom_margins[2];
4773 *top = ppd_file->custom_margins[3];
4778 static GtkPrintCapabilities
4779 cups_printer_get_capabilities (GtkPrinter *printer)
4782 GTK_PRINT_CAPABILITY_COPIES |
4783 GTK_PRINT_CAPABILITY_COLLATE |
4784 GTK_PRINT_CAPABILITY_REVERSE |
4785 GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT |
4786 GTK_PRINT_CAPABILITY_NUMBER_UP;