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-ps-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 double width; /* FIXME unused */
49 double height; /* FIXME unused */
50 gboolean duplex; /* FIXME unused */
67 struct _EvPrintJobClass {
68 GObjectClass parent_class;
78 G_DEFINE_TYPE (EvPrintJob, ev_print_job, G_TYPE_OBJECT);
81 ev_print_job_finalize (GObject *object)
83 EvPrintJob *job = EV_PRINT_JOB (object);
85 if (job && job->document) {
86 g_object_unref (job->document);
90 if (job && job->gnome_print_job) {
91 g_object_unref (job->gnome_print_job);
92 job->gnome_print_job = NULL;
95 G_OBJECT_CLASS (ev_print_job_parent_class)->finalize (object);
99 ev_print_job_set_property (GObject *object, guint prop_id,
100 const GValue *value, GParamSpec *pspec)
104 job = EV_PRINT_JOB (object);
107 case PROP_GNOME_PRINT_JOB:
108 ev_print_job_set_gnome_print_job (
109 job, GNOME_PRINT_JOB (g_value_get_object (value)));
112 ev_print_job_set_document (job, EV_DOCUMENT (g_value_get_object (value)));
114 case PROP_PRINT_DIALOG:
115 ev_print_job_use_print_dialog_settings (
116 job, GNOME_PRINT_DIALOG (g_value_get_object (value)));
119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
126 ev_print_job_get_property (GObject *object, guint prop_id,
127 GValue *value, GParamSpec *pspec)
131 job = EV_PRINT_JOB (object);
134 case PROP_GNOME_PRINT_JOB:
135 g_value_set_object (value, job->gnome_print_job);
138 g_value_set_object (value, job->document);
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148 ev_print_job_class_init (EvPrintJobClass *ev_print_job_class)
150 GObjectClass *g_object_class;
152 g_object_class = G_OBJECT_CLASS (ev_print_job_class);
154 g_object_class->finalize = ev_print_job_finalize;
155 g_object_class->set_property = ev_print_job_set_property;
156 g_object_class->get_property = ev_print_job_get_property;
158 g_object_class_install_property (g_object_class,
159 PROP_GNOME_PRINT_JOB,
160 g_param_spec_object ("gnome_print_job",
163 GNOME_TYPE_PRINT_JOB,
165 g_object_class_install_property (g_object_class,
167 g_param_spec_object ("document",
169 "Document from which to print",
170 G_TYPE_OBJECT, /* EV_TYPE_DOCUMENT, */
172 g_object_class_install_property (g_object_class,
174 g_param_spec_object ("print_dialog",
176 "GnomePrintDialog with user settings",
177 GNOME_TYPE_PRINT_DIALOG,
183 ev_print_job_init (EvPrintJob *ev_print_job)
188 ev_print_job_set_gnome_print_job (EvPrintJob *job, GnomePrintJob *gpj)
190 g_return_if_fail (EV_IS_PRINT_JOB (job));
192 if (job->gnome_print_job == gpj)
195 if (job->gnome_print_job)
196 g_object_unref (job->gnome_print_job);
201 job->gnome_print_job = gpj;
205 ev_print_job_set_document (EvPrintJob *job, EvDocument *document)
207 g_return_if_fail (EV_IS_PRINT_JOB (job));
209 if (job->document == document)
213 g_object_ref (job->document);
216 g_object_ref (document);
218 job->document = document;
222 ev_print_job_use_print_dialog_settings (EvPrintJob *job, GnomePrintDialog *dialog)
224 GnomePrintConfig *print_config;
225 EvPageCache *page_cache = ev_page_cache_get (job->document);
227 g_return_if_fail (EV_IS_PRINT_JOB (job));
228 g_return_if_fail (GNOME_IS_PRINT_DIALOG (dialog));
230 print_config = gnome_print_dialog_get_config (dialog);
231 gnome_print_dialog_get_copies (dialog, &job->copies, &job->collate);
232 gnome_print_config_get_page_size (print_config,
233 &job->width, &job->height);
234 gnome_print_config_get_boolean (print_config,
235 (guchar *)GNOME_PRINT_KEY_DUPLEX, &job->duplex);
237 page_cache = ev_page_cache_get (job->document);
239 /* get the printing ranges */
240 switch (gnome_print_dialog_get_range (dialog)) {
241 case GNOME_PRINT_RANGE_ALL:
243 job->last_page = ev_page_cache_get_n_pages (page_cache) - 1;
245 case GNOME_PRINT_RANGE_RANGE:
246 gnome_print_dialog_get_range_page (dialog, &job->first_page, &job->last_page);
247 /* convert 1-based user interface to 0-based internal numbers */
252 g_assert_not_reached ();
255 gnome_print_config_unref (print_config);
259 idle_print_handler (EvPrintJob *job)
261 if (!job->printing) {
262 ev_document_doc_mutex_lock ();
263 ev_ps_exporter_begin (
264 EV_PS_EXPORTER (job->document),
266 MIN (job->first_page, job->last_page),
267 MAX (job->first_page, job->last_page),
268 job->width, job->height, job->duplex);
269 ev_document_doc_mutex_unlock ();
270 job->next_page = job->first_page;
271 job->shift = (job->first_page > job->last_page) ? -1 : 1;
272 job->printing = TRUE;
276 if ((job->next_page - job->last_page) * job->shift <= 0) {
279 g_printerr ("Printing page %d\n", job->next_page);
281 rc = ev_render_context_new (0, job->next_page, 1.0);
283 ev_document_doc_mutex_lock ();
284 ev_ps_exporter_do_page (EV_PS_EXPORTER (job->document), rc);
285 ev_document_doc_mutex_unlock ();
290 /* collate must repeat the same page */
292 if(job->copies == job->copies_done) {
293 job->next_page += job->shift;
294 job->copies_done = 0;
297 job->next_page += job->shift;
298 if ((job->next_page - job->last_page) * job->shift > 0){
300 if(job->copies_done < job->copies) {
301 /* more copies to go, restart to the first page */
302 job->next_page = job->first_page;
307 } else { /* no more pages or copies */
308 ev_document_doc_mutex_lock ();
309 ev_ps_exporter_end (EV_PS_EXPORTER (job->document));
310 ev_document_doc_mutex_unlock ();
315 gnome_print_job_print (job->gnome_print_job);
317 unlink (job->temp_file);
318 g_free (job->temp_file);
320 g_object_unref (job->gnome_print_job);
321 job->gnome_print_job = NULL;
323 job->printing = FALSE;
330 print_closure_finalize (EvPrintJob *job, GClosure *closure)
332 g_object_unref (job);
336 ev_print_job_print (EvPrintJob *job, GtkWindow *parent)
339 GSource *idle_source;
341 g_return_if_fail (EV_IS_PRINT_JOB (job));
342 g_return_if_fail (job->document != NULL);
343 g_return_if_fail (EV_IS_PS_EXPORTER (job->document));
345 g_printerr ("Printing...\n");
348 job->fd = g_file_open_tmp ("evince_print.ps.XXXXXX", &job->temp_file, NULL);
350 return; /* FIXME use GError */
352 gnome_print_job_set_file (job->gnome_print_job, job->temp_file);
355 closure = g_cclosure_new (G_CALLBACK (idle_print_handler), job, NULL);
356 g_closure_add_finalize_notifier (
357 closure, job, (GClosureNotify)print_closure_finalize);
359 idle_source = g_idle_source_new ();
360 g_source_set_closure (idle_source, closure);
361 job->idle_id = g_source_attach (idle_source, NULL);
362 g_source_unref (idle_source);