2007-08-27 [colin] 2.10.0cvs179
[claws.git] / src / recv.c
blob5f7dc3acad08075e248809de728fa12b0df96a89
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
32 #ifdef G_OS_WIN32
33 #include "w32lib.h"
34 #else
35 #include <sys/time.h>
36 #endif
38 #include "recv.h"
39 #include "socket.h"
40 #include "utils.h"
42 static RecvUIFunc recv_ui_func;
43 static gpointer recv_ui_func_data;
45 gint recv_write_to_file(SockInfo *sock, const gchar *filename)
47 FILE *fp;
48 gint ret;
50 g_return_val_if_fail(filename != NULL, -1);
52 if ((fp = g_fopen(filename, "wb")) == NULL) {
53 FILE_OP_ERROR(filename, "fopen");
54 recv_write(sock, NULL);
55 return -1;
58 if (change_file_mode_rw(fp, filename) < 0)
59 FILE_OP_ERROR(filename, "chmod");
61 if ((ret = recv_write(sock, fp)) < 0) {
62 fclose(fp);
63 g_unlink(filename);
64 return ret;
67 if (fclose(fp) == EOF) {
68 FILE_OP_ERROR(filename, "fclose");
69 g_unlink(filename);
70 return -1;
73 return 0;
76 gint recv_bytes_write_to_file(SockInfo *sock, glong size, const gchar *filename)
78 FILE *fp;
79 gint ret;
81 g_return_val_if_fail(filename != NULL, -1);
83 if ((fp = g_fopen(filename, "wb")) == NULL) {
84 FILE_OP_ERROR(filename, "fopen");
85 recv_write(sock, NULL);
86 return -1;
89 if (change_file_mode_rw(fp, filename) < 0)
90 FILE_OP_ERROR(filename, "chmod");
92 if ((ret = recv_bytes_write(sock, size, fp)) < 0) {
93 fclose(fp);
94 g_unlink(filename);
95 return ret;
98 if (fclose(fp) == EOF) {
99 FILE_OP_ERROR(filename, "fclose");
100 g_unlink(filename);
101 return -1;
104 return 0;
107 gint recv_write(SockInfo *sock, FILE *fp)
109 gchar buf[BUFFSIZE];
110 gint len;
111 gint count = 0;
112 gint bytes = 0;
113 struct timeval tv_prev, tv_cur;
115 gettimeofday(&tv_prev, NULL);
117 for (;;) {
118 if (sock_gets(sock, buf, sizeof(buf)) < 0) {
119 g_warning("error occurred while retrieving data.\n");
120 return -2;
123 len = strlen(buf);
124 if (len > 1 && buf[0] == '.' && buf[1] == '\r') {
125 if (recv_ui_func)
126 recv_ui_func(sock, count, bytes,
127 recv_ui_func_data);
128 break;
130 count++;
131 bytes += len;
133 if (recv_ui_func) {
134 gettimeofday(&tv_cur, NULL);
135 /* if elapsed time from previous update is greater
136 than 50msec, update UI */
137 if (tv_cur.tv_sec - tv_prev.tv_sec > 0 ||
138 tv_cur.tv_usec - tv_prev.tv_usec > UI_REFRESH_INTERVAL) {
139 gboolean ret;
140 ret = recv_ui_func(sock, count, bytes,
141 recv_ui_func_data);
142 if (ret == FALSE) return -1;
143 gettimeofday(&tv_prev, NULL);
147 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
148 buf[len - 2] = '\n';
149 buf[len - 1] = '\0';
150 len--;
153 if (buf[0] == '.' && buf[1] == '.')
154 memmove(buf, buf + 1, len--);
156 if (!strncmp(buf, ">From ", 6))
157 memmove(buf, buf + 1, len--);
159 if (fp && fputs(buf, fp) == EOF) {
160 perror("fputs");
161 g_warning("Can't write to file.\n");
162 fp = NULL;
166 if (!fp) return -1;
168 return 0;
171 gint recv_bytes_write(SockInfo *sock, glong size, FILE *fp)
173 gchar *buf;
174 glong count = 0;
175 gchar *prev, *cur;
177 if (size == 0)
178 return 0;
180 buf = g_malloc(size);
182 do {
183 gint read_count;
185 read_count = sock_read(sock, buf + count, size - count);
186 if (read_count < 0) {
187 g_free(buf);
188 return -2;
190 count += read_count;
191 } while (count < size);
193 /* +------------------+----------------+--------------------------+ *
194 * ^buf ^prev ^cur buf+size-1^ */
196 prev = buf;
197 while ((cur = memchr(prev, '\r', size - (prev - buf))) != NULL) {
198 if (cur == buf + size - 1) break;
200 if (fwrite(prev, sizeof(gchar), cur - prev, fp) == EOF ||
201 fwrite("\n", sizeof(gchar), 1, fp) == EOF) {
202 perror("fwrite");
203 g_warning("Can't write to file.\n");
204 g_free(buf);
205 return -1;
208 if (*(cur + 1) == '\n')
209 prev = cur + 2;
210 else
211 prev = cur + 1;
213 if (prev - buf >= size) break;
216 if (prev - buf < size && fwrite(buf, sizeof(gchar),
217 size - (prev - buf), fp) == EOF) {
218 perror("fwrite");
219 g_warning("Can't write to file.\n");
220 g_free(buf);
221 return -1;
224 g_free(buf);
225 return 0;
228 void recv_set_ui_func(RecvUIFunc func, gpointer data)
230 recv_ui_func = func;
231 recv_ui_func_data = data;