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"
50 extern ThreeBarProgressPage Progress
;
52 static int max_bytes
= 0;
53 static int is_local_install
= 0;
55 int total_download_bytes
= 0;
56 int total_download_bytes_sofar
= 0;
58 static DWORD start_tics
;
61 init_dialog (String
const &url
, int length
, HWND owner
)
66 char const *temp
= url
.cstr();
67 char const *sl
= temp
;
69 for (cp
= sl
; *cp
; cp
++)
70 if (*cp
== '/' || *cp
== '\\' || *cp
== ':')
73 Progress
.SetText1("Downloading...");
74 Progress
.SetText2(sl
);
75 Progress
.SetText3("Connecting...");
77 start_tics
= GetTickCount ();
89 static unsigned int last_tics
= 0;
90 DWORD tics
= GetTickCount ();
91 if (tics
== start_tics
) // to prevent division by zero
93 if (tics
< last_tics
+ 200) // to prevent flickering updates
97 kbps
= ((double)bytes
) / (double)(tics
- start_tics
);
100 int perc
= (int)(100.0 * ((double)bytes
) / (double)max_bytes
);
101 Progress
.SetBar1(bytes
, max_bytes
);
102 sprintf (buf
, "%d %% (%dk/%dk) %03.1f kb/s\n",
103 perc
, bytes
/ 1000, max_bytes
/ 1000, kbps
);
104 if (total_download_bytes
> 0)
106 Progress
.SetBar2(total_download_bytes_sofar
+ bytes
, total_download_bytes
);
110 sprintf (buf
, "%d %2.1f kb/s\n", bytes
, kbps
);
112 Progress
.SetText3(buf
);
116 get_url_to_membuf (String
const &_url
, HWND owner
)
118 log (LOG_BABBLE
, String ("get_url_to_membuf ") + _url
);
119 is_local_install
= (source
== IDC_SOURCE_CWD
);
120 init_dialog (_url
, 0, owner
);
121 NetIO
*n
= NetIO::open (_url
.cstr_oneuse());
125 log (LOG_BABBLE
, "get_url_to_membuf failed!");
130 max_bytes
= n
->file_size
;
132 io_stream_memory
*membuf
= new io_stream_memory ();
140 rlen
= n
->read (buf
, 2048);
143 wlen
= membuf
->write (buf
, rlen
);
145 /* FIXME: Show an error message */
148 progress (total_bytes
);
154 if (membuf
->seek (0, IO_SEEK_SET
))
160 log (LOG_BABBLE
, "get_url_to_membuf(): seek (0) failed for membuf!");
169 // predicate: url has no '\0''s in it.
171 get_url_to_string (String
const &_url
, HWND owner
)
173 io_stream
*stream
= get_url_to_membuf (_url
, owner
);
176 size_t bytes
= stream
->get_size ();
179 /* zero length, or error retrieving length */
181 log (LOG_BABBLE
, "get_url_to_string(): couldn't retrieve buffer size, or zero length buffer");
184 char temp
[bytes
+ 1];
185 /* membufs are quite safe */
186 stream
->read (temp
, bytes
);
193 get_url_to_file (String
const &_url
, String
const &_filename
, int expected_length
,
194 HWND owner
, BOOL allow_ftp_auth
)
196 log (LOG_BABBLE
, String ("get_url_to_file ") + _url
+ " " + _filename
);
197 if (total_download_bytes
> 0)
199 int df
= diskfull (get_root_dir ().cstr_oneuse());
200 Progress
.SetBar3(df
);
202 init_dialog (_url
, expected_length
, owner
);
204 remove (_filename
.cstr_oneuse()); /* but ignore errors */
206 NetIO
*n
= NetIO::open (_url
.cstr_oneuse(), allow_ftp_auth
);
210 log (LOG_BABBLE
, "get_url_to_file failed!");
214 FILE *f
= fopen (_filename
.cstr_oneuse(), "wb");
217 const char *err
= strerror (errno
);
219 err
= "(unknown error)";
220 fatal (owner
, IDS_ERR_OPEN_WRITE
, _filename
.cstr_oneuse(), 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 ().cstr_oneuse());
249 Progress
.SetBar3(df
);