Add a bool member 'site_list_type::is_mirror' to indicate whether a site is a mirror (possibly private) rather than a user URL (corresponding to a non-mirror package repo).
Display only mirrors in the site chooser listbox. Rename several SiteList variables to indicate that they now contain only mirrors. Add a few new ones, as well as some variables for lists of user URLs. Start using the user setting "last-mirror" only for mirrors. Add a new user setting "last-user-site" for saved user URLs. Set the initial state of allow_user_url depending on whether there are any saved sites that are known not to be 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 | 221 ++++++++++++++++++++++++++++++++++++++++++++++------------------ site.h | 10 ++- 2 files changed, 167 insertions(+), 64 deletions(-) diff --git a/site.cc b/site.cc index c0ce487..f2fa9fa 100644 --- a/site.cc +++ b/site.cc @@ -84,17 +84,26 @@ bool cache_is_usable; bool cache_needs_writing; string cache_warn_urls; -/* Selected sites */ +/* Selected sites; may be inaccurate until save_dialog() is called */ SiteList site_list; /* Fresh mirrors + selected sites */ SiteList all_site_list; +/* Selected mirrors; recomputed by save_dialog() */ +SiteList selected_mirror_list; + +/* All mirrors; recomputed by PopulateListBox() */ +SiteList all_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; +/* Selected URLs for non-mirror package repos */ +SiteList selected_usersite_list; StringArrayOption SiteOption('s', "site", "Download site"); @@ -108,10 +117,14 @@ SiteSetting::SiteSetting (): saved (false) { for (vector<string>::const_iterator n = SiteOptionStrings.begin (); n != SiteOptionStrings.end (); ++n) - registerSavedSite (n->c_str ()); + // With no information to the contrary, assume the site is a mirror. + registerSavedSite (n->c_str (), true); } else - getSavedSites (); + { + getSavedSites (lastMirrorKey (), true); + getSavedSites ("last-user-site", false); + } } const char * @@ -129,8 +142,17 @@ SiteSetting::save() io_stream *f = UserSettings::instance().open (lastMirrorKey ()); if (f) { - 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) + *f << n->url; + delete f; + } + + f = UserSettings::instance().open ("last-user-site"); + if (f) + { + for (SiteList::const_iterator n = selected_usersite_list.begin (); + n != selected_usersite_list.end (); ++n) *f << n->url; delete f; } @@ -147,13 +169,15 @@ site_list_type::site_list_type (const string &_url, const string &_servername, const string &_area, const string &_location, - bool _from_mirrors_lst) + bool _from_mirrors_lst, + bool _is_mirror) { url = _url; servername = _servername; area = _area; location = _location; from_mirrors_lst = _from_mirrors_lst; + is_mirror = _is_mirror; /* Canonicalize URL to ensure it ends with a '/' */ if (url.at(url.length()-1) != '/') @@ -193,6 +217,7 @@ site_list_type::site_list_type (site_list_type const &rhs) area = rhs.area; location = rhs.location; from_mirrors_lst = rhs.from_mirrors_lst; + is_mirror = rhs.is_mirror; displayed_url = rhs.displayed_url; } @@ -205,6 +230,7 @@ site_list_type::operator= (site_list_type const &rhs) area = rhs.area; location = rhs.location; from_mirrors_lst = rhs.from_mirrors_lst; + is_mirror = rhs.is_mirror; displayed_url = rhs.displayed_url; return *this; } @@ -224,8 +250,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); @@ -237,9 +263,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 @@ -294,17 +334,11 @@ load_site_list (SiteList& theSites, char *theString) if (!semi || !semi2 || !semi3) continue; - site_list_type newsite (bol, semi, semi2, semi3, true); + site_list_type newsite (bol, semi, semi2, semi3, true, true); 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; @@ -316,6 +350,17 @@ load_site_list (SiteList& theSites, char *theString) } } +// all_site_list should have the latest info about which URLs are mirrors. +static bool +not_mirror (site_list_type site) +{ + SiteList::iterator i = find (all_site_list.begin (), all_site_list.end (), + site); + if (i == all_site_list.end ()) + return true; + return !i->is_mirror; +} + static int get_site_list (HINSTANCE h, HWND owner) { @@ -361,11 +406,20 @@ get_site_list (HINSTANCE h, HWND owner) theCachedString = new_cstr_char_array (cached_mirrors); load_site_list (all_site_list, theMirrorString); - load_site_list (cached_site_list, theCachedString); + load_site_list (cached_mirror_list, theCachedString); delete[] theMirrorString; delete[] theCachedString; + // Did any saved user sites turn out to be mirrors? + SiteList::iterator i = partition (selected_usersite_list.begin (), + selected_usersite_list.end (), not_mirror); + selected_mirror_list.insert (selected_mirror_list.end (), i, + selected_usersite_list. end ()); + selected_usersite_list.erase (i, selected_usersite_list.end ()); + + allow_user_url = selected_usersite_list.size (); + return 0; } @@ -379,11 +433,18 @@ get_site_list (HINSTANCE h, HWND owner) #define NOSAVE3_LEN (sizeof (NOSAVE3) - 1) void -SiteSetting::registerSavedSite (const char * site) +SiteSetting::registerSavedSite (const char * site, bool mirror) { - site_list_type tempSite(site, "", "", "", false); + site_list_type tempSite(site, "", "", "", false, mirror); SiteList::iterator i = find (all_site_list.begin(), all_site_list.end(), tempSite); + if (i != all_site_list.end() && i->is_mirror) + { + /* site was already registered as a mirror; leave it alone. */ + selected_mirror_list.push_back (*i); + return; + } + if (i == all_site_list.end()) { /* Don't default to certain machines if they suffer @@ -392,26 +453,26 @@ 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; - site_list.push_back (tempSite); } else - site_list.push_back (*i); + all_site_list.erase (i); + + merge_site (all_site_list, tempSite); + if (mirror) + selected_mirror_list.push_back (tempSite); + else + selected_usersite_list.push_back (tempSite); } void -SiteSetting::getSavedSites () +SiteSetting::getSavedSites (const char *key, bool mirror) { - const char *buf = UserSettings::instance().get (lastMirrorKey ()); + const char *buf = UserSettings::instance().get (key); if (!buf) return; char *fg_ret = strdup (buf); for (char *site = strtok (fg_ret, "\n"); site; site = strtok (NULL, "\n")) - registerSavedSite (site); + registerSavedSite (site, mirror); free (fg_ret); } @@ -504,22 +565,28 @@ 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(), *n); if (i == all_site_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()) + SiteList::iterator j = find (cached_mirror_list.begin(), + cached_mirror_list.end(), *n); + if (j != cached_mirror_list.end()) { + // *i is stale. + // FIXME - There should be a better way to determine this. + // Copy info from *j to *i... + *i = *j; + // ...but remember that *i is stale. + i->from_mirrors_lst = false; Log (LOG_PLAIN) << "Dropped selected mirror: " << n->url << endLog; - dropped_site_list.push_back (*j); + dropped_mirror_list.push_back (*j); if (cache_warn_urls.size()) cache_warn_urls += "\r\n"; cache_warn_urls += i->url; @@ -552,13 +619,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; } @@ -569,6 +636,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 () { @@ -583,10 +657,10 @@ SitePage::OnNext () if (cache_needs_writing) 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) - Log (LOG_PLAIN) << "site: " << n->url << endLog; + // Log all the selected mirrors. + for (SiteList::const_iterator n = selected_mirror_list.begin (); + n != selected_mirror_list.end (); ++n) + Log (LOG_PLAIN) << "mirror: " << n->url << endLog; Progress.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD); return IDD_INSTATUS; @@ -608,10 +682,10 @@ SitePage::OnBack () void SitePage::OnActivate () { - // Fill the list box with all known sites. + // Fill the list box with all known mirrors. PopulateListBox (); - // Load the user URL box with nothing - it is in the list already. + // Load the mirror 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. @@ -647,10 +721,16 @@ SitePage::PopulateListBox () int j; HWND listbox = GetDlgItem (IDC_URL_LIST); + // Recompute all_mirror_list. + all_mirror_list.clear (); + copy_if (all_site_list.begin (), all_site_list.end (), + back_inserter (all_mirror_list), + [] (site_list_type s) {return s.is_mirror;}); + // 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()); @@ -658,14 +738,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); @@ -703,17 +783,31 @@ bool SitePage::OnMessageCmd (int id, HWND hwndctl, UINT code) std::string other_url = egetString (GetHWND (), IDC_EDIT_USER_URL); if (other_url.size()) { - site_list_type newsite (other_url, "", "", "", false); + site_list_type newsite (other_url, "", "", "", false, true); SiteList::iterator i = find (all_site_list.begin(), all_site_list.end(), newsite); if (i == all_site_list.end()) { all_site_list.push_back (newsite); - Log (LOG_BABBLE) << "Adding site: " << other_url << endLog; - site_list.push_back (newsite); + Log (LOG_BABBLE) << "Adding mirror: " << other_url << endLog; + selected_mirror_list.push_back (newsite); } else - site_list.push_back (*i); + { + if (i->is_mirror) + selected_mirror_list.push_back (*i); + else + { + all_site_list.erase (i); + merge_site (all_site_list, newsite); + selected_mirror_list.push_back (newsite); + SiteList::iterator j = find (selected_usersite_list. begin (), + selected_usersite_list.end (), + newsite); + if (j != selected_usersite_list.end ()) + selected_usersite_list.erase (j); + } + } // Update the list box. PopulateListBox (); @@ -722,11 +816,16 @@ bool SitePage::OnMessageCmd (int id, HWND hwndctl, UINT code) eset (GetHWND (), IDC_EDIT_USER_URL, ""); } } + break; } 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 (); + } break; } default: diff --git a/site.h b/site.h index d16db8e..3525931 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 (); @@ -50,7 +52,7 @@ public: site_list_type () : url (), displayed_url (), key () {}; site_list_type (const site_list_type &); site_list_type (const std::string& , const std::string& , - const std::string& , const std::string&, bool); + const std::string& , const std::string&, bool, bool); ~site_list_type () {}; site_list_type &operator= (const site_list_type &); std::string url; @@ -60,6 +62,8 @@ public: std::string location; // did this site come from mirrors.lst? bool from_mirrors_lst; + // is it a mirror of cygwin.com (possibly private)? + bool is_mirror; std::string displayed_url; // sort key std::string key; @@ -86,8 +90,8 @@ class SiteSetting ~SiteSetting (); private: bool saved; - void getSavedSites(); - void registerSavedSite(char const *); + void getSavedSites(char const *, bool); + void registerSavedSite(char const *, bool); const char *lastMirrorKey(); }; -- 2.15.1