1 /* GTK - The GIMP Toolkit
2 * gtkprintbackendcups.h: Default implementation of GtkPrintBackend
3 * for the Common Unix Print System (CUPS)
4 * Copyright (C) 2006, 2007 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 #include <sys/types.h>
34 #include <cups/cups.h>
35 #include <cups/language.h>
36 #include <cups/http.h>
40 #include <cairo-pdf.h>
43 #include <glib/gstdio.h>
44 #include <glib/gi18n-lib.h>
48 #include <gtk/gtkprintbackend.h>
49 #include <gtk/gtkunixprint.h>
50 #include <gtk/gtkprinter-private.h>
52 #include "gtkprintbackendcups.h"
53 #include "gtkprintercups.h"
55 #include "gtkcupsutils.h"
61 typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
63 #define GTK_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
64 #define GTK_IS_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_CUPS))
65 #define GTK_PRINT_BACKEND_CUPS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
67 #define _CUPS_MAX_ATTEMPTS 10
68 #define _CUPS_MAX_CHUNK_SIZE 8192
70 /* define this to see warnings about ignored ppd options */
71 #undef PRINT_IGNORED_OPTIONS
73 #define _CUPS_MAP_ATTR_INT(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].integer;}
74 #define _CUPS_MAP_ATTR_STR(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].string.text;}
76 static GType print_backend_cups_type = 0;
78 typedef void (* GtkPrintCupsResponseCallbackFunc) (GtkPrintBackend *print_backend,
79 GtkCupsResult *result,
90 } GtkPrintCupsDispatchState;
97 GtkCupsRequest *request;
98 GtkCupsPollState poll_state;
100 GtkPrintBackendCups *backend;
101 GtkPrintCupsResponseCallbackFunc callback;
102 gpointer callback_data;
104 } GtkPrintCupsDispatchWatch;
106 struct _GtkPrintBackendCupsClass
108 GtkPrintBackendClass parent_class;
111 struct _GtkPrintBackendCups
113 GtkPrintBackend parent_instance;
115 char *default_printer;
117 guint list_printers_poll;
118 guint list_printers_pending : 1;
119 gint list_printers_attempts;
120 guint got_default_printer : 1;
121 guint default_printer_poll;
122 GtkCupsConnectionTest *cups_connection_test;
126 int number_of_covers;
131 gboolean authentication_lock;
133 CdClient *colord_client;
137 static GObjectClass *backend_parent_class;
139 static void gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class);
140 static void gtk_print_backend_cups_init (GtkPrintBackendCups *impl);
141 static void gtk_print_backend_cups_finalize (GObject *object);
142 static void gtk_print_backend_cups_dispose (GObject *object);
143 static void cups_get_printer_list (GtkPrintBackend *print_backend);
144 static void cups_get_default_printer (GtkPrintBackendCups *print_backend);
145 static void cups_get_local_default_printer (GtkPrintBackendCups *print_backend);
146 static void cups_request_execute (GtkPrintBackendCups *print_backend,
147 GtkCupsRequest *request,
148 GtkPrintCupsResponseCallbackFunc callback,
150 GDestroyNotify notify);
151 static void cups_printer_get_settings_from_options (GtkPrinter *printer,
152 GtkPrinterOptionSet *options,
153 GtkPrintSettings *settings);
154 static gboolean cups_printer_mark_conflicts (GtkPrinter *printer,
155 GtkPrinterOptionSet *options);
156 static GtkPrinterOptionSet *cups_printer_get_options (GtkPrinter *printer,
157 GtkPrintSettings *settings,
158 GtkPageSetup *page_setup,
159 GtkPrintCapabilities capabilities);
160 static void cups_printer_prepare_for_print (GtkPrinter *printer,
161 GtkPrintJob *print_job,
162 GtkPrintSettings *settings,
163 GtkPageSetup *page_setup);
164 static GList * cups_printer_list_papers (GtkPrinter *printer);
165 static GtkPageSetup * cups_printer_get_default_page_size (GtkPrinter *printer);
166 static void cups_printer_request_details (GtkPrinter *printer);
167 static gboolean cups_request_default_printer (GtkPrintBackendCups *print_backend);
168 static gboolean cups_request_ppd (GtkPrinter *printer);
169 static gboolean cups_printer_get_hard_margins (GtkPrinter *printer,
174 static GtkPrintCapabilities cups_printer_get_capabilities (GtkPrinter *printer);
175 static void set_option_from_settings (GtkPrinterOption *option,
176 GtkPrintSettings *setting);
177 static void cups_begin_polling_info (GtkPrintBackendCups *print_backend,
180 static gboolean cups_job_info_poll_timeout (gpointer user_data);
181 static void gtk_print_backend_cups_print_stream (GtkPrintBackend *backend,
184 GtkPrintJobCompleteFunc callback,
186 GDestroyNotify dnotify);
187 static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter *printer,
188 GtkPrintSettings *settings,
191 GIOChannel *cache_io);
193 static void gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
194 gchar **auth_info_required,
197 void overwrite_and_free (gpointer data);
198 static gboolean is_address_local (const gchar *address);
199 static gboolean request_auth_info (gpointer data);
202 gtk_print_backend_cups_register_type (GTypeModule *module)
204 const GTypeInfo print_backend_cups_info =
206 sizeof (GtkPrintBackendCupsClass),
207 NULL, /* base_init */
208 NULL, /* base_finalize */
209 (GClassInitFunc) gtk_print_backend_cups_class_init,
210 NULL, /* class_finalize */
211 NULL, /* class_data */
212 sizeof (GtkPrintBackendCups),
214 (GInstanceInitFunc) gtk_print_backend_cups_init
217 print_backend_cups_type = g_type_module_register_type (module,
218 GTK_TYPE_PRINT_BACKEND,
219 "GtkPrintBackendCups",
220 &print_backend_cups_info, 0);
224 pb_module_init (GTypeModule *module)
227 g_print ("CUPS Backend: Initializing the CUPS print backend module\n"));
229 gtk_print_backend_cups_register_type (module);
230 gtk_printer_cups_register_type (module);
234 pb_module_exit (void)
239 G_MODULE_EXPORT GtkPrintBackend *
240 pb_module_create (void)
242 return gtk_print_backend_cups_new ();
246 * GtkPrintBackendCups
249 gtk_print_backend_cups_get_type (void)
251 return print_backend_cups_type;
255 * gtk_print_backend_cups_new:
257 * Creates a new #GtkPrintBackendCups object. #GtkPrintBackendCups
258 * implements the #GtkPrintBackend interface with direct access to
259 * the filesystem using Unix/Linux API calls
261 * Return value: the new #GtkPrintBackendCups object
264 gtk_print_backend_cups_new (void)
267 g_print ("CUPS Backend: Creating a new CUPS print backend object\n"));
269 return g_object_new (GTK_TYPE_PRINT_BACKEND_CUPS, NULL);
273 gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
275 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
276 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
278 backend_parent_class = g_type_class_peek_parent (class);
280 gobject_class->finalize = gtk_print_backend_cups_finalize;
281 gobject_class->dispose = gtk_print_backend_cups_dispose;
283 backend_class->request_printer_list = cups_get_printer_list;
284 backend_class->print_stream = gtk_print_backend_cups_print_stream;
285 backend_class->printer_request_details = cups_printer_request_details;
286 backend_class->printer_create_cairo_surface = cups_printer_create_cairo_surface;
287 backend_class->printer_get_options = cups_printer_get_options;
288 backend_class->printer_mark_conflicts = cups_printer_mark_conflicts;
289 backend_class->printer_get_settings_from_options = cups_printer_get_settings_from_options;
290 backend_class->printer_prepare_for_print = cups_printer_prepare_for_print;
291 backend_class->printer_list_papers = cups_printer_list_papers;
292 backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
293 backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
294 backend_class->printer_get_capabilities = cups_printer_get_capabilities;
295 backend_class->set_password = gtk_print_backend_cups_set_password;
298 static cairo_status_t
299 _cairo_write_to_cups (void *closure,
300 const unsigned char *data,
303 GIOChannel *io = (GIOChannel *)closure;
310 g_print ("CUPS Backend: Writing %i byte chunk to temp file\n", length));
314 g_io_channel_write_chars (io, (gchar *)data, length, &written, &error);
319 g_print ("CUPS Backend: Error writing to temp file, %s\n",
322 g_error_free (error);
323 return CAIRO_STATUS_WRITE_ERROR;
327 g_print ("CUPS Backend: Wrote %"G_GSIZE_FORMAT" bytes to temp file\n", written));
333 return CAIRO_STATUS_SUCCESS;
336 static cairo_surface_t *
337 cups_printer_create_cairo_surface (GtkPrinter *printer,
338 GtkPrintSettings *settings,
341 GIOChannel *cache_io)
343 cairo_surface_t *surface;
344 ppd_file_t *ppd_file = NULL;
345 ppd_attr_t *ppd_attr = NULL;
346 ppd_attr_t *ppd_attr_res = NULL;
347 ppd_attr_t *ppd_attr_screen_freq = NULL;
348 ppd_attr_t *ppd_attr_res_screen_freq = NULL;
349 gchar *res_string = NULL;
352 if (gtk_printer_accepts_pdf (printer))
353 surface = cairo_pdf_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
355 surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
357 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
359 if (ppd_file != NULL)
361 ppd_attr = ppdFindAttr (ppd_file, "LanguageLevel", NULL);
363 if (ppd_attr != NULL)
364 level = atoi (ppd_attr->value);
366 if (gtk_print_settings_get_resolution (settings) == 0)
368 ppd_attr_res = ppdFindAttr (ppd_file, "DefaultResolution", NULL);
370 if (ppd_attr_res != NULL)
372 int res, res_x, res_y;
374 if (sscanf (ppd_attr_res->value, "%dx%ddpi", &res_x, &res_y) == 2)
376 if (res_x > 0 && res_y > 0)
377 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
379 else if (sscanf (ppd_attr_res->value, "%ddpi", &res) == 1)
382 gtk_print_settings_set_resolution (settings, res);
387 res_string = g_strdup_printf ("%ddpi",
388 gtk_print_settings_get_resolution (settings));
389 ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
392 if (ppd_attr_res_screen_freq == NULL)
394 res_string = g_strdup_printf ("%dx%ddpi",
395 gtk_print_settings_get_resolution_x (settings),
396 gtk_print_settings_get_resolution_y (settings));
397 ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
401 ppd_attr_screen_freq = ppdFindAttr (ppd_file, "ScreenFreq", NULL);
403 if (ppd_attr_res_screen_freq != NULL && atof (ppd_attr_res_screen_freq->value) > 0.0)
404 gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_res_screen_freq->value));
405 else if (ppd_attr_screen_freq != NULL && atof (ppd_attr_screen_freq->value) > 0.0)
406 gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_screen_freq->value));
409 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PS)
412 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
415 cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
418 cairo_surface_set_fallback_resolution (surface,
419 2.0 * gtk_print_settings_get_printer_lpi (settings),
420 2.0 * gtk_print_settings_get_printer_lpi (settings));
426 GtkPrintJobCompleteFunc callback;
429 GDestroyNotify dnotify;
430 } CupsPrintStreamData;
433 cups_free_print_stream_data (CupsPrintStreamData *data)
436 g_print ("CUPS Backend: %s\n", G_STRFUNC));
439 data->dnotify (data->user_data);
440 g_object_unref (data->job);
445 cups_print_cb (GtkPrintBackendCups *print_backend,
446 GtkCupsResult *result,
449 GError *error = NULL;
450 CupsPrintStreamData *ps = user_data;
452 GDK_THREADS_ENTER ();
455 g_print ("CUPS Backend: %s\n", G_STRFUNC));
457 if (gtk_cups_result_is_error (result))
458 error = g_error_new_literal (gtk_print_error_quark (),
459 GTK_PRINT_ERROR_INTERNAL_ERROR,
460 gtk_cups_result_get_error_string (result));
463 ps->callback (ps->job, ps->user_data, error);
468 ipp_attribute_t *attr; /* IPP job-id attribute */
469 ipp_t *response = gtk_cups_result_get_response (result);
471 if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
472 job_id = attr->values[0].integer;
474 if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
475 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
478 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_PENDING);
479 cups_begin_polling_info (print_backend, ps->job, job_id);
483 gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED_ABORTED);
487 g_error_free (error);
489 GDK_THREADS_LEAVE ();
493 GtkCupsRequest *request;
494 GtkPrinterCups *printer;
498 add_cups_options (const gchar *key,
502 CupsOptionsData *data = (CupsOptionsData *) user_data;
503 GtkCupsRequest *request = data->request;
504 GtkPrinterCups *printer = data->printer;
505 gboolean custom_value = FALSE;
506 gchar *new_value = NULL;
512 if (!g_str_has_prefix (key, "cups-"))
515 if (strcmp (value, "gtk-ignore-value") == 0)
518 key = key + strlen ("cups-");
520 if (printer && printer->ppd_file)
522 ppd_coption_t *coption;
523 gboolean found = FALSE;
524 gboolean custom_values_enabled = FALSE;
526 coption = ppdFindCustomOption (printer->ppd_file, key);
527 if (coption && coption->option)
529 for (i = 0; i < coption->option->num_choices; i++)
531 /* Are custom values enabled ? */
532 if (g_str_equal (coption->option->choices[i].choice, "Custom"))
533 custom_values_enabled = TRUE;
535 /* Is the value among available choices ? */
536 if (g_str_equal (coption->option->choices[i].choice, value))
540 if (custom_values_enabled && !found)
545 /* Add "Custom." prefix to custom values. */
548 new_value = g_strdup_printf ("Custom.%s", value);
549 gtk_cups_request_encode_option (request, key, new_value);
553 gtk_cups_request_encode_option (request, key, value);
557 gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
560 GtkPrintJobCompleteFunc callback,
562 GDestroyNotify dnotify)
564 GtkPrinterCups *cups_printer;
565 CupsPrintStreamData *ps;
566 CupsOptionsData *options_data;
567 GtkCupsRequest *request;
568 GtkPrintSettings *settings;
570 char printer_absolute_uri[HTTP_MAX_URI];
573 g_print ("CUPS Backend: %s\n", G_STRFUNC));
575 cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
576 settings = gtk_print_job_get_settings (job);
578 request = gtk_cups_request_new_with_username (NULL,
583 cups_printer->device_uri,
584 GTK_PRINT_BACKEND_CUPS (print_backend)->username);
586 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
587 httpAssembleURIf (HTTP_URI_CODING_ALL,
588 printer_absolute_uri,
589 sizeof (printer_absolute_uri),
595 gtk_printer_get_name (gtk_print_job_get_printer (job)));
597 g_snprintf (printer_absolute_uri,
598 sizeof (printer_absolute_uri),
599 "ipp://localhost:%d/printers/%s",
601 gtk_printer_get_name (gtk_print_job_get_printer (job)));
604 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
605 IPP_TAG_URI, "printer-uri",
606 NULL, printer_absolute_uri);
608 title = gtk_print_job_get_title (job);
610 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
611 IPP_TAG_NAME, "job-name",
614 options_data = g_new0 (CupsOptionsData, 1);
615 options_data->request = request;
616 options_data->printer = cups_printer;
617 gtk_print_settings_foreach (settings, add_cups_options, options_data);
618 g_free (options_data);
620 ps = g_new0 (CupsPrintStreamData, 1);
621 ps->callback = callback;
622 ps->user_data = user_data;
623 ps->dnotify = dnotify;
624 ps->job = g_object_ref (job);
626 request->need_auth_info = cups_printer->auth_info_required != NULL;
627 request->auth_info_required = g_strdupv (cups_printer->auth_info_required);
629 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
631 (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
633 (GDestroyNotify)cups_free_print_stream_data);
636 void overwrite_and_free (gpointer data)
638 gchar *password = (gchar *) data;
640 if (password != NULL)
642 memset (password, 0, strlen (password));
648 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
650 backend_cups->list_printers_poll = FALSE;
651 backend_cups->got_default_printer = FALSE;
652 backend_cups->list_printers_pending = FALSE;
653 backend_cups->list_printers_attempts = 0;
654 backend_cups->reading_ppds = 0;
656 backend_cups->requests = NULL;
657 backend_cups->auth = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, overwrite_and_free);
658 backend_cups->authentication_lock = FALSE;
660 backend_cups->covers = NULL;
661 backend_cups->number_of_covers = 0;
663 backend_cups->default_printer_poll = 0;
664 backend_cups->cups_connection_test = NULL;
666 backend_cups->username = NULL;
669 backend_cups->colord_client = cd_client_new ();
672 cups_get_local_default_printer (backend_cups);
676 gtk_print_backend_cups_finalize (GObject *object)
678 GtkPrintBackendCups *backend_cups;
681 g_print ("CUPS Backend: finalizing CUPS backend module\n"));
683 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
685 g_free (backend_cups->default_printer);
686 backend_cups->default_printer = NULL;
688 g_strfreev (backend_cups->covers);
689 backend_cups->number_of_covers = 0;
691 gtk_cups_connection_test_free (backend_cups->cups_connection_test);
692 backend_cups->cups_connection_test = NULL;
694 g_hash_table_destroy (backend_cups->auth);
696 g_free (backend_cups->username);
699 g_object_unref (backend_cups->colord_client);
702 backend_parent_class->finalize (object);
706 gtk_print_backend_cups_dispose (GObject *object)
708 GtkPrintBackendCups *backend_cups;
711 g_print ("CUPS Backend: %s\n", G_STRFUNC));
713 backend_cups = GTK_PRINT_BACKEND_CUPS (object);
715 if (backend_cups->list_printers_poll > 0)
716 g_source_remove (backend_cups->list_printers_poll);
717 backend_cups->list_printers_poll = 0;
718 backend_cups->list_printers_attempts = 0;
720 if (backend_cups->default_printer_poll > 0)
721 g_source_remove (backend_cups->default_printer_poll);
722 backend_cups->default_printer_poll = 0;
724 backend_parent_class->dispose (object);
728 is_address_local (const gchar *address)
730 if (address[0] == '/' ||
731 strcmp (address, "127.0.0.1") == 0 ||
732 strcmp (address, "[::1]") == 0)
738 #ifndef HAVE_CUPS_API_1_2
739 /* Included from CUPS library because of backward compatibility */
741 httpGetHostname(http_t *http,
745 struct hostent *host;
752 if (http->hostname[0] == '/')
753 g_strlcpy (s, "localhost", slen);
755 g_strlcpy (s, http->hostname, slen);
759 if (gethostname (s, slen) < 0)
760 g_strlcpy (s, "localhost", slen);
762 if (!strchr (s, '.'))
764 if ((host = gethostbyname (s)) != NULL && host->h_name)
765 g_strlcpy (s, host->h_name, slen);
773 gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
774 gchar **auth_info_required,
777 GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
779 char dispatch_hostname[HTTP_MAX_URI];
780 gchar *username = NULL;
781 gchar *hostname = NULL;
782 gchar *password = NULL;
786 length = g_strv_length (auth_info_required);
788 if (auth_info != NULL)
789 for (i = 0; i < length; i++)
791 if (g_strcmp0 (auth_info_required[i], "username") == 0)
792 username = g_strdup (auth_info[i]);
793 else if (g_strcmp0 (auth_info_required[i], "hostname") == 0)
794 hostname = g_strdup (auth_info[i]);
795 else if (g_strcmp0 (auth_info_required[i], "password") == 0)
796 password = g_strdup (auth_info[i]);
799 if (hostname != NULL && username != NULL && password != NULL)
801 gchar *key = g_strconcat (username, "@", hostname, NULL);
802 g_hash_table_insert (cups_backend->auth, key, g_strdup (password));
804 g_print ("CUPS backend: storing password for %s\n", key));
807 g_free (cups_backend->username);
808 cups_backend->username = g_strdup (username);
811 for (l = cups_backend->requests; l; l = l->next)
813 GtkPrintCupsDispatchWatch *dispatch = l->data;
815 httpGetHostname (dispatch->request->http, dispatch_hostname, sizeof (dispatch_hostname));
816 if (is_address_local (dispatch_hostname))
817 strcpy (dispatch_hostname, "localhost");
819 if (dispatch->request->need_auth_info)
821 if (auth_info != NULL)
823 dispatch->request->auth_info = g_new0 (gchar *, length + 1);
824 for (i = 0; i < length; i++)
825 dispatch->request->auth_info[i] = g_strdup (auth_info[i]);
827 dispatch->backend->authentication_lock = FALSE;
828 dispatch->request->need_auth_info = FALSE;
830 else if (dispatch->request->password_state == GTK_CUPS_PASSWORD_REQUESTED || auth_info == NULL)
832 overwrite_and_free (dispatch->request->password);
833 dispatch->request->password = g_strdup (password);
834 g_free (dispatch->request->username);
835 dispatch->request->username = g_strdup (username);
836 dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
837 dispatch->backend->authentication_lock = FALSE;
843 request_password (gpointer data)
845 GtkPrintCupsDispatchWatch *dispatch = data;
846 const gchar *username;
848 gchar *prompt = NULL;
850 char hostname[HTTP_MAX_URI];
851 gchar **auth_info_required;
852 gchar **auth_info_default;
853 gchar **auth_info_display;
854 gboolean *auth_info_visible;
858 if (dispatch->backend->authentication_lock)
859 return G_SOURCE_REMOVE;
861 httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
862 if (is_address_local (hostname))
863 strcpy (hostname, "localhost");
865 if (dispatch->backend->username != NULL)
866 username = dispatch->backend->username;
868 username = cupsUser ();
870 auth_info_required = g_new0 (gchar*, length + 1);
871 auth_info_required[0] = g_strdup ("hostname");
872 auth_info_required[1] = g_strdup ("username");
873 auth_info_required[2] = g_strdup ("password");
875 auth_info_default = g_new0 (gchar*, length + 1);
876 auth_info_default[0] = g_strdup (hostname);
877 auth_info_default[1] = g_strdup (username);
879 auth_info_display = g_new0 (gchar*, length + 1);
880 auth_info_display[1] = g_strdup (_("Username:"));
881 auth_info_display[2] = g_strdup (_("Password:"));
883 auth_info_visible = g_new0 (gboolean, length + 1);
884 auth_info_visible[1] = TRUE;
886 key = g_strconcat (username, "@", hostname, NULL);
887 password = g_hash_table_lookup (dispatch->backend->auth, key);
889 if (password && dispatch->request->password_state != GTK_CUPS_PASSWORD_NOT_VALID)
892 g_print ("CUPS backend: using stored password for %s\n", key));
894 overwrite_and_free (dispatch->request->password);
895 dispatch->request->password = g_strdup (password);
896 g_free (dispatch->request->username);
897 dispatch->request->username = g_strdup (username);
898 dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
902 const char *job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
903 const char *printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
904 char *printer_name = NULL;
906 if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
907 printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
909 if (dispatch->request->password_state == GTK_CUPS_PASSWORD_NOT_VALID)
910 g_hash_table_remove (dispatch->backend->auth, key);
912 dispatch->request->password_state = GTK_CUPS_PASSWORD_REQUESTED;
914 dispatch->backend->authentication_lock = TRUE;
916 switch (dispatch->request->ipp_request->request.op.operation_id)
919 if (job_title != NULL && printer_name != NULL)
920 prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
922 prompt = g_strdup_printf ( _("Authentication is required to print a document on %s"), hostname);
924 case IPP_GET_JOB_ATTRIBUTES:
925 if (job_title != NULL)
926 prompt = g_strdup_printf ( _("Authentication is required to get attributes of job '%s'"), job_title);
928 prompt = g_strdup ( _("Authentication is required to get attributes of a job"));
930 case IPP_GET_PRINTER_ATTRIBUTES:
931 if (printer_name != NULL)
932 prompt = g_strdup_printf ( _("Authentication is required to get attributes of printer %s"), printer_name);
934 prompt = g_strdup ( _("Authentication is required to get attributes of a printer"));
936 case CUPS_GET_DEFAULT:
937 prompt = g_strdup_printf ( _("Authentication is required to get default printer of %s"), hostname);
939 case CUPS_GET_PRINTERS:
940 prompt = g_strdup_printf ( _("Authentication is required to get printers from %s"), hostname);
943 /* work around gcc warning about 0 not being a value for this enum */
944 if (dispatch->request->ipp_request->request.op.operation_id == 0)
945 prompt = g_strdup_printf ( _("Authentication is required to get a file from %s"), hostname);
947 prompt = g_strdup_printf ( _("Authentication is required on %s"), hostname);
951 g_free (printer_name);
953 g_signal_emit_by_name (dispatch->backend, "request-password",
954 auth_info_required, auth_info_default, auth_info_display, auth_info_visible, prompt);
959 for (i = 0; i < length; i++)
961 g_free (auth_info_required[i]);
962 g_free (auth_info_default[i]);
963 g_free (auth_info_display[i]);
966 g_free (auth_info_required);
967 g_free (auth_info_default);
968 g_free (auth_info_display);
969 g_free (auth_info_visible);
972 return G_SOURCE_REMOVE;
976 cups_dispatch_add_poll (GSource *source)
978 GtkPrintCupsDispatchWatch *dispatch;
979 GtkCupsPollState poll_state;
981 dispatch = (GtkPrintCupsDispatchWatch *) source;
983 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
985 /* Remove the old source if the poll state changed. */
986 if (poll_state != dispatch->poll_state && dispatch->data_poll != NULL)
988 g_source_remove_poll (source, dispatch->data_poll);
989 g_free (dispatch->data_poll);
990 dispatch->data_poll = NULL;
993 if (dispatch->request->http != NULL)
995 if (dispatch->data_poll == NULL)
997 dispatch->data_poll = g_new0 (GPollFD, 1);
998 dispatch->poll_state = poll_state;
1000 if (poll_state == GTK_CUPS_HTTP_READ)
1001 dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
1002 else if (poll_state == GTK_CUPS_HTTP_WRITE)
1003 dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
1005 dispatch->data_poll->events = 0;
1007 #ifdef HAVE_CUPS_API_1_2
1008 dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
1010 dispatch->data_poll->fd = dispatch->request->http->fd;
1012 g_source_add_poll (source, dispatch->data_poll);
1018 check_auth_info (gpointer user_data)
1020 GtkPrintCupsDispatchWatch *dispatch;
1021 dispatch = (GtkPrintCupsDispatchWatch *) user_data;
1023 if (!dispatch->request->need_auth_info)
1025 if (dispatch->request->auth_info == NULL)
1027 dispatch->callback (GTK_PRINT_BACKEND (dispatch->backend),
1028 gtk_cups_request_get_result (dispatch->request),
1029 dispatch->callback_data);
1030 g_source_destroy ((GSource *) dispatch);
1037 length = g_strv_length (dispatch->request->auth_info_required);
1039 gtk_cups_request_ipp_add_strings (dispatch->request,
1045 (const char * const *) dispatch->request->auth_info);
1047 g_source_attach ((GSource *) dispatch, NULL);
1048 g_source_unref ((GSource *) dispatch);
1050 for (i = 0; i < length; i++)
1051 overwrite_and_free (dispatch->request->auth_info[i]);
1052 g_free (dispatch->request->auth_info);
1053 dispatch->request->auth_info = NULL;
1056 return G_SOURCE_REMOVE;
1059 return G_SOURCE_CONTINUE;
1063 request_auth_info (gpointer user_data)
1065 GtkPrintCupsDispatchWatch *dispatch;
1066 const char *job_title;
1067 const char *printer_uri;
1068 gchar *prompt = NULL;
1069 char *printer_name = NULL;
1072 gboolean *auth_info_visible = NULL;
1073 gchar **auth_info_default = NULL;
1074 gchar **auth_info_display = NULL;
1076 dispatch = (GtkPrintCupsDispatchWatch *) user_data;
1078 if (dispatch->backend->authentication_lock)
1081 job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
1082 printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
1083 length = g_strv_length (dispatch->request->auth_info_required);
1085 auth_info_visible = g_new0 (gboolean, length);
1086 auth_info_default = g_new0 (gchar *, length + 1);
1087 auth_info_display = g_new0 (gchar *, length + 1);
1089 for (i = 0; i < length; i++)
1091 if (g_strcmp0 (dispatch->request->auth_info_required[i], "domain") == 0)
1093 auth_info_display[i] = g_strdup (_("Domain:"));
1094 auth_info_default[i] = g_strdup ("WORKGROUP");
1095 auth_info_visible[i] = TRUE;
1097 else if (g_strcmp0 (dispatch->request->auth_info_required[i], "username") == 0)
1099 auth_info_display[i] = g_strdup (_("Username:"));
1100 if (dispatch->backend->username != NULL)
1101 auth_info_default[i] = g_strdup (dispatch->backend->username);
1103 auth_info_default[i] = g_strdup (cupsUser ());
1104 auth_info_visible[i] = TRUE;
1106 else if (g_strcmp0 (dispatch->request->auth_info_required[i], "password") == 0)
1108 auth_info_display[i] = g_strdup (_("Password:"));
1109 auth_info_visible[i] = FALSE;
1113 if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
1114 printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
1116 dispatch->backend->authentication_lock = TRUE;
1118 if (job_title != NULL)
1120 if (printer_name != NULL)
1121 prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
1123 prompt = g_strdup_printf ( _("Authentication is required to print document '%s'"), job_title);
1127 if (printer_name != NULL)
1128 prompt = g_strdup_printf ( _("Authentication is required to print this document on printer %s"), printer_name);
1130 prompt = g_strdup ( _("Authentication is required to print this document"));
1133 g_signal_emit_by_name (dispatch->backend, "request-password",
1134 dispatch->request->auth_info_required,
1140 for (i = 0; i < length; i++)
1142 g_free (auth_info_default[i]);
1143 g_free (auth_info_display[i]);
1146 g_free (auth_info_default);
1147 g_free (auth_info_display);
1148 g_free (printer_name);
1151 g_idle_add (check_auth_info, user_data);
1157 cups_dispatch_watch_check (GSource *source)
1159 GtkPrintCupsDispatchWatch *dispatch;
1160 GtkCupsPollState poll_state;
1164 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1166 dispatch = (GtkPrintCupsDispatchWatch *) source;
1168 poll_state = gtk_cups_request_get_poll_state (dispatch->request);
1170 if (poll_state != GTK_CUPS_HTTP_IDLE && !dispatch->request->need_password)
1171 if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
1174 result = gtk_cups_request_read_write (dispatch->request, FALSE);
1175 if (result && dispatch->data_poll != NULL)
1177 g_source_remove_poll (source, dispatch->data_poll);
1178 g_free (dispatch->data_poll);
1179 dispatch->data_poll = NULL;
1182 if (dispatch->request->need_password && dispatch->request->password_state != GTK_CUPS_PASSWORD_REQUESTED)
1184 dispatch->request->need_password = FALSE;
1185 g_idle_add (request_password, dispatch);
1193 cups_dispatch_watch_prepare (GSource *source,
1196 GtkPrintCupsDispatchWatch *dispatch;
1199 dispatch = (GtkPrintCupsDispatchWatch *) source;
1202 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1206 result = gtk_cups_request_read_write (dispatch->request, TRUE);
1208 cups_dispatch_add_poll (source);
1214 cups_dispatch_watch_dispatch (GSource *source,
1215 GSourceFunc callback,
1218 GtkPrintCupsDispatchWatch *dispatch;
1219 GtkPrintCupsResponseCallbackFunc ep_callback;
1220 GtkCupsResult *result;
1222 g_assert (callback != NULL);
1224 ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
1226 dispatch = (GtkPrintCupsDispatchWatch *) source;
1228 result = gtk_cups_request_get_result (dispatch->request);
1231 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1233 if (gtk_cups_result_is_error (result))
1236 g_print("Error result: %s (type %i, status %i, code %i)\n",
1237 gtk_cups_result_get_error_string (result),
1238 gtk_cups_result_get_error_type (result),
1239 gtk_cups_result_get_error_status (result),
1240 gtk_cups_result_get_error_code (result)));
1243 ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
1249 cups_dispatch_watch_finalize (GSource *source)
1251 GtkPrintCupsDispatchWatch *dispatch;
1252 GtkCupsResult *result;
1255 g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
1257 dispatch = (GtkPrintCupsDispatchWatch *) source;
1259 result = gtk_cups_request_get_result (dispatch->request);
1260 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH)
1262 const gchar *username;
1263 gchar hostname[HTTP_MAX_URI];
1266 httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
1267 if (is_address_local (hostname))
1268 strcpy (hostname, "localhost");
1270 if (dispatch->backend->username != NULL)
1271 username = dispatch->backend->username;
1273 username = cupsUser ();
1275 key = g_strconcat (username, "@", hostname, NULL);
1277 g_print ("CUPS backend: removing stored password for %s\n", key));
1278 g_hash_table_remove (dispatch->backend->auth, key);
1281 if (dispatch->backend)
1282 dispatch->backend->authentication_lock = FALSE;
1285 gtk_cups_request_free (dispatch->request);
1287 if (dispatch->backend)
1289 /* We need to unref this at idle time, because it might be the
1290 * last reference to this module causing the code to be
1291 * unloaded (including this particular function!)
1292 * Update: Doing this at idle caused a deadlock taking the
1293 * mainloop context lock while being in a GSource callout for
1294 * multithreaded apps. So, for now we just disable unloading
1295 * of print backends. See _gtk_print_backend_create for the
1299 dispatch->backend->requests = g_list_remove (dispatch->backend->requests, dispatch);
1302 g_object_unref (dispatch->backend);
1303 dispatch->backend = NULL;
1306 if (dispatch->data_poll)
1308 g_source_remove_poll (source, dispatch->data_poll);
1309 g_free (dispatch->data_poll);
1310 dispatch->data_poll = NULL;
1314 static GSourceFuncs _cups_dispatch_watch_funcs = {
1315 cups_dispatch_watch_prepare,
1316 cups_dispatch_watch_check,
1317 cups_dispatch_watch_dispatch,
1318 cups_dispatch_watch_finalize
1323 cups_request_execute (GtkPrintBackendCups *print_backend,
1324 GtkCupsRequest *request,
1325 GtkPrintCupsResponseCallbackFunc callback,
1327 GDestroyNotify notify)
1329 GtkPrintCupsDispatchWatch *dispatch;
1331 dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
1332 sizeof (GtkPrintCupsDispatchWatch));
1333 g_source_set_name (&dispatch->source, "GTK+ CUPS backend");
1336 g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
1338 dispatch->request = request;
1339 dispatch->backend = g_object_ref (print_backend);
1340 dispatch->poll_state = GTK_CUPS_HTTP_IDLE;
1341 dispatch->data_poll = NULL;
1342 dispatch->callback = NULL;
1343 dispatch->callback_data = NULL;
1345 print_backend->requests = g_list_prepend (print_backend->requests, dispatch);
1347 g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
1349 if (request->need_auth_info)
1351 dispatch->callback = callback;
1352 dispatch->callback_data = user_data;
1353 request_auth_info (dispatch);
1357 g_source_attach ((GSource *) dispatch, NULL);
1358 g_source_unref ((GSource *) dispatch);
1364 cups_request_printer_info_cb (GtkPrintBackendCups *backend,
1365 GtkCupsResult *result,
1368 ipp_attribute_t *attr;
1370 gchar *printer_name;
1371 GtkPrinterCups *cups_printer;
1372 GtkPrinter *printer;
1377 gboolean status_changed;
1379 g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
1381 printer_name = (gchar *)user_data;
1382 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
1386 g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
1391 g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
1395 cups_printer = GTK_PRINTER_CUPS (printer);
1397 if (gtk_cups_result_is_error (result))
1399 if (gtk_printer_is_new (printer))
1401 gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
1406 return; /* TODO: mark as inactive printer */
1409 response = gtk_cups_result_get_response (result);
1411 /* TODO: determine printer type and use correct icon */
1412 gtk_printer_set_icon_name (printer, "printer");
1418 for (attr = response->attrs; attr != NULL; attr = attr->next)
1423 _CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
1424 _CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
1425 _CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
1426 _CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
1427 _CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
1430 status_changed = gtk_printer_set_job_count (printer, job_count);
1432 status_changed |= gtk_printer_set_location (printer, loc);
1433 status_changed |= gtk_printer_set_description (printer, desc);
1434 status_changed |= gtk_printer_set_state_message (printer, state_msg);
1437 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
1438 "printer-status-changed", printer);
1442 cups_request_printer_info (GtkPrintBackendCups *print_backend,
1443 const gchar *printer_name)
1445 GtkCupsRequest *request;
1447 static const char * const pattrs[] = /* Attributes we're interested in */
1451 "printer-state-message",
1454 "job-sheets-supported",
1455 "job-sheets-default"
1458 request = gtk_cups_request_new_with_username (NULL,
1460 IPP_GET_PRINTER_ATTRIBUTES,
1464 print_backend->username);
1466 printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
1468 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
1469 "printer-uri", NULL, printer_uri);
1472 g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
1474 g_free (printer_uri);
1476 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1477 "requested-attributes", G_N_ELEMENTS (pattrs),
1480 cups_request_execute (print_backend,
1482 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
1483 g_strdup (printer_name),
1484 (GDestroyNotify) g_free);
1489 GtkPrintBackendCups *print_backend;
1496 job_object_died (gpointer user_data,
1497 GObject *where_the_object_was)
1499 CupsJobPollData *data = user_data;
1504 cups_job_poll_data_free (CupsJobPollData *data)
1507 g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
1513 cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
1514 GtkCupsResult *result,
1517 CupsJobPollData *data = user_data;
1518 ipp_attribute_t *attr;
1523 GDK_THREADS_ENTER ();
1525 if (data->job == NULL)
1527 cups_job_poll_data_free (data);
1533 response = gtk_cups_result_get_response (result);
1536 for (attr = response->attrs; attr != NULL; attr = attr->next)
1541 _CUPS_MAP_ATTR_INT (attr, state, "job-state");
1547 case IPP_JOB_PENDING:
1549 case IPP_JOB_STOPPED:
1550 gtk_print_job_set_status (data->job,
1551 GTK_PRINT_STATUS_PENDING);
1553 case IPP_JOB_PROCESSING:
1554 gtk_print_job_set_status (data->job,
1555 GTK_PRINT_STATUS_PRINTING);
1558 case IPP_JOB_CANCELLED:
1559 case IPP_JOB_ABORTED:
1560 gtk_print_job_set_status (data->job,
1561 GTK_PRINT_STATUS_FINISHED_ABORTED);
1565 case IPP_JOB_COMPLETED:
1566 gtk_print_job_set_status (data->job,
1567 GTK_PRINT_STATUS_FINISHED);
1572 if (!done && data->job != NULL)
1576 if (data->counter < 5)
1578 else if (data->counter < 10)
1583 g_timeout_add (timeout, cups_job_info_poll_timeout, data);
1586 cups_job_poll_data_free (data);
1589 GDK_THREADS_LEAVE ();
1593 cups_request_job_info (CupsJobPollData *data)
1595 GtkCupsRequest *request;
1598 request = gtk_cups_request_new_with_username (NULL,
1600 IPP_GET_JOB_ATTRIBUTES,
1604 data->print_backend->username);
1606 job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
1607 gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
1608 "job-uri", NULL, job_uri);
1611 cups_request_execute (data->print_backend,
1613 (GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
1619 cups_job_info_poll_timeout (gpointer user_data)
1621 CupsJobPollData *data = user_data;
1623 if (data->job == NULL)
1624 cups_job_poll_data_free (data);
1626 cups_request_job_info (data);
1628 return G_SOURCE_REMOVE;
1632 cups_begin_polling_info (GtkPrintBackendCups *print_backend,
1636 CupsJobPollData *data;
1638 data = g_new0 (CupsJobPollData, 1);
1640 data->print_backend = print_backend;
1642 data->job_id = job_id;
1645 g_object_weak_ref (G_OBJECT (job), job_object_died, data);
1647 cups_request_job_info (data);
1651 mark_printer_inactive (GtkPrinter *printer,
1652 GtkPrintBackend *backend)
1654 gtk_printer_set_is_active (printer, FALSE);
1655 g_signal_emit_by_name (backend, "printer-removed", printer);
1659 find_printer (GtkPrinter *printer,
1660 const gchar *find_name)
1662 const gchar *printer_name;
1664 printer_name = gtk_printer_get_name (printer);
1665 return g_ascii_strcasecmp (printer_name, find_name);
1669 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
1670 GtkCupsResult *result,
1673 GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
1674 ipp_attribute_t *attr;
1676 gboolean list_has_changed;
1677 GList *removed_printer_checklist;
1678 gchar *remote_default_printer = NULL;
1680 GDK_THREADS_ENTER ();
1682 list_has_changed = FALSE;
1685 g_print ("CUPS Backend: %s\n", G_STRFUNC));
1687 cups_backend->list_printers_pending = FALSE;
1689 if (gtk_cups_result_is_error (result))
1692 g_warning ("CUPS Backend: Error getting printer list: %s %d %d",
1693 gtk_cups_result_get_error_string (result),
1694 gtk_cups_result_get_error_type (result),
1695 gtk_cups_result_get_error_code (result)));
1697 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
1698 gtk_cups_result_get_error_code (result) == 1)
1700 /* Canceled by user, stop popping up more password dialogs */
1701 if (cups_backend->list_printers_poll > 0)
1702 g_source_remove (cups_backend->list_printers_poll);
1703 cups_backend->list_printers_poll = 0;
1704 cups_backend->list_printers_attempts = 0;
1710 /* Gather the names of the printers in the current queue
1711 * so we may check to see if they were removed
1713 removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
1715 response = gtk_cups_result_get_response (result);
1717 for (attr = response->attrs; attr != NULL; attr = attr->next)
1719 GtkPrinter *printer;
1720 const gchar *printer_name = NULL;
1721 const gchar *printer_uri = NULL;
1722 const gchar *member_uris = NULL;
1723 const gchar *location = NULL;
1724 const gchar *description = NULL;
1725 const gchar *state_msg = NULL;
1728 gboolean status_changed = FALSE;
1731 const gchar *reason_msg = NULL;
1732 gchar *reason_msg_desc = NULL;
1733 gchar *tmp_msg = NULL;
1734 gchar *tmp_msg2 = NULL;
1735 gint printer_state_reason_level = 0; /* 0 - none, 1 - report, 2 - warning, 3 - error */
1736 gboolean interested_in = FALSE;
1737 gboolean found = FALSE;
1738 static const char * const reasons[] = /* Reasons we're interested in */
1744 "marker-supply-low",
1745 "marker-supply-empty",
1753 static const char * reasons_descs[] =
1755 N_("Printer '%s' is low on toner."),
1756 N_("Printer '%s' has no toner left."),
1757 /* Translators: "Developer" like on photo development context */
1758 N_("Printer '%s' is low on developer."),
1759 /* Translators: "Developer" like on photo development context */
1760 N_("Printer '%s' is out of developer."),
1761 /* Translators: "marker" is one color bin of the printer */
1762 N_("Printer '%s' is low on at least one marker supply."),
1763 /* Translators: "marker" is one color bin of the printer */
1764 N_("Printer '%s' is out of at least one marker supply."),
1765 N_("The cover is open on printer '%s'."),
1766 N_("The door is open on printer '%s'."),
1767 N_("Printer '%s' is low on paper."),
1768 N_("Printer '%s' is out of paper."),
1769 N_("Printer '%s' is currently offline."),
1770 N_("There is a problem on printer '%s'.")
1772 gboolean is_paused = FALSE;
1773 gboolean is_accepting_jobs = TRUE;
1774 gboolean default_printer = FALSE;
1775 gboolean got_printer_type = FALSE;
1776 gchar *default_cover_before = NULL;
1777 gchar *default_cover_after = NULL;
1778 gboolean remote_printer = FALSE;
1779 gchar **auth_info_required = NULL;
1781 /* Skip leading attributes until we hit a printer...
1783 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1789 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
1791 if (strcmp (attr->name, "printer-name") == 0 &&
1792 attr->value_tag == IPP_TAG_NAME)
1793 printer_name = attr->values[0].string.text;
1794 else if (strcmp (attr->name, "printer-uri-supported") == 0 &&
1795 attr->value_tag == IPP_TAG_URI)
1796 printer_uri = attr->values[0].string.text;
1797 else if (strcmp (attr->name, "member-uris") == 0 &&
1798 attr->value_tag == IPP_TAG_URI)
1799 member_uris = attr->values[0].string.text;
1800 else if (strcmp (attr->name, "printer-location") == 0)
1801 location = attr->values[0].string.text;
1802 else if (strcmp (attr->name, "printer-info") == 0)
1803 description = attr->values[0].string.text;
1804 else if (strcmp (attr->name, "printer-state-message") == 0)
1805 state_msg = attr->values[0].string.text;
1806 else if (strcmp (attr->name, "printer-state-reasons") == 0)
1807 /* Store most important reason to reason_msg and set
1808 its importance at printer_state_reason_level */
1810 for (i = 0; i < attr->num_values; i++)
1812 if (strcmp (attr->values[i].string.text, "none") != 0)
1814 /* Sets is_paused flag for paused printer. */
1815 if (strcmp (attr->values[i].string.text, "paused") == 0)
1820 interested_in = FALSE;
1821 for (j = 0; j < G_N_ELEMENTS (reasons); j++)
1822 if (strncmp (attr->values[i].string.text, reasons[j], strlen (reasons[j])) == 0)
1824 interested_in = TRUE;
1830 if (g_str_has_suffix (attr->values[i].string.text, "-report"))
1832 if (printer_state_reason_level <= 1)
1834 reason_msg = attr->values[i].string.text;
1835 printer_state_reason_level = 1;
1838 else if (g_str_has_suffix (attr->values[i].string.text, "-warning"))
1840 if (printer_state_reason_level <= 2)
1842 reason_msg = attr->values[i].string.text;
1843 printer_state_reason_level = 2;
1846 else /* It is error in the case of no suffix. */
1848 reason_msg = attr->values[i].string.text;
1849 printer_state_reason_level = 3;
1855 else if (strcmp (attr->name, "printer-state") == 0)
1856 state = attr->values[0].integer;
1857 else if (strcmp (attr->name, "queued-job-count") == 0)
1858 job_count = attr->values[0].integer;
1859 else if (strcmp (attr->name, "printer-is-accepting-jobs") == 0)
1861 if (attr->values[0].boolean == 1)
1862 is_accepting_jobs = TRUE;
1864 is_accepting_jobs = FALSE;
1866 else if (strcmp (attr->name, "job-sheets-supported") == 0)
1868 if (cups_backend->covers == NULL)
1870 cups_backend->number_of_covers = attr->num_values;
1871 cups_backend->covers = g_new (char *, cups_backend->number_of_covers + 1);
1872 for (i = 0; i < cups_backend->number_of_covers; i++)
1873 cups_backend->covers[i] = g_strdup (attr->values[i].string.text);
1874 cups_backend->covers[cups_backend->number_of_covers] = NULL;
1877 else if (strcmp (attr->name, "job-sheets-default") == 0)
1879 if (attr->num_values == 2)
1881 default_cover_before = attr->values[0].string.text;
1882 default_cover_after = attr->values[1].string.text;
1885 else if (strcmp (attr->name, "printer-type") == 0)
1887 got_printer_type = TRUE;
1888 if (attr->values[0].integer & 0x00020000)
1889 default_printer = TRUE;
1891 default_printer = FALSE;
1893 if (attr->values[0].integer & 0x00000002)
1894 remote_printer = TRUE;
1896 remote_printer = FALSE;
1898 else if (strcmp (attr->name, "auth-info-required") == 0)
1900 if (strcmp (attr->values[0].string.text, "none") != 0)
1902 auth_info_required = g_new0 (gchar *, attr->num_values + 1);
1903 for (i = 0; i < attr->num_values; i++)
1904 auth_info_required[i] = g_strdup (attr->values[i].string.text);
1910 g_print ("CUPS Backend: Attribute %s ignored", attr->name));
1916 if (printer_name == NULL ||
1917 (printer_uri == NULL && member_uris == NULL))
1925 if (got_printer_type)
1927 if (default_printer && !cups_backend->got_default_printer)
1929 if (!remote_printer)
1931 cups_backend->got_default_printer = TRUE;
1932 cups_backend->default_printer = g_strdup (printer_name);
1936 if (remote_default_printer == NULL)
1937 remote_default_printer = g_strdup (printer_name);
1943 if (!cups_backend->got_default_printer)
1944 cups_get_default_printer (cups_backend);
1947 /* remove name from checklist if it was found */
1948 node = g_list_find_custom (removed_printer_checklist, printer_name, (GCompareFunc) find_printer);
1949 removed_printer_checklist = g_list_delete_link (removed_printer_checklist, node);
1951 printer = gtk_print_backend_find_printer (backend, printer_name);
1954 GtkPrinterCups *cups_printer;
1955 char uri[HTTP_MAX_URI]; /* Printer URI */
1956 char method[HTTP_MAX_URI]; /* Method/scheme name */
1957 char username[HTTP_MAX_URI]; /* Username:password */
1958 char hostname[HTTP_MAX_URI]; /* Hostname */
1959 char resource[HTTP_MAX_URI]; /* Resource name */
1960 int port; /* Port number */
1961 char *cups_server; /* CUPS server */
1963 list_has_changed = TRUE;
1965 cups_printer = gtk_printer_cups_new (printer_name,
1967 cups_backend->colord_client);
1969 cups_printer = gtk_printer_cups_new (printer_name, backend, NULL);
1972 cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
1974 /* Check to see if we are looking at a class */
1977 cups_printer->printer_uri = g_strdup (member_uris);
1978 /* TODO if member_uris is a class we need to recursivly find a printer */
1980 g_print ("CUPS Backend: Found class with printer %s\n", member_uris));
1984 cups_printer->printer_uri = g_strdup (printer_uri);
1986 g_print ("CUPS Backend: Found printer %s\n", printer_uri));
1989 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
1990 httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
1991 method, sizeof (method),
1992 username, sizeof (username),
1993 hostname, sizeof (hostname),
1995 resource, sizeof (resource));
1998 httpSeparate (cups_printer->printer_uri,
2006 if (strncmp (resource, "/printers/", 10) == 0)
2008 cups_printer->ppd_name = g_strdup (resource + 10);
2010 g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
2013 gethostname (uri, sizeof (uri));
2014 cups_server = g_strdup (cupsServer());
2016 if (strcasecmp (uri, hostname) == 0)
2017 strcpy (hostname, "localhost");
2019 /* if the cups server is local and listening at a unix domain socket
2020 * then use the socket connection
2022 if ((strstr (hostname, "localhost") != NULL) &&
2023 (cups_server[0] == '/'))
2024 strcpy (hostname, cups_server);
2026 g_free (cups_server);
2028 cups_printer->default_cover_before = g_strdup (default_cover_before);
2029 cups_printer->default_cover_after = g_strdup (default_cover_after);
2031 cups_printer->hostname = g_strdup (hostname);
2032 cups_printer->port = port;
2034 cups_printer->auth_info_required = g_strdupv (auth_info_required);
2035 g_strfreev (auth_info_required);
2037 printer = GTK_PRINTER (cups_printer);
2039 if (cups_backend->default_printer != NULL &&
2040 strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
2041 gtk_printer_set_is_default (printer, TRUE);
2044 gtk_print_backend_add_printer (backend, printer);
2047 g_object_ref (printer);
2049 GTK_PRINTER_CUPS (printer)->remote = remote_printer;
2051 gtk_printer_set_is_paused (printer, is_paused);
2052 gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
2054 if (!gtk_printer_is_active (printer))
2056 gtk_printer_set_is_active (printer, TRUE);
2057 gtk_printer_set_is_new (printer, TRUE);
2058 list_has_changed = TRUE;
2061 if (gtk_printer_is_new (printer))
2063 g_signal_emit_by_name (backend, "printer-added", printer);
2065 gtk_printer_set_is_new (printer, FALSE);
2069 /* Getting printer info with separate requests overwhelms cups
2070 * when the printer list has more than a handful of printers.
2072 cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
2075 GTK_PRINTER_CUPS (printer)->state = state;
2076 status_changed = gtk_printer_set_job_count (printer, job_count);
2077 status_changed |= gtk_printer_set_location (printer, location);
2078 status_changed |= gtk_printer_set_description (printer, description);
2080 if (state_msg != NULL && strlen (state_msg) == 0)
2082 if (is_paused && !is_accepting_jobs)
2083 /* Translators: this is a printer status. */
2084 tmp_msg2 = g_strdup ( N_("Paused ; Rejecting Jobs"));
2085 if (is_paused && is_accepting_jobs)
2086 /* Translators: this is a printer status. */
2087 tmp_msg2 = g_strdup ( N_("Paused"));
2088 if (!is_paused && !is_accepting_jobs)
2089 /* Translators: this is a printer status. */
2090 tmp_msg2 = g_strdup ( N_("Rejecting Jobs"));
2092 if (tmp_msg2 != NULL)
2093 state_msg = tmp_msg2;
2096 /* Set description of the reason and combine it with printer-state-message. */
2097 if ( (reason_msg != NULL))
2099 for (i = 0; i < G_N_ELEMENTS (reasons); i++)
2101 if (strncmp (reason_msg, reasons[i], strlen (reasons[i])) == 0)
2103 reason_msg_desc = g_strdup_printf (reasons_descs[i], printer_name);
2110 printer_state_reason_level = 0;
2112 if (printer_state_reason_level >= 2)
2114 if (strlen (state_msg) == 0)
2115 state_msg = reason_msg_desc;
2118 tmp_msg = g_strjoin (" ; ", state_msg, reason_msg_desc, NULL);
2119 state_msg = tmp_msg;
2124 status_changed |= gtk_printer_set_state_message (printer, state_msg);
2125 status_changed |= gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
2127 if (tmp_msg != NULL)
2130 if (tmp_msg2 != NULL)
2133 if (reason_msg_desc != NULL)
2134 g_free (reason_msg_desc);
2136 /* Set printer icon according to importance
2137 (none, report, warning, error - report is omitted). */
2138 if (printer_state_reason_level == 3)
2139 gtk_printer_set_icon_name (printer, "printer-error");
2140 else if (printer_state_reason_level == 2)
2141 gtk_printer_set_icon_name (printer, "printer-warning");
2142 else if (gtk_printer_is_paused (printer))
2143 gtk_printer_set_icon_name (printer, "printer-paused");
2145 gtk_printer_set_icon_name (printer, "printer");
2148 g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
2149 "printer-status-changed", printer);
2151 /* The ref is held by GtkPrintBackend, in add_printer() */
2152 g_object_unref (printer);
2158 /* look at the removed printers checklist and mark any printer
2159 as inactive if it is in the list, emitting a printer_removed signal */
2160 if (removed_printer_checklist != NULL)
2162 g_list_free_full (removed_printer_checklist, (GDestroyNotify) mark_printer_inactive);
2163 list_has_changed = TRUE;
2167 if (list_has_changed)
2168 g_signal_emit_by_name (backend, "printer-list-changed");
2170 gtk_print_backend_set_list_done (backend);
2172 if (!cups_backend->got_default_printer && remote_default_printer != NULL)
2174 cups_backend->default_printer = g_strdup (remote_default_printer);
2175 cups_backend->got_default_printer = TRUE;
2176 g_free (remote_default_printer);
2178 if (cups_backend->default_printer != NULL)
2180 GtkPrinter *default_printer = NULL;
2181 default_printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (cups_backend),
2182 cups_backend->default_printer);
2183 if (default_printer != NULL)
2185 gtk_printer_set_is_default (default_printer, TRUE);
2186 g_signal_emit_by_name (GTK_PRINT_BACKEND (cups_backend),
2187 "printer-status-changed", default_printer);
2192 GDK_THREADS_LEAVE ();
2196 update_backend_status (GtkPrintBackendCups *cups_backend,
2197 GtkCupsConnectionState state)
2201 case GTK_CUPS_CONNECTION_NOT_AVAILABLE:
2202 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_UNAVAILABLE, NULL);
2204 case GTK_CUPS_CONNECTION_AVAILABLE:
2205 g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_OK, NULL);
2212 cups_request_printer_list (GtkPrintBackendCups *cups_backend)
2214 GtkCupsConnectionState state;
2215 GtkCupsRequest *request;
2216 static const char * const pattrs[] = /* Attributes we're interested in */
2219 "printer-uri-supported",
2223 "printer-state-message",
2224 "printer-state-reasons",
2227 "printer-is-accepting-jobs",
2228 "job-sheets-supported",
2229 "job-sheets-default",
2231 "auth-info-required"
2234 if (cups_backend->reading_ppds > 0 || cups_backend->list_printers_pending)
2237 state = gtk_cups_connection_test_get_state (cups_backend->cups_connection_test);
2238 update_backend_status (cups_backend, state);
2240 if (cups_backend->list_printers_attempts == 60)
2242 cups_backend->list_printers_attempts = -1;
2243 if (cups_backend->list_printers_poll > 0)
2244 g_source_remove (cups_backend->list_printers_poll);
2245 cups_backend->list_printers_poll = gdk_threads_add_timeout (200,
2246 (GSourceFunc) cups_request_printer_list,
2249 else if (cups_backend->list_printers_attempts != -1)
2250 cups_backend->list_printers_attempts++;
2252 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2255 if (cups_backend->list_printers_attempts > 0)
2256 cups_backend->list_printers_attempts = 60;
2258 cups_backend->list_printers_pending = TRUE;
2260 request = gtk_cups_request_new_with_username (NULL,
2266 cups_backend->username);
2268 gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2269 "requested-attributes", G_N_ELEMENTS (pattrs),
2272 cups_request_execute (cups_backend,
2274 (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
2282 cups_get_printer_list (GtkPrintBackend *backend)
2284 GtkPrintBackendCups *cups_backend;
2286 cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
2288 if (cups_backend->cups_connection_test == NULL)
2289 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
2291 if (cups_backend->list_printers_poll == 0)
2293 if (cups_request_printer_list (cups_backend))
2294 cups_backend->list_printers_poll = gdk_threads_add_timeout (50,
2295 (GSourceFunc) cups_request_printer_list,
2301 GtkPrinterCups *printer;
2307 get_ppd_data_free (GetPPDData *data)
2310 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2311 httpClose (data->http);
2312 g_io_channel_unref (data->ppd_io);
2313 g_object_unref (data->printer);
2318 cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
2319 GtkCupsResult *result,
2322 GtkPrinter *printer;
2324 GDK_THREADS_ENTER ();
2327 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2329 printer = GTK_PRINTER (data->printer);
2330 GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
2331 print_backend->reading_ppds--;
2333 if (gtk_cups_result_is_error (result))
2335 gboolean success = FALSE;
2337 /* if we get a 404 then it is just a raw printer without a ppd
2339 if ((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
2340 (gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
2342 gtk_printer_set_has_details (printer, TRUE);
2346 g_signal_emit_by_name (printer, "details-acquired", success);
2350 /* let ppdOpenFd take over the ownership of the open file */
2351 g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
2352 data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
2354 ppdMarkDefaults (data->printer->ppd_file);
2356 gtk_printer_set_has_details (printer, TRUE);
2357 g_signal_emit_by_name (printer, "details-acquired", TRUE);
2360 GDK_THREADS_LEAVE ();
2364 cups_request_ppd (GtkPrinter *printer)
2367 GtkPrintBackend *print_backend;
2368 GtkPrinterCups *cups_printer;
2369 GtkCupsRequest *request;
2376 cups_printer = GTK_PRINTER_CUPS (printer);
2381 g_print ("CUPS Backend: %s\n", G_STRFUNC));
2383 if (cups_printer->remote)
2385 GtkCupsConnectionState state;
2387 state = gtk_cups_connection_test_get_state (cups_printer->remote_cups_connection_test);
2389 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS)
2391 if (cups_printer->get_remote_ppd_attempts == 60)
2393 cups_printer->get_remote_ppd_attempts = -1;
2394 if (cups_printer->get_remote_ppd_poll > 0)
2395 g_source_remove (cups_printer->get_remote_ppd_poll);
2396 cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (200,
2397 (GSourceFunc) cups_request_ppd,
2400 else if (cups_printer->get_remote_ppd_attempts != -1)
2401 cups_printer->get_remote_ppd_attempts++;
2406 gtk_cups_connection_test_free (cups_printer->remote_cups_connection_test);
2407 cups_printer->remote_cups_connection_test = NULL;
2408 cups_printer->get_remote_ppd_poll = 0;
2409 cups_printer->get_remote_ppd_attempts = 0;
2411 if (state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2413 g_signal_emit_by_name (printer, "details-acquired", FALSE);
2418 http = httpConnectEncrypt (cups_printer->hostname,
2422 data = g_new0 (GetPPDData, 1);
2424 fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
2428 #ifdef G_ENABLE_DEBUG
2429 /* If we are debugging printing don't delete the tmp files */
2430 if (!(gtk_get_debug_flags () & GTK_DEBUG_PRINTING))
2431 unlink (ppd_filename);
2433 unlink (ppd_filename);
2434 #endif /* G_ENABLE_DEBUG */
2439 g_warning ("CUPS Backend: Failed to create temp file, %s\n",
2441 g_error_free (error);
2443 g_free (ppd_filename);
2446 g_signal_emit_by_name (printer, "details-acquired", FALSE);
2451 fchmod (fd, S_IRUSR | S_IWUSR);
2452 data->ppd_io = g_io_channel_unix_new (fd);
2453 g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
2454 g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
2456 data->printer = g_object_ref (printer);
2458 resource = g_strdup_printf ("/printers/%s.ppd",
2459 gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
2461 print_backend = gtk_printer_get_backend (printer);
2463 request = gtk_cups_request_new_with_username (data->http,
2467 cups_printer->hostname,
2469 GTK_PRINT_BACKEND_CUPS (print_backend)->username);
2472 g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
2475 cups_printer->reading_ppd = TRUE;
2476 GTK_PRINT_BACKEND_CUPS (print_backend)->reading_ppds++;
2478 cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
2480 (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
2482 (GDestroyNotify)get_ppd_data_free);
2485 g_free (ppd_filename);
2490 /* Ordering matters for default preference */
2491 static const char *lpoptions_locations[] = {
2492 "/etc/cups/lpoptions",
2498 cups_parse_user_default_printer (const char *filename,
2499 char **printer_name)
2502 char line[1024], *lineptr, *defname = NULL;
2504 if ((fp = g_fopen (filename, "r")) == NULL)
2507 while (fgets (line, sizeof (line), fp) != NULL)
2509 if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
2513 while (isspace (*lineptr))
2520 while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
2525 if (*printer_name != NULL)
2526 g_free (*printer_name);
2528 *printer_name = g_strdup (defname);
2535 cups_get_user_default_printer (char **printer_name)
2539 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
2541 if (g_path_is_absolute (lpoptions_locations[i]))
2543 cups_parse_user_default_printer (lpoptions_locations[i],
2550 filename = g_build_filename (g_get_home_dir (),
2551 lpoptions_locations[i], NULL);
2552 cups_parse_user_default_printer (filename, printer_name);
2559 cups_parse_user_options (const char *filename,
2560 const char *printer_name,
2562 cups_option_t **options)
2565 gchar line[1024], *lineptr, *name;
2567 if ((fp = g_fopen (filename, "r")) == NULL)
2570 while (fgets (line, sizeof (line), fp) != NULL)
2572 if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
2574 else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
2579 /* Skip leading whitespace */
2580 while (isspace (*lineptr))
2586 /* NUL-terminate the name, stripping the instance name */
2588 while (!isspace (*lineptr) && *lineptr)
2590 if (*lineptr == '/')
2600 if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
2603 /* We found our printer, parse the options */
2604 num_options = cupsParseOptions (lineptr, num_options, options);
2613 cups_get_user_options (const char *printer_name,
2615 cups_option_t **options)
2619 for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
2621 if (g_path_is_absolute (lpoptions_locations[i]))
2623 num_options = cups_parse_user_options (lpoptions_locations[i],
2632 filename = g_build_filename (g_get_home_dir (),
2633 lpoptions_locations[i], NULL);
2634 num_options = cups_parse_user_options (filename, printer_name,
2635 num_options, options);
2643 /* This function requests default printer from a CUPS server in regular intervals.
2644 * In the case of unreachable CUPS server the request is repeated later.
2645 * The default printer is not requested in the case of previous success.
2648 cups_get_default_printer (GtkPrintBackendCups *backend)
2650 GtkPrintBackendCups *cups_backend;
2652 cups_backend = backend;
2654 if (cups_backend->cups_connection_test == NULL)
2655 cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL);
2657 if (cups_backend->default_printer_poll == 0)
2659 if (cups_request_default_printer (cups_backend))
2660 cups_backend->default_printer_poll = gdk_threads_add_timeout (200,
2661 (GSourceFunc) cups_request_default_printer,
2666 /* This function gets default printer from local settings.*/
2668 cups_get_local_default_printer (GtkPrintBackendCups *backend)
2673 if ((str = g_getenv ("LPDEST")) != NULL)
2675 backend->default_printer = g_strdup (str);
2676 backend->got_default_printer = TRUE;
2679 else if ((str = g_getenv ("PRINTER")) != NULL &&
2680 strcmp (str, "lp") != 0)
2682 backend->default_printer = g_strdup (str);
2683 backend->got_default_printer = TRUE;
2687 /* Figure out user setting for default printer */
2688 cups_get_user_default_printer (&name);
2691 backend->default_printer = name;
2692 backend->got_default_printer = TRUE;
2698 cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
2699 GtkCupsResult *result,
2703 ipp_attribute_t *attr;
2704 GtkPrinter *printer;
2706 GDK_THREADS_ENTER ();
2708 if (gtk_cups_result_is_error (result))
2710 if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
2711 gtk_cups_result_get_error_code (result) == 1)
2713 /* Canceled by user, stop popping up more password dialogs */
2714 if (print_backend->list_printers_poll > 0)
2715 g_source_remove (print_backend->list_printers_poll);
2716 print_backend->list_printers_poll = 0;
2722 response = gtk_cups_result_get_response (result);
2724 if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
2725 print_backend->default_printer = g_strdup (attr->values[0].string.text);
2727 print_backend->got_default_printer = TRUE;
2729 if (print_backend->default_printer != NULL)
2731 printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (print_backend), print_backend->default_printer);
2732 if (printer != NULL)
2734 gtk_printer_set_is_default (printer, TRUE);
2735 g_signal_emit_by_name (GTK_PRINT_BACKEND (print_backend), "printer-status-changed", printer);
2739 /* Make sure to kick off get_printers if we are polling it,
2740 * as we could have blocked this reading the default printer
2742 if (print_backend->list_printers_poll != 0)
2743 cups_request_printer_list (print_backend);
2745 GDK_THREADS_LEAVE ();
2749 cups_request_default_printer (GtkPrintBackendCups *print_backend)
2751 GtkCupsConnectionState state;
2752 GtkCupsRequest *request;
2754 state = gtk_cups_connection_test_get_state (print_backend->cups_connection_test);
2755 update_backend_status (print_backend, state);
2757 if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
2760 request = gtk_cups_request_new_with_username (NULL,
2766 print_backend->username);
2768 cups_request_execute (print_backend,
2770 (GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
2771 g_object_ref (print_backend),
2778 cups_printer_request_details (GtkPrinter *printer)
2780 GtkPrinterCups *cups_printer;
2782 cups_printer = GTK_PRINTER_CUPS (printer);
2783 if (!cups_printer->reading_ppd &&
2784 gtk_printer_cups_get_ppd (cups_printer) == NULL)
2786 if (cups_printer->remote)
2788 if (cups_printer->get_remote_ppd_poll == 0)
2790 cups_printer->remote_cups_connection_test = gtk_cups_connection_test_new (cups_printer->hostname);
2792 if (cups_request_ppd (printer))
2793 cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (50,
2794 (GSourceFunc) cups_request_ppd,
2799 cups_request_ppd (printer);
2804 ppd_text_to_utf8 (ppd_file_t *ppd_file,
2807 const char *encoding = NULL;
2810 if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
2812 return g_strdup (text);
2814 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
2816 encoding = "ISO-8859-1";
2818 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
2820 encoding = "ISO-8859-2";
2822 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
2824 encoding = "ISO-8859-5";
2826 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
2828 encoding = "SHIFT-JIS";
2830 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
2832 encoding = "MACINTOSH";
2834 else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
2836 encoding = "WINDOWS-1252";
2840 /* Fallback, try iso-8859-1... */
2841 encoding = "ISO-8859-1";
2844 res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
2849 g_warning ("CUPS Backend: Unable to convert PPD text\n"));
2850 res = g_strdup ("???");
2856 /* TODO: Add more translations for common settings here */
2858 static const struct {
2859 const char *keyword;
2860 const char *translation;
2861 } cups_option_translations[] = {
2862 { "Duplex", N_("Two Sided") },
2863 { "MediaType", N_("Paper Type") },
2864 { "InputSlot", N_("Paper Source") },
2865 { "OutputBin", N_("Output Tray") },
2866 { "Resolution", N_("Resolution") },
2867 { "PreFilter", N_("GhostScript pre-filtering") },
2871 static const struct {
2872 const char *keyword;
2874 const char *translation;
2875 } cups_choice_translations[] = {
2876 { "Duplex", "None", N_("One Sided") },
2877 /* Translators: this is an option of "Two Sided" */
2878 { "Duplex", "DuplexNoTumble", N_("Long Edge (Standard)") },
2879 /* Translators: this is an option of "Two Sided" */
2880 { "Duplex", "DuplexTumble", N_("Short Edge (Flip)") },
2881 /* Translators: this is an option of "Paper Source" */
2882 { "InputSlot", "Auto", N_("Auto Select") },
2883 /* Translators: this is an option of "Paper Source" */
2884 { "InputSlot", "AutoSelect", N_("Auto Select") },
2885 /* Translators: this is an option of "Paper Source" */
2886 { "InputSlot", "Default", N_("Printer Default") },
2887 /* Translators: this is an option of "Paper Source" */
2888 { "InputSlot", "None", N_("Printer Default") },
2889 /* Translators: this is an option of "Paper Source" */
2890 { "InputSlot", "PrinterDefault", N_("Printer Default") },
2891 /* Translators: this is an option of "Paper Source" */
2892 { "InputSlot", "Unspecified", N_("Auto Select") },
2893 /* Translators: this is an option of "Resolution" */
2894 { "Resolution", "default", N_("Printer Default") },
2895 /* Translators: this is an option of "GhostScript" */
2896 { "PreFilter", "EmbedFonts", N_("Embed GhostScript fonts only") },
2897 /* Translators: this is an option of "GhostScript" */
2898 { "PreFilter", "Level1", N_("Convert to PS level 1") },
2899 /* Translators: this is an option of "GhostScript" */
2900 { "PreFilter", "Level2", N_("Convert to PS level 2") },
2901 /* Translators: this is an option of "GhostScript" */
2902 { "PreFilter", "No", N_("No pre-filtering") },
2905 static const struct {
2907 const char *translation;
2908 } cups_group_translations[] = {
2909 /* Translators: "Miscellaneous" is the label for a button, that opens
2910 up an extra panel of settings in a print dialog. */
2911 { "Miscellaneous", N_("Miscellaneous") },
2914 static const struct {
2915 const char *ppd_keyword;
2917 } ppd_option_names[] = {
2918 {"Duplex", "gtk-duplex" },
2919 {"MediaType", "gtk-paper-type"},
2920 {"InputSlot", "gtk-paper-source"},
2921 {"OutputBin", "gtk-output-tray"},
2924 static const struct {
2925 const char *lpoption;
2927 } lpoption_names[] = {
2928 {"number-up", "gtk-n-up" },
2929 {"number-up-layout", "gtk-n-up-layout"},
2930 {"job-billing", "gtk-billing-info"},
2931 {"job-priority", "gtk-job-prio"},
2934 /* keep sorted when changing */
2935 static const char *color_option_whitelist[] = {
2936 "BRColorEnhancement",
2942 "BlackSubstitution",
2950 "RPSBlackOverPrint",
2954 /* keep sorted when changing */
2955 static const char *color_group_whitelist[] = {
2962 "HPColorOptionsPanel",
2965 /* keep sorted when changing */
2966 static const char *image_quality_option_whitelist[] = {
2968 "BRHalfTonePattern",
2978 "HPGraphicsHalftone",
2993 /* keep sorted when changing */
2994 static const char *image_quality_group_whitelist[] = {
3001 /* keep sorted when changing */
3002 static const char * finishing_option_whitelist[] = {
3015 "StapleOrientation",
3021 /* keep sorted when changing */
3022 static const char *finishing_group_whitelist[] = {
3031 /* keep sorted when changing */
3032 static const char *cups_option_blacklist[] = {
3041 get_option_text (ppd_file_t *ppd_file,
3042 ppd_option_t *option)
3047 for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
3049 if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
3050 return g_strdup (_(cups_option_translations[i].translation));
3053 utf8 = ppd_text_to_utf8 (ppd_file, option->text);
3055 /* Some ppd files have spaces in the text before the colon */
3062 get_choice_text (ppd_file_t *ppd_file,
3063 ppd_choice_t *choice)
3066 ppd_option_t *option = choice->option;
3067 const char *keyword = option->keyword;
3069 for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
3071 if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
3072 strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
3073 return g_strdup (_(cups_choice_translations[i].translation));
3075 return ppd_text_to_utf8 (ppd_file, choice->text);
3079 group_has_option (ppd_group_t *group,
3080 ppd_option_t *option)
3087 if (group->num_options > 0 &&
3088 option >= group->options && option < group->options + group->num_options)
3091 for (i = 0; i < group->num_subgroups; i++)
3093 if (group_has_option (&group->subgroups[i],option))
3100 set_option_off (GtkPrinterOption *option)
3102 /* Any of these will do, _set only applies the value
3103 * if its allowed of the option */
3104 gtk_printer_option_set (option, "False");
3105 gtk_printer_option_set (option, "Off");
3106 gtk_printer_option_set (option, "None");
3110 value_is_off (const char *value)
3112 return (strcasecmp (value, "None") == 0 ||
3113 strcasecmp (value, "Off") == 0 ||
3114 strcasecmp (value, "False") == 0);
3118 ppd_group_name (ppd_group_t *group)
3120 #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)
3128 available_choices (ppd_file_t *ppd,
3129 ppd_option_t *option,
3130 ppd_choice_t ***available,
3131 gboolean keep_if_only_one_option)
3133 ppd_option_t *other_option;
3136 ppd_const_t *constraint;
3137 const char *choice, *other_choice;
3138 ppd_option_t *option1, *option2;
3139 ppd_group_t *installed_options;
3141 gboolean all_default;
3147 conflicts = g_new0 (char, option->num_choices);
3149 installed_options = NULL;
3150 for (i = 0; i < ppd->num_groups; i++)
3154 name = ppd_group_name (&ppd->groups[i]);
3155 if (strcmp (name, "InstallableOptions") == 0)
3157 installed_options = &ppd->groups[i];
3162 for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
3164 option1 = ppdFindOption (ppd, constraint->option1);
3165 if (option1 == NULL)
3168 option2 = ppdFindOption (ppd, constraint->option2);
3169 if (option2 == NULL)
3172 if (option == option1)
3174 choice = constraint->choice1;
3175 other_option = option2;
3176 other_choice = constraint->choice2;
3178 else if (option == option2)
3180 choice = constraint->choice2;
3181 other_option = option1;
3182 other_choice = constraint->choice1;
3187 /* We only care of conflicts with installed_options and
3189 if (!group_has_option (installed_options, other_option) &&
3190 (strcmp (other_option->keyword, "PageSize") != 0))
3193 if (*other_choice == 0)
3195 /* Conflict only if the installed option is not off */
3196 if (value_is_off (other_option->defchoice))
3199 /* Conflict if the installed option has the specified default */
3200 else if (strcasecmp (other_choice, other_option->defchoice) != 0)
3205 /* Conflict with all non-off choices */
3206 for (j = 0; j < option->num_choices; j++)
3208 if (!value_is_off (option->choices[j].choice))
3214 for (j = 0; j < option->num_choices; j++)
3216 if (strcasecmp (option->choices[j].choice, choice) == 0)
3224 for (j = 0; j < option->num_choices; j++)
3228 else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
3229 all_default = FALSE;
3232 if ((all_default && !keep_if_only_one_option) ||
3233 (num_conflicts == option->num_choices))
3240 /* Some ppds don't have a "use printer default" option for
3241 * InputSlot. This means you always have to select a particular slot,
3242 * and you can't auto-pick source based on the paper size. To support
3243 * this we always add an auto option if there isn't one already. If
3244 * the user chooses the generated option we don't send any InputSlot
3245 * value when printing. The way we detect existing auto-cases is based
3246 * on feedback from Michael Sweet of cups fame.
3249 if (strcmp (option->keyword, "InputSlot") == 0)
3251 gboolean found_auto = FALSE;
3252 for (j = 0; j < option->num_choices; j++)
3256 if (strcmp (option->choices[j].choice, "Auto") == 0 ||
3257 strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
3258 strcmp (option->choices[j].choice, "Default") == 0 ||
3259 strcmp (option->choices[j].choice, "None") == 0 ||
3260 strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
3261 strcmp (option->choices[j].choice, "Unspecified") == 0 ||
3262 option->choices[j].code == NULL ||
3263 option->choices[j].code[0] == 0)
3277 *available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
3280 for (j = 0; j < option->num_choices; j++)
3283 (*available)[i++] = &option->choices[j];
3287 (*available)[i++] = NULL;
3292 return option->num_choices - num_conflicts + add_auto;
3295 static GtkPrinterOption *
3296 create_pickone_option (ppd_file_t *ppd_file,
3297 ppd_option_t *ppd_option,
3298 const gchar *gtk_name)
3300 GtkPrinterOption *option;
3301 ppd_choice_t **available;
3305 #ifdef HAVE_CUPS_API_1_2
3306 ppd_coption_t *coption;
3309 g_assert (ppd_option->ui == PPD_UI_PICKONE);
3313 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
3317 /* right now only support one parameter per custom option
3318 * if more than one print warning and only offer the default choices
3321 label = get_option_text (ppd_file, ppd_option);
3323 #ifdef HAVE_CUPS_API_1_2
3324 coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
3328 ppd_cparam_t *cparam;
3330 cparam = ppdFirstCustomParam (coption);
3332 if (ppdNextCustomParam (coption) == NULL)
3334 switch (cparam->type)
3336 case PPD_CUSTOM_INT:
3337 option = gtk_printer_option_new (gtk_name, label,
3338 GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
3340 case PPD_CUSTOM_PASSCODE:
3341 option = gtk_printer_option_new (gtk_name, label,
3342 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
3344 case PPD_CUSTOM_PASSWORD:
3345 option = gtk_printer_option_new (gtk_name, label,
3346 GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
3348 case PPD_CUSTOM_REAL:
3349 option = gtk_printer_option_new (gtk_name, label,
3350 GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
3352 case PPD_CUSTOM_STRING:
3353 option = gtk_printer_option_new (gtk_name, label,
3354 GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
3356 #ifdef PRINT_IGNORED_OPTIONS
3357 case PPD_CUSTOM_POINTS:
3358 g_warning ("CUPS Backend: PPD Custom Points Option not supported");
3360 case PPD_CUSTOM_CURVE:
3361 g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
3363 case PPD_CUSTOM_INVCURVE:
3364 g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
3371 #ifdef PRINT_IGNORED_OPTIONS
3373 g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
3376 #endif /* HAVE_CUPS_API_1_2 */
3379 option = gtk_printer_option_new (gtk_name, label,
3380 GTK_PRINTER_OPTION_TYPE_PICKONE);
3383 gtk_printer_option_allocate_choices (option, n_choices);
3384 for (i = 0; i < n_choices; i++)
3386 if (available[i] == NULL)
3388 /* This was auto-added */
3389 option->choices[i] = g_strdup ("gtk-ignore-value");
3390 option->choices_display[i] = g_strdup (_("Printer Default"));
3394 option->choices[i] = g_strdup (available[i]->choice);
3395 option->choices_display[i] = get_choice_text (ppd_file, available[i]);
3399 if (option->type != GTK_PRINTER_OPTION_TYPE_PICKONE)
3401 if (g_str_has_prefix (ppd_option->defchoice, "Custom."))
3402 gtk_printer_option_set (option, ppd_option->defchoice + 7);
3404 gtk_printer_option_set (option, ppd_option->defchoice);
3408 gtk_printer_option_set (option, ppd_option->defchoice);
3411 #ifdef PRINT_IGNORED_OPTIONS
3413 g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
3420 static GtkPrinterOption *
3421 create_boolean_option (ppd_file_t *ppd_file,
3422 ppd_option_t *ppd_option,
3423 const gchar *gtk_name)
3425 GtkPrinterOption *option;
3426 ppd_choice_t **available;
3430 g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
3434 n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
3437 label = get_option_text (ppd_file, ppd_option);
3438 option = gtk_printer_option_new (gtk_name, label,
3439 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
3442 gtk_printer_option_allocate_choices (option, 2);
3443 option->choices[0] = g_strdup ("True");
3444 option->choices_display[0] = g_strdup ("True");
3445 option->choices[1] = g_strdup ("False");
3446 option->choices_display[1] = g_strdup ("False");
3448 gtk_printer_option_set (option, ppd_option->defchoice);
3450 #ifdef PRINT_IGNORED_OPTIONS
3452 g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
3460 get_ppd_option_name (const gchar *keyword)
3464 for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
3465 if (strcmp (ppd_option_names[i].ppd_keyword, keyword) == 0)
3466 return g_strdup (ppd_option_names[i].name);
3468 return g_strdup_printf ("cups-%s", keyword);
3472 get_lpoption_name (const gchar *lpoption)
3476 for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
3477 if (strcmp (ppd_option_names[i].ppd_keyword, lpoption) == 0)
3478 return g_strdup (ppd_option_names[i].name);
3480 for (i = 0; i < G_N_ELEMENTS (lpoption_names); i++)
3481 if (strcmp (lpoption_names[i].lpoption, lpoption) == 0)
3482 return g_strdup (lpoption_names[i].name);
3484 return g_strdup_printf ("cups-%s", lpoption);
3488 strptr_cmp (const void *a,
3491 char **aa = (char **)a;
3492 char **bb = (char **)b;
3493 return strcmp (*aa, *bb);
3498 string_in_table (gchar *str,
3499 const gchar *table[],
3502 return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
3505 #define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
3508 handle_option (GtkPrinterOptionSet *set,
3509 ppd_file_t *ppd_file,
3510 ppd_option_t *ppd_option,
3511 ppd_group_t *toplevel_group,
3512 GtkPrintSettings *settings)
3514 GtkPrinterOption *option;
3518 if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
3521 name = get_ppd_option_name (ppd_option->keyword);
3524 if (ppd_option->ui == PPD_UI_PICKONE)
3526 option = create_pickone_option (ppd_file, ppd_option, name);
3528 else if (ppd_option->ui == PPD_UI_BOOLEAN)
3530 option = create_boolean_option (ppd_file, ppd_option, name);
3532 #ifdef PRINT_IGNORED_OPTIONS
3534 g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
3541 name = ppd_group_name (toplevel_group);
3542 if (STRING_IN_TABLE (name,
3543 color_group_whitelist) ||
3544 STRING_IN_TABLE (ppd_option->keyword,
3545 color_option_whitelist))
3547 option->group = g_strdup ("ColorPage");
3549 else if (STRING_IN_TABLE (name,
3550 image_quality_group_whitelist) ||
3551 STRING_IN_TABLE (ppd_option->keyword,
3552 image_quality_option_whitelist))
3554 option->group = g_strdup ("ImageQualityPage");
3556 else if (STRING_IN_TABLE (name,
3557 finishing_group_whitelist) ||
3558 STRING_IN_TABLE (ppd_option->keyword,
3559 finishing_option_whitelist))
3561 option->group = g_strdup ("FinishingPage");
3565 for (i = 0; i < G_N_ELEMENTS (cups_group_translations); i++)
3567 if (strcmp (cups_group_translations[i].name, toplevel_group->name) == 0)
3569 option->group = g_strdup (_(cups_group_translations[i].translation));
3574 if (i == G_N_ELEMENTS (cups_group_translations))
3575 option->group = g_strdup (toplevel_group->text);
3578 set_option_from_settings (option, settings);
3580 gtk_printer_option_set_add (set, option);
3587 handle_group (GtkPrinterOptionSet *set,
3588 ppd_file_t *ppd_file,
3590 ppd_group_t *toplevel_group,
3591 GtkPrintSettings *settings)
3596 /* Ignore installable options */
3597 name = ppd_group_name (toplevel_group);
3598 if (strcmp (name, "InstallableOptions") == 0)
3601 for (i = 0; i < group->num_options; i++)
3602 handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
3604 for (i = 0; i < group->num_subgroups; i++)
3605 handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
3612 GtkPrintSettings *settings;
3613 GtkPrinter *printer;
3614 } GtkPrintBackendCupsColordHelper;
3617 colord_printer_option_set_changed_cb (GtkPrinterOptionSet *set,
3618 GtkPrintBackendCupsColordHelper *helper)
3620 gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (helper->printer),
3626 static GtkPrinterOptionSet *
3627 cups_printer_get_options (GtkPrinter *printer,
3628 GtkPrintSettings *settings,
3629 GtkPageSetup *page_setup,
3630 GtkPrintCapabilities capabilities)
3632 GtkPrinterOptionSet *set;
3633 GtkPrinterOption *option;
3634 ppd_file_t *ppd_file;
3636 char *print_at[] = { "now", "at", "on-hold" };
3637 char *n_up[] = {"1", "2", "4", "6", "9", "16" };
3638 char *prio[] = {"100", "80", "50", "30" };
3639 /* Translators: These strings name the possible values of the
3640 * job priority option in the print dialog
3642 char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
3643 char *n_up_layout[] = { "lrtb", "lrbt", "rltb", "rlbt", "tblr", "tbrl", "btlr", "btrl" };
3644 /* Translators: These strings name the possible arrangements of
3645 * multiple pages on a sheet when printing
3647 char *n_up_layout_display[] = { N_("Left to right, top to bottom"), N_("Left to right, bottom to top"),
3648 N_("Right to left, top to bottom"), N_("Right to left, bottom to top"),
3649 N_("Top to bottom, left to right"), N_("Top to bottom, right to left"),
3650 N_("Bottom to top, left to right"), N_("Bottom to top, right to left") };
3653 cups_option_t *opts = NULL;
3654 GtkPrintBackendCups *backend;
3655 GtkTextDirection text_direction;
3656 GtkPrinterCups *cups_printer = NULL;
3658 GtkPrintBackendCupsColordHelper *helper;
3661 set = gtk_printer_option_set_new ();
3663 /* Cups specific, non-ppd related settings */
3665 /* Translators, this string is used to label the pages-per-sheet option
3666 * in the print dialog
3668 option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3669 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
3671 gtk_printer_option_set (option, "1");
3672 set_option_from_settings (option, settings);
3673 gtk_printer_option_set_add (set, option);
3674 g_object_unref (option);
3676 if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
3678 for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
3679 n_up_layout_display[i] = _(n_up_layout_display[i]);
3681 /* Translators, this string is used to label the option in the print
3682 * dialog that controls in what order multiple pages are arranged
3684 option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3685 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
3686 n_up_layout, n_up_layout_display);
3688 text_direction = gtk_widget_get_default_direction ();
3689 if (text_direction == GTK_TEXT_DIR_LTR)
3690 gtk_printer_option_set (option, "lrtb");
3692 gtk_printer_option_set (option, "rltb");
3694 set_option_from_settings (option, settings);
3695 gtk_printer_option_set_add (set, option);
3696 g_object_unref (option);
3699 for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
3700 prio_display[i] = _(prio_display[i]);
3702 /* Translators, this string is used to label the job priority option
3703 * in the print dialog
3705 option = gtk_printer_option_new ("gtk-job-prio", _("Job Priority"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3706 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
3707 prio, prio_display);
3708 gtk_printer_option_set (option, "50");
3709 set_option_from_settings (option, settings);
3710 gtk_printer_option_set_add (set, option);
3711 g_object_unref (option);
3713 /* Translators, this string is used to label the billing info entry
3714 * in the print dialog
3716 option = gtk_printer_option_new ("gtk-billing-info", _("Billing Info"), GTK_PRINTER_OPTION_TYPE_STRING);
3717 gtk_printer_option_set (option, "");
3718 set_option_from_settings (option, settings);
3719 gtk_printer_option_set_add (set, option);
3720 g_object_unref (option);
3722 backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
3723 cups_printer = GTK_PRINTER_CUPS (printer);
3725 if (backend != NULL && printer != NULL)
3727 char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
3728 /* Translators, these strings are names for various 'standard' cover
3729 * pages that the printing system may support.
3731 char *cover_display_default[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
3732 char **cover = NULL;
3733 char **cover_display = NULL;
3734 char **cover_display_translated = NULL;
3735 gint num_of_covers = 0;
3739 num_of_covers = backend->number_of_covers;
3740 cover = g_new (char *, num_of_covers + 1);
3741 cover[num_of_covers] = NULL;
3742 cover_display = g_new (char *, num_of_covers + 1);
3743 cover_display[num_of_covers] = NULL;
3744 cover_display_translated = g_new (char *, num_of_covers + 1);
3745 cover_display_translated[num_of_covers] = NULL;
3747 for (i = 0; i < num_of_covers; i++)
3749 cover[i] = g_strdup (backend->covers[i]);
3751 for (j = 0; j < G_N_ELEMENTS (cover_default); j++)
3752 if (strcmp (cover_default[j], cover[i]) == 0)
3754 value = cover_display_default[j];
3757 cover_display[i] = (value != NULL) ? g_strdup (value) : g_strdup (backend->covers[i]);
3760 for (i = 0; i < num_of_covers; i++)
3761 cover_display_translated[i] = _(cover_display[i]);
3763 /* Translators, this is the label used for the option in the print
3764 * dialog that controls the front cover page.
3766 option = gtk_printer_option_new ("gtk-cover-before", _("Before"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3767 gtk_printer_option_choices_from_array (option, num_of_covers,
3768 cover, cover_display_translated);
3770 if (cups_printer->default_cover_before != NULL)
3771 gtk_printer_option_set (option, cups_printer->default_cover_before);
3773 gtk_printer_option_set (option, "none");
3774 set_option_from_settings (option, settings);
3775 gtk_printer_option_set_add (set, option);
3776 g_object_unref (option);
3778 /* Translators, this is the label used for the option in the print
3779 * dialog that controls the back cover page.
3781 option = gtk_printer_option_new ("gtk-cover-after", _("After"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3782 gtk_printer_option_choices_from_array (option, num_of_covers,
3783 cover, cover_display_translated);
3784 if (cups_printer->default_cover_after != NULL)
3785 gtk_printer_option_set (option, cups_printer->default_cover_after);
3787 gtk_printer_option_set (option, "none");
3788 set_option_from_settings (option, settings);
3789 gtk_printer_option_set_add (set, option);
3790 g_object_unref (option);
3793 g_strfreev (cover_display);
3794 g_free (cover_display_translated);
3797 /* Translators: this is the name of the option that controls when
3798 * a print job is printed. Possible values are 'now', a specified time,
3801 option = gtk_printer_option_new ("gtk-print-time", _("Print at"), GTK_PRINTER_OPTION_TYPE_PICKONE);
3802 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
3803 print_at, print_at);
3804 gtk_printer_option_set (option, "now");
3805 set_option_from_settings (option, settings);
3806 gtk_printer_option_set_add (set, option);
3807 g_object_unref (option);
3809 /* Translators: this is the name of the option that allows the user
3810 * to specify a time when a print job will be printed.
3812 option = gtk_printer_option_new ("gtk-print-time-text", _("Print at time"), GTK_PRINTER_OPTION_TYPE_STRING);
3813 gtk_printer_option_set (option, "");
3814 set_option_from_settings (option, settings);
3815 gtk_printer_option_set_add (set, option);
3816 g_object_unref (option);
3818 /* Printer (ppd) specific settings */
3819 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
3822 GtkPaperSize *paper_size;
3823 ppd_option_t *option;
3824 const gchar *ppd_name;
3826 ppdMarkDefaults (ppd_file);
3828 paper_size = gtk_page_setup_get_paper_size (page_setup);
3830 option = ppdFindOption (ppd_file, "PageSize");
3831 ppd_name = gtk_paper_size_get_ppd_name (paper_size);
3834 strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
3838 char width[G_ASCII_DTOSTR_BUF_SIZE];
3839 char height[G_ASCII_DTOSTR_BUF_SIZE];
3841 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
3842 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
3843 /* Translators: this format is used to display a custom paper
3844 * size. The two placeholders are replaced with the width and height
3845 * in points. E.g: "Custom 230.4x142.9"
3847 custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
3848 strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
3849 g_free (custom_name);
3852 for (i = 0; i < ppd_file->num_groups; i++)
3853 handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
3856 /* Now honor the user set defaults for this printer */
3857 num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
3859 for (i = 0; i < num_opts; i++)
3861 if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
3864 name = get_lpoption_name (opts[i].name);
3865 if (strcmp (name, "cups-job-sheets") == 0)
3870 values = g_strsplit (opts[i].value, ",", 2);
3871 num_values = g_strv_length (values);
3873 option = gtk_printer_option_set_lookup (set, "gtk-cover-before");
3874 if (option && num_values > 0)
3875 gtk_printer_option_set (option, g_strstrip (values[0]));
3877 option = gtk_printer_option_set_lookup (set, "gtk-cover-after");
3878 if (option && num_values > 1)
3879 gtk_printer_option_set (option, g_strstrip (values[1]));
3881 g_strfreev (values);
3883 else if (strcmp (name, "cups-job-hold-until") == 0)
3885 GtkPrinterOption *option2 = NULL;
3887 option = gtk_printer_option_set_lookup (set, "gtk-print-time-text");
3888 if (option && opts[i].value)
3890 option2 = gtk_printer_option_set_lookup (set, "gtk-print-time");
3893 if (strcmp (opts[i].value, "indefinite") == 0)
3894 gtk_printer_option_set (option2, "on-hold");
3897 gtk_printer_option_set (option2, "at");
3898 gtk_printer_option_set (option, opts[i].value);
3903 else if (strcmp (name, "cups-sides") == 0)
3905 option = gtk_printer_option_set_lookup (set, "gtk-duplex");
3906 if (option && opts[i].value)
3908 if (strcmp (opts[i].value, "two-sided-short-edge") == 0)
3909 gtk_printer_option_set (option, "DuplexTumble");
3910 else if (strcmp (opts[i].value, "two-sided-long-edge") == 0)
3911 gtk_printer_option_set (option, "DuplexNoTumble");
3916 option = gtk_printer_option_set_lookup (set, name);
3918 gtk_printer_option_set (option, opts[i].value);
3923 cupsFreeOptions (num_opts, opts);
3926 /* TRANSLATORS: this this the ICC color profile to use for this job */
3927 option = gtk_printer_option_new ("colord-profile",
3928 _("Printer Profile"),
3929 GTK_PRINTER_OPTION_TYPE_INFO);
3931 /* assign it to the color page */
3932 option->group = g_strdup ("ColorPage");
3934 /* TRANSLATORS: this is when color profile information is unavailable */
3935 gtk_printer_option_set (option, _("Unavailable"));
3936 gtk_printer_option_set_add (set, option);
3938 /* watch to see if the user changed the options */
3939 helper = g_new (GtkPrintBackendCupsColordHelper, 1);
3940 helper->printer = printer;
3941 helper->settings = settings;
3942 g_signal_connect_data (set, "changed",
3943 G_CALLBACK (colord_printer_option_set_changed_cb),
3945 (GClosureNotify) g_free,
3948 /* initial coldplug */
3949 gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (printer),
3951 g_object_bind_property (printer, "profile-title",
3962 mark_option_from_set (GtkPrinterOptionSet *set,
3963 ppd_file_t *ppd_file,
3964 ppd_option_t *ppd_option)
3966 GtkPrinterOption *option;
3967 char *name = get_ppd_option_name (ppd_option->keyword);
3969 option = gtk_printer_option_set_lookup (set, name);
3972 ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
3979 mark_group_from_set (GtkPrinterOptionSet *set,
3980 ppd_file_t *ppd_file,
3985 for (i = 0; i < group->num_options; i++)
3986 mark_option_from_set (set, ppd_file, &group->options[i]);
3988 for (i = 0; i < group->num_subgroups; i++)
3989 mark_group_from_set (set, ppd_file, &group->subgroups[i]);
3993 set_conflicts_from_option (GtkPrinterOptionSet *set,
3994 ppd_file_t *ppd_file,
3995 ppd_option_t *ppd_option)
3997 GtkPrinterOption *option;
4000 if (ppd_option->conflicted)
4002 name = get_ppd_option_name (ppd_option->keyword);
4003 option = gtk_printer_option_set_lookup (set, name);
4006 gtk_printer_option_set_has_conflict (option, TRUE);
4007 #ifdef PRINT_IGNORED_OPTIONS
4009 g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
4017 set_conflicts_from_group (GtkPrinterOptionSet *set,
4018 ppd_file_t *ppd_file,
4023 for (i = 0; i < group->num_options; i++)
4024 set_conflicts_from_option (set, ppd_file, &group->options[i]);
4026 for (i = 0; i < group->num_subgroups; i++)
4027 set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
4031 cups_printer_mark_conflicts (GtkPrinter *printer,
4032 GtkPrinterOptionSet *options)
4034 ppd_file_t *ppd_file;
4038 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4040 if (ppd_file == NULL)
4043 ppdMarkDefaults (ppd_file);
4045 for (i = 0; i < ppd_file->num_groups; i++)
4046 mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
4048 num_conflicts = ppdConflicts (ppd_file);
4050 if (num_conflicts > 0)
4052 for (i = 0; i < ppd_file->num_groups; i++)
4053 set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
4056 return num_conflicts > 0;
4060 GtkPrinter *printer;
4061 GtkPrinterOptionSet *options;
4062 GtkPrintSettings *settings;
4063 ppd_file_t *ppd_file;
4068 const char *standard;
4072 map_settings_to_option (GtkPrinterOption *option,
4073 const NameMapping table[],
4075 GtkPrintSettings *settings,
4076 const gchar *standard_name,
4077 const gchar *cups_name)
4081 const char *cups_value;
4082 const char *standard_value;
4084 /* If the cups-specific setting is set, always use that */
4085 name = g_strdup_printf ("cups-%s", cups_name);
4086 cups_value = gtk_print_settings_get (settings, name);
4089 if (cups_value != NULL)
4091 gtk_printer_option_set (option, cups_value);
4095 /* Otherwise we try to convert from the general setting */
4096 standard_value = gtk_print_settings_get (settings, standard_name);
4097 if (standard_value == NULL)
4100 for (i = 0; i < n_elements; i++)
4102 if (table[i].cups == NULL && table[i].standard == NULL)
4104 gtk_printer_option_set (option, standard_value);
4107 else if (table[i].cups == NULL &&
4108 strcmp (table[i].standard, standard_value) == 0)
4110 set_option_off (option);
4113 else if (strcmp (table[i].standard, standard_value) == 0)
4115 gtk_printer_option_set (option, table[i].cups);
4122 map_option_to_settings (const gchar *value,
4123 const NameMapping table[],
4125 GtkPrintSettings *settings,
4126 const gchar *standard_name,
4127 const gchar *cups_name)
4132 for (i = 0; i < n_elements; i++)
4134 if (table[i].cups == NULL && table[i].standard == NULL)
4136 gtk_print_settings_set (settings,
4141 else if (table[i].cups == NULL && table[i].standard != NULL)
4143 if (value_is_off (value))
4145 gtk_print_settings_set (settings,
4151 else if (strcmp (table[i].cups, value) == 0)
4153 gtk_print_settings_set (settings,
4160 /* Always set the corresponding cups-specific setting */
4161 name = g_strdup_printf ("cups-%s", cups_name);
4162 gtk_print_settings_set (settings, name, value);
4167 static const NameMapping paper_source_map[] = {
4168 { "Lower", "lower"},
4169 { "Middle", "middle"},
4170 { "Upper", "upper"},
4172 { "Envelope", "envelope"},
4173 { "Cassette", "cassette"},
4174 { "LargeCapacity", "large-capacity"},
4175 { "AnySmallFormat", "small-format"},
4176 { "AnyLargeFormat", "large-format"},
4180 static const NameMapping output_tray_map[] = {
4181 { "Upper", "upper"},
4182 { "Lower", "lower"},
4187 static const NameMapping duplex_map[] = {
4188 { "DuplexTumble", "vertical" },
4189 { "DuplexNoTumble", "horizontal" },
4193 static const NameMapping output_mode_map[] = {
4194 { "Standard", "normal" },
4195 { "Normal", "normal" },
4196 { "Draft", "draft" },
4197 { "Fast", "draft" },
4200 static const NameMapping media_type_map[] = {
4201 { "Transparency", "transparency"},
4202 { "Standard", "stationery"},
4206 static const NameMapping all_map[] = {
4212 set_option_from_settings (GtkPrinterOption *option,
4213 GtkPrintSettings *settings)
4215 const char *cups_value;
4218 if (settings == NULL)
4221 if (strcmp (option->name, "gtk-paper-source") == 0)
4222 map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
4223 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
4224 else if (strcmp (option->name, "gtk-output-tray") == 0)
4225 map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
4226 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
4227 else if (strcmp (option->name, "gtk-duplex") == 0)
4228 map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
4229 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
4230 else if (strcmp (option->name, "cups-OutputMode") == 0)
4231 map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
4232 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
4233 else if (strcmp (option->name, "cups-Resolution") == 0)
4235 cups_value = gtk_print_settings_get (settings, option->name);
4237 gtk_printer_option_set (option, cups_value);
4240 if (gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION, -1) != -1 ||
4241 gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_X, -1) != -1 ||
4242 gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_Y, -1) != -1 ||
4243 option->value == NULL || option->value[0] == '\0')
4245 int res = gtk_print_settings_get_resolution (settings);
4246 int res_x = gtk_print_settings_get_resolution_x (settings);
4247 int res_y = gtk_print_settings_get_resolution_y (settings);
4251 value = g_strdup_printf ("%dx%ddpi", res_x, res_y);
4252 gtk_printer_option_set (option, value);
4257 value = g_strdup_printf ("%ddpi", res);
4258 gtk_printer_option_set (option, value);
4264 else if (strcmp (option->name, "gtk-paper-type") == 0)
4265 map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
4266 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
4267 else if (strcmp (option->name, "gtk-n-up") == 0)
4269 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
4270 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
4272 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
4274 map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
4275 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
4277 else if (strcmp (option->name, "gtk-billing-info") == 0)
4279 cups_value = gtk_print_settings_get (settings, "cups-job-billing");
4281 gtk_printer_option_set (option, cups_value);
4283 else if (strcmp (option->name, "gtk-job-prio") == 0)
4285 cups_value = gtk_print_settings_get (settings, "cups-job-priority");
4287 gtk_printer_option_set (option, cups_value);
4289 else if (strcmp (option->name, "gtk-cover-before") == 0)
4291 cups_value = gtk_print_settings_get (settings, "cover-before");
4293 gtk_printer_option_set (option, cups_value);
4295 else if (strcmp (option->name, "gtk-cover-after") == 0)
4297 cups_value = gtk_print_settings_get (settings, "cover-after");
4299 gtk_printer_option_set (option, cups_value);
4301 else if (strcmp (option->name, "gtk-print-time") == 0)
4303 cups_value = gtk_print_settings_get (settings, "print-at");
4305 gtk_printer_option_set (option, cups_value);
4307 else if (strcmp (option->name, "gtk-print-time-text") == 0)
4309 cups_value = gtk_print_settings_get (settings, "print-at-time");
4311 gtk_printer_option_set (option, cups_value);
4313 else if (g_str_has_prefix (option->name, "cups-"))
4315 cups_value = gtk_print_settings_get (settings, option->name);
4317 gtk_printer_option_set (option, cups_value);
4322 foreach_option_get_settings (GtkPrinterOption *option,
4325 struct OptionData *data = user_data;
4326 GtkPrintSettings *settings = data->settings;
4329 value = option->value;
4331 if (strcmp (option->name, "gtk-paper-source") == 0)
4332 map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
4333 settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
4334 else if (strcmp (option->name, "gtk-output-tray") == 0)
4335 map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
4336 settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
4337 else if (strcmp (option->name, "gtk-duplex") == 0)
4338 map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
4339 settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
4340 else if (strcmp (option->name, "cups-OutputMode") == 0)
4341 map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
4342 settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
4343 else if (strcmp (option->name, "cups-Resolution") == 0)
4345 int res, res_x, res_y;
4347 if (sscanf (value, "%dx%ddpi", &res_x, &res_y) == 2)
4349 if (res_x > 0 && res_y > 0)
4350 gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
4352 else if (sscanf (value, "%ddpi", &res) == 1)
4355 gtk_print_settings_set_resolution (settings, res);
4358 gtk_print_settings_set (settings, option->name, value);
4360 else if (strcmp (option->name, "gtk-paper-type") == 0)
4361 map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
4362 settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
4363 else if (strcmp (option->name, "gtk-n-up") == 0)
4364 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
4365 settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
4366 else if (strcmp (option->name, "gtk-n-up-layout") == 0)
4367 map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
4368 settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
4369 else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
4370 gtk_print_settings_set (settings, "cups-job-billing", value);
4371 else if (strcmp (option->name, "gtk-job-prio") == 0)
4372 gtk_print_settings_set (settings, "cups-job-priority", value);
4373 else if (strcmp (option->name, "gtk-cover-before") == 0)
4374 gtk_print_settings_set (settings, "cover-before", value);
4375 else if (strcmp (option->name, "gtk-cover-after") == 0)
4376 gtk_print_settings_set (settings, "cover-after", value);
4377 else if (strcmp (option->name, "gtk-print-time") == 0)
4378 gtk_print_settings_set (settings, "print-at", value);
4379 else if (strcmp (option->name, "gtk-print-time-text") == 0)
4380 gtk_print_settings_set (settings, "print-at-time", value);
4381 else if (g_str_has_prefix (option->name, "cups-"))
4382 gtk_print_settings_set (settings, option->name, value);
4386 supports_am_pm (void)
4388 struct tm tmp_tm = { 0 };
4392 length = strftime (time, sizeof (time), "%p", &tmp_tm);
4397 /* Converts local time to UTC time. Local time has to be in one of these
4398 * formats: HH:MM:SS, HH:MM, HH:MM:SS {am, pm}, HH:MM {am, pm}, HH {am, pm},
4399 * {am, pm} HH:MM:SS, {am, pm} HH:MM, {am, pm} HH.
4400 * Returns a newly allocated string holding UTC time in HH:MM:SS format
4404 localtime_to_utctime (const char *local_time)
4406 const char *formats_0[] = {" %I : %M : %S %p ", " %p %I : %M : %S ",
4408 " %I : %M %p ", " %p %I : %M ",
4410 " %I %p ", " %p %I "};
4411 const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
4412 const char *end = NULL;
4413 struct tm *actual_local_time;
4414 struct tm *actual_utc_time;
4415 struct tm local_print_time;
4416 struct tm utc_print_time;
4417 struct tm diff_time;
4418 gchar *utc_time = NULL;
4421 if (local_time == NULL || local_time[0] == '\0')
4424 n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
4426 for (i = 0; i < n; i++)
4428 local_print_time.tm_hour = 0;
4429 local_print_time.tm_min = 0;
4430 local_print_time.tm_sec = 0;
4432 if (supports_am_pm ())
4433 end = strptime (local_time, formats_0[i], &local_print_time);
4435 end = strptime (local_time, formats_1[i], &local_print_time);
4437 if (end != NULL && end[0] == '\0')
4441 if (end != NULL && end[0] == '\0')
4446 actual_utc_time = g_memdup (gmtime (&rawtime), sizeof (struct tm));
4447 actual_local_time = g_memdup (localtime (&rawtime), sizeof (struct tm));
4449 diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
4450 diff_time.tm_min = actual_utc_time->tm_min - actual_local_time->tm_min;
4451 diff_time.tm_sec = actual_utc_time->tm_sec - actual_local_time->tm_sec;
4453 utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
4454 utc_print_time.tm_min = ((local_print_time.tm_min + diff_time.tm_min) + 60) % 60;
4455 utc_print_time.tm_sec = ((local_print_time.tm_sec + diff_time.tm_sec) + 60) % 60;
4457 utc_time = g_strdup_printf ("%02d:%02d:%02d",
4458 utc_print_time.tm_hour,
4459 utc_print_time.tm_min,
4460 utc_print_time.tm_sec);
4467 cups_printer_get_settings_from_options (GtkPrinter *printer,
4468 GtkPrinterOptionSet *options,
4469 GtkPrintSettings *settings)
4471 struct OptionData data;
4472 const char *print_at, *print_at_time;
4474 data.printer = printer;
4475 data.options = options;
4476 data.settings = settings;
4477 data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4479 if (data.ppd_file != NULL)
4481 GtkPrinterOption *cover_before, *cover_after;
4483 gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
4485 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
4486 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
4487 if (cover_before && cover_after)
4489 char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
4490 gtk_print_settings_set (settings, "cups-job-sheets", value);
4494 print_at = gtk_print_settings_get (settings, "print-at");
4495 print_at_time = gtk_print_settings_get (settings, "print-at-time");
4497 if (strcmp (print_at, "at") == 0)
4499 gchar *utc_time = NULL;
4501 utc_time = localtime_to_utctime (print_at_time);
4503 if (utc_time != NULL)
4505 gtk_print_settings_set (settings, "cups-job-hold-until", utc_time);
4509 gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
4511 else if (strcmp (print_at, "on-hold") == 0)
4512 gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
4517 cups_printer_prepare_for_print (GtkPrinter *printer,
4518 GtkPrintJob *print_job,
4519 GtkPrintSettings *settings,
4520 GtkPageSetup *page_setup)
4522 GtkPrintPages pages;
4523 GtkPageRange *ranges;
4525 GtkPageSet page_set;
4526 GtkPaperSize *paper_size;
4527 const char *ppd_paper_name;
4530 pages = gtk_print_settings_get_print_pages (settings);
4531 gtk_print_job_set_pages (print_job, pages);
4533 if (pages == GTK_PRINT_PAGES_RANGES)
4534 ranges = gtk_print_settings_get_page_ranges (settings, &n_ranges);
4541 gtk_print_job_set_page_ranges (print_job, ranges, n_ranges);
4542 if (gtk_print_settings_get_collate (settings))
4543 gtk_print_settings_set (settings, "cups-Collate", "True");
4544 gtk_print_job_set_collate (print_job, FALSE);
4546 if (gtk_print_settings_get_reverse (settings))
4547 gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
4548 gtk_print_job_set_reverse (print_job, FALSE);
4550 if (gtk_print_settings_get_n_copies (settings) > 1)
4551 gtk_print_settings_set_int (settings, "cups-copies",
4552 gtk_print_settings_get_n_copies (settings));
4553 gtk_print_job_set_num_copies (print_job, 1);
4555 scale = gtk_print_settings_get_scale (settings);
4557 gtk_print_job_set_scale (print_job, scale / 100.0);
4559 page_set = gtk_print_settings_get_page_set (settings);
4560 if (page_set == GTK_PAGE_SET_EVEN)
4561 gtk_print_settings_set (settings, "cups-page-set", "even");
4562 else if (page_set == GTK_PAGE_SET_ODD)
4563 gtk_print_settings_set (settings, "cups-page-set", "odd");
4564 gtk_print_job_set_page_set (print_job, GTK_PAGE_SET_ALL);
4566 paper_size = gtk_page_setup_get_paper_size (page_setup);
4567 ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
4568 if (ppd_paper_name != NULL)
4569 gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
4572 char width[G_ASCII_DTOSTR_BUF_SIZE];
4573 char height[G_ASCII_DTOSTR_BUF_SIZE];
4576 g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
4577 g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
4578 custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
4579 gtk_print_settings_set (settings, "cups-PageSize", custom_name);
4580 g_free (custom_name);
4583 if (gtk_print_settings_get_number_up (settings) > 1)
4585 GtkNumberUpLayout layout = gtk_print_settings_get_number_up_layout (settings);
4586 GEnumClass *enum_class;
4587 GEnumValue *enum_value;
4589 switch (gtk_page_setup_get_orientation (page_setup))
4591 case GTK_PAGE_ORIENTATION_PORTRAIT:
4593 case GTK_PAGE_ORIENTATION_LANDSCAPE:
4595 layout = layout + 2 + 4 * (1 - layout / 2);
4597 layout = layout - 3 - 2 * (layout % 2);
4599 case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
4600 layout = (layout + 3 - 2 * (layout % 2)) % 4 + 4 * (layout / 4);
4602 case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
4604 layout = layout + 5 - 2 * (layout % 2);
4606 layout = layout - 6 + 4 * (1 - (layout - 4) / 2);
4610 enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
4611 enum_value = g_enum_get_value (enum_class, layout);
4612 gtk_print_settings_set (settings, "cups-number-up-layout", enum_value->value_nick);
4613 g_type_class_unref (enum_class);
4616 gtk_print_job_set_rotate (print_job, TRUE);
4619 static GtkPageSetup *
4620 create_page_setup (ppd_file_t *ppd_file,
4624 GtkPageSetup *page_setup;
4625 GtkPaperSize *paper_size;
4626 ppd_option_t *option;
4627 ppd_choice_t *choice;
4629 display_name = NULL;
4630 option = ppdFindOption (ppd_file, "PageSize");
4633 choice = ppdFindChoice (option, size->name);
4635 display_name = ppd_text_to_utf8 (ppd_file, choice->text);
4638 if (display_name == NULL)
4639 display_name = g_strdup (size->name);
4641 page_setup = gtk_page_setup_new ();
4642 paper_size = gtk_paper_size_new_from_ppd (size->name,
4646 gtk_page_setup_set_paper_size (page_setup, paper_size);
4647 gtk_paper_size_free (paper_size);
4649 gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
4650 gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
4651 gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
4652 gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
4654 g_free (display_name);
4660 cups_printer_list_papers (GtkPrinter *printer)
4662 ppd_file_t *ppd_file;
4664 GtkPageSetup *page_setup;
4668 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4669 if (ppd_file == NULL)
4674 for (i = 0; i < ppd_file->num_sizes; i++)
4676 size = &ppd_file->sizes[i];
4678 page_setup = create_page_setup (ppd_file, size);
4680 l = g_list_prepend (l, page_setup);
4683 return g_list_reverse (l);
4686 static GtkPageSetup *
4687 cups_printer_get_default_page_size (GtkPrinter *printer)
4689 ppd_file_t *ppd_file;
4691 ppd_option_t *option;
4694 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4695 if (ppd_file == NULL)
4698 option = ppdFindOption (ppd_file, "PageSize");
4702 size = ppdPageSize (ppd_file, option->defchoice);
4706 return create_page_setup (ppd_file, size);
4710 cups_printer_get_hard_margins (GtkPrinter *printer,
4716 ppd_file_t *ppd_file;
4718 ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
4719 if (ppd_file == NULL)
4722 *left = ppd_file->custom_margins[0];
4723 *bottom = ppd_file->custom_margins[1];
4724 *right = ppd_file->custom_margins[2];
4725 *top = ppd_file->custom_margins[3];
4730 static GtkPrintCapabilities
4731 cups_printer_get_capabilities (GtkPrinter *printer)
4734 GTK_PRINT_CAPABILITY_COPIES |
4735 GTK_PRINT_CAPABILITY_COLLATE |
4736 GTK_PRINT_CAPABILITY_REVERSE |
4737 #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
4738 GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT |
4740 GTK_PRINT_CAPABILITY_NUMBER_UP;