]> www.fi.muni.cz Git - evince.git/commitdiff
Show progress information when printing. Fixes bug #482770.
authorCarlos Garcia Campos <carlosgc@gnome.org>
Sat, 27 Dec 2008 18:37:45 +0000 (18:37 +0000)
committerCarlos Garcia Campos <carlosgc@src.gnome.org>
Sat, 27 Dec 2008 18:37:45 +0000 (18:37 +0000)
2008-12-27  Carlos Garcia Campos  <carlosgc@gnome.org>

* shell/ev-print-operation.[ch]: (ev_print_operation_class_init),
(ev_print_operation_get_job_name),
(ev_print_operation_get_status),
(ev_print_operation_get_progress),
(ev_print_operation_update_status),
(ev_print_operation_export_get_job_name), (clamp_ranges),
(get_first_and_last_page), (ev_print_operation_export_run_next),
(print_job_finished), (export_print_done), (export_job_cancelled),
(update_progress), (export_print_page),
(ev_print_operation_export_begin),
(ev_print_operation_export_print_dialog_response_cb),
(ev_print_operation_export_run),
(ev_print_operation_export_cancel),
(ev_print_operation_export_class_init):
* shell/ev-window.c:
(ev_window_print_update_pending_jobs_message),
(ev_window_print_operation_done),
(ev_window_print_progress_response_cb),
(ev_window_print_operation_status_changed),
(ev_window_print_operation_begin_print), (ev_window_print_range),
(ev_window_dispose):

Show progress information when printing. Fixes bug #482770.

svn path=/trunk/; revision=3309

ChangeLog
shell/ev-print-operation.c
shell/ev-print-operation.h
shell/ev-window.c

index f6a308db49932e259a3002919bb6c409e82cf16d..2eb94ef4ecc0f0d377d59a2a0fee7108873bdc9b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,29 @@
+2008-12-27  Carlos Garcia Campos  <carlosgc@gnome.org>
+
+       * shell/ev-print-operation.[ch]: (ev_print_operation_class_init),
+       (ev_print_operation_get_job_name),
+       (ev_print_operation_get_status),
+       (ev_print_operation_get_progress),
+       (ev_print_operation_update_status),
+       (ev_print_operation_export_get_job_name), (clamp_ranges),
+       (get_first_and_last_page), (ev_print_operation_export_run_next),
+       (print_job_finished), (export_print_done), (export_job_cancelled),
+       (update_progress), (export_print_page),
+       (ev_print_operation_export_begin),
+       (ev_print_operation_export_print_dialog_response_cb),
+       (ev_print_operation_export_run),
+       (ev_print_operation_export_cancel),
+       (ev_print_operation_export_class_init):
+       * shell/ev-window.c:
+       (ev_window_print_update_pending_jobs_message),
+       (ev_window_print_operation_done),
+       (ev_window_print_progress_response_cb),
+       (ev_window_print_operation_status_changed),
+       (ev_window_print_operation_begin_print), (ev_window_print_range),
+       (ev_window_dispose):
+
+       Show progress information when printing. Fixes bug #482770.
+       
 2008-12-27  Carlos Garcia Campos  <carlosgc@gnome.org>
 
        * shell/Makefile.am:
index c71ff42c487f1a579a71c68bed6a39102d24cdf8..af9cb677654a83d3fe38ae8ad2809688849e250f 100644 (file)
@@ -45,6 +45,8 @@ enum {
 
 enum {
        DONE,
+       BEGIN_PRINT,
+       STATUS_CHANGED,
        LAST_SIGNAL
 };
 
@@ -54,6 +56,10 @@ struct _EvPrintOperation {
        GObject parent;
 
        EvDocument *document;
+
+       /* Progress */
+       gchar      *status;
+       gdouble     progress;
 };
 
 struct _EvPrintOperationClass {
@@ -69,6 +75,7 @@ struct _EvPrintOperationClass {
        GtkPageSetup     *(* get_default_page_setup) (EvPrintOperation       *op);
        void              (* set_job_name)           (EvPrintOperation       *op,
                                                      const gchar            *job_name);
+       const gchar      *(* get_job_name)           (EvPrintOperation       *op);
        void              (* run)                    (EvPrintOperation       *op,
                                                      GtkWindow              *parent);
        void              (* cancel)                 (EvPrintOperation       *op);
@@ -78,6 +85,9 @@ struct _EvPrintOperationClass {
        /* signals */
        void              (* done)                   (EvPrintOperation       *op,
                                                      GtkPrintOperationResult result);
+       void              (* begin_print)            (EvPrintOperation       *op);
+       void              (* status_changed)         (EvPrintOperation       *op);
+                                                     
 };
 
 G_DEFINE_ABSTRACT_TYPE (EvPrintOperation, ev_print_operation, G_TYPE_OBJECT)
@@ -92,6 +102,11 @@ ev_print_operation_finalize (GObject *object)
                op->document = NULL;
        }
 
+       if (op->status) {
+               g_free (op->status);
+               op->status = NULL;
+       }
+
        (* G_OBJECT_CLASS (ev_print_operation_parent_class)->finalize) (object);
 }
 
@@ -142,7 +157,22 @@ ev_print_operation_class_init (EvPrintOperationClass *klass)
                              g_cclosure_marshal_VOID__ENUM,
                              G_TYPE_NONE, 1,
                              GTK_TYPE_PRINT_OPERATION_RESULT);
-       
+       signals[BEGIN_PRINT] =
+               g_signal_new ("begin_print",
+                             G_TYPE_FROM_CLASS (g_object_class),
+                             G_SIGNAL_RUN_LAST,
+                             G_STRUCT_OFFSET (EvPrintOperationClass, begin_print),
+                             NULL, NULL,
+                             g_cclosure_marshal_VOID__VOID,
+                             G_TYPE_NONE, 0);
+       signals[STATUS_CHANGED] =
+               g_signal_new ("status_changed",
+                             G_TYPE_FROM_CLASS (g_object_class),
+                             G_SIGNAL_RUN_LAST,
+                             G_STRUCT_OFFSET (EvPrintOperationClass, status_changed),
+                             NULL, NULL,
+                             g_cclosure_marshal_VOID__VOID,
+                             G_TYPE_NONE, 0);
 }
 
 /* Public methods */
@@ -214,6 +244,16 @@ ev_print_operation_set_job_name (EvPrintOperation *op,
        class->set_job_name (op, job_name);
 }
 
+const gchar *
+ev_print_operation_get_job_name (EvPrintOperation *op)
+{
+       EvPrintOperationClass *class = EV_PRINT_OPERATION_GET_CLASS (op);
+
+       g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
+
+       return class->get_job_name (op);
+}
+
 void
 ev_print_operation_run (EvPrintOperation *op,
                        GtkWindow        *parent)
@@ -246,6 +286,48 @@ ev_print_operation_get_error (EvPrintOperation *op,
        class->get_error (op, error);
 }
 
+const gchar *
+ev_print_operation_get_status (EvPrintOperation *op)
+{
+       g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), NULL);
+
+       return op->status ? op->status : "";
+}
+
+gdouble
+ev_print_operation_get_progress (EvPrintOperation *op)
+{
+       g_return_val_if_fail (EV_IS_PRINT_OPERATION (op), 0.0);
+
+       return op->progress;
+}
+
+static void
+ev_print_operation_update_status (EvPrintOperation *op,
+                                 gint              page,
+                                 gint              n_pages,
+                                 gdouble           progress)
+{
+       if (op->status && op->progress == progress)
+               return;
+
+       g_free (op->status);
+
+       if (page == -1) {
+               /* Initial state */
+               op->status = g_strdup (_("Preparing to print ..."));
+       } else if (page > n_pages) {
+               op->status = g_strdup (_("Finishing..."));
+       } else {
+               op->status = g_strdup_printf (_("Printing page %d of %d..."),
+                                             page, n_pages);
+       }
+
+       op->progress = MIN (1.0, progress);
+
+       g_signal_emit (op, signals[STATUS_CHANGED], 0);
+}
+
 /* Export interface */
 #define EV_TYPE_PRINT_OPERATION_EXPORT         (ev_print_operation_export_get_type())
 #define EV_PRINT_OPERATION_EXPORT(object)      (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_PRINT_OPERATION_EXPORT, EvPrintOperationExport))
@@ -257,6 +339,7 @@ typedef struct _EvPrintOperationExportClass EvPrintOperationExportClass;
 
 GType           ev_print_operation_export_get_type (void) G_GNUC_CONST;
 
+static void     ev_print_operation_export_begin    (EvPrintOperationExport *export);
 static gboolean export_print_page                  (EvPrintOperationExport *export);
 
 struct _EvPrintOperationExport {
@@ -284,6 +367,8 @@ struct _EvPrintOperationExport {
        guint idle_id;
        
        /* Context */
+       EvFileExporterContext fc;
+       gint n_pages_to_print;
        gint uncollated_copies;
        gint collated_copies;
        gint uncollated, collated, total;
@@ -301,6 +386,91 @@ struct _EvPrintOperationExportClass {
 
 G_DEFINE_TYPE (EvPrintOperationExport, ev_print_operation_export, EV_TYPE_PRINT_OPERATION)
 
+/* Internal print queue */
+static GHashTable *print_queue = NULL;
+
+static void
+queue_free (GQueue *queue)
+{
+       g_queue_foreach (queue, (GFunc)g_object_unref, NULL);
+       g_queue_free (queue);
+}
+
+static void
+ev_print_queue_init (void)
+{
+       if (G_UNLIKELY (print_queue == NULL)) {
+               print_queue = g_hash_table_new_full (g_direct_hash,
+                                                    g_direct_equal,
+                                                    NULL,
+                                                    (GDestroyNotify)queue_free);
+       }
+}
+
+static void
+remove_document_queue (gpointer data,
+                      GObject *document)
+{
+       if (print_queue)
+               g_hash_table_remove (print_queue, document);
+}
+
+static gboolean
+ev_print_queue_is_empty (EvDocument *document)
+{
+       GQueue *queue;
+
+       queue = g_hash_table_lookup (print_queue, document);
+       return (!queue || g_queue_is_empty (queue));
+}
+
+static void
+ev_print_queue_push (EvPrintOperation *op)
+{
+       GQueue *queue;
+
+       queue = g_hash_table_lookup (print_queue, op->document);
+       if (!queue) {
+               queue = g_queue_new ();
+               g_hash_table_insert (print_queue,
+                                    op->document,
+                                    queue);
+               g_object_weak_ref (G_OBJECT (op->document),
+                                  (GWeakNotify)remove_document_queue,
+                                  NULL);
+       }
+
+       g_queue_push_head (queue, g_object_ref (op));
+}
+
+static EvPrintOperation *
+ev_print_queue_pop (EvDocument *document)
+{
+       EvPrintOperation *op;
+       GQueue           *queue;
+
+       queue = g_hash_table_lookup (print_queue, document);
+       if (!queue || g_queue_is_empty (queue))
+               return NULL;
+       
+       op = g_queue_pop_tail (queue);
+       g_object_unref (op);
+
+       return op;
+}
+
+static EvPrintOperation *
+ev_print_queue_peek (EvDocument *document)
+{
+       GQueue *queue;
+
+       queue = g_hash_table_lookup (print_queue, document);
+       if (!queue || g_queue_is_empty (queue))
+               return NULL;
+
+       return g_queue_peek_tail (queue);
+}
+
 static void
 ev_print_operation_export_set_current_page (EvPrintOperation *op,
                                            gint              current_page)
@@ -368,6 +538,14 @@ ev_print_operation_export_set_job_name (EvPrintOperation *op,
        export->job_name = g_strdup (job_name);
 }
 
+static const gchar *
+ev_print_operation_export_get_job_name (EvPrintOperation *op)
+{
+       EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
+
+       return export->job_name;
+}
+
 static void
 ev_print_operation_export_set_printer (EvPrintOperationExport *export,
                                       GtkPrinter             *printer)
@@ -401,9 +579,12 @@ static void
 clamp_ranges (EvPrintOperationExport *export)
 {
        gint num_of_correct_ranges = 0;
+       gint n_pages_to_print = 0;
        gint i;
 
        for (i = 0; i < export->n_ranges; i++) {
+               gint n_pages;
+               
                if ((export->ranges[i].start >= 0) &&
                    (export->ranges[i].start < export->n_pages) &&
                    (export->ranges[i].end >= 0) &&
@@ -423,44 +604,51 @@ clamp_ranges (EvPrintOperationExport *export)
                        export->ranges[num_of_correct_ranges] = export->ranges[i];
                        num_of_correct_ranges++;
                }
+               
+               n_pages = export->ranges[i].end - export->ranges[i].start + 1;
+               if (export->page_set == GTK_PAGE_SET_ALL) {
+                       n_pages_to_print += n_pages;
+               } else if (n_pages % 2 == 0) {
+                       n_pages_to_print += n_pages / 2;
+               } else if (export->page_set == GTK_PAGE_SET_EVEN) {
+                       n_pages_to_print += export->ranges[i].start % 2 == 0 ?
+                               n_pages / 2 : (n_pages / 2) + 1;
+               } else if (export->page_set == GTK_PAGE_SET_ODD) {
+                       n_pages_to_print += export->ranges[i].start % 2 == 0 ?
+                               (n_pages / 2) + 1 : n_pages / 2;
+               }
        }
 
        export->n_ranges = num_of_correct_ranges;
+       export->n_pages_to_print = n_pages_to_print;
 }
 
-static gint
-get_first_page (EvPrintOperationExport *export)
+static void
+get_first_and_last_page (EvPrintOperationExport *export,
+                        gint                   *first,
+                        gint                   *last)
 {
        gint i;
        gint first_page = G_MAXINT;
-
-       if (export->n_ranges == 0)
-               return 0;
-
-       for (i = 0; i < export->n_ranges; i++) {
-               if (export->ranges[i].start < first_page)
-                       first_page = export->ranges[i].start;
-       }
-
-       return MAX (0, first_page);
-}
-
-static gint
-get_last_page (EvPrintOperationExport *export)
-{
-       gint i;
        gint last_page = G_MININT;
        gint max_page = export->n_pages - 1;
 
-       if (export->n_ranges == 0)
-               return max_page;
+       if (export->n_ranges == 0) {
+               *first = 0;
+               *last = max_page;
+
+               return;
+       }
 
        for (i = 0; i < export->n_ranges; i++) {
+               if (export->ranges[i].start < first_page)
+                       first_page = export->ranges[i].start;
                if (export->ranges[i].end > last_page)
                        last_page = export->ranges[i].end;
        }
 
-       return MIN (max_page, last_page);
+       *first = MAX (0, first_page);
+       *last = MIN (max_page, last_page);
 }
 
 static gboolean
@@ -497,13 +685,29 @@ ev_print_operation_export_clear_temp_file (EvPrintOperationExport *export)
        export->temp_file = NULL;
 }
 
+static void
+ev_print_operation_export_run_next (EvPrintOperationExport *export)
+{
+       EvPrintOperation *op = EV_PRINT_OPERATION (export);
+       EvPrintOperation *next;
+       EvDocument       *document;
+
+       /* First pop the current job */
+       document = op->document;
+       ev_print_queue_pop (document);
+       
+       next = ev_print_queue_peek (document);
+       if (next)
+               ev_print_operation_export_begin (EV_PRINT_OPERATION_EXPORT (next));
+}
+
 static void
 print_job_finished (GtkPrintJob            *print_job,
                    EvPrintOperationExport *export,
                    GError                 *error)
 {
        EvPrintOperation *op = EV_PRINT_OPERATION (export);
-       
+
        if (error) {
                g_set_error_literal (&export->error,
                                     GTK_PRINT_ERROR,
@@ -516,6 +720,8 @@ print_job_finished (GtkPrintJob            *print_job,
 
        ev_print_operation_export_clear_temp_file (export);
        g_object_unref (print_job);
+
+       ev_print_operation_export_run_next (export);
 }
 
 static void
@@ -525,6 +731,8 @@ export_print_done (EvPrintOperationExport *export)
        GtkPrintSettings *settings;
        EvFileExporterCapabilities capabilities;
 
+       g_assert (export->temp_file != NULL);
+       
        /* Some printers take into account some print settings,
         * and others don't. However we have exported the document
         * to a ps or pdf file according to such print settings. So,
@@ -573,6 +781,8 @@ export_print_done (EvPrintOperationExport *export)
 
                g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
                /* temp_file will be deleted by the previewer */
+
+               ev_print_operation_export_run_next (export);
        } else {
                GtkPrintJob *job;
                GError      *error = NULL;
@@ -590,6 +800,8 @@ export_print_done (EvPrintOperationExport *export)
                        g_error_free (error);
                        ev_print_operation_export_clear_temp_file (export);
                        g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
+
+                       ev_print_operation_export_run_next (export);
                } else {
                        gtk_print_job_send (job,
                                            (GtkPrintJobCompleteFunc)print_job_finished,
@@ -652,12 +864,27 @@ export_job_cancelled (EvJobExport            *job,
        ev_print_operation_export_clear_temp_file (export);
 
        g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
+       
+       ev_print_operation_export_run_next (export);
+}
+
+static void
+update_progress (EvPrintOperationExport *export)
+{
+       EvPrintOperation *op = EV_PRINT_OPERATION (export);
+
+       ev_print_operation_update_status (op, export->total,
+                                         export->n_pages_to_print,
+                                         export->total / (gdouble)export->n_pages_to_print);
 }
 
 static gboolean
 export_print_page (EvPrintOperationExport *export)
 {
        EvPrintOperation *op = EV_PRINT_OPERATION (export);
+
+       if (!export->temp_file)
+               return FALSE; /* cancelled */
        
        export->total++;
        export->collated++;
@@ -675,8 +902,10 @@ export_print_page (EvPrintOperationExport *export)
                        close (export->fd);
                        export->fd = -1;
 
-                       export_print_done (export);
+                       update_progress (export);
                        
+                       export_print_done (export);
+
                        return FALSE;
                }
        }
@@ -699,37 +928,56 @@ export_print_page (EvPrintOperationExport *export)
 
        ev_job_export_set_page (EV_JOB_EXPORT (export->job_export), export->page);
        ev_job_scheduler_push_job (export->job_export, EV_JOB_PRIORITY_NONE);
+
+       update_progress (export);
        
        return FALSE;
 }
 
-static gboolean
+static void
+ev_print_operation_export_begin (EvPrintOperationExport *export)
+{
+       EvPrintOperation *op = EV_PRINT_OPERATION (export);
+       
+       ev_document_doc_mutex_lock ();
+       ev_file_exporter_begin (EV_FILE_EXPORTER (op->document), &export->fc);
+       ev_document_doc_mutex_unlock ();
+
+       export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
+                                          (GSourceFunc)export_print_page,
+                                          export,
+                                          (GDestroyNotify)export_print_page_idle_finished);    
+}
+
+static void
 ev_print_operation_export_print_dialog_response_cb (GtkDialog              *dialog,
                                                    gint                    response,
                                                    EvPrintOperationExport *export)
 {
-       GtkPrintPages         print_pages;
-       GtkPrintSettings     *print_settings;
-       GtkPageSetup         *page_setup;
-       GtkPrinter           *printer;
-       gdouble               scale;
-       gdouble               width;
-       gdouble               height;
-       gint                  first_page;
-       gint                  last_page;
-       const gchar          *file_format;
-       gchar                *filename;
-       EvFileExporterContext fc;
-       GError               *error = NULL;
-       EvPrintOperation     *op = EV_PRINT_OPERATION (export);
+       GtkPrintPages     print_pages;
+       GtkPrintSettings *print_settings;
+       GtkPageSetup     *page_setup;
+       GtkPrinter       *printer;
+       gdouble           scale;
+       gdouble           width;
+       gdouble           height;
+       gint              first_page;
+       gint              last_page;
+       const gchar      *file_format;
+       gchar            *filename;
+       GError           *error = NULL;
+       EvPrintOperation *op = EV_PRINT_OPERATION (export);
        
        if (response != GTK_RESPONSE_OK &&
            response != GTK_RESPONSE_APPLY) {
                gtk_widget_destroy (GTK_WIDGET (dialog));
+               g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_CANCEL);
 
-               return FALSE;
+               return;
        }
 
+       ev_print_operation_update_status (op, -1, -1, 0.0);
+       
        export->print_preview = (response == GTK_RESPONSE_APPLY);
        
        printer = gtk_print_unix_dialog_get_selected_printer (GTK_PRINT_UNIX_DIALOG (dialog));
@@ -742,14 +990,15 @@ ev_print_operation_export_print_dialog_response_cb (GtkDialog              *dial
        ev_print_operation_export_set_default_page_setup (op, page_setup);
 
        if (!gtk_printer_accepts_ps (export->printer)) {
+               gtk_widget_destroy (GTK_WIDGET (dialog));
+               
                g_set_error (&export->error,
                             GTK_PRINT_ERROR,
                             GTK_PRINT_ERROR_GENERAL,
                             "%s", _("Printing is not supported on this printer."));
                g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
-               gtk_widget_destroy (GTK_WIDGET (dialog));
                
-               return FALSE;
+               return;
        }
 
        file_format = gtk_print_settings_get (print_settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
@@ -758,18 +1007,20 @@ ev_print_operation_export_print_dialog_response_cb (GtkDialog              *dial
        export->fd = g_file_open_tmp (filename, &export->temp_file, &error);
        g_free (filename);
        if (export->fd <= -1) {
+               gtk_widget_destroy (GTK_WIDGET (dialog));
+               
                g_set_error_literal (&export->error,
                                     GTK_PRINT_ERROR,
                                     GTK_PRINT_ERROR_GENERAL,
                                     error->message);
                g_error_free (error);
                g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_ERROR);
-               gtk_widget_destroy (GTK_WIDGET (dialog));
 
-               return FALSE;
+               return;
        }
 
        export->current_page = gtk_print_unix_dialog_get_current_page (GTK_PRINT_UNIX_DIALOG (dialog));
+       export->page_set = gtk_print_settings_get_page_set (print_settings);
        print_pages = gtk_print_settings_get_print_pages (print_settings);
        
        switch (print_pages) {
@@ -801,8 +1052,6 @@ ev_print_operation_export_print_dialog_response_cb (GtkDialog              *dial
        }
        clamp_ranges (export);
 
-       export->page_set = gtk_print_settings_get_page_set (print_settings);
-
        width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
        height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
        scale = gtk_print_settings_get_scale (print_settings) * 0.01;
@@ -837,31 +1086,26 @@ ev_print_operation_export_print_dialog_response_cb (GtkDialog              *dial
        export->page = export->start - export->inc;
        export->collated = export->collated_copies - 1;
 
-       first_page = get_first_page (export);
-       last_page = get_last_page (export);
+       get_first_and_last_page (export, &first_page, &last_page);
 
-       fc.format = g_ascii_strcasecmp (file_format, "pdf") == 0 ?
+       export->fc.format = g_ascii_strcasecmp (file_format, "pdf") == 0 ?
                EV_FILE_FORMAT_PDF : EV_FILE_FORMAT_PS;
-       fc.filename = export->temp_file;
-       fc.first_page = MIN (first_page, last_page);
-       fc.last_page = MAX (first_page, last_page);
-       fc.paper_width = width;
-       fc.paper_height = height;
-       fc.duplex = FALSE;
-       fc.pages_per_sheet = MAX (1, export->pages_per_sheet);
+       export->fc.filename = export->temp_file;
+       export->fc.first_page = MIN (first_page, last_page);
+       export->fc.last_page = MAX (first_page, last_page);
+       export->fc.paper_width = width;
+       export->fc.paper_height = height;
+       export->fc.duplex = FALSE;
+       export->fc.pages_per_sheet = MAX (1, export->pages_per_sheet);
 
-       ev_document_doc_mutex_lock ();
-       ev_file_exporter_begin (EV_FILE_EXPORTER (op->document), &fc);
-       ev_document_doc_mutex_unlock ();
+       if (ev_print_queue_is_empty (op->document))
+               ev_print_operation_export_begin (export);
 
-       export->idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
-                                          (GSourceFunc)export_print_page,
-                                          export,
-                                          (GDestroyNotify)export_print_page_idle_finished);
+       ev_print_queue_push (op);
+
+       g_signal_emit (op, signals[BEGIN_PRINT], 0);
        
        gtk_widget_destroy (GTK_WIDGET (dialog));
-
-       return TRUE;
 }
 
 static void
@@ -872,6 +1116,8 @@ ev_print_operation_export_run (EvPrintOperation *op,
        GtkWidget              *dialog;
        GtkPrintCapabilities    capabilities;
 
+       ev_print_queue_init ();
+
        export->parent_window = parent;
        export->error = NULL;
        
@@ -906,7 +1152,12 @@ ev_print_operation_export_cancel (EvPrintOperation *op)
        EvPrintOperationExport *export = EV_PRINT_OPERATION_EXPORT (op);
 
        if (export->job_export) {
-               ev_job_cancel (export->job_export);
+               if (!ev_job_is_finished (export->job_export)) {
+                       ev_job_cancel (export->job_export);
+               } else {
+                       export_job_cancelled (EV_JOB_EXPORT (export->job_export),
+                                             export);
+               }
        }
 }
 
@@ -1024,6 +1275,7 @@ ev_print_operation_export_class_init (EvPrintOperationExportClass *klass)
        ev_print_op_class->set_default_page_setup = ev_print_operation_export_set_default_page_setup;
        ev_print_op_class->get_default_page_setup = ev_print_operation_export_get_default_page_setup;
        ev_print_op_class->set_job_name = ev_print_operation_export_set_job_name;
+       ev_print_op_class->get_job_name = ev_print_operation_export_get_job_name;
        ev_print_op_class->run = ev_print_operation_export_run;
        ev_print_op_class->cancel = ev_print_operation_export_cancel;
        ev_print_op_class->get_error = ev_print_operation_export_get_error;
index 8e7d6f853a0031e25d43601d9adad70b382adc18..6930f80feaab4b59b74c5e79aab27023dad3f531 100644 (file)
@@ -49,11 +49,15 @@ void              ev_print_operation_set_default_page_setup (EvPrintOperation *o
 GtkPageSetup     *ev_print_operation_get_default_page_setup (EvPrintOperation *op);
 void              ev_print_operation_set_job_name           (EvPrintOperation *op,
                                                             const gchar      *job_name);
+const gchar      *ev_print_operation_get_job_name           (EvPrintOperation *op);
 void              ev_print_operation_run                    (EvPrintOperation *op,
                                                             GtkWindow        *parent);
 void              ev_print_operation_cancel                 (EvPrintOperation *op);
 void              ev_print_operation_get_error              (EvPrintOperation *op,
                                                             GError          **error);
+const gchar      *ev_print_operation_get_status             (EvPrintOperation *op);
+gdouble           ev_print_operation_get_progress           (EvPrintOperation *op);
+
 G_END_DECLS
        
 #endif /* __EV_PRINT_OPERATION_H__ */
index 324cddade02589bbd4f1ba930dee41b75389b550..697eb737f5605a4b5a673c32c42ba956c1c29aad 100644 (file)
@@ -1,6 +1,7 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
 /* this file is part of evince, a gnome document viewer
  *
+ *  Copyright (C) 2008 Carlos Garcia Campos
  *  Copyright (C) 2004 Martin Kretzschmar
  *  Copyright (C) 2004 Red Hat, Inc.
  *  Copyright (C) 2000, 2001, 2002, 2003, 2004 Marco Pesenti Gritti
@@ -88,6 +89,7 @@
 #include "ev-window.h"
 #include "ev-window-title.h"
 #include "ev-print-operation.h"
+#include "ev-progress-message-area.h"
 
 #ifdef ENABLE_DBUS
 #include "ev-media-player-keys.h"
@@ -188,7 +190,7 @@ struct _EvWindowPrivate {
        EvJob            *find_job;
 
        /* Printing */
-       gboolean          print_preview;
+       GQueue           *print_queue;
        GtkPrinter       *printer;
        GtkPrintSettings *print_settings;
        GtkPageSetup     *print_page_setup;
@@ -677,7 +679,7 @@ ev_window_error_message (EvWindow    *window,
        area = ev_message_area_new (GTK_MESSAGE_ERROR,
                                    msg,
                                    GTK_STOCK_CLOSE,
-                                   GTK_RESPONSE_CANCEL,
+                                   GTK_RESPONSE_CLOSE,
                                    NULL);
        g_free (msg);
        
@@ -709,7 +711,7 @@ ev_window_warning_message (EvWindow    *window,
        area = ev_message_area_new (GTK_MESSAGE_WARNING,
                                    msg,
                                    GTK_STOCK_CLOSE,
-                                   GTK_RESPONSE_CANCEL,
+                                   GTK_RESPONSE_CLOSE,
                                    NULL);
        g_free (msg);
        
@@ -2324,6 +2326,33 @@ ev_window_save_print_settings (EvWindow *window)
        }
 }
 
+static void
+ev_window_print_update_pending_jobs_message (EvWindow *ev_window)
+{
+       gint   n_jobs;
+       gchar *text = NULL;
+       
+       if (!EV_IS_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area) ||
+           !ev_window->priv->print_queue)
+               return;
+
+       n_jobs = g_queue_get_length (ev_window->priv->print_queue);
+       if (n_jobs == 0) {
+               ev_window_set_message_area (ev_window, NULL);
+               return;
+       }
+       
+       if (n_jobs > 1) {
+               text = g_strdup_printf (ngettext ("%d pending job in queue",
+                                                 "%d pending jobs in queue",
+                                                 n_jobs - 1), n_jobs - 1);
+       }
+
+       ev_message_area_set_secondary_text (EV_MESSAGE_AREA (ev_window->priv->message_area),
+                                           text);
+       g_free (text);
+}
+
 static void
 ev_window_print_operation_done (EvPrintOperation       *op,
                                GtkPrintOperationResult result,
@@ -2332,22 +2361,42 @@ ev_window_print_operation_done (EvPrintOperation       *op,
        switch (result) {
        case GTK_PRINT_OPERATION_RESULT_APPLY: {
                GtkPrintSettings *print_settings;
-
-               if (ev_window->priv->print_settings)
-                       g_object_unref (ev_window->priv->print_settings);
+               
                print_settings = ev_print_operation_get_print_settings (op);
+               if (ev_window->priv->print_settings != print_settings) {
+                       if (ev_window->priv->print_settings)
+                               g_object_unref (ev_window->priv->print_settings);
+                       ev_window->priv->print_settings = g_object_ref (print_settings);
+               }
                
                ev_application_set_print_settings (EV_APP, print_settings);
-               ev_window->priv->print_settings = g_object_ref (print_settings);
                ev_window_save_print_settings (ev_window);
        }
+
                break;
        case GTK_PRINT_OPERATION_RESULT_ERROR: {
-               GError *error = NULL;
+               GtkWidget *dialog;
+               GError    *error = NULL;
+
 
                ev_print_operation_get_error (op, &error);
-               ev_window_error_message (ev_window, error,
-                                        "%s", _("Failed to print document"));
+               
+               /* The message area is already used by
+                * the printing progress, so it's better to
+                * use a popup dialog in this case
+                */
+               dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
+                                                GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                GTK_MESSAGE_ERROR,
+                                                GTK_BUTTONS_CLOSE,
+                                                "%s", _("Failed to print document"));
+               gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+                                                         "%s", error->message);
+               g_signal_connect (dialog, "response",
+                                 G_CALLBACK (gtk_widget_destroy),
+                                 NULL);
+               gtk_widget_show (dialog);
+               
                g_error_free (error);
        }
                break;
@@ -2356,7 +2405,82 @@ ev_window_print_operation_done (EvPrintOperation       *op,
                break;
        }
 
+       g_queue_remove (ev_window->priv->print_queue, op);
        g_object_unref (op);
+       ev_window_print_update_pending_jobs_message (ev_window);
+}
+
+static void
+ev_window_print_progress_response_cb (EvProgressMessageArea *area,
+                                     gint                   response,
+                                     EvWindow              *ev_window)
+{
+       if (response == GTK_RESPONSE_CANCEL) {
+               EvPrintOperation *op;
+
+               op = g_queue_peek_tail (ev_window->priv->print_queue);
+               ev_print_operation_cancel (op);
+       } else {
+               gtk_widget_hide (GTK_WIDGET (area));
+       }
+}
+
+static void
+ev_window_print_operation_status_changed (EvPrintOperation *op,
+                                         EvWindow         *ev_window)
+{
+       const gchar *status;
+       gdouble      fraction;
+
+       status = ev_print_operation_get_status (op);
+       fraction = ev_print_operation_get_progress (op);
+       
+       if (!ev_window->priv->message_area) {
+               GtkWidget   *area;
+               const gchar *job_name;
+               gchar       *text;
+
+               job_name = ev_print_operation_get_job_name (op);
+               text = g_strdup_printf (_("Printing job ā€œ%sā€"), job_name);
+
+               area = ev_progress_message_area_new (GTK_STOCK_PRINT,
+                                                    text,
+                                                    GTK_STOCK_CLOSE,
+                                                    GTK_RESPONSE_CLOSE,
+                                                    GTK_STOCK_CANCEL,
+                                                    GTK_RESPONSE_CANCEL,
+                                                    NULL);
+               ev_window_print_update_pending_jobs_message (ev_window);
+               g_signal_connect (area, "response",
+                                 G_CALLBACK (ev_window_print_progress_response_cb),
+                                 ev_window);
+               gtk_widget_show (area);
+               ev_window_set_message_area (ev_window, area);
+               g_free (text);
+       }
+
+       ev_progress_message_area_set_status (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
+                                            status);
+       ev_progress_message_area_set_fraction (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
+                                              fraction);
+}
+
+static void
+ev_window_print_operation_begin_print (EvPrintOperation *op,
+                                      EvWindow         *ev_window)
+{
+       GtkPrintSettings *print_settings;
+       
+       if (!ev_window->priv->print_queue)
+               ev_window->priv->print_queue = g_queue_new ();
+
+       g_queue_push_head (ev_window->priv->print_queue, op);
+       ev_window_print_update_pending_jobs_message (ev_window);
+       
+       if (ev_window->priv->print_settings)
+               g_object_unref (ev_window->priv->print_settings);
+       print_settings = ev_print_operation_get_print_settings (op);
+       ev_window->priv->print_settings = g_object_ref (print_settings);
 }
 
 void
@@ -2364,15 +2488,24 @@ ev_window_print_range (EvWindow *ev_window,
                       gint      first_page,
                       gint      last_page)
 {
-       EvPrintOperation    *op;
-       EvPageCache         *page_cache;
-       gint                 current_page;
-       gint                 document_last_page;
+       EvPrintOperation *op;
+       EvPageCache      *page_cache;
+       gint              current_page;
+       gint              document_last_page;
 
        g_return_if_fail (EV_IS_WINDOW (ev_window));
        g_return_if_fail (ev_window->priv->document != NULL);
 
+       if (!ev_window->priv->print_queue)
+               ev_window->priv->print_queue = g_queue_new ();
+
        op = ev_print_operation_new (ev_window->priv->document);
+       g_signal_connect (G_OBJECT (op), "begin_print",
+                         G_CALLBACK (ev_window_print_operation_begin_print),
+                         (gpointer)ev_window);
+       g_signal_connect (G_OBJECT (op), "status_changed",
+                         G_CALLBACK (ev_window_print_operation_status_changed),
+                         (gpointer)ev_window);
        g_signal_connect (G_OBJECT (op), "done",
                          G_CALLBACK (ev_window_print_operation_done),
                          (gpointer)ev_window);
@@ -4216,6 +4349,11 @@ ev_window_dispose (GObject *object)
                priv->presentation_timeout_id = 0;
        }
 
+       if (priv->print_queue) {
+               g_queue_free (priv->print_queue);
+               priv->print_queue = NULL;
+       }
+
        G_OBJECT_CLASS (ev_window_parent_class)->dispose (object);
 }