]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintbackend.c
RH bug 204621 - "GtkPrint" asks for "Letter" size paper when "A4" size
[~andy/gtk] / gtk / gtkprintbackend.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintbackend.h: Abstract printer backend interfaces
3  * Copyright (C) 2003, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include <string.h>
23
24 #include <gmodule.h>
25
26 #include "gtkintl.h"
27 #include "gtkmodules.h"
28 #include "gtkprivate.h"
29 #include "gtkprintbackend.h"
30 #include "gtkprinter-private.h"
31 #include "gtkalias.h"
32
33 #define GTK_PRINT_BACKEND_GET_PRIVATE(o)  \
34    (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_PRINT_BACKEND, GtkPrintBackendPrivate))
35
36 static void gtk_print_backend_dispose (GObject *object);
37
38 struct _GtkPrintBackendPrivate
39 {
40   GHashTable *printers;
41   guint printer_list_requested : 1;
42   guint printer_list_done : 1;
43 };
44
45 enum {
46   PRINTER_LIST_CHANGED,
47   PRINTER_LIST_DONE,
48   PRINTER_ADDED,
49   PRINTER_REMOVED,
50   PRINTER_STATUS_CHANGED,
51   LAST_SIGNAL
52 };
53
54 static guint signals[LAST_SIGNAL] = { 0 };
55
56 static GObjectClass *backend_parent_class;
57
58 GQuark
59 gtk_print_backend_error_quark (void)
60 {
61   static GQuark quark = 0;
62   if (quark == 0)
63     quark = g_quark_from_static_string ("gtk-print-backend-error-quark");
64   return quark;
65 }
66
67 /*****************************************
68  *     GtkPrintBackendModule modules     *
69  *****************************************/
70
71 typedef struct _GtkPrintBackendModule GtkPrintBackendModule;
72 typedef struct _GtkPrintBackendModuleClass GtkPrintBackendModuleClass;
73
74 struct _GtkPrintBackendModule
75 {
76   GTypeModule parent_instance;
77   
78   GModule *library;
79
80   void             (*init)     (GTypeModule    *module);
81   void             (*exit)     (void);
82   GtkPrintBackend* (*create)   (void);
83
84   gchar *path;
85 };
86
87 struct _GtkPrintBackendModuleClass
88 {
89   GTypeModuleClass parent_class;
90 };
91
92 G_DEFINE_TYPE (GtkPrintBackendModule, _gtk_print_backend_module, G_TYPE_TYPE_MODULE)
93 #define GTK_TYPE_PRINT_BACKEND_MODULE      (_gtk_print_backend_module_get_type ())
94 #define GTK_PRINT_BACKEND_MODULE(module)   (G_TYPE_CHECK_INSTANCE_CAST ((module), GTK_TYPE_PRINT_BACKEND_MODULE, GtkPrintBackendModule))
95
96 static GSList *loaded_backends;
97
98 static gboolean
99 gtk_print_backend_module_load (GTypeModule *module)
100 {
101   GtkPrintBackendModule *pb_module = GTK_PRINT_BACKEND_MODULE (module); 
102   gpointer initp, exitp, createp;
103  
104   pb_module->library = g_module_open (pb_module->path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
105   if (!pb_module->library)
106     {
107       g_warning (g_module_error());
108       return FALSE;
109     }
110   
111   /* extract symbols from the lib */
112   if (!g_module_symbol (pb_module->library, "pb_module_init",
113                         &initp) ||
114       !g_module_symbol (pb_module->library, "pb_module_exit", 
115                         &exitp) ||
116       !g_module_symbol (pb_module->library, "pb_module_create", 
117                         &createp))
118     {
119       g_warning (g_module_error());
120       g_module_close (pb_module->library);
121       
122       return FALSE;
123     }
124
125   pb_module->init = initp;
126   pb_module->exit = exitp;
127   pb_module->create = createp;
128
129   /* call the filesystems's init function to let it */
130   /* setup anything it needs to set up. */
131   pb_module->init (module);
132
133   return TRUE;
134 }
135
136 static void
137 gtk_print_backend_module_unload (GTypeModule *module)
138 {
139   GtkPrintBackendModule *pb_module = GTK_PRINT_BACKEND_MODULE (module);
140   
141   pb_module->exit();
142
143   g_module_close (pb_module->library);
144   pb_module->library = NULL;
145
146   pb_module->init = NULL;
147   pb_module->exit = NULL;
148   pb_module->create = NULL;
149 }
150
151 /* This only will ever be called if an error occurs during
152  * initialization
153  */
154 static void
155 gtk_print_backend_module_finalize (GObject *object)
156 {
157   GtkPrintBackendModule *module = GTK_PRINT_BACKEND_MODULE (object);
158
159   g_free (module->path);
160
161   G_OBJECT_CLASS (_gtk_print_backend_module_parent_class)->finalize (object);
162 }
163
164 static void
165 _gtk_print_backend_module_class_init (GtkPrintBackendModuleClass *class)
166 {
167   GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
168   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
169
170   module_class->load = gtk_print_backend_module_load;
171   module_class->unload = gtk_print_backend_module_unload;
172
173   gobject_class->finalize = gtk_print_backend_module_finalize;
174 }
175
176 static void
177 _gtk_print_backend_module_init (GtkPrintBackendModule *pb_module)
178 {
179 }
180
181 static GtkPrintBackend *
182 _gtk_print_backend_module_create (GtkPrintBackendModule *pb_module)
183 {
184   GtkPrintBackend *pb;
185   
186   if (g_type_module_use (G_TYPE_MODULE (pb_module)))
187     {
188       pb = pb_module->create ();
189       g_type_module_unuse (G_TYPE_MODULE (pb_module));
190       return pb;
191     }
192   return NULL;
193 }
194
195 static GtkPrintBackend *
196 _gtk_print_backend_create (const gchar *backend_name)
197 {
198   GSList *l;
199   gchar *module_path;
200   gchar *full_name;
201   GtkPrintBackendModule *pb_module;
202   GtkPrintBackend *pb;
203
204   for (l = loaded_backends; l != NULL; l = l->next)
205     {
206       pb_module = l->data;
207       
208       if (strcmp (G_TYPE_MODULE (pb_module)->name, backend_name) == 0)
209         return _gtk_print_backend_module_create (pb_module);
210     }
211
212   pb = NULL;
213   if (g_module_supported ())
214     {
215       full_name = g_strconcat ("printbackend-", backend_name, NULL);
216       module_path = _gtk_find_module (full_name, "printbackends");
217       g_free (full_name);
218
219       if (module_path)
220         {
221           pb_module = g_object_new (GTK_TYPE_PRINT_BACKEND_MODULE, NULL);
222
223           g_type_module_set_name (G_TYPE_MODULE (pb_module), backend_name);
224           pb_module->path = g_strdup (module_path);
225
226           loaded_backends = g_slist_prepend (loaded_backends,
227                                              pb_module);
228
229           pb = _gtk_print_backend_module_create (pb_module);
230
231           /* Increase use-count so that we don't unload print backends.
232            * There is a problem with module unloading in the cups module,
233            * see cups_dispatch_watch_finalize for details. 
234            */
235           g_type_module_use (G_TYPE_MODULE (pb_module));
236         }
237       
238       g_free (module_path);
239     }
240
241   return pb;
242 }
243
244 GList *
245 gtk_print_backend_load_modules (void)
246 {
247   GList *result;
248   GtkPrintBackend *backend;
249   gchar *setting;
250   gchar **backends;
251   gint i;
252   GtkSettings *settings;
253
254   result = NULL;
255
256   settings = gtk_settings_get_default ();
257   if (settings)
258     g_object_get (settings, "gtk-print-backends", &setting, NULL);
259   else
260     setting = g_strdup (GTK_PRINT_BACKENDS);
261
262   backends = g_strsplit (setting, ",", -1);
263
264   for (i = 0; backends[i]; i++)
265     {
266       g_strchug (backends[i]);
267       g_strchomp (backends[i]);
268       backend = _gtk_print_backend_create (backends[i]);
269       
270       if (backend)
271         result = g_list_append (result, backend);
272     }
273
274   g_strfreev (backends);
275   g_free (setting);
276
277   return result;
278 }
279
280 /*****************************************
281  *             GtkPrintBackend           *
282  *****************************************/
283
284 G_DEFINE_TYPE (GtkPrintBackend, gtk_print_backend, G_TYPE_OBJECT)
285
286 static void                 fallback_printer_request_details       (GtkPrinter          *printer);
287 static gboolean             fallback_printer_mark_conflicts        (GtkPrinter          *printer,
288                                                                     GtkPrinterOptionSet *options);
289 static void                 fallback_printer_get_hard_margins      (GtkPrinter          *printer,
290                                                                     gdouble             *top,
291                                                                     gdouble             *bottom,
292                                                                     gdouble             *left,
293                                                                     gdouble             *right);
294 static GList *              fallback_printer_list_papers           (GtkPrinter          *printer);
295 static GtkPageSetup *       fallback_printer_get_default_page_size (GtkPrinter          *printer);
296 static GtkPrintCapabilities fallback_printer_get_capabilities      (GtkPrinter          *printer);
297   
298 static void
299 gtk_print_backend_class_init (GtkPrintBackendClass *class)
300 {
301   GObjectClass *object_class;
302   object_class = (GObjectClass *) class;
303
304   backend_parent_class = g_type_class_peek_parent (class);
305   
306   object_class->dispose = gtk_print_backend_dispose;
307
308   class->printer_request_details = fallback_printer_request_details;
309   class->printer_mark_conflicts = fallback_printer_mark_conflicts;
310   class->printer_get_hard_margins = fallback_printer_get_hard_margins;
311   class->printer_list_papers = fallback_printer_list_papers;
312   class->printer_get_default_page_size = fallback_printer_get_default_page_size;
313   class->printer_get_capabilities = fallback_printer_get_capabilities;
314   
315   g_type_class_add_private (class, sizeof (GtkPrintBackendPrivate));
316   
317   signals[PRINTER_LIST_CHANGED] =
318     g_signal_new (I_("printer-list-changed"),
319                   G_TYPE_FROM_CLASS (class),
320                   G_SIGNAL_RUN_LAST,
321                   G_STRUCT_OFFSET (GtkPrintBackendClass, printer_list_changed),
322                   NULL, NULL,
323                   g_cclosure_marshal_VOID__VOID,
324                   G_TYPE_NONE, 0);
325   signals[PRINTER_LIST_DONE] =
326     g_signal_new (I_("printer-list-done"),
327                     G_TYPE_FROM_CLASS (class),
328                     G_SIGNAL_RUN_LAST,
329                     G_STRUCT_OFFSET (GtkPrintBackendClass, printer_list_done),
330                     NULL, NULL,
331                     g_cclosure_marshal_VOID__VOID,
332                     G_TYPE_NONE, 0);
333   signals[PRINTER_ADDED] =
334     g_signal_new (I_("printer-added"),
335                   G_TYPE_FROM_CLASS (class),
336                   G_SIGNAL_RUN_LAST,
337                   G_STRUCT_OFFSET (GtkPrintBackendClass, printer_added),
338                   NULL, NULL,
339                   g_cclosure_marshal_VOID__OBJECT,
340                   G_TYPE_NONE, 1, GTK_TYPE_PRINTER);
341   signals[PRINTER_REMOVED] =
342     g_signal_new (I_("printer-removed"),
343                   G_TYPE_FROM_CLASS (class),
344                   G_SIGNAL_RUN_LAST,
345                   G_STRUCT_OFFSET (GtkPrintBackendClass, printer_removed),
346                   NULL, NULL,
347                   g_cclosure_marshal_VOID__OBJECT,
348                   G_TYPE_NONE, 1, GTK_TYPE_PRINTER);
349   signals[PRINTER_STATUS_CHANGED] =
350     g_signal_new (I_("printer-status-changed"),
351                   G_TYPE_FROM_CLASS (class),
352                   G_SIGNAL_RUN_LAST,
353                   G_STRUCT_OFFSET (GtkPrintBackendClass, printer_status_changed),
354                   NULL, NULL,
355                   g_cclosure_marshal_VOID__OBJECT,
356                   G_TYPE_NONE, 1, GTK_TYPE_PRINTER);
357 }
358
359 static void
360 gtk_print_backend_init (GtkPrintBackend *backend)
361 {
362   GtkPrintBackendPrivate *priv;
363
364   priv = backend->priv = GTK_PRINT_BACKEND_GET_PRIVATE (backend); 
365
366   priv->printers = g_hash_table_new_full (g_str_hash, g_str_equal, 
367                                           (GDestroyNotify) g_free,
368                                           (GDestroyNotify) g_object_unref);
369 }
370
371 static void
372 gtk_print_backend_dispose (GObject *object)
373 {
374   GtkPrintBackend *backend;
375   GtkPrintBackendPrivate *priv;
376
377   backend = GTK_PRINT_BACKEND (object);
378   priv = backend->priv;
379
380   /* We unref the printers in dispose, not in finalize so that
381    * we can break refcount cycles with gtk_print_backend_destroy 
382    */
383   if (priv->printers)
384     {
385       g_hash_table_destroy (priv->printers);
386       priv->printers = NULL;
387     }
388
389   backend_parent_class->dispose (object);
390 }
391
392
393 static void
394 fallback_printer_request_details (GtkPrinter *printer)
395 {
396 }
397
398 static gboolean
399 fallback_printer_mark_conflicts (GtkPrinter          *printer,
400                                  GtkPrinterOptionSet *options)
401 {
402   return FALSE;
403 }
404
405 static void
406 fallback_printer_get_hard_margins (GtkPrinter *printer,
407                                    gdouble    *top,
408                                    gdouble    *bottom,
409                                    gdouble    *left,
410                                    gdouble    *right)
411 {
412   *top = 0;
413   *bottom = 0;
414   *left = 0;
415   *right = 0;
416 }
417
418 static GList *
419 fallback_printer_list_papers (GtkPrinter *printer)
420 {
421   return NULL;
422 }
423
424 static GtkPageSetup *
425 fallback_printer_get_default_page_size (GtkPrinter *printer)
426 {
427   return NULL;
428 }
429
430 static GtkPrintCapabilities
431 fallback_printer_get_capabilities (GtkPrinter *printer)
432 {
433   return 0;
434 }
435
436
437 static void
438 printer_hash_to_sorted_active_list (const gchar  *key,
439                                     gpointer      value,
440                                     GList       **out_list)
441 {
442   GtkPrinter *printer;
443
444   printer = GTK_PRINTER (value);
445
446   if (gtk_printer_get_name (printer) == NULL)
447     return;
448
449   if (!gtk_printer_is_active (printer))
450     return;
451
452   *out_list = g_list_insert_sorted (*out_list, value, (GCompareFunc) gtk_printer_compare);
453 }
454
455
456 void
457 gtk_print_backend_add_printer (GtkPrintBackend *backend,
458                                GtkPrinter      *printer)
459 {
460   GtkPrintBackendPrivate *priv;
461   
462   g_return_if_fail (GTK_IS_PRINT_BACKEND (backend));
463
464   priv = backend->priv;
465
466   if (!priv->printers)
467     return;
468   
469   g_hash_table_insert (priv->printers,
470                        g_strdup (gtk_printer_get_name (printer)), 
471                        g_object_ref (printer));
472 }
473
474 void
475 gtk_print_backend_remove_printer (GtkPrintBackend *backend,
476                                   GtkPrinter      *printer)
477 {
478   GtkPrintBackendPrivate *priv;
479   
480   g_return_if_fail (GTK_IS_PRINT_BACKEND (backend));
481   priv = backend->priv;
482
483   if (!priv->printers)
484     return;
485   
486   g_hash_table_remove (priv->printers,
487                        gtk_printer_get_name (printer));
488 }
489
490 void
491 gtk_print_backend_set_list_done (GtkPrintBackend *backend)
492 {
493   if (!backend->priv->printer_list_done)
494     {
495       backend->priv->printer_list_done = TRUE;
496       g_signal_emit (backend, signals[PRINTER_LIST_DONE], 0);
497     }
498 }
499
500
501 GList *
502 gtk_print_backend_get_printer_list (GtkPrintBackend *backend)
503 {
504   GtkPrintBackendPrivate *priv;
505   GList *result;
506   
507   g_return_val_if_fail (GTK_IS_PRINT_BACKEND (backend), NULL);
508
509   priv = backend->priv;
510
511   result = NULL;
512   if (priv->printers != NULL)
513     g_hash_table_foreach (priv->printers,
514                           (GHFunc) printer_hash_to_sorted_active_list,
515                           &result);
516
517   if (!priv->printer_list_requested && priv->printers != NULL)
518     {
519       if (GTK_PRINT_BACKEND_GET_CLASS (backend)->request_printer_list)
520         GTK_PRINT_BACKEND_GET_CLASS (backend)->request_printer_list (backend);
521       priv->printer_list_requested = TRUE;
522     }
523   
524   return result;
525 }
526
527 gboolean
528 gtk_print_backend_printer_list_is_done (GtkPrintBackend *print_backend)
529 {
530   g_return_val_if_fail (GTK_IS_PRINT_BACKEND (print_backend), TRUE);
531
532   return print_backend->priv->printer_list_done;
533 }
534
535 GtkPrinter *
536 gtk_print_backend_find_printer (GtkPrintBackend *backend,
537                                 const gchar     *printer_name)
538 {
539   GtkPrintBackendPrivate *priv;
540   GtkPrinter *printer;
541   
542   g_return_val_if_fail (GTK_IS_PRINT_BACKEND (backend), NULL);
543
544   priv = backend->priv;
545
546   if (priv->printers)
547     printer = g_hash_table_lookup (priv->printers, printer_name);
548   else
549     printer = NULL;
550
551   return printer;  
552 }
553
554 void
555 gtk_print_backend_print_stream (GtkPrintBackend        *backend,
556                                 GtkPrintJob            *job,
557                                 GIOChannel             *data_io,
558                                 GtkPrintJobCompleteFunc callback,
559                                 gpointer                user_data,
560                                 GDestroyNotify          dnotify)
561 {
562   g_return_if_fail (GTK_IS_PRINT_BACKEND (backend));
563
564   GTK_PRINT_BACKEND_GET_CLASS (backend)->print_stream (backend,
565                                                        job,
566                                                        data_io,
567                                                        callback,
568                                                        user_data,
569                                                        dnotify);
570 }
571
572 void
573 gtk_print_backend_destroy (GtkPrintBackend *print_backend)
574 {
575   /* The lifecycle of print backends and printers are tied, such that
576    * the backend owns the printers, but the printers also ref the backend.
577    * This is so that if the app has a reference to a printer its backend
578    * will be around. However, this results in a cycle, which we break
579    * with this call, which causes the print backend to release its printers.
580    */
581   g_object_run_dispose (G_OBJECT (print_backend));
582 }
583
584 #define __GTK_PRINT_BACKEND_C__
585 #include "gtkaliasdef.c"