Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/libgnomeprint/ChangeLog,v
retrieving revision 1.531
diff -p -u -r1.531 ChangeLog
--- ChangeLog	1 Apr 2005 08:37:35 -0000	1.531
+++ ChangeLog	8 May 2005 22:23:54 -0000
@@ -1,3 +1,9 @@
+2005-05-09  Juerg Billeter <j@bitron.ch>
+
+	* libgnomeprint/gnome-print-job.c: (gnome_print_job_print_file),
+	(exec_filter): When using input file, preprocess file using cups resp.
+	ghostscript filter to respect layout and paper settings.
+
 2005-04-01  Adi Attar  <aattar@cvs.gnome.org>
 
 	* configure.in: Added "xh" to ALL_LINGUAS.
Index: libgnomeprint/gnome-print-job.c
===================================================================
RCS file: /cvs/gnome/libgnomeprint/libgnomeprint/gnome-print-job.c,v
retrieving revision 1.69
diff -p -u -r1.69 gnome-print-job.c
--- libgnomeprint/gnome-print-job.c	31 Dec 2004 17:14:53 -0000	1.69
+++ libgnomeprint/gnome-print-job.c	8 May 2005 22:23:55 -0000
@@ -31,6 +31,8 @@
 #include <stdlib.h>
 #include <locale.h>
 
+#include <glib/gstdio.h>
+
 #include <libart_lgpl/art_affine.h>
 #include <libgnomeprint/gpa/gpa-node.h>
 #include <libgnomeprint/gnome-print-private.h>
@@ -491,6 +493,123 @@ metadata_printer_done:
 	return TRUE;
 }
 
+static char *
+exec_filter (const char *cmd_line)
+{
+	char *temp_file, *buf;
+	FILE *p;
+	int fd, count;
+	
+	temp_file = g_build_filename (g_get_tmp_dir (), "gnome-print-XXXXXX",
+				      NULL);
+	p = popen (cmd_line, "r");
+	if (p == NULL) {
+		g_warning ("Opening '%s' for input failed", cmd_line);
+		g_free (temp_file);
+		return NULL;
+	}
+	fd = g_mkstemp (temp_file);
+	if (fd < 0) {
+		g_warning ("Cannot create temporary file");
+		pclose (p);
+		g_free (temp_file);
+		return NULL;
+	}
+	buf = (char *) g_malloc (1024);
+	while (count = fread (buf, 1, 1024, p))
+		write (fd, buf, count);
+	g_free (buf);
+	close (fd);
+	if (pclose (p) != 0) {
+		g_warning ("Executing '%s' failed", cmd_line);
+		g_free (temp_file);
+		return NULL;
+	}
+	
+	return temp_file;
+}
+
+#define PSTOPS_PATH "/usr/lib/cups/filter/pstops"
+#define GENERIC_PPD "/usr/share/ppd/Generic/Generic-PostScript_Printer-Postscript.ppd"
+
+static gint
+gnome_print_job_print_file (GnomePrintJob *job)
+{
+	char *ps_temp_file, *pdf_temp_file, *output_file, *cmd_line;
+	char *ncopies, *value, *sides, *nup, *page_size, *drivername;
+	gboolean duplex = FALSE, tumble = FALSE;
+	int ret;
+	GnomePrintTransport *transport;
+
+	ps_temp_file = pdf_temp_file = NULL;
+	output_file = job->input_file;
+
+	if (g_file_test (PSTOPS_PATH, G_FILE_TEST_IS_EXECUTABLE)) {
+		ncopies = gnome_print_config_get (job->config,
+						  GNOME_PRINT_KEY_NUM_COPIES);
+		page_size = gnome_print_config_get (job->config,
+						    GNOME_PRINT_KEY_PAPER_SIZE);
+
+		gnome_print_config_get_boolean (job->config,
+					GNOME_PRINT_KEY_DUPLEX, &duplex);
+		gnome_print_config_get_boolean (job->config,
+					GNOME_PRINT_KEY_DUPLEX, &tumble);
+		if (!duplex)
+			sides = "one-sided";
+		else if (!tumble)
+			sides = "two-sided-long-edge";
+		else
+			sides = "two-sided-short-edge";
+
+		value = gnome_print_config_get (job->config,
+						GNOME_PRINT_KEY_LAYOUT);
+		if (strcmp (value, "2_1") == 0)
+			nup = "2";
+		else if (strcmp (value, "4_1") == 0)
+			nup = "4";
+		else
+			nup = "1";
+		g_free (value);
+
+		cmd_line = g_strdup_printf ("PPD=" GENERIC_PPD " " PSTOPS_PATH
+			" x x x %s \"PageSize=%s sides=%s number-up=%s\" %s 2>/dev/null",
+			ncopies, page_size, sides, nup, output_file);
+		g_free (ncopies);
+		g_free (page_size);
+		ps_temp_file = exec_filter (cmd_line);
+		g_free (cmd_line);
+		
+		if (ps_temp_file)
+			output_file = ps_temp_file;
+	}
+
+	drivername = gnome_print_config_get (job->config,
+					     "Settings.Engine.Backend.Driver");
+	if (drivername && strcmp (drivername, "gnome-print-pdf") == 0) {
+		cmd_line = g_strdup_printf ("ps2pdf %s - 2>/dev/null", output_file);
+		pdf_temp_file = exec_filter (cmd_line);
+		g_free (cmd_line);
+
+		if (pdf_temp_file)
+			output_file = pdf_temp_file;
+	}
+	g_free (drivername);
+
+	transport = gnome_print_transport_new (job->config);
+	ret = gnome_print_transport_print_file (transport, output_file);
+
+	if (pdf_temp_file) {
+		g_unlink (pdf_temp_file);
+		g_free (pdf_temp_file);
+	}
+	if (ps_temp_file) {
+		g_unlink (ps_temp_file);
+		g_free (ps_temp_file);
+	}
+
+	return ret;
+}
+
 /**
  * gnome_print_job_print:
  * @job: A closed GnomePrintJob.
@@ -515,10 +634,8 @@ gnome_print_job_print (GnomePrintJob *jo
 	g_return_val_if_fail (GNOME_IS_PRINT_JOB (job), GNOME_PRINT_ERROR_UNKNOWN);
 	g_return_val_if_fail (job->priv, GNOME_PRINT_ERROR_UNKNOWN);
 
-	if (job->input_file) {
-		GnomePrintTransport *transport = gnome_print_transport_new (job->config);
-		return gnome_print_transport_print_file (transport, job->input_file);
-	}
+	if (job->input_file)
+		return gnome_print_job_print_file (job);
 
 	if (!job->priv->closed) {
 		g_warning ("You should call gnome_print_job_close before calling\n"
