]>
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 | |
23c9e63c DD |
46 | static HWND gw_dialog = 0; |
47 | static HWND gw_url = 0; | |
48 | static HWND gw_rate = 0; | |
49 | static HWND gw_progress = 0; | |
2f9645a1 CV |
50 | static HWND gw_pprogress = 0; |
51 | static HWND gw_iprogress = 0; | |
52 | static HWND gw_progress_text = 0; | |
53 | static HWND gw_pprogress_text = 0; | |
54 | static HWND gw_iprogress_text = 0; | |
23c9e63c DD |
55 | static HANDLE init_event; |
56 | static int max_bytes = 0; | |
7cefe128 | 57 | static int is_local_install = 0; |
23c9e63c | 58 | |
2f9645a1 CV |
59 | int total_download_bytes = 0; |
60 | int total_download_bytes_sofar = 0; | |
61 | ||
23c9e63c DD |
62 | static BOOL |
63 | dialog_cmd (HWND h, int id, HWND hwndctl, UINT code) | |
64 | { | |
65 | switch (id) | |
66 | { | |
67 | case IDCANCEL: | |
89b1a15b | 68 | exit_setup (0); |
23c9e63c | 69 | } |
b24c88b3 | 70 | return 0; |
23c9e63c DD |
71 | } |
72 | ||
73 | static BOOL CALLBACK | |
74 | dialog_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam) | |
75 | { | |
23c9e63c DD |
76 | switch (message) |
77 | { | |
78 | case WM_INITDIALOG: | |
79 | gw_dialog = h; | |
80 | gw_url = GetDlgItem (h, IDC_DLS_URL); | |
81 | gw_rate = GetDlgItem (h, IDC_DLS_RATE); | |
82 | gw_progress = GetDlgItem (h, IDC_DLS_PROGRESS); | |
2f9645a1 CV |
83 | gw_pprogress = GetDlgItem (h, IDC_DLS_PPROGRESS); |
84 | gw_iprogress = GetDlgItem (h, IDC_DLS_IPROGRESS); | |
85 | gw_progress_text = GetDlgItem (h, IDC_DLS_PROGRESS_TEXT); | |
86 | gw_pprogress_text = GetDlgItem (h, IDC_DLS_PPROGRESS_TEXT); | |
87 | gw_iprogress_text = GetDlgItem (h, IDC_DLS_IPROGRESS_TEXT); | |
23c9e63c | 88 | SetEvent (init_event); |
7cefe128 | 89 | return TRUE; |
23c9e63c | 90 | case WM_COMMAND: |
1fd6d0a2 | 91 | return HANDLE_WM_COMMAND (h, wParam, lParam, dialog_cmd); |
23c9e63c DD |
92 | } |
93 | return FALSE; | |
94 | } | |
95 | ||
96 | static WINAPI DWORD | |
97 | dialog (void *) | |
98 | { | |
23c9e63c | 99 | MSG m; |
b24c88b3 RC |
100 | HWND local_gw_dialog = |
101 | CreateDialog (hinstance, MAKEINTRESOURCE (IDD_DLSTATUS), | |
102 | 0, dialog_proc); | |
f97a1ece RC |
103 | ShowWindow (local_gw_dialog, SW_SHOWNORMAL); |
104 | UpdateWindow (local_gw_dialog); | |
b24c88b3 RC |
105 | while (GetMessage (&m, 0, 0, 0) > 0) |
106 | { | |
107 | TranslateMessage (&m); | |
108 | DispatchMessage (&m); | |
109 | } | |
110 | return 0; | |
23c9e63c DD |
111 | } |
112 | ||
113 | static DWORD start_tics; | |
114 | ||
115 | static void | |
116 | init_dialog (char *url, int length) | |
117 | { | |
7cefe128 RC |
118 | if (is_local_install) |
119 | return; | |
23c9e63c DD |
120 | if (gw_dialog == 0) |
121 | { | |
122 | DWORD tid; | |
123 | HANDLE thread; | |
124 | init_event = CreateEvent (0, 0, 0, 0); | |
125 | thread = CreateThread (0, 0, dialog, 0, 0, &tid); | |
126 | WaitForSingleObject (init_event, 1000); | |
127 | CloseHandle (init_event); | |
128 | SendMessage (gw_progress, PBM_SETRANGE, 0, MAKELPARAM (0, 100)); | |
2f9645a1 CV |
129 | SendMessage (gw_pprogress, PBM_SETRANGE, 0, MAKELPARAM (0, 100)); |
130 | SendMessage (gw_iprogress, PBM_SETRANGE, 0, MAKELPARAM (0, 100)); | |
23c9e63c | 131 | } |
b24c88b3 RC |
132 | char *sl = url, *cp; |
133 | for (cp = url; *cp; cp++) | |
23c9e63c | 134 | if (*cp == '/' || *cp == '\\' || *cp == ':') |
b24c88b3 | 135 | sl = cp + 1; |
23c9e63c DD |
136 | max_bytes = length; |
137 | SetWindowText (gw_url, sl); | |
138 | SetWindowText (gw_rate, "Connecting..."); | |
139 | SendMessage (gw_progress, PBM_SETPOS, (WPARAM) 0, 0); | |
140 | ShowWindow (gw_progress, (length > 0) ? SW_SHOW : SW_HIDE); | |
b24c88b3 | 141 | if (length > 0) |
2f9645a1 CV |
142 | SetWindowText (gw_progress_text, "Package"); |
143 | else | |
144 | SetWindowText (gw_progress_text, " "); | |
145 | ShowWindow (gw_pprogress, (total_download_bytes > 0) ? SW_SHOW : SW_HIDE); | |
146 | if (total_download_bytes > 0) | |
147 | { | |
148 | SetWindowText (gw_pprogress_text, "Total"); | |
149 | SetWindowText (gw_iprogress_text, "Disk"); | |
150 | } | |
42bf5b92 | 151 | else |
2f9645a1 CV |
152 | { |
153 | SetWindowText (gw_pprogress_text, " "); | |
154 | SetWindowText (gw_iprogress_text, " "); | |
155 | } | |
156 | ShowWindow (gw_iprogress, (total_download_bytes > 0) ? SW_SHOW : SW_HIDE); | |
23c9e63c | 157 | ShowWindow (gw_dialog, SW_SHOWNORMAL); |
1fd6d0a2 | 158 | start_tics = GetTickCount (); |
23c9e63c DD |
159 | } |
160 | ||
161 | ||
162 | static void | |
163 | progress (int bytes) | |
164 | { | |
7cefe128 RC |
165 | if (is_local_install) |
166 | return; | |
23c9e63c DD |
167 | static char buf[100]; |
168 | int kbps; | |
c168185f | 169 | static unsigned int last_tics = 0; |
1fd6d0a2 | 170 | DWORD tics = GetTickCount (); |
b24c88b3 | 171 | if (tics == start_tics) // to prevent division by zero |
23c9e63c | 172 | return; |
b24c88b3 | 173 | if (tics < last_tics + 200) // to prevent flickering updates |
23c9e63c DD |
174 | return; |
175 | last_tics = tics; | |
176 | ||
177 | kbps = bytes / (tics - start_tics); | |
178 | ShowWindow (gw_progress, (max_bytes > 0) ? SW_SHOW : SW_HIDE); | |
2f9645a1 CV |
179 | ShowWindow (gw_pprogress, (total_download_bytes > 0) ? SW_SHOW : SW_HIDE); |
180 | ShowWindow (gw_iprogress, (total_download_bytes > 0) ? SW_SHOW : SW_HIDE); | |
bf1d5889 | 181 | if (max_bytes > 100) |
23c9e63c | 182 | { |
bf1d5889 | 183 | int perc = bytes / (max_bytes / 100); |
23c9e63c | 184 | SendMessage (gw_progress, PBM_SETPOS, (WPARAM) perc, 0); |
1fd6d0a2 | 185 | sprintf (buf, "%3d %% (%dk/%dk) %d kb/s\n", |
b24c88b3 | 186 | perc, bytes / 1000, max_bytes / 1000, kbps); |
2f9645a1 | 187 | if (total_download_bytes > 0) |
b24c88b3 RC |
188 | { |
189 | int totalperc = | |
190 | (total_download_bytes_sofar + | |
191 | bytes) / (total_download_bytes / 100); | |
192 | SendMessage (gw_pprogress, PBM_SETPOS, (WPARAM) totalperc, 0); | |
193 | } | |
23c9e63c DD |
194 | } |
195 | else | |
1fd6d0a2 | 196 | sprintf (buf, "%d %d kb/s\n", bytes, kbps); |
23c9e63c DD |
197 | |
198 | SetWindowText (gw_rate, buf); | |
199 | } | |
200 | ||
23c9e63c DD |
201 | char * |
202 | get_url_to_string (char *_url) | |
203 | { | |
89b1a15b | 204 | log (LOG_BABBLE, "get_url_to_string %s", _url); |
7cefe128 | 205 | is_local_install = (source == IDC_SOURCE_CWD); |
23c9e63c DD |
206 | init_dialog (_url, 0); |
207 | NetIO *n = NetIO::open (_url); | |
208 | if (!n || !n->ok ()) | |
209 | { | |
210 | delete n; | |
89b1a15b | 211 | log (LOG_BABBLE, "get_url_to_string failed!"); |
23c9e63c DD |
212 | return 0; |
213 | } | |
214 | ||
215 | if (n->file_size) | |
216 | max_bytes = n->file_size; | |
217 | ||
ca9506cc RC |
218 | io_stream_memory * membuf = new io_stream_memory (); |
219 | ||
220 | int total_bytes = 1; /* for the NUL terminator */ | |
23c9e63c DD |
221 | progress (0); |
222 | while (1) | |
223 | { | |
ca9506cc RC |
224 | char buf[2048]; |
225 | ssize_t rlen,wlen; | |
226 | rlen = n->read (buf, 2048); | |
227 | if (rlen > 0) | |
228 | { | |
229 | wlen = membuf->write (buf, rlen); | |
230 | if (wlen != rlen) | |
231 | /* FIXME: Show an error message */ | |
23c9e63c | 232 | break; |
ca9506cc | 233 | total_bytes += rlen; |
23c9e63c | 234 | progress (total_bytes); |
ca9506cc RC |
235 | } |
236 | else | |
237 | break; | |
23c9e63c DD |
238 | } |
239 | ||
240 | char *rv = (char *) malloc (total_bytes); | |
ca9506cc | 241 | if (NULL == rv || membuf->seek (0, IO_SEEK_SET)) |
c168185f | 242 | { |
8e58f8fd RC |
243 | if (n) |
244 | delete n; | |
ca9506cc RC |
245 | if (membuf) |
246 | delete membuf; | |
c168185f RC |
247 | log (LOG_BABBLE, "get_url_to_string(): malloc failed for rv!"); |
248 | return 0; | |
249 | } | |
ca9506cc RC |
250 | |
251 | ssize_t rlen; | |
252 | rlen = membuf->read (rv, total_bytes); | |
253 | rv [total_bytes] = '\0'; | |
8e58f8fd RC |
254 | if (n) |
255 | delete n; | |
ca9506cc RC |
256 | if (membuf) |
257 | delete membuf; | |
23c9e63c DD |
258 | return rv; |
259 | } | |
260 | ||
261 | int | |
85553593 CV |
262 | get_url_to_file (char *_url, char *_filename, int expected_length, |
263 | BOOL allow_ftp_auth) | |
23c9e63c | 264 | { |
89b1a15b | 265 | log (LOG_BABBLE, "get_url_to_file %s %s", _url, _filename); |
2f9645a1 CV |
266 | if (total_download_bytes > 0) |
267 | { | |
85b43844 | 268 | int df = diskfull (get_root_dir ()); |
2f9645a1 CV |
269 | SendMessage (gw_iprogress, PBM_SETPOS, (WPARAM) df, 0); |
270 | } | |
23c9e63c DD |
271 | init_dialog (_url, expected_length); |
272 | ||
b24c88b3 | 273 | remove (_filename); /* but ignore errors */ |
23c9e63c | 274 | |
85553593 | 275 | NetIO *n = NetIO::open (_url, allow_ftp_auth); |
23c9e63c DD |
276 | if (!n || !n->ok ()) |
277 | { | |
278 | delete n; | |
89b1a15b | 279 | log (LOG_BABBLE, "get_url_to_file failed!"); |
23c9e63c DD |
280 | return 1; |
281 | } | |
282 | ||
1fd6d0a2 | 283 | FILE *f = fopen (_filename, "wb"); |
23c9e63c DD |
284 | if (!f) |
285 | { | |
b24c88b3 | 286 | const char *err = strerror (errno); |
23c9e63c DD |
287 | if (!err) |
288 | err = "(unknown error)"; | |
289 | fatal (IDS_ERR_OPEN_WRITE, _filename, err); | |
290 | } | |
291 | ||
292 | if (n->file_size) | |
293 | max_bytes = n->file_size; | |
294 | ||
295 | int total_bytes = 0; | |
296 | progress (0); | |
297 | while (1) | |
298 | { | |
4a83b7b0 | 299 | char buf[8192]; |
23c9e63c DD |
300 | int count; |
301 | count = n->read (buf, sizeof (buf)); | |
302 | if (count <= 0) | |
303 | break; | |
304 | fwrite (buf, 1, count, f); | |
305 | total_bytes += count; | |
306 | progress (total_bytes); | |
307 | } | |
308 | ||
2f9645a1 CV |
309 | total_download_bytes_sofar += total_bytes; |
310 | ||
23c9e63c | 311 | fclose (f); |
8e58f8fd RC |
312 | if (n) |
313 | delete n; | |
23c9e63c | 314 | |
2f9645a1 CV |
315 | if (total_download_bytes > 0) |
316 | { | |
85b43844 | 317 | int df = diskfull (get_root_dir ()); |
2f9645a1 CV |
318 | SendMessage (gw_iprogress, PBM_SETPOS, (WPARAM) df, 0); |
319 | } | |
320 | ||
23c9e63c DD |
321 | return 0; |
322 | } | |
323 | ||
324 | void | |
325 | dismiss_url_status_dialog () | |
326 | { | |
7cefe128 RC |
327 | if (!is_local_install) |
328 | ShowWindow (gw_dialog, SW_HIDE); | |
23c9e63c | 329 | } |