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