1 #include <nm_core.h>
2 #include <nm_menu.h>
3 #include <nm_dbus.h>
4 #include <nm_utils.h>
5 #include <nm_string.h>
6 #include <nm_window.h>
7 #include <nm_network.h>
8 #include <nm_cfg_file.h>
9 #include <nm_stat_usage.h>
10 #include <nm_qmp_control.h>
11 #include <nm_lan_settings.h>
12 
nm_print_dropdown_menu(nm_menu_data_t * values,nm_window_t * w)13 void nm_print_dropdown_menu(nm_menu_data_t *values, nm_window_t *w)
14 {
15     int x = 2, y = 1;
16 
17     box(w, 0, 0);
18     curs_set(0);
19     wattroff(w, COLOR_PAIR(NM_COLOR_HIGHLIGHT));
20 
21     for (size_t n = values->item_first, i = 0; n < values->item_last; n++, i++) {
22         if (values->highlight == i + 1) {
23             wattron(w, A_REVERSE);
24             mvwprintw(w, y, x, "%s", values->v->data[n]);
25             wattroff(w, A_REVERSE);
26         } else {
27             mvwprintw(w, y, x, "%s", values->v->data[n]);
28         }
29 
30         y++;
31     }
32     wrefresh(w);
33 }
34 
nm_print_base_menu(nm_menu_data_t * ifs)35 void nm_print_base_menu(nm_menu_data_t *ifs)
36 {
37     int x = 2, y = 3;
38     size_t screen_x;
39     static nm_menu_data_t *ifs_ = NULL;
40 
41     if (ifs)
42         ifs_ = ifs;
43 
44     if (!ifs_)
45         return;
46 
47     wattroff(side_window, COLOR_PAIR(NM_COLOR_HIGHLIGHT));
48 
49     screen_x = getmaxx(side_window);
50     if (screen_x < 20) { /* window to small */
51         mvwaddstr(side_window, 3, 1, "...");
52         wrefresh(side_window);
53         return;
54     }
55 
56     for (size_t n = ifs_->item_first, i = 0; n < ifs_->item_last; n++, i++) {
57         nm_str_t if_name = NM_INIT_STR;
58         int space_num;
59 
60         if (n >= ifs_->v->n_memb)
61             nm_bug(_("%s: invalid index: %zu"), __func__, n);
62 
63         nm_str_alloc_text(&if_name, (char *) ifs_->v->data[n]);
64         nm_align2line(&if_name, screen_x);
65 
66         space_num = (screen_x - if_name.len - 4);
67         if (space_num > 0) {
68             for (int s = 0; s < space_num; s++)
69                 nm_str_add_char_opt(&if_name, ' ');
70         }
71 
72         if (ifs_->highlight == i + 1) {
73             wattron(side_window, A_REVERSE);
74             mvwprintw(side_window, y, x, "%s", if_name.data);
75             wattroff(side_window, A_REVERSE);
76         } else {
77             mvwprintw(side_window, y, x, "%s", if_name.data);
78         }
79 
80         y++;
81         wrefresh(side_window);
82         nm_str_free(&if_name);
83     }
84 }
85 
nm_print_vm_menu(nm_menu_data_t * vm)86 void nm_print_vm_menu(nm_menu_data_t *vm)
87 {
88     int x = 2, y = 3;
89     size_t screen_x;
90     static nm_menu_data_t *vm_ = NULL;
91 
92     if (vm)
93         vm_ = vm;
94 
95     if(!vm_)
96         return;
97 
98     screen_x = getmaxx(side_window);
99     if (screen_x < 20) { /* window to small */
100         mvwaddstr(side_window, 3, 1, "...");
101         wrefresh(side_window);
102         return;
103     }
104 
105     wattroff(side_window, COLOR_PAIR(NM_COLOR_HIGHLIGHT));
106 
107     for (size_t n = vm_->item_first, i = 0; n < vm_->item_last; n++, i++) {
108         int space_num;
109         nm_str_t vm_name = NM_INIT_STR;
110 
111         if (n >= vm_->v->n_memb)
112             nm_bug(_("%s: invalid index: %zu"), __func__, n);
113 
114         nm_str_alloc_text(&vm_name, nm_vect_item_name_ctx(vm_->v, n));
115         nm_align2line(&vm_name, screen_x);
116 
117         space_num = (screen_x - vm_name.len - 4);
118         if (space_num > 0) {
119             for (int s = 0; s < space_num; s++)
120                 nm_str_add_char_opt(&vm_name, ' ');
121         }
122 
123         if (nm_qmp_test_socket(nm_vect_item_name(vm_->v, n)) == NM_OK) {
124             nm_vect_set_item_status(vm_->v, n, 1);
125             wattron(side_window, COLOR_PAIR(NM_COLOR_HIGHLIGHT));
126         } else {
127             nm_vect_set_item_status(vm_->v, n, 0);
128             wattroff(side_window, COLOR_PAIR(NM_COLOR_HIGHLIGHT));
129         }
130 
131         if (vm_->highlight == i + 1) {
132             wattron(side_window, A_REVERSE);
133             mvwprintw(side_window, y, x, "%s", vm_name.data);
134             wattroff(side_window, A_REVERSE);
135         } else {
136             mvwprintw(side_window, y, x, "%s", vm_name.data);
137         }
138 
139         y++;
140         wrefresh(side_window);
141         nm_str_free(&vm_name);
142     }
143 }
144 
nm_menu_scroll(nm_menu_data_t * menu,size_t list_len,int ch)145 void nm_menu_scroll(nm_menu_data_t *menu, size_t list_len, int ch)
146 {
147     if ((ch == KEY_UP) && (menu->highlight == 1) && (menu->item_first == 0) &&
148             (list_len < menu->v->n_memb)) {
149         menu->highlight = list_len;
150         menu->item_first = menu->v->n_memb - list_len;
151         menu->item_last = menu->v->n_memb;
152     } else if (ch == KEY_UP) {
153         if ((menu->highlight == 1) && (menu->v->n_memb <= list_len)) {
154             menu->highlight = menu->v->n_memb;
155         } else if ((menu->highlight == 1) && (menu->item_first != 0)) {
156             menu->item_first--;
157             menu->item_last--;
158         } else {
159             menu->highlight--;
160         }
161     } else if ((ch == KEY_DOWN) && (menu->highlight == list_len) &&
162             (menu->item_last == menu->v->n_memb)) {
163         menu->highlight = 1;
164         menu->item_first = 0;
165         menu->item_last = list_len;
166     } else if (ch == KEY_DOWN) {
167         if ((menu->highlight == menu->v->n_memb) && (menu->v->n_memb <= list_len)) {
168             menu->highlight = 1;
169         } else if ((menu->highlight == list_len) && (menu->item_last < menu->v->n_memb)) {
170             menu->item_first++;
171             menu->item_last++;
172         } else {
173             menu->highlight++;
174         }
175     } else if (ch == KEY_HOME) {
176         menu->highlight = 1;
177         menu->item_first = 0;
178         menu->item_last = list_len;
179     } else if (ch == KEY_END) {
180         menu->highlight = list_len;
181         menu->item_first = menu->v->n_memb - list_len;
182         menu->item_last = menu->v->n_memb;
183     }
184 
185     if (ch == KEY_UP || ch == KEY_DOWN || ch == KEY_HOME || ch == KEY_END)
186         NM_STAT_CLEAN();
187 }
188 
189 #if defined (NM_OS_LINUX)
nm_print_veth_menu(nm_menu_data_t * veth,int get_status)190 void nm_print_veth_menu(nm_menu_data_t *veth, int get_status)
191 {
192     int x = 2, y = 3;
193     size_t screen_x;
194     nm_str_t veth_name = NM_INIT_STR;
195     nm_str_t veth_copy = NM_INIT_STR;
196     nm_str_t veth_lname = NM_INIT_STR;
197     nm_str_t veth_rname = NM_INIT_STR;
198     static nm_menu_data_t *veth_ = NULL;
199 
200     if (veth)
201         veth_ = veth;
202 
203     if (!veth_)
204         return;
205 
206     screen_x = getmaxx(side_window);
207     if (screen_x < 20) { /* window to small */
208         mvwaddstr(side_window, 3, 1, "...");
209         wrefresh(side_window);
210         return;
211     }
212 
213     wattroff(side_window, COLOR_PAIR(NM_COLOR_HIGHLIGHT));
214 
215     for (size_t n = veth_->item_first, i = 0; n < veth_->item_last; n++, i++) {
216         int space_num;
217 
218         if (n >= veth_->v->n_memb)
219             nm_bug(_("%s: invalid index: %zu"), __func__, n);
220 
221         nm_str_alloc_text(&veth_name, nm_vect_item_name_ctx(veth_->v, n));
222         nm_str_copy(&veth_copy, &veth_name);
223         nm_align2line(&veth_name, screen_x);
224         nm_lan_parse_name(&veth_copy, &veth_lname, &veth_rname);
225 
226         space_num = (screen_x - veth_name.len - 4);
227         if (space_num > 0) {
228             for (int s = 0; s < space_num; s++)
229                 nm_str_add_char_opt(&veth_name, ' ');
230         }
231 
232         if (get_status) {
233             if (nm_net_link_status(&veth_lname) == NM_OK)
234                 nm_vect_set_item_status(veth_->v, n, 1);
235             else
236                 nm_vect_set_item_status(veth_->v, n, 0);
237         }
238 
239         if (nm_vect_item_status(veth_->v, n))
240             wattron(side_window, COLOR_PAIR(NM_COLOR_HIGHLIGHT));
241         else
242             wattroff(side_window, COLOR_PAIR(NM_COLOR_HIGHLIGHT));
243 
244         if (veth_->highlight == i + 1) {
245             wattron(side_window, A_REVERSE);
246             mvwprintw(side_window, y, x, "%s", veth_name.data);
247             wattroff(side_window, A_REVERSE);
248         } else {
249             mvwprintw(side_window, y, x, "%s", veth_name.data);
250         }
251 
252         y++;
253         wrefresh(side_window);
254 
255         nm_str_trunc(&veth_name, 0);
256         nm_str_trunc(&veth_copy, 0);
257         nm_str_trunc(&veth_lname, 0);
258         nm_str_trunc(&veth_rname, 0);
259     }
260 
261     nm_str_free(&veth_name);
262     nm_str_free(&veth_copy);
263     nm_str_free(&veth_lname);
264     nm_str_free(&veth_rname);
265 }
266 #endif /* NM_OS_LINUX */
267 
268 /* vim:set ts=4 sw=4: */
269