Ticket #1630: 0001-Add-models-for-detecting-and-parsing-the-providers-D.patch

File 0001-Add-models-for-detecting-and-parsing-the-providers-D.patch, 5.5 KB (added by aa, 9 years ago)
  • extensions/cpsection/modemconfiguration/model.py

    From 90106170b4d8bf0a6dbe3c25e75114b754add56e Mon Sep 17 00:00:00 2001
    Message-Id: <90106170b4d8bf0a6dbe3c25e75114b754add56e.1282025114.git.andresambrois@gmail.com>
    From: =?UTF-8?q?Andr=C3=A9s=20Ambrois?= <andresambrois@gmail.com>
    Date: Sun, 13 Jun 2010 23:55:22 -0300
    Subject: [PATCH 1/3] Add models for detecting and parsing the providers DB.
    MIME-Version: 1.0
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 8bit
    
    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.
    
    Signed-off-by: Andrés Ambrois <andresambrois@gmail.com>
    ---
     extensions/cpsection/modemconfiguration/model.py |  109 ++++++++++++++++++++++
     1 files changed, 109 insertions(+), 0 deletions(-)
     mode change 100755 => 100644 extensions/cpsection/modemconfiguration/model.py
    
    diff --git a/extensions/cpsection/modemconfiguration/model.py b/extensions/cpsection/modemconfiguration/model.py
    old mode 100755
    new mode 100644
    index 2545ce1..42f7563
    a b  
    1515# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
    1616
    1717import gconf
     18import gtk
     19import os
     20import locale
     21from xml.etree.cElementTree import ElementTree
     22from gettext import gettext as _
    1823
    1924from jarabe.model.network import GSM_USERNAME_PATH, GSM_PASSWORD_PATH, \
    2025                                 GSM_NUMBER_PATH, GSM_APN_PATH, GSM_PIN_PATH, \
    2126                                 GSM_PUK_PATH
    2227
     28from cpsection.modemconfiguration.config import PROVIDERS_PATH, \
     29                                                PROVIDERS_FORMAT_SUPPORTED, \
     30                                                COUNTRY_CODES_PATH
     31
    2332def get_username():
    2433    client = gconf.client_get_default()
    2534    return client.get_string(GSM_USERNAME_PATH) or ''
    def set_puk(puk): 
    6877    client = gconf.client_get_default()
    6978    client.set_string(GSM_PUK_PATH, puk)
    7079
     80def has_providers_db():
     81    if not os.path.isfile(COUNTRY_CODES_PATH):
     82        return False
     83    try:
     84        tree = ElementTree(file=PROVIDERS_PATH)
     85        elem = tree.getroot()
     86        if elem is None or elem.get('format') != PROVIDERS_FORMAT_SUPPORTED:
     87            return False
     88        return True
     89    except IOError:
     90        return False
     91
     92class CountryListStore(gtk.ListStore):
     93    COUNTRY_CODE = locale.getdefaultlocale()[0][3:5].lower()
     94
     95    def __init__(self):
     96        gtk.ListStore.__init__(self, str, object)
     97        codes = {}
     98        with open(COUNTRY_CODES_PATH) as codes_file:
     99            for line in codes_file:
     100                if line.startswith('#'):
     101                    continue
     102                code, name = line.split('\t')[:2]
     103                codes[code.lower()] = name.strip()
     104        etree = ElementTree(file=PROVIDERS_PATH).getroot()
     105        self._country_idx = None
     106        i = 0
     107        for elem in etree.findall('.//country'):
     108            code = elem.attrib['code']
     109            if code == self.COUNTRY_CODE:
     110                self._country_idx = i
     111            else:
     112                i += 1
     113            if code in codes:
     114                self.append((codes[code], elem))
     115            else:
     116                self.append((code, elem))
     117
     118    def get_row_providers(self, row):
     119        return self[row][1]
     120
     121    def guess_country_row(self):
     122        if self._country_idx is not None:
     123            return self._country_idx
     124        else:
     125            return -1
     126
     127class ProviderListStore(gtk.ListStore):
     128    def __init__(self, elem):
     129        gtk.ListStore.__init__(self, str, object)
     130        for provider_elem in elem.findall('.//provider'):
     131            apns = provider_elem.findall('.//apn')
     132            if not apns:
     133                # Skip carriers with CDMA entries only
     134                continue
     135            self.append((provider_elem.find('.//name').text, apns))
     136
     137    def get_row_plans(self, row):
     138        return self[row][1]
     139
     140class PlanListStore(gtk.ListStore):
     141    LANG_NS_ATTR = '{http://www.w3.org/XML/1998/namespace}lang'
     142    LANG = locale.getdefaultlocale()[0][:2]
     143    DEFAULT_NUMBER = '*99#'
     144
     145    def __init__(self, elems):
     146        gtk.ListStore.__init__(self, str, object)
     147        for apn_elem in elems:
     148            plan = {}
     149            names = apn_elem.findall('.//name')
     150            if names:
     151                for name in names:
     152                    if name.get(self.LANG_NS_ATTR) is None:
     153                        # serviceproviders.xml default value
     154                        plan['name'] = name.text
     155                    elif name.get(self.LANG_NS_ATTR) == self.LANG:
     156                        # Great! We found a name value for our locale!
     157                        plan['name'] = name.text
     158                        break
     159            else:
     160                plan['name'] = _('Default')
     161            plan['apn'] = apn_elem.get('value')
     162            user = apn_elem.find('.//username')
     163            if user is not None:
     164                plan['username'] = user.text
     165            else:
     166                plan['username'] = ''
     167            passwd = apn_elem.find('.//password')
     168            if passwd is not None:
     169                plan['password'] = passwd.text
     170            else:
     171                plan['password'] = ''
     172
     173            plan['number'] = self.DEFAULT_NUMBER
     174
     175            self.append((plan['name'], plan))
     176
     177    def get_row_plan(self, row):
     178        return self[row][1]
     179