3af3c8b59cee7d1d7cb6f025a567fc56a11f612d
[samba.git] / source3 / utils / regedit_treeview.c
1 /*
2  * Samba Unix/Linux SMB client library
3  * Registry Editor
4  * Copyright (C) Christopher Davis 2012
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "regedit_treeview.h"
21 #include "regedit_list.h"
22 #include "lib/registry/registry.h"
23
24 #define HEADING_X 3
25
26 struct tree_node *tree_node_new(TALLOC_CTX *ctx, struct tree_node *parent,
27                                 const char *name, struct registry_key *key)
28 {
29         struct tree_node *node;
30
31         node = talloc_zero(ctx, struct tree_node);
32         if (!node) {
33                 return NULL;
34         }
35
36         node->name = talloc_strdup(node, name);
37         if (!node->name) {
38                 talloc_free(node);
39                 return NULL;
40         }
41
42         node->key = talloc_steal(node, key);
43
44         if (parent) {
45                 /* Check if this node is the first descendant of parent. */
46                 if (!parent->child_head) {
47                         parent->child_head = node;
48                 }
49                 node->parent = parent;
50         }
51
52         return node;
53 }
54
55 void tree_node_append(struct tree_node *left, struct tree_node *right)
56 {
57         if (left->next) {
58                 right->next = left->next;
59                 left->next->previous = right;
60         }
61         left->next = right;
62         right->previous = left;
63 }
64
65 void tree_node_append_last(struct tree_node *list, struct tree_node *node)
66 {
67         tree_node_append(tree_node_last(list), node);
68 }
69
70 struct tree_node *tree_node_pop(struct tree_node **plist)
71 {
72         struct tree_node *node;
73
74         node = *plist;
75
76         if (node == NULL)
77                 return NULL;
78
79         *plist = node->previous;
80         if (*plist == NULL) {
81                 *plist = node->next;
82         }
83         if (node->previous) {
84                 node->previous->next = node->next;
85         }
86         if (node->next) {
87                 node->next->previous = node->previous;
88         }
89         if (node->parent && node->parent->child_head == node) {
90                 node->parent->child_head = node->next;
91         }
92         node->next = NULL;
93         node->previous = NULL;
94
95         return node;
96 }
97
98 struct tree_node *tree_node_first(struct tree_node *list)
99 {
100         /* Grab the first node in this list from the parent if available. */
101         if (list->parent) {
102                 return list->parent->child_head;
103         }
104
105         while (list && list->previous) {
106                 list = list->previous;
107         }
108
109         return list;
110 }
111
112 struct tree_node *tree_node_last(struct tree_node *list)
113 {
114         while (list && list->next) {
115                 list = list->next;
116         }
117
118         return list;
119 }
120
121 static uint32_t get_num_subkeys(struct tree_node *node)
122 {
123         const char *classname;
124         uint32_t num_subkeys;
125         uint32_t num_values;
126         NTTIME last_change_time;
127         uint32_t max_subkeynamelen;
128         uint32_t max_valnamelen;
129         uint32_t max_valbufsize;
130         WERROR rv;
131
132         rv = reg_key_get_info(node, node->key, &classname, &num_subkeys,
133                               &num_values, &last_change_time,
134                               &max_subkeynamelen, &max_valnamelen,
135                               &max_valbufsize);
136
137         if (W_ERROR_IS_OK(rv)) {
138                 return num_subkeys;
139         }
140
141         return 0;
142 }
143
144 WERROR tree_node_reopen_key(struct tree_node *node)
145 {
146         SMB_ASSERT(node->parent != NULL);
147         SMB_ASSERT(node->name != NULL);
148         TALLOC_FREE(node->key);
149         return reg_open_key(node->parent, node->parent->key, node->name,
150                             &node->key);
151 }
152
153 bool tree_node_has_children(struct tree_node *node)
154 {
155         if (node->child_head) {
156                 return true;
157         }
158
159         return get_num_subkeys(node) > 0;
160 }
161
162 static int node_cmp(struct tree_node **a, struct tree_node **b)
163 {
164         return strcmp((*a)->name, (*b)->name);
165 }
166
167 void tree_node_insert_sorted(struct tree_node *list, struct tree_node *node)
168 {
169         list = tree_node_first(list);
170
171         if (node_cmp(&list, &node) >= 0) {
172                 tree_node_append(node, list);
173                 if (list->parent) {
174                         list->parent->child_head = node;
175                 }
176                 return;
177         }
178
179         while (list->next && node_cmp(&list->next, &node) < 0) {
180                 list = list->next;
181         }
182
183         tree_node_append(list, node);
184 }
185
186 WERROR tree_node_load_children(struct tree_node *node)
187 {
188         struct registry_key *key;
189         const char *key_name, *klass;
190         NTTIME modified;
191         uint32_t i, nsubkeys, count;
192         WERROR rv;
193         struct tree_node *prev, **array;
194
195         /* does this node already have it's children loaded? */
196         if (node->child_head)
197                 return WERR_OK;
198
199         nsubkeys = get_num_subkeys(node);
200         if (nsubkeys == 0)
201                 return WERR_OK;
202
203         array = talloc_zero_array(node, struct tree_node *, nsubkeys);
204         if (array == NULL) {
205                 return WERR_NOMEM;
206         }
207
208         for (count = 0, i = 0; i < nsubkeys; ++i) {
209                 rv = reg_key_get_subkey_by_index(node, node->key, i,
210                                                  &key_name, &klass,
211                                                  &modified);
212                 if (!W_ERROR_IS_OK(rv)) {
213                         goto finish;
214                 }
215
216                 rv = reg_open_key(node, node->key, key_name, &key);
217                 if (!W_ERROR_IS_OK(rv)) {
218                         continue;
219                 }
220
221                 array[count] = tree_node_new(node, node, key_name, key);
222                 if (array[count] == NULL) {
223                         rv = WERR_NOMEM;
224                         goto finish;
225                 }
226                 ++count;
227         }
228
229         if (count) {
230                 TYPESAFE_QSORT(array, count, node_cmp);
231
232                 for (i = 1, prev = array[0]; i < count; ++i) {
233                         tree_node_append(prev, array[i]);
234                         prev = array[i];
235                 }
236                 node->child_head = array[0];
237
238                 rv = WERR_OK;
239         }
240
241 finish:
242         if (!W_ERROR_IS_OK(rv)) {
243                 for (i = 0; i < nsubkeys; ++i) {
244                         talloc_free(array[i]);
245                 }
246                 node->child_head = NULL;
247         }
248         talloc_free(array);
249
250         return rv;
251 }
252
253 void tree_node_free_recursive(struct tree_node *list)
254 {
255         struct tree_node *node;
256
257         if (list == NULL) {
258                 return;
259         }
260
261         while ((node = tree_node_pop(&list)) != NULL) {
262                 if (node->child_head) {
263                         tree_node_free_recursive(node->child_head);
264                 }
265                 node->child_head = NULL;
266                 talloc_free(node);
267         }
268 }
269
270 void tree_view_clear(struct tree_view *view)
271 {
272         multilist_set_data(view->list, NULL);
273 }
274
275 WERROR tree_view_update(struct tree_view *view, struct tree_node *list)
276 {
277         WERROR rv;
278
279         rv = multilist_set_data(view->list, list);
280         if (W_ERROR_IS_OK(rv)) {
281                 multilist_refresh(view->list);
282         }
283
284         return rv;
285 }
286
287 /* is this node in the current level? */
288 bool tree_view_is_node_visible(struct tree_view *view, struct tree_node *node)
289 {
290         const struct tree_node *first;
291
292         first = multilist_get_data(view->list);
293
294         return first && first->parent == node->parent;
295 }
296
297 void tree_view_set_current_node(struct tree_view *view, struct tree_node *node)
298 {
299         multilist_set_current_row(view->list, node);
300 }
301
302 struct tree_node *tree_view_get_current_node(struct tree_view *view)
303 {
304         const void *row = multilist_get_current_row(view->list);
305         return talloc_get_type_abort(row, struct tree_node);
306 }
307
308 void tree_view_driver(struct tree_view *view, int c)
309 {
310         multilist_driver(view->list, c);
311 }
312
313 void tree_view_set_selected(struct tree_view *view, bool select)
314 {
315         attr_t attr = A_NORMAL;
316
317         if (select) {
318                 attr = A_REVERSE;
319         }
320         mvwchgat(view->window, 0, HEADING_X, 3, attr, 0, NULL);
321 }
322
323 void tree_view_show(struct tree_view *view)
324 {
325         multilist_refresh(view->list);
326         touchwin(view->window);
327         wnoutrefresh(view->window);
328         wnoutrefresh(view->sub);
329 }
330
331 static int tree_view_free(struct tree_view *view)
332 {
333         if (view->panel) {
334                 del_panel(view->panel);
335         }
336         if (view->sub) {
337                 delwin(view->sub);
338         }
339         if (view->window) {
340                 delwin(view->window);
341         }
342         tree_node_free_recursive(view->root);
343
344         return 0;
345 }
346
347 static const char *tv_get_column_header(const void *data, unsigned col)
348 {
349         SMB_ASSERT(col == 0);
350         return "Name";
351 }
352
353 static const void *tv_get_first_row(const void *data)
354 {
355         if (data == NULL) {
356                 return NULL;
357         }
358
359         return talloc_get_type_abort(data, struct tree_node);
360 }
361
362 static const void *tv_get_next_row(const void *data, const void *row)
363 {
364         const struct tree_node *node;
365         SMB_ASSERT(row != NULL);
366         node = talloc_get_type_abort(row, struct tree_node);
367         return node->next;
368 }
369
370 static const void *tv_get_prev_row(const void *data, const void *row)
371 {
372         const struct tree_node *node;
373         SMB_ASSERT(row != NULL);
374         node = talloc_get_type_abort(row, struct tree_node);
375         return node->previous;
376 }
377
378 static const char *tv_get_item_prefix(const void *row, unsigned col)
379 {
380         struct tree_node *node;
381
382         SMB_ASSERT(col == 0);
383         SMB_ASSERT(row != NULL);
384         node = talloc_get_type_abort(row, struct tree_node);
385         if (tree_node_has_children(node)) {
386                 return "+";
387         }
388         return " ";
389 }
390
391 static const char *tv_get_item_label(const void *row, unsigned col)
392 {
393         const struct tree_node *node;
394         SMB_ASSERT(col == 0);
395         SMB_ASSERT(row != NULL);
396         node = talloc_get_type_abort(row, struct tree_node);
397         return node->name;
398 }
399
400 static struct multilist_accessors tv_accessors = {
401         .get_column_header = tv_get_column_header,
402         .get_first_row = tv_get_first_row,
403         .get_next_row = tv_get_next_row,
404         .get_prev_row = tv_get_prev_row,
405         .get_item_prefix = tv_get_item_prefix,
406         .get_item_label = tv_get_item_label
407 };
408
409 struct tree_view *tree_view_new(TALLOC_CTX *ctx, struct tree_node *root,
410                                 int nlines, int ncols, int begin_y,
411                                 int begin_x)
412 {
413         struct tree_view *view;
414
415         view = talloc_zero(ctx, struct tree_view);
416         if (view == NULL) {
417                 return NULL;
418         }
419
420         talloc_set_destructor(view, tree_view_free);
421
422         view->window = newwin(nlines, ncols, begin_y, begin_x);
423         if (view->window == NULL) {
424                 goto fail;
425         }
426         view->sub = subwin(view->window, nlines - 2, ncols - 2,
427                            begin_y + 1, begin_x + 1);
428         if (view->sub == NULL) {
429                 goto fail;
430         }
431         box(view->window, 0, 0);
432         mvwprintw(view->window, 0, HEADING_X, "Key");
433
434         view->panel = new_panel(view->window);
435         if (view->panel == NULL) {
436                 goto fail;
437         }
438         view->root = root;
439
440         view->list = multilist_new(view, view->sub, &tv_accessors, 1);
441         if (view->list == NULL) {
442                 goto fail;
443         }
444         tree_view_update(view, root);
445
446         return view;
447
448 fail:
449         talloc_free(view);
450
451         return NULL;
452 }
453
454 void tree_view_resize(struct tree_view *view, int nlines, int ncols,
455                       int begin_y, int begin_x)
456 {
457         WINDOW *nwin, *nsub;
458
459         nwin = newwin(nlines, ncols, begin_y, begin_x);
460         if (nwin == NULL) {
461                 return;
462         }
463         nsub = subwin(nwin, nlines - 2, ncols - 2, begin_y + 1, begin_x + 1);
464         if (nsub == NULL) {
465                 delwin(nwin);
466                 return;
467         }
468         replace_panel(view->panel, nwin);
469         delwin(view->sub);
470         delwin(view->window);
471         view->window = nwin;
472         view->sub = nsub;
473         box(view->window, 0, 0);
474         mvwprintw(view->window, 0, HEADING_X, "Key");
475         multilist_set_window(view->list, view->sub);
476         tree_view_show(view);
477 }
478
479 static void print_path_recursive(WINDOW *label, struct tree_node *node,
480                                  size_t *len)
481 {
482         if (node->parent)
483                 print_path_recursive(label, node->parent, len);
484
485         wprintw(label, "%s/", node->name);
486         *len += 1 + strlen(node->name);
487 }
488
489 /* print the path of node to label */
490 size_t tree_node_print_path(WINDOW *label, struct tree_node *node)
491 {
492         size_t len = 1;
493
494         if (node == NULL)
495                 return 0;
496
497         werase(label);
498         wprintw(label, "/");
499
500         if (node->parent)
501                 print_path_recursive(label, node->parent, &len);
502
503         return len;
504 }