Ticket #1630: gsm-provider-db.patch
File gsm-provider-db.patch, 7.0 KB (added by aa, 14 years ago) |
---|
-
extensions/cpsection/modemconfiguration/model.py
old new 15 15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 US 16 16 17 17 from jarabe.model.network import StorageGsm 18 import gtk 19 import os 20 from xml.etree.cElementTree import ElementTree 21 from gettext import gettext as _ 22 23 PROVIDERS_PATH = '/usr/share/mobile-broadband-provider-info/serviceproviders.xml' 24 PROVIDERS_FORMAT_SUPPORTED = '2.0' 25 COUNTRY_CODES_PATH = '/usr/share/zoneinfo/iso3166.tab' 18 26 19 27 storage = StorageGsm() 20 28 … … 41 49 42 50 def set_apn(apn): 43 51 storage.set_apn(apn) 52 53 def has_providers_db(): 54 if not os.path.isfile(COUNTRY_CODES_PATH): 55 return False 56 try: 57 et = ElementTree(file=PROVIDERS_PATH) 58 elem = et.getroot() 59 if elem is None or elem.get('format') != PROVIDERS_FORMAT_SUPPORTED: 60 return False 61 return True 62 except IOError: 63 return False 64 65 class CountryListStore(gtk.ListStore): 66 COUNTRY_CODE = os.environ['LANG'][3:5].lower() 67 68 def __init__(self): 69 gtk.ListStore.__init__(self, str, object) 70 codes = {} 71 with open(COUNTRY_CODES_PATH) as f: 72 for line in f: 73 if line.startswith('#'): 74 continue 75 code, name = line.split('\t')[:2] 76 codes[code.lower()] = name.strip() 77 etree = ElementTree(file=PROVIDERS_PATH).getroot() 78 self._country_idx = None 79 i = 0 80 for elem in etree.findall('.//country'): 81 code = elem.attrib['code'] 82 if code == self.COUNTRY_CODE: 83 self._country_idx = i 84 else: 85 i += 1 86 self.append((codes[code], elem)) 87 88 def get_row_providers(self, n): 89 return self[n][1] 90 91 def guess_country_row(self): 92 if self._country_idx is not None: 93 return self._country_idx 94 else: 95 return -1 96 97 class ProviderListStore(gtk.ListStore): 98 def __init__(self, elem): 99 gtk.ListStore.__init__(self, str, object) 100 for provider_elem in elem.findall('.//provider'): 101 apns = provider_elem.findall('.//apn') 102 if not apns: 103 # Skip carriers with CDMA entries only 104 continue 105 self.append((provider_elem.find('.//name').text, apns)) 106 107 def get_row_plans(self, n): 108 return self[n][1] 109 110 class PlanListStore(gtk.ListStore): 111 LANG_NS_ATTR = '{http://www.w3.org/XML/1998/namespace}lang' 112 LANG = os.environ['LANG'][:2] # FIXME: Is this the best way? 113 DEFAULT_NUMBER = '*99#' 114 115 def __init__(self, elems): 116 gtk.ListStore.__init__(self, str, object) 117 for apn_elem in elems: 118 plan = {} 119 names = apn_elem.findall('.//name') 120 if names: 121 for name in names: 122 if name.get(self.LANG_NS_ATTR) is None: 123 # serviceproviders.xml default value 124 plan['name'] = name.text 125 elif name.get(self.LANG_NS_ATTR) == self.LANG: 126 # Great! We found a name value for our locale! 127 plan['name'] = name.text 128 break 129 else: 130 plan['name'] = _('Default') 131 plan['apn'] = apn_elem.get('value') 132 user = apn_elem.find('.//username') 133 if user is not None: 134 plan['username'] = user.text 135 else: 136 plan['username'] = '' 137 passwd = apn_elem.find('.//password') 138 if passwd is not None: 139 plan['password'] = passwd.text 140 else: 141 plan['password'] = '' 142 143 plan['number'] = self.DEFAULT_NUMBER 144 145 self.append((plan['name'], plan)) 146 147 def get_row_plan(self, n): 148 return self[n][1] -
extensions/cpsection/modemconfiguration/view.py
old new 59 59 self._password_valid = True 60 60 self._number_valid = True 61 61 self._apn_valid = True 62 self._root = gtk.VBox() 63 self.pack_start(self._root) 64 65 if self._model.has_providers_db(): 66 self._upper_box = gtk.VBox(spacing=style.DEFAULT_SPACING) 67 self._root.pack_start(self._upper_box, fill=False) 68 self._root.pack_start(gtk.HSeparator(), False) 69 70 #Country 71 box = gtk.HBox(True) 72 box.pack_start(gtk.Label(_('Country')), False) 73 store = self._model.CountryListStore() 74 country_combo = gtk.ComboBox(store) 75 cell = gtk.CellRendererText() 76 cell.props.xalign = 0.5 77 country_combo.pack_start(cell) 78 country_combo.add_attribute(cell, 'text', 0) 79 country_combo.connect('changed', self._country_selected) 80 box.pack_start(country_combo, False) 81 self._upper_box.pack_start(box, False) 82 83 #Provider 84 box = gtk.HBox(True) 85 box.pack_start(gtk.Label(_('Provider')), False) 86 self._prov_combo = gtk.ComboBox() 87 cell = gtk.CellRendererText() 88 cell.props.xalign = 0.5 89 self._prov_combo.pack_start(cell) 90 self._prov_combo.add_attribute(cell, 'text', 0) 91 self._prov_combo.connect('changed', self._provider_selected) 92 box.pack_start(self._prov_combo, False) 93 self._upper_box.pack_start(box, False) 94 95 #Plan 96 box = gtk.HBox(True) 97 box.pack_start(gtk.Label(_('Plan')), False) 98 self._plan_combo = gtk.ComboBox() 99 cell = gtk.CellRendererText() 100 cell.props.xalign = 0.5 101 self._plan_combo.pack_start(cell) 102 self._plan_combo.add_attribute(cell, 'text', 0) 103 self._plan_combo.connect('changed', self._plan_selected) 104 box.pack_start(self._plan_combo, False) 105 self._upper_box.pack_start(box, False) 106 107 country_combo.set_active(store.guess_country_row()) 108 109 self._lower_box = gtk.VBox(spacing=style.DEFAULT_SPACING) 110 self._root.pack_start(self._lower_box, fill=False) 62 111 63 112 self.set_border_width(style.DEFAULT_SPACING) 64 113 self.set_spacing(style.DEFAULT_SPACING) … … 69 118 self._number_entry = self._setup_field('number') 70 119 self._apn_entry = self._setup_field('apn') 71 120 121 self._root.show_all() 72 122 self.setup() 73 123 124 def _country_selected(self, combo): 125 model = combo.get_model() 126 providers = model.get_row_providers(combo.get_active()) 127 self._prov_combo.set_model(self._model.ProviderListStore(providers)) 128 129 def _provider_selected(self, combo): 130 model = combo.get_model() 131 plans = model.get_row_plans(combo.get_active()) 132 self._plan_combo.set_model(self._model.PlanListStore(plans)) 133 134 def _plan_selected(self, combo): 135 model = combo.get_model() 136 plan = model.get_row_plan(combo.get_active()) 137 self._username_entry.set_text(plan['username']) 138 self._password_entry.set_text(plan['password']) 139 self._number_entry.set_text(plan['number']) 140 self._apn_entry.set_text(plan['apn']) 141 74 142 def _setup_field(self, attr_name): 75 143 box = gtk.HBox(spacing=style.DEFAULT_SPACING) 76 144 … … 80 148 81 149 entry = MyEntry(self._model, attr_name) 82 150 box.pack_start(entry, False) 83 box.show_all()84 151 85 self. pack_start(box, False)152 self._lower_box.pack_start(box, False) 86 153 return entry 87 154 88 155 def setup(self):