Again, this will ultimately make it possible to specify, or store and
retrieve from settings a site, without having a GUI.
---
 Makefile.am                |   6 +-
 SiteSetting.cc             | 193 +++++++++++++++++++++++++++++++++++++
 site.h => SiteSetting.h    |  57 +++--------
 site.cc => gui/SitePage.cc | 169 +-------------------------------
 gui/SitePage.h             |  45 +++++++++
 ini.cc                     |   2 +-
 main.cc                    |   3 +-
 threebar.cc                |   2 +-
 8 files changed, 264 insertions(+), 213 deletions(-)
 create mode 100644 SiteSetting.cc
 rename site.h => SiteSetting.h (74%)
 rename site.cc => gui/SitePage.cc (77%)
 create mode 100644 gui/SitePage.h

diff --git a/Makefile.am b/Makefile.am
index 82efbd8..f753961 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -266,8 +266,10 @@ endif
        setup_version.c \
        sha2.h \
        sha2.c \
-       site.cc \
-       site.h \
+       gui/SitePage.cc \
+       gui/SitePage.h \
+       SiteSetting.cc \
+       SiteSetting.h \
        source.cc \
        source.h \
        SourceSetting.cc \
diff --git a/SiteSetting.cc b/SiteSetting.cc
new file mode 100644
index 0000000..be5f5d8
--- /dev/null
+++ b/SiteSetting.cc
@@ -0,0 +1,193 @@
+/*
+ * Copyright (c) 2000, Red Hat, Inc.
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ * Written by DJ Delorie <d...@cygnus.com>
+ *
+ */
+
+#include "io_stream.h"
+#include "SiteSetting.h"
+#include "UserSettings.h"
+#include "getopt++/StringArrayOption.h"
+#include "getopt++/BoolOption.h"
+#include "resource.h"
+
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <string.h>
+
+StringArrayOption SiteOption('s', "site", IDS_HELPTEXT_SITE);
+extern BoolOption UnsupportedOption;
+
+/* Selected sites */
+SiteList site_list;
+
+/* Fresh mirrors + selected sites */
+SiteList all_site_list;
+
+SiteSetting::SiteSetting (): saved (false)
+{
+  std::vector<std::string> SiteOptionStrings = SiteOption;
+  if (SiteOptionStrings.size())
+    {
+      for (std::vector<std::string>::const_iterator n = 
SiteOptionStrings.begin ();
+           n != SiteOptionStrings.end (); ++n)
+        registerSavedSite (n->c_str ());
+    }
+  else
+    getSavedSites ();
+}
+
+const char *
+SiteSetting::lastMirrorKey ()
+{
+  if (UnsupportedOption)
+    return "last-mirror-unsupported";
+
+  return "last-mirror";
+}
+
+void
+SiteSetting::save()
+{
+  io_stream *f = UserSettings::instance().open (lastMirrorKey ());
+  if (f)
+    {
+      for (SiteList::const_iterator n = site_list.begin ();
+           n != site_list.end (); ++n)
+        *f << n->url;
+      delete f;
+    }
+  saved = true;
+}
+
+SiteSetting::~SiteSetting ()
+{
+  if (!saved)
+    save ();
+}
+
+/* List of machines that should not be used by default when saved
+   in "last-mirror". */
+#define NOSAVE1 "ftp://sourceware.org/";
+#define NOSAVE1_LEN (sizeof (NOSAVE2) - 1)
+#define NOSAVE2 "ftp://sources.redhat.com/";
+#define NOSAVE2_LEN (sizeof (NOSAVE1) - 1)
+#define NOSAVE3 "ftp://gcc.gnu.org/";
+#define NOSAVE3_LEN (sizeof (NOSAVE3) - 1)
+
+void
+SiteSetting::registerSavedSite (const char * site)
+{
+  site_list_type tempSite(site, "", "", "", false);
+
+  /* Don't default to certain machines if they suffer from bandwidth
+     limitations. */
+  if (strnicmp (site, NOSAVE1, NOSAVE1_LEN) == 0
+      || strnicmp (site, NOSAVE2, NOSAVE2_LEN) == 0
+      || strnicmp (site, NOSAVE3, NOSAVE3_LEN) == 0)
+    return;
+
+  site_list_insert (all_site_list, tempSite);
+  site_list.push_back (tempSite);
+}
+
+void
+SiteSetting::getSavedSites ()
+{
+  const char *buf = UserSettings::instance().get (lastMirrorKey ());
+  if (!buf)
+    return;
+  char *fg_ret = strdup (buf);
+  for (char *site = strtok (fg_ret, "\n"); site; site = strtok (NULL, "\n"))
+    registerSavedSite (site);
+  free (fg_ret);
+}
+
+site_list_type::site_list_type (const std::string &_url,
+                                const std::string &_servername,
+                                const std::string &_area,
+                                const std::string &_location,
+                                bool _from_mirrors_lst,
+                                bool _noshow /* default: false */)
+{
+  url = _url;
+  servername = _servername;
+  area = _area;
+  location = _location;
+  from_mirrors_lst = _from_mirrors_lst;
+  noshow = _noshow;
+
+  /* Canonicalize URL to ensure it ends with a '/' */
+  if (url.at(url.length()-1) != '/')
+    url.append("/");
+
+  /* displayed_url is protocol and site name part of url */
+  std::string::size_type path_offset = url.find ("/", url.find ("//") + 2);
+  displayed_url = url.substr(0, path_offset);
+
+  /* the sorting key is hostname components in reverse order (to sort by 
country code)
+     plus the url (to ensure uniqueness) */
+  key = std::string();
+  std::string::size_type last_idx = displayed_url.length () - 1;
+  std::string::size_type idx = url.find_last_of("./", last_idx);
+  if (last_idx - idx == 3)
+  {
+    /* Sort non-country TLDs (.com, .net, ...) together. */
+    key += " ";
+  }
+  do
+  {
+    key += url.substr(idx + 1, last_idx - idx);
+    key += " ";
+    last_idx = idx - 1;
+    idx = url.find_last_of("./", last_idx);
+    if (idx == std::string::npos)
+      idx = 0;
+  } while (idx > 0);
+  key += url;
+}
+
+bool
+site_list_type::operator == (site_list_type const &rhs) const
+{
+  return stricmp (key.c_str(), rhs.key.c_str()) == 0;
+}
+
+bool
+site_list_type::operator < (site_list_type const &rhs) const
+{
+  return stricmp (key.c_str(), rhs.key.c_str()) < 0;
+}
+
+/*
+  A SiteList is maintained as an in-order std::vector of site_list_type, by
+  replacing it with a new object with the new item inserted in the correct
+  place.
+
+  Yes, we could just use an ordered container, instead.
+*/
+void
+site_list_insert(SiteList &site_list, site_list_type newsite)
+{
+  SiteList::iterator i = find (site_list.begin(), site_list.end(), newsite);
+  if (i == site_list.end())
+    {
+      SiteList result;
+      merge (site_list.begin(), site_list.end(),
+             &newsite, &newsite + 1,
+             inserter (result, result.begin()));
+      site_list = result;
+    }
+  else
+    *i = newsite;
+}
diff --git a/site.h b/SiteSetting.h
similarity index 74%
rename from site.h
rename to SiteSetting.h
index 6ec7cf3..0582a2e 100644
--- a/site.h
+++ b/SiteSetting.h
@@ -13,44 +13,30 @@
  *
  */
 
-#ifndef SETUP_SITE_H
-#define SETUP_SITE_H
+#ifndef SETUP_SITESETTING_H
+#define SETUP_SITESETTING_H
 
-#include <string>
 #include <vector>
 
-#include "proppage.h"
-
-class SitePage : public PropertyPage
+class SiteSetting
 {
-public:
-  SitePage ();
-  virtual ~ SitePage ()
-  {
-  };
-
-  bool Create ();
-
-  virtual void OnInit ();
-  virtual void OnActivate ();
-  virtual long OnNext ();
-  virtual long OnBack ();
-  virtual long OnUnattended ();
-
-  virtual bool OnMessageCmd (int id, HWND hwndctl, UINT code);
-
-  void PopulateListBox();
-  void CheckControlsAndDisableAccordingly () const;
+  public:
+    SiteSetting ();
+    void save ();
+    ~SiteSetting ();
+  private:
+    bool saved;
+    void getSavedSites();
+    void registerSavedSite(char const *);
+    const char *lastMirrorKey();
 };
 
-void do_download_site_info (HINSTANCE h, HWND owner);
-
 class site_list_type
 {
 public:
   site_list_type () : url (), displayed_url (), key () {};
   site_list_type (const std::string& , const std::string& ,
-                  const std::string& , const std::string&, bool, bool);
+                  const std::string& , const std::string&, bool, bool = false);
   ~site_list_type () {};
   std::string url;
   // provided by mirrors.lst but not used
@@ -74,22 +60,11 @@ public:
 
 typedef std::vector <site_list_type> SiteList;
 
+void site_list_insert(SiteList &site_list, site_list_type newsite);
+
 /* user chosen sites */
 extern SiteList site_list;
 /* potential sites */
 extern SiteList all_site_list;
 
-class SiteSetting
-{
-  public:
-    SiteSetting ();
-    void save ();
-    ~SiteSetting ();
-  private:
-    bool saved;
-    void getSavedSites();
-    void registerSavedSite(char const *);
-    const char *lastMirrorKey();
-};
-
-#endif /* SETUP_SITE_H */
+#endif /* SETUP_SITESETTING_H */
diff --git a/site.cc b/gui/SitePage.cc
similarity index 77%
rename from site.cc
rename to gui/SitePage.cc
index 569235a..69bac1d 100644
--- a/site.cc
+++ b/gui/SitePage.cc
@@ -19,7 +19,6 @@
 #include <string>
 #include <algorithm>
 
-#include "site.h"
 #include "win32.h"
 #include <stdio.h>
 #include <stdlib.h>
@@ -32,7 +31,7 @@
 #include "msg.h"
 #include "LogSingleton.h"
 #include "io_stream.h"
-#include "site.h"
+#include "gui/SitePage.h"
 
 #include "propsheet.h"
 
@@ -71,19 +70,14 @@ SitePage::SitePage ()
   sizeProcessor.AddControlInfo (SiteControlsInfo);
 }
 
-#include "getopt++/StringArrayOption.h"
 #include "getopt++/BoolOption.h"
 #include "UserSettings.h"
+#include "SiteSetting.h"
 
 bool cache_is_usable;
 bool cache_needs_writing;
 std::string cache_warn_urls;
 
-/* Selected sites */
-SiteList site_list;
-
-/* Fresh mirrors + selected sites */
-SiteList all_site_list;
 
 /* Previously fresh + cached before */
 SiteList cached_site_list;
@@ -91,131 +85,9 @@ SiteList cached_site_list;
 /* Stale selected sites to warn about and add to cache */
 SiteList dropped_site_list;
 
-StringArrayOption SiteOption('s', "site", IDS_HELPTEXT_SITE);
 BoolOption OnlySiteOption(false, 'O', "only-site", IDS_HELPTEXT_ONLY_SITE);
 extern BoolOption UnsupportedOption;
 
-SiteSetting::SiteSetting (): saved (false)
-{
-  std::vector<std::string> SiteOptionStrings = SiteOption;
-  if (SiteOptionStrings.size())
-    {
-      for (std::vector<std::string>::const_iterator n = 
SiteOptionStrings.begin ();
-          n != SiteOptionStrings.end (); ++n)
-       registerSavedSite (n->c_str ());
-    }
-  else
-    getSavedSites ();
-}
-
-const char *
-SiteSetting::lastMirrorKey ()
-{
-  if (UnsupportedOption)
-    return "last-mirror-unsupported";
-
-  return "last-mirror";
-}
-
-void 
-SiteSetting::save()
-{
-  io_stream *f = UserSettings::instance().open (lastMirrorKey ());
-  if (f)
-    {
-      for (SiteList::const_iterator n = site_list.begin ();
-          n != site_list.end (); ++n)
-       *f << n->url;
-      delete f;
-    }
-  saved = true;
-}
-
-SiteSetting::~SiteSetting ()
-{
-  if (!saved)
-    save ();
-}
-
-site_list_type::site_list_type (const std::string &_url,
-                               const std::string &_servername,
-                               const std::string &_area,
-                               const std::string &_location,
-                               bool _from_mirrors_lst,
-                                bool _noshow = false)
-{
-  url = _url;
-  servername = _servername;
-  area = _area;
-  location = _location;
-  from_mirrors_lst = _from_mirrors_lst;
-  noshow = _noshow;
-
-  /* Canonicalize URL to ensure it ends with a '/' */
-  if (url.at(url.length()-1) != '/')
-    url.append("/");
-
-  /* displayed_url is protocol and site name part of url */
-  std::string::size_type path_offset = url.find ("/", url.find ("//") + 2);
-  displayed_url = url.substr(0, path_offset);
-
-  /* the sorting key is hostname components in reverse order (to sort by 
country code)
-     plus the url (to ensure uniqueness) */
-  key = std::string();
-  std::string::size_type last_idx = displayed_url.length () - 1;
-  std::string::size_type idx = url.find_last_of("./", last_idx);
-  if (last_idx - idx == 3)
-  {
-    /* Sort non-country TLDs (.com, .net, ...) together. */
-    key += " ";
-  }
-  do
-  {
-    key += url.substr(idx + 1, last_idx - idx);
-    key += " ";
-    last_idx = idx - 1;
-    idx = url.find_last_of("./", last_idx);
-    if (idx == std::string::npos)
-      idx = 0;
-  } while (idx > 0);
-  key += url;
-}
-
-bool
-site_list_type::operator == (site_list_type const &rhs) const
-{
-  return stricmp (key.c_str(), rhs.key.c_str()) == 0; 
-}
-
-bool
-site_list_type::operator < (site_list_type const &rhs) const
-{
-  return stricmp (key.c_str(), rhs.key.c_str()) < 0; 
-}
-
-/*
-  A SiteList is maintained as an in-order std::vector of site_list_type, by
-  replacing it with a new object with the new item inserted in the correct
-  place.
-
-  Yes, we could just use an ordered container, instead.
-*/
-static void
-site_list_insert(SiteList &site_list, site_list_type newsite)
-{
-  SiteList::iterator i = find (site_list.begin(), site_list.end(), newsite);
-  if (i == site_list.end())
-    {
-      SiteList result;
-      merge (site_list.begin(), site_list.end(),
-             &newsite, &newsite + 1,
-             inserter (result, result.begin()));
-      site_list = result;
-    }
-  else
-    *i = newsite;
-}
-
 static void
 save_dialog (HWND h)
 {
@@ -381,43 +253,6 @@ get_site_list (HINSTANCE h, HWND owner)
   return 0;
 }
 
-/* List of machines that should not be used by default when saved
-   in "last-mirror". */
-#define NOSAVE1 "ftp://sourceware.org/";
-#define NOSAVE1_LEN (sizeof (NOSAVE2) - 1)
-#define NOSAVE2 "ftp://sources.redhat.com/";
-#define NOSAVE2_LEN (sizeof (NOSAVE1) - 1)
-#define NOSAVE3 "ftp://gcc.gnu.org/";
-#define NOSAVE3_LEN (sizeof (NOSAVE3) - 1)
-
-void
-SiteSetting::registerSavedSite (const char * site)
-{
-  site_list_type tempSite(site, "", "", "", false);
-
-  /* Don't default to certain machines if they suffer from bandwidth
-     limitations. */
-  if (strnicmp (site, NOSAVE1, NOSAVE1_LEN) == 0
-      || strnicmp (site, NOSAVE2, NOSAVE2_LEN) == 0
-      || strnicmp (site, NOSAVE3, NOSAVE3_LEN) == 0)
-    return;
-
-  site_list_insert (all_site_list, tempSite);
-  site_list.push_back (tempSite);
-}
-
-void
-SiteSetting::getSavedSites ()
-{
-  const char *buf = UserSettings::instance().get (lastMirrorKey ());
-  if (!buf)
-    return;
-  char *fg_ret = strdup (buf);
-  for (char *site = strtok (fg_ret, "\n"); site; site = strtok (NULL, "\n"))
-    registerSavedSite (site);
-  free (fg_ret);
-}
-
 static DWORD WINAPI
 do_download_site_info_thread (void *p)
 {
diff --git a/gui/SitePage.h b/gui/SitePage.h
new file mode 100644
index 0000000..1208338
--- /dev/null
+++ b/gui/SitePage.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2001, Robert Collins.
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ * Written by Robert Collins <rbtcoll...@hotmail.com>
+ *
+ */
+
+#ifndef SETUP_SITE_H
+#define SETUP_SITE_H
+
+#include "proppage.h"
+
+class SitePage : public PropertyPage
+{
+public:
+  SitePage ();
+  virtual ~ SitePage ()
+  {
+  };
+
+  bool Create ();
+
+  virtual void OnInit ();
+  virtual void OnActivate ();
+  virtual long OnNext ();
+  virtual long OnBack ();
+  virtual long OnUnattended ();
+
+  virtual bool OnMessageCmd (int id, HWND hwndctl, UINT code);
+
+  void PopulateListBox();
+  void CheckControlsAndDisableAccordingly () const;
+};
+
+void do_download_site_info (HINSTANCE h, HWND owner);
+
+#endif /* SETUP_SITE_H */
diff --git a/ini.cc b/ini.cc
index 95c9964..3d3cbde 100644
--- a/ini.cc
+++ b/ini.cc
@@ -36,7 +36,7 @@
 #include "geturl.h"
 #include "dialog.h"
 #include "mount.h"
-#include "site.h"
+#include "SiteSetting.h"
 #include "find.h"
 #include "IniParseFeedback.h"
 
diff --git a/main.cc b/main.cc
index cf9e323..2ce3b30 100644
--- a/main.cc
+++ b/main.cc
@@ -53,7 +53,7 @@
 #include "root.h"
 #include "localdir.h"
 #include "net.h"
-#include "site.h"
+#include "gui/SitePage.h"
 #include "choose.h"
 #include "prereq.h"
 #include "confirm.h"
@@ -74,6 +74,7 @@
 #include "SourceSetting.h"
 #include "ConnectionSetting.h"
 #include "KeysSetting.h"
+#include "SiteSetting.h"
 
 #include <wincon.h>
 #include <fstream>
diff --git a/threebar.cc b/threebar.cc
index bc356c0..16430bb 100644
--- a/threebar.cc
+++ b/threebar.cc
@@ -23,7 +23,7 @@
 #include "resource.h"
 
 #include "dialog.h"
-#include "site.h"
+#include "gui/SitePage.h"
 
 #include "propsheet.h"
 #include "threebar.h"
-- 
2.43.0

Reply via email to