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"
44 #include "filemanip.h"
48 #include "Exception.h"
50 #include "LogSingleton.h"
54 extern ThreeBarProgressPage Progress
;
56 static int max_bytes
= 0;
57 static int is_local_install
= 0;
59 long long int total_download_bytes
= 0;
60 long long int total_download_bytes_sofar
= 0;
62 static DWORD start_tics
;
65 init_dialog (const string
&url
, int length
)
70 string::size_type divide
= url
.find_last_of('/');
72 Progress
.SetText1("Downloading...");
73 Progress
.SetText2((url
.substr(divide
+ 1) + " from "
74 + url
.substr(0, divide
)).c_str());
75 Progress
.SetText3("Connecting...");
77 start_tics
= GetTickCount ();
88 static unsigned int last_tics
= 0;
89 DWORD tics
= GetTickCount ();
90 if (tics
== start_tics
) // to prevent division by zero
92 if (tics
< last_tics
+ 200) // to prevent flickering updates
96 kbps
= ((double)bytes
) / (double)(tics
- start_tics
);
99 int perc
= (int)(100.0 * ((double)bytes
) / (double)max_bytes
);
100 Progress
.SetBar1(bytes
, max_bytes
);
101 sprintf (buf
, "%d %% (%dk/%dk) %03.1f kB/s",
102 perc
, bytes
/ 1000, max_bytes
/ 1000, kbps
);
103 if (total_download_bytes
> 0)
104 Progress
.SetBar2(total_download_bytes_sofar
+ bytes
,
105 total_download_bytes
);
108 sprintf (buf
, "%d %2.1f kB/s", bytes
, kbps
);
110 Progress
.SetText3(buf
);
114 getUrlToStream (const string
&_url
, io_stream
*output
)
116 Log (LOG_BABBLE
) << "getUrlToStream " << _url
<< endLog
;
117 is_local_install
= (source
== IDC_SOURCE_LOCALDIR
);
118 init_dialog (_url
, 0);
119 NetIO
*n
= NetIO::open (_url
.c_str());
123 Log (LOG_BABBLE
) << "getUrlToStream failed!" << endLog
;
124 throw new Exception (TOSTRING(__LINE__
) " " __FILE__
, "Error opening url", APPERR_IO_ERROR
);
128 max_bytes
= n
->file_size
;
136 rlen
= n
->read (buf
, 2048);
139 wlen
= output
->write (buf
, rlen
);
141 /* FIXME: Show an error message */
144 progress (total_bytes
);
151 /* reseeking is up to the recipient if desired */
155 get_url_to_membuf (const string
&_url
, HWND owner
)
157 io_stream_memory
*membuf
= new io_stream_memory ();
160 Log (LOG_BABBLE
) << "get_url_to_membuf " << _url
<< endLog
;
161 getUrlToStream (_url
, membuf
);
163 if (membuf
->seek (0, IO_SEEK_SET
))
167 Log (LOG_BABBLE
) << "get_url_to_membuf(): seek (0) failed for membuf!" << endLog
;
174 if (e
->errNo() != APPERR_IO_ERROR
)
176 Log (LOG_BABBLE
) << "get_url_to_membuf failed!" << endLog
;
182 // predicate: url has no '\0''s in it.
184 get_url_to_string (const string
&_url
, HWND owner
)
186 io_stream
*stream
= get_url_to_membuf (_url
, owner
);
189 size_t bytes
= stream
->get_size ();
192 /* zero length, or error retrieving length */
194 Log (LOG_BABBLE
) << "get_url_to_string(): couldn't retrieve buffer size, or zero length buffer" << endLog
;
197 char temp
[bytes
+ 1];
198 /* membufs are quite safe */
199 stream
->read (temp
, bytes
);
206 get_url_to_file (const string
&_url
,
207 const string
&_filename
,
211 Log (LOG_BABBLE
) << "get_url_to_file " << _url
<< " " << _filename
<< endLog
;
212 if (total_download_bytes
> 0)
214 int df
= diskfull (get_root_dir ().c_str());
215 Progress
.SetBar3(df
);
217 init_dialog (_url
, expected_length
);
219 remove (_filename
.c_str()); /* but ignore errors */
221 NetIO
*n
= NetIO::open (_url
.c_str());
225 Log (LOG_BABBLE
) << "get_url_to_file failed!" << endLog
;
229 FILE *f
= nt_fopen (_filename
.c_str(), "wb");
232 const char *err
= strerror (errno
);
234 err
= "(unknown error)";
235 fatal (owner
, IDS_ERR_OPEN_WRITE
, _filename
.c_str(), err
);
239 max_bytes
= n
->file_size
;
247 count
= n
->read (buf
, sizeof (buf
));
250 fwrite (buf
, 1, count
, f
);
251 total_bytes
+= count
;
252 progress (total_bytes
);
255 total_download_bytes_sofar
+= total_bytes
;
261 if (total_download_bytes
> 0)
263 int df
= diskfull (get_root_dir ().c_str());
264 Progress
.SetBar3(df
);