]> cygwin.com Git - cygwin-apps/setup.git/blobdiff - site.cc
2002-07-15 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / site.cc
diff --git a/site.cc b/site.cc
index 56b497974fd223c533f75b6eb4c7fd704f7c863a..61045e2f7e1dfd7360d66186ad81435b85a91577 100644 (file)
--- a/site.cc
+++ b/site.cc
@@ -21,53 +21,53 @@ static const char *cvsid =
   "\n%%% $Id$\n";
 #endif
 
+#include "site.h"
 #include "win32.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <process.h>
+#include <algorithm>
 
 #include "dialog.h"
 #include "resource.h"
 #include "state.h"
 #include "geturl.h"
 #include "msg.h"
-#include "concat.h"
-#include "log.h"
+#include "LogSingleton.h"
 #include "io_stream.h"
 #include "site.h"
 
 #include "port.h"
 
-#include "site.h"
 #include "propsheet.h"
 
 #include "threebar.h"
 extern ThreeBarProgressPage Progress;
 
-list < site_list_type, const char *, strcasecmp > site_list;
-list < site_list_type, const char *, strcasecmp > all_site_list;
-
-static char *other_url = 0;
+SiteList site_list;
+SiteList all_site_list;
 
 void
-site_list_type::init (char const *newurl)
+site_list_type::init (String const &newurl)
 {
-  url = _strdup (newurl);
-  displayed_url = _strdup (newurl);
-  char *dot = strchr (displayed_url, '.');
-  if (dot)
-    {
-      dot = strchr (dot, '/');
-      if (dot)
-       *dot = 0;
-    }
-  key = (char *) malloc (2 * strlen (newurl) + 3);
+  url = newurl;
 
-  dot = displayed_url;
-  dot += strlen (dot);
-  char *dp = key;
-  while (dot != displayed_url)
+  char *dots = newurl.cstr();
+  char *dot = strchr (dots, '.');
+  if (dot)
+      {
+         dot = strchr (dot, '/');
+        if (dot)
+               *dot = 0;
+   }
+  displayed_url = String (dots);
+
+  
+  dot = dots + strlen (dots);
+  char *dpsave, *dp = new char[2 * newurl.size() + 3];
+  dpsave = dp;
+  while (dot != dots)
     {
       if (*dot == '.' || *dot == '/')
        {
@@ -78,26 +78,53 @@ site_list_type::init (char const *newurl)
            *dp++ = *sp++;
          *dp++ = ' ';
        }
-      dot--;
+      --dot;
     }
   *dp++ = ' ';
-  strcpy (dp, displayed_url);
+  strcpy (dp, dots);
+  delete[] dots;
+  key = String (dp);
+  delete[] dpsave;
 }
 
-site_list_type::site_list_type (char const *newurl)
+site_list_type::site_list_type (String const &newurl)
 {
   init (newurl);
 }
 
+site_list_type::site_list_type (site_list_type const &rhs)
+{
+  key = rhs.key;
+  url = rhs.url;
+  displayed_url = rhs.displayed_url;
+}
+
+site_list_type &
+site_list_type::operator= (site_list_type const &rhs)
+{
+  key = rhs.key;
+  url = rhs.url;
+  displayed_url = rhs.displayed_url;
+  return *this;
+}
+
+bool
+site_list_type::operator == (site_list_type const &rhs) const
+{
+  return key.casecompare (rhs.key) == 0; 
+}
+
+bool
+site_list_type::operator < (site_list_type const &rhs) const
+{
+  return key.casecompare (rhs.key) < 0;
+}
+
 static void
 save_dialog (HWND h)
 {
   // Remove anything that was previously in the selected site list.
-  while (site_list.number () > 0)
-    {
-      // we don't delete the object because it's stored in the all_site_list.
-      site_list.removebyindex (1);
-    }
+  site_list.clear ();
 
   HWND listbox = GetDlgItem (h, IDC_URL_LIST);
   int sel_count = SendMessage (listbox, LB_GETSELCOUNT, 0, 0);
@@ -114,7 +141,7 @@ save_dialog (HWND h)
        {
          int mirror =
            SendMessage (listbox, LB_GETITEMDATA, sel_buffer[n], 0);
-         site_list.registerbyobject (*all_site_list[mirror]);
+         site_list.push_back (all_site_list[mirror]);
        }
     }
 }
@@ -123,19 +150,13 @@ void
 save_site_url ()
 {
   io_stream *f = io_stream::open ("cygfile:///etc/setup/last-mirror", "wb");
-  for (size_t n = 1; n <= site_list.number (); n++)
+  if (f)
     {
-      if (f)
-       {
-         // FIXME: write all selected sites
-         TCHAR *temp;
-         temp = new TCHAR[sizeof (TCHAR) * (strlen (site_list[n]->url) + 2)];
-         sprintf (temp, "%s\n", site_list[n]->url);
-         f->write (temp, strlen (temp));
-         delete[]temp;
-       }
+      for (SiteList::const_iterator n = site_list.begin ();
+          n != site_list.end (); ++n)
+        f->write ((n->url + "\n").cstr_oneuse(), n->url.size() + 1);
+      delete f;
     }
-  delete f;
 }
 
 static int
@@ -145,13 +166,14 @@ get_site_list (HINSTANCE h, HWND owner)
 
   if (LoadString (h, IDS_MIRROR_LST, mirror_url, sizeof (mirror_url)) <= 0)
     return 1;
-  char *mirrors = get_url_to_string (mirror_url, owner);
-  if (!mirrors)
+  char *bol, *eol, *nl, *theString;
+  {
+  String mirrors = get_url_to_string (mirror_url, owner);
+  if (!mirrors.size())
     return 1;
 
-  char *bol, *eol, *nl;
+  nl = theString = mirrors.cstr();}
 
-  nl = mirrors;
   while (*nl)
     {
       bol = nl;
@@ -168,15 +190,23 @@ get_site_list (HINSTANCE h, HWND owner)
          char *semi = strchr (bol, ';');
          if (semi)
            *semi = 0;
-         site_list_type *newsite = new site_list_type (bol);
-         site_list_type & listobj =
-           all_site_list.registerbyobject (*newsite);
-         if (&listobj != newsite)
-           /* That site was already registered */
-           delete newsite;
+         site_list_type newsite (bol);
+         SiteList::iterator i = find (all_site_list.begin(),
+                                      all_site_list.end(), newsite);
+         if (i == all_site_list.end())
+           {
+             SiteList result;
+             merge (all_site_list.begin(), all_site_list.end(),
+                    &newsite, &newsite + 1,
+                    inserter (result, result.begin()));
+             all_site_list = result;
+           }
+         else
+           //TODO: remove and remerge 
+           *i = newsite;
        }
     }
-  delete[]mirrors;
+  delete[] theString;
 
   return 0;
 }
@@ -209,12 +239,10 @@ get_saved_sites ()
       if (eos < site)
        continue;
 
-      bool found = false;
-      for (size_t i = 1; !found && i <= all_site_list.number (); i++)
-       if (!strcasecmp (site, all_site_list[i]->url))
-         found = true;
-
-      if (!found)
+      site_list_type tempSite(site);
+      SiteList::iterator i = find (all_site_list.begin(),
+                                  all_site_list.end(), tempSite);
+      if (i == all_site_list.end())
        {
          /* Don't default to certain machines ever since they suffer
             from bandwidth limitations. */
@@ -222,26 +250,21 @@ get_saved_sites ()
              || strnicmp (site, NOSAVE2, NOSAVE2_LEN) == 0
              || strnicmp (site, NOSAVE3, NOSAVE3_LEN) == 0)
            return;
-         site_list_type *newsite = new site_list_type (site);
-         site_list_type & listobj =
-           all_site_list.registerbyobject (*newsite);
-         if (&listobj != newsite)
-           /* That site was already registered - shouldn't happen, but safety first */
-           delete newsite;
+         SiteList result;
+         merge (all_site_list.begin(), all_site_list.end(),
+                &tempSite, &tempSite + 1,
+                inserter (result, result.begin()));
+         all_site_list = result;
+         site_list.push_back (tempSite);
        }
-      /* TODO: make a site_type method to create a serach key on-the-fly from a 
-         URL
-       */
-      found = false;
-      for (size_t i = 1; !found && i <= all_site_list.number (); i++)
-       if (!strcasecmp (site, all_site_list[i]->url))
-         site_list.registerbyobject (*all_site_list[i]);
+      else
+       site_list.push_back (tempSite);
     }
   delete f;
 
 }
 
-static void
+static DWORD WINAPI
 do_download_site_info_thread (void *p)
 {
   HANDLE *context;
@@ -252,28 +275,24 @@ do_download_site_info_thread (void *p)
   hinst = (HINSTANCE) (context[0]);
   h = (HWND) (context[1]);
 
-  if (all_site_list.number () == 0)
-    {
-      if (get_site_list (hinst, h))
+  if (all_site_list.size() == 0
+      && get_site_list (hinst, h))
        {
          // Error: Couldn't download the site info.  Go back to the Net setup page.
          MessageBox (h, TEXT ("Can't get list of download sites.\n\
-Make sure your network settings are corect and try again."), NULL, MB_OK);
+Make sure your network settings are correct and try again."), NULL, MB_OK);
 
          // Tell the progress page that we're done downloading
          Progress.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE, 0,
                                IDD_NET);
 
-         _endthread ();
        }
-    }
-
+  else
   // Everything worked, go to the site select page
-
   // Tell the progress page that we're done downloading
   Progress.PostMessage (WM_APP_SITE_INFO_DOWNLOAD_COMPLETE, 0, IDD_SITE);
 
-  _endthread ();
+  ExitThread(0);
 }
 
 static HANDLE context[2];
@@ -285,8 +304,8 @@ do_download_site_info (HINSTANCE hinst, HWND owner)
   context[0] = hinst;
   context[1] = owner;
 
-  _beginthread (do_download_site_info_thread, 0, context);
-
+  DWORD threadID;
+  CreateThread (NULL, 0, do_download_site_info_thread, context, 0, &threadID);
 }
 
 bool SitePage::Create ()
@@ -308,9 +327,10 @@ SitePage::OnNext ()
   save_dialog (h);
   save_site_url ();
 
-  // Log all the selected URLs from the list.    
-  for (size_t n = 1; n <= site_list.number (); n++)
-    log (0, "site: %s", site_list[n]->url);
+  // Log all the selected URLs from the list.
+  for (SiteList::const_iterator n = site_list.begin ();
+       n != site_list.end (); ++n)
+    log (LOG_PLAIN) << "site: " << n->url << endLog;
 
   Progress.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD);
   return IDD_INSTATUS;
@@ -335,8 +355,8 @@ SitePage::OnActivate ()
   // Fill the list box with all known sites.
   PopulateListBox ();
 
-  // Load the user URL box with whatever it was last time.
-  eset (GetHWND (), IDC_EDIT_USER_URL, other_url);
+  // Load the user URL box with nothing - it is in the list already.
+  eset (GetHWND (), IDC_EDIT_USER_URL, "");
 
   // Get the enabled/disabled states of the controls set accordingly.
   CheckControlsAndDisableAccordingly ();
@@ -350,7 +370,7 @@ SitePage::CheckControlsAndDisableAccordingly () const
   // Check that at least one download site is selected.
   if (SendMessage (GetDlgItem (IDC_URL_LIST), LB_GETSELCOUNT, 0, 0) > 0)
     {
-      // At least one official site selected, enable "Next".
+      // At least one site selected, enable "Next".
       ButtonFlags |= PSWIZB_NEXT;
     }
   GetOwner ()->SetButtons (ButtonFlags);
@@ -364,18 +384,20 @@ SitePage::PopulateListBox ()
 
   // Populate the list box with the URLs.
   SendMessage (listbox, LB_RESETCONTENT, 0, 0);
-  for (size_t i = 1; i <= all_site_list.number (); i++)
+  for (SiteList::const_iterator i = all_site_list.begin ();
+       i != all_site_list.end (); ++i)
     {
       j = SendMessage (listbox, LB_ADDSTRING, 0,
-                      (LPARAM) all_site_list[i]->displayed_url);
-      SendMessage (listbox, LB_SETITEMDATA, j, i);
+                      (LPARAM) i->displayed_url.cstr_oneuse());
+      SendMessage (listbox, LB_SETITEMDATA, j, j);
     }
 
   // Select the selected ones.
-  for (size_t n = 1; n <= site_list.number (); n++)
+  for (SiteList::const_iterator n = site_list.begin ();
+       n != site_list.end (); ++n)
     {
       int index = SendMessage (listbox, LB_FINDSTRING, (WPARAM) - 1,
-                              (LPARAM) site_list[n]->displayed_url);
+                              (LPARAM) n->displayed_url.cstr_oneuse());
       if (index != LB_ERR)
        {
          // Highlight the selected item
@@ -392,11 +414,7 @@ bool SitePage::OnMessageCmd (int id, HWND hwndctl, UINT code)
     {
     case IDC_EDIT_USER_URL:
       {
-       if (code == EN_CHANGE)
-         {
-           // Text in edit box may have changed.
-           other_url = eget (GetHWND (), IDC_EDIT_USER_URL, other_url);
-         }
+       // FIXME: Make Enter here cause an ADD, not a NEXT.
        break;
       }
     case IDC_URL_LIST:
@@ -404,6 +422,7 @@ bool SitePage::OnMessageCmd (int id, HWND hwndctl, UINT code)
        if (code == LBN_SELCHANGE)
          {
            CheckControlsAndDisableAccordingly ();
+           save_dialog (GetHWND ());
          }
        break;
       }
@@ -412,30 +431,32 @@ bool SitePage::OnMessageCmd (int id, HWND hwndctl, UINT code)
        if (code == BN_CLICKED)
          {
            // User pushed the Add button.
-           other_url = eget (GetHWND (), IDC_EDIT_USER_URL, other_url);
-           site_list_type *
-             newsite =
-             new
-             site_list_type (other_url);
-           site_list_type & listobj =
-             all_site_list.registerbyobject (*newsite);
-           if (&listobj != newsite)
+           String other_url = egetString (GetHWND (), IDC_EDIT_USER_URL);
+           if (other_url.size())
+           {
+           site_list_type newsite (other_url);
+           SiteList::iterator i = find (all_site_list.begin(),
+                                        all_site_list.end(), newsite);
+           if (i == all_site_list.end())
              {
-               // That site was already registered
-               delete
-                 newsite;
+               all_site_list.push_back (newsite);
+               log (LOG_BABBLE) << "Adding site: " << other_url << endLog;
              }
            else
              {
-               // Log the adding of this new URL.
-               log (0, "Adding site: %s", other_url);
+               *i = newsite;
+               log (LOG_BABBLE) << "Replacing site: " << other_url << endLog;
              }
 
            // Assume the user wants to use it and select it for him.
-           site_list.registerbyobject (listobj);
+           site_list.push_back (newsite);
 
            // Update the list box.
            PopulateListBox ();
+           // And allow the user to continue
+           CheckControlsAndDisableAccordingly ();
+           eset (GetHWND (), IDC_EDIT_USER_URL, "");
+           }
          }
        break;
       }
This page took 0.036258 seconds and 5 git commands to generate.