+request_password (gpointer data)
+{
+ GtkPrintCupsDispatchWatch *dispatch = data;
+ const gchar *username;
+ gchar *password;
+ gchar *prompt = NULL;
+ gchar *key = NULL;
+ char hostname[HTTP_MAX_URI];
+ gchar **auth_info_required;
+ gchar **auth_info_default;
+ gchar **auth_info_display;
+ gboolean *auth_info_visible;
+ gint length = 3;
+ gint i;
+
+ if (dispatch->backend->authentication_lock)
+ return G_SOURCE_REMOVE;
+
+ httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
+ if (is_address_local (hostname))
+ strcpy (hostname, "localhost");
+
+ if (dispatch->backend->username != NULL)
+ username = dispatch->backend->username;
+ else
+ username = cupsUser ();
+
+ auth_info_required = g_new0 (gchar*, length + 1);
+ auth_info_required[0] = g_strdup ("hostname");
+ auth_info_required[1] = g_strdup ("username");
+ auth_info_required[2] = g_strdup ("password");
+
+ auth_info_default = g_new0 (gchar*, length + 1);
+ auth_info_default[0] = g_strdup (hostname);
+ auth_info_default[1] = g_strdup (username);
+
+ auth_info_display = g_new0 (gchar*, length + 1);
+ auth_info_display[1] = g_strdup (_("Username:"));
+ auth_info_display[2] = g_strdup (_("Password:"));
+
+ auth_info_visible = g_new0 (gboolean, length + 1);
+ auth_info_visible[1] = TRUE;
+
+ key = g_strconcat (username, "@", hostname, NULL);
+ password = g_hash_table_lookup (dispatch->backend->auth, key);
+
+ if (password && dispatch->request->password_state != GTK_CUPS_PASSWORD_NOT_VALID)
+ {
+ GTK_NOTE (PRINTING,
+ g_print ("CUPS backend: using stored password for %s\n", key));
+
+ overwrite_and_free (dispatch->request->password);
+ dispatch->request->password = g_strdup (password);
+ g_free (dispatch->request->username);
+ dispatch->request->username = g_strdup (username);
+ dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
+ }
+ else
+ {
+ const char *job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
+ const char *printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
+ char *printer_name = NULL;
+
+ if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
+ printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
+
+ if (dispatch->request->password_state == GTK_CUPS_PASSWORD_NOT_VALID)
+ g_hash_table_remove (dispatch->backend->auth, key);
+
+ dispatch->request->password_state = GTK_CUPS_PASSWORD_REQUESTED;
+
+ dispatch->backend->authentication_lock = TRUE;
+
+ switch (ippGetOperation (dispatch->request->ipp_request))
+ {
+ case IPP_PRINT_JOB:
+ if (job_title != NULL && printer_name != NULL)
+ prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
+ else
+ prompt = g_strdup_printf ( _("Authentication is required to print a document on %s"), hostname);
+ break;
+ case IPP_GET_JOB_ATTRIBUTES:
+ if (job_title != NULL)
+ prompt = g_strdup_printf ( _("Authentication is required to get attributes of job '%s'"), job_title);
+ else
+ prompt = g_strdup ( _("Authentication is required to get attributes of a job"));
+ break;
+ case IPP_GET_PRINTER_ATTRIBUTES:
+ if (printer_name != NULL)
+ prompt = g_strdup_printf ( _("Authentication is required to get attributes of printer %s"), printer_name);
+ else
+ prompt = g_strdup ( _("Authentication is required to get attributes of a printer"));
+ break;
+ case CUPS_GET_DEFAULT:
+ prompt = g_strdup_printf ( _("Authentication is required to get default printer of %s"), hostname);
+ break;
+ case CUPS_GET_PRINTERS:
+ prompt = g_strdup_printf ( _("Authentication is required to get printers from %s"), hostname);
+ break;
+ default:
+ /* work around gcc warning about 0 not being a value for this enum */
+ if (ippGetOperation (dispatch->request->ipp_request) == 0)
+ prompt = g_strdup_printf ( _("Authentication is required to get a file from %s"), hostname);
+ else
+ prompt = g_strdup_printf ( _("Authentication is required on %s"), hostname);
+ break;
+ }
+
+ g_free (printer_name);
+
+ g_signal_emit_by_name (dispatch->backend, "request-password",
+ auth_info_required, auth_info_default, auth_info_display, auth_info_visible, prompt);
+
+ g_free (prompt);
+ }
+
+ for (i = 0; i < length; i++)
+ {
+ g_free (auth_info_required[i]);
+ g_free (auth_info_default[i]);
+ g_free (auth_info_display[i]);
+ }
+
+ g_free (auth_info_required);
+ g_free (auth_info_default);
+ g_free (auth_info_display);
+ g_free (auth_info_visible);
+ g_free (key);
+
+ return G_SOURCE_REMOVE;
+}
+
+static void
+cups_dispatch_add_poll (GSource *source)