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
= new char [strlen (newurl
) +1];
58 displayed_url
= new char [strlen (newurl
) +1];
59 strcpy (displayed_url
, newurl
);
60 char *dot
= strchr (displayed_url
, '.');
63 dot
= strchr (dot
, '/');
67 key
= new char[2 * strlen (newurl
) + 3];
72 while (dot
!= displayed_url
)
74 if (*dot
== '.' || *dot
== '/')
78 *dp
++ = '~'; /* sort .com/.edu/.org together */
79 for (sp
= dot
+ 1; *sp
&& *sp
!= '.' && *sp
!= '/';)
86 strcpy (dp
, displayed_url
);
89 site_list_type::site_list_type (char const *newurl
)
97 // Remove anything that was previously in the selected site list.
98 while (site_list
.number () > 0)
100 // we don't delete the object because it's stored in the all_site_list.
101 site_list
.removebyindex (1);
104 HWND listbox
= GetDlgItem (h
, IDC_URL_LIST
);
105 int sel_count
= SendMessage (listbox
, LB_GETSELCOUNT
, 0, 0);
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
)
115 for (int n
= 0; n
< sel_count
; n
++)
118 SendMessage (listbox
, LB_GETITEMDATA
, sel_buffer
[n
], 0);
119 site_list
.registerbyobject (*all_site_list
[mirror
]);
127 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
128 for (size_t n
= 1; n
<= site_list
.number (); n
++)
132 // FIXME: write all selected sites
134 temp
= new TCHAR
[sizeof (TCHAR
) * (strlen (site_list
[n
]->url
) + 2)];
135 sprintf (temp
, "%s\n", site_list
[n
]->url
);
136 f
->write (temp
, strlen (temp
));
144 get_site_list (HINSTANCE h
, HWND owner
)
146 char mirror_url
[1000];
148 if (LoadString (h
, IDS_MIRROR_LST
, mirror_url
, sizeof (mirror_url
)) <= 0)
150 char *mirrors
= get_url_to_string (mirror_url
, owner
);
154 char *bol
, *eol
, *nl
;
160 for (eol
= bol
; *eol
&& *eol
!= '\n'; eol
++);
165 while (eol
> bol
&& eol
[-1] == '\r')
168 if (bol
[0] != '#' && bol
[0] > ' ')
170 char *semi
= strchr (bol
, ';');
173 site_list_type
*newsite
= new site_list_type (bol
);
174 site_list_type
& listobj
=
175 all_site_list
.registerbyobject (*newsite
);
176 if (&listobj
!= newsite
)
177 /* That site was already registered */
186 /* List of machines that should not be used by default when saved
188 #define NOSAVE1 "ftp://sources.redhat.com/"
189 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
190 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
191 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
192 #define NOSAVE3 "ftp://gcc.gnu.org/"
193 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
198 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
204 while ((fg_ret
= f
->gets (site
, 1000)))
207 char *eos
= site
+ strlen (site
) - 1;
208 while (eos
>= site
&& (*eos
== '\n' || *eos
== '\r'))
215 for (size_t i
= 1; !found
&& i
<= all_site_list
.number (); i
++)
216 if (!strcasecmp (site
, all_site_list
[i
]->url
))
221 /* Don't default to certain machines ever since they suffer
222 from bandwidth limitations. */
223 if (strnicmp (site
, NOSAVE1
, NOSAVE1_LEN
) == 0
224 || strnicmp (site
, NOSAVE2
, NOSAVE2_LEN
) == 0
225 || strnicmp (site
, NOSAVE3
, NOSAVE3_LEN
) == 0)
227 site_list_type
*newsite
= new site_list_type (site
);
228 site_list_type
& listobj
=
229 all_site_list
.registerbyobject (*newsite
);
230 if (&listobj
!= newsite
)
231 /* That site was already registered - shouldn't happen, but safety first */
234 /* TODO: make a site_type method to create a serach key on-the-fly from a
238 for (size_t i
= 1; !found
&& i
<= all_site_list
.number (); i
++)
239 if (!strcasecmp (site
, all_site_list
[i
]->url
))
240 site_list
.registerbyobject (*all_site_list
[i
]);
247 do_download_site_info_thread (void *p
)
252 context
= (HANDLE
*) p
;
254 hinst
= (HINSTANCE
) (context
[0]);
255 h
= (HWND
) (context
[1]);
257 if (all_site_list
.number () == 0)
259 if (get_site_list (hinst
, h
))
261 // Error: Couldn't download the site info. Go back to the Net setup page.
262 MessageBox (h
, TEXT ("Can't get list of download sites.\n\
263 Make sure your network settings are corect and try again."), NULL
, MB_OK
);
265 // Tell the progress page that we're done downloading
266 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0,
273 // Everything worked, go to the site select page
275 // Tell the progress page that we're done downloading
276 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0, IDD_SITE
);
281 static HANDLE context
[2];
284 do_download_site_info (HINSTANCE hinst
, HWND owner
)
290 _beginthread (do_download_site_info_thread
, 0, context
);
294 bool SitePage::Create ()
296 return PropertyPage::Create (IDD_SITE
);
313 // Log all the selected URLs from the list.
314 for (size_t n
= 1; n
<= site_list
.number (); n
++)
315 log (0, "site: %s", site_list
[n
]->url
);
317 Progress
.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD
);
330 // Go back to the net connection type page
335 SitePage::OnActivate ()
337 // Fill the list box with all known sites.
340 // Load the user URL box with whatever it was last time.
341 eset (GetHWND (), IDC_EDIT_USER_URL
, other_url
);
343 // Get the enabled/disabled states of the controls set accordingly.
344 CheckControlsAndDisableAccordingly ();
348 SitePage::CheckControlsAndDisableAccordingly () const
350 DWORD ButtonFlags
= PSWIZB_BACK
;
352 // Check that at least one download site is selected.
353 if (SendMessage (GetDlgItem (IDC_URL_LIST
), LB_GETSELCOUNT
, 0, 0) > 0)
355 // At least one official site selected, enable "Next".
356 ButtonFlags
|= PSWIZB_NEXT
;
358 GetOwner ()->SetButtons (ButtonFlags
);
362 SitePage::PopulateListBox ()
365 HWND listbox
= GetDlgItem (IDC_URL_LIST
);
367 // Populate the list box with the URLs.
368 SendMessage (listbox
, LB_RESETCONTENT
, 0, 0);
369 for (size_t i
= 1; i
<= all_site_list
.number (); i
++)
371 j
= SendMessage (listbox
, LB_ADDSTRING
, 0,
372 (LPARAM
) all_site_list
[i
]->displayed_url
);
373 SendMessage (listbox
, LB_SETITEMDATA
, j
, i
);
376 // Select the selected ones.
377 for (size_t n
= 1; n
<= site_list
.number (); n
++)
379 int index
= SendMessage (listbox
, LB_FINDSTRING
, (WPARAM
) - 1,
380 (LPARAM
) site_list
[n
]->displayed_url
);
383 // Highlight the selected item
384 SendMessage (listbox
, LB_SELITEMRANGE
, TRUE
, (index
<< 16) | index
);
385 // Make sure it's fully visible
386 SendMessage (listbox
, LB_SETCARETINDEX
, index
, FALSE
);
391 bool SitePage::OnMessageCmd (int id
, HWND hwndctl
, UINT code
)
395 case IDC_EDIT_USER_URL
:
397 if (code
== EN_CHANGE
)
399 // Text in edit box may have changed.
400 other_url
= eget (GetHWND (), IDC_EDIT_USER_URL
, other_url
);
406 if (code
== LBN_SELCHANGE
)
408 CheckControlsAndDisableAccordingly ();
412 case IDC_BUTTON_ADD_URL
:
414 if (code
== BN_CLICKED
)
416 // User pushed the Add button.
417 other_url
= eget (GetHWND (), IDC_EDIT_USER_URL
, other_url
);
421 site_list_type (other_url
);
422 site_list_type
& listobj
=
423 all_site_list
.registerbyobject (*newsite
);
424 if (&listobj
!= newsite
)
426 // That site was already registered
432 // Log the adding of this new URL.
433 log (0, "Adding site: %s", other_url
);
436 // Assume the user wants to use it and select it for him.
437 site_list
.registerbyobject (listobj
);
439 // Update the list box.
445 // Wasn't recognized or handled.
449 // Was handled since we never got to default above.