]> Pileus Git - ~andy/gtk/blob - modules/printbackends/file/gtkprintbackendfile.c
Rename gtk_paper_size_get_builtins() to gtk_paper_size_get_paper_sizes()
[~andy/gtk] / modules / printbackends / file / gtkprintbackendfile.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintbackendpdf.c: Default implementation of GtkPrintBackend 
3  * for printing to a file
4  * Copyright (C) 2003, Red Hat, Inc.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <config.h>
23
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <errno.h>
32 #include <cairo.h>
33 #include <cairo-pdf.h>
34 #include <cairo-ps.h>
35
36 #include <glib/gi18n-lib.h>
37
38 #include "gtkprintoperation.h"
39
40 #include "gtkprintbackend.h"
41 #include "gtkprintbackendfile.h"
42
43 #include "gtkprinter.h"
44 #include "gtkprinter-private.h"
45
46 typedef struct _GtkPrintBackendFileClass GtkPrintBackendFileClass;
47
48 #define GTK_PRINT_BACKEND_FILE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_FILE, GtkPrintBackendFileClass))
49 #define GTK_IS_PRINT_BACKEND_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_FILE))
50 #define GTK_PRINT_BACKEND_FILE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_FILE, GtkPrintBackendFileClass))
51
52 #define _STREAM_MAX_CHUNK_SIZE 8192
53
54 static GType print_backend_file_type = 0;
55
56 struct _GtkPrintBackendFileClass
57 {
58   GtkPrintBackendClass parent_class;
59 };
60
61 struct _GtkPrintBackendFile
62 {
63   GtkPrintBackend parent_instance;
64 };
65
66 typedef enum
67 {
68   FORMAT_PDF,
69   FORMAT_PS,
70   N_FORMATS
71 } OutputFormat;
72
73 static const gchar* formats[N_FORMATS] =
74 {
75   "pdf",
76   "ps"
77 };
78
79 static GObjectClass *backend_parent_class;
80
81 static void                 gtk_print_backend_file_class_init      (GtkPrintBackendFileClass *class);
82 static void                 gtk_print_backend_file_init            (GtkPrintBackendFile      *impl);
83 static void                 file_printer_get_settings_from_options (GtkPrinter              *printer,
84                                                                     GtkPrinterOptionSet     *options,
85                                                                     GtkPrintSettings        *settings);
86 static GtkPrinterOptionSet *file_printer_get_options               (GtkPrinter              *printer,
87                                                                     GtkPrintSettings        *settings,
88                                                                     GtkPageSetup            *page_setup,
89                                                                     GtkPrintCapabilities     capabilities);
90 static void                 file_printer_prepare_for_print         (GtkPrinter              *printer,
91                                                                     GtkPrintJob             *print_job,
92                                                                     GtkPrintSettings        *settings,
93                                                                     GtkPageSetup            *page_setup);
94 static void                 gtk_print_backend_file_print_stream    (GtkPrintBackend         *print_backend,
95                                                                     GtkPrintJob             *job,
96                                                                     GIOChannel              *data_io,
97                                                                     GtkPrintJobCompleteFunc  callback,
98                                                                     gpointer                 user_data,
99                                                                     GDestroyNotify           dnotify);
100 static cairo_surface_t *    file_printer_create_cairo_surface      (GtkPrinter              *printer,
101                                                                     GtkPrintSettings        *settings,
102                                                                     gdouble                  width,
103                                                                     gdouble                  height,
104                                                                     GIOChannel              *cache_io);
105
106 static GList *              file_printer_list_papers               (GtkPrinter              *printer);
107
108 static void
109 gtk_print_backend_file_register_type (GTypeModule *module)
110 {
111   static const GTypeInfo print_backend_file_info =
112   {
113     sizeof (GtkPrintBackendFileClass),
114     NULL,               /* base_init */
115     NULL,               /* base_finalize */
116     (GClassInitFunc) gtk_print_backend_file_class_init,
117     NULL,               /* class_finalize */
118     NULL,               /* class_data */
119     sizeof (GtkPrintBackendFile),
120     0,          /* n_preallocs */
121     (GInstanceInitFunc) gtk_print_backend_file_init,
122   };
123
124   print_backend_file_type = g_type_module_register_type (module,
125                                                          GTK_TYPE_PRINT_BACKEND,
126                                                          "GtkPrintBackendFile",
127                                                          &print_backend_file_info, 0);
128 }
129
130 G_MODULE_EXPORT void 
131 pb_module_init (GTypeModule *module)
132 {
133   gtk_print_backend_file_register_type (module);
134 }
135
136 G_MODULE_EXPORT void 
137 pb_module_exit (void)
138 {
139
140 }
141   
142 G_MODULE_EXPORT GtkPrintBackend * 
143 pb_module_create (void)
144 {
145   return gtk_print_backend_file_new ();
146 }
147
148 /*
149  * GtkPrintBackendFile
150  */
151 GType
152 gtk_print_backend_file_get_type (void)
153 {
154   return print_backend_file_type;
155 }
156
157 /**
158  * gtk_print_backend_file_new:
159  *
160  * Creates a new #GtkPrintBackendFile object. #GtkPrintBackendFile
161  * implements the #GtkPrintBackend interface with direct access to
162  * the filesystem using Unix/Linux API calls
163  *
164  * Return value: the new #GtkPrintBackendFile object
165  **/
166 GtkPrintBackend *
167 gtk_print_backend_file_new (void)
168 {
169   return g_object_new (GTK_TYPE_PRINT_BACKEND_FILE, NULL);
170 }
171
172 static void
173 gtk_print_backend_file_class_init (GtkPrintBackendFileClass *class)
174 {
175   GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
176
177   backend_parent_class = g_type_class_peek_parent (class);
178
179   backend_class->print_stream = gtk_print_backend_file_print_stream;
180   backend_class->printer_create_cairo_surface = file_printer_create_cairo_surface;
181   backend_class->printer_get_options = file_printer_get_options;
182   backend_class->printer_get_settings_from_options = file_printer_get_settings_from_options;
183   backend_class->printer_prepare_for_print = file_printer_prepare_for_print;
184   backend_class->printer_list_papers = file_printer_list_papers;
185 }
186
187 /* return N_FORMATS if no explicit format in the settings */
188 static OutputFormat
189 format_from_settings (GtkPrintSettings *settings)
190 {
191   const gchar *value;
192   gint i;
193
194   if (settings == NULL)
195     return N_FORMATS;
196
197   value = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
198   if (value == NULL)
199     return N_FORMATS;
200
201   for (i = 0; i < N_FORMATS; ++i)
202     if (strcmp (value, formats[i]) == 0)
203       break;
204
205   g_assert (i < N_FORMATS);
206
207   return (OutputFormat) i;
208 }
209
210 static gchar *
211 output_file_from_settings (GtkPrintSettings *settings,
212                            const gchar      *default_format)
213 {
214   gchar *uri = NULL;
215   
216   if (settings)
217     uri = g_strdup (gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_URI));
218
219   if (uri == NULL)
220     { 
221       const gchar *extension;
222       gchar *name, *locale_name, *path;
223
224       if (default_format)
225         extension = default_format;
226       else
227         {
228           OutputFormat format;
229
230           format = format_from_settings (settings);
231           extension = format == FORMAT_PS ? "ps" : "pdf";
232         }
233  
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);
237       g_free (name);
238
239       if (locale_name != NULL)
240         {
241           path = g_build_filename (g_get_current_dir (), locale_name, NULL);
242           g_free (locale_name);
243
244           uri = g_filename_to_uri (path, NULL, NULL);
245           g_free (path);
246         }
247     }
248
249   return uri;
250 }
251
252 static cairo_status_t
253 _cairo_write (void                *closure,
254               const unsigned char *data,
255               unsigned int         length)
256 {
257   GIOChannel *io = (GIOChannel *)closure;
258   gsize written;
259   GError *error;
260
261   error = NULL;
262
263   GTK_NOTE (PRINTING,
264             g_print ("FILE Backend: Writting %i byte chunk to temp file\n", length));
265
266   while (length > 0) 
267     {
268       g_io_channel_write_chars (io, (const gchar *) data, length, &written, &error);
269
270       if (error != NULL)
271         {
272           GTK_NOTE (PRINTING,
273                      g_print ("FILE Backend: Error writting to temp file, %s\n", error->message));
274
275           g_error_free (error);
276           return CAIRO_STATUS_WRITE_ERROR;
277         }    
278
279       GTK_NOTE (PRINTING,
280                 g_print ("FILE Backend: Wrote %i bytes to temp file\n", written));
281       
282       data += written;
283       length -= written;
284     }
285
286   return CAIRO_STATUS_SUCCESS;
287 }
288
289
290 static cairo_surface_t *
291 file_printer_create_cairo_surface (GtkPrinter       *printer,
292                                    GtkPrintSettings *settings,
293                                    gdouble           width, 
294                                    gdouble           height,
295                                    GIOChannel       *cache_io)
296 {
297   cairo_surface_t *surface;
298   OutputFormat format;
299
300   format = format_from_settings (settings);
301
302   if (format == FORMAT_PS)
303     surface = cairo_ps_surface_create_for_stream (_cairo_write, cache_io, width, height);
304   else
305     surface = cairo_pdf_surface_create_for_stream (_cairo_write, cache_io, width, height);
306
307   /* TODO: DPI from settings object? */
308   cairo_surface_set_fallback_resolution (surface, 300, 300);
309
310   return surface;
311 }
312
313 typedef struct {
314   GtkPrintBackend *backend;
315   GtkPrintJobCompleteFunc callback;
316   GtkPrintJob *job;
317   GIOChannel *target_io;
318   gpointer user_data;
319   GDestroyNotify dnotify;
320 } _PrintStreamData;
321
322 static void
323 file_print_cb (GtkPrintBackendFile *print_backend,
324                GError              *error,
325                gpointer            user_data)
326 {
327   _PrintStreamData *ps = (_PrintStreamData *) user_data;
328
329   if (ps->target_io != NULL)
330     g_io_channel_unref (ps->target_io);
331
332   if (ps->callback)
333     ps->callback (ps->job, ps->user_data, error);
334
335   if (ps->dnotify)
336     ps->dnotify (ps->user_data);
337
338   gtk_print_job_set_status (ps->job,
339                             (error != NULL)?GTK_PRINT_STATUS_FINISHED_ABORTED:GTK_PRINT_STATUS_FINISHED);
340
341   if (ps->job)
342     g_object_unref (ps->job);
343  
344   g_free (ps);
345 }
346
347 static gboolean
348 file_write (GIOChannel   *source,
349             GIOCondition  con,
350             gpointer      user_data)
351 {
352   gchar buf[_STREAM_MAX_CHUNK_SIZE];
353   gsize bytes_read;
354   GError *error;
355   GIOStatus read_status;
356   _PrintStreamData *ps = (_PrintStreamData *) user_data;
357
358   error = NULL;
359
360   read_status = 
361     g_io_channel_read_chars (source,
362                              buf,
363                              _STREAM_MAX_CHUNK_SIZE,
364                              &bytes_read,
365                              &error);
366
367   if (read_status != G_IO_STATUS_ERROR)
368     {
369       gsize bytes_written;
370
371       g_io_channel_write_chars (ps->target_io, 
372                                 buf, 
373                                 bytes_read, 
374                                 &bytes_written, 
375                                 &error);
376     }
377
378   if (error != NULL || read_status == G_IO_STATUS_EOF)
379     {
380       file_print_cb (GTK_PRINT_BACKEND_FILE (ps->backend), error, user_data);
381
382       if (error != NULL)
383         {
384           GTK_NOTE (PRINTING,
385                     g_print ("FILE Backend: %s\n", error->message));
386
387           g_error_free (error);
388         }
389
390       return FALSE;
391     }
392
393   GTK_NOTE (PRINTING,
394             g_print ("FILE Backend: Writting %i byte chunk to target file\n", bytes_read));
395
396   return TRUE;
397 }
398
399 static void
400 gtk_print_backend_file_print_stream (GtkPrintBackend        *print_backend,
401                                      GtkPrintJob            *job,
402                                      GIOChannel             *data_io,
403                                      GtkPrintJobCompleteFunc callback,
404                                      gpointer                user_data,
405                                      GDestroyNotify          dnotify)
406 {
407   GError *internal_error = NULL;
408   GtkPrinter *printer;
409   _PrintStreamData *ps;
410   GtkPrintSettings *settings;
411   gchar *uri, *filename;
412
413   printer = gtk_print_job_get_printer (job);
414   settings = gtk_print_job_get_settings (job);
415
416   ps = g_new0 (_PrintStreamData, 1);
417   ps->callback = callback;
418   ps->user_data = user_data;
419   ps->dnotify = dnotify;
420   ps->job = g_object_ref (job);
421   ps->backend = print_backend;
422
423   internal_error = NULL;
424   uri = output_file_from_settings (settings, NULL);
425   filename = g_filename_from_uri (uri, NULL, &internal_error);
426   g_free (uri);
427
428   if (filename == NULL)
429     goto error;
430
431   ps->target_io = g_io_channel_new_file (filename, "w", &internal_error);
432
433   g_free (filename);
434
435   if (internal_error == NULL)
436     g_io_channel_set_encoding (ps->target_io, NULL, &internal_error);
437
438 error:
439   if (internal_error != NULL)
440     {
441       file_print_cb (GTK_PRINT_BACKEND_FILE (print_backend),
442                     internal_error, ps);
443
444       g_error_free (internal_error);
445       return;
446     }
447
448   g_io_add_watch (data_io, 
449                   G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
450                   (GIOFunc) file_write,
451                   ps);
452 }
453
454 static void
455 gtk_print_backend_file_init (GtkPrintBackendFile *backend)
456 {
457   GtkPrinter *printer;
458   
459   printer = g_object_new (GTK_TYPE_PRINTER,
460                           "name", _("Print to File"),
461                           "backend", backend,
462                           "is-virtual", TRUE,
463                           NULL); 
464
465   gtk_printer_set_has_details (printer, TRUE);
466   gtk_printer_set_icon_name (printer, "gtk-floppy");
467   gtk_printer_set_is_active (printer, TRUE);
468
469   gtk_print_backend_add_printer (GTK_PRINT_BACKEND (backend), printer);
470   g_object_unref (printer);
471
472   gtk_print_backend_set_list_done (GTK_PRINT_BACKEND (backend));
473 }
474
475 static GtkPrinterOptionSet *
476 file_printer_get_options (GtkPrinter           *printer,
477                           GtkPrintSettings     *settings,
478                           GtkPageSetup         *page_setup,
479                           GtkPrintCapabilities  capabilities)
480 {
481   GtkPrinterOptionSet *set;
482   GtkPrinterOption *option;
483   const gchar *n_up[] = { "1" };
484   const gchar *format_names[N_FORMATS] = { N_("PDF"), N_("Postscript") };
485   const gchar *supported_formats[N_FORMATS];
486   gchar *display_format_names[N_FORMATS];
487   gint n_formats = 0;
488   OutputFormat format;
489   gchar *uri;
490   gint current_format = 0;
491
492   format = format_from_settings (settings);
493
494   set = gtk_printer_option_set_new ();
495
496   option = gtk_printer_option_new ("gtk-n-up", _("Pages per _sheet:"), GTK_PRINTER_OPTION_TYPE_PICKONE);
497   gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
498                                          (char **) n_up, (char **) n_up /* FIXME i18n (localised digits)! */);
499   gtk_printer_option_set (option, "1");
500   gtk_printer_option_set_add (set, option);
501   g_object_unref (option);
502
503   if (capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF | GTK_PRINT_CAPABILITY_GENERATE_PS))
504     {
505       if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF)
506         {
507           if (format == FORMAT_PDF || format == N_FORMATS)
508             {
509               format = FORMAT_PDF;
510               current_format = n_formats;
511             }
512           supported_formats[n_formats] = formats[FORMAT_PDF];
513           display_format_names[n_formats] = _(format_names[FORMAT_PDF]);
514           n_formats++;
515         }
516       if (capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS)
517         {
518           if (format == FORMAT_PS || format == N_FORMATS)
519             current_format = n_formats;
520           supported_formats[n_formats] = formats[FORMAT_PS];
521           display_format_names[n_formats] = _(format_names[FORMAT_PS]);
522           n_formats++;
523         }
524     }
525   else
526     {
527       current_format = format == FORMAT_PS ? FORMAT_PS : FORMAT_PDF;
528       for (n_formats = 0; n_formats < N_FORMATS; ++n_formats)
529         {
530           supported_formats[n_formats] = formats[n_formats];
531           display_format_names[n_formats] = _(format_names[n_formats]);
532         }
533     }
534
535   uri = output_file_from_settings (settings, supported_formats[current_format]);
536
537   option = gtk_printer_option_new ("gtk-main-page-custom-input", _("File"), 
538                                    GTK_PRINTER_OPTION_TYPE_FILESAVE);
539   gtk_printer_option_set (option, uri);
540   g_free (uri);
541   option->group = g_strdup ("GtkPrintDialogExtension");
542   gtk_printer_option_set_add (set, option);
543
544   if (n_formats > 1)
545     {
546       option = gtk_printer_option_new ("output-file-format", _("_Output format"), 
547                                        GTK_PRINTER_OPTION_TYPE_ALTERNATIVE);
548       option->group = g_strdup ("GtkPrintDialogExtension");
549
550       gtk_printer_option_choices_from_array (option, n_formats,
551                                              (char **) supported_formats,
552                                              display_format_names);
553       gtk_printer_option_set (option, supported_formats[current_format]);
554       gtk_printer_option_set_add (set, option);
555       
556       g_object_unref (option);
557     }
558
559   return set;
560 }
561
562 static void
563 file_printer_get_settings_from_options (GtkPrinter          *printer,
564                                         GtkPrinterOptionSet *options,
565                                         GtkPrintSettings    *settings)
566 {
567   GtkPrinterOption *option;
568
569   option = gtk_printer_option_set_lookup (options, "gtk-main-page-custom-input");
570   gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_URI, option->value);
571
572   option = gtk_printer_option_set_lookup (options, "output-file-format");
573   if (option)
574     gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, option->value);
575     
576 }
577
578 static void
579 file_printer_prepare_for_print (GtkPrinter       *printer,
580                                 GtkPrintJob      *print_job,
581                                 GtkPrintSettings *settings,
582                                 GtkPageSetup     *page_setup)
583 {
584   gdouble scale;
585
586   print_job->print_pages = gtk_print_settings_get_print_pages (settings);
587   print_job->page_ranges = NULL;
588   print_job->num_page_ranges = 0;
589   
590   if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
591     print_job->page_ranges =
592       gtk_print_settings_get_page_ranges (settings,
593                                           &print_job->num_page_ranges);
594   
595   print_job->collate = gtk_print_settings_get_collate (settings);
596   print_job->reverse = gtk_print_settings_get_reverse (settings);
597   print_job->num_copies = gtk_print_settings_get_n_copies (settings);
598
599   scale = gtk_print_settings_get_scale (settings);
600   if (scale != 100.0)
601     print_job->scale = scale/100.0;
602
603   print_job->page_set = gtk_print_settings_get_page_set (settings);
604   print_job->rotate_to_orientation = TRUE;
605 }
606
607 static GList *
608 file_printer_list_papers (GtkPrinter *printer)
609 {
610   GList *result = NULL;
611   GList *papers, *p;
612   GtkPageSetup *page_setup;
613
614   papers = gtk_paper_size_get_paper_sizes (TRUE);
615
616   for (p = papers; p; p = p->next)
617     {
618       GtkPaperSize *paper_size = p->data;
619
620       page_setup = gtk_page_setup_new ();
621       gtk_page_setup_set_paper_size (page_setup, paper_size);
622       gtk_paper_size_free (paper_size);
623       result = g_list_prepend (result, page_setup);
624     }
625
626   g_list_free (papers);
627
628   return g_list_reverse (result);
629 }