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