[PATCH 2/7] Use ID columns in the protocol combo boxes

David Shea dshea at redhat.com
Tue Apr 15 18:22:27 UTC 2014


This way we just need to share some constant strings instead of having
everything depend on the order of items.

Obviously this makes it harder to remove the 'Closest mirror' option,
but it makes the other uses of the combo boxes less fragile.
---
 pyanaconda/ui/gui/spokes/source.glade | 16 +++++-----
 pyanaconda/ui/gui/spokes/source.py    | 57 ++++++++++++++++++++---------------
 2 files changed, 41 insertions(+), 32 deletions(-)

diff --git a/pyanaconda/ui/gui/spokes/source.glade b/pyanaconda/ui/gui/spokes/source.glade
index 85300b7..01814aa 100644
--- a/pyanaconda/ui/gui/spokes/source.glade
+++ b/pyanaconda/ui/gui/spokes/source.glade
@@ -752,11 +752,11 @@
                             <property name="can_focus">False</property>
                             <property name="active">0</property>
                             <items>
-                              <item>http://</item>
-                              <item>https://</item>
-                              <item>ftp://</item>
-                              <item translatable="yes">nfs</item>
-                              <item translatable="yes">Closest mirror</item>
+                              <item id="http">http://</item>
+                              <item id="https">https://</item>
+                              <item id="ftp">ftp://</item>
+                              <item id="nfs" translatable="yes">nfs</item>
+                              <item id="Closest mirror" translatable="yes">Closest mirror</item>
                             </items>
                             <signal name="changed" handler="on_protocol_changed" swapped="no"/>
                           </object>
@@ -1215,9 +1215,9 @@
                             <property name="tooltip_text" translatable="yes">Protocol for the repository URL.</property>
                             <property name="active">0</property>
                             <items>
-                              <item>http://</item>
-                              <item>https://</item>
-                              <item>ftp://</item>
+                              <item id="http">http://</item>
+                              <item id="https">https://</item>
+                              <item id="ftp">ftp://</item>
                             </items>
                             <signal name="changed" handler="on_repoUrl_changed" swapped="no"/>
                           </object>
diff --git a/pyanaconda/ui/gui/spokes/source.py b/pyanaconda/ui/gui/spokes/source.py
index f905450..12c2143 100644
--- a/pyanaconda/ui/gui/spokes/source.py
+++ b/pyanaconda/ui/gui/spokes/source.py
@@ -52,19 +52,22 @@ BASEREPO_SETUP_MESSAGE = N_("Setting up installation source...")
 METADATA_DOWNLOAD_MESSAGE = N_("Downloading package metadata...")
 METADATA_ERROR_MESSAGE = N_("Error downloading package metadata...")
 
-# These need to be in the same order as the items in protocolComboBox in source.glade.
-PROTOCOL_HTTP = 0
-PROTOCOL_HTTPS = 1
-PROTOCOL_FTP = 2
-PROTOCOL_NFS = 3
-PROTOCOL_MIRROR = 4
+# These need to match the IDs in protocolComboBox and repoProtocolComboBox in source.glade.
+PROTOCOL_HTTP = 'http'
+PROTOCOL_HTTPS = 'https'
+PROTOCOL_FTP = 'ftp'
+PROTOCOL_NFS = 'nfs'
+PROTOCOL_MIRROR = 'Closest mirror'
 
 # Repo Store Columns
 REPO_ENABLED_COL = 0
 REPO_NAME_COL = 1
 REPO_OBJ = 2
 
-REPO_PROTO = [(0, "http://"), (1, "https://"), (2, "ftp://")]
+REPO_PROTO = {PROTOCOL_HTTP:  "http://",
+              PROTOCOL_HTTPS: "https://",
+              PROTOCOL_FTP:   "ftp://"
+              }
 
 class ProxyDialog(GUIObject, GUIDialogInputCheckHandler):
     builderObjects = ["proxyDialog"]
@@ -393,10 +396,10 @@ class SourceSpoke(NormalSpoke):
             # revisited.
             if self._ftp_active() and not url.startswith("ftp://"):
                 url = "ftp://" + url
-            elif self._protocolComboBox.get_active() == PROTOCOL_HTTP and not url.startswith("http://"):
+            elif self._protocolComboBox.get_active_id() == PROTOCOL_HTTP and not url.startswith("http://"):
                 url = "http://" + url
                 mirrorlist = self._mirrorlistCheckbox.get_active()
-            elif self._protocolComboBox.get_active() == PROTOCOL_HTTPS and not url.startswith("https://"):
+            elif self._protocolComboBox.get_active_id() == PROTOCOL_HTTPS and not url.startswith("https://"):
                 url = "https://" + url
                 mirrorlist = self._mirrorlistCheckbox.get_active()
 
@@ -637,7 +640,13 @@ class SourceSpoke(NormalSpoke):
         # If there's no fallback mirror to use, we should just disable that option
         # in the UI.
         if not self.payload.mirrorEnabled:
-            self._protocolComboBox.remove(PROTOCOL_MIRROR)
+            model = self._protocolComboBox.get_model()
+            itr = model.get_iter_first()
+            while itr and model[itr][self._protocolComboBox.get_id_column()] != PROTOCOL_MIRROR:
+                itr = model.iter_next(itr)
+
+            if itr:
+                model.remove(itr)
 
         # If we've previously set up to use a CD/DVD method, the media has
         # already been mounted by payload.setup.  We can't try to mount it
@@ -721,7 +730,7 @@ class SourceSpoke(NormalSpoke):
 
         # We default to the mirror list, and then if the method tells us
         # something different later, we can change it.
-        self._protocolComboBox.set_active(PROTOCOL_MIRROR)
+        self._protocolComboBox.set_active_id(PROTOCOL_MIRROR)
         self._urlEntry.set_sensitive(False)
 
         # Set up the default state of UI elements.
@@ -730,16 +739,16 @@ class SourceSpoke(NormalSpoke):
 
             proto = self.data.method.url or self.data.method.mirrorlist
             if proto.startswith("http:"):
-                self._protocolComboBox.set_active(PROTOCOL_HTTP)
+                self._protocolComboBox.set_active_id(PROTOCOL_HTTP)
                 l = 7
             elif proto.startswith("https:"):
-                self._protocolComboBox.set_active(PROTOCOL_HTTPS)
+                self._protocolComboBox.set_active_id(PROTOCOL_HTTPS)
                 l = 8
             elif proto.startswith("ftp:"):
-                self._protocolComboBox.set_active(PROTOCOL_FTP)
+                self._protocolComboBox.set_active_id(PROTOCOL_FTP)
                 l = 6
             else:
-                self._protocolComboBox.set_active(PROTOCOL_HTTP)
+                self._protocolComboBox.set_active_id(PROTOCOL_HTTP)
                 l = 0
 
             self._urlEntry.set_sensitive(True)
@@ -748,7 +757,7 @@ class SourceSpoke(NormalSpoke):
             self._proxyUrl = self.data.method.proxy
         elif self.data.method.method == "nfs":
             self._networkButton.set_active(True)
-            self._protocolComboBox.set_active(PROTOCOL_NFS)
+            self._protocolComboBox.set_active_id(PROTOCOL_NFS)
 
             self._urlEntry.set_text("%s:%s" % (self.data.method.server, self.data.method.dir))
             self._urlEntry.set_sensitive(True)
@@ -801,16 +810,16 @@ class SourceSpoke(NormalSpoke):
         return not flags.livecdInstall and not self.data.method.method == "liveimg"
 
     def _mirror_active(self):
-        return self._protocolComboBox.get_active() == PROTOCOL_MIRROR
+        return self._protocolComboBox.get_active_id() == PROTOCOL_MIRROR
 
     def _http_active(self):
-        return self._protocolComboBox.get_active() in [PROTOCOL_HTTP, PROTOCOL_HTTPS]
+        return self._protocolComboBox.get_active_id() in [PROTOCOL_HTTP, PROTOCOL_HTTPS]
 
     def _ftp_active(self):
-        return self._protocolComboBox.get_active() == PROTOCOL_FTP
+        return self._protocolComboBox.get_active_id() == PROTOCOL_FTP
 
     def _nfs_active(self):
-        return self._protocolComboBox.get_active() == PROTOCOL_NFS
+        return self._protocolComboBox.get_active_id() == PROTOCOL_NFS
 
     def _get_selected_partition(self):
         store = self.builder.get_object("partitionStore")
@@ -1030,9 +1039,9 @@ class SourceSpoke(NormalSpoke):
         self._repoMirrorlistCheckbox.handler_unblock_by_func(self.on_repoMirrorlistCheckbox_toggled)
 
         if url:
-            for idx, proto in REPO_PROTO:
+            for idx, proto in REPO_PROTO.iteritems():
                 if url.startswith(proto):
-                    self._repoProtocolComboBox.set_active(idx)
+                    self._repoProtocolComboBox.set_active_id(idx)
                     self._repoUrlEntry.set_text(url[len(proto):])
                     break
             else:
@@ -1191,8 +1200,8 @@ class SourceSpoke(NormalSpoke):
         if not itr:
             return
         repo = self._repoStore[itr][REPO_OBJ]
-        idx = self._repoProtocolComboBox.get_active()
-        proto = REPO_PROTO[idx][1]
+        idx = self._repoProtocolComboBox.get_active_id()
+        proto = REPO_PROTO[idx]
         url = self._repoUrlEntry.get_text().strip()
         if self._repoMirrorlistCheckbox.get_active():
             repo.mirorlist = proto + url
-- 
1.9.0



More information about the anaconda-patches mailing list