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 "LogSingleton.h"
38 #include "io_stream.h"
43 #include "propsheet.h"
46 extern ThreeBarProgressPage Progress
;
48 #include "getopt++/StringOption.h"
53 SiteList all_site_list
;
55 StringOption
SiteOption("", 's', "site", "Download site");
57 /* XXX make into a singleton? */
58 static SiteSetting ChosenSites
;
61 site_list_type::init (String
const &newurl
)
65 char *dots
= newurl
.cstr();
66 char *dot
= strchr (dots
, '.');
69 dot
= strchr (dot
, '/');
73 displayed_url
= String (dots
);
76 dot
= dots
+ strlen (dots
);
77 char *dpsave
, *dp
= new char[2 * newurl
.size() + 3];
81 if (*dot
== '.' || *dot
== '/')
85 *dp
++ = '~'; /* sort .com/.edu/.org together */
86 for (sp
= dot
+ 1; *sp
&& *sp
!= '.' && *sp
!= '/';)
99 site_list_type::site_list_type (String
const &newurl
)
104 site_list_type::site_list_type (site_list_type
const &rhs
)
108 displayed_url
= rhs
.displayed_url
;
112 site_list_type::operator= (site_list_type
const &rhs
)
116 displayed_url
= rhs
.displayed_url
;
121 site_list_type::operator == (site_list_type
const &rhs
) const
123 return key
.casecompare (rhs
.key
) == 0;
127 site_list_type::operator < (site_list_type
const &rhs
) const
129 return key
.casecompare (rhs
.key
) < 0;
135 // Remove anything that was previously in the selected site list.
138 HWND listbox
= GetDlgItem (h
, IDC_URL_LIST
);
139 int sel_count
= SendMessage (listbox
, LB_GETSELCOUNT
, 0, 0);
142 int sel_buffer
[sel_count
];
143 int sel_count2
= SendMessage (listbox
, LB_GETSELITEMS
, sel_count
,
144 (LPARAM
) sel_buffer
);
145 if (sel_count
!= sel_count2
)
149 for (int n
= 0; n
< sel_count
; n
++)
152 SendMessage (listbox
, LB_GETITEMDATA
, sel_buffer
[n
], 0);
153 site_list
.push_back (all_site_list
[mirror
]);
161 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
164 for (SiteList::const_iterator n
= site_list
.begin ();
165 n
!= site_list
.end (); ++n
)
166 f
->write ((n
->url
+ "\n").cstr_oneuse(), n
->url
.size() + 1);
172 get_site_list (HINSTANCE h
, HWND owner
)
174 char mirror_url
[1000];
176 if (LoadString (h
, IDS_MIRROR_LST
, mirror_url
, sizeof (mirror_url
)) <= 0)
178 char *bol
, *eol
, *nl
, *theString
;
180 String mirrors
= get_url_to_string (mirror_url
, owner
);
184 nl
= theString
= mirrors
.cstr();}
189 for (eol
= bol
; *eol
&& *eol
!= '\n'; eol
++);
194 while (eol
> bol
&& eol
[-1] == '\r')
197 if (bol
[0] != '#' && bol
[0] > ' ')
199 char *semi
= strchr (bol
, ';');
202 site_list_type
newsite (bol
);
203 SiteList::iterator i
= find (all_site_list
.begin(),
204 all_site_list
.end(), newsite
);
205 if (i
== all_site_list
.end())
208 merge (all_site_list
.begin(), all_site_list
.end(),
209 &newsite
, &newsite
+ 1,
210 inserter (result
, result
.begin()));
211 all_site_list
= result
;
214 //TODO: remove and remerge
223 /* List of machines that should not be used by default when saved
225 #define NOSAVE1 "ftp://sources.redhat.com/"
226 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
227 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
228 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
229 #define NOSAVE3 "ftp://gcc.gnu.org/"
230 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
233 register_saved_site (const char * site
)
235 site_list_type
tempSite(site
);
236 SiteList::iterator i
= find (all_site_list
.begin(),
237 all_site_list
.end(), tempSite
);
238 if (i
== all_site_list
.end())
240 /* Don't default to certain machines ever since they suffer
241 from bandwidth limitations. */
242 if (strnicmp (site
, NOSAVE1
, NOSAVE1_LEN
) == 0
243 || strnicmp (site
, NOSAVE2
, NOSAVE2_LEN
) == 0
244 || strnicmp (site
, NOSAVE3
, NOSAVE3_LEN
) == 0)
247 merge (all_site_list
.begin(), all_site_list
.end(),
248 &tempSite
, &tempSite
+ 1,
249 inserter (result
, result
.begin()));
250 all_site_list
= result
;
251 site_list
.push_back (tempSite
);
254 site_list
.push_back (tempSite
);
260 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
266 while ((fg_ret
= f
->gets (site
, 1000)))
269 char *eos
= site
+ strlen (site
) - 1;
270 while (eos
>= site
&& (*eos
== '\n' || *eos
== '\r'))
276 register_saved_site (site
);
284 do_download_site_info_thread (void *p
)
289 context
= (HANDLE
*) p
;
291 hinst
= (HINSTANCE
) (context
[0]);
292 h
= (HWND
) (context
[1]);
294 if (all_site_list
.size() == 0
295 && get_site_list (hinst
, h
))
297 // Error: Couldn't download the site info. Go back to the Net setup page.
298 MessageBox (h
, TEXT ("Can't get list of download sites.\n\
299 Make sure your network settings are correct and try again."), NULL
, MB_OK
);
301 // Tell the progress page that we're done downloading
302 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0,
307 // Everything worked, go to the site select page
308 // Tell the progress page that we're done downloading
309 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0, IDD_SITE
);
314 static HANDLE context
[2];
317 do_download_site_info (HINSTANCE hinst
, HWND owner
)
324 CreateThread (NULL
, 0, do_download_site_info_thread
, context
, 0, &threadID
);
327 bool SitePage::Create ()
329 return PropertyPage::Create (IDD_SITE
);
335 string SiteOptionString
= SiteOption
;
336 if (SiteOptionString
.size())
337 register_saved_site (SiteOptionString
.c_str());
350 // Log all the selected URLs from the list.
351 for (SiteList::const_iterator n
= site_list
.begin ();
352 n
!= site_list
.end (); ++n
)
353 log (LOG_PLAIN
) << "site: " << n
->url
<< endLog
;
355 Progress
.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD
);
368 // Go back to the net connection type page
373 SitePage::OnActivate ()
375 // Fill the list box with all known sites.
378 // Load the user URL box with nothing - it is in the list already.
379 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
381 // Get the enabled/disabled states of the controls set accordingly.
382 CheckControlsAndDisableAccordingly ();
386 SitePage::OnUnattended ()
388 if (SendMessage (GetDlgItem (IDC_URL_LIST
), LB_GETSELCOUNT
, 0, 0) > 0)
395 SitePage::CheckControlsAndDisableAccordingly () const
397 DWORD ButtonFlags
= PSWIZB_BACK
;
399 // Check that at least one download site is selected.
400 if (SendMessage (GetDlgItem (IDC_URL_LIST
), LB_GETSELCOUNT
, 0, 0) > 0)
402 // At least one site selected, enable "Next".
403 ButtonFlags
|= PSWIZB_NEXT
;
405 GetOwner ()->SetButtons (ButtonFlags
);
409 SitePage::PopulateListBox ()
412 HWND listbox
= GetDlgItem (IDC_URL_LIST
);
414 // Populate the list box with the URLs.
415 SendMessage (listbox
, LB_RESETCONTENT
, 0, 0);
416 for (SiteList::const_iterator i
= all_site_list
.begin ();
417 i
!= all_site_list
.end (); ++i
)
419 j
= SendMessage (listbox
, LB_ADDSTRING
, 0,
420 (LPARAM
) i
->displayed_url
.cstr_oneuse());
421 SendMessage (listbox
, LB_SETITEMDATA
, j
, j
);
424 // Select the selected ones.
425 for (SiteList::const_iterator n
= site_list
.begin ();
426 n
!= site_list
.end (); ++n
)
428 int index
= SendMessage (listbox
, LB_FINDSTRING
, (WPARAM
) - 1,
429 (LPARAM
) n
->displayed_url
.cstr_oneuse());
432 // Highlight the selected item
433 SendMessage (listbox
, LB_SELITEMRANGE
, TRUE
, (index
<< 16) | index
);
434 // Make sure it's fully visible
435 SendMessage (listbox
, LB_SETCARETINDEX
, index
, FALSE
);
440 bool SitePage::OnMessageCmd (int id
, HWND hwndctl
, UINT code
)
444 case IDC_EDIT_USER_URL
:
446 // FIXME: Make Enter here cause an ADD, not a NEXT.
451 if (code
== LBN_SELCHANGE
)
453 CheckControlsAndDisableAccordingly ();
454 save_dialog (GetHWND ());
458 case IDC_BUTTON_ADD_URL
:
460 if (code
== BN_CLICKED
)
462 // User pushed the Add button.
463 String other_url
= egetString (GetHWND (), IDC_EDIT_USER_URL
);
464 if (other_url
.size())
466 site_list_type
newsite (other_url
);
467 SiteList::iterator i
= find (all_site_list
.begin(),
468 all_site_list
.end(), newsite
);
469 if (i
== all_site_list
.end())
471 all_site_list
.push_back (newsite
);
472 log (LOG_BABBLE
) << "Adding site: " << other_url
<< endLog
;
477 log (LOG_BABBLE
) << "Replacing site: " << other_url
<< endLog
;
480 // Assume the user wants to use it and select it for him.
481 site_list
.push_back (newsite
);
483 // Update the list box.
485 // And allow the user to continue
486 CheckControlsAndDisableAccordingly ();
487 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
493 // Wasn't recognized or handled.
497 // Was handled since we never got to default above.