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