Debian Codegraph @

gtk/print/backends/gtkprintbackendcpdb.c

raw symbols SCIP
1 /* GTK - The GIMP Toolkit
2 * gtkprintbackendcpdb.h: Default implementation of GtkPrintBackend
3 * for the Common Print Dialog Backends (CPDB)
4 * Copyright (C) 2022, 2023 TinyTrebuchet <[email protected]>
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, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #include <config.h>
22 #include <cpdb/frontend.h>
23
24 #include <gtk/gtkprivate.h>
25 #include <gtk/gtkmodulesprivate.h>
26 #include "gtkprintbackendcpdb.h"
27 #include "gtkprintbackendutils.h"
28 #include "gtkprintercpdb.h"
29
30 #include <cairo.h>
31 #include <cairo-pdf.h>
32
33 #include <glib/gi18n-lib.h>
34
35 #define GTK_PRINT_BACKEND_CPDB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_CPDB, GtkPrintBackendCpdbClass))
36 #define GTK_IS_PRINT_BACKEND_CPDB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_CPDB))
37 #define GTK_PRINT_BACKEND_CPDB_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_CPDB, GtkPrintBackendCpdbClass))
38
39 /*
40 * Multiplier for converting points to mm
41 */
42 #define points_multiplier 2.83464567
43
44 #define _CPDB_MAX_CHUNK_SIZE 8192
45
46
47
48 static void gtk_print_backend_cpdb_finalize (GObject *object);
49
50 static void cpdb_request_printer_list (GtkPrintBackend *backend);
51
52 static void cpdb_printer_request_details (GtkPrinter *printer);
53
54 static void cpdb_acquire_details_cb (cpdb_printer_obj_t *cpdb_printer_obj,
55 int success,
56 gpointer user_data);
57
58 static GtkPrintCapabilities cpdb_printer_get_capabilities (GtkPrinter *printer);
59
60 static GtkPrinterOptionSet *cpdb_printer_get_options (GtkPrinter *printer,
61 GtkPrintSettings *settings,
62 GtkPageSetup *page_setup,
63 GtkPrintCapabilities capabilities);
64
65 static GtkPageSetup *cpdb_get_gtk_page_setup (cpdb_printer_obj_t *printer_obj,
66 const char *media);
67 static GList *cpdb_printer_list_papers (GtkPrinter *printer);
68 static GtkPageSetup *cpdb_printer_get_default_page_size (GtkPrinter *printer);
69
70 static gboolean cpdb_printer_get_hard_margins (GtkPrinter *printer,
71 double *top,
72 double *bottom,
73 double *left,
74 double *right);
75 static gboolean cpdb_printer_get_hard_margins_for_paper_size (GtkPrinter *printer,
76 GtkPaperSize *paper_size,
77 double *top,
78 double *bottom,
79 double *left,
80 double *right);
81
82 static void cpdb_printer_get_settings_from_options (GtkPrinter *printer,
83 GtkPrinterOptionSet *options,
84 GtkPrintSettings *settings);
85
86 static void cpdb_printer_prepare_for_print (GtkPrinter *printer,
87 GtkPrintJob *print_job,
88 GtkPrintSettings *settings,
89 GtkPageSetup *page_setup);
90
91 static void cpdb_print_cb (GtkPrintBackendCpdb *cpdb_backend,
92 GError *error,
93 gpointer user_data);
94
95 static gboolean cpdb_write (GIOChannel *source,
96 GIOCondition con,
97 gpointer user_data);
98
99 static void cpdb_print_stream (GtkPrintBackend *backend,
100 GtkPrintJob *job,
101 GIOChannel *data_io,
102 GtkPrintJobCompleteFunc callback,
103 gpointer user_data,
104 GDestroyNotify dnotify);
105
106 static void gtk_printer_cpdb_configure_page_setup (GtkPrinter *printer,
107 GtkPageSetup *page_setup,
108 GtkPrintSettings *settings);
109
110 static void gtk_printer_cpdb_configure_settings (const char *key,
111 const char *value,
112 gpointer user_data);
113
114 static cairo_surface_t *cpdb_printer_create_cairo_surface (GtkPrinter *printer,
115 GtkPrintSettings *settings,
116 double width,
117 double height,
118 GIOChannel *cache_io);
119
120 static void cpdb_fill_gtk_option (GtkPrinterOption *gtk_option,
121 cpdb_option_t *cpdb_option,
122 cpdb_printer_obj_t *printer_obj);
123
124 static void printer_updates_callback (cpdb_frontend_obj_t *frontend_obj,
125 cpdb_printer_obj_t *printer_obj,
126 cpdb_printer_update_t change);
127
128 static void add_option_to_settings (GtkPrinterOption *option,
129 gpointer user_data);
130
131 static void cpdb_printer_add_hash_table (gpointer key,
132 gpointer value,
133 gpointer user_data);
134
135 static void cpdb_add_gtk_printer (GtkPrintBackend *backend,
136 cpdb_printer_obj_t *printer_obj);
137
138 static void cpdb_remove_gtk_printer (GtkPrintBackend *backend,
139 cpdb_printer_obj_t *printer_obj);
140
141 static void set_state_message (GtkPrinter *printer);
142
143 static char *cpdb_option_translation (cpdb_printer_obj_t *printer_obj,
144 const char *option_name);
145 static char *cpdb_choice_translation (cpdb_printer_obj_t *printer_obj,
146 const char *option_name,
147 const char *choice_name);
148
149 static void initialize (void);
150 static gchar *get_gtk_group (cpdb_printer_obj_t *printer_obj,
151 const char *group_name);
152
153
154 /*
155 * List of locales for text translation
156 */
157 static const gchar* const* locales = NULL;
158
159 /*
160 * Mark the options which have already been displayed in other tabs,
161 * and exclude them from the "Advanced" tab, while getting printer options
162 */
163 static GHashTable *already_used_options = NULL;
164
165
166 struct _GtkPrintBackendCpdbClass
167 {
168 GtkPrintBackendClass parent_class;
169 };
170
171 struct _GtkPrintBackendCpdb
172 {
173 GtkPrintBackend parent_instance;
174 cpdb_frontend_obj_t *frontend_obj;
175 };
176
177 typedef struct {
178 GtkPrintBackend *backend;
179 GtkPrintJobCompleteFunc callback;
180 GtkPrintJob *job;
181 char *path;
182 GIOStream *target_io_stream;
183 gpointer user_data;
184 GDestroyNotify dnotify;
185 } _PrintStreamData;
186
187 GTK_DEFINE_BUILTIN_MODULE_TYPE_WITH_CODE (GtkPrintBackendCpdb, gtk_print_backend_cpdb, GTK_TYPE_PRINT_BACKEND,
188 g_io_extension_point_implement (GTK_PRINT_BACKEND_EXTENSION_POINT_NAME,
189 g_define_type_id,
190 "cpdb",
191 20))
192
193 /*
194 * GtkPrintBackend object for currently opened print dialog
195 */
196 static GtkPrintBackend *gtk_print_backend = NULL;
197
198 /*
199 * GtkPrintBackendCpdb
200 */
201
202 /*
203 * gtk_print_backend_cpdb_new:
204 *
205 * Creates a new #GtkPrintBackendCpdb object. #GtkPrintBackendCpdb
206 * implements the #GtkPrintBackend interface with direct access to
207 * the filesystem using Unix/Linux API calls
208 *
209 * Returns: the new #GtkPrintBackendCpdb object
210 */
211 GtkPrintBackend *
212 gtk_print_backend_cpdb_new (void)
213 {
214 GTK_DEBUG (PRINTING, "CPDB Backend: Creating a new CPDB print backend object");
215
216 return g_object_new (GTK_TYPE_PRINT_BACKEND_CPDB, NULL);
217 }
218
219
220 static void
221 gtk_print_backend_cpdb_class_init (GtkPrintBackendCpdbClass *klass)
222 {
223 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
224 GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (klass);
225
226 gobject_class->finalize = gtk_print_backend_cpdb_finalize;
227
228 backend_class->request_printer_list = cpdb_request_printer_list;
229 backend_class->printer_request_details = cpdb_printer_request_details;
230 backend_class->printer_get_capabilities = cpdb_printer_get_capabilities;
231 backend_class->printer_get_options = cpdb_printer_get_options;
232 backend_class->printer_list_papers = cpdb_printer_list_papers;
233 backend_class->printer_get_default_page_size = cpdb_printer_get_default_page_size;
234 backend_class->printer_get_hard_margins = cpdb_printer_get_hard_margins;
235 backend_class->printer_get_hard_margins_for_paper_size = cpdb_printer_get_hard_margins_for_paper_size;
236 backend_class->printer_get_settings_from_options = cpdb_printer_get_settings_from_options;
237 backend_class->printer_prepare_for_print = cpdb_printer_prepare_for_print;
238 backend_class->printer_create_cairo_surface = cpdb_printer_create_cairo_surface;
239 backend_class->print_stream = cpdb_print_stream;
240 }
241
242 static void
243 gtk_print_backend_cpdb_init (GtkPrintBackendCpdb *backend_cpdb)
244 {
245 initialize ();
246
247 GTK_DEBUG (PRINTING, "Creating frontendObj for CPDB backend");
248 backend_cpdb->frontend_obj = cpdbGetNewFrontendObj ((cpdb_printer_callback) printer_updates_callback);
249 cpdbIgnoreLastSavedSettings (backend_cpdb->frontend_obj);
250 gtk_print_backend = GTK_PRINT_BACKEND (backend_cpdb);
251 }
252
253 static void
254 gtk_print_backend_cpdb_finalize (GObject *object)
255 {
256 GtkPrintBackendCpdb *backend_cpdb;
257 GObjectClass *backend_parent_class;
258
259 GTK_DEBUG (PRINTING, "Finalizing CPDB backend object");
260
261 backend_cpdb = GTK_PRINT_BACKEND_CPDB (object);
262 backend_parent_class = G_OBJECT_CLASS (gtk_print_backend_cpdb_parent_class);
263 cpdbDeleteFrontendObj (backend_cpdb->frontend_obj);
264 gtk_print_backend = NULL;
265
266 backend_parent_class->finalize (object);
267 }
268
269 /*
270 * This function is responsible for displaying the printer
271 * list obtained from CPDB backend on the print dialog.
272 */
273 static void
274 cpdb_request_printer_list (GtkPrintBackend *backend)
275 {
276 GtkPrintBackendCpdb *backend_cpdb = GTK_PRINT_BACKEND_CPDB (backend);
277
278 cpdbConnectToDBus (backend_cpdb->frontend_obj);
279 g_hash_table_foreach (backend_cpdb->frontend_obj->printer,
280 cpdb_printer_add_hash_table,
281 backend);
282 gtk_print_backend_set_list_done (backend);
283 }
284
285 /*
286 * This function is responsible for making a printer acquire all
287 * the details and supported options list, which need not be queried
288 * until the user clicks on the printer in the printer list in the dialog.
289 * The print dialog runs this function asynchronously and displays
290 * "Getting printer attributes..." as the printer status meanwhile.
291 * This function is needed as some printers (like temporary CUPS queue)
292 * can take a couple of seconds to materialize and acquire all the details,
293 * and it's important the dialog doesn't block during that time.
294 */
295 static void
296 cpdb_printer_request_details (GtkPrinter *printer)
297 {
298 GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (printer);
299 cpdb_printer_obj_t *printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
300
301 cpdbAcquireDetails (printer_obj, cpdb_acquire_details_cb, printer);
302 }
303
304 static void
305 cpdb_acquire_details_cb (cpdb_printer_obj_t *printer_obj,
306 int success,
307 gpointer user_data)
308 {
309 GtkPrinter *printer = GTK_PRINTER (user_data);
310 GtkPrintBackend *backend = gtk_printer_get_backend (printer);
311 gboolean accepting_jobs, paused, status_changed;
312
313 if (success == 0)
314 {
315 GTK_DEBUG (PRINTING, "Error acquiring printer details");
316 g_signal_emit_by_name (printer, "details-acquired", FALSE);
317 return;
318 }
319
320 accepting_jobs = cpdbIsAcceptingJobs (printer_obj);
321 paused = g_strcmp0 (cpdbGetState (printer_obj), "stopped") == 0;
322 status_changed = paused ^ gtk_printer_is_paused (printer);
323
324 gtk_printer_set_is_accepting_jobs (printer, accepting_jobs);
325 gtk_printer_set_is_paused (printer, paused);
326 set_state_message (printer);
327
328 gtk_printer_set_has_details (printer, TRUE);
329 g_signal_emit_by_name (printer, "details-acquired", TRUE);
330
331 if (status_changed)
332 g_signal_emit_by_name (backend, "printer-status-changed", printer);
333 }
334
335
336 /*
337 * This function is responsible for specifying which features the
338 * print dialog should offer for the given printer.
339 */
340 static GtkPrintCapabilities
341 cpdb_printer_get_capabilities (GtkPrinter *printer)
342 {
343 GtkPrintCapabilities capabilities = 0;
344 cpdb_option_t *cpdb_option;
345 GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (printer);
346 cpdb_printer_obj_t *printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
347
348 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_PAGE_SET);
349 if (cpdb_option != NULL && cpdb_option->num_supported >= 3)
350 capabilities |= GTK_PRINT_CAPABILITY_PAGE_SET;
351
352 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_COPIES);
353 if (cpdb_option != NULL &&
354 g_strcmp0 (cpdb_option->supported_values[0], "1") != 0 &&
355 g_strcmp0 (cpdb_option->supported_values[0], "1-1") != 0)
356 capabilities |= GTK_PRINT_CAPABILITY_COPIES;
357
358 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_COLLATE);
359 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
360 capabilities |= GTK_PRINT_CAPABILITY_COLLATE;
361
362 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_PAGE_DELIVERY);
363 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
364 capabilities |= GTK_PRINT_CAPABILITY_REVERSE;
365
366 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_PRINT_SCALING);
367 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
368 capabilities |= GTK_PRINT_CAPABILITY_SCALE;
369
370 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_NUMBER_UP);
371 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
372 capabilities |= GTK_PRINT_CAPABILITY_NUMBER_UP;
373
374 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_NUMBER_UP_LAYOUT);
375 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
376 capabilities |= GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT;
377
378 return capabilities;
379 }
380
381
382 /*
383 * This function is responsible for getting all the options
384 * that the printer supports and display them into the
385 * GUI template of GTK+ print dialog box.
386 * The backend should obtain whatever options it supports,
387 * from either its print server or PPDs.
388 */
389 static GtkPrinterOptionSet *
390 cpdb_printer_get_options (GtkPrinter *printer,
391 GtkPrintSettings *settings,
392 GtkPageSetup *page_setup,
393 GtkPrintCapabilities capabilities)
394 {
395 char *option_name;
396 char *display_name;
397 gpointer key, value;
398 GHashTableIter iter;
399 GtkPrinterCpdb *printer_cpdb;
400 cpdb_printer_obj_t *printer_obj;
401 cpdb_option_t *cpdb_option;
402 GtkPrinterOption *gtk_option;
403 GtkPrinterOptionSet *gtk_option_set;
404
405 gtk_option_set = gtk_printer_option_set_new ();
406 printer_cpdb = GTK_PRINTER_CPDB (printer);
407 printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
408
409 /** Page-Setup **/
410 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_NUMBER_UP);
411 if ((capabilities & GTK_PRINT_CAPABILITY_NUMBER_UP) && cpdb_option != NULL)
412 {
413 display_name = cpdb_option_translation (printer_obj,
414 cpdb_option->option_name);
415 gtk_option = gtk_printer_option_new ("gtk-n-up",
416 display_name,
417 GTK_PRINTER_OPTION_TYPE_PICKONE);
418 cpdb_fill_gtk_option (gtk_option, cpdb_option, printer_obj);
419 gtk_printer_option_set_add (gtk_option_set, gtk_option);
420 g_object_unref (gtk_option);
421 }
422
423 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_NUMBER_UP_LAYOUT);
424 if ((capabilities & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT) && cpdb_option != NULL)
425 {
426 display_name = cpdb_option_translation (printer_obj,
427 cpdb_option->option_name);
428 gtk_option = gtk_printer_option_new ("gtk-n-up-layout",
429 display_name,
430 GTK_PRINTER_OPTION_TYPE_PICKONE);
431 cpdb_fill_gtk_option (gtk_option, cpdb_option, printer_obj);
432 gtk_printer_option_set_add (gtk_option_set, gtk_option);
433 g_object_unref (gtk_option);
434 }
435
436 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_SIDES);
437 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
438 {
439 display_name = cpdb_option_translation (printer_obj,
440 cpdb_option->option_name);
441 gtk_option = gtk_printer_option_new ("gtk-duplex",
442 display_name,
443 GTK_PRINTER_OPTION_TYPE_PICKONE);
444 cpdb_fill_gtk_option (gtk_option, cpdb_option, printer_obj);
445 gtk_printer_option_set_add (gtk_option_set, gtk_option);
446 g_object_unref (gtk_option);
447 }
448
449 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_MEDIA_SOURCE);
450 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
451 {
452 display_name = cpdb_option_translation (printer_obj,
453 cpdb_option->option_name);
454 gtk_option = gtk_printer_option_new ("gtk-paper-source",
455 display_name,
456 GTK_PRINTER_OPTION_TYPE_PICKONE);
457 cpdb_fill_gtk_option (gtk_option, cpdb_option, printer_obj);
458 gtk_printer_option_set_add (gtk_option_set, gtk_option);
459 g_object_unref (gtk_option);
460 }
461
462 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_MEDIA_TYPE);
463 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
464 {
465 display_name = cpdb_option_translation (printer_obj,
466 cpdb_option->option_name);
467 gtk_option = gtk_printer_option_new ("gtk-paper-type",
468 display_name,
469 GTK_PRINTER_OPTION_TYPE_PICKONE);
470 cpdb_fill_gtk_option (gtk_option, cpdb_option, printer_obj);
471 gtk_printer_option_set_add (gtk_option_set, gtk_option);
472 g_object_unref (gtk_option);
473 }
474
475 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_OUTPUT_BIN);
476 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
477 {
478 display_name = cpdb_option_translation (printer_obj,
479 cpdb_option->option_name);
480 gtk_option = gtk_printer_option_new ("gtk-output-tray",
481 display_name,
482 GTK_PRINTER_OPTION_TYPE_PICKONE);
483 cpdb_fill_gtk_option (gtk_option, cpdb_option, printer_obj);
484 gtk_printer_option_set_add (gtk_option_set, gtk_option);
485 g_object_unref (gtk_option);
486 }
487
488
489 /** Jobs **/
490 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_JOB_PRIORITY);
491 if (cpdb_option != NULL)
492 {
493 /* job-priority is represented as a number from 1-100 */
494 const char *prio[] = {"100", "80", "50", "30"};
495 const char *prio_display[] = {
496 NC_("Print job priority", "Urgent"),
497 NC_("Print job priority", "High"),
498 NC_("Print job priority", "Medium"),
499 NC_("Print job priority", "Low")
500 };
501
502 for (int i = 0; i < G_N_ELEMENTS(prio_display); i++)
503 prio_display[i] = _(prio_display[i]);
504 display_name = cpdb_option_translation (printer_obj,
505 cpdb_option->option_name);
506 gtk_option = gtk_printer_option_new ("gtk-job-prio",
507 display_name,
508 GTK_PRINTER_OPTION_TYPE_PICKONE);
509 gtk_printer_option_choices_from_array (gtk_option,
510 G_N_ELEMENTS (prio),
511 prio, prio_display);
512 gtk_printer_option_set (gtk_option, "50");
513 gtk_printer_option_set_add (gtk_option_set, gtk_option);
514 g_object_unref (gtk_option);
515 }
516
517 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_JOB_SHEETS);
518 if (cpdb_option != NULL && cpdb_option->num_supported > 1)
519 {
520 gtk_option = gtk_printer_option_new ("gtk-cover-before",
521 C_("printer option", "Before"),
522 GTK_PRINTER_OPTION_TYPE_PICKONE);
523 cpdb_fill_gtk_option (gtk_option, cpdb_option, printer_obj);
524 gtk_printer_option_set_add (gtk_option_set, gtk_option);
525 g_object_unref (gtk_option);
526
527 gtk_option = gtk_printer_option_new ("gtk-cover-after",
528 C_("printer option", "After"),
529 GTK_PRINTER_OPTION_TYPE_PICKONE);
530
531 cpdb_fill_gtk_option (gtk_option, cpdb_option, printer_obj);
532 gtk_printer_option_set_add (gtk_option_set, gtk_option);
533 g_object_unref (gtk_option);
534 }
535
536 cpdb_option = cpdbGetOption (printer_obj, CPDB_OPTION_BILLING_INFO);
537 if (cpdb_option != NULL)
538 {
539 display_name = cpdb_option_translation (printer_obj,
540 cpdb_option->option_name);
541 gtk_option = gtk_printer_option_new ("gtk-billing-info",
542 display_name,
543 GTK_PRINTER_OPTION_TYPE_STRING);
544 gtk_printer_option_set (gtk_option, "");
545 gtk_printer_option_set_add (gtk_option_set, gtk_option);
546 g_object_unref (gtk_option);
547 }
548
549 const char *print_at[] = {"now", "at", "on-hold"};
550 gtk_option = gtk_printer_option_new ("gtk-print-time",
551 _("Print at"),
552 GTK_PRINTER_OPTION_TYPE_PICKONE);
553 gtk_printer_option_choices_from_array (gtk_option,
554 G_N_ELEMENTS (print_at),
555 print_at, print_at);
556 gtk_printer_option_set (gtk_option, "now");
557 gtk_printer_option_set_add (gtk_option_set, gtk_option);
558 g_object_unref (gtk_option);
559
560 gtk_option = gtk_printer_option_new ("gtk-print-time-text",
561 _("Print at time"),
562 GTK_PRINTER_OPTION_TYPE_STRING);
563 gtk_printer_option_set (gtk_option, "");
564 gtk_printer_option_set_add (gtk_option_set, gtk_option);
565 g_object_unref (gtk_option);
566
567 /** Other options **/
568 g_hash_table_iter_init (&iter, printer_obj->options->table);
569 while (g_hash_table_iter_next (&iter, &key, &value))
570 {
571 option_name = (char *) key;
572 if (g_hash_table_contains (already_used_options, option_name))
573 continue;
574
575 cpdb_option = (cpdb_option_t *) value;
576 if (cpdb_option->num_supported <= 1)
577 continue;
578
579 display_name = cpdb_option_translation (printer_obj,
580 cpdb_option->option_name);
581 gtk_option = gtk_printer_option_new (cpdb_option->option_name,
582 display_name,
583 GTK_PRINTER_OPTION_TYPE_PICKONE);
584 cpdb_fill_gtk_option (gtk_option, cpdb_option, printer_obj);
585 gtk_option->group = get_gtk_group (printer_obj,
586 cpdb_option->group_name);
587 gtk_printer_option_set_add (gtk_option_set, gtk_option);
588 g_object_unref (gtk_option);
589 }
590
591 /*
592 * Check if borderles printing is supported
593 */
594 gboolean borderless = TRUE;
595 const char *attrs[] = {CPDB_OPTION_MARGIN_TOP, CPDB_OPTION_MARGIN_BOTTOM,
596 CPDB_OPTION_MARGIN_LEFT, CPDB_OPTION_MARGIN_RIGHT};
597 for (int i = 0; i < 4; i++)
598 {
599 cpdb_option = cpdbGetOption (printer_obj, (gchar *) attrs[i]);
600 gboolean found = FALSE;
601
602 if (cpdb_option != NULL)
603 {
604 for (int j = 0; j < cpdb_option->num_supported; j++)
605 {
606 if (g_strcmp0 (cpdb_option->supported_values[j], "0") == 0)
607 {
608 found = TRUE;
609 break;
610 }
611 }
612 }
613
614 if (!found)
615 {
616 borderless = FALSE;
617 break;
618 }
619 }
620
621 if (borderless)
622 {
623 gtk_option = gtk_printer_option_new ("borderless",
624 C_("print option", "Borderless"),
625 GTK_PRINTER_OPTION_TYPE_BOOLEAN);
626 gtk_option->group = get_gtk_group (printer_obj, CPDB_GROUP_MEDIA);
627 gtk_printer_option_set_add (gtk_option_set, gtk_option);
628 g_object_unref (gtk_option);
629 }
630
631 return gtk_option_set;
632 }
633
634 /*
635 * Helper function to create GtkPageSetup from given "media" size
636 */
637 static GtkPageSetup *
638 cpdb_get_gtk_page_setup (cpdb_printer_obj_t *printer_obj,
639 const char *media)
640 {
641 char *display_name;
642 int width, height, num_margins, ok;
643 cpdb_margin_t *margins;
644 GtkPaperSize *paper_size;
645 GtkPageSetup *page_setup;
646
647 page_setup = gtk_page_setup_new ();
648 display_name = cpdb_choice_translation (printer_obj,
649 CPDB_OPTION_MEDIA,
650 media);
651 ok = cpdbGetMediaSize (printer_obj,
652 media,
653 &width,
654 &height);
655 if (ok == 1)
656 {
657 paper_size = gtk_paper_size_new_custom (media,
658 display_name,
659 width / 100.0,
660 height / 100.0,
661 GTK_UNIT_MM);
662 gtk_page_setup_set_paper_size (page_setup, paper_size);
663 gtk_paper_size_free (paper_size);
664 }
665 num_margins = cpdbGetMediaMargins (printer_obj,
666 media,
667 &margins);
668 if (num_margins > 0)
669 {
670 gtk_page_setup_set_left_margin (page_setup,
671 margins[0].left / 100.0,
672 GTK_UNIT_MM);
673 gtk_page_setup_set_right_margin (page_setup,
674 margins[0].right / 100.0,
675 GTK_UNIT_MM);
676 gtk_page_setup_set_top_margin (page_setup,
677 margins[0].top / 100.0,
678 GTK_UNIT_MM);
679 gtk_page_setup_set_bottom_margin (page_setup,
680 margins[0].bottom / 100.0,
681 GTK_UNIT_MM);
682 }
683 return page_setup;
684 }
685
686 /*
687 * This function is responsible for listing all the page sizes supported by a printer
688 */
689 static GList *
690 cpdb_printer_list_papers (GtkPrinter *printer)
691 {
692 cpdb_option_t *media;
693 GList *result = NULL;
694 GtkPageSetup *page_setup;
695
696 GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (printer);
697 cpdb_printer_obj_t *printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
698
699 media = cpdbGetOption (printer_obj, CPDB_OPTION_MEDIA);
700 if (media == NULL)
701 return NULL;
702
703 for (int i = 0; i < media->num_supported; i++)
704 {
705 if (g_str_has_prefix (media->supported_values[i], "custom_min") ||
706 g_str_has_prefix (media->supported_values[i], "custom_max"))
707 continue;
708
709 page_setup = cpdb_get_gtk_page_setup (printer_obj,
710 media->supported_values[i]);
711 result = g_list_prepend (result, page_setup);
712 }
713 result = g_list_reverse (result);
714 return result;
715 }
716
717 /*
718 * This function is responsible for getting the default page size for a printer
719 */
720 static GtkPageSetup *
721 cpdb_printer_get_default_page_size (GtkPrinter *printer)
722 {
723 char *default_media;
724 GtkPageSetup *page_setup = NULL;
725
726 GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (printer);
727 cpdb_printer_obj_t *printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
728
729 default_media = cpdbGetDefault (printer_obj, CPDB_OPTION_MEDIA);
730 if (default_media != NULL)
731 {
732 page_setup = cpdb_get_gtk_page_setup (printer_obj, default_media);
733 }
734
735 return page_setup;
736 }
737
738 /*
739 * This function is responsible for getting the
740 * default page size margins supported by a printer
741 */
742 static gboolean
743 cpdb_printer_get_hard_margins (GtkPrinter *printer,
744 double *top,
745 double *bottom,
746 double *left,
747 double *right)
748 {
749 char *left_margin, *right_margin, *top_margin, *bottom_margin;
750
751 GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (printer);
752 cpdb_printer_obj_t *printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
753
754 top_margin = cpdbGetDefault (printer_obj, CPDB_OPTION_MARGIN_TOP);
755 left_margin = cpdbGetDefault (printer_obj, CPDB_OPTION_MARGIN_LEFT);
756 right_margin = cpdbGetDefault (printer_obj, CPDB_OPTION_MARGIN_RIGHT);
757 bottom_margin = cpdbGetDefault (printer_obj, CPDB_OPTION_MARGIN_BOTTOM);
758
759 if (top_margin != NULL &&
760 left_margin != NULL &&
761 right_margin != NULL &&
762 bottom_margin != NULL)
763 {
764 *top = g_ascii_strtod (top_margin, NULL) * points_multiplier / 100.0;
765 *left = g_ascii_strtod (left_margin, NULL) * points_multiplier / 100.0;
766 *right = g_ascii_strtod (right_margin, NULL) * points_multiplier / 100.0;
767 *bottom = g_ascii_strtod (bottom_margin, NULL) * points_multiplier / 100.0;
768 return TRUE;
769 }
770
771 return FALSE;
772 }
773
774 /*
775 * This function is responsible for getting the
776 * default page size margins for give paper size
777 */
778 static gboolean
779 cpdb_printer_get_hard_margins_for_paper_size (GtkPrinter *printer,
780 GtkPaperSize *paper_size,
781 double *top,
782 double *bottom,
783 double *left,
784 double *right)
785 {
786 int num_margins;
787 const char *media;
788 cpdb_margin_t *margins;
789
790 GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (printer);
791 cpdb_printer_obj_t *printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
792
793 media = gtk_paper_size_get_name (paper_size);
794 if (media != NULL)
795 {
796 num_margins = cpdbGetMediaMargins (printer_obj, media, &margins);
797 if (num_margins > 0)
798 {
799 *top = margins[0].top * points_multiplier / 100.0;
800 *left = margins[0].left * points_multiplier / 100.0;
801 *right = margins[0].right * points_multiplier / 100.0;
802 *bottom = margins[0].bottom * points_multiplier / 100.0;
803
804 return TRUE;
805 }
806 }
807
808 return FALSE;
809 }
810
811 /*
812 * @user_data: GtkPrintSettings
813 */
814 static void
815 add_option_to_settings (GtkPrinterOption *option,
816 gpointer user_data)
817 {
818 GtkPrintSettings *settings = (GtkPrintSettings *) user_data;
819 gtk_print_settings_set (settings, option->name, option->value);
820 }
821
822 /*
823 * This method is invoked when the print button on the print dialog is pressed.
824 * It's responsible for gathering all the settings from the print dialog that the
825 * user may have selected before pressing the print button.
826 */
827 static void
828 cpdb_printer_get_settings_from_options (GtkPrinter *printer,
829 GtkPrinterOptionSet *options,
830 GtkPrintSettings *settings)
831 {
832 char *value;
833 GtkPrinterOption *option, *cover_before, *cover_after;
834
835 option = gtk_printer_option_set_lookup (options, "gtk-n-up");
836 if (option != NULL)
837 {
838 gtk_print_settings_set (settings,
839 CPDB_OPTION_NUMBER_UP,
840 option->value);
841 }
842
843 option = gtk_printer_option_set_lookup (options, "gtk-n-up-layout");
844 if (option != NULL)
845 {
846 gtk_print_settings_set (settings,
847 CPDB_OPTION_NUMBER_UP_LAYOUT,
848 option->value);
849 }
850
851 option = gtk_printer_option_set_lookup (options, "gtk-duplex");
852 if (option != NULL)
853 {
854 gtk_print_settings_set (settings,
855 CPDB_OPTION_SIDES,
856 option->value);
857 }
858
859 option = gtk_printer_option_set_lookup (options, "gtk-paper-source");
860 if (option != NULL)
861 {
862 gtk_print_settings_set (settings,
863 CPDB_OPTION_MEDIA_SOURCE,
864 option->value);
865 }
866
867 option = gtk_printer_option_set_lookup (options, "gtk-paper-type");
868 if (option != NULL)
869 {
870 gtk_print_settings_set (settings,
871 CPDB_OPTION_MEDIA_TYPE,
872 option->value);
873 }
874
875 option = gtk_printer_option_set_lookup (options, "gtk-output-tray");
876 if (option != NULL)
877 {
878 gtk_print_settings_set (settings,
879 CPDB_OPTION_OUTPUT_BIN,
880 option->value);
881 }
882
883 option = gtk_printer_option_set_lookup (options, "gtk-job-prio");
884 if (option != NULL)
885 {
886 gtk_print_settings_set (settings,
887 CPDB_OPTION_JOB_PRIORITY,
888 option->value);
889 }
890
891 cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
892 cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
893 if (cover_before != NULL && cover_after != NULL)
894 {
895 value = g_strdup_printf ("%s,%s",
896 cover_before->value,
897 cover_after->value);
898 gtk_print_settings_set (settings,
899 CPDB_OPTION_JOB_SHEETS,
900 value);
901 g_free (value);
902 }
903
904 option = gtk_printer_option_set_lookup (options, "gtk-billing-info");
905 if (option != NULL)
906 {
907 gtk_print_settings_set (settings,
908 CPDB_OPTION_BILLING_INFO,
909 option->value);
910 }
911
912 char *print_at = NULL;
913 option = gtk_printer_option_set_lookup (options, "gtk-print-time");
914 if (option != NULL)
915 print_at = g_strdup (option->value);
916
917 char *print_at_time = NULL;
918 option = gtk_printer_option_set_lookup (options, "gtk-print-time-text");
919 if (option != NULL)
920 print_at_time = g_strdup (option->value);
921
922 if (print_at != NULL && print_at_time != NULL)
923 {
924 if (g_strcmp0 (print_at, "at") == 0)
925 {
926 char *utc_time = NULL;
927
928 utc_time = localtime_to_utctime (print_at_time);
929 if (utc_time != NULL)
930 {
931 gtk_print_settings_set (settings,
932 CPDB_OPTION_JOB_HOLD_UNTIL,
933 utc_time);
934 g_free (utc_time);
935 }
936 else
937 gtk_print_settings_set (settings,
938 CPDB_OPTION_JOB_HOLD_UNTIL,
939 print_at_time);
940 }
941 else if (g_strcmp0 (print_at, "on-hold") == 0)
942 gtk_print_settings_set (settings,
943 CPDB_OPTION_JOB_HOLD_UNTIL,
944 CPDB_JOB_HOLD_INDEFINITE);
945 }
946 if (print_at != NULL)
947 g_free (print_at);
948 if (print_at_time != NULL)
949 g_free (print_at_time);
950
951 gtk_printer_option_set_foreach (options, add_option_to_settings, settings);
952 }
953
954 static cairo_status_t
955 _cairo_write (void *closure,
956 const unsigned char *data,
957 unsigned int length)
958 {
959 GIOChannel *io = (GIOChannel *)closure;
960 gsize written;
961 GError *error;
962
963 error = NULL;
964
965 GTK_DEBUG (PRINTING, "CPDB Backend: Writing %i byte chunk to temp file", length);
966
967 while (length > 0)
968 {
969 g_io_channel_write_chars (io, (const char *)data, length, &written, &error);
970
971 if (error != NULL)
972 {
973 GTK_DEBUG (PRINTING, "CPDB Backend: Error writing to temp file, %s", error->message);
974
975 g_error_free (error);
976 return CAIRO_STATUS_WRITE_ERROR;
977 }
978
979 GTK_DEBUG (PRINTING, "CPDB Backend: Wrote %" G_GSIZE_FORMAT " bytes to temp file\n", written);
980
981 data += written;
982 length -= written;
983 }
984
985 return CAIRO_STATUS_SUCCESS;
986 }
987
988
989 /*
990 * called after prepare_for_print ()
991 */
992 static cairo_surface_t *
993 cpdb_printer_create_cairo_surface (GtkPrinter *printer,
994 GtkPrintSettings *settings,
995 double width,
996 double height,
997 GIOChannel *cache_io)
998 {
999 cairo_surface_t *surface;
1000
1001 surface = cairo_pdf_surface_create_for_stream (_cairo_write, cache_io, width, height);
1002
1003 cairo_surface_set_fallback_resolution (surface,
1004 2.0 * gtk_print_settings_get_printer_lpi (settings),
1005 2.0 * gtk_print_settings_get_printer_lpi (settings));
1006
1007 return surface;
1008 }
1009
1010 static void
1011 gtk_printer_cpdb_configure_settings (const char *key,
1012 const char *value,
1013 gpointer user_data)
1014 {
1015 const char *option_name, *option_value;
1016 GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (user_data);
1017 cpdb_printer_obj_t *printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
1018
1019 option_name = key;
1020 option_value = value;
1021 if (g_str_has_prefix (option_name, "gtk") ||
1022 g_strcmp0 (option_value, "") == 0)
1023 {
1024 return;
1025 }
1026
1027 cpdbAddSettingToPrinter (printer_obj, option_name, option_value);
1028 }
1029
1030 static void
1031 gtk_printer_cpdb_configure_page_setup (GtkPrinter *printer,
1032 GtkPageSetup *page_setup,
1033 GtkPrintSettings *settings)
1034 {
1035 char *value, *orientation, *default_orientation;
1036 const char *borderless;
1037 double width, height, left, top, right, bottom;
1038 GtkPageOrientation page_orientation;
1039
1040 GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (printer);
1041 cpdb_printer_obj_t *printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
1042
1043 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM) * 100.0;
1044 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM) * 100.0;
1045 left = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM) * 100.0;
1046 right = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM) * 100.0;
1047 top = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM) * 100.0;
1048 bottom = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM) * 100.0;
1049
1050 borderless = gtk_print_settings_get (settings, "borderless");
1051 if (g_ascii_strcasecmp (borderless, "True") == 0)
1052 left = right = top = bottom = 0;
1053
1054 value = g_strdup_printf ("{media-size={x-dimension=%.0f y-dimension=%.0f} "
1055 "media-bottom-margin=%.0f "
1056 "media-left-margin=%.0f "
1057 "media-right-margin=%.0f "
1058 "media-top-margin=%.0f}",
1059 width, height, bottom, left, right, top);
1060 gtk_print_settings_set (settings, CPDB_OPTION_MEDIA_COL, value);
1061 g_free (value);
1062
1063 page_orientation = gtk_page_setup_get_orientation (page_setup);
1064 default_orientation = cpdbGetDefault (printer_obj, CPDB_OPTION_ORIENTATION);
1065
1066 switch (page_orientation)
1067 {
1068 case GTK_PAGE_ORIENTATION_PORTRAIT:
1069 orientation = g_strdup_printf (CPDB_ORIENTATION_PORTRAIT);
1070 break;
1071
1072 case GTK_PAGE_ORIENTATION_LANDSCAPE:
1073 orientation = g_strdup_printf (CPDB_ORIENTATION_LANDSCAPE);
1074 break;
1075
1076 case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
1077 orientation = g_strdup_printf (CPDB_ORIENTATION_RLANDSCAPE);
1078 break;
1079
1080 case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
1081 orientation = g_strdup_printf (CPDB_ORIENTATION_RPORTRAIT);
1082 break;
1083
1084 default:
1085 orientation = default_orientation;
1086 }
1087
1088 gtk_print_settings_set (settings, CPDB_OPTION_ORIENTATION, orientation);
1089 g_free (orientation);
1090 }
1091
1092 static void
1093 cpdb_printer_prepare_for_print (GtkPrinter *printer,
1094 GtkPrintJob *print_job,
1095 GtkPrintSettings *settings,
1096 GtkPageSetup *page_setup)
1097 {
1098 int n_ranges;
1099 double scale;
1100
1101 GtkPrintPages pages;
1102 GtkPageRange *ranges;
1103 GtkPageSet page_set;
1104 GtkPrintCapabilities capabilities;
1105
1106 capabilities = cpdb_printer_get_capabilities (printer);
1107
1108 pages = gtk_print_settings_get_print_pages (settings);
1109 gtk_print_job_set_pages (print_job, pages);
1110 gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_PRINT_PAGES);
1111
1112 if (pages == GTK_PRINT_PAGES_RANGES)
1113 ranges = gtk_print_settings_get_page_ranges (settings, &n_ranges);
1114 else
1115 {
1116 ranges = NULL;
1117 n_ranges = 0;
1118 }
1119 gtk_print_job_set_page_ranges (print_job, ranges, n_ranges);
1120 gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_PAGE_RANGES);
1121
1122 scale = gtk_print_settings_get_scale (settings);
1123 if (scale != 100.0)
1124 gtk_print_job_set_scale (print_job, scale / 100.0);
1125 gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_SCALE);
1126
1127 if (capabilities & GTK_PRINT_CAPABILITY_COLLATE)
1128 {
1129 if (gtk_print_settings_get_collate (settings))
1130 gtk_print_settings_set (settings,
1131 CPDB_OPTION_COLLATE,
1132 CPDB_COLLATE_ENABLED);
1133 }
1134 gtk_print_job_set_collate (print_job, FALSE);
1135 gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_COLLATE);
1136
1137 if (capabilities & GTK_PRINT_CAPABILITY_REVERSE)
1138 {
1139 if (gtk_print_settings_get_reverse (settings))
1140 gtk_print_settings_set (settings,
1141 CPDB_OPTION_PAGE_DELIVERY,
1142 CPDB_PAGE_DELIVERY_REVERSE);
1143 }
1144 gtk_print_job_set_reverse (print_job, FALSE);
1145 gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_REVERSE);
1146
1147 if (capabilities & GTK_PRINT_CAPABILITY_COPIES)
1148 {
1149 int copies = gtk_print_settings_get_n_copies (settings);
1150 if (copies > 1)
1151 {
1152 char *value = g_strdup_printf ("%d", copies);
1153 gtk_print_settings_set (settings, CPDB_OPTION_COPIES, value);
1154 g_free (value);
1155 }
1156 }
1157 gtk_print_job_set_num_copies (print_job, 1);
1158 gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_N_COPIES);
1159
1160 page_set = gtk_print_settings_get_page_set (settings);
1161 switch (page_set)
1162 {
1163 case GTK_PAGE_SET_EVEN :
1164 gtk_print_settings_set (settings,
1165 CPDB_OPTION_PAGE_SET,
1166 CPDB_PAGE_SET_EVEN);
1167 break;
1168
1169 case GTK_PAGE_SET_ODD :
1170 gtk_print_settings_set (settings,
1171 CPDB_OPTION_PAGE_SET,
1172 CPDB_PAGE_SET_ODD);
1173 break;
1174
1175 case GTK_PAGE_SET_ALL :
1176 default :
1177 gtk_print_settings_set (settings,
1178 CPDB_OPTION_PAGE_SET,
1179 CPDB_PAGE_SET_ALL);
1180 }
1181 gtk_print_job_set_page_set (print_job, GTK_PAGE_SET_ALL);
1182 gtk_print_settings_unset (settings, GTK_PRINT_SETTINGS_PAGE_SET);
1183
1184 gtk_print_settings_unset (settings, "printer");
1185
1186 gtk_printer_cpdb_configure_page_setup (printer, page_setup, settings);
1187 gtk_print_settings_unset (settings, "borderless");
1188
1189 gtk_print_settings_foreach (settings,
1190 gtk_printer_cpdb_configure_settings,
1191 printer);
1192 }
1193
1194
1195 static void
1196 cpdb_print_cb (GtkPrintBackendCpdb *backend_cpdb,
1197 GError *error,
1198 gpointer user_data)
1199 {;
1200 GtkPrinterCpdb *printer_cpdb;
1201 cpdb_printer_obj_t *printer_obj;
1202 _PrintStreamData *ps = (_PrintStreamData *) user_data;
1203
1204 if (ps->target_io_stream != NULL)
1205 g_io_stream_close (G_IO_STREAM (ps->target_io_stream), NULL, NULL);
1206
1207 if (ps->callback)
1208 ps->callback (ps->job, ps->user_data, error);
1209
1210 if (ps->dnotify)
1211 ps->dnotify (ps->user_data);
1212
1213 gtk_print_job_set_status (ps->job,
1214 error ? GTK_PRINT_STATUS_FINISHED_ABORTED
1215 : GTK_PRINT_STATUS_FINISHED);
1216
1217 if (!error)
1218 {
1219 printer_cpdb = GTK_PRINTER_CPDB (gtk_print_job_get_printer (ps->job));
1220 printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
1221
1222 GTK_DEBUG (PRINTING, "CPDB Backend: Sending file to CPDB for printing - %s\n", ps->path);
1223
1224 cpdbPrintFile (printer_obj, ps->path);
1225
1226 g_free (ps->path);
1227 }
1228
1229 if (ps->job)
1230 g_object_unref (ps->job);
1231
1232 g_free (ps);
1233 }
1234
1235 static gboolean
1236 cpdb_write (GIOChannel *source,
1237 GIOCondition con,
1238 gpointer user_data)
1239 {
1240 char buf[_CPDB_MAX_CHUNK_SIZE];
1241 gsize bytes_read;
1242 GError *error;
1243 GIOStatus status;
1244 GOutputStream *out_stream;
1245 _PrintStreamData *ps = (_PrintStreamData *) user_data;
1246
1247 error = NULL;
1248
1249 status = g_io_channel_read_chars (source,
1250 buf,
1251 _CPDB_MAX_CHUNK_SIZE,
1252 &bytes_read,
1253 &error);
1254
1255 if (status != G_IO_STATUS_ERROR)
1256 {
1257 gsize bytes_written;
1258
1259 out_stream = g_io_stream_get_output_stream (ps->target_io_stream);
1260 g_output_stream_write_all (out_stream,
1261 buf,
1262 bytes_read,
1263 &bytes_written,
1264 NULL,
1265 &error);
1266 }
1267
1268 if (error != NULL || status == G_IO_STATUS_EOF)
1269 {
1270 cpdb_print_cb (GTK_PRINT_BACKEND_CPDB (ps->backend),
1271 error,
1272 user_data);
1273
1274 if (error != NULL)
1275 {
1276 GTK_DEBUG (PRINTING, "CPDB Backend: Error writing to file - %s\n", error->message);
1277
1278 g_error_free (error);
1279 }
1280
1281 return FALSE;
1282 }
1283
1284 GTK_DEBUG (PRINTING, "CPDB Backend: Writing %" G_GSIZE_FORMAT " byte chunk to cpdb pipe\n", bytes_read);
1285
1286 return TRUE;
1287 }
1288
1289
1290
1291 static void
1292 cpdb_print_stream (GtkPrintBackend *backend,
1293 GtkPrintJob *job,
1294 GIOChannel *data_io,
1295 GtkPrintJobCompleteFunc callback,
1296 gpointer user_data,
1297 GDestroyNotify dnotify)
1298 {
1299 GError *error;
1300 GFile *file;
1301 _PrintStreamData *ps;
1302 GFileIOStream *iostream;
1303
1304 ps = g_new0 (_PrintStreamData, 1);
1305 ps->callback = callback;
1306 ps->user_data = user_data;
1307 ps->dnotify = dnotify;
1308 ps->job = g_object_ref (job);
1309 ps->backend = backend;
1310
1311 error = NULL;
1312 file = g_file_new_tmp (NULL, &iostream, &error);
1313 if (file == NULL)
1314 goto error;
1315
1316 ps->path = g_file_get_path (file);
1317 ps->target_io_stream = G_IO_STREAM (iostream);
1318
1319 g_object_unref (file);
1320
1321 error:
1322 if (error != NULL)
1323 {
1324 GTK_DEBUG (PRINTING, "Error: %s\n", error->message);
1325
1326 cpdb_print_cb (GTK_PRINT_BACKEND_CPDB (backend), error, ps);
1327
1328 g_error_free (error);
1329 return;
1330 }
1331
1332 g_io_add_watch (data_io,
1333 G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
1334 (GIOFunc) cpdb_write,
1335 ps);
1336
1337 }
1338
1339 /*
1340 * Reflect changes in printers to the GTK print dialog.
1341 */
1342 static void
1343 printer_updates_callback (cpdb_frontend_obj_t *frontend_obj,
1344 cpdb_printer_obj_t *printer_obj,
1345 cpdb_printer_update_t change)
1346 {
1347 GtkPrinter *printer;
1348
1349 if (gtk_print_backend == NULL)
1350 return;
1351
1352 switch (change)
1353 {
1354 case CPDB_CHANGE_PRINTER_ADDED:
1355 cpdb_add_gtk_printer (gtk_print_backend, printer_obj);
1356 break;
1357
1358 case CPDB_CHANGE_PRINTER_REMOVED:
1359 cpdb_remove_gtk_printer (gtk_print_backend, printer_obj);
1360 break;
1361
1362 case CPDB_CHANGE_PRINTER_STATE_CHANGED:
1363 printer = gtk_print_backend_find_printer (gtk_print_backend, printer_obj->name);
1364 set_state_message (printer);
1365 g_signal_emit_by_name (gtk_print_backend, "printer-status-changed", printer);
1366 break;
1367
1368 default:
1369 break;
1370 }
1371 }
1372
1373 /*
1374 * @key: printer name
1375 * @value: cpdb_printer_obj_t
1376 * @user_data: GtkPrintBackend
1377 */
1378 static void
1379 cpdb_printer_add_hash_table (gpointer key,
1380 gpointer value,
1381 gpointer user_data)
1382 {
1383 cpdb_add_gtk_printer (user_data, value);
1384 }
1385
1386 /*
1387 * Adds given printer to given GtkPrintBackend
1388 */
1389 static void
1390 cpdb_add_gtk_printer (GtkPrintBackend *backend,
1391 cpdb_printer_obj_t *printer_obj)
1392 {
1393 GtkPrinter *printer;
1394 GtkPrinterCpdb *printer_cpdb;
1395 cpdb_printer_obj_t *default_printer;
1396
1397 GtkPrintBackendCpdb *backend_cpdb = GTK_PRINT_BACKEND_CPDB (backend);
1398
1399 /*
1400 * Ignore printers from FILE backend,
1401 * since we are using "Print To File" GTK print backend.
1402 */
1403 if (g_strcmp0 (printer_obj->backend_name, "FILE") == 0)
1404 return;
1405
1406 printer_cpdb = g_object_new (GTK_TYPE_PRINTER_CPDB,
1407 "name", printer_obj->name,
1408 "backend", backend,
1409 NULL);
1410 gtk_printer_cpdb_set_printer_obj (printer_cpdb, printer_obj);
1411
1412 printer = GTK_PRINTER (printer_cpdb);
1413 gtk_printer_set_icon_name (printer, "printer");
1414 gtk_printer_set_location (printer, printer_obj->location);
1415 gtk_printer_set_description (printer, printer_obj->info);
1416 gtk_printer_set_accepts_pdf (printer, TRUE);
1417 gtk_printer_set_accepts_ps (printer, TRUE);
1418 gtk_printer_set_is_active (printer, TRUE);
1419 gtk_printer_set_has_details (printer, FALSE);
1420
1421 default_printer = cpdbGetDefaultPrinter (backend_cpdb->frontend_obj);
1422 if (default_printer == printer_obj)
1423 gtk_printer_set_is_default (printer, TRUE);
1424
1425 /*
1426 * If printer state is not available, wait till cpdbAcquireDetails ()
1427 * is called when the printer is clicked on in the print dialog
1428 */
1429 if (g_strcmp0 (printer_obj->state, "NA") == 0)
1430 {
1431 gtk_printer_set_is_accepting_jobs (printer, TRUE);
1432 gtk_printer_set_is_paused (printer, FALSE);
1433 gtk_printer_set_state_message (printer, "");
1434 }
1435 else
1436 {
1437 gtk_printer_set_is_accepting_jobs (printer,
1438 cpdbIsAcceptingJobs (printer_obj));
1439 gtk_printer_set_is_paused (printer,
1440 g_strcmp0 (cpdbGetState (printer_obj),
1441 CPDB_STATE_STOPPED) == 0);
1442 set_state_message (printer);
1443 }
1444
1445 gtk_print_backend_add_printer (backend, printer);
1446 if (gtk_print_backend_printer_list_is_done (backend))
1447 {
1448 g_signal_emit_by_name (backend, "printer-added", printer);
1449 g_signal_emit_by_name (backend, "printer-list-changed");
1450 }
1451 g_object_unref (printer);
1452 }
1453
1454 /*
1455 * Removes given printer from given GtkPrintBackend
1456 */
1457 static void
1458 cpdb_remove_gtk_printer (GtkPrintBackend *backend,
1459 cpdb_printer_obj_t *printer_obj)
1460 {
1461 GtkPrinter *printer = gtk_print_backend_find_printer (backend, printer_obj->name);
1462
1463 gtk_print_backend_remove_printer (backend, printer);
1464 g_signal_emit_by_name (backend, "printer-removed", printer);
1465 g_signal_emit_by_name (backend, "printer-list-changed");
1466 }
1467
1468 /*
1469 * Sets printer status
1470 */
1471 static void
1472 set_state_message (GtkPrinter *printer)
1473 {
1474 gboolean stopped, accepting_jobs;
1475 GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (printer);
1476 cpdb_printer_obj_t *printer_obj = gtk_printer_cpdb_get_printer_obj (printer_cpdb);
1477
1478 stopped = g_strcmp0 (cpdbGetState (printer_obj), CPDB_STATE_STOPPED) == 0;
1479 accepting_jobs = cpdbIsAcceptingJobs (printer_obj);
1480
1481 if (stopped && !accepting_jobs)
1482 /* Translators: this is a printer status. */
1483 gtk_printer_set_state_message (printer, _("Paused; Rejecting Jobs"));
1484 else if (stopped && accepting_jobs)
1485 /* Translators: this is a printer status. */
1486 gtk_printer_set_state_message (printer, _("Paused"));
1487 else if (!accepting_jobs)
1488 /* Translators: this is a printer status. */
1489 gtk_printer_set_state_message (printer, _("Rejecting Jobs"));
1490 else
1491 gtk_printer_set_state_message (printer, "");
1492 }
1493
1494 /*
1495 * Fills option choices & sets default in gtk_option from cpdb_option
1496 */
1497 static void
1498 cpdb_fill_gtk_option (GtkPrinterOption *gtk_option,
1499 cpdb_option_t *cpdb_option,
1500 cpdb_printer_obj_t *printer_obj)
1501 {
1502 char *display_val;
1503 char **default_val;
1504
1505 gtk_printer_option_allocate_choices (gtk_option, cpdb_option->num_supported);
1506 for (int i = 0; i < cpdb_option->num_supported; i++)
1507 {
1508 gtk_option->choices[i] = g_strdup (cpdb_option->supported_values[i]);
1509 display_val = cpdb_choice_translation (printer_obj,
1510 cpdb_option->option_name,
1511 cpdb_option->supported_values[i]);
1512 gtk_option->choices_display[i] = g_strdup (display_val);
1513 }
1514
1515 if (g_strcmp0 (cpdb_option->default_value, "NA") != 0)
1516 {
1517 if (g_strcmp0 (cpdb_option->option_name, CPDB_OPTION_JOB_SHEETS) == 0)
1518 {
1519 default_val = g_strsplit (cpdb_option->default_value, ",", 2);
1520 if (g_strcmp0 (gtk_option->name, "gtk-cover-before") == 0)
1521 gtk_printer_option_set (gtk_option, default_val[0]);
1522 else if (g_strcmp0 (gtk_option->name, "gtk-cover-after") == 0)
1523 gtk_printer_option_set (gtk_option, default_val[1]);
1524 }
1525 else
1526 {
1527 gtk_printer_option_set (gtk_option, g_strdup (cpdb_option->default_value));
1528 }
1529 }
1530 }
1531
1532 /*
1533 * Wrapper for getting translation of an option name
1534 */
1535 static char *
1536 cpdb_option_translation (cpdb_printer_obj_t *printer_obj,
1537 const char *option_name)
1538 {
1539 int i;
1540 char *translation = NULL;
1541
1542 for (i = 0; locales[i] != NULL; i++)
1543 {
1544 translation = cpdbGetOptionTranslation (printer_obj,
1545 option_name,
1546 locales[i]);
1547 if (translation != NULL)
1548 break;
1549 }
1550 return translation;
1551 }
1552
1553 /*
1554 * Wrapper for getting translation of a choice name
1555 */
1556 static char *
1557 cpdb_choice_translation (cpdb_printer_obj_t *printer_obj,
1558 const char *option_name,
1559 const char *choice_name)
1560 {
1561 int i;
1562 char *translation = NULL;
1563
1564 for (i = 0; locales[i] != NULL; i++)
1565 {
1566 translation = cpdbGetChoiceTranslation (printer_obj,
1567 option_name,
1568 choice_name,
1569 locales[i]);
1570 if (translation != NULL)
1571 break;
1572 }
1573 return translation;
1574 }
1575
1576 /*
1577 * Wrapper for getting translation of a group name
1578 */
1579 static char *
1580 cpdb_group_translation (cpdb_printer_obj_t *printer_obj,
1581 const char *group_name)
1582 {
1583 int i;
1584 char *translation = NULL;
1585
1586 for (i = 0; locales[i] != NULL; i++)
1587 {
1588 translation = cpdbGetGroupTranslation (printer_obj,
1589 group_name,
1590 locales[i]);
1591 if (translation != NULL)
1592 break;
1593 }
1594 return translation;
1595 }
1596
1597 /*
1598 * Convert CPDB groups explicitly supported by GTK print dialog
1599 */
1600 static gchar *
1601 get_gtk_group (cpdb_printer_obj_t *printer_obj,
1602 const char *group_name)
1603 {
1604 if (g_strcmp0 (group_name, CPDB_GROUP_COLOR) == 0)
1605 return g_strdup ("ColorPage");
1606 if (g_strcmp0 (group_name, CPDB_GROUP_QUALITY) == 0)
1607 return g_strdup ("ImageQualityPage");
1608 if (g_strcmp0 (group_name, CPDB_GROUP_FINISHINGS) == 0)
1609 return g_strdup ("FinishingPage");
1610 return cpdb_group_translation (printer_obj, group_name);
1611 }
1612
1613 /*
1614 * Do initial setup
1615 */
1616 static void
1617 initialize (void)
1618 {
1619 cpdbInit ();
1620
1621 if (locales == NULL)
1622 locales = g_get_language_names ();
1623
1624 if (already_used_options == NULL)
1625 {
1626 already_used_options = g_hash_table_new (g_str_hash, g_str_equal);
1627 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_COPIES);
1628 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_PAGE_RANGES);
1629 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_ORIENTATION);
1630 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_PAGE_DELIVERY);
1631 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_COLLATE);
1632 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_NUMBER_UP);
1633 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_NUMBER_UP_LAYOUT);
1634 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_PAGE_SET);
1635 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_MEDIA);
1636 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_MARGIN_TOP);
1637 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_MARGIN_BOTTOM);
1638 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_MARGIN_LEFT);
1639 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_MARGIN_RIGHT);
1640 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_SIDES);
1641 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_MEDIA_SOURCE);
1642 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_MEDIA_TYPE);
1643 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_OUTPUT_BIN);
1644 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_JOB_PRIORITY);
1645 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_JOB_SHEETS);
1646 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_JOB_HOLD_UNTIL);
1647 g_hash_table_add (already_used_options, (gpointer) CPDB_OPTION_BILLING_INFO);
1648 g_hash_table_add (already_used_options, (gpointer) "borderless");
1649 }
1650 }