2 * Copyright (c) 2000, 2001, 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 */
21 static const char *cvsid
=
42 static HWND gw_dialog
= 0;
43 static HWND gw_url
= 0;
44 static HWND gw_rate
= 0;
45 static HWND gw_progress
= 0;
46 static HWND gw_pprogress
= 0;
47 static HWND gw_iprogress
= 0;
48 static HWND gw_progress_text
= 0;
49 static HWND gw_pprogress_text
= 0;
50 static HWND gw_iprogress_text
= 0;
51 static HANDLE init_event
;
52 static int max_bytes
= 0;
53 static int is_local_install
= 0;
55 int total_download_bytes
= 0;
56 int total_download_bytes_sofar
= 0;
59 dialog_cmd (HWND h
, int id
, HWND hwndctl
, UINT code
)
70 dialog_proc (HWND h
, UINT message
, WPARAM wParam
, LPARAM lParam
)
76 gw_url
= GetDlgItem (h
, IDC_DLS_URL
);
77 gw_rate
= GetDlgItem (h
, IDC_DLS_RATE
);
78 gw_progress
= GetDlgItem (h
, IDC_DLS_PROGRESS
);
79 gw_pprogress
= GetDlgItem (h
, IDC_DLS_PPROGRESS
);
80 gw_iprogress
= GetDlgItem (h
, IDC_DLS_IPROGRESS
);
81 gw_progress_text
= GetDlgItem (h
, IDC_DLS_PROGRESS_TEXT
);
82 gw_pprogress_text
= GetDlgItem (h
, IDC_DLS_PPROGRESS_TEXT
);
83 gw_iprogress_text
= GetDlgItem (h
, IDC_DLS_IPROGRESS_TEXT
);
84 SetEvent (init_event
);
87 return HANDLE_WM_COMMAND (h
, wParam
, lParam
, dialog_cmd
);
96 HWND local_gw_dialog
=
97 CreateDialog (hinstance
, MAKEINTRESOURCE (IDD_DLSTATUS
),
99 ShowWindow (local_gw_dialog
, SW_SHOWNORMAL
);
100 UpdateWindow (local_gw_dialog
);
101 while (GetMessage (&m
, 0, 0, 0) > 0)
103 TranslateMessage (&m
);
104 DispatchMessage (&m
);
109 static DWORD start_tics
;
112 init_dialog (char *url
, int length
)
114 if (is_local_install
)
120 init_event
= CreateEvent (0, 0, 0, 0);
121 thread
= CreateThread (0, 0, dialog
, 0, 0, &tid
);
122 WaitForSingleObject (init_event
, 1000);
123 CloseHandle (init_event
);
124 SendMessage (gw_progress
, PBM_SETRANGE
, 0, MAKELPARAM (0, 100));
125 SendMessage (gw_pprogress
, PBM_SETRANGE
, 0, MAKELPARAM (0, 100));
126 SendMessage (gw_iprogress
, PBM_SETRANGE
, 0, MAKELPARAM (0, 100));
129 for (cp
= url
; *cp
; cp
++)
130 if (*cp
== '/' || *cp
== '\\' || *cp
== ':')
133 SetWindowText (gw_url
, sl
);
134 SetWindowText (gw_rate
, "Connecting...");
135 SendMessage (gw_progress
, PBM_SETPOS
, (WPARAM
) 0, 0);
136 ShowWindow (gw_progress
, (length
> 0) ? SW_SHOW
: SW_HIDE
);
138 SetWindowText (gw_progress_text
, "Package");
140 SetWindowText (gw_progress_text
, " ");
141 ShowWindow (gw_pprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
142 if (total_download_bytes
> 0)
144 SetWindowText (gw_pprogress_text
, "Total");
145 SetWindowText (gw_iprogress_text
, "Disk");
149 SetWindowText (gw_pprogress_text
, " ");
150 SetWindowText (gw_iprogress_text
, " ");
152 ShowWindow (gw_iprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
153 ShowWindow (gw_dialog
, SW_SHOWNORMAL
);
154 start_tics
= GetTickCount ();
161 if (is_local_install
)
163 static char buf
[100];
165 static unsigned int last_tics
= 0;
166 DWORD tics
= GetTickCount ();
167 if (tics
== start_tics
) // to prevent division by zero
169 if (tics
< last_tics
+ 200) // to prevent flickering updates
173 kbps
= bytes
/ (tics
- start_tics
);
174 ShowWindow (gw_progress
, (max_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
175 ShowWindow (gw_pprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
176 ShowWindow (gw_iprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
179 int perc
= bytes
/ (max_bytes
/ 100);
180 SendMessage (gw_progress
, PBM_SETPOS
, (WPARAM
) perc
, 0);
181 sprintf (buf
, "%3d %% (%dk/%dk) %d kb/s\n",
182 perc
, bytes
/ 1000, max_bytes
/ 1000, kbps
);
183 if (total_download_bytes
> 0)
186 (total_download_bytes_sofar
+
187 bytes
) / (total_download_bytes
/ 100);
188 SendMessage (gw_pprogress
, PBM_SETPOS
, (WPARAM
) totalperc
, 0);
192 sprintf (buf
, "%d %d kb/s\n", bytes
, kbps
);
194 SetWindowText (gw_rate
, buf
);
205 get_url_to_string (char *_url
)
207 log (LOG_BABBLE
, "get_url_to_string %s", _url
);
208 is_local_install
= (source
== IDC_SOURCE_CWD
);
209 init_dialog (_url
, 0);
210 NetIO
*n
= NetIO::open (_url
);
214 log (LOG_BABBLE
, "get_url_to_string failed!");
219 max_bytes
= n
->file_size
;
222 GUBuf
**nextp
= &bufs
;
223 int total_bytes
= 1; /* for the NUL */
227 GUBuf
*b
= new GUBuf
;
232 b
->count
= n
->read (b
->buf
, sizeof (b
->buf
));
235 total_bytes
+= b
->count
;
236 progress (total_bytes
);
239 char *rv
= (char *) malloc (total_bytes
);
244 log (LOG_BABBLE
, "get_url_to_string(): malloc failed for rv!");
248 while (bufs
&& bufs
->count
> 0)
250 GUBuf
*tmp
= bufs
->next
;
251 memcpy (rvp
, bufs
->buf
, bufs
->count
);
263 get_url_to_file (char *_url
, char *_filename
, int expected_length
,
266 log (LOG_BABBLE
, "get_url_to_file %s %s", _url
, _filename
);
267 if (total_download_bytes
> 0)
269 int df
= diskfull (get_root_dir ());
270 SendMessage (gw_iprogress
, PBM_SETPOS
, (WPARAM
) df
, 0);
272 init_dialog (_url
, expected_length
);
274 remove (_filename
); /* but ignore errors */
276 NetIO
*n
= NetIO::open (_url
, allow_ftp_auth
);
280 log (LOG_BABBLE
, "get_url_to_file failed!");
284 FILE *f
= fopen (_filename
, "wb");
287 const char *err
= strerror (errno
);
289 err
= "(unknown error)";
290 fatal (IDS_ERR_OPEN_WRITE
, _filename
, err
);
294 max_bytes
= n
->file_size
;
302 count
= n
->read (buf
, sizeof (buf
));
305 fwrite (buf
, 1, count
, f
);
306 total_bytes
+= count
;
307 progress (total_bytes
);
310 total_download_bytes_sofar
+= total_bytes
;
316 if (total_download_bytes
> 0)
318 int df
= diskfull (get_root_dir ());
319 SendMessage (gw_iprogress
, PBM_SETPOS
, (WPARAM
) df
, 0);
326 dismiss_url_status_dialog ()
328 if (!is_local_install
)
329 ShowWindow (gw_dialog
, SW_HIDE
);