]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintbackend.c
Cosmetic cleanups.
[~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 static void
245 gtk_print_backend_initialize (void)
246 {
247   static gboolean initialized = FALSE;
248
249   if (!initialized)
250     {
251       gtk_settings_install_property (g_param_spec_string ("gtk-print-backends",
252                                                           P_("Default print backend"),
253                                                           P_("List of the GtkPrintBackend backends to use by default"),
254                                                           GTK_PRINT_BACKENDS,
255                                                           GTK_PARAM_READWRITE));
256
257       gtk_settings_install_property (g_param_spec_string ("gtk-print-preview-command",
258                                                           P_("Default command to run when displaying a print preview"),
259                                                           P_("Command to run when displaying a print preview"),
260                                                           GTK_PRINT_PREVIEW_COMMAND,
261                                                           GTK_PARAM_READWRITE)); 
262       initialized = TRUE;
263     }
264 }
265
266
267
268 GList *
269 gtk_print_backend_load_modules (void)
270 {
271   GList *result;
272   GtkPrintBackend *backend;
273   gchar *setting;
274   gchar **backends;
275   gint i;
276   GtkSettings *settings;
277
278   result = NULL;
279
280   gtk_print_backend_initialize ();
281   
282   settings = gtk_settings_get_default ();
283
284   g_object_get (settings, "gtk-print-backends", &setting, NULL);
285
286   backends = g_strsplit (setting, ",", -1);
287
288   for (i = 0; backends[i]; i++)
289     {
290       g_strchug (backends[i]);
291       g_strchomp (backends[i]);
292       backend = _gtk_print_backend_create (backends[i]);
293       
294       if (backend)
295         result = g_list_append (result, backend);
296     }
297
298   g_strfreev (backends);
299   g_free (setting);
300
301   return result;
302 }
303
304 /*****************************************
305  *             GtkPrintBackend           *
306  *****************************************/
307
308 G_DEFINE_TYPE (GtkPrintBackend, gtk_print_backend, G_TYPE_OBJECT)
309
310 static void                 fallback_printer_request_details  (GtkPrinter          *printer);
311 static gboolean             fallback_printer_mark_conflicts   (GtkPrinter          *printer,
312                                                                GtkPrinterOptionSet *options);
313 static void                 fallback_printer_get_hard_margins (GtkPrinter          *printer,
314                                                                gdouble             *top,
315                                                                gdouble             *bottom,
316                                                                gdouble             *left,
317                                                                gdouble             *right);
318 static GList *              fallback_printer_list_papers      (GtkPrinter          *printer);
319 static GtkPrintCapabilities fallback_printer_get_capabilities (GtkPrinter          *printer);
320   
321 static void
322 gtk_print_backend_class_init (GtkPrintBackendClass *class)
323 {
324   GObjectClass *object_class;
325   object_class = (GObjectClass *) class;
326
327   backend_parent_class = g_type_class_peek_parent (class);
328   
329   object_class->dispose = gtk_print_backend_dispose;
330
331   class->printer_request_details = fallback_printer_request_details;
332   class->printer_mark_conflicts = fallback_printer_mark_conflicts;
333   class->printer_get_hard_margins = fallback_printer_get_hard_margins;
334   class->printer_list_papers = fallback_printer_list_papers;
335   class->printer_get_capabilities = fallback_printer_get_capabilities;
336   
337   g_type_class_add_private (class, sizeof (GtkPrintBackendPrivate));
338   
339   signals[PRINTER_LIST_CHANGED] =
340     g_signal_new (I_("printer-list-changed"),
341                   G_TYPE_FROM_CLASS (class),
342                   G_SIGNAL_RUN_LAST,
343                   G_STRUCT_OFFSET (GtkPrintBackendClass, printer_list_changed),
344                   NULL, NULL,
345                   g_cclosure_marshal_VOID__VOID,
346                   G_TYPE_NONE, 0);
347   signals[PRINTER_LIST_DONE] =
348     g_signal_new (I_("printer-list-done"),
349                     G_TYPE_FROM_CLASS (class),
350                     G_SIGNAL_RUN_LAST,
351                     G_STRUCT_OFFSET (GtkPrintBackendClass, printer_list_done),
352                     NULL, NULL,
353                     g_cclosure_marshal_VOID__VOID,
354                     G_TYPE_NONE, 0);
355   signals[PRINTER_ADDED] =
356     g_signal_new (I_("printer-added"),
357                   G_TYPE_FROM_CLASS (class),
358                   G_SIGNAL_RUN_LAST,
359                   G_STRUCT_OFFSET (GtkPrintBackendClass, printer_added),
360                   NULL, NULL,
361                   g_cclosure_marshal_VOID__OBJECT,
362                   G_TYPE_NONE, 1, GTK_TYPE_PRINTER);
363   signals[PRINTER_REMOVED] =
364     g_signal_new (I_("printer-removed"),
365                   G_TYPE_FROM_CLASS (class),
366                   G_SIGNAL_RUN_LAST,
367                   G_STRUCT_OFFSET (GtkPrintBackendClass, printer_removed),
368                   NULL, NULL,
369                   g_cclosure_marshal_VOID__OBJECT,
370                   G_TYPE_NONE, 1, GTK_TYPE_PRINTER);
371   signals[PRINTER_STATUS_CHANGED] =
372     g_signal_new (I_("printer-status-changed"),
373                   G_TYPE_FROM_CLASS (class),
374                   G_SIGNAL_RUN_LAST,
375                   G_STRUCT_OFFSET (GtkPrintBackendClass, printer_status_changed),
376                   NULL, NULL,
377                   g_cclosure_marshal_VOID__OBJECT,
378                   G_TYPE_NONE, 1, GTK_TYPE_PRINTER);
379 }
380
381 static void
382 gtk_print_backend_init (GtkPrintBackend *backend)
383 {
384   GtkPrintBackendPrivate *priv;
385
386   priv = backend->priv = GTK_PRINT_BACKEND_GET_PRIVATE (backend); 
387
388   priv->printers = g_hash_table_new_full (g_str_hash, g_str_equal, 
389                                           (GDestroyNotify) g_free,
390                                           (GDestroyNotify) g_object_unref);
391 }
392
393 static void
394 gtk_print_backend_dispose (GObject *object)
395 {
396   GtkPrintBackend *backend;
397   GtkPrintBackendPrivate *priv;
398
399   backend = GTK_PRINT_BACKEND (object);
400   priv = backend->priv;
401
402   /* We unref the printers in dispose, not in finalize so that
403    * we can break refcount cycles with gtk_print_backend_destroy 
404    */
405   if (priv->printers)
406     {
407       g_hash_table_destroy (priv->printers);
408       priv->printers = NULL;
409     }
410
411   backend_parent_class->dispose (object);
412 }
413
414
415 static void
416 fallback_printer_request_details (GtkPrinter *printer)
417 {
418 }
419
420 static gboolean
421 fallback_printer_mark_conflicts (GtkPrinter          *printer,
422                                  GtkPrinterOptionSet *options)
423 {
424   return FALSE;
425 }
426
427 static void
428 fallback_printer_get_hard_margins (GtkPrinter *printer,
429                                    gdouble    *top,
430                                    gdouble    *bottom,
431                                    gdouble    *left,
432                                    gdouble    *right)
433 {
434   *top = 0;
435   *bottom = 0;
436   *left = 0;
437   *right = 0;
438 }
439
440 static GList *
441 fallback_printer_list_papers (GtkPrinter *printer)
442 {
443   return NULL;
444 }
445
446 static GtkPrintCapabilities
447 fallback_printer_get_capabilities (GtkPrinter *printer)
448 {
449   return 0;
450 }
451
452
453 static void
454 printer_hash_to_sorted_active_list (const gchar  *key,
455                                     gpointer      value,
456                                     GList       **out_list)
457 {
458   GtkPrinter *printer;
459
460   printer = GTK_PRINTER (value);
461
462   if (gtk_printer_get_name (printer) == NULL)
463     return;
464
465   if (!gtk_printer_is_active (printer))
466     return;
467
468   *out_list = g_list_insert_sorted (*out_list, value, (GCompareFunc) gtk_printer_compare);
469 }
470
471
472 void
473 gtk_print_backend_add_printer (GtkPrintBackend *backend,
474                                GtkPrinter      *printer)
475 {
476   GtkPrintBackendPrivate *priv;
477   
478   g_return_if_fail (GTK_IS_PRINT_BACKEND (backend));
479
480   priv = backend->priv;
481
482   if (!priv->printers)
483     return;
484   
485   g_hash_table_insert (priv->printers,
486                        g_strdup (gtk_printer_get_name (printer)), 
487                        g_object_ref (printer));
488 }
489
490 void
491 gtk_print_backend_remove_printer (GtkPrintBackend *backend,
492                                   GtkPrinter      *printer)
493 {
494   GtkPrintBackendPrivate *priv;
495   
496   g_return_if_fail (GTK_IS_PRINT_BACKEND (backend));
497   priv = backend->priv;
498
499   if (!priv->printers)
500     return;
501   
502   g_hash_table_remove (priv->printers,
503                        gtk_printer_get_name (printer));
504 }
505
506 void
507 gtk_print_backend_set_list_done (GtkPrintBackend *backend)
508 {
509   if (!backend->priv->printer_list_done)
510     {
511       backend->priv->printer_list_done = TRUE;
512       g_signal_emit (backend, signals[PRINTER_LIST_DONE], 0);
513     }
514 }
515
516
517 GList *
518 gtk_print_backend_get_printer_list (GtkPrintBackend *backend)
519 {
520   GtkPrintBackendPrivate *priv;
521   GList *result;
522   
523   g_return_val_if_fail (GTK_IS_PRINT_BACKEND (backend), NULL);
524
525   priv = backend->priv;
526
527   result = NULL;
528   if (priv->printers != NULL)
529     g_hash_table_foreach (priv->printers,
530                           (GHFunc) printer_hash_to_sorted_active_list,
531                           &result);
532
533   if (!priv->printer_list_requested && priv->printers != NULL)
534     {
535       if (GTK_PRINT_BACKEND_GET_CLASS (backend)->request_printer_list)
536         GTK_PRINT_BACKEND_GET_CLASS (backend)->request_printer_list (backend);
537       priv->printer_list_requested = TRUE;
538     }
539   
540   return result;
541 }
542
543 gboolean
544 gtk_print_backend_printer_list_is_done (GtkPrintBackend *print_backend)
545 {
546   g_return_val_if_fail (GTK_IS_PRINT_BACKEND (print_backend), TRUE);
547
548   return print_backend->priv->printer_list_done;
549 }
550
551 GtkPrinter *
552 gtk_print_backend_find_printer (GtkPrintBackend *backend,
553                                 const gchar     *printer_name)
554 {
555   GtkPrintBackendPrivate *priv;
556   GtkPrinter *printer;
557   
558   g_return_val_if_fail (GTK_IS_PRINT_BACKEND (backend), NULL);
559
560   priv = backend->priv;
561
562   if (priv->printers)
563     printer = g_hash_table_lookup (priv->printers, printer_name);
564   else
565     printer = NULL;
566
567   return printer;  
568 }
569
570 void
571 gtk_print_backend_print_stream (GtkPrintBackend        *backend,
572                                 GtkPrintJob            *job,
573                                 gint                    data_fd,
574                                 GtkPrintJobCompleteFunc callback,
575                                 gpointer                user_data,
576                                 GDestroyNotify          dnotify)
577 {
578   g_return_if_fail (GTK_IS_PRINT_BACKEND (backend));
579
580   GTK_PRINT_BACKEND_GET_CLASS (backend)->print_stream (backend,
581                                                        job,
582                                                        data_fd,
583                                                        callback,
584                                                        user_data,
585                                                        dnotify);
586 }
587
588 void
589 gtk_print_backend_destroy (GtkPrintBackend *print_backend)
590 {
591   /* The lifecycle of print backends and printers are tied, such that
592    * the backend owns the printers, but the printers also ref the backend.
593    * This is so that if the app has a reference to a printer its backend
594    * will be around. However, this results in a cycle, which we break
595    * with this call, which causes the print backend to release its printers.
596    */
597   g_object_run_dispose (G_OBJECT (print_backend));
598 }
599
600 #define __GTK_PRINT_BACKEND_C__
601 #include "gtkaliasdef.c"