comparison src/rvous.c @ 200:bc117fbcf527

[gaim-migrate @ 210] Made the receive non-blocking, added a cancel button, and a few other updates. No, sending a file to someone does not work yet. Be patient. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Sun, 30 Apr 2000 21:47:04 +0000
parents 73e9a0bfff62
children d9a5f7b1d500
comparison
equal deleted inserted replaced
199:be5fbefdb195 200:bc117fbcf527
37 #include "gaim.h" 37 #include "gaim.h"
38 38
39 static void do_send_file(GtkWidget *, struct file_transfer *); 39 static void do_send_file(GtkWidget *, struct file_transfer *);
40 static void do_get_file (GtkWidget *, struct file_transfer *); 40 static void do_get_file (GtkWidget *, struct file_transfer *);
41 41
42 static void toggle(GtkWidget *w, int *m)
43 {
44 *m = !(*m);
45 }
46
47 static void free_ft(struct file_transfer *ft)
48 {
49 if (ft->window) { gtk_widget_destroy(ft->window); ft->window = NULL; }
50 if (ft->filename) g_free(ft->filename);
51 if (ft->user) g_free(ft->user);
52 if (ft->message) g_free(ft->message);
53 if (ft->ip) g_free(ft->ip);
54 if (ft->cookie) g_free(ft->cookie);
55 g_free(ft);
56 }
57
42 static void warn_callback(GtkWidget *widget, struct file_transfer *ft) 58 static void warn_callback(GtkWidget *widget, struct file_transfer *ft)
43 { 59 {
44 show_warn_dialog(ft->user); 60 show_warn_dialog(ft->user);
45 } 61 }
46 62
60 76
61 g_snprintf(send, 255, "toc_rvous_cancel %s %s %s", normalize(ft->user), 77 g_snprintf(send, 255, "toc_rvous_cancel %s %s %s", normalize(ft->user),
62 ft->cookie, ft->UID); 78 ft->cookie, ft->UID);
63 sflap_send(send, strlen(send), TYPE_DATA); 79 sflap_send(send, strlen(send), TYPE_DATA);
64 g_free(send); 80 g_free(send);
65 gtk_widget_destroy(ft->window); 81 free_ft(ft);
66 ft->window = NULL;
67 g_free(ft->user);
68 if (ft->message)
69 g_free(ft->message);
70 g_free(ft->filename);
71 if (ft->cookie) g_free(ft->cookie);
72 g_free(ft->ip);
73 g_free(ft);
74 } 82 }
75 83
76 static void accept_callback(GtkWidget *widget, struct file_transfer *ft) 84 static void accept_callback(GtkWidget *widget, struct file_transfer *ft)
77 { 85 {
78 char *buf = g_malloc(BUF_LEN); 86 char *buf = g_malloc(BUF_LEN);
138 int read_rv; 146 int read_rv;
139 char bmagic[7]; 147 char bmagic[7];
140 struct sockaddr_in sin; 148 struct sockaddr_in sin;
141 guint32 rcv; 149 guint32 rcv;
142 char *c; 150 char *c;
143 GtkWidget *fw = NULL, *fbar = NULL, *label; 151 int cont = 1;
152 GtkWidget *fw = NULL, *fbar = NULL, *label = NULL, *button = NULL;
144 153
145 if (!(ft->f = fopen(file,"w"))) { 154 if (!(ft->f = fopen(file,"w"))) {
146 buf = g_malloc(BUF_LONG); 155 buf = g_malloc(BUF_LONG);
147 g_snprintf(buf, BUF_LONG / 2, "Error writing file %s", file); 156 g_snprintf(buf, BUF_LONG / 2, "Error writing file %s", file);
148 do_error_dialog(buf, "Error"); 157 do_error_dialog(buf, "Error");
178 while (rcv != 6) { 187 while (rcv != 6) {
179 read_rv = read(ft->fd, header + rcv, 6 - rcv); 188 read_rv = read(ft->fd, header + rcv, 6 - rcv);
180 if(read_rv < 0) { 189 if(read_rv < 0) {
181 close(ft->fd); 190 close(ft->fd);
182 g_free(header); 191 g_free(header);
192 free_ft(ft);
183 return; 193 return;
184 } 194 }
185 rcv += read_rv; 195 rcv += read_rv;
186 while(gtk_events_pending()) 196 while(gtk_events_pending())
187 gtk_main_iteration(); 197 gtk_main_iteration();
206 while (rcv != hdrlen) { 216 while (rcv != hdrlen) {
207 read_rv = read(ft->fd, header + rcv, hdrlen - rcv); 217 read_rv = read(ft->fd, header + rcv, hdrlen - rcv);
208 if(read_rv < 0) { 218 if(read_rv < 0) {
209 close(ft->fd); 219 close(ft->fd);
210 g_free(header); 220 g_free(header);
221 free_ft(ft);
211 return; 222 return;
212 } 223 }
213 rcv += read_rv; 224 rcv += read_rv;
214 while(gtk_events_pending()) 225 while(gtk_events_pending())
215 gtk_main_iteration(); 226 gtk_main_iteration();
243 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fw)->vbox), label, 0, 0, 5); 254 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fw)->vbox), label, 0, 0, 5);
244 gtk_widget_show(label); 255 gtk_widget_show(label);
245 fbar = gtk_progress_bar_new(); 256 fbar = gtk_progress_bar_new();
246 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fw)->action_area), fbar, 0, 0, 5); 257 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fw)->action_area), fbar, 0, 0, 5);
247 gtk_widget_show(fbar); 258 gtk_widget_show(fbar);
259 button = gtk_button_new_with_label("Cancel");
260 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fw)->action_area), button, 0, 0, 5);
261 gtk_widget_show(button);
262 gtk_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc)toggle, &cont);
248 gtk_window_set_title(GTK_WINDOW(fw), "File Transfer"); 263 gtk_window_set_title(GTK_WINDOW(fw), "File Transfer");
249 gtk_widget_realize(fw); 264 gtk_widget_realize(fw);
250 aol_icon(fw->window); 265 aol_icon(fw->window);
251 gtk_widget_show(fw); 266 gtk_widget_show(fw);
252 267
253 sprintf(debug_buff, "Receiving %s from %s (%d bytes)\n", ft->filename, 268 sprintf(debug_buff, "Receiving %s from %s (%d bytes)\n", ft->filename,
254 ft->user, ft->size); 269 ft->user, ft->size);
255 debug_print(debug_buff); 270 debug_print(debug_buff);
256 271
257 while (rcv != ft->size) { 272 while (rcv != ft->size && cont) {
258 int i; 273 int i;
259 int remain = ft->size - rcv > 1024 ? 1024 : ft->size - rcv; 274 int remain = ft->size - rcv > 1024 ? 1024 : ft->size - rcv;
260 read_rv = read(ft->fd, buf, remain); 275 read_rv = recv(ft->fd, buf, remain, O_NONBLOCK);
261 if(read_rv < 0) { 276 if(read_rv < 0) {
262 fclose(ft->f); 277 fclose(ft->f);
263 close(ft->fd); 278 close(ft->fd);
264 g_free(buf); 279 g_free(buf);
265 g_free(header); 280 g_free(header);
281 free_ft(ft);
266 return; 282 return;
267 } 283 }
268 rcv += read_rv; 284 rcv += read_rv;
269 for (i = 0; i < read_rv; i++) 285 for (i = 0; i < read_rv; i++)
270 fprintf(ft->f, "%c", buf[i]); 286 fprintf(ft->f, "%c", buf[i]);
274 gtk_progress_bar_update(GTK_PROGRESS_BAR(fbar), 290 gtk_progress_bar_update(GTK_PROGRESS_BAR(fbar),
275 (float)(rcv)/(float)(ft->size)); 291 (float)(rcv)/(float)(ft->size));
276 while(gtk_events_pending()) 292 while(gtk_events_pending())
277 gtk_main_iteration(); 293 gtk_main_iteration();
278 } 294 }
279
280 fclose(ft->f); 295 fclose(ft->f);
296
297 if (!cont) {
298 char *tmp = frombase64(ft->cookie);
299 sprintf(buf, "toc_rvous_cancel %s %s %s", ft->user, tmp, ft->UID);
300 sflap_send(buf, strlen(buf), TYPE_DATA);
301 gtk_widget_destroy(fw);
302 fw = NULL;
303 close(ft->fd);
304 free_ft(ft);
305 return;
306 }
307
281 memset(header + 18, 0, 4); 308 memset(header + 18, 0, 4);
282 header[94] = 0; 309 header[94] = 0;
283 header[1] = 0x04; 310 header[1] = 0x04;
284 memcpy(header+58, header+34, 4); 311 memcpy(header+58, header+34, 4);
285 memcpy(header+54, header+22, 4); 312 memcpy(header+54, header+22, 4);
287 write(ft->fd, header, hdrlen); 314 write(ft->fd, header, hdrlen);
288 close(ft->fd); 315 close(ft->fd);
289 316
290 g_free(buf); 317 g_free(buf);
291 g_free(header); 318 g_free(header);
292 gtk_widget_destroy(fw); 319 free_ft(ft);
293 g_free(ft->cookie);
294 g_free(ft->ip);
295 if (ft->message)
296 g_free(ft->message);
297 g_free(ft->filename);
298 g_free(ft->user);
299 g_free(ft);
300 } 320 }
301 321
302 static void do_send_file(GtkWidget *w, struct file_transfer *ft) { 322 static void do_send_file(GtkWidget *w, struct file_transfer *ft) {
303 char *send = g_malloc(256); 323 char *send = g_malloc(256);
304 char *file = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION(ft->window))); 324 char *file = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION(ft->window)));
354 * 5. receive header 374 * 5. receive header
355 */ 375 */
356 376
357 /* 1. build/send header */ 377 /* 1. build/send header */
358 buf = frombase64(ft->cookie); 378 buf = frombase64(ft->cookie);
359 sprintf(debug_buff, "Building header to send %s\n", file); 379 sprintf(debug_buff, "Building header to send %s (cookie: %s)\n", file, buf);
360 debug_print(debug_buff); 380 debug_print(debug_buff);
361 fhdr->hdrtype = 0x101; 381 fhdr->hdrtype = 0x1108;
362 snprintf(fhdr->bcookie, 9, "%s", buf); 382 snprintf(fhdr->bcookie, 9, "%s", buf);
363 g_free(buf); 383 g_free(buf);
364 fhdr->encrypt = htons(0); 384 fhdr->encrypt = 0;
365 fhdr->compress = htons(0); 385 fhdr->compress = 0;
366 fhdr->totfiles = htons(1); 386 fhdr->totfiles = 1;
367 fhdr->filesleft = htons(1); 387 fhdr->filesleft = 1;
368 fhdr->totparts = htons(1); 388 fhdr->totparts = 1;
369 fhdr->partsleft = htons(1); 389 fhdr->partsleft = 1;
370 fhdr->totsize = htonl((long)(st.st_size)); 390 fhdr->totsize = (long)st.st_size;
371 fhdr->size = htonl((long)(st.st_size)); 391 fhdr->size = htonl((long)(st.st_size));
372 fhdr->modtime = htonl(0); 392 fhdr->modtime = htonl(0);
373 fhdr->checksum = htonl(0); /* FIXME? */ 393 fhdr->checksum = htonl(0); /* FIXME? */
374 fhdr->rfrcsum = htonl(0); 394 fhdr->rfrcsum = 0;
375 fhdr->rfsize = htonl(0); 395 fhdr->rfsize = 0;
376 fhdr->cretime = htonl(0); 396 fhdr->cretime = 0;
377 fhdr->rfcsum = htonl(0); 397 fhdr->rfcsum = 0;
378 fhdr->nrecvd = htonl(0); 398 fhdr->nrecvd = 0;
379 fhdr->recvcsum = htonl(0); 399 fhdr->recvcsum = 0;
380 snprintf(fhdr->idstring, 32, "Gaim"); 400 snprintf(fhdr->idstring, 32, "Gaim");
381 fhdr->flags = 0x20; /* don't ask me why */ 401 fhdr->flags = 0x20; /* don't ask me why */
382 fhdr->lnameoffset = htonl(0); 402 fhdr->lnameoffset = 0x1A;
383 fhdr->lsizeoffset = htonl(0); 403 fhdr->lsizeoffset = 0x10;
384 snprintf(fhdr->dummy, 69, ""); 404 fhdr->dummy[0] = 0;
385 snprintf(fhdr->macfileinfo, 16, ""); 405 fhdr->macfileinfo[0] = 0;
386 fhdr->nencode = htons(0); 406 fhdr->nencode = 0;
387 fhdr->nlanguage = htons(0); 407 fhdr->nlanguage = 0;
388 c = &file[strlen(file) - 1]; 408 snprintf(fhdr->name, 64, "listing.txt");
389 while (*(c - 1) != '/') c--; 409 snprintf(bmagic, 7, "OFT2\001\000");
390 snprintf(fhdr->name, 64, "%s", c);
391 snprintf(bmagic, 7, "GAIM\001\000");
392 read_rv = write(ft->fd, bmagic, 6); 410 read_rv = write(ft->fd, bmagic, 6);
393 if (read_rv <= -1) { 411 if (read_rv <= -1) {
394 sprintf(debug_buff, "Couldn't write opening header \n"); 412 sprintf(debug_buff, "Couldn't write opening header \n");
395 debug_print(debug_buff); 413 debug_print(debug_buff);
396 close(ft->fd); 414 close(ft->fd);
446 sprintf(debug_buff, "Sending file\n"); 464 sprintf(debug_buff, "Sending file\n");
447 debug_print(debug_buff); 465 debug_print(debug_buff);
448 rcv = 0; 466 rcv = 0;
449 buf = g_malloc(2048); 467 buf = g_malloc(2048);
450 fw = gtk_dialog_new(); 468 fw = gtk_dialog_new();
451 snprintf(buf, 2048, "Sendin %s to %s (%d bytes)", fhdr->name, 469 snprintf(buf, 2048, "Sendin %s to %s (%ld bytes)", fhdr->name,
452 ft->user, fhdr->size); 470 ft->user, fhdr->size);
453 label = gtk_label_new(buf); 471 label = gtk_label_new(buf);
454 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fw)->vbox), label, 0, 0, 5); 472 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(fw)->vbox), label, 0, 0, 5);
455 gtk_widget_show(label); 473 gtk_widget_show(label);
456 fbar = gtk_progress_bar_new(); 474 fbar = gtk_progress_bar_new();
477 close(ft->fd); 495 close(ft->fd);
478 gtk_widget_destroy(fw); 496 gtk_widget_destroy(fw);
479 return; 497 return;
480 } 498 }
481 rcv += read_rv; 499 rcv += read_rv;
482 snprintf(buf, 2048, "Sending %s to %s (%d / %d bytes)", 500 snprintf(buf, 2048, "Sending %s to %s (%d / %ld bytes)",
483 fhdr->name, ft->user, rcv, st.st_size); 501 fhdr->name, ft->user, rcv, st.st_size);
484 gtk_label_set_text(GTK_LABEL(label), buf); 502 gtk_label_set_text(GTK_LABEL(label), buf);
485 gtk_progress_bar_update(GTK_PROGRESS_BAR(fbar), 503 gtk_progress_bar_update(GTK_PROGRESS_BAR(fbar),
486 (float)(rcv)/(float)(ft->size)); 504 (float)(rcv)/(float)(ft->size));
487 while(gtk_events_pending()) 505 while(gtk_events_pending())
508 } 526 }
509 527
510 fclose(ft->f); 528 fclose(ft->f);
511 close(ft->fd); 529 close(ft->fd);
512 g_free(fhdr); 530 g_free(fhdr);
513 g_free(ft->cookie);
514 g_free(ft->ip);
515 if (ft->message)
516 g_free(ft->message);
517 g_free(ft->user);
518 g_free(ft);
519 } 531 }
520 532
521 void accept_file_dialog(struct file_transfer *ft) 533 void accept_file_dialog(struct file_transfer *ft)
522 { 534 {
523 GtkWidget *accept, *info, *warn, *cancel; 535 GtkWidget *accept, *info, *warn, *cancel;