]>
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 | ||
b24c88b3 RC |
22 | #if 0 |
23 | static const char *cvsid = | |
24 | "\n%%% $Id$\n"; | |
25 | #endif | |
8507f105 | 26 | |
23c9e63c DD |
27 | #include "win32.h" |
28 | #include "commctrl.h" | |
29 | ||
30 | #include <stdio.h> | |
31 | #include <stdlib.h> | |
32 | #include <errno.h> | |
33 | ||
34 | #include "dialog.h" | |
35 | #include "geturl.h" | |
36 | #include "resource.h" | |
37 | #include "netio.h" | |
38 | #include "msg.h" | |
89b1a15b | 39 | #include "log.h" |
ca9506cc RC |
40 | #include "io_stream.h" |
41 | #include "io_stream_memory.h" | |
2f9645a1 CV |
42 | #include "state.h" |
43 | #include "diskfull.h" | |
85b43844 | 44 | #include "mount.h" |
23c9e63c | 45 | |
ab57ceaa | 46 | #include "threebar.h" |
3c054baf RC |
47 | |
48 | #include "String++.h" | |
49 | ||
ab57ceaa RC |
50 | extern ThreeBarProgressPage Progress; |
51 | ||
23c9e63c | 52 | static int max_bytes = 0; |
7cefe128 | 53 | static int is_local_install = 0; |
23c9e63c | 54 | |
2f9645a1 CV |
55 | int total_download_bytes = 0; |
56 | int total_download_bytes_sofar = 0; | |
57 | ||
23c9e63c DD |
58 | static DWORD start_tics; |
59 | ||
60 | static void | |
3c054baf | 61 | init_dialog (String const &url, int length, HWND owner) |
23c9e63c | 62 | { |
7cefe128 RC |
63 | if (is_local_install) |
64 | return; | |
ab57ceaa | 65 | |
3c054baf RC |
66 | char const *temp = url.cstr(); |
67 | char const *sl = temp; | |
341988b9 | 68 | char const *cp; |
3c054baf | 69 | for (cp = sl; *cp; cp++) |
23c9e63c | 70 | if (*cp == '/' || *cp == '\\' || *cp == ':') |
b24c88b3 | 71 | sl = cp + 1; |
23c9e63c | 72 | max_bytes = length; |
ab57ceaa RC |
73 | Progress.SetText1("Downloading..."); |
74 | Progress.SetText2(sl); | |
75 | Progress.SetText3("Connecting..."); | |
76 | Progress.SetBar1(0); | |
1fd6d0a2 | 77 | start_tics = GetTickCount (); |
3c054baf | 78 | delete[] temp; |
23c9e63c DD |
79 | } |
80 | ||
81 | ||
82 | static void | |
83 | progress (int bytes) | |
84 | { | |
7cefe128 RC |
85 | if (is_local_install) |
86 | return; | |
23c9e63c | 87 | static char buf[100]; |
ab57ceaa | 88 | double kbps; |
c168185f | 89 | static unsigned int last_tics = 0; |
1fd6d0a2 | 90 | DWORD tics = GetTickCount (); |
b24c88b3 | 91 | if (tics == start_tics) // to prevent division by zero |
23c9e63c | 92 | return; |
b24c88b3 | 93 | if (tics < last_tics + 200) // to prevent flickering updates |
23c9e63c DD |
94 | return; |
95 | last_tics = tics; | |
96 | ||
ab57ceaa RC |
97 | kbps = ((double)bytes) / (double)(tics - start_tics); |
98 | if (max_bytes > 0) | |
23c9e63c | 99 | { |
ab57ceaa RC |
100 | int perc = (int)(100.0 * ((double)bytes) / (double)max_bytes); |
101 | Progress.SetBar1(bytes, max_bytes); | |
b7301c43 | 102 | sprintf (buf, "%d %% (%dk/%dk) %03.1f kb/s\n", |
b24c88b3 | 103 | perc, bytes / 1000, max_bytes / 1000, kbps); |
2f9645a1 | 104 | if (total_download_bytes > 0) |
b24c88b3 | 105 | { |
ab57ceaa | 106 | Progress.SetBar2(total_download_bytes_sofar + bytes, total_download_bytes); |
b24c88b3 | 107 | } |
23c9e63c DD |
108 | } |
109 | else | |
ab57ceaa | 110 | sprintf (buf, "%d %2.1f kb/s\n", bytes, kbps); |
23c9e63c | 111 | |
ab57ceaa | 112 | Progress.SetText3(buf); |
23c9e63c DD |
113 | } |
114 | ||
341988b9 | 115 | io_stream * |
3c054baf | 116 | get_url_to_membuf (String const &_url, HWND owner) |
23c9e63c | 117 | { |
1ac649ed | 118 | log (LOG_BABBLE, String ("get_url_to_membuf ") + _url); |
7cefe128 | 119 | is_local_install = (source == IDC_SOURCE_CWD); |
ab57ceaa | 120 | init_dialog (_url, 0, owner); |
3c054baf | 121 | NetIO *n = NetIO::open (_url.cstr_oneuse()); |
23c9e63c DD |
122 | if (!n || !n->ok ()) |
123 | { | |
124 | delete n; | |
341988b9 | 125 | log (LOG_BABBLE, "get_url_to_membuf failed!"); |
23c9e63c DD |
126 | return 0; |
127 | } | |
128 | ||
129 | if (n->file_size) | |
130 | max_bytes = n->file_size; | |
131 | ||
4fe323f9 RC |
132 | io_stream_memory *membuf = new io_stream_memory (); |
133 | ||
341988b9 | 134 | int total_bytes = 0; |
23c9e63c DD |
135 | progress (0); |
136 | while (1) | |
137 | { | |
ca9506cc | 138 | char buf[2048]; |
4fe323f9 | 139 | ssize_t rlen, wlen; |
ca9506cc RC |
140 | rlen = n->read (buf, 2048); |
141 | if (rlen > 0) | |
4fe323f9 RC |
142 | { |
143 | wlen = membuf->write (buf, rlen); | |
144 | if (wlen != rlen) | |
145 | /* FIXME: Show an error message */ | |
146 | break; | |
147 | total_bytes += rlen; | |
148 | progress (total_bytes); | |
149 | } | |
ca9506cc RC |
150 | else |
151 | break; | |
23c9e63c DD |
152 | } |
153 | ||
341988b9 | 154 | if (membuf->seek (0, IO_SEEK_SET)) |
c168185f | 155 | { |
8e58f8fd RC |
156 | if (n) |
157 | delete n; | |
ca9506cc RC |
158 | if (membuf) |
159 | delete membuf; | |
341988b9 | 160 | log (LOG_BABBLE, "get_url_to_membuf(): seek (0) failed for membuf!"); |
c168185f RC |
161 | return 0; |
162 | } | |
4fe323f9 | 163 | |
8e58f8fd RC |
164 | if (n) |
165 | delete n; | |
341988b9 RC |
166 | return membuf; |
167 | } | |
168 | ||
3c054baf RC |
169 | // predicate: url has no '\0''s in it. |
170 | String | |
171 | get_url_to_string (String const &_url, HWND owner) | |
341988b9 | 172 | { |
ab57ceaa | 173 | io_stream *stream = get_url_to_membuf (_url, owner); |
341988b9 | 174 | if (!stream) |
3c054baf | 175 | return String(); |
341988b9 RC |
176 | size_t bytes = stream->get_size (); |
177 | if (!bytes) | |
178 | { | |
179 | /* zero length, or error retrieving length */ | |
180 | delete stream; | |
181 | log (LOG_BABBLE, "get_url_to_string(): couldn't retrieve buffer size, or zero length buffer"); | |
3c054baf | 182 | return String(); |
341988b9 | 183 | } |
3c054baf | 184 | char temp [bytes + 1]; |
341988b9 | 185 | /* membufs are quite safe */ |
3c054baf RC |
186 | stream->read (temp, bytes); |
187 | temp [bytes] = '\0'; | |
341988b9 | 188 | delete stream; |
3c054baf | 189 | return String(temp); |
23c9e63c DD |
190 | } |
191 | ||
192 | int | |
3c054baf | 193 | get_url_to_file (String const &_url, String const &_filename, int expected_length, |
ab57ceaa | 194 | HWND owner, BOOL allow_ftp_auth) |
23c9e63c | 195 | { |
1ac649ed | 196 | log (LOG_BABBLE, String ("get_url_to_file ") + _url + " " + _filename); |
2f9645a1 CV |
197 | if (total_download_bytes > 0) |
198 | { | |
3c054baf RC |
199 | int df = diskfull (get_root_dir ().cstr_oneuse()); |
200 | Progress.SetBar3(df); | |
2f9645a1 | 201 | } |
ab57ceaa | 202 | init_dialog (_url, expected_length, owner); |
23c9e63c | 203 | |
3c054baf | 204 | remove (_filename.cstr_oneuse()); /* but ignore errors */ |
23c9e63c | 205 | |
3c054baf | 206 | NetIO *n = NetIO::open (_url.cstr_oneuse(), allow_ftp_auth); |
23c9e63c DD |
207 | if (!n || !n->ok ()) |
208 | { | |
209 | delete n; | |
89b1a15b | 210 | log (LOG_BABBLE, "get_url_to_file failed!"); |
23c9e63c DD |
211 | return 1; |
212 | } | |
213 | ||
3c054baf | 214 | FILE *f = fopen (_filename.cstr_oneuse(), "wb"); |
23c9e63c DD |
215 | if (!f) |
216 | { | |
b24c88b3 | 217 | const char *err = strerror (errno); |
23c9e63c DD |
218 | if (!err) |
219 | err = "(unknown error)"; | |
3c054baf | 220 | fatal (owner, IDS_ERR_OPEN_WRITE, _filename.cstr_oneuse(), err); |
23c9e63c DD |
221 | } |
222 | ||
223 | if (n->file_size) | |
224 | max_bytes = n->file_size; | |
225 | ||
226 | int total_bytes = 0; | |
227 | progress (0); | |
228 | while (1) | |
229 | { | |
4a83b7b0 | 230 | char buf[8192]; |
23c9e63c DD |
231 | int count; |
232 | count = n->read (buf, sizeof (buf)); | |
233 | if (count <= 0) | |
234 | break; | |
235 | fwrite (buf, 1, count, f); | |
236 | total_bytes += count; | |
237 | progress (total_bytes); | |
238 | } | |
239 | ||
2f9645a1 CV |
240 | total_download_bytes_sofar += total_bytes; |
241 | ||
23c9e63c | 242 | fclose (f); |
8e58f8fd RC |
243 | if (n) |
244 | delete n; | |
23c9e63c | 245 | |
2f9645a1 CV |
246 | if (total_download_bytes > 0) |
247 | { | |
3c054baf | 248 | int df = diskfull (get_root_dir ().cstr_oneuse()); |
ab57ceaa | 249 | Progress.SetBar3(df); |
2f9645a1 CV |
250 | } |
251 | ||
23c9e63c DD |
252 | return 0; |
253 | } | |
254 |