]>
Commit | Line | Data |
---|---|---|
23c9e63c | 1 | /* |
ca9506cc RC |
2 | |
3 | ||
85553593 | 4 | * Copyright (c) 2000, 2001, Red Hat, Inc. |
23c9e63c DD |
5 | * |
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. | |
10 | * | |
11 | * A copy of the GNU General Public License can be found at | |
12 | * http://www.gnu.org/ | |
13 | * | |
14 | * Written by DJ Delorie <dj@cygnus.com> | |
15 | * | |
16 | */ | |
17 | ||
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 */ | |
21 | ||
22 | #include "win32.h" | |
23 | #include "commctrl.h" | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <errno.h> | |
28 | ||
29 | #include "dialog.h" | |
30 | #include "geturl.h" | |
31 | #include "resource.h" | |
32 | #include "netio.h" | |
33 | #include "msg.h" | |
ca9506cc RC |
34 | #include "io_stream.h" |
35 | #include "io_stream_memory.h" | |
2f9645a1 CV |
36 | #include "state.h" |
37 | #include "diskfull.h" | |
85b43844 | 38 | #include "mount.h" |
4d0b3b8c | 39 | #include "filemanip.h" |
23c9e63c | 40 | |
ab57ceaa | 41 | #include "threebar.h" |
3c054baf | 42 | |
fc687221 RC |
43 | #include "Exception.h" |
44 | ||
74617327 RC |
45 | #include "LogSingleton.h" |
46 | ||
ab57ceaa RC |
47 | extern ThreeBarProgressPage Progress; |
48 | ||
23c9e63c | 49 | static int max_bytes = 0; |
7cefe128 | 50 | static int is_local_install = 0; |
23c9e63c | 51 | |
c0e6cf04 JT |
52 | long long int total_download_bytes = 0; |
53 | long long int total_download_bytes_sofar = 0; | |
2f9645a1 | 54 | |
23c9e63c DD |
55 | static DWORD start_tics; |
56 | ||
57 | static void | |
155eacb6 | 58 | init_dialog (const std::string &url, int length) |
23c9e63c | 59 | { |
7cefe128 RC |
60 | if (is_local_install) |
61 | return; | |
ab57ceaa | 62 | |
155eacb6 | 63 | std::string::size_type divide = url.find_last_of('/'); |
23c9e63c | 64 | max_bytes = length; |
ab57ceaa | 65 | Progress.SetText1("Downloading..."); |
966a9815 MB |
66 | Progress.SetText2((url.substr(divide + 1) + " from " |
67 | + url.substr(0, divide)).c_str()); | |
ab57ceaa RC |
68 | Progress.SetText3("Connecting..."); |
69 | Progress.SetBar1(0); | |
1fd6d0a2 | 70 | start_tics = GetTickCount (); |
23c9e63c DD |
71 | } |
72 | ||
73 | ||
74 | static void | |
75 | progress (int bytes) | |
76 | { | |
7cefe128 RC |
77 | if (is_local_install) |
78 | return; | |
23c9e63c | 79 | static char buf[100]; |
ab57ceaa | 80 | double kbps; |
c168185f | 81 | static unsigned int last_tics = 0; |
1fd6d0a2 | 82 | DWORD tics = GetTickCount (); |
b24c88b3 | 83 | if (tics == start_tics) // to prevent division by zero |
23c9e63c | 84 | return; |
b24c88b3 | 85 | if (tics < last_tics + 200) // to prevent flickering updates |
23c9e63c DD |
86 | return; |
87 | last_tics = tics; | |
88 | ||
ab57ceaa RC |
89 | kbps = ((double)bytes) / (double)(tics - start_tics); |
90 | if (max_bytes > 0) | |
23c9e63c | 91 | { |
ab57ceaa RC |
92 | int perc = (int)(100.0 * ((double)bytes) / (double)max_bytes); |
93 | Progress.SetBar1(bytes, max_bytes); | |
22ade75c | 94 | sprintf (buf, "%d %% (%dk/%dk) %03.1f kB/s", |
b24c88b3 | 95 | perc, bytes / 1000, max_bytes / 1000, kbps); |
2f9645a1 | 96 | if (total_download_bytes > 0) |
fc687221 RC |
97 | Progress.SetBar2(total_download_bytes_sofar + bytes, |
98 | total_download_bytes); | |
23c9e63c DD |
99 | } |
100 | else | |
22ade75c | 101 | sprintf (buf, "%d %2.1f kB/s", bytes, kbps); |
23c9e63c | 102 | |
ab57ceaa | 103 | Progress.SetText3(buf); |
23c9e63c DD |
104 | } |
105 | ||
f5d45c3b | 106 | static void |
155eacb6 | 107 | getUrlToStream (const std::string &_url, io_stream *output) |
23c9e63c | 108 | { |
7cc4d95e | 109 | is_local_install = (source == IDC_SOURCE_LOCALDIR); |
966a9815 | 110 | init_dialog (_url, 0); |
ca875ed3 | 111 | NetIO *n = NetIO::open (_url.c_str(), true); |
23c9e63c DD |
112 | if (!n || !n->ok ()) |
113 | { | |
114 | delete n; | |
346627e7 | 115 | throw new Exception (TOSTRING(__LINE__) " " __FILE__, "Error opening url", APPERR_IO_ERROR); |
23c9e63c DD |
116 | } |
117 | ||
118 | if (n->file_size) | |
119 | max_bytes = n->file_size; | |
120 | ||
341988b9 | 121 | int total_bytes = 0; |
23c9e63c DD |
122 | progress (0); |
123 | while (1) | |
124 | { | |
ca9506cc | 125 | char buf[2048]; |
4fe323f9 | 126 | ssize_t rlen, wlen; |
ca9506cc RC |
127 | rlen = n->read (buf, 2048); |
128 | if (rlen > 0) | |
4fe323f9 | 129 | { |
fc687221 | 130 | wlen = output->write (buf, rlen); |
4fe323f9 RC |
131 | if (wlen != rlen) |
132 | /* FIXME: Show an error message */ | |
133 | break; | |
134 | total_bytes += rlen; | |
135 | progress (total_bytes); | |
136 | } | |
ca9506cc RC |
137 | else |
138 | break; | |
23c9e63c | 139 | } |
fc687221 RC |
140 | if (n) |
141 | delete (n); | |
142 | /* reseeking is up to the recipient if desired */ | |
a10fdeb9 JT |
143 | |
144 | Log (LOG_BABBLE) << "Fetched URL: " << _url << endLog; | |
fc687221 | 145 | } |
23c9e63c | 146 | |
fc687221 | 147 | io_stream * |
155eacb6 | 148 | get_url_to_membuf (const std::string &_url, HWND owner) |
fc687221 RC |
149 | { |
150 | io_stream_memory *membuf = new io_stream_memory (); | |
151 | try | |
152 | { | |
966a9815 | 153 | getUrlToStream (_url, membuf); |
fc687221 RC |
154 | |
155 | if (membuf->seek (0, IO_SEEK_SET)) | |
156 | { | |
157 | if (membuf) | |
158 | delete membuf; | |
157dc2b8 | 159 | Log (LOG_BABBLE) << "get_url_to_membuf(): seek (0) failed for membuf!" << endLog; |
fc687221 RC |
160 | return 0; |
161 | } | |
162 | return membuf; | |
163 | } | |
164 | catch (Exception *e) | |
c168185f | 165 | { |
fc687221 RC |
166 | if (e->errNo() != APPERR_IO_ERROR) |
167 | throw e; | |
fc687221 | 168 | delete membuf; |
c168185f RC |
169 | return 0; |
170 | } | |
341988b9 RC |
171 | } |
172 | ||
3c054baf | 173 | // predicate: url has no '\0''s in it. |
155eacb6 AG |
174 | std::string |
175 | get_url_to_string (const std::string &_url, HWND owner) | |
341988b9 | 176 | { |
ab57ceaa | 177 | io_stream *stream = get_url_to_membuf (_url, owner); |
341988b9 | 178 | if (!stream) |
155eacb6 | 179 | return std::string(); |
341988b9 RC |
180 | size_t bytes = stream->get_size (); |
181 | if (!bytes) | |
182 | { | |
183 | /* zero length, or error retrieving length */ | |
184 | delete stream; | |
157dc2b8 | 185 | Log (LOG_BABBLE) << "get_url_to_string(): couldn't retrieve buffer size, or zero length buffer" << endLog; |
155eacb6 | 186 | return std::string(); |
341988b9 | 187 | } |
3c054baf | 188 | char temp [bytes + 1]; |
341988b9 | 189 | /* membufs are quite safe */ |
3c054baf RC |
190 | stream->read (temp, bytes); |
191 | temp [bytes] = '\0'; | |
341988b9 | 192 | delete stream; |
155eacb6 | 193 | return std::string(temp); |
23c9e63c DD |
194 | } |
195 | ||
196 | int | |
155eacb6 AG |
197 | get_url_to_file (const std::string &_url, |
198 | const std::string &_filename, | |
f5d45c3b MB |
199 | int expected_length, |
200 | HWND owner) | |
23c9e63c | 201 | { |
157dc2b8 | 202 | Log (LOG_BABBLE) << "get_url_to_file " << _url << " " << _filename << endLog; |
2f9645a1 CV |
203 | if (total_download_bytes > 0) |
204 | { | |
d2a3615c | 205 | int df = diskfull (get_root_dir ().c_str()); |
3c054baf | 206 | Progress.SetBar3(df); |
2f9645a1 | 207 | } |
966a9815 | 208 | init_dialog (_url, expected_length); |
23c9e63c | 209 | |
d2a3615c | 210 | remove (_filename.c_str()); /* but ignore errors */ |
23c9e63c | 211 | |
ca875ed3 | 212 | NetIO *n = NetIO::open (_url.c_str(), false); |
23c9e63c DD |
213 | if (!n || !n->ok ()) |
214 | { | |
215 | delete n; | |
216 | return 1; | |
217 | } | |
218 | ||
4d0b3b8c | 219 | FILE *f = nt_fopen (_filename.c_str(), "wb"); |
23c9e63c DD |
220 | if (!f) |
221 | { | |
b24c88b3 | 222 | const char *err = strerror (errno); |
23c9e63c DD |
223 | if (!err) |
224 | err = "(unknown error)"; | |
d2a3615c | 225 | fatal (owner, IDS_ERR_OPEN_WRITE, _filename.c_str(), err); |
23c9e63c DD |
226 | } |
227 | ||
228 | if (n->file_size) | |
229 | max_bytes = n->file_size; | |
230 | ||
231 | int total_bytes = 0; | |
232 | progress (0); | |
233 | while (1) | |
234 | { | |
4a83b7b0 | 235 | char buf[8192]; |
23c9e63c DD |
236 | int count; |
237 | count = n->read (buf, sizeof (buf)); | |
238 | if (count <= 0) | |
239 | break; | |
240 | fwrite (buf, 1, count, f); | |
241 | total_bytes += count; | |
242 | progress (total_bytes); | |
243 | } | |
244 | ||
2f9645a1 CV |
245 | total_download_bytes_sofar += total_bytes; |
246 | ||
23c9e63c | 247 | fclose (f); |
8e58f8fd RC |
248 | if (n) |
249 | delete n; | |
23c9e63c | 250 | |
2f9645a1 CV |
251 | if (total_download_bytes > 0) |
252 | { | |
d2a3615c | 253 | int df = diskfull (get_root_dir ().c_str()); |
ab57ceaa | 254 | Progress.SetBar3(df); |
2f9645a1 CV |
255 | } |
256 | ||
23c9e63c DD |
257 | return 0; |
258 | } | |
259 |