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/>.
27 #include <glib/gi18n.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
)
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
);
58 if (change_file_mode_rw(fp
, filename
) < 0)
59 FILE_OP_ERROR(filename
, "chmod");
61 if ((ret
= recv_write(sock
, fp
)) < 0) {
67 if (fclose(fp
) == EOF
) {
68 FILE_OP_ERROR(filename
, "fclose");
76 gint
recv_bytes_write_to_file(SockInfo
*sock
, glong size
, const gchar
*filename
)
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
);
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) {
98 if (fclose(fp
) == EOF
) {
99 FILE_OP_ERROR(filename
, "fclose");
107 gint
recv_write(SockInfo
*sock
, FILE *fp
)
113 struct timeval tv_prev
, tv_cur
;
115 gettimeofday(&tv_prev
, NULL
);
118 if (sock_gets(sock
, buf
, sizeof(buf
)) < 0) {
119 g_warning("error occurred while retrieving data.\n");
124 if (len
> 1 && buf
[0] == '.' && buf
[1] == '\r') {
126 recv_ui_func(sock
, count
, bytes
,
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
) {
140 ret
= recv_ui_func(sock
, count
, bytes
,
142 if (ret
== FALSE
) return -1;
143 gettimeofday(&tv_prev
, NULL
);
147 if (len
> 1 && buf
[len
- 1] == '\n' && buf
[len
- 2] == '\r') {
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
) {
161 g_warning("Can't write to file.\n");
171 gint
recv_bytes_write(SockInfo
*sock
, glong size
, FILE *fp
)
180 buf
= g_malloc(size
);
185 read_count
= sock_read(sock
, buf
+ count
, size
- count
);
186 if (read_count
< 0) {
191 } while (count
< size
);
193 /* +------------------+----------------+--------------------------+ *
194 * ^buf ^prev ^cur buf+size-1^ */
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
) {
203 g_warning("Can't write to file.\n");
208 if (*(cur
+ 1) == '\n')
213 if (prev
- buf
>= size
) break;
216 if (prev
- buf
< size
&& fwrite(buf
, sizeof(gchar
),
217 size
- (prev
- buf
), fp
) == EOF
) {
219 g_warning("Can't write to file.\n");
228 void recv_set_ui_func(RecvUIFunc func
, gpointer data
)
231 recv_ui_func_data
= data
;