annotate src/ft.c @ 3718:fd3880c03f0d

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