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 "io_stream.h"
43 #include "propsheet.h"
46 extern ThreeBarProgressPage Progress
;
49 #define OTHER_IDX (-2)
51 list
< site_list_type
, const char *, strcasecmp
> site_list
;
52 list
< site_list_type
, const char *, strcasecmp
> all_site_list
;
53 static int mirror_idx
= NO_IDX
;
56 site_list_type::init (char const *newurl
)
58 url
= _strdup (newurl
);
59 displayed_url
= _strdup (newurl
);
60 char *dot
= strchr (displayed_url
, '.');
63 dot
= strchr (dot
, '/');
67 key
= (char *) malloc (2 * strlen (newurl
) + 3);
72 while (dot
!= displayed_url
)
74 if (*dot
== '.' || *dot
== '/')
78 *dp
++ = '~'; /* sort .com/.edu/.org together */
79 for (sp
= dot
+ 1; *sp
&& *sp
!= '.' && *sp
!= '/';)
86 strcpy (dp
, displayed_url
);
89 site_list_type::site_list_type (char const *newurl
)
95 check_if_enable_next (HWND h
)
97 EnableWindow (GetDlgItem (h
, IDOK
),
98 SendMessage (GetDlgItem (h
, IDC_URL_LIST
), LB_GETSELCOUNT
, 0,
105 HWND listbox
= GetDlgItem (h
, IDC_URL_LIST
);
106 for (size_t n
= 1; n
<= site_list
.number (); n
++)
108 int index
= SendMessage (listbox
, LB_FINDSTRING
, (WPARAM
) - 1,
109 (LPARAM
) site_list
[n
]->displayed_url
);
112 // Highlight the selected item
113 SendMessage (listbox
, LB_SELITEMRANGE
, TRUE
, (index
<< 16) | index
);
114 // Make sure it's fully visible
115 SendMessage (listbox
, LB_SETCARETINDEX
, index
, FALSE
);
118 check_if_enable_next (h
);
124 HWND listbox
= GetDlgItem (h
, IDC_URL_LIST
);
126 while (site_list
.number () > 0)
127 /* we don't delete the object because it's stored in the all_site_list. */
128 site_list
.removebyindex (1);
129 int sel_count
= SendMessage (listbox
, LB_GETSELCOUNT
, 0, 0);
132 int sel_buffer
[sel_count
];
133 int sel_count2
= SendMessage (listbox
, LB_GETSELITEMS
, sel_count
,
134 (LPARAM
) sel_buffer
);
135 if (sel_count
!= sel_count2
)
139 for (int n
= 0; n
< sel_count
; n
++)
142 SendMessage (listbox
, LB_GETITEMDATA
, sel_buffer
[n
], 0);
143 if (mirror
== OTHER_IDX
)
144 mirror_idx
= OTHER_IDX
;
146 site_list
.registerbyobject (*all_site_list
[mirror
]);
158 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
159 for (size_t n
= 1; n
<= site_list
.number (); n
++)
163 char temp
[_MAX_PATH
];
164 /* TODO: potential buffer overflow. we need snprintf asap. */
165 // FIXME: write all selected sites
166 sprintf (temp
, "%s\n", site_list
[n
]->url
);
167 f
->write (temp
, strlen (temp
));
174 dialog_cmd (HWND h
, int id
, HWND hwndctl
, UINT code
)
180 check_if_enable_next (h
);
187 get_site_list (HINSTANCE h
, HWND owner
)
189 char mirror_url
[1000];
191 if (LoadString (h
, IDS_MIRROR_LST
, mirror_url
, sizeof (mirror_url
)) <= 0)
193 char *mirrors
= get_url_to_string (mirror_url
, owner
);
197 char *bol
, *eol
, *nl
;
203 for (eol
= bol
; *eol
&& *eol
!= '\n'; eol
++);
208 while (eol
> bol
&& eol
[-1] == '\r')
211 if (bol
[0] != '#' && bol
[0] > ' ')
213 char *semi
= strchr (bol
, ';');
216 site_list_type
*newsite
= new site_list_type (bol
);
217 site_list_type
& listobj
=
218 all_site_list
.registerbyobject (*newsite
);
219 if (&listobj
!= newsite
)
220 /* That site was already registered */
229 /* List of machines that should not be used by default when saved
231 #define NOSAVE1 "ftp://sources.redhat.com/"
232 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
233 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
234 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
235 #define NOSAVE3 "ftp://gcc.gnu.org/"
236 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
241 io_stream
*f
= io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
247 while ((fg_ret
= f
->gets (site
, 1000)))
250 char *eos
= site
+ strlen (site
) - 1;
251 while (eos
>= site
&& (*eos
== '\n' || *eos
== '\r'))
258 for (size_t i
= 1; !found
&& i
<= all_site_list
.number (); i
++)
259 if (!strcasecmp (site
, all_site_list
[i
]->url
))
264 /* Don't default to certain machines ever since they suffer
265 from bandwidth limitations. */
266 if (strnicmp (site
, NOSAVE1
, NOSAVE1_LEN
) == 0
267 || strnicmp (site
, NOSAVE2
, NOSAVE2_LEN
) == 0
268 || strnicmp (site
, NOSAVE3
, NOSAVE3_LEN
) == 0)
270 site_list_type
*newsite
= new site_list_type (site
);
271 site_list_type
& listobj
=
272 all_site_list
.registerbyobject (*newsite
);
273 if (&listobj
!= newsite
)
274 /* That site was already registered - shouldn't happen, but safety first */
277 /* TODO: make a site_type method to create a serach key on-the-fly from a
281 for (size_t i
= 1; !found
&& i
<= all_site_list
.number (); i
++)
282 if (!strcasecmp (site
, all_site_list
[i
]->url
))
283 site_list
.registerbyobject (*all_site_list
[i
]);
290 do_download_site_info_thread (void *p
)
295 context
= (HANDLE
*) p
;
297 hinst
= (HINSTANCE
) (context
[0]);
298 h
= (HWND
) (context
[1]);
300 if (all_site_list
.number () == 0)
302 if (get_site_list (hinst
, h
))
304 // Error: Couldn't download the site info. Go back to the Net setup page.
307 // Tell the progress page that we're done downloading
308 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0,
315 // Everything worked, go to the site select page
318 // Tell the progress page that we're done downloading
319 Progress
.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE
, 0, IDD_SITE
);
324 static HANDLE context
[2];
327 do_download_site_info (HINSTANCE hinst
, HWND owner
)
333 _beginthread (do_download_site_info_thread
, 0, context
);
340 return PropertyPage::Create (NULL
, dialog_cmd
, IDD_SITE
);
352 listbox
= GetDlgItem (IDC_URL_LIST
);
353 for (size_t i
= 1; i
<= all_site_list
.number (); i
++)
356 SendMessage (listbox
, LB_ADDSTRING
, 0,
357 (LPARAM
) all_site_list
[i
]->displayed_url
);
358 SendMessage (listbox
, LB_SETITEMDATA
, j
, i
);
360 j
= SendMessage (listbox
, LB_ADDSTRING
, 0, (LPARAM
) "Other URL");
361 SendMessage (listbox
, LB_SETITEMDATA
, j
, OTHER_IDX
);
371 if (mirror_idx
== OTHER_IDX
)
372 NEXT (IDD_OTHER_URL
);
376 NEXT (IDD_S_LOAD_INI
);
378 for (size_t n
= 1; n
<= site_list
.number (); n
++)
379 log (0, "site: %s", site_list
[n
]->url
);
381 Progress
.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD
);