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"
41 #include "propsheet.h"
44 #include "ControlAdjuster.h"
45 #include "Exception.h"
47 extern ThreeBarProgressPage Progress
;
52 static ControlAdjuster::ControlInfo SiteControlsInfo
[] = {
53 {IDC_URL_LIST
, CP_STRETCH
, CP_STRETCH
},
54 {IDC_EDIT_USER_URL
, CP_STRETCH
, CP_BOTTOM
},
55 {IDC_BUTTON_ADD_URL
, CP_RIGHT
, CP_BOTTOM
},
56 {IDC_SITE_USERURL
, CP_LEFT
, CP_BOTTOM
},
62 sizeProcessor
.AddControlInfo (SiteControlsInfo
);
65 #include "getopt++/StringOption.h"
66 #include "UserSettings.h"
71 SiteList all_site_list
;
73 StringOption
SiteOption("", 's', "site", "Download site", false);
75 /* XXX make into a singleton? */
76 static SiteSetting ChosenSites
;
81 string SiteOptionString
= SiteOption
;
82 if (SiteOptionString
.size())
83 registerSavedSite (SiteOptionString
.c_str());
91 io_stream
*f
= UserSettings::Instance().settingFileForSave("last-mirror");
94 for (SiteList::const_iterator n
= site_list
.begin ();
95 n
!= site_list
.end (); ++n
)
96 f
->write ((n
->url
+ "\n").c_str(), n
->url
.size() + 1);
102 site_list_type::init (String
const &newurl
)
106 char *dots
= new_cstr_char_array (newurl
);
107 char *dot
= strchr (dots
, '.');
110 dot
= strchr (dot
, '/');
114 displayed_url
= String (dots
);
117 dot
= dots
+ strlen (dots
);
118 char *dpsave
, *dp
= new char[2 * newurl
.size() + 3];
122 if (*dot
== '.' || *dot
== '/')
126 *dp
++ = '~'; /* sort .com/.edu/.org together */
127 for (sp
= dot
+ 1; *sp
&& *sp
!= '.' && *sp
!= '/';)
140 site_list_type::site_list_type (String
const &newurl
)
145 site_list_type::site_list_type (site_list_type
const &rhs
)
149 displayed_url
= rhs
.displayed_url
;
153 site_list_type::operator= (site_list_type
const &rhs
)
157 displayed_url
= rhs
.displayed_url
;
162 site_list_type::operator == (site_list_type
const &rhs
) const
164 return key
.casecompare (rhs
.key
) == 0;
168 site_list_type::operator < (site_list_type
const &rhs
) const
170 return key
.casecompare (rhs
.key
) < 0;
176 // Remove anything that was previously in the selected site list.
179 HWND listbox
= GetDlgItem (h
, IDC_URL_LIST
);
180 int sel_count
= SendMessage (listbox
, LB_GETSELCOUNT
, 0, 0);
183 int sel_buffer
[sel_count
];
184 SendMessage (listbox
, LB_GETSELITEMS
, sel_count
, (LPARAM
) sel_buffer
);
185 for (int n
= 0; n
< sel_count
; n
++)
188 SendMessage (listbox
, LB_GETITEMDATA
, sel_buffer
[n
], 0);
189 site_list
.push_back (all_site_list
[mirror
]);
195 get_site_list (HINSTANCE h
, HWND owner
)
197 char mirror_url
[1000];
199 if (LoadString (h
, IDS_MIRROR_LST
, mirror_url
, sizeof (mirror_url
)) <= 0)
201 char *bol
, *eol
, *nl
, *theString
;
203 String mirrors
= get_url_to_string (mirror_url
, owner
);
207 theString
= new_cstr_char_array (mirrors
);
214 for (eol
= bol
; *eol
&& *eol
!= '\n'; eol
++);
219 while (eol
> bol
&& eol
[-1] == '\r')
222 if (*bol
== '#' || !*bol
)
224 /* Accept only the URL schemes we can understand. */
225 if (strncmp(bol
, "http://", 7) == 0 ||
226 strncmp(bol
, "ftp://", 6) == 0)
228 char *semi
= strchr (bol
, ';');
231 site_list_type
newsite (bol
);
232 SiteList::iterator i
= find (all_site_list
.begin(),
233 all_site_list
.end(), newsite
);
234 if (i
== all_site_list
.end())
237 merge (all_site_list
.begin(), all_site_list
.end(),
238 &newsite
, &newsite
+ 1,
239 inserter (result
, result
.begin()));
240 all_site_list
= result
;
243 //TODO: remove and remerge
252 /* List of machines that should not be used by default when saved
254 #define NOSAVE1 "ftp://sources.redhat.com/"
255 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
256 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
257 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
258 #define NOSAVE3 "ftp://gcc.gnu.org/"
259 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
262 SiteSetting::registerSavedSite (const char * site
)
264 site_list_type
tempSite(site
);
265 SiteList::iterator i
= find (all_site_list
.begin(),
266 all_site_list
.end(), tempSite
);
267 if (i
== all_site_list
.end())
269 /* Don't default to certain machines ever since they suffer
270 from bandwidth limitations. */
271 if (strnicmp (site
, NOSAVE1
, NOSAVE1_LEN
) == 0
272 || strnicmp (site
, NOSAVE2
, NOSAVE2_LEN
) == 0
273 || strnicmp (site
, NOSAVE3
, NOSAVE3_LEN
) == 0)
276 merge (all_site_list
.begin(), all_site_list
.end(),
277 &tempSite
, &tempSite
+ 1,
278 inserter (result
, result
.begin()));
279 all_site_list
= result
;
280 site_list
.push_back (tempSite
);
283 site_list
.push_back (tempSite
);
287 SiteSetting::getSavedSites ()
289 io_stream
*f
= UserSettings::Instance().settingFileForLoad("last-mirror");
295 while ((fg_ret
= f
->gets (site
, 1000)))
298 char *eos
= site
+ strlen (site
) - 1;
299 while (eos
>= site
&& (*eos
== '\n' || *eos
== '\r'))
305 registerSavedSite (site
);
313 do_download_site_info_thread (void *p
)
318 context
= (HANDLE
*) p
;
322 hinst
= (HINSTANCE
) (context
[0]);
323 h
= (HWND
) (context
[1]);
324 static bool downloaded
= false;
325 if (!downloaded
&& get_site_list (hinst
, h
))
327 // Error: Couldn't download the site info.
328 // Go back to the Net setup page.
329 MessageBox (h
, TEXT ("Can't get list of download sites.\n")
330 TEXT("Make sure your network settings are correct and try again."),
333 // Tell the progress page that we're done downloading
334 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0, IDD_NET
);
339 // Everything worked, go to the site select page
340 // Tell the progress page that we're done downloading
341 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0, IDD_SITE
);
344 TOPLEVEL_CATCH("site");
349 static HANDLE context
[2];
352 do_download_site_info (HINSTANCE hinst
, HWND owner
)
359 CreateThread (NULL
, 0, do_download_site_info_thread
, context
, 0, &threadID
);
362 bool SitePage::Create ()
364 return PropertyPage::Create (IDD_SITE
);
375 // Log all the selected URLs from the list.
376 for (SiteList::const_iterator n
= site_list
.begin ();
377 n
!= site_list
.end (); ++n
)
378 log (LOG_PLAIN
) << "site: " << n
->url
<< endLog
;
380 Progress
.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD
);
393 // Go back to the net connection type page
398 SitePage::OnActivate ()
400 // Fill the list box with all known sites.
403 // Load the user URL box with nothing - it is in the list already.
404 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
406 // Get the enabled/disabled states of the controls set accordingly.
407 CheckControlsAndDisableAccordingly ();
411 SitePage::OnUnattended ()
413 if (SendMessage (GetDlgItem (IDC_URL_LIST
), LB_GETSELCOUNT
, 0, 0) > 0)
420 SitePage::CheckControlsAndDisableAccordingly () const
422 DWORD ButtonFlags
= PSWIZB_BACK
;
424 // Check that at least one download site is selected.
425 if (SendMessage (GetDlgItem (IDC_URL_LIST
), LB_GETSELCOUNT
, 0, 0) > 0)
427 // At least one site selected, enable "Next".
428 ButtonFlags
|= PSWIZB_NEXT
;
430 GetOwner ()->SetButtons (ButtonFlags
);
434 SitePage::PopulateListBox ()
437 HWND listbox
= GetDlgItem (IDC_URL_LIST
);
439 // Populate the list box with the URLs.
440 SendMessage (listbox
, LB_RESETCONTENT
, 0, 0);
441 for (SiteList::const_iterator i
= all_site_list
.begin ();
442 i
!= all_site_list
.end (); ++i
)
444 j
= SendMessage (listbox
, LB_ADDSTRING
, 0,
445 (LPARAM
) i
->displayed_url
.c_str());
446 SendMessage (listbox
, LB_SETITEMDATA
, j
, j
);
449 // Select the selected ones.
450 for (SiteList::const_iterator n
= site_list
.begin ();
451 n
!= site_list
.end (); ++n
)
453 int index
= SendMessage (listbox
, LB_FINDSTRING
, (WPARAM
) - 1,
454 (LPARAM
) n
->displayed_url
.c_str());
457 // Highlight the selected item
458 SendMessage (listbox
, LB_SELITEMRANGE
, TRUE
, (index
<< 16) | index
);
459 // Make sure it's fully visible
460 SendMessage (listbox
, LB_SETCARETINDEX
, index
, FALSE
);
465 bool SitePage::OnMessageCmd (int id
, HWND hwndctl
, UINT code
)
469 case IDC_EDIT_USER_URL
:
471 // FIXME: Make Enter here cause an ADD, not a NEXT.
476 if (code
== LBN_SELCHANGE
)
478 CheckControlsAndDisableAccordingly ();
479 save_dialog (GetHWND ());
483 case IDC_BUTTON_ADD_URL
:
485 if (code
== BN_CLICKED
)
487 // User pushed the Add button.
488 String other_url
= egetString (GetHWND (), IDC_EDIT_USER_URL
);
489 if (other_url
.size())
491 site_list_type
newsite (other_url
);
492 SiteList::iterator i
= find (all_site_list
.begin(),
493 all_site_list
.end(), newsite
);
494 if (i
== all_site_list
.end())
496 all_site_list
.push_back (newsite
);
497 log (LOG_BABBLE
) << "Adding site: " << other_url
<< endLog
;
502 log (LOG_BABBLE
) << "Replacing site: " << other_url
<< endLog
;
505 // Assume the user wants to use it and select it for him.
506 site_list
.push_back (newsite
);
508 // Update the list box.
510 // And allow the user to continue
511 CheckControlsAndDisableAccordingly ();
512 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
518 // Wasn't recognized or handled.
522 // Was handled since we never got to default above.