regedit: Simplify notice dialogs, don't overwrite existing values.
[Samba/bjacke.git] / source3 / utils / regedit_dialog.c
blob94c3f362bda8cf72c62f42f3afab1d6225946d53
1 /*
2 * Samba Unix/Linux SMB client library
3 * Registry Editor
4 * Copyright (C) Christopher Davis 2012
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "regedit_dialog.h"
22 #include "regedit_valuelist.h"
23 #include "util_reg.h"
24 #include "lib/registry/registry.h"
25 #include <stdarg.h>
26 #include <form.h>
28 static int dialog_free(struct dialog *dia)
30 if (dia->window) {
31 delwin(dia->window);
33 if (dia->sub_window) {
34 delwin(dia->sub_window);
36 if (dia->panel) {
37 del_panel(dia->panel);
39 if (dia->choices) {
40 unpost_menu(dia->choices);
41 free_menu(dia->choices);
43 if (dia->choice_items) {
44 ITEM **it;
45 for (it = dia->choice_items; *it != NULL; ++it) {
46 free_item(*it);
50 return 0;
53 struct dialog *dialog_new(TALLOC_CTX *ctx, const char *title, int nlines,
54 int ncols, int y, int x)
56 struct dialog *dia;
58 dia = talloc_zero(ctx, struct dialog);
59 if (dia == NULL) {
60 return NULL;
63 talloc_set_destructor(dia, dialog_free);
65 dia->window = newwin(nlines, ncols, y, x);
66 if (dia->window == NULL) {
67 goto fail;
70 box(dia->window, 0, 0);
71 mvwaddstr(dia->window, 0, 1, title);
73 /* body of the dialog within the box outline */
74 dia->sub_window = derwin(dia->window, nlines - 2, ncols - 2, 1, 1);
75 if (dia->sub_window == NULL) {
76 goto fail;
79 dia->panel = new_panel(dia->window);
80 if (dia->panel == NULL) {
81 goto fail;
84 return dia;
86 fail:
87 talloc_free(dia);
89 return NULL;
93 static void center_dialog_above_window(WINDOW *below, int *nlines, int *ncols,
94 int *y, int *x)
96 int maxy, maxx;
97 int centery, centerx;
99 getmaxyx(below, maxy, maxx);
101 centery = maxy / 2;
102 centerx = maxx / 2;
103 *y = 0;
104 *x = 0;
106 if (*nlines > maxy) {
107 *nlines = maxy;
109 if (*ncols > maxx) {
110 *ncols = maxx;
113 if (*nlines < centery) {
114 *y = centery - *nlines;
116 if (*ncols < centerx) {
117 *x = centerx - *ncols;
121 struct dialog *dialog_center_new(TALLOC_CTX *ctx, const char *title, int nlines,
122 int ncols, WINDOW *below)
124 int y, x;
126 center_dialog_above_window(below, &nlines, &ncols, &y, &x);
128 return dialog_new(ctx, title, nlines, ncols, y, x);
131 struct dialog *dialog_choice_new(TALLOC_CTX *ctx, const char *title,
132 const char **choices, int nlines,
133 int ncols, int y, int x)
135 size_t nchoices, i;
136 struct dialog *dia;
138 dia = dialog_new(ctx, title, nlines, ncols, y, x);
139 if (dia == NULL) {
140 return NULL;
143 dia->menu_window = derwin(dia->sub_window, 1, ncols - 3,
144 nlines - 3, 0);
145 if (dia->menu_window == NULL) {
146 goto fail;
149 for (nchoices = 0; choices[nchoices] != NULL; ++nchoices)
151 dia->choice_items = talloc_zero_array(dia, ITEM *, nchoices + 1);
152 if (dia->choice_items == NULL) {
153 goto fail;
155 for (i = 0; i < nchoices; ++i) {
156 char *desc = talloc_strdup(dia, choices[i]);
157 if (desc == NULL) {
158 goto fail;
160 dia->choice_items[i] = new_item(desc, desc);
161 if (dia->choice_items[i] == NULL) {
162 goto fail;
164 /* store choice index */
165 set_item_userptr(dia->choice_items[i], (void*)(uintptr_t)i);
168 dia->choices = new_menu(dia->choice_items);
169 if (dia->choices == NULL) {
170 goto fail;
173 set_menu_format(dia->choices, 1, ncols);
174 set_menu_win(dia->choices, dia->sub_window);
175 set_menu_sub(dia->choices, dia->menu_window);
176 menu_opts_off(dia->choices, O_SHOWDESC);
177 set_menu_mark(dia->choices, "* ");
178 post_menu(dia->choices);
179 wmove(dia->sub_window, 0, 0);
181 return dia;
183 fail:
184 talloc_free(dia);
186 return NULL;
189 struct dialog *dialog_choice_center_new(TALLOC_CTX *ctx, const char *title,
190 const char **choices, int nlines,
191 int ncols, WINDOW *below)
193 int y, x;
195 center_dialog_above_window(below, &nlines, &ncols, &y, &x);
197 return dialog_choice_new(ctx, title, choices, nlines, ncols, y, x);
200 static int handle_menu_input(MENU *menu, int c)
202 ITEM *item;
204 switch (c) {
205 case KEY_LEFT:
206 menu_driver(menu, REQ_LEFT_ITEM);
207 break;
208 case KEY_RIGHT:
209 menu_driver(menu, REQ_RIGHT_ITEM);
210 break;
211 case KEY_ENTER:
212 case '\n':
213 item = current_item(menu);
214 return (int)(uintptr_t)item_userptr(item);
217 return -1;
220 static int modal_loop(struct dialog *dia)
222 int c;
223 int selection = -1;
225 keypad(dia->window, true);
226 update_panels();
227 doupdate();
229 while (selection == -1) {
230 c = wgetch(dia->window);
231 selection = handle_menu_input(dia->choices, c);
232 update_panels();
233 doupdate();
236 talloc_free(dia);
238 return selection;
241 int dialog_notice(TALLOC_CTX *ctx, enum dialog_type type,
242 const char *title, WINDOW *below,
243 const char *msg, ...)
245 va_list ap;
246 struct dialog *dia;
247 char *str;
248 const char *choices[] = {
249 "Ok",
250 "Cancel",
251 NULL
253 int width;
255 va_start(ap, msg);
256 str = talloc_vasprintf(ctx, msg, ap);
257 va_end(ap);
258 if (str == NULL) {
259 return -1;
262 width = strlen(str) + 2;
264 if (type == DIA_ALERT) {
265 choices[1] = NULL;
267 dia = dialog_choice_center_new(ctx, title, choices, 5, width, below);
268 if (dia == NULL) {
269 return -1;
272 waddstr(dia->sub_window, str);
273 talloc_free(str);
275 return modal_loop(dia);
278 static void handle_form_input(FORM *frm, int c)
280 switch (c) {
281 case '\n':
282 form_driver(frm, REQ_NEW_LINE);
283 break;
284 case KEY_UP:
285 form_driver(frm, REQ_UP_CHAR);
286 break;
287 case KEY_DOWN:
288 form_driver(frm, REQ_DOWN_CHAR);
289 break;
290 case '\b':
291 case KEY_BACKSPACE:
292 form_driver(frm, REQ_DEL_PREV);
293 break;
294 case KEY_LEFT:
295 form_driver(frm, REQ_LEFT_CHAR);
296 break;
297 case KEY_RIGHT:
298 form_driver(frm, REQ_RIGHT_CHAR);
299 break;
300 default:
301 form_driver(frm, c);
302 break;
306 #define MAX_FIELDS 8
308 enum input_section {
309 IN_NAME,
310 IN_DATA,
311 IN_MENU
314 struct edit_dialog {
315 struct dialog *dia;
316 WINDOW *input_win;
317 FORM *input;
318 FIELD *field[MAX_FIELDS];
319 enum input_section section;
322 static int edit_dialog_free(struct edit_dialog *edit)
324 if (edit->input) {
325 unpost_form(edit->input);
327 if (edit->field[0]) {
328 free_field(edit->field[0]);
330 if (edit->field[1]) {
331 free_field(edit->field[1]);
333 delwin(edit->input_win);
335 return 0;
338 static WERROR fill_value_buffer(struct edit_dialog *edit,
339 const struct value_item *vitem)
341 char *tmp;
343 switch (vitem->type) {
344 case REG_DWORD: {
345 uint32_t v = 0;
346 if (vitem->data.length >= 4) {
347 v = IVAL(vitem->data.data, 0);
349 tmp = talloc_asprintf(edit, "0x%x", v);
350 if (tmp == NULL) {
351 return WERR_NOMEM;
353 set_field_buffer(edit->field[1], 0, tmp);
354 talloc_free(tmp);
355 break;
357 case REG_SZ:
358 case REG_EXPAND_SZ: {
359 const char *s;
361 if (!pull_reg_sz(edit, &vitem->data, &s)) {
362 return WERR_NOMEM;
364 set_field_buffer(edit->field[1], 0, s);
365 break;
367 case REG_MULTI_SZ: {
368 const char **p, **a;
369 char *buf = NULL;
371 if (!pull_reg_multi_sz(edit, &vitem->data, &a)) {
372 return WERR_NOMEM;
374 for (p = a; *p != NULL; ++p) {
375 if (buf == NULL) {
376 buf = talloc_asprintf(edit, "%s\n", *p);
377 } else {
378 buf = talloc_asprintf_append(buf, "%s\n", *p);
380 if (buf == NULL) {
381 return WERR_NOMEM;
384 set_field_buffer(edit->field[1], 0, buf);
385 talloc_free(buf);
390 return WERR_OK;
393 static char *string_trim_n(TALLOC_CTX *ctx, const char *buf, size_t n)
395 char *str;
397 str = talloc_strndup(ctx, buf, n);
399 if (str) {
400 trim_string(str, " ", " ");
403 return str;
406 static char *string_trim(TALLOC_CTX *ctx, const char *buf)
408 char *str;
410 str = talloc_strdup(ctx, buf);
412 if (str) {
413 trim_string(str, " ", " ");
416 return str;
419 static bool value_exists(TALLOC_CTX *ctx, const struct registry_key *key,
420 const char *name)
422 uint32_t type;
423 DATA_BLOB blob;
424 WERROR rv;
426 rv = reg_key_get_value_by_name(ctx, key, name, &type, &blob);
428 return W_ERROR_IS_OK(rv);
431 static WERROR set_value(struct edit_dialog *edit, struct registry_key *key,
432 uint32_t type, bool new_value)
434 WERROR rv;
435 DATA_BLOB blob;
436 const char *buf = field_buffer(edit->field[1], 0);
437 char *name = string_trim(edit, field_buffer(edit->field[0], 0));
439 if (!buf) {
440 return WERR_OK;
442 if (!new_value && !field_status(edit->field[1])) {
443 return WERR_OK;
445 if (new_value && value_exists(edit, key, name)) {
446 return WERR_FILE_EXISTS;
449 switch (type) {
450 case REG_DWORD: {
451 uint32_t val;
452 int base = 10;
454 if (buf[0] == '0' && tolower(buf[1]) == 'x') {
455 base = 16;
458 val = strtoul(buf, NULL, base);
459 blob = data_blob_talloc(edit, NULL, sizeof(val));
460 SIVAL(blob.data, 0, val);
461 rv = WERR_OK;
462 break;
464 case REG_SZ:
465 case REG_EXPAND_SZ: {
466 char *str = string_trim(edit, buf);
467 if (!str || !push_reg_sz(edit, &blob, str)) {
468 rv = WERR_NOMEM;
470 break;
472 case REG_MULTI_SZ: {
473 int rows, cols, max;
474 const char **arr;
475 size_t i;
477 dynamic_field_info(edit->field[1], &rows, &cols, &max);
479 arr = talloc_zero_array(edit, const char *, rows + 1);
480 if (arr == NULL) {
481 return WERR_NOMEM;
483 for (i = 0; *buf; ++i, buf += cols) {
484 SMB_ASSERT(i < rows);
485 arr[i] = string_trim_n(edit, buf, cols);
487 if (!push_reg_multi_sz(edit, &blob, arr)) {
488 rv = WERR_NOMEM;
490 break;
494 rv = reg_val_set(key, name, type, blob);
496 return rv;
499 static void section_down(struct edit_dialog *edit)
501 switch (edit->section) {
502 case IN_NAME:
503 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
504 edit->section = IN_DATA;
505 set_current_field(edit->input, edit->field[1]);
507 break;
508 case IN_DATA:
509 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
510 edit->section = IN_MENU;
511 menu_driver(edit->dia->choices, REQ_FIRST_ITEM);
513 break;
514 case IN_MENU:
515 edit->section = IN_NAME;
516 set_current_field(edit->input, edit->field[0]);
517 break;
521 static void section_up(struct edit_dialog *edit)
523 switch (edit->section) {
524 case IN_NAME:
525 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
526 edit->section = IN_MENU;
527 menu_driver(edit->dia->choices, REQ_FIRST_ITEM);
529 break;
530 case IN_DATA:
531 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
532 edit->section = IN_NAME;
533 set_current_field(edit->input, edit->field[0]);
535 break;
536 case IN_MENU:
537 edit->section = IN_DATA;
538 set_current_field(edit->input, edit->field[1]);
539 break;
543 WERROR dialog_edit_value(TALLOC_CTX *ctx, struct registry_key *key, uint32_t type,
544 const struct value_item *vitem, WINDOW *below)
546 struct edit_dialog *edit;
547 const char *choices[] = {
548 "Ok",
549 "Cancel",
550 NULL
552 char *title;
553 int nlines, ncols, val_rows;
554 WERROR rv = WERR_NOMEM;
555 int selection;
557 edit = talloc_zero(ctx, struct edit_dialog);
558 if (edit == NULL) {
559 return rv;
561 talloc_set_destructor(edit, edit_dialog_free);
563 title = talloc_asprintf(edit, "Edit %s value", str_regtype(type));
564 if (title == NULL) {
565 goto finish;
568 nlines = 9;
569 if (type == REG_MULTI_SZ) {
570 nlines += 4;
572 ncols = 50;
573 edit->dia = dialog_choice_center_new(edit, title, choices, nlines,
574 ncols, below);
575 talloc_free(title);
576 if (edit->dia == NULL) {
577 goto finish;
580 /* name */
581 edit->field[0] = new_field(1, ncols - 4, 1, 1, 0, 0);
582 if (edit->field[0] == NULL) {
583 goto finish;
586 /* data */
587 val_rows = 1;
588 if (type == REG_MULTI_SZ) {
589 val_rows += 4;
591 edit->field[1] = new_field(val_rows, ncols - 4, 4, 1, 0, 0);
592 if (edit->field[1] == NULL) {
593 goto finish;
595 set_field_back(edit->field[0], A_REVERSE);
596 set_field_back(edit->field[1], A_REVERSE);
597 field_opts_off(edit->field[0], O_BLANK | O_AUTOSKIP | O_STATIC);
598 field_opts_off(edit->field[1], O_BLANK | O_AUTOSKIP | O_STATIC | O_WRAP);
599 if (type == REG_DWORD) {
600 set_field_type(edit->field[1], TYPE_REGEXP,
601 "^ *([0-9]+|0[xX][0-9a-fA-F]+) *$");
604 if (vitem) {
605 set_field_buffer(edit->field[0], 0, vitem->value_name);
606 field_opts_off(edit->field[0], O_EDIT);
607 fill_value_buffer(edit, vitem);
610 edit->input = new_form(edit->field);
611 if (edit->input == NULL) {
612 goto finish;
614 form_opts_off(edit->input, O_NL_OVERLOAD | O_BS_OVERLOAD);
616 edit->input_win = derwin(edit->dia->sub_window, nlines - 3, ncols - 3,
617 0, 0);
618 if (edit->input_win == NULL) {
619 goto finish;
622 set_form_win(edit->input, edit->dia->sub_window);
623 set_form_sub(edit->input, edit->input_win);
624 post_form(edit->input);
625 mvwprintw(edit->dia->sub_window, 0, 0, "Name");
626 mvwprintw(edit->dia->sub_window, 3, 0, "Data");
628 keypad(edit->dia->window, true);
629 update_panels();
630 doupdate();
632 edit->section = IN_NAME;
634 while (1) {
635 int c = wgetch(edit->dia->window);
636 if (c == '\t') {
637 section_down(edit);
638 continue;
639 } else if (c == KEY_BTAB) {
640 section_up(edit);
641 continue;
644 if (edit->section == IN_NAME || edit->section == IN_DATA) {
645 handle_form_input(edit->input, c);
646 } else {
647 selection = handle_menu_input(edit->dia->choices, c);
648 if (selection == DIALOG_OK) {
649 rv = set_value(edit, key, type, vitem == NULL);
650 if (W_ERROR_EQUAL(rv, WERR_FILE_EXISTS)) {
651 dialog_notice(edit, DIA_ALERT,
652 "Value exists", below,
653 "Value name already exists.");
654 selection = -1;
655 } else {
656 goto finish;
658 } else if (selection == DIALOG_CANCEL) {
659 rv = WERR_OK;
660 goto finish;
664 update_panels();
665 doupdate();
668 finish:
669 talloc_free(edit);
671 return rv;
674 int dialog_select_type(TALLOC_CTX *ctx, int *type, WINDOW *below)
676 struct dialog *dia;
677 const char *choices[] = {
678 "OK",
679 "Cancel",
680 NULL
682 const char *reg_types[] = {
683 "REG_DWORD",
684 "REG_SZ",
685 "REG_EXPAND_SZ",
686 "REG_MULTI_SZ",
688 #define NTYPES (sizeof(reg_types) / sizeof(const char*))
689 ITEM **item;
690 MENU *list;
691 WINDOW *type_win;
692 int sel = -1;
693 size_t i;
695 dia = dialog_choice_center_new(ctx, "New Value", choices, 10, 20,
696 below);
697 if (dia == NULL) {
698 return -1;
701 mvwprintw(dia->sub_window, 0, 0, "Choose type:");
702 type_win = derwin(dia->sub_window, 6, 18, 1, 0);
703 if (type_win == NULL) {
704 goto finish;
707 item = talloc_zero_array(dia, ITEM *, NTYPES + 1);
708 if (item == NULL) {
709 goto finish;
712 for (i = 0; i < NTYPES; ++i) {
713 int t = regtype_by_string(reg_types[i]);
715 item[i] = new_item(reg_types[i], reg_types[i]);
716 if (item[i] == NULL) {
717 goto finish;
719 set_item_userptr(item[i], (void*)(uintptr_t)t);
722 list = new_menu(item);
723 if (list == NULL) {
724 goto finish;
727 set_menu_format(list, 7, 1);
728 set_menu_win(list, dia->sub_window);
729 set_menu_sub(list, type_win);
730 menu_opts_off(list, O_SHOWDESC);
731 set_menu_mark(list, "* ");
732 post_menu(list);
734 keypad(dia->window, true);
735 update_panels();
736 doupdate();
738 while (sel == -1) {
739 ITEM *it;
740 int c = wgetch(dia->window);
742 switch (c) {
743 case KEY_UP:
744 menu_driver(list, REQ_UP_ITEM);
745 break;
746 case KEY_DOWN:
747 menu_driver(list, REQ_DOWN_ITEM);
748 break;
749 case KEY_LEFT:
750 menu_driver(dia->choices, REQ_LEFT_ITEM);
751 break;
752 case KEY_RIGHT:
753 menu_driver(dia->choices, REQ_RIGHT_ITEM);
754 break;
755 case '\n':
756 case KEY_ENTER:
757 it = current_item(list);
758 *type = (int)(uintptr_t)item_userptr(it);
759 it = current_item(dia->choices);
760 sel = (int)(uintptr_t)item_userptr(it);
761 break;
765 finish:
766 if (list) {
767 unpost_menu(list);
768 free_menu(list);
770 if (item) {
771 ITEM **it;
772 for (it = item; *it; ++it) {
773 free_item(*it);
776 if (type_win) {
777 delwin(type_win);
779 talloc_free(dia);
781 return sel;