messaging3: The backend send_fn doesn't need a messaging_context
[Samba.git] / examples / libsmbclient / tree.c
blob6e34cd05627c4d47ee05b654e0329baeadf6ba4e
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 SMB client GTK+ tree-based application
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Richard Sharpe 2001
7 Copyright (C) John Terpstra 2001
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* example-gtk+ application, ripped off from the gtk+ tree.c sample */
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <gtk/gtk.h>
30 #include "libsmbclient.h"
32 static GtkWidget *clist;
34 struct tree_data {
36 guint32 type; /* Type of tree item, an SMBC_TYPE */
37 char name[256]; /* May need to change this later */
41 void error_message(gchar *message) {
43 GtkWidget *dialog, *label, *okay_button;
45 /* Create the widgets */
47 dialog = gtk_dialog_new();
48 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
49 label = gtk_label_new (message);
50 okay_button = gtk_button_new_with_label("Okay");
52 /* Ensure that the dialog box is destroyed when the user clicks ok. */
54 gtk_signal_connect_object (GTK_OBJECT (okay_button), "clicked",
55 GTK_SIGNAL_FUNC (gtk_widget_destroy),
56 GTK_OBJECT(dialog));
57 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area),
58 okay_button);
60 /* Add the label, and show everything we've added to the dialog. */
62 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
63 label);
64 gtk_widget_show_all (dialog);
68 * We are given a widget, and we want to retrieve its URL so we
69 * can do a directory listing.
71 * We walk back up the tree, picking up pieces until we hit a server or
72 * workgroup type and return a path from there
75 static char path_string[1024];
77 char *get_path(GtkWidget *item)
79 GtkWidget *p = item;
80 struct tree_data *pd;
81 char *comps[1024]; /* We keep pointers to the components here */
82 int i = 0, j, level,type;
84 /* Walk back up the tree, getting the private data */
86 level = GTK_TREE(item->parent)->level;
88 /* Pick up this item's component info */
90 pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(item));
92 comps[i++] = pd->name;
93 type = pd->type;
95 while (level > 0 && type != SMBC_SERVER && type != SMBC_WORKGROUP) {
97 /* Find the parent and extract the data etc ... */
99 p = GTK_WIDGET(p->parent);
100 p = GTK_WIDGET(GTK_TREE(p)->tree_owner);
102 pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(p));
104 level = GTK_TREE(item->parent)->level;
106 comps[i++] = pd->name;
107 type = pd->type;
112 * Got a list of comps now, should check that we did not hit a workgroup
113 * when we got other things as well ... Later
115 * Now, build the path
118 snprintf(path_string, sizeof(path_string), "smb:/");
120 for (j = i - 1; j >= 0; j--) {
122 strncat(path_string, "/", sizeof(path_string) - strlen(path_string));
123 strncat(path_string, comps[j], sizeof(path_string) - strlen(path_string));
127 fprintf(stdout, "Path string = %s\n", path_string);
129 return path_string;
133 struct tree_data *make_tree_data(guint32 type, const char *name)
135 struct tree_data *p = (struct tree_data *)malloc(sizeof(struct tree_data));
137 if (p) {
139 p->type = type;
140 strncpy(p->name, name, sizeof(p->name));
144 return p;
148 /* Note that this is called every time the user clicks on an item,
149 whether it is already selected or not. */
150 static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
151 GtkWidget *subtree)
153 gint dh, err, dirlen;
154 char dirbuf[512];
155 struct smbc_dirent *dirp;
156 struct stat st1;
157 char path[1024], path1[1024];
159 g_print ("select_child called for root tree %p, subtree %p, child %p\n",
160 root_tree, subtree, child);
162 /* Now, figure out what it is, and display it in the clist ... */
164 gtk_clist_clear(GTK_CLIST(clist)); /* Clear the CLIST */
166 /* Now, get the private data for the subtree */
168 strncpy(path, get_path(child), 1024);
170 if ((dh = smbc_opendir(path)) < 0) { /* Handle error */
172 g_print("cb_select_child: Could not open dir %s, %s\n", path,
173 strerror(errno));
175 gtk_main_quit();
177 return;
181 while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
182 sizeof(dirbuf))) != 0) {
184 if (err < 0) {
186 g_print("cb_select_child: Could not read dir %s, %s\n", path,
187 strerror(errno));
189 gtk_main_quit();
191 return;
195 dirp = (struct smbc_dirent *)dirbuf;
197 while (err > 0) {
198 gchar col1[128], col2[128], col3[128], col4[128];
199 gchar *rowdata[4] = {col1, col2, col3, col4};
201 dirlen = dirp->dirlen;
203 /* Format each of the items ... */
205 strncpy(col1, dirp->name, 128);
207 col2[0] = col3[0] = col4[0] = (char)0;
209 switch (dirp->smbc_type) {
211 case SMBC_WORKGROUP:
213 break;
215 case SMBC_SERVER:
217 strncpy(col2, (dirp->comment?dirp->comment:""), 128);
219 break;
221 case SMBC_FILE_SHARE:
223 strncpy(col2, (dirp->comment?dirp->comment:""), 128);
225 break;
227 case SMBC_PRINTER_SHARE:
229 strncpy(col2, (dirp->comment?dirp->comment:""), 128);
230 break;
232 case SMBC_COMMS_SHARE:
234 break;
236 case SMBC_IPC_SHARE:
238 break;
240 case SMBC_DIR:
241 case SMBC_FILE:
243 /* Get stats on the file/dir and see what we have */
245 if ((strcmp(dirp->name, ".") != 0) &&
246 (strcmp(dirp->name, "..") != 0)) {
248 strncpy(path1, path, sizeof(path1));
249 strncat(path1, "/", sizeof(path) - strlen(path));
250 strncat(path1, dirp->name, sizeof(path) - strlen(path));
252 if (smbc_stat(path1, &st1) < 0) {
254 if (errno != EBUSY) {
256 g_print("cb_select_child: Could not stat file %s, %s\n", path1,
257 strerror(errno));
259 gtk_main_quit();
261 return;
264 else {
266 strncpy(col2, "Device or resource busy", sizeof(col2));
270 else {
271 /* Now format each of the relevant things ... */
273 snprintf(col2, sizeof(col2), "%c%c%c%c%c%c%c%c%c(%0X)",
274 (st1.st_mode&S_IRUSR?'r':'-'),
275 (st1.st_mode&S_IWUSR?'w':'-'),
276 (st1.st_mode&S_IXUSR?'x':'-'),
277 (st1.st_mode&S_IRGRP?'r':'-'),
278 (st1.st_mode&S_IWGRP?'w':'-'),
279 (st1.st_mode&S_IXGRP?'x':'-'),
280 (st1.st_mode&S_IROTH?'r':'-'),
281 (st1.st_mode&S_IWOTH?'w':'-'),
282 (st1.st_mode&S_IXOTH?'x':'-'),
283 st1.st_mode);
284 snprintf(col3, sizeof(col3), "%u", st1.st_size);
285 snprintf(col4, sizeof(col4), "%s", ctime(&st1.st_mtime));
289 break;
291 default:
293 break;
296 gtk_clist_append(GTK_CLIST(clist), rowdata);
298 dirp = (struct smbc_dirent *) ((char *) dirp + dirlen);
299 err -= dirlen;
307 /* Note that this is never called */
308 static void cb_unselect_child( GtkWidget *root_tree,
309 GtkWidget *child,
310 GtkWidget *subtree )
312 g_print ("unselect_child called for root tree %p, subtree %p, child %p\n",
313 root_tree, subtree, child);
316 /* for all the GtkItem:: and GtkTreeItem:: signals */
317 static void cb_itemsignal( GtkWidget *item,
318 gchar *signame )
320 GtkWidget *real_tree, *aitem, *subtree;
321 gchar *name;
322 GtkLabel *label;
323 gint dh, err, dirlen, level;
324 char dirbuf[512];
325 struct smbc_dirent *dirp;
327 label = GTK_LABEL (GTK_BIN (item)->child);
328 /* Get the text of the label */
329 gtk_label_get (label, &name);
331 level = GTK_TREE(item->parent)->level;
333 /* Get the level of the tree which the item is in */
334 g_print ("%s called for item %s->%p, level %d\n", signame, name,
335 item, GTK_TREE (item->parent)->level);
337 real_tree = GTK_TREE_ITEM_SUBTREE(item); /* Get the subtree */
339 if (strncmp(signame, "expand", 6) == 0) { /* Expand called */
340 char server[128];
342 if ((dh = smbc_opendir(get_path(item))) < 0) { /* Handle error */
343 gchar errmsg[256];
345 g_print("cb_itemsignal: Could not open dir %s, %s\n", get_path(item),
346 strerror(errno));
348 snprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not open dir %s, %s\n", get_path(item), strerror(errno));
350 error_message(errmsg);
352 /* gtk_main_quit();*/
354 return;
358 while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
359 sizeof(dirbuf))) != 0) {
361 if (err < 0) { /* An error, report it */
362 gchar errmsg[256];
364 g_print("cb_itemsignal: Could not read dir smbc://, %s\n",
365 strerror(errno));
367 snprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not read dir smbc://, %s\n", strerror(errno));
369 error_message(errmsg);
371 /* gtk_main_quit();*/
373 return;
377 dirp = (struct smbc_dirent *)dirbuf;
379 while (err > 0) {
380 struct tree_data *my_data;
382 dirlen = dirp->dirlen;
384 my_data = make_tree_data(dirp->smbc_type, dirp->name);
386 if (!my_data) {
388 g_print("Could not allocate space for tree_data: %s\n",
389 dirp->name);
391 gtk_main_quit();
392 return;
396 aitem = gtk_tree_item_new_with_label(dirp->name);
398 /* Connect all GtkItem:: and GtkTreeItem:: signals */
399 gtk_signal_connect (GTK_OBJECT(aitem), "select",
400 GTK_SIGNAL_FUNC(cb_itemsignal), "select");
401 gtk_signal_connect (GTK_OBJECT(aitem), "deselect",
402 GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
403 gtk_signal_connect (GTK_OBJECT(aitem), "toggle",
404 GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
405 gtk_signal_connect (GTK_OBJECT(aitem), "expand",
406 GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
407 gtk_signal_connect (GTK_OBJECT(aitem), "collapse",
408 GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
409 /* Add it to the parent tree */
410 gtk_tree_append (GTK_TREE(real_tree), aitem);
412 gtk_widget_show (aitem);
414 gtk_object_set_user_data(GTK_OBJECT(aitem), (gpointer)my_data);
416 fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen);
418 if (dirp->smbc_type != SMBC_FILE &&
419 dirp->smbc_type != SMBC_IPC_SHARE &&
420 (strcmp(dirp->name, ".") != 0) &&
421 (strcmp(dirp->name, "..") !=0)){
423 subtree = gtk_tree_new();
424 gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem), subtree);
426 gtk_signal_connect(GTK_OBJECT(subtree), "select_child",
427 GTK_SIGNAL_FUNC(cb_select_child), real_tree);
428 gtk_signal_connect(GTK_OBJECT(subtree), "unselect_child",
429 GTK_SIGNAL_FUNC(cb_unselect_child), real_tree);
433 dirp = (struct smbc_dirent *) ((char *) dirp + dirlen);
434 err -= dirlen;
440 smbc_closedir(dh);
443 else if (strncmp(signame, "collapse", 8) == 0) {
444 GtkWidget *subtree = gtk_tree_new();
446 gtk_tree_remove_items(GTK_TREE(real_tree), GTK_TREE(real_tree)->children);
448 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree);
450 gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
451 GTK_SIGNAL_FUNC(cb_select_child), real_tree);
452 gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
453 GTK_SIGNAL_FUNC(cb_unselect_child), real_tree);
459 static void cb_selection_changed( GtkWidget *tree )
461 GList *i;
463 g_print ("selection_change called for tree %p\n", tree);
464 g_print ("selected objects are:\n");
466 i = GTK_TREE_SELECTION(tree);
467 while (i){
468 gchar *name;
469 GtkLabel *label;
470 GtkWidget *item;
472 /* Get a GtkWidget pointer from the list node */
473 item = GTK_WIDGET (i->data);
474 label = GTK_LABEL (GTK_BIN (item)->child);
475 gtk_label_get (label, &name);
476 g_print ("\t%s on level %d\n", name, GTK_TREE
477 (item->parent)->level);
478 i = i->next;
483 * Expand or collapse the whole network ...
485 static void cb_wholenet(GtkWidget *item, gchar *signame)
487 GtkWidget *real_tree, *aitem, *subtree;
488 gchar *name;
489 GtkLabel *label;
490 gint dh, err, dirlen;
491 char dirbuf[512];
492 struct smbc_dirent *dirp;
494 label = GTK_LABEL (GTK_BIN (item)->child);
495 gtk_label_get (label, &name);
496 g_print ("%s called for item %s->%p, level %d\n", signame, name,
497 item, GTK_TREE (item->parent)->level);
499 real_tree = GTK_TREE_ITEM_SUBTREE(item); /* Get the subtree */
501 if (strncmp(signame, "expand", 6) == 0) { /* Expand called */
503 if ((dh = smbc_opendir("smb://")) < 0) { /* Handle error */
505 g_print("cb_wholenet: Could not open dir smbc://, %s\n",
506 strerror(errno));
508 gtk_main_quit();
510 return;
514 while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
515 sizeof(dirbuf))) != 0) {
517 if (err < 0) { /* An error, report it */
519 g_print("cb_wholenet: Could not read dir smbc://, %s\n",
520 strerror(errno));
522 gtk_main_quit();
524 return;
528 dirp = (struct smbc_dirent *)dirbuf;
530 while (err > 0) {
531 struct tree_data *my_data;
533 dirlen = dirp->dirlen;
535 my_data = make_tree_data(dirp->smbc_type, dirp->name);
537 aitem = gtk_tree_item_new_with_label(dirp->name);
539 /* Connect all GtkItem:: and GtkTreeItem:: signals */
540 gtk_signal_connect (GTK_OBJECT(aitem), "select",
541 GTK_SIGNAL_FUNC(cb_itemsignal), "select");
542 gtk_signal_connect (GTK_OBJECT(aitem), "deselect",
543 GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
544 gtk_signal_connect (GTK_OBJECT(aitem), "toggle",
545 GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
546 gtk_signal_connect (GTK_OBJECT(aitem), "expand",
547 GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
548 gtk_signal_connect (GTK_OBJECT(aitem), "collapse",
549 GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
551 gtk_tree_append (GTK_TREE(real_tree), aitem);
552 /* Show it - this can be done at any time */
553 gtk_widget_show (aitem);
555 gtk_object_set_user_data(GTK_OBJECT(aitem), (gpointer)my_data);
557 fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen);
559 subtree = gtk_tree_new();
561 gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem), subtree);
563 gtk_signal_connect(GTK_OBJECT(subtree), "select_child",
564 GTK_SIGNAL_FUNC(cb_select_child), real_tree);
565 gtk_signal_connect(GTK_OBJECT(subtree), "unselect_child",
566 GTK_SIGNAL_FUNC(cb_unselect_child), real_tree);
568 dirp = (struct smbc_dirent *) ((char *) dirp + dirlen);
569 err -= dirlen;
575 smbc_closedir(dh);
578 else { /* Must be collapse ... FIXME ... */
579 GtkWidget *subtree = gtk_tree_new();
581 gtk_tree_remove_items(GTK_TREE(real_tree), GTK_TREE(real_tree)->children);
583 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree);
585 gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
586 GTK_SIGNAL_FUNC(cb_select_child), real_tree);
587 gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
588 GTK_SIGNAL_FUNC(cb_unselect_child), real_tree);
595 /* Should put up a dialog box to ask the user for username and password */
597 static void
598 auth_fn(const char *server, const char *share,
599 char *workgroup, int wgmaxlen, char *username, int unmaxlen,
600 char *password, int pwmaxlen)
603 strncpy(username, "test", unmaxlen);
604 strncpy(password, "test", pwmaxlen);
608 static char *col_titles[] = {
609 "Name", "Attributes", "Size", "Modification Date",
612 int main( int argc,
613 char *argv[] )
615 GtkWidget *window, *scrolled_win, *scrolled_win2, *tree;
616 GtkWidget *subtree, *item, *main_hbox, *r_pane, *l_pane;
617 gint err, dh;
618 gint i;
619 char dirbuf[512];
620 struct smbc_dirent *dirp;
622 gtk_init (&argc, &argv);
624 /* Init the smbclient library */
626 err = smbc_init(auth_fn, 10);
628 /* Print an error response ... */
630 if (err < 0) {
632 fprintf(stderr, "smbc_init returned %s (%i)\nDo you have a ~/.smb/smb.conf file?\n", strerror(errno), errno);
633 exit(1);
637 /* a generic toplevel window */
638 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
639 gtk_widget_set_name(window, "main browser window");
640 gtk_signal_connect (GTK_OBJECT(window), "delete_event",
641 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
642 gtk_window_set_title(GTK_WINDOW(window), "The Linux Windows Network Browser");
643 gtk_widget_set_usize(GTK_WIDGET(window), 750, -1);
644 gtk_container_set_border_width (GTK_CONTAINER(window), 5);
646 gtk_widget_show (window);
648 /* A container for the two panes ... */
650 main_hbox = gtk_hbox_new(FALSE, 1);
651 gtk_container_border_width(GTK_CONTAINER(main_hbox), 1);
652 gtk_container_add(GTK_CONTAINER(window), main_hbox);
654 gtk_widget_show(main_hbox);
656 l_pane = gtk_hpaned_new();
657 gtk_paned_gutter_size(GTK_PANED(l_pane), (GTK_PANED(l_pane))->handle_size);
658 r_pane = gtk_hpaned_new();
659 gtk_paned_gutter_size(GTK_PANED(r_pane), (GTK_PANED(r_pane))->handle_size);
660 gtk_container_add(GTK_CONTAINER(main_hbox), l_pane);
661 gtk_widget_show(l_pane);
663 /* A generic scrolled window */
664 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
665 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
666 GTK_POLICY_AUTOMATIC,
667 GTK_POLICY_AUTOMATIC);
668 gtk_widget_set_usize (scrolled_win, 150, 200);
669 gtk_container_add (GTK_CONTAINER(l_pane), scrolled_win);
670 gtk_widget_show (scrolled_win);
672 /* Another generic scrolled window */
673 scrolled_win2 = gtk_scrolled_window_new (NULL, NULL);
674 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win2),
675 GTK_POLICY_AUTOMATIC,
676 GTK_POLICY_AUTOMATIC);
677 gtk_widget_set_usize (scrolled_win2, 150, 200);
678 gtk_paned_add2 (GTK_PANED(l_pane), scrolled_win2);
679 gtk_widget_show (scrolled_win2);
681 /* Create the root tree */
682 tree = gtk_tree_new();
683 g_print ("root tree is %p\n", tree);
684 /* connect all GtkTree:: signals */
685 gtk_signal_connect (GTK_OBJECT(tree), "select_child",
686 GTK_SIGNAL_FUNC(cb_select_child), tree);
687 gtk_signal_connect (GTK_OBJECT(tree), "unselect_child",
688 GTK_SIGNAL_FUNC(cb_unselect_child), tree);
689 gtk_signal_connect (GTK_OBJECT(tree), "selection_changed",
690 GTK_SIGNAL_FUNC(cb_selection_changed), tree);
691 /* Add it to the scrolled window */
692 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrolled_win),
693 tree);
694 /* Set the selection mode */
695 gtk_tree_set_selection_mode (GTK_TREE(tree),
696 GTK_SELECTION_MULTIPLE);
697 /* Show it */
698 gtk_widget_show (tree);
700 /* Now, create a clist and attach it to the second pane */
702 clist = gtk_clist_new_with_titles(4, col_titles);
704 gtk_container_add (GTK_CONTAINER(scrolled_win2), clist);
706 gtk_widget_show(clist);
708 /* Now, build the top level display ... */
710 if ((dh = smbc_opendir("smb://")) < 0) {
712 fprintf(stderr, "Could not list workgroups: smb://: %s\n",
713 strerror(errno));
715 exit(1);
719 /* Create a tree item for Whole Network */
721 item = gtk_tree_item_new_with_label ("Whole Network");
722 /* Connect all GtkItem:: and GtkTreeItem:: signals */
723 gtk_signal_connect (GTK_OBJECT(item), "select",
724 GTK_SIGNAL_FUNC(cb_itemsignal), "select");
725 gtk_signal_connect (GTK_OBJECT(item), "deselect",
726 GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
727 gtk_signal_connect (GTK_OBJECT(item), "toggle",
728 GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
729 gtk_signal_connect (GTK_OBJECT(item), "expand",
730 GTK_SIGNAL_FUNC(cb_wholenet), "expand");
731 gtk_signal_connect (GTK_OBJECT(item), "collapse",
732 GTK_SIGNAL_FUNC(cb_wholenet), "collapse");
733 /* Add it to the parent tree */
734 gtk_tree_append (GTK_TREE(tree), item);
735 /* Show it - this can be done at any time */
736 gtk_widget_show (item);
738 subtree = gtk_tree_new(); /* A subtree for Whole Network */
740 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree);
742 gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
743 GTK_SIGNAL_FUNC(cb_select_child), tree);
744 gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
745 GTK_SIGNAL_FUNC(cb_unselect_child), tree);
747 /* Now, get the items in smb:/// and add them to the tree */
749 while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
750 sizeof(dirbuf))) != 0) {
752 if (err < 0) { /* Handle the error */
754 fprintf(stderr, "Could not read directory for smbc:///: %s\n",
755 strerror(errno));
757 exit(1);
761 dirp = (struct smbc_dirent *)dirbuf;
763 fprintf(stdout, "Dir len: %u\n", err);
765 while (err > 0) { /* Extract each entry and make a sub-tree */
766 struct tree_data *my_data;
767 int dirlen = dirp->dirlen;
769 my_data = make_tree_data(dirp->smbc_type, dirp->name);
771 item = gtk_tree_item_new_with_label(dirp->name);
772 /* Connect all GtkItem:: and GtkTreeItem:: signals */
773 gtk_signal_connect (GTK_OBJECT(item), "select",
774 GTK_SIGNAL_FUNC(cb_itemsignal), "select");
775 gtk_signal_connect (GTK_OBJECT(item), "deselect",
776 GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
777 gtk_signal_connect (GTK_OBJECT(item), "toggle",
778 GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
779 gtk_signal_connect (GTK_OBJECT(item), "expand",
780 GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
781 gtk_signal_connect (GTK_OBJECT(item), "collapse",
782 GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
783 /* Add it to the parent tree */
784 gtk_tree_append (GTK_TREE(tree), item);
785 /* Show it - this can be done at any time */
786 gtk_widget_show (item);
788 gtk_object_set_user_data(GTK_OBJECT(item), (gpointer)my_data);
790 fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen);
792 subtree = gtk_tree_new();
794 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree);
796 gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
797 GTK_SIGNAL_FUNC(cb_select_child), tree);
798 gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
799 GTK_SIGNAL_FUNC(cb_unselect_child), tree);
801 dirp = (struct smbc_dirent *) ((char *) dirp + dirlen);
802 err -= dirlen;
808 smbc_closedir(dh); /* FIXME, check for error :-) */
810 /* Show the window and loop endlessly */
811 gtk_main();
812 return 0;
814 /* example-end */