From be8f3c72ea1ae469f7f1fa29be40f9e750f9f1ee Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 21 Jun 2006 18:16:58 +0000 Subject: [PATCH] Apply a patch by John Palmieri to use buffered io using GIOChannels, clean 2006-06-21 Matthias Clasen Apply a patch by John Palmieri to use buffered io using GIOChannels, clean up error handling and support Cups 1.2 api in the printing code. * acconfig.h: * configure.in: Detect Cups 1.2 * gtk/gtkdebug.h: * gtk/gtkmain.c: Add a printing debug key. * gtk/gtkprintbackend.[hc]: Remove the GError parameter from gtk_print_backend_print_stream, and take a GIOChannel instead of an fd. * gtk/gtkprinter-private.h: * gtk/gtkprinter.c: Replace the fd parameter of _gtk_printer_create_cairo_surface by a GIOChannel. * gtk/gtkprintjob.[hc]: Remove the GError parameter from gtk_print_job_send and make it return void. * gtk/gtkprintoperation-unix.c: * gtk/gtkprintjob.c: Adjust callers, add some debugging support. * modules/printbackends/Makefile.am: Fix DIST_SUBDIRS. * modules/printbackends/cups/Makefile.am: * modules/printbackends/lpr/Makefile.am: Add debug CFLAGS. * modules/printbackends/cups/gtkcupsutils.[hc]: * modules/printbackends/cups/gtkprintbackendcups.c: * modules/printbackends/file/gtkprintbackendfile.c: * modules/printbackends/lpr/gtkprintbackendfile.c: Adapt to the GIOChannel and GError changes, add debug output --- ChangeLog | 37 ++++ ChangeLog.pre-2-10 | 37 ++++ acconfig.h | 2 + configure.in | 11 ++ gtk/gtkdebug.h | 3 +- gtk/gtkmain.c | 3 +- gtk/gtkprintbackend.c | 10 +- gtk/gtkprintbackend.h | 12 +- gtk/gtkprinter-private.h | 2 +- gtk/gtkprinter.c | 4 +- gtk/gtkprintjob.c | 113 ++++++------ gtk/gtkprintjob.h | 5 +- gtk/gtkprintoperation-unix.c | 3 +- modules/printbackends/Makefile.am | 2 +- modules/printbackends/cups/Makefile.am | 3 +- modules/printbackends/cups/gtkcupsutils.c | 124 +++++++++---- modules/printbackends/cups/gtkcupsutils.h | 4 +- .../printbackends/cups/gtkprintbackendcups.c | 163 +++++++++++++----- .../printbackends/file/gtkprintbackendfile.c | 120 +++++++------ modules/printbackends/lpr/Makefile.am | 3 +- .../printbackends/lpr/gtkprintbackendlpr.c | 159 +++++++++-------- 21 files changed, 540 insertions(+), 280 deletions(-) diff --git a/ChangeLog b/ChangeLog index 07ea5ae6a7..1c10760646 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,40 @@ +2006-06-21 Matthias Clasen + + Apply a patch by John Palmieri to use buffered io using + GIOChannels, clean up error handling and support Cups 1.2 + api in the printing code. + + * acconfig.h: + * configure.in: Detect Cups 1.2 + + * gtk/gtkdebug.h: + * gtk/gtkmain.c: Add a printing debug key. + + * gtk/gtkprintbackend.[hc]: Remove the GError parameter from + gtk_print_backend_print_stream, and take a GIOChannel instead + of an fd. + + * gtk/gtkprinter-private.h: + * gtk/gtkprinter.c: Replace the fd parameter of + _gtk_printer_create_cairo_surface by a GIOChannel. + + * gtk/gtkprintjob.[hc]: Remove the GError parameter from + gtk_print_job_send and make it return void. + + * gtk/gtkprintoperation-unix.c: + * gtk/gtkprintjob.c: Adjust callers, add some debugging support. + + * modules/printbackends/Makefile.am: Fix DIST_SUBDIRS. + + * modules/printbackends/cups/Makefile.am: + * modules/printbackends/lpr/Makefile.am: Add debug CFLAGS. + + * modules/printbackends/cups/gtkcupsutils.[hc]: + * modules/printbackends/cups/gtkprintbackendcups.c: + * modules/printbackends/file/gtkprintbackendfile.c: + * modules/printbackends/lpr/gtkprintbackendfile.c: Adapt to + the GIOChannel and GError changes, add debug output + 2006-06-21 Matthias Clasen * gtk/gtkprinter.c (gtk_enumerate_printers): Remove an diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 07ea5ae6a7..1c10760646 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,40 @@ +2006-06-21 Matthias Clasen + + Apply a patch by John Palmieri to use buffered io using + GIOChannels, clean up error handling and support Cups 1.2 + api in the printing code. + + * acconfig.h: + * configure.in: Detect Cups 1.2 + + * gtk/gtkdebug.h: + * gtk/gtkmain.c: Add a printing debug key. + + * gtk/gtkprintbackend.[hc]: Remove the GError parameter from + gtk_print_backend_print_stream, and take a GIOChannel instead + of an fd. + + * gtk/gtkprinter-private.h: + * gtk/gtkprinter.c: Replace the fd parameter of + _gtk_printer_create_cairo_surface by a GIOChannel. + + * gtk/gtkprintjob.[hc]: Remove the GError parameter from + gtk_print_job_send and make it return void. + + * gtk/gtkprintoperation-unix.c: + * gtk/gtkprintjob.c: Adjust callers, add some debugging support. + + * modules/printbackends/Makefile.am: Fix DIST_SUBDIRS. + + * modules/printbackends/cups/Makefile.am: + * modules/printbackends/lpr/Makefile.am: Add debug CFLAGS. + + * modules/printbackends/cups/gtkcupsutils.[hc]: + * modules/printbackends/cups/gtkprintbackendcups.c: + * modules/printbackends/file/gtkprintbackendfile.c: + * modules/printbackends/lpr/gtkprintbackendfile.c: Adapt to + the GIOChannel and GError changes, add debug output + 2006-06-21 Matthias Clasen * gtk/gtkprinter.c (gtk_enumerate_printers): Remove an diff --git a/acconfig.h b/acconfig.h index f30310c241..1ea1445cc0 100644 --- a/acconfig.h +++ b/acconfig.h @@ -68,6 +68,8 @@ #undef GETTEXT_PACKAGE +#undef HAVE_CUPS_API_1_2 + /* #undef PACKAGE */ /* #undef VERSION */ diff --git a/configure.in b/configure.in index 4248eb09d0..b3517760a3 100644 --- a/configure.in +++ b/configure.in @@ -1544,6 +1544,17 @@ if test "x$CUPS_CONFIG" != "xno"; then CUPS_CFLAGS=`cups-config --cflags | sed 's/-O[0-9]*//' | sed 's/-m[^\t]*//g'` CUPS_LIBS=`cups-config --libs` + CUPS_API_VERSION=`cups-config --api-version` + CUPS_API_MAJOR=`echo -n $CUPS_API_VERSION | awk -F. '{print $1}'` + CUPS_API_MINOR=`echo -n $CUPS_API_VERSION | awk -F. '{print $2}'` + + if test $CUPS_API_MAJOR -gt 1 -o \ + $CUPS_API_MAJOR -eq 1 -a $CUPS_API_MINOR -ge 2; then + AC_DEFINE(HAVE_CUPS_API_1_2) + fi + + AC_SUBST(CUPS_API_MAJOR) + AC_SUBST(CUPS_API_MINOR) AC_SUBST(CUPS_CFLAGS) AC_SUBST(CUPS_LIBS) fi diff --git a/gtk/gtkdebug.h b/gtk/gtkdebug.h index 2edfb41575..c70c61768a 100644 --- a/gtk/gtkdebug.h +++ b/gtk/gtkdebug.h @@ -41,7 +41,8 @@ typedef enum { GTK_DEBUG_MULTIHEAD = 1 << 6, GTK_DEBUG_MODULES = 1 << 7, GTK_DEBUG_GEOMETRY = 1 << 8, - GTK_DEBUG_ICONTHEME = 1 << 9 + GTK_DEBUG_ICONTHEME = 1 << 9, + GTK_DEBUG_PRINTING = 1 << 10 } GtkDebugFlag; #ifdef G_ENABLE_DEBUG diff --git a/gtk/gtkmain.c b/gtk/gtkmain.c index 8f2be6b798..770cbd1700 100644 --- a/gtk/gtkmain.c +++ b/gtk/gtkmain.c @@ -159,7 +159,8 @@ static const GDebugKey gtk_debug_keys[] = { {"multihead", GTK_DEBUG_MULTIHEAD}, {"modules", GTK_DEBUG_MODULES}, {"geometry", GTK_DEBUG_GEOMETRY}, - {"icontheme", GTK_DEBUG_ICONTHEME} + {"icontheme", GTK_DEBUG_ICONTHEME}, + {"printing", GTK_DEBUG_PRINTING} }; #endif /* G_ENABLE_DEBUG */ diff --git a/gtk/gtkprintbackend.c b/gtk/gtkprintbackend.c index 137312f499..14d6aedcd2 100644 --- a/gtk/gtkprintbackend.c +++ b/gtk/gtkprintbackend.c @@ -576,21 +576,19 @@ gtk_print_backend_find_printer (GtkPrintBackend *backend, void gtk_print_backend_print_stream (GtkPrintBackend *backend, GtkPrintJob *job, - gint data_fd, + GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error) + GDestroyNotify dnotify) { g_return_if_fail (GTK_IS_PRINT_BACKEND (backend)); GTK_PRINT_BACKEND_GET_CLASS (backend)->print_stream (backend, job, - data_fd, + data_io, callback, user_data, - dnotify, - error); + dnotify); } void diff --git a/gtk/gtkprintbackend.h b/gtk/gtkprintbackend.h index abebbd1155..ea2f0f7d9b 100644 --- a/gtk/gtkprintbackend.h +++ b/gtk/gtkprintbackend.h @@ -73,11 +73,10 @@ struct _GtkPrintBackendClass void (*request_printer_list) (GtkPrintBackend *backend); void (*print_stream) (GtkPrintBackend *backend, GtkPrintJob *job, - gint data_fd, + GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error); + GDestroyNotify dnotify); /* Printer methods: */ void (*printer_request_details) (GtkPrinter *printer); @@ -85,7 +84,7 @@ struct _GtkPrintBackendClass GtkPrintSettings *settings, gdouble height, gdouble width, - gint cache_fd); + GIOChannel *cache_io); GtkPrinterOptionSet * (*printer_get_options) (GtkPrinter *printer, GtkPrintSettings *settings, GtkPageSetup *page_setup, @@ -135,11 +134,10 @@ GtkPrinter *gtk_print_backend_find_printer (GtkPrintBackend *pri const gchar *printer_name); void gtk_print_backend_print_stream (GtkPrintBackend *print_backend, GtkPrintJob *job, - gint data_fd, + GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error); + GDestroyNotify dnotify); GList * gtk_print_backend_load_modules (void); void gtk_print_backend_destroy (GtkPrintBackend *print_backend); diff --git a/gtk/gtkprinter-private.h b/gtk/gtkprinter-private.h index 37a23a8bd2..58ea80780b 100644 --- a/gtk/gtkprinter-private.h +++ b/gtk/gtkprinter-private.h @@ -48,7 +48,7 @@ cairo_surface_t * _gtk_printer_create_cairo_surface (GtkPrinter GtkPrintSettings *settings, gdouble width, gdouble height, - gint cache_fd); + GIOChannel *cache_io); GList * _gtk_printer_list_papers (GtkPrinter *printer); void _gtk_printer_get_hard_margins (GtkPrinter *printer, gdouble *top, diff --git a/gtk/gtkprinter.c b/gtk/gtkprinter.c index eab78b3f9c..d0320452e9 100644 --- a/gtk/gtkprinter.c +++ b/gtk/gtkprinter.c @@ -754,12 +754,12 @@ _gtk_printer_create_cairo_surface (GtkPrinter *printer, GtkPrintSettings *settings, gdouble width, gdouble height, - gint cache_fd) + GIOChannel *cache_io) { GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend); return backend_class->printer_create_cairo_surface (printer, settings, - width, height, cache_fd); + width, height, cache_io); } GList * diff --git a/gtk/gtkprintjob.c b/gtk/gtkprintjob.c index 88f1d58e8f..d84796b7e3 100644 --- a/gtk/gtkprintjob.c +++ b/gtk/gtkprintjob.c @@ -47,7 +47,7 @@ struct _GtkPrintJobPrivate { gchar *title; - gint spool_file_fd; + GIOChannel *spool_io; cairo_surface_t *surface; GtkPrintStatus status; @@ -183,7 +183,7 @@ gtk_print_job_init (GtkPrintJob *job) priv = job->priv = GTK_PRINT_JOB_GET_PRIVATE (job); - priv->spool_file_fd = -1; + priv->spool_io = NULL; priv->title = g_strdup (""); priv->surface = NULL; @@ -244,10 +244,10 @@ gtk_print_job_finalize (GObject *object) GtkPrintJob *job = GTK_PRINT_JOB (object); GtkPrintJobPrivate *priv = job->priv; - if (priv->spool_file_fd >= 0) + if (priv->spool_io != NULL) { - close (priv->spool_file_fd); - priv->spool_file_fd = -1; + g_io_channel_unref (priv->spool_io); + priv->spool_io = NULL; } if (priv->backend) @@ -414,29 +414,26 @@ gtk_print_job_set_source_file (GtkPrintJob *job, GError **error) { GtkPrintJobPrivate *priv; + GError *tmp_error; + + tmp_error = NULL; g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE); priv = job->priv; - priv->spool_file_fd = g_open (filename, O_RDONLY|O_BINARY); - if (priv->spool_file_fd < 0) - { - gchar *display_filename = g_filename_display_name (filename); - int save_errno = errno; - - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (save_errno), - _("Failed to open file '%s': %s"), - display_filename, - g_strerror (save_errno)); - - g_free (display_filename); + priv->spool_io = g_io_channel_new_file (filename, "r", &tmp_error); + if (tmp_error == NULL) + g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error); + + if (tmp_error != NULL) + { + g_propagate_error (error, tmp_error); return FALSE; } - return TRUE; + + return TRUE; } /** @@ -459,7 +456,11 @@ gtk_print_job_get_surface (GtkPrintJob *job, gchar *filename; gdouble width, height; GtkPaperSize *paper_size; - + int fd; + GError *tmp_error; + + tmp_error = NULL; + g_return_val_if_fail (GTK_IS_PRINT_JOB (job), NULL); priv = job->priv; @@ -467,26 +468,46 @@ gtk_print_job_get_surface (GtkPrintJob *job, if (priv->surface) return priv->surface; - g_return_val_if_fail (priv->spool_file_fd == -1, NULL); + g_return_val_if_fail (priv->spool_io == NULL, NULL); - priv->spool_file_fd = g_file_open_tmp ("gtkprint_XXXXXX", - &filename, - error); - if (priv->spool_file_fd == -1) - return NULL; + fd = g_file_open_tmp ("gtkprint_XXXXXX", + &filename, + &tmp_error); + if (fd == -1) + { + g_propagate_error (error, tmp_error); + return NULL; + } - fchmod (priv->spool_file_fd, S_IRUSR | S_IWUSR); + fchmod (fd, S_IRUSR | S_IWUSR); + +#ifdef G_ENABLE_DEBUG + /* If we are debugging printing don't delete the tmp files */ + if (!(gtk_debug_flags & GTK_DEBUG_PRINTING)) +#endif /* G_ENABLE_DEBUG */ unlink (filename); paper_size = gtk_page_setup_get_paper_size (priv->page_setup); width = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS); height = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS); + + priv->spool_io = g_io_channel_unix_new (fd); + g_io_channel_set_close_on_unref (priv->spool_io, TRUE); + g_io_channel_set_encoding (priv->spool_io, NULL, &tmp_error); + if (tmp_error != NULL) + { + g_io_channel_unref (priv->spool_io); + priv->spool_io = NULL; + g_propagate_error (error, tmp_error); + return NULL; + } + priv->surface = _gtk_printer_create_cairo_surface (priv->printer, priv->settings, width, height, - priv->spool_file_fd); - + priv->spool_io); + return priv->surface; } @@ -629,46 +650,34 @@ gtk_print_job_get_property (GObject *object, /** * gtk_print_job_send: * @job: a GtkPrintJob - * @callback: function to call when the job completes + * @callback: function to call when the job completes or an error occures * @user_data: user data that gets passed to @callback * @dnotify: destroy notify for @user_data - * @error: return location for errors, or %NULL * * Sends the print job off to the printer. * - * Return value: %FALSE if an error occurred - * * Since: 2.10 **/ -gboolean +void gtk_print_job_send (GtkPrintJob *job, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error) + GDestroyNotify dnotify) { GtkPrintJobPrivate *priv; - GError *print_error = NULL; - g_return_val_if_fail (GTK_IS_PRINT_JOB (job), FALSE); + g_return_if_fail (GTK_IS_PRINT_JOB (job)); priv = job->priv; - g_return_val_if_fail (priv->spool_file_fd > 0, FALSE); + g_return_if_fail (priv->spool_io != NULL); gtk_print_job_set_status (job, GTK_PRINT_STATUS_SENDING_DATA); - lseek (priv->spool_file_fd, 0, SEEK_SET); + + g_io_channel_seek_position (priv->spool_io, 0, G_SEEK_SET, NULL); + gtk_print_backend_print_stream (priv->backend, job, - priv->spool_file_fd, - callback, user_data, dnotify, - &print_error); - if (print_error) - { - g_propagate_error (error, print_error); - - return FALSE; - } - - return TRUE; + priv->spool_io, + callback, user_data, dnotify); } GType diff --git a/gtk/gtkprintjob.h b/gtk/gtkprintjob.h index ae71832a9a..0a205e7438 100644 --- a/gtk/gtkprintjob.h +++ b/gtk/gtkprintjob.h @@ -108,11 +108,10 @@ cairo_surface_t *gtk_print_job_get_surface (GtkPrintJob void gtk_print_job_set_track_print_status (GtkPrintJob *job, gboolean track_status); gboolean gtk_print_job_get_track_print_status (GtkPrintJob *job); -gboolean gtk_print_job_send (GtkPrintJob *job, +void gtk_print_job_send (GtkPrintJob *job, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error); + GDestroyNotify dnotify); GType gtk_print_capabilities_get_type (void) G_GNUC_CONST; diff --git a/gtk/gtkprintoperation-unix.c b/gtk/gtkprintoperation-unix.c index a8fbd03eee..126771491d 100644 --- a/gtk/gtkprintoperation-unix.c +++ b/gtk/gtkprintoperation-unix.c @@ -306,8 +306,7 @@ unix_end_run (GtkPrintOperation *op, if (op_unix->job != NULL) gtk_print_job_send (op_unix->job, unix_finish_send, - op_unix, NULL, - NULL); + op_unix, NULL); if (wait) { diff --git a/modules/printbackends/Makefile.am b/modules/printbackends/Makefile.am index 970ef195ce..48d3fa26ba 100644 --- a/modules/printbackends/Makefile.am +++ b/modules/printbackends/Makefile.am @@ -4,4 +4,4 @@ if HAVE_CUPS SUBDIRS += cups endif -DIST_SUBDIRS=cups pdf lpr +DIST_SUBDIRS=cups file lpr diff --git a/modules/printbackends/cups/Makefile.am b/modules/printbackends/cups/Makefile.am index 0174598256..e768c5bd47 100644 --- a/modules/printbackends/cups/Makefile.am +++ b/modules/printbackends/cups/Makefile.am @@ -10,7 +10,8 @@ INCLUDES = \ -I$(top_builddir)/gdk \ $(CUPS_CFLAGS) \ -DGTK_PRINT_BACKEND_ENABLE_UNSUPPORTED \ - $(GTK_DEP_CFLAGS) + $(GTK_DEP_CFLAGS) \ + $(GTK_DEBUG_FLAGS) LDADDS = \ $(GTK_DEP_LIBS) \ diff --git a/modules/printbackends/cups/gtkcupsutils.c b/modules/printbackends/cups/gtkcupsutils.c index 32503feab3..d2c5462554 100644 --- a/modules/printbackends/cups/gtkcupsutils.c +++ b/modules/printbackends/cups/gtkcupsutils.c @@ -20,6 +20,8 @@ */ #include "gtkcupsutils.h" +#include "config.h" +#include "gtkdebug.h" #include #include @@ -86,7 +88,7 @@ GtkCupsRequest * gtk_cups_request_new (http_t *connection, GtkCupsRequestType req_type, gint operation_id, - gint data_fd, + GIOChannel *data_io, const char *server, const char *resource) { @@ -135,7 +137,7 @@ gtk_cups_request_new (http_t *connection, request->last_status = HTTP_CONTINUE; request->attempts = 0; - request->data_fd = data_fd; + request->data_io = data_io; request->ipp_request = ippNew(); request->ipp_request->request.op.operation_id = operation_id; @@ -514,11 +516,14 @@ _post_send (GtkCupsRequest *request) gchar length[255]; struct stat data_info; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + request->poll_state = GTK_CUPS_HTTP_WRITE; - if (request->data_fd != 0) + if (request->data_io != NULL) { - fstat (request->data_fd, &data_info); + fstat (g_io_channel_unix_get_fd (request->data_io), &data_info); sprintf (length, "%lu", (unsigned long)ippLength(request->ipp_request) + data_info.st_size); } else @@ -556,6 +561,9 @@ _post_write_request (GtkCupsRequest *request) { ipp_state_t ipp_status; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + request->poll_state = GTK_CUPS_HTTP_WRITE; ipp_status = ippWrite(request->http, request->ipp_request); @@ -571,7 +579,7 @@ _post_write_request (GtkCupsRequest *request) if (ipp_status == IPP_DATA) { - if (request->data_fd != 0) + if (request->data_io != NULL) request->state = GTK_CUPS_POST_WRITE_DATA; else { @@ -584,10 +592,13 @@ _post_write_request (GtkCupsRequest *request) static void _post_write_data (GtkCupsRequest *request) { - ssize_t bytes; + gsize bytes; char buffer[_GTK_CUPS_MAX_CHUNK_SIZE]; http_status_t http_status; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + request->poll_state = GTK_CUPS_HTTP_WRITE; if (httpCheck (request->http)) @@ -600,10 +611,30 @@ _post_write_data (GtkCupsRequest *request) if (http_status == HTTP_CONTINUE || http_status == HTTP_OK) { - /* send data */ - bytes = read(request->data_fd, buffer, _GTK_CUPS_MAX_CHUNK_SIZE); + GIOStatus io_status; + GError *error; - if (bytes == 0) + error = NULL; + + /* send data */ + io_status = + g_io_channel_read_chars (request->data_io, + buffer, + _GTK_CUPS_MAX_CHUNK_SIZE, + &bytes, + &error); + + if (io_status == G_IO_STATUS_ERROR) + { + request->state = GTK_CUPS_POST_DONE; + request->poll_state = GTK_CUPS_HTTP_IDLE; + + gtk_cups_result_set_error (request->result, "Error reading from cache file: %s", error->message); + + g_error_free (error); + return; + } + else if (bytes == 0 && io_status == G_IO_STATUS_EOF) { request->state = GTK_CUPS_POST_CHECK; request->poll_state = GTK_CUPS_HTTP_READ; @@ -611,16 +642,13 @@ _post_write_data (GtkCupsRequest *request) request->attempts = 0; return; } - else if (bytes == -1) - { - request->state = GTK_CUPS_POST_DONE; - request->poll_state = GTK_CUPS_HTTP_IDLE; - - gtk_cups_result_set_error (request->result, "Error reading from cache file: %s", strerror (errno)); - return; - } - - if (httpWrite(request->http, buffer, (int)bytes) < bytes) + + +#if HAVE_CUPS_API_1_2 + if (httpWrite2(request->http, buffer, bytes) < bytes) +#else + if (httpWrite(request->http, buffer, (int) bytes) < bytes) +#endif /* HAVE_CUPS_API_1_2 */ { request->state = GTK_CUPS_POST_DONE; request->poll_state = GTK_CUPS_HTTP_IDLE; @@ -642,6 +670,9 @@ _post_check (GtkCupsRequest *request) http_status = request->last_status; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s - status %i\n", G_STRFUNC, http_status)); + request->poll_state = GTK_CUPS_HTTP_READ; if (http_status == HTTP_CONTINUE) @@ -744,6 +775,9 @@ _post_read_response (GtkCupsRequest *request) { ipp_state_t ipp_status; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + request->poll_state = GTK_CUPS_HTTP_READ; if (request->result->ipp_response == NULL) @@ -772,11 +806,14 @@ _post_read_response (GtkCupsRequest *request) static void _get_send (GtkCupsRequest *request) { + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + request->poll_state = GTK_CUPS_HTTP_WRITE; - if (request->data_fd == 0) + if (request->data_io == NULL) { - gtk_cups_result_set_error (request->result, "Get requires an open file descriptor"); + gtk_cups_result_set_error (request->result, "Get requires an open io channel"); request->state = GTK_CUPS_GET_DONE; request->poll_state = GTK_CUPS_HTTP_IDLE; @@ -813,6 +850,9 @@ _get_check (GtkCupsRequest *request) { http_status_t http_status; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + http_status = request->last_status; request->poll_state = GTK_CUPS_HTTP_READ; @@ -894,12 +934,27 @@ static void _get_read_data (GtkCupsRequest *request) { char buffer[_GTK_CUPS_MAX_CHUNK_SIZE]; - int bytes; - - request->poll_state = GTK_CUPS_HTTP_READ; - - bytes = httpRead(request->http, buffer, sizeof(buffer)); + gsize bytes; + gsize bytes_written; + GIOStatus io_status; + GError *error; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + + error = NULL; + + request->poll_state = GTK_CUPS_HTTP_READ; + +#if HAVE_CUPS_API_1_2 + bytes = httpRead2(request->http, buffer, sizeof(buffer)); +#else + bytes = httpRead(request->http, buffer, sizeof(buffer)); +#endif /* HAVE_CUPS_API_1_2 */ + + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %i bytes read\n", bytes)); + if (bytes == 0) { request->state = GTK_CUPS_GET_DONE; @@ -907,16 +962,21 @@ _get_read_data (GtkCupsRequest *request) return; } - - if (write (request->data_fd, buffer, bytes) == -1) - { - char *error_msg; + + io_status = + g_io_channel_write_chars (request->data_io, + buffer, + bytes, + &bytes_written, + &error); + if (io_status == G_IO_STATUS_ERROR) + { request->state = GTK_CUPS_POST_DONE; request->poll_state = GTK_CUPS_HTTP_IDLE; - error_msg = strerror (errno); - gtk_cups_result_set_error (request->result, error_msg ? error_msg:""); + gtk_cups_result_set_error (request->result, error->message); + g_error_free (error); } } diff --git a/modules/printbackends/cups/gtkcupsutils.h b/modules/printbackends/cups/gtkcupsutils.h index 88fb8810f2..d759459aea 100644 --- a/modules/printbackends/cups/gtkcupsutils.h +++ b/modules/printbackends/cups/gtkcupsutils.h @@ -61,7 +61,7 @@ struct _GtkCupsRequest gchar *server; gchar *resource; - gint data_fd; + GIOChannel *data_io; gint attempts; GtkCupsResult *result; @@ -100,7 +100,7 @@ enum GtkCupsRequest * gtk_cups_request_new (http_t *connection, GtkCupsRequestType req_type, gint operation_id, - gint data_fd, + GIOChannel *data_io, const char *server, const char *resource); void gtk_cups_request_ipp_add_string (GtkCupsRequest *request, diff --git a/modules/printbackends/cups/gtkprintbackendcups.c b/modules/printbackends/cups/gtkprintbackendcups.c index a9eb39a12b..1877aaa797 100644 --- a/modules/printbackends/cups/gtkprintbackendcups.c +++ b/modules/printbackends/cups/gtkprintbackendcups.c @@ -47,7 +47,7 @@ #include "gtkprintercups.h" #include "gtkcupsutils.h" - +#include "gtkdebug.h" typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass; @@ -147,16 +147,15 @@ static void cups_begin_polling_info (GtkPrintBack static gboolean cups_job_info_poll_timeout (gpointer user_data); static void gtk_print_backend_cups_print_stream (GtkPrintBackend *backend, GtkPrintJob *job, - gint data_fd, + GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error); + GDestroyNotify dnotify); static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter *printer, GtkPrintSettings *settings, gdouble width, gdouble height, - gint cache_fd); + GIOChannel *cache_io); static void @@ -184,6 +183,9 @@ gtk_print_backend_cups_register_type (GTypeModule *module) G_MODULE_EXPORT void pb_module_init (GTypeModule *module) { + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: Initializing the CUPS print backend module\n")); + gtk_print_backend_cups_register_type (module); gtk_printer_cups_register_type (module); } @@ -221,6 +223,9 @@ gtk_print_backend_cups_get_type (void) GtkPrintBackend * gtk_print_backend_cups_new (void) { + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: Creating a new CUPS print backend object\n")); + return g_object_new (GTK_TYPE_PRINT_BACKEND_CUPS, NULL); } @@ -253,21 +258,31 @@ _cairo_write_to_cups (void *closure, const unsigned char *data, unsigned int length) { - gint fd = GPOINTER_TO_INT (closure); - gssize written; - + GIOChannel *io = (GIOChannel *)closure; + gsize written; + GError *error; + + error = NULL; + + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: Writting %i byte chunk to temp file\n", length)); + while (length > 0) { - written = write (fd, data, length); + g_io_channel_write_chars (io, data, length, &written, &error); - if (written == -1) + if (error != NULL) { - if (errno == EAGAIN || errno == EINTR) - continue; - + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: Error writting to temp file, %s\n", error->message)); + + g_error_free (error); return CAIRO_STATUS_WRITE_ERROR; } + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: Wrote %i bytes to temp file\n", written)); + data += written; length -= written; } @@ -280,13 +295,13 @@ cups_printer_create_cairo_surface (GtkPrinter *printer, GtkPrintSettings *settings, gdouble width, gdouble height, - gint cache_fd) + GIOChannel *cache_io) { cairo_surface_t *surface; /* TODO: check if it is a ps or pdf printer */ - surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, GINT_TO_POINTER (cache_fd), width, height); + surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height); /* TODO: DPI from settings object? */ cairo_surface_set_fallback_resolution (surface, 300, 300); @@ -304,6 +319,9 @@ typedef struct { static void cups_free_print_stream_data (CupsPrintStreamData *data) { + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + if (data->dnotify) data->dnotify (data->user_data); g_object_unref (data->job); @@ -318,6 +336,9 @@ cups_print_cb (GtkPrintBackendCups *print_backend, GError *error = NULL; CupsPrintStreamData *ps = user_data; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + if (gtk_cups_result_is_error (result)) error = g_error_new_literal (gtk_print_error_quark (), GTK_PRINT_ERROR_INTERNAL_ERROR, @@ -373,11 +394,10 @@ add_cups_options (const gchar *key, static void gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend, GtkPrintJob *job, - gint data_fd, + GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error) + GDestroyNotify dnotify) { GtkPrinterCups *cups_printer; CupsPrintStreamData *ps; @@ -385,13 +405,16 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend, GtkPrintSettings *settings; const gchar *title; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job)); settings = gtk_print_job_get_settings (job); request = gtk_cups_request_new (NULL, GTK_CUPS_POST, IPP_PRINT_JOB, - data_fd, + data_io, NULL, cups_printer->device_uri); @@ -435,6 +458,9 @@ static void gtk_print_backend_cups_finalize (GObject *object) { GtkPrintBackendCups *backend_cups; + + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: finalizing CUPS backend module\n")); backend_cups = GTK_PRINT_BACKEND_CUPS (object); @@ -449,6 +475,9 @@ gtk_print_backend_cups_dispose (GObject *object) { GtkPrintBackendCups *backend_cups; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + backend_cups = GTK_PRINT_BACKEND_CUPS (object); if (backend_cups->list_printers_poll > 0) @@ -466,6 +495,9 @@ cups_dispatch_watch_check (GSource *source) GtkCupsPollState poll_state; gboolean result; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s \n", G_STRFUNC, source)); + dispatch = (GtkPrintCupsDispatchWatch *) source; poll_state = gtk_cups_request_get_poll_state (dispatch->request); @@ -514,7 +546,10 @@ cups_dispatch_watch_prepare (GSource *source, GtkPrintCupsDispatchWatch *dispatch; dispatch = (GtkPrintCupsDispatchWatch *) source; - + + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s \n", G_STRFUNC, source)); + *timeout_ = -1; return gtk_cups_request_read_write (dispatch->request); @@ -537,8 +572,11 @@ cups_dispatch_watch_dispatch (GSource *source, result = gtk_cups_request_get_result (dispatch->request); + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s \n", G_STRFUNC, source)); + if (gtk_cups_result_is_error (result)) - g_warning (gtk_cups_result_get_error_string (result)); + g_warning ("Error result: %s", gtk_cups_result_get_error_string (result)); ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data); @@ -550,6 +588,9 @@ cups_dispatch_watch_finalize (GSource *source) { GtkPrintCupsDispatchWatch *dispatch; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s \n", G_STRFUNC, source)); + dispatch = (GtkPrintCupsDispatchWatch *) source; gtk_cups_request_free (dispatch->request); @@ -593,6 +634,9 @@ cups_request_execute (GtkPrintBackendCups *print_backend, dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs, sizeof (GtkPrintCupsDispatchWatch)); + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource)); + dispatch->request = request; dispatch->backend = g_object_ref (print_backend); dispatch->data_poll = NULL; @@ -625,8 +669,15 @@ cups_request_printer_info_cb (GtkPrintBackendCups *backend, printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend), printer_name); + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name)); + if (!printer) - return; + { + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name)); + return; + } cups_printer = GTK_PRINTER_CUPS (printer); @@ -692,7 +743,7 @@ cups_request_printer_info (GtkPrintBackendCups *print_backend, request = gtk_cups_request_new (NULL, GTK_CUPS_POST, IPP_GET_PRINTER_ATTRIBUTES, - 0, + NULL, NULL, NULL); @@ -701,6 +752,9 @@ cups_request_printer_info (GtkPrintBackendCups *print_backend, gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer_uri); + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri)); + g_free (printer_uri); gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, @@ -823,7 +877,7 @@ cups_request_job_info (CupsJobPollData *data) request = gtk_cups_request_new (NULL, GTK_CUPS_POST, IPP_GET_JOB_ATTRIBUTES, - 0, + NULL, NULL, NULL); @@ -1080,7 +1134,7 @@ cups_request_printer_list (GtkPrintBackendCups *cups_backend) request = gtk_cups_request_new (NULL, GTK_CUPS_POST, CUPS_GET_PRINTERS, - 0, + NULL, NULL, NULL); @@ -1114,16 +1168,16 @@ cups_get_printer_list (GtkPrintBackend *backend) typedef struct { GtkPrinterCups *printer; - gint ppd_fd; - gchar *ppd_filename; + GIOChannel *ppd_io; } GetPPDData; static void get_ppd_data_free (GetPPDData *data) { - close (data->ppd_fd); - unlink (data->ppd_filename); - g_free (data->ppd_filename); + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + + g_io_channel_unref (data->ppd_io); g_object_unref (data->printer); g_free (data); } @@ -1136,6 +1190,9 @@ cups_request_ppd_cb (GtkPrintBackendCups *print_backend, ipp_t *response; GtkPrinter *printer; + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + printer = GTK_PRINTER (data->printer); GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE; @@ -1147,7 +1204,10 @@ cups_request_ppd_cb (GtkPrintBackendCups *print_backend, response = gtk_cups_result_get_response (result); - data->printer->ppd_file = ppdOpenFile (data->ppd_filename); + /* let ppdOpenFd take over the ownership of the open file */ + g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL); + data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io))); + gtk_printer_set_has_details (printer, TRUE); g_signal_emit_by_name (printer, "details-acquired", printer, TRUE); } @@ -1159,13 +1219,19 @@ cups_request_ppd (GtkPrinter *printer) GtkPrintBackend *print_backend; GtkPrinterCups *cups_printer; GtkCupsRequest *request; + char *ppd_filename; gchar *resource; http_t *http; GetPPDData *data; - + int fd; + cups_printer = GTK_PRINTER_CUPS (printer); error = NULL; + + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: %s\n", G_STRFUNC)); + /* FIXME this can return NULL! */ http = httpConnectEncrypt (cups_printer->hostname, cups_printer->port, @@ -1173,22 +1239,34 @@ cups_request_ppd (GtkPrinter *printer) data = g_new0 (GetPPDData, 1); - data->ppd_fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX", - &data->ppd_filename, - &error); + fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX", + &ppd_filename, + &error); + +#ifdef G_ENABLE_DEBUG + /* If we are debugging printing don't delete the tmp files */ + if (!(gtk_debug_flags & GTK_DEBUG_PRINTING)) + unlink (ppd_filename); +#else + unlink (ppd_filename); +#endif /* G_ENABLE_DEBUG */ if (error != NULL) { g_warning ("%s", error->message); g_error_free (error); httpClose (http); + g_free (ppd_filename); g_free (data); g_signal_emit_by_name (printer, "details-acquired", printer, FALSE); return; } - fchmod (data->ppd_fd, S_IRUSR | S_IWUSR); + fchmod (fd, S_IRUSR | S_IWUSR); + data->ppd_io = g_io_channel_unix_new (fd); + g_io_channel_set_encoding (data->ppd_io, NULL, NULL); + g_io_channel_set_close_on_unref (data->ppd_io, TRUE); data->printer = g_object_ref (printer); @@ -1196,12 +1274,16 @@ cups_request_ppd (GtkPrinter *printer) request = gtk_cups_request_new (http, GTK_CUPS_GET, 0, - data->ppd_fd, + data->ppd_io, cups_printer->hostname, resource); + GTK_NOTE (PRINTING, + g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename)); + g_free (resource); - + g_free (ppd_filename); + cups_printer->reading_ppd = TRUE; print_backend = gtk_printer_get_backend (printer); @@ -1259,7 +1341,7 @@ cups_request_default_printer (GtkPrintBackendCups *print_backend) request = gtk_cups_request_new (NULL, GTK_CUPS_POST, CUPS_GET_DEFAULT, - 0, + NULL, NULL, NULL); @@ -2007,7 +2089,8 @@ cups_printer_get_options (GtkPrinter *printer, { GtkPaperSize *paper_size; ppd_option_t *option; - + ppd_coption_t *coption; + ppdMarkDefaults (ppd_file); paper_size = gtk_page_setup_get_paper_size (page_setup); diff --git a/modules/printbackends/file/gtkprintbackendfile.c b/modules/printbackends/file/gtkprintbackendfile.c index f4461ea44e..6482d3ca1b 100644 --- a/modules/printbackends/file/gtkprintbackendfile.c +++ b/modules/printbackends/file/gtkprintbackendfile.c @@ -79,16 +79,15 @@ static void file_printer_prepare_for_print (GtkPrinter GtkPageSetup *page_setup); static void gtk_print_backend_file_print_stream (GtkPrintBackend *print_backend, GtkPrintJob *job, - gint data_fd, + GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error); + GDestroyNotify dnotify); static cairo_surface_t * file_printer_create_cairo_surface (GtkPrinter *printer, GtkPrintSettings *settings, gdouble width, gdouble height, - gint cache_fd); + GIOChannel *cache_io); static void gtk_print_backend_file_register_type (GTypeModule *module) @@ -173,21 +172,31 @@ _cairo_write (void *closure, const unsigned char *data, unsigned int length) { - gint fd = GPOINTER_TO_INT (closure); - gssize written; - + GIOChannel *io = (GIOChannel *)closure; + gsize written; + GError *error; + + error = NULL; + + GTK_NOTE (PRINTING, + g_print ("FILE Backend: Writting %i byte chunk to temp file\n", length)); + while (length > 0) { - written = write (fd, data, length); + g_io_channel_write_chars (io, data, length, &written, &error); - if (written == -1) + if (error != NULL) { - if (errno == EAGAIN || errno == EINTR) - continue; - + GTK_NOTE (PRINTING, + g_print ("FILE Backend: Error writting to temp file, %s\n", error->message)); + + g_error_free (error); return CAIRO_STATUS_WRITE_ERROR; } + GTK_NOTE (PRINTING, + g_print ("FILE Backend: Wrote %i bytes to temp file\n", written)); + data += written; length -= written; } @@ -201,11 +210,11 @@ file_printer_create_cairo_surface (GtkPrinter *printer, GtkPrintSettings *settings, gdouble width, gdouble height, - gint cache_fd) + GIOChannel *cache_io) { cairo_surface_t *surface; - surface = cairo_pdf_surface_create_for_stream (_cairo_write, GINT_TO_POINTER (cache_fd), width, height); + surface = cairo_pdf_surface_create_for_stream (_cairo_write, cache_io, width, height); /* TODO: DPI from settings object? */ cairo_surface_set_fallback_resolution (surface, 300, 300); @@ -217,7 +226,7 @@ typedef struct { GtkPrintBackend *backend; GtkPrintJobCompleteFunc callback; GtkPrintJob *job; - gint target_fd; + GIOChannel *target_io; gpointer user_data; GDestroyNotify dnotify; } _PrintStreamData; @@ -229,8 +238,8 @@ file_print_cb (GtkPrintBackendFile *print_backend, { _PrintStreamData *ps = (_PrintStreamData *) user_data; - if (ps->target_fd > 0) - close (ps->target_fd); + if (ps->target_io != NULL) + g_io_channel_unref (ps->target_io); if (ps->callback) ps->callback (ps->job, ps->user_data, error); @@ -255,64 +264,69 @@ file_write (GIOChannel *source, gchar buf[_STREAM_MAX_CHUNK_SIZE]; gsize bytes_read; GError *error; + GIOStatus read_status; _PrintStreamData *ps = (_PrintStreamData *) user_data; - gint source_fd; error = NULL; - source_fd = g_io_channel_unix_get_fd (source); + read_status = + g_io_channel_read_chars (source, + buf, + _STREAM_MAX_CHUNK_SIZE, + &bytes_read, + &error); - bytes_read = read (source_fd, - buf, - _STREAM_MAX_CHUNK_SIZE); + if (read_status != G_IO_STATUS_ERROR) + { + gsize bytes_written; - if (bytes_read > 0) - { - if (write (ps->target_fd, buf, bytes_read) == -1) - { - error = g_error_new (GTK_PRINT_ERROR, - GTK_PRINT_ERROR_INTERNAL_ERROR, - g_strerror (errno)); - } - } - else if (bytes_read == -1) - { - error = g_error_new (GTK_PRINT_ERROR, - GTK_PRINT_ERROR_INTERNAL_ERROR, - g_strerror (errno)); + g_io_channel_write_chars (ps->target_io, + buf, + bytes_read, + &bytes_written, + &error); } - if (bytes_read == 0 || error != NULL) + if (error != NULL || read_status == G_IO_STATUS_EOF) { file_print_cb (GTK_PRINT_BACKEND_FILE (ps->backend), error, user_data); + if (error != NULL) + { + GTK_NOTE (PRINTING, + g_print ("FILE Backend: %s\n", error->message)); + + g_error_free (error); + } + return FALSE; } + GTK_NOTE (PRINTING, + g_print ("FILE Backend: Writting %i byte chunk to target file\n", bytes_read)); + return TRUE; } static void gtk_print_backend_file_print_stream (GtkPrintBackend *print_backend, GtkPrintJob *job, - gint data_fd, + GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error) + GDestroyNotify dnotify) { GError *internal_error = NULL; GtkPrinter *printer; _PrintStreamData *ps; GtkPrintSettings *settings; - GIOChannel *save_channel; const gchar *uri; gchar *filename = NULL; printer = gtk_print_job_get_printer (job); settings = gtk_print_job_get_settings (job); - error = NULL; + internal_error = NULL; uri = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_URI); if (uri) @@ -330,24 +344,22 @@ gtk_print_backend_file_print_stream (GtkPrintBackend *print_backend, ps->job = g_object_ref (job); ps->backend = print_backend; - ps->target_fd = creat (filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); - g_free (filename); + ps->target_io = g_io_channel_new_file (filename, "w", &internal_error); - if (ps->target_fd == -1) + if (internal_error == NULL) + g_io_channel_set_encoding (ps->target_io, NULL, &internal_error); + + if (internal_error != NULL) { - internal_error = g_error_new (GTK_PRINT_ERROR, - GTK_PRINT_ERROR_INTERNAL_ERROR, - g_strerror (errno)); + file_print_cb (GTK_PRINT_BACKEND_FILE (print_backend), + internal_error, + ps); - file_print_cb (GTK_PRINT_BACKEND_FILE (print_backend), - internal_error, ps); - g_propagate_error (error, internal_error); + g_error_free (internal_error); return; } - - save_channel = g_io_channel_unix_new (data_fd); - g_io_add_watch (save_channel, + g_io_add_watch (data_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, (GIOFunc) file_write, ps); diff --git a/modules/printbackends/lpr/Makefile.am b/modules/printbackends/lpr/Makefile.am index 9cd83e81f5..305399c20f 100644 --- a/modules/printbackends/lpr/Makefile.am +++ b/modules/printbackends/lpr/Makefile.am @@ -9,7 +9,8 @@ INCLUDES = \ -I$(top_srcdir)/gdk \ -I$(top_builddir)/gdk \ -DGTK_PRINT_BACKEND_ENABLE_UNSUPPORTED \ - $(GTK_DEP_CFLAGS) + $(GTK_DEP_CFLAGS) \ + $(GTK_DEBUG_FLAGS) LDADDS = \ $(GTK_DEP_LIBS) \ diff --git a/modules/printbackends/lpr/gtkprintbackendlpr.c b/modules/printbackends/lpr/gtkprintbackendlpr.c index 1942131ebe..23c041df34 100644 --- a/modules/printbackends/lpr/gtkprintbackendlpr.c +++ b/modules/printbackends/lpr/gtkprintbackendlpr.c @@ -33,6 +33,7 @@ #include +#include "gtkdebug.h" #include "gtkprintoperation.h" #include "gtkprintbackendlpr.h" #include "gtkprinter.h" @@ -77,14 +78,13 @@ static cairo_surface_t * lpr_printer_create_cairo_surface (GtkPrinter GtkPrintSettings *settings, gdouble width, gdouble height, - gint cache_fd); + GIOChannel *cache_io); static void gtk_print_backend_lpr_print_stream (GtkPrintBackend *print_backend, GtkPrintJob *job, - gint data_fd, + GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error); + GDestroyNotify dnotify); static void gtk_print_backend_lpr_register_type (GTypeModule *module) @@ -169,21 +169,31 @@ _cairo_write (void *closure, const unsigned char *data, unsigned int length) { - gint fd = GPOINTER_TO_INT (closure); - gssize written; - + GIOChannel *io = (GIOChannel *)closure; + gsize written; + GError *error; + + error = NULL; + + GTK_NOTE (PRINTING, + g_print ("LPR Backend: Writting %i byte chunk to temp file\n", length)); + while (length > 0) { - written = write (fd, data, length); + g_io_channel_write_chars (io, data, length, &written, &error); - if (written == -1) + if (error != NULL) { - if (errno == EAGAIN || errno == EINTR) - continue; - + GTK_NOTE (PRINTING, + g_print ("LPR Backend: Error writting to temp file, %s\n", error->message)); + + g_error_free (error); return CAIRO_STATUS_WRITE_ERROR; } + GTK_NOTE (PRINTING, + g_print ("LPR Backend: Wrote %i bytes to temp file\n", written)); + data += written; length -= written; } @@ -191,17 +201,16 @@ _cairo_write (void *closure, return CAIRO_STATUS_SUCCESS; } - static cairo_surface_t * lpr_printer_create_cairo_surface (GtkPrinter *printer, GtkPrintSettings *settings, gdouble width, gdouble height, - gint cache_fd) + GIOChannel *cache_io) { cairo_surface_t *surface; - surface = cairo_ps_surface_create_for_stream (_cairo_write, GINT_TO_POINTER (cache_fd), width, height); + surface = cairo_ps_surface_create_for_stream (_cairo_write, cache_io, width, height); /* TODO: DPI from settings object? */ cairo_surface_set_fallback_resolution (surface, 300, 300); @@ -216,10 +225,7 @@ typedef struct { gpointer user_data; GDestroyNotify dnotify; - gint in; - gint out; - gint err; - + GIOChannel *in; } _PrintStreamData; static void @@ -229,14 +235,8 @@ lpr_print_cb (GtkPrintBackendLpr *print_backend, { _PrintStreamData *ps = (_PrintStreamData *) user_data; - if (ps->in > 0) - close (ps->in); - - if (ps->out > 0) - close (ps->out); - - if (ps->err > 0) - close (ps->err); + if (ps->in != NULL) + g_io_channel_unref (ps->in); if (ps->callback) ps->callback (ps->job, ps->user_data, error); @@ -262,34 +262,30 @@ lpr_write (GIOChannel *source, gchar buf[_LPR_MAX_CHUNK_SIZE]; gsize bytes_read; GError *error; + GIOStatus status; _PrintStreamData *ps = (_PrintStreamData *) user_data; - gint source_fd; error = NULL; - source_fd = g_io_channel_unix_get_fd (source); + status = + g_io_channel_read_chars (source, + buf, + _LPR_MAX_CHUNK_SIZE, + &bytes_read, + &error); - bytes_read = read (source_fd, - buf, - _LPR_MAX_CHUNK_SIZE); + if (status != G_IO_STATUS_ERROR) + { + gsize bytes_written; - if (bytes_read > 0) - { - if (write (ps->in, buf, bytes_read) == -1) - { - error = g_error_new (GTK_PRINT_ERROR, - GTK_PRINT_ERROR_INTERNAL_ERROR, - g_strerror (errno)); - } - } - else if (bytes_read == -1) - { - error = g_error_new (GTK_PRINT_ERROR, - GTK_PRINT_ERROR_INTERNAL_ERROR, - g_strerror (errno)); + g_io_channel_write_chars (ps->in, + buf, + bytes_read, + &bytes_written, + &error); } - if (bytes_read == 0 || error != NULL) + if (error != NULL || status == G_IO_STATUS_EOF) { lpr_print_cb (GTK_PRINT_BACKEND_LPR (ps->backend), error, user_data); @@ -297,9 +293,21 @@ lpr_write (GIOChannel *source, if (error) g_error_free (error); + if (error != NULL) + { + GTK_NOTE (PRINTING, + g_print ("LPR Backend: %s\n", error->message)); + + g_error_free (error); + } + return FALSE; } + GTK_NOTE (PRINTING, + g_print ("LPR Backend: Writting %i byte chunk to lpr pipe\n", bytes_read)); + + return TRUE; } @@ -308,21 +316,20 @@ lpr_write (GIOChannel *source, static void gtk_print_backend_lpr_print_stream (GtkPrintBackend *print_backend, GtkPrintJob *job, - gint data_fd, + GIOChannel *data_io, GtkPrintJobCompleteFunc callback, gpointer user_data, - GDestroyNotify dnotify, - GError **error) + GDestroyNotify dnotify) { GError *print_error = NULL; GtkPrinter *printer; _PrintStreamData *ps; GtkPrintSettings *settings; - GIOChannel *send_channel; gint argc; - gchar **argv; - const gchar *cmd_line; - + gint in_fd; + gchar **argv = NULL; + const char *cmd_line; + printer = gtk_print_job_get_printer (job); settings = gtk_print_job_get_settings (job); @@ -335,18 +342,11 @@ gtk_print_backend_lpr_print_stream (GtkPrintBackend *print_backend, ps->user_data = user_data; ps->dnotify = dnotify; ps->job = g_object_ref (job); - ps->in = 0; - ps->out = 0; - ps->err = 0; + ps->in = NULL; /* spawn lpr with pipes and pipe ps file to lpr */ if (!g_shell_parse_argv (cmd_line, &argc, &argv, &print_error)) - { - lpr_print_cb (GTK_PRINT_BACKEND_LPR (print_backend), - print_error, ps); - g_propagate_error (error, print_error); - return; - } + goto out; if (!g_spawn_async_with_pipes (NULL, argv, @@ -355,27 +355,38 @@ gtk_print_backend_lpr_print_stream (GtkPrintBackend *print_backend, NULL, NULL, NULL, - &ps->in, - &ps->out, - &ps->err, + &in_fd, + NULL, + NULL, &print_error)) - { - lpr_print_cb (GTK_PRINT_BACKEND_LPR (print_backend), - print_error, ps); - g_propagate_error (error, print_error); goto out; + ps->in = g_io_channel_unix_new (in_fd); + + g_io_channel_set_encoding (ps->in, NULL, &print_error); + if (print_error != NULL) + { + if (ps->in != NULL) + g_io_channel_unref (ps->in); + + goto out; } - send_channel = g_io_channel_unix_new (data_fd); - - g_io_add_watch (send_channel, + g_io_add_watch (data_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, (GIOFunc) lpr_write, ps); out: - g_strfreev (argv); + if (argv != NULL) + g_strfreev (argv); + + if (print_error != NULL) + { + lpr_print_cb (GTK_PRINT_BACKEND_LPR (print_backend), + print_error, ps); + g_error_free (print_error); + } } static void