2 Unix SMB/Netbios implementation.
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 */
29 #include "libsmbclient.h"
31 /* for all the GtkItem:: and GtkTreeItem:: signals */
32 static void cb_itemsignal( GtkWidget
*item
,
35 GtkWidget
*real_tree
, *aitem
, *subtree
;
40 struct smbc_dirent
*dirp
;
42 /* It's a Bin, so it has one child, which we know to be a
44 label
= GTK_LABEL (GTK_BIN (item
)->child
);
45 /* Get the text of the label */
46 gtk_label_get (label
, &name
);
47 /* Get the level of the tree which the item is in */
48 g_print ("%s called for item %s->%p, level %d\n", signame
, name
,
49 item
, GTK_TREE (item
->parent
)->level
);
51 if (strncmp(signame
, "expand", 6) == 0) { /* Expand called */
55 slprintf(server
, 128, "smb://%s", name
);
57 if ((dh
= smbc_opendir(server
)) < 0) { /* Handle error */
59 g_print("cb_wholenet: Could not open dir %s, %s\n", server
,
68 real_tree
= GTK_TREE_ITEM_SUBTREE(item
); /* Get the subtree */
70 while ((err
= smbc_getdents(dh
, (struct smbc_dirent
*)dirbuf
,
71 sizeof(dirbuf
))) != 0) {
73 if (err
< 0) { /* An error, report it */
75 g_print("cb_wholenet: Could not read dir smbc://, %s\n",
84 dirp
= (struct smbc_dirent
*)dirbuf
;
88 dirlen
= sizeof(struct smbc_dirent
) + dirp
->namelen
+
91 aitem
= gtk_tree_item_new_with_label(dirp
->name
);
93 /* Connect all GtkItem:: and GtkTreeItem:: signals */
94 gtk_signal_connect (GTK_OBJECT(aitem
), "select",
95 GTK_SIGNAL_FUNC(cb_itemsignal
), "select");
96 gtk_signal_connect (GTK_OBJECT(aitem
), "deselect",
97 GTK_SIGNAL_FUNC(cb_itemsignal
), "deselect");
98 gtk_signal_connect (GTK_OBJECT(aitem
), "toggle",
99 GTK_SIGNAL_FUNC(cb_itemsignal
), "toggle");
100 gtk_signal_connect (GTK_OBJECT(aitem
), "expand",
101 GTK_SIGNAL_FUNC(cb_itemsignal
), "expand");
102 gtk_signal_connect (GTK_OBJECT(aitem
), "collapse",
103 GTK_SIGNAL_FUNC(cb_itemsignal
), "collapse");
104 /* Add it to the parent tree */
105 gtk_tree_append (GTK_TREE(real_tree
), aitem
);
106 /* Show it - this can be done at any time */
107 gtk_widget_show (aitem
);
109 fprintf(stdout
, "Added: %s, len: %u\n", dirp
->name
, dirlen
);
111 subtree
= gtk_tree_new();
113 gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem
), subtree
);
115 (char *)dirp
+= dirlen
;
128 /* Note that this is never called */
129 static void cb_unselect_child( GtkWidget
*root_tree
,
133 g_print ("unselect_child called for root tree %p, subtree %p, child %p\n",
134 root_tree
, subtree
, child
);
137 /* Note that this is called every time the user clicks on an item,
138 whether it is already selected or not. */
139 static void cb_select_child (GtkWidget
*root_tree
, GtkWidget
*child
,
142 g_print ("select_child called for root tree %p, subtree %p, child %p\n",
143 root_tree
, subtree
, child
);
146 static void cb_selection_changed( GtkWidget
*tree
)
150 g_print ("selection_change called for tree %p\n", tree
);
151 g_print ("selected objects are:\n");
153 i
= GTK_TREE_SELECTION(tree
);
159 /* Get a GtkWidget pointer from the list node */
160 item
= GTK_WIDGET (i
->data
);
161 label
= GTK_LABEL (GTK_BIN (item
)->child
);
162 gtk_label_get (label
, &name
);
163 g_print ("\t%s on level %d\n", name
, GTK_TREE
164 (item
->parent
)->level
);
170 * Expand or collapse the whole network ...
172 static void cb_wholenet(GtkWidget
*item
, gchar
*signame
)
174 GtkWidget
*real_tree
, *aitem
, *subtree
;
177 gint dh
, err
, dirlen
;
179 struct smbc_dirent
*dirp
;
181 label
= GTK_LABEL (GTK_BIN (item
)->child
);
182 gtk_label_get (label
, &name
);
183 g_print ("%s called for item %s->%p, level %d\n", signame
, name
,
184 item
, GTK_TREE (item
->parent
)->level
);
186 if (strncmp(signame
, "expand", 6) == 0) { /* Expand called */
188 if ((dh
= smbc_opendir("smb://")) < 0) { /* Handle error */
190 g_print("cb_wholenet: Could not open dir smbc://, %s\n",
199 real_tree
= GTK_TREE_ITEM_SUBTREE(item
); /* Get the subtree */
201 while ((err
= smbc_getdents(dh
, (struct smbc_dirent
*)dirbuf
,
202 sizeof(dirbuf
))) != 0) {
204 if (err
< 0) { /* An error, report it */
206 g_print("cb_wholenet: Could not read dir smbc://, %s\n",
215 dirp
= (struct smbc_dirent
*)dirbuf
;
219 dirlen
= sizeof(struct smbc_dirent
) + dirp
->namelen
+
220 dirp
->commentlen
+ 1;
222 aitem
= gtk_tree_item_new_with_label(dirp
->name
);
224 /* Connect all GtkItem:: and GtkTreeItem:: signals */
225 gtk_signal_connect (GTK_OBJECT(aitem
), "select",
226 GTK_SIGNAL_FUNC(cb_itemsignal
), "select");
227 gtk_signal_connect (GTK_OBJECT(aitem
), "deselect",
228 GTK_SIGNAL_FUNC(cb_itemsignal
), "deselect");
229 gtk_signal_connect (GTK_OBJECT(aitem
), "toggle",
230 GTK_SIGNAL_FUNC(cb_itemsignal
), "toggle");
231 gtk_signal_connect (GTK_OBJECT(aitem
), "expand",
232 GTK_SIGNAL_FUNC(cb_itemsignal
), "expand");
233 gtk_signal_connect (GTK_OBJECT(aitem
), "collapse",
234 GTK_SIGNAL_FUNC(cb_itemsignal
), "collapse");
235 /* Add it to the parent tree */
236 gtk_tree_append (GTK_TREE(real_tree
), aitem
);
237 /* Show it - this can be done at any time */
238 gtk_widget_show (aitem
);
240 fprintf(stdout
, "Added: %s, len: %u\n", dirp
->name
, dirlen
);
242 subtree
= gtk_tree_new();
244 gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem
), subtree
);
246 (char *)dirp
+= dirlen
;
257 /* Create this item's subtree */
258 /* subtree = gtk_tree_new();
259 g_print ("-> item %s->%p, subtree %p\n", "Whole Network", item,
262 /* This is still necessary if you want these signals to be called
263 for the subtree's children. Note that selection_change will be
264 signalled for the root tree regardless. */
265 /* gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
266 GTK_SIGNAL_FUNC(cb_select_child), subtree);
267 gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
268 GTK_SIGNAL_FUNC(cb_unselect_child), subtree);
269 /* This has absolutely no effect, because it is completely ignored
271 /* gtk_tree_set_selection_mode (GTK_TREE(subtree),
272 GTK_SELECTION_SINGLE);
273 /* Neither does this, but for a rather different reason - the
274 view_mode and view_line values of a tree are propagated to
275 subtrees when they are mapped. So, setting it later on would
276 actually have a (somewhat unpredictable) effect */
277 /* gtk_tree_set_view_mode (GTK_TREE(subtree), GTK_TREE_VIEW_ITEM);
278 /* Set this item's subtree - note that you cannot do this until
279 AFTER the item has been added to its parent tree! */
280 /* gtk_tree_item_set_subtree (GTK_TREE_ITEM(item), subtree);
282 for (j = 0; j < 5; j++){
285 /* Create a subtree item, in much the same way */
286 /* subitem = gtk_tree_item_new_with_label (itemnames[j]);
287 /* Connect all GtkItem:: and GtkTreeItem:: signals */
288 /* gtk_signal_connect (GTK_OBJECT(subitem), "select",
289 GTK_SIGNAL_FUNC(cb_itemsignal), "select");
290 gtk_signal_connect (GTK_OBJECT(subitem), "deselect",
291 GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
292 gtk_signal_connect (GTK_OBJECT(subitem), "toggle",
293 GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
294 gtk_signal_connect (GTK_OBJECT(subitem), "expand",
295 GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
296 gtk_signal_connect (GTK_OBJECT(subitem), "collapse",
297 GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
298 g_print ("-> -> item %s->%p\n", itemnames[j], subitem);
299 /* Add it to its parent tree */
300 /* gtk_tree_append (GTK_TREE(subtree), subitem);
302 /* gtk_widget_show (subitem);
308 auth_fn(char *server
, char *share
,
309 char **workgroup
, char **username
, char **password
)
312 /* Do nothing for now ... */
319 GtkWidget
*window
, *scrolled_win
, *tree
;
320 GtkWidget
*subtree
, *item
;
324 struct smbc_dirent
*dirp
;
326 gtk_init (&argc
, &argv
);
328 /* Init the smbclient library */
330 err
= smbc_init(auth_fn
, "", 10);
332 /* a generic toplevel window */
333 window
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
334 gtk_signal_connect (GTK_OBJECT(window
), "delete_event",
335 GTK_SIGNAL_FUNC (gtk_main_quit
), NULL
);
336 gtk_container_set_border_width (GTK_CONTAINER(window
), 5);
338 /* A generic scrolled window */
339 scrolled_win
= gtk_scrolled_window_new (NULL
, NULL
);
340 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win
),
341 GTK_POLICY_AUTOMATIC
,
342 GTK_POLICY_AUTOMATIC
);
343 gtk_widget_set_usize (scrolled_win
, 150, 200);
344 gtk_container_add (GTK_CONTAINER(window
), scrolled_win
);
345 gtk_widget_show (scrolled_win
);
347 /* Create the root tree */
348 tree
= gtk_tree_new();
349 g_print ("root tree is %p\n", tree
);
350 /* connect all GtkTree:: signals */
351 gtk_signal_connect (GTK_OBJECT(tree
), "select_child",
352 GTK_SIGNAL_FUNC(cb_select_child
), tree
);
353 gtk_signal_connect (GTK_OBJECT(tree
), "unselect_child",
354 GTK_SIGNAL_FUNC(cb_unselect_child
), tree
);
355 gtk_signal_connect (GTK_OBJECT(tree
), "selection_changed",
356 GTK_SIGNAL_FUNC(cb_selection_changed
), tree
);
357 /* Add it to the scrolled window */
358 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrolled_win
),
360 /* Set the selection mode */
361 gtk_tree_set_selection_mode (GTK_TREE(tree
),
362 GTK_SELECTION_MULTIPLE
);
364 gtk_widget_show (tree
);
366 /* Now, build the top level display ... */
368 if ((dh
= smbc_opendir("smb:///")) < 0) {
370 fprintf(stderr
, "Could not list default workgroup: smb:///: %s\n",
377 /* Create a tree item for Whole Network */
379 item
= gtk_tree_item_new_with_label ("Whole Network");
380 /* Connect all GtkItem:: and GtkTreeItem:: signals */
381 gtk_signal_connect (GTK_OBJECT(item
), "select",
382 GTK_SIGNAL_FUNC(cb_itemsignal
), "select");
383 gtk_signal_connect (GTK_OBJECT(item
), "deselect",
384 GTK_SIGNAL_FUNC(cb_itemsignal
), "deselect");
385 gtk_signal_connect (GTK_OBJECT(item
), "toggle",
386 GTK_SIGNAL_FUNC(cb_itemsignal
), "toggle");
387 gtk_signal_connect (GTK_OBJECT(item
), "expand",
388 GTK_SIGNAL_FUNC(cb_wholenet
), "expand");
389 gtk_signal_connect (GTK_OBJECT(item
), "collapse",
390 GTK_SIGNAL_FUNC(cb_wholenet
), "collapse");
391 /* Add it to the parent tree */
392 gtk_tree_append (GTK_TREE(tree
), item
);
393 /* Show it - this can be done at any time */
394 gtk_widget_show (item
);
396 subtree
= gtk_tree_new(); /* A subtree for Whole Network */
398 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item
), subtree
);
400 /* Now, get the items in smb:/// and add them to the tree */
402 dirp
= (struct smbc_dirent
*)dirbuf
;
404 while ((err
= smbc_getdents(dh
, (struct smbc_dirent
*)dirbuf
,
405 sizeof(dirbuf
))) != 0) {
407 if (err
< 0) { /* Handle the error */
409 fprintf(stderr
, "Could not read directory for smbc:///: %s\n",
416 fprintf(stdout
, "Dir len: %u\n", err
);
418 while (err
> 0) { /* Extract each entry and make a sub-tree */
420 int dirlen
= sizeof(struct smbc_dirent
) + dirp
->namelen
+
421 dirp
->commentlen
+ 1;
423 item
= gtk_tree_item_new_with_label(dirp
->name
);
424 /* Connect all GtkItem:: and GtkTreeItem:: signals */
425 gtk_signal_connect (GTK_OBJECT(item
), "select",
426 GTK_SIGNAL_FUNC(cb_itemsignal
), "select");
427 gtk_signal_connect (GTK_OBJECT(item
), "deselect",
428 GTK_SIGNAL_FUNC(cb_itemsignal
), "deselect");
429 gtk_signal_connect (GTK_OBJECT(item
), "toggle",
430 GTK_SIGNAL_FUNC(cb_itemsignal
), "toggle");
431 gtk_signal_connect (GTK_OBJECT(item
), "expand",
432 GTK_SIGNAL_FUNC(cb_itemsignal
), "expand");
433 gtk_signal_connect (GTK_OBJECT(item
), "collapse",
434 GTK_SIGNAL_FUNC(cb_itemsignal
), "collapse");
435 /* Add it to the parent tree */
436 gtk_tree_append (GTK_TREE(tree
), item
);
437 /* Show it - this can be done at any time */
438 gtk_widget_show (item
);
440 fprintf(stdout
, "Added: %s, len: %u\n", dirp
->name
, dirlen
);
442 subtree
= gtk_tree_new();
444 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item
), subtree
);
446 (char *)dirp
+= dirlen
;
453 smbc_closedir(dh
); /* FIXME, check for error :-) */
455 /* Show the window and loop endlessly */
456 gtk_widget_show (window
);