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
;
49 SiteList all_site_list
;
52 site_list_type::init (String
const &newurl
)
56 char *dots
= newurl
.cstr();
57 char *dot
= strchr (dots
, '.');
60 dot
= strchr (dot
, '/');
64 displayed_url
= String (dots
);
67 dot
= dots
+ strlen (dots
);
68 char *dpsave
, *dp
= new char[2 * newurl
.size() + 3];
72 if (*dot
== '.' || *dot
== '/')
76 *dp
++ = '~'; /* sort .com/.edu/.org together */
77 for (sp
= dot
+ 1; *sp
&& *sp
!= '.' && *sp
!= '/';)
90 site_list_type::site_list_type (String
const &newurl
)
95 site_list_type::site_list_type (site_list_type
const &rhs
)
99 displayed_url
= rhs
.displayed_url
;
103 site_list_type::operator= (site_list_type
const &rhs
)
107 displayed_url
= rhs
.displayed_url
;
112 site_list_type::operator == (site_list_type
const &rhs
) const
114 return key
.casecompare (rhs
.key
) == 0;
118 site_list_type::operator < (site_list_type
const &rhs
) const
120 return key
.casecompare (rhs
.key
) < 0;
126 // Remove anything that was previously in the selected site list.
129 HWND listbox
= GetDlgItem (h
, IDC_URL_LIST
);
130 int sel_count
= SendMessage (listbox
, LB_GETSELCOUNT
, 0, 0);
133 int sel_buffer
[sel_count
];
134 int sel_count2
= SendMessage (listbox
, LB_GETSELITEMS
, sel_count
,
135 (LPARAM
) sel_buffer
);
136 if (sel_count
!= sel_count2
)
140 for (int n
= 0; n
< sel_count
; n
++)
143 SendMessage (listbox
, LB_GETITEMDATA
, sel_buffer
[n
], 0);
144 site_list
.push_back (all_site_list
[mirror
]);
152 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
155 for (SiteList::const_iterator n
= site_list
.begin ();
156 n
!= site_list
.end (); ++n
)
157 f
->write ((n
->url
+ "\n").cstr_oneuse(), n
->url
.size() + 1);
163 get_site_list (HINSTANCE h
, HWND owner
)
165 char mirror_url
[1000];
167 if (LoadString (h
, IDS_MIRROR_LST
, mirror_url
, sizeof (mirror_url
)) <= 0)
169 char *bol
, *eol
, *nl
, *theString
;
171 String mirrors
= get_url_to_string (mirror_url
, owner
);
175 nl
= theString
= mirrors
.cstr();}
180 for (eol
= bol
; *eol
&& *eol
!= '\n'; eol
++);
185 while (eol
> bol
&& eol
[-1] == '\r')
188 if (bol
[0] != '#' && bol
[0] > ' ')
190 char *semi
= strchr (bol
, ';');
193 site_list_type
newsite (bol
);
194 SiteList::iterator i
= find (all_site_list
.begin(),
195 all_site_list
.end(), newsite
);
196 if (i
== all_site_list
.end())
199 merge (all_site_list
.begin(), all_site_list
.end(),
200 &newsite
, &newsite
+ 1,
201 inserter (result
, result
.begin()));
202 all_site_list
= result
;
205 //TODO: remove and remerge
214 /* List of machines that should not be used by default when saved
216 #define NOSAVE1 "ftp://sources.redhat.com/"
217 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
218 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
219 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
220 #define NOSAVE3 "ftp://gcc.gnu.org/"
221 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
226 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
232 while ((fg_ret
= f
->gets (site
, 1000)))
235 char *eos
= site
+ strlen (site
) - 1;
236 while (eos
>= site
&& (*eos
== '\n' || *eos
== '\r'))
242 site_list_type
tempSite(site
);
243 SiteList::iterator i
= find (all_site_list
.begin(),
244 all_site_list
.end(), tempSite
);
245 if (i
== all_site_list
.end())
247 /* Don't default to certain machines ever since they suffer
248 from bandwidth limitations. */
249 if (strnicmp (site
, NOSAVE1
, NOSAVE1_LEN
) == 0
250 || strnicmp (site
, NOSAVE2
, NOSAVE2_LEN
) == 0
251 || strnicmp (site
, NOSAVE3
, NOSAVE3_LEN
) == 0)
254 merge (all_site_list
.begin(), all_site_list
.end(),
255 &tempSite
, &tempSite
+ 1,
256 inserter (result
, result
.begin()));
257 all_site_list
= result
;
258 site_list
.push_back (tempSite
);
261 site_list
.push_back (tempSite
);
268 do_download_site_info_thread (void *p
)
273 context
= (HANDLE
*) p
;
275 hinst
= (HINSTANCE
) (context
[0]);
276 h
= (HWND
) (context
[1]);
278 if (all_site_list
.size() == 0
279 && get_site_list (hinst
, h
))
281 // Error: Couldn't download the site info. Go back to the Net setup page.
282 MessageBox (h
, TEXT ("Can't get list of download sites.\n\
283 Make sure your network settings are correct and try again."), NULL
, MB_OK
);
285 // Tell the progress page that we're done downloading
286 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0,
291 // Everything worked, go to the site select page
292 // Tell the progress page that we're done downloading
293 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0, IDD_SITE
);
298 static HANDLE context
[2];
301 do_download_site_info (HINSTANCE hinst
, HWND owner
)
308 CreateThread (NULL
, 0, do_download_site_info_thread
, context
, 0, &threadID
);
311 bool SitePage::Create ()
313 return PropertyPage::Create (IDD_SITE
);
330 // Log all the selected URLs from the list.
331 for (SiteList::const_iterator n
= site_list
.begin ();
332 n
!= site_list
.end (); ++n
)
333 log (LOG_PLAIN
) << "site: " << n
->url
<< endLog
;
335 Progress
.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD
);
348 // Go back to the net connection type page
353 SitePage::OnActivate ()
355 // Fill the list box with all known sites.
358 // Load the user URL box with nothing - it is in the list already.
359 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
361 // Get the enabled/disabled states of the controls set accordingly.
362 CheckControlsAndDisableAccordingly ();
366 SitePage::CheckControlsAndDisableAccordingly () const
368 DWORD ButtonFlags
= PSWIZB_BACK
;
370 // Check that at least one download site is selected.
371 if (SendMessage (GetDlgItem (IDC_URL_LIST
), LB_GETSELCOUNT
, 0, 0) > 0)
373 // At least one site selected, enable "Next".
374 ButtonFlags
|= PSWIZB_NEXT
;
376 GetOwner ()->SetButtons (ButtonFlags
);
380 SitePage::PopulateListBox ()
383 HWND listbox
= GetDlgItem (IDC_URL_LIST
);
385 // Populate the list box with the URLs.
386 SendMessage (listbox
, LB_RESETCONTENT
, 0, 0);
387 for (SiteList::const_iterator i
= all_site_list
.begin ();
388 i
!= all_site_list
.end (); ++i
)
390 j
= SendMessage (listbox
, LB_ADDSTRING
, 0,
391 (LPARAM
) i
->displayed_url
.cstr_oneuse());
392 SendMessage (listbox
, LB_SETITEMDATA
, j
, j
);
395 // Select the selected ones.
396 for (SiteList::const_iterator n
= site_list
.begin ();
397 n
!= site_list
.end (); ++n
)
399 int index
= SendMessage (listbox
, LB_FINDSTRING
, (WPARAM
) - 1,
400 (LPARAM
) n
->displayed_url
.cstr_oneuse());
403 // Highlight the selected item
404 SendMessage (listbox
, LB_SELITEMRANGE
, TRUE
, (index
<< 16) | index
);
405 // Make sure it's fully visible
406 SendMessage (listbox
, LB_SETCARETINDEX
, index
, FALSE
);
411 bool SitePage::OnMessageCmd (int id
, HWND hwndctl
, UINT code
)
415 case IDC_EDIT_USER_URL
:
417 // FIXME: Make Enter here cause an ADD, not a NEXT.
422 if (code
== LBN_SELCHANGE
)
424 CheckControlsAndDisableAccordingly ();
425 save_dialog (GetHWND ());
429 case IDC_BUTTON_ADD_URL
:
431 if (code
== BN_CLICKED
)
433 // User pushed the Add button.
434 String other_url
= egetString (GetHWND (), IDC_EDIT_USER_URL
);
435 if (other_url
.size())
437 site_list_type
newsite (other_url
);
438 SiteList::iterator i
= find (all_site_list
.begin(),
439 all_site_list
.end(), newsite
);
440 if (i
== all_site_list
.end())
442 all_site_list
.push_back (newsite
);
443 log (LOG_BABBLE
) << "Adding site: " << other_url
<< endLog
;
448 log (LOG_BABBLE
) << "Replacing site: " << other_url
<< endLog
;
451 // Assume the user wants to use it and select it for him.
452 site_list
.push_back (newsite
);
454 // Update the list box.
456 // And allow the user to continue
457 CheckControlsAndDisableAccordingly ();
458 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
464 // Wasn't recognized or handled.
468 // Was handled since we never got to default above.