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
=
39 #include "io_stream.h"
40 #include "io_stream_memory.h"
49 #include "Exception.h"
51 #include "LogSingleton.h"
53 extern ThreeBarProgressPage Progress
;
55 static int max_bytes
= 0;
56 static int is_local_install
= 0;
58 int total_download_bytes
= 0;
59 int total_download_bytes_sofar
= 0;
61 static DWORD start_tics
;
64 init_dialog (String
const &url
, int length
, HWND owner
)
69 char const *temp
= url
.cstr();
70 char const *sl
= temp
;
72 for (cp
= sl
; *cp
; cp
++)
73 if (*cp
== '/' || *cp
== '\\' || *cp
== ':')
76 Progress
.SetText1("Downloading...");
77 Progress
.SetText2(sl
);
78 Progress
.SetText3("Connecting...");
80 start_tics
= GetTickCount ();
92 static unsigned int last_tics
= 0;
93 DWORD tics
= GetTickCount ();
94 if (tics
== start_tics
) // to prevent division by zero
96 if (tics
< last_tics
+ 200) // to prevent flickering updates
100 kbps
= ((double)bytes
) / (double)(tics
- start_tics
);
103 int perc
= (int)(100.0 * ((double)bytes
) / (double)max_bytes
);
104 Progress
.SetBar1(bytes
, max_bytes
);
105 sprintf (buf
, "%d %% (%dk/%dk) %03.1f kb/s\n",
106 perc
, bytes
/ 1000, max_bytes
/ 1000, kbps
);
107 if (total_download_bytes
> 0)
108 Progress
.SetBar2(total_download_bytes_sofar
+ bytes
,
109 total_download_bytes
);
112 sprintf (buf
, "%d %2.1f kb/s\n", bytes
, kbps
);
114 Progress
.SetText3(buf
);
118 getUrlToStream (String
const &_url
, HWND owner
, io_stream
*output
)
120 log (LOG_BABBLE
) << "getUrlToStream " << _url
<< endLog
;
121 is_local_install
= (source
== IDC_SOURCE_CWD
);
122 init_dialog (_url
, 0, owner
);
123 NetIO
*n
= NetIO::open (_url
.cstr_oneuse());
127 log (LOG_BABBLE
) << "getUrlToStream failed!" << endLog
;
128 throw new Exception ("__LINE__ __FILE__", "Error opening url", APPERR_IO_ERROR
);
132 max_bytes
= n
->file_size
;
140 rlen
= n
->read (buf
, 2048);
143 wlen
= output
->write (buf
, rlen
);
145 /* FIXME: Show an error message */
148 progress (total_bytes
);
155 /* reseeking is up to the recipient if desired */
159 get_url_to_membuf (String
const &_url
, HWND owner
)
161 io_stream_memory
*membuf
= new io_stream_memory ();
164 log (LOG_BABBLE
) << "get_url_to_membuf " << _url
<< endLog
;
165 getUrlToStream (_url
, owner
, membuf
);
167 if (membuf
->seek (0, IO_SEEK_SET
))
171 log (LOG_BABBLE
) << "get_url_to_membuf(): seek (0) failed for membuf!" << endLog
;
178 if (e
->errNo() != APPERR_IO_ERROR
)
180 log (LOG_BABBLE
) << "get_url_to_membuf failed!" << endLog
;
186 // predicate: url has no '\0''s in it.
188 get_url_to_string (String
const &_url
, HWND owner
)
190 io_stream
*stream
= get_url_to_membuf (_url
, owner
);
193 size_t bytes
= stream
->get_size ();
196 /* zero length, or error retrieving length */
198 log (LOG_BABBLE
) << "get_url_to_string(): couldn't retrieve buffer size, or zero length buffer" << endLog
;
201 char temp
[bytes
+ 1];
202 /* membufs are quite safe */
203 stream
->read (temp
, bytes
);
210 get_url_to_file (String
const &_url
, String
const &_filename
, int expected_length
,
211 HWND owner
, BOOL allow_ftp_auth
)
213 log (LOG_BABBLE
) << "get_url_to_file " << _url
<< " " << _filename
<< endLog
;
214 if (total_download_bytes
> 0)
216 int df
= diskfull (get_root_dir ().cstr_oneuse());
217 Progress
.SetBar3(df
);
219 init_dialog (_url
, expected_length
, owner
);
221 remove (_filename
.cstr_oneuse()); /* but ignore errors */
223 NetIO
*n
= NetIO::open (_url
.cstr_oneuse(), allow_ftp_auth
);
227 log (LOG_BABBLE
) << "get_url_to_file failed!" << endLog
;
231 FILE *f
= fopen (_filename
.cstr_oneuse(), "wb");
234 const char *err
= strerror (errno
);
236 err
= "(unknown error)";
237 fatal (owner
, IDS_ERR_OPEN_WRITE
, _filename
.cstr_oneuse(), err
);
241 max_bytes
= n
->file_size
;
249 count
= n
->read (buf
, sizeof (buf
));
252 fwrite (buf
, 1, count
, f
);
253 total_bytes
+= count
;
254 progress (total_bytes
);
257 total_download_bytes_sofar
+= total_bytes
;
263 if (total_download_bytes
> 0)
265 int df
= diskfull (get_root_dir ().cstr_oneuse());
266 Progress
.SetBar3(df
);