]> cygwin.com Git - cygwin-apps/setup.git/blob - site.cc
2001-12-22 Gary R. Van Sickle <g.r.vansickle@worldnet.att.net>
[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 #if 0
20 static const char *cvsid =
21 "\n%%% $Id$\n";
22 #endif
23
24 #include "win32.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <process.h>
29
30 #include "dialog.h"
31 #include "resource.h"
32 #include "state.h"
33 #include "geturl.h"
34 #include "msg.h"
35 #include "concat.h"
36 #include "log.h"
37 #include "io_stream.h"
38 #include "site.h"
39
40 #include "port.h"
41
42 #include "site.h"
43 #include "propsheet.h"
44
45 #include "threebar.h"
46 extern ThreeBarProgressPage Progress;
47
48 #define NO_IDX (-1)
49 #define OTHER_IDX (-2)
50
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;
54
55 void
56 site_list_type::init (char const *newurl)
57 {
58 url = _strdup (newurl);
59 displayed_url = _strdup (newurl);
60 char *dot = strchr (displayed_url, '.');
61 if (dot)
62 {
63 dot = strchr (dot, '/');
64 if (dot)
65 *dot = 0;
66 }
67 key = (char *) malloc (2 * strlen (newurl) + 3);
68
69 dot = displayed_url;
70 dot += strlen (dot);
71 char *dp = key;
72 while (dot != displayed_url)
73 {
74 if (*dot == '.' || *dot == '/')
75 {
76 char *sp;
77 if (dot[3] == 0)
78 *dp++ = '~'; /* sort .com/.edu/.org together */
79 for (sp = dot + 1; *sp && *sp != '.' && *sp != '/';)
80 *dp++ = *sp++;
81 *dp++ = ' ';
82 }
83 dot--;
84 }
85 *dp++ = ' ';
86 strcpy (dp, displayed_url);
87 }
88
89 site_list_type::site_list_type (char const *newurl)
90 {
91 init (newurl);
92 }
93
94 static void
95 check_if_enable_next (HWND h)
96 {
97 EnableWindow (GetDlgItem (h, IDOK),
98 SendMessage (GetDlgItem (h, IDC_URL_LIST), LB_GETSELCOUNT, 0,
99 0) > 0 ? 1 : 0);
100 }
101
102 static void
103 load_dialog (HWND h)
104 {
105 HWND listbox = GetDlgItem (h, IDC_URL_LIST);
106 for (size_t n = 1; n <= site_list.number (); n++)
107 {
108 int index = SendMessage (listbox, LB_FINDSTRING, (WPARAM) - 1,
109 (LPARAM) site_list[n]->displayed_url);
110 if (index != LB_ERR)
111 {
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);
116 }
117 }
118 check_if_enable_next (h);
119 }
120
121 static void
122 save_dialog (HWND h)
123 {
124 HWND listbox = GetDlgItem (h, IDC_URL_LIST);
125 mirror_idx = 0;
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);
130 if (sel_count > 0)
131 {
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)
136 {
137 NEXT (IDD_SITE);
138 }
139 for (int n = 0; n < sel_count; n++)
140 {
141 int mirror =
142 SendMessage (listbox, LB_GETITEMDATA, sel_buffer[n], 0);
143 if (mirror == OTHER_IDX)
144 mirror_idx = OTHER_IDX;
145 else
146 site_list.registerbyobject (*all_site_list[mirror]);
147 }
148 }
149 else
150 {
151 NEXT (IDD_SITE);
152 }
153 }
154
155 void
156 save_site_url ()
157 {
158 io_stream *f = io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
159 for (size_t n = 1; n <= site_list.number (); n++)
160 {
161 if (f)
162 {
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));
168 }
169 }
170 delete f;
171 }
172
173 static BOOL
174 dialog_cmd (HWND h, int id, HWND hwndctl, UINT code)
175 {
176 switch (id)
177 {
178
179 case IDC_URL_LIST:
180 check_if_enable_next (h);
181 break;
182 }
183 return 0;
184 }
185
186 static int
187 get_site_list (HINSTANCE h, HWND owner)
188 {
189 char mirror_url[1000];
190
191 if (LoadString (h, IDS_MIRROR_LST, mirror_url, sizeof (mirror_url)) <= 0)
192 return 1;
193 char *mirrors = get_url_to_string (mirror_url, owner);
194 if (!mirrors)
195 return 1;
196
197 char *bol, *eol, *nl;
198
199 nl = mirrors;
200 while (*nl)
201 {
202 bol = nl;
203 for (eol = bol; *eol && *eol != '\n'; eol++);
204 if (*eol)
205 nl = eol + 1;
206 else
207 nl = eol;
208 while (eol > bol && eol[-1] == '\r')
209 eol--;
210 *eol = 0;
211 if (bol[0] != '#' && bol[0] > ' ')
212 {
213 char *semi = strchr (bol, ';');
214 if (semi)
215 *semi = 0;
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 */
221 delete newsite;
222 }
223 }
224 delete[]mirrors;
225
226 return 0;
227 }
228
229 /* List of machines that should not be used by default when saved
230 in "last-mirror". */
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)
237
238 static void
239 get_saved_sites ()
240 {
241 io_stream *f = io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
242 if (!f)
243 return;
244
245 char site[1000];
246 char *fg_ret;
247 while ((fg_ret = f->gets (site, 1000)))
248 {
249
250 char *eos = site + strlen (site) - 1;
251 while (eos >= site && (*eos == '\n' || *eos == '\r'))
252 *eos-- = '\0';
253
254 if (eos < site)
255 continue;
256
257 bool found = false;
258 for (size_t i = 1; !found && i <= all_site_list.number (); i++)
259 if (!strcasecmp (site, all_site_list[i]->url))
260 found = true;
261
262 if (!found)
263 {
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)
269 return;
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 */
275 delete newsite;
276 }
277 /* TODO: make a site_type method to create a serach key on-the-fly from a
278 URL
279 */
280 found = false;
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]);
284 }
285 delete f;
286
287 }
288
289 static void
290 do_download_site_info_thread (void *p)
291 {
292 HANDLE *context;
293 HINSTANCE hinst;
294 HWND h;
295 context = (HANDLE *) p;
296
297 hinst = (HINSTANCE) (context[0]);
298 h = (HWND) (context[1]);
299
300 if (all_site_list.number () == 0)
301 {
302 if (get_site_list (hinst, h))
303 {
304 // Error: Couldn't download the site info. Go back to the Net setup page.
305 NEXT (IDD_NET);
306
307 // Tell the progress page that we're done downloading
308 Progress.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE, 0,
309 IDD_NET);
310
311 _endthread ();
312 }
313 }
314
315 // Everything worked, go to the site select page
316 NEXT (IDD_SITE);
317
318 // Tell the progress page that we're done downloading
319 Progress.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE, 0, IDD_SITE);
320
321 _endthread ();
322 }
323
324 static HANDLE context[2];
325
326 void
327 do_download_site_info (HINSTANCE hinst, HWND owner)
328 {
329
330 context[0] = hinst;
331 context[1] = owner;
332
333 _beginthread (do_download_site_info_thread, 0, context);
334
335 }
336
337 bool
338 SitePage::Create ()
339 {
340 return PropertyPage::Create (NULL, dialog_cmd, IDD_SITE);
341 }
342
343 void
344 SitePage::OnInit ()
345 {
346 HWND h = GetHWND ();
347 int j;
348 HWND listbox;
349
350 get_saved_sites ();
351
352 listbox = GetDlgItem (IDC_URL_LIST);
353 for (size_t i = 1; i <= all_site_list.number (); i++)
354 {
355 j =
356 SendMessage (listbox, LB_ADDSTRING, 0,
357 (LPARAM) all_site_list[i]->displayed_url);
358 SendMessage (listbox, LB_SETITEMDATA, j, i);
359 }
360 j = SendMessage (listbox, LB_ADDSTRING, 0, (LPARAM) "Other URL");
361 SendMessage (listbox, LB_SETITEMDATA, j, OTHER_IDX);
362 load_dialog (h);
363 }
364
365 long
366 SitePage::OnNext ()
367 {
368 HWND h = GetHWND ();
369
370 save_dialog (h);
371 if (mirror_idx == OTHER_IDX)
372 NEXT (IDD_OTHER_URL);
373 else
374 {
375 save_site_url ();
376 NEXT (IDD_S_LOAD_INI);
377
378 for (size_t n = 1; n <= site_list.number (); n++)
379 log (0, "site: %s", site_list[n]->url);
380
381 Progress.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD);
382 return IDD_INSTATUS;
383 }
384
385 return 0;
386 }
387
388 long
389 SitePage::OnBack ()
390 {
391 HWND h = GetHWND ();
392
393 save_dialog (h);
394 NEXT (IDD_NET);
395 return 0;
396 }
This page took 0.052891 seconds and 6 git commands to generate.