1 /* GTK - The GIMP Toolkit
2 * gtkprintbackendfile.c: Default implementation of GtkPrintBackend
3 * for printing to a file
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
25 #include <sys/types.h>
33 #include <cairo-pdf.h>
36 #include <glib/gi18n-lib.h>
39 #include "gtk/gtkprinter-private.h"
41 #include "gtkprintbackendfile.h"
43 typedef struct _GtkPrintBackendFileClass GtkPrintBackendFileClass;
45 #define GTK_PRINT_BACKEND_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_FILE, GtkPrintBackendFileClass))
46 #define GTK_IS_PRINT_BACKEND_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_FILE))
47 #define GTK_PRINT_BACKEND_FILE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_FILE, GtkPrintBackendFileClass))
49 #define _STREAM_MAX_CHUNK_SIZE 8192
51 static GType print_backend_file_type = 0;
53 struct _GtkPrintBackendFileClass
55 GtkPrintBackendClass parent_class;
58 struct _GtkPrintBackendFile
60 GtkPrintBackend parent_instance;
70 static const gchar* formats[N_FORMATS] =
76 static GObjectClass *backend_parent_class;
78 static void gtk_print_backend_file_class_init (GtkPrintBackendFileClass *class);
79 static void gtk_print_backend_file_init (GtkPrintBackendFile *impl);
80 static void file_printer_get_settings_from_options (GtkPrinter *printer,
81 GtkPrinterOptionSet *options,
82 GtkPrintSettings *settings);
83 static GtkPrinterOptionSet *file_printer_get_options (GtkPrinter *printer,
84 GtkPrintSettings *settings,
85 GtkPageSetup *page_setup,
86 GtkPrintCapabilities capabilities);
87 static void file_printer_prepare_for_print (GtkPrinter *printer,
88 GtkPrintJob *print_job,
89 GtkPrintSettings *settings,
90 GtkPageSetup *page_setup);
91 static void gtk_print_backend_file_print_stream (GtkPrintBackend *print_backend,
94 GtkPrintJobCompleteFunc callback,
96 GDestroyNotify dnotify);
97 static cairo_surface_t * file_printer_create_cairo_surface (GtkPrinter *printer,
98 GtkPrintSettings *settings,
101 GIOChannel *cache_io);
103 static GList * file_printer_list_papers (GtkPrinter *printer);
104 static GtkPageSetup * file_printer_get_default_page_size (GtkPrinter *printer);
107 gtk_print_backend_file_register_type (GTypeModule *module)
109 static const GTypeInfo print_backend_file_info =
111 sizeof (GtkPrintBackendFileClass),
112 NULL, /* base_init */
113 NULL, /* base_finalize */
114 (GClassInitFunc) gtk_print_backend_file_class_init,
115 NULL, /* class_finalize */
116 NULL, /* class_data */
117 sizeof (GtkPrintBackendFile),
119 (GInstanceInitFunc) gtk_print_backend_file_init,
122 print_backend_file_type = g_type_module_register_type (module,
123 GTK_TYPE_PRINT_BACKEND,
124 "GtkPrintBackendFile",
125 &print_backend_file_info, 0);
129 pb_module_init (GTypeModule *module)
131 gtk_print_backend_file_register_type (module);
135 pb_module_exit (void)
140 G_MODULE_EXPORT GtkPrintBackend *
141 pb_module_create (void)
143 return gtk_print_backend_file_new ();
147 * GtkPrintBackendFile
150 gtk_print_backend_file_get_type (void)
152 return print_backend_file_type;
156 * gtk_print_backend_file_new:
158 * Creates a new #GtkPrintBackendFile object. #GtkPrintBackendFile
159 * implements the #GtkPrintBackend interface with direct access to
160 * the filesystem using Unix/Linux API calls
162 * Return value: the new #GtkPrintBackendFile object
165 gtk_print_backend_file_new (void)
167 return g_object_new (GTK_TYPE_PRINT_BACKEND_FILE, NULL);
171 gtk_print_backend_file_class_init (GtkPrintBackendFileClass *class)
173 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
175 backend_parent_class = g_type_class_peek_parent (class);
177 backend_class->print_stream = gtk_print_backend_file_print_stream;
178 backend_class->printer_create_cairo_surface = file_printer_create_cairo_surface;
179 backend_class->printer_get_options = file_printer_get_options;
180 backend_class->printer_get_settings_from_options = file_printer_get_settings_from_options;
181 backend_class->printer_prepare_for_print = file_printer_prepare_for_print;
182 backend_class->printer_list_papers = file_printer_list_papers;
183 backend_class->printer_get_default_page_size = file_printer_get_default_page_size;
186 /* return N_FORMATS if no explicit format in the settings */
188 format_from_settings (GtkPrintSettings *settings)
193 if (settings == NULL)
196 value = gtk_print_settings_get (settings,
197 GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
201 for (i = 0; i < N_FORMATS; ++i)
202 if (strcmp (value, formats[i]) == 0)
205 g_assert (i < N_FORMATS);
207 return (OutputFormat) i;
211 output_file_from_settings (GtkPrintSettings *settings,
212 const gchar *default_format)
217 uri = g_strdup (gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_URI));
221 const gchar *extension;
222 gchar *name, *locale_name, *path;
225 extension = default_format;
230 format = format_from_settings (settings);
231 extension = format == FORMAT_PS ? "ps" : "pdf";
234 /* default filename used for print-to-file */
235 name = g_strdup_printf (_("output.%s"), extension);
236 locale_name = g_filename_from_utf8 (name, -1, NULL, NULL, NULL);
239 if (locale_name != NULL)
241 gchar *current_dir = g_get_current_dir ();
242 path = g_build_filename (current_dir, locale_name, NULL);
243 g_free (locale_name);
245 uri = g_filename_to_uri (path, NULL, NULL);
247 g_free (current_dir);
254 static cairo_status_t
255 _cairo_write (void *closure,
256 const unsigned char *data,
259 GIOChannel *io = (GIOChannel *)closure;
266 g_print ("FILE Backend: Writting %i byte chunk to temp file\n", length));
270 g_io_channel_write_chars (io, (const gchar *) data, length, &written, &error);
275 g_print ("FILE Backend: Error writting to temp file, %s\n", error->message));
277 g_error_free (error);
278 return CAIRO_STATUS_WRITE_ERROR;
282 g_print ("FILE Backend: Wrote %i bytes to temp file\n", written));
288 return CAIRO_STATUS_SUCCESS;
292 static cairo_surface_t *
293 file_printer_create_cairo_surface (GtkPrinter *printer,
294 GtkPrintSettings *settings,
297 GIOChannel *cache_io)
299 cairo_surface_t *surface;
302 format = format_from_settings (settings);
304 if (format == FORMAT_PS)
305 surface = cairo_ps_surface_create_for_stream (_cairo_write, cache_io, width, height);
307 surface = cairo_pdf_surface_create_for_stream (_cairo_write, cache_io, width, height);
309 if (gtk_print_settings_get_printer_lpi (settings) == 0.0)
310 gtk_print_settings_set_printer_lpi (settings, 150.0);
312 cairo_surface_set_fallback_resolution (surface,
313 2.0 * gtk_print_settings_get_printer_lpi (settings),
314 2.0 * gtk_print_settings_get_printer_lpi (settings));
320 GtkPrintBackend *backend;
321 GtkPrintJobCompleteFunc callback;
323 GFileOutputStream *target_io_stream;
325 GDestroyNotify dnotify;
329 file_print_cb (GtkPrintBackendFile *print_backend,
333 _PrintStreamData *ps = (_PrintStreamData *) user_data;
335 GDK_THREADS_ENTER ();
337 if (ps->target_io_stream != NULL)
338 g_output_stream_close (G_OUTPUT_STREAM (ps->target_io_stream), NULL, NULL);
341 ps->callback (ps->job, ps->user_data, error);
344 ps->dnotify (ps->user_data);
346 gtk_print_job_set_status (ps->job,
347 (error != NULL)?GTK_PRINT_STATUS_FINISHED_ABORTED:GTK_PRINT_STATUS_FINISHED);
350 g_object_unref (ps->job);
354 GDK_THREADS_LEAVE ();
358 file_write (GIOChannel *source,
362 gchar buf[_STREAM_MAX_CHUNK_SIZE];
365 GIOStatus read_status;
366 _PrintStreamData *ps = (_PrintStreamData *) user_data;
371 g_io_channel_read_chars (source,
373 _STREAM_MAX_CHUNK_SIZE,
377 if (read_status != G_IO_STATUS_ERROR)
381 g_output_stream_write_all (G_OUTPUT_STREAM (ps->target_io_stream),
389 if (error != NULL || read_status == G_IO_STATUS_EOF)
391 file_print_cb (GTK_PRINT_BACKEND_FILE (ps->backend), error, user_data);
396 g_print ("FILE Backend: %s\n", error->message));
398 g_error_free (error);
405 g_print ("FILE Backend: Writting %i byte chunk to target file\n", bytes_read));
411 gtk_print_backend_file_print_stream (GtkPrintBackend *print_backend,
414 GtkPrintJobCompleteFunc callback,
416 GDestroyNotify dnotify)
418 GError *internal_error = NULL;
420 _PrintStreamData *ps;
421 GtkPrintSettings *settings;
425 printer = gtk_print_job_get_printer (job);
426 settings = gtk_print_job_get_settings (job);
428 ps = g_new0 (_PrintStreamData, 1);
429 ps->callback = callback;
430 ps->user_data = user_data;
431 ps->dnotify = dnotify;
432 ps->job = g_object_ref (job);
433 ps->backend = print_backend;
435 internal_error = NULL;
436 uri = output_file_from_settings (settings, NULL);
441 file = g_file_new_for_uri (uri);
442 ps->target_io_stream = g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &internal_error);
444 g_object_unref (file);
448 if (internal_error != NULL)
450 file_print_cb (GTK_PRINT_BACKEND_FILE (print_backend),
453 g_error_free (internal_error);
457 g_io_add_watch (data_io,
458 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
459 (GIOFunc) file_write,
464 gtk_print_backend_file_init (GtkPrintBackendFile *backend)
468 printer = g_object_new (GTK_TYPE_PRINTER,
469 "name", _("Print to File"),
474 gtk_printer_set_has_details (printer, TRUE);
475 gtk_printer_set_icon_name (printer, "gtk-floppy");
476 gtk_printer_set_is_active (printer, TRUE);
478 gtk_print_backend_add_printer (GTK_PRINT_BACKEND (backend), printer);
479 g_object_unref (printer);
481 gtk_print_backend_set_list_done (GTK_PRINT_BACKEND (backend));
485 file_printer_output_file_format_changed (GtkPrinterOption *format_option,
486 GtkPrinterOptionSet *set)
488 GtkPrinterOption *uri_option;
491 if (! format_option->value)
494 uri_option = gtk_printer_option_set_lookup (set,
495 "gtk-main-page-custom-input");
497 if (uri_option && uri_option->value)
499 const gchar *uri = uri_option->value;
500 const gchar *dot = strrchr (uri, '.');
506 /* check if the file extension matches one of the known ones */
507 for (i = 0; i < N_FORMATS; i++)
508 if (strcmp (dot + 1, formats[i]) == 0)
511 if (i < N_FORMATS && strcmp (formats[i], format_option->value))
513 /* the file extension is known but doesn't match the
514 * selected one, strip it away
516 base = g_strndup (uri, dot - uri);
521 /* there's no file extension */
522 base = g_strdup (uri);
528 gchar *tmp = g_strdup_printf ("%s.%s", base, format_option->value);
530 gtk_printer_option_set (uri_option, tmp);
536 static GtkPrinterOptionSet *
537 file_printer_get_options (GtkPrinter *printer,
538 GtkPrintSettings *settings,
539 GtkPageSetup *page_setup,
540 GtkPrintCapabilities capabilities)
542 GtkPrinterOptionSet *set;
543 GtkPrinterOption *option;
544 const gchar *n_up[] = {"1", "2", "4", "6", "9", "16" };
545 const gchar *pages_per_sheet = NULL;
546 const gchar *format_names[N_FORMATS] = { N_("PDF"), N_("Postscript") };
547 const gchar *supported_formats[N_FORMATS];
548 gchar *display_format_names[N_FORMATS];
552 gint current_format = 0;
554 format = format_from_settings (settings);
556 set = gtk_printer_option_set_new ();
558 option = gtk_printer_option_new ("gtk-n-up", _("Pages per _sheet:"), GTK_PRINTER_OPTION_TYPE_PICKONE);
559 gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
560 (char **) n_up, (char **) n_up /* FIXME i18n (localised digits)! */);
562 pages_per_sheet = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_NUMBER_UP);
564 gtk_printer_option_set (option, pages_per_sheet);
566 gtk_printer_option_set (option, "1");
567 gtk_printer_option_set_add (set, option);
568 g_object_unref (option);
570 if (capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_GENERATE_PS))
572 if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF)
574 if (format == FORMAT_PDF || format == N_FORMATS)
577 current_format = n_formats;
579 supported_formats[n_formats] = formats[FORMAT_PDF];
580 display_format_names[n_formats] = _(format_names[FORMAT_PDF]);
583 if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS)
585 if (format == FORMAT_PS || format == N_FORMATS)
586 current_format = n_formats;
587 supported_formats[n_formats] = formats[FORMAT_PS];
588 display_format_names[n_formats] = _(format_names[FORMAT_PS]);
594 current_format = format == FORMAT_PS ? FORMAT_PS : FORMAT_PDF;
595 for (n_formats = 0; n_formats < N_FORMATS; ++n_formats)
597 supported_formats[n_formats] = formats[n_formats];
598 display_format_names[n_formats] = _(format_names[n_formats]);
602 uri = output_file_from_settings (settings, supported_formats[current_format]);
604 option = gtk_printer_option_new ("gtk-main-page-custom-input", _("File"),
605 GTK_PRINTER_OPTION_TYPE_FILESAVE);
606 gtk_printer_option_set (option, uri);
608 option->group = g_strdup ("GtkPrintDialogExtension");
609 gtk_printer_option_set_add (set, option);
613 option = gtk_printer_option_new ("output-file-format", _("_Output format"),
614 GTK_PRINTER_OPTION_TYPE_ALTERNATIVE);
615 option->group = g_strdup ("GtkPrintDialogExtension");
617 gtk_printer_option_choices_from_array (option, n_formats,
618 (char **) supported_formats,
619 display_format_names);
620 gtk_printer_option_set (option, supported_formats[current_format]);
621 gtk_printer_option_set_add (set, option);
623 g_signal_connect (option, "changed",
624 G_CALLBACK (file_printer_output_file_format_changed),
627 g_object_unref (option);
634 file_printer_get_settings_from_options (GtkPrinter *printer,
635 GtkPrinterOptionSet *options,
636 GtkPrintSettings *settings)
638 GtkPrinterOption *option;
640 option = gtk_printer_option_set_lookup (options, "gtk-main-page-custom-input");
641 gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_URI, option->value);
643 option = gtk_printer_option_set_lookup (options, "output-file-format");
645 gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, option->value);
647 option = gtk_printer_option_set_lookup (options, "gtk-n-up");
649 gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_NUMBER_UP, option->value);
651 option = gtk_printer_option_set_lookup (options, "gtk-n-up-layout");
653 gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, option->value);
657 file_printer_prepare_for_print (GtkPrinter *printer,
658 GtkPrintJob *print_job,
659 GtkPrintSettings *settings,
660 GtkPageSetup *page_setup)
664 print_job->print_pages = gtk_print_settings_get_print_pages (settings);
665 print_job->page_ranges = NULL;
666 print_job->num_page_ranges = 0;
668 if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
669 print_job->page_ranges =
670 gtk_print_settings_get_page_ranges (settings,
671 &print_job->num_page_ranges);
673 print_job->collate = gtk_print_settings_get_collate (settings);
674 print_job->reverse = gtk_print_settings_get_reverse (settings);
675 print_job->num_copies = gtk_print_settings_get_n_copies (settings);
676 print_job->number_up = gtk_print_settings_get_number_up (settings);
677 print_job->number_up_layout = gtk_print_settings_get_number_up_layout (settings);
679 scale = gtk_print_settings_get_scale (settings);
681 print_job->scale = scale/100.0;
683 print_job->page_set = gtk_print_settings_get_page_set (settings);
684 print_job->rotate_to_orientation = TRUE;
688 file_printer_list_papers (GtkPrinter *printer)
690 GList *result = NULL;
692 GtkPageSetup *page_setup;
694 papers = gtk_paper_size_get_paper_sizes (TRUE);
696 for (p = papers; p; p = p->next)
698 GtkPaperSize *paper_size = p->data;
700 page_setup = gtk_page_setup_new ();
701 gtk_page_setup_set_paper_size (page_setup, paper_size);
702 gtk_paper_size_free (paper_size);
703 result = g_list_prepend (result, page_setup);
706 g_list_free (papers);
708 return g_list_reverse (result);
711 static GtkPageSetup *
712 file_printer_get_default_page_size (GtkPrinter *printer)
714 GtkPageSetup *result = NULL;