]> cygwin.com Git - cygwin-apps/setup.git/blame - site.cc
2002-02-19 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / site.cc
CommitLineData
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
b24c88b3
RC
19#if 0
20static const char *cvsid =
21 "\n%%% $Id$\n";
22#endif
8507f105 23
23c9e63c
DD
24#include "win32.h"
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
ab57ceaa 28#include <process.h>
23c9e63c
DD
29
30#include "dialog.h"
31#include "resource.h"
32#include "state.h"
33#include "geturl.h"
34#include "msg.h"
89b1a15b 35#include "log.h"
b24c88b3 36#include "io_stream.h"
de6a1a64 37#include "site.h"
23c9e63c
DD
38
39#include "port.h"
40
ab57ceaa
RC
41#include "site.h"
42#include "propsheet.h"
43
44#include "threebar.h"
45extern ThreeBarProgressPage Progress;
46
3c054baf
RC
47list < site_list_type, String, String::casecompare > site_list;
48list < site_list_type, String, String::casecompare > all_site_list;
23c9e63c 49
de6a1a64 50void
3c054baf 51site_list_type::init (String const &newurl)
de6a1a64 52{
3c054baf 53 url = newurl;
de6a1a64 54
3c054baf
RC
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)
de6a1a64
RC
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 }
3c054baf 79 --dot;
de6a1a64
RC
80 }
81 *dp++ = ' ';
3c054baf
RC
82 strcpy (dp, dots);
83 delete[] dots;
84 key = String (dp);
85 delete[] dp;
de6a1a64
RC
86}
87
3c054baf 88site_list_type::site_list_type (String const &newurl)
de6a1a64
RC
89{
90 init (newurl);
91}
92
23c9e63c 93static void
b7301c43 94save_dialog (HWND h)
23c9e63c 95{
b7301c43
RC
96 // Remove anything that was previously in the selected site list.
97 while (site_list.number () > 0)
de6a1a64 98 {
b7301c43
RC
99 // we don't delete the object because it's stored in the all_site_list.
100 site_list.removebyindex (1);
de6a1a64 101 }
23c9e63c 102
23c9e63c 103 HWND listbox = GetDlgItem (h, IDC_URL_LIST);
de6a1a64
RC
104 int sel_count = SendMessage (listbox, LB_GETSELCOUNT, 0, 0);
105 if (sel_count > 0)
23c9e63c 106 {
de6a1a64
RC
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);
b7301c43 118 site_list.registerbyobject (*all_site_list[mirror]);
de6a1a64 119 }
23c9e63c 120 }
23c9e63c
DD
121}
122
ed3e8b9b
DD
123void
124save_site_url ()
125{
b24c88b3 126 io_stream *f = io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
3c054baf 127 if (f)
b24c88b3 128 {
3c054baf
RC
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;
b24c88b3 132 }
ed3e8b9b
DD
133}
134
23c9e63c 135static int
ab57ceaa 136get_site_list (HINSTANCE h, HWND owner)
23c9e63c
DD
137{
138 char mirror_url[1000];
ab57ceaa 139
ed3e8b9b 140 if (LoadString (h, IDS_MIRROR_LST, mirror_url, sizeof (mirror_url)) <= 0)
23c9e63c 141 return 1;
3c054baf
RC
142 char *bol, *eol, *nl, *theString;
143 {
144 String mirrors = get_url_to_string (mirror_url, owner);
145 if (!mirrors.size())
23c9e63c
DD
146 return 1;
147
3c054baf 148 nl = theString = mirrors.cstr();}
23c9e63c 149
23c9e63c
DD
150 while (*nl)
151 {
152 bol = nl;
b24c88b3 153 for (eol = bol; *eol && *eol != '\n'; eol++);
23c9e63c 154 if (*eol)
b24c88b3 155 nl = eol + 1;
23c9e63c
DD
156 else
157 nl = eol;
158 while (eol > bol && eol[-1] == '\r')
159 eol--;
160 *eol = 0;
161 if (bol[0] != '#' && bol[0] > ' ')
162 {
b5b282c4 163 char *semi = strchr (bol, ';');
23c9e63c
DD
164 if (semi)
165 *semi = 0;
de6a1a64
RC
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;
23c9e63c
DD
172 }
173 }
3c054baf 174 delete[] theString;
b5b282c4 175
23c9e63c
DD
176 return 0;
177}
178
6fbc690d
CF
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
ed3e8b9b 188static void
de6a1a64 189get_saved_sites ()
ed3e8b9b 190{
b24c88b3 191 io_stream *f = io_stream::open ("cygfile:///etc/setup/last-mirror", "rt");
ed3e8b9b
DD
192 if (!f)
193 return;
194
195 char site[1000];
de6a1a64
RC
196 char *fg_ret;
197 while ((fg_ret = f->gets (site, 1000)))
198 {
ed3e8b9b 199
de6a1a64
RC
200 char *eos = site + strlen (site) - 1;
201 while (eos >= site && (*eos == '\n' || *eos == '\r'))
202 *eos-- = '\0';
ed3e8b9b 203
de6a1a64
RC
204 if (eos < site)
205 continue;
ed3e8b9b 206
3c054baf
RC
207 String tempKey = site_list_type (String(site)).key;
208 if (!all_site_list.getbykey (tempKey))
de6a1a64
RC
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 */
3c054baf
RC
226 // Was it an allowed URL?
227 site_list_type *tempSite;
228 if ((tempSite = all_site_list.getbykey (tempKey)))
229 site_list.registerbyobject (*tempSite);
ed3e8b9b 230 }
de6a1a64 231 delete f;
ed3e8b9b 232
ed3e8b9b
DD
233}
234
ab57ceaa
RC
235static void
236do_download_site_info_thread (void *p)
23c9e63c 237{
ab57ceaa
RC
238 HANDLE *context;
239 HINSTANCE hinst;
240 HWND h;
241 context = (HANDLE *) p;
242
243 hinst = (HINSTANCE) (context[0]);
244 h = (HWND) (context[1]);
23c9e63c 245
3c054baf
RC
246 if (all_site_list.number () == 0
247 && get_site_list (hinst, h))
ab57ceaa
RC
248 {
249 // Error: Couldn't download the site info. Go back to the Net setup page.
b7301c43
RC
250 MessageBox (h, TEXT ("Can't get list of download sites.\n\
251Make sure your network settings are corect and try again."), NULL, MB_OK);
ab57ceaa
RC
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
ab57ceaa 257 }
3c054baf 258 else
ab57ceaa 259 // Everything worked, go to the site select page
ab57ceaa
RC
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
266static HANDLE context[2];
267
268void
269do_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
b7301c43 279bool SitePage::Create ()
ab57ceaa 280{
b7301c43 281 return PropertyPage::Create (IDD_SITE);
ab57ceaa
RC
282}
283
284void
285SitePage::OnInit ()
286{
de6a1a64 287 get_saved_sites ();
ab57ceaa 288}
89b1a15b 289
ab57ceaa
RC
290long
291SitePage::OnNext ()
292{
293 HWND h = GetHWND ();
294
295 save_dialog (h);
b7301c43 296 save_site_url ();
ab57ceaa 297
b7301c43
RC
298 // Log all the selected URLs from the list.
299 for (size_t n = 1; n <= site_list.number (); n++)
1ac649ed 300 log (LOG_PLAIN, String ("site: ") + site_list[n]->url);
ab57ceaa 301
b7301c43
RC
302 Progress.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD);
303 return IDD_INSTATUS;
ab57ceaa
RC
304
305 return 0;
306}
307
308long
309SitePage::OnBack ()
310{
311 HWND h = GetHWND ();
312
313 save_dialog (h);
b7301c43
RC
314
315 // Go back to the net connection type page
ab57ceaa 316 return 0;
23c9e63c 317}
b7301c43
RC
318
319void
320SitePage::OnActivate ()
321{
322 // Fill the list box with all known sites.
323 PopulateListBox ();
324
3c054baf
RC
325 // Load the user URL box with nothing - it is in the list already.
326 eset (GetHWND (), IDC_EDIT_USER_URL, "");
b7301c43
RC
327
328 // Get the enabled/disabled states of the controls set accordingly.
329 CheckControlsAndDisableAccordingly ();
330}
331
332void
333SitePage::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
346void
347SitePage::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,
3c054baf 357 (LPARAM) all_site_list[i]->displayed_url.cstr_oneuse());
b7301c43
RC
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,
3c054baf 365 (LPARAM) site_list[n]->displayed_url.cstr_oneuse());
b7301c43
RC
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
376bool SitePage::OnMessageCmd (int id, HWND hwndctl, UINT code)
377{
378 switch (id)
379 {
380 case IDC_EDIT_USER_URL:
381 {
3c054baf 382 // FIXME: Make Enter here cause an ADD, not a NEXT.
b7301c43
RC
383 break;
384 }
385 case IDC_URL_LIST:
386 {
387 if (code == LBN_SELCHANGE)
388 {
389 CheckControlsAndDisableAccordingly ();
3c054baf 390 save_dialog (GetHWND ());
b7301c43
RC
391 }
392 break;
393 }
394 case IDC_BUTTON_ADD_URL:
395 {
396 if (code == BN_CLICKED)
397 {
398 // User pushed the Add button.
3c054baf
RC
399 String other_url = egetString (GetHWND (), IDC_EDIT_USER_URL);
400 if (other_url.size())
401 {
b7301c43
RC
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.
1ac649ed 417 log (LOG_BABBLE, String ("Adding site: ") + other_url);
b7301c43
RC
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 ();
3c054baf
RC
425 eset (GetHWND (), IDC_EDIT_USER_URL, "");
426 }
b7301c43
RC
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.077606 seconds and 5 git commands to generate.