4 * Copyright (c) 2000, 2001, Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * A copy of the GNU General Public License can be found at
14 * Written by DJ Delorie <dj@cygnus.com>
18 /* The purpose of this file is to act as a pretty interface to
19 netio.cc. We add a progress dialog and some convenience functions
20 (like collect to string or file */
23 static const char *cvsid
=
40 #include "io_stream.h"
41 #include "io_stream_memory.h"
47 extern ThreeBarProgressPage Progress
;
49 static int max_bytes
= 0;
50 static int is_local_install
= 0;
52 int total_download_bytes
= 0;
53 int total_download_bytes_sofar
= 0;
55 static DWORD start_tics
;
58 init_dialog (char const *url
, int length
, HWND owner
)
65 for (cp
= url
; *cp
; cp
++)
66 if (*cp
== '/' || *cp
== '\\' || *cp
== ':')
69 Progress
.SetText1("Downloading...");
70 Progress
.SetText2(sl
);
71 Progress
.SetText3("Connecting...");
73 start_tics
= GetTickCount ();
84 static unsigned int last_tics
= 0;
85 DWORD tics
= GetTickCount ();
86 if (tics
== start_tics
) // to prevent division by zero
88 if (tics
< last_tics
+ 200) // to prevent flickering updates
92 kbps
= ((double)bytes
) / (double)(tics
- start_tics
);
95 int perc
= (int)(100.0 * ((double)bytes
) / (double)max_bytes
);
96 Progress
.SetBar1(bytes
, max_bytes
);
97 sprintf (buf
, "%d %% (%dk/%dk) %03.1f kb/s\n",
98 perc
, bytes
/ 1000, max_bytes
/ 1000, kbps
);
99 if (total_download_bytes
> 0)
101 Progress
.SetBar2(total_download_bytes_sofar
+ bytes
, total_download_bytes
);
105 sprintf (buf
, "%d %2.1f kb/s\n", bytes
, kbps
);
107 Progress
.SetText3(buf
);
111 get_url_to_membuf (char const *_url
, HWND owner
)
113 log (LOG_BABBLE
, "get_url_to_membuf %s", _url
);
114 is_local_install
= (source
== IDC_SOURCE_CWD
);
115 init_dialog (_url
, 0, owner
);
116 NetIO
*n
= NetIO::open (_url
);
120 log (LOG_BABBLE
, "get_url_to_membuf failed!");
125 max_bytes
= n
->file_size
;
127 io_stream_memory
*membuf
= new io_stream_memory ();
135 rlen
= n
->read (buf
, 2048);
138 wlen
= membuf
->write (buf
, rlen
);
140 /* FIXME: Show an error message */
143 progress (total_bytes
);
149 if (membuf
->seek (0, IO_SEEK_SET
))
155 log (LOG_BABBLE
, "get_url_to_membuf(): seek (0) failed for membuf!");
165 get_url_to_string (char const *_url
, HWND owner
)
167 io_stream
*stream
= get_url_to_membuf (_url
, owner
);
170 size_t bytes
= stream
->get_size ();
173 /* zero length, or error retrieving length */
175 log (LOG_BABBLE
, "get_url_to_string(): couldn't retrieve buffer size, or zero length buffer");
178 char *rv
= new char [bytes
+ 1];
182 log (LOG_BABBLE
, "get_url_to_string(): new failed for rv!");
185 /* membufs are quite safe */
186 stream
->read (rv
, bytes
);
193 get_url_to_file (char *_url
, char *_filename
, int expected_length
,
194 HWND owner
, BOOL allow_ftp_auth
)
196 log (LOG_BABBLE
, "get_url_to_file %s %s", _url
, _filename
);
197 if (total_download_bytes
> 0)
199 int df
= diskfull (get_root_dir ());
200 Progress
.SetBar3(df
);
202 init_dialog (_url
, expected_length
, owner
);
204 remove (_filename
); /* but ignore errors */
206 NetIO
*n
= NetIO::open (_url
, allow_ftp_auth
);
210 log (LOG_BABBLE
, "get_url_to_file failed!");
214 FILE *f
= fopen (_filename
, "wb");
217 const char *err
= strerror (errno
);
219 err
= "(unknown error)";
220 fatal (owner
, IDS_ERR_OPEN_WRITE
, _filename
, err
);
224 max_bytes
= n
->file_size
;
232 count
= n
->read (buf
, sizeof (buf
));
235 fwrite (buf
, 1, count
, f
);
236 total_bytes
+= count
;
237 progress (total_bytes
);
240 total_download_bytes_sofar
+= total_bytes
;
246 if (total_download_bytes
> 0)
248 int df
= diskfull (get_root_dir ());
249 Progress
.SetBar3(df
);