Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrés Ambrois <andresambrois@gmail.com>2010-06-14 02:55:22 (GMT)
committer Andrés Ambrois <andresambrois@gmail.com>2010-08-30 19:13:15 (GMT)
commit668884388062b2e5586897f655c97b009fec6d04 (patch)
tree8392d2286dfaf9694fdf8a7ad8570beaefbac82e
parentd0675f45139fe17eb666c938ae0882d799b48174 (diff)
Add models for detecting and parsing the providers DB.
has_providers_db() checks for the files needed and is used by the view to decide whether to show the combo boxes. The models are gtk.ListStore subclasses that parse the XML element they receive as a parameter in their constructors.
-rw-r--r--[-rwxr-xr-x]extensions/cpsection/modemconfiguration/model.py137
1 files changed, 137 insertions, 0 deletions
diff --git a/extensions/cpsection/modemconfiguration/model.py b/extensions/cpsection/modemconfiguration/model.py
index 2545ce1..73b9c63 100755..100644
--- a/extensions/cpsection/modemconfiguration/model.py
+++ b/extensions/cpsection/modemconfiguration/model.py
@@ -14,12 +14,38 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 US
+from __future__ import with_statement
import gconf
+import gtk
+import os
+import locale
+import logging
+
+from xml.etree.cElementTree import ElementTree
+from gettext import gettext as _
from jarabe.model.network import GSM_USERNAME_PATH, GSM_PASSWORD_PATH, \
GSM_NUMBER_PATH, GSM_APN_PATH, GSM_PIN_PATH, \
GSM_PUK_PATH
+from cpsection.modemconfiguration.config import PROVIDERS_PATH, \
+ PROVIDERS_FORMAT_SUPPORTED, \
+ COUNTRY_CODES_PATH
+
+_country_code, _encoding = locale.getdefaultlocale()
+if _country_code is not None:
+ _tags = _country_code.lower().split('_')
+ LANG = _tags[0]
+ if len(_tags) > 1 and len(_tags[1]) == 2:
+ COUNTRY_CODE = _tags[1]
+ else:
+ COUNTRY_CODE = None
+else:
+ LANG = None
+ COUNTRY_CODE = None
+
+LANG_NS_ATTR = '{http://www.w3.org/XML/1998/namespace}lang'
+
def get_username():
client = gconf.client_get_default()
return client.get_string(GSM_USERNAME_PATH) or ''
@@ -68,3 +94,114 @@ def set_puk(puk):
client = gconf.client_get_default()
client.set_string(GSM_PUK_PATH, puk)
+def has_providers_db():
+ if not os.path.isfile(COUNTRY_CODES_PATH):
+ logging.debug("Mobile broadband provider database: Country " \
+ "codes path %s not found.", COUNTRY_CODES_PATH)
+ return False
+ try:
+ tree = ElementTree(file=PROVIDERS_PATH)
+ except (IOError, SyntaxError), e:
+ logging.debug("Mobile broadband provider database: Could not read " \
+ "provider information %s error=%s", PROVIDERS_PATH)
+ return False
+ else:
+ elem = tree.getroot()
+ if elem is None or elem.get('format') != PROVIDERS_FORMAT_SUPPORTED:
+ logging.debug("Mobile broadband provider database: Could not " \
+ "read provider information. %s is wrong format.",
+ elem.get('format'))
+ return False
+ return True
+
+
+class CountryListStore(gtk.ListStore):
+ def __init__(self):
+ gtk.ListStore.__init__(self, str, object)
+ codes = {}
+ with open(COUNTRY_CODES_PATH) as codes_file:
+ for line in codes_file:
+ if line.startswith('#'):
+ continue
+ code, name = line.split('\t')[:2]
+ codes[code.lower()] = name.strip()
+ etree = ElementTree(file=PROVIDERS_PATH).getroot()
+ self._country_idx = None
+ i = 0
+ for elem in etree.findall('.//country'):
+ code = elem.attrib['code']
+ if code == COUNTRY_CODE:
+ self._country_idx = i
+ else:
+ i += 1
+ if code in codes:
+ self.append((codes[code], elem))
+ else:
+ self.append((code, elem))
+
+ def get_row_providers(self, row):
+ return self[row][1]
+
+ def guess_country_row(self):
+ if self._country_idx is not None:
+ return self._country_idx
+ else:
+ return -1
+
+class ProviderListStore(gtk.ListStore):
+ def __init__(self, elem):
+ gtk.ListStore.__init__(self, str, object)
+ for provider_elem in elem.findall('.//provider'):
+ apns = provider_elem.findall('.//apn')
+ if not apns:
+ # Skip carriers with CDMA entries only
+ continue
+ names = provider_elem.findall('.//name')
+ for name in names:
+ if name.get(LANG_NS_ATTR) is None:
+ # serviceproviders.xml default value
+ provider_name = name.text
+ elif name.get(LANG_NS_ATTR) == LANG:
+ # Great! We found a name value for our locale!
+ provider_name = name.text
+ break
+ self.append((provider_name, apns))
+
+ def get_row_plans(self, row):
+ return self[row][1]
+
+class PlanListStore(gtk.ListStore):
+ DEFAULT_NUMBER = '*99#'
+
+ def __init__(self, elems):
+ gtk.ListStore.__init__(self, str, object)
+ for apn_elem in elems:
+ plan = {}
+ names = apn_elem.findall('.//name')
+ if names:
+ for name in names:
+ if name.get(LANG_NS_ATTR) is None:
+ plan['name'] = name.text
+ elif name.get(LANG_NS_ATTR) == LANG:
+ plan['name'] = name.text
+ break
+ else:
+ plan['name'] = _('Default')
+ plan['apn'] = apn_elem.get('value')
+ user = apn_elem.find('.//username')
+ if user is not None:
+ plan['username'] = user.text
+ else:
+ plan['username'] = ''
+ passwd = apn_elem.find('.//password')
+ if passwd is not None:
+ plan['password'] = passwd.text
+ else:
+ plan['password'] = ''
+
+ plan['number'] = self.DEFAULT_NUMBER
+
+ self.append((plan['name'], plan))
+
+ def get_row_plan(self, row):
+ return self[row][1]