]> cygwin.com Git - cygwin-apps/setup.git/blob - site.cc
* net.cc (do_net): Default to direct download.
[cygwin-apps/setup.git] / site.cc
1 /*
2 * Copyright (c) 2000, Red Hat, Inc.
3 *
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.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 *
14 */
15
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. */
18
19 static char *cvsid = "\n%%% $Id$\n";
20
21 #include "win32.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "dialog.h"
27 #include "resource.h"
28 #include "state.h"
29 #include "geturl.h"
30 #include "msg.h"
31 #include "concat.h"
32 #include "mount.h"
33 #include "log.h"
34
35 #include "port.h"
36
37 #define NO_IDX (-1)
38 #define OTHER_IDX (-2)
39
40 typedef struct {
41 char *url;
42 char *displayed_url;
43 char *sort_key;
44 } site_list_type;
45
46 static site_list_type *site_list = 0;
47 static int list_idx = NO_IDX;
48 static int mirror_idx = NO_IDX;
49
50 static void
51 check_if_enable_next (HWND h)
52 {
53 EnableWindow (GetDlgItem (h, IDOK), (mirror_idx != NO_IDX) ? 1 : 0);
54 }
55
56 static void
57 load_dialog (HWND h)
58 {
59 HWND listbox = GetDlgItem (h, IDC_URL_LIST);
60 SendMessage (listbox, LB_SETCURSEL, list_idx, 0);
61 check_if_enable_next (h);
62 }
63
64 static void
65 save_dialog (HWND h)
66 {
67 HWND listbox = GetDlgItem (h, IDC_URL_LIST);
68 list_idx = SendMessage (listbox, LB_GETCURSEL, 0, 0);
69 if (list_idx == LB_ERR)
70 {
71 mirror_site = 0;
72 mirror_idx = NO_IDX;
73 list_idx = NO_IDX;
74 }
75 else
76 {
77 mirror_idx = SendMessage (listbox, LB_GETITEMDATA, list_idx, 0);
78 if (mirror_idx == OTHER_IDX)
79 mirror_site = 0;
80 else
81 mirror_site = site_list[mirror_idx].url;
82 }
83 }
84
85 static void
86 get_root_dir ()
87 {
88 int istext;
89 int issystem;
90 if (root_dir)
91 return;
92 root_dir = find_root_mount (&istext, &issystem);
93 }
94
95 void
96 save_site_url ()
97 {
98 if (! MIRROR_SITE)
99 return;
100
101 get_root_dir ();
102 if (! root_dir)
103 return;
104
105 FILE *f = fopen (concat (root_dir, "/etc/setup/last-mirror", 0), "wb");
106 if (!f)
107 return;
108 fprintf (f, "%s\n", MIRROR_SITE);
109 fclose (f);
110 }
111
112 static BOOL
113 dialog_cmd (HWND h, int id, HWND hwndctl, UINT code)
114 {
115 switch (id)
116 {
117
118 case IDC_URL_LIST:
119 save_dialog (h);
120 check_if_enable_next (h);
121 break;
122
123 case IDOK:
124 save_dialog (h);
125 if (mirror_idx == OTHER_IDX)
126 NEXT (IDD_OTHER_URL);
127 else
128 {
129 other_url = 0;
130 save_site_url ();
131 NEXT (IDD_S_LOAD_INI);
132 }
133 break;
134
135 case IDC_BACK:
136 save_dialog (h);
137 NEXT (IDD_NET);
138 break;
139
140 case IDCANCEL:
141 NEXT (0);
142 break;
143 }
144 }
145
146 static BOOL CALLBACK
147 dialog_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
148 {
149 int i, j;
150 HWND listbox;
151 switch (message)
152 {
153 case WM_INITDIALOG:
154 listbox = GetDlgItem (h, IDC_URL_LIST);
155 for (i=0; site_list[i].url; i++)
156 {
157 j = SendMessage (listbox, LB_ADDSTRING, 0, (LPARAM)site_list[i].displayed_url);
158 SendMessage (listbox, LB_SETITEMDATA, j, i);
159 }
160 j = SendMessage (listbox, LB_ADDSTRING, 0, (LPARAM)"Other URL");
161 SendMessage (listbox, LB_SETITEMDATA, j, OTHER_IDX);
162 load_dialog (h);
163 return FALSE;
164 case WM_COMMAND:
165 return HANDLE_WM_COMMAND (h, wParam, lParam, dialog_cmd);
166 }
167 return FALSE;
168 }
169
170 static int
171 site_sort (const void *va, const void *vb)
172 {
173 site_list_type *a = (site_list_type *)va;
174 site_list_type *b = (site_list_type *)vb;
175 return strcmp (a->sort_key, b->sort_key);
176 }
177
178 static int
179 get_site_list (HINSTANCE h)
180 {
181 char mirror_url[1000];
182 if (LoadString (h, IDS_MIRROR_LST, mirror_url, sizeof (mirror_url)) <= 0)
183 return 1;
184 char *mirrors = get_url_to_string (mirror_url);
185 dismiss_url_status_dialog ();
186 if (!mirrors)
187 return 1;
188
189 char *bol, *eol, *nl;
190
191
192 /* null plus account for possibly missing NL plus account for "Other
193 URL" from previous run. */
194 int nmirrors = 3;
195
196 for (bol=mirrors; *bol; bol++)
197 if (*bol == '\n')
198 nmirrors++;
199
200 site_list = (site_list_type *) malloc (nmirrors * sizeof (site_list_type));
201 nmirrors = 0;
202
203 nl = mirrors;
204 while (*nl)
205 {
206 bol = nl;
207 for (eol = bol; *eol && *eol != '\n'; eol++) ;
208 if (*eol)
209 nl = eol+1;
210 else
211 nl = eol;
212 while (eol > bol && eol[-1] == '\r')
213 eol--;
214 *eol = 0;
215 if (bol[0] != '#' && bol[0] > ' ')
216 {
217 char *semi = strchr (bol, ';');
218 if (semi)
219 *semi = 0;
220 site_list[nmirrors].url = _strdup (bol);
221 site_list[nmirrors].displayed_url = _strdup (bol);
222 char *dot = strchr (site_list[nmirrors].displayed_url, '.');
223 if (dot)
224 {
225 dot = strchr (dot, '/');
226 if (dot)
227 *dot = 0;
228 }
229 site_list[nmirrors].sort_key = (char *) malloc (2*strlen (bol) + 3);
230
231 dot = site_list[nmirrors].displayed_url;
232 dot += strlen (dot);
233 char *dp = site_list[nmirrors].sort_key;
234 while (dot != site_list[nmirrors].displayed_url)
235 {
236 if (*dot == '.' || *dot == '/')
237 {
238 char *sp;
239 if (dot[3] == 0)
240 *dp++ = '~'; /* sort .com/.edu/.org together */
241 for (sp=dot+1; *sp && *sp != '.' && *sp != '/';)
242 *dp++ = *sp++;
243 *dp++ = ' ';
244 }
245 dot--;
246 }
247 *dp++ = ' ';
248 strcpy (dp, site_list[nmirrors].displayed_url);
249
250 nmirrors++;
251 }
252 }
253 site_list[nmirrors].url = 0;
254
255 qsort (site_list, nmirrors, sizeof (site_list_type), site_sort);
256
257 return 0;
258 }
259
260 /* List of machines that should not be used by default when saved
261 in "last-mirror". */
262 #define NOSAVE1 "ftp://sources.redhat.com/"
263 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
264 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
265 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
266 #define NOSAVE3 "ftp://gcc.gnu.org/"
267 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
268
269 static void
270 get_initial_list_idx ()
271 {
272 get_root_dir ();
273 if (! root_dir)
274 return;
275
276 FILE *f = fopen (concat (root_dir, "/etc/setup/last-mirror", 0), "rt");
277 if (!f)
278 return;
279
280 char site[1000];
281 site[0]='\0';
282 char * fg_ret = fgets (site, 1000, f);
283 fclose (f);
284 if (! fg_ret)
285 return;
286
287 char *eos = site + strlen (site) - 1;
288 while (eos >= site && (*eos == '\n' || *eos == '\r'))
289 *eos-- = '\0';
290
291 if (eos < site)
292 return;
293
294 int i;
295 for (i = 0; site_list[i].url; i++)
296 if (strcmp (site_list[i].url, site) == 0)
297 break;
298
299 if (! site_list[i].url)
300 {
301 /* Don't default to certain machines ever since they suffer
302 from bandwidth limitations. */
303 if (strnicmp (site, NOSAVE1, NOSAVE1_LEN) == 0
304 || strnicmp (site, NOSAVE2, NOSAVE2_LEN) == 0
305 || strnicmp (site, NOSAVE3, NOSAVE3_LEN) == 0)
306 return;
307 site_list[i].displayed_url =
308 site_list[i].url = _strdup (site);
309 site_list[i+1].url = 0;
310 }
311
312 mirror_idx = list_idx = i;
313 }
314
315 void
316 do_site (HINSTANCE h)
317 {
318 int rv = 0;
319
320 if (site_list == 0)
321 if (get_site_list (h))
322 {
323 NEXT (IDD_NET);
324 return;
325 }
326
327 get_initial_list_idx ();
328
329 rv = DialogBox (h, MAKEINTRESOURCE (IDD_SITE), 0, dialog_proc);
330 if (rv == -1)
331 fatal (IDS_DIALOG_FAILED);
332
333 if (mirror_idx != OTHER_IDX)
334 log (0, "site: %s", mirror_site);
335 }
336
This page took 0.047936 seconds and 5 git commands to generate.