4 * Copyright (c) 2000, 2001, Red Hat, Inc.
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.
11 * A copy of the GNU General Public License can be found at
14 * Written by DJ Delorie <dj@cygnus.com>
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 */
23 static const char *cvsid
=
40 #include "io_stream.h"
41 #include "io_stream_memory.h"
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;
59 int total_download_bytes
= 0;
60 int total_download_bytes_sofar
= 0;
63 dialog_cmd (HWND h
, int id
, HWND hwndctl
, UINT code
)
74 dialog_proc (HWND h
, UINT message
, WPARAM wParam
, LPARAM lParam
)
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
);
91 return HANDLE_WM_COMMAND (h
, wParam
, lParam
, dialog_cmd
);
100 HWND local_gw_dialog
=
101 CreateDialog (hinstance
, MAKEINTRESOURCE (IDD_DLSTATUS
),
103 ShowWindow (local_gw_dialog
, SW_SHOWNORMAL
);
104 UpdateWindow (local_gw_dialog
);
105 while (GetMessage (&m
, 0, 0, 0) > 0)
107 TranslateMessage (&m
);
108 DispatchMessage (&m
);
113 static DWORD start_tics
;
116 init_dialog (char *url
, int length
)
118 if (is_local_install
)
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));
133 for (cp
= url
; *cp
; cp
++)
134 if (*cp
== '/' || *cp
== '\\' || *cp
== ':')
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
);
142 SetWindowText (gw_progress_text
, "Package");
144 SetWindowText (gw_progress_text
, " ");
145 ShowWindow (gw_pprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
146 if (total_download_bytes
> 0)
148 SetWindowText (gw_pprogress_text
, "Total");
149 SetWindowText (gw_iprogress_text
, "Disk");
153 SetWindowText (gw_pprogress_text
, " ");
154 SetWindowText (gw_iprogress_text
, " ");
156 ShowWindow (gw_iprogress
, (total_download_bytes
> 0) ? SW_SHOW
: SW_HIDE
);
157 ShowWindow (gw_dialog
, SW_SHOWNORMAL
);
158 start_tics
= GetTickCount ();
165 if (is_local_install
)
167 static char buf
[100];
169 static unsigned int last_tics
= 0;
170 DWORD tics
= GetTickCount ();
171 if (tics
== start_tics
) // to prevent division by zero
173 if (tics
< last_tics
+ 200) // to prevent flickering updates
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
);
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)
190 (total_download_bytes_sofar
+
191 bytes
) / (total_download_bytes
/ 100);
192 SendMessage (gw_pprogress
, PBM_SETPOS
, (WPARAM
) totalperc
, 0);
196 sprintf (buf
, "%d %d kb/s\n", bytes
, kbps
);
198 SetWindowText (gw_rate
, buf
);
202 get_url_to_string (char *_url
)
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
);
211 log (LOG_BABBLE
, "get_url_to_string failed!");
216 max_bytes
= n
->file_size
;
218 io_stream_memory
* membuf
= new io_stream_memory ();
220 int total_bytes
= 1; /* for the NUL terminator */
226 rlen
= n
->read (buf
, 2048);
229 wlen
= membuf
->write (buf
, rlen
);
231 /* FIXME: Show an error message */
234 progress (total_bytes
);
240 char *rv
= (char *) malloc (total_bytes
);
241 if (NULL
== rv
|| membuf
->seek (0, IO_SEEK_SET
))
247 log (LOG_BABBLE
, "get_url_to_string(): malloc failed for rv!");
252 rlen
= membuf
->read (rv
, total_bytes
);
253 rv
[total_bytes
] = '\0';
262 get_url_to_file (char *_url
, char *_filename
, int expected_length
,
265 log (LOG_BABBLE
, "get_url_to_file %s %s", _url
, _filename
);
266 if (total_download_bytes
> 0)
268 int df
= diskfull (get_root_dir ());
269 SendMessage (gw_iprogress
, PBM_SETPOS
, (WPARAM
) df
, 0);
271 init_dialog (_url
, expected_length
);
273 remove (_filename
); /* but ignore errors */
275 NetIO
*n
= NetIO::open (_url
, allow_ftp_auth
);
279 log (LOG_BABBLE
, "get_url_to_file failed!");
283 FILE *f
= fopen (_filename
, "wb");
286 const char *err
= strerror (errno
);
288 err
= "(unknown error)";
289 fatal (IDS_ERR_OPEN_WRITE
, _filename
, err
);
293 max_bytes
= n
->file_size
;
301 count
= n
->read (buf
, sizeof (buf
));
304 fwrite (buf
, 1, count
, f
);
305 total_bytes
+= count
;
306 progress (total_bytes
);
309 total_download_bytes_sofar
+= total_bytes
;
315 if (total_download_bytes
> 0)
317 int df
= diskfull (get_root_dir ());
318 SendMessage (gw_iprogress
, PBM_SETPOS
, (WPARAM
) df
, 0);
325 dismiss_url_status_dialog ()
327 if (!is_local_install
)
328 ShowWindow (gw_dialog
, SW_HIDE
);