2 * Copyright (C) 2013 IBM Corporation
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.
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.
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
18 #if defined(HAVE_CONFIG_H)
26 #include <talloc/talloc.h>
27 #include <types/types.h>
28 #include <i18n/i18n.h>
31 #include "ui/common/discover-client.h"
33 #include "nc-add-url.h"
34 #include "nc-widgets.h"
38 extern const struct help_text add_url_help_text;
40 struct add_url_screen {
43 struct nc_widgetset *widgetset;
49 void (*on_exit)(struct cui *);
57 struct nc_widget_textbox *url_f;
58 struct nc_widget_label *url_l;
60 struct nc_widget_button *ok_b;
61 struct nc_widget_button *help_b;
62 struct nc_widget_button *cancel_b;
66 static struct add_url_screen *add_url_screen_from_scr(struct nc_scr *scr)
68 struct add_url_screen *add_url_screen;
70 assert(scr->sig == pb_add_url_screen_sig);
71 add_url_screen = (struct add_url_screen *)
72 ((char *)scr - (size_t)&((struct add_url_screen *)0)->scr);
73 assert(add_url_screen->scr.sig == pb_add_url_screen_sig);
74 return add_url_screen;
77 static void pad_refresh(struct add_url_screen *screen)
81 getmaxyx(screen->scr.sub_ncw, rows, cols);
82 getbegyx(screen->scr.sub_ncw, y, x);
84 prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
87 static void add_url_screen_process_key(struct nc_scr *scr, int key)
89 struct add_url_screen *screen = add_url_screen_from_scr(scr);
92 handled = widgetset_process_key(screen->widgetset, key);
101 screen->show_help = true;
107 screen->on_exit(screen->cui);
109 } else if (screen->show_help) {
110 screen->show_help = false;
111 screen->need_redraw = true;
112 cui_show_help(screen->cui, _("Retrieve Config"),
115 } else if (handled && (screen->cui->current == scr)) {
120 static int screen_process_form(struct add_url_screen *screen)
125 url = widget_textbox_get_value(screen->widgets.url_f);
126 if (!url || !strlen(url))
129 /* Once we have all the info we need, tell the server */
130 rc = cui_send_url(screen->cui, url);
133 pb_log("cui_send_retreive failed!\n");
135 pb_debug("add_url url sent!\n");
139 static int add_url_screen_post(struct nc_scr *scr)
141 struct add_url_screen *screen = add_url_screen_from_scr(scr);
144 screen->on_exit(screen->cui);
146 widgetset_post(screen->widgetset);
147 nc_scr_frame_draw(scr);
148 if (screen->need_redraw) {
149 redrawwin(scr->main_ncw);
150 screen->need_redraw = false;
152 wrefresh(screen->scr.main_ncw);
157 static int add_url_screen_unpost(struct nc_scr *scr)
159 struct add_url_screen *screen = add_url_screen_from_scr(scr);
160 widgetset_unpost(screen->widgetset);
164 struct nc_scr *add_url_screen_scr(struct add_url_screen *screen)
169 static void add_url_process_cb(struct nc_scr *scr)
171 struct add_url_screen *screen = add_url_screen_from_scr(scr);
173 if (!screen_process_form(screen))
177 static void ok_click(void *arg)
179 struct add_url_screen *screen = arg;
181 if (discover_client_authenticated(screen->cui->client)) {
182 if (screen_process_form(screen))
183 /* errors are written to the status line, so we'll need
185 wrefresh(screen->scr.main_ncw);
189 cui_show_auth(screen->cui, screen->scr.main_ncw, false,
194 static void help_click(void *arg)
196 struct add_url_screen *screen = arg;
197 screen->show_help = true;
200 static void cancel_click(void *arg)
202 struct add_url_screen *screen = arg;
206 static int layout_pair(struct add_url_screen *screen, int y,
207 struct nc_widget_label *label,
208 struct nc_widget *field)
210 struct nc_widget *label_w = widget_label_base(label);
211 widget_move(label_w, y, screen->label_x);
212 widget_move(field, y, screen->field_x);
213 return max(widget_height(label_w), widget_height(field));
216 static void add_url_screen_layout_widgets(struct add_url_screen *screen)
221 y += layout_pair(screen, y, screen->widgets.url_l,
222 widget_textbox_base(screen->widgets.url_f));
224 /* ok, help, cancel */
227 widget_move(widget_button_base(screen->widgets.ok_b),
229 widget_move(widget_button_base(screen->widgets.help_b),
230 y, screen->field_x + 14);
231 widget_move(widget_button_base(screen->widgets.cancel_b),
232 y, screen->field_x + 28);
235 static void add_url_screen_widget_focus(struct nc_widget *widget, void *arg)
237 struct add_url_screen *screen = arg;
238 int w_y, w_height, w_focus, s_max, adjust;
240 w_height = widget_height(widget);
241 w_focus = widget_focus_y(widget);
242 w_y = widget_y(widget) + w_focus;
243 s_max = getmaxy(screen->scr.sub_ncw) - 1;
245 if (w_y < screen->scroll_y)
246 screen->scroll_y = w_y;
248 else if (w_y + screen->scroll_y + 1 > s_max) {
249 /* Fit as much of the widget into the screen as possible */
250 adjust = min(s_max - 1, w_height - w_focus);
251 if (w_y + adjust >= screen->scroll_y + s_max)
252 screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
259 static void add_url_screen_setup_widgets(struct add_url_screen *screen)
261 struct nc_widgetset *set = screen->widgetset;
263 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
266 screen->widgets.url_l = widget_new_label(set, 0, 0,
267 _("Configuration URL:"));
268 screen->widgets.url_f = widget_new_textbox(set, 0, 0, 50, NULL);
270 screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
272 screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
274 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
275 cancel_click, screen);
278 static int add_url_screen_destroy(void *arg)
280 struct add_url_screen *screen = arg;
286 struct add_url_screen *add_url_screen_init(struct cui *cui,
287 void (*on_exit)(struct cui *))
289 struct add_url_screen *screen;
291 screen = talloc_zero(cui, struct add_url_screen);
292 talloc_set_destructor(screen, add_url_screen_destroy);
295 screen->on_exit = on_exit;
297 screen->field_x = 25;
298 screen->need_redraw = false;
300 nc_scr_init(&screen->scr, pb_add_url_screen_sig, 0,
301 cui, add_url_screen_process_key,
302 add_url_screen_post, add_url_screen_unpost,
305 screen->scr.frame.ltitle = talloc_strdup(screen,
306 _("Petitboot Config Retrieval"));
307 screen->scr.frame.rtitle = NULL;
308 screen->scr.frame.help = talloc_strdup(screen,
309 _("tab=next, shift+tab=previous, x=exit, h=help"));
310 nc_scr_frame_draw(&screen->scr);
312 screen->pad = newpad(LINES, COLS);
313 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
315 widgetset_set_widget_focus(screen->widgetset,
316 add_url_screen_widget_focus, screen);
318 add_url_screen_setup_widgets(screen);
319 add_url_screen_layout_widgets(screen);