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. */
37 #define OTHER_IDX (-2)
45 static site_list_type *site_list = 0;
46 static int list_idx = NO_IDX;
47 static int mirror_idx = NO_IDX;
50 check_if_enable_next (HWND h)
52 EnableWindow (GetDlgItem (h, IDOK), (mirror_idx != NO_IDX) ? 1 : 0);
58 HWND listbox = GetDlgItem (h, IDC_URL_LIST);
59 SendMessage (listbox, LB_SETCURSEL, list_idx, 0);
60 check_if_enable_next (h);
66 HWND listbox = GetDlgItem (h, IDC_URL_LIST);
67 list_idx = SendMessage (listbox, LB_GETCURSEL, 0, 0);
68 if (list_idx == LB_ERR)
76 mirror_idx = SendMessage (listbox, LB_GETITEMDATA, list_idx, 0);
77 if (mirror_idx == OTHER_IDX)
80 mirror_site = site_list[mirror_idx].url;
87 int issystem, isnative;
90 root_dir = find_root_location (&issystem, &isnative);
103 FILE *f = fopen (concat (root_dir, XEMACS_SETUP_DIR, "last-mirror", 0), "wb");
106 fprintf (f, "%s\n", MIRROR_SITE);
111 dialog_cmd (HWND h, int id, HWND hwndctl, UINT code)
118 check_if_enable_next (h);
123 if (mirror_idx == OTHER_IDX)
124 NEXT (IDD_OTHER_URL);
129 NEXT (IDD_S_LOAD_INI);
146 dialog_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
153 listbox = GetDlgItem (h, IDC_URL_LIST);
154 for (i=0; site_list[i].url; i++)
156 j = SendMessage (listbox, LB_ADDSTRING, 0, (LPARAM)site_list[i].displayed_url);
157 SendMessage (listbox, LB_SETITEMDATA, j, i);
159 j = SendMessage (listbox, LB_ADDSTRING, 0, (LPARAM)"Other URL");
160 SendMessage (listbox, LB_SETITEMDATA, j, OTHER_IDX);
164 return HANDLE_WM_COMMAND (h, wParam, lParam, dialog_cmd);
170 site_sort (const void *va, const void *vb)
172 site_list_type *a = (site_list_type *)va;
173 site_list_type *b = (site_list_type *)vb;
174 return strcmp (a->sort_key, b->sort_key);
178 get_site_list (HINSTANCE h)
180 char mirror_url[1000];
181 if (LoadString (h, IDS_MIRROR_LST, mirror_url, sizeof (mirror_url)) <= 0)
183 char *mirrors = get_url_to_string (mirror_url);
184 dismiss_url_status_dialog ();
188 char *bol, *eol, *nl;
191 /* null plus account for possibly missing NL plus account for "Other
192 URL" from previous run. */
195 for (bol=mirrors; *bol; bol++)
199 site_list = (site_list_type *) malloc (nmirrors * sizeof (site_list_type));
206 for (eol = bol; *eol && *eol != '\n'; eol++) ;
211 while (eol > bol && eol[-1] == '\r')
214 if (bol[0] != '#' && bol[0] > ' ')
216 char *semi = strchr (bol, ';');
219 site_list[nmirrors].url = _strdup (bol);
220 site_list[nmirrors].displayed_url = _strdup (bol);
221 char *dot = strchr (site_list[nmirrors].displayed_url, '.');
224 dot = strchr (dot, '/');
228 site_list[nmirrors].sort_key = (char *) malloc (2*strlen (bol) + 3);
230 dot = site_list[nmirrors].displayed_url;
232 char *dp = site_list[nmirrors].sort_key;
233 while (dot != site_list[nmirrors].displayed_url)
235 if (*dot == '.' || *dot == '/')
239 *dp++ = '~'; /* sort .com/.edu/.org together */
240 for (sp=dot+1; *sp && *sp != '.' && *sp != '/';)
247 strcpy (dp, site_list[nmirrors].displayed_url);
252 site_list[nmirrors].url = 0;
254 qsort (site_list, nmirrors, sizeof (site_list_type), site_sort);
259 /* List of machines that should not be used by default when saved
261 #define NOSAVE1 "ftp://ftp.xemacs.org/"
262 #define NOSAVE1_LEN (sizeof ("ftp://ftp.xemacs.org/") - 1)
265 get_initial_list_idx ()
271 FILE *f = fopen (concat (root_dir, XEMACS_SETUP_DIR, "last-mirror", 0), "rt");
277 char * fg_ret = fgets (site, 1000, f);
282 char *eos = site + strlen (site) - 1;
283 while (eos >= site && (*eos == '\n' || *eos == '\r'))
290 for (i = 0; site_list[i].url; i++)
291 if (strcmp (site_list[i].url, site) == 0)
294 if (! site_list[i].url)
296 /* Don't default to certain machines ever since they suffer
297 from bandwidth limitations. */
298 if (strnicmp (site, NOSAVE1, NOSAVE1_LEN) == 0)
300 site_list[i].displayed_url =
301 site_list[i].url = _strdup (site);
302 site_list[i+1].url = 0;
305 mirror_idx = list_idx = i;
309 do_site (HINSTANCE h)
314 if (get_site_list (h))
320 get_initial_list_idx ();
322 rv = DialogBox (h, MAKEINTRESOURCE (IDD_SITE), 0, dialog_proc);
324 fatal (IDS_DIALOG_FAILED);
326 if (mirror_idx != OTHER_IDX)
327 log (0, "site: %s", mirror_site);