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 get the list of mirror sites and ask
17 the user which mirror site they want to download from. */
20 static const char *cvsid
=
37 #include "io_stream.h"
43 #include "propsheet.h"
46 extern ThreeBarProgressPage Progress
;
48 list
< site_list_type
, const char *, strcasecmp
> site_list
;
49 list
< site_list_type
, const char *, strcasecmp
> all_site_list
;
51 static char *other_url
= 0;
54 site_list_type::init (char const *newurl
)
56 url
= _strdup (newurl
);
57 displayed_url
= _strdup (newurl
);
58 char *dot
= strchr (displayed_url
, '.');
61 dot
= strchr (dot
, '/');
65 key
= (char *) malloc (2 * strlen (newurl
) + 3);
70 while (dot
!= displayed_url
)
72 if (*dot
== '.' || *dot
== '/')
76 *dp
++ = '~'; /* sort .com/.edu/.org together */
77 for (sp
= dot
+ 1; *sp
&& *sp
!= '.' && *sp
!= '/';)
84 strcpy (dp
, displayed_url
);
87 site_list_type::site_list_type (char const *newurl
)
95 // Remove anything that was previously in the selected site list.
96 while (site_list
.number () > 0)
98 // we don't delete the object because it's stored in the all_site_list.
99 site_list
.removebyindex (1);
102 HWND listbox
= GetDlgItem (h
, IDC_URL_LIST
);
103 int sel_count
= SendMessage (listbox
, LB_GETSELCOUNT
, 0, 0);
106 int sel_buffer
[sel_count
];
107 int sel_count2
= SendMessage (listbox
, LB_GETSELITEMS
, sel_count
,
108 (LPARAM
) sel_buffer
);
109 if (sel_count
!= sel_count2
)
113 for (int n
= 0; n
< sel_count
; n
++)
116 SendMessage (listbox
, LB_GETITEMDATA
, sel_buffer
[n
], 0);
117 site_list
.registerbyobject (*all_site_list
[mirror
]);
125 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
126 for (size_t n
= 1; n
<= site_list
.number (); n
++)
130 // FIXME: write all selected sites
132 temp
= new TCHAR
[sizeof (TCHAR
) * (strlen (site_list
[n
]->url
) + 2)];
133 sprintf (temp
, "%s\n", site_list
[n
]->url
);
134 f
->write (temp
, strlen (temp
));
142 get_site_list (HINSTANCE h
, HWND owner
)
144 char mirror_url
[1000];
146 if (LoadString (h
, IDS_MIRROR_LST
, mirror_url
, sizeof (mirror_url
)) <= 0)
148 char *mirrors
= get_url_to_string (mirror_url
, owner
);
152 char *bol
, *eol
, *nl
;
158 for (eol
= bol
; *eol
&& *eol
!= '\n'; eol
++);
163 while (eol
> bol
&& eol
[-1] == '\r')
166 if (bol
[0] != '#' && bol
[0] > ' ')
168 char *semi
= strchr (bol
, ';');
171 site_list_type
*newsite
= new site_list_type (bol
);
172 site_list_type
& listobj
=
173 all_site_list
.registerbyobject (*newsite
);
174 if (&listobj
!= newsite
)
175 /* That site was already registered */
184 /* List of machines that should not be used by default when saved
186 #define NOSAVE1 "ftp://sources.redhat.com/"
187 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
188 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
189 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
190 #define NOSAVE3 "ftp://gcc.gnu.org/"
191 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
196 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
202 while ((fg_ret
= f
->gets (site
, 1000)))
205 char *eos
= site
+ strlen (site
) - 1;
206 while (eos
>= site
&& (*eos
== '\n' || *eos
== '\r'))
213 for (size_t i
= 1; !found
&& i
<= all_site_list
.number (); i
++)
214 if (!strcasecmp (site
, all_site_list
[i
]->url
))
219 /* Don't default to certain machines ever since they suffer
220 from bandwidth limitations. */
221 if (strnicmp (site
, NOSAVE1
, NOSAVE1_LEN
) == 0
222 || strnicmp (site
, NOSAVE2
, NOSAVE2_LEN
) == 0
223 || strnicmp (site
, NOSAVE3
, NOSAVE3_LEN
) == 0)
225 site_list_type
*newsite
= new site_list_type (site
);
226 site_list_type
& listobj
=
227 all_site_list
.registerbyobject (*newsite
);
228 if (&listobj
!= newsite
)
229 /* That site was already registered - shouldn't happen, but safety first */
232 /* TODO: make a site_type method to create a serach key on-the-fly from a
236 for (size_t i
= 1; !found
&& i
<= all_site_list
.number (); i
++)
237 if (!strcasecmp (site
, all_site_list
[i
]->url
))
238 site_list
.registerbyobject (*all_site_list
[i
]);
245 do_download_site_info_thread (void *p
)
250 context
= (HANDLE
*) p
;
252 hinst
= (HINSTANCE
) (context
[0]);
253 h
= (HWND
) (context
[1]);
255 if (all_site_list
.number () == 0)
257 if (get_site_list (hinst
, h
))
259 // Error: Couldn't download the site info. Go back to the Net setup page.
260 MessageBox (h
, TEXT ("Can't get list of download sites.\n\
261 Make sure your network settings are corect and try again."), NULL
, MB_OK
);
263 // Tell the progress page that we're done downloading
264 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0,
271 // Everything worked, go to the site select page
273 // Tell the progress page that we're done downloading
274 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0, IDD_SITE
);
279 static HANDLE context
[2];
282 do_download_site_info (HINSTANCE hinst
, HWND owner
)
288 _beginthread (do_download_site_info_thread
, 0, context
);
292 bool SitePage::Create ()
294 return PropertyPage::Create (IDD_SITE
);
311 // Log all the selected URLs from the list.
312 for (size_t n
= 1; n
<= site_list
.number (); n
++)
313 log (0, "site: %s", site_list
[n
]->url
);
315 Progress
.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD
);
328 // Go back to the net connection type page
333 SitePage::OnActivate ()
335 // Fill the list box with all known sites.
338 // Load the user URL box with whatever it was last time.
339 eset (GetHWND (), IDC_EDIT_USER_URL
, other_url
);
341 // Get the enabled/disabled states of the controls set accordingly.
342 CheckControlsAndDisableAccordingly ();
346 SitePage::CheckControlsAndDisableAccordingly () const
348 DWORD ButtonFlags
= PSWIZB_BACK
;
350 // Check that at least one download site is selected.
351 if (SendMessage (GetDlgItem (IDC_URL_LIST
), LB_GETSELCOUNT
, 0, 0) > 0)
353 // At least one official site selected, enable "Next".
354 ButtonFlags
|= PSWIZB_NEXT
;
356 GetOwner ()->SetButtons (ButtonFlags
);
360 SitePage::PopulateListBox ()
363 HWND listbox
= GetDlgItem (IDC_URL_LIST
);
365 // Populate the list box with the URLs.
366 SendMessage (listbox
, LB_RESETCONTENT
, 0, 0);
367 for (size_t i
= 1; i
<= all_site_list
.number (); i
++)
369 j
= SendMessage (listbox
, LB_ADDSTRING
, 0,
370 (LPARAM
) all_site_list
[i
]->displayed_url
);
371 SendMessage (listbox
, LB_SETITEMDATA
, j
, i
);
374 // Select the selected ones.
375 for (size_t n
= 1; n
<= site_list
.number (); n
++)
377 int index
= SendMessage (listbox
, LB_FINDSTRING
, (WPARAM
) - 1,
378 (LPARAM
) site_list
[n
]->displayed_url
);
381 // Highlight the selected item
382 SendMessage (listbox
, LB_SELITEMRANGE
, TRUE
, (index
<< 16) | index
);
383 // Make sure it's fully visible
384 SendMessage (listbox
, LB_SETCARETINDEX
, index
, FALSE
);
389 bool SitePage::OnMessageCmd (int id
, HWND hwndctl
, UINT code
)
393 case IDC_EDIT_USER_URL
:
395 if (code
== EN_CHANGE
)
397 // Text in edit box may have changed.
398 other_url
= eget (GetHWND (), IDC_EDIT_USER_URL
, other_url
);
404 if (code
== LBN_SELCHANGE
)
406 CheckControlsAndDisableAccordingly ();
410 case IDC_BUTTON_ADD_URL
:
412 if (code
== BN_CLICKED
)
414 // User pushed the Add button.
415 other_url
= eget (GetHWND (), IDC_EDIT_USER_URL
, other_url
);
419 site_list_type (other_url
);
420 site_list_type
& listobj
=
421 all_site_list
.registerbyobject (*newsite
);
422 if (&listobj
!= newsite
)
424 // That site was already registered
430 // Log the adding of this new URL.
431 log (0, "Adding site: %s", other_url
);
434 // Assume the user wants to use it and select it for him.
435 site_list
.registerbyobject (listobj
);
437 // Update the list box.
443 // Wasn't recognized or handled.
447 // Was handled since we never got to default above.