]>
Commit | Line | Data |
---|---|---|
23c9e63c DD |
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 | ||
8507f105 DD |
19 | static char *cvsid = "\n%%% $Id$\n"; |
20 | ||
23c9e63c DD |
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" | |
ed3e8b9b DD |
31 | #include "concat.h" |
32 | #include "mount.h" | |
23c9e63c DD |
33 | |
34 | #include "port.h" | |
35 | ||
36 | #define NO_IDX (-1) | |
37 | #define OTHER_IDX (-2) | |
b5b282c4 DD |
38 | |
39 | typedef struct { | |
40 | char *url; | |
41 | char *displayed_url; | |
42 | char *sort_key; | |
43 | } site_list_type; | |
44 | ||
45 | static site_list_type *site_list = 0; | |
23c9e63c DD |
46 | static int list_idx = NO_IDX; |
47 | static int mirror_idx = NO_IDX; | |
48 | ||
49 | static void | |
50 | check_if_enable_next (HWND h) | |
51 | { | |
52 | EnableWindow (GetDlgItem (h, IDOK), (mirror_idx != NO_IDX) ? 1 : 0); | |
53 | } | |
54 | ||
55 | static void | |
56 | load_dialog (HWND h) | |
57 | { | |
58 | HWND listbox = GetDlgItem (h, IDC_URL_LIST); | |
59 | SendMessage (listbox, LB_SETCURSEL, list_idx, 0); | |
60 | check_if_enable_next (h); | |
61 | } | |
62 | ||
63 | static void | |
64 | save_dialog (HWND h) | |
65 | { | |
66 | HWND listbox = GetDlgItem (h, IDC_URL_LIST); | |
67 | list_idx = SendMessage (listbox, LB_GETCURSEL, 0, 0); | |
68 | if (list_idx == LB_ERR) | |
69 | { | |
70 | mirror_site = 0; | |
71 | mirror_idx = NO_IDX; | |
72 | list_idx = NO_IDX; | |
73 | } | |
74 | else | |
75 | { | |
76 | mirror_idx = SendMessage (listbox, LB_GETITEMDATA, list_idx, 0); | |
77 | if (mirror_idx == OTHER_IDX) | |
78 | mirror_site = 0; | |
79 | else | |
b5b282c4 | 80 | mirror_site = site_list[mirror_idx].url; |
23c9e63c DD |
81 | } |
82 | } | |
83 | ||
ed3e8b9b DD |
84 | static void |
85 | get_root_dir () | |
86 | { | |
87 | int istext; | |
88 | int issystem; | |
89 | if (root_dir) | |
90 | return; | |
91 | root_dir = find_root_mount (&istext, &issystem); | |
92 | } | |
93 | ||
94 | void | |
95 | save_site_url () | |
96 | { | |
97 | if (! MIRROR_SITE) | |
98 | return; | |
99 | ||
100 | get_root_dir (); | |
101 | if (! root_dir) | |
102 | return; | |
103 | ||
104 | FILE *f = fopen (concat (root_dir, "/etc/setup/last-mirror", 0), "wb"); | |
105 | if (!f) | |
106 | return; | |
107 | fprintf (f, "%s\n", MIRROR_SITE); | |
108 | fclose (f); | |
109 | } | |
110 | ||
23c9e63c DD |
111 | static BOOL |
112 | dialog_cmd (HWND h, int id, HWND hwndctl, UINT code) | |
113 | { | |
114 | switch (id) | |
115 | { | |
116 | ||
117 | case IDC_URL_LIST: | |
118 | save_dialog (h); | |
119 | check_if_enable_next (h); | |
120 | break; | |
121 | ||
122 | case IDOK: | |
ed3e8b9b | 123 | save_dialog (h); |
23c9e63c | 124 | if (mirror_idx == OTHER_IDX) |
1fd6d0a2 | 125 | NEXT (IDD_OTHER_URL); |
23c9e63c | 126 | else |
ed3e8b9b DD |
127 | { |
128 | other_url = 0; | |
129 | save_site_url (); | |
1fd6d0a2 | 130 | NEXT (IDD_S_LOAD_INI); |
ed3e8b9b | 131 | } |
23c9e63c DD |
132 | break; |
133 | ||
134 | case IDC_BACK: | |
ed3e8b9b | 135 | save_dialog (h); |
1fd6d0a2 | 136 | NEXT (IDD_NET); |
23c9e63c DD |
137 | break; |
138 | ||
139 | case IDCANCEL: | |
1fd6d0a2 | 140 | NEXT (0); |
23c9e63c DD |
141 | break; |
142 | } | |
143 | } | |
144 | ||
145 | static BOOL CALLBACK | |
146 | dialog_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam) | |
147 | { | |
148 | int i, j; | |
149 | HWND listbox; | |
150 | switch (message) | |
151 | { | |
152 | case WM_INITDIALOG: | |
153 | listbox = GetDlgItem (h, IDC_URL_LIST); | |
b5b282c4 | 154 | for (i=0; site_list[i].url; i++) |
23c9e63c | 155 | { |
b5b282c4 | 156 | j = SendMessage (listbox, LB_ADDSTRING, 0, (LPARAM)site_list[i].displayed_url); |
23c9e63c DD |
157 | SendMessage (listbox, LB_SETITEMDATA, j, i); |
158 | } | |
159 | j = SendMessage (listbox, LB_ADDSTRING, 0, (LPARAM)"Other URL"); | |
160 | SendMessage (listbox, LB_SETITEMDATA, j, OTHER_IDX); | |
ed3e8b9b | 161 | load_dialog (h); |
23c9e63c DD |
162 | return FALSE; |
163 | case WM_COMMAND: | |
1fd6d0a2 | 164 | return HANDLE_WM_COMMAND (h, wParam, lParam, dialog_cmd); |
23c9e63c DD |
165 | } |
166 | return FALSE; | |
167 | } | |
168 | ||
b5b282c4 DD |
169 | static int |
170 | site_sort (const void *va, const void *vb) | |
171 | { | |
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); | |
175 | } | |
176 | ||
23c9e63c DD |
177 | static int |
178 | get_site_list (HINSTANCE h) | |
179 | { | |
180 | char mirror_url[1000]; | |
ed3e8b9b | 181 | if (LoadString (h, IDS_MIRROR_LST, mirror_url, sizeof (mirror_url)) <= 0) |
23c9e63c DD |
182 | return 1; |
183 | char *mirrors = get_url_to_string (mirror_url); | |
184 | dismiss_url_status_dialog (); | |
185 | if (!mirrors) | |
186 | return 1; | |
187 | ||
188 | char *bol, *eol, *nl; | |
189 | ||
ed3e8b9b DD |
190 | |
191 | /* null plus account for possibly missing NL plus account for "Other | |
192 | URL" from previous run. */ | |
193 | int nmirrors = 3; | |
194 | ||
23c9e63c DD |
195 | for (bol=mirrors; *bol; bol++) |
196 | if (*bol == '\n') | |
197 | nmirrors ++; | |
198 | ||
b5b282c4 | 199 | site_list = (site_list_type *) malloc (nmirrors * sizeof (site_list_type)); |
23c9e63c DD |
200 | nmirrors = 0; |
201 | ||
202 | nl = mirrors; | |
203 | while (*nl) | |
204 | { | |
205 | bol = nl; | |
206 | for (eol = bol; *eol && *eol != '\n'; eol++) ; | |
207 | if (*eol) | |
208 | nl = eol+1; | |
209 | else | |
210 | nl = eol; | |
211 | while (eol > bol && eol[-1] == '\r') | |
212 | eol--; | |
213 | *eol = 0; | |
214 | if (bol[0] != '#' && bol[0] > ' ') | |
215 | { | |
b5b282c4 | 216 | char *semi = strchr (bol, ';'); |
23c9e63c DD |
217 | if (semi) |
218 | *semi = 0; | |
b5b282c4 DD |
219 | site_list[nmirrors].url = _strdup (bol); |
220 | site_list[nmirrors].displayed_url = _strdup (bol); | |
221 | char *dot = strchr (site_list[nmirrors].displayed_url, '.'); | |
222 | if (dot) | |
223 | { | |
224 | dot = strchr (dot, '/'); | |
225 | if (dot) | |
226 | *dot = 0; | |
227 | } | |
228 | site_list[nmirrors].sort_key = (char *) malloc (2*strlen (bol) + 3); | |
229 | ||
230 | dot = site_list[nmirrors].displayed_url; | |
231 | dot += strlen (dot); | |
232 | char *dp = site_list[nmirrors].sort_key; | |
233 | while (dot != site_list[nmirrors].displayed_url) | |
234 | { | |
235 | if (*dot == '.' || *dot == '/') | |
236 | { | |
237 | char *sp; | |
238 | if (dot[3] == 0) | |
239 | *dp++ = '~'; /* sort .com/.edu/.org together */ | |
240 | for (sp=dot+1; *sp && *sp != '.' && *sp != '/';) | |
241 | *dp++ = *sp++; | |
242 | *dp++ = ' '; | |
243 | } | |
244 | dot--; | |
245 | } | |
246 | *dp++ = ' '; | |
247 | strcpy (dp, site_list[nmirrors].displayed_url); | |
248 | ||
23c9e63c DD |
249 | nmirrors++; |
250 | } | |
251 | } | |
b5b282c4 DD |
252 | site_list[nmirrors].url = 0; |
253 | ||
254 | qsort (site_list, nmirrors, sizeof (site_list_type), site_sort); | |
255 | ||
23c9e63c DD |
256 | return 0; |
257 | } | |
258 | ||
ed3e8b9b DD |
259 | static void |
260 | get_initial_list_idx () | |
261 | { | |
262 | get_root_dir (); | |
263 | if (! root_dir) | |
264 | return; | |
265 | ||
266 | FILE *f = fopen (concat (root_dir, "/etc/setup/last-mirror", 0), "rt"); | |
267 | if (!f) | |
268 | return; | |
269 | ||
270 | char site[1000]; | |
271 | site[0]='\0'; | |
272 | char * fg_ret = fgets (site, 1000, f); | |
273 | fclose (f); | |
274 | if (! fg_ret) | |
275 | return; | |
276 | ||
277 | char *eos = site + strlen (site) - 1; | |
278 | while (eos >= site && (*eos == '\n' || *eos == '\r')) | |
279 | *eos-- = '\0'; | |
280 | ||
281 | if (eos < site) | |
282 | return; | |
283 | ||
284 | int i; | |
b5b282c4 DD |
285 | for (i = 0; site_list[i].url; i++) |
286 | if (strcmp (site_list[i].url, site) == 0) | |
ed3e8b9b DD |
287 | break; |
288 | ||
b5b282c4 | 289 | if (! site_list[i].url) |
ed3e8b9b | 290 | { |
b5b282c4 DD |
291 | site_list[i].displayed_url = |
292 | site_list[i].url = _strdup (site); | |
293 | site_list[i+1].url = 0; | |
ed3e8b9b DD |
294 | } |
295 | ||
296 | mirror_idx = list_idx = i; | |
297 | } | |
298 | ||
23c9e63c DD |
299 | void |
300 | do_site (HINSTANCE h) | |
301 | { | |
302 | int rv = 0; | |
303 | ||
304 | if (site_list == 0) | |
305 | if (get_site_list (h)) | |
306 | { | |
1fd6d0a2 | 307 | NEXT (0); |
23c9e63c DD |
308 | return; |
309 | } | |
310 | ||
ed3e8b9b DD |
311 | get_initial_list_idx (); |
312 | ||
23c9e63c DD |
313 | rv = DialogBox (h, MAKEINTRESOURCE (IDD_SITE), 0, dialog_proc); |
314 | if (rv == -1) | |
315 | fatal (IDS_DIALOG_FAILED); | |
316 | } | |
317 |