Index: backend/dvi/dvi-document.c
===================================================================
--- backend/dvi/dvi-document.c	(revision 2488)
+++ backend/dvi/dvi-document.c	(working copy)
@@ -20,6 +20,7 @@
 #include "dvi-document.h"
 #include "ev-document-thumbnails.h"
 #include "ev-document-misc.h"
+#include "ev-file-exporter.h"
 
 #include "mdvi.h"
 #include "fonts.h"
@@ -54,6 +55,10 @@
 	double base_height;
 	
 	gchar *uri;
+
+	/* PS exporter */
+	gchar		 *ps_filename;
+        GString 	 *opts;
 };
 
 typedef struct _DviDocumentClass DviDocumentClass;
@@ -61,6 +66,7 @@
 static void dvi_document_do_color_special (DviContext *dvi, const char *prefix, const char *arg);
 static void dvi_document_document_iface_init (EvDocumentIface *iface);
 static void dvi_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
+static void dvi_document_file_exporter_iface_init (EvFileExporterIface *iface);
 static void dvi_document_get_page_size 			(EvDocument   *document,
 					    		 int       page,
 							 double    *width,
@@ -70,7 +76,8 @@
     (DviDocument, dvi_document, G_TYPE_OBJECT, 
     {
       G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, dvi_document_document_iface_init);    
-      G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init)
+      G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, dvi_document_document_thumbnails_iface_init);
+      G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, dvi_document_file_exporter_iface_init);
      });
 
 static gboolean
@@ -216,6 +223,12 @@
 	if (dvi_document->params)
 		g_free (dvi_document->params);
 
+	if (dvi_document->ps_filename)
+		g_free (dvi_document->ps_filename);
+	
+	if (dvi_document->opts)
+		g_string_free (dvi_document->opts, TRUE);
+
         g_free (dvi_document->uri);
 		
 	G_OBJECT_CLASS (dvi_document_parent_class)->finalize (object);
@@ -345,6 +358,88 @@
 	iface->get_dimensions = dvi_document_thumbnails_get_dimensions;
 }
 
+/* EvFileExporterIface */
+static gboolean
+dvi_document_file_exporter_format_supported (EvFileExporter      *exporter,
+                                              EvFileExporterFormat format)
+{
+        return (format == EV_FILE_FORMAT_PS); /* only exporting to PS is implemented. */
+}
+
+static void
+dvi_document_file_exporter_begin (EvFileExporter      *exporter,
+				   EvFileExporterFormat format,
+				   const char          *filename, /* for storing the temp ps file */
+				   int                  first_page,
+				   int                  last_page,
+				   double               width,
+				   double               height,
+				   gboolean             duplex)
+{
+	DviDocument *dvi_document = DVI_DOCUMENT(exporter);
+	
+	if (dvi_document->ps_filename)
+		g_free (dvi_document->ps_filename);	
+	dvi_document->ps_filename = g_strdup(filename);
+	
+	if (dvi_document->opts) {
+		g_string_free (dvi_document->opts, TRUE);
+	}
+	dvi_document->opts = g_string_new ("");
+	g_string_assign(dvi_document->opts, "-pp");
+}
+
+static void
+dvi_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
+{
+       DviDocument *dvi_document = DVI_DOCUMENT(exporter);
+
+       g_string_append_printf(dvi_document->opts, "%d,", (rc->page)+1);
+}
+
+static void
+dvi_document_file_exporter_end (EvFileExporter *exporter)
+{
+	gchar *command_line;
+	gint exit_stat;
+	GError *err = NULL;
+	gboolean success;
+	
+	DviDocument *dvi_document = DVI_DOCUMENT(exporter);
+	
+	command_line = g_strdup_printf ("%s %s %s -o%s", /* dvips filename.dvi -pp1,2,.. -ops_filename */
+					"dvips",
+					dvi_document->context->filename,
+					dvi_document->opts->str,
+					dvi_document->ps_filename);
+	
+	success = g_spawn_command_line_sync (command_line,
+					     NULL,
+					     NULL,
+					     &exit_stat,
+					     &err);
+
+	g_free(command_line);
+
+	if (success == FALSE) {
+		g_warning (_("Error: %s"), err->message);
+	} else if (exit_stat != 0) {
+		g_warning (_("Error: dvips exited with non-zero status."));
+	}
+
+	if (err)
+		g_error_free(err);
+}
+
+static void
+dvi_document_file_exporter_iface_init (EvFileExporterIface *iface)
+{
+        iface->format_supported = dvi_document_file_exporter_format_supported;
+        iface->begin = dvi_document_file_exporter_begin;
+        iface->do_page = dvi_document_file_exporter_do_page;
+        iface->end = dvi_document_file_exporter_end;
+}
+
 #define RGB2ULONG(r,g,b) ((0xFF<<24)|(r<<16)|(g<<8)|(b))
 
 static gboolean
@@ -480,4 +575,7 @@
 {
 	dvi_document->context = NULL;
 	dvi_document_init_params (dvi_document);
+
+	dvi_document->ps_filename = NULL;
+	dvi_document->opts = NULL;
 }
