]> www.fi.muni.cz Git - evince.git/blob - shell/ev-print-job.c
Hungarian translation updated.
[evince.git] / shell / ev-print-job.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Martin Kretzschmar
4  *
5  *  Author:
6  *    Martin Kretzschmar <martink@gnome.org>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <config.h>
24
25 #include <unistd.h>
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <glib-object.h>
30
31 /* for gnome_print_job_set_file */
32 #define GNOME_PRINT_UNSTABLE_API
33 #include <libgnomeprint/gnome-print-job.h>
34
35 #include "ev-ps-exporter.h"
36 #include "ev-print-job.h"
37 #include "ev-page-cache.h"
38
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))
42
43 struct _EvPrintJob {
44         GObject parent_instance;
45
46         EvDocument *document;
47         GnomePrintJob *gnome_print_job;
48         double width; /* FIXME unused */
49         double height; /* FIXME unused */
50         gboolean duplex; /* FIXME unused */
51         int copies;
52         int collate;
53
54         /* range printing */
55         int first_page;
56         int last_page;
57
58         int fd;
59         char *temp_file;
60         guint idle_id;
61         gboolean printing;
62         int next_page;
63         int copies_done;
64 };
65
66 struct _EvPrintJobClass {
67         GObjectClass parent_class;
68 };
69
70 enum {
71         PROP_0,
72         PROP_GNOME_PRINT_JOB,
73         PROP_DOCUMENT,
74         PROP_PRINT_DIALOG
75 };
76
77 G_DEFINE_TYPE (EvPrintJob, ev_print_job, G_TYPE_OBJECT);
78
79 static void
80 ev_print_job_finalize (GObject *object)
81 {
82         EvPrintJob *job = EV_PRINT_JOB (object);
83
84         if (job && job->document) {
85                 g_object_unref (job->document);
86                 job->document = NULL;
87         }
88
89         if (job && job->gnome_print_job) {
90                 g_object_unref (job->gnome_print_job);
91                 job->gnome_print_job = NULL;
92         }
93
94         G_OBJECT_CLASS (ev_print_job_parent_class)->finalize (object);
95 }
96
97 static void
98 ev_print_job_set_property (GObject *object, guint prop_id,
99                            const GValue *value, GParamSpec *pspec)
100 {
101         EvPrintJob *job;
102
103         job = EV_PRINT_JOB (object);
104
105         switch (prop_id) {
106         case PROP_GNOME_PRINT_JOB:
107                 ev_print_job_set_gnome_print_job (
108                         job, GNOME_PRINT_JOB (g_value_get_object (value)));
109                 break;
110         case PROP_DOCUMENT:
111                 ev_print_job_set_document (job, EV_DOCUMENT (g_value_get_object (value)));
112                 break;
113         case PROP_PRINT_DIALOG:
114                 ev_print_job_use_print_dialog_settings (
115                         job, GNOME_PRINT_DIALOG (g_value_get_object (value)));
116                 break;
117         default:
118                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119                 break;
120         }
121
122 }
123
124 static void
125 ev_print_job_get_property (GObject *object, guint prop_id,
126                            GValue *value, GParamSpec *pspec)
127 {
128         EvPrintJob *job;
129
130         job = EV_PRINT_JOB (object);
131
132         switch (prop_id) {
133         case PROP_GNOME_PRINT_JOB:
134                 g_value_set_object (value, job->gnome_print_job);
135                 break;
136         case PROP_DOCUMENT:
137                 g_value_set_object (value, job->document);
138                 break;
139         default:
140                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141                 break;
142         }
143 }
144
145
146 static void
147 ev_print_job_class_init (EvPrintJobClass *ev_print_job_class)
148 {
149         GObjectClass *g_object_class;
150
151         g_object_class = G_OBJECT_CLASS (ev_print_job_class);
152
153         g_object_class->finalize = ev_print_job_finalize;
154         g_object_class->set_property = ev_print_job_set_property;
155         g_object_class->get_property = ev_print_job_get_property;
156
157         g_object_class_install_property (g_object_class,
158                                          PROP_GNOME_PRINT_JOB,
159                                          g_param_spec_object ("gnome_print_job",
160                                                               "GnomePrintJob",
161                                                               "GnomePrintJob",
162                                                               GNOME_TYPE_PRINT_JOB,
163                                                               G_PARAM_READWRITE));
164         g_object_class_install_property (g_object_class,
165                                          PROP_DOCUMENT,
166                                          g_param_spec_object ("document",
167                                                               "Document object",
168                                                               "Document from which to print",
169                                                               G_TYPE_OBJECT, /* EV_TYPE_DOCUMENT, */
170                                                               G_PARAM_READWRITE));
171         g_object_class_install_property (g_object_class,
172                                          PROP_PRINT_DIALOG,
173                                          g_param_spec_object ("print_dialog",
174                                                               "GnomePrintDialog",
175                                                               "GnomePrintDialog with user settings",
176                                                               GNOME_TYPE_PRINT_DIALOG,
177                                                               G_PARAM_WRITABLE));
178
179 }
180
181 static void
182 ev_print_job_init (EvPrintJob *ev_print_job)
183 {
184 }
185
186 void
187 ev_print_job_set_gnome_print_job (EvPrintJob *job, GnomePrintJob *gpj)
188 {
189         g_return_if_fail (EV_IS_PRINT_JOB (job));
190
191         if (job->gnome_print_job == gpj)
192                 return;
193
194         if (job->gnome_print_job)
195                 g_object_unref (job->gnome_print_job);
196         
197         if (gpj)
198                 g_object_ref (gpj);
199
200         job->gnome_print_job = gpj;
201 }
202
203 void
204 ev_print_job_set_document (EvPrintJob *job, EvDocument *document)
205 {
206         g_return_if_fail (EV_IS_PRINT_JOB (job));
207
208         if (job->document == document)
209                 return;
210
211         if (job->document)
212                 g_object_ref (job->document);
213
214         if (document)
215                 g_object_ref (document);
216         
217         job->document = document;
218 }
219
220 void
221 ev_print_job_use_print_dialog_settings (EvPrintJob *job, GnomePrintDialog *dialog)
222 {
223         GnomePrintConfig *print_config;
224         EvPageCache *page_cache = ev_page_cache_get (job->document);
225
226         g_return_if_fail (EV_IS_PRINT_JOB (job));
227         g_return_if_fail (GNOME_IS_PRINT_DIALOG (dialog));
228
229         print_config = gnome_print_dialog_get_config (dialog);
230         gnome_print_dialog_get_copies (dialog, &job->copies, &job->collate);
231         gnome_print_config_get_page_size (print_config,
232                                           &job->width, &job->height);
233         gnome_print_config_get_boolean (print_config,
234                                         (guchar *)GNOME_PRINT_KEY_DUPLEX, &job->duplex);
235
236         page_cache = ev_page_cache_get (job->document);
237
238         /* get the printing ranges */
239         switch (gnome_print_dialog_get_range (dialog)) {
240         case GNOME_PRINT_RANGE_ALL:
241                 job->first_page = 0;
242                 job->last_page = ev_page_cache_get_n_pages (page_cache) - 1;
243                 break;
244         case GNOME_PRINT_RANGE_RANGE:
245                 gnome_print_dialog_get_range_page (dialog, &job->first_page, &job->last_page);
246                 /* convert 1-based user interface to 0-based internal numbers */
247                 job->first_page--;
248                 job->last_page--;
249                 break;
250         default:
251                 g_assert_not_reached ();
252         }
253
254         gnome_print_config_unref (print_config);
255 }
256
257 static gboolean
258 idle_print_handler (EvPrintJob *job)
259 {
260         if (!job->printing) {
261                 ev_document_doc_mutex_lock ();
262                 ev_ps_exporter_begin (
263                         EV_PS_EXPORTER (job->document),
264                         job->temp_file, job->first_page, job->last_page,
265                         job->width, job->height, job->duplex);
266                 ev_document_doc_mutex_unlock ();
267                 job->next_page = job->first_page;
268                 job->printing = TRUE;
269                 return TRUE;
270         }
271
272         if (job->next_page <= job->last_page) {
273                 EvRenderContext *rc;
274 #if 0
275                 g_printerr ("Printing page %d\n", job->next_page);
276 #endif
277                 rc = ev_render_context_new (0, job->next_page, 1.0);
278
279                 ev_document_doc_mutex_lock ();
280                 ev_ps_exporter_do_page (EV_PS_EXPORTER (job->document), rc);
281                 ev_document_doc_mutex_unlock ();
282
283                 g_object_unref (rc);
284
285                 if (job->collate) {
286                         /* collate must repeat the same page */
287                         job->copies_done++;
288                         if(job->copies == job->copies_done) {
289                                 job->next_page++;
290                                 job->copies_done = 0;
291                         }
292                 } else {
293                         job->next_page++;
294                         if (job->next_page > job->last_page){
295                                 job->copies_done++;
296                                 if(job->copies_done < job->copies) {
297                                         /* more copies to go, restart to the first page */
298                                         job->next_page = job->first_page;
299                                 }
300                         }
301                 }
302                 return TRUE;
303         } else { /* no more pages or copies */
304                 ev_document_doc_mutex_lock ();
305                 ev_ps_exporter_end (EV_PS_EXPORTER (job->document));
306                 ev_document_doc_mutex_unlock ();
307
308                 close (job->fd);
309                 job->fd = 0;
310
311                 gnome_print_job_print (job->gnome_print_job);
312
313                 unlink (job->temp_file);
314                 g_free (job->temp_file);
315
316                 g_object_unref (job->gnome_print_job);
317                 job->gnome_print_job = NULL;
318
319                 job->printing = FALSE;
320                 job->idle_id = 0;
321                 return FALSE;
322         }
323 }
324
325 static void
326 print_closure_finalize (EvPrintJob *job, GClosure *closure)
327 {
328         g_object_unref (job);
329 }
330
331 void
332 ev_print_job_print (EvPrintJob *job, GtkWindow *parent)
333 {
334         GClosure *closure;
335         GSource *idle_source;
336
337         g_return_if_fail (EV_IS_PRINT_JOB (job));
338         g_return_if_fail (job->document != NULL);
339         g_return_if_fail (EV_IS_PS_EXPORTER (job->document));
340 #if 0
341         g_printerr ("Printing...\n");
342 #endif
343
344         job->fd = g_file_open_tmp ("evince_print.ps.XXXXXX", &job->temp_file, NULL);
345         if (job->fd <= -1)
346                 return; /* FIXME use GError */
347
348         gnome_print_job_set_file (job->gnome_print_job, job->temp_file);
349
350         g_object_ref (job);
351         closure = g_cclosure_new (G_CALLBACK (idle_print_handler), job, NULL);
352         g_closure_add_finalize_notifier (
353                 closure, job, (GClosureNotify)print_closure_finalize);
354
355         idle_source = g_idle_source_new ();
356         g_source_set_closure (idle_source, closure);
357         job->idle_id = g_source_attach (idle_source, NULL);
358         g_source_unref (idle_source);
359 }