]> cygwin.com Git - cygwin-apps/setup.git/blob - geturl.cc
0144996f0be0d9d3e7e8406cc802736d149c6b59
[cygwin-apps/setup.git] / geturl.cc
1 /*
2
3
4 * Copyright (c) 2000, 2001, Red Hat, Inc.
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 #if 0
23 static const char *cvsid =
24 "\n%%% $Id$\n";
25 #endif
26
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"
39 #include "log.h"
40 #include "io_stream.h"
41 #include "io_stream_memory.h"
42 #include "state.h"
43 #include "diskfull.h"
44 #include "mount.h"
45
46 static HWND gw_dialog = 0;
47 static HWND gw_url = 0;
48 static HWND gw_rate = 0;
49 static HWND gw_progress = 0;
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;
55 static HANDLE init_event;
56 static int max_bytes = 0;
57 static int is_local_install = 0;
58
59 int total_download_bytes = 0;
60 int total_download_bytes_sofar = 0;
61
62 static BOOL
63 dialog_cmd (HWND h, int id, HWND hwndctl, UINT code)
64 {
65 switch (id)
66 {
67 case IDCANCEL:
68 exit_setup (0);
69 }
70 return 0;
71 }
72
73 static BOOL CALLBACK
74 dialog_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
75 {
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);
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);
88 SetEvent (init_event);
89 return TRUE;
90 case WM_COMMAND:
91 return HANDLE_WM_COMMAND (h, wParam, lParam, dialog_cmd);
92 }
93 return FALSE;
94 }
95
96 static WINAPI DWORD
97 dialog (void *)
98 {
99 MSG m;
100 HWND local_gw_dialog =
101 CreateDialog (hinstance, MAKEINTRESOURCE (IDD_DLSTATUS),
102 0, dialog_proc);
103 ShowWindow (local_gw_dialog, SW_SHOWNORMAL);
104 UpdateWindow (local_gw_dialog);
105 while (GetMessage (&m, 0, 0, 0) > 0)
106 {
107 TranslateMessage (&m);
108 DispatchMessage (&m);
109 }
110 return 0;
111 }
112
113 static DWORD start_tics;
114
115 static void
116 init_dialog (char *url, int length)
117 {
118 if (is_local_install)
119 return;
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));
129 SendMessage (gw_pprogress, PBM_SETRANGE, 0, MAKELPARAM (0, 100));
130 SendMessage (gw_iprogress, PBM_SETRANGE, 0, MAKELPARAM (0, 100));
131 }
132 char *sl = url, *cp;
133 for (cp = url; *cp; cp++)
134 if (*cp == '/' || *cp == '\\' || *cp == ':')
135 sl = cp + 1;
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);
141 if (length > 0)
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 }
151 else
152 {
153 SetWindowText (gw_pprogress_text, " ");
154 SetWindowText (gw_iprogress_text, " ");
155 }
156 ShowWindow (gw_iprogress, (total_download_bytes > 0) ? SW_SHOW : SW_HIDE);
157 ShowWindow (gw_dialog, SW_SHOWNORMAL);
158 start_tics = GetTickCount ();
159 }
160
161
162 static void
163 progress (int bytes)
164 {
165 if (is_local_install)
166 return;
167 static char buf[100];
168 int kbps;
169 static unsigned int last_tics = 0;
170 DWORD tics = GetTickCount ();
171 if (tics == start_tics) // to prevent division by zero
172 return;
173 if (tics < last_tics + 200) // to prevent flickering updates
174 return;
175 last_tics = tics;
176
177 kbps = bytes / (tics - start_tics);
178 ShowWindow (gw_progress, (max_bytes > 0) ? SW_SHOW : SW_HIDE);
179 ShowWindow (gw_pprogress, (total_download_bytes > 0) ? SW_SHOW : SW_HIDE);
180 ShowWindow (gw_iprogress, (total_download_bytes > 0) ? SW_SHOW : SW_HIDE);
181 if (max_bytes > 100)
182 {
183 int perc = bytes / (max_bytes / 100);
184 SendMessage (gw_progress, PBM_SETPOS, (WPARAM) perc, 0);
185 sprintf (buf, "%3d %% (%dk/%dk) %d kb/s\n",
186 perc, bytes / 1000, max_bytes / 1000, kbps);
187 if (total_download_bytes > 0)
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 }
194 }
195 else
196 sprintf (buf, "%d %d kb/s\n", bytes, kbps);
197
198 SetWindowText (gw_rate, buf);
199 }
200
201 char *
202 get_url_to_string (char *_url)
203 {
204 log (LOG_BABBLE, "get_url_to_string %s", _url);
205 is_local_install = (source == IDC_SOURCE_CWD);
206 init_dialog (_url, 0);
207 NetIO *n = NetIO::open (_url);
208 if (!n || !n->ok ())
209 {
210 delete n;
211 log (LOG_BABBLE, "get_url_to_string failed!");
212 return 0;
213 }
214
215 if (n->file_size)
216 max_bytes = n->file_size;
217
218 io_stream_memory * membuf = new io_stream_memory ();
219
220 int total_bytes = 1; /* for the NUL terminator */
221 progress (0);
222 while (1)
223 {
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 */
232 break;
233 total_bytes += rlen;
234 progress (total_bytes);
235 }
236 else
237 break;
238 }
239
240 char *rv = (char *) malloc (total_bytes);
241 if (NULL == rv || membuf->seek (0, IO_SEEK_SET))
242 {
243 if (n)
244 delete n;
245 if (membuf)
246 delete membuf;
247 log (LOG_BABBLE, "get_url_to_string(): malloc failed for rv!");
248 return 0;
249 }
250
251 ssize_t rlen;
252 rlen = membuf->read (rv, total_bytes);
253 rv [total_bytes] = '\0';
254 if (n)
255 delete n;
256 if (membuf)
257 delete membuf;
258 return rv;
259 }
260
261 int
262 get_url_to_file (char *_url, char *_filename, int expected_length,
263 BOOL allow_ftp_auth)
264 {
265 log (LOG_BABBLE, "get_url_to_file %s %s", _url, _filename);
266 if (total_download_bytes > 0)
267 {
268 int df = diskfull (get_root_dir ());
269 SendMessage (gw_iprogress, PBM_SETPOS, (WPARAM) df, 0);
270 }
271 init_dialog (_url, expected_length);
272
273 remove (_filename); /* but ignore errors */
274
275 NetIO *n = NetIO::open (_url, allow_ftp_auth);
276 if (!n || !n->ok ())
277 {
278 delete n;
279 log (LOG_BABBLE, "get_url_to_file failed!");
280 return 1;
281 }
282
283 FILE *f = fopen (_filename, "wb");
284 if (!f)
285 {
286 const char *err = strerror (errno);
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 {
299 char buf[8192];
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
309 total_download_bytes_sofar += total_bytes;
310
311 fclose (f);
312 if (n)
313 delete n;
314
315 if (total_download_bytes > 0)
316 {
317 int df = diskfull (get_root_dir ());
318 SendMessage (gw_iprogress, PBM_SETPOS, (WPARAM) df, 0);
319 }
320
321 return 0;
322 }
323
324 void
325 dismiss_url_status_dialog ()
326 {
327 if (!is_local_install)
328 ShowWindow (gw_dialog, SW_HIDE);
329 }
This page took 0.105832 seconds and 4 git commands to generate.