Merge remote-tracking branch 'sammj/master'
[petitboot] / ui / ncurses / nc-add-url.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <talloc/talloc.h>
27 #include <types/types.h>
28 #include <i18n/i18n.h>
29 #include <log/log.h>
30
31 #include "nc-cui.h"
32 #include "nc-add-url.h"
33 #include "nc-widgets.h"
34
35 #define N_FIELDS        5
36
37 extern const struct help_text add_url_help_text;
38
39 struct add_url_screen {
40         struct nc_scr           scr;
41         struct cui              *cui;
42         struct nc_widgetset     *widgetset;
43
44         bool                    exit;
45         bool                    show_help;
46         bool                    need_redraw;
47         void                    (*on_exit)(struct cui *);
48
49         int                     label_x;
50         int                     field_x;
51
52         struct {
53                 struct nc_widget_textbox        *url_f;
54                 struct nc_widget_label          *url_l;
55
56                 struct nc_widget_button         *ok_b;
57                 struct nc_widget_button         *help_b;
58                 struct nc_widget_button         *cancel_b;
59         } widgets;
60 };
61
62 static struct add_url_screen *add_url_screen_from_scr(struct nc_scr *scr)
63 {
64         struct add_url_screen *add_url_screen;
65
66         assert(scr->sig == pb_add_url_screen_sig);
67         add_url_screen = (struct add_url_screen *)
68                 ((char *)scr - (size_t)&((struct add_url_screen *)0)->scr);
69         assert(add_url_screen->scr.sig == pb_add_url_screen_sig);
70         return add_url_screen;
71 }
72
73 static void add_url_screen_process_key(struct nc_scr *scr, int key)
74 {
75         struct add_url_screen *screen = add_url_screen_from_scr(scr);
76         bool handled;
77
78         handled = widgetset_process_key(screen->widgetset, key);
79
80         if (!handled) {
81                 switch (key) {
82                 case 'x':
83                 case 27: /* esc */
84                         screen->exit = true;
85                         break;
86                 case 'h':
87                         screen->show_help = true;
88                         break;
89                 }
90         }
91
92         if (screen->exit) {
93                 screen->on_exit(screen->cui);
94
95         } else if (screen->show_help) {
96                 screen->show_help = false;
97                 screen->need_redraw = true;
98                 cui_show_help(screen->cui, _("Retrieve Config"),
99                         &add_url_help_text);
100
101         } else if (handled) {
102                 wrefresh(screen->scr.main_ncw);
103         }
104 }
105
106 static int add_url_screen_post(struct nc_scr *scr)
107 {
108         struct add_url_screen *screen = add_url_screen_from_scr(scr);
109         widgetset_post(screen->widgetset);
110         nc_scr_frame_draw(scr);
111         if (screen->need_redraw) {
112                 redrawwin(scr->main_ncw);
113                 screen->need_redraw = false;
114         }
115         wrefresh(screen->scr.main_ncw);
116         return 0;
117 }
118
119 static int add_url_screen_unpost(struct nc_scr *scr)
120 {
121         struct add_url_screen *screen = add_url_screen_from_scr(scr);
122         widgetset_unpost(screen->widgetset);
123         return 0;
124 }
125
126 struct nc_scr *add_url_screen_scr(struct add_url_screen *screen)
127 {
128         return &screen->scr;
129 }
130
131 static int screen_process_form(struct add_url_screen *screen)
132 {
133         char *url;
134         int rc;
135
136         url = widget_textbox_get_value(screen->widgets.url_f);
137         if (!url || !strlen(url))
138                 return 0;
139
140         /* Once we have all the info we need, tell the server */
141         rc = cui_send_url(screen->cui, url);
142
143         if (rc)
144                 pb_log("cui_send_retreive failed!\n");
145         else
146                 pb_debug("add_url url sent!\n");
147         return 0;
148 }
149
150 static void ok_click(void *arg)
151 {
152         struct add_url_screen *screen = arg;
153         if (screen_process_form(screen))
154                 /* errors are written to the status line, so we'll need
155                  * to refresh */
156                 wrefresh(screen->scr.main_ncw);
157         else
158                 screen->exit = true;
159 }
160
161 static void help_click(void *arg)
162 {
163         struct add_url_screen *screen = arg;
164         screen->show_help = true;
165 }
166
167 static void cancel_click(void *arg)
168 {
169         struct add_url_screen *screen = arg;
170         screen->exit = true;
171 }
172
173 static int layout_pair(struct add_url_screen *screen, int y,
174         struct nc_widget_label *label,
175         struct nc_widget *field)
176 {
177         struct nc_widget *label_w = widget_label_base(label);
178         widget_move(label_w, y, screen->label_x);
179         widget_move(field, y, screen->field_x);
180         return max(widget_height(label_w), widget_height(field));
181 }
182
183 static void add_url_screen_layout_widgets(struct add_url_screen *screen)
184 {
185         int y = 2;
186
187         /* url field */
188         y += layout_pair(screen, y, screen->widgets.url_l,
189                          widget_textbox_base(screen->widgets.url_f));
190
191         /* ok, help, cancel */
192         y += 1;
193
194         widget_move(widget_button_base(screen->widgets.ok_b),
195                 y, screen->field_x);
196         widget_move(widget_button_base(screen->widgets.help_b),
197                 y, screen->field_x + 10);
198         widget_move(widget_button_base(screen->widgets.cancel_b),
199                 y, screen->field_x + 24);
200 }
201
202 static void add_url_screen_setup_widgets(struct add_url_screen *screen)
203 {
204         struct nc_widgetset *set = screen->widgetset;
205
206         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
207                         == N_FIELDS);
208
209         screen->widgets.url_l = widget_new_label(set, 0, 0,
210                         _("Configuration URL:"));
211         screen->widgets.url_f = widget_new_textbox(set, 0, 0, 50, NULL);
212
213         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, _("OK"),
214                         ok_click, screen);
215         screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
216                         help_click, screen);
217         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
218                         cancel_click, screen);
219 }
220
221 struct add_url_screen *add_url_screen_init(struct cui *cui,
222                 void (*on_exit)(struct cui *))
223 {
224         struct add_url_screen *screen;
225
226         screen = talloc_zero(cui, struct add_url_screen);
227
228         screen->cui = cui;
229         screen->on_exit = on_exit;
230         screen->label_x = 2;
231         screen->field_x = 25;
232         screen->need_redraw = false;
233
234         nc_scr_init(&screen->scr, pb_add_url_screen_sig, 0,
235                 cui, add_url_screen_process_key,
236                 add_url_screen_post, add_url_screen_unpost,
237                 NULL);
238
239         screen->scr.frame.ltitle = talloc_strdup(screen,
240                         _("Petitboot Config Retrieval"));
241         screen->scr.frame.rtitle = NULL;
242         screen->scr.frame.help = talloc_strdup(screen,
243                         _("tab=next, shift+tab=previous, x=exit, h=help"));
244         nc_scr_frame_draw(&screen->scr);
245
246         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
247                         NULL);
248
249         add_url_screen_setup_widgets(screen);
250         add_url_screen_layout_widgets(screen);
251
252         return screen;
253 }
254