2008-07-01 22:57:50 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
2007-09-24 17:12:33 +00:00
|
|
|
* gtkprintbackendfile.c: Default implementation of GtkPrintBackend
|
2006-06-20 18:42:46 +00:00
|
|
|
* for printing to a file
|
2006-04-21 15:09:32 +00:00
|
|
|
* Copyright (C) 2003, Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2006-06-20 18:42:46 +00:00
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <cairo.h>
|
|
|
|
#include <cairo-pdf.h>
|
2006-06-21 19:06:58 +00:00
|
|
|
#include <cairo-ps.h>
|
2009-08-03 09:25:19 +00:00
|
|
|
#include <cairo-svg.h>
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
#include <glib/gi18n-lib.h>
|
|
|
|
|
2008-05-28 12:49:11 +00:00
|
|
|
#include "gtk/gtk.h"
|
|
|
|
#include "gtk/gtkprinter-private.h"
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
#include "gtkprintbackendfile.h"
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
typedef struct _GtkPrintBackendFileClass GtkPrintBackendFileClass;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
#define GTK_PRINT_BACKEND_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_FILE, GtkPrintBackendFileClass))
|
|
|
|
#define GTK_IS_PRINT_BACKEND_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_FILE))
|
|
|
|
#define GTK_PRINT_BACKEND_FILE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_FILE, GtkPrintBackendFileClass))
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
#define _STREAM_MAX_CHUNK_SIZE 8192
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
static GType print_backend_file_type = 0;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
struct _GtkPrintBackendFileClass
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-05-04 13:43:32 +00:00
|
|
|
GtkPrintBackendClass parent_class;
|
2006-04-21 15:09:32 +00:00
|
|
|
};
|
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
struct _GtkPrintBackendFile
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-05-04 13:43:32 +00:00
|
|
|
GtkPrintBackend parent_instance;
|
2006-04-21 15:09:32 +00:00
|
|
|
};
|
|
|
|
|
2006-06-21 19:06:58 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
FORMAT_PDF,
|
|
|
|
FORMAT_PS,
|
2009-08-03 09:25:19 +00:00
|
|
|
FORMAT_SVG,
|
2006-06-21 19:06:58 +00:00
|
|
|
N_FORMATS
|
|
|
|
} OutputFormat;
|
|
|
|
|
|
|
|
static const gchar* formats[N_FORMATS] =
|
|
|
|
{
|
|
|
|
"pdf",
|
2009-08-03 09:25:19 +00:00
|
|
|
"ps",
|
|
|
|
"svg"
|
2006-06-21 19:06:58 +00:00
|
|
|
};
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
static GObjectClass *backend_parent_class;
|
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
static void gtk_print_backend_file_class_init (GtkPrintBackendFileClass *class);
|
|
|
|
static void gtk_print_backend_file_init (GtkPrintBackendFile *impl);
|
|
|
|
static void file_printer_get_settings_from_options (GtkPrinter *printer,
|
|
|
|
GtkPrinterOptionSet *options,
|
|
|
|
GtkPrintSettings *settings);
|
|
|
|
static GtkPrinterOptionSet *file_printer_get_options (GtkPrinter *printer,
|
|
|
|
GtkPrintSettings *settings,
|
|
|
|
GtkPageSetup *page_setup,
|
|
|
|
GtkPrintCapabilities capabilities);
|
|
|
|
static void file_printer_prepare_for_print (GtkPrinter *printer,
|
|
|
|
GtkPrintJob *print_job,
|
|
|
|
GtkPrintSettings *settings,
|
|
|
|
GtkPageSetup *page_setup);
|
2006-06-21 05:56:14 +00:00
|
|
|
static void gtk_print_backend_file_print_stream (GtkPrintBackend *print_backend,
|
2006-06-20 18:42:46 +00:00
|
|
|
GtkPrintJob *job,
|
2006-06-21 18:16:58 +00:00
|
|
|
GIOChannel *data_io,
|
2006-06-20 18:42:46 +00:00
|
|
|
GtkPrintJobCompleteFunc callback,
|
|
|
|
gpointer user_data,
|
2006-06-21 18:16:58 +00:00
|
|
|
GDestroyNotify dnotify);
|
2006-06-20 18:42:46 +00:00
|
|
|
static cairo_surface_t * file_printer_create_cairo_surface (GtkPrinter *printer,
|
|
|
|
GtkPrintSettings *settings,
|
|
|
|
gdouble width,
|
|
|
|
gdouble height,
|
2006-06-21 18:16:58 +00:00
|
|
|
GIOChannel *cache_io);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2007-04-30 06:03:01 +00:00
|
|
|
static GList * file_printer_list_papers (GtkPrinter *printer);
|
2008-05-21 02:17:30 +00:00
|
|
|
static GtkPageSetup * file_printer_get_default_page_size (GtkPrinter *printer);
|
2007-04-30 06:03:01 +00:00
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
static void
|
2006-06-20 18:42:46 +00:00
|
|
|
gtk_print_backend_file_register_type (GTypeModule *module)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2009-11-06 00:21:09 +00:00
|
|
|
const GTypeInfo print_backend_file_info =
|
2006-05-05 20:22:27 +00:00
|
|
|
{
|
2006-06-20 18:42:46 +00:00
|
|
|
sizeof (GtkPrintBackendFileClass),
|
2006-05-05 20:22:27 +00:00
|
|
|
NULL, /* base_init */
|
|
|
|
NULL, /* base_finalize */
|
2006-06-20 18:42:46 +00:00
|
|
|
(GClassInitFunc) gtk_print_backend_file_class_init,
|
2006-05-05 20:22:27 +00:00
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
2006-06-20 18:42:46 +00:00
|
|
|
sizeof (GtkPrintBackendFile),
|
2006-05-05 20:22:27 +00:00
|
|
|
0, /* n_preallocs */
|
2006-06-20 18:42:46 +00:00
|
|
|
(GInstanceInitFunc) gtk_print_backend_file_init,
|
2006-05-05 20:22:27 +00:00
|
|
|
};
|
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
print_backend_file_type = g_type_module_register_type (module,
|
|
|
|
GTK_TYPE_PRINT_BACKEND,
|
|
|
|
"GtkPrintBackendFile",
|
|
|
|
&print_backend_file_info, 0);
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
G_MODULE_EXPORT void
|
|
|
|
pb_module_init (GTypeModule *module)
|
|
|
|
{
|
2006-06-20 18:42:46 +00:00
|
|
|
gtk_print_backend_file_register_type (module);
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
G_MODULE_EXPORT void
|
|
|
|
pb_module_exit (void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
G_MODULE_EXPORT GtkPrintBackend *
|
|
|
|
pb_module_create (void)
|
|
|
|
{
|
2006-06-20 18:42:46 +00:00
|
|
|
return gtk_print_backend_file_new ();
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-06-20 18:42:46 +00:00
|
|
|
* GtkPrintBackendFile
|
2006-04-21 15:09:32 +00:00
|
|
|
*/
|
|
|
|
GType
|
2006-06-20 18:42:46 +00:00
|
|
|
gtk_print_backend_file_get_type (void)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-06-20 18:42:46 +00:00
|
|
|
return print_backend_file_type;
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-06-20 18:42:46 +00:00
|
|
|
* gtk_print_backend_file_new:
|
2006-04-21 15:09:32 +00:00
|
|
|
*
|
2006-06-20 18:42:46 +00:00
|
|
|
* Creates a new #GtkPrintBackendFile object. #GtkPrintBackendFile
|
2006-04-21 15:09:32 +00:00
|
|
|
* implements the #GtkPrintBackend interface with direct access to
|
|
|
|
* the filesystem using Unix/Linux API calls
|
|
|
|
*
|
2006-06-20 18:42:46 +00:00
|
|
|
* Return value: the new #GtkPrintBackendFile object
|
2006-04-21 15:09:32 +00:00
|
|
|
**/
|
|
|
|
GtkPrintBackend *
|
2006-06-20 18:42:46 +00:00
|
|
|
gtk_print_backend_file_new (void)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-06-20 18:42:46 +00:00
|
|
|
return g_object_new (GTK_TYPE_PRINT_BACKEND_FILE, NULL);
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-06-20 18:42:46 +00:00
|
|
|
gtk_print_backend_file_class_init (GtkPrintBackendFileClass *class)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-05-04 13:43:32 +00:00
|
|
|
GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
backend_parent_class = g_type_class_peek_parent (class);
|
|
|
|
|
2006-06-20 18:42:46 +00:00
|
|
|
backend_class->print_stream = gtk_print_backend_file_print_stream;
|
|
|
|
backend_class->printer_create_cairo_surface = file_printer_create_cairo_surface;
|
|
|
|
backend_class->printer_get_options = file_printer_get_options;
|
|
|
|
backend_class->printer_get_settings_from_options = file_printer_get_settings_from_options;
|
|
|
|
backend_class->printer_prepare_for_print = file_printer_prepare_for_print;
|
2007-04-30 06:03:01 +00:00
|
|
|
backend_class->printer_list_papers = file_printer_list_papers;
|
2008-05-21 02:17:30 +00:00
|
|
|
backend_class->printer_get_default_page_size = file_printer_get_default_page_size;
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
2006-08-17 04:57:00 +00:00
|
|
|
/* return N_FORMATS if no explicit format in the settings */
|
2006-06-21 19:06:58 +00:00
|
|
|
static OutputFormat
|
|
|
|
format_from_settings (GtkPrintSettings *settings)
|
|
|
|
{
|
|
|
|
const gchar *value;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (settings == NULL)
|
2006-08-17 04:57:00 +00:00
|
|
|
return N_FORMATS;
|
2006-06-21 19:06:58 +00:00
|
|
|
|
2007-08-09 15:05:23 +00:00
|
|
|
value = gtk_print_settings_get (settings,
|
|
|
|
GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
|
2006-06-21 19:06:58 +00:00
|
|
|
if (value == NULL)
|
2006-08-17 04:57:00 +00:00
|
|
|
return N_FORMATS;
|
2006-06-21 19:06:58 +00:00
|
|
|
|
|
|
|
for (i = 0; i < N_FORMATS; ++i)
|
|
|
|
if (strcmp (value, formats[i]) == 0)
|
|
|
|
break;
|
2006-08-17 04:57:00 +00:00
|
|
|
|
2006-06-21 19:06:58 +00:00
|
|
|
g_assert (i < N_FORMATS);
|
|
|
|
|
|
|
|
return (OutputFormat) i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
2006-08-18 01:30:24 +00:00
|
|
|
output_file_from_settings (GtkPrintSettings *settings,
|
|
|
|
const gchar *default_format)
|
2006-06-21 19:06:58 +00:00
|
|
|
{
|
2006-08-18 01:30:24 +00:00
|
|
|
gchar *uri = NULL;
|
|
|
|
|
2006-06-21 19:06:58 +00:00
|
|
|
if (settings)
|
2006-08-18 01:30:24 +00:00
|
|
|
uri = g_strdup (gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_URI));
|
2006-06-21 19:06:58 +00:00
|
|
|
|
2006-08-18 01:30:24 +00:00
|
|
|
if (uri == NULL)
|
2006-06-21 19:06:58 +00:00
|
|
|
{
|
2006-08-18 01:30:24 +00:00
|
|
|
const gchar *extension;
|
|
|
|
gchar *name, *locale_name, *path;
|
2006-06-21 19:06:58 +00:00
|
|
|
|
2006-08-17 04:57:00 +00:00
|
|
|
if (default_format)
|
|
|
|
extension = default_format;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OutputFormat format;
|
2006-06-21 19:06:58 +00:00
|
|
|
|
2006-08-17 04:57:00 +00:00
|
|
|
format = format_from_settings (settings);
|
2009-08-03 09:25:19 +00:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case FORMAT_PDF:
|
|
|
|
extension = "pdf";
|
|
|
|
break;
|
|
|
|
case FORMAT_PS:
|
|
|
|
extension = "ps";
|
|
|
|
break;
|
|
|
|
case FORMAT_SVG:
|
|
|
|
extension = "svg";
|
|
|
|
break;
|
|
|
|
}
|
2006-08-17 04:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* default filename used for print-to-file */
|
2006-08-18 01:30:24 +00:00
|
|
|
name = g_strdup_printf (_("output.%s"), extension);
|
|
|
|
locale_name = g_filename_from_utf8 (name, -1, NULL, NULL, NULL);
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
if (locale_name != NULL)
|
|
|
|
{
|
2008-08-29 18:14:32 +00:00
|
|
|
gchar *current_dir = g_get_current_dir ();
|
|
|
|
path = g_build_filename (current_dir, locale_name, NULL);
|
2006-08-18 01:30:24 +00:00
|
|
|
g_free (locale_name);
|
|
|
|
|
|
|
|
uri = g_filename_to_uri (path, NULL, NULL);
|
|
|
|
g_free (path);
|
2008-08-29 18:14:32 +00:00
|
|
|
g_free (current_dir);
|
2006-08-18 01:30:24 +00:00
|
|
|
}
|
2006-06-21 19:06:58 +00:00
|
|
|
}
|
|
|
|
|
2006-08-18 01:30:24 +00:00
|
|
|
return uri;
|
2006-06-21 19:06:58 +00:00
|
|
|
}
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
static cairo_status_t
|
2006-06-16 18:29:09 +00:00
|
|
|
_cairo_write (void *closure,
|
2006-04-21 15:09:32 +00:00
|
|
|
const unsigned char *data,
|
|
|
|
unsigned int length)
|
|
|
|
{
|
2006-06-21 18:16:58 +00:00
|
|
|
GIOChannel *io = (GIOChannel *)closure;
|
|
|
|
gsize written;
|
|
|
|
GError *error;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
|
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("FILE Backend: Writting %i byte chunk to temp file\n", length));
|
|
|
|
|
2006-06-16 18:29:09 +00:00
|
|
|
while (length > 0)
|
|
|
|
{
|
2006-08-18 01:30:24 +00:00
|
|
|
g_io_channel_write_chars (io, (const gchar *) data, length, &written, &error);
|
2006-06-16 18:29:09 +00:00
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
if (error != NULL)
|
2006-06-16 18:29:09 +00:00
|
|
|
{
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("FILE Backend: Error writting to temp file, %s\n", error->message));
|
|
|
|
|
|
|
|
g_error_free (error);
|
2006-06-16 18:29:09 +00:00
|
|
|
return CAIRO_STATUS_WRITE_ERROR;
|
|
|
|
}
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("FILE Backend: Wrote %i bytes to temp file\n", written));
|
|
|
|
|
2006-06-16 18:29:09 +00:00
|
|
|
data += written;
|
|
|
|
length -= written;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CAIRO_STATUS_SUCCESS;
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static cairo_surface_t *
|
2006-06-20 18:42:46 +00:00
|
|
|
file_printer_create_cairo_surface (GtkPrinter *printer,
|
|
|
|
GtkPrintSettings *settings,
|
|
|
|
gdouble width,
|
|
|
|
gdouble height,
|
2006-06-21 18:16:58 +00:00
|
|
|
GIOChannel *cache_io)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
cairo_surface_t *surface;
|
2006-06-21 19:06:58 +00:00
|
|
|
OutputFormat format;
|
2009-08-03 09:25:19 +00:00
|
|
|
const cairo_svg_version_t *versions;
|
|
|
|
int num_versions = 0;
|
2006-06-21 19:06:58 +00:00
|
|
|
|
|
|
|
format = format_from_settings (settings);
|
|
|
|
|
2009-08-03 09:25:19 +00:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case FORMAT_PDF:
|
|
|
|
surface = cairo_pdf_surface_create_for_stream (_cairo_write, cache_io, width, height);
|
|
|
|
break;
|
|
|
|
case FORMAT_PS:
|
|
|
|
surface = cairo_ps_surface_create_for_stream (_cairo_write, cache_io, width, height);
|
|
|
|
break;
|
|
|
|
case FORMAT_SVG:
|
|
|
|
surface = cairo_svg_surface_create_for_stream (_cairo_write, cache_io, width, height);
|
|
|
|
cairo_svg_get_versions (&versions, &num_versions);
|
|
|
|
if (num_versions > 0)
|
|
|
|
cairo_svg_surface_restrict_to_version (surface, versions[num_versions - 1]);
|
|
|
|
break;
|
|
|
|
}
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2008-10-30 16:24:27 +00:00
|
|
|
cairo_surface_set_fallback_resolution (surface,
|
|
|
|
2.0 * gtk_print_settings_get_printer_lpi (settings),
|
|
|
|
2.0 * gtk_print_settings_get_printer_lpi (settings));
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GtkPrintBackend *backend;
|
|
|
|
GtkPrintJobCompleteFunc callback;
|
|
|
|
GtkPrintJob *job;
|
2008-09-30 08:45:01 +00:00
|
|
|
GFileOutputStream *target_io_stream;
|
2006-04-21 15:09:32 +00:00
|
|
|
gpointer user_data;
|
|
|
|
GDestroyNotify dnotify;
|
|
|
|
} _PrintStreamData;
|
|
|
|
|
|
|
|
static void
|
2006-06-20 18:42:46 +00:00
|
|
|
file_print_cb (GtkPrintBackendFile *print_backend,
|
|
|
|
GError *error,
|
|
|
|
gpointer user_data)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
_PrintStreamData *ps = (_PrintStreamData *) user_data;
|
|
|
|
|
2007-06-12 02:12:32 +00:00
|
|
|
GDK_THREADS_ENTER ();
|
|
|
|
|
2008-09-30 08:45:01 +00:00
|
|
|
if (ps->target_io_stream != NULL)
|
|
|
|
g_output_stream_close (G_OUTPUT_STREAM (ps->target_io_stream), NULL, NULL);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
if (ps->callback)
|
|
|
|
ps->callback (ps->job, ps->user_data, error);
|
|
|
|
|
|
|
|
if (ps->dnotify)
|
|
|
|
ps->dnotify (ps->user_data);
|
|
|
|
|
|
|
|
gtk_print_job_set_status (ps->job,
|
|
|
|
(error != NULL)?GTK_PRINT_STATUS_FINISHED_ABORTED:GTK_PRINT_STATUS_FINISHED);
|
|
|
|
|
|
|
|
if (ps->job)
|
|
|
|
g_object_unref (ps->job);
|
|
|
|
|
|
|
|
g_free (ps);
|
2007-06-12 02:12:32 +00:00
|
|
|
|
|
|
|
GDK_THREADS_LEAVE ();
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2006-06-20 18:42:46 +00:00
|
|
|
file_write (GIOChannel *source,
|
|
|
|
GIOCondition con,
|
|
|
|
gpointer user_data)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-06-20 18:42:46 +00:00
|
|
|
gchar buf[_STREAM_MAX_CHUNK_SIZE];
|
2006-04-21 15:09:32 +00:00
|
|
|
gsize bytes_read;
|
|
|
|
GError *error;
|
2006-06-21 18:16:58 +00:00
|
|
|
GIOStatus read_status;
|
2006-04-21 15:09:32 +00:00
|
|
|
_PrintStreamData *ps = (_PrintStreamData *) user_data;
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
read_status =
|
|
|
|
g_io_channel_read_chars (source,
|
|
|
|
buf,
|
|
|
|
_STREAM_MAX_CHUNK_SIZE,
|
|
|
|
&bytes_read,
|
|
|
|
&error);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
if (read_status != G_IO_STATUS_ERROR)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-06-21 18:16:58 +00:00
|
|
|
gsize bytes_written;
|
|
|
|
|
2008-09-30 08:45:01 +00:00
|
|
|
g_output_stream_write_all (G_OUTPUT_STREAM (ps->target_io_stream),
|
|
|
|
buf,
|
|
|
|
bytes_read,
|
|
|
|
&bytes_written,
|
|
|
|
NULL,
|
|
|
|
&error);
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
if (error != NULL || read_status == G_IO_STATUS_EOF)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-06-20 18:42:46 +00:00
|
|
|
file_print_cb (GTK_PRINT_BACKEND_FILE (ps->backend), error, user_data);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
if (error != NULL)
|
|
|
|
{
|
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("FILE Backend: %s\n", error->message));
|
|
|
|
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
GTK_NOTE (PRINTING,
|
|
|
|
g_print ("FILE Backend: Writting %i byte chunk to target file\n", bytes_read));
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-06-20 18:42:46 +00:00
|
|
|
gtk_print_backend_file_print_stream (GtkPrintBackend *print_backend,
|
2006-06-21 05:56:14 +00:00
|
|
|
GtkPrintJob *job,
|
2006-06-21 18:16:58 +00:00
|
|
|
GIOChannel *data_io,
|
2006-06-21 05:56:14 +00:00
|
|
|
GtkPrintJobCompleteFunc callback,
|
|
|
|
gpointer user_data,
|
2006-06-21 18:16:58 +00:00
|
|
|
GDestroyNotify dnotify)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-06-21 05:56:14 +00:00
|
|
|
GError *internal_error = NULL;
|
2006-04-21 15:09:32 +00:00
|
|
|
_PrintStreamData *ps;
|
|
|
|
GtkPrintSettings *settings;
|
2008-09-30 08:45:01 +00:00
|
|
|
gchar *uri;
|
|
|
|
GFile *file = NULL;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
settings = gtk_print_job_get_settings (job);
|
|
|
|
|
|
|
|
ps = g_new0 (_PrintStreamData, 1);
|
|
|
|
ps->callback = callback;
|
|
|
|
ps->user_data = user_data;
|
2006-06-11 02:40:02 +00:00
|
|
|
ps->dnotify = dnotify;
|
2006-04-21 15:09:32 +00:00
|
|
|
ps->job = g_object_ref (job);
|
2006-06-20 17:36:55 +00:00
|
|
|
ps->backend = print_backend;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-21 19:06:58 +00:00
|
|
|
internal_error = NULL;
|
2006-08-18 01:30:24 +00:00
|
|
|
uri = output_file_from_settings (settings, NULL);
|
|
|
|
|
2008-09-30 08:45:01 +00:00
|
|
|
if (uri == NULL)
|
2006-08-18 01:30:24 +00:00
|
|
|
goto error;
|
|
|
|
|
2008-09-30 08:45:01 +00:00
|
|
|
file = g_file_new_for_uri (uri);
|
|
|
|
ps->target_io_stream = g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &internal_error);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2008-09-30 08:45:01 +00:00
|
|
|
g_object_unref (file);
|
|
|
|
g_free (uri);
|
2006-06-21 18:16:58 +00:00
|
|
|
|
2006-08-18 01:30:24 +00:00
|
|
|
error:
|
2006-06-21 18:16:58 +00:00
|
|
|
if (internal_error != NULL)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-06-21 18:16:58 +00:00
|
|
|
file_print_cb (GTK_PRINT_BACKEND_FILE (print_backend),
|
2006-06-21 19:06:58 +00:00
|
|
|
internal_error, ps);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
g_error_free (internal_error);
|
2006-04-21 15:09:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-06-21 18:16:58 +00:00
|
|
|
g_io_add_watch (data_io,
|
2006-04-21 15:09:32 +00:00
|
|
|
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
|
2006-06-20 18:42:46 +00:00
|
|
|
(GIOFunc) file_write,
|
2006-04-21 15:09:32 +00:00
|
|
|
ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-06-20 18:42:46 +00:00
|
|
|
gtk_print_backend_file_init (GtkPrintBackendFile *backend)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
GtkPrinter *printer;
|
|
|
|
|
2006-06-12 17:45:49 +00:00
|
|
|
printer = g_object_new (GTK_TYPE_PRINTER,
|
2006-06-20 18:42:46 +00:00
|
|
|
"name", _("Print to File"),
|
2006-06-12 17:45:49 +00:00
|
|
|
"backend", backend,
|
|
|
|
"is-virtual", TRUE,
|
|
|
|
NULL);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
gtk_printer_set_has_details (printer, TRUE);
|
2010-08-10 00:09:02 +00:00
|
|
|
gtk_printer_set_icon_name (printer, "document-save");
|
2006-04-21 15:09:32 +00:00
|
|
|
gtk_printer_set_is_active (printer, TRUE);
|
|
|
|
|
2006-05-04 13:43:32 +00:00
|
|
|
gtk_print_backend_add_printer (GTK_PRINT_BACKEND (backend), printer);
|
|
|
|
g_object_unref (printer);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
2006-05-04 13:43:32 +00:00
|
|
|
gtk_print_backend_set_list_done (GTK_PRINT_BACKEND (backend));
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
2007-08-09 15:05:23 +00:00
|
|
|
static void
|
|
|
|
file_printer_output_file_format_changed (GtkPrinterOption *format_option,
|
|
|
|
GtkPrinterOptionSet *set)
|
|
|
|
{
|
|
|
|
GtkPrinterOption *uri_option;
|
|
|
|
gchar *base = NULL;
|
|
|
|
|
|
|
|
if (! format_option->value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uri_option = gtk_printer_option_set_lookup (set,
|
|
|
|
"gtk-main-page-custom-input");
|
|
|
|
|
|
|
|
if (uri_option && uri_option->value)
|
|
|
|
{
|
|
|
|
const gchar *uri = uri_option->value;
|
|
|
|
const gchar *dot = strrchr (uri, '.');
|
|
|
|
|
|
|
|
if (dot)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
/* check if the file extension matches one of the known ones */
|
|
|
|
for (i = 0; i < N_FORMATS; i++)
|
|
|
|
if (strcmp (dot + 1, formats[i]) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i < N_FORMATS && strcmp (formats[i], format_option->value))
|
|
|
|
{
|
|
|
|
/* the file extension is known but doesn't match the
|
|
|
|
* selected one, strip it away
|
|
|
|
*/
|
|
|
|
base = g_strndup (uri, dot - uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* there's no file extension */
|
|
|
|
base = g_strdup (uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base)
|
|
|
|
{
|
|
|
|
gchar *tmp = g_strdup_printf ("%s.%s", base, format_option->value);
|
|
|
|
|
|
|
|
gtk_printer_option_set (uri_option, tmp);
|
|
|
|
g_free (tmp);
|
|
|
|
g_free (base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-21 15:09:32 +00:00
|
|
|
static GtkPrinterOptionSet *
|
2006-06-20 18:42:46 +00:00
|
|
|
file_printer_get_options (GtkPrinter *printer,
|
|
|
|
GtkPrintSettings *settings,
|
|
|
|
GtkPageSetup *page_setup,
|
|
|
|
GtkPrintCapabilities capabilities)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
GtkPrinterOptionSet *set;
|
|
|
|
GtkPrinterOption *option;
|
2007-05-28 10:41:09 +00:00
|
|
|
const gchar *n_up[] = {"1", "2", "4", "6", "9", "16" };
|
|
|
|
const gchar *pages_per_sheet = NULL;
|
2009-08-03 09:25:19 +00:00
|
|
|
const gchar *format_names[N_FORMATS] = { N_("PDF"), N_("Postscript"), N_("SVG") };
|
2006-06-21 19:06:58 +00:00
|
|
|
const gchar *supported_formats[N_FORMATS];
|
|
|
|
gchar *display_format_names[N_FORMATS];
|
|
|
|
gint n_formats = 0;
|
|
|
|
OutputFormat format;
|
2006-08-18 01:30:24 +00:00
|
|
|
gchar *uri;
|
2006-08-17 04:57:00 +00:00
|
|
|
gint current_format = 0;
|
2006-06-21 19:06:58 +00:00
|
|
|
|
|
|
|
format = format_from_settings (settings);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
set = gtk_printer_option_set_new ();
|
|
|
|
|
2006-06-21 19:06:58 +00:00
|
|
|
option = gtk_printer_option_new ("gtk-n-up", _("Pages per _sheet:"), GTK_PRINTER_OPTION_TYPE_PICKONE);
|
2006-04-21 15:09:32 +00:00
|
|
|
gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
|
2006-06-21 19:06:58 +00:00
|
|
|
(char **) n_up, (char **) n_up /* FIXME i18n (localised digits)! */);
|
2007-05-28 10:41:09 +00:00
|
|
|
if (settings)
|
|
|
|
pages_per_sheet = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_NUMBER_UP);
|
|
|
|
if (pages_per_sheet)
|
|
|
|
gtk_printer_option_set (option, pages_per_sheet);
|
|
|
|
else
|
|
|
|
gtk_printer_option_set (option, "1");
|
2006-04-21 15:09:32 +00:00
|
|
|
gtk_printer_option_set_add (set, option);
|
|
|
|
g_object_unref (option);
|
|
|
|
|
2006-06-21 19:06:58 +00:00
|
|
|
if (capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_GENERATE_PS))
|
|
|
|
{
|
|
|
|
if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF)
|
|
|
|
{
|
2006-08-17 04:57:00 +00:00
|
|
|
if (format == FORMAT_PDF || format == N_FORMATS)
|
|
|
|
{
|
|
|
|
format = FORMAT_PDF;
|
|
|
|
current_format = n_formats;
|
|
|
|
}
|
2006-06-21 19:06:58 +00:00
|
|
|
supported_formats[n_formats] = formats[FORMAT_PDF];
|
|
|
|
display_format_names[n_formats] = _(format_names[FORMAT_PDF]);
|
|
|
|
n_formats++;
|
|
|
|
}
|
|
|
|
if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS)
|
|
|
|
{
|
2006-08-17 04:57:00 +00:00
|
|
|
if (format == FORMAT_PS || format == N_FORMATS)
|
2006-06-21 19:06:58 +00:00
|
|
|
current_format = n_formats;
|
|
|
|
supported_formats[n_formats] = formats[FORMAT_PS];
|
|
|
|
display_format_names[n_formats] = _(format_names[FORMAT_PS]);
|
|
|
|
n_formats++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-03 09:25:19 +00:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case FORMAT_PDF:
|
|
|
|
current_format = FORMAT_PDF;
|
|
|
|
break;
|
|
|
|
case FORMAT_PS:
|
|
|
|
current_format = FORMAT_PS;
|
|
|
|
break;
|
|
|
|
case FORMAT_SVG:
|
|
|
|
current_format = FORMAT_SVG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-06-21 19:06:58 +00:00
|
|
|
for (n_formats = 0; n_formats < N_FORMATS; ++n_formats)
|
|
|
|
{
|
|
|
|
supported_formats[n_formats] = formats[n_formats];
|
|
|
|
display_format_names[n_formats] = _(format_names[n_formats]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-18 01:30:24 +00:00
|
|
|
uri = output_file_from_settings (settings, supported_formats[current_format]);
|
2006-08-17 04:57:00 +00:00
|
|
|
|
|
|
|
option = gtk_printer_option_new ("gtk-main-page-custom-input", _("File"),
|
|
|
|
GTK_PRINTER_OPTION_TYPE_FILESAVE);
|
2009-08-11 07:24:18 +00:00
|
|
|
gtk_printer_option_set_activates_default (option, TRUE);
|
2006-08-18 01:30:24 +00:00
|
|
|
gtk_printer_option_set (option, uri);
|
2007-01-30 20:19:11 +00:00
|
|
|
g_free (uri);
|
2006-08-17 04:57:00 +00:00
|
|
|
option->group = g_strdup ("GtkPrintDialogExtension");
|
|
|
|
gtk_printer_option_set_add (set, option);
|
|
|
|
|
2006-06-21 19:06:58 +00:00
|
|
|
if (n_formats > 1)
|
|
|
|
{
|
|
|
|
option = gtk_printer_option_new ("output-file-format", _("_Output format"),
|
|
|
|
GTK_PRINTER_OPTION_TYPE_ALTERNATIVE);
|
|
|
|
option->group = g_strdup ("GtkPrintDialogExtension");
|
|
|
|
|
|
|
|
gtk_printer_option_choices_from_array (option, n_formats,
|
|
|
|
(char **) supported_formats,
|
|
|
|
display_format_names);
|
|
|
|
gtk_printer_option_set (option, supported_formats[current_format]);
|
|
|
|
gtk_printer_option_set_add (set, option);
|
2007-08-09 15:05:23 +00:00
|
|
|
|
|
|
|
g_signal_connect (option, "changed",
|
|
|
|
G_CALLBACK (file_printer_output_file_format_changed),
|
|
|
|
set);
|
|
|
|
|
2006-06-21 19:06:58 +00:00
|
|
|
g_object_unref (option);
|
|
|
|
}
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-06-20 18:42:46 +00:00
|
|
|
file_printer_get_settings_from_options (GtkPrinter *printer,
|
|
|
|
GtkPrinterOptionSet *options,
|
|
|
|
GtkPrintSettings *settings)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
|
|
|
GtkPrinterOption *option;
|
|
|
|
|
|
|
|
option = gtk_printer_option_set_lookup (options, "gtk-main-page-custom-input");
|
2006-06-20 17:36:55 +00:00
|
|
|
gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_URI, option->value);
|
2006-06-21 19:06:58 +00:00
|
|
|
|
|
|
|
option = gtk_printer_option_set_lookup (options, "output-file-format");
|
2006-07-19 15:52:48 +00:00
|
|
|
if (option)
|
|
|
|
gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, option->value);
|
2007-05-28 10:41:09 +00:00
|
|
|
|
|
|
|
option = gtk_printer_option_set_lookup (options, "gtk-n-up");
|
|
|
|
if (option)
|
|
|
|
gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_NUMBER_UP, option->value);
|
2009-05-13 16:28:42 +00:00
|
|
|
|
|
|
|
option = gtk_printer_option_set_lookup (options, "gtk-n-up-layout");
|
|
|
|
if (option)
|
|
|
|
gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, option->value);
|
2006-04-21 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-06-20 18:42:46 +00:00
|
|
|
file_printer_prepare_for_print (GtkPrinter *printer,
|
|
|
|
GtkPrintJob *print_job,
|
|
|
|
GtkPrintSettings *settings,
|
|
|
|
GtkPageSetup *page_setup)
|
2006-04-21 15:09:32 +00:00
|
|
|
{
|
2006-06-20 18:42:46 +00:00
|
|
|
gdouble scale;
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
print_job->print_pages = gtk_print_settings_get_print_pages (settings);
|
|
|
|
print_job->page_ranges = NULL;
|
|
|
|
print_job->num_page_ranges = 0;
|
|
|
|
|
|
|
|
if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
|
|
|
|
print_job->page_ranges =
|
|
|
|
gtk_print_settings_get_page_ranges (settings,
|
|
|
|
&print_job->num_page_ranges);
|
|
|
|
|
|
|
|
print_job->collate = gtk_print_settings_get_collate (settings);
|
|
|
|
print_job->reverse = gtk_print_settings_get_reverse (settings);
|
2006-05-12 09:47:58 +00:00
|
|
|
print_job->num_copies = gtk_print_settings_get_n_copies (settings);
|
2009-05-13 16:28:42 +00:00
|
|
|
print_job->number_up = gtk_print_settings_get_number_up (settings);
|
|
|
|
print_job->number_up_layout = gtk_print_settings_get_number_up_layout (settings);
|
2006-04-21 15:09:32 +00:00
|
|
|
|
|
|
|
scale = gtk_print_settings_get_scale (settings);
|
|
|
|
if (scale != 100.0)
|
|
|
|
print_job->scale = scale/100.0;
|
|
|
|
|
|
|
|
print_job->page_set = gtk_print_settings_get_page_set (settings);
|
|
|
|
print_job->rotate_to_orientation = TRUE;
|
|
|
|
}
|
2007-04-30 06:03:01 +00:00
|
|
|
|
|
|
|
static GList *
|
|
|
|
file_printer_list_papers (GtkPrinter *printer)
|
|
|
|
{
|
|
|
|
GList *result = NULL;
|
|
|
|
GList *papers, *p;
|
|
|
|
GtkPageSetup *page_setup;
|
|
|
|
|
|
|
|
papers = gtk_paper_size_get_paper_sizes (TRUE);
|
|
|
|
|
|
|
|
for (p = papers; p; p = p->next)
|
|
|
|
{
|
|
|
|
GtkPaperSize *paper_size = p->data;
|
|
|
|
|
|
|
|
page_setup = gtk_page_setup_new ();
|
|
|
|
gtk_page_setup_set_paper_size (page_setup, paper_size);
|
|
|
|
gtk_paper_size_free (paper_size);
|
|
|
|
result = g_list_prepend (result, page_setup);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_free (papers);
|
|
|
|
|
|
|
|
return g_list_reverse (result);
|
|
|
|
}
|
2008-05-21 02:17:30 +00:00
|
|
|
|
|
|
|
static GtkPageSetup *
|
|
|
|
file_printer_get_default_page_size (GtkPrinter *printer)
|
|
|
|
{
|
|
|
|
GtkPageSetup *result = NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|