2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Christopher Davis 2012
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.
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.
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/>.
20 #include "regedit_treeview.h"
21 #include "regedit_list.h"
22 #include "lib/registry/registry.h"
26 struct tree_node *tree_node_new(TALLOC_CTX *ctx, struct tree_node *parent,
27 const char *name, struct registry_key *key)
29 struct tree_node *node;
31 node = talloc_zero(ctx, struct tree_node);
36 node->name = talloc_strdup(node, name);
42 node->key = talloc_steal(node, key);
45 /* Check if this node is the first descendant of parent. */
46 if (!parent->child_head) {
47 parent->child_head = node;
49 node->parent = parent;
55 void tree_node_append(struct tree_node *left, struct tree_node *right)
58 right->next = left->next;
59 left->next->previous = right;
62 right->previous = left;
65 void tree_node_append_last(struct tree_node *list, struct tree_node *node)
67 tree_node_append(tree_node_last(list), node);
70 struct tree_node *tree_node_pop(struct tree_node **plist)
72 struct tree_node *node;
79 *plist = node->previous;
84 node->previous->next = node->next;
87 node->next->previous = node->previous;
89 if (node->parent && node->parent->child_head == node) {
90 node->parent->child_head = node->next;
93 node->previous = NULL;
98 struct tree_node *tree_node_first(struct tree_node *list)
100 /* Grab the first node in this list from the parent if available. */
102 return list->parent->child_head;
105 while (list && list->previous) {
106 list = list->previous;
112 struct tree_node *tree_node_last(struct tree_node *list)
114 while (list && list->next) {
121 static uint32_t get_num_subkeys(struct tree_node *node)
123 const char *classname;
124 uint32_t num_subkeys;
126 NTTIME last_change_time;
127 uint32_t max_subkeynamelen;
128 uint32_t max_valnamelen;
129 uint32_t max_valbufsize;
132 rv = reg_key_get_info(node, node->key, &classname, &num_subkeys,
133 &num_values, &last_change_time,
134 &max_subkeynamelen, &max_valnamelen,
137 if (W_ERROR_IS_OK(rv)) {
144 WERROR tree_node_reopen_key(struct tree_node *node)
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,
153 bool tree_node_has_children(struct tree_node *node)
155 if (node->child_head) {
159 return get_num_subkeys(node) > 0;
162 static int node_cmp(struct tree_node **a, struct tree_node **b)
164 return strcmp((*a)->name, (*b)->name);
167 void tree_node_insert_sorted(struct tree_node *list, struct tree_node *node)
169 list = tree_node_first(list);
171 if (node_cmp(&list, &node) >= 0) {
172 tree_node_append(node, list);
174 list->parent->child_head = node;
179 while (list->next && node_cmp(&list->next, &node) < 0) {
183 tree_node_append(list, node);
186 WERROR tree_node_load_children(struct tree_node *node)
188 struct registry_key *key;
189 const char *key_name, *klass;
191 uint32_t i, nsubkeys, count;
193 struct tree_node *prev, **array;
195 /* does this node already have it's children loaded? */
196 if (node->child_head)
199 nsubkeys = get_num_subkeys(node);
203 array = talloc_zero_array(node, struct tree_node *, nsubkeys);
208 for (count = 0, i = 0; i < nsubkeys; ++i) {
209 rv = reg_key_get_subkey_by_index(node, node->key, i,
212 if (!W_ERROR_IS_OK(rv)) {
216 rv = reg_open_key(node, node->key, key_name, &key);
217 if (!W_ERROR_IS_OK(rv)) {
221 array[count] = tree_node_new(node, node, key_name, key);
222 if (array[count] == NULL) {
230 TYPESAFE_QSORT(array, count, node_cmp);
232 for (i = 1, prev = array[0]; i < count; ++i) {
233 tree_node_append(prev, array[i]);
236 node->child_head = array[0];
242 if (!W_ERROR_IS_OK(rv)) {
243 for (i = 0; i < nsubkeys; ++i) {
244 talloc_free(array[i]);
246 node->child_head = NULL;
253 void tree_node_free_recursive(struct tree_node *list)
255 struct tree_node *node;
261 while ((node = tree_node_pop(&list)) != NULL) {
262 if (node->child_head) {
263 tree_node_free_recursive(node->child_head);
265 node->child_head = NULL;
270 void tree_view_clear(struct tree_view *view)
272 multilist_set_data(view->list, NULL);
275 WERROR tree_view_update(struct tree_view *view, struct tree_node *list)
279 rv = multilist_set_data(view->list, list);
280 if (W_ERROR_IS_OK(rv)) {
281 multilist_refresh(view->list);
287 /* is this node in the current level? */
288 bool tree_view_is_node_visible(struct tree_view *view, struct tree_node *node)
290 const struct tree_node *first;
292 first = multilist_get_data(view->list);
294 return first && first->parent == node->parent;
297 void tree_view_set_current_node(struct tree_view *view, struct tree_node *node)
299 multilist_set_current_row(view->list, node);
302 struct tree_node *tree_view_get_current_node(struct tree_view *view)
304 const void *row = multilist_get_current_row(view->list);
305 return talloc_get_type_abort(row, struct tree_node);
308 void tree_view_driver(struct tree_view *view, int c)
310 multilist_driver(view->list, c);
313 void tree_view_set_selected(struct tree_view *view, bool select)
315 attr_t attr = A_NORMAL;
320 mvwchgat(view->window, 0, HEADING_X, 3, attr, 0, NULL);
323 void tree_view_show(struct tree_view *view)
325 multilist_refresh(view->list);
326 touchwin(view->window);
327 wnoutrefresh(view->window);
328 wnoutrefresh(view->sub);
331 static int tree_view_free(struct tree_view *view)
334 del_panel(view->panel);
340 delwin(view->window);
342 tree_node_free_recursive(view->root);
347 static const char *tv_get_column_header(const void *data, unsigned col)
349 SMB_ASSERT(col == 0);
353 static const void *tv_get_first_row(const void *data)
359 return talloc_get_type_abort(data, struct tree_node);
362 static const void *tv_get_next_row(const void *data, const void *row)
364 const struct tree_node *node;
365 SMB_ASSERT(row != NULL);
366 node = talloc_get_type_abort(row, struct tree_node);
370 static const void *tv_get_prev_row(const void *data, const void *row)
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;
378 static const char *tv_get_item_prefix(const void *row, unsigned col)
380 struct tree_node *node;
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)) {
391 static const char *tv_get_item_label(const void *row, unsigned col)
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);
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
409 struct tree_view *tree_view_new(TALLOC_CTX *ctx, struct tree_node *root,
410 int nlines, int ncols, int begin_y,
413 struct tree_view *view;
415 view = talloc_zero(ctx, struct tree_view);
420 talloc_set_destructor(view, tree_view_free);
422 view->window = newwin(nlines, ncols, begin_y, begin_x);
423 if (view->window == NULL) {
426 view->sub = subwin(view->window, nlines - 2, ncols - 2,
427 begin_y + 1, begin_x + 1);
428 if (view->sub == NULL) {
431 box(view->window, 0, 0);
432 mvwprintw(view->window, 0, HEADING_X, "Key");
434 view->panel = new_panel(view->window);
435 if (view->panel == NULL) {
440 view->list = multilist_new(view, view->sub, &tv_accessors, 1);
441 if (view->list == NULL) {
444 tree_view_update(view, root);
454 void tree_view_resize(struct tree_view *view, int nlines, int ncols,
455 int begin_y, int begin_x)
459 nwin = newwin(nlines, ncols, begin_y, begin_x);
463 nsub = subwin(nwin, nlines - 2, ncols - 2, begin_y + 1, begin_x + 1);
468 replace_panel(view->panel, nwin);
470 delwin(view->window);
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);
479 static void print_path_recursive(WINDOW *label, struct tree_node *node,
483 print_path_recursive(label, node->parent, len);
485 wprintw(label, "%s/", node->name);
486 *len += 1 + strlen(node->name);
489 /* print the path of node to label */
490 size_t tree_node_print_path(WINDOW *label, struct tree_node *node)
501 print_path_recursive(label, node->parent, &len);