Debian Codegraph @

src/modules/frontend/bogl/bogl.c

raw
1 #include "common.h"
2 #include "template.h"
3 #include "question.h"
4 #include "frontend.h"
5 #include "database.h"
6 #include "strutl.h"
7
8 #include <bogl/bogl.h>
9 #include <bogl/bowl.h>
10
11 /* Any private variables we may need. */
12 struct uidata {
13 };
14
15 typedef int (*handler_t)(struct frontend *ui, struct question *q);
16
17 struct question_handlers {
18 const char *type;
19 handler_t handler;
20 };
21
22 int bogl_handler_boolean(struct frontend *ui, struct question *q);
23 int bogl_handler_multiselect(struct frontend *ui, struct question *q);
24 int bogl_handler_select(struct frontend *ui, struct question *q);
25 int bogl_handler_note(struct frontend *ui, struct question *q);
26 int bogl_handler_string(struct frontend *ui, struct question *q);
27
28 static struct question_handlers question_handlers[] = {
29 { "boolean", bogl_handler_boolean },
30 { "multiselect", bogl_handler_multiselect },
31 { "select", bogl_handler_select },
32 { "note", bogl_handler_note },
33 { "string", bogl_handler_string },
34 { "error", bogl_handler_note },
35 };
36
37 static const char *
38 get_text(struct frontend *obj, const char *template, const char *fallback )
39 {
40 struct question *q = obj->qdb->methods.get(obj->qdb, template);
41 return q ? q_get_description(obj, q) : fallback;
42 }
43
44 static handler_t handler(const char *type)
45 {
46 int i;
47 for(i = 0; i < DIM(question_handlers); i++)
48 if(! strcasecmp(type, question_handlers[i].type))
49 return question_handlers[i].handler;
50 return NULL;
51 }
52
53 /* FIXME: Needs disabled button widget. */
54 static void drawnavbuttons(struct frontend *ui, struct question *q)
55 {
56 if(ui->methods.can_go_back(ui, q))
57 bowl_new_button(get_text(ui, "debconf/button-goback", "Go Back"), DC_GOBACK);
58 if(ui->methods.can_go_forward(ui, q))
59 bowl_new_button(get_text(ui, "debconf/button-continue", "Next"), DC_OK);
60 }
61
62 static void drawdesctop(struct frontend *ui, struct question *q)
63 {
64 bowl_title(ui->title);
65 bowl_new_text(q_get_description(ui, q));
66 }
67
68 static void drawdescbot(struct frontend *ui, struct question *q)
69 {
70 bowl_new_text(q_get_extended_description(ui, q));
71 }
72
73
74 /* boolean requires a new widget, the radio button :( */
75 /* Pretend with buttons - loses default info. */
76 int bogl_handler_boolean(struct frontend *ui, struct question *q)
77 {
78 /* Should just make bowl_new_checkbox be properly const-qualified. */
79 char *desc = strdup(q_get_description(ui, q));
80 int ret;
81
82 #if 0
83 char ans = ' ';
84
85 if(q->value && *(q->value))
86 ans = (strcmp(q->value, "true") == 0) ? '*' : ' ';
87 #endif
88
89 bowl_flush();
90 drawdesctop(ui, q);
91
92 /* Should be: bowl_new_radio(); drawnavbuttons(ui, q); */
93 if(ui->methods.can_go_back(ui, q))
94 bowl_new_button(get_text(ui, "debconf/button-goback", "Go Back"), 0);
95 bowl_new_button(get_text(ui, "debconf/button-yes", "Yes"), 1);
96 bowl_new_button(get_text(ui, "debconf/button-no", "No"), 2);
97
98 drawdescbot(ui, q);
99 bowl_layout();
100 ret = bowl_run ();
101
102 if(ret != 0)
103 question_setvalue(q, (ret == 1) ? "true" : "false");
104
105 free(desc);
106 return (ret == 0) ? DC_GOBACK : DC_OK;
107 }
108
109 int bogl_handler_note(struct frontend *ui, struct question *q)
110 {
111 bowl_flush();
112 drawdesctop(ui, q);
113 drawnavbuttons(ui, q);
114 drawdescbot(ui, q);
115 bowl_layout();
116 return bowl_run();
117 }
118
119 int bogl_handler_select(struct frontend *ui, struct question *q)
120 {
121 return DC_OK;
122 }
123
124 int bogl_handler_multiselect(struct frontend *ui, struct question *q)
125 {
126 char **choices, **choices_translated, **defaults, *selected;
127 int i, j, count, dcount, ret;
128 const char *p;
129
130 count = 0;
131 p = q_get_choices_vals(ui, q);
132 if (*p)
133 {
134 count++;
135 for(; *p; p++)
136 if(*p == ',')
137 count++;
138 }
139
140 if (count <= 0) return DC_NOTOK;
141
142 choices = malloc(sizeof(char *) * count);
143 strchoicesplit(q_get_choices_vals(ui, q), choices, count);
144 choices_translated = malloc(sizeof(char *) * count);
145 strchoicesplit(q_get_choices(ui, q), choices_translated, count);
146 selected = malloc(sizeof(char) * count);
147 memset(selected, ' ', count);
148
149 dcount = 1;
150 for(p = question_get_field(ui, q, "C", "value"); *p; p++)
151 if(*p == ',')
152 dcount++;
153 defaults = malloc(sizeof(char *) * dcount);
154 dcount = strchoicesplit(question_get_field(ui, q, "C", "value"), defaults, dcount);
155 for(j = 0; j < dcount; j++)
156 {
157 for(i = 0; i < count; i++)
158 if(strcmp(choices[i], defaults[j]) == 0)
159 {
160 selected[i] = '*';
161 break;
162 }
163 free(defaults[j]);
164 }
165 free(defaults);
166
167 bowl_flush();
168 drawdesctop(ui, q);
169 bowl_new_checkbox(choices, selected, count, (count > 15) ? 15 : count);
170 drawnavbuttons(ui, q);
171 drawdescbot(ui, q);
172
173 bowl_layout();
174 ret = bowl_run();
175
176 if(ret == DC_OK)
177 {
178 /* Be safe - allow for commas and spaces. */
179 char *answer = malloc(strlen(q_get_choices(ui, q)) + 1 + count);
180 answer[0] = 0;
181 for(i = 0; i < count; i++)
182 if (selected[i] == '*')
183 {
184 if(answer[0] != 0)
185 strcat(answer, ", ");
186 strcat(answer, choices_translated[i]);
187 }
188 question_setvalue(q, answer);
189 }
190
191 for(i = 0; i < count; i++)
192 free(choices[i]);
193 free(choices);
194
195 return ret;
196 }
197
198 int bogl_handler_string(struct frontend *ui, struct question *q)
199 {
200 char *s;
201 int ret;
202
203 bowl_flush();
204 drawdesctop(ui, q);
205 bowl_new_input(&s, question_get_field(ui, q, "C", "value"));
206 drawnavbuttons(ui, q);
207 drawdescbot(ui, q);
208 bowl_layout();
209 ret = bowl_run();
210
211 if(ret == DC_OK)
212 question_setvalue(q, s);
213 free(s);
214
215 return ret;
216 }
217
218 static int bogl_initialize(struct frontend *ui, struct configuration *cfg)
219 {
220 ui->interactive = 1;
221 ui->data = NULL;
222
223 bowl_init();
224
225 bowl_done ();
226
227 return DC_OK;
228 }
229
230 static int bogl_shutdown(struct frontend *ui)
231 {
232 bowl_done();
233
234 return DC_OK;
235 }
236
237 static int bogl_go(struct frontend *ui)
238 {
239 struct question *q = ui->questions;
240 int ret;
241
242 bowl_init ();
243
244 while(q)
245 {
246 handler_t hdl;
247 ret = DC_OK;
248 hdl = handler(q->template->type);
249 if (hdl == NULL)
250 return DC_NOTIMPL;
251 (*hdl)(ui, q);
252 if(ret == DC_OK)
253 frontend_qdb_set(ui->qdb, q, 0);
254 else
255 return ret;
256
257 q = q->next;
258 }
259
260 bowl_done ();
261
262 return DC_OK;
263 }
264
265 struct frontend_module debconf_frontend_module = {
266 initialize: bogl_initialize,
267 shutdown: bogl_shutdown,
268 go: bogl_go,
269 };