Fix calls to smbc_init in testsmbc.c and tree.c
[Samba.git] / source3 / client / tree.c
blob941a09667740fc58e8f565166d71cef862dba048
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* example-gtk+ application, ripped off from the gtk+ tree.c sample */
26 #include <stdio.h>
27 #include <errno.h>
28 #include <gtk/gtk.h>
29 #include "libsmbclient.h"
31 static GtkWidget *clist;
33 struct tree_data {
35 guint32 type; /* Type of tree item, an SMBC_TYPE */
36 char name[256]; /* May need to change this later */
41 * We are given a widget, and we want to retrieve its URL so we
42 * can do a directory listing.
44 * We walk back up the tree, picking up pieces until we hit a server or
45 * workgroup type and return a path from there
48 static char path_string[1024];
50 char *get_path(GtkWidget *item)
52 GtkWidget *p = item;
53 struct tree_data *pd;
54 char *comps[1024]; /* We keep pointers to the components here */
55 int i = 0, j, level,type;
57 /* Walk back up the tree, getting the private data */
59 level = GTK_TREE(item->parent)->level;
61 /* Pick up this item's component info */
63 pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(item));
65 comps[i++] = pd->name;
66 type = pd->type;
68 while (level > 0 && type != SMBC_SERVER && type != SMBC_WORKGROUP) {
70 /* Find the parent and extract the data etc ... */
72 p = GTK_WIDGET(p->parent);
73 p = GTK_WIDGET(GTK_TREE(p)->tree_owner);
75 pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(p));
77 level = GTK_TREE(item->parent)->level;
79 comps[i++] = pd->name;
80 type = pd->type;
84 /*
85 * Got a list of comps now, should check that we did not hit a workgroup
86 * when we got other things as well ... Later
88 * Now, build the path
91 snprintf(path_string, sizeof(path_string), "smb:/");
93 for (j = i - 1; j >= 0; j--) {
95 strncat(path_string, "/", sizeof(path_string) - strlen(path_string));
96 strncat(path_string, comps[j], sizeof(path_string) - strlen(path_string));
100 fprintf(stdout, "Path string = %s\n", path_string);
102 return path_string;
106 struct tree_data *make_tree_data(guint32 type, const char *name)
108 struct tree_data *p = (struct tree_data *)malloc(sizeof(struct tree_data));
110 if (p) {
112 p->type = type;
113 strncpy(p->name, name, sizeof(p->name));
117 return p;
121 /* for all the GtkItem:: and GtkTreeItem:: signals */
122 static void cb_itemsignal( GtkWidget *item,
123 gchar *signame )
125 GtkWidget *real_tree, *aitem, *subtree;
126 gchar *name;
127 GtkLabel *label;
128 gint dh, err, dirlen, level;
129 char dirbuf[512];
130 struct smbc_dirent *dirp;
132 label = GTK_LABEL (GTK_BIN (item)->child);
133 /* Get the text of the label */
134 gtk_label_get (label, &name);
136 level = GTK_TREE(item->parent)->level;
138 /* Get the level of the tree which the item is in */
139 g_print ("%s called for item %s->%p, level %d\n", signame, name,
140 item, GTK_TREE (item->parent)->level);
142 if (strncmp(signame, "expand", 6) == 0) { /* Expand called */
143 char server[128];
145 if ((dh = smbc_opendir(get_path(item))) < 0) { /* Handle error */
147 g_print("cb_wholenet: Could not open dir %s, %s\n", get_path(item),
148 strerror(errno));
150 gtk_main_quit();
152 return;
156 real_tree = GTK_TREE_ITEM_SUBTREE(item); /* Get the subtree */
158 while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
159 sizeof(dirbuf))) != 0) {
161 if (err < 0) { /* An error, report it */
163 g_print("cb_wholenet: Could not read dir smbc://, %s\n",
164 strerror(errno));
166 gtk_main_quit();
168 return;
172 dirp = (struct smbc_dirent *)dirbuf;
174 while (err > 0) {
175 struct tree_data *my_data;
177 dirlen = dirp->dirlen;
179 my_data = make_tree_data(dirp->smbc_type, dirp->name);
181 if (!my_data) {
183 g_print("Could not allocate space for tree_data: %s\n",
184 dirp->name);
186 gtk_main_quit();
187 return;
191 aitem = gtk_tree_item_new_with_label(dirp->name);
193 /* Connect all GtkItem:: and GtkTreeItem:: signals */
194 gtk_signal_connect (GTK_OBJECT(aitem), "select",
195 GTK_SIGNAL_FUNC(cb_itemsignal), "select");
196 gtk_signal_connect (GTK_OBJECT(aitem), "deselect",
197 GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
198 gtk_signal_connect (GTK_OBJECT(aitem), "toggle",
199 GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
200 gtk_signal_connect (GTK_OBJECT(aitem), "expand",
201 GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
202 gtk_signal_connect (GTK_OBJECT(aitem), "collapse",
203 GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
204 /* Add it to the parent tree */
205 gtk_tree_append (GTK_TREE(real_tree), aitem);
207 gtk_widget_show (aitem);
209 gtk_object_set_user_data(GTK_OBJECT(aitem), (gpointer)my_data);
211 fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen);
213 if (dirp->smbc_type != SMBC_FILE &&
214 dirp->smbc_type != SMBC_IPC_SHARE &&
215 (strcmp(dirp->name, ".") != 0) &&
216 (strcmp(dirp->name, "..") !=0)){
218 subtree = gtk_tree_new();
219 gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem), subtree);
223 (char *)dirp += dirlen;
224 err -= dirlen;
230 smbc_closedir(dh);
236 /* Note that this is never called */
237 static void cb_unselect_child( GtkWidget *root_tree,
238 GtkWidget *child,
239 GtkWidget *subtree )
241 g_print ("unselect_child called for root tree %p, subtree %p, child %p\n",
242 root_tree, subtree, child);
245 /* Note that this is called every time the user clicks on an item,
246 whether it is already selected or not. */
247 static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
248 GtkWidget *subtree)
250 gint dh, err, dirlen;
251 char dirbuf[512];
252 struct smbc_dirent *dirp;
253 struct stat st1;
254 char path[1024], path1[1024];
256 g_print ("select_child called for root tree %p, subtree %p, child %p\n",
257 root_tree, subtree, child);
259 /* Now, figure out what it is, and display it in the clist ... */
261 gtk_clist_clear(GTK_CLIST(clist)); /* Clear the CLIST */
263 /* Now, get the private data for the subtree */
265 strncpy(path, get_path(child), 1024);
267 if ((dh = smbc_opendir(path)) < 0) { /* Handle error */
269 g_print("cb_select_child: Could not open dir %s, %s\n", path,
270 strerror(errno));
272 gtk_main_quit();
274 return;
278 while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
279 sizeof(dirbuf))) != 0) {
281 if (err < 0) {
283 g_print("cb_select_child: Could not read dir %s, %s\n", path,
284 strerror(errno));
286 gtk_main_quit();
288 return;
292 dirp = (struct smbc_dirent *)dirbuf;
294 while (err > 0) {
295 gchar col1[128], col2[128], col3[128], col4[128];
296 gchar *rowdata[4] = {col1, col2, col3, col4};
298 dirlen = dirp->dirlen;
300 /* Format each of the items ... */
302 strncpy(col1, dirp->name, 128);
304 col2[0] = col3[0] = col4[0] = (char)0;
306 switch (dirp->smbc_type) {
308 case SMBC_WORKGROUP:
310 break;
312 case SMBC_SERVER:
314 strncpy(col2, (dirp->comment?dirp->comment:""), 128);
316 break;
318 case SMBC_FILE_SHARE:
320 strncpy(col2, (dirp->comment?dirp->comment:""), 128);
322 break;
324 case SMBC_PRINTER_SHARE:
326 strncpy(col2, (dirp->comment?dirp->comment:""), 128);
327 break;
329 case SMBC_COMMS_SHARE:
331 break;
333 case SMBC_IPC_SHARE:
335 break;
337 case SMBC_DIR:
338 case SMBC_FILE:
340 /* Get stats on the file/dir and see what we have */
342 if ((strcmp(dirp->name, ".") != 0) &&
343 (strcmp(dirp->name, "..") != 0)) {
345 strncpy(path1, path, sizeof(path1));
346 strncat(path1, "/", sizeof(path) - strlen(path));
347 strncat(path1, dirp->name, sizeof(path) - strlen(path));
349 if (smbc_stat(path1, &st1) < 0) {
351 if (errno != EBUSY) {
353 g_print("cb_select_child: Could not stat file %s, %s\n", path1,
354 strerror(errno));
356 gtk_main_quit();
358 return;
361 else {
363 strncpy(col2, "Device or resource busy", sizeof(col2));
367 else {
368 /* Now format each of the relevant things ... */
370 snprintf(col2, sizeof(col2), "%s%s%s%s%s%s(%0X)",
371 (st1.st_mode&0x20?"A":""),
372 (st1.st_mode&0x10?"D":""),
373 (st1.st_mode&0x08?"V":""),
374 (st1.st_mode&0x04?"S":""),
375 (st1.st_mode&0x02?"H":""),
376 (st1.st_mode&0x01?"R":""),
377 st1.st_mode);
378 snprintf(col3, sizeof(col3), "%u", st1.st_size);
379 snprintf(col4, sizeof(col4), "%s", ctime(&st1.st_ctime));
383 break;
385 default:
387 break;
390 gtk_clist_append(GTK_CLIST(clist), rowdata);
392 (char *)dirp += dirlen;
393 err -= dirlen;
401 static void cb_selection_changed( GtkWidget *tree )
403 GList *i;
405 g_print ("selection_change called for tree %p\n", tree);
406 g_print ("selected objects are:\n");
408 i = GTK_TREE_SELECTION(tree);
409 while (i){
410 gchar *name;
411 GtkLabel *label;
412 GtkWidget *item;
414 /* Get a GtkWidget pointer from the list node */
415 item = GTK_WIDGET (i->data);
416 label = GTK_LABEL (GTK_BIN (item)->child);
417 gtk_label_get (label, &name);
418 g_print ("\t%s on level %d\n", name, GTK_TREE
419 (item->parent)->level);
420 i = i->next;
425 * Expand or collapse the whole network ...
427 static void cb_wholenet(GtkWidget *item, gchar *signame)
429 GtkWidget *real_tree, *aitem, *subtree;
430 gchar *name;
431 GtkLabel *label;
432 gint dh, err, dirlen;
433 char dirbuf[512];
434 struct smbc_dirent *dirp;
436 label = GTK_LABEL (GTK_BIN (item)->child);
437 gtk_label_get (label, &name);
438 g_print ("%s called for item %s->%p, level %d\n", signame, name,
439 item, GTK_TREE (item->parent)->level);
441 if (strncmp(signame, "expand", 6) == 0) { /* Expand called */
443 if ((dh = smbc_opendir("smb://")) < 0) { /* Handle error */
445 g_print("cb_wholenet: Could not open dir smbc://, %s\n",
446 strerror(errno));
448 gtk_main_quit();
450 return;
454 real_tree = GTK_TREE_ITEM_SUBTREE(item); /* Get the subtree */
456 while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
457 sizeof(dirbuf))) != 0) {
459 if (err < 0) { /* An error, report it */
461 g_print("cb_wholenet: Could not read dir smbc://, %s\n",
462 strerror(errno));
464 gtk_main_quit();
466 return;
470 dirp = (struct smbc_dirent *)dirbuf;
472 while (err > 0) {
473 struct tree_data *my_data;
475 dirlen = dirp->dirlen;
477 my_data = make_tree_data(dirp->smbc_type, dirp->name);
479 aitem = gtk_tree_item_new_with_label(dirp->name);
481 /* Connect all GtkItem:: and GtkTreeItem:: signals */
482 gtk_signal_connect (GTK_OBJECT(aitem), "select",
483 GTK_SIGNAL_FUNC(cb_itemsignal), "select");
484 gtk_signal_connect (GTK_OBJECT(aitem), "deselect",
485 GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
486 gtk_signal_connect (GTK_OBJECT(aitem), "toggle",
487 GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
488 gtk_signal_connect (GTK_OBJECT(aitem), "expand",
489 GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
490 gtk_signal_connect (GTK_OBJECT(aitem), "collapse",
491 GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
493 gtk_tree_append (GTK_TREE(real_tree), aitem);
494 /* Show it - this can be done at any time */
495 gtk_widget_show (aitem);
497 gtk_object_set_user_data(GTK_OBJECT(aitem), (gpointer)my_data);
499 fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen);
501 subtree = gtk_tree_new();
503 gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem), subtree);
505 (char *)dirp += dirlen;
506 err -= dirlen;
512 smbc_closedir(dh);
518 /* Should put up a dialog box to ask the user for username and password */
520 static void
521 auth_fn(const char *server, const char *share,
522 char *workgroup, int wgmaxlen, char *username, int unmaxlen,
523 char *password, int pwmaxlen)
526 strncpy(username, "test", unmaxlen);
527 strncpy(password, "test", pwmaxlen);
531 static char *col_titles[] = {
532 "Name", "Attributes", "Size", "Creation Date",
535 int main( int argc,
536 char *argv[] )
538 GtkWidget *window, *scrolled_win, *scrolled_win2, *tree;
539 GtkWidget *subtree, *item, *main_hbox, *r_pane, *l_pane;
540 gint err, dh;
541 gint i;
542 char dirbuf[512];
543 struct smbc_dirent *dirp;
545 gtk_init (&argc, &argv);
547 /* Init the smbclient library */
549 err = smbc_init(auth_fn, 10);
551 /* a generic toplevel window */
552 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
553 gtk_widget_set_name(window, "main browser window");
554 gtk_signal_connect (GTK_OBJECT(window), "delete_event",
555 GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
556 gtk_window_set_title(GTK_WINDOW(window), "The Linux Windows Network Browser");
557 gtk_widget_set_usize(GTK_WIDGET(window), 750, -1);
558 gtk_container_set_border_width (GTK_CONTAINER(window), 5);
560 gtk_widget_show (window);
562 /* A container for the two panes ... */
564 main_hbox = gtk_hbox_new(FALSE, 1);
565 gtk_container_border_width(GTK_CONTAINER(main_hbox), 1);
566 gtk_container_add(GTK_CONTAINER(window), main_hbox);
568 gtk_widget_show(main_hbox);
570 l_pane = gtk_hpaned_new();
571 gtk_paned_gutter_size(GTK_PANED(l_pane), (GTK_PANED(l_pane))->handle_size);
572 r_pane = gtk_hpaned_new();
573 gtk_paned_gutter_size(GTK_PANED(r_pane), (GTK_PANED(r_pane))->handle_size);
574 gtk_container_add(GTK_CONTAINER(main_hbox), l_pane);
575 gtk_widget_show(l_pane);
576 /*gtk_container_add(GTK_CONTAINER(main_hbox), r_pane);
577 gtk_widget_show(r_pane); */
580 /* A generic scrolled window */
581 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
582 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
583 GTK_POLICY_AUTOMATIC,
584 GTK_POLICY_AUTOMATIC);
585 gtk_widget_set_usize (scrolled_win, 150, 200);
586 gtk_container_add (GTK_CONTAINER(l_pane), scrolled_win);
587 gtk_widget_show (scrolled_win);
589 /* Another generic scrolled window */
590 scrolled_win2 = gtk_scrolled_window_new (NULL, NULL);
591 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win2),
592 GTK_POLICY_AUTOMATIC,
593 GTK_POLICY_AUTOMATIC);
594 gtk_widget_set_usize (scrolled_win2, 150, 200);
595 gtk_paned_add2 (GTK_PANED(l_pane), scrolled_win2);
596 gtk_widget_show (scrolled_win2);
598 /* Create the root tree */
599 tree = gtk_tree_new();
600 g_print ("root tree is %p\n", tree);
601 /* connect all GtkTree:: signals */
602 gtk_signal_connect (GTK_OBJECT(tree), "select_child",
603 GTK_SIGNAL_FUNC(cb_select_child), tree);
604 gtk_signal_connect (GTK_OBJECT(tree), "unselect_child",
605 GTK_SIGNAL_FUNC(cb_unselect_child), tree);
606 gtk_signal_connect (GTK_OBJECT(tree), "selection_changed",
607 GTK_SIGNAL_FUNC(cb_selection_changed), tree);
608 /* Add it to the scrolled window */
609 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrolled_win),
610 tree);
611 /* Set the selection mode */
612 gtk_tree_set_selection_mode (GTK_TREE(tree),
613 GTK_SELECTION_MULTIPLE);
614 /* Show it */
615 gtk_widget_show (tree);
617 /* Now, create a clist and attach it to the second pane */
619 clist = gtk_clist_new_with_titles(4, col_titles);
621 gtk_container_add (GTK_CONTAINER(scrolled_win2), clist);
623 gtk_widget_show(clist);
625 /* Now, build the top level display ... */
627 if ((dh = smbc_opendir("smb:///")) < 0) {
629 fprintf(stderr, "Could not list default workgroup: smb:///: %s\n",
630 strerror(errno));
632 exit(1);
636 /* Create a tree item for Whole Network */
638 item = gtk_tree_item_new_with_label ("Whole Network");
639 /* Connect all GtkItem:: and GtkTreeItem:: signals */
640 gtk_signal_connect (GTK_OBJECT(item), "select",
641 GTK_SIGNAL_FUNC(cb_itemsignal), "select");
642 gtk_signal_connect (GTK_OBJECT(item), "deselect",
643 GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
644 gtk_signal_connect (GTK_OBJECT(item), "toggle",
645 GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
646 gtk_signal_connect (GTK_OBJECT(item), "expand",
647 GTK_SIGNAL_FUNC(cb_wholenet), "expand");
648 gtk_signal_connect (GTK_OBJECT(item), "collapse",
649 GTK_SIGNAL_FUNC(cb_wholenet), "collapse");
650 /* Add it to the parent tree */
651 gtk_tree_append (GTK_TREE(tree), item);
652 /* Show it - this can be done at any time */
653 gtk_widget_show (item);
655 subtree = gtk_tree_new(); /* A subtree for Whole Network */
657 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree);
659 gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
660 GTK_SIGNAL_FUNC(cb_select_child), tree);
661 gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
662 GTK_SIGNAL_FUNC(cb_unselect_child), tree);
664 /* Now, get the items in smb:/// and add them to the tree */
666 dirp = (struct smbc_dirent *)dirbuf;
668 while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
669 sizeof(dirbuf))) != 0) {
671 if (err < 0) { /* Handle the error */
673 fprintf(stderr, "Could not read directory for smbc:///: %s\n",
674 strerror(errno));
676 exit(1);
680 fprintf(stdout, "Dir len: %u\n", err);
682 while (err > 0) { /* Extract each entry and make a sub-tree */
683 struct tree_data *my_data;
684 int dirlen = dirp->dirlen;
686 my_data = make_tree_data(dirp->smbc_type, dirp->name);
688 item = gtk_tree_item_new_with_label(dirp->name);
689 /* Connect all GtkItem:: and GtkTreeItem:: signals */
690 gtk_signal_connect (GTK_OBJECT(item), "select",
691 GTK_SIGNAL_FUNC(cb_itemsignal), "select");
692 gtk_signal_connect (GTK_OBJECT(item), "deselect",
693 GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
694 gtk_signal_connect (GTK_OBJECT(item), "toggle",
695 GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
696 gtk_signal_connect (GTK_OBJECT(item), "expand",
697 GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
698 gtk_signal_connect (GTK_OBJECT(item), "collapse",
699 GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
700 /* Add it to the parent tree */
701 gtk_tree_append (GTK_TREE(tree), item);
702 /* Show it - this can be done at any time */
703 gtk_widget_show (item);
705 gtk_object_set_user_data(GTK_OBJECT(item), (gpointer)my_data);
707 fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen);
709 subtree = gtk_tree_new();
711 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree);
713 gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
714 GTK_SIGNAL_FUNC(cb_select_child), tree);
715 gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
716 GTK_SIGNAL_FUNC(cb_unselect_child), tree);
718 (char *)dirp += dirlen;
719 err -= dirlen;
725 smbc_closedir(dh); /* FIXME, check for error :-) */
727 /* Show the window and loop endlessly */
728 gtk_main();
729 return 0;
731 /* example-end */