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 */
34 #include "io_stream.h"
35 #include "io_stream_memory.h"
39 #include "filemanip.h"
43 #include "Exception.h"
45 #include "LogSingleton.h"
49 extern ThreeBarProgressPage Progress
;
51 static int max_bytes
= 0;
52 static int is_local_install
= 0;
54 long long int total_download_bytes
= 0;
55 long long int total_download_bytes_sofar
= 0;
57 static DWORD start_tics
;
60 init_dialog (const string
&url
, int length
)
65 string::size_type divide
= url
.find_last_of('/');
67 Progress
.SetText1("Downloading...");
68 Progress
.SetText2((url
.substr(divide
+ 1) + " from "
69 + url
.substr(0, divide
)).c_str());
70 Progress
.SetText3("Connecting...");
72 start_tics
= GetTickCount ();
83 static unsigned int last_tics
= 0;
84 DWORD tics
= GetTickCount ();
85 if (tics
== start_tics
) // to prevent division by zero
87 if (tics
< last_tics
+ 200) // to prevent flickering updates
91 kbps
= ((double)bytes
) / (double)(tics
- start_tics
);
94 int perc
= (int)(100.0 * ((double)bytes
) / (double)max_bytes
);
95 Progress
.SetBar1(bytes
, max_bytes
);
96 sprintf (buf
, "%d %% (%dk/%dk) %03.1f kB/s",
97 perc
, bytes
/ 1000, max_bytes
/ 1000, kbps
);
98 if (total_download_bytes
> 0)
99 Progress
.SetBar2(total_download_bytes_sofar
+ bytes
,
100 total_download_bytes
);
103 sprintf (buf
, "%d %2.1f kB/s", bytes
, kbps
);
105 Progress
.SetText3(buf
);
109 getUrlToStream (const string
&_url
, io_stream
*output
)
111 is_local_install
= (source
== IDC_SOURCE_LOCALDIR
);
112 init_dialog (_url
, 0);
113 NetIO
*n
= NetIO::open (_url
.c_str(), true);
117 throw new Exception (TOSTRING(__LINE__
) " " __FILE__
, "Error opening url", APPERR_IO_ERROR
);
121 max_bytes
= n
->file_size
;
129 rlen
= n
->read (buf
, 2048);
132 wlen
= output
->write (buf
, rlen
);
134 /* FIXME: Show an error message */
137 progress (total_bytes
);
144 /* reseeking is up to the recipient if desired */
146 Log (LOG_BABBLE
) << "Fetched URL: " << _url
<< endLog
;
150 get_url_to_membuf (const string
&_url
, HWND owner
)
152 io_stream_memory
*membuf
= new io_stream_memory ();
155 getUrlToStream (_url
, membuf
);
157 if (membuf
->seek (0, IO_SEEK_SET
))
161 Log (LOG_BABBLE
) << "get_url_to_membuf(): seek (0) failed for membuf!" << endLog
;
168 if (e
->errNo() != APPERR_IO_ERROR
)
175 // predicate: url has no '\0''s in it.
177 get_url_to_string (const string
&_url
, HWND owner
)
179 io_stream
*stream
= get_url_to_membuf (_url
, owner
);
182 size_t bytes
= stream
->get_size ();
185 /* zero length, or error retrieving length */
187 Log (LOG_BABBLE
) << "get_url_to_string(): couldn't retrieve buffer size, or zero length buffer" << endLog
;
190 char temp
[bytes
+ 1];
191 /* membufs are quite safe */
192 stream
->read (temp
, bytes
);
199 get_url_to_file (const string
&_url
,
200 const string
&_filename
,
204 Log (LOG_BABBLE
) << "get_url_to_file " << _url
<< " " << _filename
<< endLog
;
205 if (total_download_bytes
> 0)
207 int df
= diskfull (get_root_dir ().c_str());
208 Progress
.SetBar3(df
);
210 init_dialog (_url
, expected_length
);
212 remove (_filename
.c_str()); /* but ignore errors */
214 NetIO
*n
= NetIO::open (_url
.c_str(), false);
221 FILE *f
= nt_fopen (_filename
.c_str(), "wb");
224 const char *err
= strerror (errno
);
226 err
= "(unknown error)";
227 fatal (owner
, IDS_ERR_OPEN_WRITE
, _filename
.c_str(), err
);
231 max_bytes
= n
->file_size
;
239 count
= n
->read (buf
, sizeof (buf
));
242 fwrite (buf
, 1, count
, f
);
243 total_bytes
+= count
;
244 progress (total_bytes
);
247 total_download_bytes_sofar
+= total_bytes
;
253 if (total_download_bytes
> 0)
255 int df
= diskfull (get_root_dir ().c_str());
256 Progress
.SetBar3(df
);