2 * Copyright (c) 2000, Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by DJ Delorie <dj@cygnus.com>
16 /* The purpose of this file is to act as a pretty interface to
17 netio.cc. We add a progress dialog and some convenience functions
18 (like collect to string or file */
20 static char *cvsid
= "\n%%% $Id$\n";
38 static int is_showing
= 0;
39 static HWND gw_dialog
= 0;
40 static HWND gw_url
= 0;
41 static HWND gw_rate
= 0;
42 static HWND gw_progress
= 0;
43 static HWND gw_pprogress
= 0;
44 static HWND gw_iprogress
= 0;
45 static HWND gw_progress_text
= 0;
46 static HWND gw_pprogress_text
= 0;
47 static HWND gw_iprogress_text
= 0;
48 static HANDLE init_event
;
49 static int max_bytes
= 0;
51 int total_download_bytes
= 0;
52 int total_download_bytes_sofar
= 0;
55 dialog_cmd (HWND h
, int id
, HWND hwndctl
, UINT code
)
65 dialog_proc (HWND h
, UINT message
, WPARAM wParam
, LPARAM lParam
)
73 gw_url
= GetDlgItem (h
, IDC_DLS_URL
);
74 gw_rate
= GetDlgItem (h
, IDC_DLS_RATE
);
75 gw_progress
= GetDlgItem (h
, IDC_DLS_PROGRESS
);
76 gw_pprogress
= GetDlgItem (h
, IDC_DLS_PPROGRESS
);
77 gw_iprogress
= GetDlgItem (h
, IDC_DLS_IPROGRESS
);
78 gw_progress_text
= GetDlgItem (h
, IDC_DLS_PROGRESS_TEXT
);
79 gw_pprogress_text
= GetDlgItem (h
, IDC_DLS_PPROGRESS_TEXT
);
80 gw_iprogress_text
= GetDlgItem (h
, IDC_DLS_IPROGRESS_TEXT
);
81 SetEvent (init_event
);
84 return HANDLE_WM_COMMAND (h
, wParam
, lParam
, dialog_cmd
);
94 HWND gw_dialog
= CreateDialog (hinstance
, MAKEINTRESOURCE (IDD_DLSTATUS
),
96 ShowWindow (gw_dialog
, SW_SHOWNORMAL
);
97 UpdateWindow (gw_dialog
);
98 while (GetMessage (&m
, 0, 0, 0) > 0) {
99 TranslateMessage (&m
);
100 DispatchMessage (&m
);
104 static DWORD start_tics
;
107 init_dialog (char *url
, int length
)
113 init_event
= CreateEvent (0, 0, 0, 0);
114 thread
= CreateThread (0, 0, dialog
, 0, 0, &tid
);
115 WaitForSingleObject (init_event
, 1000);
116 CloseHandle (init_event
);
117 SendMessage (gw_progress
, PBM_SETRANGE
, 0, MAKELPARAM (0, 100));
118 SendMessage (gw_pprogress
, PBM_SETRANGE
, 0, MAKELPARAM (0, 100));
119 SendMessage (gw_iprogress
, PBM_SETRANGE
, 0, MAKELPARAM (0, 100));
123 for (cp
=url
; *cp
; cp
++)
124 if (*cp
== '/' || *cp
== '\\' || *cp
== ':')
127 SetWindowText (gw_url
, sl
);
128 SetWindowText (gw_rate
, "Connecting...");
129 SendMessage (gw_progress
, PBM_SETPOS
, (WPARAM
) 0, 0);
130 ShowWindow (gw_progress
, (length
> 0) ? SW_SHOW
: SW_HIDE
);
132 SetWindowText (gw_progress_text
, "Package");
134 SetWindowText (gw_progress_text
, " ");
135 ShowWindow (gw_pprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
136 if (total_download_bytes
> 0)
138 SetWindowText (gw_pprogress_text
, "Total");
139 SetWindowText (gw_iprogress_text
, "Disk");
143 SetWindowText (gw_pprogress_text
, " ");
144 SetWindowText (gw_iprogress_text
, " ");
146 ShowWindow (gw_iprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
147 ShowWindow (gw_dialog
, SW_SHOWNORMAL
);
150 SetForegroundWindow (gw_dialog
);
153 start_tics
= GetTickCount ();
160 static char buf
[100];
162 static int last_tics
= 0;
163 DWORD tics
= GetTickCount ();
164 if (tics
== start_tics
) // to prevent division by zero
166 if (tics
< last_tics
+ 200) // to prevent flickering updates
170 kbps
= bytes
/ (tics
- start_tics
);
171 ShowWindow (gw_progress
, (max_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
172 ShowWindow (gw_pprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
173 ShowWindow (gw_iprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
176 int perc
= bytes
/ (max_bytes
/ 100);
177 SendMessage (gw_progress
, PBM_SETPOS
, (WPARAM
) perc
, 0);
178 sprintf (buf
, "%3d %% (%dk/%dk) %d kb/s\n",
179 perc
, bytes
/1000, max_bytes
/1000, kbps
);
180 if (total_download_bytes
> 0)
182 int totalperc
= (total_download_bytes_sofar
+ bytes
) / (
183 total_download_bytes
/ 100);
184 SendMessage (gw_pprogress
, PBM_SETPOS
, (WPARAM
) totalperc
, 0);
188 sprintf (buf
, "%d %d kb/s\n", bytes
, kbps
);
190 SetWindowText (gw_rate
, buf
);
200 get_url_to_string (char *_url
)
202 log (LOG_BABBLE
, "get_url_to_string %s", _url
);
203 init_dialog (_url
, 0);
204 NetIO
*n
= NetIO::open (_url
);
208 log (LOG_BABBLE
, "get_url_to_string failed!");
213 max_bytes
= n
->file_size
;
216 GUBuf
**nextp
= &bufs
;
217 int total_bytes
= 1; /* for the NUL */
221 GUBuf
*b
= new GUBuf
;
226 b
->count
= n
->read (b
->buf
, sizeof (b
->buf
));
229 total_bytes
+= b
->count
;
230 progress (total_bytes
);
233 char *rv
= (char *) malloc (total_bytes
);
235 while (bufs
&& bufs
->count
> 0)
237 GUBuf
*tmp
= bufs
->next
;
238 memcpy (rvp
, bufs
->buf
, bufs
->count
);
248 get_url_to_file (char *_url
, char *_filename
, int expected_length
)
250 log (LOG_BABBLE
, "get_url_to_file %s %s", _url
, _filename
);
251 if (total_download_bytes
> 0)
253 int df
= diskfull (root_dir
);
254 SendMessage (gw_iprogress
, PBM_SETPOS
, (WPARAM
) df
, 0);
256 init_dialog (_url
, expected_length
);
258 remove (_filename
); /* but ignore errors */
260 NetIO
*n
= NetIO::open (_url
);
264 log (LOG_BABBLE
, "get_url_to_file failed!");
268 FILE *f
= fopen (_filename
, "wb");
271 char *err
= strerror (errno
);
273 err
= "(unknown error)";
274 fatal (IDS_ERR_OPEN_WRITE
, _filename
, err
);
278 max_bytes
= n
->file_size
;
286 count
= n
->read (buf
, sizeof (buf
));
289 fwrite (buf
, 1, count
, f
);
290 total_bytes
+= count
;
291 progress (total_bytes
);
294 total_download_bytes_sofar
+= total_bytes
;
298 if (total_download_bytes
> 0)
300 int df
= diskfull (root_dir
);
301 SendMessage (gw_iprogress
, PBM_SETPOS
, (WPARAM
) df
, 0);
308 dismiss_url_status_dialog ()
310 ShowWindow (gw_dialog
, SW_HIDE
);