| 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 2 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, write to the Free Software
|
|---|
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 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 <gtk/gtk.h>
|
|---|
| 28 | #include "libsmbclient.h"
|
|---|
| 29 |
|
|---|
| 30 | static GtkWidget *clist;
|
|---|
| 31 |
|
|---|
| 32 | struct tree_data {
|
|---|
| 33 |
|
|---|
| 34 | guint32 type; /* Type of tree item, an SMBC_TYPE */
|
|---|
| 35 | char name[256]; /* May need to change this later */
|
|---|
| 36 |
|
|---|
| 37 | };
|
|---|
| 38 |
|
|---|
| 39 | static void tree_error_message(gchar *message) {
|
|---|
| 40 |
|
|---|
| 41 | GtkWidget *dialog, *label, *okay_button;
|
|---|
| 42 |
|
|---|
| 43 | /* Create the widgets */
|
|---|
| 44 |
|
|---|
| 45 | dialog = gtk_dialog_new();
|
|---|
| 46 | gtk_window_set_modal(GTK_WINDOW(dialog), True);
|
|---|
| 47 | label = gtk_label_new (message);
|
|---|
| 48 | okay_button = gtk_button_new_with_label("Okay");
|
|---|
| 49 |
|
|---|
| 50 | /* Ensure that the dialog box is destroyed when the user clicks ok. */
|
|---|
| 51 |
|
|---|
| 52 | gtk_signal_connect_object (GTK_OBJECT (okay_button), "clicked",
|
|---|
| 53 | GTK_SIGNAL_FUNC (gtk_widget_destroy), dialog);
|
|---|
| 54 | gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area),
|
|---|
| 55 | okay_button);
|
|---|
| 56 |
|
|---|
| 57 | /* Add the label, and show everything we've added to the dialog. */
|
|---|
| 58 |
|
|---|
| 59 | gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
|
|---|
| 60 | label);
|
|---|
| 61 | gtk_widget_show_all (dialog);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /*
|
|---|
| 65 | * We are given a widget, and we want to retrieve its URL so we
|
|---|
| 66 | * can do a directory listing.
|
|---|
| 67 | *
|
|---|
| 68 | * We walk back up the tree, picking up pieces until we hit a server or
|
|---|
| 69 | * workgroup type and return a path from there
|
|---|
| 70 | */
|
|---|
| 71 |
|
|---|
| 72 | static pstring path_string;
|
|---|
| 73 |
|
|---|
| 74 | char *get_path(GtkWidget *item)
|
|---|
| 75 | {
|
|---|
| 76 | GtkWidget *p = item;
|
|---|
| 77 | struct tree_data *pd;
|
|---|
| 78 | char *comps[1024]; /* We keep pointers to the components here */
|
|---|
| 79 | int i = 0, j, level,type;
|
|---|
| 80 |
|
|---|
| 81 | /* Walk back up the tree, getting the private data */
|
|---|
| 82 |
|
|---|
| 83 | level = GTK_TREE(item->parent)->level;
|
|---|
| 84 |
|
|---|
| 85 | /* Pick up this item's component info */
|
|---|
| 86 |
|
|---|
| 87 | pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(item));
|
|---|
| 88 |
|
|---|
| 89 | comps[i++] = pd->name;
|
|---|
| 90 | type = pd->type;
|
|---|
| 91 |
|
|---|
| 92 | while (level > 0 && type != SMBC_SERVER && type != SMBC_WORKGROUP) {
|
|---|
| 93 |
|
|---|
| 94 | /* Find the parent and extract the data etc ... */
|
|---|
| 95 |
|
|---|
| 96 | p = GTK_WIDGET(p->parent);
|
|---|
| 97 | p = GTK_WIDGET(GTK_TREE(p)->tree_owner);
|
|---|
| 98 |
|
|---|
| 99 | pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(p));
|
|---|
| 100 |
|
|---|
| 101 | level = GTK_TREE(item->parent)->level;
|
|---|
| 102 |
|
|---|
| 103 | comps[i++] = pd->name;
|
|---|
| 104 | type = pd->type;
|
|---|
| 105 |
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /*
|
|---|
| 109 | * Got a list of comps now, should check that we did not hit a workgroup
|
|---|
| 110 | * when we got other things as well ... Later
|
|---|
| 111 | *
|
|---|
| 112 | * Now, build the path
|
|---|
| 113 | */
|
|---|
| 114 |
|
|---|
| 115 | pstrcpy( path_string, "smb:/" );
|
|---|
| 116 |
|
|---|
| 117 | for (j = i - 1; j >= 0; j--) {
|
|---|
| 118 |
|
|---|
| 119 | strncat(path_string, "/", sizeof(path_string) - strlen(path_string));
|
|---|
| 120 | strncat(path_string, comps[j], sizeof(path_string) - strlen(path_string));
|
|---|
| 121 |
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | fprintf(stdout, "Path string = %s\n", path_string);
|
|---|
| 125 |
|
|---|
| 126 | return path_string;
|
|---|
| 127 |
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | struct tree_data *make_tree_data(guint32 type, const char *name)
|
|---|
| 131 | {
|
|---|
| 132 | struct tree_data *p = SMB_MALLOC_P(struct tree_data);
|
|---|
| 133 |
|
|---|
| 134 | if (p) {
|
|---|
| 135 |
|
|---|
| 136 | p->type = type;
|
|---|
| 137 | strncpy(p->name, name, sizeof(p->name));
|
|---|
| 138 |
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | return p;
|
|---|
| 142 |
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /* Note that this is called every time the user clicks on an item,
|
|---|
| 146 | whether it is already selected or not. */
|
|---|
| 147 | static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
|
|---|
| 148 | GtkWidget *subtree)
|
|---|
| 149 | {
|
|---|
| 150 | gint dh, err, dirlen;
|
|---|
| 151 | char dirbuf[512];
|
|---|
| 152 | struct smbc_dirent *dirp;
|
|---|
| 153 | struct stat st1;
|
|---|
| 154 | pstring path, path1;
|
|---|
| 155 |
|
|---|
| 156 | g_print ("select_child called for root tree %p, subtree %p, child %p\n",
|
|---|
| 157 | root_tree, subtree, child);
|
|---|
| 158 |
|
|---|
| 159 | /* Now, figure out what it is, and display it in the clist ... */
|
|---|
| 160 |
|
|---|
| 161 | gtk_clist_clear(GTK_CLIST(clist)); /* Clear the CLIST */
|
|---|
| 162 |
|
|---|
| 163 | /* Now, get the private data for the subtree */
|
|---|
| 164 |
|
|---|
| 165 | strncpy(path, get_path(child), 1024);
|
|---|
| 166 |
|
|---|
| 167 | if ((dh = smbc_opendir(path)) < 0) { /* Handle error */
|
|---|
| 168 |
|
|---|
| 169 | g_print("cb_select_child: Could not open dir %s, %s\n", path,
|
|---|
| 170 | strerror(errno));
|
|---|
| 171 |
|
|---|
| 172 | gtk_main_quit();
|
|---|
| 173 |
|
|---|
| 174 | return;
|
|---|
| 175 |
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
|
|---|
| 179 | sizeof(dirbuf))) != 0) {
|
|---|
| 180 |
|
|---|
| 181 | if (err < 0) {
|
|---|
| 182 |
|
|---|
| 183 | g_print("cb_select_child: Could not read dir %s, %s\n", path,
|
|---|
| 184 | strerror(errno));
|
|---|
| 185 |
|
|---|
| 186 | gtk_main_quit();
|
|---|
| 187 |
|
|---|
| 188 | return;
|
|---|
| 189 |
|
|---|
| 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 |
|
|---|
| 245 | strncpy(path1, path, sizeof(path1));
|
|---|
| 246 | strncat(path1, "/", sizeof(path) - strlen(path));
|
|---|
| 247 | strncat(path1, dirp->name, sizeof(path) - strlen(path));
|
|---|
| 248 |
|
|---|
| 249 | if (smbc_stat(path1, &st1) < 0) {
|
|---|
| 250 |
|
|---|
| 251 | if (errno != EBUSY) {
|
|---|
| 252 |
|
|---|
| 253 | g_print("cb_select_child: Could not stat file %s, %s\n", path1,
|
|---|
| 254 | strerror(errno));
|
|---|
| 255 |
|
|---|
| 256 | gtk_main_quit();
|
|---|
| 257 |
|
|---|
| 258 | return;
|
|---|
| 259 |
|
|---|
| 260 | }
|
|---|
| 261 | else {
|
|---|
| 262 |
|
|---|
| 263 | strncpy(col2, "Device or resource busy", sizeof(col2));
|
|---|
| 264 |
|
|---|
| 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 | }
|
|---|
| 301 |
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /* Note that this is never called */
|
|---|
| 305 | static void cb_unselect_child( GtkWidget *root_tree,
|
|---|
| 306 | GtkWidget *child,
|
|---|
| 307 | GtkWidget *subtree )
|
|---|
| 308 | {
|
|---|
| 309 | g_print ("unselect_child called for root tree %p, subtree %p, child %p\n",
|
|---|
| 310 | root_tree, subtree, child);
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | /* for all the GtkItem:: and GtkTreeItem:: signals */
|
|---|
| 314 | static void cb_itemsignal( GtkWidget *item,
|
|---|
| 315 | gchar *signame )
|
|---|
| 316 | {
|
|---|
| 317 | GtkWidget *real_tree, *aitem, *subtree;
|
|---|
| 318 | gchar *name;
|
|---|
| 319 | GtkLabel *label;
|
|---|
| 320 | gint dh, err, dirlen, level;
|
|---|
| 321 | char dirbuf[512];
|
|---|
| 322 | struct smbc_dirent *dirp;
|
|---|
| 323 |
|
|---|
| 324 | label = GTK_LABEL (GTK_BIN (item)->child);
|
|---|
| 325 | /* Get the text of the label */
|
|---|
| 326 | gtk_label_get (label, &name);
|
|---|
| 327 |
|
|---|
| 328 | level = GTK_TREE(item->parent)->level;
|
|---|
| 329 |
|
|---|
| 330 | /* Get the level of the tree which the item is in */
|
|---|
| 331 | g_print ("%s called for item %s->%p, level %d\n", signame, name,
|
|---|
| 332 | item, GTK_TREE (item->parent)->level);
|
|---|
| 333 |
|
|---|
| 334 | real_tree = GTK_TREE_ITEM_SUBTREE(item); /* Get the subtree */
|
|---|
| 335 |
|
|---|
| 336 | if (strncmp(signame, "expand", 6) == 0) { /* Expand called */
|
|---|
| 337 | char server[128];
|
|---|
| 338 |
|
|---|
| 339 | if ((dh = smbc_opendir(get_path(item))) < 0) { /* Handle error */
|
|---|
| 340 | gchar errmsg[256];
|
|---|
| 341 |
|
|---|
| 342 | g_print("cb_itemsignal: Could not open dir %s, %s\n", get_path(item),
|
|---|
| 343 | strerror(errno));
|
|---|
| 344 |
|
|---|
| 345 | slprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not open dir %s, %s\n", get_path(item), strerror(errno));
|
|---|
| 346 |
|
|---|
| 347 | tree_error_message(errmsg);
|
|---|
| 348 |
|
|---|
| 349 | /* gtk_main_quit();*/
|
|---|
| 350 |
|
|---|
| 351 | return;
|
|---|
| 352 |
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
|
|---|
| 356 | sizeof(dirbuf))) != 0) {
|
|---|
| 357 |
|
|---|
| 358 | if (err < 0) { /* An error, report it */
|
|---|
| 359 | gchar errmsg[256];
|
|---|
| 360 |
|
|---|
| 361 | g_print("cb_itemsignal: Could not read dir smbc://, %s\n",
|
|---|
| 362 | strerror(errno));
|
|---|
| 363 |
|
|---|
| 364 | slprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not read dir smbc://, %s\n", strerror(errno));
|
|---|
| 365 |
|
|---|
| 366 | tree_error_message(errmsg);
|
|---|
| 367 |
|
|---|
| 368 | /* gtk_main_quit();*/
|
|---|
| 369 |
|
|---|
| 370 | return;
|
|---|
| 371 |
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | dirp = (struct smbc_dirent *)dirbuf;
|
|---|
| 375 |
|
|---|
| 376 | while (err > 0) {
|
|---|
| 377 | struct tree_data *my_data;
|
|---|
| 378 |
|
|---|
| 379 | dirlen = dirp->dirlen;
|
|---|
| 380 |
|
|---|
| 381 | my_data = make_tree_data(dirp->smbc_type, dirp->name);
|
|---|
| 382 |
|
|---|
| 383 | if (!my_data) {
|
|---|
| 384 |
|
|---|
| 385 | g_print("Could not allocate space for tree_data: %s\n",
|
|---|
| 386 | dirp->name);
|
|---|
| 387 |
|
|---|
| 388 | gtk_main_quit();
|
|---|
| 389 | return;
|
|---|
| 390 |
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | aitem = gtk_tree_item_new_with_label(dirp->name);
|
|---|
| 394 |
|
|---|
| 395 | /* Connect all GtkItem:: and GtkTreeItem:: signals */
|
|---|
| 396 | gtk_signal_connect (GTK_OBJECT(aitem), "select",
|
|---|
| 397 | GTK_SIGNAL_FUNC(cb_itemsignal), "select");
|
|---|
| 398 | gtk_signal_connect (GTK_OBJECT(aitem), "deselect",
|
|---|
| 399 | GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
|
|---|
| 400 | gtk_signal_connect (GTK_OBJECT(aitem), "toggle",
|
|---|
| 401 | GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
|
|---|
| 402 | gtk_signal_connect (GTK_OBJECT(aitem), "expand",
|
|---|
| 403 | GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
|
|---|
| 404 | gtk_signal_connect (GTK_OBJECT(aitem), "collapse",
|
|---|
| 405 | GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
|
|---|
| 406 | /* Add it to the parent tree */
|
|---|
| 407 | gtk_tree_append (GTK_TREE(real_tree), aitem);
|
|---|
| 408 |
|
|---|
| 409 | gtk_widget_show (aitem);
|
|---|
| 410 |
|
|---|
| 411 | gtk_object_set_user_data(GTK_OBJECT(aitem), (gpointer)my_data);
|
|---|
| 412 |
|
|---|
| 413 | fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen);
|
|---|
| 414 |
|
|---|
| 415 | if (dirp->smbc_type != SMBC_FILE &&
|
|---|
| 416 | dirp->smbc_type != SMBC_IPC_SHARE &&
|
|---|
| 417 | (strcmp(dirp->name, ".") != 0) &&
|
|---|
| 418 | (strcmp(dirp->name, "..") !=0)){
|
|---|
| 419 |
|
|---|
| 420 | subtree = gtk_tree_new();
|
|---|
| 421 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem), subtree);
|
|---|
| 422 |
|
|---|
| 423 | gtk_signal_connect(GTK_OBJECT(subtree), "select_child",
|
|---|
| 424 | GTK_SIGNAL_FUNC(cb_select_child), real_tree);
|
|---|
| 425 | gtk_signal_connect(GTK_OBJECT(subtree), "unselect_child",
|
|---|
| 426 | GTK_SIGNAL_FUNC(cb_unselect_child), real_tree);
|
|---|
| 427 |
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | (char *)dirp += dirlen;
|
|---|
| 431 | err -= dirlen;
|
|---|
| 432 |
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | smbc_closedir(dh);
|
|---|
| 438 |
|
|---|
| 439 | }
|
|---|
| 440 | else if (strncmp(signame, "collapse", 8) == 0) {
|
|---|
| 441 | GtkWidget *subtree = gtk_tree_new();
|
|---|
| 442 |
|
|---|
| 443 | gtk_tree_remove_items(GTK_TREE(real_tree), GTK_TREE(real_tree)->children);
|
|---|
| 444 |
|
|---|
| 445 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree);
|
|---|
| 446 |
|
|---|
| 447 | gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
|
|---|
| 448 | GTK_SIGNAL_FUNC(cb_select_child), real_tree);
|
|---|
| 449 | gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
|
|---|
| 450 | GTK_SIGNAL_FUNC(cb_unselect_child), real_tree);
|
|---|
| 451 |
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | static void cb_selection_changed( GtkWidget *tree )
|
|---|
| 457 | {
|
|---|
| 458 | GList *i;
|
|---|
| 459 |
|
|---|
| 460 | g_print ("selection_change called for tree %p\n", tree);
|
|---|
| 461 | g_print ("selected objects are:\n");
|
|---|
| 462 |
|
|---|
| 463 | i = GTK_TREE_SELECTION(tree);
|
|---|
| 464 | while (i){
|
|---|
| 465 | gchar *name;
|
|---|
| 466 | GtkLabel *label;
|
|---|
| 467 | GtkWidget *item;
|
|---|
| 468 |
|
|---|
| 469 | /* Get a GtkWidget pointer from the list node */
|
|---|
| 470 | item = GTK_WIDGET (i->data);
|
|---|
| 471 | label = GTK_LABEL (GTK_BIN (item)->child);
|
|---|
| 472 | gtk_label_get (label, &name);
|
|---|
| 473 | g_print ("\t%s on level %d\n", name, GTK_TREE
|
|---|
| 474 | (item->parent)->level);
|
|---|
| 475 | i = i->next;
|
|---|
| 476 | }
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | /*
|
|---|
| 480 | * Expand or collapse the whole network ...
|
|---|
| 481 | */
|
|---|
| 482 | static void cb_wholenet(GtkWidget *item, gchar *signame)
|
|---|
| 483 | {
|
|---|
| 484 | GtkWidget *real_tree, *aitem, *subtree;
|
|---|
| 485 | gchar *name;
|
|---|
| 486 | GtkLabel *label;
|
|---|
| 487 | gint dh, err, dirlen;
|
|---|
| 488 | char dirbuf[512];
|
|---|
| 489 | struct smbc_dirent *dirp;
|
|---|
| 490 |
|
|---|
| 491 | label = GTK_LABEL (GTK_BIN (item)->child);
|
|---|
| 492 | gtk_label_get (label, &name);
|
|---|
| 493 | g_print ("%s called for item %s->%p, level %d\n", signame, name,
|
|---|
| 494 | item, GTK_TREE (item->parent)->level);
|
|---|
| 495 |
|
|---|
| 496 | real_tree = GTK_TREE_ITEM_SUBTREE(item); /* Get the subtree */
|
|---|
| 497 |
|
|---|
| 498 | if (strncmp(signame, "expand", 6) == 0) { /* Expand called */
|
|---|
| 499 |
|
|---|
| 500 | if ((dh = smbc_opendir("smb://")) < 0) { /* Handle error */
|
|---|
| 501 |
|
|---|
| 502 | g_print("cb_wholenet: Could not open dir smbc://, %s\n",
|
|---|
| 503 | strerror(errno));
|
|---|
| 504 |
|
|---|
| 505 | gtk_main_quit();
|
|---|
| 506 |
|
|---|
| 507 | return;
|
|---|
| 508 |
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
|
|---|
| 512 | sizeof(dirbuf))) != 0) {
|
|---|
| 513 |
|
|---|
| 514 | if (err < 0) { /* An error, report it */
|
|---|
| 515 |
|
|---|
| 516 | g_print("cb_wholenet: Could not read dir smbc://, %s\n",
|
|---|
| 517 | strerror(errno));
|
|---|
| 518 |
|
|---|
| 519 | gtk_main_quit();
|
|---|
| 520 |
|
|---|
| 521 | return;
|
|---|
| 522 |
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | dirp = (struct smbc_dirent *)dirbuf;
|
|---|
| 526 |
|
|---|
| 527 | while (err > 0) {
|
|---|
| 528 | struct tree_data *my_data;
|
|---|
| 529 |
|
|---|
| 530 | dirlen = dirp->dirlen;
|
|---|
| 531 |
|
|---|
| 532 | my_data = make_tree_data(dirp->smbc_type, dirp->name);
|
|---|
| 533 |
|
|---|
| 534 | aitem = gtk_tree_item_new_with_label(dirp->name);
|
|---|
| 535 |
|
|---|
| 536 | /* Connect all GtkItem:: and GtkTreeItem:: signals */
|
|---|
| 537 | gtk_signal_connect (GTK_OBJECT(aitem), "select",
|
|---|
| 538 | GTK_SIGNAL_FUNC(cb_itemsignal), "select");
|
|---|
| 539 | gtk_signal_connect (GTK_OBJECT(aitem), "deselect",
|
|---|
| 540 | GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
|
|---|
| 541 | gtk_signal_connect (GTK_OBJECT(aitem), "toggle",
|
|---|
| 542 | GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
|
|---|
| 543 | gtk_signal_connect (GTK_OBJECT(aitem), "expand",
|
|---|
| 544 | GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
|
|---|
| 545 | gtk_signal_connect (GTK_OBJECT(aitem), "collapse",
|
|---|
| 546 | GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
|
|---|
| 547 |
|
|---|
| 548 | gtk_tree_append (GTK_TREE(real_tree), aitem);
|
|---|
| 549 | /* Show it - this can be done at any time */
|
|---|
| 550 | gtk_widget_show (aitem);
|
|---|
| 551 |
|
|---|
| 552 | gtk_object_set_user_data(GTK_OBJECT(aitem), (gpointer)my_data);
|
|---|
| 553 |
|
|---|
| 554 | fprintf(stdout, "Added: %s, len: %u\n", dirp->name, dirlen);
|
|---|
| 555 |
|
|---|
| 556 | subtree = gtk_tree_new();
|
|---|
| 557 |
|
|---|
| 558 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(aitem), subtree);
|
|---|
| 559 |
|
|---|
| 560 | gtk_signal_connect(GTK_OBJECT(subtree), "select_child",
|
|---|
| 561 | GTK_SIGNAL_FUNC(cb_select_child), real_tree);
|
|---|
| 562 | gtk_signal_connect(GTK_OBJECT(subtree), "unselect_child",
|
|---|
| 563 | GTK_SIGNAL_FUNC(cb_unselect_child), real_tree);
|
|---|
| 564 |
|
|---|
| 565 | (char *)dirp += dirlen;
|
|---|
| 566 | err -= dirlen;
|
|---|
| 567 |
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | smbc_closedir(dh);
|
|---|
| 573 |
|
|---|
| 574 | }
|
|---|
| 575 | else { /* Must be collapse ... FIXME ... */
|
|---|
| 576 | GtkWidget *subtree = gtk_tree_new();
|
|---|
| 577 |
|
|---|
| 578 | gtk_tree_remove_items(GTK_TREE(real_tree), GTK_TREE(real_tree)->children);
|
|---|
| 579 |
|
|---|
| 580 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), subtree);
|
|---|
| 581 |
|
|---|
| 582 | gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
|
|---|
| 583 | GTK_SIGNAL_FUNC(cb_select_child), real_tree);
|
|---|
| 584 | gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
|
|---|
| 585 | GTK_SIGNAL_FUNC(cb_unselect_child), real_tree);
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | /* Should put up a dialog box to ask the user for username and password */
|
|---|
| 593 |
|
|---|
| 594 | static void
|
|---|
| 595 | auth_fn(const char *server, const char *share,
|
|---|
| 596 | char *workgroup, int wgmaxlen, char *username, int unmaxlen,
|
|---|
| 597 | char *password, int pwmaxlen)
|
|---|
| 598 | {
|
|---|
| 599 |
|
|---|
| 600 | strncpy(username, "test", unmaxlen);
|
|---|
| 601 | strncpy(password, "test", pwmaxlen);
|
|---|
| 602 |
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | static char *col_titles[] = {
|
|---|
| 606 | "Name", "Attributes", "Size", "Modification Date",
|
|---|
| 607 | };
|
|---|
| 608 |
|
|---|
| 609 | int main( int argc,
|
|---|
| 610 | char *argv[] )
|
|---|
| 611 | {
|
|---|
| 612 | GtkWidget *window, *scrolled_win, *scrolled_win2, *tree;
|
|---|
| 613 | GtkWidget *subtree, *item, *main_hbox, *r_pane, *l_pane;
|
|---|
| 614 | gint err, dh;
|
|---|
| 615 | gint i;
|
|---|
| 616 | char dirbuf[512];
|
|---|
| 617 | struct smbc_dirent *dirp;
|
|---|
| 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 | return 0;
|
|---|
| 810 | }
|
|---|
| 811 | /* example-end */
|
|---|