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. */
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 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
607 httpAssembleURIf (HTTP_URI_CODING_ALL,
608 printer_absolute_uri,
609 sizeof (printer_absolute_uri),
615 gtk_printer_get_name (gtk_print_job_get_printer (job)));
617 g_snprintf (printer_absolute_uri,
618 sizeof (printer_absolute_uri),
619 "ipp://localhost:%d/printers/%s",
621 gtk_printer_get_name (gtk_print_job_get_printer (job)));
624 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
625 IPP_TAG_URI, "printer-uri",
626 NULL, printer_absolute_uri);
628 title = gtk_print_job_get_title (job);
630 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
631 IPP_TAG_NAME, "job-name",
634 options_data = g_new0 (CupsOptionsData, 1);
635 options_data->request = request;
636 options_data->printer = cups_printer;
637 gtk_print_settings_foreach (settings, add_cups_options, options_data);
638 g_free (options_data);
640 ps = g_new0 (CupsPrintStreamData, 1);
641 ps->callback = callback;
642 ps->user_data = user_data;
643 ps->dnotify = dnotify;
644 ps->job = g_object_ref (job);
646 request->need_auth_info = cups_printer->auth_info_required != NULL;
647 request->auth_info_required = g_strdupv (cups_printer->auth_info_required);
649 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
651 (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
653 (GDestroyNotify)cups_free_print_stream_data);
656 void overwrite_and_free (gpointer data)
658 gchar *password = (gchar *) data;
660 if (password != NULL)
662 memset (password, 0, strlen (password));
668 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
670 backend_cups->list_printers_poll = FALSE;
671 backend_cups->got_default_printer = FALSE;
672 backend_cups->list_printers_pending = FALSE;
673 backend_cups->list_printers_attempts = 0;
674 backend_cups->reading_ppds = 0;
676 backend_cups->requests = NULL;
677 backend_cups->auth = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, overwrite_and_free);
678 backend_cups->authentication_lock = FALSE;
680 backend_cups->covers = NULL;
681 backend_cups->number_of_covers = 0;
683 backend_cups->default_printer_poll = 0;
684 backend_cups->cups_connection_test = NULL;
686 backend_cups->username = NULL;
689 backend_cups->colord_client = cd_client_new ();
692 cups_get_local_default_printer (backend_cups);
696 gtk_print_backend_cups_finalize (GObject *object)
698 GtkPrintBackendCups *backend_cups;
701 g_print ("CUPS Backend: finalizing CUPS backend module\n"));
703 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
705 g_free (backend_cups->default_printer);
706 backend_cups->default_printer = NULL;
708 g_strfreev (backend_cups->covers);
709 backend_cups->number_of_covers = 0;
711 gtk_cups_connection_test_free (backend_cups->cups_connection_test);
712 backend_cups->cups_connection_test = NULL;
714 g_hash_table_destroy (backend_cups->auth);
716 g_free (backend_cups->username);
719 g_object_unref (backend_cups->colord_client);
722 backend_parent_class->finalize (object);
726 gtk_print_backend_cups_dispose (GObject *object)
728 GtkPrintBackendCups *backend_cups;
731 g_print ("CUPS Backend: %s\n", G_STRFUNC));
733 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
735 if (backend_cups->list_printers_poll > 0)
736 g_source_remove (backend_cups->list_printers_poll);
737 backend_cups->list_printers_poll = 0;
738 backend_cups->list_printers_attempts = 0;
740 if (backend_cups->default_printer_poll > 0)
741 g_source_remove (backend_cups->default_printer_poll);
742 backend_cups->default_printer_poll = 0;
744 backend_parent_class->dispose (object);
748 is_address_local (const gchar *address)
750 if (address[0] == '/' ||
751 strcmp (address, "127.0.0.1") == 0 ||
752 strcmp (address, "[::1]") == 0)
759 gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
760 gchar **auth_info_required,
763 GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
765 char dispatch_hostname[HTTP_MAX_URI];
766 gchar *username = NULL;
767 gchar *hostname = NULL;
768 gchar *password = NULL;
772 length = g_strv_length (auth_info_required);
774 if (auth_info != NULL)
775 for (i = 0; i < length; i++)
777 if (g_strcmp0 (auth_info_required[i], "username") == 0)
778 username = g_strdup (auth_info[i]);
779 else if (g_strcmp0 (auth_info_required[i], "hostname") == 0)
780 hostname = g_strdup (auth_info[i]);
781 else if (g_strcmp0 (auth_info_required[i], "password") == 0)
782 password = g_strdup (auth_info[i]);
785 if (hostname != NULL && username != NULL && password != NULL)
787 gchar *key = g_strconcat (username, "@", hostname, NULL);
788 g_hash_table_insert (cups_backend->auth, key, g_strdup (password));
790 g_print ("CUPS backend: storing password for %s\n", key));
793 g_free (cups_backend->username);
794 cups_backend->username = g_strdup (username);
797 for (l = cups_backend->requests; l; l = l->next)
799 GtkPrintCupsDispatchWatch *dispatch = l->data;
801 httpGetHostname (dispatch->request->http, dispatch_hostname, sizeof (dispatch_hostname));
802 if (is_address_local (dispatch_hostname))
803 strcpy (dispatch_hostname, "localhost");
805 if (dispatch->request->need_auth_info)
807 if (auth_info != NULL)
809 dispatch->request->auth_info = g_new0 (gchar *, length + 1);
810 for (i = 0; i < length; i++)
811 dispatch->request->auth_info[i] = g_strdup (auth_info[i]);
813 dispatch->backend->authentication_lock = FALSE;
814 dispatch->request->need_auth_info = FALSE;
816 else if (dispatch->request->password_state == GTK_CUPS_PASSWORD_REQUESTED || auth_info == NULL)
818 overwrite_and_free (dispatch->request->password);
819 dispatch->request->password = g_strdup (password);
820 g_free (dispatch->request->username);
821 dispatch->request->username = g_strdup (username);
822 dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
823 dispatch->backend->authentication_lock = FALSE;
829 request_password (gpointer data)
831 GtkPrintCupsDispatchWatch *dispatch = data;
832 const gchar *username;
834 gchar *prompt = NULL;
836 char hostname[HTTP_MAX_URI];
837 gchar **auth_info_required;
838 gchar **auth_info_default;
839 gchar **auth_info_display;
840 gboolean *auth_info_visible;
844 if (dispatch->backend->authentication_lock)
845 return G_SOURCE_REMOVE;
847 httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
848 if (is_address_local (hostname))
849 strcpy (hostname, "localhost");
851 if (dispatch->backend->username != NULL)
852 username = dispatch->backend->username;
854 username = cupsUser ();
856 auth_info_required = g_new0 (gchar*, length + 1);
857 auth_info_required[0] = g_strdup ("hostname");
858 auth_info_required[1] = g_strdup ("username");
859 auth_info_required[2] = g_strdup ("password");
861 auth_info_default = g_new0 (gchar*, length + 1);
862 auth_info_default[0] = g_strdup (hostname);
863 auth_info_default[1] = g_strdup (username);
865 auth_info_display = g_new0 (gchar*, length + 1);
866 auth_info_display[1] = g_strdup (_("Username:"));
867 auth_info_display[2] = g_strdup (_("Password:"));
869 auth_info_visible = g_new0 (gboolean, length + 1);
870 auth_info_visible[1] = TRUE;
872 key = g_strconcat (username, "@", hostname, NULL);
873 password = g_hash_table_lookup (dispatch->backend->auth, key);
875 if (password && dispatch->request->password_state != GTK_CUPS_PASSWORD_NOT_VALID)
878 g_print ("CUPS backend: using stored password for %s\n", key));
880 overwrite_and_free (dispatch->request->password);
881 dispatch->request->password = g_strdup (password);
882 g_free (dispatch->request->username);
883 dispatch->request->username = g_strdup (username);
884 dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
888 const char *job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
889 const char *printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
890 char *printer_name = NULL;
892 if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
893 printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
895 if (dispatch->request->password_state == GTK_CUPS_PASSWORD_NOT_VALID)
896 g_hash_table_remove (dispatch->backend->auth, key);
898 dispatch->request->password_state = GTK_CUPS_PASSWORD_REQUESTED;
900 dispatch->backend->authentication_lock = TRUE;
902 switch (ippGetOperation (dispatch->request->ipp_request))
905 if (job_title != NULL && printer_name != NULL)
906 prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
908 prompt = g_strdup_printf ( _("Authentication is required to print a document on %s"), hostname);
910 case IPP_GET_JOB_ATTRIBUTES:
911 if (job_title != NULL)
912 prompt = g_strdup_printf ( _("Authentication is required to get attributes of job '%s'"), job_title);
914 prompt = g_strdup ( _("Authentication is required to get attributes of a job"));
916 case IPP_GET_PRINTER_ATTRIBUTES:
917 if (printer_name != NULL)
918 prompt = g_strdup_printf ( _("Authentication is required to get attributes of printer %s"), printer_name);
920 prompt = g_strdup ( _("Authentication is required to get attributes of a printer"));
922 case CUPS_GET_DEFAULT:
923 prompt = g_strdup_printf ( _("Authentication is required to get default printer of %s"), hostname);
925 case CUPS_GET_PRINTERS:
926 prompt = g_strdup_printf ( _("Authentication is required to get printers from %s"), hostname);
929 /* work around gcc warning about 0 not being a value for this enum */
930 if (ippGetOperation (dispatch->request->ipp_request) == 0)
931 prompt = g_strdup_printf ( _("Authentication is required to get a file from %s"), hostname);
933 prompt = g_strdup_printf ( _("Authentication is required on %s"), hostname);
937 g_free (printer_name);
939 g_signal_emit_by_name (dispatch->backend, "request-password",
940 auth_info_required, auth_info_default, auth_info_display, auth_info_visible, prompt);
945 for (i = 0; i < length; i++)
947 g_free (auth_info_required[i]);
948 g_free (auth_info_default[i]);
949 g_free (auth_info_display[i]);
952 g_free (auth_info_required);
953 g_free (auth_info_default);
954 g_free (auth_info_display);
955 g_free (auth_info_visible);
958 return G_SOURCE_REMOVE;
962 cups_dispatch_add_poll (GSource *source)
964 GtkPrintCupsDispatchWatch *dispatch;
965 GtkCupsPollState poll_state;
967 dispatch = (GtkPrintCupsDispatchWatch *) source;
969 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
971 /* Remove the old source if the poll state changed. */
972 if (poll_state != dispatch->poll_state && dispatch->data_poll != NULL)
974 g_source_remove_poll (source, dispatch->data_poll);
975 g_free (dispatch->data_poll);
976 dispatch->data_poll = NULL;
979 if (dispatch->request->http != NULL)
981 if (dispatch->data_poll == NULL)
983 dispatch->data_poll = g_new0 (GPollFD, 1);
984 dispatch->poll_state = poll_state;
986 if (poll_state == GTK_CUPS_HTTP_READ)
987 dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
988 else if (poll_state == GTK_CUPS_HTTP_WRITE)
989 dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
991 dispatch->data_poll->events = 0;
993 dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
994 g_source_add_poll (source, dispatch->data_poll);
1000 check_auth_info (gpointer user_data)
1002 GtkPrintCupsDispatchWatch *dispatch;
1003 dispatch = (GtkPrintCupsDispatchWatch *) user_data;
1005 if (!dispatch->request->need_auth_info)
1007 if (dispatch->request->auth_info == NULL)
1009 dispatch->callback (GTK_PRINT_BACKEND (dispatch->backend),
1010 gtk_cups_request_get_result (dispatch->request),
1011 dispatch->callback_data);
1012 g_source_destroy ((GSource *) dispatch);
1019 length = g_strv_length (dispatch->request->auth_info_required);
1021 gtk_cups_request_ipp_add_strings (dispatch->request,
1027 (const char * const *) dispatch->request->auth_info);
1029 g_source_attach ((GSource *) dispatch, NULL);
1030 g_source_unref ((GSource *) dispatch);
1032 for (i = 0; i < length; i++)
1033 overwrite_and_free (dispatch->request->auth_info[i]);
1034 g_free (dispatch->request->auth_info);
1035 dispatch->request->auth_info = NULL;
1038 return G_SOURCE_REMOVE;
1041 return G_SOURCE_CONTINUE;
1045 request_auth_info (gpointer user_data)
1047 GtkPrintCupsDispatchWatch *dispatch;
1048 const char *job_title;
1049 const char *printer_uri;
1050 gchar *prompt = NULL;
1051 char *printer_name = NULL;
1054 gboolean *auth_info_visible = NULL;
1055 gchar **auth_info_default = NULL;
1056 gchar **auth_info_display = NULL;
1058 dispatch = (GtkPrintCupsDispatchWatch *) user_data;
1060 if (dispatch->backend->authentication_lock)
1063 job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
1064 printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
1065 length = g_strv_length (dispatch->request->auth_info_required);
1067 auth_info_visible = g_new0 (gboolean, length);
1068 auth_info_default = g_new0 (gchar *, length + 1);
1069 auth_info_display = g_new0 (gchar *, length + 1);
1071 for (i = 0; i < length; i++)
1073 if (g_strcmp0 (dispatch->request->auth_info_required[i], "domain") == 0)
1075 auth_info_display[i] = g_strdup (_("Domain:"));
1076 auth_info_default[i] = g_strdup ("WORKGROUP");
1077 auth_info_visible[i] = TRUE;
1079 else if (g_strcmp0 (dispatch->request->auth_info_required[i], "username") == 0)
1081 auth_info_display[i] = g_strdup (_("Username:"));
1082 if (dispatch->backend->username != NULL)
1083 auth_info_default[i] = g_strdup (dispatch->backend->username);
1085 auth_info_default[i] = g_strdup (cupsUser ());
1086 auth_info_visible[i] = TRUE;
1088 else if (g_strcmp0 (dispatch->request->auth_info_required[i], "password") == 0)
1090 auth_info_display[i] = g_strdup (_("Password:"));
1091 auth_info_visible[i] = FALSE;
1095 if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
1096 printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
1098 dispatch->backend->authentication_lock = TRUE;
1100 if (job_title != NULL)
1102 if (printer_name != NULL)
1103 prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
1105 prompt = g_strdup_printf ( _("Authentication is required to print document '%s'"), job_title);
1109 if (printer_name != NULL)
1110 prompt = g_strdup_printf ( _("Authentication is required to print this document on printer %s"), printer_name);
1112 prompt = g_strdup ( _("Authentication is required to print this document"));
1115 g_signal_emit_by_name (dispatch->backend, "request-password",
1116 dispatch->request->auth_info_required,
1122 for (i = 0; i < length; i++)
1124 g_free (auth_info_default[i]);
1125 g_free (auth_info_display[i]);
1128 g_free (auth_info_default);
1129 g_free (auth_info_display);
1130 g_free (printer_name);
1133 g_idle_add (check_auth_info, user_data);
1139 cups_dispatch_watch_check (GSource *source)
1141 GtkPrintCupsDispatchWatch *dispatch;
1142 GtkCupsPollState poll_state;
1146 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1148 dispatch = (GtkPrintCupsDispatchWatch *) source;
1150 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
1152 if (poll_state != GTK_CUPS_HTTP_IDLE && !dispatch->request->need_password)
1153 if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
1156 result = gtk_cups_request_read_write (dispatch->request, FALSE);
1157 if (result && dispatch->data_poll != NULL)
1159 g_source_remove_poll (source, dispatch->data_poll);
1160 g_free (dispatch->data_poll);
1161 dispatch->data_poll = NULL;
1164 if (dispatch->request->need_password && dispatch->request->password_state != GTK_CUPS_PASSWORD_REQUESTED)
1166 dispatch->request->need_password = FALSE;
1167 g_idle_add (request_password, dispatch);
1175 cups_dispatch_watch_prepare (GSource *source,
1178 GtkPrintCupsDispatchWatch *dispatch;
1181 dispatch = (GtkPrintCupsDispatchWatch *) source;
1184 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1188 result = gtk_cups_request_read_write (dispatch->request, TRUE);
1190 cups_dispatch_add_poll (source);
1196 cups_dispatch_watch_dispatch (GSource *source,
1197 GSourceFunc callback,
1200 GtkPrintCupsDispatchWatch *dispatch;
1201 GtkPrintCupsResponseCallbackFunc ep_callback;
1202 GtkCupsResult *result;
1204 g_assert (callback != NULL);
1206 ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
1208 dispatch = (GtkPrintCupsDispatchWatch *) source;
1210 result = gtk_cups_request_get_result (dispatch->request);
1213 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1215 if (gtk_cups_result_is_error (result))
1218 g_print("Error result: %s (type %i, status %i, code %i)\n",
1219 gtk_cups_result_get_error_string (result),
1220 gtk_cups_result_get_error_type (result),
1221 gtk_cups_result_get_error_status (result),
1222 gtk_cups_result_get_error_code (result)));
1225 ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
1231 cups_dispatch_watch_finalize (GSource *source)
1233 GtkPrintCupsDispatchWatch *dispatch;
1234 GtkCupsResult *result;
1237 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1239 dispatch = (GtkPrintCupsDispatchWatch *) source;
1241 result = gtk_cups_request_get_result (dispatch->request);
1242 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH)
1244 const gchar *username;
1245 gchar hostname[HTTP_MAX_URI];
1248 httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
1249 if (is_address_local (hostname))
1250 strcpy (hostname, "localhost");
1252 if (dispatch->backend->username != NULL)
1253 username = dispatch->backend->username;
1255 username = cupsUser ();
1257 key = g_strconcat (username, "@", hostname, NULL);
1259 g_print ("CUPS backend: removing stored password for %s\n", key));
1260 g_hash_table_remove (dispatch->backend->auth, key);
1263 if (dispatch->backend)
1264 dispatch->backend->authentication_lock = FALSE;
1267 gtk_cups_request_free (dispatch->request);
1269 if (dispatch->backend)
1271 /* We need to unref this at idle time, because it might be the
1272 * last reference to this module causing the code to be
1273 * unloaded (including this particular function!)
1274 * Update: Doing this at idle caused a deadlock taking the
1275 * mainloop context lock while being in a GSource callout for
1276 * multithreaded apps. So, for now we just disable unloading
1277 * of print backends. See _gtk_print_backend_create for the
1281 dispatch->backend->requests = g_list_remove (dispatch->backend->requests, dispatch);
1284 g_object_unref (dispatch->backend);
1285 dispatch->backend = NULL;
1288 if (dispatch->data_poll)
1290 g_source_remove_poll (source, dispatch->data_poll);
1291 g_free (dispatch->data_poll);
1292 dispatch->data_poll = NULL;
1296 static GSourceFuncs _cups_dispatch_watch_funcs = {
1297 cups_dispatch_watch_prepare,
1298 cups_dispatch_watch_check,
1299 cups_dispatch_watch_dispatch,
1300 cups_dispatch_watch_finalize
1305 cups_request_execute (GtkPrintBackendCups *print_backend,
1306 GtkCupsRequest *request,
1307 GtkPrintCupsResponseCallbackFunc callback,
1309 GDestroyNotify notify)
1311 GtkPrintCupsDispatchWatch *dispatch;
1313 dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
1314 sizeof (GtkPrintCupsDispatchWatch));
1315 g_source_set_name (&dispatch->source, "GTK+ CUPS backend");
1318 g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
1320 dispatch->request = request;
1321 dispatch->backend = g_object_ref (print_backend);
1322 dispatch->poll_state = GTK_CUPS_HTTP_IDLE;
1323 dispatch->data_poll = NULL;
1324 dispatch->callback = NULL;
1325 dispatch->callback_data = NULL;
1327 print_backend->requests = g_list_prepend (print_backend->requests, dispatch);
1329 g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
1331 if (request->need_auth_info)
1333 dispatch->callback = callback;
1334 dispatch->callback_data = user_data;
1335 request_auth_info (dispatch);
1339 g_source_attach ((GSource *) dispatch, NULL);
1340 g_source_unref ((GSource *) dispatch);
1346 cups_request_printer_info_cb (GtkPrintBackendCups *backend,
1347 GtkCupsResult *result,
1350 ipp_attribute_t *attr;
1352 gchar *printer_name;
1353 GtkPrinterCups *cups_printer;
1354 GtkPrinter *printer;
1359 gboolean status_changed;
1361 g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
1363 printer_name = (gchar *)user_data;
1364 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
1368 g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
1373 g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
1377 cups_printer = GTK_PRINTER_CUPS (printer);
1379 if (gtk_cups_result_is_error (result))
1381 if (gtk_printer_is_new (printer))
1383 gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
1388 return; /* TODO: mark as inactive printer */
1391 response = gtk_cups_result_get_response (result);
1393 /* TODO: determine printer type and use correct icon */
1394 gtk_printer_set_icon_name (printer, "printer");
1400 for (attr = response->attrs; attr != NULL; attr = attr->next)
1405 _CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
1406 _CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
1407 _CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
1408 _CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
1409 _CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
1412 status_changed = gtk_printer_set_job_count (printer, job_count);
1414 status_changed |= gtk_printer_set_location (printer, loc);
1415 status_changed |= gtk_printer_set_description (printer, desc);
1416 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1419 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1420 "printer-status-changed", printer);
1424 cups_request_printer_info (GtkPrintBackendCups *print_backend,
1425 const gchar *printer_name)
1427 GtkCupsRequest *request;
1429 static const char * const pattrs[] = /* Attributes we're interested in */
1433 "printer-state-message",
1436 "job-sheets-supported",
1437 "job-sheets-default"
1440 request = gtk_cups_request_new_with_username (NULL,
1442 IPP_GET_PRINTER_ATTRIBUTES,
1446 print_backend->username);
1448 printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
1450 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
1451 "printer-uri", NULL, printer_uri);
1454 g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
1456 g_free (printer_uri);
1458 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1459 "requested-attributes", G_N_ELEMENTS (pattrs),
1462 cups_request_execute (print_backend,
1464 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
1465 g_strdup (printer_name),
1466 (GDestroyNotify) g_free);
1471 GtkPrintBackendCups *print_backend;
1478 job_object_died (gpointer user_data,
1479 GObject *where_the_object_was)
1481 CupsJobPollData *data = user_data;
1486 cups_job_poll_data_free (CupsJobPollData *data)
1489 g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
1495 cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
1496 GtkCupsResult *result,
1499 CupsJobPollData *data = user_data;
1500 ipp_attribute_t *attr;
1505 GDK_THREADS_ENTER ();
1507 if (data->job == NULL)
1509 cups_job_poll_data_free (data);
1515 response = gtk_cups_result_get_response (result);
1519 #ifdef HAVE_CUPS_API_1_6
1520 attr = ippFindAttribute (response, "job-state", IPP_TAG_INTEGER);
1521 state = ippGetInteger (attr, 0);
1523 for (attr = response->attrs; attr != NULL; attr = attr->next)
1528 _CUPS_MAP_ATTR_INT (attr, state, "job-state");
1535 case IPP_JOB_PENDING:
1537 case IPP_JOB_STOPPED:
1538 gtk_print_job_set_status (data->job,
1539 GTK_PRINT_STATUS_PENDING);
1541 case IPP_JOB_PROCESSING:
1542 gtk_print_job_set_status (data->job,
1543 GTK_PRINT_STATUS_PRINTING);
1546 case IPP_JOB_CANCELLED:
1547 case IPP_JOB_ABORTED:
1548 gtk_print_job_set_status (data->job,
1549 GTK_PRINT_STATUS_FINISHED_ABORTED);
1553 case IPP_JOB_COMPLETED:
1554 gtk_print_job_set_status (data->job,
1555 GTK_PRINT_STATUS_FINISHED);
1560 if (!done && data->job != NULL)
1564 if (data->counter < 5)
1566 else if (data->counter < 10)
1571 g_timeout_add (timeout, cups_job_info_poll_timeout, data);
1574 cups_job_poll_data_free (data);
1577 GDK_THREADS_LEAVE ();
1581 cups_request_job_info (CupsJobPollData *data)
1583 GtkCupsRequest *request;
1586 request = gtk_cups_request_new_with_username (NULL,
1588 IPP_GET_JOB_ATTRIBUTES,
1592 data->print_backend->username);
1594 job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
1595 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
1596 "job-uri", NULL, job_uri);
1599 cups_request_execute (data->print_backend,
1601 (GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
1607 cups_job_info_poll_timeout (gpointer user_data)
1609 CupsJobPollData *data = user_data;
1611 if (data->job == NULL)
1612 cups_job_poll_data_free (data);
1614 cups_request_job_info (data);
1616 return G_SOURCE_REMOVE;
1620 cups_begin_polling_info (GtkPrintBackendCups *print_backend,
1624 CupsJobPollData *data;
1626 data = g_new0 (CupsJobPollData, 1);
1628 data->print_backend = print_backend;
1630 data->job_id = job_id;
1633 g_object_weak_ref (G_OBJECT (job), job_object_died, data);
1635 cups_request_job_info (data);
1639 mark_printer_inactive (GtkPrinter *printer,
1640 GtkPrintBackend *backend)
1642 gtk_printer_set_is_active (printer, FALSE);
1643 g_signal_emit_by_name (backend, "printer-removed", printer);
1647 find_printer (GtkPrinter *printer,
1648 const gchar *find_name)
1650 const gchar *printer_name;
1652 printer_name = gtk_printer_get_name (printer);
1653 return g_ascii_strcasecmp (printer_name, find_name);
1655 /* Printer messages we're interested in */
1656 static const char * const printer_messages[] =
1662 "marker-supply-low",
1663 "marker-supply-empty",
1671 /* Our translatable versions of the printer messages */
1672 static const char * printer_strings[] =
1674 N_("Printer '%s' is low on toner."),
1675 N_("Printer '%s' has no toner left."),
1676 /* Translators: "Developer" like on photo development context */
1677 N_("Printer '%s' is low on developer."),
1678 /* Translators: "Developer" like on photo development context */
1679 N_("Printer '%s' is out of developer."),
1680 /* Translators: "marker" is one color bin of the printer */
1681 N_("Printer '%s' is low on at least one marker supply."),
1682 /* Translators: "marker" is one color bin of the printer */
1683 N_("Printer '%s' is out of at least one marker supply."),
1684 N_("The cover is open on printer '%s'."),
1685 N_("The door is open on printer '%s'."),
1686 N_("Printer '%s' is low on paper."),
1687 N_("Printer '%s' is out of paper."),
1688 N_("Printer '%s' is currently offline."),
1689 N_("There is a problem on printer '%s'.")
1694 GTK_PRINTER_STATE_LEVEL_NONE = 0,
1695 GTK_PRINTER_STATE_LEVEL_INFO = 1,
1696 GTK_PRINTER_STATE_LEVEL_WARNING = 2,
1697 GTK_PRINTER_STATE_LEVEL_ERROR = 3
1698 } PrinterStateLevel;
1702 const gchar *printer_name;
1703 const gchar *printer_uri;
1704 const gchar *member_uris;
1705 const gchar *location;
1706 const gchar *description;
1707 const gchar *state_msg;
1708 const gchar *reason_msg;
1709 PrinterStateLevel reason_level;
1713 gboolean is_accepting_jobs;
1714 const gchar *default_cover_before;
1715 const gchar *default_cover_after;
1716 gboolean default_printer;
1717 gboolean got_printer_type;
1718 gboolean remote_printer;
1719 gchar **auth_info_required;
1720 gint default_number_up;
1724 cups_printer_handle_attribute (GtkPrintBackendCups *cups_backend,
1725 ipp_attribute_t *attr,
1726 PrinterSetupInfo *info)
1729 if (strcmp (ippGetName (attr), "printer-name") == 0 &&
1730 ippGetValueTag (attr) == IPP_TAG_NAME)
1731 info->printer_name = ippGetString (attr, 0, NULL);
1732 else if (strcmp (ippGetName (attr), "printer-uri-supported") == 0 &&
1733 ippGetValueTag (attr) == IPP_TAG_URI)
1734 info->printer_uri = ippGetString (attr, 0, NULL);
1735 else if (strcmp (ippGetName (attr), "member-uris") == 0 &&
1736 ippGetValueTag (attr) == IPP_TAG_URI)
1737 info->member_uris = ippGetString (attr, 0, NULL);
1738 else if (strcmp (ippGetName (attr), "printer-location") == 0)
1739 info->location = ippGetString (attr, 0, NULL);
1740 else if (strcmp (ippGetName (attr), "printer-info") == 0)
1741 info->description = ippGetString (attr, 0, NULL);
1742 else if (strcmp (ippGetName (attr), "printer-state-message") == 0)
1743 info->state_msg = ippGetString (attr, 0, NULL);
1744 else if (strcmp (ippGetName (attr), "printer-state-reasons") == 0)
1745 /* Store most important reason to reason_msg and set
1746 its importance at printer_state_reason_level */
1748 for (i = 0; i < ippGetCount (attr); i++)
1750 if (strcmp (ippGetString (attr, i, NULL), "none") != 0)
1752 gboolean interested_in = FALSE;
1753 /* Sets is_paused flag for paused printer. */
1754 if (strcmp (ippGetString (attr, i, NULL), "paused") == 0)
1756 info->is_paused = TRUE;
1759 for (j = 0; j < G_N_ELEMENTS (printer_messages); j++)
1760 if (strncmp (ippGetString (attr, i, NULL), printer_messages[j],
1761 strlen (printer_messages[j])) == 0)
1763 interested_in = TRUE;
1769 if (g_str_has_suffix (ippGetString (attr, i, NULL), "-report"))
1771 if (info->reason_level <= GTK_PRINTER_STATE_LEVEL_INFO)
1773 info->reason_msg = ippGetString (attr, i, NULL);
1774 info->reason_level = GTK_PRINTER_STATE_LEVEL_INFO;
1777 else if (g_str_has_suffix (ippGetString (attr, i, NULL), "-warning"))
1779 if (info->reason_level <= GTK_PRINTER_STATE_LEVEL_WARNING)
1781 info->reason_msg = ippGetString (attr, i, NULL);
1782 info->reason_level = GTK_PRINTER_STATE_LEVEL_WARNING;
1785 else /* It is error in the case of no suffix. */
1787 info->reason_msg = ippGetString (attr, i, NULL);
1788 info->reason_level = GTK_PRINTER_STATE_LEVEL_ERROR;
1794 else if (strcmp (ippGetName (attr), "printer-state") == 0)
1795 info->state = ippGetInteger (attr, 0);
1796 else if (strcmp (ippGetName (attr), "queued-job-count") == 0)
1797 info->job_count = ippGetInteger (attr, 0);
1798 else if (strcmp (ippGetName (attr), "printer-is-accepting-jobs") == 0)
1800 if (ippGetBoolean (attr, 0) == 1)
1801 info->is_accepting_jobs = TRUE;
1803 info->is_accepting_jobs = FALSE;
1805 else if (strcmp (ippGetName (attr), "job-sheets-supported") == 0)
1807 if (cups_backend->covers == NULL)
1809 cups_backend->number_of_covers = ippGetCount (attr);
1810 cups_backend->covers = g_new (char *, cups_backend->number_of_covers + 1);
1811 for (i = 0; i < cups_backend->number_of_covers; i++)
1812 cups_backend->covers[i] = g_strdup (ippGetString (attr, i, NULL));
1813 cups_backend->covers[cups_backend->number_of_covers] = NULL;
1816 else if (strcmp (ippGetName (attr), "job-sheets-default") == 0)
1818 if (ippGetCount (attr) == 2)
1820 info->default_cover_before = ippGetString (attr, 0, NULL);
1821 info->default_cover_after = ippGetString (attr, 1, NULL);
1824 else if (strcmp (ippGetName (attr), "printer-type") == 0)
1826 info->got_printer_type = TRUE;
1827 if (ippGetInteger (attr, 0) & 0x00020000)
1828 info->default_printer = TRUE;
1830 info->default_printer = FALSE;
1832 if (ippGetInteger (attr, 0) & 0x00000002)
1833 info->remote_printer = TRUE;
1835 info->remote_printer = FALSE;
1837 else if (strcmp (ippGetName (attr), "auth-info-required") == 0)
1839 if (strcmp (ippGetString (attr, 0, NULL), "none") != 0)
1841 info->auth_info_required = g_new0 (gchar *, ippGetCount (attr) + 1);
1842 for (i = 0; i < ippGetCount (attr); i++)
1843 info->auth_info_required[i] = g_strdup (ippGetString (attr, i, NULL));
1846 else if (strcmp (attr->name, "number-up-default") == 0)
1848 info->default_number_up = attr->values[0].integer;
1853 g_print ("CUPS Backend: Attribute %s ignored", ippGetName (attr)));
1858 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
1859 GtkCupsResult *result,
1862 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
1863 ipp_attribute_t *attr;
1865 gboolean list_has_changed;
1866 GList *removed_printer_checklist;
1867 gchar *remote_default_printer = NULL;
1869 GDK_THREADS_ENTER ();
1871 list_has_changed = FALSE;
1874 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1876 cups_backend->list_printers_pending = FALSE;
1878 if (gtk_cups_result_is_error (result))
1881 g_warning ("CUPS Backend: Error getting printer list: %s %d %d",
1882 gtk_cups_result_get_error_string (result),
1883 gtk_cups_result_get_error_type (result),
1884 gtk_cups_result_get_error_code (result)));
1886 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
1887 gtk_cups_result_get_error_code (result) == 1)
1889 /* Canceled by user, stop popping up more password dialogs */
1890 if (cups_backend->list_printers_poll > 0)
1891 g_source_remove (cups_backend->list_printers_poll);
1892 cups_backend->list_printers_poll = 0;
1893 cups_backend->list_printers_attempts = 0;
1899 /* Gather the names of the printers in the current queue
1900 * so we may check to see if they were removed
1902 removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
1904 response = gtk_cups_result_get_response (result);
1906 for (attr = response->attrs; attr != NULL; attr = attr->next)
1908 GtkPrinter *printer;
1909 gboolean status_changed = FALSE;
1912 gchar *reason_msg_desc = NULL;
1913 gchar *tmp_msg = NULL;
1914 gchar *tmp_msg2 = NULL;
1915 gboolean found = FALSE;
1916 PrinterSetupInfo *info = g_slice_new0 (PrinterSetupInfo);
1917 info->default_number_up = 1;
1919 /* Skip leading attributes until we hit a printer...
1921 while (attr != NULL && ippGetGroupTag (attr) != IPP_TAG_PRINTER)
1927 while (attr != NULL && ippGetGroupTag (attr) == IPP_TAG_PRINTER)
1929 cups_printer_handle_attribute (cups_backend, attr, info);
1933 if (info->printer_name == NULL ||
1934 (info->printer_uri == NULL && info->member_uris == NULL))
1942 if (info->got_printer_type)
1944 if (info->default_printer && !cups_backend->got_default_printer)
1946 if (!info->remote_printer)
1948 cups_backend->got_default_printer = TRUE;
1949 cups_backend->default_printer = g_strdup (info->printer_name);
1953 if (remote_default_printer == NULL)
1954 remote_default_printer = g_strdup (info->printer_name);
1960 if (!cups_backend->got_default_printer)
1961 cups_get_default_printer (cups_backend);
1964 /* remove name from checklist if it was found */
1965 node = g_list_find_custom (removed_printer_checklist,
1967 (GCompareFunc) find_printer);
1968 removed_printer_checklist = g_list_delete_link (removed_printer_checklist,
1971 printer = gtk_print_backend_find_printer (backend, info->printer_name);
1974 GtkPrinterCups *cups_printer;
1975 char uri[HTTP_MAX_URI]; /* Printer URI */
1976 char method[HTTP_MAX_URI]; /* Method/scheme name */
1977 char username[HTTP_MAX_URI]; /* Username:password */
1978 char hostname[HTTP_MAX_URI]; /* Hostname */
1979 char resource[HTTP_MAX_URI]; /* Resource name */
1980 int port; /* Port number */
1981 char *cups_server; /* CUPS server */
1983 list_has_changed = TRUE;
1985 cups_printer = gtk_printer_cups_new (printer_name,
1987 cups_backend->colord_client);
1989 cups_printer = gtk_printer_cups_new (printer_name, backend, NULL);
1992 cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
1994 /* Check to see if we are looking at a class */
1997 cups_printer->printer_uri = g_strdup (member_uris);
1998 /* TODO if member_uris is a class we need to recursivly find a printer */
2000 g_print ("CUPS Backend: Found class with printer %s\n", member_uris));
2004 cups_printer->printer_uri = g_strdup (printer_uri);
2006 g_print ("CUPS Backend: Found printer %s\n", printer_uri));
2009 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
2010 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
2011 method, sizeof (method),
2012 username, sizeof (username),
2013 hostname, sizeof (hostname),
2015 resource, sizeof (resource));
2018 httpSeparate (cups_printer->printer_uri,
2026 if (strncmp (resource, "/printers/", 10) == 0)
2028 cups_printer->ppd_name = g_strdup (resource + 10);
2030 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
2033 gethostname (uri, sizeof (uri));
2034 cups_server = g_strdup (cupsServer());
2036 if (strcasecmp (uri, hostname) == 0)
2037 strcpy (hostname, "localhost");
2039 /* if the cups server is local and listening at a unix domain socket
2040 * then use the socket connection
2042 if ((strstr (hostname, "localhost") != NULL) &&
2043 (cups_server[0] == '/'))
2044 strcpy (hostname, cups_server);
2046 g_free (cups_server);
2048 cups_printer->default_cover_before = g_strdup (default_cover_before);
2049 cups_printer->default_cover_after = g_strdup (default_cover_after);
2051 cups_printer->default_number_up = default_number_up;
2053 cups_printer->hostname = g_strdup (hostname);
2054 cups_printer->port = port;
2056 cups_printer->auth_info_required = g_strdupv (auth_info_required);
2057 g_strfreev (auth_info_required);
2059 printer = GTK_PRINTER (cups_printer);
2061 if (cups_backend->default_printer != NULL &&
2062 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
2063 gtk_printer_set_is_default (printer, TRUE);
2066 gtk_print_backend_add_printer (backend, printer);
2069 g_object_ref (printer);
2071 GTK_PRINTER_CUPS (printer)->remote = info->remote_printer;
2073 gtk_printer_set_is_paused (printer, info->is_paused);
2074 gtk_printer_set_is_accepting_jobs (printer, info->is_accepting_jobs);
2076 if (!gtk_printer_is_active (printer))
2078 gtk_printer_set_is_active (printer, TRUE);
2079 gtk_printer_set_is_new (printer, TRUE);
2080 list_has_changed = TRUE;
2083 if (gtk_printer_is_new (printer))
2085 g_signal_emit_by_name (backend, "printer-added", printer);
2087 gtk_printer_set_is_new (printer, FALSE);
2091 /* Getting printer info with separate requests overwhelms cups
2092 * when the printer list has more than a handful of printers.
2094 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
2097 GTK_PRINTER_CUPS (printer)->state = info->state;
2098 status_changed = gtk_printer_set_job_count (printer, info->job_count);
2099 status_changed |= gtk_printer_set_location (printer, info->location);
2100 status_changed |= gtk_printer_set_description (printer,
2103 if (info->state_msg != NULL && strlen (info->state_msg) == 0)
2105 if (info->is_paused && !info->is_accepting_jobs)
2106 /* Translators: this is a printer status. */
2107 tmp_msg2 = g_strdup ( N_("Paused ; Rejecting Jobs"));
2108 if (info->is_paused && info->is_accepting_jobs)
2109 /* Translators: this is a printer status. */
2110 tmp_msg2 = g_strdup ( N_("Paused"));
2111 if (!info->is_paused && !info->is_accepting_jobs)
2112 /* Translators: this is a printer status. */
2113 tmp_msg2 = g_strdup ( N_("Rejecting Jobs"));
2115 if (tmp_msg2 != NULL)
2116 info->state_msg = tmp_msg2;
2119 /* Set description of the reason and combine it with printer-state-message. */
2120 if ( (info->reason_msg != NULL))
2122 for (i = 0; i < G_N_ELEMENTS (printer_messages); i++)
2124 if (strncmp (info->reason_msg, printer_messages[i],
2125 strlen (printer_messages[i])) == 0)
2127 reason_msg_desc = g_strdup_printf (printer_strings[i],
2128 info->printer_name);
2135 info->reason_level = GTK_PRINTER_STATE_LEVEL_NONE;
2137 if (info->reason_level >= GTK_PRINTER_STATE_LEVEL_WARNING)
2139 if (strlen (info->state_msg) == 0)
2140 info->state_msg = reason_msg_desc;
2143 tmp_msg = g_strjoin (" ; ", info->state_msg, reason_msg_desc, NULL);
2144 info->state_msg = tmp_msg;
2149 status_changed |= gtk_printer_set_state_message (printer, info->state_msg);
2150 status_changed |= gtk_printer_set_is_accepting_jobs (printer, info->is_accepting_jobs);
2152 if (tmp_msg != NULL)
2155 if (tmp_msg2 != NULL)
2158 if (reason_msg_desc != NULL)
2159 g_free (reason_msg_desc);
2161 /* Set printer icon according to importance
2162 (none, report, warning, error - report is omitted). */
2163 if (info->reason_level == GTK_PRINTER_STATE_LEVEL_ERROR)
2164 gtk_printer_set_icon_name (printer, "printer-error");
2165 else if (info->reason_level == GTK_PRINTER_STATE_LEVEL_WARNING)
2166 gtk_printer_set_icon_name (printer, "printer-warning");
2167 else if (gtk_printer_is_paused (printer))
2168 gtk_printer_set_icon_name (printer, "printer-paused");
2170 gtk_printer_set_icon_name (printer, "printer");
2173 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
2174 "printer-status-changed", printer);
2176 /* The ref is held by GtkPrintBackend, in add_printer() */
2177 g_object_unref (printer);
2178 g_slice_free (PrinterSetupInfo, info);
2184 /* look at the removed printers checklist and mark any printer
2185 as inactive if it is in the list, emitting a printer_removed signal */
2186 if (removed_printer_checklist != NULL)
2188 g_list_free_full (removed_printer_checklist, (GDestroyNotify) mark_printer_inactive);
2189 list_has_changed = TRUE;
2193 if (list_has_changed)
2194 g_signal_emit_by_name (backend, "printer-list-changed");
2196 gtk_print_backend_set_list_done (backend);
2198 if (!cups_backend->got_default_printer && remote_default_printer != NULL)
2200 cups_backend->default_printer = g_strdup (remote_default_printer);
2201 cups_backend->got_default_printer = TRUE;
2202 g_free (remote_default_printer);
2204 if (cups_backend->default_printer != NULL)
2206 GtkPrinter *default_printer = NULL;
2207 default_printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (cups_backend),
2208 cups_backend->default_printer);
2209 if (default_printer != NULL)
2211 gtk_printer_set_is_default (default_printer, TRUE);
2212 g_signal_emit_by_name (GTK_PRINT_BACKEND (cups_backend),
2213 "printer-status-changed", default_printer);
2218 GDK_THREADS_LEAVE ();
2222 update_backend_status (GtkPrintBackendCups *cups_backend,
2223 GtkCupsConnectionState state)
2227 case GTK_CUPS_CONNECTION_NOT_AVAILABLE:
2228 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_UNAVAILABLE, NULL);
2230 case GTK_CUPS_CONNECTION_AVAILABLE:
2231 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_OK, NULL);
2238 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
2240 GtkCupsConnectionState state;
2241 GtkCupsRequest *request;
2242 static const char * const pattrs[] = /* Attributes we're interested in */
2245 "printer-uri-supported",
2249 "printer-state-message",
2250 "printer-state-reasons",
2253 "printer-is-accepting-jobs",
2254 "job-sheets-supported",
2255 "job-sheets-default",
2257 "auth-info-required",
2261 if (cups_backend->reading_ppds > 0 || cups_backend->list_printers_pending)
2264 state = gtk_cups_connection_test_get_state (cups_backend->cups_connection_test);
2265 update_backend_status (cups_backend, state);
2267 if (cups_backend->list_printers_attempts == 60)
2269 cups_backend->list_printers_attempts = -1;
2270 if (cups_backend->list_printers_poll > 0)
2271 g_source_remove (cups_backend->list_printers_poll);
2272 cups_backend->list_printers_poll = gdk_threads_add_timeout (200,
2273 (GSourceFunc) cups_request_printer_list,
2276 else if (cups_backend->list_printers_attempts != -1)
2277 cups_backend->list_printers_attempts++;
2279 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2282 if (cups_backend->list_printers_attempts > 0)
2283 cups_backend->list_printers_attempts = 60;
2285 cups_backend->list_printers_pending = TRUE;
2287 request = gtk_cups_request_new_with_username (NULL,
2293 cups_backend->username);
2295 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2296 "requested-attributes", G_N_ELEMENTS (pattrs),
2299 cups_request_execute (cups_backend,
2301 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
2309 cups_get_printer_list (GtkPrintBackend *backend)
2311 GtkPrintBackendCups *cups_backend;
2313 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
2315 if (cups_backend->cups_connection_test == NULL)
2316 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
2318 if (cups_backend->list_printers_poll == 0)
2320 if (cups_request_printer_list (cups_backend))
2321 cups_backend->list_printers_poll = gdk_threads_add_timeout (50,
2322 (GSourceFunc) cups_request_printer_list,
2328 GtkPrinterCups *printer;
2334 get_ppd_data_free (GetPPDData *data)
2337 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2338 httpClose (data->http);
2339 g_io_channel_unref (data->ppd_io);
2340 g_object_unref (data->printer);
2345 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
2346 GtkCupsResult *result,
2349 GtkPrinter *printer;
2351 GDK_THREADS_ENTER ();
2354 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2356 printer = GTK_PRINTER (data->printer);
2357 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
2358 print_backend->reading_ppds--;
2360 if (gtk_cups_result_is_error (result))
2362 gboolean success = FALSE;
2364 /* if we get a 404 then it is just a raw printer without a ppd
2366 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
2367 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
2369 gtk_printer_set_has_details (printer, TRUE);
2373 g_signal_emit_by_name (printer, "details-acquired", success);
2377 /* let ppdOpenFd take over the ownership of the open file */
2378 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
2379 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
2380 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
2381 ppdLocalize (data->printer->ppd_file);
2384 ppdMarkDefaults (data->printer->ppd_file);
2386 gtk_printer_set_has_details (printer, TRUE);
2387 g_signal_emit_by_name (printer, "details-acquired", TRUE);
2390 GDK_THREADS_LEAVE ();
2394 cups_request_ppd (GtkPrinter *printer)
2397 GtkPrintBackend *print_backend;
2398 GtkPrinterCups *cups_printer;
2399 GtkCupsRequest *request;
2406 cups_printer = GTK_PRINTER_CUPS (printer);
2411 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2413 if (cups_printer->remote)
2415 GtkCupsConnectionState state;
2417 state = gtk_cups_connection_test_get_state (cups_printer->remote_cups_connection_test);
2419 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS)
2421 if (cups_printer->get_remote_ppd_attempts == 60)
2423 cups_printer->get_remote_ppd_attempts = -1;
2424 if (cups_printer->get_remote_ppd_poll > 0)
2425 g_source_remove (cups_printer->get_remote_ppd_poll);
2426 cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (200,
2427 (GSourceFunc) cups_request_ppd,
2430 else if (cups_printer->get_remote_ppd_attempts != -1)
2431 cups_printer->get_remote_ppd_attempts++;
2436 gtk_cups_connection_test_free (cups_printer->remote_cups_connection_test);
2437 cups_printer->remote_cups_connection_test = NULL;
2438 cups_printer->get_remote_ppd_poll = 0;
2439 cups_printer->get_remote_ppd_attempts = 0;
2441 if (state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2443 g_signal_emit_by_name (printer, "details-acquired", FALSE);
2448 http = httpConnectEncrypt (cups_printer->hostname,
2452 data = g_new0 (GetPPDData, 1);
2454 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
2458 #ifdef G_ENABLE_DEBUG
2459 /* If we are debugging printing don't delete the tmp files */
2460 if (!(gtk_get_debug_flags () & GTK_DEBUG_PRINTING))
2461 unlink (ppd_filename);
2463 unlink (ppd_filename);
2464 #endif /* G_ENABLE_DEBUG */
2469 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
2471 g_error_free (error);
2473 g_free (ppd_filename);
2476 g_signal_emit_by_name (printer, "details-acquired", FALSE);
2481 fchmod (fd, S_IRUSR | S_IWUSR);
2482 data->ppd_io = g_io_channel_unix_new (fd);
2483 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
2484 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
2486 data->printer = g_object_ref (printer);
2488 resource = g_strdup_printf ("/printers/%s.ppd",
2489 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
2491 print_backend = gtk_printer_get_backend (printer);
2493 request = gtk_cups_request_new_with_username (data->http,
2497 cups_printer->hostname,
2499 GTK_PRINT_BACKEND_CUPS (print_backend)->username);
2502 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
2505 cups_printer->reading_ppd = TRUE;
2506 GTK_PRINT_BACKEND_CUPS (print_backend)->reading_ppds++;
2508 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
2510 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
2512 (GDestroyNotify)get_ppd_data_free);
2515 g_free (ppd_filename);
2520 /* Ordering matters for default preference */
2521 static const char *lpoptions_locations[] = {
2522 "/etc/cups/lpoptions",
2528 cups_parse_user_default_printer (const char *filename,
2529 char **printer_name)
2532 char line[1024], *lineptr, *defname = NULL;
2534 if ((fp = g_fopen (filename, "r")) == NULL)
2537 while (fgets (line, sizeof (line), fp) != NULL)
2539 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
2543 while (isspace (*lineptr))
2550 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
2555 if (*printer_name != NULL)
2556 g_free (*printer_name);
2558 *printer_name = g_strdup (defname);
2565 cups_get_user_default_printer (char **printer_name)
2569 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
2571 if (g_path_is_absolute (lpoptions_locations[i]))
2573 cups_parse_user_default_printer (lpoptions_locations[i],
2580 filename = g_build_filename (g_get_home_dir (),
2581 lpoptions_locations[i], NULL);
2582 cups_parse_user_default_printer (filename, printer_name);
2589 cups_parse_user_options (const char *filename,
2590 const char *printer_name,
2592 cups_option_t **options)
2595 gchar line[1024], *lineptr, *name;
2597 if ((fp = g_fopen (filename, "r")) == NULL)
2600 while (fgets (line, sizeof (line), fp) != NULL)
2602 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
2604 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
2609 /* Skip leading whitespace */
2610 while (isspace (*lineptr))
2616 /* NUL-terminate the name, stripping the instance name */
2618 while (!isspace (*lineptr) && *lineptr)
2620 if (*lineptr == '/')
2630 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
2633 /* We found our printer, parse the options */
2634 num_options = cupsParseOptions (lineptr, num_options, options);
2643 cups_get_user_options (const char *printer_name,
2645 cups_option_t **options)
2649 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
2651 if (g_path_is_absolute (lpoptions_locations[i]))
2653 num_options = cups_parse_user_options (lpoptions_locations[i],
2662 filename = g_build_filename (g_get_home_dir (),
2663 lpoptions_locations[i], NULL);
2664 num_options = cups_parse_user_options (filename, printer_name,
2665 num_options, options);
2673 /* This function requests default printer from a CUPS server in regular intervals.
2674 * In the case of unreachable CUPS server the request is repeated later.
2675 * The default printer is not requested in the case of previous success.
2678 cups_get_default_printer (GtkPrintBackendCups *backend)
2680 GtkPrintBackendCups *cups_backend;
2682 cups_backend = backend;
2684 if (cups_backend->cups_connection_test == NULL)
2685 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
2687 if (cups_backend->default_printer_poll == 0)
2689 if (cups_request_default_printer (cups_backend))
2690 cups_backend->default_printer_poll = gdk_threads_add_timeout (200,
2691 (GSourceFunc) cups_request_default_printer,
2696 /* This function gets default printer from local settings.*/
2698 cups_get_local_default_printer (GtkPrintBackendCups *backend)
2703 if ((str = g_getenv ("LPDEST")) != NULL)
2705 backend->default_printer = g_strdup (str);
2706 backend->got_default_printer = TRUE;
2709 else if ((str = g_getenv ("PRINTER")) != NULL &&
2710 strcmp (str, "lp") != 0)
2712 backend->default_printer = g_strdup (str);
2713 backend->got_default_printer = TRUE;
2717 /* Figure out user setting for default printer */
2718 cups_get_user_default_printer (&name);
2721 backend->default_printer = name;
2722 backend->got_default_printer = TRUE;
2728 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
2729 GtkCupsResult *result,
2733 ipp_attribute_t *attr;
2734 GtkPrinter *printer;
2736 GDK_THREADS_ENTER ();
2738 if (gtk_cups_result_is_error (result))
2740 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
2741 gtk_cups_result_get_error_code (result) == 1)
2743 /* Canceled by user, stop popping up more password dialogs */
2744 if (print_backend->list_printers_poll > 0)
2745 g_source_remove (print_backend->list_printers_poll);
2746 print_backend->list_printers_poll = 0;
2752 response = gtk_cups_result_get_response (result);
2754 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
2755 print_backend->default_printer = g_strdup (ippGetString (attr, 0, NULL));
2757 print_backend->got_default_printer = TRUE;
2759 if (print_backend->default_printer != NULL)
2761 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (print_backend), print_backend->default_printer);
2762 if (printer != NULL)
2764 gtk_printer_set_is_default (printer, TRUE);
2765 g_signal_emit_by_name (GTK_PRINT_BACKEND (print_backend), "printer-status-changed", printer);
2769 /* Make sure to kick off get_printers if we are polling it,
2770 * as we could have blocked this reading the default printer
2772 if (print_backend->list_printers_poll != 0)
2773 cups_request_printer_list (print_backend);
2775 GDK_THREADS_LEAVE ();
2779 cups_request_default_printer (GtkPrintBackendCups *print_backend)
2781 GtkCupsConnectionState state;
2782 GtkCupsRequest *request;
2784 state = gtk_cups_connection_test_get_state (print_backend->cups_connection_test);
2785 update_backend_status (print_backend, state);
2787 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2790 request = gtk_cups_request_new_with_username (NULL,
2796 print_backend->username);
2798 cups_request_execute (print_backend,
2800 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
2801 g_object_ref (print_backend),
2808 cups_printer_request_details (GtkPrinter *printer)
2810 GtkPrinterCups *cups_printer;
2812 cups_printer = GTK_PRINTER_CUPS (printer);
2813 if (!cups_printer->reading_ppd &&
2814 gtk_printer_cups_get_ppd (cups_printer) == NULL)
2816 if (cups_printer->remote)
2818 if (cups_printer->get_remote_ppd_poll == 0)
2820 cups_printer->remote_cups_connection_test = gtk_cups_connection_test_new (cups_printer->hostname);
2822 if (cups_request_ppd (printer))
2823 cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (50,
2824 (GSourceFunc) cups_request_ppd,
2829 cups_request_ppd (printer);
2834 ppd_text_to_utf8 (ppd_file_t *ppd_file,
2837 const char *encoding = NULL;
2840 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
2842 return g_strdup (text);
2844 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
2846 encoding = "ISO-8859-1";
2848 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
2850 encoding = "ISO-8859-2";
2852 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
2854 encoding = "ISO-8859-5";
2856 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
2858 encoding = "SHIFT-JIS";
2860 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
2862 encoding = "MACINTOSH";
2864 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
2866 encoding = "WINDOWS-1252";
2870 /* Fallback, try iso-8859-1... */
2871 encoding = "ISO-8859-1";
2874 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
2879 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
2880 res = g_strdup ("???");
2886 /* TODO: Add more translations for common settings here */
2888 static const struct {
2889 const char *keyword;
2890 const char *translation;
2891 } cups_option_translations[] = {
2892 { "Duplex", N_("Two Sided") },
2893 { "MediaType", N_("Paper Type") },
2894 { "InputSlot", N_("Paper Source") },
2895 { "OutputBin", N_("Output Tray") },
2896 { "Resolution", N_("Resolution") },
2897 { "PreFilter", N_("GhostScript pre-filtering") },
2901 static const struct {
2902 const char *keyword;
2904 const char *translation;
2905 } cups_choice_translations[] = {
2906 { "Duplex", "None", N_("One Sided") },
2907 /* Translators: this is an option of "Two Sided" */
2908 { "Duplex", "DuplexNoTumble", N_("Long Edge (Standard)") },
2909 /* Translators: this is an option of "Two Sided" */
2910 { "Duplex", "DuplexTumble", N_("Short Edge (Flip)") },
2911 /* Translators: this is an option of "Paper Source" */
2912 { "InputSlot", "Auto", N_("Auto Select") },
2913 /* Translators: this is an option of "Paper Source" */
2914 { "InputSlot", "AutoSelect", N_("Auto Select") },
2915 /* Translators: this is an option of "Paper Source" */
2916 { "InputSlot", "Default", N_("Printer Default") },
2917 /* Translators: this is an option of "Paper Source" */
2918 { "InputSlot", "None", N_("Printer Default") },
2919 /* Translators: this is an option of "Paper Source" */
2920 { "InputSlot", "PrinterDefault", N_("Printer Default") },
2921 /* Translators: this is an option of "Paper Source" */
2922 { "InputSlot", "Unspecified", N_("Auto Select") },
2923 /* Translators: this is an option of "Resolution" */
2924 { "Resolution", "default", N_("Printer Default") },
2925 /* Translators: this is an option of "GhostScript" */
2926 { "PreFilter", "EmbedFonts", N_("Embed GhostScript fonts only") },
2927 /* Translators: this is an option of "GhostScript" */
2928 { "PreFilter", "Level1", N_("Convert to PS level 1") },
2929 /* Translators: this is an option of "GhostScript" */
2930 { "PreFilter", "Level2", N_("Convert to PS level 2") },
2931 /* Translators: this is an option of "GhostScript" */
2932 { "PreFilter", "No", N_("No pre-filtering") },
2935 static const struct {
2937 const char *translation;
2938 } cups_group_translations[] = {
2939 /* Translators: "Miscellaneous" is the label for a button, that opens
2940 up an extra panel of settings in a print dialog. */
2941 { "Miscellaneous", N_("Miscellaneous") },
2944 static const struct {
2945 const char *ppd_keyword;
2947 } ppd_option_names[] = {
2948 {"Duplex", "gtk-duplex" },
2949 {"MediaType", "gtk-paper-type"},
2950 {"InputSlot", "gtk-paper-source"},
2951 {"OutputBin", "gtk-output-tray"},
2954 static const struct {
2955 const char *lpoption;
2957 } lpoption_names[] = {
2958 {"number-up", "gtk-n-up" },
2959 {"number-up-layout", "gtk-n-up-layout"},
2960 {"job-billing", "gtk-billing-info"},
2961 {"job-priority", "gtk-job-prio"},
2964 /* keep sorted when changing */
2965 static const char *color_option_whitelist[] = {
2966 "BRColorEnhancement",
2972 "BlackSubstitution",
2980 "RPSBlackOverPrint",
2984 /* keep sorted when changing */
2985 static const char *color_group_whitelist[] = {
2992 "HPColorOptionsPanel",
2995 /* keep sorted when changing */
2996 static const char *image_quality_option_whitelist[] = {
2998 "BRHalfTonePattern",
3008 "HPGraphicsHalftone",
3023 /* keep sorted when changing */
3024 static const char *image_quality_group_whitelist[] = {
3031 /* keep sorted when changing */
3032 static const char * finishing_option_whitelist[] = {
3045 "StapleOrientation",
3051 /* keep sorted when changing */
3052 static const char *finishing_group_whitelist[] = {
3061 /* keep sorted when changing */
3062 static const char *cups_option_blacklist[] = {
3071 get_option_text (ppd_file_t *ppd_file,
3072 ppd_option_t *option)
3077 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
3079 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
3080 return g_strdup (_(cups_option_translations[i].translation));
3083 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
3085 /* Some ppd files have spaces in the text before the colon */
3092 get_choice_text (ppd_file_t *ppd_file,
3093 ppd_choice_t *choice)
3096 ppd_option_t *option = choice->option;
3097 const char *keyword = option->keyword;
3099 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
3101 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
3102 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
3103 return g_strdup (_(cups_choice_translations[i].translation));
3105 return ppd_text_to_utf8 (ppd_file, choice->text);
3109 group_has_option (ppd_group_t *group,
3110 ppd_option_t *option)
3117 if (group->num_options > 0 &&
3118 option >= group->options && option < group->options + group->num_options)
3121 for (i = 0; i < group->num_subgroups; i++)
3123 if (group_has_option (&group->subgroups[i],option))
3130 set_option_off (GtkPrinterOption *option)
3132 /* Any of these will do, _set only applies the value
3133 * if its allowed of the option */
3134 gtk_printer_option_set (option, "False");
3135 gtk_printer_option_set (option, "Off");
3136 gtk_printer_option_set (option, "None");
3140 value_is_off (const char *value)
3142 return (strcasecmp (value, "None") == 0 ||
3143 strcasecmp (value, "Off") == 0 ||
3144 strcasecmp (value, "False") == 0);
3148 ppd_group_name (ppd_group_t *group)
3150 #if CUPS_VERSION_MAJOR > 1 || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR > 1) || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR == 1 && CUPS_VERSION_PATCH >= 18)
3158 available_choices (ppd_file_t *ppd,
3159 ppd_option_t *option,
3160 ppd_choice_t ***available,
3161 gboolean keep_if_only_one_option)
3163 ppd_option_t *other_option;
3166 ppd_const_t *constraint;
3167 const char *choice, *other_choice;
3168 ppd_option_t *option1, *option2;
3169 ppd_group_t *installed_options;
3171 gboolean all_default;
3177 conflicts = g_new0 (char, option->num_choices);
3179 installed_options = NULL;
3180 for (i = 0; i < ppd->num_groups; i++)
3184 name = ppd_group_name (&ppd->groups[i]);
3185 if (strcmp (name, "InstallableOptions") == 0)
3187 installed_options = &ppd->groups[i];
3192 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
3194 option1 = ppdFindOption (ppd, constraint->option1);
3195 if (option1 == NULL)
3198 option2 = ppdFindOption (ppd, constraint->option2);
3199 if (option2 == NULL)
3202 if (option == option1)
3204 choice = constraint->choice1;
3205 other_option = option2;
3206 other_choice = constraint->choice2;
3208 else if (option == option2)
3210 choice = constraint->choice2;
3211 other_option = option1;
3212 other_choice = constraint->choice1;
3217 /* We only care of conflicts with installed_options and
3219 if (!group_has_option (installed_options, other_option) &&
3220 (strcmp (other_option->keyword, "PageSize") != 0))
3223 if (*other_choice == 0)
3225 /* Conflict only if the installed option is not off */
3226 if (value_is_off (other_option->defchoice))
3229 /* Conflict if the installed option has the specified default */
3230 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
3235 /* Conflict with all non-off choices */
3236 for (j = 0; j < option->num_choices; j++)
3238 if (!value_is_off (option->choices[j].choice))
3244 for (j = 0; j < option->num_choices; j++)
3246 if (strcasecmp (option->choices[j].choice, choice) == 0)
3254 for (j = 0; j < option->num_choices; j++)
3258 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
3259 all_default = FALSE;
3262 if ((all_default && !keep_if_only_one_option) ||
3263 (num_conflicts == option->num_choices))
3270 /* Some ppds don't have a "use printer default" option for
3271 * InputSlot. This means you always have to select a particular slot,
3272 * and you can't auto-pick source based on the paper size. To support
3273 * this we always add an auto option if there isn't one already. If
3274 * the user chooses the generated option we don't send any InputSlot
3275 * value when printing. The way we detect existing auto-cases is based
3276 * on feedback from Michael Sweet of cups fame.
3279 if (strcmp (option->keyword, "InputSlot") == 0)
3281 gboolean found_auto = FALSE;
3282 for (j = 0; j < option->num_choices; j++)
3286 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
3287 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
3288 strcmp (option->choices[j].choice, "Default") == 0 ||
3289 strcmp (option->choices[j].choice, "None") == 0 ||
3290 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
3291 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
3292 option->choices[j].code == NULL ||
3293 option->choices[j].code[0] == 0)
3307 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
3310 for (j = 0; j < option->num_choices; j++)
3313 (*available)[i++] = &option->choices[j];
3317 (*available)[i++] = NULL;
3322 return option->num_choices - num_conflicts + add_auto;
3325 static GtkPrinterOption *
3326 create_pickone_option (ppd_file_t *ppd_file,
3327 ppd_option_t *ppd_option,
3328 const gchar *gtk_name)
3330 GtkPrinterOption *option;
3331 ppd_choice_t **available;
3335 ppd_coption_t *coption;
3337 g_assert (ppd_option->ui == PPD_UI_PICKONE);
3341 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
3345 /* right now only support one parameter per custom option
3346 * if more than one print warning and only offer the default choices
3349 label = get_option_text (ppd_file, ppd_option);
3351 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
3355 ppd_cparam_t *cparam;
3357 cparam = ppdFirstCustomParam (coption);
3359 if (ppdNextCustomParam (coption) == NULL)
3361 switch (cparam->type)
3363 case PPD_CUSTOM_INT:
3364 option = gtk_printer_option_new (gtk_name, label,
3365 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
3367 case PPD_CUSTOM_PASSCODE:
3368 option = gtk_printer_option_new (gtk_name, label,
3369 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
3371 case PPD_CUSTOM_PASSWORD:
3372 option = gtk_printer_option_new (gtk_name, label,
3373 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
3375 case PPD_CUSTOM_REAL:
3376 option = gtk_printer_option_new (gtk_name, label,
3377 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
3379 case PPD_CUSTOM_STRING:
3380 option = gtk_printer_option_new (gtk_name, label,
3381 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
3383 #ifdef PRINT_IGNORED_OPTIONS
3384 case PPD_CUSTOM_POINTS:
3385 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
3387 case PPD_CUSTOM_CURVE:
3388 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
3390 case PPD_CUSTOM_INVCURVE:
3391 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
3398 #ifdef PRINT_IGNORED_OPTIONS
3400 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
3405 option = gtk_printer_option_new (gtk_name, label,
3406 GTK_PRINTER_OPTION_TYPE_PICKONE);
3409 gtk_printer_option_allocate_choices (option, n_choices);
3410 for (i = 0; i < n_choices; i++)
3412 if (available[i] == NULL)
3414 /* This was auto-added */
3415 option->choices[i] = g_strdup ("gtk-ignore-value");
3416 option->choices_display[i] = g_strdup (_("Printer Default"));
3420 option->choices[i] = g_strdup (available[i]->choice);
3421 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
3425 if (option->type != GTK_PRINTER_OPTION_TYPE_PICKONE)
3427 if (g_str_has_prefix (ppd_option->defchoice, "Custom."))
3428 gtk_printer_option_set (option, ppd_option->defchoice + 7);
3430 gtk_printer_option_set (option, ppd_option->defchoice);
3434 gtk_printer_option_set (option, ppd_option->defchoice);
3437 #ifdef PRINT_IGNORED_OPTIONS
3439 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
3446 static GtkPrinterOption *
3447 create_boolean_option (ppd_file_t *ppd_file,
3448 ppd_option_t *ppd_option,
3449 const gchar *gtk_name)
3451 GtkPrinterOption *option;
3452 ppd_choice_t **available;
3456 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
3460 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
3463 label = get_option_text (ppd_file, ppd_option);
3464 option = gtk_printer_option_new (gtk_name, label,
3465 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
3468 gtk_printer_option_allocate_choices (option, 2);
3469 option->choices[0] = g_strdup ("True");
3470 option->choices_display[0] = g_strdup ("True");
3471 option->choices[1] = g_strdup ("False");
3472 option->choices_display[1] = g_strdup ("False");
3474 gtk_printer_option_set (option, ppd_option->defchoice);
3476 #ifdef PRINT_IGNORED_OPTIONS
3478 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
3486 get_ppd_option_name (const gchar *keyword)
3490 for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
3491 if (strcmp (ppd_option_names[i].ppd_keyword, keyword) == 0)
3492 return g_strdup (ppd_option_names[i].name);
3494 return g_strdup_printf ("cups-%s", keyword);
3498 get_lpoption_name (const gchar *lpoption)
3502 for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
3503 if (strcmp (ppd_option_names[i].ppd_keyword, lpoption) == 0)
3504 return g_strdup (ppd_option_names[i].name);
3506 for (i = 0; i < G_N_ELEMENTS (lpoption_names); i++)
3507 if (strcmp (lpoption_names[i].lpoption, lpoption) == 0)
3508 return g_strdup (lpoption_names[i].name);
3510 return g_strdup_printf ("cups-%s", lpoption);
3514 strptr_cmp (const void *a,
3517 char **aa = (char **)a;
3518 char **bb = (char **)b;
3519 return strcmp (*aa, *bb);
3524 string_in_table (gchar *str,
3525 const gchar *table[],
3528 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
3531 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
3534 handle_option (GtkPrinterOptionSet *set,
3535 ppd_file_t *ppd_file,
3536 ppd_option_t *ppd_option,
3537 ppd_group_t *toplevel_group,
3538 GtkPrintSettings *settings)
3540 GtkPrinterOption *option;
3544 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
3547 name = get_ppd_option_name (ppd_option->keyword);
3550 if (ppd_option->ui == PPD_UI_PICKONE)
3552 option = create_pickone_option (ppd_file, ppd_option, name);
3554 else if (ppd_option->ui == PPD_UI_BOOLEAN)
3556 option = create_boolean_option (ppd_file, ppd_option, name);
3558 #ifdef PRINT_IGNORED_OPTIONS
3560 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
3567 name = ppd_group_name (toplevel_group);
3568 if (STRING_IN_TABLE (name,
3569 color_group_whitelist) ||
3570 STRING_IN_TABLE (ppd_option->keyword,
3571 color_option_whitelist))
3573 option->group = g_strdup ("ColorPage");
3575 else if (STRING_IN_TABLE (name,
3576 image_quality_group_whitelist) ||
3577 STRING_IN_TABLE (ppd_option->keyword,
3578 image_quality_option_whitelist))
3580 option->group = g_strdup ("ImageQualityPage");
3582 else if (STRING_IN_TABLE (name,
3583 finishing_group_whitelist) ||
3584 STRING_IN_TABLE (ppd_option->keyword,
3585 finishing_option_whitelist))
3587 option->group = g_strdup ("FinishingPage");
3591 for (i = 0; i < G_N_ELEMENTS (cups_group_translations); i++)
3593 if (strcmp (cups_group_translations[i].name, toplevel_group->name) == 0)
3595 option->group = g_strdup (_(cups_group_translations[i].translation));
3600 if (i == G_N_ELEMENTS (cups_group_translations))
3601 option->group = g_strdup (toplevel_group->text);
3604 set_option_from_settings (option, settings);
3606 gtk_printer_option_set_add (set, option);
3613 handle_group (GtkPrinterOptionSet *set,
3614 ppd_file_t *ppd_file,
3616 ppd_group_t *toplevel_group,
3617 GtkPrintSettings *settings)
3622 /* Ignore installable options */
3623 name = ppd_group_name (toplevel_group);
3624 if (strcmp (name, "InstallableOptions") == 0)
3627 for (i = 0; i < group->num_options; i++)
3628 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
3630 for (i = 0; i < group->num_subgroups; i++)
3631 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
3638 GtkPrintSettings *settings;
3639 GtkPrinter *printer;
3640 } GtkPrintBackendCupsColordHelper;
3643 colord_printer_option_set_changed_cb (GtkPrinterOptionSet *set,
3644 GtkPrintBackendCupsColordHelper *helper)
3646 gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (helper->printer),
3652 static GtkPrinterOptionSet *
3653 cups_printer_get_options (GtkPrinter *printer,
3654 GtkPrintSettings *settings,
3655 GtkPageSetup *page_setup,
3656 GtkPrintCapabilities capabilities)
3658 GtkPrinterOptionSet *set;
3659 GtkPrinterOption *option;
3660 ppd_file_t *ppd_file;
3662 char *print_at[] = { "now", "at", "on-hold" };
3663 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
3664 char *prio[] = {"100", "80", "50", "30" };
3665 /* Translators: These strings name the possible values of the
3666 * job priority option in the print dialog
3668 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
3669 char *n_up_layout[] = { "lrtb", "lrbt", "rltb", "rlbt", "tblr", "tbrl", "btlr", "btrl" };
3670 /* Translators: These strings name the possible arrangements of
3671 * multiple pages on a sheet when printing
3673 char *n_up_layout_display[] = { N_("Left to right, top to bottom"), N_("Left to right, bottom to top"),
3674 N_("Right to left, top to bottom"), N_("Right to left, bottom to top"),
3675 N_("Top to bottom, left to right"), N_("Top to bottom, right to left"),
3676 N_("Bottom to top, left to right"), N_("Bottom to top, right to left") };
3679 cups_option_t *opts = NULL;
3680 GtkPrintBackendCups *backend;
3681 GtkTextDirection text_direction;
3682 GtkPrinterCups *cups_printer = NULL;
3684 GtkPrintBackendCupsColordHelper *helper;
3686 char *default_number_up;
3688 set = gtk_printer_option_set_new ();
3690 /* Cups specific, non-ppd related settings */
3692 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
3693 prio_display[i] = _(prio_display[i]);
3695 /* Translators, this string is used to label the job priority option
3696 * in the print dialog
3698 option = gtk_printer_option_new ("gtk-job-prio", _("Job Priority"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3699 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
3700 prio, prio_display);
3701 gtk_printer_option_set (option, "50");
3702 set_option_from_settings (option, settings);
3703 gtk_printer_option_set_add (set, option);
3704 g_object_unref (option);
3706 /* Translators, this string is used to label the billing info entry
3707 * in the print dialog
3709 option = gtk_printer_option_new ("gtk-billing-info", _("Billing Info"), GTK_PRINTER_OPTION_TYPE_STRING);
3710 gtk_printer_option_set (option, "");
3711 set_option_from_settings (option, settings);
3712 gtk_printer_option_set_add (set, option);
3713 g_object_unref (option);
3715 backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
3716 cups_printer = GTK_PRINTER_CUPS (printer);
3718 if (backend != NULL && printer != NULL)
3720 char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
3721 /* Translators, these strings are names for various 'standard' cover
3722 * pages that the printing system may support.
3724 char *cover_display_default[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
3725 char **cover = NULL;
3726 char **cover_display = NULL;
3727 char **cover_display_translated = NULL;
3728 gint num_of_covers = 0;
3732 /* Translators, this string is used to label the pages-per-sheet option
3733 * in the print dialog
3735 option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3736 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
3738 default_number_up = g_strdup_printf ("%d", cups_printer->default_number_up);
3739 gtk_printer_option_set (option, default_number_up);
3740 g_free (default_number_up);
3741 set_option_from_settings (option, settings);
3742 gtk_printer_option_set_add (set, option);
3743 g_object_unref (option);
3745 if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
3747 for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
3748 n_up_layout_display[i] = _(n_up_layout_display[i]);
3750 /* Translators, this string is used to label the option in the print
3751 * dialog that controls in what order multiple pages are arranged
3753 option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3754 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
3755 n_up_layout, n_up_layout_display);
3757 text_direction = gtk_widget_get_default_direction ();
3758 if (text_direction == GTK_TEXT_DIR_LTR)
3759 gtk_printer_option_set (option, "lrtb");
3761 gtk_printer_option_set (option, "rltb");
3763 set_option_from_settings (option, settings);
3764 gtk_printer_option_set_add (set, option);
3765 g_object_unref (option);
3768 num_of_covers = backend->number_of_covers;
3769 cover = g_new (char *, num_of_covers + 1);
3770 cover[num_of_covers] = NULL;
3771 cover_display = g_new (char *, num_of_covers + 1);
3772 cover_display[num_of_covers] = NULL;
3773 cover_display_translated = g_new (char *, num_of_covers + 1);
3774 cover_display_translated[num_of_covers] = NULL;
3776 for (i = 0; i < num_of_covers; i++)
3778 cover[i] = g_strdup (backend->covers[i]);
3780 for (j = 0; j < G_N_ELEMENTS (cover_default); j++)
3781 if (strcmp (cover_default[j], cover[i]) == 0)
3783 value = cover_display_default[j];
3786 cover_display[i] = (value != NULL) ? g_strdup (value) : g_strdup (backend->covers[i]);
3789 for (i = 0; i < num_of_covers; i++)
3790 cover_display_translated[i] = _(cover_display[i]);
3792 /* Translators, this is the label used for the option in the print
3793 * dialog that controls the front cover page.
3795 option = gtk_printer_option_new ("gtk-cover-before", _("Before"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3796 gtk_printer_option_choices_from_array (option, num_of_covers,
3797 cover, cover_display_translated);
3799 if (cups_printer->default_cover_before != NULL)
3800 gtk_printer_option_set (option, cups_printer->default_cover_before);
3802 gtk_printer_option_set (option, "none");
3803 set_option_from_settings (option, settings);
3804 gtk_printer_option_set_add (set, option);
3805 g_object_unref (option);
3807 /* Translators, this is the label used for the option in the print
3808 * dialog that controls the back cover page.
3810 option = gtk_printer_option_new ("gtk-cover-after", _("After"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3811 gtk_printer_option_choices_from_array (option, num_of_covers,
3812 cover, cover_display_translated);
3813 if (cups_printer->default_cover_after != NULL)
3814 gtk_printer_option_set (option, cups_printer->default_cover_after);
3816 gtk_printer_option_set (option, "none");
3817 set_option_from_settings (option, settings);
3818 gtk_printer_option_set_add (set, option);
3819 g_object_unref (option);
3822 g_strfreev (cover_display);
3823 g_free (cover_display_translated);
3826 /* Translators: this is the name of the option that controls when
3827 * a print job is printed. Possible values are 'now', a specified time,
3830 option = gtk_printer_option_new ("gtk-print-time", _("Print at"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3831 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
3832 print_at, print_at);
3833 gtk_printer_option_set (option, "now");
3834 set_option_from_settings (option, settings);
3835 gtk_printer_option_set_add (set, option);
3836 g_object_unref (option);
3838 /* Translators: this is the name of the option that allows the user
3839 * to specify a time when a print job will be printed.
3841 option = gtk_printer_option_new ("gtk-print-time-text", _("Print at time"), GTK_PRINTER_OPTION_TYPE_STRING);
3842 gtk_printer_option_set (option, "");
3843 set_option_from_settings (option, settings);
3844 gtk_printer_option_set_add (set, option);
3845 g_object_unref (option);
3847 /* Printer (ppd) specific settings */
3848 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3851 GtkPaperSize *paper_size;
3852 ppd_option_t *option;
3853 const gchar *ppd_name;
3855 ppdMarkDefaults (ppd_file);
3857 paper_size = gtk_page_setup_get_paper_size (page_setup);
3859 option = ppdFindOption (ppd_file, "PageSize");
3860 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
3863 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
3867 char width[G_ASCII_DTOSTR_BUF_SIZE];
3868 char height[G_ASCII_DTOSTR_BUF_SIZE];
3870 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3871 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3872 /* Translators: this format is used to display a custom paper
3873 * size. The two placeholders are replaced with the width and height
3874 * in points. E.g: "Custom 230.4x142.9"
3876 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
3877 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
3878 g_free (custom_name);
3881 for (i = 0; i < ppd_file->num_groups; i++)
3882 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
3885 /* Now honor the user set defaults for this printer */
3886 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
3888 for (i = 0; i < num_opts; i++)
3890 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
3893 name = get_lpoption_name (opts[i].name);
3894 if (strcmp (name, "cups-job-sheets") == 0)
3899 values = g_strsplit (opts[i].value, ",", 2);
3900 num_values = g_strv_length (values);
3902 option = gtk_printer_option_set_lookup (set, "gtk-cover-before");
3903 if (option && num_values > 0)
3904 gtk_printer_option_set (option, g_strstrip (values[0]));
3906 option = gtk_printer_option_set_lookup (set, "gtk-cover-after");
3907 if (option && num_values > 1)
3908 gtk_printer_option_set (option, g_strstrip (values[1]));
3910 g_strfreev (values);
3912 else if (strcmp (name, "cups-job-hold-until") == 0)
3914 GtkPrinterOption *option2 = NULL;
3916 option = gtk_printer_option_set_lookup (set, "gtk-print-time-text");
3917 if (option && opts[i].value)
3919 option2 = gtk_printer_option_set_lookup (set, "gtk-print-time");
3922 if (strcmp (opts[i].value, "indefinite") == 0)
3923 gtk_printer_option_set (option2, "on-hold");
3926 gtk_printer_option_set (option2, "at");
3927 gtk_printer_option_set (option, opts[i].value);
3932 else if (strcmp (name, "cups-sides") == 0)
3934 option = gtk_printer_option_set_lookup (set, "gtk-duplex");
3935 if (option && opts[i].value)
3937 if (strcmp (opts[i].value, "two-sided-short-edge") == 0)
3938 gtk_printer_option_set (option, "DuplexTumble");
3939 else if (strcmp (opts[i].value, "two-sided-long-edge") == 0)
3940 gtk_printer_option_set (option, "DuplexNoTumble");
3945 option = gtk_printer_option_set_lookup (set, name);
3947 gtk_printer_option_set (option, opts[i].value);
3952 cupsFreeOptions (num_opts, opts);
3955 /* TRANSLATORS: this this the ICC color profile to use for this job */
3956 option = gtk_printer_option_new ("colord-profile",
3957 _("Printer Profile"),
3958 GTK_PRINTER_OPTION_TYPE_INFO);
3960 /* assign it to the color page */
3961 option->group = g_strdup ("ColorPage");
3963 /* TRANSLATORS: this is when color profile information is unavailable */
3964 gtk_printer_option_set (option, _("Unavailable"));
3965 gtk_printer_option_set_add (set, option);
3967 /* watch to see if the user changed the options */
3968 helper = g_new (GtkPrintBackendCupsColordHelper, 1);
3969 helper->printer = printer;
3970 helper->settings = settings;
3971 g_signal_connect_data (set, "changed",
3972 G_CALLBACK (colord_printer_option_set_changed_cb),
3974 (GClosureNotify) g_free,
3977 /* initial coldplug */
3978 gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (printer),
3980 g_object_bind_property (printer, "profile-title",
3991 mark_option_from_set (GtkPrinterOptionSet *set,
3992 ppd_file_t *ppd_file,
3993 ppd_option_t *ppd_option)
3995 GtkPrinterOption *option;
3996 char *name = get_ppd_option_name (ppd_option->keyword);
3998 option = gtk_printer_option_set_lookup (set, name);
4001 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
4008 mark_group_from_set (GtkPrinterOptionSet *set,
4009 ppd_file_t *ppd_file,
4014 for (i = 0; i < group->num_options; i++)
4015 mark_option_from_set (set, ppd_file, &group->options[i]);
4017 for (i = 0; i < group->num_subgroups; i++)
4018 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
4022 set_conflicts_from_option (GtkPrinterOptionSet *set,
4023 ppd_file_t *ppd_file,
4024 ppd_option_t *ppd_option)
4026 GtkPrinterOption *option;
4029 if (ppd_option->conflicted)
4031 name = get_ppd_option_name (ppd_option->keyword);
4032 option = gtk_printer_option_set_lookup (set, name);
4035 gtk_printer_option_set_has_conflict (option, TRUE);
4036 #ifdef PRINT_IGNORED_OPTIONS
4038 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
4046 set_conflicts_from_group (GtkPrinterOptionSet *set,
4047 ppd_file_t *ppd_file,
4052 for (i = 0; i < group->num_options; i++)
4053 set_conflicts_from_option (set, ppd_file, &group->options[i]);
4055 for (i = 0; i < group->num_subgroups; i++)
4056 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
4060 cups_printer_mark_conflicts (GtkPrinter *printer,
4061 GtkPrinterOptionSet *options)
4063 ppd_file_t *ppd_file;
4067 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4069 if (ppd_file == NULL)
4072 ppdMarkDefaults (ppd_file);
4074 for (i = 0; i < ppd_file->num_groups; i++)
4075 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
4077 num_conflicts = ppdConflicts (ppd_file);
4079 if (num_conflicts > 0)
4081 for (i = 0; i < ppd_file->num_groups; i++)
4082 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
4085 return num_conflicts > 0;
4089 GtkPrinter *printer;
4090 GtkPrinterOptionSet *options;
4091 GtkPrintSettings *settings;
4092 ppd_file_t *ppd_file;
4097 const char *standard;
4101 map_settings_to_option (GtkPrinterOption *option,
4102 const NameMapping table[],
4104 GtkPrintSettings *settings,
4105 const gchar *standard_name,
4106 const gchar *cups_name)
4110 const char *cups_value;
4111 const char *standard_value;
4113 /* If the cups-specific setting is set, always use that */
4114 name = g_strdup_printf ("cups-%s", cups_name);
4115 cups_value = gtk_print_settings_get (settings, name);
4118 if (cups_value != NULL)
4120 gtk_printer_option_set (option, cups_value);
4124 /* Otherwise we try to convert from the general setting */
4125 standard_value = gtk_print_settings_get (settings, standard_name);
4126 if (standard_value == NULL)
4129 for (i = 0; i < n_elements; i++)
4131 if (table[i].cups == NULL && table[i].standard == NULL)
4133 gtk_printer_option_set (option, standard_value);
4136 else if (table[i].cups == NULL &&
4137 strcmp (table[i].standard, standard_value) == 0)
4139 set_option_off (option);
4142 else if (strcmp (table[i].standard, standard_value) == 0)
4144 gtk_printer_option_set (option, table[i].cups);
4151 map_option_to_settings (const gchar *value,
4152 const NameMapping table[],
4154 GtkPrintSettings *settings,
4155 const gchar *standard_name,
4156 const gchar *cups_name)
4161 for (i = 0; i < n_elements; i++)
4163 if (table[i].cups == NULL && table[i].standard == NULL)
4165 gtk_print_settings_set (settings,
4170 else if (table[i].cups == NULL && table[i].standard != NULL)
4172 if (value_is_off (value))
4174 gtk_print_settings_set (settings,
4180 else if (strcmp (table[i].cups, value) == 0)
4182 gtk_print_settings_set (settings,
4189 /* Always set the corresponding cups-specific setting */
4190 name = g_strdup_printf ("cups-%s", cups_name);
4191 gtk_print_settings_set (settings, name, value);
4196 static const NameMapping paper_source_map[] = {
4197 { "Lower", "lower"},
4198 { "Middle", "middle"},
4199 { "Upper", "upper"},
4201 { "Envelope", "envelope"},
4202 { "Cassette", "cassette"},
4203 { "LargeCapacity", "large-capacity"},
4204 { "AnySmallFormat", "small-format"},
4205 { "AnyLargeFormat", "large-format"},
4209 static const NameMapping output_tray_map[] = {
4210 { "Upper", "upper"},
4211 { "Lower", "lower"},
4216 static const NameMapping duplex_map[] = {
4217 { "DuplexTumble", "vertical" },
4218 { "DuplexNoTumble", "horizontal" },
4222 static const NameMapping output_mode_map[] = {
4223 { "Standard", "normal" },
4224 { "Normal", "normal" },
4225 { "Draft", "draft" },
4226 { "Fast", "draft" },
4229 static const NameMapping media_type_map[] = {
4230 { "Transparency", "transparency"},
4231 { "Standard", "stationery"},
4235 static const NameMapping all_map[] = {
4241 set_option_from_settings (GtkPrinterOption *option,
4242 GtkPrintSettings *settings)
4244 const char *cups_value;
4247 if (settings == NULL)
4250 if (strcmp (option->name, "gtk-paper-source") == 0)
4251 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
4252 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
4253 else if (strcmp (option->name, "gtk-output-tray") == 0)
4254 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
4255 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
4256 else if (strcmp (option->name, "gtk-duplex") == 0)
4257 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
4258 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
4259 else if (strcmp (option->name, "cups-OutputMode") == 0)
4260 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
4261 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
4262 else if (strcmp (option->name, "cups-Resolution") == 0)
4264 cups_value = gtk_print_settings_get (settings, option->name);
4266 gtk_printer_option_set (option, cups_value);
4269 if (gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION, -1) != -1 ||
4270 gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_X, -1) != -1 ||
4271 gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_Y, -1) != -1 ||
4272 option->value == NULL || option->value[0] == '\0')
4274 int res = gtk_print_settings_get_resolution (settings);
4275 int res_x = gtk_print_settings_get_resolution_x (settings);
4276 int res_y = gtk_print_settings_get_resolution_y (settings);
4280 value = g_strdup_printf ("%dx%ddpi", res_x, res_y);
4281 gtk_printer_option_set (option, value);
4286 value = g_strdup_printf ("%ddpi", res);
4287 gtk_printer_option_set (option, value);
4293 else if (strcmp (option->name, "gtk-paper-type") == 0)
4294 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
4295 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
4296 else if (strcmp (option->name, "gtk-n-up") == 0)
4298 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
4299 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
4301 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
4303 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
4304 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
4306 else if (strcmp (option->name, "gtk-billing-info") == 0)
4308 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
4310 gtk_printer_option_set (option, cups_value);
4312 else if (strcmp (option->name, "gtk-job-prio") == 0)
4314 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
4316 gtk_printer_option_set (option, cups_value);
4318 else if (strcmp (option->name, "gtk-cover-before") == 0)
4320 cups_value = gtk_print_settings_get (settings, "cover-before");
4322 gtk_printer_option_set (option, cups_value);
4324 else if (strcmp (option->name, "gtk-cover-after") == 0)
4326 cups_value = gtk_print_settings_get (settings, "cover-after");
4328 gtk_printer_option_set (option, cups_value);
4330 else if (strcmp (option->name, "gtk-print-time") == 0)
4332 cups_value = gtk_print_settings_get (settings, "print-at");
4334 gtk_printer_option_set (option, cups_value);
4336 else if (strcmp (option->name, "gtk-print-time-text") == 0)
4338 cups_value = gtk_print_settings_get (settings, "print-at-time");
4340 gtk_printer_option_set (option, cups_value);
4342 else if (g_str_has_prefix (option->name, "cups-"))
4344 cups_value = gtk_print_settings_get (settings, option->name);
4346 gtk_printer_option_set (option, cups_value);
4351 foreach_option_get_settings (GtkPrinterOption *option,
4354 struct OptionData *data = user_data;
4355 GtkPrintSettings *settings = data->settings;
4358 value = option->value;
4360 if (strcmp (option->name, "gtk-paper-source") == 0)
4361 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
4362 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
4363 else if (strcmp (option->name, "gtk-output-tray") == 0)
4364 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
4365 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
4366 else if (strcmp (option->name, "gtk-duplex") == 0)
4367 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
4368 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
4369 else if (strcmp (option->name, "cups-OutputMode") == 0)
4370 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
4371 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
4372 else if (strcmp (option->name, "cups-Resolution") == 0)
4374 int res, res_x, res_y;
4376 if (sscanf (value, "%dx%ddpi", &res_x, &res_y) == 2)
4378 if (res_x > 0 && res_y > 0)
4379 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
4381 else if (sscanf (value, "%ddpi", &res) == 1)
4384 gtk_print_settings_set_resolution (settings, res);
4387 gtk_print_settings_set (settings, option->name, value);
4389 else if (strcmp (option->name, "gtk-paper-type") == 0)
4390 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
4391 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
4392 else if (strcmp (option->name, "gtk-n-up") == 0)
4393 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
4394 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
4395 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
4396 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
4397 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
4398 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
4399 gtk_print_settings_set (settings, "cups-job-billing", value);
4400 else if (strcmp (option->name, "gtk-job-prio") == 0)
4401 gtk_print_settings_set (settings, "cups-job-priority", value);
4402 else if (strcmp (option->name, "gtk-cover-before") == 0)
4403 gtk_print_settings_set (settings, "cover-before", value);
4404 else if (strcmp (option->name, "gtk-cover-after") == 0)
4405 gtk_print_settings_set (settings, "cover-after", value);
4406 else if (strcmp (option->name, "gtk-print-time") == 0)
4407 gtk_print_settings_set (settings, "print-at", value);
4408 else if (strcmp (option->name, "gtk-print-time-text") == 0)
4409 gtk_print_settings_set (settings, "print-at-time", value);
4410 else if (g_str_has_prefix (option->name, "cups-"))
4411 gtk_print_settings_set (settings, option->name, value);
4415 supports_am_pm (void)
4417 struct tm tmp_tm = { 0 };
4421 length = strftime (time, sizeof (time), "%p", &tmp_tm);
4426 /* Converts local time to UTC time. Local time has to be in one of these
4427 * formats: HH:MM:SS, HH:MM, HH:MM:SS {am, pm}, HH:MM {am, pm}, HH {am, pm},
4428 * {am, pm} HH:MM:SS, {am, pm} HH:MM, {am, pm} HH.
4429 * Returns a newly allocated string holding UTC time in HH:MM:SS format
4433 localtime_to_utctime (const char *local_time)
4435 const char *formats_0[] = {" %I : %M : %S %p ", " %p %I : %M : %S ",
4437 " %I : %M %p ", " %p %I : %M ",
4439 " %I %p ", " %p %I "};
4440 const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
4441 const char *end = NULL;
4442 struct tm *actual_local_time;
4443 struct tm *actual_utc_time;
4444 struct tm local_print_time;
4445 struct tm utc_print_time;
4446 struct tm diff_time;
4447 gchar *utc_time = NULL;
4450 if (local_time == NULL || local_time[0] == '\0')
4453 n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
4455 for (i = 0; i < n; i++)
4457 local_print_time.tm_hour = 0;
4458 local_print_time.tm_min = 0;
4459 local_print_time.tm_sec = 0;
4461 if (supports_am_pm ())
4462 end = strptime (local_time, formats_0[i], &local_print_time);
4464 end = strptime (local_time, formats_1[i], &local_print_time);
4466 if (end != NULL && end[0] == '\0')
4470 if (end != NULL && end[0] == '\0')
4475 actual_utc_time = g_memdup (gmtime (&rawtime), sizeof (struct tm));
4476 actual_local_time = g_memdup (localtime (&rawtime), sizeof (struct tm));
4478 diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
4479 diff_time.tm_min = actual_utc_time->tm_min - actual_local_time->tm_min;
4480 diff_time.tm_sec = actual_utc_time->tm_sec - actual_local_time->tm_sec;
4482 utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
4483 utc_print_time.tm_min = ((local_print_time.tm_min + diff_time.tm_min) + 60) % 60;
4484 utc_print_time.tm_sec = ((local_print_time.tm_sec + diff_time.tm_sec) + 60) % 60;
4486 utc_time = g_strdup_printf ("%02d:%02d:%02d",
4487 utc_print_time.tm_hour,
4488 utc_print_time.tm_min,
4489 utc_print_time.tm_sec);
4496 cups_printer_get_settings_from_options (GtkPrinter *printer,
4497 GtkPrinterOptionSet *options,
4498 GtkPrintSettings *settings)
4500 struct OptionData data;
4501 const char *print_at, *print_at_time;
4503 data.printer = printer;
4504 data.options = options;
4505 data.settings = settings;
4506 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4508 if (data.ppd_file != NULL)
4510 GtkPrinterOption *cover_before, *cover_after;
4512 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
4514 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
4515 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
4516 if (cover_before && cover_after)
4518 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
4519 gtk_print_settings_set (settings, "cups-job-sheets", value);
4523 print_at = gtk_print_settings_get (settings, "print-at");
4524 print_at_time = gtk_print_settings_get (settings, "print-at-time");
4526 if (strcmp (print_at, "at") == 0)
4528 gchar *utc_time = NULL;
4530 utc_time = localtime_to_utctime (print_at_time);
4532 if (utc_time != NULL)
4534 gtk_print_settings_set (settings, "cups-job-hold-until", utc_time);
4538 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
4540 else if (strcmp (print_at, "on-hold") == 0)
4541 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
4546 cups_printer_prepare_for_print (GtkPrinter *printer,
4547 GtkPrintJob *print_job,
4548 GtkPrintSettings *settings,
4549 GtkPageSetup *page_setup)
4551 GtkPrintPages pages;
4552 GtkPageRange *ranges;
4554 GtkPageSet page_set;
4555 GtkPaperSize *paper_size;
4556 const char *ppd_paper_name;
4559 pages = gtk_print_settings_get_print_pages (settings);
4560 gtk_print_job_set_pages (print_job, pages);
4562 if (pages == GTK_PRINT_PAGES_RANGES)
4563 ranges = gtk_print_settings_get_page_ranges (settings, &n_ranges);
4570 gtk_print_job_set_page_ranges (print_job, ranges, n_ranges);
4571 if (gtk_print_settings_get_collate (settings))
4572 gtk_print_settings_set (settings, "cups-Collate", "True");
4573 gtk_print_job_set_collate (print_job, FALSE);
4575 if (gtk_print_settings_get_reverse (settings))
4576 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
4577 gtk_print_job_set_reverse (print_job, FALSE);
4579 if (gtk_print_settings_get_n_copies (settings) > 1)
4580 gtk_print_settings_set_int (settings, "cups-copies",
4581 gtk_print_settings_get_n_copies (settings));
4582 gtk_print_job_set_num_copies (print_job, 1);
4584 scale = gtk_print_settings_get_scale (settings);
4586 gtk_print_job_set_scale (print_job, scale / 100.0);
4588 page_set = gtk_print_settings_get_page_set (settings);
4589 if (page_set == GTK_PAGE_SET_EVEN)
4590 gtk_print_settings_set (settings, "cups-page-set", "even");
4591 else if (page_set == GTK_PAGE_SET_ODD)
4592 gtk_print_settings_set (settings, "cups-page-set", "odd");
4593 gtk_print_job_set_page_set (print_job, GTK_PAGE_SET_ALL);
4595 paper_size = gtk_page_setup_get_paper_size (page_setup);
4596 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
4597 if (ppd_paper_name != NULL)
4598 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
4601 char width[G_ASCII_DTOSTR_BUF_SIZE];
4602 char height[G_ASCII_DTOSTR_BUF_SIZE];
4605 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
4606 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
4607 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
4608 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
4609 g_free (custom_name);
4612 if (gtk_print_settings_get_number_up (settings) > 1)
4614 GtkNumberUpLayout layout = gtk_print_settings_get_number_up_layout (settings);
4615 GEnumClass *enum_class;
4616 GEnumValue *enum_value;
4618 switch (gtk_page_setup_get_orientation (page_setup))
4620 case GTK_PAGE_ORIENTATION_PORTRAIT:
4622 case GTK_PAGE_ORIENTATION_LANDSCAPE:
4624 layout = layout + 2 + 4 * (1 - layout / 2);
4626 layout = layout - 3 - 2 * (layout % 2);
4628 case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
4629 layout = (layout + 3 - 2 * (layout % 2)) % 4 + 4 * (layout / 4);
4631 case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
4633 layout = layout + 5 - 2 * (layout % 2);
4635 layout = layout - 6 + 4 * (1 - (layout - 4) / 2);
4639 enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
4640 enum_value = g_enum_get_value (enum_class, layout);
4641 gtk_print_settings_set (settings, "cups-number-up-layout", enum_value->value_nick);
4642 g_type_class_unref (enum_class);
4645 gtk_print_job_set_rotate (print_job, TRUE);
4648 static GtkPageSetup *
4649 create_page_setup (ppd_file_t *ppd_file,
4653 GtkPageSetup *page_setup;
4654 GtkPaperSize *paper_size;
4655 ppd_option_t *option;
4656 ppd_choice_t *choice;
4658 display_name = NULL;
4659 option = ppdFindOption (ppd_file, "PageSize");
4662 choice = ppdFindChoice (option, size->name);
4664 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
4667 if (display_name == NULL)
4668 display_name = g_strdup (size->name);
4670 page_setup = gtk_page_setup_new ();
4671 paper_size = gtk_paper_size_new_from_ppd (size->name,
4675 gtk_page_setup_set_paper_size (page_setup, paper_size);
4676 gtk_paper_size_free (paper_size);
4678 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
4679 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
4680 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
4681 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
4683 g_free (display_name);
4689 cups_printer_list_papers (GtkPrinter *printer)
4691 ppd_file_t *ppd_file;
4693 GtkPageSetup *page_setup;
4697 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4698 if (ppd_file == NULL)
4703 for (i = 0; i < ppd_file->num_sizes; i++)
4705 size = &ppd_file->sizes[i];
4707 page_setup = create_page_setup (ppd_file, size);
4709 l = g_list_prepend (l, page_setup);
4712 return g_list_reverse (l);
4715 static GtkPageSetup *
4716 cups_printer_get_default_page_size (GtkPrinter *printer)
4718 ppd_file_t *ppd_file;
4720 ppd_option_t *option;
4723 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4724 if (ppd_file == NULL)
4727 option = ppdFindOption (ppd_file, "PageSize");
4731 size = ppdPageSize (ppd_file, option->defchoice);
4735 return create_page_setup (ppd_file, size);
4739 cups_printer_get_hard_margins (GtkPrinter *printer,
4745 ppd_file_t *ppd_file;
4747 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4748 if (ppd_file == NULL)
4751 *left = ppd_file->custom_margins[0];
4752 *bottom = ppd_file->custom_margins[1];
4753 *right = ppd_file->custom_margins[2];
4754 *top = ppd_file->custom_margins[3];
4759 static GtkPrintCapabilities
4760 cups_printer_get_capabilities (GtkPrinter *printer)
4763 GTK_PRINT_CAPABILITY_COPIES |
4764 GTK_PRINT_CAPABILITY_COLLATE |
4765 GTK_PRINT_CAPABILITY_REVERSE |
4766 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 1 && CUPS_VERSION_PATCH >= 15) || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR > 1) || CUPS_VERSION_MAJOR > 1
4767 GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT |
4769 GTK_PRINT_CAPABILITY_NUMBER_UP;