Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/ev-print-job.c
blob: 39e334be08121efd196c0e09a73daf265f10354a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* this file is part of evince, a gnome document viewer
 *
 *  Copyright (C) 2004 Martin Kretzschmar
 *
 *  Author:
 *    Martin Kretzschmar <martink@gnome.org>
 *
 * Evince is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Evince is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include <unistd.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>

/* for gnome_print_job_set_file */
#define GNOME_PRINT_UNSTABLE_API
#include <libgnomeprint/gnome-print-job.h>

#include "ev-file-exporter.h"
#include "ev-print-job.h"
#include "ev-page-cache.h"

#define EV_PRINT_JOB_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST((klass), EV_PRINT_JOB, EvPrintJobClass))
#define EV_IS_PRINT_JOB_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE((klass), EV_PRINT_JOB))
#define EV_PRINT_JOB_GET_CLASS(object)	(G_TYPE_INSTANCE_GET_CLASS((object), EV_PRINT_JOB, EvPrintJobClass))

struct _EvPrintJob {
	GObject parent_instance;

	EvDocument *document;
	GnomePrintJob *gnome_print_job;
	EvFileExporterContext fc;
	int copies;
	int collate;

	int fd;
	char *temp_file;
	guint idle_id;
	gboolean printing;
	int next_page;
	int copies_done;
	int shift;
};

struct _EvPrintJobClass {
	GObjectClass parent_class;
};

enum {
	PROP_0,
	PROP_GNOME_PRINT_JOB,
	PROP_DOCUMENT,
	PROP_PRINT_DIALOG
};

G_DEFINE_TYPE (EvPrintJob, ev_print_job, G_TYPE_OBJECT);

static void
ev_print_job_finalize (GObject *object)
{
	EvPrintJob *job = EV_PRINT_JOB (object);

	if (job && job->document) {
		g_object_unref (job->document);
		job->document = NULL;
	}

	if (job && job->gnome_print_job) {
		g_object_unref (job->gnome_print_job);
		job->gnome_print_job = NULL;
	}

	G_OBJECT_CLASS (ev_print_job_parent_class)->finalize (object);
}

static void
ev_print_job_set_property (GObject *object, guint prop_id,
			   const GValue *value, GParamSpec *pspec)
{
	EvPrintJob *job;

	job = EV_PRINT_JOB (object);

	switch (prop_id) {
	case PROP_GNOME_PRINT_JOB:
		ev_print_job_set_gnome_print_job (
			job, GNOME_PRINT_JOB (g_value_get_object (value)));
		break;
	case PROP_DOCUMENT:
		ev_print_job_set_document (job, EV_DOCUMENT (g_value_get_object (value)));
		break;
	case PROP_PRINT_DIALOG:
		ev_print_job_use_print_dialog_settings (
			job, GNOME_PRINT_DIALOG (g_value_get_object (value)));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}

}

static void
ev_print_job_get_property (GObject *object, guint prop_id,
			   GValue *value, GParamSpec *pspec)
{
	EvPrintJob *job;

	job = EV_PRINT_JOB (object);

	switch (prop_id) {
	case PROP_GNOME_PRINT_JOB:
		g_value_set_object (value, job->gnome_print_job);
		break;
	case PROP_DOCUMENT:
		g_value_set_object (value, job->document);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}


static void
ev_print_job_class_init (EvPrintJobClass *ev_print_job_class)
{
	GObjectClass *g_object_class;

	g_object_class = G_OBJECT_CLASS (ev_print_job_class);

	g_object_class->finalize = ev_print_job_finalize;
	g_object_class->set_property = ev_print_job_set_property;
	g_object_class->get_property = ev_print_job_get_property;

	g_object_class_install_property (g_object_class,
					 PROP_GNOME_PRINT_JOB,
					 g_param_spec_object ("gnome_print_job",
							      "GnomePrintJob",
							      "GnomePrintJob",
							      GNOME_TYPE_PRINT_JOB,
							      G_PARAM_READWRITE));
	g_object_class_install_property (g_object_class,
					 PROP_DOCUMENT,
					 g_param_spec_object ("document",
							      "Document object",
							      "Document from which to print",
							      G_TYPE_OBJECT, /* EV_TYPE_DOCUMENT, */
							      G_PARAM_READWRITE));
	g_object_class_install_property (g_object_class,
					 PROP_PRINT_DIALOG,
					 g_param_spec_object ("print_dialog",
							      "GnomePrintDialog",
							      "GnomePrintDialog with user settings",
							      GNOME_TYPE_PRINT_DIALOG,
							      G_PARAM_WRITABLE));

}

static void
ev_print_job_init (EvPrintJob *ev_print_job)
{
	ev_print_job->fc.pages_per_sheet = 1;
}

void
ev_print_job_set_gnome_print_job (EvPrintJob *job, GnomePrintJob *gpj)
{
	g_return_if_fail (EV_IS_PRINT_JOB (job));

	if (job->gnome_print_job == gpj)
		return;

	if (job->gnome_print_job)
		g_object_unref (job->gnome_print_job);
	
	if (gpj)
		g_object_ref (gpj);

	job->gnome_print_job = gpj;
}

void
ev_print_job_set_document (EvPrintJob *job, EvDocument *document)
{
	g_return_if_fail (EV_IS_PRINT_JOB (job));

	if (job->document == document)
		return;

	if (job->document)
		g_object_ref (job->document);

	if (document)
		g_object_ref (document);
	
	job->document = document;
}

void
ev_print_job_use_print_dialog_settings (EvPrintJob *job, GnomePrintDialog *dialog)
{
	GnomePrintConfig *print_config;
	EvPageCache *page_cache = ev_page_cache_get (job->document);
	gint first_page, last_page;

	g_return_if_fail (EV_IS_PRINT_JOB (job));
	g_return_if_fail (GNOME_IS_PRINT_DIALOG (dialog));

	print_config = gnome_print_dialog_get_config (dialog);
	gnome_print_dialog_get_copies (dialog, &job->copies, &job->collate);
	gnome_print_config_get_page_size (print_config,
					  &job->fc.paper_width, &job->fc.paper_height);
	gnome_print_config_get_boolean (print_config,
					(guchar *)GNOME_PRINT_KEY_DUPLEX, &job->fc.duplex);

	page_cache = ev_page_cache_get (job->document);

	/* get the printing ranges */
	switch (gnome_print_dialog_get_range (dialog)) {
	case GNOME_PRINT_RANGE_ALL:
		first_page = 0;
		last_page = ev_page_cache_get_n_pages (page_cache) - 1;
		break;
	case GNOME_PRINT_RANGE_RANGE:
		gnome_print_dialog_get_range_page (dialog, &first_page, &last_page);
		/* convert 1-based user interface to 0-based internal numbers */
		first_page--;
		last_page--;
		break;
	default:
		g_assert_not_reached ();
	}

	job->fc.first_page = MIN (first_page, last_page);
	job->fc.last_page = MAX (first_page, last_page);

	gnome_print_config_unref (print_config);
}

static gboolean
idle_print_handler (EvPrintJob *job)
{
	if (!job->printing) {
		ev_document_doc_mutex_lock ();
		ev_file_exporter_begin (EV_FILE_EXPORTER (job->document),
					&(job->fc));
		ev_document_doc_mutex_unlock ();
		job->next_page = job->fc.first_page;
		job->shift = (job->fc.first_page > job->fc.last_page) ? -1 : 1;
		job->printing = TRUE;
		return TRUE;
	}
	
	if ((job->next_page - job->fc.last_page) * job->shift <= 0) {
		EvRenderContext *rc;
#if 0
		g_printerr ("Printing page %d\n", job->next_page);
#endif
		rc = ev_render_context_new (0, job->next_page, 1.0);

		ev_document_doc_mutex_lock ();
		ev_file_exporter_do_page (EV_FILE_EXPORTER (job->document), rc);
		ev_document_doc_mutex_unlock ();

		g_object_unref (rc);

		if (job->collate) {
			/* collate must repeat the same page */
			job->copies_done++;
			if(job->copies == job->copies_done) {
				job->next_page += job->shift;
				job->copies_done = 0;
			}
		} else {
			job->next_page += job->shift;
			if ((job->next_page - job->fc.last_page) * job->shift > 0){
			        job->copies_done++;
				if(job->copies_done < job->copies) {
					/* more copies to go, restart to the first page */
					job->next_page = job->fc.first_page;
				}
			}
		}
		return TRUE;
	} else { /* no more pages or copies */
		ev_document_doc_mutex_lock ();
		ev_file_exporter_end (EV_FILE_EXPORTER (job->document));
		ev_document_doc_mutex_unlock ();

		close (job->fd);
		job->fd = 0;

		gnome_print_job_print (job->gnome_print_job);

		unlink (job->temp_file);
		g_free (job->temp_file);

		g_object_unref (job->gnome_print_job);
		job->gnome_print_job = NULL;

		job->printing = FALSE;
		job->idle_id = 0;
		return FALSE;
	}
}

static void
print_closure_finalize (EvPrintJob *job, GClosure *closure)
{
	g_object_unref (job);
}

void
ev_print_job_print (EvPrintJob *job, GtkWindow *parent)
{
	GClosure *closure;
	GSource *idle_source;

	g_return_if_fail (EV_IS_PRINT_JOB (job));
	g_return_if_fail (job->document != NULL);
	g_return_if_fail (EV_IS_FILE_EXPORTER (job->document));
#if 0
	g_printerr ("Printing...\n");
#endif

	job->fd = g_file_open_tmp ("evince_print.ps.XXXXXX", &job->temp_file, NULL);
	if (job->fd <= -1)
		return; /* FIXME use GError */

	job->fc.format = EV_FILE_FORMAT_PS;
	job->fc.filename = job->temp_file;
	gnome_print_job_set_file (job->gnome_print_job, job->temp_file);

	g_object_ref (job);
	closure = g_cclosure_new (G_CALLBACK (idle_print_handler), job, NULL);
	g_closure_add_finalize_notifier (
		closure, job, (GClosureNotify)print_closure_finalize);

	idle_source = g_idle_source_new ();
	g_source_set_closure (idle_source, closure);
	job->idle_id = g_source_attach (idle_source, NULL);
	g_source_unref (idle_source);
}