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>
32 #include "nc-add-url.h"
33 #include "nc-widgets.h"
37 extern const struct help_text add_url_help_text;
39 struct add_url_screen {
42 struct nc_widgetset *widgetset;
46 void (*on_exit)(struct cui *);
52 struct nc_widget_textbox *url_f;
53 struct nc_widget_label *url_l;
55 struct nc_widget_button *ok_b;
56 struct nc_widget_button *help_b;
57 struct nc_widget_button *cancel_b;
61 static struct add_url_screen *add_url_screen_from_scr(struct nc_scr *scr)
63 struct add_url_screen *add_url_screen;
65 assert(scr->sig == pb_add_url_screen_sig);
66 add_url_screen = (struct add_url_screen *)
67 ((char *)scr - (size_t)&((struct add_url_screen *)0)->scr);
68 assert(add_url_screen->scr.sig == pb_add_url_screen_sig);
69 return add_url_screen;
72 static void add_url_screen_process_key(struct nc_scr *scr, int key)
74 struct add_url_screen *screen = add_url_screen_from_scr(scr);
77 handled = widgetset_process_key(screen->widgetset, key);
86 screen->show_help = true;
92 screen->on_exit(screen->cui);
94 } else if (screen->show_help) {
95 screen->show_help = false;
96 cui_show_help(screen->cui, _("Retrieve Config"),
100 wrefresh(screen->scr.main_ncw);
104 static int add_url_screen_post(struct nc_scr *scr)
106 struct add_url_screen *screen = add_url_screen_from_scr(scr);
107 widgetset_post(screen->widgetset);
108 nc_scr_frame_draw(scr);
109 redrawwin(scr->main_ncw);
110 wrefresh(screen->scr.main_ncw);
114 static int add_url_screen_unpost(struct nc_scr *scr)
116 struct add_url_screen *screen = add_url_screen_from_scr(scr);
117 widgetset_unpost(screen->widgetset);
121 struct nc_scr *add_url_screen_scr(struct add_url_screen *screen)
126 static int screen_process_form(struct add_url_screen *screen)
131 url = widget_textbox_get_value(screen->widgets.url_f);
132 if (!url || !strlen(url))
135 /* Once we have all the info we need, tell the server */
136 rc = cui_send_url(screen->cui, url);
139 pb_log("cui_send_retreive failed!\n");
141 pb_debug("add_url url sent!\n");
145 static void ok_click(void *arg)
147 struct add_url_screen *screen = arg;
148 if (screen_process_form(screen))
149 /* errors are written to the status line, so we'll need
151 wrefresh(screen->scr.main_ncw);
156 static void help_click(void *arg)
158 struct add_url_screen *screen = arg;
159 screen->show_help = true;
162 static void cancel_click(void *arg)
164 struct add_url_screen *screen = arg;
168 static int layout_pair(struct add_url_screen *screen, int y,
169 struct nc_widget_label *label,
170 struct nc_widget *field)
172 struct nc_widget *label_w = widget_label_base(label);
173 widget_move(label_w, y, screen->label_x);
174 widget_move(field, y, screen->field_x);
175 return max(widget_height(label_w), widget_height(field));
178 static void add_url_screen_layout_widgets(struct add_url_screen *screen)
183 y += layout_pair(screen, y, screen->widgets.url_l,
184 widget_textbox_base(screen->widgets.url_f));
186 /* ok, help, cancel */
189 widget_move(widget_button_base(screen->widgets.ok_b),
191 widget_move(widget_button_base(screen->widgets.help_b),
192 y, screen->field_x + 10);
193 widget_move(widget_button_base(screen->widgets.cancel_b),
194 y, screen->field_x + 23);
197 static void add_url_screen_setup_widgets(struct add_url_screen *screen)
199 struct nc_widgetset *set = screen->widgetset;
201 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
204 screen->widgets.url_l = widget_new_label(set, 0, 0,
205 _("Configuration URL:"));
206 screen->widgets.url_f = widget_new_textbox(set, 0, 0, 50, NULL);
208 screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, _("OK"),
210 screen->widgets.help_b = widget_new_button(set, 0, 0, 9, _("Help"),
212 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 9, _("Cancel"),
213 cancel_click, screen);
216 struct add_url_screen *add_url_screen_init(struct cui *cui,
217 void (*on_exit)(struct cui *))
219 struct add_url_screen *screen;
221 screen = talloc_zero(cui, struct add_url_screen);
224 screen->on_exit = on_exit;
226 screen->field_x = 22;
228 nc_scr_init(&screen->scr, pb_add_url_screen_sig, 0,
229 cui, add_url_screen_process_key,
230 add_url_screen_post, add_url_screen_unpost,
233 screen->scr.frame.ltitle = talloc_strdup(screen,
234 _("Petitboot Config Retrieval"));
235 screen->scr.frame.rtitle = NULL;
236 screen->scr.frame.help = talloc_strdup(screen,
237 _("tab=next, shift+tab=previous, x=exit, h=help"));
238 nc_scr_frame_draw(&screen->scr);
240 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
243 add_url_screen_setup_widgets(screen);
244 add_url_screen_layout_widgets(screen);