X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=modules%2Fprintbackends%2Fcups%2Fgtkprintbackendcups.c;h=f26214af6a17c5cc6abf5dc2cbf62140e47b85fc;hb=60969ff9515d516ada21467bffa7149401526392;hp=f84c920c17d2a0ba99de5aa0d8cb263fccd34892;hpb=3dc377a2ca46531acc864c18c516c7d81f0c25e0;p=~andy%2Fgtk
diff --git a/modules/printbackends/cups/gtkprintbackendcups.c b/modules/printbackends/cups/gtkprintbackendcups.c
index f84c920c1..f26214af6 100644
--- a/modules/printbackends/cups/gtkprintbackendcups.c
+++ b/modules/printbackends/cups/gtkprintbackendcups.c
@@ -14,9 +14,7 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see .
*/
#ifdef __linux__
@@ -30,6 +28,13 @@
#include
#include
#include
+/* Cups 1.6 deprecates ppdFindAttr(), ppdFindCustomOption(),
+ * ppdFirstCustomParam(), and ppdNextCustomParam() among others. This
+ * turns off the warning so that it will compile.
+ */
+#ifdef HAVE_CUPS_API_1_6
+# define _PPD_DEPRECATED
+#endif
#include
#include
@@ -54,6 +59,9 @@
#include "gtkcupsutils.h"
+#ifdef HAVE_COLORD
+#include
+#endif
typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
@@ -92,8 +100,11 @@ typedef struct
http_t *http;
GtkCupsRequest *request;
+ GtkCupsPollState poll_state;
GPollFD *data_poll;
GtkPrintBackendCups *backend;
+ GtkPrintCupsResponseCallbackFunc callback;
+ gpointer callback_data;
} GtkPrintCupsDispatchWatch;
@@ -110,9 +121,11 @@ struct _GtkPrintBackendCups
guint list_printers_poll;
guint list_printers_pending : 1;
+ gint list_printers_attempts;
guint got_default_printer : 1;
guint default_printer_poll;
GtkCupsConnectionTest *cups_connection_test;
+ gint reading_ppds;
char **covers;
int number_of_covers;
@@ -121,6 +134,9 @@ struct _GtkPrintBackendCups
GHashTable *auth;
gchar *username;
gboolean authentication_lock;
+#ifdef HAVE_COLORD
+ CdClient *colord_client;
+#endif
};
static GObjectClass *backend_parent_class;
@@ -154,12 +170,12 @@ static GList * cups_printer_list_papers (GtkPrinter
static GtkPageSetup * cups_printer_get_default_page_size (GtkPrinter *printer);
static void cups_printer_request_details (GtkPrinter *printer);
static gboolean cups_request_default_printer (GtkPrintBackendCups *print_backend);
-static void cups_request_ppd (GtkPrinter *printer);
-static void cups_printer_get_hard_margins (GtkPrinter *printer,
- double *top,
- double *bottom,
- double *left,
- double *right);
+static gboolean cups_request_ppd (GtkPrinter *printer);
+static gboolean cups_printer_get_hard_margins (GtkPrinter *printer,
+ gdouble *top,
+ gdouble *bottom,
+ gdouble *left,
+ gdouble *right);
static GtkPrintCapabilities cups_printer_get_capabilities (GtkPrinter *printer);
static void set_option_from_settings (GtkPrinterOption *option,
GtkPrintSettings *setting);
@@ -179,18 +195,18 @@ static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter
gdouble height,
GIOChannel *cache_io);
-static void gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
- const gchar *hostname,
- const gchar *username,
- const gchar *password);
+static void gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
+ gchar **auth_info_required,
+ gchar **auth_info);
-void overwrite_and_free (gpointer data);
-static gboolean is_address_local (const gchar *address);
+void overwrite_and_free (gpointer data);
+static gboolean is_address_local (const gchar *address);
+static gboolean request_auth_info (gpointer data);
static void
gtk_print_backend_cups_register_type (GTypeModule *module)
{
- static const GTypeInfo print_backend_cups_info =
+ const GTypeInfo print_backend_cups_info =
{
sizeof (GtkPrintBackendCupsClass),
NULL, /* base_init */
@@ -230,7 +246,22 @@ pb_module_create (void)
{
return gtk_print_backend_cups_new ();
}
-
+/* CUPS 1.6 Getter/Setter Functions CUPS 1.6 makes private most of the
+ * IPP structures and enforces access via new getter functions, which
+ * are unfortunately not available in earlier versions. We define
+ * below those getter functions as macros for use when building
+ * against earlier CUPS versions.
+ */
+#ifndef HAVE_CUPS_API_1_6
+#define ippGetOperation(ipp_request) ipp_request->request.op.operation_id
+#define ippGetInteger(attr, index) attr->values[index].integer
+#define ippGetBoolean(attr, index) attr->values[index].boolean
+#define ippGetString(attr, index, foo) attr->values[index].string.text
+#define ippGetValueTag(attr) attr->value_tag
+#define ippGetName(attr) attr->name
+#define ippGetCount(attr) attr->num_values
+#define ippGetGroupTag(attr) attr->group_tag
+#endif
/*
* GtkPrintBackendCups
*/
@@ -313,7 +344,7 @@ _cairo_write_to_cups (void *closure,
}
GTK_NOTE (PRINTING,
- g_print ("CUPS Backend: Wrote %i bytes to temp file\n", written));
+ g_print ("CUPS Backend: Wrote %"G_GSIZE_FORMAT" bytes to temp file\n", written));
data += written;
length -= written;
@@ -329,18 +360,19 @@ cups_printer_create_cairo_surface (GtkPrinter *printer,
gdouble height,
GIOChannel *cache_io)
{
- cairo_surface_t *surface;
+ cairo_surface_t *surface;
ppd_file_t *ppd_file = NULL;
ppd_attr_t *ppd_attr = NULL;
ppd_attr_t *ppd_attr_res = NULL;
ppd_attr_t *ppd_attr_screen_freq = NULL;
ppd_attr_t *ppd_attr_res_screen_freq = NULL;
gchar *res_string = NULL;
- int level = 2;
-
- /* TODO: check if it is a ps or pdf printer */
-
- surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
+ gint level = 2;
+
+ if (gtk_printer_accepts_pdf (printer))
+ surface = cairo_pdf_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
+ else
+ surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
@@ -361,27 +393,25 @@ cups_printer_create_cairo_surface (GtkPrinter *printer,
if (sscanf (ppd_attr_res->value, "%dx%ddpi", &res_x, &res_y) == 2)
{
- if (res_x != 0 && res_y != 0)
+ if (res_x > 0 && res_y > 0)
gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
}
else if (sscanf (ppd_attr_res->value, "%ddpi", &res) == 1)
{
- if (res != 0)
+ if (res > 0)
gtk_print_settings_set_resolution (settings, res);
}
- else
- gtk_print_settings_set_resolution (settings, 300);
}
}
- res_string = g_strdup_printf ("%ddpi",
+ res_string = g_strdup_printf ("%ddpi",
gtk_print_settings_get_resolution (settings));
ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
g_free (res_string);
if (ppd_attr_res_screen_freq == NULL)
{
- res_string = g_strdup_printf ("%dx%ddpi",
+ res_string = g_strdup_printf ("%dx%ddpi",
gtk_print_settings_get_resolution_x (settings),
gtk_print_settings_get_resolution_y (settings));
ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
@@ -390,24 +420,20 @@ cups_printer_create_cairo_surface (GtkPrinter *printer,
ppd_attr_screen_freq = ppdFindAttr (ppd_file, "ScreenFreq", NULL);
- if (ppd_attr_res_screen_freq != NULL)
+ if (ppd_attr_res_screen_freq != NULL && atof (ppd_attr_res_screen_freq->value) > 0.0)
gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_res_screen_freq->value));
- else if (ppd_attr_screen_freq != NULL)
+ else if (ppd_attr_screen_freq != NULL && atof (ppd_attr_screen_freq->value) > 0.0)
gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_screen_freq->value));
- else
- gtk_print_settings_set_printer_lpi (settings, 150.0);
- }
- else
- {
- gtk_print_settings_set_resolution (settings, 300);
- gtk_print_settings_set_printer_lpi (settings, 150.0);
}
- if (level == 2)
- cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
+ if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PS)
+ {
+ if (level == 2)
+ cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
- if (level == 3)
- cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
+ if (level == 3)
+ cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
+ }
cairo_surface_set_fallback_resolution (surface,
2.0 * gtk_print_settings_get_printer_lpi (settings),
@@ -463,7 +489,7 @@ cups_print_cb (GtkPrintBackendCups *print_backend,
ipp_t *response = gtk_cups_result_get_response (result);
if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
- job_id = attr->values[0].integer;
+ job_id = ippGetInteger (attr, 0);
if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
@@ -483,22 +509,68 @@ cups_print_cb (GtkPrintBackendCups *print_backend,
GDK_THREADS_LEAVE ();
}
+typedef struct {
+ GtkCupsRequest *request;
+ GtkPrinterCups *printer;
+} CupsOptionsData;
+
static void
add_cups_options (const gchar *key,
const gchar *value,
gpointer user_data)
{
- GtkCupsRequest *request = user_data;
+ CupsOptionsData *data = (CupsOptionsData *) user_data;
+ GtkCupsRequest *request = data->request;
+ GtkPrinterCups *printer = data->printer;
+ gboolean custom_value = FALSE;
+ gchar *new_value = NULL;
+ gint i;
+
+ if (!key || !value)
+ return;
if (!g_str_has_prefix (key, "cups-"))
return;
if (strcmp (value, "gtk-ignore-value") == 0)
return;
-
+
key = key + strlen ("cups-");
- gtk_cups_request_encode_option (request, key, value);
+ if (printer && printer->ppd_file)
+ {
+ ppd_coption_t *coption;
+ gboolean found = FALSE;
+ gboolean custom_values_enabled = FALSE;
+
+ coption = ppdFindCustomOption (printer->ppd_file, key);
+ if (coption && coption->option)
+ {
+ for (i = 0; i < coption->option->num_choices; i++)
+ {
+ /* Are custom values enabled ? */
+ if (g_str_equal (coption->option->choices[i].choice, "Custom"))
+ custom_values_enabled = TRUE;
+
+ /* Is the value among available choices ? */
+ if (g_str_equal (coption->option->choices[i].choice, value))
+ found = TRUE;
+ }
+
+ if (custom_values_enabled && !found)
+ custom_value = TRUE;
+ }
+ }
+
+ /* Add "Custom." prefix to custom values. */
+ if (custom_value)
+ {
+ new_value = g_strdup_printf ("Custom.%s", value);
+ gtk_cups_request_encode_option (request, key, new_value);
+ g_free (new_value);
+ }
+ else
+ gtk_cups_request_encode_option (request, key, value);
}
static void
@@ -511,6 +583,7 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
{
GtkPrinterCups *cups_printer;
CupsPrintStreamData *ps;
+ CupsOptionsData *options_data;
GtkCupsRequest *request;
GtkPrintSettings *settings;
const gchar *title;
@@ -558,14 +631,21 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
IPP_TAG_NAME, "job-name",
NULL, title);
- gtk_print_settings_foreach (settings, add_cups_options, request);
-
+ options_data = g_new0 (CupsOptionsData, 1);
+ options_data->request = request;
+ options_data->printer = cups_printer;
+ gtk_print_settings_foreach (settings, add_cups_options, options_data);
+ g_free (options_data);
+
ps = g_new0 (CupsPrintStreamData, 1);
ps->callback = callback;
ps->user_data = user_data;
ps->dnotify = dnotify;
ps->job = g_object_ref (job);
+ request->need_auth_info = cups_printer->auth_info_required != NULL;
+ request->auth_info_required = g_strdupv (cups_printer->auth_info_required);
+
cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
request,
(GtkPrintCupsResponseCallbackFunc) cups_print_cb,
@@ -590,6 +670,8 @@ gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
backend_cups->list_printers_poll = FALSE;
backend_cups->got_default_printer = FALSE;
backend_cups->list_printers_pending = FALSE;
+ backend_cups->list_printers_attempts = 0;
+ backend_cups->reading_ppds = 0;
backend_cups->requests = NULL;
backend_cups->auth = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, overwrite_and_free);
@@ -603,6 +685,10 @@ gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
backend_cups->username = NULL;
+#ifdef HAVE_COLORD
+ backend_cups->colord_client = cd_client_new ();
+#endif
+
cups_get_local_default_printer (backend_cups);
}
@@ -629,6 +715,10 @@ gtk_print_backend_cups_finalize (GObject *object)
g_free (backend_cups->username);
+#ifdef HAVE_COLORD
+ g_object_unref (backend_cups->colord_client);
+#endif
+
backend_parent_class->finalize (object);
}
@@ -645,6 +735,7 @@ gtk_print_backend_cups_dispose (GObject *object)
if (backend_cups->list_printers_poll > 0)
g_source_remove (backend_cups->list_printers_poll);
backend_cups->list_printers_poll = 0;
+ backend_cups->list_printers_attempts = 0;
if (backend_cups->default_printer_poll > 0)
g_source_remove (backend_cups->default_printer_poll);
@@ -665,24 +756,43 @@ is_address_local (const gchar *address)
}
static void
-gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
- const gchar *hostname,
- const gchar *username,
- const gchar *password)
+gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
+ gchar **auth_info_required,
+ gchar **auth_info)
{
GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
GList *l;
char dispatch_hostname[HTTP_MAX_URI];
- gchar *key;
+ gchar *username = NULL;
+ gchar *hostname = NULL;
+ gchar *password = NULL;
+ gint length;
+ gint i;
- key = g_strconcat (username, "@", hostname, NULL);
- g_hash_table_insert (cups_backend->auth, key, g_strdup (password));
+ length = g_strv_length (auth_info_required);
+
+ if (auth_info != NULL)
+ for (i = 0; i < length; i++)
+ {
+ if (g_strcmp0 (auth_info_required[i], "username") == 0)
+ username = g_strdup (auth_info[i]);
+ else if (g_strcmp0 (auth_info_required[i], "hostname") == 0)
+ hostname = g_strdup (auth_info[i]);
+ else if (g_strcmp0 (auth_info_required[i], "password") == 0)
+ password = g_strdup (auth_info[i]);
+ }
+
+ if (hostname != NULL && username != NULL && password != NULL)
+ {
+ gchar *key = g_strconcat (username, "@", hostname, NULL);
+ g_hash_table_insert (cups_backend->auth, key, g_strdup (password));
+ GTK_NOTE (PRINTING,
+ g_print ("CUPS backend: storing password for %s\n", key));
+ }
g_free (cups_backend->username);
cups_backend->username = g_strdup (username);
- GTK_NOTE (PRINTING,
- g_print ("CUPS backend: storing password for %s\n", key));
for (l = cups_backend->requests; l; l = l->next)
{
@@ -692,7 +802,18 @@ gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
if (is_address_local (dispatch_hostname))
strcpy (dispatch_hostname, "localhost");
- if (strcmp (hostname, dispatch_hostname) == 0)
+ if (dispatch->request->need_auth_info)
+ {
+ if (auth_info != NULL)
+ {
+ dispatch->request->auth_info = g_new0 (gchar *, length + 1);
+ for (i = 0; i < length; i++)
+ dispatch->request->auth_info[i] = g_strdup (auth_info[i]);
+ }
+ dispatch->backend->authentication_lock = FALSE;
+ dispatch->request->need_auth_info = FALSE;
+ }
+ else if (dispatch->request->password_state == GTK_CUPS_PASSWORD_REQUESTED || auth_info == NULL)
{
overwrite_and_free (dispatch->request->password);
dispatch->request->password = g_strdup (password);
@@ -713,9 +834,15 @@ request_password (gpointer data)
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 FALSE;
+ return G_SOURCE_REMOVE;
httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
if (is_address_local (hostname))
@@ -726,6 +853,22 @@ request_password (gpointer data)
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);
@@ -756,11 +899,8 @@ request_password (gpointer data)
dispatch->backend->authentication_lock = TRUE;
- switch (dispatch->request->ipp_request->request.op.operation_id)
+ switch (ippGetOperation (dispatch->request->ipp_request))
{
- case 0:
- prompt = g_strdup_printf ( _("Authentication is required to get a file from %s"), hostname);
- break;
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);
@@ -786,66 +926,234 @@ request_password (gpointer data)
prompt = g_strdup_printf ( _("Authentication is required to get printers from %s"), hostname);
break;
default:
- prompt = g_strdup_printf ( _("Authentication is required on %s"), hostname);
+ /* 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",
- hostname, username, prompt);
+ 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 FALSE;
+ return G_SOURCE_REMOVE;
}
-static gboolean
-cups_dispatch_watch_check (GSource *source)
+static void
+cups_dispatch_add_poll (GSource *source)
{
GtkPrintCupsDispatchWatch *dispatch;
GtkCupsPollState poll_state;
- gboolean result;
-
- GTK_NOTE (PRINTING,
- g_print ("CUPS Backend: %s