[Dextrose] [PATCH] sl#3325, Make the Modem-Configuration "Country, Provider, Plan" parameters persistent.

Ajay Garg ajay at activitycentral.com
Tue Feb 14 10:23:22 EST 2012


This patch stores the values of these 3 parameters in GConf (similar to other parameters).

 extensions/cpsection/modemconfiguration/model.py |   58 ++++++++++++++-
 extensions/cpsection/modemconfiguration/view.py  |   87 +++++++++++++++++-----
 src/jarabe/model/network.py                      |    3 +
 3 files changed, 127 insertions(+), 21 deletions(-)

diff --git a/extensions/cpsection/modemconfiguration/model.py b/extensions/cpsection/modemconfiguration/model.py
index ca9f75c..e0a792d 100644
--- a/extensions/cpsection/modemconfiguration/model.py
+++ b/extensions/cpsection/modemconfiguration/model.py
@@ -23,7 +23,8 @@ import logging
 from xml.etree.cElementTree import ElementTree
 from gettext import gettext as _
 
-from jarabe.model.network import GSM_USERNAME_PATH, GSM_PASSWORD_PATH, \
+from jarabe.model.network import GSM_COUNTRY_PATH, GSM_PROVIDERS_PATH, GSM_PLAN_PATH, \
+                                 GSM_USERNAME_PATH, GSM_PASSWORD_PATH, \
                                  GSM_NUMBER_PATH, GSM_APN_PATH, GSM_PIN_PATH, \
                                  GSM_PUK_PATH
 
@@ -32,6 +33,18 @@ from cpsection.modemconfiguration.config import PROVIDERS_PATH, \
                                                 COUNTRY_CODES_PATH
 
 
+def get_country():
+    client = gconf.client_get_default()
+    return client.get_string(GSM_COUNTRY_PATH) or ''
+
+def get_provider():
+    client = gconf.client_get_default()
+    return client.get_string(GSM_PROVIDERS_PATH) or ''
+
+def get_plan():
+    client = gconf.client_get_default()
+    return client.get_string(GSM_PLAN_PATH) or ''
+
 def get_username():
     client = gconf.client_get_default()
     return client.get_string(GSM_USERNAME_PATH) or ''
@@ -62,6 +75,21 @@ def get_puk():
     return client.get_string(GSM_PUK_PATH) or ''
 
 
+def set_country(country):
+    client = gconf.client_get_default()
+    client.set_string(GSM_COUNTRY_PATH, country)
+
+
+def set_provider(provider):
+    client = gconf.client_get_default()
+    client.set_string(GSM_PROVIDERS_PATH, provider)
+
+
+def set_plan(plan):
+    client = gconf.client_get_default()
+    client.set_string(GSM_PLAN_PATH, plan)
+
+
 def set_username(username):
     client = gconf.client_get_default()
     client.set_string(GSM_USERNAME_PATH, username)
@@ -146,7 +174,13 @@ class CountryListStore(gtk.ListStore):
         if self._country_idx is not None:
             return self._country_idx
         else:
-            return -1
+            return 0
+
+    def search_index_by_code(self, code):
+        for index in range(0, len(self)):
+            if self[index][0] == code:
+                return index
+        return -1
 
 
 class ProviderListStore(gtk.ListStore):
@@ -162,6 +196,16 @@ class ProviderListStore(gtk.ListStore):
     def get_row_plans(self, row):
         return self[row][1]
 
+    def guess_providers_row(self):
+        # Simply return the first entry as the default.
+        return 0
+
+    def search_index_by_code(self, code):
+        for index in range(0, len(self)):
+            if self[index][0] == code:
+                return index
+        return -1
+
 
 class PlanListStore(gtk.ListStore):
     LANG_NS_ATTR = '{http://www.w3.org/XML/1998/namespace}lang'
@@ -202,3 +246,13 @@ class PlanListStore(gtk.ListStore):
 
     def get_row_plan(self, row):
         return self[row][1]
+
+    def guess_plan_row(self):
+        # Simply return the first entry as the default.
+        return 0
+
+    def search_index_by_code(self, code):
+        for index in range(0, len(self)):
+            if self[index][0] == code:
+                return index
+        return -1
diff --git a/extensions/cpsection/modemconfiguration/view.py b/extensions/cpsection/modemconfiguration/view.py
index a16de88..67d8544 100644
--- a/extensions/cpsection/modemconfiguration/view.py
+++ b/extensions/cpsection/modemconfiguration/view.py
@@ -204,16 +204,15 @@ class ModemConfiguration(SectionView):
             label_group.add_widget(label)
             box.pack_start(label, False)
             label.show()
-            country_store = model.CountryListStore()
-            country_combo = gtk.ComboBox(country_store)
-            combo_group.add_widget(country_combo)
+            self._country_store = model.CountryListStore()
+            self._country_combo = gtk.ComboBox(self._country_store)
+            combo_group.add_widget(self._country_combo)
             cell = gtk.CellRendererText()
             cell.props.xalign = 0.5
-            country_combo.pack_start(cell)
-            country_combo.add_attribute(cell, 'text', 0)
-            country_combo.connect('changed', self.__country_selected_cb)
-            box.pack_start(country_combo, False)
-            country_combo.show()
+            self._country_combo.pack_start(cell)
+            self._country_combo.add_attribute(cell, 'text', 0)
+            box.pack_start(self._country_combo, False)
+            self._country_combo.show()
             self._upper_box.pack_start(box, False)
             box.show()
 
@@ -229,8 +228,6 @@ class ModemConfiguration(SectionView):
             cell.props.xalign = 0.5
             self._providers_combo.pack_start(cell)
             self._providers_combo.add_attribute(cell, 'text', 0)
-            self._providers_combo.connect('changed',
-                                          self.__provider_selected_cb)
             box.pack_start(self._providers_combo, False)
             self._providers_combo.show()
             self._upper_box.pack_start(box, False)
@@ -248,14 +245,11 @@ class ModemConfiguration(SectionView):
             cell.props.xalign = 0.5
             self._plan_combo.pack_start(cell)
             self._plan_combo.add_attribute(cell, 'text', 0)
-            self._plan_combo.connect('changed', self.__plan_selected_cb)
             box.pack_start(self._plan_combo, False)
             self._plan_combo.show()
             self._upper_box.pack_start(box, False)
             box.show()
 
-            country_combo.set_active(country_store.guess_country_row())
-
             separator = gtk.HSeparator()
             main_box.pack_start(separator, False)
             separator.show()
@@ -315,6 +309,17 @@ class ModemConfiguration(SectionView):
         self.setup()
 
     def setup(self):
+        if self._model.has_providers_db():
+            persisted_country = self._model.get_country()
+            if (self._model.has_providers_db()) and (persisted_country != ''):
+                self._country_combo.set_active(self._country_store.search_index_by_code(persisted_country))
+            else:
+                self._country_combo.set_active(self._country_store.guess_country_row())
+
+            # Call the selected callback anyway, so as to chain-set the
+            # default values for providers and the plans.
+            self.__country_selected_cb(self._country_combo, setup=True)
+
         self._username_entry.set_text_from_model()
         self._password_entry.set_text_from_model()
         self._number_entry.set_text_from_model()
@@ -327,18 +332,62 @@ class ModemConfiguration(SectionView):
     def undo(self):
         self._model.undo()
 
-    def __country_selected_cb(self, combo):
+    def _get_selected_text(self, combo):
+        active_iter = combo.get_active_iter()
+        return combo.get_model().get(active_iter, 0)[0]
+
+    def __country_selected_cb(self, combo, setup=False):
+        country = self._get_selected_text(combo)
+        self._model.set_country(country)
+
         model = combo.get_model()
         providers = model.get_row_providers(combo.get_active())
-        self._providers_combo.set_model(
-            self._model.ProviderListStore(providers))
+        self._providers_liststore = self._model.ProviderListStore(providers)
+        self._providers_combo.set_model(self._providers_liststore)
+
+        # Set the default provider as well.
+        if setup:
+            persisted_provider = self._model.get_provider()
+            if persisted_provider == '':
+                self._providers_combo.set_active(self._providers_liststore.guess_providers_row())
+            else:
+                self._providers_combo.set_active(self._providers_liststore.search_index_by_code(persisted_provider))
+        else:
+            self._providers_combo.set_active(self._providers_liststore.guess_providers_row())
+
+        # Call the callback, so that default provider may be set.
+        self.__provider_selected_cb(self._providers_combo, setup)
+
+        self._country_combo.connect('changed', self.__country_selected_cb, False)
+        self._providers_combo.connect('changed', self.__provider_selected_cb, False)
+        self._plan_combo.connect('changed', self.__plan_selected_cb, False)
+
+    def __provider_selected_cb(self, combo, setup=False):
+        provider = self._get_selected_text(combo)
+        self._model.set_provider(provider)
 
-    def __provider_selected_cb(self, combo):
         model = combo.get_model()
         plans = model.get_row_plans(combo.get_active())
-        self._plan_combo.set_model(self._model.PlanListStore(plans))
+        self._plan_liststore = self._model.PlanListStore(plans)
+        self._plan_combo.set_model(self._plan_liststore)
+
+        # Set the default plan as well.
+        if setup:
+            persisted_plan = self._model.get_plan()
+            if persisted_plan == '':
+                self._plan_combo.set_active(self._plan_liststore.guess_plan_row())
+            else:
+                self._plan_combo.set_active(self._plan_liststore.search_index_by_code(persisted_plan))
+        else:
+            self._plan_combo.set_active(self._plan_liststore.guess_plan_row())
+
+        # Call the callback, so that the default plan is set.
+        self.__plan_selected_cb(self._plan_combo, setup)
+
+    def __plan_selected_cb(self, combo, setup):
+        plan = self._get_selected_text(combo)
+        self._model.set_plan(plan)
 
-    def __plan_selected_cb(self, combo):
         model = combo.get_model()
         plan = model.get_row_plan(combo.get_active())
         self._username_entry.entry.set_text(plan['username'])
diff --git a/src/jarabe/model/network.py b/src/jarabe/model/network.py
index 84e3666..8e60e0d 100644
--- a/src/jarabe/model/network.py
+++ b/src/jarabe/model/network.py
@@ -141,6 +141,9 @@ NM_SECRETS_IFACE = 'org.freedesktop.NetworkManagerSettings.Connection.Secrets'
 NM_ACCESSPOINT_IFACE = 'org.freedesktop.NetworkManager.AccessPoint'
 NM_ACTIVE_CONN_IFACE = 'org.freedesktop.NetworkManager.Connection.Active'
 
+GSM_COUNTRY_PATH = '/desktop/sugar/network/gsm/country'
+GSM_PROVIDERS_PATH = '/desktop/sugar/network/gsm/providers'
+GSM_PLAN_PATH = '/desktop/sugar/network/gsm/plan'
 GSM_USERNAME_PATH = '/desktop/sugar/network/gsm/username'
 GSM_PASSWORD_PATH = '/desktop/sugar/network/gsm/password'
 GSM_NUMBER_PATH = '/desktop/sugar/network/gsm/number'
-- 
1.7.4.4



More information about the Dextrose mailing list