+}
+
+static gboolean
+check_auth_info (gpointer user_data)
+{
+ GtkPrintCupsDispatchWatch *dispatch;
+ dispatch = (GtkPrintCupsDispatchWatch *) user_data;
+
+ if (!dispatch->request->need_auth_info)
+ {
+ if (dispatch->request->auth_info == NULL)
+ {
+ dispatch->callback (GTK_PRINT_BACKEND (dispatch->backend),
+ gtk_cups_request_get_result (dispatch->request),
+ dispatch->callback_data);
+ g_source_destroy ((GSource *) dispatch);
+ }
+ else
+ {
+ gint length;
+ gint i;
+
+ length = g_strv_length (dispatch->request->auth_info_required);
+
+ gtk_cups_request_ipp_add_strings (dispatch->request,
+ IPP_TAG_JOB,
+ IPP_TAG_TEXT,
+ "auth-info",
+ length,
+ NULL,
+ (const char * const *) dispatch->request->auth_info);
+
+ g_source_attach ((GSource *) dispatch, NULL);
+ g_source_unref ((GSource *) dispatch);
+
+ for (i = 0; i < length; i++)
+ overwrite_and_free (dispatch->request->auth_info[i]);
+ g_free (dispatch->request->auth_info);
+ dispatch->request->auth_info = NULL;
+ }
+
+ return G_SOURCE_REMOVE;
+ }
+
+ return G_SOURCE_CONTINUE;
+}
+
+static gboolean
+request_auth_info (gpointer user_data)
+{
+ GtkPrintCupsDispatchWatch *dispatch;
+ const char *job_title;
+ const char *printer_uri;
+ gchar *prompt = NULL;
+ char *printer_name = NULL;
+ gint length;
+ gint i;
+ gboolean *auth_info_visible = NULL;
+ gchar **auth_info_default = NULL;
+ gchar **auth_info_display = NULL;
+
+ dispatch = (GtkPrintCupsDispatchWatch *) user_data;
+
+ if (dispatch->backend->authentication_lock)
+ return FALSE;
+
+ job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
+ printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
+ length = g_strv_length (dispatch->request->auth_info_required);
+
+ auth_info_visible = g_new0 (gboolean, length);
+ auth_info_default = g_new0 (gchar *, length + 1);
+ auth_info_display = g_new0 (gchar *, length + 1);
+
+ for (i = 0; i < length; i++)
+ {
+ if (g_strcmp0 (dispatch->request->auth_info_required[i], "domain") == 0)
+ {
+ auth_info_display[i] = g_strdup (_("Domain:"));
+ auth_info_default[i] = g_strdup ("WORKGROUP");
+ auth_info_visible[i] = TRUE;
+ }
+ else if (g_strcmp0 (dispatch->request->auth_info_required[i], "username") == 0)
+ {
+ auth_info_display[i] = g_strdup (_("Username:"));
+ if (dispatch->backend->username != NULL)
+ auth_info_default[i] = g_strdup (dispatch->backend->username);
+ else
+ auth_info_default[i] = g_strdup (cupsUser ());
+ auth_info_visible[i] = TRUE;
+ }
+ else if (g_strcmp0 (dispatch->request->auth_info_required[i], "password") == 0)
+ {
+ auth_info_display[i] = g_strdup (_("Password:"));
+ auth_info_visible[i] = FALSE;
+ }
+ }
+
+ if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
+ printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
+
+ dispatch->backend->authentication_lock = TRUE;
+
+ if (job_title != NULL)
+ {
+ if (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 document '%s'"), job_title);
+ }
+ else
+ {
+ if (printer_name != NULL)
+ prompt = g_strdup_printf ( _("Authentication is required to print this document on printer %s"), printer_name);
+ else
+ prompt = g_strdup ( _("Authentication is required to print this document"));
+ }
+
+ g_signal_emit_by_name (dispatch->backend, "request-password",
+ dispatch->request->auth_info_required,
+ auth_info_default,
+ auth_info_display,
+ auth_info_visible,
+ prompt);
+
+ for (i = 0; i < length; i++)
+ {
+ g_free (auth_info_default[i]);
+ g_free (auth_info_display[i]);
+ }
+
+ g_free (auth_info_default);
+ g_free (auth_info_display);
+ g_free (printer_name);
+ g_free (prompt);
+
+ g_idle_add (check_auth_info, user_data);
+
+ return FALSE;
+}
+
+static gboolean
+cups_dispatch_watch_check (GSource *source)
+{
+ GtkPrintCupsDispatchWatch *dispatch;
+ GtkCupsPollState poll_state;
+ gboolean result;
+
+ GTK_NOTE (PRINTING,
+ g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
+
+ dispatch = (GtkPrintCupsDispatchWatch *) source;
+
+ poll_state = gtk_cups_request_get_poll_state (dispatch->request);
+