| 80 | def 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 | |
| 92 | class 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 | |
| 127 | class 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 | |
| 140 | class 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 | |