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
=
36 #include "io_stream.h"
42 #include "propsheet.h"
45 extern ThreeBarProgressPage Progress
;
47 list
< site_list_type
, String
, String::casecompare
> site_list
;
48 list
< site_list_type
, String
, String::casecompare
> all_site_list
;
51 site_list_type::init (String
const &newurl
)
55 char *dots
= newurl
.cstr();
56 char *dot
= strchr (dots
, '.');
59 dot
= strchr (dot
, '/');
63 displayed_url
= String (dots
);
66 dot
= dots
+ strlen (dots
);
67 char *dp
= new char[2 * newurl
.size() + 3];
70 if (*dot
== '.' || *dot
== '/')
74 *dp
++ = '~'; /* sort .com/.edu/.org together */
75 for (sp
= dot
+ 1; *sp
&& *sp
!= '.' && *sp
!= '/';)
88 site_list_type::site_list_type (String
const &newurl
)
96 // Remove anything that was previously in the selected site list.
97 while (site_list
.number () > 0)
99 // we don't delete the object because it's stored in the all_site_list.
100 site_list
.removebyindex (1);
103 HWND listbox
= GetDlgItem (h
, IDC_URL_LIST
);
104 int sel_count
= SendMessage (listbox
, LB_GETSELCOUNT
, 0, 0);
107 int sel_buffer
[sel_count
];
108 int sel_count2
= SendMessage (listbox
, LB_GETSELITEMS
, sel_count
,
109 (LPARAM
) sel_buffer
);
110 if (sel_count
!= sel_count2
)
114 for (int n
= 0; n
< sel_count
; n
++)
117 SendMessage (listbox
, LB_GETITEMDATA
, sel_buffer
[n
], 0);
118 site_list
.registerbyobject (*all_site_list
[mirror
]);
126 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
129 for (size_t n
= 1; n
<= site_list
.number (); n
++)
130 f
->write ((site_list
[n
]->url
+ "\n").cstr_oneuse(), site_list
[n
]->url
.size() + 1);
136 get_site_list (HINSTANCE h
, HWND owner
)
138 char mirror_url
[1000];
140 if (LoadString (h
, IDS_MIRROR_LST
, mirror_url
, sizeof (mirror_url
)) <= 0)
142 char *bol
, *eol
, *nl
, *theString
;
144 String mirrors
= get_url_to_string (mirror_url
, owner
);
148 nl
= theString
= mirrors
.cstr();}
153 for (eol
= bol
; *eol
&& *eol
!= '\n'; eol
++);
158 while (eol
> bol
&& eol
[-1] == '\r')
161 if (bol
[0] != '#' && bol
[0] > ' ')
163 char *semi
= strchr (bol
, ';');
166 site_list_type
*newsite
= new site_list_type (bol
);
167 site_list_type
& listobj
=
168 all_site_list
.registerbyobject (*newsite
);
169 if (&listobj
!= newsite
)
170 /* That site was already registered */
179 /* List of machines that should not be used by default when saved
181 #define NOSAVE1 "ftp://sources.redhat.com/"
182 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
183 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
184 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
185 #define NOSAVE3 "ftp://gcc.gnu.org/"
186 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
191 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
197 while ((fg_ret
= f
->gets (site
, 1000)))
200 char *eos
= site
+ strlen (site
) - 1;
201 while (eos
>= site
&& (*eos
== '\n' || *eos
== '\r'))
207 String tempKey
= site_list_type (String(site
)).key
;
208 if (!all_site_list
.getbykey (tempKey
))
210 /* Don't default to certain machines ever since they suffer
211 from bandwidth limitations. */
212 if (strnicmp (site
, NOSAVE1
, NOSAVE1_LEN
) == 0
213 || strnicmp (site
, NOSAVE2
, NOSAVE2_LEN
) == 0
214 || strnicmp (site
, NOSAVE3
, NOSAVE3_LEN
) == 0)
216 site_list_type
*newsite
= new site_list_type (site
);
217 site_list_type
& listobj
=
218 all_site_list
.registerbyobject (*newsite
);
219 if (&listobj
!= newsite
)
220 /* That site was already registered - shouldn't happen, but safety first */
223 /* TODO: make a site_type method to create a serach key on-the-fly from a
226 // Was it an allowed URL?
227 site_list_type
*tempSite
;
228 if ((tempSite
= all_site_list
.getbykey (tempKey
)))
229 site_list
.registerbyobject (*tempSite
);
236 do_download_site_info_thread (void *p
)
241 context
= (HANDLE
*) p
;
243 hinst
= (HINSTANCE
) (context
[0]);
244 h
= (HWND
) (context
[1]);
246 if (all_site_list
.number () == 0
247 && get_site_list (hinst
, h
))
249 // Error: Couldn't download the site info. Go back to the Net setup page.
250 MessageBox (h
, TEXT ("Can't get list of download sites.\n\
251 Make sure your network settings are corect and try again."), NULL
, MB_OK
);
253 // Tell the progress page that we're done downloading
254 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0,
259 // Everything worked, go to the site select page
260 // Tell the progress page that we're done downloading
261 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0, IDD_SITE
);
266 static HANDLE context
[2];
269 do_download_site_info (HINSTANCE hinst
, HWND owner
)
275 _beginthread (do_download_site_info_thread
, 0, context
);
279 bool SitePage::Create ()
281 return PropertyPage::Create (IDD_SITE
);
298 // Log all the selected URLs from the list.
299 for (size_t n
= 1; n
<= site_list
.number (); n
++)
300 log (LOG_PLAIN
, String ("site: ") + site_list
[n
]->url
);
302 Progress
.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD
);
315 // Go back to the net connection type page
320 SitePage::OnActivate ()
322 // Fill the list box with all known sites.
325 // Load the user URL box with nothing - it is in the list already.
326 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
328 // Get the enabled/disabled states of the controls set accordingly.
329 CheckControlsAndDisableAccordingly ();
333 SitePage::CheckControlsAndDisableAccordingly () const
335 DWORD ButtonFlags
= PSWIZB_BACK
;
337 // Check that at least one download site is selected.
338 if (SendMessage (GetDlgItem (IDC_URL_LIST
), LB_GETSELCOUNT
, 0, 0) > 0)
340 // At least one official site selected, enable "Next".
341 ButtonFlags
|= PSWIZB_NEXT
;
343 GetOwner ()->SetButtons (ButtonFlags
);
347 SitePage::PopulateListBox ()
350 HWND listbox
= GetDlgItem (IDC_URL_LIST
);
352 // Populate the list box with the URLs.
353 SendMessage (listbox
, LB_RESETCONTENT
, 0, 0);
354 for (size_t i
= 1; i
<= all_site_list
.number (); i
++)
356 j
= SendMessage (listbox
, LB_ADDSTRING
, 0,
357 (LPARAM
) all_site_list
[i
]->displayed_url
.cstr_oneuse());
358 SendMessage (listbox
, LB_SETITEMDATA
, j
, i
);
361 // Select the selected ones.
362 for (size_t n
= 1; n
<= site_list
.number (); n
++)
364 int index
= SendMessage (listbox
, LB_FINDSTRING
, (WPARAM
) - 1,
365 (LPARAM
) site_list
[n
]->displayed_url
.cstr_oneuse());
368 // Highlight the selected item
369 SendMessage (listbox
, LB_SELITEMRANGE
, TRUE
, (index
<< 16) | index
);
370 // Make sure it's fully visible
371 SendMessage (listbox
, LB_SETCARETINDEX
, index
, FALSE
);
376 bool SitePage::OnMessageCmd (int id
, HWND hwndctl
, UINT code
)
380 case IDC_EDIT_USER_URL
:
382 // FIXME: Make Enter here cause an ADD, not a NEXT.
387 if (code
== LBN_SELCHANGE
)
389 CheckControlsAndDisableAccordingly ();
390 save_dialog (GetHWND ());
394 case IDC_BUTTON_ADD_URL
:
396 if (code
== BN_CLICKED
)
398 // User pushed the Add button.
399 String other_url
= egetString (GetHWND (), IDC_EDIT_USER_URL
);
400 if (other_url
.size())
405 site_list_type (other_url
);
406 site_list_type
& listobj
=
407 all_site_list
.registerbyobject (*newsite
);
408 if (&listobj
!= newsite
)
410 // That site was already registered
416 // Log the adding of this new URL.
417 log (LOG_BABBLE
, String ("Adding site: ") + other_url
);
420 // Assume the user wants to use it and select it for him.
421 site_list
.registerbyobject (listobj
);
423 // Update the list box.
425 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
431 // Wasn't recognized or handled.
435 // Was handled since we never got to default above.