]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-widgets.c
ui/ncurses: Validate URL field
[petitboot] / ui / ncurses / nc-widgets.c
index 8f8816ecaa7c472b33fe9c52d0e0305556b6c621..93c882b0849af43a00cbe417584e7edd375eb46b 100644 (file)
@@ -53,6 +53,7 @@
 #include <util/util.h>
 #include <i18n/i18n.h>
 #include <fold/fold.h>
+#include <url/url.h>
 
 #include "nc-cui.h"
 #include "nc-widgets.h"
@@ -83,6 +84,7 @@ struct nc_widgetset {
 
        /* custom validators */
        FIELDTYPE *ipv4_multi_type;
+       FIELDTYPE *url_type;
 };
 
 struct nc_widget {
@@ -337,6 +339,14 @@ static bool textbox_process_key(
        case KEY_DC:
                form_driver(form, REQ_DEL_CHAR);
                break;
+       case '\t':
+       case KEY_BTAB:
+       case KEY_UP:
+       case KEY_DOWN:
+       case KEY_PPAGE:
+       case KEY_NPAGE:
+               /* Don't catch navigational keys */
+               return false;
        default:
                form_driver(form, key);
                break;
@@ -391,6 +401,20 @@ void widget_textbox_set_validator_integer(struct nc_widget_textbox *textbox,
        set_field_type(textbox->widget.field, TYPE_INTEGER, 1, min, max);
 }
 
+static bool check_url_field(FIELD *field,
+               const void *arg __attribute__((unused)))
+{
+       return is_url(field_buffer(field, 0));
+}
+
+void widget_textbox_set_validator_url(struct nc_widget_textbox *textbox)
+{
+       if (!textbox->set->url_type)
+               textbox->set->url_type = new_fieldtype(check_url_field, NULL);
+
+       set_field_type(textbox->widget.field, textbox->set->url_type);
+}
+
 void widget_textbox_set_validator_ipv4(struct nc_widget_textbox *textbox)
 {
        set_field_type(textbox->widget.field, TYPE_IPV4);
@@ -1110,6 +1134,12 @@ bool widgetset_process_key(struct nc_widgetset *set, int key)
        field = current_field(set->form);
        assert(field);
 
+       widget = field_userptr(field);
+
+       if (widget->process_key)
+               if (widget->process_key(widget, set->form, key))
+                       return true;
+
        tab = false;
 
        /* handle field change events */
@@ -1118,14 +1148,12 @@ bool widgetset_process_key(struct nc_widgetset *set, int key)
                tab = true;
                /* fall through */
        case KEY_UP:
-       case KEY_LEFT:
                req = REQ_SPREV_FIELD;
                break;
        case '\t':
                tab = true;
                /* fall through */
        case KEY_DOWN:
-       case KEY_RIGHT:
                req = REQ_SNEXT_FIELD;
                break;
        case KEY_PPAGE:
@@ -1134,9 +1162,14 @@ bool widgetset_process_key(struct nc_widgetset *set, int key)
        case KEY_NPAGE:
                req = REQ_SLAST_FIELD;
                break;
+       case KEY_LEFT:
+               req = REQ_LEFT_FIELD;
+               break;
+       case KEY_RIGHT:
+               req = REQ_RIGHT_FIELD;
+               break;
        }
 
-       widget = field_userptr(field);
        if (req) {
                widget_focus_change(widget, field, false);
                form_driver(set->form, req);
@@ -1161,10 +1194,7 @@ bool widgetset_process_key(struct nc_widgetset *set, int key)
                return true;
        }
 
-       if (!widget->process_key)
-               return false;
-
-       return widget->process_key(widget, set->form, key);
+       return false;
 }
 
 static int widgetset_destructor(void *ptr)