]>
Commit | Line | Data |
---|---|---|
23c9e63c DD |
1 | /* |
2 | * Copyright (c) 2000, Red Hat, Inc. | |
3 | * | |
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. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by DJ Delorie <dj@cygnus.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* The purpose of this file is to get the list of mirror sites and ask | |
17 | the user which mirror site they want to download from. */ | |
18 | ||
b24c88b3 RC |
19 | #if 0 |
20 | static const char *cvsid = | |
21 | "\n%%% $Id$\n"; | |
22 | #endif | |
8507f105 | 23 | |
23c9e63c DD |
24 | #include "win32.h" |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
ab57ceaa | 28 | #include <process.h> |
23c9e63c DD |
29 | |
30 | #include "dialog.h" | |
31 | #include "resource.h" | |
32 | #include "state.h" | |
33 | #include "geturl.h" | |
34 | #include "msg.h" | |
89b1a15b | 35 | #include "log.h" |
b24c88b3 | 36 | #include "io_stream.h" |
de6a1a64 | 37 | #include "site.h" |
23c9e63c DD |
38 | |
39 | #include "port.h" | |
40 | ||
ab57ceaa RC |
41 | #include "site.h" |
42 | #include "propsheet.h" | |
43 | ||
44 | #include "threebar.h" | |
45 | extern ThreeBarProgressPage Progress; | |
46 | ||
3c054baf RC |
47 | list < site_list_type, String, String::casecompare > site_list; |
48 | list < site_list_type, String, String::casecompare > all_site_list; | |
23c9e63c | 49 | |
de6a1a64 | 50 | void |
3c054baf | 51 | site_list_type::init (String const &newurl) |
de6a1a64 | 52 | { |
3c054baf | 53 | url = newurl; |
de6a1a64 | 54 | |
3c054baf RC |
55 | char *dots = newurl.cstr(); |
56 | char *dot = strchr (dots, '.'); | |
57 | if (dot) | |
58 | { | |
59 | dot = strchr (dot, '/'); | |
60 | if (dot) | |
61 | *dot = 0; | |
62 | } | |
63 | displayed_url = String (dots); | |
64 | ||
65 | ||
66 | dot = dots + strlen (dots); | |
51ebb760 RC |
67 | char *dpsave, *dp = new char[2 * newurl.size() + 3]; |
68 | dpsave = dp; | |
3c054baf | 69 | while (dot != dots) |
de6a1a64 RC |
70 | { |
71 | if (*dot == '.' || *dot == '/') | |
72 | { | |
73 | char *sp; | |
74 | if (dot[3] == 0) | |
75 | *dp++ = '~'; /* sort .com/.edu/.org together */ | |
76 | for (sp = dot + 1; *sp && *sp != '.' && *sp != '/';) | |
77 | *dp++ = *sp++; | |
78 | *dp++ = ' '; | |
79 | } | |
3c054baf | 80 | --dot; |
de6a1a64 RC |
81 | } |
82 | *dp++ = ' '; | |
3c054baf RC |
83 | strcpy (dp, dots); |
84 | delete[] dots; | |
85 | key = String (dp); | |
51ebb760 | 86 | delete[] dpsave; |
de6a1a64 RC |
87 | } |
88 | ||
3c054baf | 89 | site_list_type::site_list_type (String const &newurl) |
de6a1a64 RC |
90 | { |
91 | init (newurl); | |
92 | } | |
93 | ||
23c9e63c | 94 | static void |
b7301c43 | 95 | save_dialog (HWND h) |
23c9e63c | 96 | { |
b7301c43 RC |
97 | // Remove anything that was previously in the selected site list. |
98 | while (site_list.number () > 0) | |
de6a1a64 | 99 | { |
b7301c43 RC |
100 | // we don't delete the object because it's stored in the all_site_list. |
101 | site_list.removebyindex (1); | |
de6a1a64 | 102 | } |
23c9e63c | 103 | |
23c9e63c | 104 | HWND listbox = GetDlgItem (h, IDC_URL_LIST); |
de6a1a64 RC |
105 | int sel_count = SendMessage (listbox, LB_GETSELCOUNT, 0, 0); |
106 | if (sel_count > 0) | |
23c9e63c | 107 | { |
de6a1a64 RC |
108 | int sel_buffer[sel_count]; |
109 | int sel_count2 = SendMessage (listbox, LB_GETSELITEMS, sel_count, | |
110 | (LPARAM) sel_buffer); | |
111 | if (sel_count != sel_count2) | |
112 | { | |
113 | NEXT (IDD_SITE); | |
114 | } | |
115 | for (int n = 0; n < sel_count; n++) | |
116 | { | |
117 | int mirror = | |
118 | SendMessage (listbox, LB_GETITEMDATA, sel_buffer[n], 0); | |
b7301c43 | 119 | site_list.registerbyobject (*all_site_list[mirror]); |
de6a1a64 | 120 | } |
23c9e63c | 121 | } |
23c9e63c DD |
122 | } |
123 | ||
ed3e8b9b DD |
124 | void |
125 | save_site_url () | |
126 | { | |
b24c88b3 | 127 | io_stream *f = io_stream::open ("cygfile:///etc/setup/last-mirror", "wb"); |
3c054baf | 128 | if (f) |
b24c88b3 | 129 | { |
3c054baf RC |
130 | for (size_t n = 1; n <= site_list.number (); n++) |
131 | f->write ((site_list[n]->url + "\n").cstr_oneuse(), site_list[n]->url.size() + 1); | |
132 | delete f; | |
b24c88b3 | 133 | } |
ed3e8b9b DD |
134 | } |
135 | ||
23c9e63c | 136 | static int |
ab57ceaa | 137 | get_site_list (HINSTANCE h, HWND owner) |
23c9e63c DD |
138 | { |
139 | char mirror_url[1000]; | |
ab57ceaa | 140 | |
ed3e8b9b | 141 | if (LoadString (h, IDS_MIRROR_LST, mirror_url, sizeof (mirror_url)) <= 0) |
23c9e63c | 142 | return 1; |
3c054baf RC |
143 | char *bol, *eol, *nl, *theString; |
144 | { | |
145 | String mirrors = get_url_to_string (mirror_url, owner); | |
146 | if (!mirrors.size()) | |
23c9e63c DD |
147 | return 1; |
148 | ||
3c054baf | 149 | nl = theString = mirrors.cstr();} |
23c9e63c | 150 | |
23c9e63c DD |
151 | while (*nl) |
152 | { | |
153 | bol = nl; | |
b24c88b3 | 154 | for (eol = bol; *eol && *eol != '\n'; eol++); |
23c9e63c | 155 | if (*eol) |
b24c88b3 | 156 | nl = eol + 1; |
23c9e63c DD |
157 | else |
158 | nl = eol; | |
159 | while (eol > bol && eol[-1] == '\r') | |
160 | eol--; | |
161 | *eol = 0; | |
162 | if (bol[0] != '#' && bol[0] > ' ') | |
163 | { | |
b5b282c4 | 164 | char *semi = strchr (bol, ';'); |
23c9e63c DD |
165 | if (semi) |
166 | *semi = 0; | |
de6a1a64 RC |
167 | site_list_type *newsite = new site_list_type (bol); |
168 | site_list_type & listobj = | |
169 | all_site_list.registerbyobject (*newsite); | |
170 | if (&listobj != newsite) | |
171 | /* That site was already registered */ | |
172 | delete newsite; | |
23c9e63c DD |
173 | } |
174 | } | |
3c054baf | 175 | delete[] theString; |
b5b282c4 | 176 | |
23c9e63c DD |
177 | return 0; |
178 | } | |
179 | ||
6fbc690d CF |
180 | /* List of machines that should not be used by default when saved |
181 | in "last-mirror". */ | |
182 | #define NOSAVE1 "ftp://sources.redhat.com/" | |
183 | #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1) | |
184 | #define NOSAVE2 "ftp://sourceware.cygnus.com/" | |
185 | #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1) | |
186 | #define NOSAVE3 "ftp://gcc.gnu.org/" | |
187 | #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1) | |
188 | ||
ed3e8b9b | 189 | static void |
de6a1a64 | 190 | get_saved_sites () |
ed3e8b9b | 191 | { |
b24c88b3 | 192 | io_stream *f = io_stream::open ("cygfile:///etc/setup/last-mirror", "rt"); |
ed3e8b9b DD |
193 | if (!f) |
194 | return; | |
195 | ||
196 | char site[1000]; | |
de6a1a64 RC |
197 | char *fg_ret; |
198 | while ((fg_ret = f->gets (site, 1000))) | |
199 | { | |
ed3e8b9b | 200 | |
de6a1a64 RC |
201 | char *eos = site + strlen (site) - 1; |
202 | while (eos >= site && (*eos == '\n' || *eos == '\r')) | |
203 | *eos-- = '\0'; | |
ed3e8b9b | 204 | |
de6a1a64 RC |
205 | if (eos < site) |
206 | continue; | |
ed3e8b9b | 207 | |
3c054baf RC |
208 | String tempKey = site_list_type (String(site)).key; |
209 | if (!all_site_list.getbykey (tempKey)) | |
de6a1a64 RC |
210 | { |
211 | /* Don't default to certain machines ever since they suffer | |
212 | from bandwidth limitations. */ | |
213 | if (strnicmp (site, NOSAVE1, NOSAVE1_LEN) == 0 | |
214 | || strnicmp (site, NOSAVE2, NOSAVE2_LEN) == 0 | |
215 | || strnicmp (site, NOSAVE3, NOSAVE3_LEN) == 0) | |
216 | return; | |
217 | site_list_type *newsite = new site_list_type (site); | |
218 | site_list_type & listobj = | |
219 | all_site_list.registerbyobject (*newsite); | |
220 | if (&listobj != newsite) | |
221 | /* That site was already registered - shouldn't happen, but safety first */ | |
222 | delete newsite; | |
223 | } | |
224 | /* TODO: make a site_type method to create a serach key on-the-fly from a | |
225 | URL | |
226 | */ | |
3c054baf RC |
227 | // Was it an allowed URL? |
228 | site_list_type *tempSite; | |
229 | if ((tempSite = all_site_list.getbykey (tempKey))) | |
230 | site_list.registerbyobject (*tempSite); | |
ed3e8b9b | 231 | } |
de6a1a64 | 232 | delete f; |
ed3e8b9b | 233 | |
ed3e8b9b DD |
234 | } |
235 | ||
45e01f23 | 236 | static DWORD WINAPI |
ab57ceaa | 237 | do_download_site_info_thread (void *p) |
23c9e63c | 238 | { |
ab57ceaa RC |
239 | HANDLE *context; |
240 | HINSTANCE hinst; | |
241 | HWND h; | |
242 | context = (HANDLE *) p; | |
243 | ||
244 | hinst = (HINSTANCE) (context[0]); | |
245 | h = (HWND) (context[1]); | |
23c9e63c | 246 | |
3c054baf RC |
247 | if (all_site_list.number () == 0 |
248 | && get_site_list (hinst, h)) | |
ab57ceaa RC |
249 | { |
250 | // Error: Couldn't download the site info. Go back to the Net setup page. | |
b7301c43 RC |
251 | MessageBox (h, TEXT ("Can't get list of download sites.\n\ |
252 | Make sure your network settings are corect and try again."), NULL, MB_OK); | |
ab57ceaa RC |
253 | |
254 | // Tell the progress page that we're done downloading | |
255 | Progress.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE, 0, | |
256 | IDD_NET); | |
257 | ||
ab57ceaa | 258 | } |
3c054baf | 259 | else |
ab57ceaa | 260 | // Everything worked, go to the site select page |
ab57ceaa RC |
261 | // Tell the progress page that we're done downloading |
262 | Progress.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE, 0, IDD_SITE); | |
263 | ||
45e01f23 | 264 | ExitThread(0); |
ab57ceaa RC |
265 | } |
266 | ||
267 | static HANDLE context[2]; | |
268 | ||
269 | void | |
270 | do_download_site_info (HINSTANCE hinst, HWND owner) | |
271 | { | |
272 | ||
273 | context[0] = hinst; | |
274 | context[1] = owner; | |
275 | ||
45e01f23 RC |
276 | DWORD threadID; |
277 | CreateThread (NULL, 0, do_download_site_info_thread, context, 0, &threadID); | |
ab57ceaa RC |
278 | } |
279 | ||
b7301c43 | 280 | bool SitePage::Create () |
ab57ceaa | 281 | { |
b7301c43 | 282 | return PropertyPage::Create (IDD_SITE); |
ab57ceaa RC |
283 | } |
284 | ||
285 | void | |
286 | SitePage::OnInit () | |
287 | { | |
de6a1a64 | 288 | get_saved_sites (); |
ab57ceaa | 289 | } |
89b1a15b | 290 | |
ab57ceaa RC |
291 | long |
292 | SitePage::OnNext () | |
293 | { | |
294 | HWND h = GetHWND (); | |
295 | ||
296 | save_dialog (h); | |
b7301c43 | 297 | save_site_url (); |
ab57ceaa | 298 | |
b7301c43 RC |
299 | // Log all the selected URLs from the list. |
300 | for (size_t n = 1; n <= site_list.number (); n++) | |
1ac649ed | 301 | log (LOG_PLAIN, String ("site: ") + site_list[n]->url); |
ab57ceaa | 302 | |
b7301c43 RC |
303 | Progress.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD); |
304 | return IDD_INSTATUS; | |
ab57ceaa RC |
305 | |
306 | return 0; | |
307 | } | |
308 | ||
309 | long | |
310 | SitePage::OnBack () | |
311 | { | |
312 | HWND h = GetHWND (); | |
313 | ||
314 | save_dialog (h); | |
b7301c43 RC |
315 | |
316 | // Go back to the net connection type page | |
ab57ceaa | 317 | return 0; |
23c9e63c | 318 | } |
b7301c43 RC |
319 | |
320 | void | |
321 | SitePage::OnActivate () | |
322 | { | |
323 | // Fill the list box with all known sites. | |
324 | PopulateListBox (); | |
325 | ||
3c054baf RC |
326 | // Load the user URL box with nothing - it is in the list already. |
327 | eset (GetHWND (), IDC_EDIT_USER_URL, ""); | |
b7301c43 RC |
328 | |
329 | // Get the enabled/disabled states of the controls set accordingly. | |
330 | CheckControlsAndDisableAccordingly (); | |
331 | } | |
332 | ||
333 | void | |
334 | SitePage::CheckControlsAndDisableAccordingly () const | |
335 | { | |
336 | DWORD ButtonFlags = PSWIZB_BACK; | |
337 | ||
338 | // Check that at least one download site is selected. | |
339 | if (SendMessage (GetDlgItem (IDC_URL_LIST), LB_GETSELCOUNT, 0, 0) > 0) | |
340 | { | |
3548fbc3 | 341 | // At least one site selected, enable "Next". |
b7301c43 RC |
342 | ButtonFlags |= PSWIZB_NEXT; |
343 | } | |
344 | GetOwner ()->SetButtons (ButtonFlags); | |
345 | } | |
346 | ||
347 | void | |
348 | SitePage::PopulateListBox () | |
349 | { | |
350 | int j; | |
351 | HWND listbox = GetDlgItem (IDC_URL_LIST); | |
352 | ||
353 | // Populate the list box with the URLs. | |
354 | SendMessage (listbox, LB_RESETCONTENT, 0, 0); | |
355 | for (size_t i = 1; i <= all_site_list.number (); i++) | |
356 | { | |
357 | j = SendMessage (listbox, LB_ADDSTRING, 0, | |
3c054baf | 358 | (LPARAM) all_site_list[i]->displayed_url.cstr_oneuse()); |
b7301c43 RC |
359 | SendMessage (listbox, LB_SETITEMDATA, j, i); |
360 | } | |
361 | ||
362 | // Select the selected ones. | |
363 | for (size_t n = 1; n <= site_list.number (); n++) | |
364 | { | |
365 | int index = SendMessage (listbox, LB_FINDSTRING, (WPARAM) - 1, | |
3c054baf | 366 | (LPARAM) site_list[n]->displayed_url.cstr_oneuse()); |
b7301c43 RC |
367 | if (index != LB_ERR) |
368 | { | |
369 | // Highlight the selected item | |
370 | SendMessage (listbox, LB_SELITEMRANGE, TRUE, (index << 16) | index); | |
371 | // Make sure it's fully visible | |
372 | SendMessage (listbox, LB_SETCARETINDEX, index, FALSE); | |
373 | } | |
374 | } | |
375 | } | |
376 | ||
377 | bool SitePage::OnMessageCmd (int id, HWND hwndctl, UINT code) | |
378 | { | |
379 | switch (id) | |
380 | { | |
381 | case IDC_EDIT_USER_URL: | |
382 | { | |
3c054baf | 383 | // FIXME: Make Enter here cause an ADD, not a NEXT. |
b7301c43 RC |
384 | break; |
385 | } | |
386 | case IDC_URL_LIST: | |
387 | { | |
388 | if (code == LBN_SELCHANGE) | |
389 | { | |
390 | CheckControlsAndDisableAccordingly (); | |
3c054baf | 391 | save_dialog (GetHWND ()); |
b7301c43 RC |
392 | } |
393 | break; | |
394 | } | |
395 | case IDC_BUTTON_ADD_URL: | |
396 | { | |
397 | if (code == BN_CLICKED) | |
398 | { | |
399 | // User pushed the Add button. | |
3c054baf RC |
400 | String other_url = egetString (GetHWND (), IDC_EDIT_USER_URL); |
401 | if (other_url.size()) | |
402 | { | |
b7301c43 RC |
403 | site_list_type * |
404 | newsite = | |
405 | new | |
406 | site_list_type (other_url); | |
407 | site_list_type & listobj = | |
408 | all_site_list.registerbyobject (*newsite); | |
409 | if (&listobj != newsite) | |
410 | { | |
411 | // That site was already registered | |
412 | delete | |
413 | newsite; | |
414 | } | |
415 | else | |
416 | { | |
417 | // Log the adding of this new URL. | |
1ac649ed | 418 | log (LOG_BABBLE, String ("Adding site: ") + other_url); |
b7301c43 RC |
419 | } |
420 | ||
421 | // Assume the user wants to use it and select it for him. | |
422 | site_list.registerbyobject (listobj); | |
423 | ||
424 | // Update the list box. | |
425 | PopulateListBox (); | |
3548fbc3 RC |
426 | // And allow the user to continue |
427 | CheckControlsAndDisableAccordingly (); | |
3c054baf RC |
428 | eset (GetHWND (), IDC_EDIT_USER_URL, ""); |
429 | } | |
b7301c43 RC |
430 | } |
431 | break; | |
432 | } | |
433 | default: | |
434 | // Wasn't recognized or handled. | |
435 | return false; | |
436 | } | |
437 | ||
438 | // Was handled since we never got to default above. | |
439 | return true; | |
440 | } |