Debian Codegraph @

menuselect/menuselect_newt.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2008 Sean Bright
5 *
6 * Sean Bright <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 *
22 * \author Sean Bright <[email protected]>
23 *
24 * \brief newt frontend for selection maintenance
25 */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <newt.h>
31
32 #include "menuselect.h"
33
34 #define MIN_X 80
35 #define MIN_Y 21
36
37 #define MIN(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a > __b) ? __b : __a);})
38 #define MAX(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a < __b) ? __b : __a);})
39
40 extern int changes_made;
41
42 static newtComponent rootOptions;
43 static newtComponent subOptions;
44
45 static newtComponent memberNameTextbox;
46 static newtComponent dependsLabel;
47 static newtComponent usesLabel;
48 static newtComponent conflictsLabel;
49 static newtComponent supportLevelLabel;
50 static newtComponent dependsDataTextbox;
51 static newtComponent usesDataTextbox;
52 static newtComponent conflictsDataTextbox;
53 static newtComponent supportLevelDataTextbox;
54
55 static newtComponent exitButton;
56 static newtComponent saveAndExitButton;
57
58 static void build_members_menu(int overlay);
59 static void root_menu_callback(newtComponent component, void *data);
60
61 static void toggle_all_options(int select)
62 {
63 struct category *cat = newtListboxGetCurrent(rootOptions);
64
65 set_all(cat, select);
66
67 /* Redraw */
68 build_members_menu(1);
69
70 return;
71 }
72
73 static void toggle_selected_option()
74 {
75 int i;
76 struct member *mem = newtListboxGetCurrent(subOptions);
77
78 toggle_enabled(mem);
79
80 /* Redraw */
81 build_members_menu(1);
82
83 /* Select the next item in the list */
84 for (i = 0; i < newtListboxItemCount(subOptions); i++) {
85 struct member *cur;
86
87 newtListboxGetEntry(subOptions, i, NULL, (void **) &cur);
88
89 if (cur == mem) {
90 i = MIN(i + 1, newtListboxItemCount(subOptions) - 1);
91 break;
92 }
93 }
94
95 newtListboxSetCurrent(subOptions, i);
96
97 return;
98 }
99
100 static void reset_display()
101 {
102 newtTextboxSetText(memberNameTextbox, "");
103 newtTextboxSetText(dependsDataTextbox, "");
104 newtTextboxSetText(usesDataTextbox, "");
105 newtTextboxSetText(conflictsDataTextbox, "");
106 newtTextboxSetText(supportLevelDataTextbox, "");
107 newtRefresh();
108 }
109
110 static void display_member_info(struct member *mem)
111 {
112 char buffer[128] = { 0 };
113 char buf2[64];
114
115 struct reference *dep;
116 struct reference *con;
117 struct reference *uses;
118
119 reset_display();
120
121 if (mem->displayname) {
122 newtTextboxSetText(memberNameTextbox, mem->displayname);
123 }
124
125 if (AST_LIST_EMPTY(&mem->deps)) {
126 if (mem->is_separator) {
127 newtTextboxSetText(dependsDataTextbox, "");
128 } else {
129 newtTextboxSetText(dependsDataTextbox, "N/A");
130 }
131 } else {
132 strcpy(buffer, "");
133 AST_LIST_TRAVERSE(&mem->deps, dep, list) {
134 strncat(buffer, dep->displayname, sizeof(buffer) - strlen(buffer) - 1);
135 strncat(buffer, dep->member ? "(M)" : "(E)", sizeof(buffer) - strlen(buffer) - 1);
136 if (AST_LIST_NEXT(dep, list))
137 strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1);
138 }
139 newtTextboxSetText(dependsDataTextbox, buffer);
140 }
141
142 if (AST_LIST_EMPTY(&mem->uses)) {
143 if (mem->is_separator) {
144 newtTextboxSetText(usesDataTextbox, "");
145 } else {
146 newtTextboxSetText(usesDataTextbox, "N/A");
147 }
148 } else {
149 strcpy(buffer, "");
150 AST_LIST_TRAVERSE(&mem->uses, uses, list) {
151 strncat(buffer, uses->displayname, sizeof(buffer) - strlen(buffer) - 1);
152 strncat(buffer, uses->member ? "(M)" : "(E)", sizeof(buffer) - strlen(buffer) - 1);
153 if (AST_LIST_NEXT(uses, list))
154 strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1);
155 }
156 newtTextboxSetText(usesDataTextbox, buffer);
157 }
158
159 if (AST_LIST_EMPTY(&mem->conflicts)) {
160 if (!mem->is_separator) {
161 newtTextboxSetText(conflictsDataTextbox, "N/A");
162 } else {
163 newtTextboxSetText(conflictsDataTextbox, "");
164 }
165 } else {
166 strcpy(buffer, "");
167 AST_LIST_TRAVERSE(&mem->conflicts, con, list) {
168 strncat(buffer, con->displayname, sizeof(buffer) - strlen(buffer) - 1);
169 strncat(buffer, con->member ? "(M)" : "(E)", sizeof(buffer) - strlen(buffer) - 1);
170 if (AST_LIST_NEXT(con, list))
171 strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1);
172 }
173 newtTextboxSetText(conflictsDataTextbox, buffer);
174 }
175
176 { /* Support Level */
177 snprintf(buffer, sizeof(buffer), "%s", mem->support_level);
178 if (mem->replacement && *mem->replacement) {
179 snprintf(buf2, sizeof(buf2), ", Replaced by: %s", mem->replacement);
180 strncat(buffer, buf2, sizeof(buffer) - strlen(buffer) - 1);
181 }
182 if (mem->deprecated_in && *mem->deprecated_in) {
183 snprintf(buf2, sizeof(buf2), ", Deprecated in: %s", mem->deprecated_in);
184 strncat(buffer, buf2, sizeof(buffer) - strlen(buffer) - 1);
185 }
186 if (mem->removed_in && *mem->removed_in) {
187 snprintf(buf2, sizeof(buf2), ", Removed in: %s", mem->removed_in);
188 strncat(buffer, buf2, sizeof(buffer) - strlen(buffer) - 1);
189 }
190 if (mem->is_separator) {
191 newtTextboxSetText(supportLevelDataTextbox, "");
192 } else {
193 newtTextboxSetText(supportLevelDataTextbox, buffer);
194 }
195 }
196 }
197
198 static void build_members_menu(int overlay)
199 {
200 struct category *cat;
201 struct member *mem;
202 char buf[64];
203 int i = 0;
204
205 if (!overlay) {
206 reset_display();
207 newtListboxClear(subOptions);
208 }
209
210 cat = newtListboxGetCurrent(rootOptions);
211
212 AST_LIST_TRAVERSE(&cat->members, mem, list) {
213
214 if ((mem->depsfailed == HARD_FAILURE) || (mem->conflictsfailed == HARD_FAILURE)) {
215 snprintf(buf, sizeof(buf), "XXX %s", mem->name);
216 } else if (mem->is_separator) {
217 snprintf(buf, sizeof(buf), " --- %s ---", mem->name);
218 } else if (mem->depsfailed == SOFT_FAILURE) {
219 snprintf(buf, sizeof(buf), "<%s> %s", mem->enabled ? "*" : " ", mem->name);
220 } else if (mem->conflictsfailed == SOFT_FAILURE) {
221 snprintf(buf, sizeof(buf), "(%s) %s", mem->enabled ? "*" : " ", mem->name);
222 } else {
223 snprintf(buf, sizeof(buf), "[%s] %s", mem->enabled ? "*" : " ", mem->name);
224 }
225
226 if (overlay) {
227 newtListboxSetEntry(subOptions, i, buf);
228 } else {
229 newtListboxAppendEntry(subOptions, buf, mem);
230 }
231
232 i++;
233 }
234
235 if (!overlay) {
236 display_member_info(AST_LIST_FIRST(&cat->members));
237 }
238
239 return;
240 }
241
242 static void build_main_menu()
243 {
244 struct category *cat;
245 char buf[64];
246 int i = 1;
247
248 newtListboxClear(rootOptions);
249
250 AST_LIST_TRAVERSE(&categories, cat, list) {
251 if (!strlen_zero(cat->displayname))
252 snprintf(buf, sizeof(buf), " %s ", cat->displayname);
253 else
254 snprintf(buf, sizeof(buf), " %s ", cat->name);
255
256 newtListboxAppendEntry(rootOptions, buf, cat);
257
258 i++;
259 }
260 }
261
262 static void category_menu_callback(newtComponent component, void *data)
263 {
264 display_member_info(newtListboxGetCurrent(subOptions));
265 }
266
267 static void root_menu_callback(newtComponent component, void *data)
268 {
269 build_members_menu(0);
270 }
271
272 int run_confirmation_dialog(int *result)
273 {
274 int res = newtWinTernary("Are You Sure?", "Discard changes & Exit", "Save & Exit", "Cancel",
275 "It appears you have made some changes, and you have opted to Quit "
276 "without saving these changes. Please choose \"Discard changes & Exit\" to exit "
277 "without saving; Choose \"Cancel\" to cancel your decision to quit, and keep "
278 "working in menuselect, or choose \"Save & Exit\" to save your changes, and exit.");
279
280 switch (res) {
281 case 1:
282 /* Discard and exit */
283 *result = -1;
284 return 1;
285 case 2:
286 /* Save and exit */
287 *result = 0;
288 return 1;
289 case 3:
290 /* They either chose "No" or they hit F12 */
291 default:
292 *result = -1;
293 return 0;
294 }
295 }
296
297 int run_menu(void)
298 {
299 struct newtExitStruct es;
300 newtComponent form;
301 int x = 0, y = 0, res = 0;
302
303 newtInit();
304 newtCls();
305 newtGetScreenSize(&x, &y);
306
307 if (x < MIN_X || y < MIN_Y) {
308 newtFinished();
309 fprintf(stderr, "Terminal must be at least %d x %d.\n", MIN_X, MIN_Y);
310 return -1;
311 }
312
313 newtPushHelpLine(" <ENTER> toggles selection | <F12> saves & exits | <ESC> exits without save");
314 newtRefresh();
315
316 newtCenteredWindow(x - 8, y - 7, menu_name);
317 form = newtForm(NULL, NULL, 0);
318
319 /* F8 for select all */
320 newtFormAddHotKey(form, NEWT_KEY_F8);
321
322 /* F7 for deselect all */
323 newtFormAddHotKey(form, NEWT_KEY_F7);
324
325 newtFormSetTimer(form, 200);
326
327 rootOptions = newtListbox(2, 1, y - 15, 0);
328 newtListboxSetWidth(rootOptions, 34);
329 newtFormAddComponent(form, rootOptions);
330 newtComponentAddCallback(rootOptions, root_menu_callback, NULL);
331
332 subOptions = newtListbox(38, 1, y - 15, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
333 newtListboxSetWidth(subOptions, x - 47);
334 newtFormAddComponent(form, subOptions);
335 newtComponentAddCallback(subOptions, category_menu_callback, NULL);
336
337 memberNameTextbox = newtTextbox(2, y - 13, x - 10, 2, NEWT_FLAG_WRAP);
338 dependsLabel = newtLabel(2, y - 11, " Depends on:");
339 usesLabel = newtLabel(2, y - 10, " Can use:");
340 conflictsLabel = newtLabel(2, y - 9, "Conflicts with:");
341 supportLevelLabel = newtLabel(2, y - 8, " Support Level:");
342 dependsDataTextbox = newtTextbox(18, y - 11, x - 27, 1, 0);
343 usesDataTextbox = newtTextbox(18, y - 10, x - 27, 1, 0);
344 conflictsDataTextbox = newtTextbox(18, y - 9, x - 27, 1, 0);
345 supportLevelDataTextbox = newtTextbox(18, y - 8, x - 27, 1, 0);
346
347 exitButton = newtButton(x - 23, y - 11, " Exit ");
348 saveAndExitButton = newtButton(x - 43, y - 11, " Save & Exit ");
349
350 newtFormAddComponents(
351 form,
352 memberNameTextbox,
353 dependsLabel,
354 dependsDataTextbox,
355 usesLabel,
356 usesDataTextbox,
357 conflictsLabel,
358 conflictsDataTextbox,
359 supportLevelLabel,
360 supportLevelDataTextbox,
361 saveAndExitButton,
362 exitButton,
363 NULL);
364
365 build_main_menu();
366
367 root_menu_callback(rootOptions, AST_LIST_FIRST(&categories));
368
369 for (;;) {
370 do {
371 newtFormRun(form, &es);
372 } while (es.reason == NEWT_EXIT_TIMER);
373
374 if (es.reason == NEWT_EXIT_HOTKEY) {
375 int done = 1;
376
377 switch (es.u.key) {
378 case NEWT_KEY_F12:
379 res = 0;
380 break;
381 case NEWT_KEY_F7:
382 toggle_all_options(0);
383 done = 0;
384 break;
385 case NEWT_KEY_F8:
386 toggle_all_options(1);
387 done = 0;
388 break;
389 case NEWT_KEY_ESCAPE:
390 if (changes_made) {
391 done = run_confirmation_dialog(&res);
392 } else {
393 res = -1;
394 }
395 break;
396 default:
397 done = 0;
398 break;
399 }
400
401 if (done) {
402 break;
403 }
404 } else if (es.reason == NEWT_EXIT_COMPONENT) {
405 if (es.u.co == saveAndExitButton) {
406 res = 0;
407 break;
408 } else if (es.u.co == exitButton) {
409 int done = 1;
410
411 if (changes_made) {
412 done = run_confirmation_dialog(&res);
413 } else {
414 res = -1;
415 }
416
417 if (done) {
418 break;
419 }
420 } else if (es.u.co == subOptions) {
421 toggle_selected_option();
422 }
423 }
424 }
425
426 /* Cleanup */
427 reset_display();
428 newtFormDestroy(form);
429 newtPopWindow();
430 newtPopHelpLine();
431 newtCls();
432 newtFinished();
433
434 return res;
435 }