| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 | SMB client GTK+ tree-based application | 
|---|
| 4 | Copyright (C) Andrew Tridgell 1998 | 
|---|
| 5 | Copyright (C) Richard Sharpe 2001 | 
|---|
| 6 | Copyright (C) John Terpstra 2001 | 
|---|
| 7 |  | 
|---|
| 8 | This program is free software; you can redistribute it and/or modify | 
|---|
| 9 | it under the terms of the GNU General Public License as published by | 
|---|
| 10 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 11 | (at your option) any later version. | 
|---|
| 12 |  | 
|---|
| 13 | This program is distributed in the hope that it will be useful, | 
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | GNU General Public License for more details. | 
|---|
| 17 |  | 
|---|
| 18 | You should have received a copy of the GNU General Public License | 
|---|
| 19 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | /* example-gtk+ application, ripped off from the gtk+ tree.c sample */ | 
|---|
| 23 |  | 
|---|
| 24 | #include <stdio.h> | 
|---|
| 25 | #include <errno.h> | 
|---|
| 26 | #include <gtk/gtk.h> | 
|---|
| 27 | #include "libsmbclient.h" | 
|---|
| 28 |  | 
|---|
| 29 | static GtkWidget *clist; | 
|---|
| 30 |  | 
|---|
| 31 | struct tree_data { | 
|---|
| 32 |  | 
|---|
| 33 | guint32 type;    /* Type of tree item, an SMBC_TYPE */ | 
|---|
| 34 | char name[256];  /* May need to change this later   */ | 
|---|
| 35 |  | 
|---|
| 36 | }; | 
|---|
| 37 |  | 
|---|
| 38 | static void tree_error_message(gchar *message) { | 
|---|
| 39 |  | 
|---|
| 40 | GtkWidget *dialog, *label, *okay_button; | 
|---|
| 41 |  | 
|---|
| 42 | /* Create the widgets */ | 
|---|
| 43 |  | 
|---|
| 44 | dialog = gtk_dialog_new(); | 
|---|
| 45 | gtk_window_set_modal(GTK_WINDOW(dialog), True); | 
|---|
| 46 | label = gtk_label_new (message); | 
|---|
| 47 | okay_button = gtk_button_new_with_label("Okay"); | 
|---|
| 48 |  | 
|---|
| 49 | /* Ensure that the dialog box is destroyed when the user clicks ok. */ | 
|---|
| 50 |  | 
|---|
| 51 | gtk_signal_connect_object (GTK_OBJECT (okay_button), "clicked", | 
|---|
| 52 | GTK_SIGNAL_FUNC (gtk_widget_destroy), dialog); | 
|---|
| 53 | gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area), | 
|---|
| 54 | okay_button); | 
|---|
| 55 |  | 
|---|
| 56 | /* Add the label, and show everything we've added to the dialog. */ | 
|---|
| 57 |  | 
|---|
| 58 | gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), | 
|---|
| 59 | label); | 
|---|
| 60 | gtk_widget_show_all (dialog); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | /* | 
|---|
| 64 | * We are given a widget, and we want to retrieve its URL so we | 
|---|
| 65 | * can do a directory listing. | 
|---|
| 66 | * | 
|---|
| 67 | * We walk back up the tree, picking up pieces until we hit a server or | 
|---|
| 68 | * workgroup type and return a path from there | 
|---|
| 69 | */ | 
|---|
| 70 |  | 
|---|
| 71 | static char *path_string; | 
|---|
| 72 |  | 
|---|
| 73 | char *get_path(TALLOC_CTX *ctx, GtkWidget *item) | 
|---|
| 74 | { | 
|---|
| 75 | GtkWidget *p = item; | 
|---|
| 76 | struct tree_data *pd; | 
|---|
| 77 | char *comps[1024];  /* We keep pointers to the components here */ | 
|---|
| 78 | int i = 0, j, level,type; | 
|---|
| 79 |  | 
|---|
| 80 | /* Walk back up the tree, getting the private data */ | 
|---|
| 81 |  | 
|---|
| 82 | level = GTK_TREE(item->parent)->level; | 
|---|
| 83 |  | 
|---|
| 84 | /* Pick up this item's component info */ | 
|---|
| 85 |  | 
|---|
| 86 | pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(item)); | 
|---|
| 87 |  | 
|---|
| 88 | comps[i++] = pd->name; | 
|---|
| 89 | type = pd->type; | 
|---|
| 90 |  | 
|---|
| 91 | while (level > 0 && type != SMBC_SERVER && type != SMBC_WORKGROUP) { | 
|---|
| 92 |  | 
|---|
| 93 | /* Find the parent and extract the data etc ... */ | 
|---|
| 94 |  | 
|---|
| 95 | p = GTK_WIDGET(p->parent); | 
|---|
| 96 | p = GTK_WIDGET(GTK_TREE(p)->tree_owner); | 
|---|
| 97 |  | 
|---|
| 98 | pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(p)); | 
|---|
| 99 |  | 
|---|
| 100 | level = GTK_TREE(item->parent)->level; | 
|---|
| 101 |  | 
|---|
| 102 | comps[i++] = pd->name; | 
|---|
| 103 | type = pd->type; | 
|---|
| 104 |  | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | /* | 
|---|
| 108 | * Got a list of comps now, should check that we did not hit a workgroup | 
|---|
| 109 | * when we got other things as well ... Later | 
|---|
| 110 | * | 
|---|
| 111 | * Now, build the path | 
|---|
| 112 | */ | 
|---|
| 113 |  | 
|---|
| 114 | TALLOC_FREE(path_string); | 
|---|
| 115 | path_string = talloc_strdup(ctx, "smb:/"); | 
|---|
| 116 |  | 
|---|
| 117 | if (path_string) { | 
|---|
| 118 | for (j = i - 1; j >= 0; j--) { | 
|---|
| 119 | path_string = talloc_asprintf_append(path_string, "/%s", comps[j]); | 
|---|
| 120 | } | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | if (path_string) { | 
|---|
| 124 | fprintf(stdout, "Path string = %s\n", path_string); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | return path_string; | 
|---|
| 128 |  | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | struct tree_data *make_tree_data(guint32 type, const char *name) | 
|---|
| 132 | { | 
|---|
| 133 | struct tree_data *p = SMB_MALLOC_P(struct tree_data); | 
|---|
| 134 |  | 
|---|
| 135 | if (p) { | 
|---|
| 136 |  | 
|---|
| 137 | p->type = type; | 
|---|
| 138 | strncpy(p->name, name, sizeof(p->name)); | 
|---|
| 139 |  | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | return p; | 
|---|
| 143 |  | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | /* Note that this is called every time the user clicks on an item, | 
|---|
| 147 | whether it is already selected or not. */ | 
|---|
| 148 | static void cb_select_child (GtkWidget *root_tree, GtkWidget *child, | 
|---|
| 149 | GtkWidget *subtree) | 
|---|
| 150 | { | 
|---|
| 151 | gint dh, err, dirlen; | 
|---|
| 152 | char dirbuf[512]; | 
|---|
| 153 | struct smbc_dirent *dirp; | 
|---|
| 154 | struct stat st1; | 
|---|
| 155 | char *path; | 
|---|
| 156 | TALLOC_CTX *ctx = talloc_stackframe(); | 
|---|
| 157 |  | 
|---|
| 158 | g_print ("select_child called for root tree %p, subtree %p, child %p\n", | 
|---|
| 159 | root_tree, subtree, child); | 
|---|
| 160 |  | 
|---|
| 161 | /* Now, figure out what it is, and display it in the clist ... */ | 
|---|
| 162 |  | 
|---|
| 163 | gtk_clist_clear(GTK_CLIST(clist));  /* Clear the CLIST */ | 
|---|
| 164 |  | 
|---|
| 165 | /* Now, get the private data for the subtree */ | 
|---|
| 166 |  | 
|---|
| 167 | path = get_path(ctx, child); | 
|---|
| 168 | if (!path) { | 
|---|
| 169 | gtk_main_quit(); | 
|---|
| 170 | TALLOC_FREE(ctx); | 
|---|
| 171 | return; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | if ((dh = smbc_opendir(path)) < 0) { /* Handle error */ | 
|---|
| 175 | g_print("cb_select_child: Could not open dir %s, %s\n", path, | 
|---|
| 176 | strerror(errno)); | 
|---|
| 177 | gtk_main_quit(); | 
|---|
| 178 | TALLOC_FREE(ctx); | 
|---|
| 179 | return; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf, | 
|---|
| 183 | sizeof(dirbuf))) != 0) { | 
|---|
| 184 | if (err < 0) { | 
|---|
| 185 | g_print("cb_select_child: Could not read dir %s, %s\n", path, | 
|---|
| 186 | strerror(errno)); | 
|---|
| 187 | gtk_main_quit(); | 
|---|
| 188 | TALLOC_FREE(ctx); | 
|---|
| 189 | return; | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | dirp = (struct smbc_dirent *)dirbuf; | 
|---|
| 193 |  | 
|---|
| 194 | while (err > 0) { | 
|---|
| 195 | gchar col1[128], col2[128], col3[128], col4[128]; | 
|---|
| 196 | gchar *rowdata[4] = {col1, col2, col3, col4}; | 
|---|
| 197 |  | 
|---|
| 198 | dirlen = dirp->dirlen; | 
|---|
| 199 |  | 
|---|
| 200 | /* Format each of the items ... */ | 
|---|
| 201 |  | 
|---|
| 202 | strncpy(col1, dirp->name, 128); | 
|---|
| 203 |  | 
|---|
| 204 | col2[0] = col3[0] = col4[0] = (char)0; | 
|---|
| 205 |  | 
|---|
| 206 | switch (dirp->smbc_type) { | 
|---|
| 207 |  | 
|---|
| 208 | case SMBC_WORKGROUP: | 
|---|
| 209 |  | 
|---|
| 210 | break; | 
|---|
| 211 |  | 
|---|
| 212 | case SMBC_SERVER: | 
|---|
| 213 |  | 
|---|
| 214 | strncpy(col2, (dirp->comment?dirp->comment:""), 128); | 
|---|
| 215 |  | 
|---|
| 216 | break; | 
|---|
| 217 |  | 
|---|
| 218 | case SMBC_FILE_SHARE: | 
|---|
| 219 |  | 
|---|
| 220 | strncpy(col2, (dirp->comment?dirp->comment:""), 128); | 
|---|
| 221 |  | 
|---|
| 222 | break; | 
|---|
| 223 |  | 
|---|
| 224 | case SMBC_PRINTER_SHARE: | 
|---|
| 225 |  | 
|---|
| 226 | strncpy(col2, (dirp->comment?dirp->comment:""), 128); | 
|---|
| 227 | break; | 
|---|
| 228 |  | 
|---|
| 229 | case SMBC_COMMS_SHARE: | 
|---|
| 230 |  | 
|---|
| 231 | break; | 
|---|
| 232 |  | 
|---|
| 233 | case SMBC_IPC_SHARE: | 
|---|
| 234 |  | 
|---|
| 235 | break; | 
|---|
| 236 |  | 
|---|
| 237 | case SMBC_DIR: | 
|---|
| 238 | case SMBC_FILE: | 
|---|
| 239 |  | 
|---|
| 240 | /* Get stats on the file/dir and see what we have */ | 
|---|
| 241 |  | 
|---|
| 242 | if ((strcmp(dirp->name, ".") != 0) && | 
|---|
| 243 | (strcmp(dirp->name, "..") != 0)) { | 
|---|
| 244 | char *path1; | 
|---|
| 245 |  | 
|---|
| 246 | path1 = talloc_asprintf(ctx, | 
|---|
| 247 | "%s/%s", | 
|---|
| 248 | path, | 
|---|
| 249 | dirp->name); | 
|---|
| 250 | if (!path1) { | 
|---|
| 251 | gtk_main_quit(); | 
|---|
| 252 | TALLOC_FREE(ctx); | 
|---|
| 253 | return; | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | if (smbc_stat(path1, &st1) < 0) { | 
|---|
| 257 | if (errno != EBUSY) { | 
|---|
| 258 | g_print("cb_select_child: Could not stat file %s, %s\n", path1, | 
|---|
| 259 | strerror(errno)); | 
|---|
| 260 | gtk_main_quit(); | 
|---|
| 261 | TALLOC_FREE(ctx); | 
|---|
| 262 | return; | 
|---|
| 263 | } else { | 
|---|
| 264 | strncpy(col2, "Device or resource busy", sizeof(col2)); | 
|---|
| 265 | } | 
|---|
| 266 | } | 
|---|
| 267 | else { | 
|---|
| 268 | /* Now format each of the relevant things ... */ | 
|---|
| 269 |  | 
|---|
| 270 | snprintf(col2, sizeof(col2), "%c%c%c%c%c%c%c%c%c(%0X)", | 
|---|
| 271 | (st1.st_mode&S_IRUSR?'r':'-'), | 
|---|
| 272 | (st1.st_mode&S_IWUSR?'w':'-'), | 
|---|
| 273 | (st1.st_mode&S_IXUSR?'x':'-'), | 
|---|
| 274 | (st1.st_mode&S_IRGRP?'r':'-'), | 
|---|
| 275 | (st1.st_mode&S_IWGRP?'w':'-'), | 
|---|
| 276 | (st1.st_mode&S_IXGRP?'x':'-'), | 
|---|
| 277 | (st1.st_mode&S_IROTH?'r':'-'), | 
|---|
| 278 | (st1.st_mode&S_IWOTH?'w':'-'), | 
|---|
| 279 | (st1.st_mode&S_IXOTH?'x':'-'), | 
|---|
| 280 | st1.st_mode); | 
|---|
| 281 | snprintf(col3, sizeof(col3), "%u", st1.st_size); | 
|---|
| 282 | snprintf(col4, sizeof(col4), "%s", ctime(&st1.st_mtime)); | 
|---|
| 283 | } | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | break; | 
|---|
| 287 |  | 
|---|
| 288 | default: | 
|---|
| 289 |  | 
|---|
| 290 | break; | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | gtk_clist_append(GTK_CLIST(clist), rowdata); | 
|---|
| 294 |  | 
|---|
| 295 | (char *)dirp += dirlen; | 
|---|
| 296 | err -= dirlen; | 
|---|
| 297 |  | 
|---|
| 298 | } | 
|---|
| 299 | } | 
|---|
| 300 | TALLOC_FREE(ctx); | 
|---|
| 301 | } | 
|---|
| 302 |  | 
|---|
| 303 | /* Note that this is never called */ | 
|---|
| 304 | static void cb_unselect_child( GtkWidget *root_tree, | 
|---|
| 305 | GtkWidget *child, | 
|---|
| 306 | GtkWidget *subtree ) | 
|---|
| 307 | { | 
|---|
| 308 | g_print ("unselect_child called for root tree %p, subtree %p, child %p\n", | 
|---|
| 309 | root_tree, subtree, child); | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 | /* for all the GtkItem:: and GtkTreeItem:: signals */ | 
|---|
| 313 | static void cb_itemsignal( GtkWidget *item, | 
|---|
| 314 | gchar     *signame ) | 
|---|
| 315 | { | 
|---|
| 316 | GtkWidget *real_tree, *aitem, *subtree; | 
|---|
| 317 | gchar *name; | 
|---|
| 318 | GtkLabel *label; | 
|---|
| 319 | gint dh, err, dirlen, level; | 
|---|
| 320 | char dirbuf[512]; | 
|---|
| 321 | struct smbc_dirent *dirp; | 
|---|
| 322 |  | 
|---|
| 323 | label = GTK_LABEL (GTK_BIN (item)->child); | 
|---|
| 324 | /* Get the text of the label */ | 
|---|
| 325 | gtk_label_get (label, &name); | 
|---|
| 326 |  | 
|---|
| 327 | level = GTK_TREE(item->parent)->level; | 
|---|
| 328 |  | 
|---|
| 329 | /* Get the level of the tree which the item is in */ | 
|---|
| 330 | g_print ("%s called for item %s->%p, level %d\n", signame, name, | 
|---|
| 331 | item, GTK_TREE (item->parent)->level); | 
|---|
| 332 |  | 
|---|
| 333 | real_tree = GTK_TREE_ITEM_SUBTREE(item);  /* Get the subtree */ | 
|---|
| 334 |  | 
|---|
| 335 | if (strncmp(signame, "expand", 6) == 0) { /* Expand called */ | 
|---|
| 336 | char server[128]; | 
|---|
| 337 |  | 
|---|
| 338 | if ((dh = smbc_opendir(get_path(item))) < 0) { /* Handle error */ | 
|---|
| 339 | gchar errmsg[256]; | 
|---|
| 340 |  | 
|---|
| 341 | g_print("cb_itemsignal: Could not open dir %s, %s\n", get_path(item), | 
|---|
| 342 | strerror(errno)); | 
|---|
| 343 |  | 
|---|
| 344 | slprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not open dir %s, %s\n", get_path(item), strerror(errno)); | 
|---|
| 345 |  | 
|---|
| 346 | tree_error_message(errmsg); | 
|---|
| 347 |  | 
|---|
| 348 | /*      gtk_main_quit();*/ | 
|---|
| 349 |  | 
|---|
| 350 | return; | 
|---|
| 351 |  | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf, | 
|---|
| 355 | sizeof(dirbuf))) != 0) { | 
|---|
| 356 |  | 
|---|
| 357 | if (err < 0) { /* An error, report it */ | 
|---|
| 358 | gchar errmsg[256]; | 
|---|
| 359 |  | 
|---|
| 360 | g_print("cb_itemsignal: Could not read dir smbc://, %s\n", | 
|---|
| 361 | strerror(errno)); | 
|---|
| 362 |  | 
|---|
| 363 | slprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not read dir smbc://, %s\n", strerror(errno)); | 
|---|
| 364 |  | 
|---|
| 365 | tree_error_message(errmsg); | 
|---|
| 366 |  | 
|---|
| 367 | /*      gtk_main_quit();*/ | 
|---|
| 368 |  | 
|---|
| 369 | return; | 
|---|
| 370 |  | 
|---|
| 371 | } | 
|---|
| 372 |  | 
|---|
| 373 | dirp = (struct smbc_dirent *)dirbuf; | 
|---|
| 374 |  | 
|---|
| 375 | while (err > 0) { | 
|---|
| 376 | struct tree_data *my_data; | 
|---|
| 377 |  | 
|---|
| 378 | dirlen = dirp->dirlen; | 
|---|
| 379 |  | 
|---|
| 380 | my_data = make_tree_data(dirp->smbc_type, dirp->name); | 
|---|
| 381 |  | 
|---|
| 382 | if (!my_data) { | 
|---|
| 383 |  | 
|---|
| 384 | g_print("Could not allocate space for tree_data: %s\n", | 
|---|
| 385 | dirp->name); | 
|---|
| 386 |  | 
|---|
| 387 | gtk_main_quit(); | 
|---|
| 388 | return; | 
|---|
| 389 |  | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | aitem = gtk_tree_item_new_with_label(dirp->name); | 
|---|
| 393 |  | 
|---|
| 394 | /* Connect all GtkItem:: and GtkTreeItem:: signals */ | 
|---|
| 395 | gtk_signal_connect (GTK_OBJECT(aitem), "select", | 
|---|
| 396 | GTK_SIGNAL_FUNC(cb_itemsignal), "select"); | 
|---|
| 397 | gtk_signal_connect (GTK_OBJECT(aitem), "deselect", | 
|---|
| 398 | GTK_SIGNAL_FUNC(cb_itemsignal), "deselect"); | 
|---|
| 399 | gtk_signal_connect (GTK_OBJECT(aitem), "toggle", | 
|---|
| 400 | GTK_SIGNAL_FUNC(cb_itemsignal), "toggle"); | 
|---|
| 401 | gtk_signal_connect (GTK_OBJECT(aitem), "expand", | 
|---|
| 402 | GTK_SIGNAL_FUNC(cb_itemsignal), "expand"); | 
|---|
| 403 | gtk_signal_connect (GTK_OBJECT(aitem), "collapse", | 
|---|
| 404 | GTK_SIGNAL_FUNC(cb_itemsignal), "collapse"); | 
|---|
| 405 | /* Add it to the parent tree */ | 
|---|
| 406 | gtk_tree_append (GTK_TREE(real_tree), aitem); | 
|---|
| 407 |  | 
|---|
| 408 | gtk_widget_show (aitem); | 
|---|
| 409 |  | 
|---|
| 410 | gtk_object_set_user_data(GTK_OBJECT(aitem), (gpointer)my_data); | 
|---|
| 411 |  | 
|---|
| 412 | fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen); | 
|---|
| 413 |  | 
|---|
| 414 | if (dirp->smbc_type != SMBC_FILE && | 
|---|
| 415 | dirp->smbc_type != SMBC_IPC_SHARE && | 
|---|
| 416 | (strcmp(dirp->name, ".") != 0) && | 
|---|
| 417 | (strcmp(dirp->name, "..") !=0)){ | 
|---|
| 418 |  | 
|---|
| 419 | subtree = gtk_tree_new(); | 
|---|
| 420 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem), subtree); | 
|---|
| 421 |  | 
|---|
| 422 | gtk_signal_connect(GTK_OBJECT(subtree), "select_child", | 
|---|
| 423 | GTK_SIGNAL_FUNC(cb_select_child), real_tree); | 
|---|
| 424 | gtk_signal_connect(GTK_OBJECT(subtree), "unselect_child", | 
|---|
| 425 | GTK_SIGNAL_FUNC(cb_unselect_child), real_tree); | 
|---|
| 426 |  | 
|---|
| 427 | } | 
|---|
| 428 |  | 
|---|
| 429 | (char *)dirp += dirlen; | 
|---|
| 430 | err -= dirlen; | 
|---|
| 431 |  | 
|---|
| 432 | } | 
|---|
| 433 |  | 
|---|
| 434 | } | 
|---|
| 435 |  | 
|---|
| 436 | smbc_closedir(dh); | 
|---|
| 437 |  | 
|---|
| 438 | } | 
|---|
| 439 | else if (strncmp(signame, "collapse", 8) == 0) { | 
|---|
| 440 | GtkWidget *subtree = gtk_tree_new(); | 
|---|
| 441 |  | 
|---|
| 442 | gtk_tree_remove_items(GTK_TREE(real_tree), GTK_TREE(real_tree)->children); | 
|---|
| 443 |  | 
|---|
| 444 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree); | 
|---|
| 445 |  | 
|---|
| 446 | gtk_signal_connect (GTK_OBJECT(subtree), "select_child", | 
|---|
| 447 | GTK_SIGNAL_FUNC(cb_select_child), real_tree); | 
|---|
| 448 | gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child", | 
|---|
| 449 | GTK_SIGNAL_FUNC(cb_unselect_child), real_tree); | 
|---|
| 450 |  | 
|---|
| 451 | } | 
|---|
| 452 |  | 
|---|
| 453 | } | 
|---|
| 454 |  | 
|---|
| 455 | static void cb_selection_changed( GtkWidget *tree ) | 
|---|
| 456 | { | 
|---|
| 457 | GList *i; | 
|---|
| 458 |  | 
|---|
| 459 | g_print ("selection_change called for tree %p\n", tree); | 
|---|
| 460 | g_print ("selected objects are:\n"); | 
|---|
| 461 |  | 
|---|
| 462 | i = GTK_TREE_SELECTION(tree); | 
|---|
| 463 | while (i){ | 
|---|
| 464 | gchar *name; | 
|---|
| 465 | GtkLabel *label; | 
|---|
| 466 | GtkWidget *item; | 
|---|
| 467 |  | 
|---|
| 468 | /* Get a GtkWidget pointer from the list node */ | 
|---|
| 469 | item = GTK_WIDGET (i->data); | 
|---|
| 470 | label = GTK_LABEL (GTK_BIN (item)->child); | 
|---|
| 471 | gtk_label_get (label, &name); | 
|---|
| 472 | g_print ("\t%s on level %d\n", name, GTK_TREE | 
|---|
| 473 | (item->parent)->level); | 
|---|
| 474 | i = i->next; | 
|---|
| 475 | } | 
|---|
| 476 | } | 
|---|
| 477 |  | 
|---|
| 478 | /* | 
|---|
| 479 | * Expand or collapse the whole network ... | 
|---|
| 480 | */ | 
|---|
| 481 | static void cb_wholenet(GtkWidget *item, gchar *signame) | 
|---|
| 482 | { | 
|---|
| 483 | GtkWidget *real_tree, *aitem, *subtree; | 
|---|
| 484 | gchar *name; | 
|---|
| 485 | GtkLabel *label; | 
|---|
| 486 | gint dh, err, dirlen; | 
|---|
| 487 | char dirbuf[512]; | 
|---|
| 488 | struct smbc_dirent *dirp; | 
|---|
| 489 |  | 
|---|
| 490 | label = GTK_LABEL (GTK_BIN (item)->child); | 
|---|
| 491 | gtk_label_get (label, &name); | 
|---|
| 492 | g_print ("%s called for item %s->%p, level %d\n", signame, name, | 
|---|
| 493 | item, GTK_TREE (item->parent)->level); | 
|---|
| 494 |  | 
|---|
| 495 | real_tree = GTK_TREE_ITEM_SUBTREE(item);  /* Get the subtree */ | 
|---|
| 496 |  | 
|---|
| 497 | if (strncmp(signame, "expand", 6) == 0) { /* Expand called */ | 
|---|
| 498 |  | 
|---|
| 499 | if ((dh = smbc_opendir("smb://")) < 0) { /* Handle error */ | 
|---|
| 500 |  | 
|---|
| 501 | g_print("cb_wholenet: Could not open dir smbc://, %s\n", | 
|---|
| 502 | strerror(errno)); | 
|---|
| 503 |  | 
|---|
| 504 | gtk_main_quit(); | 
|---|
| 505 |  | 
|---|
| 506 | return; | 
|---|
| 507 |  | 
|---|
| 508 | } | 
|---|
| 509 |  | 
|---|
| 510 | while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf, | 
|---|
| 511 | sizeof(dirbuf))) != 0) { | 
|---|
| 512 |  | 
|---|
| 513 | if (err < 0) { /* An error, report it */ | 
|---|
| 514 |  | 
|---|
| 515 | g_print("cb_wholenet: Could not read dir smbc://, %s\n", | 
|---|
| 516 | strerror(errno)); | 
|---|
| 517 |  | 
|---|
| 518 | gtk_main_quit(); | 
|---|
| 519 |  | 
|---|
| 520 | return; | 
|---|
| 521 |  | 
|---|
| 522 | } | 
|---|
| 523 |  | 
|---|
| 524 | dirp = (struct smbc_dirent *)dirbuf; | 
|---|
| 525 |  | 
|---|
| 526 | while (err > 0) { | 
|---|
| 527 | struct tree_data *my_data; | 
|---|
| 528 |  | 
|---|
| 529 | dirlen = dirp->dirlen; | 
|---|
| 530 |  | 
|---|
| 531 | my_data = make_tree_data(dirp->smbc_type, dirp->name); | 
|---|
| 532 |  | 
|---|
| 533 | aitem = gtk_tree_item_new_with_label(dirp->name); | 
|---|
| 534 |  | 
|---|
| 535 | /* Connect all GtkItem:: and GtkTreeItem:: signals */ | 
|---|
| 536 | gtk_signal_connect (GTK_OBJECT(aitem), "select", | 
|---|
| 537 | GTK_SIGNAL_FUNC(cb_itemsignal), "select"); | 
|---|
| 538 | gtk_signal_connect (GTK_OBJECT(aitem), "deselect", | 
|---|
| 539 | GTK_SIGNAL_FUNC(cb_itemsignal), "deselect"); | 
|---|
| 540 | gtk_signal_connect (GTK_OBJECT(aitem), "toggle", | 
|---|
| 541 | GTK_SIGNAL_FUNC(cb_itemsignal), "toggle"); | 
|---|
| 542 | gtk_signal_connect (GTK_OBJECT(aitem), "expand", | 
|---|
| 543 | GTK_SIGNAL_FUNC(cb_itemsignal), "expand"); | 
|---|
| 544 | gtk_signal_connect (GTK_OBJECT(aitem), "collapse", | 
|---|
| 545 | GTK_SIGNAL_FUNC(cb_itemsignal), "collapse"); | 
|---|
| 546 |  | 
|---|
| 547 | gtk_tree_append (GTK_TREE(real_tree), aitem); | 
|---|
| 548 | /* Show it - this can be done at any time */ | 
|---|
| 549 | gtk_widget_show (aitem); | 
|---|
| 550 |  | 
|---|
| 551 | gtk_object_set_user_data(GTK_OBJECT(aitem), (gpointer)my_data); | 
|---|
| 552 |  | 
|---|
| 553 | fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen); | 
|---|
| 554 |  | 
|---|
| 555 | subtree = gtk_tree_new(); | 
|---|
| 556 |  | 
|---|
| 557 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem), subtree); | 
|---|
| 558 |  | 
|---|
| 559 | gtk_signal_connect(GTK_OBJECT(subtree), "select_child", | 
|---|
| 560 | GTK_SIGNAL_FUNC(cb_select_child), real_tree); | 
|---|
| 561 | gtk_signal_connect(GTK_OBJECT(subtree), "unselect_child", | 
|---|
| 562 | GTK_SIGNAL_FUNC(cb_unselect_child), real_tree); | 
|---|
| 563 |  | 
|---|
| 564 | (char *)dirp += dirlen; | 
|---|
| 565 | err -= dirlen; | 
|---|
| 566 |  | 
|---|
| 567 | } | 
|---|
| 568 |  | 
|---|
| 569 | } | 
|---|
| 570 |  | 
|---|
| 571 | smbc_closedir(dh); | 
|---|
| 572 |  | 
|---|
| 573 | } | 
|---|
| 574 | else { /* Must be collapse ... FIXME ... */ | 
|---|
| 575 | GtkWidget *subtree = gtk_tree_new(); | 
|---|
| 576 |  | 
|---|
| 577 | gtk_tree_remove_items(GTK_TREE(real_tree), GTK_TREE(real_tree)->children); | 
|---|
| 578 |  | 
|---|
| 579 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree); | 
|---|
| 580 |  | 
|---|
| 581 | gtk_signal_connect (GTK_OBJECT(subtree), "select_child", | 
|---|
| 582 | GTK_SIGNAL_FUNC(cb_select_child), real_tree); | 
|---|
| 583 | gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child", | 
|---|
| 584 | GTK_SIGNAL_FUNC(cb_unselect_child), real_tree); | 
|---|
| 585 |  | 
|---|
| 586 |  | 
|---|
| 587 | } | 
|---|
| 588 |  | 
|---|
| 589 | } | 
|---|
| 590 |  | 
|---|
| 591 | /* Should put up a dialog box to ask the user for username and password */ | 
|---|
| 592 |  | 
|---|
| 593 | static void | 
|---|
| 594 | auth_fn(const char *server, const char *share, | 
|---|
| 595 | char *workgroup, int wgmaxlen, char *username, int unmaxlen, | 
|---|
| 596 | char *password, int pwmaxlen) | 
|---|
| 597 | { | 
|---|
| 598 |  | 
|---|
| 599 | strncpy(username, "test", unmaxlen); | 
|---|
| 600 | strncpy(password, "test", pwmaxlen); | 
|---|
| 601 |  | 
|---|
| 602 | } | 
|---|
| 603 |  | 
|---|
| 604 | static char *col_titles[] = { | 
|---|
| 605 | "Name", "Attributes", "Size", "Modification Date", | 
|---|
| 606 | }; | 
|---|
| 607 |  | 
|---|
| 608 | int main( int   argc, | 
|---|
| 609 | char *argv[] ) | 
|---|
| 610 | { | 
|---|
| 611 | GtkWidget *window, *scrolled_win, *scrolled_win2, *tree; | 
|---|
| 612 | GtkWidget *subtree, *item, *main_hbox, *r_pane, *l_pane; | 
|---|
| 613 | gint err, dh; | 
|---|
| 614 | gint i; | 
|---|
| 615 | char dirbuf[512]; | 
|---|
| 616 | struct smbc_dirent *dirp; | 
|---|
| 617 | TALLOC_CTX *frame = talloc_stackframe(); | 
|---|
| 618 |  | 
|---|
| 619 | gtk_init (&argc, &argv); | 
|---|
| 620 |  | 
|---|
| 621 | /* Init the smbclient library */ | 
|---|
| 622 |  | 
|---|
| 623 | err = smbc_init(auth_fn, 10); | 
|---|
| 624 |  | 
|---|
| 625 | /* Print an error response ... */ | 
|---|
| 626 |  | 
|---|
| 627 | if (err < 0) { | 
|---|
| 628 |  | 
|---|
| 629 | fprintf(stderr, "smbc_init returned %s (%i)\nDo you have a ~/.smb/smb.conf file?\n", strerror(errno), errno); | 
|---|
| 630 | exit(1); | 
|---|
| 631 |  | 
|---|
| 632 | } | 
|---|
| 633 |  | 
|---|
| 634 | /* a generic toplevel window */ | 
|---|
| 635 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); | 
|---|
| 636 | gtk_widget_set_name(window, "main browser window"); | 
|---|
| 637 | gtk_signal_connect (GTK_OBJECT(window), "delete_event", | 
|---|
| 638 | GTK_SIGNAL_FUNC (gtk_main_quit), NULL); | 
|---|
| 639 | gtk_window_set_title(GTK_WINDOW(window), "The Linux Windows Network Browser"); | 
|---|
| 640 | gtk_widget_set_usize(GTK_WIDGET(window), 750, -1); | 
|---|
| 641 | gtk_container_set_border_width (GTK_CONTAINER(window), 5); | 
|---|
| 642 |  | 
|---|
| 643 | gtk_widget_show (window); | 
|---|
| 644 |  | 
|---|
| 645 | /* A container for the two panes ... */ | 
|---|
| 646 |  | 
|---|
| 647 | main_hbox = gtk_hbox_new(FALSE, 1); | 
|---|
| 648 | gtk_container_border_width(GTK_CONTAINER(main_hbox), 1); | 
|---|
| 649 | gtk_container_add(GTK_CONTAINER(window), main_hbox); | 
|---|
| 650 |  | 
|---|
| 651 | gtk_widget_show(main_hbox); | 
|---|
| 652 |  | 
|---|
| 653 | l_pane = gtk_hpaned_new(); | 
|---|
| 654 | gtk_paned_gutter_size(GTK_PANED(l_pane), (GTK_PANED(l_pane))->handle_size); | 
|---|
| 655 | r_pane = gtk_hpaned_new(); | 
|---|
| 656 | gtk_paned_gutter_size(GTK_PANED(r_pane), (GTK_PANED(r_pane))->handle_size); | 
|---|
| 657 | gtk_container_add(GTK_CONTAINER(main_hbox), l_pane); | 
|---|
| 658 | gtk_widget_show(l_pane); | 
|---|
| 659 |  | 
|---|
| 660 | /* A generic scrolled window */ | 
|---|
| 661 | scrolled_win = gtk_scrolled_window_new (NULL, NULL); | 
|---|
| 662 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win), | 
|---|
| 663 | GTK_POLICY_AUTOMATIC, | 
|---|
| 664 | GTK_POLICY_AUTOMATIC); | 
|---|
| 665 | gtk_widget_set_usize (scrolled_win, 150, 200); | 
|---|
| 666 | gtk_container_add (GTK_CONTAINER(l_pane), scrolled_win); | 
|---|
| 667 | gtk_widget_show (scrolled_win); | 
|---|
| 668 |  | 
|---|
| 669 | /* Another generic scrolled window */ | 
|---|
| 670 | scrolled_win2 = gtk_scrolled_window_new (NULL, NULL); | 
|---|
| 671 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win2), | 
|---|
| 672 | GTK_POLICY_AUTOMATIC, | 
|---|
| 673 | GTK_POLICY_AUTOMATIC); | 
|---|
| 674 | gtk_widget_set_usize (scrolled_win2, 150, 200); | 
|---|
| 675 | gtk_paned_add2 (GTK_PANED(l_pane), scrolled_win2); | 
|---|
| 676 | gtk_widget_show (scrolled_win2); | 
|---|
| 677 |  | 
|---|
| 678 | /* Create the root tree */ | 
|---|
| 679 | tree = gtk_tree_new(); | 
|---|
| 680 | g_print ("root tree is %p\n", tree); | 
|---|
| 681 | /* connect all GtkTree:: signals */ | 
|---|
| 682 | gtk_signal_connect (GTK_OBJECT(tree), "select_child", | 
|---|
| 683 | GTK_SIGNAL_FUNC(cb_select_child), tree); | 
|---|
| 684 | gtk_signal_connect (GTK_OBJECT(tree), "unselect_child", | 
|---|
| 685 | GTK_SIGNAL_FUNC(cb_unselect_child), tree); | 
|---|
| 686 | gtk_signal_connect (GTK_OBJECT(tree), "selection_changed", | 
|---|
| 687 | GTK_SIGNAL_FUNC(cb_selection_changed), tree); | 
|---|
| 688 | /* Add it to the scrolled window */ | 
|---|
| 689 | gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrolled_win), | 
|---|
| 690 | tree); | 
|---|
| 691 | /* Set the selection mode */ | 
|---|
| 692 | gtk_tree_set_selection_mode (GTK_TREE(tree), | 
|---|
| 693 | GTK_SELECTION_MULTIPLE); | 
|---|
| 694 | /* Show it */ | 
|---|
| 695 | gtk_widget_show (tree); | 
|---|
| 696 |  | 
|---|
| 697 | /* Now, create a clist and attach it to the second pane */ | 
|---|
| 698 |  | 
|---|
| 699 | clist = gtk_clist_new_with_titles(4, col_titles); | 
|---|
| 700 |  | 
|---|
| 701 | gtk_container_add (GTK_CONTAINER(scrolled_win2), clist); | 
|---|
| 702 |  | 
|---|
| 703 | gtk_widget_show(clist); | 
|---|
| 704 |  | 
|---|
| 705 | /* Now, build the top level display ... */ | 
|---|
| 706 |  | 
|---|
| 707 | if ((dh = smbc_opendir("smb:///")) < 0) { | 
|---|
| 708 |  | 
|---|
| 709 | fprintf(stderr, "Could not list default workgroup: smb:///: %s\n", | 
|---|
| 710 | strerror(errno)); | 
|---|
| 711 |  | 
|---|
| 712 | exit(1); | 
|---|
| 713 |  | 
|---|
| 714 | } | 
|---|
| 715 |  | 
|---|
| 716 | /* Create a tree item for Whole Network */ | 
|---|
| 717 |  | 
|---|
| 718 | item = gtk_tree_item_new_with_label ("Whole Network"); | 
|---|
| 719 | /* Connect all GtkItem:: and GtkTreeItem:: signals */ | 
|---|
| 720 | gtk_signal_connect (GTK_OBJECT(item), "select", | 
|---|
| 721 | GTK_SIGNAL_FUNC(cb_itemsignal), "select"); | 
|---|
| 722 | gtk_signal_connect (GTK_OBJECT(item), "deselect", | 
|---|
| 723 | GTK_SIGNAL_FUNC(cb_itemsignal), "deselect"); | 
|---|
| 724 | gtk_signal_connect (GTK_OBJECT(item), "toggle", | 
|---|
| 725 | GTK_SIGNAL_FUNC(cb_itemsignal), "toggle"); | 
|---|
| 726 | gtk_signal_connect (GTK_OBJECT(item), "expand", | 
|---|
| 727 | GTK_SIGNAL_FUNC(cb_wholenet), "expand"); | 
|---|
| 728 | gtk_signal_connect (GTK_OBJECT(item), "collapse", | 
|---|
| 729 | GTK_SIGNAL_FUNC(cb_wholenet), "collapse"); | 
|---|
| 730 | /* Add it to the parent tree */ | 
|---|
| 731 | gtk_tree_append (GTK_TREE(tree), item); | 
|---|
| 732 | /* Show it - this can be done at any time */ | 
|---|
| 733 | gtk_widget_show (item); | 
|---|
| 734 |  | 
|---|
| 735 | subtree = gtk_tree_new();  /* A subtree for Whole Network */ | 
|---|
| 736 |  | 
|---|
| 737 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree); | 
|---|
| 738 |  | 
|---|
| 739 | gtk_signal_connect (GTK_OBJECT(subtree), "select_child", | 
|---|
| 740 | GTK_SIGNAL_FUNC(cb_select_child), tree); | 
|---|
| 741 | gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child", | 
|---|
| 742 | GTK_SIGNAL_FUNC(cb_unselect_child), tree); | 
|---|
| 743 |  | 
|---|
| 744 | /* Now, get the items in smb:/// and add them to the tree */ | 
|---|
| 745 |  | 
|---|
| 746 | dirp = (struct smbc_dirent *)dirbuf; | 
|---|
| 747 |  | 
|---|
| 748 | while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf, | 
|---|
| 749 | sizeof(dirbuf))) != 0) { | 
|---|
| 750 |  | 
|---|
| 751 | if (err < 0) { /* Handle the error */ | 
|---|
| 752 |  | 
|---|
| 753 | fprintf(stderr, "Could not read directory for smbc:///: %s\n", | 
|---|
| 754 | strerror(errno)); | 
|---|
| 755 |  | 
|---|
| 756 | exit(1); | 
|---|
| 757 |  | 
|---|
| 758 | } | 
|---|
| 759 |  | 
|---|
| 760 | fprintf(stdout, "Dir len: %u\n", err); | 
|---|
| 761 |  | 
|---|
| 762 | while (err > 0) { /* Extract each entry and make a sub-tree */ | 
|---|
| 763 | struct tree_data *my_data; | 
|---|
| 764 | int dirlen = dirp->dirlen; | 
|---|
| 765 |  | 
|---|
| 766 | my_data = make_tree_data(dirp->smbc_type, dirp->name); | 
|---|
| 767 |  | 
|---|
| 768 | item = gtk_tree_item_new_with_label(dirp->name); | 
|---|
| 769 | /* Connect all GtkItem:: and GtkTreeItem:: signals */ | 
|---|
| 770 | gtk_signal_connect (GTK_OBJECT(item), "select", | 
|---|
| 771 | GTK_SIGNAL_FUNC(cb_itemsignal), "select"); | 
|---|
| 772 | gtk_signal_connect (GTK_OBJECT(item), "deselect", | 
|---|
| 773 | GTK_SIGNAL_FUNC(cb_itemsignal), "deselect"); | 
|---|
| 774 | gtk_signal_connect (GTK_OBJECT(item), "toggle", | 
|---|
| 775 | GTK_SIGNAL_FUNC(cb_itemsignal), "toggle"); | 
|---|
| 776 | gtk_signal_connect (GTK_OBJECT(item), "expand", | 
|---|
| 777 | GTK_SIGNAL_FUNC(cb_itemsignal), "expand"); | 
|---|
| 778 | gtk_signal_connect (GTK_OBJECT(item), "collapse", | 
|---|
| 779 | GTK_SIGNAL_FUNC(cb_itemsignal), "collapse"); | 
|---|
| 780 | /* Add it to the parent tree */ | 
|---|
| 781 | gtk_tree_append (GTK_TREE(tree), item); | 
|---|
| 782 | /* Show it - this can be done at any time */ | 
|---|
| 783 | gtk_widget_show (item); | 
|---|
| 784 |  | 
|---|
| 785 | gtk_object_set_user_data(GTK_OBJECT(item), (gpointer)my_data); | 
|---|
| 786 |  | 
|---|
| 787 | fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen); | 
|---|
| 788 |  | 
|---|
| 789 | subtree = gtk_tree_new(); | 
|---|
| 790 |  | 
|---|
| 791 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree); | 
|---|
| 792 |  | 
|---|
| 793 | gtk_signal_connect (GTK_OBJECT(subtree), "select_child", | 
|---|
| 794 | GTK_SIGNAL_FUNC(cb_select_child), tree); | 
|---|
| 795 | gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child", | 
|---|
| 796 | GTK_SIGNAL_FUNC(cb_unselect_child), tree); | 
|---|
| 797 |  | 
|---|
| 798 | (char *)dirp += dirlen; | 
|---|
| 799 | err -= dirlen; | 
|---|
| 800 |  | 
|---|
| 801 | } | 
|---|
| 802 |  | 
|---|
| 803 | } | 
|---|
| 804 |  | 
|---|
| 805 | smbc_closedir(dh); /* FIXME, check for error :-) */ | 
|---|
| 806 |  | 
|---|
| 807 | /* Show the window and loop endlessly */ | 
|---|
| 808 | gtk_main(); | 
|---|
| 809 | TALLOC_FREE(frame); | 
|---|
| 810 | return 0; | 
|---|
| 811 | } | 
|---|
| 812 | /* example-end */ | 
|---|