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 "lib/registry/registry.h"
23 struct tree_node
*tree_node_new(TALLOC_CTX
*ctx
, struct tree_node
*parent
,
24 const char *name
, struct registry_key
*key
)
26 struct tree_node
*node
;
28 node
= talloc_zero(ctx
, struct tree_node
);
33 node
->name
= talloc_strdup(node
, name
);
39 node
->key
= talloc_steal(node
, key
);
42 /* Check if this node is the first descendant of parent. */
43 if (!parent
->child_head
) {
44 parent
->child_head
= node
;
46 node
->parent
= parent
;
52 void tree_node_append(struct tree_node
*left
, struct tree_node
*right
)
55 right
->next
= left
->next
;
56 left
->next
->previous
= right
;
59 right
->previous
= left
;
62 void tree_node_append_last(struct tree_node
*list
, struct tree_node
*node
)
64 tree_node_append(tree_node_last(list
), node
);
67 struct tree_node
*tree_node_pop(struct tree_node
**plist
)
69 struct tree_node
*node
;
76 *plist
= node
->previous
;
81 node
->previous
->next
= node
->next
;
84 node
->next
->previous
= node
->previous
;
86 if (node
->parent
&& node
->parent
->child_head
== node
) {
87 node
->parent
->child_head
= node
->next
;
90 node
->previous
= NULL
;
95 struct tree_node
*tree_node_first(struct tree_node
*list
)
97 /* Grab the first node in this list from the parent if available. */
99 return list
->parent
->child_head
;
102 while (list
&& list
->previous
) {
103 list
= list
->previous
;
109 struct tree_node
*tree_node_last(struct tree_node
*list
)
111 while (list
&& list
->next
) {
118 bool tree_node_has_children(struct tree_node
*node
)
120 const char *classname
;
121 uint32_t num_subkeys
;
123 NTTIME last_change_time
;
124 uint32_t max_subkeynamelen
;
125 uint32_t max_valnamelen
;
126 uint32_t max_valbufsize
;
129 if (node
->child_head
) {
133 rv
= reg_key_get_info(node
, node
->key
, &classname
, &num_subkeys
,
134 &num_values
, &last_change_time
,
135 &max_subkeynamelen
, &max_valnamelen
,
138 if (W_ERROR_IS_OK(rv
)) {
139 return num_subkeys
!= 0;
145 WERROR
tree_node_load_children(struct tree_node
*node
)
147 struct registry_key
*key
;
148 const char *key_name
, *klass
;
152 struct tree_node
*new_node
, *prev
;
154 /* does this node already have it's children loaded? */
155 if (node
->child_head
)
158 for (prev
= NULL
, i
= 0; ; ++i
) {
159 rv
= reg_key_get_subkey_by_index(node
, node
->key
, i
,
162 if (!W_ERROR_IS_OK(rv
)) {
163 if (W_ERROR_EQUAL(rv
, WERR_NO_MORE_ITEMS
)) {
170 rv
= reg_open_key(node
, node
->key
, key_name
, &key
);
171 if (!W_ERROR_IS_OK(rv
)) {
175 new_node
= tree_node_new(node
, node
, key_name
, key
);
176 if (new_node
== NULL
) {
181 tree_node_append(prev
, new_node
);
189 void tree_node_free_recursive(struct tree_node
*list
)
191 struct tree_node
*node
;
197 while ((node
= tree_node_pop(&list
)) != NULL
) {
198 if (node
->child_head
) {
199 tree_node_free_recursive(node
->child_head
);
201 node
->child_head
= NULL
;
206 static void tree_view_free_current_items(ITEM
**items
)
209 struct tree_node
*node
;
216 for (i
= 0; items
[i
] != NULL
; ++i
) {
218 node
= item_userptr(item
);
219 if (node
&& node
->label
) {
220 talloc_free(node
->label
);
229 void tree_view_clear(struct tree_view
*view
)
231 unpost_menu(view
->menu
);
232 set_menu_items(view
->menu
, view
->empty
);
233 tree_view_free_current_items(view
->current_items
);
234 view
->current_items
= NULL
;
237 WERROR
tree_view_update(struct tree_view
*view
, struct tree_node
*list
)
240 struct tree_node
*node
;
246 for (n_items
= 0, node
= list
; node
!= NULL
; node
= node
->next
) {
250 items
= talloc_zero_array(view
, ITEM
*, n_items
+ 1);
255 for (i
= 0, node
= list
; node
!= NULL
; ++i
, node
= node
->next
) {
256 const char *label
= node
->name
;
258 /* Add a '+' marker to indicate that the item has
260 if (tree_node_has_children(node
)) {
261 SMB_ASSERT(node
->label
== NULL
);
262 node
->label
= talloc_asprintf(node
, "+%s", node
->name
);
263 if (node
->label
== NULL
) {
269 items
[i
] = new_item(label
, node
->name
);
270 set_item_userptr(items
[i
], node
);
273 unpost_menu(view
->menu
);
274 set_menu_items(view
->menu
, items
);
275 tree_view_free_current_items(view
->current_items
);
276 view
->current_items
= items
;
281 tree_view_free_current_items(items
);
286 void tree_view_show(struct tree_view
*view
)
288 post_menu(view
->menu
);
291 static int tree_view_free(struct tree_view
*view
)
294 unpost_menu(view
->menu
);
295 free_menu(view
->menu
);
297 if (view
->empty
[0]) {
298 free_item(view
->empty
[0]);
301 del_panel(view
->panel
);
304 delwin(view
->window
);
306 tree_view_free_current_items(view
->current_items
);
307 tree_node_free_recursive(view
->root
);
312 struct tree_view
*tree_view_new(TALLOC_CTX
*ctx
, struct tree_node
*root
,
313 int nlines
, int ncols
, int begin_y
,
316 struct tree_view
*view
;
317 static const char *dummy
= "(empty)";
319 view
= talloc_zero(ctx
, struct tree_view
);
324 talloc_set_destructor(view
, tree_view_free
);
326 view
->empty
[0] = new_item(dummy
, dummy
);
327 if (view
->empty
[0] == NULL
) {
330 view
->window
= newwin(nlines
, ncols
, begin_y
, begin_x
);
331 if (view
->window
== NULL
) {
334 view
->panel
= new_panel(view
->window
);
335 if (view
->panel
== NULL
) {
340 view
->menu
= new_menu(view
->empty
);
341 if (view
->menu
== NULL
) {
344 set_menu_format(view
->menu
, nlines
, 1);
345 set_menu_win(view
->menu
, view
->window
);
346 menu_opts_off(view
->menu
, O_SHOWDESC
);
347 set_menu_mark(view
->menu
, "* ");
349 tree_view_update(view
, root
);
359 void tree_view_resize(struct tree_view
*view
, int nlines
, int ncols
,
360 int begin_y
, int begin_x
)
364 unpost_menu(view
->menu
);
365 nwin
= newwin(nlines
, ncols
, begin_y
, begin_x
);
366 replace_panel(view
->panel
, nwin
);
367 delwin(view
->window
);
369 set_menu_format(view
->menu
, nlines
, 1);
370 set_menu_win(view
->menu
, view
->window
);
371 post_menu(view
->menu
);
374 static void print_path_recursive(WINDOW
*label
, struct tree_node
*node
,
378 print_path_recursive(label
, node
->parent
, len
);
380 wprintw(label
, "%s/", node
->name
);
381 *len
+= 1 + strlen(node
->name
);
384 /* print the path of node to label */
385 size_t tree_node_print_path(WINDOW
*label
, struct tree_node
*node
)
396 print_path_recursive(label
, node
->parent
, &len
);