comparison src/ft.c @ 3609:4faf84dfdda2

[gaim-migrate @ 3722] First draft of the file transfer prpl interface. committer: Tailor Script <tailor@pidgin.im>
author Rob Flynn <gaim@robflynn.com>
date Wed, 09 Oct 2002 04:55:29 +0000
parents
children 9313e146daf4
comparison
equal deleted inserted replaced
3608:7703b2689791 3609:4faf84dfdda2
1 /*
2 * gaim - file transfer functions
3 *
4 * Copyright (C) 2002, Wil Mahan <wtm2@duke.edu>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <string.h>
30
31 #define FT_BUFFER_SIZE (4096)
32
33 #include <gtk/gtk.h>
34 #include "gaim.h"
35 #include "proxy.h"
36 #include "prpl.h"
37
38 /* Completely describes a file transfer. Opaque to callers. */
39 struct file_transfer {
40 enum { FILE_TRANSFER_TYPE_SEND, FILE_TRANSFER_TYPE_RECEIVE } type;
41
42 enum {
43 FILE_TRANSFER_STATE_ASK, /* waiting for confirmation */
44 FILE_TRANSFER_STATE_CHOOSEFILE, /* waiting for file dialog */
45 FILE_TRANSFER_STATE_TRANSFERRING, /* in actual transfer */
46 FILE_TRANSFER_STATE_INTERRUPTED, /* other user canceled */
47 FILE_TRANSFER_STATE_CANCELED, /* we canceled */
48 FILE_TRANSFER_STATE_DONE, /* transfer complete */
49 FILE_TRANSFER_STATE_CLEANUP /* freeing memory */
50 } state;
51
52 char *who;
53
54 /* file selection dialog */
55 GtkWidget *w;
56 char **names;
57 int *sizes;
58 char *dir;
59 char *initname;
60 FILE *file;
61
62 /* connection info */
63 struct gaim_connection *gc;
64 int fd;
65 int watcher;
66
67 /* state */
68 int totfiles;
69 int filesdone;
70 int totsize;
71 int bytessent;
72 int bytesleft;
73 };
74
75 static int ft_choose_file(gpointer a, struct file_transfer *xfer);
76 static void ft_cancel(gpointer w, struct file_transfer *xfer);
77 static void ft_delete(struct file_transfer *xfer);
78 static void ft_callback(gpointer data, gint source,
79 GaimInputCondition condition);
80 static void ft_nextfile(struct file_transfer *xfer);
81 static int ft_mkdir(const char *name);
82 static int ft_mkdir_help(char *dir);
83
84 static struct file_transfer *ft_new(int type, struct gaim_connection *gc,
85 const char *who)
86 {
87 struct file_transfer *xfer = g_new0(struct file_transfer, 1);
88 xfer->type = type;
89 xfer->state = FILE_TRANSFER_STATE_ASK;
90 xfer->gc = gc;
91 xfer->who = g_strdup(who);
92 xfer->filesdone = 0;
93
94 return xfer;
95 }
96
97 struct file_transfer *transfer_in_add(struct gaim_connection *gc,
98 const char *who, const char *initname, int totsize,
99 int totfiles, const char *msg)
100 {
101 struct file_transfer *xfer = ft_new(FILE_TRANSFER_TYPE_RECEIVE, gc,
102 who);
103 char *buf;
104 static const char *sizestr[4] = { "bytes", "KB", "MB", "GB" };
105 float sizemag = (float)totsize;
106 int szindex = 0;
107
108 xfer->initname = g_strdup(initname);
109 xfer->totsize = totsize;
110 xfer->totfiles = totfiles;
111 xfer->filesdone = 0;
112
113 /* Ask the user whether he or she accepts the transfer. */
114 while ((szindex < 4) && (sizemag > 1024)) {
115 sizemag /= 1024;
116 szindex++;
117 }
118
119 if (xfer->totfiles == 1)
120 buf = g_strdup_printf(_("%s requests that %s accept a file: %s (%.3g %s)"),
121 who, xfer->gc->username, initname,
122 sizemag, sizestr[szindex]);
123 else
124 buf = g_strdup_printf(_("%s requests that %s accept %d files: %s (%.3g %s)"),
125 who, xfer->gc->username, xfer->totfiles,
126 initname, sizemag, sizestr[szindex]);
127
128 if (msg) {
129 char *newmsg = g_strconcat(buf, ": ", msg, NULL);
130 g_free(buf);
131 buf = newmsg;
132 }
133
134 do_ask_dialog(buf, xfer, ft_choose_file, ft_cancel);
135 g_free(buf);
136
137 return xfer;
138 }
139
140 struct file_transfer *transfer_out_add(struct gaim_connection *gc,
141 const char *who)
142 {
143 struct file_transfer *xfer = ft_new(FILE_TRANSFER_TYPE_SEND, gc,
144 who);
145
146 ft_choose_file(NULL, xfer);
147
148 return xfer;
149 }
150
151 /* We canceled the transfer, either by declining the initial
152 * confirmation dialog or canceling the file dialog.
153 */
154 static void ft_cancel(gpointer w, struct file_transfer *xfer)
155 {
156 /* Make sure we weren't aborted while waiting for
157 * confirmation from the user.
158 */
159 if (xfer->state == FILE_TRANSFER_STATE_INTERRUPTED) {
160 xfer->state = FILE_TRANSFER_STATE_CLEANUP;
161 transfer_abort(xfer, NULL);
162 return;
163 }
164
165 xfer->state = FILE_TRANSFER_STATE_CANCELED;
166 if (xfer->w) {
167 gtk_widget_destroy(xfer->w);
168 xfer->w = NULL;
169 }
170
171 if (xfer->gc->prpl->file_transfer_cancel)
172 xfer->gc->prpl->file_transfer_cancel(xfer->gc, xfer);
173
174 ft_delete(xfer);
175 }
176
177 /* This is called when the other user aborts the transfer,
178 * possibly in the middle of a transfer.
179 */
180 int transfer_abort(struct file_transfer *xfer, const char *why)
181 {
182 if (xfer->state == FILE_TRANSFER_STATE_INTERRUPTED) {
183 /* If for some reason we have already been
184 * here and are waiting on some event before
185 * cleaning up, but we get another abort request,
186 * we don't need to do anything else.
187 */
188 return 1;
189 }
190 else if (xfer->state == FILE_TRANSFER_STATE_ASK) {
191 /* Kludge: since there is no way to kill a
192 * do_ask_dialog() window, we just note the
193 * status here and clean up after the user
194 * makes a selection.
195 */
196 xfer->state = FILE_TRANSFER_STATE_INTERRUPTED;
197 return 1;
198 }
199 else if (xfer->state == FILE_TRANSFER_STATE_TRANSFERRING) {
200 if (xfer->watcher) {
201 gaim_input_remove(xfer->watcher);
202 xfer->watcher = 0;
203 }
204 if (xfer->file) {
205 fclose(xfer->file);
206 xfer->file = NULL;
207 }
208 /* XXX theoretically, there is a race condition here,
209 * because we could be inside ft_callback() when we
210 * free xfer below, with undefined results. Since
211 * we use non-blocking IO, this doesn't seem to be
212 * a problem, but it still makes me nervous--I don't
213 * know how to fix it other than using locks, though.
214 * -- wtm
215 */
216 }
217 else if (xfer->state == FILE_TRANSFER_STATE_CHOOSEFILE) {
218 /* It's safe to clean up now. Just make sure we
219 * destroy the dialog window first.
220 */
221 if (xfer->w) {
222 gtk_signal_disconnect_by_func(GTK_OBJECT(xfer->w),
223 GTK_SIGNAL_FUNC(ft_cancel), xfer);
224 gtk_widget_destroy(xfer->w);
225 xfer->w = NULL;
226 }
227 }
228
229 /* Let the user know that we were aborted, unless we already
230 * finished or the user aborted first.
231 */
232 /* if ((xfer->state != FILE_TRANSFER_STATE_DONE) &&
233 (xfer->state != FILE_TRANSFER_STATE_CANCELED)) { */
234 if (why) {
235 char *msg;
236
237 if (xfer->type == FILE_TRANSFER_TYPE_SEND)
238 msg = g_strdup_printf(_("File transfer to %s aborted."), xfer->who);
239 else
240 msg = g_strdup_printf(_("File transfer from %s aborted."), xfer->who);
241 do_error_dialog(msg, why, GAIM_ERROR);
242 g_free(msg);
243 }
244
245 ft_delete(xfer);
246
247 return 0;
248 }
249
250
251 static void ft_delete(struct file_transfer *xfer)
252 {
253 if (xfer->names)
254 g_strfreev(xfer->names);
255 if (xfer->initname)
256 g_free(xfer->initname);
257 if (xfer->who)
258 g_free(xfer->who);
259 if (xfer->sizes)
260 g_free(xfer->sizes);
261 g_free(xfer);
262 }
263
264 static void ft_choose_ok(gpointer a, struct file_transfer *xfer) {
265 gboolean exists, is_dir;
266 struct stat st;
267 const char *err = NULL;
268
269 xfer->names = gtk_file_selection_get_selections(GTK_FILE_SELECTION(xfer->w));
270 exists = !stat(*xfer->names, &st);
271 is_dir = (exists) ? S_ISDIR(st.st_mode) : 0;
272
273 if (exists) {
274 if (xfer->type == FILE_TRANSFER_TYPE_RECEIVE)
275 /* XXX overwrite/append/cancel prompt */
276 err = _("That file already exists; please choose another name.");
277 else { /* (xfer->type == FILE_TRANSFER_TYPE_SEND) */
278 char **cur;
279 /* First find the total number of files,
280 * so we know how much space to allocate.
281 */
282 xfer->totfiles = 0;
283 for (cur = xfer->names; *cur; cur++) {
284 xfer->totfiles++;
285 }
286
287 /* Now get sizes for each file. */
288 xfer->totsize = st.st_size;
289 xfer->sizes = g_malloc(xfer->totfiles
290 * sizeof(*xfer->sizes));
291 xfer->sizes[0] = st.st_size;
292 for (cur = xfer->names + 1; *cur; cur++) {
293 exists = !stat(*cur, &st);
294 if (!exists) {
295 err = _("File not found.");
296 break;
297 }
298 xfer->sizes[cur - xfer->names] =
299 st.st_size;
300 xfer->totsize += st.st_size;
301 }
302 }
303 }
304 else { /* doesn't exist */
305 if (xfer->type == FILE_TRANSFER_TYPE_SEND)
306 err = _("File not found.");
307 else if (xfer->totfiles > 1) {
308 if (!xfer->names[0] || xfer->names[1]) {
309 err = _("You may only choose one new directory.");
310 }
311 else {
312 if (ft_mkdir(*xfer->names))
313 err = _("Unable to create directory.");
314 else
315 xfer->dir = g_strconcat(xfer->names[0],
316 "/", NULL);
317 }
318 }
319 }
320
321 if (err)
322 do_error_dialog(err, NULL, GAIM_ERROR);
323 else {
324 /* File name looks valid */
325 gtk_widget_destroy(xfer->w);
326 xfer->w = NULL;
327
328 if (xfer->type == FILE_TRANSFER_TYPE_SEND) {
329 char *desc;
330 if (xfer->totfiles == 1)
331 desc = *xfer->names;
332 else
333 /* XXX what else? */
334 desc = "*";
335 /* desc = g_path_get_basename(g_path_get_dirname(*xfer->names)); */
336 xfer->gc->prpl->file_transfer_out(xfer->gc, xfer,
337 desc, xfer->totfiles,
338 xfer->totsize);
339 }
340 else
341 xfer->gc->prpl->file_transfer_in(xfer->gc, xfer,
342 0); /* XXX */
343 }
344 }
345
346 /* Called on outgoing transfers to get information about the
347 * current file.
348 */
349 int transfer_get_file_info(struct file_transfer *xfer, int *size,
350 char **name)
351 {
352 *size = xfer->sizes[xfer->filesdone];
353 *name = xfer->names[xfer->filesdone];
354 return 0;
355 }
356
357 static int ft_choose_file(gpointer a, struct file_transfer *xfer)
358 {
359 char *curdir = g_get_current_dir(); /* should be freed */
360 char *initstr;
361
362 /* If the connection is interrupted while we are waiting
363 * for do_ask_dialog(), then we can't clean up until we
364 * get here, after the user makes a selection.
365 */
366 if (xfer->state == FILE_TRANSFER_STATE_INTERRUPTED) {
367 xfer->state = FILE_TRANSFER_STATE_CLEANUP;
368 transfer_abort(xfer, NULL);
369 return 1;
370 }
371
372 xfer->state = FILE_TRANSFER_STATE_CHOOSEFILE;
373 if (xfer->type == FILE_TRANSFER_TYPE_RECEIVE)
374 xfer->w = gtk_file_selection_new(_("Gaim - Save As..."));
375 else /* (xfer->type == FILE_TRANSFER_TYPE_SEND) */ {
376 xfer->w = gtk_file_selection_new(_("Gaim - Open..."));
377 gtk_file_selection_set_select_multiple(GTK_FILE_SELECTION(xfer->w),
378 1);
379 }
380
381 if (xfer->initname) {
382 initstr = g_strdup_printf("%s/%s", curdir, xfer->initname);
383 } else
384 initstr = g_strconcat(curdir, "/", NULL);
385 g_free(curdir);
386
387 gtk_file_selection_set_filename(GTK_FILE_SELECTION(xfer->w),
388 initstr);
389 g_free(initstr);
390
391 gtk_signal_connect(GTK_OBJECT(xfer->w), "delete_event",
392 GTK_SIGNAL_FUNC(ft_cancel), xfer);
393 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(xfer->w)->cancel_button),
394 "clicked", GTK_SIGNAL_FUNC(ft_cancel), xfer);
395 gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(xfer->w)->ok_button),
396 "clicked", GTK_SIGNAL_FUNC(ft_choose_ok), xfer);
397 gtk_widget_show(xfer->w);
398
399 return 0;
400 }
401
402 static int ft_open_file(struct file_transfer *xfer, const char *filename,
403 int offset)
404 {
405 char *err = NULL;
406
407 if (xfer->type == FILE_TRANSFER_TYPE_RECEIVE) {
408 xfer->file = fopen(filename,
409 (offset > 0) ? "a" : "w");
410
411 if (!xfer->file)
412 err = g_strdup_printf(_("Could not open %s for writing: %s"),
413 filename, g_strerror(errno));
414
415 }
416 else /* (xfer->type == FILE_TRANSFER_TYPE_SEND) */ {
417 xfer->file = fopen(filename, "r");
418 if (!xfer->file)
419 err = g_strdup_printf(_("Could not open %s for reading: %s"),
420 filename, g_strerror(errno));
421 }
422
423 if (err) {
424 do_error_dialog(err, NULL, GAIM_ERROR);
425 g_free(err);
426 return -1;
427 }
428
429 fseek(xfer->file, offset, SEEK_SET);
430
431 return 0;
432 }
433
434 /* Takes a full file name, and creates any directories above it
435 * that don't exist already.
436 */
437 static int ft_mkdir(const char *name) {
438 int ret = 0;
439 struct stat st;
440 mode_t m = umask(0077);
441 char *dir;
442
443 dir = g_path_get_dirname(name);
444 if (stat(dir, &st))
445 ret = ft_mkdir_help(dir);
446
447 g_free(dir);
448 umask(m);
449 return ret;
450 }
451
452 /* Two functions, one recursive, just to make a directory. Yuck. */
453 static int ft_mkdir_help(char *dir) {
454 int ret;
455
456 ret = mkdir(dir, 0777);
457 if (ret) {
458 char *index = strrchr(dir, G_DIR_SEPARATOR);
459 if (!index)
460 return -1;
461 *index = '\0';
462 ret = ft_mkdir_help(dir);
463 *index = G_DIR_SEPARATOR;
464 if (!ret)
465 ret = mkdir(dir, 0777);
466 }
467
468 return ret;
469 }
470
471 int transfer_in_do(struct file_transfer *xfer, int fd,
472 const char *filename, int size)
473 {
474 char *fullname;
475
476 xfer->state = FILE_TRANSFER_STATE_TRANSFERRING;
477 xfer->fd = fd;
478 xfer->bytesleft = size;
479
480 /* XXX implement resuming incoming transfers */
481 #if 0
482 if (xfer->sizes)
483 xfer->bytesleft -= xfer->sizes[0];
484 #endif
485
486 /* Security check */
487 if (g_strrstr(filename, "..")) {
488 xfer->state = FILE_TRANSFER_STATE_CLEANUP;
489 transfer_abort(xfer, _("Invalid incoming filename component"));
490 return -1;
491 }
492
493 if (xfer->totfiles > 1)
494 fullname = g_strconcat(xfer->dir, filename, NULL);
495 else
496 /* Careful: filename is the name on the *other*
497 * end; don't use it here. */
498 fullname = g_strdup(xfer->names[xfer->filesdone]);
499
500
501 if (ft_mkdir(fullname)) {
502 xfer->state = FILE_TRANSFER_STATE_CLEANUP;
503 transfer_abort(xfer, _("Invalid incoming filename"));
504 return -1;
505 }
506
507 if (!ft_open_file(xfer, fullname, 0)) {
508 /* Special case: if we are receiving an empty file,
509 * we would never enter the callback. Just avoid the
510 * callback altogether.
511 */
512 if (xfer->bytesleft == 0)
513 ft_nextfile(xfer);
514 else
515 xfer->watcher = gaim_input_add(fd,
516 GAIM_INPUT_READ,
517 ft_callback, xfer);
518 } else {
519 /* Error opening file */
520 xfer->state = FILE_TRANSFER_STATE_CLEANUP;
521 transfer_abort(xfer, NULL);
522 g_free(fullname);
523 return -1;
524 }
525
526 g_free(fullname);
527 return 0;
528 }
529
530 int transfer_out_do(struct file_transfer *xfer, int fd, int offset) {
531 xfer->state = FILE_TRANSFER_STATE_TRANSFERRING;
532 xfer->fd = fd;
533 xfer->bytesleft = xfer->sizes[xfer->filesdone] - offset;
534
535 if (!ft_open_file(xfer, xfer->names[xfer->filesdone], offset)) {
536 /* Special case: see transfer_in_do().
537 */
538 if (xfer->bytesleft == 0)
539 ft_nextfile(xfer);
540 else
541 xfer->watcher = gaim_input_add(fd,
542 GAIM_INPUT_WRITE, ft_callback,
543 xfer);
544 }
545 else {
546 /* Error opening file */
547 xfer->state = FILE_TRANSFER_STATE_CLEANUP;
548 transfer_abort(xfer, NULL);
549 return -1;
550 }
551
552 return 0;
553 }
554
555 static void ft_callback(gpointer data, gint source,
556 GaimInputCondition condition)
557 {
558 struct file_transfer *xfer = (struct file_transfer *)data;
559 int rt, i;
560 char buf[FT_BUFFER_SIZE];
561
562 if (condition & GAIM_INPUT_READ) {
563 rt = read(xfer->fd, buf, MIN(xfer->bytesleft, FT_BUFFER_SIZE));
564 /* XXX What if the transfer is interrupted while we
565 * are inside read()? How can this be handled safely?
566 * -- wtm
567 */
568 if (rt > 0) {
569 xfer->bytesleft -= rt;
570 for (i = 0; i < rt; i++) {
571 fprintf(xfer->file, "%c", buf[i]);
572 }
573 }
574
575 }
576 else /* (condition & GAIM_INPUT_WRITE) */ {
577 int remain = MIN(xfer->bytesleft, FT_BUFFER_SIZE);
578
579 for (i = 0; i < remain; i++)
580 fscanf(xfer->file, "%c", &buf[i]);
581
582 rt = write(xfer->fd, buf, remain);
583 if (rt > 0)
584 xfer->bytesleft -= rt;
585 }
586
587 if (rt < 0)
588 return;
589
590 xfer->bytessent += rt;
591
592 if (xfer->gc->prpl->file_transfer_data_chunk)
593 xfer->gc->prpl->file_transfer_data_chunk(xfer->gc, xfer, buf, rt);
594
595 if (rt > 0 && xfer->bytesleft == 0) {
596 /* We are done with this file! */
597 gaim_input_remove(xfer->watcher);
598 xfer->watcher = 0;
599 fclose(xfer->file);
600 xfer->file = 0;
601 ft_nextfile(xfer);
602 }
603 }
604
605 static void ft_nextfile(struct file_transfer *xfer)
606 {
607 debug_printf("file transfer %d of %d done\n",
608 xfer->filesdone + 1, xfer->totfiles);
609
610 if (++xfer->filesdone == xfer->totfiles) {
611 char *msg;
612 char *msg2;
613
614 xfer->gc->prpl->file_transfer_done(xfer->gc, xfer);
615
616 if (xfer->type == FILE_TRANSFER_TYPE_RECEIVE)
617 msg = g_strdup_printf(_("File transfer from %s to %s completed successfully."),
618 xfer->who, xfer->gc->username);
619 else
620 msg = g_strdup_printf(_("File transfer from %s to %s completed successfully."),
621 xfer->gc->username, xfer->who);
622 xfer->state = FILE_TRANSFER_STATE_DONE;
623
624 if (xfer->totfiles > 1)
625 msg2 = g_strdup_printf(_("%d files transferred."),
626 xfer->totfiles);
627 else
628 msg2 = NULL;
629
630 do_error_dialog(msg, msg2, GAIM_INFO);
631 g_free(msg);
632 if (msg2)
633 g_free(msg2);
634
635 ft_delete(xfer);
636 }
637 else {
638 xfer->gc->prpl->file_transfer_nextfile(xfer->gc, xfer);
639 }
640 }
641