1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * gnome-printer-dialog.c: |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or |
---|
6 | * modify it under the terms of the GNU Library General Public License |
---|
7 | * as published by the Free Software Foundation; either version 2 of |
---|
8 | * the License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program 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 |
---|
13 | * GNU Library General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Library General Public |
---|
16 | * License along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
18 | * |
---|
19 | * Authors: |
---|
20 | * Raph Levien <raph@acm.org> |
---|
21 | * Miguel de Icaza <miguel@kernel.org> |
---|
22 | * Lauris Kaplinski <lauris@ximian.com> |
---|
23 | * Chema Celorio <chema@ximian.com> |
---|
24 | * |
---|
25 | * Copyright (C) 1999-2003 Ximian Inc. and authors |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include <config.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | #include <atk/atk.h> |
---|
33 | #include <gtk/gtk.h> |
---|
34 | |
---|
35 | #include "gnome-print-i18n.h" |
---|
36 | #include "gnome-printer-selector.h" |
---|
37 | #include "gnome-print-config-dialog.h" |
---|
38 | #include <libgnomeprint/gnome-print-config.h> |
---|
39 | #include <libgnomeprint/gnome-print.h> |
---|
40 | #include <libgnomeprint/private/gpa-printer.h> |
---|
41 | #include <libgnomeprint/private/gpa-node.h> |
---|
42 | #include <libgnomeprint/private/gpa-state.h> |
---|
43 | #include <libgnomeprint/private/gpa-root.h> |
---|
44 | |
---|
45 | #include "gpaui/gpa-printer-selector.h" |
---|
46 | #include "gpaui/gpa-settings-selector.h" |
---|
47 | #include "gpaui/gpa-transport-selector.h" |
---|
48 | |
---|
49 | #define GPS_PAD 4 |
---|
50 | #define ADD_PRINTER_APP "gnome-cups-add" |
---|
51 | |
---|
52 | static void gnome_printer_selector_class_init (GnomePrinterSelectorClass *klass); |
---|
53 | static void gnome_printer_selector_init (GObject *object); |
---|
54 | static gint gnome_printer_selector_construct (GPAWidget *gpa_widget); |
---|
55 | |
---|
56 | static void gnome_printer_selector_finalize (GObject *object); |
---|
57 | |
---|
58 | static GtkWidget *gpw_create_label_with_mnemonic (GtkTable *table, gint l, gint r, gint t, gint b, const gchar *text, GtkWidget *mnemonic_widget, unsigned y_pad); |
---|
59 | |
---|
60 | static void gpw_configure_clicked (GtkWidget *widget, GPAWidget *gpaw); |
---|
61 | static void gpw_add_clicked (GtkWidget *btn, GPAWidget *gpaw); |
---|
62 | static void start_polling (GnomePrinterSelector *ps); |
---|
63 | static void stop_polling (GnomePrinterSelector *ps); |
---|
64 | |
---|
65 | static GPAWidgetClass *parent_class; |
---|
66 | |
---|
67 | GType |
---|
68 | gnome_printer_selector_get_type (void) |
---|
69 | { |
---|
70 | static GType type = 0; |
---|
71 | if (!type) { |
---|
72 | static const GTypeInfo info = { |
---|
73 | sizeof (GnomePrinterSelectorClass), |
---|
74 | NULL, NULL, |
---|
75 | (GClassInitFunc) gnome_printer_selector_class_init, |
---|
76 | NULL, NULL, |
---|
77 | sizeof (GnomePrinterSelector), |
---|
78 | 0, |
---|
79 | (GInstanceInitFunc) gnome_printer_selector_init |
---|
80 | }; |
---|
81 | type = g_type_register_static (GPA_TYPE_WIDGET, "GnomePrinterSelector", &info, 0); |
---|
82 | } |
---|
83 | return type; |
---|
84 | } |
---|
85 | |
---|
86 | static void |
---|
87 | gnome_printer_selector_class_init (GnomePrinterSelectorClass *klass) |
---|
88 | { |
---|
89 | GObjectClass *object_class; |
---|
90 | GPAWidgetClass *gpa_class; |
---|
91 | |
---|
92 | object_class = (GObjectClass *) klass; |
---|
93 | gpa_class = (GPAWidgetClass *) klass; |
---|
94 | |
---|
95 | parent_class = gtk_type_class (GPA_TYPE_WIDGET); |
---|
96 | gpa_class->construct = gnome_printer_selector_construct; |
---|
97 | object_class->finalize = gnome_printer_selector_finalize; |
---|
98 | } |
---|
99 | |
---|
100 | static void |
---|
101 | gnome_printer_selector_init (GObject *object) |
---|
102 | { |
---|
103 | } |
---|
104 | |
---|
105 | typedef struct { |
---|
106 | GObject parent; |
---|
107 | GPANode *node; |
---|
108 | } myGnomePrintConfig; |
---|
109 | |
---|
110 | static void |
---|
111 | start_polling (GnomePrinterSelector *ps) |
---|
112 | { |
---|
113 | GPANode *child; |
---|
114 | GPANode *printers = GPA_NODE (gpa_get_printers ()); |
---|
115 | child = gpa_node_get_child (printers, NULL); |
---|
116 | while (child) { |
---|
117 | GPAPrinter *printer = GPA_PRINTER (child); |
---|
118 | gpa_printer_set_polling (printer, TRUE); |
---|
119 | child = gpa_node_get_child (printers, child); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | static void |
---|
124 | stop_polling (GnomePrinterSelector *ps) |
---|
125 | { |
---|
126 | GPANode *child; |
---|
127 | GPANode *printers = GPA_NODE (gpa_get_printers ()); |
---|
128 | child = gpa_node_get_child (printers, NULL); |
---|
129 | while (child) { |
---|
130 | GPAPrinter *printer = GPA_PRINTER (child); |
---|
131 | gpa_printer_set_polling (printer, FALSE); |
---|
132 | child = gpa_node_get_child (printers, child); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | static gint |
---|
137 | gnome_printer_selector_construct (GPAWidget *gpa_widget) |
---|
138 | { |
---|
139 | GnomePrinterSelector *gpw; |
---|
140 | GtkWidget *t, *b, *l, *v; |
---|
141 | AtkObject *atko; |
---|
142 | |
---|
143 | gpw = GNOME_PRINTER_SELECTOR (gpa_widget); |
---|
144 | |
---|
145 | gpw->accel_group = gtk_accel_group_new (); |
---|
146 | |
---|
147 | g_signal_connect (gpw, "hide", G_CALLBACK (stop_polling), NULL); |
---|
148 | g_signal_connect (gpw, "show", G_CALLBACK (start_polling), NULL); |
---|
149 | |
---|
150 | v = gtk_vbox_new (FALSE, 0); |
---|
151 | gpw->printers = gpa_widget_new (GPA_TYPE_PRINTER_SELECTOR, NULL); |
---|
152 | gtk_box_pack_start_defaults (GTK_BOX (v), gpw->printers); |
---|
153 | gtk_widget_show (gpw->printers); |
---|
154 | |
---|
155 | gtk_container_add (GTK_CONTAINER (gpw), v); |
---|
156 | gtk_widget_show (v); |
---|
157 | |
---|
158 | t = gtk_table_new (2, 6, FALSE); |
---|
159 | gtk_widget_show (t); |
---|
160 | gtk_box_pack_start (GTK_BOX (v), t, FALSE, TRUE, GPS_PAD); |
---|
161 | |
---|
162 | b = gtk_button_new_with_mnemonic (_("Co_nfigure")); |
---|
163 | gtk_widget_show (b); |
---|
164 | g_signal_connect (G_OBJECT (b), "clicked", |
---|
165 | (GCallback) gpw_configure_clicked, gpw); |
---|
166 | |
---|
167 | gtk_table_attach (GTK_TABLE (t), b, 2, 3, 1, 2, |
---|
168 | GTK_FILL, 0, |
---|
169 | GPS_PAD, GPS_PAD); |
---|
170 | atko = gtk_widget_get_accessible (b); |
---|
171 | atk_object_set_description (atko, _("Adjust the settings of the selected printer")); |
---|
172 | |
---|
173 | if (g_find_program_in_path (ADD_PRINTER_APP)) { |
---|
174 | GtkWidget *align = gtk_alignment_new (1., .5, 0., 0.); |
---|
175 | b = gtk_button_new_from_stock (GTK_STOCK_ADD); |
---|
176 | g_signal_connect (G_OBJECT (b), "clicked", |
---|
177 | (GCallback) gpw_add_clicked, gpw); |
---|
178 | |
---|
179 | gtk_container_add (GTK_CONTAINER (align), b); |
---|
180 | gtk_widget_show_all (align); |
---|
181 | gtk_table_attach (GTK_TABLE (t), align, 4, 6, 1, 2, |
---|
182 | GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, |
---|
183 | GPS_PAD, GPS_PAD); |
---|
184 | atko = gtk_widget_get_accessible (b); |
---|
185 | #warning translate after branch |
---|
186 | atk_object_set_description (atko, "Define a new local printer"); |
---|
187 | } |
---|
188 | |
---|
189 | gpw->settings = gpa_widget_new (GPA_TYPE_SETTINGS_SELECTOR, NULL); |
---|
190 | gtk_widget_show (gpw->settings); |
---|
191 | gtk_table_attach (GTK_TABLE (t), gpw->settings, 1, 2, 1, 2, |
---|
192 | GTK_FILL, 0, |
---|
193 | GPS_PAD, GPS_PAD); |
---|
194 | gpw_create_label_with_mnemonic (GTK_TABLE (t), 0, 1, 1, 2, |
---|
195 | _("_Settings:"), |
---|
196 | ((GPASettingsSelector*) gpw->settings)->menu, GPS_PAD); |
---|
197 | |
---|
198 | gpw->transport = gpa_widget_new (GPA_TYPE_TRANSPORT_SELECTOR, NULL); |
---|
199 | gtk_widget_show (gpw->transport); |
---|
200 | gtk_table_attach (GTK_TABLE (t), gpw->transport, 1, 3, 2, 3, |
---|
201 | GTK_FILL , 0, |
---|
202 | GPS_PAD, GPS_PAD); |
---|
203 | l = gpw_create_label_with_mnemonic (GTK_TABLE (t), 0, 1, 2, 3, |
---|
204 | _("_Location:"), |
---|
205 | ((GPATransportSelector*) gpw->transport)->combo, GPS_PAD*3); |
---|
206 | return TRUE; |
---|
207 | } |
---|
208 | |
---|
209 | static void |
---|
210 | gnome_printer_selector_finalize (GObject *object) |
---|
211 | { |
---|
212 | GnomePrinterSelector *gpw; |
---|
213 | |
---|
214 | gpw = GNOME_PRINTER_SELECTOR (object); |
---|
215 | |
---|
216 | if (gpw->handler_config) { |
---|
217 | g_signal_handler_disconnect |
---|
218 | (G_OBJECT (((myGnomePrintConfig *) |
---|
219 | GPA_WIDGET(gpw)->config)->node), |
---|
220 | gpw->handler_config); |
---|
221 | gpw->handler_config = 0; |
---|
222 | } |
---|
223 | |
---|
224 | if (gpw->accel_group) { |
---|
225 | g_object_unref (G_OBJECT (gpw->accel_group)); |
---|
226 | gpw->accel_group = NULL; |
---|
227 | } |
---|
228 | |
---|
229 | stop_polling (gpw); |
---|
230 | |
---|
231 | G_OBJECT_CLASS (parent_class)->finalize (object); |
---|
232 | } |
---|
233 | |
---|
234 | GtkWidget * |
---|
235 | gnome_printer_selector_new_default (void) |
---|
236 | { |
---|
237 | GtkWidget *gpw; |
---|
238 | GnomePrintConfig *config; |
---|
239 | |
---|
240 | config = gnome_print_config_default (); |
---|
241 | gpw = gnome_printer_selector_new (config); |
---|
242 | gnome_print_config_unref (config); |
---|
243 | |
---|
244 | return gpw; |
---|
245 | } |
---|
246 | |
---|
247 | GtkWidget * |
---|
248 | gnome_printer_selector_new (GnomePrintConfig *config) |
---|
249 | { |
---|
250 | GnomePrinterSelector *gpw; |
---|
251 | |
---|
252 | g_return_val_if_fail (config != NULL, NULL); |
---|
253 | |
---|
254 | gpw = (GnomePrinterSelector *) gpa_widget_new (GNOME_TYPE_PRINTER_SELECTOR, config); |
---|
255 | |
---|
256 | gpa_widget_construct (GPA_WIDGET (gpw->printers), config); |
---|
257 | gpa_widget_construct (GPA_WIDGET (gpw->settings), config); |
---|
258 | gpa_widget_construct (GPA_WIDGET (gpw->transport), config); |
---|
259 | |
---|
260 | return GTK_WIDGET (gpw); |
---|
261 | } |
---|
262 | |
---|
263 | GnomePrintConfig * |
---|
264 | gnome_printer_selector_get_config (GnomePrinterSelector *widget) |
---|
265 | { |
---|
266 | GPAWidget *gpaw; |
---|
267 | |
---|
268 | g_return_val_if_fail (widget != NULL, NULL); |
---|
269 | g_return_val_if_fail (GNOME_IS_PRINTER_SELECTOR (widget), NULL); |
---|
270 | |
---|
271 | gpaw = GPA_WIDGET (widget); |
---|
272 | |
---|
273 | if (gpaw->config) |
---|
274 | gnome_print_config_ref (gpaw->config); |
---|
275 | |
---|
276 | return gpaw->config; |
---|
277 | } |
---|
278 | |
---|
279 | static GtkWidget * |
---|
280 | gpw_create_label_with_mnemonic (GtkTable *table, gint l, gint r, gint t, gint b, const gchar *text, |
---|
281 | GtkWidget *mnemonic_widget, unsigned y_pad) |
---|
282 | { |
---|
283 | GtkWidget *w = gtk_label_new_with_mnemonic (text); |
---|
284 | gtk_widget_show (w); |
---|
285 | gtk_misc_set_alignment (GTK_MISC (w), .0, 0.5); |
---|
286 | gtk_table_attach (table, w, l, r, t, b, GTK_FILL, GTK_FILL | GTK_EXPAND, GPS_PAD, y_pad); |
---|
287 | gtk_label_set_mnemonic_widget ((GtkLabel *) w, mnemonic_widget); |
---|
288 | |
---|
289 | return w; |
---|
290 | } |
---|
291 | |
---|
292 | static void |
---|
293 | gpw_configure_clicked (GtkWidget *widget, GPAWidget *gpaw) |
---|
294 | { |
---|
295 | GnomePrinterSelector *gpw = GNOME_PRINTER_SELECTOR (gpaw); |
---|
296 | GnomePrintConfig *gp_conf = gnome_printer_selector_get_config (gpw); |
---|
297 | GtkWidget * gpcd = gnome_print_config_dialog_new (gp_conf); |
---|
298 | |
---|
299 | gnome_print_config_unref (gp_conf); |
---|
300 | |
---|
301 | while (gtk_widget_get_parent (widget)) |
---|
302 | widget = gtk_widget_get_parent (widget); |
---|
303 | |
---|
304 | gtk_window_set_transient_for (GTK_WINDOW(gpcd), GTK_WINDOW(widget)); |
---|
305 | |
---|
306 | gtk_widget_show (gpcd); |
---|
307 | |
---|
308 | gtk_dialog_run (GTK_DIALOG (gpcd)); |
---|
309 | |
---|
310 | gtk_widget_destroy (GTK_WIDGET (gpcd)); |
---|
311 | } |
---|
312 | |
---|
313 | static void |
---|
314 | gpw_add_clicked (GtkWidget *btn, GPAWidget *gpaw) |
---|
315 | { |
---|
316 | static char *argv[] = { ADD_PRINTER_APP, NULL }; |
---|
317 | GError *err = NULL; |
---|
318 | g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, |
---|
319 | NULL, NULL, NULL, &err); |
---|
320 | if (err != NULL) { |
---|
321 | GtkWidget *dialog = gtk_message_dialog_new ( |
---|
322 | (GtkWindow *)gtk_widget_get_toplevel (btn), |
---|
323 | GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, |
---|
324 | GTK_BUTTONS_CLOSE, |
---|
325 | "Unable to launch " ADD_PRINTER_APP " : %s", err->message); |
---|
326 | |
---|
327 | g_signal_connect_swapped (GTK_OBJECT (dialog), "response", |
---|
328 | G_CALLBACK (gtk_widget_destroy), dialog); |
---|
329 | gtk_widget_show (dialog); |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | gboolean |
---|
334 | gnome_printer_selector_check_consistency (GnomePrinterSelector *psel) |
---|
335 | { |
---|
336 | return gpa_transport_selector_check_consistency |
---|
337 | (GPA_TRANSPORT_SELECTOR (psel->transport)); |
---|
338 | } |
---|