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)
44 g_return_val_if_fail(filename != NULL, -1);
46 if ((fp = fopen(filename, "wb")) == NULL) {
47 FILE_OP_ERROR(filename, "fopen");
48 recv_write(sock, NULL);
52 if (change_file_mode_rw(fp, filename) < 0)
53 FILE_OP_ERROR(filename, "chmod");
55 if (recv_write(sock, fp) < 0) {
61 if (fclose(fp) == EOF) {
62 FILE_OP_ERROR(filename, "fclose");
70 gint recv_bytes_write_to_file(SockInfo *sock, glong size, const gchar *filename)
74 g_return_val_if_fail(filename != NULL, -1);
76 if ((fp = fopen(filename, "wb")) == NULL) {
77 FILE_OP_ERROR(filename, "fopen");
78 recv_write(sock, NULL);
82 if (change_file_mode_rw(fp, filename) < 0)
83 FILE_OP_ERROR(filename, "chmod");
85 if (recv_bytes_write(sock, size, fp) < 0) {
91 if (fclose(fp) == EOF) {
92 FILE_OP_ERROR(filename, "fclose");
100 gint recv_write(SockInfo *sock, FILE *fp)
106 struct timeval tv_prev, tv_cur;
108 gettimeofday(&tv_prev, NULL);
111 if (sock_gets(sock, buf, sizeof(buf)) < 0) {
112 g_warning(_("error occurred while retrieving data.\n"));
117 if (len > 1 && buf[0] == '.' && buf[1] == '\r') {
119 recv_ui_func(sock, count, bytes,
127 gettimeofday(&tv_cur, NULL);
128 /* if elapsed time from previous update is greater
129 than 50msec, update UI */
130 if (tv_cur.tv_sec - tv_prev.tv_sec > 0 ||
131 tv_cur.tv_usec - tv_prev.tv_usec > UI_REFRESH_INTERVAL) {
133 ret = recv_ui_func(sock, count, bytes,
135 if (ret == FALSE) return -1;
136 gettimeofday(&tv_prev, NULL);
140 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
146 if (buf[0] == '.' && buf[1] == '.')
147 memmove(buf, buf + 1, len--);
149 if (!strncmp(buf, ">From ", 6))
150 memmove(buf, buf + 1, len--);
152 if (fp && fputs(buf, fp) == EOF) {
154 g_warning(_("Can't write to file.\n"));
164 gint recv_bytes_write(SockInfo *sock, glong size, FILE *fp)
170 buf = g_malloc(size);
175 read_count = sock_read(sock, buf + count, size - count);
176 if (read_count < 0) {
181 } while (count < size);
183 /* +------------------+----------------+--------------------------+ *
184 * ^buf ^prev ^cur buf+size-1^ */
187 while ((cur = memchr(prev, '\r', size - (prev - buf))) != NULL) {
188 if (cur == buf + size - 1) break;
190 if (fwrite(prev, sizeof(gchar), cur - prev, fp) == EOF ||
191 fwrite("\n", sizeof(gchar), 1, fp) == EOF) {
193 g_warning(_("Can't write to file.\n"));
198 if (*(cur + 1) == '\n')
203 if (prev - buf >= size) break;
206 if (prev - buf < size && fwrite(buf, sizeof(gchar),
207 size - (prev - buf), fp) == EOF) {
209 g_warning(_("Can't write to file.\n"));
218 void recv_set_ui_func(RecvUIFunc func, gpointer data)
221 recv_ui_func_data = data;