2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 static RecvUIFunc recv_ui_func;
38 static gpointer recv_ui_func_data;
40 gint recv_write_to_file(SockInfo *sock, const gchar *filename)
45 g_return_val_if_fail(filename != NULL, -1);
47 if ((fp = fopen(filename, "wb")) == NULL) {
48 FILE_OP_ERROR(filename, "fopen");
49 recv_write(sock, NULL);
53 if (change_file_mode_rw(fp, filename) < 0)
54 FILE_OP_ERROR(filename, "chmod");
56 if ((ret = recv_write(sock, fp)) < 0) {
62 if (fclose(fp) == EOF) {
63 FILE_OP_ERROR(filename, "fclose");
71 gint recv_bytes_write_to_file(SockInfo *sock, glong size, const gchar *filename)
76 g_return_val_if_fail(filename != NULL, -1);
78 if ((fp = fopen(filename, "wb")) == NULL) {
79 FILE_OP_ERROR(filename, "fopen");
80 recv_write(sock, NULL);
84 if (change_file_mode_rw(fp, filename) < 0)
85 FILE_OP_ERROR(filename, "chmod");
87 if ((ret = recv_bytes_write(sock, size, fp)) < 0) {
93 if (fclose(fp) == EOF) {
94 FILE_OP_ERROR(filename, "fclose");
102 gint recv_write(SockInfo *sock, FILE *fp)
108 struct timeval tv_prev, tv_cur;
110 gettimeofday(&tv_prev, NULL);
113 if (sock_gets(sock, buf, sizeof(buf)) < 0) {
114 g_warning(_("error occurred while retrieving data.\n"));
119 if (len > 1 && buf[0] == '.' && buf[1] == '\r') {
121 recv_ui_func(sock, count, bytes,
129 gettimeofday(&tv_cur, NULL);
130 /* if elapsed time from previous update is greater
131 than 50msec, update UI */
132 if (tv_cur.tv_sec - tv_prev.tv_sec > 0 ||
133 tv_cur.tv_usec - tv_prev.tv_usec > UI_REFRESH_INTERVAL) {
135 ret = recv_ui_func(sock, count, bytes,
137 if (ret == FALSE) return -1;
138 gettimeofday(&tv_prev, NULL);
142 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
148 if (buf[0] == '.' && buf[1] == '.')
149 memmove(buf, buf + 1, len--);
151 if (!strncmp(buf, ">From ", 6))
152 memmove(buf, buf + 1, len--);
154 if (fp && fputs(buf, fp) == EOF) {
156 g_warning(_("Can't write to file.\n"));
166 gint recv_bytes_write(SockInfo *sock, glong size, FILE *fp)
172 buf = g_malloc(size);
177 read_count = sock_read(sock, buf + count, size - count);
178 if (read_count < 0) {
183 } while (count < size);
185 /* +------------------+----------------+--------------------------+ *
186 * ^buf ^prev ^cur buf+size-1^ */
189 while ((cur = memchr(prev, '\r', size - (prev - buf))) != NULL) {
190 if (cur == buf + size - 1) break;
192 if (fwrite(prev, sizeof(gchar), cur - prev, fp) == EOF ||
193 fwrite("\n", sizeof(gchar), 1, fp) == EOF) {
195 g_warning(_("Can't write to file.\n"));
200 if (*(cur + 1) == '\n')
205 if (prev - buf >= size) break;
208 if (prev - buf < size && fwrite(buf, sizeof(gchar),
209 size - (prev - buf), fp) == EOF) {
211 g_warning(_("Can't write to file.\n"));
220 void recv_set_ui_func(RecvUIFunc func, gpointer data)
223 recv_ui_func_data = data;