source: branches/samba-3.3.x/examples/libsmbclient/tree.c@ 206

Last change on this file since 206 was 206, checked in by Herwig Bauernfeind, 17 years ago

Import Samba 3.3 branch at 3.0.0 level (psmedley's port)

File size: 21.4 KB
Line 
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
8
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.
13
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.
18
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/>.
21*/
22
23/* example-gtk+ application, ripped off from the gtk+ tree.c sample */
24
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"
31
32static GtkWidget *clist;
33
34struct tree_data {
35
36 guint32 type; /* Type of tree item, an SMBC_TYPE */
37 char name[256]; /* May need to change this later */
38
39};
40
41void error_message(gchar *message) {
42
43 GtkWidget *dialog, *label, *okay_button;
44
45 /* Create the widgets */
46
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");
51
52 /* Ensure that the dialog box is destroyed when the user clicks ok. */
53
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);
59
60 /* Add the label, and show everything we've added to the dialog. */
61
62 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
63 label);
64 gtk_widget_show_all (dialog);
65}
66
67/*
68 * We are given a widget, and we want to retrieve its URL so we
69 * can do a directory listing.
70 *
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
73 */
74
75static char path_string[1024];
76
77char *get_path(GtkWidget *item)
78{
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;
83
84 /* Walk back up the tree, getting the private data */
85
86 level = GTK_TREE(item->parent)->level;
87
88 /* Pick up this item's component info */
89
90 pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(item));
91
92 comps[i++] = pd->name;
93 type = pd->type;
94
95 while (level > 0 && type != SMBC_SERVER && type != SMBC_WORKGROUP) {
96
97 /* Find the parent and extract the data etc ... */
98
99 p = GTK_WIDGET(p->parent);
100 p = GTK_WIDGET(GTK_TREE(p)->tree_owner);
101
102 pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(p));
103
104 level = GTK_TREE(item->parent)->level;
105
106 comps[i++] = pd->name;
107 type = pd->type;
108
109 }
110
111 /*
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
114 *
115 * Now, build the path
116 */
117
118 snprintf(path_string, sizeof(path_string), "smb:/");
119
120 for (j = i - 1; j >= 0; j--) {
121
122 strncat(path_string, "/", sizeof(path_string) - strlen(path_string));
123 strncat(path_string, comps[j], sizeof(path_string) - strlen(path_string));
124
125 }
126
127 fprintf(stdout, "Path string = %s\n", path_string);
128
129 return path_string;
130
131}
132
133struct tree_data *make_tree_data(guint32 type, const char *name)
134{
135 struct tree_data *p = (struct tree_data *)malloc(sizeof(struct tree_data));
136
137 if (p) {
138
139 p->type = type;
140 strncpy(p->name, name, sizeof(p->name));
141
142 }
143
144 return p;
145
146}
147
148/* Note that this is called every time the user clicks on an item,
149 whether it is already selected or not. */
150static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
151 GtkWidget *subtree)
152{
153 gint dh, err, dirlen;
154 char dirbuf[512];
155 struct smbc_dirent *dirp;
156 struct stat st1;
157 char path[1024], path1[1024];
158
159 g_print ("select_child called for root tree %p, subtree %p, child %p\n",
160 root_tree, subtree, child);
161
162 /* Now, figure out what it is, and display it in the clist ... */
163
164 gtk_clist_clear(GTK_CLIST(clist)); /* Clear the CLIST */
165
166 /* Now, get the private data for the subtree */
167
168 strncpy(path, get_path(child), 1024);
169
170 if ((dh = smbc_opendir(path)) < 0) { /* Handle error */
171
172 g_print("cb_select_child: Could not open dir %s, %s\n", path,
173 strerror(errno));
174
175 gtk_main_quit();
176
177 return;
178
179 }
180
181 while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
182 sizeof(dirbuf))) != 0) {
183
184 if (err < 0) {
185
186 g_print("cb_select_child: Could not read dir %s, %s\n", path,
187 strerror(errno));
188
189 gtk_main_quit();
190
191 return;
192
193 }
194
195 dirp = (struct smbc_dirent *)dirbuf;
196
197 while (err > 0) {
198 gchar col1[128], col2[128], col3[128], col4[128];
199 gchar *rowdata[4] = {col1, col2, col3, col4};
200
201 dirlen = dirp->dirlen;
202
203 /* Format each of the items ... */
204
205 strncpy(col1, dirp->name, 128);
206
207 col2[0] = col3[0] = col4[0] = (char)0;
208
209 switch (dirp->smbc_type) {
210
211 case SMBC_WORKGROUP:
212
213 break;
214
215 case SMBC_SERVER:
216
217 strncpy(col2, (dirp->comment?dirp->comment:""), 128);
218
219 break;
220
221 case SMBC_FILE_SHARE:
222
223 strncpy(col2, (dirp->comment?dirp->comment:""), 128);
224
225 break;
226
227 case SMBC_PRINTER_SHARE:
228
229 strncpy(col2, (dirp->comment?dirp->comment:""), 128);
230 break;
231
232 case SMBC_COMMS_SHARE:
233
234 break;
235
236 case SMBC_IPC_SHARE:
237
238 break;
239
240 case SMBC_DIR:
241 case SMBC_FILE:
242
243 /* Get stats on the file/dir and see what we have */
244
245 if ((strcmp(dirp->name, ".") != 0) &&
246 (strcmp(dirp->name, "..") != 0)) {
247
248 strncpy(path1, path, sizeof(path1));
249 strncat(path1, "/", sizeof(path) - strlen(path));
250 strncat(path1, dirp->name, sizeof(path) - strlen(path));
251
252 if (smbc_stat(path1, &st1) < 0) {
253
254 if (errno != EBUSY) {
255
256 g_print("cb_select_child: Could not stat file %s, %s\n", path1,
257 strerror(errno));
258
259 gtk_main_quit();
260
261 return;
262
263 }
264 else {
265
266 strncpy(col2, "Device or resource busy", sizeof(col2));
267
268 }
269 }
270 else {
271 /* Now format each of the relevant things ... */
272
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));
286 }
287 }
288
289 break;
290
291 default:
292
293 break;
294 }
295
296 gtk_clist_append(GTK_CLIST(clist), rowdata);
297
298 dirp = (struct smbc_dirent *) ((char *) dirp + dirlen);
299 err -= dirlen;
300
301 }
302
303 }
304
305}
306
307/* Note that this is never called */
308static void cb_unselect_child( GtkWidget *root_tree,
309 GtkWidget *child,
310 GtkWidget *subtree )
311{
312 g_print ("unselect_child called for root tree %p, subtree %p, child %p\n",
313 root_tree, subtree, child);
314}
315
316/* for all the GtkItem:: and GtkTreeItem:: signals */
317static void cb_itemsignal( GtkWidget *item,
318 gchar *signame )
319{
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;
326
327 label = GTK_LABEL (GTK_BIN (item)->child);
328 /* Get the text of the label */
329 gtk_label_get (label, &name);
330
331 level = GTK_TREE(item->parent)->level;
332
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);
336
337 real_tree = GTK_TREE_ITEM_SUBTREE(item); /* Get the subtree */
338
339 if (strncmp(signame, "expand", 6) == 0) { /* Expand called */
340 char server[128];