Keep separate lists of mirrors and user URLs.  Rename several SiteList
variables to indicate that they now contain only mirrors.

Introduce a helper function remove_user_urls() to move non-mirrors
from the initial list of sites to the list of user URLs.

Set the initial state of allow_user_url depending on whether there are
any saved sites that are not mirrors.

Implement the appropriate action if the user unchecks
IDC_ALLOW_USER_URL.

As an unrelated bit of cleanup, define a helper function merge_site()
to avoid repeating the same code several times.
---
 site.cc | 172 ++++++++++++++++++++++++++++++++++++++++++++--------------------
 site.h  |   4 +-
 2 files changed, 120 insertions(+), 56 deletions(-)

diff --git a/site.cc b/site.cc
index 1b04343..85ae70c 100644
--- a/site.cc
+++ b/site.cc
@@ -84,14 +84,21 @@ string cache_warn_urls;
 /* Selected sites */
 SiteList site_list;
 
-/* Fresh mirrors + selected sites */
-SiteList all_site_list;
+/* Fresh mirrors + selected stale mirrors */
+SiteList all_mirror_list;
+
+/* Selected mirrors */
+SiteList selected_mirror_list;
 
 /* Previously fresh + cached before */
-SiteList cached_site_list;
+SiteList cached_mirror_list;
+
+/* Stale selected mirrors to warn about and add to cache */
+SiteList dropped_mirror_list;
 
-/* Stale selected sites to warn about and add to cache */
-SiteList dropped_site_list;
+/* User URLs */
+SiteList all_usersite_list;
+SiteList selected_usersite_list;
 
 StringArrayOption SiteOption('s', "site", "Download site");
 
@@ -221,8 +228,8 @@ site_list_type::operator < (site_list_type const &rhs) const
 static void
 save_dialog (HWND h)
 {
-  // Remove anything that was previously in the selected site list.
-  site_list.clear ();
+  // Remove anything that was previously in the selected mirror list.
+  selected_mirror_list.clear ();
 
   HWND listbox = GetDlgItem (h, IDC_URL_LIST);
   int sel_count = SendMessage (listbox, LB_GETSELCOUNT, 0, 0);
@@ -234,9 +241,23 @@ save_dialog (HWND h)
        {
          int mirror =
            SendMessage (listbox, LB_GETITEMDATA, sel_buffer[n], 0);
-         site_list.push_back (all_site_list[mirror]);
+         selected_mirror_list.push_back (all_mirror_list[mirror]);
        }
     }
+  site_list = selected_mirror_list;
+  if (allow_user_url)
+    site_list.insert (site_list.end (), selected_usersite_list.begin (),
+                     selected_usersite_list.end ());
+}
+
+static void
+merge_site (SiteList & sites, site_list_type newsite)
+{
+  SiteList result;
+  merge (sites.begin(), sites.end(),
+        &newsite, &newsite + 1,
+        inserter (result, result.begin()));
+  sites = result;
 }
 
 // This is called only for lists of mirrors that came (now or in a
@@ -295,13 +316,7 @@ load_site_list (SiteList& theSites, char *theString)
          SiteList::iterator i = find (theSites.begin(),
                                       theSites.end(), newsite);
          if (i == theSites.end())
-           {
-             SiteList result;
-             merge (theSites.begin(), theSites.end(),
-                    &newsite, &newsite + 1,
-                    inserter (result, result.begin()));
-             theSites = result;
-           }
+           merge_site (theSites, newsite);
          else
            //TODO: remove and remerge 
            *i = newsite;
@@ -313,6 +328,49 @@ load_site_list (SiteList& theSites, char *theString)
     }
 }
 
+static void
+remove_user_urls ()
+{
+  selected_mirror_list. clear ();
+  selected_usersite_list.clear ();
+  SiteList result;
+  for (SiteList::iterator i = all_mirror_list.begin ();
+       i != all_mirror_list.end (); i++)
+    {
+      bool mirror = false;
+      SiteList::iterator j;
+      if (i->from_mirrors_lst)
+       mirror = true;
+      else if ((j = find (cached_mirror_list.begin (), cached_mirror_list.end 
(), *i))
+              != cached_mirror_list.end ())
+       {
+         // It's a stale mirror.
+         mirror = true;
+         // Copy mirror info from cached version.
+         i->servername = j->servername;
+         i->area = j->area;
+         i->location = j->location;
+         // i->from_mirrors_lst stays false so we still know that *i is stale.
+         i->displayed_url = j->displayed_url;
+         i->key = j->key;
+       }
+      if (mirror)
+       {
+         result.push_back (*i);
+         if (find (site_list.begin (), site_list.end (), *i) != site_list.end 
())
+           selected_mirror_list.push_back (*i);
+       }
+      else
+       {
+         merge_site (all_usersite_list, *i);
+         if (find (site_list.begin (), site_list.end (), *i) != site_list.end 
())
+           selected_usersite_list.push_back (*i);
+       }
+    }
+  all_mirror_list = result;
+  allow_user_url = selected_usersite_list.size ();
+}
+
 static int
 get_site_list (HINSTANCE h, HWND owner)
 {
@@ -357,12 +415,14 @@ get_site_list (HINSTANCE h, HWND owner)
   theMirrorString = new_cstr_char_array (mirrors);
   theCachedString = new_cstr_char_array (cached_mirrors);
 
-  load_site_list (all_site_list, theMirrorString);
-  load_site_list (cached_site_list, theCachedString);
+  load_site_list (all_mirror_list, theMirrorString);
+  load_site_list (cached_mirror_list, theCachedString);
   
   delete[] theMirrorString;
   delete[] theCachedString;
 
+  remove_user_urls ();
+
   return 0;
 }
 
@@ -379,9 +439,9 @@ void
 SiteSetting::registerSavedSite (const char * site)
 {
   site_list_type tempSite(site, "", "", "", false);
-  SiteList::iterator i = find (all_site_list.begin(),
-                              all_site_list.end(), tempSite);
-  if (i == all_site_list.end())
+  SiteList::iterator i = find (all_mirror_list.begin(),
+                              all_mirror_list.end(), tempSite);
+  if (i == all_mirror_list.end())
     {
       /* Don't default to certain machines if they suffer
         from bandwidth limitations. */
@@ -389,11 +449,7 @@ SiteSetting::registerSavedSite (const char * site)
          || strnicmp (site, NOSAVE2, NOSAVE2_LEN) == 0
          || strnicmp (site, NOSAVE3, NOSAVE3_LEN) == 0)
        return;
-      SiteList result;
-      merge (all_site_list.begin(), all_site_list.end(),
-            &tempSite, &tempSite + 1,
-            inserter (result, result.begin()));
-      all_site_list = result;
+      merge_site (all_mirror_list, tempSite);
       site_list.push_back (tempSite);
     }
   else
@@ -501,26 +557,20 @@ drop_proc (HWND h, UINT message, WPARAM wParam, LPARAM 
lParam)
 int check_dropped_mirrors (HWND h)
 {
   cache_warn_urls = "";
-  dropped_site_list.clear ();
+  dropped_mirror_list.clear ();
 
-  for (SiteList::const_iterator n = site_list.begin ();
-       n != site_list.end (); ++n)
+  for (SiteList::const_iterator n = selected_mirror_list.begin ();
+       n != selected_mirror_list.end (); ++n)
     {
-      SiteList::iterator i = find (all_site_list.begin(), all_site_list.end(),
+      SiteList::iterator i = find (all_mirror_list.begin(), 
all_mirror_list.end(),
                                   *n);
-      if (i == all_site_list.end() || !i->from_mirrors_lst)
+      if (i == all_mirror_list.end () || !i->from_mirrors_lst)
        {
-         SiteList::iterator j = find (cached_site_list.begin(),
-                                      cached_site_list.end(), *n);
-         if (j != cached_site_list.end())
-           {
-             Log (LOG_PLAIN) << "Dropped selected mirror: " << n->url
-                 << endLog;
-             dropped_site_list.push_back (*j);
-             if (cache_warn_urls.size())
-               cache_warn_urls += "\r\n";
-             cache_warn_urls += i->url;
-           }
+         Log (LOG_PLAIN) << "Dropped selected mirror: " << n->url << endLog;
+         dropped_mirror_list.push_back (*i);
+         if (cache_warn_urls.size())
+           cache_warn_urls += "\r\n";
+         cache_warn_urls += i->url;
        }
     }
   if (cache_warn_urls.size())
@@ -549,13 +599,13 @@ void save_cache_file (int cache_action)
   io_stream *f = UserSettings::instance().open ("mirrors-lst");
   if (f)
     {
-      write_cache_list (f, all_site_list);
+      write_cache_list (f, all_mirror_list);
       if (cache_action == CACHE_ACCEPT_WARN)
        {
          Log (LOG_PLAIN) << "Adding dropped mirrors to cache to warn again."
              << endLog;
          *f << "# Following mirrors re-added by setup.exe to warn again about 
dropped urls.";
-         write_cache_list (f, dropped_site_list);
+         write_cache_list (f, dropped_mirror_list);
        }
       delete f;
     }
@@ -566,6 +616,13 @@ bool SitePage::Create ()
   return PropertyPage::Create (IDD_SITE);
 }
 
+void
+SitePage::OnInit ()
+{
+  int a = allow_user_url ? BST_CHECKED : BST_UNCHECKED;
+  CheckDlgButton (GetHWND (), IDC_ALLOW_USER_URL, a);
+}
+
 long
 SitePage::OnNext ()
 {
@@ -581,8 +638,8 @@ SitePage::OnNext ()
     save_cache_file (cache_action);
 
   // Log all the selected URLs from the list.
-  for (SiteList::const_iterator n = site_list.begin ();
-       n != site_list.end (); ++n)
+  for (SiteList::const_iterator n = selected_mirror_list.begin ();
+       n != selected_mirror_list.end (); ++n)
     Log (LOG_PLAIN) << "site: " << n->url << endLog;
 
   Progress.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD);
@@ -646,8 +703,8 @@ SitePage::PopulateListBox ()
 
   // Populate the list box with the URLs.
   SendMessage (listbox, LB_RESETCONTENT, 0, 0);
-  for (SiteList::const_iterator i = all_site_list.begin ();
-       i != all_site_list.end (); ++i)
+  for (SiteList::const_iterator i = all_mirror_list.begin ();
+       i != all_mirror_list.end (); ++i)
     {
       j = SendMessage (listbox, LB_ADDSTRING, 0,
                       (LPARAM) i->displayed_url.c_str());
@@ -655,14 +712,14 @@ SitePage::PopulateListBox ()
     }
 
   // Select the selected ones.
-  for (SiteList::const_iterator n = site_list.begin ();
-       n != site_list.end (); ++n)
+  for (SiteList::const_iterator n = selected_mirror_list.begin ();
+       n != selected_mirror_list.end (); ++n)
     {
-      SiteList::iterator i = find (all_site_list.begin(),
-                                   all_site_list.end(), *n);
-      if (i != all_site_list.end())
+      SiteList::iterator i = find (all_mirror_list.begin(),
+                                   all_mirror_list.end(), *n);
+      if (i != all_mirror_list.end())
         {
-          int index = i - all_site_list.begin();
+          int index = i - all_mirror_list.begin();
 
          // Highlight the selected item
          SendMessage (listbox, LB_SELITEMRANGE, TRUE, (index << 16) | index);
@@ -688,7 +745,14 @@ bool SitePage::OnMessageCmd (int id, HWND hwndctl, UINT 
code)
     case IDC_ALLOW_USER_URL:
       {
        if (code == BN_CLICKED)
-         allow_user_url = IsButtonChecked (IDC_ALLOW_USER_URL);
+         {
+           allow_user_url = IsButtonChecked (IDC_ALLOW_USER_URL);
+           if (!allow_user_url)
+             {
+               selected_usersite_list.clear ();
+               site_list = selected_mirror_list;
+             }
+         }
        break;
       }
     default:
diff --git a/site.h b/site.h
index d16db8e..0b85caf 100644
--- a/site.h
+++ b/site.h
@@ -21,6 +21,7 @@
 
 #include "proppage.h"
 
+// For mirrors of cygwin.com.
 class SitePage : public PropertyPage
 {
 public:
@@ -31,6 +32,7 @@ public:
 
   bool Create ();
 
+  virtual void OnInit ();
   virtual void OnActivate ();
   virtual long OnNext ();
   virtual long OnBack ();
@@ -75,8 +77,6 @@ typedef std::vector <site_list_type> SiteList;
 
 /* user chosen sites */
 extern SiteList site_list;
-/* potential sites */
-extern SiteList all_site_list;
 
 class SiteSetting
 {
-- 
2.15.1

Reply via email to