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