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
=
35 #include "LogSingleton.h"
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 *dpsave
, *dp
= new char[2 * newurl
.size() + 3];
71 if (*dot
== '.' || *dot
== '/')
75 *dp
++ = '~'; /* sort .com/.edu/.org together */
76 for (sp
= dot
+ 1; *sp
&& *sp
!= '.' && *sp
!= '/';)
89 site_list_type::site_list_type (String
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");
130 for (size_t n
= 1; n
<= site_list
.number (); n
++)
131 f
->write ((site_list
[n
]->url
+ "\n").cstr_oneuse(), site_list
[n
]->url
.size() + 1);
137 get_site_list (HINSTANCE h
, HWND owner
)
139 char mirror_url
[1000];
141 if (LoadString (h
, IDS_MIRROR_LST
, mirror_url
, sizeof (mirror_url
)) <= 0)
143 char *bol
, *eol
, *nl
, *theString
;
145 String mirrors
= get_url_to_string (mirror_url
, owner
);
149 nl
= theString
= mirrors
.cstr();}
154 for (eol
= bol
; *eol
&& *eol
!= '\n'; eol
++);
159 while (eol
> bol
&& eol
[-1] == '\r')
162 if (bol
[0] != '#' && bol
[0] > ' ')
164 char *semi
= strchr (bol
, ';');
167 site_list_type
*newsite
= new site_list_type (bol
);
168 site_list_type
& listobj
=
169 all_site_list
.registerbyobject (*newsite
);
170 if (&listobj
!= newsite
)
171 /* That site was already registered */
180 /* List of machines that should not be used by default when saved
182 #define NOSAVE1 "ftp://sources.redhat.com/"
183 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
184 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
185 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
186 #define NOSAVE3 "ftp://gcc.gnu.org/"
187 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
192 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
198 while ((fg_ret
= f
->gets (site
, 1000)))
201 char *eos
= site
+ strlen (site
) - 1;
202 while (eos
>= site
&& (*eos
== '\n' || *eos
== '\r'))
208 String tempKey
= site_list_type (String(site
)).key
;
209 if (!all_site_list
.getbykey (tempKey
))
211 /* Don't default to certain machines ever since they suffer
212 from bandwidth limitations. */
213 if (strnicmp (site
, NOSAVE1
, NOSAVE1_LEN
) == 0
214 || strnicmp (site
, NOSAVE2
, NOSAVE2_LEN
) == 0
215 || strnicmp (site
, NOSAVE3
, NOSAVE3_LEN
) == 0)
217 site_list_type
*newsite
= new site_list_type (site
);
218 site_list_type
& listobj
=
219 all_site_list
.registerbyobject (*newsite
);
220 if (&listobj
!= newsite
)
221 /* That site was already registered - shouldn't happen, but safety first */
224 /* TODO: make a site_type method to create a serach key on-the-fly from a
227 // Was it an allowed URL?
228 site_list_type
*tempSite
;
229 if ((tempSite
= all_site_list
.getbykey (tempKey
)))
230 site_list
.registerbyobject (*tempSite
);
237 do_download_site_info_thread (void *p
)
242 context
= (HANDLE
*) p
;
244 hinst
= (HINSTANCE
) (context
[0]);
245 h
= (HWND
) (context
[1]);
247 if (all_site_list
.number () == 0
248 && get_site_list (hinst
, h
))
250 // Error: Couldn't download the site info. Go back to the Net setup page.
251 MessageBox (h
, TEXT ("Can't get list of download sites.\n\
252 Make sure your network settings are corect and try again."), NULL
, MB_OK
);
254 // Tell the progress page that we're done downloading
255 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0,
260 // Everything worked, go to the site select page
261 // Tell the progress page that we're done downloading
262 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0, IDD_SITE
);
267 static HANDLE context
[2];
270 do_download_site_info (HINSTANCE hinst
, HWND owner
)
277 CreateThread (NULL
, 0, do_download_site_info_thread
, context
, 0, &threadID
);
280 bool SitePage::Create ()
282 return PropertyPage::Create (IDD_SITE
);
299 // Log all the selected URLs from the list.
300 for (size_t n
= 1; n
<= site_list
.number (); n
++)
301 log (LOG_PLAIN
) << "site: " << site_list
[n
]->url
<< endLog
;
303 Progress
.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD
);
316 // Go back to the net connection type page
321 SitePage::OnActivate ()
323 // Fill the list box with all known sites.
326 // Load the user URL box with nothing - it is in the list already.
327 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
329 // Get the enabled/disabled states of the controls set accordingly.
330 CheckControlsAndDisableAccordingly ();
334 SitePage::CheckControlsAndDisableAccordingly () const
336 DWORD ButtonFlags
= PSWIZB_BACK
;
338 // Check that at least one download site is selected.
339 if (SendMessage (GetDlgItem (IDC_URL_LIST
), LB_GETSELCOUNT
, 0, 0) > 0)
341 // At least one site selected, enable "Next".
342 ButtonFlags
|= PSWIZB_NEXT
;
344 GetOwner ()->SetButtons (ButtonFlags
);
348 SitePage::PopulateListBox ()
351 HWND listbox
= GetDlgItem (IDC_URL_LIST
);
353 // Populate the list box with the URLs.
354 SendMessage (listbox
, LB_RESETCONTENT
, 0, 0);
355 for (size_t i
= 1; i
<= all_site_list
.number (); i
++)
357 j
= SendMessage (listbox
, LB_ADDSTRING
, 0,
358 (LPARAM
) all_site_list
[i
]->displayed_url
.cstr_oneuse());
359 SendMessage (listbox
, LB_SETITEMDATA
, j
, i
);
362 // Select the selected ones.
363 for (size_t n
= 1; n
<= site_list
.number (); n
++)
365 int index
= SendMessage (listbox
, LB_FINDSTRING
, (WPARAM
) - 1,
366 (LPARAM
) site_list
[n
]->displayed_url
.cstr_oneuse());
369 // Highlight the selected item
370 SendMessage (listbox
, LB_SELITEMRANGE
, TRUE
, (index
<< 16) | index
);
371 // Make sure it's fully visible
372 SendMessage (listbox
, LB_SETCARETINDEX
, index
, FALSE
);
377 bool SitePage::OnMessageCmd (int id
, HWND hwndctl
, UINT code
)
381 case IDC_EDIT_USER_URL
:
383 // FIXME: Make Enter here cause an ADD, not a NEXT.
388 if (code
== LBN_SELCHANGE
)
390 CheckControlsAndDisableAccordingly ();
391 save_dialog (GetHWND ());
395 case IDC_BUTTON_ADD_URL
:
397 if (code
== BN_CLICKED
)
399 // User pushed the Add button.
400 String other_url
= egetString (GetHWND (), IDC_EDIT_USER_URL
);
401 if (other_url
.size())
406 site_list_type (other_url
);
407 site_list_type
& listobj
=
408 all_site_list
.registerbyobject (*newsite
);
409 if (&listobj
!= newsite
)
411 // That site was already registered
417 // Log the adding of this new URL.
418 log (LOG_BABBLE
) << "Adding site: " << other_url
<< endLog
;
421 // Assume the user wants to use it and select it for him.
422 site_list
.registerbyobject (listobj
);
424 // Update the list box.
426 // And allow the user to continue
427 CheckControlsAndDisableAccordingly ();
428 eset (GetHWND (), IDC_EDIT_USER_URL
, "");
434 // Wasn't recognized or handled.
438 // Was handled since we never got to default above.