1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2004 Martin Kretzschmar
6 * Martin Kretzschmar <martink@gnome.org>
8 * Evince is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Evince is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
28 #include <glib/gi18n.h>
29 #include <glib-object.h>
31 /* for gnome_print_job_set_file */
32 #define GNOME_PRINT_UNSTABLE_API
33 #include <libgnomeprint/gnome-print-job.h>
35 #include "ev-file-exporter.h"
36 #include "ev-print-job.h"
37 #include "ev-page-cache.h"
39 #define EV_PRINT_JOB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), EV_PRINT_JOB, EvPrintJobClass))
40 #define EV_IS_PRINT_JOB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), EV_PRINT_JOB))
41 #define EV_PRINT_JOB_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), EV_PRINT_JOB, EvPrintJobClass))
44 GObject parent_instance;
47 GnomePrintJob *gnome_print_job;
48 EvFileExporterContext fc;
61 struct _EvPrintJobClass {
62 GObjectClass parent_class;
72 G_DEFINE_TYPE (EvPrintJob, ev_print_job, G_TYPE_OBJECT);
75 ev_print_job_finalize (GObject *object)
77 EvPrintJob *job = EV_PRINT_JOB (object);
79 if (job && job->document) {
80 g_object_unref (job->document);
84 if (job && job->gnome_print_job) {
85 g_object_unref (job->gnome_print_job);
86 job->gnome_print_job = NULL;
89 G_OBJECT_CLASS (ev_print_job_parent_class)->finalize (object);
93 ev_print_job_set_property (GObject *object, guint prop_id,
94 const GValue *value, GParamSpec *pspec)
98 job = EV_PRINT_JOB (object);
101 case PROP_GNOME_PRINT_JOB:
102 ev_print_job_set_gnome_print_job (
103 job, GNOME_PRINT_JOB (g_value_get_object (value)));
106 ev_print_job_set_document (job, EV_DOCUMENT (g_value_get_object (value)));
108 case PROP_PRINT_DIALOG:
109 ev_print_job_use_print_dialog_settings (
110 job, GNOME_PRINT_DIALOG (g_value_get_object (value)));
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120 ev_print_job_get_property (GObject *object, guint prop_id,
121 GValue *value, GParamSpec *pspec)
125 job = EV_PRINT_JOB (object);
128 case PROP_GNOME_PRINT_JOB:
129 g_value_set_object (value, job->gnome_print_job);
132 g_value_set_object (value, job->document);
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 ev_print_job_class_init (EvPrintJobClass *ev_print_job_class)
144 GObjectClass *g_object_class;
146 g_object_class = G_OBJECT_CLASS (ev_print_job_class);
148 g_object_class->finalize = ev_print_job_finalize;
149 g_object_class->set_property = ev_print_job_set_property;
150 g_object_class->get_property = ev_print_job_get_property;
152 g_object_class_install_property (g_object_class,
153 PROP_GNOME_PRINT_JOB,
154 g_param_spec_object ("gnome_print_job",
157 GNOME_TYPE_PRINT_JOB,
159 g_object_class_install_property (g_object_class,
161 g_param_spec_object ("document",
163 "Document from which to print",
164 G_TYPE_OBJECT, /* EV_TYPE_DOCUMENT, */
166 g_object_class_install_property (g_object_class,
168 g_param_spec_object ("print_dialog",
170 "GnomePrintDialog with user settings",
171 GNOME_TYPE_PRINT_DIALOG,
177 ev_print_job_init (EvPrintJob *ev_print_job)
179 ev_print_job->fc.pages_per_sheet = 1;
183 ev_print_job_set_gnome_print_job (EvPrintJob *job, GnomePrintJob *gpj)
185 g_return_if_fail (EV_IS_PRINT_JOB (job));
187 if (job->gnome_print_job == gpj)
190 if (job->gnome_print_job)
191 g_object_unref (job->gnome_print_job);
196 job->gnome_print_job = gpj;
200 ev_print_job_set_document (EvPrintJob *job, EvDocument *document)
202 g_return_if_fail (EV_IS_PRINT_JOB (job));
204 if (job->document == document)
208 g_object_ref (job->document);
211 g_object_ref (document);
213 job->document = document;
217 ev_print_job_use_print_dialog_settings (EvPrintJob *job, GnomePrintDialog *dialog)
219 GnomePrintConfig *print_config;
220 EvPageCache *page_cache = ev_page_cache_get (job->document);
221 gint first_page, last_page;
223 g_return_if_fail (EV_IS_PRINT_JOB (job));
224 g_return_if_fail (GNOME_IS_PRINT_DIALOG (dialog));
226 print_config = gnome_print_dialog_get_config (dialog);
227 gnome_print_dialog_get_copies (dialog, &job->copies, &job->collate);
228 gnome_print_config_get_page_size (print_config,
229 &job->fc.paper_width, &job->fc.paper_height);
230 gnome_print_config_get_boolean (print_config,
231 (guchar *)GNOME_PRINT_KEY_DUPLEX, &job->fc.duplex);
233 page_cache = ev_page_cache_get (job->document);
235 /* get the printing ranges */
236 switch (gnome_print_dialog_get_range (dialog)) {
237 case GNOME_PRINT_RANGE_ALL:
239 last_page = ev_page_cache_get_n_pages (page_cache) - 1;
241 case GNOME_PRINT_RANGE_RANGE:
242 gnome_print_dialog_get_range_page (dialog, &first_page, &last_page);
243 /* convert 1-based user interface to 0-based internal numbers */
248 g_assert_not_reached ();
251 job->fc.first_page = MIN (first_page, last_page);
252 job->fc.last_page = MAX (first_page, last_page);
254 gnome_print_config_unref (print_config);
258 idle_print_handler (EvPrintJob *job)
260 if (!job->printing) {
261 ev_document_doc_mutex_lock ();
262 ev_file_exporter_begin (EV_FILE_EXPORTER (job->document),
264 ev_document_doc_mutex_unlock ();
265 job->next_page = job->fc.first_page;
266 job->shift = (job->fc.first_page > job->fc.last_page) ? -1 : 1;
267 job->printing = TRUE;
271 if ((job->next_page - job->fc.last_page) * job->shift <= 0) {
274 g_printerr ("Printing page %d\n", job->next_page);
276 rc = ev_render_context_new (0, job->next_page, 1.0);
278 ev_document_doc_mutex_lock ();
279 ev_file_exporter_do_page (EV_FILE_EXPORTER (job->document), rc);
280 ev_document_doc_mutex_unlock ();
285 /* collate must repeat the same page */
287 if(job->copies == job->copies_done) {
288 job->next_page += job->shift;
289 job->copies_done = 0;
292 job->next_page += job->shift;
293 if ((job->next_page - job->fc.last_page) * job->shift > 0){
295 if(job->copies_done < job->copies) {
296 /* more copies to go, restart to the first page */
297 job->next_page = job->fc.first_page;
302 } else { /* no more pages or copies */
303 ev_document_doc_mutex_lock ();
304 ev_file_exporter_end (EV_FILE_EXPORTER (job->document));
305 ev_document_doc_mutex_unlock ();
310 gnome_print_job_print (job->gnome_print_job);
312 unlink (job->temp_file);
313 g_free (job->temp_file);
315 g_object_unref (job->gnome_print_job);
316 job->gnome_print_job = NULL;
318 job->printing = FALSE;
325 print_closure_finalize (EvPrintJob *job, GClosure *closure)
327 g_object_unref (job);
331 ev_print_job_print (EvPrintJob *job, GtkWindow *parent)
334 GSource *idle_source;
336 g_return_if_fail (EV_IS_PRINT_JOB (job));
337 g_return_if_fail (job->document != NULL);
338 g_return_if_fail (EV_IS_FILE_EXPORTER (job->document));
340 g_printerr ("Printing...\n");
343 job->fd = g_file_open_tmp ("evince_print.ps.XXXXXX", &job->temp_file, NULL);
345 return; /* FIXME use GError */
347 job->fc.format = EV_FILE_FORMAT_PS;
348 job->fc.filename = job->temp_file;
349 gnome_print_job_set_file (job->gnome_print_job, job->temp_file);
352 closure = g_cclosure_new (G_CALLBACK (idle_print_handler), job, NULL);
353 g_closure_add_finalize_notifier (
354 closure, job, (GClosureNotify)print_closure_finalize);
356 idle_source = g_idle_source_new ();
357 g_source_set_closure (idle_source, closure);
358 job->idle_id = g_source_attach (idle_source, NULL);
359 g_source_unref (idle_source);