Debian Codegraph @

src/modules/frontend/gtk/go.c

raw
1 /*****************************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * cdebconf is (c) 2000-2007 Randolph Chung and others under the following
7 * license.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *****************************************************************************/
33
34 /** @file go.c
35 * GO support for the GTK+ frontend of cdebconf
36 */
37
38 #include "go.h"
39
40 #include <string.h>
41 #include <gtk/gtk.h>
42 #include <gdk/gdkkeysyms.h>
43
44 #include "frontend.h"
45 #include "question.h"
46 #include "template.h"
47 #include "plugin.h"
48 #include "database.h"
49
50 #include "cdebconf_gtk.h"
51 #include "fe_data.h"
52 #include "ui.h"
53 #include "handlers.h"
54 #include "select_handlers.h"
55 #include "progress.h"
56 #ifdef DI_UDEB
57 # include "di.h"
58 # include "screenshot.h"
59 #endif
60
61 /** Store callbacks from question handlers run after "Continue".
62 *
63 * This structure form a single linked list of setters to be ran.
64 */
65 struct setter {
66 /** Callback function.
67 *
68 * This function should update the debconf database with the value entered
69 * by the user. */
70 setter_function func;
71
72 /** Question for which this setter was registered. */
73 struct question * question;
74
75 /** Question handler private data. */
76 void * user_data;
77
78 /** Next setter. */
79 struct setter * next;
80 };
81
82 /** Register the given setter.
83 *
84 * The setter will be prepended to the setters list.
85 *
86 * @param fe cdebconf frontend
87 * @param func function updating the debconf database with user input
88 * @param question question that will be updated
89 * @param user_data question handler private data
90 */
91 void cdebconf_gtk_register_setter(struct frontend * fe, setter_function func,
92 struct question * question, void * user_data)
93 {
94 struct frontend_data * fe_data = fe->data;
95 struct setter * setter;
96
97 setter = g_malloc0(sizeof (struct setter));
98 setter->func = func;
99 setter->question = question;
100 setter->user_data = user_data;
101 setter->next = fe_data->setters;
102 fe_data->setters = setter;
103 }
104
105 /** Call all registered setters.
106 *
107 * @param fe cdebconf frontend
108 */
109 static void call_setters(struct frontend * fe)
110 {
111 struct frontend_data * fe_data = fe->data;
112 struct setter * setter;
113
114 setter = fe_data->setters;
115 while (NULL != setter) {
116 (*setter->func)(setter->question, setter->user_data);
117 setter = setter->next;
118 }
119 }
120
121 /** Free all registered setters.
122 *
123 * @param fe_data the GTK+ frontend data
124 */
125 static void free_setters(struct frontend_data * fe_data)
126 {
127 struct setter * setter;
128 struct setter * previous;
129
130 setter = fe_data->setters;
131 while (NULL != setter) {
132 previous = setter;
133 setter = setter->next;
134 g_free(previous);
135 }
136 fe_data->setters = NULL;
137 }
138
139 /** Key event handler implementing the "Help" key shortcut.
140 *
141 * @param widget main widget
142 * @param key pressed key
143 * @param fe cdebconf frontend
144 * @return TRUE if "Help" shortcut has been pressed, FALSE otherwise
145 */
146 static gboolean handle_help_key(GtkWidget * widget, GdkEventKey * key,
147 struct frontend * fe)
148 {
149 if (GDK_KEY_F1 == key->keyval || GDK_KEY_KP_F1 == key->keyval) {
150 cdebconf_gtk_help(fe);
151 return TRUE;
152 }
153 return FALSE;
154 }
155
156 /** Create the "Help" button.
157 *
158 * The button will be added to the main action box.
159 *
160 * @param fe cdebconf frontend
161 */
162 static void create_help_button(struct frontend * fe)
163 {
164 GtkWidget * button;
165 char * label;
166
167 label = cdebconf_gtk_get_text(fe, "debconf/button-help", "Help");
168 button = gtk_button_new_with_label(label);
169 g_free(label);
170
171 g_signal_connect_swapped(G_OBJECT(button), "clicked",
172 G_CALLBACK(cdebconf_gtk_help), fe);
173
174 cdebconf_gtk_add_button(fe, button);
175 cdebconf_gtk_set_button_secondary(fe, button, TRUE);
176 cdebconf_gtk_add_global_key_handler(
177 fe, button, G_CALLBACK(handle_help_key));
178 }
179
180 /** Key event handler implementing the "Go Back" key shortcut.
181 *
182 * @param widget main window
183 * @param key pressed key
184 * @param fe cdebconf frontend
185 * @return TRUE if "Go Back" shortcut has been pressed, FALSE otherwise
186 */
187 static gboolean handle_goback_key(GtkWidget * widget, GdkEventKey * key,
188 struct frontend * fe)
189 {
190 if (GDK_KEY_Escape == key->keyval) {
191 cdebconf_gtk_set_answer_goback(fe);
192 return TRUE;
193 }
194 return FALSE;
195 }
196
197 /** Create the "Go Back" button.
198 *
199 * The button will be added to the main action box.
200 *
201 * @param fe cdebconf frontend
202 */
203 static void create_goback_button(struct frontend * fe)
204 {
205 GtkWidget * button;
206 char * label;
207
208 label = cdebconf_gtk_get_text(fe, "debconf/button-goback", "Go Back");
209 button = gtk_button_new_with_label(label);
210 g_free(label);
211
212 g_signal_connect_swapped(G_OBJECT(button), "clicked",
213 G_CALLBACK(cdebconf_gtk_set_answer_goback), fe);
214
215 cdebconf_gtk_add_button(fe, button);
216 cdebconf_gtk_add_global_key_handler(
217 fe, button, G_CALLBACK(handle_goback_key));
218 }
219
220 /* documented in cdebconf_gtk.h */
221 GtkWidget * cdebconf_gtk_create_continue_button(struct frontend * fe)
222 {
223 struct frontend_data * fe_data = fe->data;
224 GtkWidget * action_box = fe_data->action_box;
225 GList * focus_chain;
226 GtkWidget * button;
227 char * label;
228
229 label = cdebconf_gtk_get_text(fe, "debconf/button-continue", "Continue");
230 button = gtk_button_new_with_label(label);
231 g_free(label);
232
233 g_signal_connect_swapped(G_OBJECT(button), "clicked",
234 G_CALLBACK(cdebconf_gtk_set_answer_ok), fe);
235 gtk_box_pack_start(GTK_BOX(action_box), button,
236 TRUE /* expand */, TRUE /* fill */, DEFAULT_PADDING);
237 gtk_container_get_focus_chain(GTK_CONTAINER(action_box), &focus_chain);
238 focus_chain = g_list_prepend(focus_chain, button);
239 gtk_container_set_focus_chain(GTK_CONTAINER(action_box), focus_chain);
240 g_list_free(focus_chain);
241
242 gtk_widget_set_can_default(GTK_WIDGET(button), TRUE);
243 gtk_widget_grab_default(GTK_WIDGET(button));
244
245 return button;
246 }
247
248 /** Test if the action box contains action buttons.
249 *
250 * The screenshot button and "Go back" button are not considered as action
251 * buttons.
252 *
253 * @param fe cdebconf frontend
254 * @returns TRUE if there already is action buttons, FALSE otherwise.
255 */
256 static gboolean is_action_box_filled(struct frontend * fe)
257 {
258 struct frontend_data * fe_data = fe->data;
259 GtkContainer * action_box = GTK_CONTAINER(fe_data->action_box);
260 GList * children;
261 gboolean filled;
262
263 children = gtk_container_get_children(action_box);
264 if (CAN_GO_BACK(fe)) {
265 #ifdef DI_UDEB
266 /* screenshot + go back button */
267 filled = 2 != g_list_length(children);
268 #else
269 /* go back button */
270 filled = 1 != g_list_length(children);
271 #endif
272 } else {
273 #ifdef DI_UDEB
274 /* go back button */
275 filled = 1 != g_list_length(children);
276 #else
277 /* no buttons */
278 filled = 0 != g_list_length(children);
279 #endif
280 }
281 g_list_free(children);
282 return filled;
283 }
284
285 /** Create default buttons in the action box.
286 *
287 * Default buttons currently means the "Continue" button.
288 *
289 * @param fe cdebconf frontend
290 */
291 static void create_default_buttons(struct frontend * fe)
292 {
293 (void) cdebconf_gtk_create_continue_button(fe);
294 }
295
296 /** Destroy all buttons in the main action box.
297 *
298 * @param fe cdebconf frontend
299 */
300 static void destroy_buttons(struct frontend * fe)
301 {
302 struct frontend_data * fe_data = fe->data;
303 GtkContainer * action_box = GTK_CONTAINER(fe_data->action_box);
304
305 gtk_container_foreach(action_box, cdebconf_gtk_callback_widget_destroy,
306 NULL /* no user_data */);
307 }
308
309 /** List of internal question handlers.
310 */
311 static const struct {
312 /** Question type */
313 const char * type;
314 /** Handler function for the corresponding type. */
315 cdebconf_gtk_handler handler;
316 } question_handlers[] = {
317 { "boolean", cdebconf_gtk_handle_boolean },
318 { "multiselect", cdebconf_gtk_handle_multiselect },
319 { "note", cdebconf_gtk_handle_note },
320 { "password", cdebconf_gtk_handle_password },
321 { "select", cdebconf_gtk_handle_select },
322 { "string", cdebconf_gtk_handle_string },
323 { "error", cdebconf_gtk_handle_note },
324 { "text", cdebconf_gtk_handle_text },
325 { "", NULL },
326 };
327
328 /** Find a internal handler for the given question type.
329 *
330 * @param type the question type
331 * @return the corresponding handler if found, NULL otherwise
332 */
333 static cdebconf_gtk_handler find_internal_handler(const char * type)
334 {
335 int i;
336
337 for (i = 0; NULL != question_handlers[i].handler; i++) {
338 if (0 == strcmp(type, question_handlers[i].type)) {
339 return question_handlers[i].handler;
340 }
341 }
342 return NULL;
343 }
344
345 /** Find an external handler (plugin) for the given question type.
346 *
347 * This will save the plugin in a cache for future use.
348 *
349 * @param fe cdebconf frontend
350 * @param type the question type
351 * @return the corresponding handler if found, NULL otherwise
352 * @see frontend_data#plugins
353 */
354 static cdebconf_gtk_handler find_external_handler(struct frontend * fe,
355 const char * type)
356 {
357 struct frontend_data * fe_data = fe->data;
358 struct plugin * plugin;
359
360 /* Look in already loaded plugins first. */
361 plugin = g_hash_table_lookup(fe_data->plugins, type);
362 if (NULL == plugin) {
363 /* Load if available */
364 if (NULL == (plugin = plugin_find(fe, type))) {
365 g_warning("No plugin for %s", type);
366 return NULL;
367 }
368 /* Save for later use */
369 g_hash_table_insert(fe_data->plugins, g_strdup(type), plugin);
370 }
371 return (cdebconf_gtk_handler) plugin->handler;
372 }
373
374 /** Update the question database with the value currently set to each
375 * questions referenced by the frontend.
376 *
377 * @param fe cdebconf frontend
378 */
379 static void update_question_database(struct frontend * fe)
380 {
381 struct question * question;
382
383 question = fe->questions;
384 while (NULL != question) {
385 frontend_qdb_set(fe->qdb, question, 0);
386 question = question->next;
387 }
388 }
389
390 /** Call the relevant handlers for every questions currently referenced
391 * by the frontend.
392 *
393 * Handlers are expected to add their widgets in the given question box.
394 *
395 * @param fe cdebconf frontend
396 * @param question_box container for question handler widgets
397 * @return DC_NOTIMPL if no handlers are available, DC_NOTOK if an error
398 * happened, DC_OK otherwise
399 */
400 static int call_question_handlers(struct frontend * fe,
401 GtkWidget * question_box)
402 {
403 struct question * question;
404 cdebconf_gtk_handler handler;
405 int ret;
406
407 question = fe->questions;
408 while (NULL != question) {
409 handler = find_internal_handler(question->template->type);
410 if (NULL == handler) {
411 handler = find_external_handler(fe, question->template->type);
412 }
413 if (NULL == handler) {
414 return DC_NOTIMPL;
415 }
416 if (DC_OK != (ret = handler(fe, question, question_box))) {
417 g_warning("tag \"%s\" failed to display!", question->tag);
418 return ret;
419 }
420 question = question->next;
421 }
422 return DC_OK;
423 }
424
425 /** Create the container for question widgets in the given container.
426 *
427 * This will add the question box to the container on top of returning it.
428 *
429 * The actual container will vary if GO has been called for only one question
430 * or more: since all widgets used to display single questions have native
431 * scrolling capabilities or do not need scrolling (too small) they can manage
432 * scrolling be autonomously.
433 *
434 * Vice-versa the most simple approach when displaying multiple questions
435 * togheter (whose handling wigets haven't native scrolling capabilities) is to
436 * pack them all inside a viewport.
437 *
438 * @todo
439 * This currently breaks the focus chain for widgets not currently shown by the
440 * viewports. Not sure on how to fix that correctly.
441 *
442 * @param fe cdebconf frontend
443 * @param container the container in which the question box will be added
444 * @return the newly created question box
445 */
446 static GtkWidget * create_question_box(struct frontend * fe,
447 GtkWidget * container)
448 {
449 GtkWidget * question_box;
450 GtkWidget * question_box_scroll;
451 GtkWidget * hpadding_box;
452 GtkAdjustment *adj;
453
454 question_box = gtk_vbox_new(FALSE /* don't make children equal */,
455 0 /* padding */);
456
457 if (0 && IS_QUESTION_SINGLE(fe->questions)) {
458 gtk_box_pack_start(GTK_BOX(container), question_box,
459 TRUE /* expand */, TRUE /* fill */,
460 0 /* padding */);
461 } else {
462 hpadding_box = gtk_hbox_new(FALSE /* don't make children equal */,
463 0 /* padding */);
464 gtk_box_pack_start(GTK_BOX(hpadding_box), question_box,
465 TRUE /* expand */, TRUE /* fill */,
466 QUESTIONBOX_HPADDING);
467 question_box_scroll = gtk_scrolled_window_new(
468 NULL /* create horizontal adjustement */,
469 NULL /* create vertical adjustement */);
470 gtk_scrolled_window_add_with_viewport(
471 GTK_SCROLLED_WINDOW(question_box_scroll), hpadding_box);
472 gtk_scrolled_window_set_policy(
473 GTK_SCROLLED_WINDOW(question_box_scroll),
474 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
475 gtk_scrolled_window_set_shadow_type(
476 GTK_SCROLLED_WINDOW(question_box_scroll), GTK_SHADOW_NONE);
477 adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(question_box_scroll));
478 gtk_container_set_focus_vadjustment(GTK_CONTAINER(question_box), adj);
479 gtk_box_pack_start(GTK_BOX(container), question_box_scroll,
480 TRUE /* expand */, TRUE /* fill */,
481 DEFAULT_PADDING);
482 }
483 return question_box;
484 }
485
486 /** Wait for an answer to be set asynchronously, either by a button or a key
487 * shortcut.
488 *
489 * @param fe cdebconf frontend
490 * @see fe_data#answer
491 */
492 static void wait_answer(struct frontend * fe)
493 {
494 struct frontend_data * fe_data = fe->data;
495
496 g_mutex_lock(&(fe_data->answer_mutex));
497 while (DC_NO_ANSWER == fe_data->answer) {
498 g_cond_wait(&(fe_data->answer_cond), &(fe_data->answer_mutex));
499 }
500 g_mutex_unlock(&(fe_data->answer_mutex));
501 }
502
503 /** Implements the "go" method of cdebconf frontends.
504 *
505 * The processing is currently divided into three stages:
506 * - Setup widgets corresponding to questions asked:
507 * this is done by creating the question box and calling the
508 * various handlers to get the widgets added. Default action
509 * buttons are added along the way if no handlers created a
510 * specific button. The "Go Back" button is always added if
511 * supported by the frontend caller.
512 * - Wait for an answer.
513 * - Get the data back from the widgets and clean up:
514 * the "setters" registered by the various handlers are called
515 * to update the question database based on user input. The
516 * question box and buttons are then destroyed to leave room
517 * for the next frontend call.
518 *
519 * If a progress was running, the widgets are hidden at the beginning
520 * of the procedure and shown at the end.
521 *
522 * @param fe cdebconf frontend
523 * @return the debconf status corresponding to user actions
524 */
525 int cdebconf_gtk_go(struct frontend * fe)
526 {
527 struct frontend_data * fe_data = fe->data;
528 GtkWidget * question_box;
529 struct question * question;
530 int ret;
531
532 if (NULL == fe->questions) {
533 return DC_OK;
534 }
535
536 cdebconf_gtk_set_answer(fe, DC_NO_ANSWER);
537 fe_data->help_question = NULL;
538
539 gdk_threads_enter();
540 #ifdef DI_UDEB
541 /* XXX: rename */
542 cdebconf_gtk_di_run_dialog(fe);
543 cdebconf_gtk_create_screenshot_button(fe);
544 #endif /* DI_UDEB */
545 if (NULL != fe_data->progress_data) {
546 cdebconf_gtk_hide_progress(fe);
547 }
548 question_box = create_question_box(fe, fe_data->target_box);
549 /* Hide the target box while we create the questions to avoid flicker. */
550 cdebconf_gtk_hide_target_box(fe);
551 if (CAN_GO_BACK(fe)) {
552 create_goback_button(fe);
553 }
554 if (DC_OK != (ret = call_question_handlers(fe, question_box))) {
555 cdebconf_gtk_set_answer(fe, ret);
556 gdk_threads_leave();
557 goto end;
558 }
559 if (!is_action_box_filled(fe)) {
560 create_default_buttons(fe);
561 }
562
563 /* Set up the help button if necessary. FIXME: We only offer help for
564 * the first question with help text in any given question box. It's not
565 * clear what the right thing to do here is.
566 */
567 question = fe->questions;
568 while (NULL != question) {
569 char *help = q_get_help(fe, question);
570 if (help) {
571 struct question *help_q = fe->qdb->methods.get(fe->qdb, help);
572 if (help_q) {
573 fe_data->help_question = help_q;
574 create_help_button(fe);
575 break;
576 }
577 }
578 question = question->next;
579 }
580
581 cdebconf_gtk_show_target_box(fe);
582 cdebconf_gtk_show_buttons(fe);
583 gdk_threads_leave();
584
585 /* frontend blocked here until a button has been pressed */
586 wait_answer(fe);
587
588 if (DC_NOTOK == fe_data->answer) {
589 goto end;
590 }
591
592 gdk_threads_enter();
593 cdebconf_gtk_set_buttons_sensitive(fe, FALSE);
594 if (DC_OK == fe_data->answer) {
595 call_setters(fe);
596 update_question_database(fe);
597 }
598 cdebconf_gtk_empty_target_box(fe);
599 destroy_buttons(fe);
600 if (NULL != fe_data->progress_data) {
601 cdebconf_gtk_show_progress(fe);
602 }
603 gdk_threads_leave();
604
605 end:
606 question_deref(fe_data->help_question);
607 fe_data->help_question = NULL;
608 free_setters(fe_data);
609 return fe_data->answer;
610 }
611
612
613 /* vim: et sw=4 si
614 */