]> cygwin.com Git - cygwin-apps/setup.git/blob - site.cc
2002-01-27 Robert Collins <rbtcollins@hotmail.com>
[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 list < site_list_type, const char *, strcasecmp > site_list;
49 list < site_list_type, const char *, strcasecmp > all_site_list;
50
51 static char *other_url = 0;
52
53 void
54 site_list_type::init (char const *newurl)
55 {
56 url = new char [strlen (newurl) +1];
57 strcpy (url, newurl);
58 displayed_url = new char [strlen (newurl) +1];
59 strcpy (displayed_url, newurl);
60 char *dot = strchr (displayed_url, '.');
61 if (dot)
62 {
63 dot = strchr (dot, '/');
64 if (dot)
65 *dot = 0;
66 }
67 key = new char[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 save_dialog (HWND h)
96 {
97 // Remove anything that was previously in the selected site list.
98 while (site_list.number () > 0)
99 {
100 // we don't delete the object because it's stored in the all_site_list.
101 site_list.removebyindex (1);
102 }
103
104 HWND listbox = GetDlgItem (h, IDC_URL_LIST);
105 int sel_count = SendMessage (listbox, LB_GETSELCOUNT, 0, 0);
106 if (sel_count > 0)
107 {
108 int sel_buffer[sel_count];
109 int sel_count2 = SendMessage (listbox, LB_GETSELITEMS, sel_count,
110 (LPARAM) sel_buffer);
111 if (sel_count != sel_count2)
112 {
113 NEXT (IDD_SITE);
114 }
115 for (int n = 0; n < sel_count; n++)
116 {
117 int mirror =
118 SendMessage (listbox, LB_GETITEMDATA, sel_buffer[n], 0);
119 site_list.registerbyobject (*all_site_list[mirror]);
120 }
121 }
122 }
123
124 void
125 save_site_url ()
126 {
127 io_stream *f = io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
128 for (size_t n = 1; n <= site_list.number (); n++)
129 {
130 if (f)
131 {
132 // FIXME: write all selected sites
133 TCHAR *temp;
134 temp = new TCHAR[sizeof (TCHAR) * (strlen (site_list[n]->url) + 2)];
135 sprintf (temp, "%s\n", site_list[n]->url);
136 f->write (temp, strlen (temp));
137 delete[] temp;
138 }
139 }
140 delete f;
141 }
142
143 static int
144 get_site_list (HINSTANCE h, HWND owner)
145 {
146 char mirror_url[1000];
147
148 if (LoadString (h, IDS_MIRROR_LST, mirror_url, sizeof (mirror_url)) <= 0)
149 return 1;
150 char *mirrors = get_url_to_string (mirror_url, owner);
151 if (!mirrors)
152 return 1;
153
154 char *bol, *eol, *nl;
155
156 nl = mirrors;
157 while (*nl)
158 {
159 bol = nl;
160 for (eol = bol; *eol && *eol != '\n'; eol++);
161 if (*eol)
162 nl = eol + 1;
163 else
164 nl = eol;
165 while (eol > bol && eol[-1] == '\r')
166 eol--;
167 *eol = 0;
168 if (bol[0] != '#' && bol[0] > ' ')
169 {
170 char *semi = strchr (bol, ';');
171 if (semi)
172 *semi = 0;
173 site_list_type *newsite = new site_list_type (bol);
174 site_list_type & listobj =
175 all_site_list.registerbyobject (*newsite);
176 if (&listobj != newsite)
177 /* That site was already registered */
178 delete newsite;
179 }
180 }
181 delete[] mirrors;
182
183 return 0;
184 }
185
186 /* List of machines that should not be used by default when saved
187 in "last-mirror". */
188 #define NOSAVE1 "ftp://sources.redhat.com/"
189 #define NOSAVE1_LEN (sizeof ("ftp://sources.redhat.com/") - 1)
190 #define NOSAVE2 "ftp://sourceware.cygnus.com/"
191 #define NOSAVE2_LEN (sizeof ("ftp://sourceware.cygnus.com/") - 1)
192 #define NOSAVE3 "ftp://gcc.gnu.org/"
193 #define NOSAVE3_LEN (sizeof ("ftp://gcc.gnu.org/") - 1)
194
195 static void
196 get_saved_sites ()
197 {
198 io_stream *f = io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
199 if (!f)
200 return;
201
202 char site[1000];
203 char *fg_ret;
204 while ((fg_ret = f->gets (site, 1000)))
205 {
206
207 char *eos = site + strlen (site) - 1;
208 while (eos >= site && (*eos == '\n' || *eos == '\r'))
209 *eos-- = '\0';
210
211 if (eos < site)
212 continue;
213
214 bool found = false;
215 for (size_t i = 1; !found && i <= all_site_list.number (); i++)
216 if (!strcasecmp (site, all_site_list[i]->url))
217 found = true;
218
219 if (!found)
220 {
221 /* Don't default to certain machines ever since they suffer
222 from bandwidth limitations. */
223 if (strnicmp (site, NOSAVE1, NOSAVE1_LEN) == 0
224 || strnicmp (site, NOSAVE2, NOSAVE2_LEN) == 0
225 || strnicmp (site, NOSAVE3, NOSAVE3_LEN) == 0)
226 return;
227 site_list_type *newsite = new site_list_type (site);
228 site_list_type & listobj =
229 all_site_list.registerbyobject (*newsite);
230 if (&listobj != newsite)
231 /* That site was already registered - shouldn't happen, but safety first */
232 delete newsite;
233 }
234 /* TODO: make a site_type method to create a serach key on-the-fly from a
235 URL
236 */
237 found = false;
238 for (size_t i = 1; !found && i <= all_site_list.number (); i++)
239 if (!strcasecmp (site, all_site_list[i]->url))
240 site_list.registerbyobject (*all_site_list[i]);
241 }
242 delete f;
243
244 }
245
246 static void
247 do_download_site_info_thread (void *p)
248 {
249 HANDLE *context;
250 HINSTANCE hinst;
251 HWND h;
252 context = (HANDLE *) p;
253
254 hinst = (HINSTANCE) (context[0]);
255 h = (HWND) (context[1]);
256
257 if (all_site_list.number () == 0)
258 {
259 if (get_site_list (hinst, h))
260 {
261 // Error: Couldn't download the site info. Go back to the Net setup page.
262 MessageBox (h, TEXT ("Can't get list of download sites.\n\
263 Make sure your network settings are corect and try again."), NULL, MB_OK);
264
265 // Tell the progress page that we're done downloading
266 Progress.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE, 0,
267 IDD_NET);
268
269 _endthread ();
270 }
271 }
272
273 // Everything worked, go to the site select page
274
275 // Tell the progress page that we're done downloading
276 Progress.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE, 0, IDD_SITE);
277
278 _endthread ();
279 }
280
281 static HANDLE context[2];
282
283 void
284 do_download_site_info (HINSTANCE hinst, HWND owner)
285 {
286
287 context[0] = hinst;
288 context[1] = owner;
289
290 _beginthread (do_download_site_info_thread, 0, context);
291
292 }
293
294 bool SitePage::Create ()
295 {
296 return PropertyPage::Create (IDD_SITE);
297 }
298
299 void
300 SitePage::OnInit ()
301 {
302 get_saved_sites ();
303 }
304
305 long
306 SitePage::OnNext ()
307 {
308 HWND h = GetHWND ();
309
310 save_dialog (h);
311 save_site_url ();
312
313 // Log all the selected URLs from the list.
314 for (size_t n = 1; n <= site_list.number (); n++)
315 log (0, "site: %s", site_list[n]->url);
316
317 Progress.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD);
318 return IDD_INSTATUS;
319
320 return 0;
321 }
322
323 long
324 SitePage::OnBack ()
325 {
326 HWND h = GetHWND ();
327
328 save_dialog (h);
329
330 // Go back to the net connection type page
331 return 0;
332 }
333
334 void
335 SitePage::OnActivate ()
336 {
337 // Fill the list box with all known sites.
338 PopulateListBox ();
339
340 // Load the user URL box with whatever it was last time.
341 eset (GetHWND (), IDC_EDIT_USER_URL, other_url);
342
343 // Get the enabled/disabled states of the controls set accordingly.
344 CheckControlsAndDisableAccordingly ();
345 }
346
347 void
348 SitePage::CheckControlsAndDisableAccordingly () const
349 {
350 DWORD ButtonFlags = PSWIZB_BACK;
351
352 // Check that at least one download site is selected.
353 if (SendMessage (GetDlgItem (IDC_URL_LIST), LB_GETSELCOUNT, 0, 0) > 0)
354 {
355 // At least one official site selected, enable "Next".
356 ButtonFlags |= PSWIZB_NEXT;
357 }
358 GetOwner ()->SetButtons (ButtonFlags);
359 }
360
361 void
362 SitePage::PopulateListBox ()
363 {
364 int j;
365 HWND listbox = GetDlgItem (IDC_URL_LIST);
366
367 // Populate the list box with the URLs.
368 SendMessage (listbox, LB_RESETCONTENT, 0, 0);
369 for (size_t i = 1; i <= all_site_list.number (); i++)
370 {
371 j = SendMessage (listbox, LB_ADDSTRING, 0,
372 (LPARAM) all_site_list[i]->displayed_url);
373 SendMessage (listbox, LB_SETITEMDATA, j, i);
374 }
375
376 // Select the selected ones.
377 for (size_t n = 1; n <= site_list.number (); n++)
378 {
379 int index = SendMessage (listbox, LB_FINDSTRING, (WPARAM) - 1,
380 (LPARAM) site_list[n]->displayed_url);
381 if (index != LB_ERR)
382 {
383 // Highlight the selected item
384 SendMessage (listbox, LB_SELITEMRANGE, TRUE, (index << 16) | index);
385 // Make sure it's fully visible
386 SendMessage (listbox, LB_SETCARETINDEX, index, FALSE);
387 }
388 }
389 }
390
391 bool SitePage::OnMessageCmd (int id, HWND hwndctl, UINT code)
392 {
393 switch (id)
394 {
395 case IDC_EDIT_USER_URL:
396 {
397 if (code == EN_CHANGE)
398 {
399 // Text in edit box may have changed.
400 other_url = eget (GetHWND (), IDC_EDIT_USER_URL, other_url);
401 }
402 break;
403 }
404 case IDC_URL_LIST:
405 {
406 if (code == LBN_SELCHANGE)
407 {
408 CheckControlsAndDisableAccordingly ();
409 }
410 break;
411 }
412 case IDC_BUTTON_ADD_URL:
413 {
414 if (code == BN_CLICKED)
415 {
416 // User pushed the Add button.
417 other_url = eget (GetHWND (), IDC_EDIT_USER_URL, other_url);
418 site_list_type *
419 newsite =
420 new
421 site_list_type (other_url);
422 site_list_type & listobj =
423 all_site_list.registerbyobject (*newsite);
424 if (&listobj != newsite)
425 {
426 // That site was already registered
427 delete
428 newsite;
429 }
430 else
431 {
432 // Log the adding of this new URL.
433 log (0, "Adding site: %s", other_url);
434 }
435
436 // Assume the user wants to use it and select it for him.
437 site_list.registerbyobject (listobj);
438
439 // Update the list box.
440 PopulateListBox ();
441 }
442 break;
443 }
444 default:
445 // Wasn't recognized or handled.
446 return false;
447 }
448
449 // Was handled since we never got to default above.
450 return true;
451 }
This page took 0.056015 seconds and 5 git commands to generate.