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